Subject: List project buffers

From: Juri Linkov <[email-masked]>
Date: Wed, 09 Nov 2022 19:40:02 +0200

Version: 29.0.50
Severity: wishlist
Tags: patch

There are project commands and keys corresponding
to the most useful buffer commands:

  C-x k (kill-buffer)      - C-x p k (project-kill-buffers)
  C-x b (switch-to-buffer) - C-x p b (project-switch-to-buffer)

What is still missing is a command to list project buffers:

  C-x C-b (list-buffers)   - C-x p C-b (project-list-buffers)

project-kill-buffers already shows a list of project buffers,
but only as a part of confirmation for killing buffers.

Here is a patch that adds the missing command.  Its argument
has exactly the same meaning as in 'list-buffers'.

diff --git a/lisp/progmodes/project.el b/lisp/progmodes/project.el
index fc035675cec..e6f9dd750e7 100644
--- a/lisp/progmodes/project.el
+++ b/lisp/progmodes/project.el
@@ -713,6 +713,7 @@ project-prefix-map
     (define-key map "G" 'project-or-external-find-regexp)
     (define-key map "r" 'project-query-replace-regexp)
     (define-key map "x" 'project-execute-extended-command)
+    (define-key map "\C-b" 'project-list-buffers)
     map)
   "Keymap for project commands.")
 
@@ -1223,6 +1224,25 @@ project-display-buffer-other-frame
   (interactive (list (project--read-project-buffer)))
   (display-buffer-other-frame buffer-or-name))
 
+;;;###autoload
+(defun project-list-buffers (&optional arg)
+  "Display a list of project buffers.
+The list is displayed in a buffer named \"*Buffer List*\".
+
+By default, all buffers are listed except those whose names start
+with a space (which are for internal use).  With prefix argument
+ARG, show only buffers that are visiting files."
+  (interactive "P")
+  (let* ((pr (project-current t))
+         (bufs (mapcan
+                (lambda (buf)
+                  (when (and (project--buffer-check buf '("\\`[^ ]"))
+                             (or (not arg)
+                                 (project--buffer-check buf '(buffer-file-name))))
+                    (list buf)))
+                (project-buffers pr))))
+    (display-buffer (list-buffers-noselect arg bufs))))
+
 (defcustom project-kill-buffer-conditions
   '(buffer-file-name    ; All file-visiting buffers are included.
     ;; Most of temp and logging buffers (aside from hidden ones):
@@ -1283,6 +1303,7 @@ project-kill-buffers-display-buffer-list
   :package-version '(project . "0.8.2")
   :safe #'booleanp)
 
+;; UNUSED?
 (defun project--buffer-list (pr)
   "Return the list of all buffers in project PR."
   (let ((conn (file-remote-p (project-root pr)))