diff options
Diffstat (limited to 'lisp/package')
| -rw-r--r-- | lisp/package/package-compile.el | 111 | ||||
| -rw-r--r-- | lisp/package/package-core.el | 927 | ||||
| -rw-r--r-- | lisp/package/package-describe.el | 419 | ||||
| -rw-r--r-- | lisp/package/package-elpa.el | 629 | ||||
| -rw-r--r-- | lisp/package/package-install.el | 1053 | ||||
| -rw-r--r-- | lisp/package/package-menu.el | 1580 | ||||
| -rw-r--r-- | lisp/package/package-misc.el | 129 | ||||
| -rw-r--r-- | lisp/package/package-quickstart.el | 151 | ||||
| -rw-r--r-- | lisp/package/package-vc.el | 1003 | ||||
| -rw-r--r-- | lisp/package/package.el | 151 |
10 files changed, 6153 insertions, 0 deletions
diff --git a/lisp/package/package-compile.el b/lisp/package/package-compile.el new file mode 100644 index 00000000000..ffe94880efd --- /dev/null +++ b/lisp/package/package-compile.el | |||
| @@ -0,0 +1,111 @@ | |||
| 1 | ;;; package-compile.el --- Byte-Compilation of Packages -*- lexical-binding: t; -*- | ||
| 2 | |||
| 3 | ;; Copyright (C) 2025 Philip Kaludercic | ||
| 4 | |||
| 5 | ;; Author: Philip Kaludercic <philipk@posteo.net> | ||
| 6 | |||
| 7 | ;; This program is free software; you can redistribute it and/or modify | ||
| 8 | ;; it under the terms of the GNU General Public License as published by | ||
| 9 | ;; the Free Software Foundation, either version 3 of the License, or | ||
| 10 | ;; (at your option) any later version. | ||
| 11 | |||
| 12 | ;; This program is distributed in the hope that it will be useful, | ||
| 13 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 14 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 15 | ;; GNU General Public License for more details. | ||
| 16 | |||
| 17 | ;; You should have received a copy of the GNU General Public License | ||
| 18 | ;; along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
| 19 | |||
| 20 | ;;; Commentary: | ||
| 21 | |||
| 22 | ;;; Code: | ||
| 23 | |||
| 24 | (require 'package-core) | ||
| 25 | |||
| 26 | (defvar warning-minimum-level) | ||
| 27 | (defvar byte-compile-ignore-files) | ||
| 28 | |||
| 29 | (defcustom package-native-compile nil | ||
| 30 | "Non-nil means to natively compile packages as part of their installation. | ||
| 31 | This controls ahead-of-time compilation of packages when they are | ||
| 32 | installed. If this option is nil, packages will be natively | ||
| 33 | compiled when they are loaded for the first time. | ||
| 34 | |||
| 35 | This option does not have any effect if Emacs was not built with | ||
| 36 | native compilation support." | ||
| 37 | :type '(boolean) | ||
| 38 | :risky t | ||
| 39 | :version "28.1" | ||
| 40 | :group 'package) | ||
| 41 | |||
| 42 | (defun package--parse-elpaignore (pkg-desc) | ||
| 43 | "Return a list of regular expressions to match files ignored by PKG-DESC." | ||
| 44 | (let* ((pkg-dir (file-name-as-directory (package-desc-dir pkg-desc))) | ||
| 45 | (ignore (expand-file-name ".elpaignore" pkg-dir)) | ||
| 46 | files) | ||
| 47 | (when (file-exists-p ignore) | ||
| 48 | (with-temp-buffer | ||
| 49 | (insert-file-contents ignore) | ||
| 50 | (goto-char (point-min)) | ||
| 51 | (while (not (eobp)) | ||
| 52 | (push (wildcard-to-regexp | ||
| 53 | (let ((line (buffer-substring | ||
| 54 | (line-beginning-position) | ||
| 55 | (line-end-position)))) | ||
| 56 | (file-name-concat pkg-dir (string-trim-left line "/")))) | ||
| 57 | files) | ||
| 58 | (forward-line))) | ||
| 59 | files))) | ||
| 60 | |||
| 61 | (defun package--compile (pkg-desc) | ||
| 62 | "Byte-compile installed package PKG-DESC. | ||
| 63 | This assumes that `pkg-desc' has already been activated with | ||
| 64 | `package-activate-1'." | ||
| 65 | (let ((byte-compile-ignore-files (package--parse-elpaignore pkg-desc)) | ||
| 66 | (warning-minimum-level :error) | ||
| 67 | (load-path load-path)) | ||
| 68 | (byte-recompile-directory (package-desc-dir pkg-desc) 0 t))) | ||
| 69 | |||
| 70 | (defun package--native-compile-async (pkg-desc) | ||
| 71 | "Native compile installed package PKG-DESC asynchronously. | ||
| 72 | This assumes that `pkg-desc' has already been activated with | ||
| 73 | `package-activate-1'." | ||
| 74 | (when (native-comp-available-p) | ||
| 75 | (let ((warning-minimum-level :error)) | ||
| 76 | (native-compile-async (package-desc-dir pkg-desc) t)))) | ||
| 77 | |||
| 78 | |||
| 79 | |||
| 80 | ;;;###autoload | ||
| 81 | (defun package-recompile (pkg) | ||
| 82 | "Byte-compile package PKG again. | ||
| 83 | PKG should be either a symbol, the package name, or a `package-desc' | ||
| 84 | object." | ||
| 85 | (interactive (list (intern (completing-read | ||
| 86 | "Recompile package: " | ||
| 87 | (mapcar #'symbol-name | ||
| 88 | (mapcar #'car package-alist)))))) | ||
| 89 | (let ((pkg-desc (if (package-desc-p pkg) | ||
| 90 | pkg | ||
| 91 | (cadr (assq pkg package-alist))))) | ||
| 92 | ;; Delete the old .elc files to ensure that we don't inadvertently | ||
| 93 | ;; load them (in case they contain byte code/macros that are now | ||
| 94 | ;; invalid). | ||
| 95 | (dolist (elc (directory-files-recursively | ||
| 96 | (package-desc-dir pkg-desc) "\\.elc\\'")) | ||
| 97 | (delete-file elc)) | ||
| 98 | (package--compile pkg-desc))) | ||
| 99 | |||
| 100 | ;;;###autoload | ||
| 101 | (defun package-recompile-all () | ||
| 102 | "Byte-compile all installed packages. | ||
| 103 | This is meant to be used only in the case the byte-compiled files | ||
| 104 | are invalid due to changed byte-code, macros or the like." | ||
| 105 | (interactive) | ||
| 106 | (pcase-dolist (`(_ ,pkg-desc) package-alist) | ||
| 107 | (with-demoted-errors "Error while recompiling: %S" | ||
| 108 | (package-recompile pkg-desc)))) | ||
| 109 | |||
| 110 | (provide 'package-compile) | ||
| 111 | ;;; package-compile.el ends here | ||
diff --git a/lisp/package/package-core.el b/lisp/package/package-core.el new file mode 100644 index 00000000000..83a04402705 --- /dev/null +++ b/lisp/package/package-core.el | |||
| @@ -0,0 +1,927 @@ | |||
| 1 | ;;; package-core.el --- Core of the Emacs Package Manager -*- lexical-binding:t -*- | ||
| 2 | |||
| 3 | ;; Copyright (C) 2007-2025 Free Software Foundation, Inc. | ||
| 4 | |||
| 5 | ;; Author: Tom Tromey <tromey@redhat.com> | ||
| 6 | ;; Daniel Hackney <dan@haxney.org> | ||
| 7 | ;; Created: 10 Mar 2007 | ||
| 8 | ;; Version: 1.1.0 | ||
| 9 | ;; Keywords: tools | ||
| 10 | ;; Package-Requires: ((tabulated-list "1.0")) | ||
| 11 | |||
| 12 | ;; This file is part of GNU Emacs. | ||
| 13 | |||
| 14 | ;; GNU Emacs is free software: you can redistribute it and/or modify | ||
| 15 | ;; it under the terms of the GNU General Public License as published by | ||
| 16 | ;; the Free Software Foundation, either version 3 of the License, or | ||
| 17 | ;; (at your option) any later version. | ||
| 18 | |||
| 19 | ;; GNU Emacs is distributed in the hope that it will be useful, | ||
| 20 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 21 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 22 | ;; GNU General Public License for more details. | ||
| 23 | |||
| 24 | ;; You should have received a copy of the GNU General Public License | ||
| 25 | ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>. | ||
| 26 | |||
| 27 | ;;; Commentary: | ||
| 28 | |||
| 29 | ;; TODO | ||
| 30 | |||
| 31 | ;;; Code: | ||
| 32 | |||
| 33 | (eval-and-compile (require 'cl-lib)) | ||
| 34 | (eval-when-compile (require 'epg)) ;For setf accessors. | ||
| 35 | (eval-when-compile (require 'inline)) ;For `define-inline' | ||
| 36 | |||
| 37 | (defvar package--default-summary "No description available.") | ||
| 38 | |||
| 39 | (defvar package-list-unversioned nil | ||
| 40 | "If non-nil, include packages that don't have a version in `list-packages'.") | ||
| 41 | |||
| 42 | (defvar package-list-unsigned nil | ||
| 43 | "If non-nil, mention in the list which packages were installed without signature.") | ||
| 44 | |||
| 45 | (defvar package--emacs-version-list (version-to-list emacs-version) | ||
| 46 | "The value of variable `emacs-version' as a list.") | ||
| 47 | |||
| 48 | (define-inline package-vc-p (pkg-desc) | ||
| 49 | "Return non-nil if PKG-DESC is a VC package." | ||
| 50 | (inline-letevals (pkg-desc) | ||
| 51 | (inline-quote (eq (package-desc-kind ,pkg-desc) 'vc)))) | ||
| 52 | |||
| 53 | (cl-defstruct (package-desc | ||
| 54 | ;; Rename the default constructor from `make-package-desc'. | ||
| 55 | (:constructor package-desc-create) | ||
| 56 | ;; Has the same interface as the old `define-package', | ||
| 57 | ;; which is still used in the "foo-pkg.el" files. Extra | ||
| 58 | ;; options can be supported by adding additional keys. | ||
| 59 | (:constructor | ||
| 60 | package-desc-from-define | ||
| 61 | (name-string version-string &optional summary requirements | ||
| 62 | &rest rest-plist | ||
| 63 | &aux | ||
| 64 | (name (intern name-string)) | ||
| 65 | (version (if (eq (car-safe version-string) 'vc) | ||
| 66 | (version-to-list (cdr version-string)) | ||
| 67 | (version-to-list version-string))) | ||
| 68 | (reqs (mapcar (lambda (elt) | ||
| 69 | (list (car elt) | ||
| 70 | (version-to-list (cadr elt)))) | ||
| 71 | (if (eq 'quote (car requirements)) | ||
| 72 | (nth 1 requirements) | ||
| 73 | requirements))) | ||
| 74 | (kind (plist-get rest-plist :kind)) | ||
| 75 | (archive (plist-get rest-plist :archive)) | ||
| 76 | (extras (let (alist) | ||
| 77 | (while rest-plist | ||
| 78 | (unless (memq (car rest-plist) '(:kind :archive)) | ||
| 79 | (let ((value (cadr rest-plist))) | ||
| 80 | (when value | ||
| 81 | (push (cons (car rest-plist) | ||
| 82 | (if (eq (car-safe value) 'quote) | ||
| 83 | (cadr value) | ||
| 84 | value)) | ||
| 85 | alist)))) | ||
| 86 | (setq rest-plist (cddr rest-plist))) | ||
| 87 | alist))))) | ||
| 88 | "Structure containing information about an individual package. | ||
| 89 | Slots: | ||
| 90 | |||
| 91 | `name' Name of the package, as a symbol. | ||
| 92 | |||
| 93 | `version' Version of the package, as a version list. | ||
| 94 | |||
| 95 | `summary' Short description of the package, typically taken from | ||
| 96 | the first line of the file. | ||
| 97 | |||
| 98 | `reqs' Requirements of the package. A list of (PACKAGE | ||
| 99 | VERSION-LIST) naming the dependent package and the minimum | ||
| 100 | required version. | ||
| 101 | |||
| 102 | `kind' The distribution format of the package. Currently, it is | ||
| 103 | either `single' or `tar'. | ||
| 104 | |||
| 105 | `archive' The name of the archive (as a string) whence this | ||
| 106 | package came. | ||
| 107 | |||
| 108 | `dir' The directory where the package is installed (if installed), | ||
| 109 | `builtin' if it is built-in, or nil otherwise. | ||
| 110 | |||
| 111 | `extras' Optional alist of additional keyword-value pairs. | ||
| 112 | |||
| 113 | `signed' Flag to indicate that the package is signed by provider." | ||
| 114 | name | ||
| 115 | version | ||
| 116 | (summary package--default-summary) | ||
| 117 | reqs | ||
| 118 | kind | ||
| 119 | archive | ||
| 120 | dir | ||
| 121 | extras | ||
| 122 | signed) | ||
| 123 | |||
| 124 | (cl-defstruct (package--bi-desc | ||
| 125 | (:constructor package-make-builtin (version summary)) | ||
| 126 | (:type vector)) | ||
| 127 | "Package descriptor format used in finder-inf.el and package--builtins." | ||
| 128 | version | ||
| 129 | reqs | ||
| 130 | summary) | ||
| 131 | |||
| 132 | (defun package--from-builtin (bi-desc) | ||
| 133 | "Create a `package-desc' object from BI-DESC. | ||
| 134 | BI-DESC should be a `package--bi-desc' object." | ||
| 135 | (package-desc-create :name (pop bi-desc) | ||
| 136 | :version (package--bi-desc-version bi-desc) | ||
| 137 | :summary (package--bi-desc-summary bi-desc) | ||
| 138 | :dir 'builtin)) | ||
| 139 | |||
| 140 | (defun package-desc--keywords (pkg-desc) | ||
| 141 | "Return keywords of package-desc object PKG-DESC. | ||
| 142 | These keywords come from the foo-pkg.el file, and in general | ||
| 143 | corresponds to the keywords in the \"Keywords\" header of the | ||
| 144 | package." | ||
| 145 | (let ((keywords (cdr (assoc :keywords (package-desc-extras pkg-desc))))) | ||
| 146 | (if (eq (car-safe keywords) 'quote) | ||
| 147 | (nth 1 keywords) | ||
| 148 | keywords))) | ||
| 149 | |||
| 150 | (defun package--read-pkg-desc (kind) | ||
| 151 | "Read a `define-package' form in current buffer. | ||
| 152 | Return the pkg-desc, with desc-kind set to KIND." | ||
| 153 | (goto-char (point-min)) | ||
| 154 | (let* ((pkg-def-parsed (read (current-buffer))) | ||
| 155 | (pkg-desc | ||
| 156 | (when (eq (car pkg-def-parsed) 'define-package) | ||
| 157 | (apply #'package-desc-from-define | ||
| 158 | (append (cdr pkg-def-parsed)))))) | ||
| 159 | (when pkg-desc | ||
| 160 | (setf (package-desc-kind pkg-desc) kind) | ||
| 161 | pkg-desc))) | ||
| 162 | |||
| 163 | (defgroup package nil | ||
| 164 | "Manager for Emacs Lisp packages." | ||
| 165 | :group 'applications | ||
| 166 | :version "24.1") | ||
| 167 | |||
| 168 | |||
| 169 | ;;; Customization options | ||
| 170 | |||
| 171 | ;;;###autoload | ||
| 172 | (defcustom package-enable-at-startup t | ||
| 173 | "Whether to make installed packages available when Emacs starts. | ||
| 174 | If non-nil, packages are made available before reading the init | ||
| 175 | file (but after reading the early init file). This means that if | ||
| 176 | you wish to set this variable, you must do so in the early init | ||
| 177 | file. Regardless of the value of this variable, packages are not | ||
| 178 | made available if `user-init-file' is nil (e.g. Emacs was started | ||
| 179 | with \"-q\"). | ||
| 180 | |||
| 181 | Even if the value is nil, you can type \\[package-initialize] to | ||
| 182 | make installed packages available at any time, or you can | ||
| 183 | call (package-activate-all) in your init-file. | ||
| 184 | |||
| 185 | Note that this variable must be set to a non-default value in | ||
| 186 | your early-init file, as the variable's value is used before | ||
| 187 | loading the regular init file. Therefore, if you customize it | ||
| 188 | via Customize, you should save your customized setting into | ||
| 189 | your `early-init-file'." | ||
| 190 | :type 'boolean | ||
| 191 | :version "24.1") | ||
| 192 | |||
| 193 | (defcustom package-load-list '(all) | ||
| 194 | "List of packages for `package-activate-all' to make available. | ||
| 195 | Each element in this list should be a list (NAME VERSION), or the | ||
| 196 | symbol `all'. The symbol `all' says to make available the latest | ||
| 197 | installed versions of all packages not specified by other | ||
| 198 | elements. | ||
| 199 | |||
| 200 | For an element (NAME VERSION), NAME is a package name (a symbol). | ||
| 201 | VERSION should be t, a string, or nil. | ||
| 202 | If VERSION is t, the most recent version is made available. | ||
| 203 | If VERSION is a string, only that version is ever made available. | ||
| 204 | Any other version, even if newer, is silently ignored. | ||
| 205 | Hence, the package is \"held\" at that version. | ||
| 206 | If VERSION is nil, the package is not made available (it is \"disabled\")." | ||
| 207 | :type '(repeat (choice (const all) | ||
| 208 | (list :tag "Specific package" | ||
| 209 | (symbol :tag "Package name") | ||
| 210 | (choice :tag "Version" | ||
| 211 | (const :tag "disable" nil) | ||
| 212 | (const :tag "most recent" t) | ||
| 213 | (string :tag "specific version"))))) | ||
| 214 | :risky t | ||
| 215 | :version "24.1") | ||
| 216 | |||
| 217 | (defcustom package-pinned-packages nil | ||
| 218 | "An alist of packages that are pinned to specific archives. | ||
| 219 | This can be useful if you have multiple package archives enabled, | ||
| 220 | and want to control which archive a given package gets installed from. | ||
| 221 | |||
| 222 | Each element of the alist has the form (PACKAGE . ARCHIVE), where: | ||
| 223 | PACKAGE is a symbol representing a package | ||
| 224 | ARCHIVE is a string representing an archive (it should be the car of | ||
| 225 | an element in `package-archives', e.g. \"gnu\"). | ||
| 226 | |||
| 227 | Adding an entry to this variable means that only ARCHIVE will be | ||
| 228 | considered as a source for PACKAGE. If other archives provide PACKAGE, | ||
| 229 | they are ignored (for this package). If ARCHIVE does not contain PACKAGE, | ||
| 230 | the package will be unavailable." | ||
| 231 | :type '(alist :key-type (symbol :tag "Package") | ||
| 232 | :value-type (string :tag "Archive name")) | ||
| 233 | ;; This could prevent you from receiving updates for a package, | ||
| 234 | ;; via an entry (PACKAGE . NON-EXISTING). Which could be an issue | ||
| 235 | ;; if PACKAGE has a known vulnerability that is fixed in newer versions. | ||
| 236 | :risky t | ||
| 237 | :version "24.4") | ||
| 238 | |||
| 239 | ;;;###autoload | ||
| 240 | (defcustom package-user-dir (locate-user-emacs-file "elpa") | ||
| 241 | "Directory containing the user's Emacs Lisp packages. | ||
| 242 | The directory name should be absolute. | ||
| 243 | Apart from this directory, Emacs also looks for system-wide | ||
| 244 | packages in `package-directory-list'." | ||
| 245 | :type 'directory | ||
| 246 | :initialize #'custom-initialize-delay | ||
| 247 | :risky t | ||
| 248 | :group 'applications | ||
| 249 | :version "24.1") | ||
| 250 | |||
| 251 | ;;;###autoload | ||
| 252 | (defcustom package-directory-list | ||
| 253 | ;; Defaults are subdirs named "elpa" in the site-lisp dirs. | ||
| 254 | (let (result) | ||
| 255 | (dolist (f load-path) | ||
| 256 | (and (stringp f) | ||
| 257 | (equal (file-name-nondirectory f) "site-lisp") | ||
| 258 | (push (expand-file-name "elpa" f) result))) | ||
| 259 | (nreverse result)) | ||
| 260 | "List of additional directories containing Emacs Lisp packages. | ||
| 261 | Each directory name should be absolute. | ||
| 262 | |||
| 263 | These directories contain packages intended for system-wide; in | ||
| 264 | contrast, `package-user-dir' contains packages for personal use." | ||
| 265 | :type '(repeat directory) | ||
| 266 | :initialize #'custom-initialize-delay | ||
| 267 | :group 'applications | ||
| 268 | :risky t | ||
| 269 | :version "24.1") | ||
| 270 | |||
| 271 | (defcustom package-selected-packages nil | ||
| 272 | "Store here packages installed explicitly by user. | ||
| 273 | This variable is fed automatically by Emacs when installing a new package. | ||
| 274 | This variable is used by `package-autoremove' to decide | ||
| 275 | which packages are no longer needed. | ||
| 276 | You can use it to (re)install packages on other machines | ||
| 277 | by running `package-install-selected-packages'. | ||
| 278 | |||
| 279 | To check if a package is contained in this list here, use | ||
| 280 | `package--user-selected-p', as it may populate the variable with | ||
| 281 | a sane initial value." | ||
| 282 | :version "25.1" | ||
| 283 | :type '(repeat symbol)) | ||
| 284 | |||
| 285 | ;; Pseudo fields. | ||
| 286 | (defun package-version-join (vlist) | ||
| 287 | "Return the version string corresponding to the list VLIST. | ||
| 288 | This is, approximately, the inverse of `version-to-list'. | ||
| 289 | \(Actually, it returns only one of the possible inverses, since | ||
| 290 | `version-to-list' is a many-to-one operation.)" | ||
| 291 | (if (null vlist) | ||
| 292 | "" | ||
| 293 | (let ((str-list (list "." (int-to-string (car vlist))))) | ||
| 294 | (dolist (num (cdr vlist)) | ||
| 295 | (cond | ||
| 296 | ((>= num 0) | ||
| 297 | (push (int-to-string num) str-list) | ||
| 298 | (push "." str-list)) | ||
| 299 | ((< num -4) | ||
| 300 | (error "Invalid version list `%s'" vlist)) | ||
| 301 | (t | ||
| 302 | ;; pre, or beta, or alpha | ||
| 303 | (cond ((equal "." (car str-list)) | ||
| 304 | (pop str-list)) | ||
| 305 | ((not (string-match "[0-9]+" (car str-list))) | ||
| 306 | (error "Invalid version list `%s'" vlist))) | ||
| 307 | (push (cond ((= num -1) "pre") | ||
| 308 | ((= num -2) "beta") | ||
| 309 | ((= num -3) "alpha") | ||
| 310 | ((= num -4) "snapshot")) | ||
| 311 | str-list)))) | ||
| 312 | (if (equal "." (car str-list)) | ||
| 313 | (pop str-list)) | ||
| 314 | (apply #'concat (nreverse str-list))))) | ||
| 315 | |||
| 316 | |||
| 317 | |||
| 318 | ;;; Installed packages | ||
| 319 | ;; The following variables store information about packages present in | ||
| 320 | ;; the system. The most important of these is `package-alist'. The | ||
| 321 | ;; command `package-activate-all' is also closely related to this | ||
| 322 | ;; section. | ||
| 323 | |||
| 324 | (defvar package--builtins nil | ||
| 325 | "Alist of built-in packages. | ||
| 326 | The actual value is initialized by loading the library | ||
| 327 | `finder-inf'; this is not done until it is needed, e.g. by the | ||
| 328 | function `package-built-in-p'. | ||
| 329 | |||
| 330 | Each element has the form (PKG . PACKAGE-BI-DESC), where PKG is a package | ||
| 331 | name (a symbol) and DESC is a `package--bi-desc' structure.") | ||
| 332 | (put 'package--builtins 'risky-local-variable t) | ||
| 333 | |||
| 334 | (defvar package-alist nil | ||
| 335 | "Alist of all packages available for activation. | ||
| 336 | Each element has the form (PKG . DESCS), where PKG is a package | ||
| 337 | name (a symbol) and DESCS is a non-empty list of `package-desc' | ||
| 338 | structures, sorted by decreasing versions. | ||
| 339 | |||
| 340 | This variable is set automatically by `package-load-descriptor', | ||
| 341 | called via `package-activate-all'. To change which packages are | ||
| 342 | loaded and/or activated, customize `package-load-list'.") | ||
| 343 | (put 'package-alist 'risky-local-variable t) | ||
| 344 | |||
| 345 | ;;;; Public interfaces for accessing built-in package info | ||
| 346 | |||
| 347 | (defun package-versioned-builtin-packages () | ||
| 348 | "Return a list of all the versioned built-in packages. | ||
| 349 | The return value is a list of names of built-in packages represented as | ||
| 350 | symbols." | ||
| 351 | (mapcar #'car package--builtin-versions)) | ||
| 352 | |||
| 353 | (defun package-builtin-package-version (package) | ||
| 354 | "Return the version of a built-in PACKAGE given by its symbol. | ||
| 355 | The return value is a list of integers representing the version of | ||
| 356 | PACKAGE, in the format returned by `version-to-list', or nil if the | ||
| 357 | package is built-in but has no version or is not a built-in package." | ||
| 358 | (alist-get package package--builtin-versions)) | ||
| 359 | |||
| 360 | ;;;###autoload | ||
| 361 | (defvar package-activated-list nil | ||
| 362 | ;; FIXME: This should implicitly include all builtin packages. | ||
| 363 | "List of the names of currently activated packages.") | ||
| 364 | (put 'package-activated-list 'risky-local-variable t) | ||
| 365 | |||
| 366 | ;;;; Populating `package-alist'. | ||
| 367 | |||
| 368 | ;; The following functions are called on each installed package by | ||
| 369 | ;; `package-load-all-descriptors', which ultimately populates the | ||
| 370 | ;; `package-alist' variable. | ||
| 371 | |||
| 372 | (defun package-process-define-package (exp) | ||
| 373 | "Process define-package expression EXP and push it to `package-alist'. | ||
| 374 | EXP should be a form read from a foo-pkg.el file. | ||
| 375 | Convert EXP into a `package-desc' object using the | ||
| 376 | `package-desc-from-define' constructor before pushing it to | ||
| 377 | `package-alist'. | ||
| 378 | |||
| 379 | If there already exists a package by the same name in | ||
| 380 | `package-alist', insert this object there such that the packages | ||
| 381 | are sorted with the highest version first." | ||
| 382 | (when (eq (car-safe exp) 'define-package) | ||
| 383 | (let* ((new-pkg-desc (apply #'package-desc-from-define (cdr exp))) | ||
| 384 | (name (package-desc-name new-pkg-desc)) | ||
| 385 | (version (package-desc-version new-pkg-desc)) | ||
| 386 | (old-pkgs (assq name package-alist))) | ||
| 387 | (if (null old-pkgs) | ||
| 388 | ;; If there's no old package, just add this to `package-alist'. | ||
| 389 | (push (list name new-pkg-desc) package-alist) | ||
| 390 | ;; If there is, insert the new package at the right place in the list. | ||
| 391 | (while | ||
| 392 | (if (and (cdr old-pkgs) | ||
| 393 | (version-list-< version | ||
| 394 | (package-desc-version (cadr old-pkgs)))) | ||
| 395 | (setq old-pkgs (cdr old-pkgs)) | ||
| 396 | (push new-pkg-desc (cdr old-pkgs)) | ||
| 397 | nil))) | ||
| 398 | new-pkg-desc))) | ||
| 399 | |||
| 400 | (defun package-load-descriptor (pkg-dir) | ||
| 401 | "Load the package description file in directory PKG-DIR. | ||
| 402 | Create a new `package-desc' object, add it to `package-alist' and | ||
| 403 | return it." | ||
| 404 | (let ((pkg-file (expand-file-name (package--description-file pkg-dir) | ||
| 405 | pkg-dir)) | ||
| 406 | (signed-file (concat pkg-dir ".signed"))) | ||
| 407 | (when (file-exists-p pkg-file) | ||
| 408 | (with-temp-buffer | ||
| 409 | (insert-file-contents pkg-file) | ||
| 410 | (goto-char (point-min)) | ||
| 411 | (let ((pkg-desc (or (package-process-define-package | ||
| 412 | (read (current-buffer))) | ||
| 413 | (error "Can't find define-package in %s" pkg-file)))) | ||
| 414 | (setf (package-desc-dir pkg-desc) pkg-dir) | ||
| 415 | (if (file-exists-p signed-file) | ||
| 416 | (setf (package-desc-signed pkg-desc) t)) | ||
| 417 | pkg-desc))))) | ||
| 418 | |||
| 419 | (defun package-load-all-descriptors () | ||
| 420 | "Load descriptors for installed Emacs Lisp packages. | ||
| 421 | This looks for package subdirectories in `package-user-dir' and | ||
| 422 | `package-directory-list'. The variable `package-load-list' | ||
| 423 | controls which package subdirectories may be loaded. | ||
| 424 | |||
| 425 | In each valid package subdirectory, this function loads the | ||
| 426 | description file containing a call to `define-package', which | ||
| 427 | updates `package-alist'." | ||
| 428 | (dolist (dir (cons package-user-dir package-directory-list)) | ||
| 429 | (when (file-directory-p dir) | ||
| 430 | (dolist (pkg-dir (directory-files dir t "\\`[^.]")) | ||
| 431 | (when (file-directory-p pkg-dir) | ||
| 432 | (package-load-descriptor pkg-dir)))))) | ||
| 433 | |||
| 434 | (defun package--alist () | ||
| 435 | "Return `package-alist', after computing it if needed." | ||
| 436 | (or package-alist | ||
| 437 | (progn (package-load-all-descriptors) | ||
| 438 | package-alist))) | ||
| 439 | |||
| 440 | |||
| 441 | ;;; Package activation | ||
| 442 | ;; Section for functions used by `package-activate', which see. | ||
| 443 | |||
| 444 | (defun package-disabled-p (pkg-name version) | ||
| 445 | "Return whether PKG-NAME at VERSION can be activated. | ||
| 446 | The decision is made according to `package-load-list'. | ||
| 447 | Return nil if the package can be activated. | ||
| 448 | Return t if the package is completely disabled. | ||
| 449 | Return the max version (as a string) if the package is held at a lower version." | ||
| 450 | (let ((force (assq pkg-name package-load-list))) | ||
| 451 | (cond ((null force) (not (memq 'all package-load-list))) | ||
| 452 | ((null (setq force (cadr force))) t) ; disabled | ||
| 453 | ((eq force t) nil) | ||
| 454 | ((stringp force) ; held | ||
| 455 | (unless (version-list-= version (version-to-list force)) | ||
| 456 | force)) | ||
| 457 | (t (error "Invalid element in `package-load-list'"))))) | ||
| 458 | |||
| 459 | (defun package-built-in-p (package &optional min-version) | ||
| 460 | "Return non-nil if PACKAGE is built-in to Emacs. | ||
| 461 | Optional arg MIN-VERSION, if non-nil, should be a version list | ||
| 462 | specifying the minimum acceptable version." | ||
| 463 | (if (package-desc-p package) ;; was built-in and then was converted | ||
| 464 | (eq 'builtin (package-desc-dir package)) | ||
| 465 | (let ((bi (assq package package--builtin-versions))) | ||
| 466 | (cond | ||
| 467 | (bi (version-list-<= min-version (cdr bi))) | ||
| 468 | ((remove 0 min-version) nil) | ||
| 469 | (t | ||
| 470 | (require 'finder-inf nil t) ; For `package--builtins'. | ||
| 471 | (assq package package--builtins)))))) | ||
| 472 | |||
| 473 | (defun package--active-built-in-p (package) | ||
| 474 | "Return non-nil if the built-in version of PACKAGE is used. | ||
| 475 | If the built-in version of PACKAGE is used and PACKAGE is | ||
| 476 | also available for installation from an archive, it is an | ||
| 477 | indication that PACKAGE was never upgraded to any newer | ||
| 478 | version from the archive." | ||
| 479 | (and (not (assq (cond | ||
| 480 | ((package-desc-p package) | ||
| 481 | (package-desc-name package)) | ||
| 482 | ((stringp package) (intern package)) | ||
| 483 | ((symbolp package) package) | ||
| 484 | ((error "Unknown package format: %S" package))) | ||
| 485 | (package--alist))) | ||
| 486 | (package-built-in-p package))) | ||
| 487 | |||
| 488 | (defun package--autoloads-file-name (pkg-desc) | ||
| 489 | "Return the absolute name of the autoloads file, sans extension. | ||
| 490 | PKG-DESC is a `package-desc' object." | ||
| 491 | (expand-file-name | ||
| 492 | (format "%s-autoloads" (package-desc-name pkg-desc)) | ||
| 493 | (package-desc-dir pkg-desc))) | ||
| 494 | |||
| 495 | (defvar Info-directory-list) | ||
| 496 | (declare-function info-initialize "info" ()) | ||
| 497 | |||
| 498 | (defvar package--quickstart-pkgs t | ||
| 499 | "If set to a list, we're computing the set of pkgs to activate.") | ||
| 500 | |||
| 501 | (defsubst package--library-stem (file) | ||
| 502 | (catch 'done | ||
| 503 | (let (result) | ||
| 504 | (dolist (suffix (get-load-suffixes) file) | ||
| 505 | (setq result (string-trim file nil suffix)) | ||
| 506 | (unless (equal file result) | ||
| 507 | (throw 'done result)))))) | ||
| 508 | |||
| 509 | (defun package--reload-previously-loaded (pkg-desc &optional warn) | ||
| 510 | "Force reimportation of files in PKG-DESC already present in `load-history'. | ||
| 511 | New editions of files contain macro definitions and | ||
| 512 | redefinitions, the overlooking of which would cause | ||
| 513 | byte-compilation of the new package to fail. | ||
| 514 | If WARN is a string, display a warning (using WARN as a format string) | ||
| 515 | before reloading the files. WARN must have two %-sequences | ||
| 516 | corresponding to package name (a symbol) and a list of files loaded (as | ||
| 517 | sexps)." | ||
| 518 | (with-demoted-errors "Error in package--load-files-for-activation: %s" | ||
| 519 | (let* (result | ||
| 520 | (dir (package-desc-dir pkg-desc)) | ||
| 521 | ;; A previous implementation would skip `dir' itself. | ||
| 522 | ;; However, in normal use reloading from the same directory | ||
| 523 | ;; never happens anyway, while in certain cases external to | ||
| 524 | ;; Emacs a package in the same directory not necessary | ||
| 525 | ;; stays byte-identical, e.g. during development. Just | ||
| 526 | ;; don't special-case `dir'. | ||
| 527 | (effective-path (or (bound-and-true-p find-library-source-path) | ||
| 528 | load-path)) | ||
| 529 | (files (directory-files-recursively dir "\\`[^\\.].*\\.el\\'")) | ||
| 530 | (history (mapcar #'file-truename | ||
| 531 | (cl-remove-if-not #'stringp | ||
| 532 | (mapcar #'car load-history))))) | ||
| 533 | (dolist (file files) | ||
| 534 | (when-let* ((library (package--library-stem | ||
| 535 | (file-relative-name file dir))) | ||
| 536 | (canonical (locate-library library nil effective-path)) | ||
| 537 | (truename (file-truename canonical)) | ||
| 538 | ;; Normally, all files in a package are compiled by | ||
| 539 | ;; now, but don't assume that. E.g. different | ||
| 540 | ;; versions can add or remove `no-byte-compile'. | ||
| 541 | (altname (if (string-suffix-p ".el" truename) | ||
| 542 | (replace-regexp-in-string | ||
| 543 | "\\.el\\'" ".elc" truename t) | ||
| 544 | (replace-regexp-in-string | ||
| 545 | "\\.elc\\'" ".el" truename t))) | ||
| 546 | (found (or (member truename history) | ||
| 547 | (and (not (string= altname truename)) | ||
| 548 | (member altname history)))) | ||
| 549 | (recent-index (length found))) | ||
| 550 | (unless (equal (file-name-base library) | ||
| 551 | (format "%s-autoloads" (package-desc-name pkg-desc))) | ||
| 552 | (push (cons (expand-file-name library dir) recent-index) result)))) | ||
| 553 | (when (and result warn) | ||
| 554 | (display-warning 'package | ||
| 555 | (format warn (package-desc-name pkg-desc) | ||
| 556 | (mapcar #'car result)))) | ||
| 557 | (mapc (lambda (c) (load (car c) nil t)) | ||
| 558 | (sort result (lambda (x y) (< (cdr x) (cdr y)))))))) | ||
| 559 | |||
| 560 | (defun package-desc-full-name (pkg-desc) | ||
| 561 | "Return full name of package-desc object PKG-DESC. | ||
| 562 | This is the name of the package with its version appended." | ||
| 563 | (if (package-vc-p pkg-desc) | ||
| 564 | (symbol-name (package-desc-name pkg-desc)) | ||
| 565 | (format "%s-%s" | ||
| 566 | (package-desc-name pkg-desc) | ||
| 567 | (package-version-join (package-desc-version pkg-desc))))) | ||
| 568 | |||
| 569 | (defun package-activate-1 (pkg-desc &optional reload deps) | ||
| 570 | "Activate package given by PKG-DESC, even if it was already active. | ||
| 571 | If DEPS is non-nil, also activate its dependencies (unless they | ||
| 572 | are already activated). | ||
| 573 | If RELOAD is non-nil, also `load' any files inside the package which | ||
| 574 | correspond to previously loaded files." | ||
| 575 | (let* ((name (package-desc-name pkg-desc)) | ||
| 576 | (pkg-dir (package-desc-dir pkg-desc))) | ||
| 577 | (unless pkg-dir | ||
| 578 | (error "Internal error: unable to find directory for `%s'" | ||
| 579 | (package-desc-full-name pkg-desc))) | ||
| 580 | (catch 'exit | ||
| 581 | ;; Activate its dependencies recursively. | ||
| 582 | ;; FIXME: This doesn't check whether the activated version is the | ||
| 583 | ;; required version. | ||
| 584 | (when deps | ||
| 585 | (dolist (req (package-desc-reqs pkg-desc)) | ||
| 586 | (unless (package-activate (car req)) | ||
| 587 | (message "Unable to activate package `%s'.\nRequired package `%s-%s' is unavailable" | ||
| 588 | name (car req) (package-version-join (cadr req))) | ||
| 589 | (throw 'exit nil)))) | ||
| 590 | (if (listp package--quickstart-pkgs) | ||
| 591 | ;; We're only collecting the set of packages to activate! | ||
| 592 | (push pkg-desc package--quickstart-pkgs) | ||
| 593 | (when (or reload (assq name package--builtin-versions)) | ||
| 594 | (package--reload-previously-loaded | ||
| 595 | pkg-desc (unless reload | ||
| 596 | "Package %S is activated too late. | ||
| 597 | The following files have already been loaded: %S"))) | ||
| 598 | (with-demoted-errors "Error loading autoloads: %s" | ||
| 599 | (load (package--autoloads-file-name pkg-desc) nil t))) | ||
| 600 | ;; Add info node. | ||
| 601 | (when (file-exists-p (expand-file-name "dir" pkg-dir)) | ||
| 602 | ;; FIXME: not the friendliest, but simple. | ||
| 603 | (require 'info) | ||
| 604 | (info-initialize) | ||
| 605 | (add-to-list 'Info-directory-list pkg-dir)) | ||
| 606 | (push name package-activated-list) | ||
| 607 | ;; Don't return nil. | ||
| 608 | t))) | ||
| 609 | |||
| 610 | ;;;; `package-activate' | ||
| 611 | |||
| 612 | (defun package--get-activatable-pkg (pkg-name) | ||
| 613 | ;; Is "activatable" a word? | ||
| 614 | (let ((pkg-descs (cdr (assq pkg-name package-alist)))) | ||
| 615 | ;; Check if PACKAGE is available in `package-alist'. | ||
| 616 | (while | ||
| 617 | (when pkg-descs | ||
| 618 | (let ((available-version (package-desc-version (car pkg-descs)))) | ||
| 619 | (or (package-disabled-p pkg-name available-version) | ||
| 620 | ;; Prefer a builtin package. | ||
| 621 | (package-built-in-p pkg-name available-version)))) | ||
| 622 | (setq pkg-descs (cdr pkg-descs))) | ||
| 623 | (car pkg-descs))) | ||
| 624 | |||
| 625 | (defvar package--initialized nil | ||
| 626 | "Non-nil if `package-initialize' has been run.") | ||
| 627 | |||
| 628 | ;; This function activates a newer version of a package if an older | ||
| 629 | ;; one was already activated. It also loads a features of this | ||
| 630 | ;; package which were already loaded. | ||
| 631 | (defun package-activate (package &optional force) | ||
| 632 | "Activate the package named PACKAGE. | ||
| 633 | If FORCE is true, (re-)activate it if it's already activated. | ||
| 634 | Newer versions are always activated, regardless of FORCE." | ||
| 635 | (let ((pkg-desc (package--get-activatable-pkg package))) | ||
| 636 | (cond | ||
| 637 | ;; If no such package is found, maybe it's built-in. | ||
| 638 | ((null pkg-desc) | ||
| 639 | (package-built-in-p package)) | ||
| 640 | ;; If the package is already activated, just return t. | ||
| 641 | ((and (memq package package-activated-list) (not force)) | ||
| 642 | t) | ||
| 643 | ;; Otherwise, proceed with activation. | ||
| 644 | (t (package-activate-1 pkg-desc nil 'deps))))) | ||
| 645 | |||
| 646 | |||
| 647 | ;;; Installation -- Local operations | ||
| 648 | ;; This section contains a variety of features regarding installing a | ||
| 649 | ;; package to/from disk. This includes autoload generation, | ||
| 650 | ;; unpacking, compiling, as well as defining a package from the | ||
| 651 | ;; current buffer. | ||
| 652 | |||
| 653 | ;;;; Unpacking | ||
| 654 | |||
| 655 | ;;;###autoload | ||
| 656 | (defvar package--activated nil | ||
| 657 | "Non-nil if `package-activate-all' has been run.") | ||
| 658 | |||
| 659 | (declare-function package-read-all-archive-contents "package-elpa" ()) | ||
| 660 | |||
| 661 | (defvar package--compatibility-table nil | ||
| 662 | "Hash table connecting package names to their compatibility. | ||
| 663 | Each key is a symbol, the name of a package. | ||
| 664 | |||
| 665 | The value is either nil, representing an incompatible package, or | ||
| 666 | a version list, representing the highest compatible version of | ||
| 667 | that package which is available. | ||
| 668 | |||
| 669 | A package is considered incompatible if it requires an Emacs | ||
| 670 | version higher than the one being used. To check for package | ||
| 671 | \(in)compatibility, don't read this table directly, use | ||
| 672 | `package--incompatible-p' which also checks dependencies.") | ||
| 673 | |||
| 674 | (defun package--add-to-compatibility-table (pkg) | ||
| 675 | "If PKG is compatible (without dependencies), add to the compatibility table. | ||
| 676 | PKG is a package-desc object. | ||
| 677 | Only adds if its version is higher than what's already stored in | ||
| 678 | the table." | ||
| 679 | (unless (package--incompatible-p pkg 'shallow) | ||
| 680 | (let* ((name (package-desc-name pkg)) | ||
| 681 | (version (or (package-desc-version pkg) '(0))) | ||
| 682 | (table-version (gethash name package--compatibility-table))) | ||
| 683 | (when (or (not table-version) | ||
| 684 | (version-list-< table-version version)) | ||
| 685 | (puthash name version package--compatibility-table))))) | ||
| 686 | |||
| 687 | (defun package--mapc (function &optional packages) | ||
| 688 | "Call FUNCTION for all known PACKAGES. | ||
| 689 | PACKAGES can be nil or t, which means to display all known | ||
| 690 | packages, or a list of packages. | ||
| 691 | |||
| 692 | Built-in packages are converted with `package--from-builtin'." | ||
| 693 | (unless packages (setq packages t)) | ||
| 694 | (let (name) | ||
| 695 | ;; Installed packages: | ||
| 696 | (dolist (elt package-alist) | ||
| 697 | (setq name (car elt)) | ||
| 698 | (when (or (eq packages t) (memq name packages)) | ||
| 699 | (mapc function (cdr elt)))) | ||
| 700 | |||
| 701 | ;; Built-in packages: | ||
| 702 | (dolist (elt package--builtins) | ||
| 703 | (setq name (car elt)) | ||
| 704 | (when (and (not (eq name 'emacs)) ; Hide the `emacs' package. | ||
| 705 | (or package-list-unversioned | ||
| 706 | (package--bi-desc-version (cdr elt))) | ||
| 707 | (or (eq packages t) (memq name packages))) | ||
| 708 | (funcall function (package--from-builtin elt)))) | ||
| 709 | |||
| 710 | ;; Available and disabled packages: | ||
| 711 | (dolist (elt (bound-and-true-p package-archive-contents)) | ||
| 712 | (setq name (car elt)) | ||
| 713 | (when (or (eq packages t) (memq name packages)) | ||
| 714 | (dolist (pkg (cdr elt)) | ||
| 715 | ;; Hide obsolete packages. | ||
| 716 | (unless (package-installed-p (package-desc-name pkg) | ||
| 717 | (package-desc-version pkg)) | ||
| 718 | (funcall function pkg))))))) | ||
| 719 | |||
| 720 | (defun package--build-compatibility-table () | ||
| 721 | "Build `package--compatibility-table' with `package--mapc'." | ||
| 722 | ;; Initialize the list of built-ins. | ||
| 723 | (require 'finder-inf nil t) | ||
| 724 | ;; Build compat table. | ||
| 725 | (setq package--compatibility-table (make-hash-table :test 'eq)) | ||
| 726 | (package--mapc #'package--add-to-compatibility-table)) | ||
| 727 | |||
| 728 | ;;;###autoload | ||
| 729 | (defun package-initialize (&optional no-activate) | ||
| 730 | "Load Emacs Lisp packages, and activate them. | ||
| 731 | The variable `package-load-list' controls which packages to load. | ||
| 732 | If optional arg NO-ACTIVATE is non-nil, don't activate packages. | ||
| 733 | |||
| 734 | It is not necessary to adjust `load-path' or `require' the | ||
| 735 | individual packages after calling `package-initialize' -- this is | ||
| 736 | taken care of by `package-initialize'. | ||
| 737 | |||
| 738 | If `package-initialize' is called twice during Emacs startup, | ||
| 739 | signal a warning, since this is a bad idea except in highly | ||
| 740 | advanced use cases. To suppress the warning, remove the | ||
| 741 | superfluous call to `package-initialize' from your init-file. If | ||
| 742 | you have code which must run before `package-initialize', put | ||
| 743 | that code in the early init-file." | ||
| 744 | (interactive) | ||
| 745 | (when (and package--initialized (not after-init-time)) | ||
| 746 | (lwarn '(package reinitialization) :warning | ||
| 747 | "Unnecessary call to `package-initialize' in init file")) | ||
| 748 | (setq package-alist nil) | ||
| 749 | (package-load-all-descriptors) | ||
| 750 | (require 'package) | ||
| 751 | (package-read-all-archive-contents) | ||
| 752 | (setq package--initialized t) | ||
| 753 | (unless no-activate | ||
| 754 | (package-activate-all)) | ||
| 755 | ;; This uses `package--mapc' so it must be called after | ||
| 756 | ;; `package--initialized' is t. | ||
| 757 | (package--build-compatibility-table)) | ||
| 758 | |||
| 759 | ;;;###autoload | ||
| 760 | (progn ;; Make the function usable without loading `package.el'. | ||
| 761 | (defun package-activate-all () | ||
| 762 | "Activate all installed packages. | ||
| 763 | The variable `package-load-list' controls which packages to load." | ||
| 764 | (setq package--activated t) | ||
| 765 | (let* ((elc (concat package-quickstart-file "c")) | ||
| 766 | (qs (if (file-readable-p elc) elc | ||
| 767 | (if (file-readable-p package-quickstart-file) | ||
| 768 | package-quickstart-file)))) | ||
| 769 | ;; The quickstart file presumes that it has a blank slate, | ||
| 770 | ;; so don't use it if we already activated some packages. | ||
| 771 | (or (and qs (not (bound-and-true-p package-activated-list)) | ||
| 772 | ;; Skip `load-source-file-function' which would slow us down by | ||
| 773 | ;; a factor 2 when loading the .el file (this assumes we were | ||
| 774 | ;; careful to save this file so it doesn't need any decoding). | ||
| 775 | (with-demoted-errors "Error during quickstart: %S" | ||
| 776 | (let ((load-source-file-function nil)) | ||
| 777 | (unless (boundp 'package-activated-list) | ||
| 778 | (setq package-activated-list nil)) | ||
| 779 | (load qs nil 'nomessage) | ||
| 780 | t))) | ||
| 781 | (progn | ||
| 782 | (require 'package) | ||
| 783 | ;; Silence the "unknown function" warning when this is compiled | ||
| 784 | ;; inside `loaddefs.el'. | ||
| 785 | ;; FIXME: We use `with-no-warnings' because the effect of | ||
| 786 | ;; `declare-function' is currently not scoped, so if we use | ||
| 787 | ;; it here, we end up with a redefinition warning instead :-) | ||
| 788 | (with-no-warnings | ||
| 789 | (package--activate-all))))))) | ||
| 790 | |||
| 791 | (defun package--activate-all () | ||
| 792 | (dolist (elt (package--alist)) | ||
| 793 | (condition-case err | ||
| 794 | (package-activate (car elt)) | ||
| 795 | ;; Don't let failure of activation of a package arbitrarily stop | ||
| 796 | ;; activation of further packages. | ||
| 797 | (error (message "%s" (error-message-string err)))))) | ||
| 798 | |||
| 799 | (defun package-strip-rcs-id (str) | ||
| 800 | "Strip RCS version ID from the version string STR. | ||
| 801 | If the result looks like a dotted numeric version, return it. | ||
| 802 | Otherwise return nil." | ||
| 803 | (when str | ||
| 804 | (when (string-match "\\`[ \t]*[$]Revision:[ \t]+" str) | ||
| 805 | (setq str (substring str (match-end 0)))) | ||
| 806 | (let ((l (version-to-list str))) | ||
| 807 | ;; Don't return `str' but (package-version-join (version-to-list str)) | ||
| 808 | ;; to make sure we use a "canonical name"! | ||
| 809 | (if l (package-version-join l))))) | ||
| 810 | |||
| 811 | (defun package--incompatible-p (pkg &optional shallow) | ||
| 812 | "Return non-nil if PKG has no chance of being installable. | ||
| 813 | PKG is a `package-desc' object. | ||
| 814 | |||
| 815 | If SHALLOW is non-nil, this only checks if PKG depends on a | ||
| 816 | higher `emacs-version' than the one being used. Otherwise, also | ||
| 817 | checks the viability of dependencies, according to | ||
| 818 | `package--compatibility-table'. | ||
| 819 | |||
| 820 | If PKG requires an incompatible Emacs version, the return value | ||
| 821 | is this version (as a string). | ||
| 822 | If PKG requires incompatible packages, the return value is a list | ||
| 823 | of these dependencies, similar to the list returned by | ||
| 824 | `package-desc-reqs'." | ||
| 825 | (let* ((reqs (package-desc-reqs pkg)) | ||
| 826 | (version (cadr (assq 'emacs reqs)))) | ||
| 827 | (if (and version (version-list-< package--emacs-version-list version)) | ||
| 828 | (package-version-join version) | ||
| 829 | (unless shallow | ||
| 830 | (let (out) | ||
| 831 | (dolist (dep (package-desc-reqs pkg) out) | ||
| 832 | (let ((dep-name (car dep))) | ||
| 833 | (unless (eq 'emacs dep-name) | ||
| 834 | (let ((cv (gethash dep-name package--compatibility-table))) | ||
| 835 | (when (version-list-< (or cv '(0)) (or (cadr dep) '(0))) | ||
| 836 | (push dep out))))))))))) | ||
| 837 | |||
| 838 | (defun package--find-non-dependencies () | ||
| 839 | "Return a list of installed packages which are not dependencies. | ||
| 840 | Finds all packages in `package-alist' which are not dependencies | ||
| 841 | of any other packages. | ||
| 842 | Used to populate `package-selected-packages'." | ||
| 843 | (let ((dep-list | ||
| 844 | (delete-dups | ||
| 845 | (apply #'append | ||
| 846 | (mapcar (lambda (p) (mapcar #'car (package-desc-reqs (cadr p)))) | ||
| 847 | package-alist))))) | ||
| 848 | (cl-loop for p in package-alist | ||
| 849 | for name = (car p) | ||
| 850 | unless (memq name dep-list) | ||
| 851 | collect name))) | ||
| 852 | |||
| 853 | (defun package--save-selected-packages (&optional value) | ||
| 854 | "Set and save `package-selected-packages' to VALUE." | ||
| 855 | (when (or value after-init-time) | ||
| 856 | ;; It is valid to set it to nil, for example when the last package | ||
| 857 | ;; is uninstalled. But it shouldn't be done at init time, to | ||
| 858 | ;; avoid overwriting configurations that haven't yet been loaded. | ||
| 859 | (setq package-selected-packages (sort value #'string<))) | ||
| 860 | (if after-init-time | ||
| 861 | (customize-save-variable 'package-selected-packages package-selected-packages) | ||
| 862 | (add-hook 'after-init-hook #'package--save-selected-packages))) | ||
| 863 | |||
| 864 | (defun package--user-selected-p (pkg) | ||
| 865 | "Return non-nil if PKG is a package was installed by the user. | ||
| 866 | PKG is a package name. | ||
| 867 | This looks into `package-selected-packages', populating it first | ||
| 868 | if it is still empty." | ||
| 869 | (unless (consp package-selected-packages) | ||
| 870 | (package--save-selected-packages (package--find-non-dependencies))) | ||
| 871 | (memq pkg package-selected-packages)) | ||
| 872 | |||
| 873 | (defun package-desc-status (pkg-desc) | ||
| 874 | "Return the status of `package-desc' object PKG-DESC." | ||
| 875 | (let* ((name (package-desc-name pkg-desc)) | ||
| 876 | (dir (package-desc-dir pkg-desc)) | ||
| 877 | (lle (assq name package-load-list)) | ||
| 878 | (held (cadr lle)) | ||
| 879 | (version (package-desc-version pkg-desc)) | ||
| 880 | (signed (or (not package-list-unsigned) | ||
| 881 | (package-desc-signed pkg-desc)))) | ||
| 882 | (cond | ||
| 883 | ((package-vc-p pkg-desc) "source") | ||
| 884 | ((eq dir 'builtin) "built-in") | ||
| 885 | ((and lle (null held)) "disabled") | ||
| 886 | ((stringp held) | ||
| 887 | (let ((hv (if (stringp held) (version-to-list held)))) | ||
| 888 | (cond | ||
| 889 | ((version-list-= version hv) "held") | ||
| 890 | ((version-list-< version hv) "obsolete") | ||
| 891 | (t "disabled")))) | ||
| 892 | (dir ;One of the installed packages. | ||
| 893 | (cond | ||
| 894 | ((not (file-exists-p dir)) "deleted") | ||
| 895 | ;; Not inside `package-user-dir'. | ||
| 896 | ((not (file-in-directory-p dir package-user-dir)) "external") | ||
| 897 | ((eq pkg-desc (cadr (assq name package-alist))) | ||
| 898 | (if (not signed) "unsigned" | ||
| 899 | (if (package--user-selected-p name) | ||
| 900 | "installed" "dependency"))) | ||
| 901 | (t "obsolete"))) | ||
| 902 | ((package--incompatible-p pkg-desc) "incompat") | ||
| 903 | (t | ||
| 904 | (let* ((ins (cadr (assq name package-alist))) | ||
| 905 | (ins-v (if ins (package-desc-version ins)))) | ||
| 906 | (cond | ||
| 907 | ;; Installed obsolete packages are handled in the `dir' | ||
| 908 | ;; clause above. Here we handle available obsolete, which | ||
| 909 | ;; are displayed depending on `package-menu--hide-packages'. | ||
| 910 | ((and ins (version-list-<= version ins-v)) "avail-obso") | ||
| 911 | (t | ||
| 912 | (if (memq name (bound-and-true-p package-menu--new-package-list)) | ||
| 913 | "new" "available")))))))) | ||
| 914 | |||
| 915 | (defun package--query-desc (&optional alist) | ||
| 916 | "Query the user for a package or return the package at point. | ||
| 917 | The optional argument ALIST must consist of elements with the | ||
| 918 | form (PKG-NAME PKG-DESC). If not specified, it will default to | ||
| 919 | `package-alist'." | ||
| 920 | (or (and (fboundp 'tabulated-list-get-id) | ||
| 921 | (tabulated-list-get-id)) | ||
| 922 | (let ((alist (or alist package-alist))) | ||
| 923 | (cadr (assoc (completing-read "Package: " alist nil t) | ||
| 924 | alist #'string=))))) | ||
| 925 | |||
| 926 | (provide 'package-core) | ||
| 927 | ;;; package-core.el ends here | ||
diff --git a/lisp/package/package-describe.el b/lisp/package/package-describe.el new file mode 100644 index 00000000000..15a7f78ffaf --- /dev/null +++ b/lisp/package/package-describe.el | |||
| @@ -0,0 +1,419 @@ | |||
| 1 | ;;; package-describe.el --- Help buffer for packages -*- lexical-binding: t; -*- | ||
| 2 | |||
| 3 | ;; Copyright (C) 2025 Philip Kaludercic | ||
| 4 | |||
| 5 | ;; Author: Philip Kaludercic <philipk@posteo.net> | ||
| 6 | |||
| 7 | ;; This program is free software; you can redistribute it and/or modify | ||
| 8 | ;; it under the terms of the GNU General Public License as published by | ||
| 9 | ;; the Free Software Foundation, either version 3 of the License, or | ||
| 10 | ;; (at your option) any later version. | ||
| 11 | |||
| 12 | ;; This program is distributed in the hope that it will be useful, | ||
| 13 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 14 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 15 | ;; GNU General Public License for more details. | ||
| 16 | |||
| 17 | ;; You should have received a copy of the GNU General Public License | ||
| 18 | ;; along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
| 19 | |||
| 20 | ;;; Commentary: | ||
| 21 | |||
| 22 | ;;; Code: | ||
| 23 | |||
| 24 | (require 'package-core) | ||
| 25 | (require 'package-elpa) | ||
| 26 | (require 'package-misc) | ||
| 27 | (require 'package-install) | ||
| 28 | |||
| 29 | (require 'browse-url) | ||
| 30 | (require 'lisp-mnt) | ||
| 31 | |||
| 32 | (defface package-help-section-name | ||
| 33 | '((t :inherit (bold font-lock-function-name-face))) | ||
| 34 | "Face used on section names in package description buffers." | ||
| 35 | :version "25.1" | ||
| 36 | :group 'package) | ||
| 37 | |||
| 38 | (defun package--print-help-section (name &rest strings) | ||
| 39 | "Print \"NAME: \", right aligned to the 13th column. | ||
| 40 | If more STRINGS are provided, insert them followed by a newline. | ||
| 41 | Otherwise no newline is inserted." | ||
| 42 | (declare (indent 1)) | ||
| 43 | (insert (make-string (max 0 (- 11 (string-width name))) ?\s) | ||
| 44 | (propertize (concat name ": ") 'font-lock-face 'package-help-section-name)) | ||
| 45 | (when strings | ||
| 46 | (apply #'insert strings) | ||
| 47 | (insert "\n"))) | ||
| 48 | |||
| 49 | (defun package--get-description (desc) | ||
| 50 | "Return a string containing the long description of the package DESC. | ||
| 51 | The description is read from the installed package files." | ||
| 52 | ;; Installed packages have nil for kind, so we look for README | ||
| 53 | ;; first, then fall back to the Commentary header. | ||
| 54 | |||
| 55 | ;; We don’t include README.md here, because that is often the home | ||
| 56 | ;; page on a site like github, and not suitable as the package long | ||
| 57 | ;; description. | ||
| 58 | (let ((files '("README-elpa" "README-elpa.md" "README" "README.rst" "README.org")) | ||
| 59 | file | ||
| 60 | (srcdir (package-desc-dir desc)) | ||
| 61 | result) | ||
| 62 | (while (and files | ||
| 63 | (not result)) | ||
| 64 | (setq file (pop files)) | ||
| 65 | (when (file-readable-p (expand-file-name file srcdir)) | ||
| 66 | ;; Found a README. | ||
| 67 | (with-temp-buffer | ||
| 68 | (insert-file-contents (expand-file-name file srcdir)) | ||
| 69 | (setq result (buffer-string))))) | ||
| 70 | |||
| 71 | (or | ||
| 72 | result | ||
| 73 | |||
| 74 | ;; Look for Commentary header. | ||
| 75 | (lm-commentary (expand-file-name | ||
| 76 | (format "%s.el" (package-desc-name desc)) srcdir)) | ||
| 77 | ""))) | ||
| 78 | |||
| 79 | (defun package--describe-add-library-links () | ||
| 80 | "Add links to library names in package description." | ||
| 81 | (while (re-search-forward "\\<\\([-[:alnum:]]+\\.el\\)\\>" nil t) | ||
| 82 | (if (locate-library (match-string 1)) | ||
| 83 | (make-text-button (match-beginning 1) (match-end 1) | ||
| 84 | 'xref (match-string-no-properties 1) | ||
| 85 | 'help-echo "Read this file's commentary" | ||
| 86 | :type 'package--finder-xref)))) | ||
| 87 | |||
| 88 | (defun package-install-button-action (button) | ||
| 89 | "Run `package-install' on the package BUTTON points to. | ||
| 90 | Used for the `action' property of buttons in the buffer created by | ||
| 91 | `describe-package'." | ||
| 92 | (let ((pkg-desc (button-get button 'package-desc))) | ||
| 93 | (when (y-or-n-p (format-message "Install package `%s'? " | ||
| 94 | (package-desc-full-name pkg-desc))) | ||
| 95 | (package-install pkg-desc nil) | ||
| 96 | (describe-package (package-desc-name pkg-desc))))) | ||
| 97 | |||
| 98 | (defun package-delete-button-action (button) | ||
| 99 | "Run `package-delete' on the package BUTTON points to. | ||
| 100 | Used for the `action' property of buttons in the buffer created by | ||
| 101 | `describe-package'." | ||
| 102 | (let ((pkg-desc (button-get button 'package-desc))) | ||
| 103 | (when (y-or-n-p (format-message "Delete package `%s'? " | ||
| 104 | (package-desc-full-name pkg-desc))) | ||
| 105 | (package-delete pkg-desc) | ||
| 106 | (describe-package (package-desc-name pkg-desc))))) | ||
| 107 | |||
| 108 | (defun package-keyword-button-action (button) | ||
| 109 | "Show filtered \"*Packages*\" buffer for BUTTON. | ||
| 110 | The buffer is filtered by the `package-keyword' property of BUTTON. | ||
| 111 | Used for the `action' property of buttons in the buffer created by | ||
| 112 | `describe-package'." | ||
| 113 | (let ((pkg-keyword (button-get button 'package-keyword))) | ||
| 114 | (package-show-package-list t (list pkg-keyword)))) | ||
| 115 | |||
| 116 | (defun package-make-button (text &rest properties) | ||
| 117 | "Insert button labeled TEXT with button PROPERTIES at point. | ||
| 118 | PROPERTIES are passed to `insert-text-button', for which this | ||
| 119 | function is a convenience wrapper used by `describe-package-1'." | ||
| 120 | (let ((button-text (if (display-graphic-p) text (concat "[" text "]"))) | ||
| 121 | (button-face (if (display-graphic-p) | ||
| 122 | (progn | ||
| 123 | (require 'cus-edit) ; for the custom-button face | ||
| 124 | 'custom-button) | ||
| 125 | 'link))) | ||
| 126 | (apply #'insert-text-button button-text 'face button-face 'follow-link t | ||
| 127 | properties))) | ||
| 128 | |||
| 129 | (defun package--finder-goto-xref (button) | ||
| 130 | "Jump to a Lisp file for the BUTTON at point." | ||
| 131 | (let* ((file (button-get button 'xref)) | ||
| 132 | (lib (locate-library file))) | ||
| 133 | (if lib (finder-commentary lib) | ||
| 134 | (message "Unable to locate `%s'" file)))) | ||
| 135 | |||
| 136 | (define-button-type 'package--finder-xref 'action #'package--finder-goto-xref) | ||
| 137 | |||
| 138 | (defun describe-package-1 (pkg) | ||
| 139 | "Insert the package description for PKG. | ||
| 140 | Helper function for `describe-package'." | ||
| 141 | (require 'lisp-mnt) | ||
| 142 | (let* ((desc (or | ||
| 143 | (if (package-desc-p pkg) pkg) | ||
| 144 | (cadr (assq pkg package-alist)) | ||
| 145 | (let ((built-in (assq pkg package--builtins))) | ||
| 146 | (if built-in | ||
| 147 | (package--from-builtin built-in) | ||
| 148 | (cadr (assq pkg package-archive-contents)))))) | ||
| 149 | (name (if desc (package-desc-name desc) pkg)) | ||
| 150 | (pkg-dir (if desc (package-desc-dir desc))) | ||
| 151 | (reqs (if desc (package-desc-reqs desc))) | ||
| 152 | (required-by (if desc (package--used-elsewhere-p desc nil 'all))) | ||
| 153 | (version (if desc (package-desc-version desc))) | ||
| 154 | (archive (if desc (package-desc-archive desc))) | ||
| 155 | (extras (and desc (package-desc-extras desc))) | ||
| 156 | (website (cdr (assoc :url extras))) | ||
| 157 | (commit (cdr (assoc :commit extras))) | ||
| 158 | (keywords (if desc (package-desc--keywords desc))) | ||
| 159 | (built-in (eq pkg-dir 'builtin)) | ||
| 160 | (installable (and archive (not built-in))) | ||
| 161 | (status (if desc (package-desc-status desc) "orphan")) | ||
| 162 | (incompatible-reason (package--incompatible-p desc)) | ||
| 163 | (signed (if desc (package-desc-signed desc))) | ||
| 164 | (maintainers (or (cdr (assoc :maintainer extras)) | ||
| 165 | (cdr (assoc :maintainers extras)))) | ||
| 166 | (authors (cdr (assoc :authors extras))) | ||
| 167 | (news (and-let* (pkg-dir | ||
| 168 | ((not built-in)) | ||
| 169 | (file (expand-file-name "news" pkg-dir)) | ||
| 170 | ((file-regular-p file)) | ||
| 171 | ((file-readable-p file))) | ||
| 172 | file))) | ||
| 173 | (when (string= status "avail-obso") | ||
| 174 | (setq status "available obsolete")) | ||
| 175 | (when incompatible-reason | ||
| 176 | (setq status "incompatible")) | ||
| 177 | (princ (format "Package %S is %s.\n\n" name status)) | ||
| 178 | |||
| 179 | ;; TODO: Remove the string decorations and reformat the strings | ||
| 180 | ;; for future l10n. | ||
| 181 | (package--print-help-section "Status") | ||
| 182 | (cond (built-in | ||
| 183 | (insert (propertize (capitalize status) | ||
| 184 | 'font-lock-face 'package-status-built-in) | ||
| 185 | ".")) | ||
| 186 | (pkg-dir | ||
| 187 | (insert (propertize (if (member status '("unsigned" "dependency")) | ||
| 188 | "Installed" | ||
| 189 | (capitalize status)) | ||
| 190 | 'font-lock-face 'package-status-built-in)) | ||
| 191 | (insert (substitute-command-keys " in `")) | ||
| 192 | (let ((dir (abbreviate-file-name | ||
| 193 | (file-name-as-directory | ||
| 194 | (if (file-in-directory-p pkg-dir package-user-dir) | ||
| 195 | (file-relative-name pkg-dir package-user-dir) | ||
| 196 | pkg-dir))))) | ||
| 197 | (help-insert-xref-button dir 'help-package-def pkg-dir)) | ||
| 198 | (if (and (package-built-in-p name) | ||
| 199 | (not (package-built-in-p name version))) | ||
| 200 | (insert (substitute-command-keys | ||
| 201 | "',\n shadowing a ") | ||
| 202 | (propertize "built-in package" | ||
| 203 | 'font-lock-face 'package-status-built-in)) | ||
| 204 | (insert (substitute-quotes "'"))) | ||
| 205 | (if signed | ||
| 206 | (insert ".") | ||
| 207 | (insert " (unsigned).")) | ||
| 208 | (when (and (package-desc-p desc) | ||
| 209 | (not required-by) | ||
| 210 | (member status '("unsigned" "installed"))) | ||
| 211 | (insert " ") | ||
| 212 | (package-make-button "Delete" | ||
| 213 | 'action #'package-delete-button-action | ||
| 214 | 'package-desc desc))) | ||
| 215 | (incompatible-reason | ||
| 216 | (insert (propertize "Incompatible" 'font-lock-face 'font-lock-warning-face) | ||
| 217 | " because it depends on ") | ||
| 218 | (if (stringp incompatible-reason) | ||
| 219 | (insert "Emacs " incompatible-reason ".") | ||
| 220 | (insert "uninstallable packages."))) | ||
| 221 | (installable | ||
| 222 | (insert (capitalize status)) | ||
| 223 | (insert " from " (format "%s" archive)) | ||
| 224 | (insert " -- ") | ||
| 225 | (package-make-button | ||
| 226 | "Install" | ||
| 227 | 'action 'package-install-button-action | ||
| 228 | 'package-desc desc)) | ||
| 229 | (t (insert (capitalize status) "."))) | ||
| 230 | (insert "\n") | ||
| 231 | (unless (and pkg-dir (not archive)) ; Installed pkgs don't have archive. | ||
| 232 | (package--print-help-section "Archive" | ||
| 233 | (or archive "n/a"))) | ||
| 234 | (and version | ||
| 235 | (package--print-help-section "Version" | ||
| 236 | (package-version-join version))) | ||
| 237 | (when commit | ||
| 238 | (package--print-help-section "Commit" commit)) | ||
| 239 | (when desc | ||
| 240 | (package--print-help-section "Summary" | ||
| 241 | (package-desc-summary desc))) | ||
| 242 | |||
| 243 | (setq reqs (if desc (package-desc-reqs desc))) | ||
| 244 | (when reqs | ||
| 245 | (package--print-help-section "Requires") | ||
| 246 | (let ((first t)) | ||
| 247 | (dolist (req reqs) | ||
| 248 | (let* ((name (car req)) | ||
| 249 | (vers (cadr req)) | ||
| 250 | (text (format "%s-%s" (symbol-name name) | ||
| 251 | (package-version-join vers))) | ||
| 252 | (reason (if (and (listp incompatible-reason) | ||
| 253 | (assq name incompatible-reason)) | ||
| 254 | " (not available)" ""))) | ||
| 255 | (cond (first (setq first nil)) | ||
| 256 | ((>= (+ 2 (current-column) (length text) (length reason)) | ||
| 257 | (window-width)) | ||
| 258 | (insert ",\n ")) | ||
| 259 | (t (insert ", "))) | ||
| 260 | (help-insert-xref-button text 'help-package name) | ||
| 261 | (insert reason))) | ||
| 262 | (insert "\n"))) | ||
| 263 | (when required-by | ||
| 264 | (package--print-help-section "Required by") | ||
| 265 | (let ((first t)) | ||
| 266 | (dolist (pkg required-by) | ||
| 267 | (let ((text (package-desc-full-name pkg))) | ||
| 268 | (cond (first (setq first nil)) | ||
| 269 | ((>= (+ 2 (current-column) (length text)) | ||
| 270 | (window-width)) | ||
| 271 | (insert ",\n ")) | ||
| 272 | (t (insert ", "))) | ||
| 273 | (help-insert-xref-button text 'help-package | ||
| 274 | (package-desc-name pkg)))) | ||
| 275 | (insert "\n"))) | ||
| 276 | (when website | ||
| 277 | ;; Prefer https for the website of packages on common domains. | ||
| 278 | (when (string-match-p (rx bol "http://" (or "elpa." "www." "git." "") | ||
| 279 | (or "nongnu.org" "gnu.org" "sr.ht" | ||
| 280 | "emacswiki.org" "gitlab.com" "github.com") | ||
| 281 | "/") | ||
| 282 | website) | ||
| 283 | ;; But only if the user has "https" in `package-archives'. | ||
| 284 | (let ((gnu (cdr (assoc "gnu" package-archives)))) | ||
| 285 | (and gnu (string-match-p "^https" gnu) | ||
| 286 | (setq website | ||
| 287 | (replace-regexp-in-string "^http" "https" website))))) | ||
| 288 | (package--print-help-section "Website") | ||
| 289 | (help-insert-xref-button website 'help-url website) | ||
| 290 | (insert "\n")) | ||
| 291 | (when keywords | ||
| 292 | (package--print-help-section "Keywords") | ||
| 293 | (dolist (k keywords) | ||
| 294 | (package-make-button | ||
| 295 | k | ||
| 296 | 'package-keyword k | ||
| 297 | 'action 'package-keyword-button-action) | ||
| 298 | (insert " ")) | ||
| 299 | (insert "\n")) | ||
| 300 | (when maintainers | ||
| 301 | (unless (and (listp (car maintainers)) (listp (cdr maintainers))) | ||
| 302 | (setq maintainers (list maintainers))) | ||
| 303 | (package--print-help-section | ||
| 304 | (if (cdr maintainers) "Maintainers" "Maintainer")) | ||
| 305 | (dolist (maintainer maintainers) | ||
| 306 | (when (bolp) | ||
| 307 | (insert (make-string 13 ?\s))) | ||
| 308 | (package--print-email-button maintainer))) | ||
| 309 | (when authors | ||
| 310 | (package--print-help-section (if (cdr authors) "Authors" "Author")) | ||
| 311 | (dolist (author authors) | ||
| 312 | (when (bolp) | ||
| 313 | (insert (make-string 13 ?\s))) | ||
| 314 | (package--print-email-button author))) | ||
| 315 | (let* ((all-pkgs (append (cdr (assq name package-alist)) | ||
| 316 | (cdr (assq name package-archive-contents)) | ||
| 317 | (let ((bi (assq name package--builtins))) | ||
| 318 | (if bi (list (package--from-builtin bi)))))) | ||
| 319 | (other-pkgs (delete desc all-pkgs))) | ||
| 320 | (when other-pkgs | ||
| 321 | (package--print-help-section "Other versions" | ||
| 322 | (mapconcat (lambda (opkg) | ||
| 323 | (let* ((ov (package-desc-version opkg)) | ||
| 324 | (dir (package-desc-dir opkg)) | ||
| 325 | (from (or (package-desc-archive opkg) | ||
| 326 | (if (stringp dir) "installed" dir)))) | ||
| 327 | (if (not ov) (format "%s" from) | ||
| 328 | (format "%s (%s)" | ||
| 329 | (make-text-button (package-version-join ov) nil | ||
| 330 | 'font-lock-face 'link | ||
| 331 | 'follow-link t | ||
| 332 | 'action | ||
| 333 | (lambda (_button) | ||
| 334 | (describe-package opkg))) | ||
| 335 | from)))) | ||
| 336 | other-pkgs ", ") | ||
| 337 | "."))) | ||
| 338 | |||
| 339 | (insert "\n") | ||
| 340 | |||
| 341 | (let ((start-of-description (point))) | ||
| 342 | (if built-in | ||
| 343 | ;; For built-in packages, get the description from the | ||
| 344 | ;; Commentary header. | ||
| 345 | (insert (or (lm-commentary (locate-file (format "%s.el" name) | ||
| 346 | load-path | ||
| 347 | load-file-rep-suffixes)) | ||
| 348 | "")) | ||
| 349 | |||
| 350 | (if (package-installed-p desc) | ||
| 351 | ;; For installed packages, get the description from the | ||
| 352 | ;; installed files. | ||
| 353 | (insert (package--get-description desc)) | ||
| 354 | |||
| 355 | ;; For non-built-in, non-installed packages, get description from | ||
| 356 | ;; the archive. | ||
| 357 | (let* ((basename (format "%s-readme.txt" name)) | ||
| 358 | readme-string) | ||
| 359 | |||
| 360 | (package--with-response-buffer (package-archive-base desc) | ||
| 361 | :file basename :noerror t | ||
| 362 | (save-excursion | ||
| 363 | (goto-char (point-max)) | ||
| 364 | (unless (bolp) | ||
| 365 | (insert ?\n))) | ||
| 366 | (cl-assert (not enable-multibyte-characters)) | ||
| 367 | (setq readme-string | ||
| 368 | ;; The readme.txt files are defined to contain utf-8 text. | ||
| 369 | (decode-coding-region (point-min) (point-max) 'utf-8 t)) | ||
| 370 | t) | ||
| 371 | (insert (or readme-string | ||
| 372 | "This package does not provide a description."))))) | ||
| 373 | |||
| 374 | ;; Insert news if available. | ||
| 375 | (when news | ||
| 376 | (insert "\n" (make-separator-line) "\n" | ||
| 377 | (propertize "* News" 'face 'package-help-section-name) | ||
| 378 | "\n\n") | ||
| 379 | (insert-file-contents news)) | ||
| 380 | |||
| 381 | ;; Make library descriptions into links. | ||
| 382 | (goto-char start-of-description) | ||
| 383 | (package--describe-add-library-links) | ||
| 384 | ;; Make URLs in the description into links. | ||
| 385 | (goto-char start-of-description) | ||
| 386 | (browse-url-add-buttons)))) | ||
| 387 | |||
| 388 | ;;;###autoload | ||
| 389 | (defun describe-package (package) | ||
| 390 | "Display the full documentation of PACKAGE (a symbol)." | ||
| 391 | (interactive | ||
| 392 | (let* ((guess (or (function-called-at-point) | ||
| 393 | (symbol-at-point)))) | ||
| 394 | (require 'finder-inf nil t) | ||
| 395 | ;; Load the package list if necessary (but don't activate them). | ||
| 396 | (unless package--initialized | ||
| 397 | (package-initialize t)) | ||
| 398 | (let ((packages (append (mapcar #'car package-alist) | ||
| 399 | (mapcar #'car package-archive-contents) | ||
| 400 | (mapcar #'car package--builtins)))) | ||
| 401 | (unless (memq guess packages) | ||
| 402 | (setq guess nil)) | ||
| 403 | (setq packages (mapcar #'symbol-name packages)) | ||
| 404 | (let ((val | ||
| 405 | (completing-read (format-prompt "Describe package" guess) | ||
| 406 | packages nil t nil nil (when guess | ||
| 407 | (symbol-name guess))))) | ||
| 408 | (list (and (> (length val) 0) (intern val))))))) | ||
| 409 | (if (not (or (package-desc-p package) (and package (symbolp package)))) | ||
| 410 | (message "No package specified") | ||
| 411 | (help-setup-xref (list #'describe-package package) | ||
| 412 | (called-interactively-p 'interactive)) | ||
| 413 | (with-help-window (help-buffer) | ||
| 414 | (with-current-buffer standard-output | ||
| 415 | (describe-package-1 package))))) | ||
| 416 | |||
| 417 | |||
| 418 | (provide 'package-describe) | ||
| 419 | ;;; package-describe.el ends here | ||
diff --git a/lisp/package/package-elpa.el b/lisp/package/package-elpa.el new file mode 100644 index 00000000000..3eb3e504ea5 --- /dev/null +++ b/lisp/package/package-elpa.el | |||
| @@ -0,0 +1,629 @@ | |||
| 1 | ;;; package-elpa.el --- ELPA integration -*- lexical-binding: t; -*- | ||
| 2 | |||
| 3 | ;; Copyright (C) 2025 Philip Kaludercic | ||
| 4 | |||
| 5 | ;; Author: Tom Tromey <tromey@redhat.com> | ||
| 6 | ;; Daniel Hackney <dan@haxney.org> | ||
| 7 | |||
| 8 | ;; This program is free software; you can redistribute it and/or modify | ||
| 9 | ;; it under the terms of the GNU General Public License as published by | ||
| 10 | ;; the Free Software Foundation, either version 3 of the License, or | ||
| 11 | ;; (at your option) any later version. | ||
| 12 | |||
| 13 | ;; This program is distributed in the hope that it will be useful, | ||
| 14 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 15 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 16 | ;; GNU General Public License for more details. | ||
| 17 | |||
| 18 | ;; You should have received a copy of the GNU General Public License | ||
| 19 | ;; along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
| 20 | |||
| 21 | ;;; Commentary: | ||
| 22 | |||
| 23 | ;; | ||
| 24 | |||
| 25 | ;;; Code: | ||
| 26 | |||
| 27 | (require 'package-core) | ||
| 28 | |||
| 29 | (require 'epg) | ||
| 30 | (require 'url-http) | ||
| 31 | (require 'url-handlers) | ||
| 32 | |||
| 33 | (defgroup package-archive nil | ||
| 34 | "Archive configuration of the package manager." | ||
| 35 | :group 'applications | ||
| 36 | :version "31.1") | ||
| 37 | |||
| 38 | (defcustom package-archives `(("gnu" . | ||
| 39 | ,(format "http%s://elpa.gnu.org/packages/" | ||
| 40 | (if (gnutls-available-p) "s" ""))) | ||
| 41 | ("nongnu" . | ||
| 42 | ,(format "http%s://elpa.nongnu.org/nongnu/" | ||
| 43 | (if (gnutls-available-p) "s" "")))) | ||
| 44 | "An alist of archives from which to fetch. | ||
| 45 | The default value points to the GNU Emacs package repository. | ||
| 46 | |||
| 47 | Each element has the form (ID . LOCATION). | ||
| 48 | ID is an archive name, as a string. | ||
| 49 | LOCATION specifies the base location for the archive. | ||
| 50 | If it starts with \"http(s):\", it is treated as an HTTP(S) URL; | ||
| 51 | otherwise it should be an absolute directory name. | ||
| 52 | (Other types of URL are currently not supported.) | ||
| 53 | |||
| 54 | Only add locations that you trust, since fetching and installing | ||
| 55 | a package can run arbitrary code. | ||
| 56 | |||
| 57 | HTTPS URLs should be used where possible, as they offer superior | ||
| 58 | security." | ||
| 59 | :type '(alist :key-type (string :tag "Archive name") | ||
| 60 | :value-type (string :tag "URL or directory name")) | ||
| 61 | :risky t | ||
| 62 | :version "28.1") | ||
| 63 | |||
| 64 | (defcustom package-archive-priorities nil | ||
| 65 | "An alist of priorities for packages. | ||
| 66 | |||
| 67 | Each element has the form (ARCHIVE-ID . PRIORITY). | ||
| 68 | |||
| 69 | When installing packages, the package with the highest version | ||
| 70 | number from the archive with the highest priority is | ||
| 71 | selected. When higher versions are available from archives with | ||
| 72 | lower priorities, the user has to select those manually. | ||
| 73 | |||
| 74 | Archives not in this list have the priority 0, as have packages | ||
| 75 | that are already installed. If you use negative priorities for | ||
| 76 | the archives, they will not be upgraded automatically. | ||
| 77 | |||
| 78 | See also `package-menu-hide-low-priority'." | ||
| 79 | :type '(alist :key-type (string :tag "Archive name") | ||
| 80 | :value-type (integer :tag "Priority (default is 0)")) | ||
| 81 | :risky t | ||
| 82 | :version "25.1") | ||
| 83 | |||
| 84 | (defcustom package-gnupghome-dir (expand-file-name "gnupg" package-user-dir) | ||
| 85 | "Directory containing GnuPG keyring or nil. | ||
| 86 | This variable specifies the GnuPG home directory used by package. | ||
| 87 | That directory is passed via the option \"--homedir\" to GnuPG. | ||
| 88 | If nil, do not use the option \"--homedir\", but stick with GnuPG's | ||
| 89 | default directory." | ||
| 90 | :type `(choice | ||
| 91 | (const | ||
| 92 | :tag "Default Emacs package management GnuPG home directory" | ||
| 93 | ,(expand-file-name "gnupg" package-user-dir)) | ||
| 94 | (const | ||
| 95 | :tag "Default GnuPG directory (GnuPG option --homedir not used)" | ||
| 96 | nil) | ||
| 97 | (directory :tag "A specific GnuPG --homedir")) | ||
| 98 | :risky t | ||
| 99 | :version "26.1") | ||
| 100 | |||
| 101 | (defcustom package-check-signature 'allow-unsigned | ||
| 102 | "Non-nil means to check package signatures when installing. | ||
| 103 | |||
| 104 | This also applies to the \"archive-contents\" file that lists the | ||
| 105 | contents of the archive. | ||
| 106 | |||
| 107 | The value can be one of: | ||
| 108 | |||
| 109 | t Accept a package only if it comes with at least | ||
| 110 | one verified signature. | ||
| 111 | |||
| 112 | `all' Same as t, but verify all signatures if there | ||
| 113 | are more than one. | ||
| 114 | |||
| 115 | `allow-unsigned' Install a package even if it is unsigned, | ||
| 116 | but verify the signature if possible (that | ||
| 117 | is, if it is signed, we have the key for it, | ||
| 118 | and GnuPG is installed). | ||
| 119 | |||
| 120 | nil Package signatures are ignored." | ||
| 121 | :type '(choice (const :value nil :tag "Never") | ||
| 122 | (const :value allow-unsigned :tag "Allow unsigned") | ||
| 123 | (const :value t :tag "Check always") | ||
| 124 | (const :value all :tag "Check always (all signatures)")) | ||
| 125 | :risky t | ||
| 126 | :version "27.1") | ||
| 127 | |||
| 128 | (defun package-check-signature () | ||
| 129 | "Check whether we have a usable OpenPGP configuration. | ||
| 130 | If so, and variable `package-check-signature' is | ||
| 131 | `allow-unsigned', return `allow-unsigned', otherwise return the | ||
| 132 | value of variable `package-check-signature'." | ||
| 133 | (if (eq package-check-signature 'allow-unsigned) | ||
| 134 | (and (epg-find-configuration 'OpenPGP) | ||
| 135 | 'allow-unsigned) | ||
| 136 | package-check-signature)) | ||
| 137 | |||
| 138 | (defcustom package-unsigned-archives nil | ||
| 139 | "List of archives where we do not check for package signatures. | ||
| 140 | This should be a list of strings matching the names of package | ||
| 141 | archives in the variable `package-archives'." | ||
| 142 | :type '(repeat (string :tag "Archive name")) | ||
| 143 | :risky t | ||
| 144 | :version "24.4") | ||
| 145 | |||
| 146 | (defun package--write-file-no-coding (file-name) | ||
| 147 | "Write file FILE-NAME without encoding using coding system." | ||
| 148 | (let ((buffer-file-coding-system 'no-conversion)) | ||
| 149 | (write-region (point-min) (point-max) file-name nil 'silent))) | ||
| 150 | |||
| 151 | (defun package--archive-file-exists-p (location file) | ||
| 152 | "Return t if FILE exists in remote LOCATION." | ||
| 153 | (let ((http (string-match "\\`https?:" location))) | ||
| 154 | (if http | ||
| 155 | (progn | ||
| 156 | (require 'url-http) | ||
| 157 | (url-http-file-exists-p (concat location file))) | ||
| 158 | (file-exists-p (expand-file-name file location))))) | ||
| 159 | |||
| 160 | (defun package--display-verify-error (context sig-file) | ||
| 161 | "Show error details with CONTEXT for failed verification of SIG-FILE. | ||
| 162 | The details are shown in a new buffer called \"*Error\"." | ||
| 163 | (unless (equal (epg-context-error-output context) "") | ||
| 164 | (with-output-to-temp-buffer "*Error*" | ||
| 165 | (with-current-buffer standard-output | ||
| 166 | (if (epg-context-result-for context 'verify) | ||
| 167 | (insert (format "Failed to verify signature %s:\n" sig-file) | ||
| 168 | (mapconcat #'epg-signature-to-string | ||
| 169 | (epg-context-result-for context 'verify) | ||
| 170 | "\n")) | ||
| 171 | (insert (format "Error while verifying signature %s:\n" sig-file))) | ||
| 172 | (insert "\nCommand output:\n" (epg-context-error-output context)))))) | ||
| 173 | |||
| 174 | (defmacro package--with-work-buffer (location file &rest body) | ||
| 175 | "Run BODY in a buffer containing the contents of FILE at LOCATION. | ||
| 176 | LOCATION is the base location of a package archive, and should be | ||
| 177 | one of the URLs (or file names) specified in `package-archives'. | ||
| 178 | FILE is the name of a file relative to that base location. | ||
| 179 | |||
| 180 | This macro retrieves FILE from LOCATION into a temporary buffer, | ||
| 181 | and evaluates BODY while that buffer is current. This work | ||
| 182 | buffer is killed afterwards. Return the last value in BODY." | ||
| 183 | (declare (indent 2) (debug t) | ||
| 184 | (obsolete package--with-response-buffer "25.1")) | ||
| 185 | `(with-temp-buffer | ||
| 186 | (if (string-match-p "\\`https?:" ,location) | ||
| 187 | (url-insert-file-contents (concat ,location ,file)) | ||
| 188 | (unless (file-name-absolute-p ,location) | ||
| 189 | (error "Archive location %s is not an absolute file name" | ||
| 190 | ,location)) | ||
| 191 | (insert-file-contents (expand-file-name ,file ,location))) | ||
| 192 | ,@body)) | ||
| 193 | |||
| 194 | (cl-defmacro package--with-response-buffer (url &rest body &key async file error-form noerror &allow-other-keys) | ||
| 195 | "Access URL and run BODY in a buffer containing the response. | ||
| 196 | Point is after the headers when BODY runs. | ||
| 197 | FILE, if provided, is added to URL. | ||
| 198 | URL can be a local file name, which must be absolute. | ||
| 199 | ASYNC, if non-nil, runs the request asynchronously. | ||
| 200 | ERROR-FORM is run only if a connection error occurs. If NOERROR | ||
| 201 | is non-nil, don't propagate connection errors (does not apply to | ||
| 202 | errors signaled by ERROR-FORM or by BODY). | ||
| 203 | |||
| 204 | \(fn URL &key ASYNC FILE ERROR-FORM NOERROR &rest BODY)" | ||
| 205 | (declare (indent defun) (debug (sexp body))) | ||
| 206 | (while (keywordp (car body)) | ||
| 207 | (setq body (cdr (cdr body)))) | ||
| 208 | `(package--with-response-buffer-1 ,url (lambda () ,@body) | ||
| 209 | :file ,file | ||
| 210 | :async ,async | ||
| 211 | :error-function (lambda () ,error-form) | ||
| 212 | :noerror ,noerror)) | ||
| 213 | |||
| 214 | (defmacro package--unless-error (body &rest before-body) | ||
| 215 | (declare (debug t) (indent 1)) | ||
| 216 | (let ((err (make-symbol "err"))) | ||
| 217 | `(with-temp-buffer | ||
| 218 | (set-buffer-multibyte nil) | ||
| 219 | (when (condition-case ,err | ||
| 220 | (progn ,@before-body t) | ||
| 221 | (error (funcall error-function) | ||
| 222 | (unless noerror | ||
| 223 | (signal (car ,err) (cdr ,err))))) | ||
| 224 | (funcall ,body))))) | ||
| 225 | |||
| 226 | (cl-defun package--with-response-buffer-1 (url body &key async file error-function noerror &allow-other-keys) | ||
| 227 | (if (string-match-p "\\`https?:" url) | ||
| 228 | (let ((url (url-expand-file-name file url))) | ||
| 229 | (if async | ||
| 230 | (package--unless-error #'ignore | ||
| 231 | (url-retrieve | ||
| 232 | url | ||
| 233 | (lambda (status) | ||
| 234 | (let ((b (current-buffer))) | ||
| 235 | (package--unless-error body | ||
| 236 | (when-let* ((er (plist-get status :error))) | ||
| 237 | (error "Error retrieving: %s %S" url er)) | ||
| 238 | (with-current-buffer b | ||
| 239 | (goto-char (point-min)) | ||
| 240 | (unless (search-forward-regexp "^\r?\n\r?" nil t) | ||
| 241 | (error "Error retrieving: %s %S" | ||
| 242 | url "incomprehensible buffer"))) | ||
| 243 | (url-insert b) | ||
| 244 | (kill-buffer b) | ||
| 245 | (goto-char (point-min))))) | ||
| 246 | nil | ||
| 247 | 'silent)) | ||
| 248 | (package--unless-error body | ||
| 249 | ;; Copy&pasted from url-insert-file-contents, | ||
| 250 | ;; except it calls `url-insert' because we want the contents | ||
| 251 | ;; literally (but there's no url-insert-file-contents-literally). | ||
| 252 | (let ((buffer (url-retrieve-synchronously url))) | ||
| 253 | (unless buffer (signal 'file-error (list url "No Data"))) | ||
| 254 | (when (fboundp 'url-http--insert-file-helper) | ||
| 255 | ;; XXX: This is HTTP/S specific and should be moved | ||
| 256 | ;; to url-http instead. See bug#17549. | ||
| 257 | (url-http--insert-file-helper buffer url)) | ||
| 258 | (url-insert buffer) | ||
| 259 | (kill-buffer buffer) | ||
| 260 | (goto-char (point-min)))))) | ||
| 261 | (package--unless-error body | ||
| 262 | (unless (file-name-absolute-p url) | ||
| 263 | (error "Location %s is not a url nor an absolute file name" url)) | ||
| 264 | (insert-file-contents-literally (expand-file-name file url))))) | ||
| 265 | |||
| 266 | (define-error 'bad-signature "Failed to verify signature") | ||
| 267 | |||
| 268 | (defun package--check-signature-content (content string &optional sig-file) | ||
| 269 | "Check signature CONTENT against STRING. | ||
| 270 | SIG-FILE is the name of the signature file, used when signaling | ||
| 271 | errors." | ||
| 272 | (let ((context (epg-make-context 'OpenPGP))) | ||
| 273 | (when package-gnupghome-dir | ||
| 274 | (setf (epg-context-home-directory context) package-gnupghome-dir)) | ||
| 275 | (condition-case error | ||
| 276 | (epg-verify-string context content string) | ||
| 277 | (error (package--display-verify-error context sig-file) | ||
| 278 | (signal 'bad-signature error))) | ||
| 279 | (let (good-signatures had-fatal-error) | ||
| 280 | ;; The .sig file may contain multiple signatures. Success if one | ||
| 281 | ;; of the signatures is good. | ||
| 282 | (dolist (sig (epg-context-result-for context 'verify)) | ||
| 283 | (if (eq (epg-signature-status sig) 'good) | ||
| 284 | (push sig good-signatures) | ||
| 285 | ;; If `package-check-signature' is allow-unsigned, don't | ||
| 286 | ;; signal error when we can't verify signature because of | ||
| 287 | ;; missing public key. Other errors are still treated as | ||
| 288 | ;; fatal (bug#17625). | ||
| 289 | (unless (and (eq (package-check-signature) 'allow-unsigned) | ||
| 290 | (eq (epg-signature-status sig) 'no-pubkey)) | ||
| 291 | (setq had-fatal-error t)))) | ||
| 292 | (when (or (null good-signatures) | ||
| 293 | (and (eq (package-check-signature) 'all) | ||
| 294 | had-fatal-error)) | ||
| 295 | (package--display-verify-error context sig-file) | ||
| 296 | (signal 'bad-signature (list sig-file))) | ||
| 297 | good-signatures))) | ||
| 298 | |||
| 299 | (defun package--check-signature (location file &optional string async callback unwind) | ||
| 300 | "Check signature of the current buffer. | ||
| 301 | Download the signature file from LOCATION by appending \".sig\" | ||
| 302 | to FILE. | ||
| 303 | GnuPG keyring location depends on `package-gnupghome-dir'. | ||
| 304 | STRING is the string to verify, it defaults to `buffer-string'. | ||
| 305 | If ASYNC is non-nil, the download of the signature file is | ||
| 306 | done asynchronously. | ||
| 307 | |||
| 308 | If the signature does not verify, signal an error. | ||
| 309 | If the signature is verified and CALLBACK was provided, `funcall' | ||
| 310 | CALLBACK with the list of good signatures as argument (the list | ||
| 311 | can be empty). | ||
| 312 | If no signatures file is found, and `package-check-signature' is | ||
| 313 | `allow-unsigned', call CALLBACK with a nil argument. | ||
| 314 | Otherwise, an error is signaled. | ||
| 315 | |||
| 316 | UNWIND, if provided, is a function to be called after everything | ||
| 317 | else, even if an error is signaled." | ||
| 318 | (let ((sig-file (concat file ".sig")) | ||
| 319 | (string (or string (buffer-string)))) | ||
| 320 | (package--with-response-buffer location :file sig-file | ||
| 321 | :async async :noerror t | ||
| 322 | ;; Connection error is assumed to mean "no sig-file". | ||
| 323 | :error-form (let ((allow-unsigned | ||
| 324 | (eq (package-check-signature) 'allow-unsigned))) | ||
| 325 | (when (and callback allow-unsigned) | ||
| 326 | (funcall callback nil)) | ||
| 327 | (when unwind (funcall unwind)) | ||
| 328 | (unless allow-unsigned | ||
| 329 | (error "Unsigned file `%s' at %s" file location))) | ||
| 330 | ;; OTOH, an error here means "bad signature", which we never | ||
| 331 | ;; suppress. (Bug#22089) | ||
| 332 | (unwind-protect | ||
| 333 | (let ((sig (package--check-signature-content | ||
| 334 | (buffer-substring (point) (point-max)) | ||
| 335 | string sig-file))) | ||
| 336 | (when callback (funcall callback sig)) | ||
| 337 | sig) | ||
| 338 | (when unwind (funcall unwind)))))) | ||
| 339 | |||
| 340 | ;;; Packages on Archives | ||
| 341 | ;; The following variables store information about packages available | ||
| 342 | ;; from archives. The most important of these is | ||
| 343 | ;; `package-archive-contents' which is initially populated by the | ||
| 344 | ;; function `package-read-all-archive-contents' from a cache on disk. | ||
| 345 | ;; The `package-initialize' command is also closely related to this | ||
| 346 | ;; section, but it has its own section. | ||
| 347 | |||
| 348 | (defconst package-archive-version 1 | ||
| 349 | "Version number of the package archive understood by package.el. | ||
| 350 | Lower version numbers than this will probably be understood as well.") | ||
| 351 | |||
| 352 | ;; We don't prime the cache since it tends to get out of date. | ||
| 353 | (defvar package-archive-contents nil | ||
| 354 | "Cache of the contents of all archives in `package-archives'. | ||
| 355 | This is an alist mapping package names (symbols) to | ||
| 356 | non-empty lists of `package-desc' structures.") | ||
| 357 | (put 'package-archive-contents 'risky-local-variable t) | ||
| 358 | |||
| 359 | ;; Package descriptor objects used inside the "archive-contents" file. | ||
| 360 | ;; Changing this defstruct implies changing the format of the | ||
| 361 | ;; "archive-contents" files. | ||
| 362 | (cl-defstruct (package--ac-desc | ||
| 363 | (:constructor package-make-ac-desc (version reqs summary kind extras)) | ||
| 364 | (:copier nil) | ||
| 365 | (:type vector)) | ||
| 366 | version reqs summary kind extras) | ||
| 367 | |||
| 368 | (defun package-get-descriptor (pkg-name) | ||
| 369 | "Return the `package-desc' of PKG-NAME." | ||
| 370 | (unless package--initialized (package-initialize 'no-activate)) | ||
| 371 | (or (package--get-activatable-pkg pkg-name) | ||
| 372 | (cadr (assq pkg-name package-alist)) | ||
| 373 | (cadr (assq pkg-name package-archive-contents)))) | ||
| 374 | |||
| 375 | (defun package--append-to-alist (pkg-desc alist) | ||
| 376 | "Append an entry for PKG-DESC to the start of ALIST and return it. | ||
| 377 | This entry takes the form (`package-desc-name' PKG-DESC). | ||
| 378 | |||
| 379 | If ALIST already has an entry with this name, destructively add | ||
| 380 | PKG-DESC to the cdr of this entry instead, sorted by version | ||
| 381 | number." | ||
| 382 | (let* ((name (package-desc-name pkg-desc)) | ||
| 383 | (priority-version (package-desc-priority-version pkg-desc)) | ||
| 384 | (existing-packages (assq name alist))) | ||
| 385 | (if (not existing-packages) | ||
| 386 | (cons (list name pkg-desc) | ||
| 387 | alist) | ||
| 388 | (while (if (and (cdr existing-packages) | ||
| 389 | (version-list-< priority-version | ||
| 390 | (package-desc-priority-version | ||
| 391 | (cadr existing-packages)))) | ||
| 392 | (setq existing-packages (cdr existing-packages)) | ||
| 393 | (push pkg-desc (cdr existing-packages)) | ||
| 394 | nil)) | ||
| 395 | alist))) | ||
| 396 | |||
| 397 | (defun package--add-to-archive-contents (package archive) | ||
| 398 | "Add the PACKAGE from the given ARCHIVE if necessary. | ||
| 399 | PACKAGE should have the form (NAME . PACKAGE--AC-DESC). | ||
| 400 | Also, add the originating archive to the `package-desc' structure." | ||
| 401 | (let* ((name (car package)) | ||
| 402 | (version (package--ac-desc-version (cdr package))) | ||
| 403 | (pkg-desc | ||
| 404 | (package-desc-create | ||
| 405 | :name name | ||
| 406 | :version version | ||
| 407 | :reqs (package--ac-desc-reqs (cdr package)) | ||
| 408 | :summary (package--ac-desc-summary (cdr package)) | ||
| 409 | :kind (package--ac-desc-kind (cdr package)) | ||
| 410 | :archive archive | ||
| 411 | :extras (and (> (length (cdr package)) 4) | ||
| 412 | ;; Older archive-contents files have only 4 | ||
| 413 | ;; elements here. | ||
| 414 | (package--ac-desc-extras (cdr package))))) | ||
| 415 | (pinned-to-archive (assoc name package-pinned-packages))) | ||
| 416 | ;; Skip entirely if pinned to another archive. | ||
| 417 | (when (not (and pinned-to-archive | ||
| 418 | (not (equal (cdr pinned-to-archive) archive)))) | ||
| 419 | (setq package-archive-contents | ||
| 420 | (package--append-to-alist pkg-desc package-archive-contents))))) | ||
| 421 | |||
| 422 | (defun package--read-archive-file (file) | ||
| 423 | "Read cached archive FILE data, if it exists. | ||
| 424 | Return the data from the file, or nil if the file does not exist. | ||
| 425 | If the archive version is too new, signal an error." | ||
| 426 | (let ((filename (expand-file-name file package-user-dir))) | ||
| 427 | (when (file-exists-p filename) | ||
| 428 | (with-temp-buffer | ||
| 429 | (let ((coding-system-for-read 'utf-8)) | ||
| 430 | (insert-file-contents filename)) | ||
| 431 | (let ((contents (read (current-buffer)))) | ||
| 432 | (if (> (car contents) package-archive-version) | ||
| 433 | (error "Package archive version %d is higher than %d" | ||
| 434 | (car contents) package-archive-version)) | ||
| 435 | (cdr contents)))))) | ||
| 436 | |||
| 437 | (defun package-read-archive-contents (archive) | ||
| 438 | "Read cached archive file for ARCHIVE. | ||
| 439 | If successful, set or update the variable `package-archive-contents'. | ||
| 440 | ARCHIVE should be a string matching the name of a package archive | ||
| 441 | in the variable `package-archives'. | ||
| 442 | If the archive version is too new, signal an error." | ||
| 443 | ;; Version 1 of 'archive-contents' is identical to our internal | ||
| 444 | ;; representation. | ||
| 445 | (let* ((contents-file (format "archives/%s/archive-contents" archive)) | ||
| 446 | (contents (package--read-archive-file contents-file))) | ||
| 447 | (when contents | ||
| 448 | (dolist (package contents) | ||
| 449 | (if package | ||
| 450 | (package--add-to-archive-contents package archive) | ||
| 451 | (lwarn '(package refresh) :warning | ||
| 452 | "Ignoring nil package on `%s' package archive" archive)))))) | ||
| 453 | |||
| 454 | (defvar package--old-archive-priorities nil | ||
| 455 | "Store currently used `package-archive-priorities'. | ||
| 456 | This is the value of `package-archive-priorities' last time | ||
| 457 | `package-read-all-archive-contents' was called. It can be used | ||
| 458 | by arbitrary functions to decide whether it is necessary to call | ||
| 459 | it again.") | ||
| 460 | |||
| 461 | (defvar package-read-archive-hook (list #'package-read-archive-contents) | ||
| 462 | "List of functions to call to read the archive contents. | ||
| 463 | Each function must take an optional argument, a symbol indicating | ||
| 464 | what archive to read in. The symbol ought to be a key in | ||
| 465 | `package-archives'.") | ||
| 466 | |||
| 467 | (defun package-read-all-archive-contents () | ||
| 468 | "Read cached archive file for all archives in `package-archives'. | ||
| 469 | If successful, set or update `package-archive-contents'." | ||
| 470 | (setq package-archive-contents nil) | ||
| 471 | (setq package--old-archive-priorities package-archive-priorities) | ||
| 472 | (dolist (archive package-archives) | ||
| 473 | (run-hook-with-args 'package-read-archive-hook (car archive)))) | ||
| 474 | |||
| 475 | (defvar package--downloads-in-progress nil | ||
| 476 | "List of in-progress asynchronous downloads.") | ||
| 477 | |||
| 478 | ;;;###autoload | ||
| 479 | (defun package-import-keyring (&optional file) | ||
| 480 | "Import keys from FILE." | ||
| 481 | (interactive "fFile: ") | ||
| 482 | (setq file (expand-file-name file)) | ||
| 483 | (let ((context (epg-make-context 'OpenPGP))) | ||
| 484 | (when package-gnupghome-dir | ||
| 485 | (with-file-modes #o700 | ||
| 486 | (make-directory package-gnupghome-dir t)) | ||
| 487 | (setf (epg-context-home-directory context) package-gnupghome-dir)) | ||
| 488 | (message "Importing %s..." (file-name-nondirectory file)) | ||
| 489 | (epg-import-keys-from-file context file) | ||
| 490 | (message "Importing %s...done" (file-name-nondirectory file)))) | ||
| 491 | |||
| 492 | (defvar package--post-download-archives-hook nil | ||
| 493 | "Hook run after the archive contents are downloaded. | ||
| 494 | Don't run this hook directly. It is meant to be run as part of | ||
| 495 | `package--update-downloads-in-progress'.") | ||
| 496 | (put 'package--post-download-archives-hook 'risky-local-variable t) | ||
| 497 | |||
| 498 | (defun package--update-downloads-in-progress (entry) | ||
| 499 | "Remove ENTRY from `package--downloads-in-progress'. | ||
| 500 | Once it's empty, run `package--post-download-archives-hook'." | ||
| 501 | ;; Keep track of the downloading progress. | ||
| 502 | (setq package--downloads-in-progress | ||
| 503 | (remove entry package--downloads-in-progress)) | ||
| 504 | ;; If this was the last download, run the hook. | ||
| 505 | (unless package--downloads-in-progress | ||
| 506 | (package-read-all-archive-contents) | ||
| 507 | (package--build-compatibility-table) | ||
| 508 | ;; We message before running the hook, so the hook can give | ||
| 509 | ;; messages as well. | ||
| 510 | (message "Package refresh done") | ||
| 511 | (run-hooks 'package--post-download-archives-hook))) | ||
| 512 | |||
| 513 | (defun package--download-one-archive (archive file &optional async) | ||
| 514 | "Retrieve an archive file FILE from ARCHIVE, and cache it. | ||
| 515 | ARCHIVE should be a cons cell of the form (NAME . LOCATION), | ||
| 516 | similar to an entry in `package-alist'. Save the cached copy to | ||
| 517 | \"archives/NAME/FILE\" in `package-user-dir'." | ||
| 518 | ;; The downloaded archive contents will be read as part of | ||
| 519 | ;; `package--update-downloads-in-progress'. | ||
| 520 | (when async | ||
| 521 | (cl-pushnew (cons archive file) package--downloads-in-progress | ||
| 522 | :test #'equal)) | ||
| 523 | (package--with-response-buffer (cdr archive) :file file | ||
| 524 | :async async | ||
| 525 | :error-form (package--update-downloads-in-progress (cons archive file)) | ||
| 526 | (let* ((location (cdr archive)) | ||
| 527 | (name (car archive)) | ||
| 528 | (content (buffer-string)) | ||
| 529 | (dir (expand-file-name (concat "archives/" name) package-user-dir)) | ||
| 530 | (local-file (expand-file-name file dir))) | ||
| 531 | (when (listp (read content)) | ||
| 532 | (make-directory dir t) | ||
| 533 | (if (or (not (package-check-signature)) | ||
| 534 | (member name package-unsigned-archives)) | ||
| 535 | ;; If we don't care about the signature, save the file and | ||
| 536 | ;; we're done. | ||
| 537 | (progn | ||
| 538 | (cl-assert (not enable-multibyte-characters)) | ||
| 539 | (let ((coding-system-for-write 'binary)) | ||
| 540 | (write-region content nil local-file nil 'silent)) | ||
| 541 | (package--update-downloads-in-progress (cons archive file))) | ||
| 542 | ;; If we care, check it (perhaps async) and *then* write the file. | ||
| 543 | (package--check-signature | ||
| 544 | location file content async | ||
| 545 | ;; This function will be called after signature checking. | ||
| 546 | (lambda (&optional good-sigs) | ||
| 547 | (cl-assert (not enable-multibyte-characters)) | ||
| 548 | (let ((coding-system-for-write 'binary)) | ||
| 549 | (write-region content nil local-file nil 'silent)) | ||
| 550 | ;; Write out good signatures into archive-contents.signed file. | ||
| 551 | (when good-sigs | ||
| 552 | (write-region (mapconcat #'epg-signature-to-string good-sigs "\n") | ||
| 553 | nil (concat local-file ".signed") nil 'silent))) | ||
| 554 | (lambda () (package--update-downloads-in-progress (cons archive file))))))))) | ||
| 555 | |||
| 556 | (defun package--download-and-read-archives (&optional async) | ||
| 557 | "Download descriptions of all `package-archives' and read them. | ||
| 558 | Populate `package-archive-contents' with the result. | ||
| 559 | |||
| 560 | If optional argument ASYNC is non-nil, perform the downloads | ||
| 561 | asynchronously." | ||
| 562 | (dolist (archive package-archives) | ||
| 563 | (condition-case-unless-debug err | ||
| 564 | (package--download-one-archive archive "archive-contents" async) | ||
| 565 | (error (message "Failed to download `%s' archive: %s" | ||
| 566 | (car archive) | ||
| 567 | (error-message-string err)))))) | ||
| 568 | |||
| 569 | (defvar package-refresh-contents-hook (list #'package--download-and-read-archives) | ||
| 570 | "List of functions to call to refresh the package archive. | ||
| 571 | Each function may take an optional argument indicating that the | ||
| 572 | operation ought to be executed asynchronously.") | ||
| 573 | |||
| 574 | ;;;###autoload | ||
| 575 | (defun package-refresh-contents (&optional async) | ||
| 576 | "Download descriptions of all configured ELPA packages. | ||
| 577 | For each archive configured in the variable `package-archives', | ||
| 578 | inform Emacs about the latest versions of all packages it offers, | ||
| 579 | and make them available for download. | ||
| 580 | Optional argument ASYNC specifies whether to perform the | ||
| 581 | downloads in the background. This is always the case when the command | ||
| 582 | is invoked interactively." | ||
| 583 | (interactive (list t)) | ||
| 584 | (when async | ||
| 585 | (message "Refreshing package contents...")) | ||
| 586 | (unless (file-exists-p package-user-dir) | ||
| 587 | (make-directory package-user-dir t)) | ||
| 588 | (let ((default-keyring (expand-file-name "package-keyring.gpg" | ||
| 589 | data-directory)) | ||
| 590 | (inhibit-message (or inhibit-message async))) | ||
| 591 | (when (and (package-check-signature) (file-exists-p default-keyring)) | ||
| 592 | (condition-case-unless-debug error | ||
| 593 | (package-import-keyring default-keyring) | ||
| 594 | (error (message "Cannot import default keyring: %s" | ||
| 595 | (error-message-string error)))))) | ||
| 596 | |||
| 597 | (run-hook-with-args 'package-refresh-contents-hook async)) | ||
| 598 | |||
| 599 | (defun package--archives-initialize () | ||
| 600 | "Make sure the list of installed and remote packages are initialized." | ||
| 601 | (unless package--initialized | ||
| 602 | (package-initialize t)) | ||
| 603 | (unless package-archive-contents | ||
| 604 | (package-refresh-contents))) | ||
| 605 | |||
| 606 | (defun package-archive-priority (archive) | ||
| 607 | "Return the priority of ARCHIVE. | ||
| 608 | |||
| 609 | The archive priorities are specified in | ||
| 610 | `package-archive-priorities'. If not given there, the priority | ||
| 611 | defaults to 0." | ||
| 612 | (or (cdr (assoc archive package-archive-priorities)) | ||
| 613 | 0)) | ||
| 614 | |||
| 615 | (defun package-desc-priority (pkg-desc) | ||
| 616 | "Return the priority of the archive of package-desc object PKG-DESC." | ||
| 617 | (package-archive-priority (package-desc-archive pkg-desc))) | ||
| 618 | |||
| 619 | (defun package-desc-priority-version (pkg-desc) | ||
| 620 | "Return the version PKG-DESC with the archive priority prepended. | ||
| 621 | |||
| 622 | This allows for easy comparison of package versions from | ||
| 623 | different archives if archive priorities are meant to be taken in | ||
| 624 | consideration." | ||
| 625 | (cons (package-desc-priority pkg-desc) | ||
| 626 | (package-desc-version pkg-desc))) | ||
| 627 | |||
| 628 | (provide 'package-elpa) | ||
| 629 | ;;; package-elpa.el ends here | ||
diff --git a/lisp/package/package-install.el b/lisp/package/package-install.el new file mode 100644 index 00000000000..5a96fedd528 --- /dev/null +++ b/lisp/package/package-install.el | |||
| @@ -0,0 +1,1053 @@ | |||
| 1 | ;;; package-install.el --- Physical Package Management -*- lexical-binding: t; -*- | ||
| 2 | |||
| 3 | ;; Copyright (C) 2025 Philip Kaludercic | ||
| 4 | |||
| 5 | ;; Author: Philip Kaludercic <philipk@posteo.net> | ||
| 6 | ;; Keywords: | ||
| 7 | |||
| 8 | ;; This program is free software; you can redistribute it and/or modify | ||
| 9 | ;; it under the terms of the GNU General Public License as published by | ||
| 10 | ;; the Free Software Foundation, either version 3 of the License, or | ||
| 11 | ;; (at your option) any later version. | ||
| 12 | |||
| 13 | ;; This program is distributed in the hope that it will be useful, | ||
| 14 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 15 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 16 | ;; GNU General Public License for more details. | ||
| 17 | |||
| 18 | ;; You should have received a copy of the GNU General Public License | ||
| 19 | ;; along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
| 20 | |||
| 21 | ;;; Commentary: | ||
| 22 | |||
| 23 | ;; | ||
| 24 | |||
| 25 | ;;; Code: | ||
| 26 | |||
| 27 | (require 'package-core) | ||
| 28 | (require 'package-misc) | ||
| 29 | (require 'package-elpa) | ||
| 30 | (require 'package-compile) | ||
| 31 | (require 'package-quickstart) | ||
| 32 | |||
| 33 | (require 'epg) | ||
| 34 | (require 'tar-mode) | ||
| 35 | (require 'lisp-mnt) | ||
| 36 | |||
| 37 | (defcustom package-install-upgrade-built-in nil | ||
| 38 | "Non-nil means that built-in packages can be upgraded via a package archive. | ||
| 39 | If disabled, then `package-install' will not suggest to replace a | ||
| 40 | built-in package with a (possibly newer) version from a package archive." | ||
| 41 | :type 'boolean | ||
| 42 | :version "29.1" | ||
| 43 | :group 'package) | ||
| 44 | |||
| 45 | (defun package-compute-transaction (packages requirements &optional seen) | ||
| 46 | "Return a list of packages to be installed, including PACKAGES. | ||
| 47 | PACKAGES should be a list of `package-desc'. | ||
| 48 | |||
| 49 | REQUIREMENTS should be a list of additional requirements; each | ||
| 50 | element in this list should have the form (PACKAGE VERSION-LIST), | ||
| 51 | where PACKAGE is a package name and VERSION-LIST is the required | ||
| 52 | version of that package. | ||
| 53 | |||
| 54 | This function recursively computes the requirements of the | ||
| 55 | packages in REQUIREMENTS, and returns a list of all the packages | ||
| 56 | that must be installed. Packages that are already installed are | ||
| 57 | not included in this list. | ||
| 58 | |||
| 59 | SEEN is used internally to detect infinite recursion." | ||
| 60 | ;; FIXME: We really should use backtracking to explore the whole | ||
| 61 | ;; search space (e.g. if foo require bar-1.3, and bar-1.4 requires toto-1.1 | ||
| 62 | ;; whereas bar-1.3 requires toto-1.0 and the user has put a hold on toto-1.0: | ||
| 63 | ;; the current code might fail to see that it could install foo by using the | ||
| 64 | ;; older bar-1.3). | ||
| 65 | (dolist (elt requirements) | ||
| 66 | (let* ((next-pkg (car elt)) | ||
| 67 | (next-version (cadr elt)) | ||
| 68 | (already ())) | ||
| 69 | (dolist (pkg packages) | ||
| 70 | (if (eq next-pkg (package-desc-name pkg)) | ||
| 71 | (setq already pkg))) | ||
| 72 | (when already | ||
| 73 | (if (version-list-<= next-version (package-desc-version already)) | ||
| 74 | ;; `next-pkg' is already in `packages', but its position there | ||
| 75 | ;; means it might be installed too late: remove it from there, so | ||
| 76 | ;; we re-add it (along with its dependencies) at an earlier place | ||
| 77 | ;; below (bug#16994). | ||
| 78 | (if (memq already seen) ;Avoid inf-loop on dependency cycles. | ||
| 79 | (message "Dependency cycle going through %S" | ||
| 80 | (package-desc-full-name already)) | ||
| 81 | (setq packages (delq already packages)) | ||
| 82 | (setq already nil)) | ||
| 83 | (error "Need package `%s-%s', but only %s is being installed" | ||
| 84 | next-pkg (package-version-join next-version) | ||
| 85 | (package-version-join (package-desc-version already))))) | ||
| 86 | (cond | ||
| 87 | (already nil) | ||
| 88 | ((package-installed-p next-pkg next-version) nil) | ||
| 89 | |||
| 90 | (t | ||
| 91 | ;; A package is required, but not installed. It might also be | ||
| 92 | ;; blocked via `package-load-list'. | ||
| 93 | (let ((pkg-descs (cdr (assq next-pkg package-archive-contents))) | ||
| 94 | (found nil) | ||
| 95 | (found-something nil) | ||
| 96 | (problem nil)) | ||
| 97 | (while (and pkg-descs (not found)) | ||
| 98 | (let* ((pkg-desc (pop pkg-descs)) | ||
| 99 | (version (package-desc-version pkg-desc)) | ||
| 100 | (disabled (package-disabled-p next-pkg version))) | ||
| 101 | (cond | ||
| 102 | ((version-list-< version next-version) | ||
| 103 | ;; pkg-descs is sorted by priority, not version, so | ||
| 104 | ;; don't error just yet. | ||
| 105 | (unless found-something | ||
| 106 | (setq found-something (package-version-join version)))) | ||
| 107 | (disabled | ||
| 108 | (unless problem | ||
| 109 | (setq problem | ||
| 110 | (if (stringp disabled) | ||
| 111 | (format-message | ||
| 112 | "Package `%s' held at version %s, but version %s required" | ||
| 113 | next-pkg disabled | ||
| 114 | (package-version-join next-version)) | ||
| 115 | (format-message "Required package `%s' is disabled" | ||
| 116 | next-pkg))))) | ||
| 117 | (t (setq found pkg-desc))))) | ||
| 118 | (unless found | ||
| 119 | (cond | ||
| 120 | (problem (error "%s" problem)) | ||
| 121 | (found-something | ||
| 122 | (error "Need package `%s-%s', but only %s is available" | ||
| 123 | next-pkg (package-version-join next-version) | ||
| 124 | found-something)) | ||
| 125 | (t | ||
| 126 | (if (eq next-pkg 'emacs) | ||
| 127 | (error "This package requires Emacs version %s" | ||
| 128 | (package-version-join next-version)) | ||
| 129 | (error (if (not next-version) | ||
| 130 | (format "Package `%s' is unavailable" next-pkg) | ||
| 131 | (format "Package `%s' (version %s) is unavailable" | ||
| 132 | next-pkg (package-version-join next-version)))))))) | ||
| 133 | (setq packages | ||
| 134 | (package-compute-transaction (cons found packages) | ||
| 135 | (package-desc-reqs found) | ||
| 136 | (cons found seen)))))))) | ||
| 137 | packages) | ||
| 138 | |||
| 139 | (defun package--get-deps (pkgs) | ||
| 140 | (let ((seen '())) | ||
| 141 | (while pkgs | ||
| 142 | (let ((pkg (pop pkgs))) | ||
| 143 | (if (memq pkg seen) | ||
| 144 | nil ;; Done already! | ||
| 145 | (let ((pkg-desc (cadr (assq pkg package-alist)))) | ||
| 146 | (when pkg-desc | ||
| 147 | (push pkg seen) | ||
| 148 | (setq pkgs (append (mapcar #'car (package-desc-reqs pkg-desc)) | ||
| 149 | pkgs))))))) | ||
| 150 | seen)) | ||
| 151 | |||
| 152 | (defun package--user-installed-p (package) | ||
| 153 | "Return non-nil if PACKAGE is a user-installed package. | ||
| 154 | PACKAGE is the package name, a symbol. Check whether the package | ||
| 155 | was installed into `package-user-dir' where we assume to have | ||
| 156 | control over." | ||
| 157 | (let* ((pkg-desc (cadr (assq package package-alist))) | ||
| 158 | (dir (package-desc-dir pkg-desc))) | ||
| 159 | (file-in-directory-p dir package-user-dir))) | ||
| 160 | |||
| 161 | (defun package--removable-packages () | ||
| 162 | "Return a list of names of packages no longer needed. | ||
| 163 | These are packages which are neither contained in | ||
| 164 | `package-selected-packages' nor a dependency of one that is." | ||
| 165 | (let ((needed (package--get-deps package-selected-packages))) | ||
| 166 | (cl-loop for p in (mapcar #'car package-alist) | ||
| 167 | unless (or (memq p needed) | ||
| 168 | ;; Do not auto-remove external packages. | ||
| 169 | (not (package--user-installed-p p))) | ||
| 170 | collect p))) | ||
| 171 | |||
| 172 | (defun package--used-elsewhere-p (pkg-desc &optional pkg-list all) | ||
| 173 | "Non-nil if PKG-DESC is a dependency of a package in PKG-LIST. | ||
| 174 | Return the first package found in PKG-LIST of which PKG is a | ||
| 175 | dependency. If ALL is non-nil, return all such packages instead. | ||
| 176 | |||
| 177 | When not specified, PKG-LIST defaults to `package-alist' | ||
| 178 | with PKG-DESC entry removed." | ||
| 179 | (unless (string= (package-desc-status pkg-desc) "obsolete") | ||
| 180 | (let* ((pkg (package-desc-name pkg-desc)) | ||
| 181 | (alist (or pkg-list | ||
| 182 | (remove (assq pkg package-alist) | ||
| 183 | package-alist)))) | ||
| 184 | (if all | ||
| 185 | (cl-loop for p in alist | ||
| 186 | if (assq pkg (package-desc-reqs (cadr p))) | ||
| 187 | collect (cadr p)) | ||
| 188 | (cl-loop for p in alist thereis | ||
| 189 | (and (assq pkg (package-desc-reqs (cadr p))) | ||
| 190 | (cadr p))))))) | ||
| 191 | |||
| 192 | (defun package--sort-deps-in-alist (package only) | ||
| 193 | "Return a list of dependencies for PACKAGE sorted by dependency. | ||
| 194 | PACKAGE is included as the first element of the returned list. | ||
| 195 | ONLY is an alist associating package names to package objects. | ||
| 196 | Only these packages will be in the return value and their cdrs are | ||
| 197 | destructively set to nil in ONLY." | ||
| 198 | (let ((out)) | ||
| 199 | (dolist (dep (package-desc-reqs package)) | ||
| 200 | (when-let* ((cell (assq (car dep) only)) | ||
| 201 | (dep-package (cdr-safe cell))) | ||
| 202 | (setcdr cell nil) | ||
| 203 | (setq out (append (package--sort-deps-in-alist dep-package only) | ||
| 204 | out)))) | ||
| 205 | (cons package out))) | ||
| 206 | |||
| 207 | (defun package--sort-by-dependence (package-list) | ||
| 208 | "Return PACKAGE-LIST sorted by dependence. | ||
| 209 | That is, any element of the returned list is guaranteed to not | ||
| 210 | directly depend on any elements that come before it. | ||
| 211 | |||
| 212 | PACKAGE-LIST is a list of `package-desc' objects. | ||
| 213 | Indirect dependencies are guaranteed to be returned in order only | ||
| 214 | if all the in-between dependencies are also in PACKAGE-LIST." | ||
| 215 | (let ((alist (mapcar (lambda (p) (cons (package-desc-name p) p)) package-list)) | ||
| 216 | out-list) | ||
| 217 | (dolist (cell alist out-list) | ||
| 218 | ;; `package--sort-deps-in-alist' destructively changes alist, so | ||
| 219 | ;; some cells might already be empty. We check this here. | ||
| 220 | (when-let* ((pkg-desc (cdr cell))) | ||
| 221 | (setcdr cell nil) | ||
| 222 | (setq out-list | ||
| 223 | (append (package--sort-deps-in-alist pkg-desc alist) | ||
| 224 | out-list)))))) | ||
| 225 | |||
| 226 | |||
| 227 | ;;; Installation Functions | ||
| 228 | ;; As opposed to the previous section (which listed some underlying | ||
| 229 | ;; functions necessary for installation), this one contains the actual | ||
| 230 | ;; functions that install packages. The package itself can be | ||
| 231 | ;; installed in a variety of ways (archives, buffer, file), but | ||
| 232 | ;; requirements (dependencies) are always satisfied by looking in | ||
| 233 | ;; `package-archive-contents'. | ||
| 234 | |||
| 235 | (defun package-archive-base (desc) | ||
| 236 | "Return the package described by DESC." | ||
| 237 | (cdr (assoc (package-desc-archive desc) package-archives))) | ||
| 238 | |||
| 239 | (defun package-desc-suffix (pkg-desc) | ||
| 240 | "Return file-name extension of package-desc object PKG-DESC. | ||
| 241 | Depending on the `package-desc-kind' of PKG-DESC, this is one of: | ||
| 242 | |||
| 243 | \\='single - \".el\" | ||
| 244 | \\='tar - \".tar\" | ||
| 245 | \\='dir - \"\" | ||
| 246 | |||
| 247 | Signal an error if the kind is none of the above." | ||
| 248 | (pcase (package-desc-kind pkg-desc) | ||
| 249 | ('single ".el") | ||
| 250 | ('tar ".tar") | ||
| 251 | ('dir "") | ||
| 252 | (kind (error "Unknown package kind: %s" kind)))) | ||
| 253 | |||
| 254 | (defun package-install-from-archive (pkg-desc) | ||
| 255 | "Download and install a package defined by PKG-DESC." | ||
| 256 | ;; This won't happen, unless the archive is doing something wrong. | ||
| 257 | (when (eq (package-desc-kind pkg-desc) 'dir) | ||
| 258 | (error "Can't install directory package from archive")) | ||
| 259 | (let* ((location (package-archive-base pkg-desc)) | ||
| 260 | (file (concat (package-desc-full-name pkg-desc) | ||
| 261 | (package-desc-suffix pkg-desc)))) | ||
| 262 | (package--with-response-buffer location :file file | ||
| 263 | (if (or (not (package-check-signature)) | ||
| 264 | (member (package-desc-archive pkg-desc) | ||
| 265 | package-unsigned-archives)) | ||
| 266 | ;; If we don't care about the signature, unpack and we're | ||
| 267 | ;; done. | ||
| 268 | (let ((save-silently t)) | ||
| 269 | (package-unpack pkg-desc)) | ||
| 270 | ;; If we care, check it and *then* write the file. | ||
| 271 | (let ((content (buffer-string))) | ||
| 272 | (package--check-signature | ||
| 273 | location file content nil | ||
| 274 | ;; This function will be called after signature checking. | ||
| 275 | (lambda (&optional good-sigs) | ||
| 276 | ;; Signature checked, unpack now. | ||
| 277 | (with-temp-buffer ;FIXME: Just use the previous current-buffer. | ||
| 278 | (set-buffer-multibyte nil) | ||
| 279 | (cl-assert (not (multibyte-string-p content))) | ||
| 280 | (insert content) | ||
| 281 | (let ((save-silently t)) | ||
| 282 | (package-unpack pkg-desc))) | ||
| 283 | ;; Here the package has been installed successfully, mark it as | ||
| 284 | ;; signed if appropriate. | ||
| 285 | (when good-sigs | ||
| 286 | ;; Write out good signatures into NAME-VERSION.signed file. | ||
| 287 | (write-region (mapconcat #'epg-signature-to-string good-sigs "\n") | ||
| 288 | nil | ||
| 289 | (expand-file-name | ||
| 290 | (concat (package-desc-full-name pkg-desc) ".signed") | ||
| 291 | package-user-dir) | ||
| 292 | nil 'silent) | ||
| 293 | ;; Update the old pkg-desc which will be shown on the description buffer. | ||
| 294 | (setf (package-desc-signed pkg-desc) t) | ||
| 295 | ;; Update the new (activated) pkg-desc as well. | ||
| 296 | (when-let* ((pkg-descs (cdr (assq (package-desc-name pkg-desc) | ||
| 297 | package-alist)))) | ||
| 298 | (setf (package-desc-signed (car pkg-descs)) t)))))))))) | ||
| 299 | |||
| 300 | ;;;###autoload | ||
| 301 | (defun package-installed-p (package &optional min-version) | ||
| 302 | "Return non-nil if PACKAGE, of MIN-VERSION or newer, is installed. | ||
| 303 | If PACKAGE is a symbol, it is the package name and MIN-VERSION | ||
| 304 | should be a version list. | ||
| 305 | |||
| 306 | If PACKAGE is a `package-desc' object, MIN-VERSION is ignored." | ||
| 307 | (cond | ||
| 308 | ((package-desc-p package) | ||
| 309 | (let ((dir (package-desc-dir package))) | ||
| 310 | (and (stringp dir) | ||
| 311 | (file-exists-p dir)))) | ||
| 312 | ((and (not package--initialized) | ||
| 313 | (null min-version) | ||
| 314 | package-activated-list) | ||
| 315 | ;; We used the quickstart: make it possible to use package-installed-p | ||
| 316 | ;; even before package is fully initialized. | ||
| 317 | (or | ||
| 318 | (memq package package-activated-list) | ||
| 319 | ;; Also check built-in packages. | ||
| 320 | (package-built-in-p package min-version))) | ||
| 321 | (t | ||
| 322 | (or | ||
| 323 | (let ((pkg-descs (cdr (assq package (package--alist))))) | ||
| 324 | (and pkg-descs | ||
| 325 | (version-list-<= min-version | ||
| 326 | (package-desc-version (car pkg-descs))))) | ||
| 327 | ;; Also check built-in packages. | ||
| 328 | (package-built-in-p package min-version))))) | ||
| 329 | |||
| 330 | (defun package-download-transaction (packages) | ||
| 331 | "Download and install all the packages in PACKAGES. | ||
| 332 | PACKAGES should be a list of `package-desc'. | ||
| 333 | This function assumes that all package requirements in | ||
| 334 | PACKAGES are satisfied, i.e. that PACKAGES is computed | ||
| 335 | using `package-compute-transaction'." | ||
| 336 | (mapc #'package-install-from-archive packages)) | ||
| 337 | |||
| 338 | ;;;###autoload | ||
| 339 | (defun package-install (pkg &optional dont-select) | ||
| 340 | "Install the package PKG. | ||
| 341 | |||
| 342 | PKG can be a `package-desc', or a symbol naming one of the available | ||
| 343 | packages in an archive in `package-archives'. | ||
| 344 | |||
| 345 | Mark the installed package as selected by adding it to | ||
| 346 | `package-selected-packages'. | ||
| 347 | |||
| 348 | When called from Lisp and optional argument DONT-SELECT is | ||
| 349 | non-nil, install the package but do not add it to | ||
| 350 | `package-selected-packages'. | ||
| 351 | |||
| 352 | If PKG is a `package-desc' and it is already installed, don't try | ||
| 353 | to install it but still mark it as selected. | ||
| 354 | |||
| 355 | If the command is invoked with a prefix argument, it will allow | ||
| 356 | upgrading of built-in packages, as if `package-install-upgrade-built-in' | ||
| 357 | had been enabled." | ||
| 358 | (interactive | ||
| 359 | (progn | ||
| 360 | ;; Initialize the package system to get the list of package | ||
| 361 | ;; symbols for completion. | ||
| 362 | (package--archives-initialize) | ||
| 363 | (list (intern (completing-read | ||
| 364 | "Install package: " | ||
| 365 | (mapcan | ||
| 366 | (lambda (elt) | ||
| 367 | (and (or (and (or current-prefix-arg | ||
| 368 | package-install-upgrade-built-in) | ||
| 369 | (package--active-built-in-p (car elt))) | ||
| 370 | (not (package-installed-p (car elt)))) | ||
| 371 | (list (symbol-name (car elt))))) | ||
| 372 | package-archive-contents) | ||
| 373 | nil t)) | ||
| 374 | nil))) | ||
| 375 | (cl-check-type pkg (or symbol package-desc)) | ||
| 376 | (package--archives-initialize) | ||
| 377 | (when (fboundp 'package-menu--post-refresh) | ||
| 378 | (add-hook 'post-command-hook #'package-menu--post-refresh)) | ||
| 379 | (let ((name (if (package-desc-p pkg) | ||
| 380 | (package-desc-name pkg) | ||
| 381 | pkg))) | ||
| 382 | (unless (or dont-select (package--user-selected-p name)) | ||
| 383 | (package--save-selected-packages | ||
| 384 | (cons name package-selected-packages))) | ||
| 385 | (when (and (or current-prefix-arg package-install-upgrade-built-in) | ||
| 386 | (package--active-built-in-p pkg)) | ||
| 387 | (setq pkg (or (cadr (assq name package-archive-contents)) pkg))) | ||
| 388 | (if-let* ((transaction | ||
| 389 | (if (package-desc-p pkg) | ||
| 390 | (unless (package-installed-p pkg) | ||
| 391 | (package-compute-transaction (list pkg) | ||
| 392 | (package-desc-reqs pkg))) | ||
| 393 | (package-compute-transaction () (list (list pkg)))))) | ||
| 394 | (progn | ||
| 395 | (package-download-transaction transaction) | ||
| 396 | (package--quickstart-maybe-refresh) | ||
| 397 | (message "Package `%s' installed." name)) | ||
| 398 | (message "`%s' is already installed" name)))) | ||
| 399 | |||
| 400 | (declare-function package-vc-upgrade "package-vc" (pkg)) | ||
| 401 | |||
| 402 | ;;;###autoload | ||
| 403 | (defun package-upgrade (name) | ||
| 404 | "Upgrade package NAME if a newer version exists. | ||
| 405 | |||
| 406 | NAME should be a symbol." | ||
| 407 | (interactive | ||
| 408 | (list (intern (completing-read | ||
| 409 | "Upgrade package: " | ||
| 410 | (package--upgradeable-packages t) nil t)))) | ||
| 411 | (cl-check-type name symbol) | ||
| 412 | (let* ((pkg-desc (cadr (assq name package-alist))) | ||
| 413 | (package-install-upgrade-built-in (not pkg-desc))) | ||
| 414 | ;; `pkg-desc' will be nil when the package is an "active built-in". | ||
| 415 | (if (and pkg-desc (package-vc-p pkg-desc)) | ||
| 416 | (package-vc-upgrade pkg-desc) | ||
| 417 | (when pkg-desc | ||
| 418 | (package-delete pkg-desc 'force 'dont-unselect)) | ||
| 419 | (package-install name | ||
| 420 | ;; An active built-in has never been "selected" | ||
| 421 | ;; before. Mark it as installed explicitly. | ||
| 422 | (and pkg-desc 'dont-select))))) | ||
| 423 | |||
| 424 | (defun package--upgradeable-packages (&optional include-builtins) | ||
| 425 | ;; Initialize the package system to get the list of package | ||
| 426 | ;; symbols for completion. | ||
| 427 | (package--archives-initialize) | ||
| 428 | (mapcar | ||
| 429 | #'car | ||
| 430 | (seq-filter | ||
| 431 | (lambda (elt) | ||
| 432 | (or (let ((available | ||
| 433 | (assq (car elt) package-archive-contents))) | ||
| 434 | (and available | ||
| 435 | (or (and | ||
| 436 | include-builtins | ||
| 437 | (not (package-desc-version (cadr elt)))) | ||
| 438 | (version-list-< | ||
| 439 | (package-desc-version (cadr elt)) | ||
| 440 | (package-desc-version (cadr available)))))) | ||
| 441 | (package-vc-p (cadr elt)))) | ||
| 442 | (if include-builtins | ||
| 443 | (append package-alist | ||
| 444 | (mapcan | ||
| 445 | (lambda (elt) | ||
| 446 | (when (not (assq (car elt) package-alist)) | ||
| 447 | (list (list (car elt) (package--from-builtin elt))))) | ||
| 448 | package--builtins)) | ||
| 449 | package-alist)))) | ||
| 450 | |||
| 451 | ;;;###autoload | ||
| 452 | (defun package-upgrade-all (&optional query) | ||
| 453 | "Refresh package list and upgrade all packages. | ||
| 454 | If QUERY, ask the user before upgrading packages. When called | ||
| 455 | interactively, QUERY is always true. | ||
| 456 | |||
| 457 | Currently, packages which are part of the Emacs distribution are | ||
| 458 | not upgraded by this command. To enable upgrading such a package | ||
| 459 | using this command, first upgrade the package to a newer version | ||
| 460 | from ELPA by either using `\\[package-upgrade]' or | ||
| 461 | `\\<package-menu-mode-map>\\[package-menu-mark-install]' after `\\[list-packages]'." | ||
| 462 | (interactive (list (not noninteractive))) | ||
| 463 | (package-refresh-contents) | ||
| 464 | (let ((upgradeable (package--upgradeable-packages))) | ||
| 465 | (if (not upgradeable) | ||
| 466 | (message "No packages to upgrade") | ||
| 467 | (when (and query | ||
| 468 | (not (yes-or-no-p | ||
| 469 | (if (length= upgradeable 1) | ||
| 470 | "One package to upgrade. Do it? " | ||
| 471 | (format "%s packages to upgrade. Do it?" | ||
| 472 | (length upgradeable)))))) | ||
| 473 | (user-error "Upgrade aborted")) | ||
| 474 | (mapc #'package-upgrade upgradeable)))) | ||
| 475 | |||
| 476 | (defun package--dependencies (pkg) | ||
| 477 | "Return a list of all transitive dependencies of PKG. | ||
| 478 | If PKG is a package descriptor, the return value is a list of | ||
| 479 | package descriptors. If PKG is a symbol designating a package, | ||
| 480 | the return value is a list of symbols designating packages." | ||
| 481 | (when-let* ((desc (if (package-desc-p pkg) pkg | ||
| 482 | (cadr (assq pkg package-archive-contents))))) | ||
| 483 | ;; Can we have circular dependencies? Assume "nope". | ||
| 484 | (let ((all (named-let more ((pkg-desc desc)) | ||
| 485 | (let (deps) | ||
| 486 | (dolist (req (package-desc-reqs pkg-desc)) | ||
| 487 | (setq deps (nconc | ||
| 488 | (catch 'found | ||
| 489 | (dolist (p (apply #'append (mapcar #'cdr (package--alist)))) | ||
| 490 | (when (and (string= (car req) (package-desc-name p)) | ||
| 491 | (version-list-<= (cadr req) (package-desc-version p))) | ||
| 492 | (throw 'found (more p))))) | ||
| 493 | deps))) | ||
| 494 | (delete-dups (cons pkg-desc deps)))))) | ||
| 495 | (remq pkg (mapcar (if (package-desc-p pkg) #'identity #'package-desc-name) all))))) | ||
| 496 | |||
| 497 | (defun package-buffer-info () | ||
| 498 | "Return a `package-desc' describing the package in the current buffer. | ||
| 499 | |||
| 500 | If the buffer does not contain a conforming package, signal an | ||
| 501 | error. If there is a package, narrow the buffer to the file's | ||
| 502 | boundaries." | ||
| 503 | (goto-char (point-min)) | ||
| 504 | (unless (re-search-forward "^;;; \\([^ ]*\\)\\.el ---[ \t]*\\(.*?\\)[ \t]*\\(-\\*-.*-\\*-[ \t]*\\)?$" nil t) | ||
| 505 | (error "Package lacks a file header")) | ||
| 506 | (let ((file-name (match-string-no-properties 1)) | ||
| 507 | (desc (match-string-no-properties 2))) | ||
| 508 | (require 'lisp-mnt) | ||
| 509 | (let* ((version-info (lm-package-version)) | ||
| 510 | (pkg-version (package-strip-rcs-id version-info)) | ||
| 511 | (keywords (lm-keywords-list)) | ||
| 512 | (website (lm-website))) | ||
| 513 | (unless pkg-version | ||
| 514 | (if version-info | ||
| 515 | (error "Unrecognized package version: %s" version-info) | ||
| 516 | (error "Package lacks a \"Version\" or \"Package-Version\" header"))) | ||
| 517 | (package-desc-from-define | ||
| 518 | file-name pkg-version desc | ||
| 519 | (lm-package-requires) | ||
| 520 | :kind 'single | ||
| 521 | :url website | ||
| 522 | :keywords keywords | ||
| 523 | :maintainer | ||
| 524 | ;; For backward compatibility, use a single cons-cell if | ||
| 525 | ;; there's only one maintainer (the most common case). | ||
| 526 | (let ((maints (lm-maintainers))) (if (cdr maints) maints (car maints))) | ||
| 527 | :authors (lm-authors))))) | ||
| 528 | |||
| 529 | (defun package-dir-info () | ||
| 530 | "Find package information for a directory. | ||
| 531 | The return result is a `package-desc'." | ||
| 532 | (cl-assert (derived-mode-p 'dired-mode)) | ||
| 533 | (let* ((desc-file (package--description-file default-directory))) | ||
| 534 | (if (file-readable-p desc-file) | ||
| 535 | (with-temp-buffer | ||
| 536 | (insert-file-contents desc-file) | ||
| 537 | (package--read-pkg-desc 'dir)) | ||
| 538 | (catch 'found | ||
| 539 | (let ((files (or (and (derived-mode-p 'dired-mode) | ||
| 540 | (dired-get-marked-files)) | ||
| 541 | (directory-files-recursively default-directory "\\.el\\'")))) | ||
| 542 | ;; We sort the file names in lexicographical order, to ensure | ||
| 543 | ;; that we check shorter file names first (ie. those further | ||
| 544 | ;; up in the directory structure). | ||
| 545 | (dolist (file (sort files)) | ||
| 546 | ;; The file may be a link to a nonexistent file; e.g., a | ||
| 547 | ;; lock file. | ||
| 548 | (when (file-exists-p file) | ||
| 549 | (with-temp-buffer | ||
| 550 | (insert-file-contents file) | ||
| 551 | ;; When we find the file with the data, | ||
| 552 | (when-let* ((info (ignore-errors (package-buffer-info)))) | ||
| 553 | (setf (package-desc-kind info) 'dir) | ||
| 554 | (throw 'found info)))))) | ||
| 555 | (error "No .el files with package headers in `%s'" default-directory))))) | ||
| 556 | |||
| 557 | ;;;###autoload | ||
| 558 | (defun package-install-from-buffer () | ||
| 559 | "Install a package from the current buffer. | ||
| 560 | The current buffer is assumed to be a single .el or .tar file or | ||
| 561 | a directory. These must follow the packaging guidelines (see | ||
| 562 | info node `(elisp)Packaging'). | ||
| 563 | |||
| 564 | Specially, if current buffer is a directory, the -pkg.el | ||
| 565 | description file is not mandatory, in which case the information | ||
| 566 | is derived from the main .el file in the directory. Using Dired, | ||
| 567 | you can restrict what files to install by marking specific files. | ||
| 568 | |||
| 569 | Downloads and installs required packages as needed." | ||
| 570 | (interactive) | ||
| 571 | (let* ((pkg-desc | ||
| 572 | (cond | ||
| 573 | ((derived-mode-p 'dired-mode) | ||
| 574 | ;; This is the only way a package-desc object with a `dir' | ||
| 575 | ;; desc-kind can be created. Such packages can't be | ||
| 576 | ;; uploaded or installed from archives, they can only be | ||
| 577 | ;; installed from local buffers or directories. | ||
| 578 | (package-dir-info)) | ||
| 579 | ((derived-mode-p 'tar-mode) | ||
| 580 | (package-tar-file-info)) | ||
| 581 | (t | ||
| 582 | ;; Package headers should be parsed from decoded text | ||
| 583 | ;; (see Bug#48137) where possible. | ||
| 584 | (if (and (eq buffer-file-coding-system 'no-conversion) | ||
| 585 | buffer-file-name) | ||
| 586 | (let* ((package-buffer (current-buffer)) | ||
| 587 | (decoding-system | ||
| 588 | (car (find-operation-coding-system | ||
| 589 | 'insert-file-contents | ||
| 590 | (cons buffer-file-name | ||
| 591 | package-buffer))))) | ||
| 592 | (with-temp-buffer | ||
| 593 | (insert-buffer-substring package-buffer) | ||
| 594 | (decode-coding-region (point-min) (point-max) | ||
| 595 | decoding-system) | ||
| 596 | (package-buffer-info))) | ||
| 597 | |||
| 598 | (save-excursion | ||
| 599 | (package-buffer-info)))))) | ||
| 600 | (name (package-desc-name pkg-desc))) | ||
| 601 | ;; Download and install the dependencies. | ||
| 602 | (let* ((requires (package-desc-reqs pkg-desc)) | ||
| 603 | (transaction (package-compute-transaction nil requires))) | ||
| 604 | (package-download-transaction transaction)) | ||
| 605 | ;; Install the package itself. | ||
| 606 | (package-unpack pkg-desc) | ||
| 607 | (unless (package--user-selected-p name) | ||
| 608 | (package--save-selected-packages | ||
| 609 | (cons name package-selected-packages))) | ||
| 610 | (package--quickstart-maybe-refresh) | ||
| 611 | pkg-desc)) | ||
| 612 | |||
| 613 | ;;;###autoload | ||
| 614 | (defun package-install-file (file) | ||
| 615 | "Install a package from FILE. | ||
| 616 | The file can either be a tar file, an Emacs Lisp file, or a | ||
| 617 | directory." | ||
| 618 | (interactive "fPackage file name: ") | ||
| 619 | (with-temp-buffer | ||
| 620 | (if (file-directory-p file) | ||
| 621 | (progn | ||
| 622 | (setq default-directory file) | ||
| 623 | (dired-mode)) | ||
| 624 | (insert-file-contents-literally file) | ||
| 625 | (set-visited-file-name file) | ||
| 626 | (set-buffer-modified-p nil) | ||
| 627 | (when (string-match "\\.tar\\'" file) (tar-mode))) | ||
| 628 | (package-install-from-buffer))) | ||
| 629 | |||
| 630 | |||
| 631 | |||
| 632 | ;;;###autoload | ||
| 633 | (defun package-install-selected-packages (&optional noconfirm) | ||
| 634 | "Ensure packages in `package-selected-packages' are installed. | ||
| 635 | If some packages are not installed, propose to install them. | ||
| 636 | |||
| 637 | If optional argument NOCONFIRM is non-nil, or when invoked with a prefix | ||
| 638 | argument, don't ask for confirmation to install packages." | ||
| 639 | (interactive "P") | ||
| 640 | (package--archives-initialize) | ||
| 641 | ;; We don't need to populate `package-selected-packages' before | ||
| 642 | ;; using here, because the outcome is the same either way (nothing | ||
| 643 | ;; gets installed). | ||
| 644 | (if (not package-selected-packages) | ||
| 645 | (message "`package-selected-packages' is empty, nothing to install") | ||
| 646 | (let* ((not-installed (seq-remove #'package-installed-p package-selected-packages)) | ||
| 647 | (available (seq-filter (lambda (p) (assq p package-archive-contents)) not-installed)) | ||
| 648 | (difference (- (length not-installed) (length available)))) | ||
| 649 | (cond | ||
| 650 | (available | ||
| 651 | (when (or noconfirm | ||
| 652 | (y-or-n-p | ||
| 653 | (format "Packages to install: %d (%s), proceed? " | ||
| 654 | (length available) | ||
| 655 | (mapconcat #'symbol-name available " ")))) | ||
| 656 | (mapc (lambda (p) (package-install p 'dont-select)) available))) | ||
| 657 | ((> difference 0) | ||
| 658 | (message (substitute-command-keys | ||
| 659 | "Packages that are not available: %d (the rest is already \ | ||
| 660 | installed), maybe you need to \\[package-refresh-contents]") | ||
| 661 | difference)) | ||
| 662 | (t | ||
| 663 | (message "All your packages are already installed")))))) | ||
| 664 | |||
| 665 | (defun package--newest-p (pkg) | ||
| 666 | "Return non-nil if PKG is the newest package with its name." | ||
| 667 | (equal (cadr (assq (package-desc-name pkg) package-alist)) | ||
| 668 | pkg)) | ||
| 669 | |||
| 670 | (declare-function comp-el-to-eln-filename "comp.c") | ||
| 671 | (defvar package-vc-repository-store) | ||
| 672 | (defun package--delete-directory (dir) | ||
| 673 | "Delete PKG-DESC directory DIR recursively. | ||
| 674 | Clean-up the corresponding .eln files if Emacs is native | ||
| 675 | compiled." | ||
| 676 | (when (featurep 'native-compile) | ||
| 677 | (cl-loop | ||
| 678 | for file in (directory-files-recursively dir | ||
| 679 | ;; Exclude lockfiles | ||
| 680 | (rx bos (or (and "." (not "#")) (not ".")) (* nonl) ".el" eos)) | ||
| 681 | do (comp-clean-up-stale-eln (comp-el-to-eln-filename file)))) | ||
| 682 | (if (file-symlink-p (directory-file-name dir)) | ||
| 683 | (delete-file (directory-file-name dir)) | ||
| 684 | (delete-directory dir t))) | ||
| 685 | |||
| 686 | (defun package-delete (pkg-desc &optional force nosave) | ||
| 687 | "Delete package PKG-DESC. | ||
| 688 | |||
| 689 | Argument PKG-DESC is the full description of the package, for example as | ||
| 690 | obtained by `package-get-descriptor'. Interactively, prompt the user | ||
| 691 | for the package name and version. | ||
| 692 | |||
| 693 | When package is used elsewhere as dependency of another package, | ||
| 694 | refuse deleting it and return an error. | ||
| 695 | If prefix argument FORCE is non-nil, package will be deleted even | ||
| 696 | if it is used elsewhere. | ||
| 697 | If NOSAVE is non-nil, the package is not removed from | ||
| 698 | `package-selected-packages'." | ||
| 699 | (interactive | ||
| 700 | (progn | ||
| 701 | (let* ((package-table | ||
| 702 | (mapcar | ||
| 703 | (lambda (p) (cons (package-desc-full-name p) p)) | ||
| 704 | (delq nil | ||
| 705 | (mapcar (lambda (p) (unless (package-built-in-p p) p)) | ||
| 706 | (apply #'append (mapcar #'cdr (package--alist))))))) | ||
| 707 | (package-name (completing-read "Delete package: " | ||
| 708 | (mapcar #'car package-table) | ||
| 709 | nil t))) | ||
| 710 | (list (cdr (assoc package-name package-table)) | ||
| 711 | current-prefix-arg nil)))) | ||
| 712 | (let* ((dir (package-desc-dir pkg-desc)) | ||
| 713 | (name (package-desc-name pkg-desc)) | ||
| 714 | (new-package-alist (let ((pkgs (assq name package-alist))) | ||
| 715 | (if (null (remove pkg-desc (cdr pkgs))) | ||
| 716 | (remq pkgs package-alist) | ||
| 717 | package-alist))) | ||
| 718 | pkg-used-elsewhere-by) | ||
| 719 | ;; If the user is trying to delete this package, they definitely | ||
| 720 | ;; don't want it marked as selected, so we remove it from | ||
| 721 | ;; `package-selected-packages' even if it can't be deleted. | ||
| 722 | (when (and (null nosave) | ||
| 723 | (package--user-selected-p name) | ||
| 724 | ;; Don't deselect if this is an older version of an | ||
| 725 | ;; upgraded package. | ||
| 726 | (package--newest-p pkg-desc)) | ||
| 727 | (package--save-selected-packages (remove name package-selected-packages))) | ||
| 728 | (cond ((not (string-prefix-p (file-name-as-directory | ||
| 729 | (expand-file-name package-user-dir)) | ||
| 730 | (expand-file-name dir))) | ||
| 731 | ;; Don't delete "system" packages. | ||
| 732 | (error "Package `%s' is a system package, not deleting" | ||
| 733 | (package-desc-full-name pkg-desc))) | ||
| 734 | ((and (null force) | ||
| 735 | (setq pkg-used-elsewhere-by | ||
| 736 | (let ((package-alist new-package-alist)) | ||
| 737 | (package--used-elsewhere-p pkg-desc)))) ;See bug#65475 | ||
| 738 | ;; Don't delete packages used as dependency elsewhere. | ||
| 739 | (error "Package `%s' is used by `%s' as dependency, not deleting" | ||
| 740 | (package-desc-full-name pkg-desc) | ||
| 741 | (package-desc-name pkg-used-elsewhere-by))) | ||
| 742 | (t | ||
| 743 | (add-hook 'post-command-hook 'package-menu--post-refresh) | ||
| 744 | (package--delete-directory dir) | ||
| 745 | ;; Remove NAME-VERSION.signed and NAME-readme.txt files. | ||
| 746 | ;; | ||
| 747 | ;; NAME-readme.txt files are no longer created, but they | ||
| 748 | ;; may be left around from an earlier install. | ||
| 749 | (dolist (suffix '(".signed" "readme.txt")) | ||
| 750 | (let* ((version (package-version-join (package-desc-version pkg-desc))) | ||
| 751 | (file (concat (if (string= suffix ".signed") | ||
| 752 | dir | ||
| 753 | (substring dir 0 (- (length version)))) | ||
| 754 | suffix))) | ||
| 755 | (when (file-exists-p file) | ||
| 756 | (delete-file file)))) | ||
| 757 | ;; Update package-alist. | ||
| 758 | (setq package-alist new-package-alist) | ||
| 759 | (package--quickstart-maybe-refresh) | ||
| 760 | (message "Package `%s' deleted." | ||
| 761 | (package-desc-full-name pkg-desc)))))) | ||
| 762 | |||
| 763 | ;;;###autoload | ||
| 764 | (defun package-reinstall (pkg) | ||
| 765 | "Reinstall package PKG. | ||
| 766 | PKG should be either a symbol, the package name, or a `package-desc' | ||
| 767 | object." | ||
| 768 | (interactive | ||
| 769 | (progn | ||
| 770 | (package--archives-initialize) | ||
| 771 | (list (intern (completing-read | ||
| 772 | "Reinstall package: " | ||
| 773 | (mapcar #'symbol-name | ||
| 774 | (mapcar #'car package-alist))))))) | ||
| 775 | (package--archives-initialize) | ||
| 776 | (package-delete | ||
| 777 | (if (package-desc-p pkg) pkg (cadr (assq pkg package-alist))) | ||
| 778 | 'force 'nosave) | ||
| 779 | (package-install pkg 'dont-select)) | ||
| 780 | |||
| 781 | ;;;###autoload | ||
| 782 | (defun package-autoremove (&optional noconfirm) | ||
| 783 | "Remove packages that are no longer needed. | ||
| 784 | |||
| 785 | Packages that are no more needed by other packages in | ||
| 786 | `package-selected-packages' and their dependencies | ||
| 787 | will be deleted. | ||
| 788 | |||
| 789 | If optional argument NOCONFIRM is non-nil, or when invoked with a prefix | ||
| 790 | argument, don't ask for confirmation to install packages." | ||
| 791 | (interactive "P") | ||
| 792 | ;; If `package-selected-packages' is nil, it would make no sense to | ||
| 793 | ;; try to populate it here, because then `package-autoremove' will | ||
| 794 | ;; do absolutely nothing. | ||
| 795 | (when (or noconfirm | ||
| 796 | package-selected-packages | ||
| 797 | (yes-or-no-p | ||
| 798 | (format-message | ||
| 799 | "`package-selected-packages' is empty! Really remove ALL packages? "))) | ||
| 800 | (let ((removable (package--removable-packages))) | ||
| 801 | (if removable | ||
| 802 | (when (or noconfirm | ||
| 803 | (y-or-n-p | ||
| 804 | (format "Packages to delete: %d (%s), proceed? " | ||
| 805 | (length removable) | ||
| 806 | (mapconcat #'symbol-name removable " ")))) | ||
| 807 | (mapc (lambda (p) | ||
| 808 | (package-delete (cadr (assq p package-alist)) t)) | ||
| 809 | removable)) | ||
| 810 | (message "Nothing to autoremove"))))) | ||
| 811 | |||
| 812 | |||
| 813 | ;;;; Autoload | ||
| 814 | (declare-function autoload-rubric "autoload" (file &optional type feature)) | ||
| 815 | |||
| 816 | (defun package-autoload-ensure-default-file (file) | ||
| 817 | "Make sure that the autoload file FILE exists and if not create it." | ||
| 818 | (declare (obsolete nil "29.1")) | ||
| 819 | (unless (file-exists-p file) | ||
| 820 | (require 'autoload) | ||
| 821 | (let ((coding-system-for-write 'utf-8-emacs-unix)) | ||
| 822 | (with-suppressed-warnings ((obsolete autoload-rubric)) | ||
| 823 | (write-region (autoload-rubric file "package" nil) | ||
| 824 | nil file nil 'silent)))) | ||
| 825 | file) | ||
| 826 | |||
| 827 | (defvar autoload-timestamps) | ||
| 828 | (defvar version-control) | ||
| 829 | |||
| 830 | (defun package-generate-autoloads (name pkg-dir) | ||
| 831 | "Generate autoloads in PKG-DIR for package named NAME." | ||
| 832 | (let* ((auto-name (format "%s-autoloads.el" name)) | ||
| 833 | ;;(ignore-name (concat name "-pkg.el")) | ||
| 834 | (output-file (expand-file-name auto-name pkg-dir)) | ||
| 835 | ;; We don't need 'em, and this makes the output reproducible. | ||
| 836 | (autoload-timestamps nil) | ||
| 837 | (backup-inhibited t) | ||
| 838 | (version-control 'never)) | ||
| 839 | (loaddefs-generate | ||
| 840 | pkg-dir output-file nil | ||
| 841 | (prin1-to-string | ||
| 842 | '(add-to-list | ||
| 843 | 'load-path | ||
| 844 | ;; Add the directory that will contain the autoload file to | ||
| 845 | ;; the load path. We don't hard-code `pkg-dir', to avoid | ||
| 846 | ;; issues if the package directory is moved around. | ||
| 847 | ;; `loaddefs-generate' has code to do this for us, but it's | ||
| 848 | ;; not currently exposed. (Bug#63625) | ||
| 849 | (or (and load-file-name | ||
| 850 | (directory-file-name | ||
| 851 | (file-name-directory load-file-name))) | ||
| 852 | (car load-path))))) | ||
| 853 | (let ((buf (find-buffer-visiting output-file))) | ||
| 854 | (when buf (kill-buffer buf))) | ||
| 855 | auto-name)) | ||
| 856 | |||
| 857 | (defun package--make-autoloads-and-stuff (pkg-desc pkg-dir) | ||
| 858 | "Generate autoloads, description file, etc., for PKG-DESC installed at PKG-DIR." | ||
| 859 | (package-generate-autoloads (package-desc-name pkg-desc) pkg-dir) | ||
| 860 | (let ((desc-file (expand-file-name (package--description-file pkg-dir) | ||
| 861 | pkg-dir))) | ||
| 862 | (unless (file-exists-p desc-file) | ||
| 863 | (package-generate-description-file pkg-desc desc-file))) | ||
| 864 | ;; FIXME: Create foo.info and dir file from foo.texi? | ||
| 865 | ) | ||
| 866 | |||
| 867 | (defun package-tar-file-info () | ||
| 868 | "Find package information for a tar file. | ||
| 869 | The return result is a `package-desc'." | ||
| 870 | (cl-assert (derived-mode-p 'tar-mode)) | ||
| 871 | (let* ((dir-name (named-let loop | ||
| 872 | ((filename (tar-header-name (car tar-parse-info)))) | ||
| 873 | (let ((dirname (file-name-directory filename))) | ||
| 874 | ;; The first file can be in a subdir: look for the top. | ||
| 875 | (if dirname (loop (directory-file-name dirname)) | ||
| 876 | (file-name-as-directory filename))))) | ||
| 877 | (desc-file (package--description-file dir-name)) | ||
| 878 | (tar-desc (tar-get-file-descriptor (concat dir-name desc-file)))) | ||
| 879 | (unless tar-desc | ||
| 880 | (error "No package descriptor file found")) | ||
| 881 | (with-current-buffer (tar--extract tar-desc) | ||
| 882 | (unwind-protect | ||
| 883 | (or (package--read-pkg-desc 'tar) | ||
| 884 | (error "Can't find define-package in %s" | ||
| 885 | (tar-header-name tar-desc))) | ||
| 886 | (kill-buffer (current-buffer)))))) | ||
| 887 | |||
| 888 | (defun package-untar-buffer (dir) | ||
| 889 | "Untar the current buffer. | ||
| 890 | This uses `tar-untar-buffer' from Tar mode. All files should | ||
| 891 | untar into a directory named DIR; otherwise, signal an error." | ||
| 892 | (tar-mode) | ||
| 893 | ;; Make sure everything extracts into DIR. | ||
| 894 | (let ((regexp (concat "\\`" (regexp-quote (expand-file-name dir)) "/")) | ||
| 895 | (case-fold-search (file-name-case-insensitive-p dir))) | ||
| 896 | (dolist (tar-data tar-parse-info) | ||
| 897 | (let ((name (expand-file-name (tar-header-name tar-data)))) | ||
| 898 | (or (string-match regexp name) | ||
| 899 | ;; Tarballs created by some utilities don't list | ||
| 900 | ;; directories with a trailing slash (Bug#13136). | ||
| 901 | (and (string-equal (expand-file-name dir) name) | ||
| 902 | (eq (tar-header-link-type tar-data) 5)) | ||
| 903 | (error "Package does not untar cleanly into directory %s/" dir))))) | ||
| 904 | (tar-untar-buffer)) | ||
| 905 | |||
| 906 | (declare-function dired-get-marked-files "dired") | ||
| 907 | |||
| 908 | (defun package-unpack (pkg-desc) | ||
| 909 | "Install the contents of the current buffer as a package." | ||
| 910 | (let* ((name (package-desc-name pkg-desc)) | ||
| 911 | (dirname (package-desc-full-name pkg-desc)) | ||
| 912 | (pkg-dir (expand-file-name dirname package-user-dir))) | ||
| 913 | (pcase (package-desc-kind pkg-desc) | ||
| 914 | ('dir | ||
| 915 | (make-directory pkg-dir t) | ||
| 916 | (let ((file-list | ||
| 917 | (or (and (derived-mode-p 'dired-mode) | ||
| 918 | (dired-get-marked-files)) | ||
| 919 | (directory-files-recursively default-directory "" nil)))) | ||
| 920 | (dolist (source-file file-list) | ||
| 921 | (let ((target (expand-file-name | ||
| 922 | (file-relative-name source-file default-directory) | ||
| 923 | pkg-dir))) | ||
| 924 | (make-directory (file-name-directory target) t) | ||
| 925 | (copy-file source-file target t))) | ||
| 926 | ;; Now that the files have been installed, this package is | ||
| 927 | ;; indistinguishable from a `tar' or a `single'. Let's make | ||
| 928 | ;; things simple by ensuring we're one of them. | ||
| 929 | (setf (package-desc-kind pkg-desc) | ||
| 930 | (if (length> file-list 1) 'tar 'single)))) | ||
| 931 | ('tar | ||
| 932 | (make-directory package-user-dir t) | ||
| 933 | (let* ((default-directory (file-name-as-directory package-user-dir))) | ||
| 934 | (package-untar-buffer dirname))) | ||
| 935 | ('single | ||
| 936 | (let ((el-file (expand-file-name (format "%s.el" name) pkg-dir))) | ||
| 937 | (make-directory pkg-dir t) | ||
| 938 | (package--write-file-no-coding el-file))) | ||
| 939 | (kind (error "Unknown package kind: %S" kind))) | ||
| 940 | (package--make-autoloads-and-stuff pkg-desc pkg-dir) | ||
| 941 | ;; Update package-alist. | ||
| 942 | (let ((new-desc (package-load-descriptor pkg-dir))) | ||
| 943 | (unless (equal (package-desc-full-name new-desc) | ||
| 944 | (package-desc-full-name pkg-desc)) | ||
| 945 | (error "The retrieved package (`%s') doesn't match what the archive offered (`%s')" | ||
| 946 | (package-desc-full-name new-desc) (package-desc-full-name pkg-desc))) | ||
| 947 | ;; Activation has to be done before compilation, so that if we're | ||
| 948 | ;; upgrading and macros have changed we load the new definitions | ||
| 949 | ;; before compiling. | ||
| 950 | (when (package-activate-1 new-desc :reload :deps) | ||
| 951 | ;; FIXME: Compilation should be done as a separate, optional, step. | ||
| 952 | ;; E.g. for multi-package installs, we should first install all packages | ||
| 953 | ;; and then compile them. | ||
| 954 | (package--compile new-desc) | ||
| 955 | (when package-native-compile | ||
| 956 | (package--native-compile-async new-desc)) | ||
| 957 | ;; After compilation, load again any files loaded by | ||
| 958 | ;; `activate-1', so that we use the byte-compiled definitions. | ||
| 959 | (package--reload-previously-loaded new-desc))) | ||
| 960 | pkg-dir)) | ||
| 961 | |||
| 962 | (defun package-generate-description-file (pkg-desc pkg-file) | ||
| 963 | "Create the foo-pkg.el file PKG-FILE for single-file package PKG-DESC." | ||
| 964 | (let* ((name (package-desc-name pkg-desc))) | ||
| 965 | (let ((print-level nil) | ||
| 966 | (print-quoted t) | ||
| 967 | (print-length nil)) | ||
| 968 | (write-region | ||
| 969 | (concat | ||
| 970 | ";;; Generated package description from " | ||
| 971 | (replace-regexp-in-string "-pkg\\.el\\'" ".el" | ||
| 972 | (file-name-nondirectory pkg-file)) | ||
| 973 | " -*- no-byte-compile: t -*-\n" | ||
| 974 | (prin1-to-string | ||
| 975 | (nconc | ||
| 976 | (list 'define-package | ||
| 977 | (symbol-name name) | ||
| 978 | (package-version-join (package-desc-version pkg-desc)) | ||
| 979 | (package-desc-summary pkg-desc) | ||
| 980 | (let ((requires (package-desc-reqs pkg-desc))) | ||
| 981 | (list 'quote | ||
| 982 | ;; Turn version lists into string form. | ||
| 983 | (mapcar | ||
| 984 | (lambda (elt) | ||
| 985 | (list (car elt) | ||
| 986 | (package-version-join (cadr elt)))) | ||
| 987 | requires)))) | ||
| 988 | (package--alist-to-plist-args | ||
| 989 | (package-desc-extras pkg-desc)))) | ||
| 990 | "\n") | ||
| 991 | nil pkg-file nil 'silent)))) | ||
| 992 | |||
| 993 | ;;;###autoload | ||
| 994 | (defun package-isolate (packages &optional temp-init) | ||
| 995 | "Start an uncustomized Emacs and only load a set of PACKAGES. | ||
| 996 | Interactively, prompt for PACKAGES to load, which should be specified | ||
| 997 | separated by commas. | ||
| 998 | If called from Lisp, PACKAGES should be a list of packages to load. | ||
| 999 | If TEMP-INIT is non-nil, or when invoked with a prefix argument, | ||
| 1000 | the Emacs user directory is set to a temporary directory. | ||
| 1001 | This command is intended for testing Emacs and/or the packages | ||
| 1002 | in a clean environment." | ||
| 1003 | (interactive | ||
| 1004 | (cl-loop for p in (cl-loop for p in (package--alist) append (cdr p)) | ||
| 1005 | unless (package-built-in-p p) | ||
| 1006 | collect (cons (package-desc-full-name p) p) into table | ||
| 1007 | finally return | ||
| 1008 | (list | ||
| 1009 | (cl-loop for c in | ||
| 1010 | (completing-read-multiple | ||
| 1011 | "Packages to isolate: " table | ||
| 1012 | nil t) | ||
| 1013 | collect (alist-get c table nil nil #'string=)) | ||
| 1014 | current-prefix-arg))) | ||
| 1015 | (let* ((name (concat "package-isolate-" | ||
| 1016 | (mapconcat #'package-desc-full-name packages ","))) | ||
| 1017 | (all-packages (delete-consecutive-dups | ||
| 1018 | (sort (append packages (mapcan #'package--dependencies packages)) | ||
| 1019 | (lambda (p0 p1) | ||
| 1020 | (string< (package-desc-name p0) (package-desc-name p1)))))) | ||
| 1021 | initial-scratch-message package-load-list) | ||
| 1022 | (with-temp-buffer | ||
| 1023 | (insert ";; This is an isolated testing environment, with these packages enabled:\n\n") | ||
| 1024 | (dolist (package all-packages) | ||
| 1025 | (push (list (package-desc-name package) | ||
| 1026 | (package-version-join (package-desc-version package))) | ||
| 1027 | package-load-list) | ||
| 1028 | (insert ";; - " (package-desc-full-name package)) | ||
| 1029 | (unless (memq package packages) | ||
| 1030 | (insert " (dependency)")) | ||
| 1031 | (insert "\n")) | ||
| 1032 | (insert "\n") | ||
| 1033 | (setq initial-scratch-message (buffer-string))) | ||
| 1034 | (apply #'start-process (concat "*" name "*") nil | ||
| 1035 | (list (expand-file-name invocation-name invocation-directory) | ||
| 1036 | "--quick" "--debug-init" | ||
| 1037 | "--init-directory" (if temp-init | ||
| 1038 | (make-temp-file name t) | ||
| 1039 | user-emacs-directory) | ||
| 1040 | (format "--eval=%S" | ||
| 1041 | `(progn | ||
| 1042 | (setq initial-scratch-message ,initial-scratch-message) | ||
| 1043 | |||
| 1044 | (require 'package) | ||
| 1045 | ,@(mapcar | ||
| 1046 | (lambda (dir) | ||
| 1047 | `(add-to-list 'package-directory-list ,dir)) | ||
| 1048 | (cons package-user-dir package-directory-list)) | ||
| 1049 | (setq package-load-list ',package-load-list) | ||
| 1050 | (package-activate-all))))))) | ||
| 1051 | |||
| 1052 | (provide 'package-install) | ||
| 1053 | ;;; package-install.el ends here | ||
diff --git a/lisp/package/package-menu.el b/lisp/package/package-menu.el new file mode 100644 index 00000000000..4be14069999 --- /dev/null +++ b/lisp/package/package-menu.el | |||
| @@ -0,0 +1,1580 @@ | |||
| 1 | ;;; package-compile.el --- Byte-Compilation of Packages -*- lexical-binding: t; -*- | ||
| 2 | |||
| 3 | ;; Copyright (C) 2025 Philip Kaludercic | ||
| 4 | |||
| 5 | ;; Author: Philip Kaludercic <philipk@posteo.net> | ||
| 6 | |||
| 7 | ;; This program is free software; you can redistribute it and/or modify | ||
| 8 | ;; it under the terms of the GNU General Public License as published by | ||
| 9 | ;; the Free Software Foundation, either version 3 of the License, or | ||
| 10 | ;; (at your option) any later version. | ||
| 11 | |||
| 12 | ;; This program is distributed in the hope that it will be useful, | ||
| 13 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 14 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 15 | ;; GNU General Public License for more details. | ||
| 16 | |||
| 17 | ;; You should have received a copy of the GNU General Public License | ||
| 18 | ;; along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
| 19 | |||
| 20 | ;;; Commentary: | ||
| 21 | |||
| 22 | ;;; Code: | ||
| 23 | |||
| 24 | (require 'package-core) | ||
| 25 | (require 'package-install) | ||
| 26 | (require 'package-vc) | ||
| 27 | |||
| 28 | (require 'tabulated-list) | ||
| 29 | (require 'browse-url) | ||
| 30 | (require 'macroexp) | ||
| 31 | (require 'lisp-mnt) | ||
| 32 | |||
| 33 | (defgroup package-menu nil | ||
| 34 | "A interface for package management." | ||
| 35 | :group 'package | ||
| 36 | :version "24.1") | ||
| 37 | |||
| 38 | (defcustom package-menu-async t | ||
| 39 | "If non-nil, package-menu will use async operations when possible. | ||
| 40 | Currently, only the refreshing of archive contents supports | ||
| 41 | asynchronous operations. Package transactions are still done | ||
| 42 | synchronously." | ||
| 43 | :type 'boolean | ||
| 44 | :version "25.1") | ||
| 45 | |||
| 46 | (defcustom package-menu-hide-low-priority 'archive | ||
| 47 | "If non-nil, hide low priority packages from the packages menu. | ||
| 48 | A package is considered low priority if there's another version | ||
| 49 | of it available such that: | ||
| 50 | (a) the archive of the other package is higher priority than | ||
| 51 | this one, as per `package-archive-priorities'; | ||
| 52 | or | ||
| 53 | (b) they both have the same archive priority but the other | ||
| 54 | package has a higher version number. | ||
| 55 | |||
| 56 | This variable has three possible values: | ||
| 57 | nil: no packages are hidden; | ||
| 58 | `archive': only criterion (a) is used; | ||
| 59 | t: both criteria are used. | ||
| 60 | |||
| 61 | This variable has no effect if `package-menu--hide-packages' is | ||
| 62 | nil, so it can be toggled with \\<package-menu-mode-map>\\[package-menu-toggle-hiding]." | ||
| 63 | :type '(choice (const :tag "Don't hide anything" nil) | ||
| 64 | (const :tag "Hide per package-archive-priorities" | ||
| 65 | archive) | ||
| 66 | (const :tag "Hide per archive and version number" t)) | ||
| 67 | :version "25.1") | ||
| 68 | |||
| 69 | (defcustom package-hidden-regexps nil | ||
| 70 | "List of regexps matching the name of packages to hide. | ||
| 71 | If the name of a package matches any of these regexps it is | ||
| 72 | omitted from the package menu. To toggle this, type \\[package-menu-toggle-hiding]. | ||
| 73 | |||
| 74 | Values can be interactively added to this list by typing | ||
| 75 | \\[package-menu-hide-package] on a package." | ||
| 76 | :version "25.1" | ||
| 77 | :type '(repeat (regexp :tag "Hide packages with name matching"))) | ||
| 78 | |||
| 79 | (defcustom package-menu-use-current-if-no-marks t | ||
| 80 | "Whether \\<package-menu-mode-map>\\[package-menu-execute] in package menu operates on current package if none are marked. | ||
| 81 | |||
| 82 | If non-nil, and no packages are marked for installation or | ||
| 83 | deletion, \\<package-menu-mode-map>\\[package-menu-execute] will operate on the current package at point, | ||
| 84 | see `package-menu-execute' for details. | ||
| 85 | The default is t. Set to nil to get back the original behavior | ||
| 86 | of having `package-menu-execute' signal an error when no packages | ||
| 87 | are marked for installation or deletion." | ||
| 88 | :version "29.1" | ||
| 89 | :type 'boolean) | ||
| 90 | |||
| 91 | (defcustom package-name-column-width 30 | ||
| 92 | "Column width for the Package name in the package menu." | ||
| 93 | :type 'natnum | ||
| 94 | :version "28.1") | ||
| 95 | |||
| 96 | (defcustom package-version-column-width 14 | ||
| 97 | "Column width for the Package version in the package menu." | ||
| 98 | :type 'natnum | ||
| 99 | :version "28.1") | ||
| 100 | |||
| 101 | (defcustom package-status-column-width 12 | ||
| 102 | "Column width for the Package status in the package menu." | ||
| 103 | :type 'natnum | ||
| 104 | :version "28.1") | ||
| 105 | |||
| 106 | (defcustom package-archive-column-width 8 | ||
| 107 | "Column width for the Package archive in the package menu." | ||
| 108 | :type 'natnum | ||
| 109 | :version "28.1") | ||
| 110 | |||
| 111 | (defun package-browse-url (desc &optional secondary) | ||
| 112 | "Open the website of the package under point in a browser. | ||
| 113 | `browse-url' is used to determine the browser to be used. If | ||
| 114 | SECONDARY (interactively, the prefix), use the secondary browser. | ||
| 115 | DESC must be a `package-desc' object." | ||
| 116 | (interactive (list (package--query-desc) | ||
| 117 | current-prefix-arg) | ||
| 118 | package-menu-mode) | ||
| 119 | (unless desc | ||
| 120 | (user-error "No package here")) | ||
| 121 | (let ((url (cdr (assoc :url (package-desc-extras desc))))) | ||
| 122 | (unless url | ||
| 123 | (user-error "No website for %s" (package-desc-name desc))) | ||
| 124 | (if secondary | ||
| 125 | (funcall browse-url-secondary-browser-function url) | ||
| 126 | (browse-url url)))) | ||
| 127 | |||
| 128 | (defun package--imenu-prev-index-position-function () | ||
| 129 | "Move point to previous line in package-menu buffer. | ||
| 130 | This function is used as a value for | ||
| 131 | `imenu-prev-index-position-function'." | ||
| 132 | (unless (bobp) | ||
| 133 | (forward-line -1))) | ||
| 134 | |||
| 135 | (defun package--imenu-extract-index-name-function () | ||
| 136 | "Return imenu name for line at point. | ||
| 137 | This function is used as a value for | ||
| 138 | `imenu-extract-index-name-function'. Point should be at the | ||
| 139 | beginning of the line." | ||
| 140 | (let ((package-desc (tabulated-list-get-id))) | ||
| 141 | (format "%s (%s): %s" | ||
| 142 | (package-desc-name package-desc) | ||
| 143 | (package-version-join (package-desc-version package-desc)) | ||
| 144 | (package-desc-summary package-desc)))) | ||
| 145 | |||
| 146 | (defun package-menu--display (remember-pos suffix) | ||
| 147 | "Display the Package Menu. | ||
| 148 | If REMEMBER-POS is non-nil, keep point on the same entry. | ||
| 149 | |||
| 150 | If SUFFIX is non-nil, append that to \"Package\" for the first | ||
| 151 | column in the header line." | ||
| 152 | (setf (car (aref tabulated-list-format 0)) | ||
| 153 | (if suffix | ||
| 154 | (concat "Package[" suffix "]") | ||
| 155 | "Package")) | ||
| 156 | (tabulated-list-init-header) | ||
| 157 | (tabulated-list-print remember-pos)) | ||
| 158 | |||
| 159 | (defun package-menu--generate (remember-pos &optional packages keywords) | ||
| 160 | "Populate and display the Package Menu. | ||
| 161 | If REMEMBER-POS is non-nil, keep point on the same entry. | ||
| 162 | PACKAGES should be t, which means to display all known packages, | ||
| 163 | or a list of package names (symbols) to display. | ||
| 164 | |||
| 165 | With KEYWORDS given, only packages with those keywords are | ||
| 166 | shown." | ||
| 167 | (package-menu--refresh packages keywords) | ||
| 168 | (package-menu--display remember-pos | ||
| 169 | (when keywords | ||
| 170 | (let ((filters (mapconcat #'identity keywords ","))) | ||
| 171 | (concat "Package[" filters "]"))))) | ||
| 172 | |||
| 173 | (defun package-menu--print-info (pkg) | ||
| 174 | "Return a package entry suitable for `tabulated-list-entries'. | ||
| 175 | PKG has the form (PKG-DESC . STATUS). | ||
| 176 | Return (PKG-DESC [NAME VERSION STATUS DOC])." | ||
| 177 | (package-menu--print-info-simple (car pkg))) | ||
| 178 | (make-obsolete 'package-menu--print-info | ||
| 179 | 'package-menu--print-info-simple "25.1") | ||
| 180 | |||
| 181 | |||
| 182 | ;;; Package menu faces | ||
| 183 | |||
| 184 | (defface package-name | ||
| 185 | '((t :inherit link)) | ||
| 186 | "Face used on package names in the package menu." | ||
| 187 | :version "25.1") | ||
| 188 | |||
| 189 | (defface package-description | ||
| 190 | '((t :inherit default)) | ||
| 191 | "Face used on package description summaries in the package menu." | ||
| 192 | :version "25.1") | ||
| 193 | |||
| 194 | ;; Shame this hyphenates "built-in", when "font-lock-builtin-face" doesn't. | ||
| 195 | (defface package-status-built-in | ||
| 196 | '((t :inherit font-lock-builtin-face)) | ||
| 197 | "Face used on the status and version of built-in packages." | ||
| 198 | :version "25.1") | ||
| 199 | |||
| 200 | (defface package-status-external | ||
| 201 | '((t :inherit package-status-built-in)) | ||
| 202 | "Face used on the status and version of external packages." | ||
| 203 | :version "25.1") | ||
| 204 | |||
| 205 | (defface package-status-available | ||
| 206 | '((t :inherit default)) | ||
| 207 | "Face used on the status and version of available packages." | ||
| 208 | :version "25.1") | ||
| 209 | |||
| 210 | (defface package-status-new | ||
| 211 | '((t :inherit (bold package-status-available))) | ||
| 212 | "Face used on the status and version of new packages." | ||
| 213 | :version "25.1") | ||
| 214 | |||
| 215 | (defface package-status-held | ||
| 216 | '((t :inherit font-lock-constant-face)) | ||
| 217 | "Face used on the status and version of held packages." | ||
| 218 | :version "25.1") | ||
| 219 | |||
| 220 | (defface package-status-disabled | ||
| 221 | '((t :inherit font-lock-warning-face)) | ||
| 222 | "Face used on the status and version of disabled packages." | ||
| 223 | :version "25.1") | ||
| 224 | |||
| 225 | (defface package-status-installed | ||
| 226 | '((t :inherit font-lock-comment-face)) | ||
| 227 | "Face used on the status and version of installed packages." | ||
| 228 | :version "25.1") | ||
| 229 | |||
| 230 | (defface package-status-from-source | ||
| 231 | '((t :inherit font-lock-negation-char-face)) | ||
| 232 | "Face used on the status and version of installed packages." | ||
| 233 | :version "29.1") | ||
| 234 | |||
| 235 | (defface package-status-dependency | ||
| 236 | '((t :inherit package-status-installed)) | ||
| 237 | "Face used on the status and version of dependency packages." | ||
| 238 | :version "25.1") | ||
| 239 | |||
| 240 | (defface package-status-unsigned | ||
| 241 | '((t :inherit font-lock-warning-face)) | ||
| 242 | "Face used on the status and version of unsigned packages." | ||
| 243 | :version "25.1") | ||
| 244 | |||
| 245 | (defface package-status-incompat | ||
| 246 | '((t :inherit error)) | ||
| 247 | "Face used on the status and version of incompat packages." | ||
| 248 | :version "25.1") | ||
| 249 | |||
| 250 | (defface package-status-avail-obso | ||
| 251 | '((t :inherit package-status-incompat)) | ||
| 252 | "Face used on the status and version of avail-obso packages." | ||
| 253 | :version "25.1") | ||
| 254 | |||
| 255 | (defface package-mark-install-line | ||
| 256 | '((((class color) (background light)) | ||
| 257 | :background "darkolivegreen1" :extend t) | ||
| 258 | (((class color) (background dark)) | ||
| 259 | :background "seagreen" :extend t) | ||
| 260 | (t :inherit (highlight) :extend t)) | ||
| 261 | "Face used for highlighting in package-menu packages marked to be installed." | ||
| 262 | :version "31.1") | ||
| 263 | |||
| 264 | (defface package-mark-delete-line | ||
| 265 | '((((class color) (background light)) | ||
| 266 | :background "rosybrown1" :extend t) | ||
| 267 | (((class color) (background dark)) | ||
| 268 | :background "indianred4" :extend t) | ||
| 269 | (t :inherit (highlight) :extend t)) | ||
| 270 | "Face used for highlighting in package-menu packages marked to be deleted." | ||
| 271 | :version "31.1") | ||
| 272 | |||
| 273 | (defface package-mode-line-total nil | ||
| 274 | "Face for the total number of packages displayed on the mode line." | ||
| 275 | :version "31.1") | ||
| 276 | |||
| 277 | (defface package-mode-line-installed '((t :inherit package-status-installed)) | ||
| 278 | "Face for the number of installed packages displayed on the mode line." | ||
| 279 | :version "31.1") | ||
| 280 | |||
| 281 | (defface package-mode-line-to-upgrade '((t :inherit bold)) | ||
| 282 | "Face for the number of packages to upgrade displayed on the mode line." | ||
| 283 | :version "31.1") | ||
| 284 | |||
| 285 | (defface package-mode-line-new '((t :inherit package-status-new)) | ||
| 286 | "Face for the number of new packages displayed on the mode line." | ||
| 287 | :version "31.1") | ||
| 288 | |||
| 289 | ;;; Package menu printing | ||
| 290 | |||
| 291 | (defun package-menu--print-info-simple (pkg) | ||
| 292 | "Return a package entry suitable for `tabulated-list-entries'. | ||
| 293 | PKG is a `package-desc' object. | ||
| 294 | Return (PKG-DESC [NAME VERSION STATUS DOC])." | ||
| 295 | (let* ((status (package-desc-status pkg)) | ||
| 296 | (face (pcase status | ||
| 297 | ("built-in" 'package-status-built-in) | ||
| 298 | ("external" 'package-status-external) | ||
| 299 | ("available" 'package-status-available) | ||
| 300 | ("avail-obso" 'package-status-avail-obso) | ||
| 301 | ("new" 'package-status-new) | ||
| 302 | ("held" 'package-status-held) | ||
| 303 | ("disabled" 'package-status-disabled) | ||
| 304 | ("installed" 'package-status-installed) | ||
| 305 | ("source" 'package-status-from-source) | ||
| 306 | ("dependency" 'package-status-dependency) | ||
| 307 | ("unsigned" 'package-status-unsigned) | ||
| 308 | ("incompat" 'package-status-incompat) | ||
| 309 | (_ 'font-lock-warning-face)))) ; obsolete. | ||
| 310 | (list pkg | ||
| 311 | `[(,(symbol-name (package-desc-name pkg)) | ||
| 312 | face package-name | ||
| 313 | font-lock-face package-name | ||
| 314 | follow-link t | ||
| 315 | package-desc ,pkg | ||
| 316 | action package-menu-describe-package) | ||
| 317 | ,(propertize | ||
| 318 | (if (package-vc-p pkg) | ||
| 319 | (package-vc-commit pkg) | ||
| 320 | (package-version-join | ||
| 321 | (package-desc-version pkg))) | ||
| 322 | 'font-lock-face face) | ||
| 323 | ,(propertize status 'font-lock-face face) | ||
| 324 | ,(propertize (or (package-desc-archive pkg) "") | ||
| 325 | 'font-lock-face face) | ||
| 326 | ,(propertize (package-desc-summary pkg) | ||
| 327 | 'font-lock-face 'package-description)]))) | ||
| 328 | |||
| 329 | (defvar package-menu--old-archive-contents nil | ||
| 330 | "`package-archive-contents' before the latest refresh.") | ||
| 331 | |||
| 332 | (defun package--ensure-package-menu-mode () | ||
| 333 | "Signal a user-error if major mode is not `package-menu-mode'." | ||
| 334 | (unless (derived-mode-p 'package-menu-mode) | ||
| 335 | (user-error "The current buffer is not a Package Menu"))) | ||
| 336 | |||
| 337 | (defvar package-menu--new-package-list nil | ||
| 338 | "List of newly-available packages since `list-packages' was last called.") | ||
| 339 | |||
| 340 | (defun package-menu--refresh-contents (&optional _arg _noconfirm) | ||
| 341 | "In Package Menu, download the Emacs Lisp package archive. | ||
| 342 | Fetch the contents of each archive specified in | ||
| 343 | `package-archives', and then refresh the package menu. | ||
| 344 | |||
| 345 | `package-menu-mode' sets `revert-buffer-function' to this | ||
| 346 | function. The args ARG and NOCONFIRM, passed from | ||
| 347 | `revert-buffer', are ignored." | ||
| 348 | (package--ensure-package-menu-mode) | ||
| 349 | (setq package-menu--old-archive-contents package-archive-contents) | ||
| 350 | (setq package-menu--new-package-list nil) | ||
| 351 | (package-refresh-contents package-menu-async)) | ||
| 352 | (define-obsolete-function-alias 'package-menu-refresh 'revert-buffer "27.1") | ||
| 353 | |||
| 354 | (defun package-menu--overlay-line (face) | ||
| 355 | "Highlight whole line with face FACE." | ||
| 356 | (let ((ov (make-overlay (line-beginning-position) | ||
| 357 | (1+ (line-end-position))))) | ||
| 358 | (overlay-put ov 'pkg-menu-ov t) | ||
| 359 | (overlay-put ov 'evaporate t) | ||
| 360 | (overlay-put ov 'face face))) | ||
| 361 | |||
| 362 | (defun package-menu--remove-overlay () | ||
| 363 | "Remove all overlays done by `package-menu--overlay-line' in current line." | ||
| 364 | (remove-overlays (line-beginning-position) | ||
| 365 | (1+ (line-end-position)) | ||
| 366 | 'pkg-menu-ov t)) | ||
| 367 | |||
| 368 | (defun package-menu-hide-package () | ||
| 369 | "Hide in Package Menu packages that match a regexp. | ||
| 370 | Prompt for the regexp to match against package names. | ||
| 371 | The default regexp will hide only the package whose name is at point. | ||
| 372 | |||
| 373 | The regexp is added to the list in the user option | ||
| 374 | `package-hidden-regexps' and saved for future sessions. | ||
| 375 | |||
| 376 | To unhide a package, type | ||
| 377 | `\\[customize-variable] RET package-hidden-regexps', and then modify | ||
| 378 | the regexp such that it no longer matches the package's name. | ||
| 379 | |||
| 380 | Type \\[package-menu-toggle-hiding] to toggle package hiding." | ||
| 381 | (declare (interactive-only "change `package-hidden-regexps' instead.")) | ||
| 382 | (interactive nil package-menu-mode) | ||
| 383 | (package--ensure-package-menu-mode) | ||
| 384 | (let* ((name (when (derived-mode-p 'package-menu-mode) | ||
| 385 | (concat "\\`" (regexp-quote (symbol-name (package-desc-name | ||
| 386 | (tabulated-list-get-id)))) | ||
| 387 | "\\'"))) | ||
| 388 | (re (read-string "Hide packages matching regexp: " name))) | ||
| 389 | ;; Test if it is valid. | ||
| 390 | (string-match re "") | ||
| 391 | (push re package-hidden-regexps) | ||
| 392 | (customize-save-variable 'package-hidden-regexps package-hidden-regexps) | ||
| 393 | (package-menu--post-refresh) | ||
| 394 | (let ((hidden | ||
| 395 | (cl-remove-if-not (lambda (e) (string-match re (symbol-name (car e)))) | ||
| 396 | package-archive-contents))) | ||
| 397 | (message "Packages to hide: %d. Type `%s' to toggle or `%s' to customize" | ||
| 398 | (length hidden) | ||
| 399 | (substitute-command-keys "\\[package-menu-toggle-hiding]") | ||
| 400 | (substitute-command-keys "\\[customize-variable] RET package-hidden-regexps"))))) | ||
| 401 | |||
| 402 | |||
| 403 | (defun package-menu-describe-package (&optional button) | ||
| 404 | "Describe the current package. | ||
| 405 | The current package is the package at point. | ||
| 406 | If optional arg BUTTON is non-nil, describe its associated | ||
| 407 | package(s); this is always nil in interactive invocations." | ||
| 408 | (interactive nil package-menu-mode) | ||
| 409 | (let ((pkg-desc (if button (button-get button 'package-desc) | ||
| 410 | (tabulated-list-get-id)))) | ||
| 411 | (if pkg-desc | ||
| 412 | (describe-package pkg-desc) | ||
| 413 | (user-error "No package here")))) | ||
| 414 | |||
| 415 | ;; fixme numeric argument | ||
| 416 | (defun package-menu-mark-delete (&optional _num) | ||
| 417 | "Mark the current package for deletion and move to the next line. | ||
| 418 | The current package is the package at point." | ||
| 419 | (interactive "p" package-menu-mode) | ||
| 420 | (package--ensure-package-menu-mode) | ||
| 421 | (if (member (package-menu-get-status) | ||
| 422 | '("installed" "source" "dependency" "obsolete" "unsigned")) | ||
| 423 | (progn (package-menu--overlay-line 'package-mark-delete-line) | ||
| 424 | (tabulated-list-put-tag "D" t)) | ||
| 425 | (forward-line))) | ||
| 426 | |||
| 427 | (defun package-menu-mark-install (&optional _num) | ||
| 428 | "Mark the current package for installation and move to the next line. | ||
| 429 | The current package is the package at point." | ||
| 430 | (interactive "p" package-menu-mode) | ||
| 431 | (package--ensure-package-menu-mode) | ||
| 432 | (if (member (package-menu-get-status) '("available" "avail-obso" "new" "dependency")) | ||
| 433 | (progn (package-menu--overlay-line 'package-mark-install-line) | ||
| 434 | (tabulated-list-put-tag "I" t)) | ||
| 435 | (forward-line))) | ||
| 436 | |||
| 437 | (defun package-menu-mark-unmark (&optional _num) | ||
| 438 | "Clear any marks on the current package and move to the next line. | ||
| 439 | The current package is the package at point." | ||
| 440 | (interactive "p" package-menu-mode) | ||
| 441 | (package--ensure-package-menu-mode) | ||
| 442 | (package-menu--remove-overlay) | ||
| 443 | (tabulated-list-put-tag " " t)) | ||
| 444 | |||
| 445 | (defun package-menu-backup-unmark () | ||
| 446 | "Back up one line and clear any marks on that line's package." | ||
| 447 | (interactive nil package-menu-mode) | ||
| 448 | (package--ensure-package-menu-mode) | ||
| 449 | (forward-line -1) | ||
| 450 | (package-menu--remove-overlay) | ||
| 451 | (tabulated-list-put-tag " ")) | ||
| 452 | |||
| 453 | (defun package-menu-mark-obsolete-for-deletion () | ||
| 454 | "Mark all obsolete packages for deletion." | ||
| 455 | (interactive nil package-menu-mode) | ||
| 456 | (package--ensure-package-menu-mode) | ||
| 457 | (save-excursion | ||
| 458 | (goto-char (point-min)) | ||
| 459 | (while (not (eobp)) | ||
| 460 | (if (equal (package-menu-get-status) "obsolete") | ||
| 461 | (progn (package-menu--overlay-line 'package-mark-delete-line) | ||
| 462 | (tabulated-list-put-tag "D" t)) | ||
| 463 | (forward-line 1))))) | ||
| 464 | |||
| 465 | (defvar package--quick-help-keys | ||
| 466 | '((("mark for installation," . 9) | ||
| 467 | ("mark for deletion," . 9) "unmark," ("execute marked actions" . 1)) | ||
| 468 | ("next," "previous") | ||
| 469 | ("Hide-package," "(-toggle-hidden") | ||
| 470 | ("g-refresh-contents," "/-filter," "help"))) | ||
| 471 | |||
| 472 | (defun package--prettify-quick-help-key (desc) | ||
| 473 | "Prettify DESC to be displayed as a help menu." | ||
| 474 | (if (listp desc) | ||
| 475 | (if (listp (cdr desc)) | ||
| 476 | (mapconcat #'package--prettify-quick-help-key desc " ") | ||
| 477 | (let ((place (cdr desc)) | ||
| 478 | (out (copy-sequence (car desc)))) | ||
| 479 | (add-text-properties place (1+ place) | ||
| 480 | '(face help-key-binding) | ||
| 481 | out) | ||
| 482 | out)) | ||
| 483 | (package--prettify-quick-help-key (cons desc 0)))) | ||
| 484 | |||
| 485 | (defun package-menu-quick-help () | ||
| 486 | "Show short help for key bindings in `package-menu-mode'. | ||
| 487 | You can view the full list of keys with \\[describe-mode]." | ||
| 488 | (interactive nil package-menu-mode) | ||
| 489 | (package--ensure-package-menu-mode) | ||
| 490 | (message (mapconcat #'package--prettify-quick-help-key | ||
| 491 | package--quick-help-keys "\n"))) | ||
| 492 | |||
| 493 | (defun package-menu-get-status () | ||
| 494 | "Return status description of package at point in Package Menu." | ||
| 495 | (package--ensure-package-menu-mode) | ||
| 496 | (let* ((id (tabulated-list-get-id)) | ||
| 497 | (entry (and id (assoc id tabulated-list-entries)))) | ||
| 498 | (if entry | ||
| 499 | (aref (cadr entry) 2) | ||
| 500 | ""))) | ||
| 501 | |||
| 502 | (defun package-menu--find-upgrades () | ||
| 503 | "In Package Menu, return an alist of packages that can be upgraded. | ||
| 504 | The alist has the same form as `package-alist', namely a list | ||
| 505 | of elements of the form (PKG . DESCS), but where DESCS is the `package-desc' | ||
| 506 | object corresponding to the newer version." | ||
| 507 | (let (installed available upgrades) | ||
| 508 | ;; Build list of installed/available packages in this buffer. | ||
| 509 | (dolist (entry tabulated-list-entries) | ||
| 510 | ;; ENTRY is (PKG-DESC [NAME VERSION STATUS DOC]) | ||
| 511 | (let ((pkg-desc (car entry)) | ||
| 512 | (status (aref (cadr entry) 2))) | ||
| 513 | (cond ((member status '("installed" "dependency" "unsigned" "external" "built-in")) | ||
| 514 | (push pkg-desc installed)) | ||
| 515 | ((member status '("available" "new")) | ||
| 516 | (setq available (package--append-to-alist pkg-desc available)))))) | ||
| 517 | ;; Loop through list of installed packages, finding upgrades. | ||
| 518 | (dolist (pkg-desc installed) | ||
| 519 | (let* ((name (package-desc-name pkg-desc)) | ||
| 520 | (avail-pkg (cadr (assq name available)))) | ||
| 521 | (and avail-pkg | ||
| 522 | (version-list-< (package-desc-priority-version pkg-desc) | ||
| 523 | (package-desc-priority-version avail-pkg)) | ||
| 524 | (or (not (package--active-built-in-p pkg-desc)) | ||
| 525 | package-install-upgrade-built-in) | ||
| 526 | (push (cons name avail-pkg) upgrades)))) | ||
| 527 | upgrades)) | ||
| 528 | |||
| 529 | (defvar package-menu--mark-upgrades-pending nil | ||
| 530 | "Whether mark-upgrades is waiting for a refresh to finish.") | ||
| 531 | |||
| 532 | (defun package-menu--mark-upgrades-1 () | ||
| 533 | "Mark all upgradable packages in the Package Menu. | ||
| 534 | Implementation of `package-menu-mark-upgrades'." | ||
| 535 | (setq package-menu--mark-upgrades-pending nil) | ||
| 536 | (let ((upgrades (package-menu--find-upgrades))) | ||
| 537 | (if (null upgrades) | ||
| 538 | (message "No packages to upgrade") | ||
| 539 | (widen) | ||
| 540 | (save-excursion | ||
| 541 | (goto-char (point-min)) | ||
| 542 | (while (not (eobp)) | ||
| 543 | (let* ((pkg-desc (tabulated-list-get-id)) | ||
| 544 | (upgrade (cdr (assq (package-desc-name pkg-desc) upgrades)))) | ||
| 545 | (cond ((null upgrade) | ||
| 546 | (forward-line 1)) | ||
| 547 | ((equal pkg-desc upgrade) | ||
| 548 | (package-menu-mark-install)) | ||
| 549 | (t | ||
| 550 | (package-menu-mark-delete)))))) | ||
| 551 | (message "Packages marked for upgrading: %d" | ||
| 552 | (length upgrades))))) | ||
| 553 | |||
| 554 | |||
| 555 | (defun package-menu-mark-upgrades () | ||
| 556 | "Mark all upgradable packages in the Package Menu. | ||
| 557 | For each installed package for which a newer version is available, | ||
| 558 | place an (I)nstall flag on the available version and a (D)elete flag | ||
| 559 | on the installed version. A subsequent \\[package-menu-execute] command will upgrade | ||
| 560 | the marked packages. | ||
| 561 | |||
| 562 | If there's an async refresh operation in progress, the flags will | ||
| 563 | be placed as part of `package-menu--post-refresh' instead of | ||
| 564 | immediately." | ||
| 565 | (interactive nil package-menu-mode) | ||
| 566 | (package--ensure-package-menu-mode) | ||
| 567 | (if (not package--downloads-in-progress) | ||
| 568 | (package-menu--mark-upgrades-1) | ||
| 569 | (setq package-menu--mark-upgrades-pending t) | ||
| 570 | (message "Waiting for refresh to finish..."))) | ||
| 571 | |||
| 572 | (defun package-menu--list-to-prompt (packages &optional include-dependencies) | ||
| 573 | "Return a string listing PACKAGES that's usable in a prompt. | ||
| 574 | PACKAGES is a list of `package-desc' objects. | ||
| 575 | Formats the returned string to be usable in a minibuffer | ||
| 576 | prompt (see `package-menu--prompt-transaction-p'). | ||
| 577 | |||
| 578 | If INCLUDE-DEPENDENCIES, also include the number of uninstalled | ||
| 579 | dependencies." | ||
| 580 | ;; The case where `package' is empty is handled in | ||
| 581 | ;; `package-menu--prompt-transaction-p' below. | ||
| 582 | (format "%d (%s)%s" | ||
| 583 | (length packages) | ||
| 584 | (mapconcat #'package-desc-full-name packages " ") | ||
| 585 | (let ((deps | ||
| 586 | (seq-remove | ||
| 587 | #'package-installed-p | ||
| 588 | (delete-dups | ||
| 589 | (apply | ||
| 590 | #'nconc | ||
| 591 | (mapcar (lambda (package) | ||
| 592 | (package--dependencies | ||
| 593 | (package-desc-name package))) | ||
| 594 | packages)))))) | ||
| 595 | (if (and include-dependencies deps) | ||
| 596 | (if (length= deps 1) | ||
| 597 | (format " plus 1 dependency") | ||
| 598 | (format " plus %d dependencies" (length deps))) | ||
| 599 | "")))) | ||
| 600 | |||
| 601 | (defun package-menu--prompt-transaction-p (delete install upgrade) | ||
| 602 | "Prompt the user about DELETE, INSTALL, and UPGRADE. | ||
| 603 | DELETE, INSTALL, and UPGRADE are lists of `package-desc' objects. | ||
| 604 | Either may be nil, but not all." | ||
| 605 | (y-or-n-p | ||
| 606 | (concat | ||
| 607 | (when delete | ||
| 608 | (format "Packages to delete: %s. " | ||
| 609 | (package-menu--list-to-prompt delete))) | ||
| 610 | (when install | ||
| 611 | (format "Packages to install: %s. " | ||
| 612 | (package-menu--list-to-prompt install t))) | ||
| 613 | (when upgrade | ||
| 614 | (format "Packages to upgrade: %s. " | ||
| 615 | (package-menu--list-to-prompt upgrade))) | ||
| 616 | "Proceed? "))) | ||
| 617 | |||
| 618 | |||
| 619 | (defun package-menu--partition-transaction (install delete) | ||
| 620 | "Return an alist describing an INSTALL DELETE transaction. | ||
| 621 | Alist contains three entries, upgrade, delete, and install, each | ||
| 622 | with a list of package names. | ||
| 623 | |||
| 624 | The upgrade entry contains any `package-desc' objects in INSTALL | ||
| 625 | whose name coincides with an object in DELETE. The delete and | ||
| 626 | the install entries are the same as DELETE and INSTALL with such | ||
| 627 | objects removed." | ||
| 628 | (let* ((upg (cl-intersection install delete :key #'package-desc-name)) | ||
| 629 | (ins (cl-set-difference install upg :key #'package-desc-name)) | ||
| 630 | (del (cl-set-difference delete upg :key #'package-desc-name))) | ||
| 631 | `((delete . ,del) (install . ,ins) (upgrade . ,upg)))) | ||
| 632 | |||
| 633 | (defvar package-menu--transaction-status nil | ||
| 634 | "Mode-line status of ongoing package transaction.") | ||
| 635 | |||
| 636 | (defun package-menu--perform-transaction (install-list delete-list) | ||
| 637 | "Install packages in INSTALL-LIST and delete DELETE-LIST. | ||
| 638 | Return nil if there were no errors; non-nil otherwise." | ||
| 639 | (let ((errors nil)) | ||
| 640 | (if install-list | ||
| 641 | (let ((status-format (format ":Installing %%d/%d" | ||
| 642 | (length install-list))) | ||
| 643 | (i 0) | ||
| 644 | (package-menu--transaction-status)) | ||
| 645 | (dolist (pkg install-list) | ||
| 646 | (setq package-menu--transaction-status | ||
| 647 | (format status-format (incf i))) | ||
| 648 | (force-mode-line-update) | ||
| 649 | (redisplay 'force) | ||
| 650 | ;; Don't mark as selected, `package-menu-execute' already | ||
| 651 | ;; does that. | ||
| 652 | (package-install pkg 'dont-select)))) | ||
| 653 | (let ((package-menu--transaction-status ":Deleting")) | ||
| 654 | (force-mode-line-update) | ||
| 655 | (redisplay 'force) | ||
| 656 | (dolist (elt (package--sort-by-dependence delete-list)) | ||
| 657 | (condition-case-unless-debug err | ||
| 658 | (let ((inhibit-message (or inhibit-message package-menu-async))) | ||
| 659 | (package-delete elt nil 'nosave)) | ||
| 660 | (error | ||
| 661 | (push (package-desc-full-name elt) errors) | ||
| 662 | (message "Error trying to delete `%s': %s" | ||
| 663 | (package-desc-full-name elt) | ||
| 664 | (error-message-string err)))))) | ||
| 665 | errors)) | ||
| 666 | |||
| 667 | (defun package--update-selected-packages (add remove) | ||
| 668 | "Update the `package-selected-packages' list according to ADD and REMOVE. | ||
| 669 | ADD and REMOVE must be disjoint lists of package names (or | ||
| 670 | `package-desc' objects) to be added and removed to the selected | ||
| 671 | packages list, respectively." | ||
| 672 | (dolist (p add) | ||
| 673 | (cl-pushnew (if (package-desc-p p) (package-desc-name p) p) | ||
| 674 | package-selected-packages)) | ||
| 675 | (dolist (p remove) | ||
| 676 | (setq package-selected-packages | ||
| 677 | (remove (if (package-desc-p p) (package-desc-name p) p) | ||
| 678 | package-selected-packages))) | ||
| 679 | (when (or add remove) | ||
| 680 | (package--save-selected-packages package-selected-packages))) | ||
| 681 | |||
| 682 | (defun package-menu-execute (&optional noquery) | ||
| 683 | "Perform Package Menu actions on marked packages. | ||
| 684 | Packages marked for installation are downloaded and installed, | ||
| 685 | packages marked for deletion are removed, and packages marked for | ||
| 686 | upgrading are downloaded and upgraded. | ||
| 687 | |||
| 688 | If no packages are marked, the action taken depends on the state | ||
| 689 | of the current package, the one at point. If it's not already | ||
| 690 | installed, this command will install the package; if it's installed, | ||
| 691 | the command will delete the package. | ||
| 692 | |||
| 693 | Optional argument NOQUERY non-nil means do not ask the user to | ||
| 694 | confirm the installations/deletions; this is always nil in interactive | ||
| 695 | invocations." | ||
| 696 | (interactive nil package-menu-mode) | ||
| 697 | (package--ensure-package-menu-mode) | ||
| 698 | (let (install-list delete-list cmd pkg-desc) | ||
| 699 | (save-excursion | ||
| 700 | (goto-char (point-min)) | ||
| 701 | (while (not (eobp)) | ||
| 702 | (setq cmd (char-after)) | ||
| 703 | (unless (eq cmd ?\s) | ||
| 704 | ;; This is the key PKG-DESC. | ||
| 705 | (setq pkg-desc (tabulated-list-get-id)) | ||
| 706 | (cond ((eq cmd ?D) | ||
| 707 | (push pkg-desc delete-list)) | ||
| 708 | ((eq cmd ?I) | ||
| 709 | (push pkg-desc install-list)))) | ||
| 710 | (forward-line))) | ||
| 711 | ;; Nothing marked. | ||
| 712 | (unless (or delete-list install-list) | ||
| 713 | ;; Not on a package line. | ||
| 714 | (unless (and (tabulated-list-get-id) | ||
| 715 | package-menu-use-current-if-no-marks) | ||
| 716 | (user-error "No operations specified")) | ||
| 717 | (let* ((id (tabulated-list-get-id)) | ||
| 718 | (status (package-menu-get-status))) | ||
| 719 | (cond | ||
| 720 | ((member status '("installed")) | ||
| 721 | (push id delete-list)) | ||
| 722 | ((member status '("available" "avail-obso" "new" "dependency")) | ||
| 723 | (push id install-list)) | ||
| 724 | (t (user-error "No default action available for status: %s" | ||
| 725 | status))))) | ||
| 726 | (let-alist (package-menu--partition-transaction install-list delete-list) | ||
| 727 | (when (or noquery | ||
| 728 | (package-menu--prompt-transaction-p .delete .install .upgrade)) | ||
| 729 | (let ((message-template | ||
| 730 | (concat "[ " | ||
| 731 | (when .delete | ||
| 732 | (format "Delete %d " (length .delete))) | ||
| 733 | (when .install | ||
| 734 | (format "Install %d " (length .install))) | ||
| 735 | (when .upgrade | ||
| 736 | (format "Upgrade %d " (length .upgrade))) | ||
| 737 | "]"))) | ||
| 738 | (message "Operation %s started" message-template) | ||
| 739 | ;; Packages being upgraded are not marked as selected. | ||
| 740 | (package--update-selected-packages .install .delete) | ||
| 741 | (unless (package-menu--perform-transaction install-list delete-list) | ||
| 742 | ;; If there weren't errors, output data. | ||
| 743 | (if-let* ((removable (package--removable-packages))) | ||
| 744 | (message "Operation finished. Packages that are no longer needed: %d. Type `%s' to remove them" | ||
| 745 | (length removable) | ||
| 746 | (substitute-command-keys "\\[package-autoremove]")) | ||
| 747 | (message "Operation %s finished" message-template)))))))) | ||
| 748 | |||
| 749 | (defun package-menu--version-predicate (A B) | ||
| 750 | "Predicate to sort \"*Packages*\" buffer by the version column. | ||
| 751 | This is used for `tabulated-list-format' in `package-menu-mode'." | ||
| 752 | (let ((vA (or (ignore-error error (version-to-list (aref (cadr A) 1))) '(0))) | ||
| 753 | (vB (or (ignore-error error (version-to-list (aref (cadr B) 1))) '(0)))) | ||
| 754 | (if (version-list-= vA vB) | ||
| 755 | (package-menu--name-predicate A B) | ||
| 756 | (version-list-< vA vB)))) | ||
| 757 | |||
| 758 | (defun package-menu--status-predicate (A B) | ||
| 759 | "Predicate to sort \"*Packages*\" buffer by the status column. | ||
| 760 | This is used for `tabulated-list-format' in `package-menu-mode'." | ||
| 761 | (let ((sA (aref (cadr A) 2)) | ||
| 762 | (sB (aref (cadr B) 2))) | ||
| 763 | (cond ((string= sA sB) | ||
| 764 | (package-menu--name-predicate A B)) | ||
| 765 | ((string= sA "new") t) | ||
| 766 | ((string= sB "new") nil) | ||
| 767 | ((string-prefix-p "avail" sA) | ||
| 768 | (if (string-prefix-p "avail" sB) | ||
| 769 | (package-menu--name-predicate A B) | ||
| 770 | t)) | ||
| 771 | ((string-prefix-p "avail" sB) nil) | ||
| 772 | ((string= sA "installed") t) | ||
| 773 | ((string= sB "installed") nil) | ||
| 774 | ((string= sA "dependency") t) | ||
| 775 | ((string= sB "dependency") nil) | ||
| 776 | ((string= sA "source") t) | ||
| 777 | ((string= sB "source") nil) | ||
| 778 | ((string= sA "unsigned") t) | ||
| 779 | ((string= sB "unsigned") nil) | ||
| 780 | ((string= sA "held") t) | ||
| 781 | ((string= sB "held") nil) | ||
| 782 | ((string= sA "external") t) | ||
| 783 | ((string= sB "external") nil) | ||
| 784 | ((string= sA "built-in") t) | ||
| 785 | ((string= sB "built-in") nil) | ||
| 786 | ((string= sA "obsolete") t) | ||
| 787 | ((string= sB "obsolete") nil) | ||
| 788 | ((string= sA "incompat") t) | ||
| 789 | ((string= sB "incompat") nil) | ||
| 790 | (t (string< sA sB))))) | ||
| 791 | |||
| 792 | (defun package-menu--description-predicate (A B) | ||
| 793 | "Predicate to sort \"*Packages*\" buffer by the description column. | ||
| 794 | This is used for `tabulated-list-format' in `package-menu-mode'." | ||
| 795 | (let ((dA (aref (cadr A) (if (cdr package-archives) 4 3))) | ||
| 796 | (dB (aref (cadr B) (if (cdr package-archives) 4 3)))) | ||
| 797 | (if (string= dA dB) | ||
| 798 | (package-menu--name-predicate A B) | ||
| 799 | (string< dA dB)))) | ||
| 800 | |||
| 801 | (defun package-menu--name-predicate (A B) | ||
| 802 | "Predicate to sort \"*Packages*\" buffer by the name column. | ||
| 803 | This is used for `tabulated-list-format' in `package-menu-mode'." | ||
| 804 | (string< (symbol-name (package-desc-name (car A))) | ||
| 805 | (symbol-name (package-desc-name (car B))))) | ||
| 806 | |||
| 807 | (defun package-menu--archive-predicate (A B) | ||
| 808 | "Predicate to sort \"*Packages*\" buffer by the archive column. | ||
| 809 | This is used for `tabulated-list-format' in `package-menu-mode'." | ||
| 810 | (let ((a (or (package-desc-archive (car A)) "")) | ||
| 811 | (b (or (package-desc-archive (car B)) ""))) | ||
| 812 | (if (string= a b) | ||
| 813 | (package-menu--name-predicate A B) | ||
| 814 | (string< a b)))) | ||
| 815 | |||
| 816 | (defun package-menu--populate-new-package-list () | ||
| 817 | "Decide which packages are new in `package-archive-contents'. | ||
| 818 | Store this list in `package-menu--new-package-list'." | ||
| 819 | ;; Find which packages are new. | ||
| 820 | (when package-menu--old-archive-contents | ||
| 821 | (dolist (elt package-archive-contents) | ||
| 822 | (unless (assq (car elt) package-menu--old-archive-contents) | ||
| 823 | (push (car elt) package-menu--new-package-list))) | ||
| 824 | (setq package-menu--old-archive-contents nil))) | ||
| 825 | |||
| 826 | (defun package-menu--find-and-notify-upgrades () | ||
| 827 | "Notify the user of upgradable packages." | ||
| 828 | (when-let* ((upgrades (package-menu--find-upgrades))) | ||
| 829 | (message "Packages that can be upgraded: %d; type `%s' to mark for upgrading." | ||
| 830 | (length upgrades) | ||
| 831 | (substitute-command-keys "\\[package-menu-mark-upgrades]")))) | ||
| 832 | |||
| 833 | |||
| 834 | (defun package-menu--post-refresh () | ||
| 835 | "Revert \"*Packages*\" buffer and check for new packages and upgrades. | ||
| 836 | Do nothing if there's no *Packages* buffer. | ||
| 837 | |||
| 838 | This function is called after `package-refresh-contents' and it | ||
| 839 | is added to `post-command-hook' by any function which alters the | ||
| 840 | package database (`package-install' and `package-delete'). When | ||
| 841 | run, it removes itself from `post-command-hook'." | ||
| 842 | (remove-hook 'post-command-hook #'package-menu--post-refresh) | ||
| 843 | (let ((buf (get-buffer "*Packages*"))) | ||
| 844 | (when (buffer-live-p buf) | ||
| 845 | (with-current-buffer buf | ||
| 846 | (package-menu--populate-new-package-list) | ||
| 847 | (run-hooks 'tabulated-list-revert-hook) | ||
| 848 | (tabulated-list-print 'remember 'update))))) | ||
| 849 | |||
| 850 | (defun package-menu--mark-or-notify-upgrades () | ||
| 851 | "If there's a *Packages* buffer, check for upgrades and possibly mark them. | ||
| 852 | Do nothing if there's no *Packages* buffer. If there are | ||
| 853 | upgrades, mark them if `package-menu--mark-upgrades-pending' is | ||
| 854 | non-nil, otherwise just notify the user that there are upgrades. | ||
| 855 | This function is called after `package-refresh-contents'." | ||
| 856 | (let ((buf (get-buffer "*Packages*"))) | ||
| 857 | (when (buffer-live-p buf) | ||
| 858 | (with-current-buffer buf | ||
| 859 | (if package-menu--mark-upgrades-pending | ||
| 860 | (package-menu--mark-upgrades-1) | ||
| 861 | (package-menu--find-and-notify-upgrades)))))) | ||
| 862 | |||
| 863 | ;;;###autoload | ||
| 864 | (defun list-packages (&optional no-fetch) | ||
| 865 | "Display a list of packages. | ||
| 866 | This first fetches the updated list of packages before | ||
| 867 | displaying, unless a prefix argument NO-FETCH is specified. | ||
| 868 | The list is displayed in a buffer named `*Packages*', and | ||
| 869 | includes the package's version, availability status, and a | ||
| 870 | short description." | ||
| 871 | (interactive "P") | ||
| 872 | (require 'finder-inf nil t) | ||
| 873 | ;; Initialize the package system if necessary. | ||
| 874 | (unless package--initialized | ||
| 875 | (package-initialize t)) | ||
| 876 | ;; Integrate the package-menu with updating the archives. | ||
| 877 | (add-hook 'package--post-download-archives-hook | ||
| 878 | #'package-menu--post-refresh) | ||
| 879 | (add-hook 'package--post-download-archives-hook | ||
| 880 | #'package-menu--mark-or-notify-upgrades 'append) | ||
| 881 | (add-hook 'package--post-download-archives-hook | ||
| 882 | #'package-menu--set-mode-line-format 'append) | ||
| 883 | |||
| 884 | ;; Generate the Package Menu. | ||
| 885 | (let ((buf (get-buffer-create "*Packages*"))) | ||
| 886 | (with-current-buffer buf | ||
| 887 | ;; Since some packages have their descriptions include non-ASCII | ||
| 888 | ;; characters... | ||
| 889 | (setq buffer-file-coding-system 'utf-8) | ||
| 890 | (package-menu-mode) | ||
| 891 | |||
| 892 | ;; Fetch the remote list of packages. | ||
| 893 | (unless no-fetch (package-menu--refresh-contents)) | ||
| 894 | |||
| 895 | ;; If we're not async, this would be redundant. | ||
| 896 | (when package-menu-async | ||
| 897 | (package-menu--generate nil t))) | ||
| 898 | ;; The package menu buffer has keybindings. If the user types | ||
| 899 | ;; `M-x list-packages', that suggests it should become current. | ||
| 900 | (pop-to-buffer-same-window buf))) | ||
| 901 | |||
| 902 | ;;;###autoload | ||
| 903 | (defalias 'package-list-packages 'list-packages) | ||
| 904 | |||
| 905 | ;; Used in finder.el | ||
| 906 | ;;;###autoload | ||
| 907 | (defun package-show-package-list (&optional packages keywords) | ||
| 908 | "Display PACKAGES in a *Packages* buffer. | ||
| 909 | This is similar to `list-packages', but it does not fetch the | ||
| 910 | updated list of packages, and it only displays packages with | ||
| 911 | names in PACKAGES (which should be a list of symbols). | ||
| 912 | |||
| 913 | When KEYWORDS are given, only packages with those KEYWORDS are | ||
| 914 | shown." | ||
| 915 | (interactive) | ||
| 916 | (require 'finder-inf nil t) | ||
| 917 | (let* ((buf (get-buffer-create "*Packages*")) | ||
| 918 | (win (get-buffer-window buf))) | ||
| 919 | (with-current-buffer buf | ||
| 920 | (package-menu-mode) | ||
| 921 | (package-menu--generate nil packages keywords)) | ||
| 922 | (if win | ||
| 923 | (select-window win) | ||
| 924 | (switch-to-buffer buf)))) | ||
| 925 | |||
| 926 | (defun package-menu--filter-by (predicate suffix) | ||
| 927 | "Filter \"*Packages*\" buffer by PREDICATE and add SUFFIX to header. | ||
| 928 | PREDICATE is a function which will be called with one argument, a | ||
| 929 | `package-desc' object, and returns t if that object should be | ||
| 930 | listed in the Package Menu. | ||
| 931 | |||
| 932 | SUFFIX is passed on to `package-menu--display' and is added to | ||
| 933 | the header line of the first column." | ||
| 934 | ;; Update `tabulated-list-entries' so that it contains all | ||
| 935 | ;; packages before searching. | ||
| 936 | (package-menu--refresh t nil) | ||
| 937 | (let (found-entries) | ||
| 938 | (dolist (entry tabulated-list-entries) | ||
| 939 | (when (funcall predicate (car entry)) | ||
| 940 | (push entry found-entries))) | ||
| 941 | (if found-entries | ||
| 942 | (progn | ||
| 943 | (setq tabulated-list-entries found-entries) | ||
| 944 | (package-menu--display t suffix)) | ||
| 945 | (user-error "No packages found")))) | ||
| 946 | |||
| 947 | (defun package-menu-filter-by-archive (archive) | ||
| 948 | "Filter the \"*Packages*\" buffer by ARCHIVE. | ||
| 949 | Display only packages from package archive ARCHIVE. | ||
| 950 | ARCHIVE can be the name of a single archive (a string), or | ||
| 951 | a list of archive names. If ARCHIVE is nil or an empty | ||
| 952 | string, show all packages. | ||
| 953 | |||
| 954 | When called interactively, prompt for ARCHIVE. To specify | ||
| 955 | several archives, type their names separated by commas." | ||
| 956 | (interactive (list (completing-read-multiple | ||
| 957 | "Filter by archive: " | ||
| 958 | (mapcar #'car package-archives))) | ||
| 959 | package-menu-mode) | ||
| 960 | (package--ensure-package-menu-mode) | ||
| 961 | (let ((archives (ensure-list archive))) | ||
| 962 | (package-menu--filter-by | ||
| 963 | (lambda (pkg-desc) | ||
| 964 | (let ((pkg-archive (package-desc-archive pkg-desc))) | ||
| 965 | (or (null archives) | ||
| 966 | (and pkg-archive | ||
| 967 | (member pkg-archive archives))))) | ||
| 968 | (concat "archive:" (string-join archives ","))))) | ||
| 969 | |||
| 970 | (defun package-menu-filter-by-description (description) | ||
| 971 | "Filter the \"*Packages*\" buffer by the regexp DESCRIPTION. | ||
| 972 | Display only packages whose description matches the regexp | ||
| 973 | given as DESCRIPTION. | ||
| 974 | |||
| 975 | When called interactively, prompt for DESCRIPTION. | ||
| 976 | |||
| 977 | If DESCRIPTION is nil or the empty string, show all packages." | ||
| 978 | (interactive (list (read-regexp "Filter by description (regexp)")) | ||
| 979 | package-menu-mode) | ||
| 980 | (package--ensure-package-menu-mode) | ||
| 981 | (if (or (not description) (string-empty-p description)) | ||
| 982 | (package-menu--generate t t) | ||
| 983 | (package-menu--filter-by (lambda (pkg-desc) | ||
| 984 | (string-match description | ||
| 985 | (package-desc-summary pkg-desc))) | ||
| 986 | (format "desc:%s" description)))) | ||
| 987 | |||
| 988 | (defun package--has-keyword-p (desc &optional keywords) | ||
| 989 | "Test if package DESC has any of the given KEYWORDS. | ||
| 990 | When none are given, the package matches." | ||
| 991 | (if keywords | ||
| 992 | (let ((desc-keywords (and desc (package-desc--keywords desc))) | ||
| 993 | found) | ||
| 994 | (while (and (not found) keywords) | ||
| 995 | (let ((k (pop keywords))) | ||
| 996 | (setq found | ||
| 997 | (or (string= k (concat "arc:" (package-desc-archive desc))) | ||
| 998 | (string= k (concat "status:" (package-desc-status desc))) | ||
| 999 | (member k desc-keywords))))) | ||
| 1000 | found) | ||
| 1001 | t)) | ||
| 1002 | |||
| 1003 | (defun package-all-keywords () | ||
| 1004 | "Collect all package keywords." | ||
| 1005 | (let ((key-list)) | ||
| 1006 | (package--mapc (lambda (desc) | ||
| 1007 | (setq key-list (append (package-desc--keywords desc) | ||
| 1008 | key-list)))) | ||
| 1009 | key-list)) | ||
| 1010 | |||
| 1011 | (defun package-menu-filter-by-keyword (keyword) | ||
| 1012 | "Filter the \"*Packages*\" buffer by KEYWORD. | ||
| 1013 | Display only packages whose keywords match the specified KEYWORD. | ||
| 1014 | KEYWORD can be a string or a list of strings. If KEYWORD is nil | ||
| 1015 | or the empty string, show all packages. | ||
| 1016 | |||
| 1017 | In addition to package keywords, KEYWORD can include the name(s) | ||
| 1018 | of archive(s) and the package status, such as \"available\" | ||
| 1019 | or \"built-in\" or \"obsolete\". | ||
| 1020 | |||
| 1021 | When called interactively, prompt for KEYWORD. To specify several | ||
| 1022 | keywords, type them separated by commas." | ||
| 1023 | (interactive (list (completing-read-multiple | ||
| 1024 | "Keywords: " | ||
| 1025 | (package-all-keywords))) | ||
| 1026 | package-menu-mode) | ||
| 1027 | (package--ensure-package-menu-mode) | ||
| 1028 | (when (stringp keyword) | ||
| 1029 | (setq keyword (list keyword))) | ||
| 1030 | (if (not keyword) | ||
| 1031 | (package-menu--generate t t) | ||
| 1032 | (package-menu--filter-by (lambda (pkg-desc) | ||
| 1033 | (package--has-keyword-p pkg-desc keyword)) | ||
| 1034 | (concat "keyword:" (string-join keyword ","))))) | ||
| 1035 | |||
| 1036 | (define-obsolete-function-alias | ||
| 1037 | 'package-menu-filter #'package-menu-filter-by-keyword "27.1") | ||
| 1038 | |||
| 1039 | (defun package-menu-filter-by-name-or-description (name-or-description) | ||
| 1040 | "Filter the \"*Packages*\" buffer by the regexp NAME-OR-DESCRIPTION. | ||
| 1041 | Display only packages whose name or description matches the regexp | ||
| 1042 | NAME-OR-DESCRIPTION. | ||
| 1043 | |||
| 1044 | When called interactively, prompt for NAME-OR-DESCRIPTION. | ||
| 1045 | |||
| 1046 | If NAME-OR-DESCRIPTION is nil or the empty string, show all | ||
| 1047 | packages." | ||
| 1048 | (interactive (list (read-regexp "Filter by name or description (regexp)")) | ||
| 1049 | package-menu-mode) | ||
| 1050 | (package--ensure-package-menu-mode) | ||
| 1051 | (if (or (not name-or-description) (string-empty-p name-or-description)) | ||
| 1052 | (package-menu--generate t t) | ||
| 1053 | (package-menu--filter-by (lambda (pkg-desc) | ||
| 1054 | (or (string-match name-or-description | ||
| 1055 | (package-desc-summary pkg-desc)) | ||
| 1056 | (string-match name-or-description | ||
| 1057 | (symbol-name | ||
| 1058 | (package-desc-name pkg-desc))))) | ||
| 1059 | (format "name-or-desc:%s" name-or-description)))) | ||
| 1060 | |||
| 1061 | (defun package-menu-filter-by-name (name) | ||
| 1062 | "Filter the \"*Packages*\" buffer by the regexp NAME. | ||
| 1063 | Display only packages whose name matches the regexp NAME. | ||
| 1064 | |||
| 1065 | When called interactively, prompt for NAME. | ||
| 1066 | |||
| 1067 | If NAME is nil or the empty string, show all packages." | ||
| 1068 | (interactive (list (read-regexp "Filter by name (regexp)")) | ||
| 1069 | package-menu-mode) | ||
| 1070 | (package--ensure-package-menu-mode) | ||
| 1071 | (if (or (not name) (string-empty-p name)) | ||
| 1072 | (package-menu--generate t t) | ||
| 1073 | (package-menu--filter-by (lambda (pkg-desc) | ||
| 1074 | (string-match-p name (symbol-name | ||
| 1075 | (package-desc-name pkg-desc)))) | ||
| 1076 | (format "name:%s" name)))) | ||
| 1077 | |||
| 1078 | (defun package-menu-filter-by-status (status) | ||
| 1079 | "Filter the \"*Packages*\" buffer by STATUS. | ||
| 1080 | Display only packages with specified STATUS. | ||
| 1081 | STATUS can be a single status, a string, or a list of strings. | ||
| 1082 | If STATUS is nil or the empty string, show all packages. | ||
| 1083 | |||
| 1084 | When called interactively, prompt for STATUS. To specify | ||
| 1085 | several possible status values, type them separated by commas." | ||
| 1086 | (interactive (list (completing-read "Filter by status: " | ||
| 1087 | '("avail-obso" | ||
| 1088 | "available" | ||
| 1089 | "built-in" | ||
| 1090 | "dependency" | ||
| 1091 | "disabled" | ||
| 1092 | "external" | ||
| 1093 | "held" | ||
| 1094 | "incompat" | ||
| 1095 | "installed" | ||
| 1096 | "source" | ||
| 1097 | "new" | ||
| 1098 | "unsigned"))) | ||
| 1099 | package-menu-mode) | ||
| 1100 | (package--ensure-package-menu-mode) | ||
| 1101 | (if (or (not status) (string-empty-p status)) | ||
| 1102 | (package-menu--generate t t) | ||
| 1103 | (let ((status-list | ||
| 1104 | (if (listp status) | ||
| 1105 | status | ||
| 1106 | (split-string status ",")))) | ||
| 1107 | (package-menu--filter-by | ||
| 1108 | (lambda (pkg-desc) | ||
| 1109 | (member (package-desc-status pkg-desc) status-list)) | ||
| 1110 | (format "status:%s" (string-join status-list ",")))))) | ||
| 1111 | |||
| 1112 | (defun package-menu-filter-by-version (version predicate) | ||
| 1113 | "Filter the \"*Packages*\" buffer by VERSION and PREDICATE. | ||
| 1114 | Display only packages whose version satisfies the condition | ||
| 1115 | defined by VERSION and PREDICATE. | ||
| 1116 | |||
| 1117 | When called interactively, prompt for one of the comparison operators | ||
| 1118 | `<', `>' or `=', and for a version. Show only packages whose version | ||
| 1119 | is lower (`<'), equal (`=') or higher (`>') than the specified VERSION. | ||
| 1120 | |||
| 1121 | When called from Lisp, VERSION should be a version string and | ||
| 1122 | PREDICATE should be the symbol `=', `<' or `>'. | ||
| 1123 | |||
| 1124 | If VERSION is nil or the empty string, show all packages." | ||
| 1125 | (interactive (let ((choice (intern | ||
| 1126 | (char-to-string | ||
| 1127 | (read-char-choice | ||
| 1128 | "Filter by version? [Type =, <, > or q] " | ||
| 1129 | '(?< ?> ?= ?q)))))) | ||
| 1130 | (if (eq choice 'q) | ||
| 1131 | '(quit nil) | ||
| 1132 | (list (read-from-minibuffer | ||
| 1133 | (concat "Filter by version (" | ||
| 1134 | (pcase choice | ||
| 1135 | ('= "= equal to") | ||
| 1136 | ('< "< less than") | ||
| 1137 | ('> "> greater than")) | ||
| 1138 | "): ")) | ||
| 1139 | choice))) | ||
| 1140 | package-menu-mode) | ||
| 1141 | (package--ensure-package-menu-mode) | ||
| 1142 | (unless (equal predicate 'quit) | ||
| 1143 | (if (or (not version) (string-empty-p version)) | ||
| 1144 | (package-menu--generate t t) | ||
| 1145 | (package-menu--filter-by | ||
| 1146 | (let ((fun (pcase predicate | ||
| 1147 | ('= #'version-list-=) | ||
| 1148 | ('< #'version-list-<) | ||
| 1149 | ('> (lambda (a b) (not (version-list-<= a b)))) | ||
| 1150 | (_ (error "Unknown predicate: %s" predicate)))) | ||
| 1151 | (ver (version-to-list version))) | ||
| 1152 | (lambda (pkg-desc) | ||
| 1153 | (funcall fun (package-desc-version pkg-desc) ver))) | ||
| 1154 | (format "versions:%s%s" predicate version))))) | ||
| 1155 | |||
| 1156 | (defun package-menu-filter-marked () | ||
| 1157 | "Filter \"*Packages*\" buffer by non-empty mark. | ||
| 1158 | Show only the packages that have been marked for installation or deletion. | ||
| 1159 | Unlike other filters, this leaves the marks intact." | ||
| 1160 | (interactive nil package-menu-mode) | ||
| 1161 | (package--ensure-package-menu-mode) | ||
| 1162 | (widen) | ||
| 1163 | (let (found-entries mark pkg-id entry marks) | ||
| 1164 | (save-excursion | ||
| 1165 | (goto-char (point-min)) | ||
| 1166 | (while (not (eobp)) | ||
| 1167 | (setq mark (char-after)) | ||
| 1168 | (unless (eq mark ?\s) | ||
| 1169 | (setq pkg-id (tabulated-list-get-id)) | ||
| 1170 | (setq entry (package-menu--print-info-simple pkg-id)) | ||
| 1171 | (push entry found-entries) | ||
| 1172 | ;; remember the mark | ||
| 1173 | (push (cons pkg-id mark) marks)) | ||
| 1174 | (forward-line)) | ||
| 1175 | (if found-entries | ||
| 1176 | (progn | ||
| 1177 | (setq tabulated-list-entries found-entries) | ||
| 1178 | (package-menu--display t nil) | ||
| 1179 | ;; redo the marks, but we must remember the marks!! | ||
| 1180 | (goto-char (point-min)) | ||
| 1181 | (while (not (eobp)) | ||
| 1182 | (setq mark (cdr (assq (tabulated-list-get-id) marks))) | ||
| 1183 | (tabulated-list-put-tag (char-to-string mark) t))) | ||
| 1184 | (user-error "No packages found"))))) | ||
| 1185 | |||
| 1186 | (defun package-menu-filter-upgradable () | ||
| 1187 | "Filter \"*Packages*\" buffer to show only upgradable packages." | ||
| 1188 | (interactive nil package-menu-mode) | ||
| 1189 | (let ((pkgs (mapcar #'car (package-menu--find-upgrades)))) | ||
| 1190 | (package-menu--filter-by | ||
| 1191 | (lambda (pkg) | ||
| 1192 | (memql (package-desc-name pkg) pkgs)) | ||
| 1193 | "upgradable"))) | ||
| 1194 | |||
| 1195 | (defun package-menu-clear-filter () | ||
| 1196 | "Clear any filter currently applied to the \"*Packages*\" buffer." | ||
| 1197 | (interactive nil package-menu-mode) | ||
| 1198 | (package--ensure-package-menu-mode) | ||
| 1199 | (package-menu--generate t t)) | ||
| 1200 | |||
| 1201 | (defun package-list-packages-no-fetch () | ||
| 1202 | "Display a list of packages. | ||
| 1203 | Does not fetch the updated list of packages before displaying. | ||
| 1204 | The list is displayed in a buffer named `*Packages*'." | ||
| 1205 | (interactive) | ||
| 1206 | (list-packages t)) | ||
| 1207 | |||
| 1208 | ;;;###autoload | ||
| 1209 | (defun package-get-version () | ||
| 1210 | "Return the version number of the package in which this is used. | ||
| 1211 | Assumes it is used from an Elisp file placed inside the top-level directory | ||
| 1212 | of an installed ELPA package. | ||
| 1213 | The return value is a string (or nil in case we can't find it). | ||
| 1214 | It works in more cases if the call is in the file which contains | ||
| 1215 | the `Version:' header." | ||
| 1216 | ;; In a sense, this is a lie, but it does just what we want: precomputes | ||
| 1217 | ;; the version at compile time and hardcodes it into the .elc file! | ||
| 1218 | (declare (pure t)) | ||
| 1219 | ;; Hack alert! | ||
| 1220 | (let ((file (or (macroexp-file-name) buffer-file-name))) | ||
| 1221 | (cond | ||
| 1222 | ((null file) nil) | ||
| 1223 | ;; Packages are normally installed into directories named "<pkg>-<vers>", | ||
| 1224 | ;; so get the version number from there. | ||
| 1225 | ((string-match "/[^/]+-\\([0-9]\\(?:[0-9.]\\|pre\\|beta\\|alpha\\|snapshot\\)+\\)/[^/]+\\'" file) | ||
| 1226 | (match-string 1 file)) | ||
| 1227 | ;; For packages run straight from the an elpa.git clone, there's no | ||
| 1228 | ;; "-<vers>" in the directory name, so we have to fetch the version | ||
| 1229 | ;; the hard way. | ||
| 1230 | (t | ||
| 1231 | (let* ((pkgdir (file-name-directory file)) | ||
| 1232 | (pkgname (file-name-nondirectory (directory-file-name pkgdir))) | ||
| 1233 | (mainfile (expand-file-name (concat pkgname ".el") pkgdir))) | ||
| 1234 | (unless (file-readable-p mainfile) (setq mainfile file)) | ||
| 1235 | (when (file-readable-p mainfile) | ||
| 1236 | (lm-package-version mainfile))))))) | ||
| 1237 | |||
| 1238 | |||
| 1239 | |||
| 1240 | ;;;; Package menu mode. | ||
| 1241 | |||
| 1242 | (defvar-keymap package-menu-mode-map | ||
| 1243 | :doc "Local keymap for `package-menu-mode' buffers." | ||
| 1244 | :parent tabulated-list-mode-map | ||
| 1245 | "C-m" #'package-menu-describe-package | ||
| 1246 | "u" #'package-menu-mark-unmark | ||
| 1247 | "DEL" #'package-menu-backup-unmark | ||
| 1248 | "d" #'package-menu-mark-delete | ||
| 1249 | "i" #'package-menu-mark-install | ||
| 1250 | "U" #'package-menu-mark-upgrades | ||
| 1251 | "r" #'revert-buffer | ||
| 1252 | "~" #'package-menu-mark-obsolete-for-deletion | ||
| 1253 | "w" #'package-browse-url | ||
| 1254 | "b" #'package-report-bug | ||
| 1255 | "x" #'package-menu-execute | ||
| 1256 | "h" #'package-menu-quick-help | ||
| 1257 | "H" #'package-menu-hide-package | ||
| 1258 | "?" #'package-menu-describe-package | ||
| 1259 | "(" #'package-menu-toggle-hiding | ||
| 1260 | "/ /" #'package-menu-clear-filter | ||
| 1261 | "/ a" #'package-menu-filter-by-archive | ||
| 1262 | "/ d" #'package-menu-filter-by-description | ||
| 1263 | "/ k" #'package-menu-filter-by-keyword | ||
| 1264 | "/ N" #'package-menu-filter-by-name-or-description | ||
| 1265 | "/ n" #'package-menu-filter-by-name | ||
| 1266 | "/ s" #'package-menu-filter-by-status | ||
| 1267 | "/ v" #'package-menu-filter-by-version | ||
| 1268 | "/ m" #'package-menu-filter-marked | ||
| 1269 | "/ u" #'package-menu-filter-upgradable) | ||
| 1270 | |||
| 1271 | (easy-menu-define package-menu-mode-menu package-menu-mode-map | ||
| 1272 | "Menu for `package-menu-mode'." | ||
| 1273 | '("Package" | ||
| 1274 | ["Describe Package" package-menu-describe-package :help "Display information about this package"] | ||
| 1275 | ["Open Package Website" package-browse-url | ||
| 1276 | :help "Open the website of this package"] | ||
| 1277 | ["Help" package-menu-quick-help :help "Show short key binding help for package-menu-mode"] | ||
| 1278 | "--" | ||
| 1279 | ["Refresh Package List" revert-buffer | ||
| 1280 | :help "Redownload the package archive(s)" | ||
| 1281 | :active (not package--downloads-in-progress)] | ||
| 1282 | ["Execute Marked Actions" package-menu-execute :help "Perform all the marked actions"] | ||
| 1283 | |||
| 1284 | "--" | ||
| 1285 | ["Mark All Available Upgrades" package-menu-mark-upgrades | ||
| 1286 | :help "Mark packages that have a newer version for upgrading" | ||
| 1287 | :active (not package--downloads-in-progress)] | ||
| 1288 | ["Mark All Obsolete for Deletion" package-menu-mark-obsolete-for-deletion :help "Mark all obsolete packages for deletion"] | ||
| 1289 | ["Mark for Install" package-menu-mark-install :help "Mark a package for installation and move to the next line"] | ||
| 1290 | ["Mark for Deletion" package-menu-mark-delete :help "Mark a package for deletion and move to the next line"] | ||
| 1291 | ["Unmark" package-menu-mark-unmark :help "Clear any marks on a package and move to the next line"] | ||
| 1292 | |||
| 1293 | "--" | ||
| 1294 | ("Filter Packages" | ||
| 1295 | ["Filter by Archive" package-menu-filter-by-archive | ||
| 1296 | :help | ||
| 1297 | "Prompt for archive(s), display only packages from those archives"] | ||
| 1298 | ["Filter by Description" package-menu-filter-by-description | ||
| 1299 | :help | ||
| 1300 | "Prompt for regexp, display only packages with matching description"] | ||
| 1301 | ["Filter by Keyword" package-menu-filter-by-keyword | ||
| 1302 | :help | ||
| 1303 | "Prompt for keyword(s), display only packages with matching keywords"] | ||
| 1304 | ["Filter by Name" package-menu-filter-by-name | ||
| 1305 | :help | ||
| 1306 | "Prompt for regexp, display only packages whose names match the regexp"] | ||
| 1307 | ["Filter by Name or Description" package-menu-filter-by-name-or-description | ||
| 1308 | :help | ||
| 1309 | "Prompt for regexp, display only packages whose name or description matches"] | ||
| 1310 | ["Filter by Status" package-menu-filter-by-status | ||
| 1311 | :help | ||
| 1312 | "Prompt for status(es), display only packages with those statuses"] | ||
| 1313 | ["Filter by Upgrades available" package-menu-filter-upgradable | ||
| 1314 | :help "Display only installed packages for which upgrades are available"] | ||
| 1315 | ["Filter by Version" package-menu-filter-by-version | ||
| 1316 | :help | ||
| 1317 | "Prompt for version and comparison operator, display only packages of matching versions"] | ||
| 1318 | ["Filter Marked" package-menu-filter-marked | ||
| 1319 | :help "Display only packages marked for installation or deletion"] | ||
| 1320 | ["Clear Filter" package-menu-clear-filter | ||
| 1321 | :help "Clear package list filtering, display the entire list again"]) | ||
| 1322 | |||
| 1323 | ["Hide by Regexp" package-menu-hide-package | ||
| 1324 | :help "Toggle visibility of obsolete and unwanted packages"] | ||
| 1325 | ["Display Older Versions" package-menu-toggle-hiding | ||
| 1326 | :style toggle :selected (not package-menu--hide-packages) | ||
| 1327 | :help "Display package even if a newer version is already installed"] | ||
| 1328 | |||
| 1329 | "--" | ||
| 1330 | ["Quit" quit-window :help "Quit package selection"] | ||
| 1331 | ["Customize" (customize-group 'package)])) | ||
| 1332 | |||
| 1333 | (defconst package-menu-mode-line-format | ||
| 1334 | '((package-menu-mode-line-info | ||
| 1335 | (:eval (symbol-value 'package-menu-mode-line-info))))) | ||
| 1336 | |||
| 1337 | (defvar-local package-menu-mode-line-info nil | ||
| 1338 | "Variable which stores package-menu mode-line format.") | ||
| 1339 | |||
| 1340 | (defun package-menu--set-mode-line-format () | ||
| 1341 | "Display package-menu mode-line." | ||
| 1342 | (when-let* ((buf (get-buffer "*Packages*")) | ||
| 1343 | ((buffer-live-p buf))) | ||
| 1344 | (with-current-buffer buf | ||
| 1345 | (setq package-menu-mode-line-info | ||
| 1346 | (let ((installed 0) | ||
| 1347 | (new 0) | ||
| 1348 | (total (length package-archive-contents)) | ||
| 1349 | (to-upgrade (length (package-menu--find-upgrades))) | ||
| 1350 | (total-help "Total number of packages of all package archives") | ||
| 1351 | (installed-help "Total number of packages installed") | ||
| 1352 | (upgrade-help "Total number of packages to upgrade") | ||
| 1353 | (new-help "Total number of packages added recently")) | ||
| 1354 | |||
| 1355 | (save-excursion | ||
| 1356 | (goto-char (point-min)) | ||
| 1357 | (while (not (eobp)) | ||
| 1358 | (let ((status (package-menu-get-status))) | ||
| 1359 | (cond | ||
| 1360 | ((member status | ||
| 1361 | '("installed" "dependency" "unsigned")) | ||
| 1362 | (setq installed (1+ installed))) | ||
| 1363 | ((equal status "new") | ||
| 1364 | (setq new (1+ new))))) | ||
| 1365 | (forward-line))) | ||
| 1366 | |||
| 1367 | (setq installed (number-to-string installed)) | ||
| 1368 | (setq total (number-to-string total)) | ||
| 1369 | (setq to-upgrade (number-to-string to-upgrade)) | ||
| 1370 | |||
| 1371 | (list | ||
| 1372 | " [" | ||
| 1373 | (propertize "Total: " 'help-echo total-help) | ||
| 1374 | (propertize total | ||
| 1375 | 'help-echo total-help | ||
| 1376 | 'face 'package-mode-line-total) | ||
| 1377 | " / " | ||
| 1378 | (propertize "Installed: " 'help-echo installed-help) | ||
| 1379 | (propertize installed | ||
| 1380 | 'help-echo installed-help | ||
| 1381 | 'face 'package-mode-line-installed) | ||
| 1382 | " / " | ||
| 1383 | (propertize "To Upgrade: " 'help-echo upgrade-help) | ||
| 1384 | (propertize to-upgrade | ||
| 1385 | 'help-echo upgrade-help | ||
| 1386 | 'face 'package-mode-line-to-upgrade) | ||
| 1387 | (when (> new 0) | ||
| 1388 | (concat | ||
| 1389 | " / " | ||
| 1390 | (propertize "New: " 'help-echo new-help) | ||
| 1391 | (propertize (number-to-string new) | ||
| 1392 | 'help-echo new-help | ||
| 1393 | 'face 'package-mode-line-new))) | ||
| 1394 | "] ")))))) | ||
| 1395 | (defvar package-menu--tool-bar-map | ||
| 1396 | (let ((map (make-sparse-keymap))) | ||
| 1397 | (tool-bar-local-item-from-menu | ||
| 1398 | #'package-menu-execute "package-menu/execute" | ||
| 1399 | map package-menu-mode-map) | ||
| 1400 | (define-key-after map [separator-1] menu-bar-separator) | ||
| 1401 | (tool-bar-local-item-from-menu | ||
| 1402 | #'package-menu-mark-unmark "package-menu/unmark" | ||
| 1403 | map package-menu-mode-map) | ||
| 1404 | (tool-bar-local-item-from-menu | ||
| 1405 | #'package-menu-mark-install "package-menu/install" | ||
| 1406 | map package-menu-mode-map) | ||
| 1407 | (tool-bar-local-item-from-menu | ||
| 1408 | #'package-menu-mark-delete "package-menu/delete" | ||
| 1409 | map package-menu-mode-map) | ||
| 1410 | (tool-bar-local-item-from-menu | ||
| 1411 | #'package-menu-describe-package "package-menu/info" | ||
| 1412 | map package-menu-mode-map) | ||
| 1413 | (tool-bar-local-item-from-menu | ||
| 1414 | #'package-browse-url "package-menu/url" | ||
| 1415 | map package-menu-mode-map) | ||
| 1416 | (tool-bar-local-item | ||
| 1417 | "package-menu/upgrade" 'package-upgrade-all | ||
| 1418 | 'package-upgrade-all | ||
| 1419 | map :help "Upgrade all the packages") | ||
| 1420 | (define-key-after map [separator-2] menu-bar-separator) | ||
| 1421 | (tool-bar-local-item | ||
| 1422 | "search" 'isearch-forward 'search map | ||
| 1423 | :help "Search" :vert-only t) | ||
| 1424 | (tool-bar-local-item-from-menu | ||
| 1425 | #'revert-buffer "refresh" | ||
| 1426 | map package-menu-mode-map) | ||
| 1427 | (tool-bar-local-item-from-menu | ||
| 1428 | #'quit-window "close" | ||
| 1429 | map package-menu-mode-map) | ||
| 1430 | map)) | ||
| 1431 | |||
| 1432 | (define-derived-mode package-menu-mode tabulated-list-mode "Package Menu" | ||
| 1433 | "Major mode for browsing a list of packages. | ||
| 1434 | The most useful commands here are: | ||
| 1435 | |||
| 1436 | `x': Install the package under point if it isn't already installed, | ||
| 1437 | and delete it if it's already installed, | ||
| 1438 | `i': mark a package for installation, and | ||
| 1439 | `d': mark a package for deletion. Use the `x' command to perform the | ||
| 1440 | actions on the marked files. | ||
| 1441 | \\<package-menu-mode-map> | ||
| 1442 | \\{package-menu-mode-map}" | ||
| 1443 | :interactive nil | ||
| 1444 | (setq mode-line-process '((package--downloads-in-progress ":Loading") | ||
| 1445 | (package-menu--transaction-status | ||
| 1446 | package-menu--transaction-status))) | ||
| 1447 | (setq-local mode-line-misc-info | ||
| 1448 | (append | ||
| 1449 | mode-line-misc-info | ||
| 1450 | package-menu-mode-line-format)) | ||
| 1451 | (setq-local tool-bar-map package-menu--tool-bar-map) | ||
| 1452 | (setq tabulated-list-format | ||
| 1453 | `[("Package" ,package-name-column-width package-menu--name-predicate) | ||
| 1454 | ("Version" ,package-version-column-width package-menu--version-predicate) | ||
| 1455 | ("Status" ,package-status-column-width package-menu--status-predicate) | ||
| 1456 | ("Archive" ,package-archive-column-width package-menu--archive-predicate) | ||
| 1457 | ("Description" 0 package-menu--description-predicate)]) | ||
| 1458 | (setq tabulated-list-padding 2) | ||
| 1459 | (setq tabulated-list-sort-key (cons "Status" nil)) | ||
| 1460 | (add-hook 'tabulated-list-revert-hook #'package-menu--refresh nil t) | ||
| 1461 | (tabulated-list-init-header) | ||
| 1462 | (setq revert-buffer-function 'package-menu--refresh-contents) | ||
| 1463 | (setf imenu-prev-index-position-function | ||
| 1464 | #'package--imenu-prev-index-position-function) | ||
| 1465 | (setf imenu-extract-index-name-function | ||
| 1466 | #'package--imenu-extract-index-name-function)) | ||
| 1467 | |||
| 1468 | (defvar package-menu--hide-packages t | ||
| 1469 | "Whether available obsolete packages should be hidden. | ||
| 1470 | Can be toggled with \\<package-menu-mode-map> \\[package-menu-toggle-hiding]. | ||
| 1471 | Installed obsolete packages are always displayed.") | ||
| 1472 | |||
| 1473 | (defun package-menu--refresh (&optional packages keywords) | ||
| 1474 | "Re-populate the `tabulated-list-entries'. | ||
| 1475 | PACKAGES should be nil or t, which means to display all known packages. | ||
| 1476 | KEYWORDS should be nil or a list of keywords." | ||
| 1477 | ;; Construct list of (PKG-DESC . STATUS). | ||
| 1478 | (unless packages (setq packages t)) | ||
| 1479 | (let ((hidden-names (mapconcat #'identity package-hidden-regexps "\\|")) | ||
| 1480 | info-list) | ||
| 1481 | ;; Installed packages: | ||
| 1482 | (dolist (elt package-alist) | ||
| 1483 | (let ((name (car elt))) | ||
| 1484 | (when (or (eq packages t) (memq name packages)) | ||
| 1485 | (dolist (pkg (cdr elt)) | ||
| 1486 | (when (package--has-keyword-p pkg keywords) | ||
| 1487 | (push pkg info-list)))))) | ||
| 1488 | |||
| 1489 | ;; Built-in packages: | ||
| 1490 | (dolist (elt package--builtins) | ||
| 1491 | (let ((pkg (package--from-builtin elt)) | ||
| 1492 | (name (car elt))) | ||
| 1493 | (when (not (eq name 'emacs)) ; Hide the `emacs' package. | ||
| 1494 | (when (and (package--has-keyword-p pkg keywords) | ||
| 1495 | (or package-list-unversioned | ||
| 1496 | (package--bi-desc-version (cdr elt))) | ||
| 1497 | (or (eq packages t) (memq name packages))) | ||
| 1498 | (push pkg info-list))))) | ||
| 1499 | |||
| 1500 | ;; Available and disabled packages: | ||
| 1501 | (unless (equal package--old-archive-priorities package-archive-priorities) | ||
| 1502 | (package-read-all-archive-contents)) | ||
| 1503 | (dolist (elt package-archive-contents) | ||
| 1504 | (let ((name (car elt))) | ||
| 1505 | ;; To be displayed it must be in PACKAGES; | ||
| 1506 | (when (and (or (eq packages t) (memq name packages)) | ||
| 1507 | ;; and we must either not be hiding anything, | ||
| 1508 | (or (not package-menu--hide-packages) | ||
| 1509 | (not package-hidden-regexps) | ||
| 1510 | ;; or just not hiding this specific package. | ||
| 1511 | (not (string-match hidden-names (symbol-name name))))) | ||
| 1512 | ;; Hide available-obsolete or low-priority packages. | ||
| 1513 | (dolist (pkg (package--remove-hidden (cdr elt))) | ||
| 1514 | (when (package--has-keyword-p pkg keywords) | ||
| 1515 | (push pkg info-list)))))) | ||
| 1516 | |||
| 1517 | ;; Print the result. | ||
| 1518 | (tabulated-list-init-header) | ||
| 1519 | (setq tabulated-list-entries | ||
| 1520 | (mapcar #'package-menu--print-info-simple info-list)))) | ||
| 1521 | |||
| 1522 | (defun package--remove-hidden (pkg-list) | ||
| 1523 | "Filter PKG-LIST according to `package-archive-priorities'. | ||
| 1524 | PKG-LIST must be a list of `package-desc' objects, all with the | ||
| 1525 | same name, sorted by decreasing `package-desc-priority-version'. | ||
| 1526 | Return a list of packages tied for the highest priority according | ||
| 1527 | to their archives." | ||
| 1528 | (when pkg-list | ||
| 1529 | ;; Variable toggled with `package-menu-toggle-hiding'. | ||
| 1530 | (if (not package-menu--hide-packages) | ||
| 1531 | pkg-list | ||
| 1532 | (let ((installed (cadr (assq (package-desc-name (car pkg-list)) | ||
| 1533 | package-alist)))) | ||
| 1534 | (when installed | ||
| 1535 | (setq pkg-list | ||
| 1536 | (let ((ins-version (package-desc-version installed))) | ||
| 1537 | (cl-remove-if (lambda (p) (version-list-< (package-desc-version p) | ||
| 1538 | ins-version)) | ||
| 1539 | pkg-list)))) | ||
| 1540 | (let ((filtered-by-priority | ||
| 1541 | (cond | ||
| 1542 | ((not package-menu-hide-low-priority) | ||
| 1543 | pkg-list) | ||
| 1544 | ((eq package-menu-hide-low-priority 'archive) | ||
| 1545 | (let (max-priority out) | ||
| 1546 | (while pkg-list | ||
| 1547 | (let ((p (pop pkg-list))) | ||
| 1548 | (let ((priority (package-desc-priority p))) | ||
| 1549 | (if (and max-priority (< priority max-priority)) | ||
| 1550 | (setq pkg-list nil) | ||
| 1551 | (push p out) | ||
| 1552 | (setq max-priority priority))))) | ||
| 1553 | (nreverse out))) | ||
| 1554 | (pkg-list | ||
| 1555 | (list (car pkg-list)))))) | ||
| 1556 | (if (not installed) | ||
| 1557 | filtered-by-priority | ||
| 1558 | (let ((ins-version (package-desc-version installed))) | ||
| 1559 | (cl-remove-if (lambda (p) (or (version-list-= (package-desc-version p) | ||
| 1560 | ins-version) | ||
| 1561 | (package-vc-p installed))) | ||
| 1562 | filtered-by-priority)))))))) | ||
| 1563 | |||
| 1564 | (defun package-menu-toggle-hiding () | ||
| 1565 | "In Package Menu, toggle visibility of obsolete available packages. | ||
| 1566 | |||
| 1567 | Also hide packages whose name matches a regexp in user option | ||
| 1568 | `package-hidden-regexps' (a list). To add regexps to this list, | ||
| 1569 | use `package-menu-hide-package'." | ||
| 1570 | (interactive nil package-menu-mode) | ||
| 1571 | (package--ensure-package-menu-mode) | ||
| 1572 | (setq package-menu--hide-packages | ||
| 1573 | (not package-menu--hide-packages)) | ||
| 1574 | (if package-menu--hide-packages | ||
| 1575 | (message "Hiding obsolete or unwanted packages") | ||
| 1576 | (message "Displaying all packages")) | ||
| 1577 | (revert-buffer nil 'no-confirm)) | ||
| 1578 | |||
| 1579 | (provide 'package-menu) | ||
| 1580 | ;;; package-menu.el ends here | ||
diff --git a/lisp/package/package-misc.el b/lisp/package/package-misc.el new file mode 100644 index 00000000000..0432ab06f83 --- /dev/null +++ b/lisp/package/package-misc.el | |||
| @@ -0,0 +1,129 @@ | |||
| 1 | ;;; package-misc.el --- Miscellaneous Packaging Functionality -*- lexical-binding: t; -*- | ||
| 2 | |||
| 3 | ;; Copyright (C) 2025 Philip Kaludercic | ||
| 4 | |||
| 5 | ;; Author: Philip Kaludercic <philipk@posteo.net> | ||
| 6 | |||
| 7 | ;; This program is free software; you can redistribute it and/or modify | ||
| 8 | ;; it under the terms of the GNU General Public License as published by | ||
| 9 | ;; the Free Software Foundation, either version 3 of the License, or | ||
| 10 | ;; (at your option) any later version. | ||
| 11 | |||
| 12 | ;; This program is distributed in the hope that it will be useful, | ||
| 13 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 14 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 15 | ;; GNU General Public License for more details. | ||
| 16 | |||
| 17 | ;; You should have received a copy of the GNU General Public License | ||
| 18 | ;; along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
| 19 | |||
| 20 | ;;; Commentary: | ||
| 21 | |||
| 22 | ;;; Code: | ||
| 23 | |||
| 24 | (require 'package-core) | ||
| 25 | |||
| 26 | (require 'macroexp) | ||
| 27 | |||
| 28 | (defun package--print-email-button (recipient) | ||
| 29 | "Insert a button whose action will send an email to RECIPIENT. | ||
| 30 | NAME should have the form (FULLNAME . EMAIL) where FULLNAME is | ||
| 31 | either a full name or nil, and EMAIL is a valid email address." | ||
| 32 | (when (car recipient) | ||
| 33 | (insert (car recipient))) | ||
| 34 | (when (and (car recipient) (cdr recipient)) | ||
| 35 | (insert " ")) | ||
| 36 | (when (cdr recipient) | ||
| 37 | (insert "<") | ||
| 38 | (insert-text-button (cdr recipient) | ||
| 39 | 'follow-link t | ||
| 40 | 'action (lambda (_) | ||
| 41 | (compose-mail | ||
| 42 | (format "%s <%s>" (car recipient) (cdr recipient))))) | ||
| 43 | (insert ">")) | ||
| 44 | (insert "\n")) | ||
| 45 | |||
| 46 | (declare-function ietf-drums-parse-address "ietf-drums" | ||
| 47 | (string &optional decode)) | ||
| 48 | |||
| 49 | (defun package-maintainers (pkg-desc &optional no-error) | ||
| 50 | "Return an email address for the maintainers of PKG-DESC. | ||
| 51 | The email address may contain commas, if there are multiple | ||
| 52 | maintainers. If no maintainers are found, an error will be | ||
| 53 | signaled. If the optional argument NO-ERROR is non-nil no error | ||
| 54 | will be signaled in that case." | ||
| 55 | (unless (package-desc-p pkg-desc) | ||
| 56 | (error "Invalid package description: %S" pkg-desc)) | ||
| 57 | (let* ((name (package-desc-name pkg-desc)) | ||
| 58 | (extras (package-desc-extras pkg-desc)) | ||
| 59 | (maint (alist-get :maintainer extras))) | ||
| 60 | (unless (listp (cdr maint)) | ||
| 61 | (setq maint (list maint))) | ||
| 62 | (cond | ||
| 63 | ((and (null maint) (null no-error)) | ||
| 64 | (user-error "Package `%s' has no explicit maintainer" name)) | ||
| 65 | ((and (not (progn | ||
| 66 | (require 'ietf-drums) | ||
| 67 | (ietf-drums-parse-address (cdar maint)))) | ||
| 68 | (null no-error)) | ||
| 69 | (user-error "Package `%s' has no maintainer address" name)) | ||
| 70 | (t | ||
| 71 | (with-temp-buffer | ||
| 72 | (mapc #'package--print-email-button maint) | ||
| 73 | (replace-regexp-in-string | ||
| 74 | "\n" ", " (string-trim | ||
| 75 | (buffer-substring-no-properties | ||
| 76 | (point-min) (point-max))))))))) | ||
| 77 | |||
| 78 | ;;;###autoload | ||
| 79 | (defun package-report-bug (desc) | ||
| 80 | "Prepare a message to send to the maintainers of a package. | ||
| 81 | DESC must be a `package-desc' object. | ||
| 82 | |||
| 83 | Of interest to package maintainers: By default, the command will use | ||
| 84 | `reporter-submit-bug-report' to generate a message buffer. If your | ||
| 85 | package has specific needs, you can set the symbol property | ||
| 86 | `package-report-bug-function' of the symbol designating your package | ||
| 87 | name. | ||
| 88 | " | ||
| 89 | (interactive (list (package--query-desc package-alist)) | ||
| 90 | package-menu-mode) | ||
| 91 | (let ((maint (package-maintainers desc)) | ||
| 92 | (name (symbol-name (package-desc-name desc))) | ||
| 93 | (pkgdir (package-desc-dir desc)) | ||
| 94 | vars) | ||
| 95 | (when pkgdir | ||
| 96 | (dolist-with-progress-reporter (group custom-current-group-alist) | ||
| 97 | "Scanning for modified user options..." | ||
| 98 | (when (and (car group) | ||
| 99 | (file-in-directory-p (car group) pkgdir)) | ||
| 100 | (dolist (ent (get (cdr group) 'custom-group)) | ||
| 101 | (when (and (custom-variable-p (car ent)) | ||
| 102 | (boundp (car ent)) | ||
| 103 | (not (eq (custom--standard-value (car ent)) | ||
| 104 | (default-toplevel-value (car ent))))) | ||
| 105 | (push (car ent) vars)))))) | ||
| 106 | (dlet ((reporter-prompt-for-summary-p t)) | ||
| 107 | (funcall (or (get name 'package-report-bug-function) | ||
| 108 | #'reporter-submit-bug-report) | ||
| 109 | maint name vars)))) | ||
| 110 | |||
| 111 | ;;;; Inferring package from current buffer | ||
| 112 | (defun package-read-from-string (str) | ||
| 113 | "Read a Lisp expression from STR. | ||
| 114 | Signal an error if the entire string was not used." | ||
| 115 | (pcase-let ((`(,expr . ,offset) (read-from-string str))) | ||
| 116 | (condition-case () | ||
| 117 | ;; The call to `ignore' suppresses a compiler warning. | ||
| 118 | (progn (ignore (read-from-string str offset)) | ||
| 119 | (error "Can't read whole string")) | ||
| 120 | (end-of-file expr)))) | ||
| 121 | |||
| 122 | |||
| 123 | (defun package--alist-to-plist-args (alist) | ||
| 124 | (mapcar #'macroexp-quote | ||
| 125 | (apply #'nconc | ||
| 126 | (mapcar (lambda (pair) (list (car pair) (cdr pair))) alist)))) | ||
| 127 | |||
| 128 | (provide 'package-misc) | ||
| 129 | ;;; package-misc.el ends here | ||
diff --git a/lisp/package/package-quickstart.el b/lisp/package/package-quickstart.el new file mode 100644 index 00000000000..39e71c6533c --- /dev/null +++ b/lisp/package/package-quickstart.el | |||
| @@ -0,0 +1,151 @@ | |||
| 1 | ;;; package-quickstart.el --- Accelerating Package Startup -*- lexical-binding: t; -*- | ||
| 2 | |||
| 3 | ;; Copyright (C) 2025 Philip Kaludercic | ||
| 4 | |||
| 5 | ;; Author: Philip Kaludercic <philipk@posteo.net> | ||
| 6 | |||
| 7 | ;; This program is free software; you can redistribute it and/or modify | ||
| 8 | ;; it under the terms of the GNU General Public License as published by | ||
| 9 | ;; the Free Software Foundation, either version 3 of the License, or | ||
| 10 | ;; (at your option) any later version. | ||
| 11 | |||
| 12 | ;; This program is distributed in the hope that it will be useful, | ||
| 13 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 14 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 15 | ;; GNU General Public License for more details. | ||
| 16 | |||
| 17 | ;; You should have received a copy of the GNU General Public License | ||
| 18 | ;; along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
| 19 | |||
| 20 | ;;; Commentary: | ||
| 21 | |||
| 22 | ;; Activating packages via `package-initialize' is costly: for N installed | ||
| 23 | ;; packages, it needs to read all N <pkg>-pkg.el files first to decide | ||
| 24 | ;; which packages to activate, and then again N <pkg>-autoloads.el files. | ||
| 25 | ;; To speed this up, we precompute a mega-autoloads file which is the | ||
| 26 | ;; concatenation of all those <pkg>-autoloads.el, so we can activate | ||
| 27 | ;; all packages by loading this one file (and hence without initializing | ||
| 28 | ;; package.el). | ||
| 29 | |||
| 30 | ;; Other than speeding things up, this also offers a bootstrap feature: | ||
| 31 | ;; it lets us activate packages according to `package-load-list' and | ||
| 32 | ;; `package-user-dir' even before those vars are set. | ||
| 33 | |||
| 34 | ;;; Code: | ||
| 35 | |||
| 36 | (require 'package-core) | ||
| 37 | |||
| 38 | (defcustom package-quickstart nil | ||
| 39 | "Precompute activation actions to speed up startup. | ||
| 40 | This requires the use of `package-quickstart-refresh' every time the | ||
| 41 | activations need to be changed, such as when `package-load-list' is modified." | ||
| 42 | :type 'boolean | ||
| 43 | :version "27.1" | ||
| 44 | :group 'package) | ||
| 45 | |||
| 46 | ;;;###autoload | ||
| 47 | (defcustom package-quickstart-file | ||
| 48 | (locate-user-emacs-file "package-quickstart.el") | ||
| 49 | "Location of the file used to speed up activation of packages at startup." | ||
| 50 | :type 'file | ||
| 51 | :group 'applications | ||
| 52 | :initialize #'custom-initialize-delay | ||
| 53 | :version "27.1") | ||
| 54 | |||
| 55 | (defun package--quickstart-maybe-refresh () | ||
| 56 | (if package-quickstart | ||
| 57 | ;; FIXME: Delay refresh in case we're installing/deleting | ||
| 58 | ;; several packages! | ||
| 59 | (package-quickstart-refresh) | ||
| 60 | (delete-file (concat package-quickstart-file "c")) | ||
| 61 | (delete-file package-quickstart-file))) | ||
| 62 | |||
| 63 | (defvar package--quickstart-dir nil | ||
| 64 | "Set by `package-quickstart-file' to the directory containing it.") | ||
| 65 | |||
| 66 | (defun package--quickstart-rel (file) | ||
| 67 | "Return an expr depending on `package--quickstart-dir' which evaluates to FILE. | ||
| 68 | |||
| 69 | If FILE is in `package--quickstart-dir', returns an expression that is | ||
| 70 | relative to that directory, so if that directory is moved we can still | ||
| 71 | find FILE." | ||
| 72 | (if (file-in-directory-p file package--quickstart-dir) | ||
| 73 | `(file-name-concat package--quickstart-dir ,(file-relative-name file package--quickstart-dir)) | ||
| 74 | file)) | ||
| 75 | |||
| 76 | (defun package-quickstart-refresh () | ||
| 77 | "(Re)Generate the `package-quickstart-file'." | ||
| 78 | (interactive) | ||
| 79 | (package-initialize 'no-activate) | ||
| 80 | (require 'info) | ||
| 81 | (let ((package--quickstart-pkgs ()) | ||
| 82 | ;; Pretend we haven't activated anything yet! | ||
| 83 | (package-activated-list ()) | ||
| 84 | ;; Make sure we can load this file without load-source-file-function. | ||
| 85 | (coding-system-for-write 'emacs-internal) | ||
| 86 | ;; Ensure that `pp' and `prin1-to-string' calls further down | ||
| 87 | ;; aren't truncated. | ||
| 88 | (print-length nil) | ||
| 89 | (print-level nil) | ||
| 90 | (Info-directory-list '("")) | ||
| 91 | (package--quickstart-dir nil)) | ||
| 92 | (dolist (elt package-alist) | ||
| 93 | (condition-case err | ||
| 94 | (package-activate (car elt)) | ||
| 95 | ;; Don't let failure of activation of a package arbitrarily stop | ||
| 96 | ;; activation of further packages. | ||
| 97 | (error (message "%s" (error-message-string err))))) | ||
| 98 | (setq package--quickstart-pkgs (nreverse package--quickstart-pkgs)) | ||
| 99 | (with-temp-file package-quickstart-file | ||
| 100 | (emacs-lisp-mode) ;For `syntax-ppss'. | ||
| 101 | (insert ";;; Quickstart file to activate all packages at startup -*- lexical-binding:t -*-\n") | ||
| 102 | (insert ";; ¡¡ This file is autogenerated by `package-quickstart-refresh', DO NOT EDIT !!\n\n") | ||
| 103 | (setq package--quickstart-dir | ||
| 104 | (file-name-directory (expand-file-name package-quickstart-file))) | ||
| 105 | (pp '(setq package--quickstart-dir | ||
| 106 | (file-name-directory (expand-file-name load-file-name))) | ||
| 107 | (current-buffer)) | ||
| 108 | (dolist (pkg package--quickstart-pkgs) | ||
| 109 | (let* ((file | ||
| 110 | ;; Prefer uncompiled files (and don't accept .so files). | ||
| 111 | (let ((load-suffixes '(".el" ".elc"))) | ||
| 112 | (locate-library (package--autoloads-file-name pkg)))) | ||
| 113 | (pfile (prin1-to-string (package--quickstart-rel file)))) | ||
| 114 | (insert "(let* ((load-file-name " pfile ")\ | ||
| 115 | \(load-true-file-name load-file-name))\n") | ||
| 116 | (insert-file-contents file) | ||
| 117 | ;; Fixup the special #$ reader form and throw away comments. | ||
| 118 | (while (re-search-forward "#\\$\\|^;\\(.*\n\\)" nil 'move) | ||
| 119 | (unless (ppss-string-terminator (save-match-data (syntax-ppss))) | ||
| 120 | (replace-match (if (match-end 1) "" pfile) t t))) | ||
| 121 | (unless (bolp) (insert "\n")) | ||
| 122 | (insert ")\n"))) | ||
| 123 | (pp `(defvar package-activated-list) (current-buffer)) | ||
| 124 | (pp `(setq package-activated-list | ||
| 125 | (delete-dups | ||
| 126 | (append ',(mapcar #'package-desc-name package--quickstart-pkgs) | ||
| 127 | package-activated-list))) | ||
| 128 | (current-buffer)) | ||
| 129 | (let ((info-dirs | ||
| 130 | (mapcar #'package--quickstart-rel (butlast Info-directory-list)))) | ||
| 131 | (when info-dirs | ||
| 132 | (pp `(progn (require 'info) | ||
| 133 | (info-initialize) | ||
| 134 | (setq Info-directory-list | ||
| 135 | (append (list . ,info-dirs) Info-directory-list))) | ||
| 136 | (current-buffer)))) | ||
| 137 | ;; Use `\s' instead of a space character, so this code chunk is not | ||
| 138 | ;; mistaken for an actual file-local section of package.el. | ||
| 139 | (insert " | ||
| 140 | ;; Local\sVariables: | ||
| 141 | ;; version-control: never | ||
| 142 | ;; no-update-autoloads: t | ||
| 143 | ;; byte-compile-warnings: (not make-local) | ||
| 144 | ;; End: | ||
| 145 | ")) | ||
| 146 | ;; FIXME: Do it asynchronously in an Emacs subprocess, and | ||
| 147 | ;; don't show the byte-compiler warnings. | ||
| 148 | (byte-compile-file package-quickstart-file))) | ||
| 149 | |||
| 150 | (provide 'package-quickstart) | ||
| 151 | ;;; package-quickstart.el ends here | ||
diff --git a/lisp/package/package-vc.el b/lisp/package/package-vc.el new file mode 100644 index 00000000000..d40c7efb670 --- /dev/null +++ b/lisp/package/package-vc.el | |||
| @@ -0,0 +1,1003 @@ | |||
| 1 | ;;; package-vc.el --- Manage packages from VC checkouts -*- lexical-binding: t; -*- | ||
| 2 | |||
| 3 | ;; Copyright (C) 2022-2025 Free Software Foundation, Inc. | ||
| 4 | |||
| 5 | ;; Author: Philip Kaludercic <philipk@posteo.net> | ||
| 6 | ;; Maintainer: Philip Kaludercic <philipk@posteo.net> | ||
| 7 | ;; Keywords: tools | ||
| 8 | |||
| 9 | ;; This file is part of GNU Emacs. | ||
| 10 | |||
| 11 | ;; GNU Emacs is free software: you can redistribute it and/or modify | ||
| 12 | ;; it under the terms of the GNU General Public License as published by | ||
| 13 | ;; the Free Software Foundation, either version 3 of the License, or | ||
| 14 | ;; (at your option) any later version. | ||
| 15 | |||
| 16 | ;; GNU Emacs is distributed in the hope that it will be useful, | ||
| 17 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 18 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 19 | ;; GNU General Public License for more details. | ||
| 20 | |||
| 21 | ;; You should have received a copy of the GNU General Public License | ||
| 22 | ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>. | ||
| 23 | |||
| 24 | ;;; Commentary: | ||
| 25 | |||
| 26 | ;; While packages managed by package.el use tarballs for distributing | ||
| 27 | ;; the source code, this extension allows for packages to be fetched | ||
| 28 | ;; and upgraded directly from a version control system. | ||
| 29 | ;; | ||
| 30 | ;; To install a package from source use `package-vc-install'. If you | ||
| 31 | ;; aren't interested in activating a package, you can use | ||
| 32 | ;; `package-vc-checkout' instead, which will prompt you for a target | ||
| 33 | ;; directory. If you wish to reuse an existing checkout, the command | ||
| 34 | ;; `package-vc-install-from-checkout' will create a symbolic link and | ||
| 35 | ;; prepare the package. | ||
| 36 | ;; | ||
| 37 | ;; If you make local changes that you wish to share with an upstream | ||
| 38 | ;; maintainer, the command `package-vc-prepare-patch' can prepare | ||
| 39 | ;; these as patches to send via Email. | ||
| 40 | |||
| 41 | ;;; TODO: | ||
| 42 | |||
| 43 | ;; - Allow maintaining patches that are ported back onto regular | ||
| 44 | ;; packages and maintained between versions. | ||
| 45 | |||
| 46 | ;;; Code: | ||
| 47 | |||
| 48 | (eval-when-compile (require 'rx)) | ||
| 49 | (eval-when-compile (require 'map)) | ||
| 50 | (eval-when-compile (require 'cl-lib)) | ||
| 51 | (require 'package-elpa) | ||
| 52 | (require 'package-misc) | ||
| 53 | (require 'package-install) | ||
| 54 | (require 'lisp-mnt) | ||
| 55 | (require 'vc) | ||
| 56 | (require 'seq) | ||
| 57 | |||
| 58 | (defgroup package-vc nil | ||
| 59 | "Manage packages from VC checkouts." | ||
| 60 | :group 'package | ||
| 61 | :link '(custom-manual "(emacs) Fetching Package Sources") | ||
| 62 | :prefix "package-vc-" | ||
| 63 | :version "29.1") | ||
| 64 | |||
| 65 | (defconst package-vc--elpa-packages-version 1 | ||
| 66 | "Version number of the package specification format understood by package-vc.") | ||
| 67 | |||
| 68 | (define-obsolete-variable-alias | ||
| 69 | 'package-vc-heuristic-alist | ||
| 70 | 'vc-clone-heuristic-alist "31.1") | ||
| 71 | |||
| 72 | (defcustom package-vc-default-backend 'Git | ||
| 73 | "Default VC backend to use for cloning package repositories. | ||
| 74 | `package-vc-install' uses this backend when you specify neither | ||
| 75 | the backend nor a repository URL that's recognized via | ||
| 76 | `vc-clone-heuristic-alist'. | ||
| 77 | |||
| 78 | The value must be a member of `vc-handled-backends' that supports | ||
| 79 | the `clone' VC function." | ||
| 80 | :type vc-cloneable-backends-custom-type | ||
| 81 | :version "29.1") | ||
| 82 | |||
| 83 | (defcustom package-vc-register-as-project t | ||
| 84 | "Non-nil means that packages should be registered as projects." | ||
| 85 | :type 'boolean | ||
| 86 | :version "30.1") | ||
| 87 | |||
| 88 | (defvar package-vc-selected-packages) ; pacify byte-compiler | ||
| 89 | |||
| 90 | ;;;###autoload | ||
| 91 | (defun package-vc-install-selected-packages () | ||
| 92 | "Ensure packages specified in `package-vc-selected-packages' are installed." | ||
| 93 | (interactive) | ||
| 94 | (pcase-dolist (`(,name . ,spec) package-vc-selected-packages) | ||
| 95 | (when (stringp name) | ||
| 96 | (setq name (intern name))) | ||
| 97 | (let ((pkg-descs (assoc name package-alist #'string=))) | ||
| 98 | (unless (seq-some #'package-vc-p (cdr pkg-descs)) | ||
| 99 | (cond | ||
| 100 | ((null spec) | ||
| 101 | (package-vc-install name)) | ||
| 102 | ((stringp spec) | ||
| 103 | (package-vc-install name spec)) | ||
| 104 | ((listp spec) | ||
| 105 | (package-vc--archives-initialize) | ||
| 106 | (package-vc--unpack | ||
| 107 | (or (cadr (assoc name package-archive-contents)) | ||
| 108 | (package-desc-create :name name :kind 'vc)) | ||
| 109 | spec))))))) | ||
| 110 | |||
| 111 | |||
| 112 | (defcustom package-vc-selected-packages nil | ||
| 113 | "List of packages to install from their VCS repositories. | ||
| 114 | Each element is of the form (NAME . SPEC), where NAME is a symbol | ||
| 115 | designating the package and SPEC is one of: | ||
| 116 | |||
| 117 | - nil, if any package version can be installed; | ||
| 118 | - a version string, if that specific revision is to be installed; | ||
| 119 | - a property list, describing a package specification. For possible | ||
| 120 | values, see the subsection \"Specifying Package Sources\" in the | ||
| 121 | Info node `(emacs)Fetching Package Sources'. | ||
| 122 | |||
| 123 | The command `package-vc-install' updates the value of this user | ||
| 124 | option to store package specifications for packages that are not | ||
| 125 | specified in any archive." | ||
| 126 | :type '(alist :tag "List of packages you want to be installed" | ||
| 127 | :key-type (symbol :tag "Package") | ||
| 128 | :value-type | ||
| 129 | (choice (const :tag "Any revision" nil) | ||
| 130 | (string :tag "Specific revision") | ||
| 131 | (plist :options ((:url string) | ||
| 132 | (:branch string) | ||
| 133 | (:lisp-dir string) | ||
| 134 | (:main-file string) | ||
| 135 | (:doc string) | ||
| 136 | (:vc-backend symbol))))) | ||
| 137 | :version "29.1") | ||
| 138 | |||
| 139 | (defvar package-vc--archive-spec-alists nil | ||
| 140 | "List of package specifications for each archive. | ||
| 141 | The list maps each package name, as a string, to a plist as | ||
| 142 | specified in `package-vc-selected-packages'.") | ||
| 143 | |||
| 144 | (defvar package-vc--archive-data-alist nil | ||
| 145 | "List of package specification metadata for archives. | ||
| 146 | Each element of the list has the form (ARCHIVE . PLIST), where | ||
| 147 | PLIST keys are one of: | ||
| 148 | |||
| 149 | `:version' (integer) | ||
| 150 | Indicates the version of the file formatting, to be compared | ||
| 151 | with `package-vc--elpa-packages-version'. | ||
| 152 | |||
| 153 | `:vc-backend' (symbol) | ||
| 154 | A symbol of the default VC backend to use if a package specification | ||
| 155 | does not indicate a backend. The value ought to be a member of | ||
| 156 | `vc-handled-backends'. If omitted, `vc-clone' will fall back on | ||
| 157 | `package-vc-default-backend'. | ||
| 158 | |||
| 159 | All other values are ignored.") | ||
| 160 | |||
| 161 | (defun package-vc--desc->spec (pkg-desc &optional name) | ||
| 162 | "Retrieve the package specification for PKG-DESC. | ||
| 163 | The optional argument NAME can be used to override the default | ||
| 164 | name for PKG-DESC." | ||
| 165 | (alist-get | ||
| 166 | (setq name (or name (package-desc-name pkg-desc))) | ||
| 167 | (if (and (package-desc-archive pkg-desc) | ||
| 168 | (not (alist-get name package-vc-selected-packages | ||
| 169 | nil nil #'string=))) | ||
| 170 | (alist-get (intern (package-desc-archive pkg-desc)) | ||
| 171 | package-vc--archive-spec-alists) | ||
| 172 | ;; Consult both our local list of package specifications, as well | ||
| 173 | ;; as the lists provided by the archives. | ||
| 174 | (apply #'append (cons package-vc-selected-packages | ||
| 175 | (mapcar #'cdr package-vc--archive-spec-alists)))) | ||
| 176 | '() nil #'string=)) | ||
| 177 | |||
| 178 | (defun package-vc--read-archive-data (archive) | ||
| 179 | "Update `package-vc--archive-spec-alists' for ARCHIVE. | ||
| 180 | This function is meant to be used as a hook for `package-read-archive-hook'." | ||
| 181 | (let ((contents-file (expand-file-name | ||
| 182 | (format "archives/%s/elpa-packages.eld" archive) | ||
| 183 | package-user-dir))) | ||
| 184 | (when (file-exists-p contents-file) | ||
| 185 | (with-temp-buffer | ||
| 186 | (let ((coding-system-for-read 'utf-8)) | ||
| 187 | (insert-file-contents contents-file) | ||
| 188 | ;; The response from the server is expected to have the form | ||
| 189 | ;; | ||
| 190 | ;; ((("foo" :url "..." ...) ...) | ||
| 191 | ;; :version 1 | ||
| 192 | ;; :default-vc Git) | ||
| 193 | (let ((spec (read (current-buffer)))) | ||
| 194 | (when (eq package-vc--elpa-packages-version | ||
| 195 | (plist-get (cdr spec) :version)) | ||
| 196 | (setf (alist-get (intern archive) package-vc--archive-spec-alists) | ||
| 197 | (car spec))) | ||
| 198 | (setf (alist-get (intern archive) package-vc--archive-data-alist) | ||
| 199 | (cdr spec)) | ||
| 200 | (when-let* ((default-vc (plist-get (cdr spec) :default-vc)) | ||
| 201 | ((not (memq default-vc vc-handled-backends)))) | ||
| 202 | (warn "Archive `%S' expects missing VC backend %S" | ||
| 203 | archive (plist-get (cdr spec) :default-vc))))))))) | ||
| 204 | |||
| 205 | (defun package-vc--download-and-read-archives (&optional async) | ||
| 206 | "Download specifications of all `package-archives' and read them. | ||
| 207 | Populate `package-vc--archive-spec-alists' with the result. | ||
| 208 | |||
| 209 | If optional argument ASYNC is non-nil, perform the downloads | ||
| 210 | asynchronously." | ||
| 211 | (dolist (archive package-archives) | ||
| 212 | (condition-case err | ||
| 213 | (package--download-one-archive archive "elpa-packages.eld" async) | ||
| 214 | (error (message "Failed to download `%s' archive: %S" (car archive) err))))) | ||
| 215 | |||
| 216 | (add-hook 'package-read-archive-hook #'package-vc--read-archive-data 20) | ||
| 217 | |||
| 218 | (defun package-vc-commit (pkg-desc) | ||
| 219 | "Return the last commit of a development package PKG-DESC." | ||
| 220 | (cl-assert (package-vc-p pkg-desc)) | ||
| 221 | ;; FIXME: vc should be extended to allow querying the commit of a | ||
| 222 | ;; directory (as is possible when dealing with git repositories). | ||
| 223 | ;; This should be a fallback option. | ||
| 224 | (cl-loop with dir = (let ((pkg-spec (package-vc--desc->spec pkg-desc))) | ||
| 225 | (or (plist-get pkg-spec :lisp-dir) | ||
| 226 | (package-desc-dir pkg-desc))) | ||
| 227 | for file in (directory-files dir t "\\.el\\'" t) | ||
| 228 | when (vc-working-revision file) return it | ||
| 229 | finally return "unknown")) | ||
| 230 | |||
| 231 | (defun package-vc--version (pkg) | ||
| 232 | "Return the version number for the VC package PKG." | ||
| 233 | (cl-assert (package-vc-p pkg)) | ||
| 234 | (if-let* ((main-file (package-vc--main-file pkg))) | ||
| 235 | (with-temp-buffer | ||
| 236 | (insert-file-contents main-file) | ||
| 237 | (package-strip-rcs-id | ||
| 238 | (or (lm-header "package-version") | ||
| 239 | (lm-header "version") | ||
| 240 | "0"))) | ||
| 241 | "0")) | ||
| 242 | |||
| 243 | (defun package-vc--main-file (pkg-desc) | ||
| 244 | "Return the name of the main file for PKG-DESC." | ||
| 245 | (cl-assert (package-vc-p pkg-desc)) | ||
| 246 | (let* ((pkg-spec (package-vc--desc->spec pkg-desc)) | ||
| 247 | (name (symbol-name (package-desc-name pkg-desc))) | ||
| 248 | (directory (expand-file-name | ||
| 249 | (or (plist-get pkg-spec :lisp-dir) ".") | ||
| 250 | (or (package-desc-dir pkg-desc) | ||
| 251 | (expand-file-name name package-user-dir)))) | ||
| 252 | (file (expand-file-name | ||
| 253 | (or (plist-get pkg-spec :main-file) | ||
| 254 | (concat name ".el")) | ||
| 255 | directory))) | ||
| 256 | (if (file-exists-p file) file | ||
| 257 | ;; The following heuristic is only necessary when fetching a | ||
| 258 | ;; repository with URL that would break the above assumptions. | ||
| 259 | ;; Concrete example: https://github.com/sachac/waveform-el does | ||
| 260 | ;; not have a file waveform-el.el, but a file waveform.el, so we | ||
| 261 | ;; try and find the closest match. | ||
| 262 | (let ((distance most-positive-fixnum) (best nil)) | ||
| 263 | (dolist (alt (directory-files directory t "\\.el\\'" t)) | ||
| 264 | (let ((sd (string-distance file alt))) | ||
| 265 | (when (and (not (string-match-p (rx (or (: "-autoloads.el") | ||
| 266 | (: "-pkg.el")) | ||
| 267 | eos) | ||
| 268 | alt)) | ||
| 269 | (< sd distance)) | ||
| 270 | (when (< sd distance) | ||
| 271 | (setq distance (string-distance file alt) | ||
| 272 | best alt))))) | ||
| 273 | best)))) | ||
| 274 | |||
| 275 | (defun package-vc--generate-description-file (pkg-desc pkg-file) | ||
| 276 | "Generate a package description file for PKG-DESC and write it to PKG-FILE." | ||
| 277 | (let ((name (package-desc-name pkg-desc))) | ||
| 278 | (when (equal (package-desc-summary pkg-desc) package--default-summary) | ||
| 279 | ;; We unset the package description if it is just the default | ||
| 280 | ;; summary, so that the following heuristic can take effect. | ||
| 281 | (setf (package-desc-summary pkg-desc) nil)) | ||
| 282 | ;; Infer the package description if missing. | ||
| 283 | (unless (package-desc-summary pkg-desc) | ||
| 284 | (setf (package-desc-summary pkg-desc) | ||
| 285 | (let ((main-file (package-vc--main-file pkg-desc))) | ||
| 286 | (or (package-desc-summary pkg-desc) | ||
| 287 | (and-let* ((pkg (cadr (assq name package-archive-contents)))) | ||
| 288 | (package-desc-summary pkg)) | ||
| 289 | (and main-file (file-exists-p main-file) | ||
| 290 | (lm-summary main-file)) | ||
| 291 | package--default-summary)))) | ||
| 292 | (let ((print-level nil) | ||
| 293 | (print-quoted t) | ||
| 294 | (print-length nil)) | ||
| 295 | (write-region | ||
| 296 | (concat | ||
| 297 | ";;; Generated package description from " | ||
| 298 | (replace-regexp-in-string | ||
| 299 | "-pkg\\.el\\'" ".el" | ||
| 300 | (file-name-nondirectory pkg-file)) | ||
| 301 | " -*- no-byte-compile: t -*-\n" | ||
| 302 | (prin1-to-string | ||
| 303 | (nconc | ||
| 304 | (list 'define-package | ||
| 305 | (symbol-name name) | ||
| 306 | (package-vc--version pkg-desc) | ||
| 307 | (package-desc-summary pkg-desc) | ||
| 308 | (let ((requires (package-desc-reqs pkg-desc))) | ||
| 309 | (list 'quote | ||
| 310 | ;; Turn version lists into string form. | ||
| 311 | (mapcar | ||
| 312 | (lambda (elt) | ||
| 313 | (list (car elt) | ||
| 314 | (package-version-join (cadr elt)))) | ||
| 315 | requires)))) | ||
| 316 | (list :kind 'vc) | ||
| 317 | (package--alist-to-plist-args | ||
| 318 | (let ((extras (copy-alist (package-desc-extras pkg-desc)))) | ||
| 319 | (setf (alist-get :commit extras) | ||
| 320 | (package-vc-commit pkg-desc)) | ||
| 321 | extras) | ||
| 322 | ))) | ||
| 323 | "\n") | ||
| 324 | nil pkg-file nil 'silent)))) | ||
| 325 | |||
| 326 | (defcustom package-vc-allow-build-commands nil | ||
| 327 | "Whether to run extra build commands when installing VC packages. | ||
| 328 | |||
| 329 | Some packages specify \"make\" targets or other shell commands | ||
| 330 | that should run prior to building the package, by including the | ||
| 331 | :make or :shell-command keywords in their specification. By | ||
| 332 | default, Emacs ignores these keywords when installing and | ||
| 333 | upgrading VC packages, but if the value is a list of package | ||
| 334 | names (symbols), the build commands will be run for those | ||
| 335 | packages. If the value is t, always respect :make and | ||
| 336 | :shell-command keywords. | ||
| 337 | |||
| 338 | It may be necessary to run :make and :shell-command arguments in | ||
| 339 | order to initialize a package or build its documentation, but | ||
| 340 | please be careful when changing this option, as installing and | ||
| 341 | updating a package can run potentially harmful code. | ||
| 342 | |||
| 343 | This applies to package specifications that come from your | ||
| 344 | configured package archives, as well as from entries in | ||
| 345 | `package-vc-selected-packages' and specifications that you give | ||
| 346 | to `package-vc-install' directly." | ||
| 347 | :type '(choice (const :tag "Run for all packages" t) | ||
| 348 | (repeat :tag "Run only for selected packages" (symbol :tag "Package name")) | ||
| 349 | (const :tag "Never run" nil)) | ||
| 350 | :version "30.1") | ||
| 351 | |||
| 352 | (defun package-vc--make (pkg-spec pkg-desc) | ||
| 353 | "Process :make and :shell-command in PKG-SPEC. | ||
| 354 | PKG-DESC is the package descriptor for the package that is being | ||
| 355 | prepared." | ||
| 356 | (let ((target (plist-get pkg-spec :make)) | ||
| 357 | (cmd (plist-get pkg-spec :shell-command)) | ||
| 358 | (buf (format " *package-vc make %s*" (package-desc-name pkg-desc)))) | ||
| 359 | (when (or cmd target) | ||
| 360 | (with-current-buffer (get-buffer-create buf) | ||
| 361 | (erase-buffer) | ||
| 362 | (when (and cmd (/= 0 (call-process shell-file-name nil t nil shell-command-switch cmd))) | ||
| 363 | (warn "Failed to run %s, see buffer %S" cmd (buffer-name))) | ||
| 364 | (when (and target (/= 0 (apply #'call-process "make" nil t nil (if (consp target) target (list target))))) | ||
| 365 | (warn "Failed to make %s, see buffer %S" target (buffer-name))))))) | ||
| 366 | |||
| 367 | (declare-function org-export-to-file "ox" (backend file)) | ||
| 368 | |||
| 369 | (defun package-vc--build-documentation (pkg-desc file) | ||
| 370 | "Build documentation for package PKG-DESC from documentation source in FILE. | ||
| 371 | FILE can be an Org file, indicated by its \".org\" extension, | ||
| 372 | otherwise it's assumed to be an Info file." | ||
| 373 | (let* ((pkg-name (package-desc-name pkg-desc)) | ||
| 374 | (default-directory (package-desc-dir pkg-desc)) | ||
| 375 | (docs-directory (file-name-directory (expand-file-name file))) | ||
| 376 | (output (expand-file-name (format "%s.info" pkg-name))) | ||
| 377 | (log-buffer (get-buffer-create (format " *package-vc doc: %s*" pkg-name))) | ||
| 378 | clean-up) | ||
| 379 | (with-current-buffer log-buffer | ||
| 380 | (erase-buffer)) | ||
| 381 | (condition-case err | ||
| 382 | (progn | ||
| 383 | (when (string-match-p "\\.org\\'" file) | ||
| 384 | (require 'ox) | ||
| 385 | (require 'ox-texinfo) | ||
| 386 | (with-temp-buffer | ||
| 387 | (insert-file-contents file) | ||
| 388 | (setq file (make-temp-file "ox-texinfo-")) | ||
| 389 | (let ((default-directory docs-directory)) | ||
| 390 | (org-export-to-file 'texinfo file)) | ||
| 391 | (setq clean-up t))) | ||
| 392 | (cond | ||
| 393 | ((/= 0 (call-process "makeinfo" nil log-buffer nil | ||
| 394 | "-I" docs-directory | ||
| 395 | "--no-split" file | ||
| 396 | "-o" output)) | ||
| 397 | (message "Failed to build manual %s, see buffer %S" | ||
| 398 | file (buffer-name))) | ||
| 399 | ((/= 0 (call-process "install-info" nil log-buffer nil | ||
| 400 | output (expand-file-name "dir"))) | ||
| 401 | (message "Failed to install manual %s, see buffer %S" | ||
| 402 | output (buffer-name))) | ||
| 403 | ((kill-buffer log-buffer)))) | ||
| 404 | (error (with-current-buffer log-buffer | ||
| 405 | (insert (error-message-string err))) | ||
| 406 | (message "Failed to export org manual for %s, see buffer %S" pkg-name log-buffer))) | ||
| 407 | (when clean-up | ||
| 408 | (delete-file file)))) | ||
| 409 | |||
| 410 | (defun package-vc-install-dependencies (deps) | ||
| 411 | "Install missing dependencies according to DEPS. | ||
| 412 | |||
| 413 | DEPS is a list of elements (PACKAGE VERSION-LIST), where | ||
| 414 | PACKAGE is a package name and VERSION-LIST is the required | ||
| 415 | version of that package. | ||
| 416 | |||
| 417 | Return a list of dependencies that couldn't be met (or nil, when | ||
| 418 | this function successfully installs all given dependencies)." | ||
| 419 | (let ((to-install '()) (missing '())) | ||
| 420 | (cl-labels ((search (pkg) | ||
| 421 | "Attempt to find all dependencies for PKG." | ||
| 422 | (cond | ||
| 423 | ((assq (car pkg) to-install)) ;inhibit cycles | ||
| 424 | ((package-installed-p (car pkg) (cadr pkg))) | ||
| 425 | ((let* ((pac package-archive-contents) | ||
| 426 | (desc (cadr (assoc (car pkg) pac)))) | ||
| 427 | (if desc | ||
| 428 | (let ((reqs (package-desc-reqs desc))) | ||
| 429 | (push desc to-install) | ||
| 430 | (mapc #'search reqs)) | ||
| 431 | (push pkg missing)))))) | ||
| 432 | (version-order (a b) | ||
| 433 | "Predicate to sort packages in order." | ||
| 434 | (version-list-< | ||
| 435 | (package-desc-version b) | ||
| 436 | (package-desc-version a))) | ||
| 437 | (duplicate-p (a b) | ||
| 438 | "Are A and B the same package?" | ||
| 439 | (eq (package-desc-name a) (package-desc-name b))) | ||
| 440 | (depends-on-p (target package) | ||
| 441 | "Does PACKAGE depend on TARGET?" | ||
| 442 | (or (eq target package) | ||
| 443 | (let* ((pac package-archive-contents) | ||
| 444 | (desc (cadr (assoc package pac)))) | ||
| 445 | (and desc (seq-some | ||
| 446 | (apply-partially #'depends-on-p target) | ||
| 447 | (mapcar #'car (package-desc-reqs desc))))))) | ||
| 448 | (dependent-order (a b) | ||
| 449 | (let ((desc-a (package-desc-name a)) | ||
| 450 | (desc-b (package-desc-name b))) | ||
| 451 | (depends-on-p desc-a desc-b)))) | ||
| 452 | (mapc #'search deps) | ||
| 453 | (cl-callf sort to-install #'version-order) | ||
| 454 | (cl-callf seq-uniq to-install #'duplicate-p) | ||
| 455 | (cl-callf sort to-install #'dependent-order)) | ||
| 456 | (mapc #'package-install-from-archive to-install) | ||
| 457 | missing)) | ||
| 458 | |||
| 459 | (defun package-vc--unpack-1 (pkg-desc pkg-dir) | ||
| 460 | "Prepare PKG-DESC that is already checked-out in PKG-DIR. | ||
| 461 | This includes downloading missing dependencies, generating | ||
| 462 | autoloads, generating a package description file (used to | ||
| 463 | identify a package as a VC package later on), building | ||
| 464 | documentation and marking the package as installed." | ||
| 465 | (let* ((pkg-spec (package-vc--desc->spec pkg-desc)) | ||
| 466 | (lisp-dir (plist-get pkg-spec :lisp-dir)) | ||
| 467 | (lisp-path (expand-file-name (or lisp-dir ".") pkg-dir)) | ||
| 468 | missing) | ||
| 469 | |||
| 470 | ;; In case the package was installed directly from source, the | ||
| 471 | ;; dependency list wasn't know beforehand, and they might have | ||
| 472 | ;; to be installed explicitly. | ||
| 473 | (let ((ignored-files | ||
| 474 | (if (plist-get pkg-spec :ignored-files) | ||
| 475 | (mapconcat | ||
| 476 | (lambda (ignore) | ||
| 477 | (wildcard-to-regexp | ||
| 478 | (if (string-match-p "\\`/" ignore) | ||
| 479 | (concat pkg-dir ignore) | ||
| 480 | (concat "*/" ignore)))) | ||
| 481 | (plist-get pkg-spec :ignored-files) | ||
| 482 | "\\|") | ||
| 483 | regexp-unmatchable)) | ||
| 484 | (deps '())) | ||
| 485 | (dolist (file (directory-files lisp-path t "\\.el\\'" t)) | ||
| 486 | (unless (string-match-p ignored-files file) | ||
| 487 | (with-temp-buffer | ||
| 488 | (insert-file-contents file) | ||
| 489 | (when-let* ((require-lines (lm-header-multiline "package-requires"))) | ||
| 490 | (setq deps | ||
| 491 | (nconc deps | ||
| 492 | (lm--prepare-package-dependencies | ||
| 493 | (package-read-from-string | ||
| 494 | (mapconcat (function identity) | ||
| 495 | require-lines " "))))))))) | ||
| 496 | (dolist (dep deps) | ||
| 497 | (cl-callf version-to-list (cadr dep))) | ||
| 498 | (setf (package-desc-reqs pkg-desc) deps) | ||
| 499 | (setf missing (package-vc-install-dependencies (delete-dups deps))) | ||
| 500 | (setf missing (delq (assq (package-desc-name pkg-desc) | ||
| 501 | missing) | ||
| 502 | missing))) | ||
| 503 | |||
| 504 | (let ((default-directory (file-name-as-directory pkg-dir)) | ||
| 505 | (pkg-file (expand-file-name (package--description-file pkg-dir) pkg-dir))) | ||
| 506 | ;; Generate autoloads | ||
| 507 | (let* ((name (package-desc-name pkg-desc)) | ||
| 508 | (auto-name (format "%s-autoloads.el" name))) | ||
| 509 | (package-generate-autoloads name lisp-path) | ||
| 510 | (when lisp-dir | ||
| 511 | (write-region | ||
| 512 | (with-temp-buffer | ||
| 513 | (insert ";; Autoload indirection for package-vc\n\n") | ||
| 514 | (prin1 `(load (expand-file-name | ||
| 515 | ,(expand-file-name auto-name lisp-dir) | ||
| 516 | (or (and load-file-name | ||
| 517 | (file-name-directory load-file-name)) | ||
| 518 | (car load-path)))) | ||
| 519 | (current-buffer)) | ||
| 520 | (buffer-string)) | ||
| 521 | nil (expand-file-name auto-name pkg-dir)))) | ||
| 522 | |||
| 523 | ;; Generate package file | ||
| 524 | (package-vc--generate-description-file pkg-desc pkg-file) | ||
| 525 | |||
| 526 | ;; Process :make and :shell-command arguments before building documentation | ||
| 527 | (when (or (eq package-vc-allow-build-commands t) | ||
| 528 | (memq (package-desc-name pkg-desc) | ||
| 529 | package-vc-allow-build-commands)) | ||
| 530 | (package-vc--make pkg-spec pkg-desc)) | ||
| 531 | |||
| 532 | ;; Detect a manual | ||
| 533 | (when (executable-find "install-info") | ||
| 534 | (dolist (doc-file (ensure-list (plist-get pkg-spec :doc))) | ||
| 535 | (package-vc--build-documentation pkg-desc doc-file)))) | ||
| 536 | |||
| 537 | ;; Remove any previous instance of PKG-DESC from `package-alist' | ||
| 538 | (let ((pkgs (assq (package-desc-name pkg-desc) package-alist))) | ||
| 539 | (when pkgs | ||
| 540 | (setf (cdr pkgs) (seq-remove #'package-vc-p (cdr pkgs))))) | ||
| 541 | |||
| 542 | ;; Update package-alist. | ||
| 543 | (let ((new-desc (package-load-descriptor pkg-dir))) | ||
| 544 | ;; Activation has to be done before compilation, so that if we're | ||
| 545 | ;; upgrading and macros have changed we load the new definitions | ||
| 546 | ;; before compiling. | ||
| 547 | (when (package-activate-1 new-desc :reload :deps) | ||
| 548 | ;; FIXME: Compilation should be done as a separate, optional, step. | ||
| 549 | ;; E.g. for multi-package installs, we should first install all packages | ||
| 550 | ;; and then compile them. | ||
| 551 | (package--compile | ||
| 552 | (if lisp-dir | ||
| 553 | ;; In case we are installing a package from a local | ||
| 554 | ;; checkout, we want to compile the checkout, not the | ||
| 555 | ;; redirection! | ||
| 556 | (package-desc-create :dir lisp-dir) | ||
| 557 | new-desc)) | ||
| 558 | |||
| 559 | (when package-native-compile | ||
| 560 | (package--native-compile-async new-desc)) | ||
| 561 | ;; After compilation, load again any files loaded by | ||
| 562 | ;; `activate-1', so that we use the byte-compiled definitions. | ||
| 563 | (package--reload-previously-loaded new-desc))) | ||
| 564 | |||
| 565 | ;; Mark package as selected | ||
| 566 | (let ((name (package-desc-name pkg-desc))) | ||
| 567 | (unless (memq name package-selected-packages) | ||
| 568 | (package--save-selected-packages | ||
| 569 | (cons name package-selected-packages)))) | ||
| 570 | |||
| 571 | (package--quickstart-maybe-refresh) | ||
| 572 | |||
| 573 | ;; Confirm that the installation was successful | ||
| 574 | (let ((main-file (package-vc--main-file pkg-desc))) | ||
| 575 | (message "VC package `%s' installed (Version %s, Revision %S).%s" | ||
| 576 | (package-desc-name pkg-desc) | ||
| 577 | (lm-with-file main-file | ||
| 578 | (package-strip-rcs-id | ||
| 579 | (or (lm-header "package-version") | ||
| 580 | (lm-header "version")))) | ||
| 581 | (vc-working-revision main-file) | ||
| 582 | (if missing | ||
| 583 | (format | ||
| 584 | " Failed to install the following dependencies: %s" | ||
| 585 | (mapconcat | ||
| 586 | (lambda (p) | ||
| 587 | (format "%s (%s)" (car p) (cadr p))) | ||
| 588 | missing ", ")) | ||
| 589 | ""))) | ||
| 590 | t)) | ||
| 591 | |||
| 592 | (declare-function project-remember-projects-under "project" (dir &optional recursive)) | ||
| 593 | |||
| 594 | (defun package-vc--clone (pkg-desc pkg-spec dir rev) | ||
| 595 | "Clone the package PKG-DESC whose spec is PKG-SPEC into the directory DIR. | ||
| 596 | REV specifies a specific revision to checkout. This overrides the `:branch' | ||
| 597 | attribute in PKG-SPEC." | ||
| 598 | (pcase-let* ((name (package-desc-name pkg-desc)) | ||
| 599 | ((map :url :branch) pkg-spec)) | ||
| 600 | |||
| 601 | ;; Clone the repository into `repo-dir' if necessary | ||
| 602 | (unless (file-exists-p dir) | ||
| 603 | (make-directory (file-name-directory dir) t) | ||
| 604 | (let ((backend (or (plist-get pkg-spec :vc-backend) | ||
| 605 | (vc-guess-url-backend url) | ||
| 606 | (plist-get (alist-get (package-desc-archive pkg-desc) | ||
| 607 | package-vc--archive-data-alist | ||
| 608 | nil nil #'string=) | ||
| 609 | :vc-backend) | ||
| 610 | package-vc-default-backend))) | ||
| 611 | (unless (vc-clone url backend dir | ||
| 612 | (or (and (not (eq rev :last-release)) rev) branch)) | ||
| 613 | (error "Failed to clone %s from %s" name url)))) | ||
| 614 | |||
| 615 | (when package-vc-register-as-project | ||
| 616 | (let ((default-directory dir)) | ||
| 617 | (require 'project) | ||
| 618 | (project-remember-projects-under dir))) | ||
| 619 | |||
| 620 | ;; Check out the latest release if requested | ||
| 621 | (when (eq rev :last-release) | ||
| 622 | (if-let* ((release-rev (package-vc--release-rev pkg-desc))) | ||
| 623 | (vc-retrieve-tag dir release-rev) | ||
| 624 | (message "No release revision was found, continuing..."))))) | ||
| 625 | |||
| 626 | (defvar package-vc-non-code-file-names | ||
| 627 | '(".dir-locals.el" ".dir-locals-2.el") | ||
| 628 | "List of file names that do not contain Emacs Lisp code. | ||
| 629 | This list is used by `package-vc--unpack' to better check if the | ||
| 630 | user is fetching code from a repository that does not contain any | ||
| 631 | Emacs Lisp files.") | ||
| 632 | |||
| 633 | (defun package-vc--unpack (pkg-desc pkg-spec &optional rev) | ||
| 634 | "Install the package described by PKG-DESC. | ||
| 635 | PKG-SPEC is a package specification, a property list describing | ||
| 636 | how to fetch and build the package. See `package-vc--archive-spec-alists' | ||
| 637 | for details. The optional argument REV specifies a specific revision to | ||
| 638 | checkout. This overrides the `:branch' attribute in PKG-SPEC." | ||
| 639 | (unless (eq (package-desc-kind pkg-desc) 'vc) | ||
| 640 | (let ((copy (copy-package-desc pkg-desc))) | ||
| 641 | (setf (package-desc-kind copy) 'vc | ||
| 642 | pkg-desc copy))) | ||
| 643 | (pcase-let* (((map :lisp-dir) pkg-spec) | ||
| 644 | (name (package-desc-name pkg-desc)) | ||
| 645 | (dirname (package-desc-full-name pkg-desc)) | ||
| 646 | (pkg-dir (file-name-as-directory (expand-file-name dirname package-user-dir)))) | ||
| 647 | (when (string-empty-p name) | ||
| 648 | (user-error "Empty package name")) | ||
| 649 | (setf (package-desc-dir pkg-desc) pkg-dir) | ||
| 650 | (when (file-exists-p pkg-dir) | ||
| 651 | (if (yes-or-no-p (format "Overwrite previous checkout for package `%s'?" name)) | ||
| 652 | (package--delete-directory pkg-dir) | ||
| 653 | (error "There already exists a checkout for %s" name))) | ||
| 654 | (package-vc--clone pkg-desc pkg-spec pkg-dir rev) | ||
| 655 | (when (directory-empty-p pkg-dir) | ||
| 656 | (delete-directory pkg-dir) | ||
| 657 | (error "Empty checkout for %s" name)) | ||
| 658 | (unless (seq-remove | ||
| 659 | (lambda (file) | ||
| 660 | (member (file-name-nondirectory file) package-vc-non-code-file-names)) | ||
| 661 | (directory-files-recursively pkg-dir "\\.el\\'" nil)) | ||
| 662 | (when (yes-or-no-p (format "No Emacs Lisp files found when fetching \"%s\", \ | ||
| 663 | abort installation?" name)) | ||
| 664 | (delete-directory pkg-dir t) | ||
| 665 | (user-error "Installation aborted"))) | ||
| 666 | |||
| 667 | ;; When nothing is specified about a `lisp-dir', then should | ||
| 668 | ;; heuristically check if there is a sub-directory with lisp | ||
| 669 | ;; files. These are conventionally just called "lisp" or "src". | ||
| 670 | ;; If this directory exists and contains non-zero number of lisp | ||
| 671 | ;; files, we will use that instead of `pkg-dir'. | ||
| 672 | (catch 'done | ||
| 673 | (dolist (name '("lisp" "src")) | ||
| 674 | (when-let* (((null lisp-dir)) | ||
| 675 | (dir (expand-file-name name pkg-dir)) | ||
| 676 | ((file-directory-p dir)) | ||
| 677 | ((directory-files dir nil "\\`[^.].+\\.el\\'" t 1))) | ||
| 678 | ;; We won't use `dir', since dir is an absolute path and we | ||
| 679 | ;; don't want `lisp-dir' to depend on the current location of | ||
| 680 | ;; the package installation, ie. to break if moved around the | ||
| 681 | ;; file system or between installations. | ||
| 682 | (throw 'done (setq lisp-dir name))))) | ||
| 683 | |||
| 684 | ;; Ensure we have a copy of the package specification | ||
| 685 | (unless (seq-some (lambda (alist) (equal (alist-get name (cdr alist)) pkg-spec)) | ||
| 686 | package-vc--archive-spec-alists) | ||
| 687 | (customize-save-variable | ||
| 688 | 'package-vc-selected-packages | ||
| 689 | (cons (cons name pkg-spec) | ||
| 690 | (seq-remove (lambda (spec) (string= name (car spec))) | ||
| 691 | package-vc-selected-packages)))) | ||
| 692 | |||
| 693 | (package-vc--unpack-1 pkg-desc pkg-dir))) | ||
| 694 | |||
| 695 | (defun package-vc--read-package-name (prompt &optional allow-url installed) | ||
| 696 | "Query the user for a VC package and return a name with PROMPT. | ||
| 697 | If the optional argument ALLOW-URL is non-nil, the user is also | ||
| 698 | allowed to specify a non-package name. If the optional argument | ||
| 699 | INSTALLED is non-nil, the selection will be filtered down to | ||
| 700 | VC packages that have already been installed." | ||
| 701 | (package-vc--archives-initialize) | ||
| 702 | (completing-read prompt (if installed package-alist package-archive-contents) | ||
| 703 | (if installed | ||
| 704 | (lambda (pkg) (package-vc-p (cadr pkg))) | ||
| 705 | (lambda (pkg) | ||
| 706 | (or (package-vc--desc->spec (cadr pkg)) | ||
| 707 | ;; If we have no explicit VC data, we can try a kind of | ||
| 708 | ;; heuristic and use the URL header, that might already be | ||
| 709 | ;; pointing towards a repository, and use that as a backup | ||
| 710 | (and-let* ((extras (package-desc-extras (cadr pkg))) | ||
| 711 | (url (alist-get :url extras)) | ||
| 712 | ((vc-guess-url-backend url))))))) | ||
| 713 | (not allow-url))) | ||
| 714 | |||
| 715 | (defun package-vc--read-package-desc (prompt &optional installed) | ||
| 716 | "Query the user for a VC package and return a description with PROMPT. | ||
| 717 | If the optional argument INSTALLED is non-nil, the selection will | ||
| 718 | be filtered down to VC packages that have already been | ||
| 719 | installed, and the package description will be that of an | ||
| 720 | installed package." | ||
| 721 | (cadr (assoc (package-vc--read-package-name prompt nil installed) | ||
| 722 | (if installed package-alist package-archive-contents) | ||
| 723 | #'string=))) | ||
| 724 | |||
| 725 | ;;;###autoload | ||
| 726 | (defun package-vc-upgrade-all () | ||
| 727 | "Upgrade all installed VC packages. | ||
| 728 | |||
| 729 | This may fail if the local VCS state of one of the packages | ||
| 730 | conflicts with its remote repository state." | ||
| 731 | (interactive) | ||
| 732 | (dolist (package package-alist) | ||
| 733 | (dolist (pkg-desc (cdr package)) | ||
| 734 | (when (package-vc-p pkg-desc) | ||
| 735 | (package-vc-upgrade pkg-desc)))) | ||
| 736 | (message "Done upgrading packages.")) | ||
| 737 | |||
| 738 | (declare-function vc-dir-prepare-status-buffer "vc-dir" | ||
| 739 | (bname dir backend &optional create-new)) | ||
| 740 | |||
| 741 | ;;;###autoload | ||
| 742 | (defun package-vc-upgrade (pkg-desc) | ||
| 743 | "Upgrade the package described by PKG-DESC from package's VC repository. | ||
| 744 | |||
| 745 | This may fail if the local VCS state of the package conflicts | ||
| 746 | with the remote repository state." | ||
| 747 | (interactive (list (package-vc--read-package-desc "Upgrade VC package: " t))) | ||
| 748 | ;; HACK: To run `package-vc--unpack-1' after checking out the new | ||
| 749 | ;; revision, we insert a hook into `vc-post-command-functions', and | ||
| 750 | ;; remove it right after it ran. To avoid running the hook multiple | ||
| 751 | ;; times or even for the wrong repository (as `vc-pull' is often | ||
| 752 | ;; asynchronous), we extract the relevant arguments using a pseudo | ||
| 753 | ;; filter for `vc-filter-command-function', executed only for the | ||
| 754 | ;; side effect, and store them in the lexical scope. When the hook | ||
| 755 | ;; is run, we check if the arguments are the same (`eq') as the ones | ||
| 756 | ;; previously extracted, and only in that case will be call | ||
| 757 | ;; `package-vc--unpack-1'. Ugh... | ||
| 758 | ;; | ||
| 759 | ;; If there is a better way to do this, it should be done. | ||
| 760 | (cl-assert (package-vc-p pkg-desc)) | ||
| 761 | (letrec ((pkg-dir (package-desc-dir pkg-desc)) | ||
| 762 | (vc-flags) | ||
| 763 | (vc-filter-command-function | ||
| 764 | (lambda (command file-or-list flags) | ||
| 765 | (setq vc-flags flags) | ||
| 766 | (list command file-or-list flags))) | ||
| 767 | (post-upgrade | ||
| 768 | (lambda (_command _file-or-list flags) | ||
| 769 | (when (and (file-equal-p pkg-dir default-directory) | ||
| 770 | (eq flags vc-flags)) | ||
| 771 | (unwind-protect | ||
| 772 | (with-demoted-errors "Failed to activate: %S" | ||
| 773 | (package-vc--unpack-1 pkg-desc pkg-dir)) | ||
| 774 | (remove-hook 'vc-post-command-functions post-upgrade)))))) | ||
| 775 | (add-hook 'vc-post-command-functions post-upgrade) | ||
| 776 | (with-demoted-errors "Failed to fetch: %S" | ||
| 777 | (require 'vc-dir) | ||
| 778 | (with-current-buffer (vc-dir-prepare-status-buffer | ||
| 779 | (format " *package-vc-dir: %s*" pkg-dir) | ||
| 780 | pkg-dir (vc-responsible-backend pkg-dir)) | ||
| 781 | (vc-pull))))) | ||
| 782 | |||
| 783 | (defun package-vc--archives-initialize () | ||
| 784 | "Initialize package.el and fetch package specifications." | ||
| 785 | (package--archives-initialize) | ||
| 786 | (unless package-vc--archive-data-alist | ||
| 787 | (package-vc--download-and-read-archives))) | ||
| 788 | |||
| 789 | (defun package-vc--release-rev (pkg-desc) | ||
| 790 | "Return the latest revision that bumps the \"Version\" tag for PKG-DESC. | ||
| 791 | If no such revision can be found, return nil." | ||
| 792 | (with-current-buffer (find-file-noselect (package-vc--main-file pkg-desc)) | ||
| 793 | (vc-buffer-sync) | ||
| 794 | (save-excursion | ||
| 795 | (goto-char (point-min)) | ||
| 796 | (let ((case-fold-search t)) | ||
| 797 | (when (cond | ||
| 798 | ((re-search-forward | ||
| 799 | (concat (lm-get-header-re "package-version") ".*$") | ||
| 800 | (lm-code-start) t)) | ||
| 801 | ((re-search-forward | ||
| 802 | (concat (lm-get-header-re "version") ".*$") | ||
| 803 | (lm-code-start) t))) | ||
| 804 | (ignore-error vc-not-supported | ||
| 805 | (vc-call-backend (vc-backend (buffer-file-name)) | ||
| 806 | 'last-change | ||
| 807 | (buffer-file-name) | ||
| 808 | (line-number-at-pos nil t)))))))) | ||
| 809 | |||
| 810 | ;;;###autoload | ||
| 811 | (defun package-vc-install (package &optional rev backend name) | ||
| 812 | "Fetch a package described by PACKAGE and set it up for use with Emacs. | ||
| 813 | |||
| 814 | PACKAGE specifies which package to install, where to find its | ||
| 815 | source repository and how to build it. | ||
| 816 | |||
| 817 | If PACKAGE is a symbol, install the package with that name | ||
| 818 | according to metadata that package archives provide for it. This | ||
| 819 | is the simplest way to call this function, but it only works if | ||
| 820 | the package you want to install is listed in a package archive | ||
| 821 | you have configured. | ||
| 822 | |||
| 823 | If PACKAGE is a string, it specifies the URL of the package | ||
| 824 | repository. In this case, optional argument BACKEND specifies | ||
| 825 | the VC backend to use for cloning the repository; if it's nil, | ||
| 826 | this function tries to infer which backend to use according to | ||
| 827 | the value of `vc-clone-heuristic-alist' and if that fails it | ||
| 828 | uses `package-vc-default-backend'. Optional argument NAME | ||
| 829 | specifies the package name in this case; if it's nil, this | ||
| 830 | package uses `file-name-base' on the URL to obtain the package | ||
| 831 | name, otherwise NAME is the package name as a symbol. | ||
| 832 | |||
| 833 | PACKAGE can also be a cons cell (PNAME . SPEC) where PNAME is the | ||
| 834 | package name as a symbol, and SPEC is a plist that specifies how | ||
| 835 | to fetch and build the package. For possible values, see the | ||
| 836 | subsection \"Specifying Package Sources\" in the Info | ||
| 837 | node `(emacs)Fetching Package Sources'. | ||
| 838 | |||
| 839 | By default, this function installs the last revision of the | ||
| 840 | package available from its repository. If REV is a string, it | ||
| 841 | describes the revision to install, as interpreted by the relevant | ||
| 842 | VC backend. The special value `:last-release' (interactively, | ||
| 843 | the prefix argument), says to use the commit of the latest | ||
| 844 | release, if it exists. The last release is the latest revision | ||
| 845 | which changed the \"Version:\" header of the package's main Lisp | ||
| 846 | file. | ||
| 847 | |||
| 848 | If you use this function to install a package that you also have | ||
| 849 | installed from a package archive, the version this function | ||
| 850 | installs takes precedence." | ||
| 851 | (interactive | ||
| 852 | (progn | ||
| 853 | ;; Initialize the package system to get the list of package | ||
| 854 | ;; symbols for completion. | ||
| 855 | (package-vc--archives-initialize) | ||
| 856 | (let* ((name-or-url (package-vc--read-package-name | ||
| 857 | "Fetch and install package: " t)) | ||
| 858 | (name (file-name-base (directory-file-name name-or-url)))) | ||
| 859 | (when (string-empty-p name) | ||
| 860 | (user-error "Empty package name")) | ||
| 861 | (list name-or-url | ||
| 862 | (and current-prefix-arg :last-release) | ||
| 863 | nil | ||
| 864 | (intern (string-remove-prefix "emacs-" name)))))) | ||
| 865 | (package-vc--archives-initialize) | ||
| 866 | (cond | ||
| 867 | ((null package) | ||
| 868 | (signal 'wrong-type-argument nil)) | ||
| 869 | ((consp package) | ||
| 870 | (package-vc--unpack | ||
| 871 | (package-desc-create :name (car package) | ||
| 872 | :kind 'vc) | ||
| 873 | (cdr package) | ||
| 874 | rev)) | ||
| 875 | ((and-let* (((stringp package)) | ||
| 876 | (backend (or backend (vc-guess-url-backend package)))) | ||
| 877 | (package-vc--unpack | ||
| 878 | (package-desc-create | ||
| 879 | :name (or name (intern (file-name-base package))) | ||
| 880 | :kind 'vc) | ||
| 881 | (list :vc-backend backend :url package) | ||
| 882 | rev))) | ||
| 883 | ((and-let* ((desc (assoc package package-archive-contents #'string=))) | ||
| 884 | (package-vc--unpack | ||
| 885 | (cadr desc) | ||
| 886 | (or (package-vc--desc->spec (cadr desc)) | ||
| 887 | (and-let* ((extras (package-desc-extras (cadr desc))) | ||
| 888 | (url (alist-get :url extras)) | ||
| 889 | (backend (vc-guess-url-backend url))) | ||
| 890 | (list :vc-backend backend :url url)) | ||
| 891 | (user-error "Package `%s' has no VC data" package)) | ||
| 892 | rev))) | ||
| 893 | ((user-error "Unknown package to fetch: %s" package)))) | ||
| 894 | |||
| 895 | ;;;###autoload | ||
| 896 | (defun package-vc-checkout (pkg-desc directory &optional rev) | ||
| 897 | "Clone the sources for PKG-DESC into DIRECTORY and visit that directory. | ||
| 898 | Unlike `package-vc-install', this does not yet set up the package | ||
| 899 | for use with Emacs; use `package-vc-install-from-checkout' for | ||
| 900 | setting the package up after this function finishes. Optional | ||
| 901 | argument REV means to clone a specific version of the package; it | ||
| 902 | defaults to the last version available from the package's | ||
| 903 | repository. If REV has the special value | ||
| 904 | `:last-release' (interactively, the prefix argument), that stands | ||
| 905 | for the last released version of the package." | ||
| 906 | (interactive | ||
| 907 | (let* ((name (package-vc--read-package-name "Fetch package source: "))) | ||
| 908 | (list (cadr (assoc name package-archive-contents #'string=)) | ||
| 909 | (read-directory-name "Clone into new or empty directory: " nil nil | ||
| 910 | (lambda (dir) (or (not (file-exists-p dir)) | ||
| 911 | (directory-empty-p dir)))) | ||
| 912 | (and current-prefix-arg :last-release)))) | ||
| 913 | (package-vc--archives-initialize) | ||
| 914 | (let ((pkg-spec (or (package-vc--desc->spec pkg-desc) | ||
| 915 | (and-let* ((extras (package-desc-extras pkg-desc)) | ||
| 916 | (url (alist-get :url extras)) | ||
| 917 | (backend (vc-guess-url-backend url))) | ||
| 918 | (list :vc-backend backend :url url)) | ||
| 919 | (user-error "Package `%s' has no VC data" | ||
| 920 | (package-desc-name pkg-desc))))) | ||
| 921 | (package-vc--clone pkg-desc pkg-spec directory rev) | ||
| 922 | (find-file directory))) | ||
| 923 | |||
| 924 | ;;;###autoload | ||
| 925 | (defun package-vc-install-from-checkout (dir &optional name interactive) | ||
| 926 | "Install the package NAME from its source directory DIR. | ||
| 927 | NAME defaults to the base name of DIR. Interactively, prompt the user | ||
| 928 | for DIR, which should be a directory under version control, typically | ||
| 929 | one created by `package-vc-checkout'. If invoked interactively with a | ||
| 930 | prefix argument, prompt the user for the NAME of the package to set up. | ||
| 931 | If the optional argument INTERACTIVE is non-nil (as happens | ||
| 932 | interactively), DIR must be an absolute file name." | ||
| 933 | (interactive (let ((dir (expand-file-name (read-directory-name "Directory: ")))) | ||
| 934 | (list dir (and current-prefix-arg | ||
| 935 | (let ((base (file-name-base | ||
| 936 | (directory-file-name | ||
| 937 | dir)))) | ||
| 938 | (read-string | ||
| 939 | (format-prompt "Package name" base) | ||
| 940 | nil nil base))) | ||
| 941 | :interactive))) | ||
| 942 | (package-vc--archives-initialize) | ||
| 943 | (let* ((dir (if interactive dir (expand-file-name dir))) ;avoid double expansion | ||
| 944 | (name (or name (file-name-base (directory-file-name dir)))) | ||
| 945 | (pkg-dir (expand-file-name name package-user-dir)) | ||
| 946 | (package-vc-selected-packages | ||
| 947 | (cons (list name :lisp-dir dir) | ||
| 948 | package-vc-selected-packages))) | ||
| 949 | (when (file-exists-p pkg-dir) | ||
| 950 | (if (yes-or-no-p (format "Overwrite previous checkout for package `%s'?" name)) | ||
| 951 | (package--delete-directory pkg-dir) | ||
| 952 | (error "There already exists a checkout for %s" name))) | ||
| 953 | (make-directory pkg-dir t) | ||
| 954 | (package-vc--unpack-1 | ||
| 955 | (package-desc-create | ||
| 956 | :name (intern name) | ||
| 957 | :dir pkg-dir | ||
| 958 | :kind 'vc) | ||
| 959 | (file-name-as-directory pkg-dir)))) | ||
| 960 | |||
| 961 | ;;;###autoload | ||
| 962 | (defun package-vc-rebuild (pkg-desc) | ||
| 963 | "Rebuild the installation for package given by PKG-DESC. | ||
| 964 | Rebuilding an installation means scraping for new autoload | ||
| 965 | cookies, re-compiling Emacs Lisp files, building and installing | ||
| 966 | any documentation, downloading any missing dependencies. This | ||
| 967 | command does not fetch new revisions from a remote server. That | ||
| 968 | is the responsibility of `package-vc-upgrade'. Interactively, | ||
| 969 | prompt for the name of the package to rebuild." | ||
| 970 | (interactive (list (package-vc--read-package-desc "Rebuild package: " t))) | ||
| 971 | (package-vc--unpack-1 pkg-desc (package-desc-dir pkg-desc))) | ||
| 972 | |||
| 973 | ;;;###autoload | ||
| 974 | (defun package-vc-prepare-patch (pkg-desc subject revisions) | ||
| 975 | "Email patches for REVISIONS to maintainer of package PKG-DESC using SUBJECT. | ||
| 976 | |||
| 977 | PKG-DESC is a package descriptor and SUBJECT is the subject of | ||
| 978 | the message. | ||
| 979 | |||
| 980 | Interactively, prompt for PKG-DESC, SUBJECT, and REVISIONS. When | ||
| 981 | invoked with a numerical prefix argument, use the last N | ||
| 982 | revisions. When invoked interactively in a Log View buffer with | ||
| 983 | marked revisions, use those. | ||
| 984 | |||
| 985 | See also `vc-prepare-patch'." | ||
| 986 | (interactive | ||
| 987 | (list (package-vc--read-package-desc "Package to prepare a patch for: " t) | ||
| 988 | (and (not vc-prepare-patches-separately) | ||
| 989 | (read-string "Subject: " "[PATCH] " nil nil t)) | ||
| 990 | (vc-prepare-patch-prompt-revisions))) | ||
| 991 | (let ((default-directory (package-desc-dir pkg-desc))) | ||
| 992 | (vc-prepare-patch (package-maintainers pkg-desc t) | ||
| 993 | subject revisions))) | ||
| 994 | |||
| 995 | (defun package-vc-log-incoming (pkg-desc) | ||
| 996 | "Call `vc-log-incoming' for the package PKG-DESC." | ||
| 997 | (interactive | ||
| 998 | (list (package-vc--read-package-desc "Incoming log for package: " t))) | ||
| 999 | (let ((default-directory (package-desc-dir pkg-desc))) | ||
| 1000 | (call-interactively #'vc-log-incoming))) | ||
| 1001 | |||
| 1002 | (provide 'package-vc) | ||
| 1003 | ;;; package-vc.el ends here | ||
diff --git a/lisp/package/package.el b/lisp/package/package.el new file mode 100644 index 00000000000..5705a01c7da --- /dev/null +++ b/lisp/package/package.el | |||
| @@ -0,0 +1,151 @@ | |||
| 1 | ;;; package.el --- Simple package system for Emacs -*- lexical-binding:t -*- | ||
| 2 | |||
| 3 | ;; Copyright (C) 2007-2025 Free Software Foundation, Inc. | ||
| 4 | |||
| 5 | ;; Author: Tom Tromey <tromey@redhat.com> | ||
| 6 | ;; Daniel Hackney <dan@haxney.org> | ||
| 7 | ;; Created: 10 Mar 2007 | ||
| 8 | ;; Version: 1.1.0 | ||
| 9 | ;; Keywords: tools | ||
| 10 | ;; Package-Requires: ((tabulated-list "1.0")) | ||
| 11 | |||
| 12 | ;; This file is part of GNU Emacs. | ||
| 13 | |||
| 14 | ;; GNU Emacs is free software: you can redistribute it and/or modify | ||
| 15 | ;; it under the terms of the GNU General Public License as published by | ||
| 16 | ;; the Free Software Foundation, either version 3 of the License, or | ||
| 17 | ;; (at your option) any later version. | ||
| 18 | |||
| 19 | ;; GNU Emacs is distributed in the hope that it will be useful, | ||
| 20 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 21 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 22 | ;; GNU General Public License for more details. | ||
| 23 | |||
| 24 | ;; You should have received a copy of the GNU General Public License | ||
| 25 | ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>. | ||
| 26 | |||
| 27 | ;;; Commentary: | ||
| 28 | |||
| 29 | ;; The idea behind package.el is to be able to download packages and | ||
| 30 | ;; install them. Packages are versioned and have versioned | ||
| 31 | ;; dependencies. Furthermore, this supports built-in packages which | ||
| 32 | ;; may or may not be newer than user-specified packages. This makes | ||
| 33 | ;; it possible to upgrade Emacs and automatically disable packages | ||
| 34 | ;; which have moved from external to core. (Note though that we don't | ||
| 35 | ;; currently register any of these, so this feature does not actually | ||
| 36 | ;; work.) | ||
| 37 | |||
| 38 | ;; A package is described by its name and version. The distribution | ||
| 39 | ;; format is either a tar file or a single .el file. | ||
| 40 | |||
| 41 | ;; A tar file should be named "NAME-VERSION.tar". The tar file must | ||
| 42 | ;; unpack into a directory named after the package and version: | ||
| 43 | ;; "NAME-VERSION". It must contain a file named "PACKAGE-pkg.el" | ||
| 44 | ;; which consists of a call to define-package. It may also contain a | ||
| 45 | ;; "dir" file and the info files it references. | ||
| 46 | |||
| 47 | ;; A .el file is named "NAME-VERSION.el" in the remote archive, but is | ||
| 48 | ;; installed as simply "NAME.el" in a directory named "NAME-VERSION". | ||
| 49 | |||
| 50 | ;; The downloader downloads all dependent packages. By default, | ||
| 51 | ;; packages come from the official GNU sources, but others may be | ||
| 52 | ;; added by customizing the `package-archives' alist. Packages get | ||
| 53 | ;; byte-compiled at install time. | ||
| 54 | |||
| 55 | ;; At activation time we will set up the load-path and the info path, | ||
| 56 | ;; and we will load the package's autoloads. If a package's | ||
| 57 | ;; dependencies are not available, we will not activate that package. | ||
| 58 | |||
| 59 | ;; Conceptually a package has multiple state transitions: | ||
| 60 | ;; | ||
| 61 | ;; * Download. Fetching the package from ELPA. | ||
| 62 | ;; * Install. Untar the package, or write the .el file, into | ||
| 63 | ;; ~/.emacs.d/elpa/ directory. | ||
| 64 | ;; * Autoload generation. | ||
| 65 | ;; * Byte compile. Currently this phase is done during install, | ||
| 66 | ;; but we may change this. | ||
| 67 | ;; * Activate. Evaluate the autoloads for the package to make it | ||
| 68 | ;; available to the user. | ||
| 69 | ;; * Load. Actually load the package and run some code from it. | ||
| 70 | |||
| 71 | ;; Other external functions you may want to use: | ||
| 72 | ;; | ||
| 73 | ;; M-x list-packages | ||
| 74 | ;; Enters a mode similar to buffer-menu which lets you manage | ||
| 75 | ;; packages. You can choose packages for install (mark with "i", | ||
| 76 | ;; then "x" to execute) or deletion, and you can see what packages | ||
| 77 | ;; are available. This will automatically fetch the latest list of | ||
| 78 | ;; packages from ELPA. | ||
| 79 | ;; | ||
| 80 | ;; M-x package-install-from-buffer | ||
| 81 | ;; Install a package consisting of a single .el file that appears | ||
| 82 | ;; in the current buffer. This only works for packages which | ||
| 83 | ;; define a Version header properly; package.el also supports the | ||
| 84 | ;; extension headers Package-Version (in case Version is an RCS id | ||
| 85 | ;; or similar), and Package-Requires (if the package requires other | ||
| 86 | ;; packages). | ||
| 87 | ;; | ||
| 88 | ;; M-x package-install-file | ||
| 89 | ;; Install a package from the indicated file. The package can be | ||
| 90 | ;; either a tar file or a .el file. A tar file must contain an | ||
| 91 | ;; appropriately-named "-pkg.el" file; a .el file must be properly | ||
| 92 | ;; formatted as with `package-install-from-buffer'. | ||
| 93 | |||
| 94 | ;;; Thanks: | ||
| 95 | ;;; (sorted by sort-lines): | ||
| 96 | |||
| 97 | ;; Jim Blandy <jimb@red-bean.com> | ||
| 98 | ;; Karl Fogel <kfogel@red-bean.com> | ||
| 99 | ;; Kevin Ryde <user42@zip.com.au> | ||
| 100 | ;; Lawrence Mitchell | ||
| 101 | ;; Michael Olson <mwolson@member.fsf.org> | ||
| 102 | ;; Sebastian Tennant <sebyte@smolny.plus.com> | ||
| 103 | ;; Stefan Monnier <monnier@iro.umontreal.ca> | ||
| 104 | ;; Vinicius Jose Latorre <viniciusjl.gnu@gmail.com> | ||
| 105 | ;; Phil Hagelberg <phil@hagelb.org> | ||
| 106 | |||
| 107 | ;;; ToDo: | ||
| 108 | |||
| 109 | ;; - putting info dirs at the start of the info path means | ||
| 110 | ;; users see a weird ordering of categories. OTOH we want to | ||
| 111 | ;; override later entries. maybe emacs needs to enforce | ||
| 112 | ;; the standard layout? | ||
| 113 | ;; - put bytecode in a separate directory tree | ||
| 114 | ;; - perhaps give users a way to recompile their bytecode | ||
| 115 | ;; or do it automatically when emacs changes | ||
| 116 | ;; - give users a way to know whether a package is installed ok | ||
| 117 | ;; - give users a way to view a package's documentation when it | ||
| 118 | ;; only appears in the .el | ||
| 119 | ;; - use/extend checkdoc so people can tell if their package will work | ||
| 120 | ;; - "installed" instead of a blank in the status column | ||
| 121 | ;; - tramp needs its files to be compiled in a certain order. | ||
| 122 | ;; how to handle this? fix tramp? | ||
| 123 | ;; - maybe we need separate .elc directories for various emacs | ||
| 124 | ;; versions. That way conditional compilation can work. But would | ||
| 125 | ;; this break anything? | ||
| 126 | ;; - William Xu suggests being able to open a package file without | ||
| 127 | ;; installing it | ||
| 128 | ;; - Interface with desktop.el so that restarting after an install | ||
| 129 | ;; works properly | ||
| 130 | ;; - Use hierarchical layout. PKG/etc PKG/lisp PKG/info | ||
| 131 | ;; ... except maybe lisp? | ||
| 132 | ;; - It may be nice to have a macro that expands to the package's | ||
| 133 | ;; private data dir, aka ".../etc". Or, maybe data-directory | ||
| 134 | ;; needs to be a list (though this would be less nice) | ||
| 135 | ;; a few packages want this, eg sokoban | ||
| 136 | ;; - Allow multiple versions on the server, so that if a user doesn't | ||
| 137 | ;; meet the requirements for the most recent version they can still | ||
| 138 | ;; install an older one. | ||
| 139 | ;; - Allow optional package dependencies | ||
| 140 | ;; then if we require 'bbdb', bbdb-specific lisp in lisp/bbdb | ||
| 141 | ;; and just don't compile to add to load path ...? | ||
| 142 | ;; - Our treatment of the info path is somewhat bogus | ||
| 143 | |||
| 144 | ;;; Code: | ||
| 145 | |||
| 146 | (require 'package-install) | ||
| 147 | (require 'package-menu) | ||
| 148 | (require 'package-describe) | ||
| 149 | |||
| 150 | (provide 'package) | ||
| 151 | ;;; package.el ends here | ||