From 4c4b024b49b1dc670985878fcd6f812ac40a8d76 Mon Sep 17 00:00:00 2001 From: Wetlize Date: Thu, 26 Aug 2021 14:04:46 +0300 Subject: [PATCH] (fix)capture: always trigger :if-new template for existing nodes (#1807) Previously, if org-roam-capture- would be triggered for an existing node it would just move the point to the beginning of the node, ignoring the target from the :if-new part of the template. This patch will now also run the :if-new part of the template for existing nodes, just like it does right now in case of daily templates (which happened to be never recognized as existing nodes during the capture process). --- CHANGELOG.md | 1 + org-roam-capture.el | 47 +++++++++++++++++++-------------------------- 2 files changed, 21 insertions(+), 27 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b546d5f..8546a32 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ ### Fixed - [#1798](https://github.com/org-roam/org-roam/pull/1798) org-roam-node-at-point: do not skip invisible headings +- [#1807](https://github.com/org-roam/org-roam/pull/1807) capture: always trigger `:if-new` template for existing nodes ## 2.1.0 ### Added diff --git a/org-roam-capture.el b/org-roam-capture.el index 4ed4a37..bff0e08 100644 --- a/org-roam-capture.el +++ b/org-roam-capture.el @@ -452,14 +452,7 @@ Note: During the capture process this function is run by `org-capture-set-target-location', as a (function ...) based capture target." (let ((id (cond ((run-hook-with-args-until-success 'org-roam-capture-preface-hook)) - ((and (org-roam-node-file org-roam-capture--node) - (org-roam-node-point org-roam-capture--node)) - (set-buffer (org-capture-target-buffer (org-roam-node-file org-roam-capture--node))) - (goto-char (org-roam-node-point org-roam-capture--node)) - (widen) - (org-roam-node-id org-roam-capture--node)) - (t - (org-roam-capture--setup-target-location))))) + (t (org-roam-capture--setup-target-location))))) (org-roam-capture--adjust-point-for-capture-type) (org-capture-put :template (org-roam-capture--fill-template (org-capture-get :template))) @@ -474,19 +467,15 @@ Return the ID of the location." (pcase (or (org-roam-capture--get :if-new) (user-error "Template needs to specify `:if-new'")) (`(file ,path) - (setq path (expand-file-name - (string-trim (org-roam-capture--fill-template path t)) - org-roam-directory)) - (setq new-file-p (org-roam-capture--new-file-p path)) + (setq path (org-roam-capture--target-truepath path) + new-file-p (org-roam-capture--new-file-p path)) (when new-file-p (org-roam-capture--put :new-file path)) (set-buffer (org-capture-target-buffer path)) (widen) (setq p (goto-char (point-min)))) (`(file+olp ,path ,olp) - (setq path (expand-file-name - (string-trim (org-roam-capture--fill-template path t)) - org-roam-directory)) - (setq new-file-p (org-roam-capture--new-file-p path)) + (setq path (org-roam-capture--target-truepath path) + new-file-p (org-roam-capture--new-file-p path)) (when new-file-p (org-roam-capture--put :new-file path)) (set-buffer (org-capture-target-buffer path)) (setq p (point-min)) @@ -494,10 +483,8 @@ Return the ID of the location." (goto-char m)) (widen)) (`(file+head ,path ,head) - (setq path (expand-file-name - (string-trim (org-roam-capture--fill-template path t)) - org-roam-directory)) - (setq new-file-p (org-roam-capture--new-file-p path)) + (setq path (org-roam-capture--target-truepath path) + new-file-p (org-roam-capture--new-file-p path)) (set-buffer (org-capture-target-buffer path)) (when new-file-p (org-roam-capture--put :new-file path) @@ -505,10 +492,8 @@ Return the ID of the location." (widen) (setq p (goto-char (point-min)))) (`(file+head+olp ,path ,head ,olp) - (setq path (expand-file-name - (string-trim (org-roam-capture--fill-template path t)) - org-roam-directory)) - (setq new-file-p (org-roam-capture--new-file-p path)) + (setq path (org-roam-capture--target-truepath path) + new-file-p (org-roam-capture--new-file-p path)) (set-buffer (org-capture-target-buffer path)) (widen) (when new-file-p @@ -518,9 +503,7 @@ Return the ID of the location." (let ((m (org-roam-capture-find-or-create-olp olp))) (goto-char m))) (`(file+datetree ,path ,tree-type) - (setq path (expand-file-name - (string-trim (org-roam-capture--fill-template path t)) - org-roam-directory)) + (setq path (org-roam-capture--target-truepath path)) (require 'org-datetree) (widen) (set-buffer (org-capture-target-buffer path)) @@ -581,6 +564,16 @@ Return the ID of the location." (org-id-get-create) (run-hooks 'org-roam-capture-new-node-hook))))) +(defun org-roam-capture--target-truepath (path) + "From PATH get the correct path to the current capture target and return it. +PATH is a string that can optionally contain templated text in +it." + (or (org-roam-node-file org-roam-capture--node) + (thread-first path + (org-roam-capture--fill-template t) + (string-trim) + (expand-file-name org-roam-directory)))) + (defun org-roam-capture--new-file-p (path) "Return t if PATH is for a new file with no visiting buffer." (not (or (file-exists-p path)