diff --git a/org-roam-link.el b/org-roam-link.el index 94287d9..45e19d7 100644 --- a/org-roam-link.el +++ b/org-roam-link.el @@ -94,15 +94,11 @@ noabbrev Absolute path, no abbreviation of home directory." If FILE, return outline headings for passed FILE instead. If WITH-MARKER, return a cons cell of (headline . marker). If USE-STACK, include the parent paths as well." - (let* ((buf (or (and file - (or (find-buffer-visiting file) - (find-file-noselect file))) - (current-buffer))) - (outline-level-fn outline-level) - (path-separator "/") - (stack-level 0) - stack cands name level marker) - (with-current-buffer buf + (org-roam-with-file file 'keep + (let* ((outline-level-fn outline-level) + (path-separator "/") + (stack-level 0) + stack cands name level marker) (save-excursion (goto-char (point-min)) (while (re-search-forward org-complex-heading-regexp nil t) @@ -126,8 +122,8 @@ If USE-STACK, include the parent paths as well." path-separator))) (push (if with-marker (cons name marker) - name) cands))))) - (nreverse cands))) + name) cands)))) + (nreverse cands)))) (defun org-roam-link--get-file-from-title (title &optional no-interactive) "Return the file path corresponding to TITLE. @@ -147,10 +143,7 @@ When NO-INTERACTIVE, return nil if there are multiple options." If FILE, get headline from FILE instead. If there is no corresponding headline, return nil." (save-excursion - (with-current-buffer (or (and file - (or (find-buffer-visiting file) - (find-file-noselect file))) - (current-buffer)) + (org-roam-with-file file 'keep (let ((headlines (org-roam-link--get-headlines file 'with-markers))) (when-let ((marker (cdr (assoc-string headline headlines)))) (goto-char marker) diff --git a/org-roam-macs.el b/org-roam-macs.el index 4223e09..8282b72 100644 --- a/org-roam-macs.el +++ b/org-roam-macs.el @@ -52,6 +52,23 @@ (nconc new-lst (list separator it))) new-lst))) +(defmacro org-roam-with-file (file keep-file-p &rest body) + "Execute BODY within FILE. +If KEEP-FILE-P or FILE is already visited, do not kill the +buffer." + (declare (indent 2) (debug t)) + `(let* ((existing-buf (find-buffer-visiting ,file)) + (buf (or existing-buf (find-file-noselect ,file))) + (keep-buf-p (or existing-buf ,keep-file-p)) + res) + (with-current-buffer buf + (setq res (progn ,@body)) + (unless keep-buf-p + (save-buffer))) + (unless (and keep-buf-p (find-buffer-visiting ,file)) + (kill-buffer (find-buffer-visiting ,file))) + res)) + (defmacro org-roam--with-temp-buffer (file &rest body) "Execute BODY within a temp buffer. Like `with-temp-buffer', but propagates `org-roam-directory'. diff --git a/org-roam.el b/org-roam.el index c3b9c5a..04c2642 100644 --- a/org-roam.el +++ b/org-roam.el @@ -1079,12 +1079,8 @@ When STRICT is non-nil, only consider Org-roam’s database. When KEEP-BUFFER-P is non-nil, keep the buffers navigated by Org-roam open." (let ((file (org-roam-id-get-file id strict))) (when file - (let ((existing-buf (find-buffer-visiting file)) - (res (org-id-find-id-in-file id file markerp))) - (when (and (not keep-buffer-p) - (not existing-buf)) - (kill-buffer (find-buffer-visiting file))) - res)))) + (org-roam-with-file file keep-buffer-p + (org-id-find-id-in-file id file markerp))))) (defun org-roam-id-open (id-or-marker &optional strict) "Go to the entry with ID-OR-MARKER. @@ -1382,10 +1378,8 @@ To be added to `org-roam-title-change-hook'." :where (= dest $s1)] current-path))) (dolist (file files-affected) - (with-current-buffer (or (find-buffer-visiting (car file)) - (find-file-noselect (car file))) - (org-roam--replace-link current-path current-path old-title new-title) - (save-buffer))))) + (org-roam-with-file (car file) nil + (org-roam--replace-link current-path current-path old-title new-title))))) (defun org-roam--update-file-name-on-title-change (old-title new-title) "Update the file name on title change. @@ -1414,8 +1408,7 @@ When NEW-FILE-OR-DIR is a directory, we use it to compute the new file path." (let ((new-file (if (directory-name-p new-file-or-dir) (expand-file-name (file-name-nondirectory old-file) new-file-or-dir) new-file-or-dir)) - files-affected - new-buffer) + files-affected) (setq new-file (expand-file-name new-file)) (setq old-file (expand-file-name old-file)) (when (and (not (auto-save-file-name-p old-file)) @@ -1424,8 +1417,6 @@ When NEW-FILE-OR-DIR is a directory, we use it to compute the new file path." (not (backup-file-name-p new-file)) (org-roam--org-roam-file-p old-file)) (org-roam-db--ensure-built) - (setq new-buffer (or (find-buffer-visiting new-file) - (find-file-noselect new-file))) (setq files-affected (org-roam-db-query [:select :distinct [source] :from links :where (= dest $s1)] @@ -1437,8 +1428,7 @@ When NEW-FILE-OR-DIR is a directory, we use it to compute the new file path." (setq file (if (string-equal (car file) old-file) new-file (car file))) - (with-current-buffer (or (find-buffer-visiting file) - (find-file-noselect file)) + (org-roam-with-file file nil (org-roam--replace-link old-file new-file) (save-buffer) (org-roam-db--update-file))) @@ -1447,9 +1437,8 @@ When NEW-FILE-OR-DIR is a directory, we use it to compute the new file path." ;; will break. Fix all file-relative links: (unless (string= (file-name-directory old-file) (file-name-directory new-file)) - (with-current-buffer new-buffer - (org-roam--fix-relative-links old-file) - (save-buffer))) + (org-roam-with-file new-file nil + (org-roam--fix-relative-links old-file))) (when (org-roam--org-roam-file-p new-file) (org-roam-db--update-file new-file)))))