reddit / emacs

Trouble with custom Org-Roam function and (save-excursion)

gravygrowinggreen ยท 2022-06-05 00:25
I am using a custom function meant to take an entry in a .org document, and put it into a node, either a new one, or one that already existed. Org-roam-refile does not work if the node you want to put the text in doesn't already exist, and org-roam-extract-subtree doesn't work if you want to put the text into a pre-existing node. Heavily inspired by system-crafters, what I settled on doing was initiating the org-roam-capture process to guarantee that there's a node, then using org-refile to move the entry to the target document. It looks roughly like this. (defun my/org-roam-gtd-reference () (interactive) (let ( (org-refile-keep nil) ;; Set this to nil to delete the original! (org-roam-capture-templates '(("k" "knowledge" plain "%?" :if-new (file+head+olp "pages/${slug}.org" "#+title: ${title}\n" ("Knowledge"))))) (org-after-refile-insert-hook #'save-buffer) nodefile position ) (save-window-excursion (org-roam-capture "k") (setq nodefile (buffer-file-name)) (setq position (point))) (org-refile nil nil (list "Knowledge" nodefile nil position))) (org-gtd-process-inbox)) The problem in this function seems to be in the (save-window-excursion (org-roam-capture "k") (setq nodefile (buffer-file-name)) (setq position (point))) I cannot for the life of me figure this out. But buffer-file-name is returning nil, which causes nodefile to be defined as nil, which causes the eventual org-refile command to fail. Strangely, this block works fine if it uses the org-roam-dailies-capture-today command instead of org-roam-capture. Unfortunately, that wouldn't fit my workflow, since it would capture to dailies instead of the actual nodes.