unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Tino Calancha <tino.calancha@gmail.com>
To: Noam Postavsky <npostavs@gmail.com>
Cc: 32731@debbugs.gnu.org
Subject: bug#32731: 26.1.50; Ibuffer filter by mode: Handle >1 mode names
Date: Wed, 19 Sep 2018 18:23:52 +0900	[thread overview]
Message-ID: <877ejh96xj.fsf@calancha-pc.dy.bbexcite.jp> (raw)
In-Reply-To: <871s9qqtq1.fsf@gmail.com> (Noam Postavsky's message of "Tue, 18 Sep 2018 19:19:34 -0400")

Noam Postavsky <npostavs@gmail.com> writes:

> Tino Calancha <tino.calancha@gmail.com> writes:
>
>> * I don't like much such new function
>> `ibuffer-filter-by-used-modes': isn't it better that
>> `ibuffer-filter-by-used-mode' handles both inputs?
>
> In principle, I agree with this, but there is a drawback to be aware of:
> fancier completion modes (e.g., icomplete, ido, etc) don't support
> completing-read-multiple.  So users of those modes could see this as a
> regression.
OK.  Annotaded here for the records.

>> * It moves most of the code inside the macro `define-ibuffer-filter'; no
>>   need to add new functions.
>
> I don't like that define-ibuffer-filter has special code for certain
> filter types.
Me either, but I don't like code duplication either:  Macros are a
convenient way to avoid that.  Unless you have something sound in mind...

Following patch adds a key :composable, which also serves as
documentation.
--8<-----------------------------cut here---------------start------------->8---
commit 5c6092b172209cb0261f36f423f508bddc4d0dcd
Author: Tino Calancha <tino.calancha@gmail.com>
Date:   Wed Sep 19 18:08:35 2018 +0900

    Ibuffer filter by modes: Accept several mode names
    
    Extend all mode filters so that they handle >1 mode.
    For instance, if the user wants to filter all buffers in
    C or C++ mode, then s?he can call the filter interactively
    with input: 'c-mode,c++-mode'.
    
    * lisp/ibuf-macs.el(define-ibuffer-filter): Add key :composable.
    If the value of this key is non-nil, then the filter accepts
    a single qualifier or a list of them; in the latter case, the
    resultant filter is the `or' composition of the individual ones.
    
    * lisp/ibuf-ext.el (ibuffer-filter-by-used-mode)
    (ibuffer-filter-by-mode, ibuffer-filter-by-derived-mode)
    Accept a mode name or a list of mode names.
    Interactively, accept a comma separated list of mode names.
    Set :composable value non-nil.
    
    * etc/NEWS(Ibuffer): Announce this change.
    
    Co-authored-by: Noam Postavsky <npostavs@gmail.com>

diff --git a/etc/NEWS b/etc/NEWS
index fa93112c91..43d9a38b91 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -62,6 +62,10 @@ to reduce differences between developer and production builds.
 ** Ibuffer
 
 ---
+*** All mode filters accept a symbol or a list of symbols, i.e., you
+can filter several major modes with easy.
+
+---
 *** New toggle 'ibuffer-do-toggle-lock', bound to 'L'.
 
 ** Gnus
diff --git a/lisp/ibuf-ext.el b/lisp/ibuf-ext.el
index d9949d2835..fc2e21a77f 100644
--- a/lisp/ibuf-ext.el
+++ b/lisp/ibuf-ext.el
@@ -1228,28 +1228,34 @@ ibuffer-list-buffer-modes
 
 ;;;###autoload (autoload 'ibuffer-filter-by-mode "ibuf-ext")
 (define-ibuffer-filter mode
-  "Limit current view to buffers with major mode QUALIFIER."
+  "Limit current view to buffers with major mode in QUALIFIER.
+QUALIFIER is the mode name as a symbol or a list of symbols.
+Called interactively, accept a comma separated list of mode names."
   (:description "major mode"
    :reader
    (let* ((buf (ibuffer-current-buffer))
           (default (if (and buf (buffer-live-p buf))
                        (symbol-name (buffer-local-value
                                      'major-mode buf)))))
-     (intern
-      (completing-read
+     (mapcar #'intern
+      (completing-read-multiple
        (if default
            (format "Filter by major mode (default %s): " default)
          "Filter by major mode: ")
        obarray
-       #'(lambda (e)
-           (string-match "-mode\\'" (symbol-name e)))
-       t nil nil default))))
+       (lambda (e)
+           (string-match "-mode\\'" (if (symbolp e) (symbol-name e) e)))
+       t nil nil default)))
+   :composable t)
   (eq qualifier (buffer-local-value 'major-mode buf)))
 
 ;;;###autoload (autoload 'ibuffer-filter-by-used-mode "ibuf-ext")
 (define-ibuffer-filter used-mode
-  "Limit current view to buffers with major mode QUALIFIER.
-Called interactively, this function allows selection of modes
+  "Limit current view to buffers with major mode in QUALIFIER.
+QUALIFIER is the mode name as a symbol or a list of symbols.
+
+Called interactively, accept a comma separated list of mode names.
+When called interactively, this function allows selection of modes
 currently used by buffers."
   (:description "major mode in use"
    :reader
@@ -1257,23 +1263,27 @@ used-mode
           (default (if (and buf (buffer-live-p buf))
                        (symbol-name (buffer-local-value
                                      'major-mode buf)))))
-     (intern
-      (completing-read
+     (mapcar #'intern
+      (completing-read-multiple
        (if default
            (format "Filter by major mode (default %s): " default)
          "Filter by major mode: ")
-       (ibuffer-list-buffer-modes) nil t nil nil default))))
+       (ibuffer-list-buffer-modes) nil t nil nil default)))
+   :composable t)
   (eq qualifier (buffer-local-value 'major-mode buf)))
 
 ;;;###autoload (autoload 'ibuffer-filter-by-derived-mode "ibuf-ext")
 (define-ibuffer-filter derived-mode
-    "Limit current view to buffers whose major mode inherits from QUALIFIER."
+    "Limit current view to buffers whose major mode inherits from QUALIFIER.
+QUALIFIER is the mode name as a symbol or a list of symbols.
+Called interactively, accept a comma separated list of mode names."
   (:description "derived mode"
 		:reader
-		(intern
-		 (completing-read "Filter by derived mode: "
+                (mapcar #'intern
+		 (completing-read-multiple "Filter by derived mode: "
 				  (ibuffer-list-buffer-modes t)
-                                  nil t)))
+                                  nil t))
+                :composable t)
   (with-current-buffer buf (derived-mode-p qualifier)))
 
 ;;;###autoload (autoload 'ibuffer-filter-by-name "ibuf-ext")
diff --git a/lisp/ibuf-macs.el b/lisp/ibuf-macs.el
index 6a70a8341a..1030096ec1 100644
--- a/lisp/ibuf-macs.el
+++ b/lisp/ibuf-macs.el
@@ -282,12 +282,16 @@ ibuffer-save-marks
 (cl-defmacro define-ibuffer-filter (name documentation
 				       (&key
 					reader
-					description)
+					description
+                                        composable)
 				       &rest body)
   "Define a filter named NAME.
 DOCUMENTATION is the documentation of the function.
 READER is a form which should read a qualifier from the user.
 DESCRIPTION is a short string describing the filter.
+COMPOSABLE is a boolean; if non-nil, the filter accepts both,
+a single condition or a list of them; in the latter
+case the filter is the `or' composition of the conditions.
 
 BODY should contain forms which will be evaluated to test whether or
 not a particular buffer should be displayed or not.  The forms in BODY
@@ -296,21 +300,32 @@ ibuffer-save-marks
 
 \(fn NAME DOCUMENTATION (&key READER DESCRIPTION) &rest BODY)"
   (declare (indent 2) (doc-string 2))
-  (let ((fn-name (intern (concat "ibuffer-filter-by-" (symbol-name name)))))
+  (let ((fn-name (intern (concat "ibuffer-filter-by-" (symbol-name name))))
+        (filter (make-symbol "ibuffer-filter"))
+        (qualifier-str (make-symbol "ibuffer-qualifier-str")))
     `(progn
        (defun ,fn-name (qualifier)
 	 ,(or documentation "This filter is not documented.")
 	 (interactive (list ,reader))
-	 (if (null (ibuffer-push-filter (cons ',name qualifier)))
-	     (message "%s"
-		      (format ,(concat (format "Filter by %s already applied: " description)
-				       " %s")
-			      qualifier))
-           (message "%s"
-		    (format ,(concat (format "Filter by %s added: " description)
-				     " %s")
-			    qualifier))
-	   (ibuffer-update nil t)))
+         (let ((,filter (cons ',name qualifier))
+               (,qualifier-str qualifier))
+           ,(when composable
+              `(progn
+                 (unless (listp qualifier) (setq qualifier (list qualifier)))
+                 ;; Reject equivalent filters: (or f1 f2) is same as (or f2 f1).
+                 (setq qualifier (sort (delete-dups qualifier) #'string-lessp))
+                 (setq ,filter (cons ',name (car qualifier)))
+                 (setq ,qualifier-str
+                       (mapconcat (lambda (m) (if (symbolp m) (symbol-name m) m))
+                                  qualifier ","))
+                 (when (cdr qualifier) ; Compose individual filters with `or'.
+                   (setq ,filter `(or ,@(mapcar (lambda (m) (cons ',name m)) qualifier))))))
+	   (if (null (ibuffer-push-filter ,filter))
+	       (message ,(format "Filter by %s already applied:  %%s" description)
+		        ,qualifier-str)
+	     (message ,(format "Filter by %s added:  %%s" description)
+                      ,qualifier-str)
+	     (ibuffer-update nil t))))
        (push (list ',name ,description
 		   (lambda (buf qualifier)
                      (condition-case nil

--8<-----------------------------cut here---------------end--------------->8---





  reply	other threads:[~2018-09-19  9:23 UTC|newest]

Thread overview: 28+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-09-13 18:19 bug#32731: 26.1.50; Ibuffer filter by mode: Handle >1 mode names Tino Calancha
2018-09-13 19:09 ` Noam Postavsky
2018-09-13 20:04   ` Tino Calancha
2018-09-13 20:38   ` Tino Calancha
2018-09-13 23:39     ` Noam Postavsky
2018-09-15  9:15       ` Tino Calancha
2018-09-15 12:42         ` Noam Postavsky
2018-09-17 17:44           ` Tino Calancha
2018-09-17 18:27             ` Eli Zaretskii
2018-09-17 19:53               ` Tino Calancha
2018-09-18  7:14                 ` Eli Zaretskii
2018-09-18 23:19             ` Noam Postavsky
2018-09-19  9:23               ` Tino Calancha [this message]
2018-09-19  9:42                 ` Eli Zaretskii
2018-09-21  8:37                   ` Tino Calancha
2018-09-22  9:14                     ` Eli Zaretskii
2018-09-22 13:00                     ` Noam Postavsky
2018-09-23  1:37                       ` Richard Stallman
2018-09-23 12:01                         ` Noam Postavsky
2018-09-24  8:27                         ` Tino Calancha
2018-09-24 19:58                           ` Richard Stallman
2018-09-24 20:48                             ` Tino Calancha
2018-09-24 21:14                               ` Eli Zaretskii
2018-09-25  8:14                                 ` Robert Pluim
2018-09-25  9:24                                   ` Eli Zaretskii
2018-09-25 23:03                                     ` Richard Stallman
2018-09-29  9:49                                   ` Tino Calancha
2018-09-24  8:36                       ` Tino Calancha

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=877ejh96xj.fsf@calancha-pc.dy.bbexcite.jp \
    --to=tino.calancha@gmail.com \
    --cc=32731@debbugs.gnu.org \
    --cc=npostavs@gmail.com \
    /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).