reddit / emacs

Org-roam-capture function behaves differently when called interactively, and from org-metareturn-hook. Why?

Jack-o-tall-tales · 2022-08-24 19:25
I've got the following function: (defun my/org-roam-capture-to-link () "In org-roam files, capture to node linked at point." (interactive) (when-let ((_ (org-roam-file-p)) ;; Only act in org-roam files (link-start (car (org-in-regexp org-link-bracket-re 1))) (link (save-excursion (goto-char link-start) (org-element-link-parser))) (id (plist-get (cadr link) :path)) ;; This will fail if there is no node (node (org-roam-node-from-id id))) ;; If there /is/ a node, capture to it. (org-roam-capture- :node node))) This should capture to the org-roam node at the id linked at point, but only if point is at a link to such a node, and I'm in an org-roam file's buffer. Normally when I just run `org-roam-capture` and select an already existing node, *no* new text is added, and I just add whatever I need to and press `C-c C-c`. Now, If I'm on a link to an org-roam node, and run `my/org-roam-capture-to-link` interactively with `M-x`, it does exactly that: nothing new is added, exactly what I want. But I wanted to run it from `org-metareturn`. I added the symbol for the function to `org-metareturn-hook`. Now if I press `M-RET` on an appropriate link, I *do* get an org-roam capture, but it adds an empty heading on a newline (that is, it adds "\\n* |", with the cursor at |). What's going on? How can I make the meta-return version work like the M-x version? Thanks!
organic-bookworm · 2022-08-25 14:09
The [`org-meta-return`](https://github.com/emacs-straight/org-mode/blob/00adad9357b9123a7b11ecf12c148a5196debcb7/lisp/org.el#L17643-L17654) does `(run-hook-with-args-until-success 'org-metareturn-hook)`. Thus, the reason is `my/org-roam-capture-to-link` function probably returns `nil`, so `org-insert-heading` happens.
Jack-o-tall-tales · 2022-08-25 14:31
Thanks, this sounds likely!!
Jack-o-tall-tales · 2022-08-25 16:50
Just checked, and yes, this was it. Thanks again!