Subject: bug#39903: 28.0.50; Feature request: another filter for Package Menu

From: Pieter van Oostrum <[email-masked]>
Date: Thu, 05 Mar 2020 23:24:45 +0100

Here is the patch:

>From 005c0d43c8fd97caaf58d0ed3362c11022aff292 Mon Sep 17 00:00:00 2001
From: Pieter van Oostrum <[email-masked]>
Date: Thu, 5 Mar 2020 20:20:04 +0100
Subject: [PATCH] Add new filter command to Package Menu (Bug#39903)

* lisp/emacs-lisp/package.el (package-menu-filter-marked):
New filter command.
* test/lisp/emacs-lisp/package-tests.el (package-test-list-filter-marked):
New test.
(package-menu-mode-menu):
(package-menu-mode-map): Update menu to include new filter command.

* doc/emacs/package.texi (Package Menu): Document the new command.
* etc/NEWS: Announce the new command.
---
 doc/emacs/package.texi                |  6 ++++++
 etc/NEWS                              |  1 +
 lisp/emacs-lisp/package.el            | 31 +++++++++++++++++++++++++++
 test/lisp/emacs-lisp/package-tests.el | 15 +++++++++++++
 4 files changed, 53 insertions(+)

diff --git a/doc/emacs/package.texi b/doc/emacs/package.texi
index db3cf317ff..91e44b8eba 100644
--- a/doc/emacs/package.texi
+++ b/doc/emacs/package.texi
@@ -202,6 +202,12 @@ Package Menu
 @samp{=}, and then a package version, and shows packages that has a
 lower, equal or higher version than the one specified.
 
+@item / m
+@kindex / m @r{(Package Menu)}
+@findex package-menu-filter-marked
+Filter package list by non-empty mark (@code{package-menu-filter-marked}).
+This shows only the packages that have been marked to be installed or deleted.
+
 @item / /
 @kindex / / @r{(Package Menu)}
 @findex package-menu-filter-clear
diff --git a/etc/NEWS b/etc/NEWS
index fcdf6dbe24..01f46cc79d 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -172,6 +172,7 @@ key             binding
 / n             package-menu-filter-by-name
 / s             package-menu-filter-by-status
 / v             package-menu-filter-by-version
+/ m             package-menu-filter-marked
 / /             package-menu-filter-clear
 
 
diff --git a/lisp/emacs-lisp/package.el b/lisp/emacs-lisp/package.el
index 43eb038a86..efac7d888c 100644
--- a/lisp/emacs-lisp/package.el
+++ b/lisp/emacs-lisp/package.el
@@ -2692,6 +2692,7 @@ package-menu-mode-map
     (define-key map (kbd "/ n") 'package-menu-filter-by-name)
     (define-key map (kbd "/ s") 'package-menu-filter-by-status)
     (define-key map (kbd "/ v") 'package-menu-filter-by-version)
+    (define-key map (kbd "/ m") 'package-menu-filter-marked)
     map)
   "Local keymap for `package-menu-mode' buffers.")
 
@@ -2722,6 +2723,7 @@ package-menu-mode-menu
      ["Filter by Name" package-menu-filter-by-name :help "Filter packages by name"]
      ["Filter by Status" package-menu-filter-by-status :help "Filter packages by status"]
      ["Filter by Version" package-menu-filter-by-version :help "Filter packages by version"]
+     ["Filter Marked" package-menu-filter-marked :help "Filter packages marked for upgrade"]
      ["Clear Filter" package-menu-clear-filter :help "Clear package list filter"])
 
     ["Hide by Regexp" package-menu-hide-package :help "Hide all packages matching a regexp"]
@@ -3845,6 +3847,35 @@ package-menu-filter-by-version
            (funcall fun (package-desc-version pkg-desc) ver)))
        (format "versions:%s%s" predicate version)))))
 
+(defun package-menu-filter-marked ()
+  "Filter \"*Packages*\" buffer by non-empty upgrade mark.
+Unlike other filters, this leaves the marks intact."
+  (interactive)
+  (package--ensure-package-menu-mode)
+  (widen)
+  (let (found-entries mark pkg-id entry marks)
+    (save-excursion
+      (goto-char (point-min))
+      (while (not (eobp))
+        (setq mark (char-after))
+        (unless (eq mark ?\s)
+	  (setq pkg-id (tabulated-list-get-id))
+          (setq entry (package-menu--print-info-simple pkg-id))
+	  (push entry found-entries)
+	  ;; remember the mark
+	  (push (cons pkg-id mark) marks))
+        (forward-line))
+      (if found-entries
+          (progn
+            (setq tabulated-list-entries found-entries)
+            (package-menu--display t nil)
+	    ;; redo the marks, but we must remember the marks!!
+	    (goto-char (point-min))
+	    (while (not (eobp))
+	      (setq mark (cdr (assq (tabulated-list-get-id) marks)))
+	      (tabulated-list-put-tag (char-to-string mark) t)))
+	(user-error "No packages found")))))
+
 (defun package-menu-clear-filter ()
   "Clear any filter currently applied to the \"*Packages*\" buffer."
   (interactive)
diff --git a/test/lisp/emacs-lisp/package-tests.el b/test/lisp/emacs-lisp/package-tests.el
index 05770d6b58..524c7f8f94 100644
--- a/test/lisp/emacs-lisp/package-tests.el
+++ b/test/lisp/emacs-lisp/package-tests.el
@@ -413,6 +413,21 @@ package-test-list-filter-by-status
     ;; No installed packages in default environment.
     (should-error (package-menu-filter-by-status "installed"))))
 
+(ert-deftest package-test-list-filter-marked ()
+  "Ensure package list is filtered correctly by non-empty mark."
+  (with-package-test ()
+    (let ((buf (package-list-packages)))
+      (revert-buffer)
+      (search-forward-regexp "^ +simple-single")
+      (package-menu-mark-install)
+      (package-menu-filter-marked)
+      (goto-char (point-min))
+      (should (re-search-forward "^I +simple-single" nil t))
+      (should (= (count-lines (point-min) (point-max)) 1))
+      (package-menu-mark-unmark)
+      ;; No marked packages in default environment.
+      (should-error (package-menu-filter-marked)))))
+
 (ert-deftest package-test-list-filter-by-version ()
   (with-package-menu-test
     (should-error (package-menu-filter-by-version "1.1" 'unknown-symbol)))  )
-- 
2.25.1

-- 
Pieter van Oostrum
www: http://pieter.vanoostrum.org/
PGP key: [8DAE142BE17999C4]