(bugfix): Use predicate to check for a org-roam-capture process (#413)

Instead of using a global variable to check for a existing capture,
leverage the :org-roam plist property of the buffer-local variable
`org-roam-capture-current-plist`.

This prevents a lockout if the capture buffer is killed
This commit is contained in:
Jürgen Hötzel 2020-04-10 11:03:49 +02:00 committed by GitHub
parent 772504c488
commit 92500b1338
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 21 deletions

View file

@ -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))

View file

@ -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))))