Subject: Re: bug#32731: 26.1.50; Ibuffer filter by mode: Handle >1 mode names

From: Tino Calancha <[email-masked]>
Date: Tue, 18 Sep 2018 02:44:28 +0900

Noam Postavsky <[email-masked]> writes:

>Shouldn't this patch be going into master anyway (since it's adding a
>new feature)?
I think is pretty tiny and safe new feature.  We can ask Eli anyway
[probably he is reading this right now :-)]

>> +  (cond ((null (cdr modes)) (ibuffer-filter-by-used-mode (car modes)))
>> +        (t (let ((composed-filter
>
> We should handle the (null modes) case too, but otherwise looks good to
> me.
Yeah, that's right.

* 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?

* Why just 'used modes' and not the other 2
  mode filters benefit from this feature?  
Because I just use that filter, i.e. '/ m'.  A bit selfish.

Please take a look in following patch.
* It allows filtering by >1 mode with the 3 mode filters.
* It moves most of the code inside the macro `define-ibuffer-filter'; no
  need to add new functions.

--8<-----------------------------cut here---------------start------------->8---
commit 2079d7b656cef1e1d2c03f3c7e0c93ec135f348d
Author: Tino Calancha <[email-masked]>
Date:   Tue Sep 18 02:19:54 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-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.
    * lisp/ibuf-macs.el(define-ibuffer-filter): Adjust it to
    handle QUALIFIER been a list of symbols.
    * etc/NEWS(Ibuffer): Announce this change.
    
    Co-authored-by: Noam Postavsky <[email-masked]>

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..2b0292f2de 100644
--- a/lisp/ibuf-ext.el
+++ b/lisp/ibuf-ext.el
@@ -1228,28 +1228,33 @@ 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)))
+       (lambda (e)
+           (string-match "-mode\\'" (if (symbolp e) (symbol-name e) e)))
        t nil nil default))))
   (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,8 +1262,8 @@ 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: ")
@@ -1267,11 +1272,13 @@ used-mode
 
 ;;;###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)))
   (with-current-buffer buf (derived-mode-p qualifier)))
diff --git a/lisp/ibuf-macs.el b/lisp/ibuf-macs.el
index 6a70a8341a..7893ec9cae 100644
--- a/lisp/ibuf-macs.el
+++ b/lisp/ibuf-macs.el
@@ -296,21 +296,34 @@ 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))
+           ;; The mode filters accept one symbol or a list of symbols; if the
+           ;; user inputs >1 mode, compose the individual filters with `or'.
+           ,(when (memq name (list 'mode 'used-mode 'derived-mode))
+              `(progn
+                 (unless (listp qualifier) (setq qualifier (list qualifier)))
+                 ;; Reject equivalent filters: (or m1 m2) is same as (or m2 m1).
+                 (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)
+                   (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---