浏览代码

id: Close unnecessary buffers after having looked for an ID

* lisp/org-id.el (org-id-find-id-in-file): When the file was not being
visited, and the caller didn't request a marker pointing to the ID
location, close the newly created buffer.

Reported-by: this nkk <thisnkk@gmail.com>
<http://lists.gnu.org/r/emacs-orgmode/2020-09/msg01018.html>
Nicolas Goaziou 5 年之前
父节点
当前提交
41651f4ed0
共有 1 个文件被更改,包括 17 次插入10 次删除
  1. 17 10
      lisp/org-id.el

+ 17 - 10
lisp/org-id.el

@@ -661,20 +661,27 @@ When FILES is given, scan also these files."
 
 
 (defun org-id-find-id-in-file (id file &optional markerp)
 (defun org-id-find-id-in-file (id file &optional markerp)
   "Return the position of the entry ID in FILE.
   "Return the position of the entry ID in FILE.
+
 If that files does not exist, or if it does not contain this ID,
 If that files does not exist, or if it does not contain this ID,
 return nil.
 return nil.
+
 The position is returned as a cons cell (file-name . position).  With
 The position is returned as a cons cell (file-name . position).  With
 optional argument MARKERP, return the position as a new marker."
 optional argument MARKERP, return the position as a new marker."
-  (let (org-agenda-new-buffers buf pos)
-    (cond
-     ((not file) nil)
-     ((not (file-exists-p file)) nil)
-     (t (with-current-buffer (setq buf (org-get-agenda-file-buffer file))
-	  (setq pos (org-find-entry-with-id id))
-	  (when pos
-	    (if markerp
-		(move-marker (make-marker) pos buf)
-	      (cons file pos))))))))
+  (cond
+   ((not file) nil)
+   ((not (file-exists-p file)) nil)
+   (t
+    (let* ((visiting (find-buffer-visiting file))
+	   (buffer (or visiting (find-file-noselect file))))
+      (unwind-protect
+	  (with-current-buffer buffer
+	    (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)))))))
 
 
 ;; id link type
 ;; id link type