org-id-find-id-in-file: Prefer using temporary buffer

* lisp/org-id.el (org-id-find-id-in-file): Use a temporary throwaway
Org buffer to lookup IDs when we do not need to keep the buffer.  This
speeds up updating IDs.
This commit is contained in:
Ihor Radchenko 2023-08-17 12:48:52 +03:00
parent cc435cba71
commit 4cf0b0433d
No known key found for this signature in database
GPG Key ID: 6470762A7DA11D8B
1 changed files with 10 additions and 3 deletions

View File

@ -675,16 +675,23 @@ optional argument MARKERP, return the position as a new marker."
((not (file-exists-p file)) nil)
(t
(let* ((visiting (find-buffer-visiting file))
(buffer (or visiting (find-file-noselect file))))
(buffer (or visiting
(if markerp (find-file-noselect file)
(get-buffer-create " *Org ID temp*" t)))))
(unwind-protect
(with-current-buffer buffer
(unless (derived-mode-p 'org-mode) (org-mode))
(unless (or visiting markerp)
(buffer-disable-undo)
(insert-file-contents file nil nil nil 'replace))
(let ((pos (org-find-entry-with-id id)))
(cond
((null pos) nil)
(markerp (move-marker (make-marker) pos buffer))
(t (cons file pos)))))
;; Remove opened buffer in the process.
(unless (or visiting markerp) (kill-buffer buffer)))))))
;; Clean temporarily buffer if we don't need to keep it.
(unless (or visiting markerp)
(with-current-buffer buffer (erase-buffer))))))))
;; id link type