After using various VC diffing commands in a repo containing org-mode
files, org-switchb's completions become polluted with temp buffers
whose names begin with a space:

" 
*diff-syntax:/Users/aaron/todo/main/todo.org.~757282798a587edb8559306294fac7af4588f936~*"

The attached patches change org-switchb to exclude temp buffers, and
also change org-buffer-list to check for temp buffers by looking for a
leading space in the buffer name, as is conventional.  (After a
cursory search, the only place I saw in org-mode itself that
constructs a buffer whose name includes the string "tmp" also includes
a leading space anyway, so no behavior regression is expected.)

Furthermore, org-buffer-list now excludes non-file-visiting buffers
when PREDICATE is `files', to match the docstring more accurately.

Thanks,
Aaron Zeng

>From 3f012fdd92a2dc8b709614ccb31334fe40be358d Mon Sep 17 00:00:00 2001
From: "Aaron L. Zeng" <[email protected]>
Date: Wed, 17 Jun 2026 15:11:54 -0400
Subject: [PATCH 1/2] org-switchb: Exclude *diff-syntax* temporary
 fontification buffers

* lisp/org-macs.el (org-buffer-list): Treat all buffers whose
names begin with a space as temporary buffers.

* lisp/org.el (org-switchb): Exclude temporary buffers.
---
 lisp/org-macs.el | 4 ++--
 lisp/org.el      | 3 ++-
 2 files changed, 4 insertions(+), 3 deletions(-)

diff --git a/lisp/org-macs.el b/lisp/org-macs.el
index 035e5dded..1b2c2b4e8 100644
--- a/lisp/org-macs.el
+++ b/lisp/org-macs.el
@@ -390,10 +390,10 @@ If EXCLUDE-TMP is non-nil, ignore temporary buffers."
 					      (buffer-name b)))))))))
     (delq nil
 	  (mapcar
-	   (lambda(b)
+	   (lambda (b)
 	     (if (and (funcall filter b)
 		      (or (not exclude-tmp)
-			  (not (string-match "tmp" (buffer-name b)))))
+			  (/= ?\s (aref (buffer-name b) 0))))
 		 b
 	       nil))
 	   (buffer-list)))))
diff --git a/lisp/org.el b/lisp/org.el
index 04eed3088..098895246 100644
--- a/lisp/org.el
+++ b/lisp/org.el
@@ -16057,7 +16057,8 @@ prefix, restrict available buffers to agenda files."
   (interactive "P")
   (let ((blist (org-buffer-list
 		(cond ((equal arg '(4))  'files)
-		      ((equal arg '(16)) 'agenda)))))
+		      ((equal arg '(16)) 'agenda))
+                t)))
     (pop-to-buffer-same-window
      (completing-read "Org buffer: "
 		      (mapcar #'list (mapcar #'buffer-name blist))
-- 
2.54.0

>From 97a9d9101f8a9096f205fc084745c456c6be55ac Mon Sep 17 00:00:00 2001
From: "Aaron L. Zeng" <[email protected]>
Date: Wed, 17 Jun 2026 15:13:41 -0400
Subject: [PATCH 2/2] org-buffer-list: Restrict "files" predicate to
 file-visiting buffers

* lisp/org-macs.el (org-buffer-list): When PREDICATE is
`files', ensure the buffer is visiting a file.
---
 lisp/org-macs.el | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/lisp/org-macs.el b/lisp/org-macs.el
index 1b2c2b4e8..2b867818b 100644
--- a/lisp/org-macs.el
+++ b/lisp/org-macs.el
@@ -375,7 +375,10 @@ If EXCLUDE-TMP is non-nil, ignore temporary buffers."
 	 (filter
 	  (cond
 	   ((eq predicate 'files)
-	    (lambda (b) (with-current-buffer b (derived-mode-p 'org-mode))))
+	    (lambda (b)
+              (with-current-buffer b
+                (and buffer-file-name
+                     (derived-mode-p 'org-mode)))))
 	   ((eq predicate 'export)
 	    (lambda (b) (string-match "\\*Org .*Export" (buffer-name b))))
 	   ((eq predicate 'agenda)
-- 
2.54.0

Reply via email to