(fix)capture: check for buffer existence before template insertion (#1707)

Redefine a new file to Org-roam's capture system as one that does not
have a buffer visiting it yet. This means that if a buffer is already
visiting the file, then the HEAD template will not be inserted. This is
important because Org-roam capture does not enforce the save of files,
and this can lead to scenarios where the template is inserted multiple
times to a buffer for a file that has not yet been written to disk.
This commit is contained in:
Jethro Kuan
2021-07-29 22:39:24 +08:00
committed by GitHub
parent a84da59b41
commit d3b7c9b921

View File

@ -528,15 +528,15 @@ also run Org-capture's template expansion."
(defun org-roam-capture--goto-location () (defun org-roam-capture--goto-location ()
"Initialize the buffer, and goto the location of the new capture. "Initialize the buffer, and goto the location of the new capture.
Return the ID of the location." Return the ID of the location."
(let (p) (let (p new-file-p)
(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 (expand-file-name
(string-trim (org-roam-capture--fill-template path t)) (string-trim (org-roam-capture--fill-template path t))
org-roam-directory)) org-roam-directory))
(unless (file-exists-p path) (setq new-file-p (not (org-find-base-buffer-visiting path)))
(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))))
@ -544,9 +544,9 @@ Return the ID of the location."
(setq path (expand-file-name (setq path (expand-file-name
(string-trim (org-roam-capture--fill-template path t)) (string-trim (org-roam-capture--fill-template path t))
org-roam-directory)) org-roam-directory))
(setq new-file-p (not (org-find-base-buffer-visiting 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))
(unless (file-exists-p path)
(org-roam-capture--put :new-file path))
(setq p (point-min)) (setq p (point-min))
(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))
@ -555,8 +555,9 @@ Return the ID of the location."
(setq path (expand-file-name (setq path (expand-file-name
(string-trim (org-roam-capture--fill-template path t)) (string-trim (org-roam-capture--fill-template path t))
org-roam-directory)) org-roam-directory))
(setq new-file-p (not (org-find-base-buffer-visiting path)))
(set-buffer (org-capture-target-buffer path)) (set-buffer (org-capture-target-buffer path))
(unless (file-exists-p path) (when new-file-p
(org-roam-capture--put :new-file path) (org-roam-capture--put :new-file path)
(insert (org-roam-capture--fill-template head t))) (insert (org-roam-capture--fill-template head t)))
(widen) (widen)
@ -565,9 +566,10 @@ Return the ID of the location."
(setq path (expand-file-name (setq path (expand-file-name
(string-trim (org-roam-capture--fill-template path t)) (string-trim (org-roam-capture--fill-template path t))
org-roam-directory)) org-roam-directory))
(widen) (setq new-file-p (not (org-find-base-buffer-visiting path)))
(set-buffer (org-capture-target-buffer path)) (set-buffer (org-capture-target-buffer path))
(unless (file-exists-p path) (widen)
(when new-file-p
(org-roam-capture--put :new-file path) (org-roam-capture--put :new-file path)
(insert (org-roam-capture--fill-template head t))) (insert (org-roam-capture--fill-template head t)))
(setq p (point-min)) (setq p (point-min))