@@ -2767,7 +2767,7 @@ Letters do not insert themselves; instead, they are commands.
27672767 (setq tabulated-list-sort-key (cons "Status" nil))
27682768 (add-hook 'tabulated-list-revert-hook #'package-menu--refresh nil t)
27692769 (tabulated-list-init-header)
2770- (setq revert-buffer-function 'package-menu--refresh)
2770+ (setq revert-buffer-function 'package-menu--refresh-contents )
27712771 (setf imenu-prev-index-position-function
27722772 #'package--imenu-prev-index-position-function)
27732773 (setf imenu-extract-index-name-function
@@ -2791,6 +2791,11 @@ package PKG-DESC, add one. The alist is keyed with PKG-DESC."
27912791(defvar package--emacs-version-list (version-to-list emacs-version)
27922792 "The value of variable `emacs-version' as a list.")
27932793
2794+ (defun package--ensure-package-menu-mode ()
2795+ "Signal a user-error if major mode is not `package-menu-mode'."
2796+ (unless (derived-mode-p 'package-menu-mode)
2797+ (user-error "The current buffer is not a Package Menu")))
2798+
27942799(defun package--incompatible-p (pkg &optional shallow)
27952800 "Return non-nil if PKG has no chance of being installable.
27962801PKG is a `package-desc' object.
@@ -2866,8 +2871,7 @@ Installed obsolete packages are always displayed.")
28662871(defun package-menu-toggle-hiding ()
28672872 "In Package Menu, toggle visibility of obsolete available packages."
28682873 (interactive)
2869- (unless (derived-mode-p 'package-menu-mode)
2870- (user-error "The current buffer is not a Package Menu"))
2874+ (package--ensure-package-menu-mode)
28712875 (setq package-menu--hide-packages
28722876 (not package-menu--hide-packages))
28732877 (if package-menu--hide-packages
@@ -3166,7 +3170,7 @@ Return (PKG-DESC [NAME VERSION STATUS DOC])."
31663170(defvar package-menu--old-archive-contents nil
31673171 "`package-archive-contents' before the latest refresh.")
31683172
3169- (defun package-menu--refresh (&optional _arg _noconfirm)
3173+ (defun package-menu--refresh-contents (&optional _arg _noconfirm)
31703174 "In Package Menu, download the Emacs Lisp package archive.
31713175Fetch the contents of each archive specified in
31723176`package-archives', and then refresh the package menu. Signal a
@@ -3175,8 +3179,7 @@ user-error if there is already a refresh running asynchronously.
31753179`package-menu-mode' sets `revert-buffer-function' to this
31763180function. The args ARG and NOCONFIRM, passed from
31773181`revert-buffer', are ignored."
3178- (unless (derived-mode-p 'package-menu-mode)
3179- (user-error "The current buffer is not a Package Menu"))
3182+ (package--ensure-package-menu-mode)
31803183 (when (and package-menu-async package--downloads-in-progress)
31813184 (user-error "Package refresh is already in progress, please wait..."))
31823185 (setq package-menu--old-archive-contents package-archive-contents)
@@ -3188,6 +3191,7 @@ function. The args ARG and NOCONFIRM, passed from
31883191 "Hide a package under point in Package Menu.
31893192If optional arg BUTTON is non-nil, describe its associated package."
31903193 (interactive)
3194+ (package--ensure-package-menu-mode)
31913195 (declare (interactive-only "change `package-hidden-regexps' instead."))
31923196 (let* ((name (when (derived-mode-p 'package-menu-mode)
31933197 (concat "\\`" (regexp-quote (symbol-name (package-desc-name
@@ -3221,6 +3225,7 @@ If optional arg BUTTON is non-nil, describe its associated package."
32213225(defun package-menu-mark-delete (&optional _num)
32223226 "Mark a package for deletion and move to the next line."
32233227 (interactive "p")
3228+ (package--ensure-package-menu-mode)
32243229 (if (member (package-menu-get-status)
32253230 '("installed" "dependency" "obsolete" "unsigned"))
32263231 (tabulated-list-put-tag "D" t)
@@ -3229,24 +3234,28 @@ If optional arg BUTTON is non-nil, describe its associated package."
32293234(defun package-menu-mark-install (&optional _num)
32303235 "Mark a package for installation and move to the next line."
32313236 (interactive "p")
3237+ (package--ensure-package-menu-mode)
32323238 (if (member (package-menu-get-status) '("available" "avail-obso" "new" "dependency"))
32333239 (tabulated-list-put-tag "I" t)
32343240 (forward-line)))
32353241
32363242(defun package-menu-mark-unmark (&optional _num)
32373243 "Clear any marks on a package and move to the next line."
32383244 (interactive "p")
3245+ (package--ensure-package-menu-mode)
32393246 (tabulated-list-put-tag " " t))
32403247
32413248(defun package-menu-backup-unmark ()
32423249 "Back up one line and clear any marks on that package."
32433250 (interactive)
3251+ (package--ensure-package-menu-mode)
32443252 (forward-line -1)
32453253 (tabulated-list-put-tag " "))
32463254
32473255(defun package-menu-mark-obsolete-for-deletion ()
32483256 "Mark all obsolete packages for deletion."
32493257 (interactive)
3258+ (package--ensure-package-menu-mode)
32503259 (save-excursion
32513260 (goto-char (point-min))
32523261 (while (not (eobp))
@@ -3277,6 +3286,7 @@ If optional arg BUTTON is non-nil, describe its associated package."
32773286 "Show short key binding help for `package-menu-mode'.
32783287The full list of keys can be viewed with \\[describe-mode]."
32793288 (interactive)
3289+ (package--ensure-package-menu-mode)
32803290 (message (mapconcat #'package--prettify-quick-help-key
32813291 package--quick-help-keys "\n")))
32823292
@@ -3285,6 +3295,7 @@ The full list of keys can be viewed with \\[describe-mode]."
32853295
32863296(defun package-menu-get-status ()
32873297 "Return status text of package at point in Package Menu."
3298+ (package--ensure-package-menu-mode)
32883299 (let* ((id (tabulated-list-get-id))
32893300 (entry (and id (assoc id tabulated-list-entries))))
32903301 (if entry
@@ -3340,8 +3351,6 @@ corresponding to the newer version."
33403351(defun package-menu--mark-upgrades-1 ()
33413352 "Mark all upgradable packages in the Package Menu.
33423353Implementation of `package-menu-mark-upgrades'."
3343- (unless (derived-mode-p 'package-menu-mode)
3344- (error "The current buffer is not a Package Menu"))
33453354 (setq package-menu--mark-upgrades-pending nil)
33463355 (let ((upgrades (package-menu--find-upgrades)))
33473356 (if (null upgrades)
@@ -3373,6 +3382,7 @@ If there's an async refresh operation in progress, the flags will
33733382be placed as part of `package-menu--post-refresh' instead of
33743383immediately."
33753384 (interactive)
3385+ (package--ensure-package-menu-mode)
33763386 (if (not package--downloads-in-progress)
33773387 (package-menu--mark-upgrades-1)
33783388 (setq package-menu--mark-upgrades-pending t)
@@ -3466,8 +3476,7 @@ Packages marked for installation are downloaded and installed;
34663476packages marked for deletion are removed.
34673477Optional argument NOQUERY non-nil means do not ask the user to confirm."
34683478 (interactive)
3469- (unless (derived-mode-p 'package-menu-mode)
3470- (error "The current buffer is not in Package Menu mode"))
3479+ (package--ensure-package-menu-mode)
34713480 (let (install-list delete-list cmd pkg-desc)
34723481 (save-excursion
34733482 (goto-char (point-min))
@@ -3646,7 +3655,7 @@ short description."
36463655 (package-menu-mode)
36473656
36483657 ;; Fetch the remote list of packages.
3649- (unless no-fetch (package-menu--refresh))
3658+ (unless no-fetch (package-menu--refresh-contents ))
36503659
36513660 ;; If we're not async, this would be redundant.
36523661 (when package-menu-async
@@ -3693,6 +3702,7 @@ Statuses available include \"incompat\", \"available\",
36933702 (interactive
36943703 (list (completing-read-multiple
36953704 "Keywords (comma separated): " (package-all-keywords))))
3705+ (package--ensure-package-menu-mode)
36963706 (package-show-package-list t (if (stringp keyword)
36973707 (list keyword)
36983708 keyword)))
@@ -3702,6 +3712,7 @@ Statuses available include \"incompat\", \"available\",
37023712Show only those items whose name matches the regular expression
37033713NAME. If NAME is nil or the empty string, show all packages."
37043714 (interactive (list (read-from-minibuffer "Filter by name (regexp): ")))
3715+ (package--ensure-package-menu-mode)
37053716 (if (or (not name) (string-empty-p name))
37063717 (package-show-package-list t nil)
37073718 ;; 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."
37193730(defun package-menu-clear-filter ()
37203731 "Clear any filter currently applied to the \"*Packages*\" buffer."
37213732 (interactive)
3733+ (package--ensure-package-menu-mode)
37223734 (package-menu--generate t t))
37233735
37243736(defun package-list-packages-no-fetch ()
0 commit comments