Clean up major mode check in package-menu-mode

* lisp/emacs-lisp/package.el (package--ensure-package-menu-mode):
Extract function to warn about incorrect major mode...
(package-menu-toggle-hiding, package-menu-refresh)
(package-menu-execute): ...from here.
(package-menu--mark-upgrades-1): And here, but move call...
(package-menu-mark-upgrades): ...here instead.
(package-menu-hide-package, package-menu-mark-delete)
(package-menu-mark-install, package-menu-mark-unmark)
(package-menu-quick-help, package-menu-get-status)
(package-menu-filter-by-keyword, package-menu-filter-by-name)
(package-menu-clear-filter): Add call to new function.  (Bug#37891)
This commit is contained in:
Stefan Kangas 2019-11-07 04:10:43 +01:00
parent 1428dfe630
commit ddb797cf4c

View file

@ -2791,6 +2791,11 @@ package PKG-DESC, add one. The alist is keyed with PKG-DESC."
(defvar package--emacs-version-list (version-to-list emacs-version) (defvar package--emacs-version-list (version-to-list emacs-version)
"The value of variable `emacs-version' as a list.") "The value of variable `emacs-version' as a list.")
(defun package--ensure-package-menu-mode ()
"Signal a user-error if major mode is not `package-menu-mode'."
(unless (derived-mode-p 'package-menu-mode)
(user-error "The current buffer is not a Package Menu")))
(defun package--incompatible-p (pkg &optional shallow) (defun package--incompatible-p (pkg &optional shallow)
"Return non-nil if PKG has no chance of being installable. "Return non-nil if PKG has no chance of being installable.
PKG is a `package-desc' object. PKG is a `package-desc' object.
@ -2866,8 +2871,7 @@ Installed obsolete packages are always displayed.")
(defun package-menu-toggle-hiding () (defun package-menu-toggle-hiding ()
"In Package Menu, toggle visibility of obsolete available packages." "In Package Menu, toggle visibility of obsolete available packages."
(interactive) (interactive)
(unless (derived-mode-p 'package-menu-mode) (package--ensure-package-menu-mode)
(user-error "The current buffer is not a Package Menu"))
(setq package-menu--hide-packages (setq package-menu--hide-packages
(not package-menu--hide-packages)) (not package-menu--hide-packages))
(if package-menu--hide-packages (if package-menu--hide-packages
@ -3175,8 +3179,7 @@ user-error if there is already a refresh running asynchronously.
`package-menu-mode' sets `revert-buffer-function' to this `package-menu-mode' sets `revert-buffer-function' to this
function. The args ARG and NOCONFIRM, passed from function. The args ARG and NOCONFIRM, passed from
`revert-buffer', are ignored." `revert-buffer', are ignored."
(unless (derived-mode-p 'package-menu-mode) (package--ensure-package-menu-mode)
(user-error "The current buffer is not a Package Menu"))
(when (and package-menu-async package--downloads-in-progress) (when (and package-menu-async package--downloads-in-progress)
(user-error "Package refresh is already in progress, please wait...")) (user-error "Package refresh is already in progress, please wait..."))
(setq package-menu--old-archive-contents package-archive-contents) (setq package-menu--old-archive-contents package-archive-contents)
@ -3188,6 +3191,7 @@ function. The args ARG and NOCONFIRM, passed from
"Hide a package under point in Package Menu. "Hide a package under point in Package Menu.
If optional arg BUTTON is non-nil, describe its associated package." If optional arg BUTTON is non-nil, describe its associated package."
(interactive) (interactive)
(package--ensure-package-menu-mode)
(declare (interactive-only "change `package-hidden-regexps' instead.")) (declare (interactive-only "change `package-hidden-regexps' instead."))
(let* ((name (when (derived-mode-p 'package-menu-mode) (let* ((name (when (derived-mode-p 'package-menu-mode)
(concat "\\`" (regexp-quote (symbol-name (package-desc-name (concat "\\`" (regexp-quote (symbol-name (package-desc-name
@ -3221,6 +3225,7 @@ If optional arg BUTTON is non-nil, describe its associated package."
(defun package-menu-mark-delete (&optional _num) (defun package-menu-mark-delete (&optional _num)
"Mark a package for deletion and move to the next line." "Mark a package for deletion and move to the next line."
(interactive "p") (interactive "p")
(package--ensure-package-menu-mode)
(if (member (package-menu-get-status) (if (member (package-menu-get-status)
'("installed" "dependency" "obsolete" "unsigned")) '("installed" "dependency" "obsolete" "unsigned"))
(tabulated-list-put-tag "D" t) (tabulated-list-put-tag "D" t)
@ -3229,6 +3234,7 @@ If optional arg BUTTON is non-nil, describe its associated package."
(defun package-menu-mark-install (&optional _num) (defun package-menu-mark-install (&optional _num)
"Mark a package for installation and move to the next line." "Mark a package for installation and move to the next line."
(interactive "p") (interactive "p")
(package--ensure-package-menu-mode)
(if (member (package-menu-get-status) '("available" "avail-obso" "new" "dependency")) (if (member (package-menu-get-status) '("available" "avail-obso" "new" "dependency"))
(tabulated-list-put-tag "I" t) (tabulated-list-put-tag "I" t)
(forward-line))) (forward-line)))
@ -3236,17 +3242,20 @@ If optional arg BUTTON is non-nil, describe its associated package."
(defun package-menu-mark-unmark (&optional _num) (defun package-menu-mark-unmark (&optional _num)
"Clear any marks on a package and move to the next line." "Clear any marks on a package and move to the next line."
(interactive "p") (interactive "p")
(package--ensure-package-menu-mode)
(tabulated-list-put-tag " " t)) (tabulated-list-put-tag " " t))
(defun package-menu-backup-unmark () (defun package-menu-backup-unmark ()
"Back up one line and clear any marks on that package." "Back up one line and clear any marks on that package."
(interactive) (interactive)
(package--ensure-package-menu-mode)
(forward-line -1) (forward-line -1)
(tabulated-list-put-tag " ")) (tabulated-list-put-tag " "))
(defun package-menu-mark-obsolete-for-deletion () (defun package-menu-mark-obsolete-for-deletion ()
"Mark all obsolete packages for deletion." "Mark all obsolete packages for deletion."
(interactive) (interactive)
(package--ensure-package-menu-mode)
(save-excursion (save-excursion
(goto-char (point-min)) (goto-char (point-min))
(while (not (eobp)) (while (not (eobp))
@ -3277,6 +3286,7 @@ If optional arg BUTTON is non-nil, describe its associated package."
"Show short key binding help for `package-menu-mode'. "Show short key binding help for `package-menu-mode'.
The full list of keys can be viewed with \\[describe-mode]." The full list of keys can be viewed with \\[describe-mode]."
(interactive) (interactive)
(package--ensure-package-menu-mode)
(message (mapconcat #'package--prettify-quick-help-key (message (mapconcat #'package--prettify-quick-help-key
package--quick-help-keys "\n"))) package--quick-help-keys "\n")))
@ -3285,6 +3295,7 @@ The full list of keys can be viewed with \\[describe-mode]."
(defun package-menu-get-status () (defun package-menu-get-status ()
"Return status text of package at point in Package Menu." "Return status text of package at point in Package Menu."
(package--ensure-package-menu-mode)
(let* ((id (tabulated-list-get-id)) (let* ((id (tabulated-list-get-id))
(entry (and id (assoc id tabulated-list-entries)))) (entry (and id (assoc id tabulated-list-entries))))
(if entry (if entry
@ -3340,8 +3351,6 @@ corresponding to the newer version."
(defun package-menu--mark-upgrades-1 () (defun package-menu--mark-upgrades-1 ()
"Mark all upgradable packages in the Package Menu. "Mark all upgradable packages in the Package Menu.
Implementation of `package-menu-mark-upgrades'." Implementation of `package-menu-mark-upgrades'."
(unless (derived-mode-p 'package-menu-mode)
(error "The current buffer is not a Package Menu"))
(setq package-menu--mark-upgrades-pending nil) (setq package-menu--mark-upgrades-pending nil)
(let ((upgrades (package-menu--find-upgrades))) (let ((upgrades (package-menu--find-upgrades)))
(if (null upgrades) (if (null upgrades)
@ -3373,6 +3382,7 @@ If there's an async refresh operation in progress, the flags will
be placed as part of `package-menu--post-refresh' instead of be placed as part of `package-menu--post-refresh' instead of
immediately." immediately."
(interactive) (interactive)
(package--ensure-package-menu-mode)
(if (not package--downloads-in-progress) (if (not package--downloads-in-progress)
(package-menu--mark-upgrades-1) (package-menu--mark-upgrades-1)
(setq package-menu--mark-upgrades-pending t) (setq package-menu--mark-upgrades-pending t)
@ -3466,8 +3476,7 @@ Packages marked for installation are downloaded and installed;
packages marked for deletion are removed. packages marked for deletion are removed.
Optional argument NOQUERY non-nil means do not ask the user to confirm." Optional argument NOQUERY non-nil means do not ask the user to confirm."
(interactive) (interactive)
(unless (derived-mode-p 'package-menu-mode) (package--ensure-package-menu-mode)
(error "The current buffer is not in Package Menu mode"))
(let (install-list delete-list cmd pkg-desc) (let (install-list delete-list cmd pkg-desc)
(save-excursion (save-excursion
(goto-char (point-min)) (goto-char (point-min))
@ -3693,6 +3702,7 @@ Statuses available include \"incompat\", \"available\",
(interactive (interactive
(list (completing-read-multiple (list (completing-read-multiple
"Keywords (comma separated): " (package-all-keywords)))) "Keywords (comma separated): " (package-all-keywords))))
(package--ensure-package-menu-mode)
(package-show-package-list t (if (stringp keyword) (package-show-package-list t (if (stringp keyword)
(list keyword) (list keyword)
keyword))) keyword)))
@ -3702,6 +3712,7 @@ Statuses available include \"incompat\", \"available\",
Show only those items whose name matches the regular expression Show only those items whose name matches the regular expression
NAME. If NAME is nil or the empty string, show all packages." NAME. If NAME is nil or the empty string, show all packages."
(interactive (list (read-from-minibuffer "Filter by name (regexp): "))) (interactive (list (read-from-minibuffer "Filter by name (regexp): ")))
(package--ensure-package-menu-mode)
(if (or (not name) (string-empty-p name)) (if (or (not name) (string-empty-p name))
(package-show-package-list t nil) (package-show-package-list t nil)
;; Update `tabulated-list-entries' so that it contains all ;; Update `tabulated-list-entries' so that it contains all
@ -3719,6 +3730,7 @@ NAME. If NAME is nil or the empty string, show all packages."
(defun package-menu-clear-filter () (defun package-menu-clear-filter ()
"Clear any filter currently applied to the \"*Packages*\" buffer." "Clear any filter currently applied to the \"*Packages*\" buffer."
(interactive) (interactive)
(package--ensure-package-menu-mode)
(package-menu--generate t t)) (package-menu--generate t t))
(defun package-list-packages-no-fetch () (defun package-list-packages-no-fetch ()