(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).
This commit is contained in:
Wetlize
2021-08-26 14:04:46 +03:00
committed by GitHub
parent 5dde894a0c
commit 4c4b024b49
2 changed files with 21 additions and 27 deletions

View File

@ -8,6 +8,7 @@
### Fixed ### Fixed
- [#1798](https://github.com/org-roam/org-roam/pull/1798) org-roam-node-at-point: do not skip invisible headings - [#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 ## 2.1.0
### Added ### Added

View File

@ -452,14 +452,7 @@ Note: During the capture process this function is run by
`org-capture-set-target-location', as a (function ...) based `org-capture-set-target-location', as a (function ...) based
capture target." capture target."
(let ((id (cond ((run-hook-with-args-until-success 'org-roam-capture-preface-hook)) (let ((id (cond ((run-hook-with-args-until-success 'org-roam-capture-preface-hook))
((and (org-roam-node-file org-roam-capture--node) (t (org-roam-capture--setup-target-location)))))
(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)))))
(org-roam-capture--adjust-point-for-capture-type) (org-roam-capture--adjust-point-for-capture-type)
(org-capture-put :template (org-capture-put :template
(org-roam-capture--fill-template (org-capture-get :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) (pcase (or (org-roam-capture--get :if-new)
(user-error "Template needs to specify `:if-new'")) (user-error "Template needs to specify `:if-new'"))
(`(file ,path) (`(file ,path)
(setq path (expand-file-name (setq path (org-roam-capture--target-truepath path)
(string-trim (org-roam-capture--fill-template path t)) new-file-p (org-roam-capture--new-file-p path))
org-roam-directory))
(setq new-file-p (org-roam-capture--new-file-p path))
(when new-file-p (org-roam-capture--put :new-file path)) (when new-file-p (org-roam-capture--put :new-file path))
(set-buffer (org-capture-target-buffer path)) (set-buffer (org-capture-target-buffer path))
(widen) (widen)
(setq p (goto-char (point-min)))) (setq p (goto-char (point-min))))
(`(file+olp ,path ,olp) (`(file+olp ,path ,olp)
(setq path (expand-file-name (setq path (org-roam-capture--target-truepath path)
(string-trim (org-roam-capture--fill-template path t)) new-file-p (org-roam-capture--new-file-p path))
org-roam-directory))
(setq new-file-p (org-roam-capture--new-file-p path))
(when new-file-p (org-roam-capture--put :new-file path)) (when new-file-p (org-roam-capture--put :new-file path))
(set-buffer (org-capture-target-buffer path)) (set-buffer (org-capture-target-buffer path))
(setq p (point-min)) (setq p (point-min))
@ -494,10 +483,8 @@ Return the ID of the location."
(goto-char m)) (goto-char m))
(widen)) (widen))
(`(file+head ,path ,head) (`(file+head ,path ,head)
(setq path (expand-file-name (setq path (org-roam-capture--target-truepath path)
(string-trim (org-roam-capture--fill-template path t)) new-file-p (org-roam-capture--new-file-p path))
org-roam-directory))
(setq new-file-p (org-roam-capture--new-file-p path))
(set-buffer (org-capture-target-buffer path)) (set-buffer (org-capture-target-buffer path))
(when new-file-p (when new-file-p
(org-roam-capture--put :new-file path) (org-roam-capture--put :new-file path)
@ -505,10 +492,8 @@ Return the ID of the location."
(widen) (widen)
(setq p (goto-char (point-min)))) (setq p (goto-char (point-min))))
(`(file+head+olp ,path ,head ,olp) (`(file+head+olp ,path ,head ,olp)
(setq path (expand-file-name (setq path (org-roam-capture--target-truepath path)
(string-trim (org-roam-capture--fill-template path t)) new-file-p (org-roam-capture--new-file-p path))
org-roam-directory))
(setq new-file-p (org-roam-capture--new-file-p path))
(set-buffer (org-capture-target-buffer path)) (set-buffer (org-capture-target-buffer path))
(widen) (widen)
(when new-file-p (when new-file-p
@ -518,9 +503,7 @@ Return the ID of the location."
(let ((m (org-roam-capture-find-or-create-olp olp))) (let ((m (org-roam-capture-find-or-create-olp olp)))
(goto-char m))) (goto-char m)))
(`(file+datetree ,path ,tree-type) (`(file+datetree ,path ,tree-type)
(setq path (expand-file-name (setq path (org-roam-capture--target-truepath path))
(string-trim (org-roam-capture--fill-template path t))
org-roam-directory))
(require 'org-datetree) (require 'org-datetree)
(widen) (widen)
(set-buffer (org-capture-target-buffer path)) (set-buffer (org-capture-target-buffer path))
@ -581,6 +564,16 @@ Return the ID of the location."
(org-id-get-create) (org-id-get-create)
(run-hooks 'org-roam-capture-new-node-hook))))) (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) (defun org-roam-capture--new-file-p (path)
"Return t if PATH is for a new file with no visiting buffer." "Return t if PATH is for a new file with no visiting buffer."
(not (or (file-exists-p path) (not (or (file-exists-p path)