From c56070beb6cb7bc383c7f39215c915e1f43fd578 Mon Sep 17 00:00:00 2001 From: Lars Ingebrigtsen Date: Sat, 7 May 2022 14:31:42 +0200 Subject: [PATCH] Make `x' in package-menu-mode more DWIM * lisp/emacs-lisp/package.el (package-menu-mode): Make the doc string more helpful. (package-menu-execute): Make `x' when no files are installed DWIM. --- etc/NEWS | 4 ++++ lisp/emacs-lisp/package.el | 31 +++++++++++++++++++++++++++---- 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/etc/NEWS b/etc/NEWS index 2e7a1d86386..5dd87e3e9e8 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -748,6 +748,10 @@ this includes "binary" buffers like 'archive-mode' and 'image-mode'. This command allows you to upgrade packages without using 'M-x list-packages'. +*** New DWIM action on 'x'. +If no packages are marked, 'x' will install the package under point if +it isn't already, and remove it if it is installed. + ** Miscellaneous +++ diff --git a/lisp/emacs-lisp/package.el b/lisp/emacs-lisp/package.el index 58c1349e1c2..c1e14a4acb5 100644 --- a/lisp/emacs-lisp/package.el +++ b/lisp/emacs-lisp/package.el @@ -2885,7 +2885,13 @@ either a full name or nil, and EMAIL is a valid email address." (define-derived-mode package-menu-mode tabulated-list-mode "Package Menu" "Major mode for browsing a list of packages. -Letters do not insert themselves; instead, they are commands. +The most useful commands here are: + + `x': Install the package under point if it isn't already installed, + and delete it if it's already installed, + `i': mark a package for installation, and + `d': mark a package for deletion. Use the `x' command to perform the + actions on the marked files. \\ \\{package-menu-mode-map}" :interactive nil @@ -3632,8 +3638,13 @@ packages list, respectively." (defun package-menu-execute (&optional noquery) "Perform marked Package Menu actions. Packages marked for installation are downloaded and installed, -packages marked for deletion are removed, -and packages marked for upgrading are downloaded and upgraded. +packages marked for deletion are removed, and packages marked for +upgrading are downloaded and upgraded. + +If no packages are marked, the action taken depends on the state +of the package under point. If it's not already installed, this +command will install the package, and if it's installed, it will +delete the package. Optional argument NOQUERY non-nil means do not ask the user to confirm." (interactive nil package-menu-mode) @@ -3651,8 +3662,20 @@ Optional argument NOQUERY non-nil means do not ask the user to confirm." ((eq cmd ?I) (push pkg-desc install-list)))) (forward-line))) + ;; Nothing marked. (unless (or delete-list install-list) - (user-error "No operations specified")) + ;; Not on a package line. + (unless (tabulated-list-get-id) + (user-error "No operations specified")) + (let* ((id (tabulated-list-get-id)) + (status (package-menu-get-status))) + (cond + ((member status '("installed")) + (push id delete-list)) + ((member status '("available" "avail-obso" "new" "dependency")) + (push id install-list)) + (t (user-error "No default action available for status: %s" + status))))) (let-alist (package-menu--partition-transaction install-list delete-list) (when (or noquery (package-menu--prompt-transaction-p .delete .install .upgrade)) -- 2.39.2