emacs-orgmode
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [FR] Please add writing to existing heading in org-bibtex


From: Sterling Hooten
Subject: Re: [FR] Please add writing to existing heading in org-bibtex
Date: Fri, 6 Jan 2023 15:51:58 -0300

Thanks for the instructions, this is my first patch.

I think calling  `nonew’ invalidates the argument `no-indent’ as 
`org-bibtex-put’ eventually 
calls `org-entry-put’ which uses `org-indent-line’. I’m not sure what’s the 
best way to handle that.

diff --git a/etc/ORG-NEWS b/etc/ORG-NEWS
index d4e9b4368..8eab4cae2 100644
--- a/etc/ORG-NEWS
+++ b/etc/ORG-NEWS
@@ -513,6 +513,10 @@ use =attachment:= style links instead of the standard 
=file:= link type.
 *** New function ~org-get-title~ to get =#+TITLE:= property from buffers
 
 A function to collect the document title from the org-mode buffer.
+*** ~org-bibtex-write~ can now write to heading at point with optional 
interactive argument
+
+Previously, a new heading was created. Now with argument =nonew= the
+bibtex data will be added to properties of heading at point.
 
 *** ~org-fold-show-entry~ does not fold drawers by default anymore
 
diff --git a/lisp/ol-bibtex.el b/lisp/ol-bibtex.el
index 313b1cde8..91aa4c36e 100644
--- a/lisp/ol-bibtex.el
+++ b/lisp/ol-bibtex.el
@@ -718,29 +718,33 @@ 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 nonew noindent)
   "Insert a heading built from the first element of `org-bibtex-entries'.
+
+With prefix argument NONEW modify properties of heading at point.
 When optional argument NOINDENT is non-nil, do not indent the properties
 drawer."
-  (interactive)
+  (interactive "P")
   (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 nonew)))
+    (unless nonew
+      (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
@@ -748,9 +752,9 @@ 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 nonew (insert ":END:\n"))
     (mapc togtag org-bibtex-tags)
     (unless noindent
       (org-indent-region
@@ -771,7 +775,7 @@ drawer."
   (interactive "fFile: ")
   (let ((pos (point)))
     (dotimes (_ (org-bibtex-read-file file))
-      (save-excursion (org-bibtex-write 'noindent))
+      (save-excursion (org-bibtex-write nil 'noindent))
       (re-search-forward org-property-end-re)
       (insert "\n"))
     (org-indent-region pos (point))))


> On 2022-12-27, at 13:34, Ihor Radchenko <yantar92@posteo.net> wrote:
> 
> Sterling Hooten <hooten@gmail.com> writes:
> 
>> The default behavior of org-bibtex-write is to insert a new
>> heading with the bibliographic data in the properties. But an
>> alternative workflow would just update the properties of the heading at
>> point, rather than creating a new one. The below patch is a simple
>> implementation I’ve been using for a month. Would it be possible to
>> integrate this upstream?
> 
> Yes. It will make sense.
> 
>> -(defun org-bibtex-write ()
>> -  "Insert a heading built from the first element of `org-bibtex-entries'."
>> +(defun org-bibtex-write (&optional no-new)
>> +  "Insert a heading built from the first element of `org-bibtex-entries'. 
>> With non-nil optional NO-NEW write to heading at point instead of creating 
>> new."
> 
> Please put the second sentence on a separate line. By convention, first
> line is a short description of the command and should not exceed 80
> symbols. See
> https://www.gnu.org/software/emacs/manual/html_node/elisp/Documentation-Tips.html
> 
> Also, it will be a good idea to write heading at point when the command
> is called with prefix argument (C-u).
> 
>> +  ;; SWH 2022-11-22 changes to allow for writing heading at point instead 
>> of inserting new.
> 
> Please prepare a proper patch with CHANGELOG entry.
> This comment is not how we make changes in Org.
> See https://orgmode.org/worg/org-contribute.html#first-patch
> 
>>   (interactive)
> 
> You can change the interactive form to allow prefix argument.
> See 
> https://www.gnu.org/software/emacs/manual/html_node/elisp/Using-Interactive.html
> 
> -- 
> Ihor Radchenko // yantar92,
> Org mode contributor,
> Learn more about Org mode at <https://orgmode.org/>.
> Support Org development at <https://liberapay.com/org-mode>,
> or support my work at <https://liberapay.com/yantar92>




reply via email to

[Prev in Thread] Current Thread [Next in Thread]