bug-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

bug#59153: List project buffers


From: Juri Linkov
Subject: bug#59153: List project buffers
Date: Thu, 10 Nov 2022 19:59:56 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/29.0.50 (x86_64-pc-linux-gnu)

>>> +         (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))))
>>
>> This won't survive a revert buffer, I think.  (I'll show all buffers
>> then.)
>
> This means that all uses of the BUFFER-LIST arg of 'list-buffers-noselect'
> are flawed.  So better would be to provide not a list of buffers, but
> a predicate to filter out a list of buffers.  Then the revert could use
> such a predicate from e.g. a buffer-local variable.

Here is a patch that adds a buffer-local variable similar to
Buffer-menu-files-only:

diff --git a/lisp/buff-menu.el b/lisp/buff-menu.el
index abf152f058c..fbf2bbc58bc 100644
--- a/lisp/buff-menu.el
+++ b/lisp/buff-menu.el
@@ -101,6 +101,13 @@ Buffer-menu-files-only
 This is set by the prefix argument to `buffer-menu' and related
 commands.")
 
+(defvar-local Buffer-menu-filter-predicate nil
+  "Function to filter out buffers in the buffer list.
+Buffers that don't satisfy the predicate will be skipped.
+The value should be a function of one argument; it will be
+called with the buffer.  If this function returns non-nil,
+then the buffer will be displayed in the buffer list.")
+
 (defvar-keymap Buffer-menu-mode-map
   :doc "Local keymap for `Buffer-menu-mode' buffers."
   :parent tabulated-list-mode-map
@@ -597,19 +604,23 @@ Buffer-menu-view-other-window
 ;;; Functions for populating the Buffer Menu.
 
 ;;;###autoload
-(defun list-buffers-noselect (&optional files-only buffer-list)
+(defun list-buffers-noselect (&optional files-only buffer-list 
filter-predicate)
   "Create and return a Buffer Menu buffer.
 This is called by `buffer-menu' and others as a subroutine.
 
 If FILES-ONLY is non-nil, show only file-visiting buffers.
 If BUFFER-LIST is non-nil, it should be a list of buffers; it
-means list those buffers and no others."
+means list those buffers and no others.
+If FILTER-PREDICATE is non-nil, it should be a function
+that filters out buffers from the list of buffers.
+See more at `Buffer-menu-filter-predicate'."
   (let ((old-buffer (current-buffer))
        (buffer (get-buffer-create "*Buffer List*")))
     (with-current-buffer buffer
       (Buffer-menu-mode)
       (setq Buffer-menu-files-only
            (and files-only (>= (prefix-numeric-value files-only) 0)))
+      (setq Buffer-menu-filter-predicate filter-predicate)
       (list-buffers--refresh buffer-list old-buffer)
       (tabulated-list-print))
     buffer))
@@ -631,6 +642,8 @@ list-buffers--refresh
         (marked-buffers (Buffer-menu-marked-buffers))
         (buffer-menu-buffer (current-buffer))
        (show-non-file (not Buffer-menu-files-only))
+        (filter-predicate (and (functionp Buffer-menu-filter-predicate)
+                               Buffer-menu-filter-predicate))
        entries name-width)
     ;; Collect info for each buffer we're interested in.
     (dolist (buffer (or buffer-list
@@ -644,7 +657,9 @@ list-buffers--refresh
                         (and (or (not (string= (substring name 0 1) " "))
                                   file)
                              (not (eq buffer buffer-menu-buffer))
-                             (or file show-non-file))))
+                             (or file show-non-file)
+                              (or (not filter-predicate)
+                                  (funcall filter-predicate buffer)))))
            (push (list buffer
                        (vector (cond
                                  ((eq buffer old-buffer) ".")
diff --git a/lisp/progmodes/project.el b/lisp/progmodes/project.el
index fc035675cec..917d80b2d3f 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,19 @@ 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 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))
+         (filter-predicate (lambda (buf) (memq buf (project-buffers pr)))))
+    (display-buffer (list-buffers-noselect arg nil filter-predicate))))
+
 (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 +1297,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)))

reply via email to

[Prev in Thread] Current Thread [Next in Thread]