From 37e468cf1699653956684869fc1a7dd98be2ebbb Mon Sep 17 00:00:00 2001 From: Martin Kampas Date: Mon, 12 Feb 2024 13:24:54 +0100 Subject: [PATCH] org-bibtex-yank: Allow to populate existing item Align with `org-bibtex-create'. * lisp/ol-bibtex.el (org-bibtex-write): * lisp/ol-bibtex.el (org-bibtex-yank): New optional argument UPDATE-HEADING to update heading at point instead of creating a new heading. (org-bibtex-create): Rename NONEW argument to UPDATE-HEADING. * etc/ORG-NEWS (~org-bibtex-yank~ accepts a prefix argument): (New optional argument =UPDATE-HEADING= for ~org-bibtex-yank~): Announce the change. Link: https://list.orgmode.org/orgmode/4868155.GXAFRqVoOG@gt1/ --- etc/ORG-NEWS | 10 +++++++++ lisp/ol-bibtex.el | 53 +++++++++++++++++++++++++++-------------------- 2 files changed, 40 insertions(+), 23 deletions(-) diff --git a/etc/ORG-NEWS b/etc/ORG-NEWS index b79f275c4..db0c604f2 100644 --- a/etc/ORG-NEWS +++ b/etc/ORG-NEWS @@ -928,6 +928,11 @@ properties, links to headlines in the file can also be made more robust by using the file id instead of the file path. ** New features +*** ~org-bibtex-yank~ accepts a prefix argument + +When called with a prefix argument, ~org-bibtex-yank~ adds data to the +headline of the entry at point instead of creating a new one. + *** =ob-plantuml.el=: Support tikz file format output =ob-plantuml.el= now output =tikz= :file format via @@ -1127,6 +1132,11 @@ The same can be done via startup options: : #+STARTUP: fnanon ** New functions and changes in function arguments +*** New optional argument =UPDATE-HEADING= for ~org-bibtex-yank~ + +When the new argument is non-nil, add data to the headline of the +entry at point. + *** New API functions to store data within ~org-element-cache~ Elisp programs can now store data inside Org element cache. diff --git a/lisp/ol-bibtex.el b/lisp/ol-bibtex.el index 38468f32f..7f4449b48 100644 --- a/lisp/ol-bibtex.el +++ b/lisp/ol-bibtex.el @@ -640,22 +640,23 @@ With prefix argument OPTIONAL also prompt for optional fields." "Return headline text according to ENTRY title." (cdr (assq :title entry))) -(defun org-bibtex-create (&optional arg nonew) +(defun org-bibtex-create (&optional arg update-heading) "Create a new entry at the given level. -With a prefix arg, query for optional fields as well. -If nonew is t, add data to the headline of the entry at point." +With a prefix ARG, query for optional fields as well. +If UPDATE-HEADING is non-nil, add data to the headline of the entry at +point." (interactive "P") (let* ((type (completing-read "Type: " (mapcar (lambda (type) (substring (symbol-name (car type)) 1)) org-bibtex-types) - nil nil (when nonew - (org-bibtex-get org-bibtex-type-property-name)))) + nil nil (when update-heading + (org-bibtex-get org-bibtex-type-property-name)))) (type (if (keywordp type) type (intern (concat ":" type)))) - (org-bibtex-treat-headline-as-title (if nonew nil t))) + (org-bibtex-treat-headline-as-title (if update-heading nil t))) (unless (assoc type org-bibtex-types) (error "Type:%s is not known" type)) - (if nonew + (if update-heading (org-back-to-heading) (org-insert-heading) (let ((title (org-bibtex-ask :title))) @@ -722,29 +723,32 @@ Return the number of saved entries." (interactive "fFile: ") (org-bibtex-read-buffer (find-file-noselect file 'nowarn 'rawfile))) -(defun org-bibtex-write (&optional noindent) +(defun org-bibtex-write (&optional noindent update-heading) "Insert a heading built from the first element of `org-bibtex-entries'. When optional argument NOINDENT is non-nil, do not indent the properties -drawer." +drawer. If UPDATE-HEADING is non-nil, add data to the headline of the +entry at point." (interactive) (unless org-bibtex-entries (error "No entries in `org-bibtex-entries'")) (let* ((entry (pop org-bibtex-entries)) (org-special-properties nil) ; avoids errors with `org-entry-put' (val (lambda (field) (cdr (assoc field entry)))) - (togtag (lambda (tag) (org-toggle-tag tag 'on)))) - (org-insert-heading) - (insert (funcall org-bibtex-headline-format-function entry)) - (insert "\n:PROPERTIES:\n") - (org-bibtex-put "TITLE" (funcall val :title) 'insert) + (togtag (lambda (tag) (org-toggle-tag tag 'on))) + (insert-raw (not update-heading))) + (unless update-heading + (org-insert-heading) + (insert (funcall org-bibtex-headline-format-function entry)) + (insert "\n:PROPERTIES:\n")) + (org-bibtex-put "TITLE" (funcall val :title) insert-raw) (org-bibtex-put org-bibtex-type-property-name (downcase (funcall val :type)) - 'insert) + insert-raw) (dolist (pair entry) (pcase (car pair) (:title nil) (:type nil) - (:key (org-bibtex-put org-bibtex-key-property (cdr pair) 'insert)) + (:key (org-bibtex-put org-bibtex-key-property (cdr pair) insert-raw)) (:keywords (if org-bibtex-tags-are-keywords (dolist (kw (split-string (cdr pair) ", *")) (funcall @@ -752,25 +756,28 @@ drawer." (replace-regexp-in-string "[^[:alnum:]_@#%]" "" (replace-regexp-in-string "[ \t]+" "_" kw)))) - (org-bibtex-put (car pair) (cdr pair) 'insert))) - (_ (org-bibtex-put (car pair) (cdr pair) 'insert)))) - (insert ":END:\n") + (org-bibtex-put (car pair) (cdr pair) insert-raw))) + (_ (org-bibtex-put (car pair) (cdr pair) insert-raw)))) + (unless update-heading + (insert ":END:\n")) (mapc togtag org-bibtex-tags) (unless noindent (org-indent-region (save-excursion (org-back-to-heading t) (point)) (point))))) -(defun org-bibtex-yank () - "If kill ring holds a bibtex entry yank it as an Org headline." - (interactive) +(defun org-bibtex-yank (&optional update-heading) + "If kill ring holds a bibtex entry yank it as an Org headline. +When called with non-nil prefix argument UPDATE-HEADING, add data to the +headline of the entry at point." + (interactive "P") (let (entry) (with-temp-buffer (yank 1) (bibtex-mode) (setf entry (org-bibtex-read))) (if entry - (org-bibtex-write) + (org-bibtex-write nil update-heading) (error "Yanked text does not appear to contain a BibTeX entry")))) (defun org-bibtex-import-from-file (file)