emacs-devel
[Top][All Lists]
Advanced

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

[PATCH] Filter list-buffers by a regexp (interactively)


From: Bastien
Subject: [PATCH] Filter list-buffers by a regexp (interactively)
Date: Mon, 28 Jul 2014 19:32:08 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.4.50 (gnu/linux)

The patch below allows to use C-u C-u C-x C-b to interactively enter
a regular expression to only list buffers with a matching name.

The need came up on Org-mode when people have many agenda buffers and
want to only list those.

Any comment and/or improvement appreciated, thanks!

=== modified file 'lisp/buff-menu.el'
*** lisp/buff-menu.el   2014-07-21 05:38:17 +0000
--- lisp/buff-menu.el   2014-07-28 13:12:57 +0000
***************
*** 127,132 ****
--- 127,133 ----
      (define-key map "b" 'Buffer-menu-bury)
      (define-key map "V" 'Buffer-menu-view)
      (define-key map "T" 'Buffer-menu-toggle-files-only)
+     (define-key map "*" 'Buffer-menu-filter-by-regexp)
      (define-key map (kbd "M-s a C-s")   'Buffer-menu-isearch-buffers)
      (define-key map (kbd "M-s a M-C-s") 'Buffer-menu-isearch-buffers-regexp)
      (define-key map (kbd "M-s a C-o") 'Buffer-menu-multi-occur)
***************
*** 146,151 ****
--- 147,155 ----
        '(menu-item "Show Only File Buffers" Buffer-menu-toggle-files-only
                  :button (:toggle . Buffer-menu-files-only)
                  :help "Toggle whether the current buffer-menu displays only 
file buffers"))
+     (bindings--define-key menu-map [tr]
+       '(menu-item "Filter List by Regexp" Buffer-menu-filter-by-regexp
+                 :help "Filter the list of buffers by a regular expression"))
      (bindings--define-key menu-map [s1] menu-bar-separator)
      ;; FIXME: The "Select" entries could use better names...
      (bindings--define-key menu-map [sel]
***************
*** 243,258 ****
  \\[Buffer-menu-toggle-read-only]    Toggle read-only status of buffer on this 
line.
  \\[revert-buffer]    Update the list of buffers.
  \\[Buffer-menu-toggle-files-only]    Toggle whether the menu displays only 
file buffers.
  \\[Buffer-menu-bury]    Bury the buffer listed on this line."
    (set (make-local-variable 'buffer-stale-function)
         (lambda (&optional _noconfirm) 'fast))
    (add-hook 'tabulated-list-revert-hook 'list-buffers--refresh nil t))
  
! (defun buffer-menu (&optional arg)
    "Switch to the Buffer Menu.
  By default, the Buffer Menu lists all buffers except those whose
  names start with a space (which are for internal use).  With
! prefix argument ARG, show only buffers that are visiting files.
  
  In the Buffer Menu, the first column (denoted \"C\") shows \".\"
  for the buffer from which you came, \">\" for buffers you mark to
--- 247,267 ----
  \\[Buffer-menu-toggle-read-only]    Toggle read-only status of buffer on this 
line.
  \\[revert-buffer]    Update the list of buffers.
  \\[Buffer-menu-toggle-files-only]    Toggle whether the menu displays only 
file buffers.
+ \\[Buffer-menu-filter-by-regexp]     Prompt for a regexp and display matching 
buffers.
  \\[Buffer-menu-bury]    Bury the buffer listed on this line."
    (set (make-local-variable 'buffer-stale-function)
         (lambda (&optional _noconfirm) 'fast))
    (add-hook 'tabulated-list-revert-hook 'list-buffers--refresh nil t))
  
! (defun buffer-menu (&optional arg regexp)
    "Switch to the Buffer Menu.
  By default, the Buffer Menu lists all buffers except those whose
  names start with a space (which are for internal use).  With
! a prefix argument ARG, show only buffers that are visiting files.
! With two universal prefix argument, prompt for a regular expression
! to only display buffers with a matching name.
! 
! When REGEXP is non-nil, only list buffers matching the regexp.
  
  In the Buffer Menu, the first column (denoted \"C\") shows \".\"
  for the buffer from which you came, \">\" for buffers you mark to
***************
*** 268,300 ****
  See `Buffer-menu-mode' for the keybindings available the Buffer
  Menu."
    (interactive "P")
!   (switch-to-buffer (list-buffers-noselect arg))
!   (message
!    "Commands: d, s, x, u; f, o, 1, 2, m, v; ~, %%; q to quit; ? for help."))
  
! (defun buffer-menu-other-window (&optional arg)
    "Display the Buffer Menu in another window.
  See `buffer-menu' for a description of the Buffer Menu.
  
  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")
!   (switch-to-buffer-other-window (list-buffers-noselect arg))
!   (message
!    "Commands: d, s, x, u; f, o, 1, 2, m, v; ~, %%; q to quit; ? for help."))
  
  ;;;###autoload
! (defun list-buffers (&optional arg)
    "Display a list of existing buffers.
  The list is displayed in a buffer named \"*Buffer List*\".
  See `buffer-menu' for a description of the Buffer Menu.
  
  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")
!   (display-buffer (list-buffers-noselect arg)))
  
  (defun Buffer-menu-toggle-files-only (arg)
    "Toggle whether the current buffer-menu displays only file buffers.
--- 277,353 ----
  See `Buffer-menu-mode' for the keybindings available the Buffer
  Menu."
    (interactive "P")
!   (let ((re-filter
!        (or regexp
!            (and (equal arg '(16))
!                 (read-from-minibuffer "Regexp filter: ")))))
!     (switch-to-buffer
!      (list-buffers-noselect
!       arg
!       (if re-filter
!         (delq
!          nil
!          (mapcar (lambda (b)
!                    (if (string-match re-filter (buffer-name b)) b))
!                  (buffer-list)))))
!      (message
!       "Commands: d, s, x, u; f, o, 1, 2, m, v; ~, %%; q to quit; ? for 
help."))))
  
! (defun buffer-menu-other-window (&optional arg regexp)
    "Display the Buffer Menu in another window.
  See `buffer-menu' for a description of the Buffer Menu.
  
  By default, all buffers are listed except those whose names start
! with a space (which are for internal use).  With a single prefix
! argument ARG, show only buffers that are visiting files.  With two
! universal prefix argument, prompt for a regular expression to only
! display buffers with a matching name.
! 
! When REGEXP is non-nil, only list buffers matching the regexp."
    (interactive "P")
!   (let ((re-filter
!        (or regexp
!            (and (equal arg '(16))
!                 (read-from-minibuffer "Regexp filter: ")))))
!     (switch-to-buffer-other-window
!      (list-buffers-noselect
!       arg
!       (if re-filter
!         (delq
!          nil
!          (mapcar (lambda (b)
!                    (if (string-match re-filter (buffer-name b)) b))
!                  (buffer-list))))))
!     (message
!      "Commands: d, s, x, u; f, o, 1, 2, m, v; ~, %%; q to quit; ? for 
help.")))
  
  ;;;###autoload
! (defun list-buffers (&optional arg regexp)
    "Display a list of existing buffers.
  The list is displayed in a buffer named \"*Buffer List*\".
  See `buffer-menu' for a description of the Buffer Menu.
  
  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.  With two universal
! prefix argument, prompt for a regular expression to only display
! buffers with a matching name.
! 
! When REGEXP is non-nil, only list buffers matching the regexp."
    (interactive "P")
!   (let ((re-filter
!        (or regexp
!            (and (equal arg '(16))
!                 (read-from-minibuffer "Regexp filter: ")))))
!     (display-buffer
!      (list-buffers-noselect
!       arg
!       (if re-filter
!         (delq
!          nil
!          (mapcar (lambda (b)
!                    (if (string-match re-filter (buffer-name b)) b))
!                  (buffer-list))))))))
  
  (defun Buffer-menu-toggle-files-only (arg)
    "Toggle whether the current buffer-menu displays only file buffers.
***************
*** 309,314 ****
--- 362,373 ----
             "Showing all non-internal buffers."))
    (revert-buffer))
  
+ (defun Buffer-menu-filter-by-regexp ()
+   "Limit the buffer list to buffers which name matches a regexp."
+   (interactive)
+   (list-buffers '(16))
+   (message "Hit `g' to remove the filter"))
+ 
  (defalias 'Buffer-menu-sort 'tabulated-list-sort)
  
  
***************
*** 588,594 ****
      (with-current-buffer buffer
        (Buffer-menu-mode)
        (setq Buffer-menu-files-only
!           (and files-only (>= (prefix-numeric-value files-only) 0)))
        (list-buffers--refresh buffer-list old-buffer)
        (tabulated-list-print))
      buffer))
--- 647,654 ----
      (with-current-buffer buffer
        (Buffer-menu-mode)
        (setq Buffer-menu-files-only
!           (and (equal '(4) files-only)
!                (>= (prefix-numeric-value files-only) 0)))
        (list-buffers--refresh buffer-list old-buffer)
        (tabulated-list-print))
      buffer))

-- 
 Bastien

reply via email to

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