From: Bastien <bzg@gnu.org>
To: emacs-devel@gnu.org
Subject: [PATCH] Filter list-buffers by a regexp (interactively)
Date: Mon, 28 Jul 2014 19:32:08 +0200 [thread overview]
Message-ID: <87oaw9fkyv.fsf@bzg.ath.cx> (raw)
[-- Attachment #1: Type: text/plain, Size: 283 bytes --]
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!
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: buff-menu.el.patch --]
[-- Type: text/x-diff, Size: 8321 bytes --]
=== 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)
\f
***************
*** 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))
[-- Attachment #3: Type: text/plain, Size: 14 bytes --]
--
Bastien
next reply other threads:[~2014-07-28 17:32 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-07-28 17:32 Bastien [this message]
2014-08-04 11:24 ` [PATCH] Filter list-buffers by a regexp (interactively) Bastien
2014-08-06 16:52 ` Stefan Monnier
2014-08-07 6:53 ` Bastien
2014-08-07 11:17 ` Stefan Monnier
2014-08-07 15:28 ` Herring, Davis
2014-08-08 23:41 ` Juri Linkov
2014-08-09 7:15 ` Bastien
2014-08-09 13:00 ` Stefan Monnier
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.gnu.org/software/emacs/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=87oaw9fkyv.fsf@bzg.ath.cx \
--to=bzg@gnu.org \
--cc=emacs-devel@gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://git.savannah.gnu.org/cgit/emacs.git
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).