(fix): prevent link-extraction from keeping buffers open (#1131)

Fixes #1129 .

Co-authored-by: Jethro Kuan <jethrokuan95@gmail.com>
This commit is contained in:
Natnael Kahssay
2020-09-26 04:42:59 -07:00
committed by GitHub
parent 346bbf50a1
commit c05368a16b
2 changed files with 22 additions and 6 deletions

View File

@ -63,6 +63,19 @@ If FILE, set `org-roam-temp-file-name' to file and insert its contents."
(setq-local org-roam-file-name ,file)) (setq-local org-roam-file-name ,file))
,@body))))) ,@body)))))
(defmacro org-roam--with-file (file &rest body)
"Execute BODY within a FILE.
Closes the file if the file is not yet visited."
(declare (indent 1) (debug t))
`(let* ((existing-buf (find-buffer-visiting ,file))
(buf (or existing-buf
(find-file-noselect file)))
res)
(with-current-buffer buf
(setq res ,@body))
(when existing-buf (kill-buffer existing-buf))
res))
(defun org-roam-message (format-string &rest args) (defun org-roam-message (format-string &rest args)
"Pass FORMAT-STRING and ARGS to `message' when `org-roam-verbose' is t." "Pass FORMAT-STRING and ARGS to `message' when `org-roam-verbose' is t."
(when org-roam-verbose (when org-roam-verbose

View File

@ -1102,16 +1102,19 @@ Return nil otherwise."
:limit 1] :limit 1]
id))) id)))
(defun org-roam-id-find (id &optional markerp strict) (defun org-roam-id-find (id &optional markerp strict keep-buffer-p)
"Return the location of the entry with the id ID. "Return the location of the entry with the id ID.
When MARKERP is non-nil, return a marker pointing to theheadline. When MARKERP is non-nil, return a marker pointing to theheadline.
Otherwise, return a cons formatted as \(file . pos). Otherwise, return a cons formatted as \(file . pos).
When STRICT is non-nil, only consider Org-roams database." When STRICT is non-nil, only consider Org-roams database.
When KEEP-BUFFER-P is non-nil, keep the buffers navigated by Org-roam open."
(let ((file (or (org-roam-id-get-file id) (let ((file (or (org-roam-id-get-file id)
(unless strict (unless strict (org-id-find-id-file id)))))
(org-id-find-id-file id)))))
(when file (when file
(org-id-find-id-in-file id file markerp)))) (if keep-buffer-p
(org-id-find-id-in-file id file markerp)
(org-roam--with-file file
(org-id-find-id-in-file id file markerp))))))
(defun org-roam-id-open (id-or-marker &optional strict) (defun org-roam-id-open (id-or-marker &optional strict)
"Go to the entry with ID-OR-MARKER. "Go to the entry with ID-OR-MARKER.
@ -1124,7 +1127,7 @@ to the default behaviour of `org-id-open'.
When STRICT is non-nil, only consider Org-roams database." When STRICT is non-nil, only consider Org-roams database."
(when-let ((marker (if (markerp id-or-marker) (when-let ((marker (if (markerp id-or-marker)
id-or-marker id-or-marker
(org-roam-id-find id-or-marker t strict)))) (org-roam-id-find id-or-marker t strict t))))
(org-goto-marker-or-bmk marker) (org-goto-marker-or-bmk marker)
(set-marker marker nil))) (set-marker marker nil)))