emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* [FR] Please add writing to existing heading in org-bibtex
@ 2022-12-27 16:24 Sterling Hooten
  2022-12-27 16:34 ` Ihor Radchenko
  0 siblings, 1 reply; 4+ messages in thread
From: Sterling Hooten @ 2022-12-27 16:24 UTC (permalink / raw)
  To: emacs-orgmode

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?

Thanks,
Sterling

diff --git a/lisp/ol-bibtex.el b/lisp/ol-bibtex.el
index 81b99167b..38198eae4 100644
--- a/lisp/ol-bibtex.el
+++ b/lisp/ol-bibtex.el
@@ -703,8 +703,9 @@ Return the number of saved entries."
   (interactive "fFile: ")
   (org-bibtex-read-buffer (find-file-noselect file 'nowarn 'rawfile)))
 
-(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."
+  ;; SWH 2022-11-22 changes to allow for writing heading at point instead of inserting new.
   (interactive)
   (when (= (length org-bibtex-entries) 0)
     (error "No entries in `org-bibtex-entries'"))
@@ -712,8 +713,9 @@ Return the number of saved 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))
+    (unless no-new
+      (org-insert-heading)
+      (insert (funcall org-bibtex-headline-format-function entry)))
     (org-bibtex-put "TITLE" (funcall val :title))
     (org-bibtex-put org-bibtex-type-property-name
 		    (downcase (funcall val :type)))



^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [FR] Please add writing to existing heading in org-bibtex
  2022-12-27 16:24 [FR] Please add writing to existing heading in org-bibtex Sterling Hooten
@ 2022-12-27 16:34 ` Ihor Radchenko
  2023-01-06 18:51   ` Sterling Hooten
  0 siblings, 1 reply; 4+ messages in thread
From: Ihor Radchenko @ 2022-12-27 16:34 UTC (permalink / raw)
  To: Sterling Hooten; +Cc: emacs-orgmode

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>


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [FR] Please add writing to existing heading in org-bibtex
  2022-12-27 16:34 ` Ihor Radchenko
@ 2023-01-06 18:51   ` Sterling Hooten
  2023-01-11  9:58     ` Ihor Radchenko
  0 siblings, 1 reply; 4+ messages in thread
From: Sterling Hooten @ 2023-01-06 18:51 UTC (permalink / raw)
  To: Ihor Radchenko; +Cc: emacs-orgmode

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>



^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [FR] Please add writing to existing heading in org-bibtex
  2023-01-06 18:51   ` Sterling Hooten
@ 2023-01-11  9:58     ` Ihor Radchenko
  0 siblings, 0 replies; 4+ messages in thread
From: Ihor Radchenko @ 2023-01-11  9:58 UTC (permalink / raw)
  To: Sterling Hooten; +Cc: emacs-orgmode

Sterling Hooten <hooten@gmail.com> writes:

> 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.

You are indeed right.
I suggest doing the following:
1. Change the function definition to (&optional arg no-indent nonew)
2. With C-u prefix ARG or ARG = 'new, update heading at point
3. When ARG is not C-u and not 'new, treat it as non-nil no-indent in
   the past
   "22.12 Prefix Command Arguments" section of Elisp manual might be
   helpful to understand how to check C-u programmatically.
4. If NO-INDENT or NONEW arguments are non-nil, use them unconditionally
   regardless of the value of ARG

This way, the previous usage of the function will be the least affected.

> 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.

If you can, please create a proper patch with commit message, as
suggested in https://orgmode.org/worg/org-contribute.html#commit-messages

You can also refer to
https://orgmode.org/worg/org-contribute.html#org8a3a431

> +*** ~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.

Please use double space between sentences. It is our convention. See
https://git.savannah.gnu.org/cgit/emacs/org-mode.git/tree/doc/Documentation_Standards.org 
 

-- 
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>


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2023-01-11  9:59 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-12-27 16:24 [FR] Please add writing to existing heading in org-bibtex Sterling Hooten
2022-12-27 16:34 ` Ihor Radchenko
2023-01-06 18:51   ` Sterling Hooten
2023-01-11  9:58     ` Ihor Radchenko

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).