Have helm-bibtex action "edit notes" on selected entry to use a particular org-roam-capture-template without asking

For some time I had only one org-roam-capture-template called “literature”, meant for notes on scientific articles I read. This template leverages org-roam-bibtex to extract title, author, journal and year of publication from a BibTeX entry and places them on the newly created org-roam node according to a template I wrote on a separate .org file. My usage with this template was the following: I launched helm-bibtex, selected an entry from the list of articles, press TAB for the actions menu and selected “Edit notes” (alternatively, press F8). And this worked wonderfully. Due to the fact that “literature” was the only org-roam capture template, this workflow automatically created (if the note didn’t exist yet) the node file following the template.

Now I’ve recently added a second template to the list of my org-roam-capture-templates, named “notes”, and intended to create plain notes on my own ideas.

The problem I’m facing now is that if I repeat the workflow of launching helm-bibtex and selecting the “Edit notes” action, it opens a capture menu asking me for the template to use, either “notes” or “literature”. Now at this point it is evident for me that I would like this to be a “literature” capture template node, so the choice here is kind of redundant.

My question is, is there a way to tell helm-bibtex ‘Edit notes’ action that it should activate an org-roam-capture using a specific template, and pass the corresponding arguments so that stuff like author, title, journal and year are properly read by my template? In other words, is there a way to skip the capture template choice menu?

Thanks in advance for all your help.

Are you using org-roam-bibtex to interface with helm-bibtex?

If so, it looks like you’d need to do something with function orb-insert-edit-note, which in turn calls orb--new-note. See this lines of code, especially:

  • This comment: ;; if only one template is defined, use it.
  • Line 565 sets the templates by copying all the org-roam-capture-templates.

These lines of code seem to set the actions for helm-bibtex for org-roam-bibtex. By the looks of L82, orb-insert-edit-note is the actual function called by the “Edit notes” action.

My guess is that the simplest is to create your own function, replacing orb-insert-edit-note to use only one template, and set variable helm-make-actions with your own custom function.

1 Like

Thanks a lot @nobiot for your quick help. I’m new to emacs-lisp, and I couldn’t figure out how to modify those lines (I tried and failed miserably), so I came up with an alternative approach (more of a subterfuge I guess), described below.

I thought I’ll use the advice mechanics to apply it to orb–new-note, so it removes all other org-roam-capture-templates but the one I’m interested in before it runs, and then reinstate them after the creation of the note. Below is the code if anyone’s interested.

I’m open to criticisms and enhancements from all the community on this matter. Maybe this is not a healthy practice, so please let me know.

Thanks again.

(defun my-around-orb-new-note (orig-func &rest args)
  "Execute code before and after orb--new-note."
  ;; Backup the original org-roam-capture-templates
  (let ((original-templates org-roam-capture-templates))
    ;; Modify org-roam-capture-templates to have only the "r" template
    (setq org-roam-capture-templates
          (list (assoc "r" original-templates)))
    ;; Call the original function with the modified arguments
    (apply orig-func args)
    ;; Restore the original org-roam-capture-templates
    (setq org-roam-capture-templates original-templates)))

;; Add advice to orb--new-note function
(advice-add 'orb--new-note :around #'my-around-orb-new-note)
1 Like

Great! Advice is a good approach.

My guess is if you set your single template to org-roam-capture-templates with a let binding like you do, you don’t need to re-set them back to the original. The let binding would work inly for this function call, if I understand it correctly.

Thanks for your feedback. I was wondering if using “advice” methods is something frowned-upon because of it being rather inelegant, but with my limited knowledge of emacs (so far) I wasn’t able to even conceive a better approach.

Great, that’s good to hear. I’ll check that. Thanks again.