emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Sterling Hooten <hooten@gmail.com>
To: Ihor Radchenko <yantar92@posteo.net>
Cc: emacs-orgmode@gnu.org
Subject: Re: [FR] Please add writing to existing heading in org-bibtex
Date: Fri, 6 Jan 2023 15:51:58 -0300	[thread overview]
Message-ID: <0FD9DD13-EA96-440E-B873-571DA364B274@gmail.com> (raw)
In-Reply-To: <87cz8424b4.fsf@localhost>

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	other threads:[~2023-01-06 18:52 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]
2023-01-11  9:58     ` Ihor Radchenko

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=0FD9DD13-EA96-440E-B873-571DA364B274@gmail.com \
    --to=hooten@gmail.com \
    --cc=emacs-orgmode@gnu.org \
    --cc=yantar92@posteo.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).