unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* [PATCH] Filter list-buffers by a regexp (interactively)
@ 2014-07-28 17:32 Bastien
  2014-08-04 11:24 ` Bastien
  2014-08-06 16:52 ` Stefan Monnier
  0 siblings, 2 replies; 9+ messages in thread
From: Bastien @ 2014-07-28 17:32 UTC (permalink / raw)
  To: emacs-devel

[-- 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

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH] Filter list-buffers by a regexp (interactively)
  2014-07-28 17:32 [PATCH] Filter list-buffers by a regexp (interactively) Bastien
@ 2014-08-04 11:24 ` Bastien
  2014-08-06 16:52 ` Stefan Monnier
  1 sibling, 0 replies; 9+ messages in thread
From: Bastien @ 2014-08-04 11:24 UTC (permalink / raw)
  To: emacs-devel

Bastien <bzg@gnu.org> writes:

> 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!

I plan to commit this to trunk by the end of the month if there are
no comments/questions.

Thanks,

-- 
 Bastien



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH] Filter list-buffers by a regexp (interactively)
  2014-07-28 17:32 [PATCH] Filter list-buffers by a regexp (interactively) Bastien
  2014-08-04 11:24 ` Bastien
@ 2014-08-06 16:52 ` Stefan Monnier
  2014-08-07  6:53   ` Bastien
  1 sibling, 1 reply; 9+ messages in thread
From: Stefan Monnier @ 2014-08-06 16:52 UTC (permalink / raw)
  To: Bastien; +Cc: emacs-devel

> 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.

We generally try to avoid using things like C-u C-u.
IOW we should try to find some other way to provide this functionality.


        Stefan



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH] Filter list-buffers by a regexp (interactively)
  2014-08-06 16:52 ` Stefan Monnier
@ 2014-08-07  6:53   ` Bastien
  2014-08-07 11:17     ` Stefan Monnier
  0 siblings, 1 reply; 9+ messages in thread
From: Bastien @ 2014-08-07  6:53 UTC (permalink / raw)
  To: Stefan Monnier; +Cc: emacs-devel

Stefan Monnier <monnier@iro.umontreal.ca> writes:

>> 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.
>
> We generally try to avoid using things like C-u C-u.

Good to know.  (I don't dare looking at changes we need in Org to
follow this recommendation.  But I will.)

> IOW we should try to find some other way to provide this
> functionality.

Maybe adding a new command and bind it to C-x C-B?  Or perhaps
this is also not recommended?

Thanks for the feedback,

-- 
 Bastien



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH] Filter list-buffers by a regexp (interactively)
  2014-08-07  6:53   ` Bastien
@ 2014-08-07 11:17     ` Stefan Monnier
  2014-08-07 15:28       ` Herring, Davis
  0 siblings, 1 reply; 9+ messages in thread
From: Stefan Monnier @ 2014-08-07 11:17 UTC (permalink / raw)
  To: Bastien; +Cc: emacs-devel

>>> 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.
>> We generally try to avoid using things like C-u C-u.
> Good to know.  (I don't dare looking at changes we need in Org to
> follow this recommendation.  But I will.)

Org being its own program, it's not too terrible if it has its own set
of conventions.  But C-x C-b is a "core" command.

> Maybe adding a new command and bind it to C-x C-B?  Or perhaps
> this is also not recommended?

C-B is pretty hard to get to in some cases (e.g. ttys), so indeed, we
prefer not to do that.

But maybe we can change the meaning of C-u C-x C-b, or something like that.


        Stefan



^ permalink raw reply	[flat|nested] 9+ messages in thread

* RE: [PATCH] Filter list-buffers by a regexp (interactively)
  2014-08-07 11:17     ` Stefan Monnier
@ 2014-08-07 15:28       ` Herring, Davis
  2014-08-08 23:41         ` Juri Linkov
  0 siblings, 1 reply; 9+ messages in thread
From: Herring, Davis @ 2014-08-07 15:28 UTC (permalink / raw)
  To: Stefan Monnier, Bastien; +Cc: emacs-devel@gnu.org

> But maybe we can change the meaning of C-u C-x C-b, or something like that.

Since C-x C-b is fast to execute, and typing the regexp is already several keystrokes, what about providing a filter command within the buffer list?

Davis



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH] Filter list-buffers by a regexp (interactively)
  2014-08-07 15:28       ` Herring, Davis
@ 2014-08-08 23:41         ` Juri Linkov
  2014-08-09  7:15           ` Bastien
  0 siblings, 1 reply; 9+ messages in thread
From: Juri Linkov @ 2014-08-08 23:41 UTC (permalink / raw)
  To: Herring, Davis; +Cc: Bastien, Stefan Monnier, emacs-devel@gnu.org

> Since C-x C-b is fast to execute, and typing the regexp is already several keystrokes,
> what about providing a filter command within the buffer list?

Like in Ibuffer: '% n' - Mark buffers by their name, using a regexp.



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH] Filter list-buffers by a regexp (interactively)
  2014-08-08 23:41         ` Juri Linkov
@ 2014-08-09  7:15           ` Bastien
  2014-08-09 13:00             ` Stefan Monnier
  0 siblings, 1 reply; 9+ messages in thread
From: Bastien @ 2014-08-09  7:15 UTC (permalink / raw)
  To: Juri Linkov; +Cc: Stefan Monnier, emacs-devel@gnu.org

Juri Linkov <juri@jurta.org> writes:

>> Since C-x C-b is fast to execute, and typing the regexp is already several keystrokes,
>> what about providing a filter command within the buffer list?

Yes, that's already what '*' does in the original patch.

> Like in Ibuffer: '% n' - Mark buffers by their name, using a regexp.

Yes, why not too.

What about C-u C-x C-b prompting the user if she wants to filter by
[f]iles only or by a [r]egexp?

-- 
 Bastien



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH] Filter list-buffers by a regexp (interactively)
  2014-08-09  7:15           ` Bastien
@ 2014-08-09 13:00             ` Stefan Monnier
  0 siblings, 0 replies; 9+ messages in thread
From: Stefan Monnier @ 2014-08-09 13:00 UTC (permalink / raw)
  To: Bastien; +Cc: Juri Linkov, emacs-devel@gnu.org

> What about C-u C-x C-b prompting the user if she wants to filter by
> [f]iles only or by a [r]egexp?

An empty regexp could mean "only show file buffers".
It's an "incompatible change" and some users may not like it, but maybe
more users will benefit than will suffer.  What do people think?


        Stefan



^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2014-08-09 13:00 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-07-28 17:32 [PATCH] Filter list-buffers by a regexp (interactively) Bastien
2014-08-04 11:24 ` 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

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).