diff --git a/org-roam-capture.el b/org-roam-capture.el index 518dcf0..8447da2 100644 --- a/org-roam-capture.el +++ b/org-roam-capture.el @@ -72,9 +72,6 @@ The `ref' context is used by `org-roam-protocol', where the capture process is triggered upon trying to find or create a new note with the given `ref'.") -(defvar org-roam-capture--in-process nil - "Boolean tracking whether Org-roam captures are in-process.") - (defvar org-roam-capture-additional-template-props nil "Additional props to be added to the Org-roam template.") @@ -129,6 +126,13 @@ Details on how to specify for the template is given in `org-roam-capture-templat (setq org-capture-plist (plist-put org-capture-plist :org-roam p)))) +(defun org-roam-capture--in-process-p () + "Return non-nil if a `org-roam-capture' buffer exists." + (cl-some (lambda (buffer) + (and (eq (buffer-local-value 'major-mode (current-buffer)) 'org-mode) + (plist-get (buffer-local-value 'org-capture-current-plist (current-buffer)) :org-roam))) + (buffer-list))) + (defun org-roam-capture--fill-template (str &optional info) "Expands the template STR, returning the string. This is an extension of org-capture's template expansion. @@ -268,15 +272,10 @@ This function is used solely in Org-roam's capture templates: see (let ((prop (pop org-roam-capture-additional-template-props)) (val (pop org-roam-capture-additional-template-props))) (org-roam-capture--put prop val))) - (setq org-roam-capture--in-process t) (set-buffer (org-capture-target-buffer file-path)) (widen) (goto-char (point-max)))) -(defun org-roam-capture--cleanup-h () - "Cleans up after an Org-roam capture process." - (setq org-roam-capture--in-process nil)) - (defun org-roam-capture--convert-template (template) "Convert TEMPLATE from Org-roam syntax to `org-capture-templates' syntax." (let* ((copy (copy-tree template)) @@ -306,7 +305,6 @@ GOTO and KEYS argument have the same functionality as (when (= (length org-capture-templates) 1) (setq keys (caar org-capture-templates))) (add-hook 'org-capture-after-finalize-hook #'org-roam-capture--save-file-maybe-h) - (add-hook 'org-capture-after-finalize-hook #'org-roam-capture--cleanup-h 10) (org-capture goto keys))) ;;;###autoload @@ -314,7 +312,7 @@ GOTO and KEYS argument have the same functionality as "Launches an `org-capture' process for a new or existing note. This uses the templates defined at `org-roam-capture-templates'." (interactive) - (when org-roam-capture--in-process + (when (org-roam-capture--in-process-p) (user-error "Nested Org-roam capture processes not supported")) (let* ((completions (org-roam--get-title-path-completions)) (title (org-roam-completion--completing-read "File: " completions)) diff --git a/org-roam.el b/org-roam.el index 7464229..a6fcbad 100644 --- a/org-roam.el +++ b/org-roam.el @@ -335,16 +335,16 @@ If PREFIX, downcase the title before insertion." (when region ;; Remove previously selected text. (delete-region (car region) (cdr region))) (insert (org-roam--format-link target-file-path link-description))) - (if org-roam-capture--in-process - (user-error "Nested Org-roam capture processes not supported") - (let ((org-roam-capture--info (list (cons 'title title) - (cons 'slug (org-roam--title-to-slug title)))) - (org-roam-capture--context 'title)) - (add-hook 'org-capture-after-finalize-hook #'org-roam-capture--insert-link-h) - (setq org-roam-capture-additional-template-props (list :region region - :link-description link-description - :capture-fn 'org-roam-insert)) - (org-roam--capture)))))) + (when (org-roam-capture--in-process-p) + (user-error "Nested Org-roam capture processes not supported")) + (let ((org-roam-capture--info (list (cons 'title title) + (cons 'slug (org-roam--title-to-slug title)))) + (org-roam-capture--context 'title)) + (add-hook 'org-capture-after-finalize-hook #'org-roam-capture--insert-link-h) + (setq org-roam-capture-additional-template-props (list :region region + :link-description link-description + :capture-fn 'org-roam-insert)) + (org-roam--capture))))) ;;;; org-roam-find-file (defun org-roam--get-title-path-completions () @@ -371,7 +371,7 @@ INITIAL-PROMPT is the initial title prompt." (file-path (cdr (assoc title completions)))) (if file-path (find-file file-path) - (if org-roam-capture--in-process + (if (org-roam-capture--in-process-p) (user-error "Org-roam capture in process") (let ((org-roam-capture--info (list (cons 'title title) (cons 'slug (org-roam--title-to-slug title))))