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/
This commit is contained in:
Martin Kampas 2024-02-12 13:24:54 +01:00 committed by Ihor Radchenko
parent 5d186b499d
commit 37e468cf16
No known key found for this signature in database
GPG Key ID: 6470762A7DA11D8B
2 changed files with 40 additions and 23 deletions

View File

@ -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. robust by using the file id instead of the file path.
** New features ** 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=: Support tikz file format output
=ob-plantuml.el= now output =tikz= :file format via =ob-plantuml.el= now output =tikz= :file format via
@ -1127,6 +1132,11 @@ The same can be done via startup options:
: #+STARTUP: fnanon : #+STARTUP: fnanon
** New functions and changes in function arguments ** 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~ *** New API functions to store data within ~org-element-cache~
Elisp programs can now store data inside Org element cache. Elisp programs can now store data inside Org element cache.

View File

@ -640,22 +640,23 @@ With prefix argument OPTIONAL also prompt for optional fields."
"Return headline text according to ENTRY title." "Return headline text according to ENTRY title."
(cdr (assq :title entry))) (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. "Create a new entry at the given level.
With a prefix arg, query for optional fields as well. With a prefix ARG, query for optional fields as well.
If nonew is t, add data to the headline of the entry at point." If UPDATE-HEADING is non-nil, add data to the headline of the entry at
point."
(interactive "P") (interactive "P")
(let* ((type (completing-read (let* ((type (completing-read
"Type: " (mapcar (lambda (type) "Type: " (mapcar (lambda (type)
(substring (symbol-name (car type)) 1)) (substring (symbol-name (car type)) 1))
org-bibtex-types) org-bibtex-types)
nil nil (when nonew nil nil (when update-heading
(org-bibtex-get org-bibtex-type-property-name)))) (org-bibtex-get org-bibtex-type-property-name))))
(type (if (keywordp type) type (intern (concat ":" type)))) (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) (unless (assoc type org-bibtex-types)
(error "Type:%s is not known" type)) (error "Type:%s is not known" type))
(if nonew (if update-heading
(org-back-to-heading) (org-back-to-heading)
(org-insert-heading) (org-insert-heading)
(let ((title (org-bibtex-ask :title))) (let ((title (org-bibtex-ask :title)))
@ -722,29 +723,32 @@ Return the number of saved entries."
(interactive "fFile: ") (interactive "fFile: ")
(org-bibtex-read-buffer (find-file-noselect file 'nowarn 'rawfile))) (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'. "Insert a heading built from the first element of `org-bibtex-entries'.
When optional argument NOINDENT is non-nil, do not indent the properties 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) (interactive)
(unless org-bibtex-entries (unless org-bibtex-entries
(error "No entries in `org-bibtex-entries'")) (error "No entries in `org-bibtex-entries'"))
(let* ((entry (pop org-bibtex-entries)) (let* ((entry (pop org-bibtex-entries))
(org-special-properties nil) ; avoids errors with `org-entry-put' (org-special-properties nil) ; avoids errors with `org-entry-put'
(val (lambda (field) (cdr (assoc field entry)))) (val (lambda (field) (cdr (assoc field entry))))
(togtag (lambda (tag) (org-toggle-tag tag 'on)))) (togtag (lambda (tag) (org-toggle-tag tag 'on)))
(org-insert-heading) (insert-raw (not update-heading)))
(insert (funcall org-bibtex-headline-format-function entry)) (unless update-heading
(insert "\n:PROPERTIES:\n") (org-insert-heading)
(org-bibtex-put "TITLE" (funcall val :title) 'insert) (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 (org-bibtex-put org-bibtex-type-property-name
(downcase (funcall val :type)) (downcase (funcall val :type))
'insert) insert-raw)
(dolist (pair entry) (dolist (pair entry)
(pcase (car pair) (pcase (car pair)
(:title nil) (:title nil)
(:type 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 (:keywords (if org-bibtex-tags-are-keywords
(dolist (kw (split-string (cdr pair) ", *")) (dolist (kw (split-string (cdr pair) ", *"))
(funcall (funcall
@ -752,25 +756,28 @@ drawer."
(replace-regexp-in-string (replace-regexp-in-string
"[^[:alnum:]_@#%]" "" "[^[:alnum:]_@#%]" ""
(replace-regexp-in-string "[ \t]+" "_" kw)))) (replace-regexp-in-string "[ \t]+" "_" kw))))
(org-bibtex-put (car pair) (cdr pair) 'insert))) (org-bibtex-put (car pair) (cdr pair) insert-raw)))
(_ (org-bibtex-put (car pair) (cdr pair) 'insert)))) (_ (org-bibtex-put (car pair) (cdr pair) insert-raw))))
(insert ":END:\n") (unless update-heading
(insert ":END:\n"))
(mapc togtag org-bibtex-tags) (mapc togtag org-bibtex-tags)
(unless noindent (unless noindent
(org-indent-region (org-indent-region
(save-excursion (org-back-to-heading t) (point)) (save-excursion (org-back-to-heading t) (point))
(point))))) (point)))))
(defun org-bibtex-yank () (defun org-bibtex-yank (&optional update-heading)
"If kill ring holds a bibtex entry yank it as an Org headline." "If kill ring holds a bibtex entry yank it as an Org headline.
(interactive) When called with non-nil prefix argument UPDATE-HEADING, add data to the
headline of the entry at point."
(interactive "P")
(let (entry) (let (entry)
(with-temp-buffer (with-temp-buffer
(yank 1) (yank 1)
(bibtex-mode) (bibtex-mode)
(setf entry (org-bibtex-read))) (setf entry (org-bibtex-read)))
(if entry (if entry
(org-bibtex-write) (org-bibtex-write nil update-heading)
(error "Yanked text does not appear to contain a BibTeX entry")))) (error "Yanked text does not appear to contain a BibTeX entry"))))
(defun org-bibtex-import-from-file (file) (defun org-bibtex-import-from-file (file)