Subject: bug#71290: Options for viewing project buffers

From: mikpom <[email-masked]>
Date: Wed, 05 Jun 2024 11:00:53 +0700

So I implemented (thank  for a suggestion in Emacs-ru chat) optional
Ibuffer viewer. Diff on project.el is attached. It works in my setting.

Best wishes, 
-Mikhail

On Sat, 2024-06-01 at 23:07 +0700, mikpom wrote:
> Thanks for pointing to ibuffer-project. It will deliver on my request
> even if suggested feature is never implemented.
> 
> On Sat, 2024-06-01 at 18:27 +0300, Dmitry Gutov wrote:
> > Hi!
> > 
> > On 31/05/2024 15:28, mikpom wrote:
> > > Greetings devs and emacs community.
> > > 
> > > I occasionally need to list buffers of a project and do so with
> > > the
> > > built-in command project-list-buffers from project.el. The
> > > problem
> > > is
> > > that this command utilizes Buffer-menu mode and does so in a
> > > hard-
> > > coded
> > > way. Personally I prefer IBuffer which is already heavily
> > > customized
> > > while I have zero knowledge of Buffer-menu mode.
> > > 
> > > I suggest to make an option whether project.el will list buffers
> > > with
> > > Ibuffer, Buffer-menu or even something else?
> > 
> > I'm happy to add a new option, but if you look at the definition of
> > project-list-buffers, it's not as trivial as
> > 
> >   - bind default-directory
> >   - call function
> > 
> > And ibuffer seems to require some effort as well. As a consequence,
> > the 
> > option is unlikely to be infinitely extensible, it would just list 
> > 'list-buffers' and 'ibuffers' as possible values. Maybe a "Custom 
> > function" alternative too, but the users who would want to use it
> > will 
> > need to put it some work as well.
> > 
> > Speaking of ibuffer in particular, here's a hint of setup required
> > to
> > use it for project buffers: 
> > https://github.com/emacsmirror/ibuffer-project/blob/master/ibuffer-project.el
> > 
> > Of course, a lot of this could be optional (improvements to make
> > the
> > UI 
> > better), so if someone wanted to contribute a minimal
> > implementation,
> > we 
> > could certainly use it (just please don't copy the code from the
> > above, 
> > it's not copyright-assigned).
> 

diff --git a/project.el b/project.el
index b671a08..9021779 100644
--- a/project.el
+++ b/project.el
@@ -1354,6 +1354,11 @@ displayed."
   (interactive (list (project--read-project-buffer)))
   (display-buffer-other-frame buffer-or-name))
 
+(defcustom project-buffers-viewer 'project-list-buffers-buffer-menu "Function used to list buffers of a project."
+  :group 'project
+  :type '(radio (function-item project-list-buffers-buffer-menu)
+                (function-item project-list-buffers-ibuffer)))
+
 ;;;###autoload
 (defun project-list-buffers (&optional arg)
   "Display a list of project buffers.
@@ -1363,33 +1368,8 @@ By default, all project 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))
-         (buffer-list-function
-          (lambda ()
-            (seq-filter
-             (lambda (buffer)
-               (let ((name (buffer-name buffer))
-                     (file (buffer-file-name buffer)))
-                 (and (or (not (string= (substring name 0 1) " "))
-                          file)
-                      (not (eq buffer (current-buffer)))
-                      (or file (not Buffer-menu-files-only)))))
-             (project-buffers pr)))))
-    (display-buffer
-     (if (version< emacs-version "29.0.50")
-         (let ((buf (list-buffers-noselect
-                     arg (with-current-buffer
-                             (get-buffer-create "*Buffer List*")
-                           (let ((Buffer-menu-files-only arg))
-                             (funcall buffer-list-function))))))
-           (with-current-buffer buf
-             (setq-local revert-buffer-function
-                         (lambda (&rest _ignored)
-                           (list-buffers--refresh
-                            (funcall buffer-list-function))
-                           (tabulated-list-print t))))
-           buf)
-       (list-buffers-noselect arg buffer-list-function)))))
+  (let ((pr (project-current t)))
+    (funcall project-buffers-viewer pr arg)))
 
 (defcustom project-kill-buffer-conditions
   '(buffer-file-name    ; All file-visiting buffers are included.
@@ -1830,5 +1810,41 @@ to directory DIR."
     (let ((project-current-directory-override dir))
       (call-interactively command))))
 
+(defun project-list-buffers-buffer-menu (project &optional files-only)
+  "Lists buffers of a project in Buffer-menu mode"
+  (let ((buffer-list-function
+          (lambda ()
+            (seq-filter
+             (lambda (buffer)
+               (let ((name (buffer-name buffer))
+                     (file (buffer-file-name buffer)))
+                 (and (or (not (string= (substring name 0 1) " "))
+                          file)
+                      (not (eq buffer (current-buffer)))
+                      (or file (not Buffer-menu-files-only)))))
+             (project-buffers project)))))
+    (display-buffer
+     (if (version< emacs-version "29.0.50")
+         (let ((buf (list-buffers-noselect
+                     arg (with-current-buffer
+                             (get-buffer-create "*Buffer List*")
+                           (let ((Buffer-menu-files-only arg))
+                             (funcall buffer-list-function))))))
+           (with-current-buffer buf
+             (setq-local revert-buffer-function
+                         (lambda (&rest _ignored)
+                           (list-buffers--refresh
+                            (funcall buffer-list-function))
+                           (tabulated-list-print t))))
+           buf)
+       (list-buffers-noselect files-only buffer-list-function)))))
+
+(defun project-list-buffers-ibuffer (project &optional files-only)
+  "Lists buffers of a project with Ibuffer"
+  ;; TODO files-only
+  (ibuffer t (format "*Ibuffer-%s*" (project-name project))
+           `((predicate . (member (current-buffer)
+                                  (project-buffers ',project))))))
+
 (provide 'project)
 ;;; project.el ends here