mirror of
https://github.com/org-roam/org-roam
synced 2025-08-01 12:17:21 -05:00
(fix): close files intelligently after modifying (#1330)
Co-authored-by: Jethro Kuan <jethrokuan95@gmail.com>
This commit is contained in:
@ -94,15 +94,11 @@ noabbrev Absolute path, no abbreviation of home directory."
|
|||||||
If FILE, return outline headings for passed FILE instead.
|
If FILE, return outline headings for passed FILE instead.
|
||||||
If WITH-MARKER, return a cons cell of (headline . marker).
|
If WITH-MARKER, return a cons cell of (headline . marker).
|
||||||
If USE-STACK, include the parent paths as well."
|
If USE-STACK, include the parent paths as well."
|
||||||
(let* ((buf (or (and file
|
(org-roam-with-file file 'keep
|
||||||
(or (find-buffer-visiting file)
|
(let* ((outline-level-fn outline-level)
|
||||||
(find-file-noselect file)))
|
(path-separator "/")
|
||||||
(current-buffer)))
|
(stack-level 0)
|
||||||
(outline-level-fn outline-level)
|
stack cands name level marker)
|
||||||
(path-separator "/")
|
|
||||||
(stack-level 0)
|
|
||||||
stack cands name level marker)
|
|
||||||
(with-current-buffer buf
|
|
||||||
(save-excursion
|
(save-excursion
|
||||||
(goto-char (point-min))
|
(goto-char (point-min))
|
||||||
(while (re-search-forward org-complex-heading-regexp nil t)
|
(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)))
|
path-separator)))
|
||||||
(push (if with-marker
|
(push (if with-marker
|
||||||
(cons name marker)
|
(cons name marker)
|
||||||
name) cands)))))
|
name) cands))))
|
||||||
(nreverse cands)))
|
(nreverse cands))))
|
||||||
|
|
||||||
(defun org-roam-link--get-file-from-title (title &optional no-interactive)
|
(defun org-roam-link--get-file-from-title (title &optional no-interactive)
|
||||||
"Return the file path corresponding to TITLE.
|
"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 FILE, get headline from FILE instead.
|
||||||
If there is no corresponding headline, return nil."
|
If there is no corresponding headline, return nil."
|
||||||
(save-excursion
|
(save-excursion
|
||||||
(with-current-buffer (or (and file
|
(org-roam-with-file file 'keep
|
||||||
(or (find-buffer-visiting file)
|
|
||||||
(find-file-noselect file)))
|
|
||||||
(current-buffer))
|
|
||||||
(let ((headlines (org-roam-link--get-headlines file 'with-markers)))
|
(let ((headlines (org-roam-link--get-headlines file 'with-markers)))
|
||||||
(when-let ((marker (cdr (assoc-string headline headlines))))
|
(when-let ((marker (cdr (assoc-string headline headlines))))
|
||||||
(goto-char marker)
|
(goto-char marker)
|
||||||
|
@ -52,6 +52,23 @@
|
|||||||
(nconc new-lst (list separator it)))
|
(nconc new-lst (list separator it)))
|
||||||
new-lst)))
|
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)
|
(defmacro org-roam--with-temp-buffer (file &rest body)
|
||||||
"Execute BODY within a temp buffer.
|
"Execute BODY within a temp buffer.
|
||||||
Like `with-temp-buffer', but propagates `org-roam-directory'.
|
Like `with-temp-buffer', but propagates `org-roam-directory'.
|
||||||
|
27
org-roam.el
27
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."
|
When KEEP-BUFFER-P is non-nil, keep the buffers navigated by Org-roam open."
|
||||||
(let ((file (org-roam-id-get-file id strict)))
|
(let ((file (org-roam-id-get-file id strict)))
|
||||||
(when file
|
(when file
|
||||||
(let ((existing-buf (find-buffer-visiting file))
|
(org-roam-with-file file keep-buffer-p
|
||||||
(res (org-id-find-id-in-file id file markerp)))
|
(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))))
|
|
||||||
|
|
||||||
(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.
|
||||||
@ -1382,10 +1378,8 @@ To be added to `org-roam-title-change-hook'."
|
|||||||
:where (= dest $s1)]
|
:where (= dest $s1)]
|
||||||
current-path)))
|
current-path)))
|
||||||
(dolist (file files-affected)
|
(dolist (file files-affected)
|
||||||
(with-current-buffer (or (find-buffer-visiting (car file))
|
(org-roam-with-file (car file) nil
|
||||||
(find-file-noselect (car file)))
|
(org-roam--replace-link current-path current-path old-title new-title)))))
|
||||||
(org-roam--replace-link current-path current-path old-title new-title)
|
|
||||||
(save-buffer)))))
|
|
||||||
|
|
||||||
(defun org-roam--update-file-name-on-title-change (old-title new-title)
|
(defun org-roam--update-file-name-on-title-change (old-title new-title)
|
||||||
"Update the file name on title change.
|
"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)
|
(let ((new-file (if (directory-name-p new-file-or-dir)
|
||||||
(expand-file-name (file-name-nondirectory old-file) new-file-or-dir)
|
(expand-file-name (file-name-nondirectory old-file) new-file-or-dir)
|
||||||
new-file-or-dir))
|
new-file-or-dir))
|
||||||
files-affected
|
files-affected)
|
||||||
new-buffer)
|
|
||||||
(setq new-file (expand-file-name new-file))
|
(setq new-file (expand-file-name new-file))
|
||||||
(setq old-file (expand-file-name old-file))
|
(setq old-file (expand-file-name old-file))
|
||||||
(when (and (not (auto-save-file-name-p 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))
|
(not (backup-file-name-p new-file))
|
||||||
(org-roam--org-roam-file-p old-file))
|
(org-roam--org-roam-file-p old-file))
|
||||||
(org-roam-db--ensure-built)
|
(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]
|
(setq files-affected (org-roam-db-query [:select :distinct [source]
|
||||||
:from links
|
:from links
|
||||||
:where (= dest $s1)]
|
: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)
|
(setq file (if (string-equal (car file) old-file)
|
||||||
new-file
|
new-file
|
||||||
(car file)))
|
(car file)))
|
||||||
(with-current-buffer (or (find-buffer-visiting file)
|
(org-roam-with-file file nil
|
||||||
(find-file-noselect file))
|
|
||||||
(org-roam--replace-link old-file new-file)
|
(org-roam--replace-link old-file new-file)
|
||||||
(save-buffer)
|
(save-buffer)
|
||||||
(org-roam-db--update-file)))
|
(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:
|
;; will break. Fix all file-relative links:
|
||||||
(unless (string= (file-name-directory old-file)
|
(unless (string= (file-name-directory old-file)
|
||||||
(file-name-directory new-file))
|
(file-name-directory new-file))
|
||||||
(with-current-buffer new-buffer
|
(org-roam-with-file new-file nil
|
||||||
(org-roam--fix-relative-links old-file)
|
(org-roam--fix-relative-links old-file)))
|
||||||
(save-buffer)))
|
|
||||||
(when (org-roam--org-roam-file-p new-file)
|
(when (org-roam--org-roam-file-p new-file)
|
||||||
(org-roam-db--update-file new-file)))))
|
(org-roam-db--update-file new-file)))))
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user