all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Federico Tedin <federicotedin@gmail.com>
To: ndame <emacsuser@freemail.hu>
Cc: 36981@debbugs.gnu.org
Subject: bug#36981: 26.2; request: add searching by package name to list-packages
Date: Sun, 08 Sep 2019 02:47:57 +0200	[thread overview]
Message-ID: <87ef0r396q.fsf@gmail.com> (raw)
In-Reply-To: <Axwrsg.z9kQJiy2sPQf.XH9J2q5fdaP4A1bg@freemail.hu> (ndame's message of "Fri, 9 Aug 2019 09:21:02 +0200 (CEST)")

[-- Attachment #1: Type: text/plain, Size: 565 bytes --]

ndame <emacsuser@freemail.hu> writes:

> When installing a package from list-packages it's often
> inconvenient to find a package which has a string which occurs
> many times in the packages buffer. (e.g. in the descriptions).
>
> The package buffer has f for filtering, but it filters only for
> package keywords, not names.
>
> Add a new command s for search which searches (or filters) the
> package list by a substring match on the package name.

I've implemented this feature using the "s" key as you suggested. I'm
attaching a patch with my changes.

- Fede


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: patch --]
[-- Type: text/x-diff, Size: 4830 bytes --]

From cb14b951949e26bea8c12b202b70754271d9f4e2 Mon Sep 17 00:00:00 2001
From: Federico Tedin <federicotedin@gmail.com>
Date: Sun, 8 Sep 2019 02:38:43 +0200
Subject: [PATCH 1/1] Search packages by name in list-packages (Bug#36981)

* lisp/emacs-lisp/package.el (package-menu-search): Added a new
function that allows filtering packages by name.
(package-menu--generate): Show full packages list with 'q' if current
list has been filtered.
(package-menu-mode-map): Bind 's' to package-menu-search.
* test/lisp/emacs-lisp/package-tests.el (package-test-list-search):
Added a test for package-menu-search.
---
 lisp/emacs-lisp/package.el            | 30 +++++++++++++++++++++++++--
 test/lisp/emacs-lisp/package-tests.el | 11 +++++++++-
 2 files changed, 38 insertions(+), 3 deletions(-)

diff --git a/lisp/emacs-lisp/package.el b/lisp/emacs-lisp/package.el
index ef0c5171de..0993015fde 100644
--- a/lisp/emacs-lisp/package.el
+++ b/lisp/emacs-lisp/package.el
@@ -2621,6 +2621,7 @@ package-menu-mode-map
     (define-key map "U" 'package-menu-mark-upgrades)
     (define-key map "r" 'package-menu-refresh)
     (define-key map "f" 'package-menu-filter)
+    (define-key map "s" 'package-menu-search)
     (define-key map "~" 'package-menu-mark-obsolete-for-deletion)
     (define-key map "x" 'package-menu-execute)
     (define-key map "h" 'package-menu-quick-help)
@@ -2652,7 +2653,8 @@ package-menu-mode-map
     ["Unmark" package-menu-mark-unmark :help "Clear any marks on a package and move to the next line"]
 
     "--"
-    ["Filter Package List" package-menu-filter :help "Filter package selection (q to go back)"]
+    ["Filter Packages by Keywords" package-menu-filter :help "Filter packages by keywords (q to go back)"]
+    ["Filter Packages by Name" package-menu-search :help "Filter packages by name (q to go back)"]
     ["Hide by Regexp" package-menu-hide-package :help "Permanently hide all packages matching a regexp"]
     ["Display Older Versions" package-menu-toggle-hiding
      :style toggle :selected (not package-menu--hide-packages)
@@ -2963,7 +2965,7 @@ package-menu--generate
             (let ((filters (mapconcat #'identity keywords ",")))
               (concat "Package[" filters "]"))
           "Package"))
-  (if keywords
+  (if (or keywords (and packages (listp packages)))
       (define-key package-menu-mode-map "q" 'package-show-package-list)
     (define-key package-menu-mode-map "q" 'quit-window))
   (tabulated-list-init-header)
@@ -3598,6 +3600,30 @@ package-menu-filter
                                    (list keyword)
                                  keyword)))
 
+(defun package-menu-search (name)
+  "Filter the *Packages* buffer.
+Show only those items whose name matches NAME. If NAME is nil or an
+empty string, show all packages.
+
+To restore the full package list, type `q'."
+  (interactive
+   (list (read-from-minibuffer "Package name: ")
+         current-prefix-arg))
+  (if (or (not name) (string-empty-p name))
+      (package-show-package-list t nil)
+    ;; Update `tabulated-list-entries' so that in contains all
+    ;; packages before searching.
+    (package-menu--refresh t nil)
+    (let (matched)
+      (dolist (entry tabulated-list-entries)
+        (let* ((pkg-name-sym (package-desc-name (car entry)))
+               (pkg-name (symbol-name pkg-name-sym)))
+          (when (string-match name pkg-name)
+            (push pkg-name-sym matched))))
+      (if matched
+          (package-show-package-list matched nil)
+        (user-error "No packages found")))))
+
 (defun package-list-packages-no-fetch ()
   "Display a list of packages.
 Does not fetch the updated list of packages before displaying.
diff --git a/test/lisp/emacs-lisp/package-tests.el b/test/lisp/emacs-lisp/package-tests.el
index c757bccf67..ea28db83ce 100644
--- a/test/lisp/emacs-lisp/package-tests.el
+++ b/test/lisp/emacs-lisp/package-tests.el
@@ -28,7 +28,7 @@
 
 ;; Run this in a clean Emacs session using:
 ;;
-;;     $ emacs -Q --batch -L . -l package-test.el -l ert -f ert-run-tests-batch-and-exit
+;;     $ emacs -Q --batch -L . -l package-tests.el -l ert -f ert-run-tests-batch-and-exit
 
 ;;; Code:
 
@@ -360,6 +360,15 @@ package-test--compatible-p
       (should-not (re-search-forward "^\\s-+simple-single\\s-+1.3\\s-+\\(available\\|new\\)" nil t))
       (kill-buffer buf))))
 
+(ert-deftest package-test-list-search ()
+  "Ensure package list is filtered correctly by package name."
+  (with-package-test ()
+    (let ((buf (package-list-packages)))
+      (package-menu-search "tetris")
+      (should (= (length tabulated-list-entries) 1))
+      (should (eq (package-desc-name (caar tabulated-list-entries)) 'tetris))
+      (kill-buffer buf))))
+
 (ert-deftest package-test-update-archives ()
   "Test updating package archives."
   (with-package-test ()
-- 
2.17.1


  reply	other threads:[~2019-09-08  0:47 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-08-09  7:21 bug#36981: 26.2; request: add searching by package name to list-packages ndame
2019-09-08  0:47 ` Federico Tedin [this message]
2019-09-08 15:20   ` Štěpán Němec
2019-09-09 20:54     ` Federico Tedin
2019-09-14 11:15       ` Federico Tedin
2019-09-18 23:17         ` Stefan Kangas
2019-09-16  0:29   ` Stefan Kangas
2019-09-18 16:38     ` Federico Tedin
2019-09-18 22:22       ` Stefan Kangas
2019-09-19 20:29         ` Juri Linkov
2019-09-20 18:49           ` Federico Tedin
2019-09-20 22:09             ` Stefan Kangas
2019-09-26 18:28               ` Federico Tedin
2019-09-27 10:32                 ` Stefan Kangas
2019-09-30 21:26                   ` Federico Tedin
2019-09-30 21:59                     ` Stefan Kangas
2019-10-08 17:28                       ` Stefan Kangas

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

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87ef0r396q.fsf@gmail.com \
    --to=federicotedin@gmail.com \
    --cc=36981@debbugs.gnu.org \
    --cc=emacsuser@freemail.hu \
    /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 external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.