Attempt to make defclass documentation more legible
[emacs.git] / lisp / emacs-lisp / package.el
bloba0f1ab0ed673f041dc3e39646186957669015d48
1 ;;; package.el --- Simple package system for Emacs -*- lexical-binding:t -*-
3 ;; Copyright (C) 2007-2021 Free Software Foundation, Inc.
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"))
12 ;; This file is part of GNU Emacs.
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.
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.
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/>.
27 ;;; Commentary:
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.)
38 ;; A package is described by its name and version. The distribution
39 ;; format is either a tar file or a single .el file.
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.
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".
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.
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.
59 ;; Conceptually a package has multiple state transitions:
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.
71 ;; Other external functions you may want to use:
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.
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).
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'.
94 ;;; Thanks:
95 ;;; (sorted by sort-lines):
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>
107 ;;; ToDo:
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
144 ;;; Code:
146 (require 'cl-lib)
147 (eval-when-compile (require 'subr-x))
148 (eval-when-compile (require 'epg)) ;For setf accessors.
149 (require 'seq)
151 (require 'tabulated-list)
152 (require 'macroexp)
153 (require 'url-handlers)
154 (require 'browse-url)
156 (defgroup package nil
157 "Manager for Emacs Lisp packages."
158 :group 'applications
159 :version "24.1")
162 ;;; Customization options
164 ;;;###autoload
165 (defcustom package-enable-at-startup t
166 "Whether to make installed packages available when Emacs starts.
167 If non-nil, packages are made available before reading the init
168 file (but after reading the early init file). This means that if
169 you wish to set this variable, you must do so in the early init
170 file. Regardless of the value of this variable, packages are not
171 made available if `user-init-file' is nil (e.g. Emacs was started
172 with \"-q\").
174 Even if the value is nil, you can type \\[package-initialize] to
175 make installed packages available at any time, or you can
176 call (package-activate-all) in your init-file."
177 :type 'boolean
178 :version "24.1")
180 (defcustom package-load-list '(all)
181 "List of packages for `package-activate-all' to make available.
182 Each element in this list should be a list (NAME VERSION), or the
183 symbol `all'. The symbol `all' says to make available the latest
184 installed versions of all packages not specified by other
185 elements.
187 For an element (NAME VERSION), NAME is a package name (a symbol).
188 VERSION should be t, a string, or nil.
189 If VERSION is t, the most recent version is made available.
190 If VERSION is a string, only that version is ever made available.
191 Any other version, even if newer, is silently ignored.
192 Hence, the package is \"held\" at that version.
193 If VERSION is nil, the package is not made available (it is \"disabled\")."
194 :type '(repeat (choice (const all)
195 (list :tag "Specific package"
196 (symbol :tag "Package name")
197 (choice :tag "Version"
198 (const :tag "disable" nil)
199 (const :tag "most recent" t)
200 (string :tag "specific version")))))
201 :risky t
202 :version "24.1")
204 (defcustom package-archives `(("gnu" .
205 ,(format "http%s://elpa.gnu.org/packages/"
206 (if (gnutls-available-p) "s" "")))
207 ("nongnu" .
208 ,(format "http%s://elpa.nongnu.org/nongnu/"
209 (if (gnutls-available-p) "s" ""))))
210 "An alist of archives from which to fetch.
211 The default value points to the GNU Emacs package repository.
213 Each element has the form (ID . LOCATION).
214 ID is an archive name, as a string.
215 LOCATION specifies the base location for the archive.
216 If it starts with \"http(s):\", it is treated as an HTTP(S) URL;
217 otherwise it should be an absolute directory name.
218 (Other types of URL are currently not supported.)
220 Only add locations that you trust, since fetching and installing
221 a package can run arbitrary code.
223 HTTPS URLs should be used where possible, as they offer superior
224 security."
225 :type '(alist :key-type (string :tag "Archive name")
226 :value-type (string :tag "URL or directory name"))
227 :risky t
228 :version "28.1")
230 (defcustom package-menu-hide-low-priority 'archive
231 "If non-nil, hide low priority packages from the packages menu.
232 A package is considered low priority if there's another version
233 of it available such that:
234 (a) the archive of the other package is higher priority than
235 this one, as per `package-archive-priorities';
237 (b) they both have the same archive priority but the other
238 package has a higher version number.
240 This variable has three possible values:
241 nil: no packages are hidden;
242 `archive': only criterion (a) is used;
243 t: both criteria are used.
245 This variable has no effect if `package-menu--hide-packages' is
246 nil, so it can be toggled with \\<package-menu-mode-map>\\[package-menu-toggle-hiding]."
247 :type '(choice (const :tag "Don't hide anything" nil)
248 (const :tag "Hide per package-archive-priorities"
249 archive)
250 (const :tag "Hide per archive and version number" t))
251 :version "25.1")
253 (defcustom package-archive-priorities nil
254 "An alist of priorities for packages.
256 Each element has the form (ARCHIVE-ID . PRIORITY).
258 When installing packages, the package with the highest version
259 number from the archive with the highest priority is
260 selected. When higher versions are available from archives with
261 lower priorities, the user has to select those manually.
263 Archives not in this list have the priority 0, as have packages
264 that are already installed. If you use negative priorities for
265 the archives, they will not be upgraded automatically.
267 See also `package-menu-hide-low-priority'."
268 :type '(alist :key-type (string :tag "Archive name")
269 :value-type (integer :tag "Priority (default is 0)"))
270 :risky t
271 :version "25.1")
273 (defcustom package-pinned-packages nil
274 "An alist of packages that are pinned to specific archives.
275 This can be useful if you have multiple package archives enabled,
276 and want to control which archive a given package gets installed from.
278 Each element of the alist has the form (PACKAGE . ARCHIVE), where:
279 PACKAGE is a symbol representing a package
280 ARCHIVE is a string representing an archive (it should be the car of
281 an element in `package-archives', e.g. \"gnu\").
283 Adding an entry to this variable means that only ARCHIVE will be
284 considered as a source for PACKAGE. If other archives provide PACKAGE,
285 they are ignored (for this package). If ARCHIVE does not contain PACKAGE,
286 the package will be unavailable."
287 :type '(alist :key-type (symbol :tag "Package")
288 :value-type (string :tag "Archive name"))
289 ;; This could prevent you from receiving updates for a package,
290 ;; via an entry (PACKAGE . NON-EXISTING). Which could be an issue
291 ;; if PACKAGE has a known vulnerability that is fixed in newer versions.
292 :risky t
293 :version "24.4")
295 ;;;###autoload
296 (defcustom package-user-dir (locate-user-emacs-file "elpa")
297 "Directory containing the user's Emacs Lisp packages.
298 The directory name should be absolute.
299 Apart from this directory, Emacs also looks for system-wide
300 packages in `package-directory-list'."
301 :type 'directory
302 :initialize #'custom-initialize-delay
303 :risky t
304 :version "24.1")
306 ;;;###autoload
307 (defcustom package-directory-list
308 ;; Defaults are subdirs named "elpa" in the site-lisp dirs.
309 (let (result)
310 (dolist (f load-path)
311 (and (stringp f)
312 (equal (file-name-nondirectory f) "site-lisp")
313 (push (expand-file-name "elpa" f) result)))
314 (nreverse result))
315 "List of additional directories containing Emacs Lisp packages.
316 Each directory name should be absolute.
318 These directories contain packages intended for system-wide; in
319 contrast, `package-user-dir' contains packages for personal use."
320 :type '(repeat directory)
321 :initialize #'custom-initialize-delay
322 :risky t
323 :version "24.1")
325 (declare-function epg-find-configuration "epg-config"
326 (protocol &optional no-cache program-alist))
328 (defcustom package-gnupghome-dir (expand-file-name "gnupg" package-user-dir)
329 "Directory containing GnuPG keyring or nil.
330 This variable specifies the GnuPG home directory used by package.
331 That directory is passed via the option \"--homedir\" to GnuPG.
332 If nil, do not use the option \"--homedir\", but stick with GnuPG's
333 default directory."
334 :type `(choice
335 (const
336 :tag "Default Emacs package management GnuPG home directory"
337 ,(expand-file-name "gnupg" package-user-dir))
338 (const
339 :tag "Default GnuPG directory (GnuPG option --homedir not used)"
340 nil)
341 (directory :tag "A specific GnuPG --homedir"))
342 :risky t
343 :version "26.1")
345 (defcustom package-check-signature 'allow-unsigned
346 "Non-nil means to check package signatures when installing.
347 More specifically the value can be:
348 - nil: package signatures are ignored.
349 - `allow-unsigned': install a package even if it is unsigned, but
350 if it is signed, we have the key for it, and OpenGPG is
351 installed, verify the signature.
352 - t: accept a package only if it comes with at least one verified signature.
353 - `all': same as t, except when the package has several signatures,
354 in which case we verify all the signatures.
356 This also applies to the \"archive-contents\" file that lists the
357 contents of the archive."
358 :type '(choice (const nil :tag "Never")
359 (const allow-unsigned :tag "Allow unsigned")
360 (const t :tag "Check always")
361 (const all :tag "Check all signatures"))
362 :risky t
363 :version "27.1")
365 (defun package-check-signature ()
366 "Check whether we have a usable OpenPGP configuration.
367 If so, and variable `package-check-signature' is
368 `allow-unsigned', return `allow-unsigned', otherwise return the
369 value of variable `package-check-signature'."
370 (if (eq package-check-signature 'allow-unsigned)
371 (progn
372 (require 'epg-config)
373 (and (epg-find-configuration 'OpenPGP)
374 'allow-unsigned))
375 package-check-signature))
377 (defcustom package-unsigned-archives nil
378 "List of archives where we do not check for package signatures.
379 This should be a list of strings matching the names of package
380 archives in the variable `package-archives'."
381 :type '(repeat (string :tag "Archive name"))
382 :risky t
383 :version "24.4")
385 (defcustom package-selected-packages nil
386 "Store here packages installed explicitly by user.
387 This variable is fed automatically by Emacs when installing a new package.
388 This variable is used by `package-autoremove' to decide
389 which packages are no longer needed.
390 You can use it to (re)install packages on other machines
391 by running `package-install-selected-packages'.
393 To check if a package is contained in this list here, use
394 `package--user-selected-p', as it may populate the variable with
395 a sane initial value."
396 :version "25.1"
397 :type '(repeat symbol))
399 (defcustom package-native-compile nil
400 "Non-nil means to native compile packages on installation."
401 :type '(boolean)
402 :risky t
403 :version "28.1")
405 (defcustom package-menu-async t
406 "If non-nil, package-menu will use async operations when possible.
407 Currently, only the refreshing of archive contents supports
408 asynchronous operations. Package transactions are still done
409 synchronously."
410 :type 'boolean
411 :version "25.1")
413 (defcustom package-name-column-width 30
414 "Column width for the Package name in the package menu."
415 :type 'number
416 :version "28.1")
418 (defcustom package-version-column-width 14
419 "Column width for the Package version in the package menu."
420 :type 'number
421 :version "28.1")
423 (defcustom package-status-column-width 12
424 "Column width for the Package status in the package menu."
425 :type 'number
426 :version "28.1")
428 (defcustom package-archive-column-width 8
429 "Column width for the Package status in the package menu."
430 :type 'number
431 :version "28.1")
434 ;;; `package-desc' object definition
435 ;; This is the struct used internally to represent packages.
436 ;; Functions that deal with packages should generally take this object
437 ;; as an argument. In some situations (e.g. commands that query the
438 ;; user) it makes sense to take the package name as a symbol instead,
439 ;; but keep in mind there could be multiple `package-desc's with the
440 ;; same name.
442 (defvar package--default-summary "No description available.")
444 (cl-defstruct (package-desc
445 ;; Rename the default constructor from `make-package-desc'.
446 (:constructor package-desc-create)
447 ;; Has the same interface as the old `define-package',
448 ;; which is still used in the "foo-pkg.el" files. Extra
449 ;; options can be supported by adding additional keys.
450 (:constructor
451 package-desc-from-define
452 (name-string version-string &optional summary requirements
453 &rest rest-plist
454 &aux
455 (name (intern name-string))
456 (version (version-to-list version-string))
457 (reqs (mapcar (lambda (elt)
458 (list (car elt)
459 (version-to-list (cadr elt))))
460 (if (eq 'quote (car requirements))
461 (nth 1 requirements)
462 requirements)))
463 (kind (plist-get rest-plist :kind))
464 (archive (plist-get rest-plist :archive))
465 (extras (let (alist)
466 (while rest-plist
467 (unless (memq (car rest-plist) '(:kind :archive))
468 (let ((value (cadr rest-plist)))
469 (when value
470 (push (cons (car rest-plist)
471 (if (eq (car-safe value) 'quote)
472 (cadr value)
473 value))
474 alist))))
475 (setq rest-plist (cddr rest-plist)))
476 alist)))))
477 "Structure containing information about an individual package.
478 Slots:
480 `name' Name of the package, as a symbol.
482 `version' Version of the package, as a version list.
484 `summary' Short description of the package, typically taken from
485 the first line of the file.
487 `reqs' Requirements of the package. A list of (PACKAGE
488 VERSION-LIST) naming the dependent package and the minimum
489 required version.
491 `kind' The distribution format of the package. Currently, it is
492 either `single' or `tar'.
494 `archive' The name of the archive (as a string) whence this
495 package came.
497 `dir' The directory where the package is installed (if installed),
498 `builtin' if it is built-in, or nil otherwise.
500 `extras' Optional alist of additional keyword-value pairs.
502 `signed' Flag to indicate that the package is signed by provider."
503 name
504 version
505 (summary package--default-summary)
506 reqs
507 kind
508 archive
510 extras
511 signed)
513 (defun package--from-builtin (bi-desc)
514 "Create a `package-desc' object from BI-DESC.
515 BI-DESC should be a `package--bi-desc' object."
516 (package-desc-create :name (pop bi-desc)
517 :version (package--bi-desc-version bi-desc)
518 :summary (package--bi-desc-summary bi-desc)
519 :dir 'builtin))
521 ;; Pseudo fields.
522 (defun package-version-join (vlist)
523 "Return the version string corresponding to the list VLIST.
524 This is, approximately, the inverse of `version-to-list'.
525 \(Actually, it returns only one of the possible inverses, since
526 `version-to-list' is a many-to-one operation.)"
527 (if (null vlist)
529 (let ((str-list (list "." (int-to-string (car vlist)))))
530 (dolist (num (cdr vlist))
531 (cond
532 ((>= num 0)
533 (push (int-to-string num) str-list)
534 (push "." str-list))
535 ((< num -4)
536 (error "Invalid version list `%s'" vlist))
538 ;; pre, or beta, or alpha
539 (cond ((equal "." (car str-list))
540 (pop str-list))
541 ((not (string-match "[0-9]+" (car str-list)))
542 (error "Invalid version list `%s'" vlist)))
543 (push (cond ((= num -1) "pre")
544 ((= num -2) "beta")
545 ((= num -3) "alpha")
546 ((= num -4) "snapshot"))
547 str-list))))
548 (if (equal "." (car str-list))
549 (pop str-list))
550 (apply #'concat (nreverse str-list)))))
552 (defun package-desc-full-name (pkg-desc)
553 "Return full name of package-desc object PKG-DESC.
554 This is the name of the package with its version appended."
555 (format "%s-%s"
556 (package-desc-name pkg-desc)
557 (package-version-join (package-desc-version pkg-desc))))
559 (defun package-desc-suffix (pkg-desc)
560 "Return file-name extension of package-desc object PKG-DESC.
561 Depending on the `package-desc-kind' of PKG-DESC, this is one of:
563 'single - \".el\"
564 'tar - \".tar\"
565 'dir - \"\"
567 Signal an error if the kind is none of the above."
568 (pcase (package-desc-kind pkg-desc)
569 ('single ".el")
570 ('tar ".tar")
571 ('dir "")
572 (kind (error "Unknown package kind: %s" kind))))
574 (defun package-desc--keywords (pkg-desc)
575 "Return keywords of package-desc object PKG-DESC.
576 These keywords come from the foo-pkg.el file, and in general
577 corresponds to the keywords in the \"Keywords\" header of the
578 package."
579 (let ((keywords (cdr (assoc :keywords (package-desc-extras pkg-desc)))))
580 (if (eq (car-safe keywords) 'quote)
581 (nth 1 keywords)
582 keywords)))
584 (defun package-desc-priority (pkg-desc)
585 "Return the priority of the archive of package-desc object PKG-DESC."
586 (package-archive-priority (package-desc-archive pkg-desc)))
588 (cl-defstruct (package--bi-desc
589 (:constructor package-make-builtin (version summary))
590 (:type vector))
591 "Package descriptor format used in finder-inf.el and package--builtins."
592 version
593 reqs
594 summary)
597 ;;; Installed packages
598 ;; The following variables store information about packages present in
599 ;; the system. The most important of these is `package-alist'. The
600 ;; command `package-activate-all' is also closely related to this
601 ;; section.
603 (defvar package--builtins nil
604 "Alist of built-in packages.
605 The actual value is initialized by loading the library
606 `finder-inf'; this is not done until it is needed, e.g. by the
607 function `package-built-in-p'.
609 Each element has the form (PKG . PACKAGE-BI-DESC), where PKG is a package
610 name (a symbol) and DESC is a `package--bi-desc' structure.")
611 (put 'package--builtins 'risky-local-variable t)
613 (defvar package-alist nil
614 "Alist of all packages available for activation.
615 Each element has the form (PKG . DESCS), where PKG is a package
616 name (a symbol) and DESCS is a non-empty list of `package-desc'
617 structures, sorted by decreasing versions.
619 This variable is set automatically by `package-load-descriptor',
620 called via `package-activate-all'. To change which packages are
621 loaded and/or activated, customize `package-load-list'.")
622 (put 'package-alist 'risky-local-variable t)
624 (defvar package-activated-list nil
625 ;; FIXME: This should implicitly include all builtin packages.
626 "List of the names of currently activated packages.")
627 (put 'package-activated-list 'risky-local-variable t)
629 ;;;; Populating `package-alist'.
631 ;; The following functions are called on each installed package by
632 ;; `package-load-all-descriptors', which ultimately populates the
633 ;; `package-alist' variable.
635 (defun package-process-define-package (exp)
636 "Process define-package expression EXP and push it to `package-alist'.
637 EXP should be a form read from a foo-pkg.el file.
638 Convert EXP into a `package-desc' object using the
639 `package-desc-from-define' constructor before pushing it to
640 `package-alist'.
642 If there already exists a package by the same name in
643 `package-alist', insert this object there such that the packages
644 are sorted with the highest version first."
645 (when (eq (car-safe exp) 'define-package)
646 (let* ((new-pkg-desc (apply #'package-desc-from-define (cdr exp)))
647 (name (package-desc-name new-pkg-desc))
648 (version (package-desc-version new-pkg-desc))
649 (old-pkgs (assq name package-alist)))
650 (if (null old-pkgs)
651 ;; If there's no old package, just add this to `package-alist'.
652 (push (list name new-pkg-desc) package-alist)
653 ;; If there is, insert the new package at the right place in the list.
654 (while
655 (if (and (cdr old-pkgs)
656 (version-list-< version
657 (package-desc-version (cadr old-pkgs))))
658 (setq old-pkgs (cdr old-pkgs))
659 (push new-pkg-desc (cdr old-pkgs))
660 nil)))
661 new-pkg-desc)))
663 (defun package-load-descriptor (pkg-dir)
664 "Load the package description file in directory PKG-DIR.
665 Create a new `package-desc' object, add it to `package-alist' and
666 return it."
667 (let ((pkg-file (expand-file-name (package--description-file pkg-dir)
668 pkg-dir))
669 (signed-file (concat pkg-dir ".signed")))
670 (when (file-exists-p pkg-file)
671 (with-temp-buffer
672 (insert-file-contents pkg-file)
673 (goto-char (point-min))
674 (let ((pkg-desc (or (package-process-define-package
675 (read (current-buffer)))
676 (error "Can't find define-package in %s" pkg-file))))
677 (setf (package-desc-dir pkg-desc) pkg-dir)
678 (if (file-exists-p signed-file)
679 (setf (package-desc-signed pkg-desc) t))
680 pkg-desc)))))
682 (defun package-load-all-descriptors ()
683 "Load descriptors for installed Emacs Lisp packages.
684 This looks for package subdirectories in `package-user-dir' and
685 `package-directory-list'. The variable `package-load-list'
686 controls which package subdirectories may be loaded.
688 In each valid package subdirectory, this function loads the
689 description file containing a call to `define-package', which
690 updates `package-alist'."
691 (dolist (dir (cons package-user-dir package-directory-list))
692 (when (file-directory-p dir)
693 (dolist (subdir (directory-files dir))
694 (unless (equal subdir "..")
695 (let ((pkg-dir (expand-file-name subdir dir)))
696 (when (file-directory-p pkg-dir)
697 (package-load-descriptor pkg-dir))))))))
699 (defun package--alist ()
700 "Return `package-alist', after computing it if needed."
701 (or package-alist
702 (progn (package-load-all-descriptors)
703 package-alist)))
705 (defun define-package ( _name-string _version-string
706 &optional _docstring _requirements
707 &rest _extra-properties)
708 "Define a new package.
709 NAME-STRING is the name of the package, as a string.
710 VERSION-STRING is the version of the package, as a string.
711 DOCSTRING is a short description of the package, a string.
712 REQUIREMENTS is a list of dependencies on other packages.
713 Each requirement is of the form (OTHER-PACKAGE OTHER-VERSION),
714 where OTHER-VERSION is a string.
716 EXTRA-PROPERTIES is currently unused."
717 ;; FIXME: Placeholder! Should we keep it?
718 (error "Don't call me!"))
721 ;;; Package activation
722 ;; Section for functions used by `package-activate', which see.
724 (defun package-disabled-p (pkg-name version)
725 "Return whether PKG-NAME at VERSION can be activated.
726 The decision is made according to `package-load-list'.
727 Return nil if the package can be activated.
728 Return t if the package is completely disabled.
729 Return the max version (as a string) if the package is held at a lower version."
730 (let ((force (assq pkg-name package-load-list)))
731 (cond ((null force) (not (memq 'all package-load-list)))
732 ((null (setq force (cadr force))) t) ; disabled
733 ((eq force t) nil)
734 ((stringp force) ; held
735 (unless (version-list-= version (version-to-list force))
736 force))
737 (t (error "Invalid element in `package-load-list'")))))
739 (defun package-built-in-p (package &optional min-version)
740 "Return non-nil if PACKAGE is built-in to Emacs.
741 Optional arg MIN-VERSION, if non-nil, should be a version list
742 specifying the minimum acceptable version."
743 (if (package-desc-p package) ;; was built-in and then was converted
744 (eq 'builtin (package-desc-dir package))
745 (let ((bi (assq package package--builtin-versions)))
746 (cond
747 (bi (version-list-<= min-version (cdr bi)))
748 ((remove 0 min-version) nil)
750 (require 'finder-inf nil t) ; For `package--builtins'.
751 (assq package package--builtins))))))
753 (defun package--autoloads-file-name (pkg-desc)
754 "Return the absolute name of the autoloads file, sans extension.
755 PKG-DESC is a `package-desc' object."
756 (expand-file-name
757 (format "%s-autoloads" (package-desc-name pkg-desc))
758 (package-desc-dir pkg-desc)))
760 (defun package--activate-autoloads-and-load-path (pkg-desc)
761 "Load the autoloads file and add package dir to `load-path'.
762 PKG-DESC is a `package-desc' object."
763 (let* ((old-lp load-path)
764 (pkg-dir (package-desc-dir pkg-desc))
765 (pkg-dir-dir (file-name-as-directory pkg-dir)))
766 (with-demoted-errors "Error loading autoloads: %s"
767 (load (package--autoloads-file-name pkg-desc) nil t))
768 (when (and (eq old-lp load-path)
769 (not (or (member pkg-dir load-path)
770 (member pkg-dir-dir load-path))))
771 ;; Old packages don't add themselves to the `load-path', so we have to
772 ;; do it ourselves.
773 (push pkg-dir load-path))))
775 (defvar Info-directory-list)
776 (declare-function info-initialize "info" ())
778 (defvar package--quickstart-pkgs t
779 "If set to a list, we're computing the set of pkgs to activate.")
781 (defun package--load-files-for-activation (pkg-desc reload)
782 "Load files for activating a package given by PKG-DESC.
783 Load the autoloads file, and ensure `load-path' is setup. If
784 RELOAD is non-nil, also load all files in the package that
785 correspond to previously loaded files."
786 (let* ((loaded-files-list
787 (when reload
788 (package--list-loaded-files (package-desc-dir pkg-desc)))))
789 ;; Add to load path, add autoloads, and activate the package.
790 (package--activate-autoloads-and-load-path pkg-desc)
791 ;; Call `load' on all files in `package-desc-dir' already present in
792 ;; `load-history'. This is done so that macros in these files are updated
793 ;; to their new definitions. If another package is being installed which
794 ;; depends on this new definition, not doing this update would cause
795 ;; compilation errors and break the installation.
796 (with-demoted-errors "Error in package--load-files-for-activation: %s"
797 (mapc (lambda (feature) (load feature nil t))
798 ;; Skip autoloads file since we already evaluated it above.
799 (remove (file-truename (package--autoloads-file-name pkg-desc))
800 loaded-files-list)))))
802 (defun package-activate-1 (pkg-desc &optional reload deps)
803 "Activate package given by PKG-DESC, even if it was already active.
804 If DEPS is non-nil, also activate its dependencies (unless they
805 are already activated).
806 If RELOAD is non-nil, also `load' any files inside the package which
807 correspond to previously loaded files (those returned by
808 `package--list-loaded-files')."
809 (let* ((name (package-desc-name pkg-desc))
810 (pkg-dir (package-desc-dir pkg-desc)))
811 (unless pkg-dir
812 (error "Internal error: unable to find directory for `%s'"
813 (package-desc-full-name pkg-desc)))
814 (catch 'exit
815 ;; Activate its dependencies recursively.
816 ;; FIXME: This doesn't check whether the activated version is the
817 ;; required version.
818 (when deps
819 (dolist (req (package-desc-reqs pkg-desc))
820 (unless (package-activate (car req))
821 (message "Unable to activate package `%s'.\nRequired package `%s-%s' is unavailable"
822 name (car req) (package-version-join (cadr req)))
823 (throw 'exit nil))))
824 (if (listp package--quickstart-pkgs)
825 ;; We're only collecting the set of packages to activate!
826 (push pkg-desc package--quickstart-pkgs)
827 (package--load-files-for-activation pkg-desc reload))
828 ;; Add info node.
829 (when (file-exists-p (expand-file-name "dir" pkg-dir))
830 ;; FIXME: not the friendliest, but simple.
831 (require 'info)
832 (info-initialize)
833 (add-to-list 'Info-directory-list pkg-dir))
834 (push name package-activated-list)
835 ;; Don't return nil.
836 t)))
838 (defun package--files-load-history ()
839 (delq nil
840 (mapcar (lambda (x)
841 (let ((f (car x)))
842 (and (stringp f)
843 (file-name-sans-extension (file-truename f)))))
844 load-history)))
846 (defun package--list-of-conflicts (dir history)
847 (require 'find-func)
848 (declare-function find-library-name "find-func" (library))
849 (delq
851 (mapcar
852 (lambda (x) (let* ((file (file-relative-name x dir))
853 ;; Previously loaded file, if any.
854 (previous
855 (ignore-error file-error ;"Can't find library"
856 (file-name-sans-extension
857 (file-truename (find-library-name file)))))
858 (pos (when previous (member previous history))))
859 ;; Return (RELATIVE-FILENAME . HISTORY-POSITION)
860 (when pos
861 (cons (file-name-sans-extension file) (length pos)))))
862 (directory-files-recursively dir "\\`[^\\.].*\\.el\\'"))))
864 (defun package--list-loaded-files (dir)
865 "Recursively list all files in DIR which correspond to loaded features.
866 Returns the `file-name-sans-extension' of each file, relative to
867 DIR, sorted by most recently loaded last."
868 (let* ((history (package--files-load-history))
869 (dir (file-truename dir))
870 ;; List all files that have already been loaded.
871 (list-of-conflicts (package--list-of-conflicts dir history)))
872 ;; Turn the list of (FILENAME . POS) back into a list of features. Files in
873 ;; subdirectories are returned relative to DIR (so not actually features).
874 (let ((default-directory (file-name-as-directory dir)))
875 (mapcar (lambda (x) (file-truename (car x)))
876 (sort list-of-conflicts
877 ;; Sort the files by ascending HISTORY-POSITION.
878 (lambda (x y) (< (cdr x) (cdr y))))))))
880 ;;;; `package-activate'
882 (defun package--get-activatable-pkg (pkg-name)
883 ;; Is "activatable" a word?
884 (let ((pkg-descs (cdr (assq pkg-name package-alist))))
885 ;; Check if PACKAGE is available in `package-alist'.
886 (while
887 (when pkg-descs
888 (let ((available-version (package-desc-version (car pkg-descs))))
889 (or (package-disabled-p pkg-name available-version)
890 ;; Prefer a builtin package.
891 (package-built-in-p pkg-name available-version))))
892 (setq pkg-descs (cdr pkg-descs)))
893 (car pkg-descs)))
895 ;; This function activates a newer version of a package if an older
896 ;; one was already activated. It also loads a features of this
897 ;; package which were already loaded.
898 (defun package-activate (package &optional force)
899 "Activate the package named PACKAGE.
900 If FORCE is true, (re-)activate it if it's already activated.
901 Newer versions are always activated, regardless of FORCE."
902 (let ((pkg-desc (package--get-activatable-pkg package)))
903 (cond
904 ;; If no such package is found, maybe it's built-in.
905 ((null pkg-desc)
906 (package-built-in-p package))
907 ;; If the package is already activated, just return t.
908 ((and (memq package package-activated-list) (not force))
910 ;; Otherwise, proceed with activation.
911 (t (package-activate-1 pkg-desc nil 'deps)))))
914 ;;; Installation -- Local operations
915 ;; This section contains a variety of features regarding installing a
916 ;; package to/from disk. This includes autoload generation,
917 ;; unpacking, compiling, as well as defining a package from the
918 ;; current buffer.
920 ;;;; Unpacking
921 (defvar tar-parse-info)
922 (declare-function tar-untar-buffer "tar-mode" ())
923 (declare-function tar-header-name "tar-mode" (tar-header) t)
924 (declare-function tar-header-link-type "tar-mode" (tar-header) t)
926 (defun package-untar-buffer (dir)
927 "Untar the current buffer.
928 This uses `tar-untar-buffer' from Tar mode. All files should
929 untar into a directory named DIR; otherwise, signal an error."
930 (require 'tar-mode)
931 (tar-mode)
932 ;; Make sure everything extracts into DIR.
933 (let ((regexp (concat "\\`" (regexp-quote (expand-file-name dir)) "/"))
934 (case-fold-search (file-name-case-insensitive-p dir)))
935 (dolist (tar-data tar-parse-info)
936 (let ((name (expand-file-name (tar-header-name tar-data))))
937 (or (string-match regexp name)
938 ;; Tarballs created by some utilities don't list
939 ;; directories with a trailing slash (Bug#13136).
940 (and (string-equal dir name)
941 (eq (tar-header-link-type tar-data) 5))
942 (error "Package does not untar cleanly into directory %s/" dir)))))
943 (tar-untar-buffer))
945 (defun package--alist-to-plist-args (alist)
946 (mapcar #'macroexp-quote
947 (apply #'nconc
948 (mapcar (lambda (pair) (list (car pair) (cdr pair))) alist))))
950 (defun package-unpack (pkg-desc)
951 "Install the contents of the current buffer as a package."
952 (let* ((name (package-desc-name pkg-desc))
953 (dirname (package-desc-full-name pkg-desc))
954 (pkg-dir (expand-file-name dirname package-user-dir)))
955 (pcase (package-desc-kind pkg-desc)
956 ('dir
957 (make-directory pkg-dir t)
958 (let ((file-list
959 (directory-files
960 default-directory 'full "\\`[^.].*\\.el\\'" 'nosort)))
961 (dolist (source-file file-list)
962 (let ((target-el-file
963 (expand-file-name (file-name-nondirectory source-file) pkg-dir)))
964 (copy-file source-file target-el-file t)))
965 ;; Now that the files have been installed, this package is
966 ;; indistinguishable from a `tar' or a `single'. Let's make
967 ;; things simple by ensuring we're one of them.
968 (setf (package-desc-kind pkg-desc)
969 (if (> (length file-list) 1) 'tar 'single))))
970 ('tar
971 (make-directory package-user-dir t)
972 (let* ((default-directory (file-name-as-directory package-user-dir)))
973 (package-untar-buffer dirname)))
974 ('single
975 (let ((el-file (expand-file-name (format "%s.el" name) pkg-dir)))
976 (make-directory pkg-dir t)
977 (package--write-file-no-coding el-file)))
978 (kind (error "Unknown package kind: %S" kind)))
979 (package--make-autoloads-and-stuff pkg-desc pkg-dir)
980 ;; Update package-alist.
981 (let ((new-desc (package-load-descriptor pkg-dir)))
982 (unless (equal (package-desc-full-name new-desc)
983 (package-desc-full-name pkg-desc))
984 (error "The retrieved package (`%s') doesn't match what the archive offered (`%s')"
985 (package-desc-full-name new-desc) (package-desc-full-name pkg-desc)))
986 ;; Activation has to be done before compilation, so that if we're
987 ;; upgrading and macros have changed we load the new definitions
988 ;; before compiling.
989 (when (package-activate-1 new-desc :reload :deps)
990 ;; FIXME: Compilation should be done as a separate, optional, step.
991 ;; E.g. for multi-package installs, we should first install all packages
992 ;; and then compile them.
993 (package--compile new-desc)
994 (when package-native-compile
995 (package--native-compile-async new-desc))
996 ;; After compilation, load again any files loaded by
997 ;; `activate-1', so that we use the byte-compiled definitions.
998 (package--load-files-for-activation new-desc :reload)))
999 pkg-dir))
1001 (defun package-generate-description-file (pkg-desc pkg-file)
1002 "Create the foo-pkg.el file PKG-FILE for single-file package PKG-DESC."
1003 (let* ((name (package-desc-name pkg-desc)))
1004 (let ((print-level nil)
1005 (print-quoted t)
1006 (print-length nil))
1007 (write-region
1008 (concat
1009 ";;; Generated package description from "
1010 (replace-regexp-in-string "-pkg\\.el\\'" ".el"
1011 (file-name-nondirectory pkg-file))
1012 " -*- no-byte-compile: t -*-\n"
1013 (prin1-to-string
1014 (nconc
1015 (list 'define-package
1016 (symbol-name name)
1017 (package-version-join (package-desc-version pkg-desc))
1018 (package-desc-summary pkg-desc)
1019 (let ((requires (package-desc-reqs pkg-desc)))
1020 (list 'quote
1021 ;; Turn version lists into string form.
1022 (mapcar
1023 (lambda (elt)
1024 (list (car elt)
1025 (package-version-join (cadr elt))))
1026 requires))))
1027 (package--alist-to-plist-args
1028 (package-desc-extras pkg-desc))))
1029 "\n")
1030 nil pkg-file nil 'silent))))
1032 ;;;; Autoload
1033 (declare-function autoload-rubric "autoload" (file &optional type feature))
1035 (defun package-autoload-ensure-default-file (file)
1036 "Make sure that the autoload file FILE exists and if not create it."
1037 (unless (file-exists-p file)
1038 (require 'autoload)
1039 (write-region (autoload-rubric file "package" nil) nil file nil 'silent))
1040 file)
1042 (defvar autoload-timestamps)
1043 (defvar version-control)
1045 (defun package-generate-autoloads (name pkg-dir)
1046 "Generate autoloads in PKG-DIR for package named NAME."
1047 (let* ((auto-name (format "%s-autoloads.el" name))
1048 ;;(ignore-name (concat name "-pkg.el"))
1049 (output-file (expand-file-name auto-name pkg-dir))
1050 ;; We don't need 'em, and this makes the output reproducible.
1051 (autoload-timestamps nil)
1052 (backup-inhibited t)
1053 (version-control 'never))
1054 (package-autoload-ensure-default-file output-file)
1055 (make-directory-autoloads pkg-dir output-file)
1056 (let ((buf (find-buffer-visiting output-file)))
1057 (when buf (kill-buffer buf)))
1058 auto-name))
1060 (defun package--make-autoloads-and-stuff (pkg-desc pkg-dir)
1061 "Generate autoloads, description file, etc., for PKG-DESC installed at PKG-DIR."
1062 (package-generate-autoloads (package-desc-name pkg-desc) pkg-dir)
1063 (let ((desc-file (expand-file-name (package--description-file pkg-dir)
1064 pkg-dir)))
1065 (unless (file-exists-p desc-file)
1066 (package-generate-description-file pkg-desc desc-file)))
1067 ;; FIXME: Create foo.info and dir file from foo.texi?
1070 ;;;; Compilation
1071 (defvar warning-minimum-level)
1072 (defun package--compile (pkg-desc)
1073 "Byte-compile installed package PKG-DESC.
1074 This assumes that `pkg-desc' has already been activated with
1075 `package-activate-1'."
1076 (let ((warning-minimum-level :error)
1077 (load-path load-path))
1078 (byte-recompile-directory (package-desc-dir pkg-desc) 0 t)))
1080 (defun package--native-compile-async (pkg-desc)
1081 "Native compile installed package PKG-DESC asynchronously.
1082 This assumes that `pkg-desc' has already been activated with
1083 `package-activate-1'."
1084 (when (and (featurep 'native-compile)
1085 (native-comp-available-p))
1086 (let ((warning-minimum-level :error))
1087 (native-compile-async (package-desc-dir pkg-desc) t))))
1089 ;;;; Inferring package from current buffer
1090 (defun package-read-from-string (str)
1091 "Read a Lisp expression from STR.
1092 Signal an error if the entire string was not used."
1093 (pcase-let ((`(,expr . ,offset) (read-from-string str)))
1094 (condition-case ()
1095 ;; The call to `ignore' suppresses a compiler warning.
1096 (progn (ignore (read-from-string str offset))
1097 (error "Can't read whole string"))
1098 (end-of-file expr))))
1100 (defun package--prepare-dependencies (deps)
1101 "Turn DEPS into an acceptable list of dependencies.
1103 Any parts missing a version string get a default version string
1104 of \"0\" (meaning any version) and an appropriate level of lists
1105 is wrapped around any parts requiring it."
1106 (cond
1107 ((not (listp deps))
1108 (error "Invalid requirement specifier: %S" deps))
1109 (t (mapcar (lambda (dep)
1110 (cond
1111 ((symbolp dep) `(,dep "0"))
1112 ((stringp dep)
1113 (error "Invalid requirement specifier: %S" dep))
1114 ((and (listp dep) (null (cdr dep)))
1115 (list (car dep) "0"))
1116 (t dep)))
1117 deps))))
1119 (declare-function lm-header "lisp-mnt" (header))
1120 (declare-function lm-header-multiline "lisp-mnt" (header))
1121 (declare-function lm-homepage "lisp-mnt" (&optional file))
1122 (declare-function lm-keywords-list "lisp-mnt" (&optional file))
1123 (declare-function lm-maintainer "lisp-mnt" (&optional file))
1124 (declare-function lm-authors "lisp-mnt" (&optional file))
1126 (defun package-buffer-info ()
1127 "Return a `package-desc' describing the package in the current buffer.
1129 If the buffer does not contain a conforming package, signal an
1130 error. If there is a package, narrow the buffer to the file's
1131 boundaries."
1132 (goto-char (point-min))
1133 (unless (re-search-forward "^;;; \\([^ ]*\\)\\.el ---[ \t]*\\(.*?\\)[ \t]*\\(-\\*-.*-\\*-[ \t]*\\)?$" nil t)
1134 (error "Package lacks a file header"))
1135 (let ((file-name (match-string-no-properties 1))
1136 (desc (match-string-no-properties 2))
1137 (start (line-beginning-position)))
1138 ;; This warning was added in Emacs 27.1, and should be removed at
1139 ;; the earliest in version 31.1. The idea is to phase out the
1140 ;; requirement for a "footer line" without unduly impacting users
1141 ;; on earlier Emacs versions. See Bug#26490 for more details.
1142 (unless (search-forward (concat ";;; " file-name ".el ends here"))
1143 (lwarn '(package package-format) :warning
1144 "Package lacks a terminating comment"))
1145 ;; Try to include a trailing newline.
1146 (forward-line)
1147 (narrow-to-region start (point))
1148 (require 'lisp-mnt)
1149 ;; Use some headers we've invented to drive the process.
1150 (let* (;; Prefer Package-Version; if defined, the package author
1151 ;; probably wants us to use it. Otherwise try Version.
1152 (version-info
1153 (or (lm-header "package-version") (lm-header "version")))
1154 (pkg-version (package-strip-rcs-id version-info))
1155 (keywords (lm-keywords-list))
1156 (homepage (lm-homepage)))
1157 (unless pkg-version
1158 (if version-info
1159 (error "Unrecognized package version: %s" version-info)
1160 (error "Package lacks a \"Version\" or \"Package-Version\" header")))
1161 (package-desc-from-define
1162 file-name pkg-version desc
1163 (and-let* ((require-lines (lm-header-multiline "package-requires")))
1164 (package--prepare-dependencies
1165 (package-read-from-string (mapconcat #'identity require-lines " "))))
1166 :kind 'single
1167 :url homepage
1168 :keywords keywords
1169 :maintainer (lm-maintainer)
1170 :authors (lm-authors)))))
1172 (defun package--read-pkg-desc (kind)
1173 "Read a `define-package' form in current buffer.
1174 Return the pkg-desc, with desc-kind set to KIND."
1175 (goto-char (point-min))
1176 (unwind-protect
1177 (let* ((pkg-def-parsed (read (current-buffer)))
1178 (pkg-desc
1179 (when (eq (car pkg-def-parsed) 'define-package)
1180 (apply #'package-desc-from-define
1181 (append (cdr pkg-def-parsed))))))
1182 (when pkg-desc
1183 (setf (package-desc-kind pkg-desc) kind)
1184 pkg-desc))))
1186 (declare-function tar-get-file-descriptor "tar-mode" (file))
1187 (declare-function tar--extract "tar-mode" (descriptor))
1189 (defun package-tar-file-info ()
1190 "Find package information for a tar file.
1191 The return result is a `package-desc'."
1192 (cl-assert (derived-mode-p 'tar-mode))
1193 (let* ((dir-name (file-name-directory
1194 (tar-header-name (car tar-parse-info))))
1195 (desc-file (package--description-file dir-name))
1196 (tar-desc (tar-get-file-descriptor (concat dir-name desc-file))))
1197 (unless tar-desc
1198 (error "No package descriptor file found"))
1199 (with-current-buffer (tar--extract tar-desc)
1200 (unwind-protect
1201 (or (package--read-pkg-desc 'tar)
1202 (error "Can't find define-package in %s"
1203 (tar-header-name tar-desc)))
1204 (kill-buffer (current-buffer))))))
1206 (defun package-dir-info ()
1207 "Find package information for a directory.
1208 The return result is a `package-desc'."
1209 (cl-assert (derived-mode-p 'dired-mode))
1210 (let* ((desc-file (package--description-file default-directory)))
1211 (if (file-readable-p desc-file)
1212 (with-temp-buffer
1213 (insert-file-contents desc-file)
1214 (package--read-pkg-desc 'dir))
1215 (let ((files (directory-files default-directory t "\\.el\\'" t))
1216 info)
1217 (while files
1218 (with-temp-buffer
1219 (insert-file-contents (pop files))
1220 ;; When we find the file with the data,
1221 (when (setq info (ignore-errors (package-buffer-info)))
1222 ;; stop looping,
1223 (setq files nil)
1224 ;; set the 'dir kind,
1225 (setf (package-desc-kind info) 'dir))))
1226 (unless info
1227 (error "No .el files with package headers in `%s'" default-directory))
1228 ;; and return the info.
1229 info))))
1232 ;;; Communicating with Archives
1233 ;; Set of low-level functions for communicating with archives and
1234 ;; signature checking.
1236 (defun package--write-file-no-coding (file-name)
1237 "Write file FILE-NAME without encoding using coding system."
1238 (let ((buffer-file-coding-system 'no-conversion))
1239 (write-region (point-min) (point-max) file-name nil 'silent)))
1241 (declare-function url-http-file-exists-p "url-http" (url))
1243 (defun package--archive-file-exists-p (location file)
1244 "Return t if FILE exists in remote LOCATION."
1245 (let ((http (string-match "\\`https?:" location)))
1246 (if http
1247 (progn
1248 (require 'url-http)
1249 (url-http-file-exists-p (concat location file)))
1250 (file-exists-p (expand-file-name file location)))))
1252 (declare-function epg-make-context "epg"
1253 (&optional protocol armor textmode include-certs
1254 cipher-algorithm
1255 digest-algorithm
1256 compress-algorithm))
1257 (declare-function epg-verify-string "epg" ( context signature
1258 &optional signed-text))
1259 (declare-function epg-context-result-for "epg" (context name))
1260 (declare-function epg-signature-status "epg" (signature) t)
1261 (declare-function epg-signature-to-string "epg" (signature))
1263 (defun package--display-verify-error (context sig-file)
1264 "Show error details with CONTEXT for failed verification of SIG-FILE.
1265 The details are shown in a new buffer called \"*Error\"."
1266 (unless (equal (epg-context-error-output context) "")
1267 (with-output-to-temp-buffer "*Error*"
1268 (with-current-buffer standard-output
1269 (if (epg-context-result-for context 'verify)
1270 (insert (format "Failed to verify signature %s:\n" sig-file)
1271 (mapconcat #'epg-signature-to-string
1272 (epg-context-result-for context 'verify)
1273 "\n"))
1274 (insert (format "Error while verifying signature %s:\n" sig-file)))
1275 (insert "\nCommand output:\n" (epg-context-error-output context))))))
1277 (defmacro package--with-work-buffer (location file &rest body)
1278 "Run BODY in a buffer containing the contents of FILE at LOCATION.
1279 LOCATION is the base location of a package archive, and should be
1280 one of the URLs (or file names) specified in `package-archives'.
1281 FILE is the name of a file relative to that base location.
1283 This macro retrieves FILE from LOCATION into a temporary buffer,
1284 and evaluates BODY while that buffer is current. This work
1285 buffer is killed afterwards. Return the last value in BODY."
1286 (declare (indent 2) (debug t)
1287 (obsolete package--with-response-buffer "25.1"))
1288 `(with-temp-buffer
1289 (if (string-match-p "\\`https?:" ,location)
1290 (url-insert-file-contents (concat ,location ,file))
1291 (unless (file-name-absolute-p ,location)
1292 (error "Archive location %s is not an absolute file name"
1293 ,location))
1294 (insert-file-contents (expand-file-name ,file ,location)))
1295 ,@body))
1297 (cl-defmacro package--with-response-buffer (url &rest body &key async file error-form noerror &allow-other-keys)
1298 "Access URL and run BODY in a buffer containing the response.
1299 Point is after the headers when BODY runs.
1300 FILE, if provided, is added to URL.
1301 URL can be a local file name, which must be absolute.
1302 ASYNC, if non-nil, runs the request asynchronously.
1303 ERROR-FORM is run only if a connection error occurs. If NOERROR
1304 is non-nil, don't propagate connection errors (does not apply to
1305 errors signaled by ERROR-FORM or by BODY).
1307 \(fn URL &key ASYNC FILE ERROR-FORM NOERROR &rest BODY)"
1308 (declare (indent defun)
1309 ;; FIXME: This should be something like
1310 ;; `form def-body &rest form', but that doesn't work.
1311 (debug (form &rest sexp)))
1312 (while (keywordp (car body))
1313 (setq body (cdr (cdr body))))
1314 `(package--with-response-buffer-1 ,url (lambda () ,@body)
1315 :file ,file
1316 :async ,async
1317 :error-function (lambda () ,error-form)
1318 :noerror ,noerror))
1320 (defmacro package--unless-error (body &rest before-body)
1321 (declare (debug t) (indent 1))
1322 (let ((err (make-symbol "err")))
1323 `(with-temp-buffer
1324 (set-buffer-multibyte nil)
1325 (when (condition-case ,err
1326 (progn ,@before-body t)
1327 (error (funcall error-function)
1328 (unless noerror
1329 (signal (car ,err) (cdr ,err)))))
1330 (funcall ,body)))))
1332 (cl-defun package--with-response-buffer-1 (url body &key async file error-function noerror &allow-other-keys)
1333 (if (string-match-p "\\`https?:" url)
1334 (let ((url (concat url file)))
1335 (if async
1336 (package--unless-error #'ignore
1337 (url-retrieve
1339 (lambda (status)
1340 (let ((b (current-buffer)))
1341 (require 'url-handlers)
1342 (package--unless-error body
1343 (when-let* ((er (plist-get status :error)))
1344 (error "Error retrieving: %s %S" url er))
1345 (with-current-buffer b
1346 (goto-char (point-min))
1347 (unless (search-forward-regexp "^\r?\n\r?" nil t)
1348 (error "Error retrieving: %s %S"
1349 url "incomprehensible buffer")))
1350 (url-insert b)
1351 (kill-buffer b)
1352 (goto-char (point-min)))))
1354 'silent))
1355 (package--unless-error body
1356 ;; Copy&pasted from url-insert-file-contents,
1357 ;; except it calls `url-insert' because we want the contents
1358 ;; literally (but there's no url-insert-file-contents-literally).
1359 (let ((buffer (url-retrieve-synchronously url)))
1360 (unless buffer (signal 'file-error (list url "No Data")))
1361 (when (fboundp 'url-http--insert-file-helper)
1362 ;; XXX: This is HTTP/S specific and should be moved
1363 ;; to url-http instead. See bug#17549.
1364 (url-http--insert-file-helper buffer url))
1365 (url-insert buffer)
1366 (kill-buffer buffer)
1367 (goto-char (point-min))))))
1368 (package--unless-error body
1369 (let ((url (expand-file-name file url)))
1370 (unless (file-name-absolute-p url)
1371 (error "Location %s is not a url nor an absolute file name"
1372 url))
1373 (insert-file-contents-literally url)))))
1375 (define-error 'bad-signature "Failed to verify signature")
1377 (defun package--check-signature-content (content string &optional sig-file)
1378 "Check signature CONTENT against STRING.
1379 SIG-FILE is the name of the signature file, used when signaling
1380 errors."
1381 (let ((context (epg-make-context 'OpenPGP)))
1382 (when package-gnupghome-dir
1383 (setf (epg-context-home-directory context) package-gnupghome-dir))
1384 (condition-case error
1385 (epg-verify-string context content string)
1386 (error (package--display-verify-error context sig-file)
1387 (signal 'bad-signature error)))
1388 (let (good-signatures had-fatal-error)
1389 ;; The .sig file may contain multiple signatures. Success if one
1390 ;; of the signatures is good.
1391 (dolist (sig (epg-context-result-for context 'verify))
1392 (if (eq (epg-signature-status sig) 'good)
1393 (push sig good-signatures)
1394 ;; If `package-check-signature' is allow-unsigned, don't
1395 ;; signal error when we can't verify signature because of
1396 ;; missing public key. Other errors are still treated as
1397 ;; fatal (bug#17625).
1398 (unless (and (eq (package-check-signature) 'allow-unsigned)
1399 (eq (epg-signature-status sig) 'no-pubkey))
1400 (setq had-fatal-error t))))
1401 (when (or (null good-signatures)
1402 (and (eq (package-check-signature) 'all)
1403 had-fatal-error))
1404 (package--display-verify-error context sig-file)
1405 (signal 'bad-signature (list sig-file)))
1406 good-signatures)))
1408 (defun package--check-signature (location file &optional string async callback unwind)
1409 "Check signature of the current buffer.
1410 Download the signature file from LOCATION by appending \".sig\"
1411 to FILE.
1412 GnuPG keyring location depends on `package-gnupghome-dir'.
1413 STRING is the string to verify, it defaults to `buffer-string'.
1414 If ASYNC is non-nil, the download of the signature file is
1415 done asynchronously.
1417 If the signature does not verify, signal an error.
1418 If the signature is verified and CALLBACK was provided, `funcall'
1419 CALLBACK with the list of good signatures as argument (the list
1420 can be empty).
1421 If no signatures file is found, and `package-check-signature' is
1422 `allow-unsigned', call CALLBACK with a nil argument.
1423 Otherwise, an error is signaled.
1425 UNWIND, if provided, is a function to be called after everything
1426 else, even if an error is signaled."
1427 (let ((sig-file (concat file ".sig"))
1428 (string (or string (buffer-string))))
1429 (package--with-response-buffer location :file sig-file
1430 :async async :noerror t
1431 ;; Connection error is assumed to mean "no sig-file".
1432 :error-form (let ((allow-unsigned
1433 (eq (package-check-signature) 'allow-unsigned)))
1434 (when (and callback allow-unsigned)
1435 (funcall callback nil))
1436 (when unwind (funcall unwind))
1437 (unless allow-unsigned
1438 (error "Unsigned file `%s' at %s" file location)))
1439 ;; OTOH, an error here means "bad signature", which we never
1440 ;; suppress. (Bug#22089)
1441 (unwind-protect
1442 (let ((sig (package--check-signature-content
1443 (buffer-substring (point) (point-max))
1444 string sig-file)))
1445 (when callback (funcall callback sig))
1446 sig)
1447 (when unwind (funcall unwind))))))
1449 ;;; Packages on Archives
1450 ;; The following variables store information about packages available
1451 ;; from archives. The most important of these is
1452 ;; `package-archive-contents' which is initially populated by the
1453 ;; function `package-read-all-archive-contents' from a cache on disk.
1454 ;; The `package-initialize' command is also closely related to this
1455 ;; section, but it has its own section.
1457 (defconst package-archive-version 1
1458 "Version number of the package archive understood by package.el.
1459 Lower version numbers than this will probably be understood as well.")
1461 ;; We don't prime the cache since it tends to get out of date.
1462 (defvar package-archive-contents nil
1463 "Cache of the contents of all archives in `package-archives'.
1464 This is an alist mapping package names (symbols) to
1465 non-empty lists of `package-desc' structures.")
1466 (put 'package-archive-contents 'risky-local-variable t)
1468 (defvar package--compatibility-table nil
1469 "Hash table connecting package names to their compatibility.
1470 Each key is a symbol, the name of a package.
1472 The value is either nil, representing an incompatible package, or
1473 a version list, representing the highest compatible version of
1474 that package which is available.
1476 A package is considered incompatible if it requires an Emacs
1477 version higher than the one being used. To check for package
1478 \(in)compatibility, don't read this table directly, use
1479 `package--incompatible-p' which also checks dependencies.")
1481 (defun package--build-compatibility-table ()
1482 "Build `package--compatibility-table' with `package--mapc'."
1483 ;; Initialize the list of built-ins.
1484 (require 'finder-inf nil t)
1485 ;; Build compat table.
1486 (setq package--compatibility-table (make-hash-table :test 'eq))
1487 (package--mapc #'package--add-to-compatibility-table))
1489 (defun package--add-to-compatibility-table (pkg)
1490 "If PKG is compatible (without dependencies), add to the compatibility table.
1491 PKG is a package-desc object.
1492 Only adds if its version is higher than what's already stored in
1493 the table."
1494 (unless (package--incompatible-p pkg 'shallow)
1495 (let* ((name (package-desc-name pkg))
1496 (version (or (package-desc-version pkg) '(0)))
1497 (table-version (gethash name package--compatibility-table)))
1498 (when (or (not table-version)
1499 (version-list-< table-version version))
1500 (puthash name version package--compatibility-table)))))
1502 ;; Package descriptor objects used inside the "archive-contents" file.
1503 ;; Changing this defstruct implies changing the format of the
1504 ;; "archive-contents" files.
1505 (cl-defstruct (package--ac-desc
1506 (:constructor package-make-ac-desc (version reqs summary kind extras))
1507 (:copier nil)
1508 (:type vector))
1509 version reqs summary kind extras)
1511 (defun package--append-to-alist (pkg-desc alist)
1512 "Append an entry for PKG-DESC to the start of ALIST and return it.
1513 This entry takes the form (`package-desc-name' PKG-DESC).
1515 If ALIST already has an entry with this name, destructively add
1516 PKG-DESC to the cdr of this entry instead, sorted by version
1517 number."
1518 (let* ((name (package-desc-name pkg-desc))
1519 (priority-version (package-desc-priority-version pkg-desc))
1520 (existing-packages (assq name alist)))
1521 (if (not existing-packages)
1522 (cons (list name pkg-desc)
1523 alist)
1524 (while (if (and (cdr existing-packages)
1525 (version-list-< priority-version
1526 (package-desc-priority-version
1527 (cadr existing-packages))))
1528 (setq existing-packages (cdr existing-packages))
1529 (push pkg-desc (cdr existing-packages))
1530 nil))
1531 alist)))
1533 (defun package--add-to-archive-contents (package archive)
1534 "Add the PACKAGE from the given ARCHIVE if necessary.
1535 PACKAGE should have the form (NAME . PACKAGE--AC-DESC).
1536 Also, add the originating archive to the `package-desc' structure."
1537 (let* ((name (car package))
1538 (version (package--ac-desc-version (cdr package)))
1539 (pkg-desc
1540 (package-desc-create
1541 :name name
1542 :version version
1543 :reqs (package--ac-desc-reqs (cdr package))
1544 :summary (package--ac-desc-summary (cdr package))
1545 :kind (package--ac-desc-kind (cdr package))
1546 :archive archive
1547 :extras (and (> (length (cdr package)) 4)
1548 ;; Older archive-contents files have only 4
1549 ;; elements here.
1550 (package--ac-desc-extras (cdr package)))))
1551 (pinned-to-archive (assoc name package-pinned-packages)))
1552 ;; Skip entirely if pinned to another archive.
1553 (when (not (and pinned-to-archive
1554 (not (equal (cdr pinned-to-archive) archive))))
1555 (setq package-archive-contents
1556 (package--append-to-alist pkg-desc package-archive-contents)))))
1558 (defun package--read-archive-file (file)
1559 "Read cached archive FILE data, if it exists.
1560 Return the data from the file, or nil if the file does not exist.
1561 If the archive version is too new, signal an error."
1562 (let ((filename (expand-file-name file package-user-dir)))
1563 (when (file-exists-p filename)
1564 (with-temp-buffer
1565 (let ((coding-system-for-read 'utf-8))
1566 (insert-file-contents filename))
1567 (let ((contents (read (current-buffer))))
1568 (if (> (car contents) package-archive-version)
1569 (error "Package archive version %d is higher than %d"
1570 (car contents) package-archive-version))
1571 (cdr contents))))))
1573 (defun package-read-archive-contents (archive)
1574 "Read cached archive file for ARCHIVE.
1575 If successful, set or update the variable `package-archive-contents'.
1576 ARCHIVE should be a string matching the name of a package archive
1577 in the variable `package-archives'.
1578 If the archive version is too new, signal an error."
1579 ;; Version 1 of 'archive-contents' is identical to our internal
1580 ;; representation.
1581 (let* ((contents-file (format "archives/%s/archive-contents" archive))
1582 (contents (package--read-archive-file contents-file)))
1583 (when contents
1584 (dolist (package contents)
1585 (if package
1586 (package--add-to-archive-contents package archive)
1587 (lwarn '(package refresh) :warning
1588 "Ignoring `nil' package on `%s' package archive" archive))))))
1590 (defvar package--old-archive-priorities nil
1591 "Store currently used `package-archive-priorities'.
1592 This is the value of `package-archive-priorities' last time
1593 `package-read-all-archive-contents' was called. It can be used
1594 by arbitrary functions to decide whether it is necessary to call
1595 it again.")
1597 (defun package-read-all-archive-contents ()
1598 "Read cached archive file for all archives in `package-archives'.
1599 If successful, set or update `package-archive-contents'."
1600 (setq package-archive-contents nil)
1601 (setq package--old-archive-priorities package-archive-priorities)
1602 (dolist (archive package-archives)
1603 (package-read-archive-contents (car archive))))
1606 ;;;; Package Initialize
1607 ;; A bit of a milestone. This brings together some of the above
1608 ;; sections and populates all relevant lists of packages from contents
1609 ;; available on disk.
1611 (defvar package--initialized nil
1612 "Non-nil if `package-initialize' has been run.")
1614 ;;;###autoload
1615 (defvar package--activated nil
1616 "Non-nil if `package-activate-all' has been run.")
1618 ;;;###autoload
1619 (defun package-initialize (&optional no-activate)
1620 "Load Emacs Lisp packages, and activate them.
1621 The variable `package-load-list' controls which packages to load.
1622 If optional arg NO-ACTIVATE is non-nil, don't activate packages.
1624 It is not necessary to adjust `load-path' or `require' the
1625 individual packages after calling `package-initialize' -- this is
1626 taken care of by `package-initialize'.
1628 If `package-initialize' is called twice during Emacs startup,
1629 signal a warning, since this is a bad idea except in highly
1630 advanced use cases. To suppress the warning, remove the
1631 superfluous call to `package-initialize' from your init-file. If
1632 you have code which must run before `package-initialize', put
1633 that code in the early init-file."
1634 (interactive)
1635 (when (and package--initialized (not after-init-time))
1636 (lwarn '(package reinitialization) :warning
1637 "Unnecessary call to `package-initialize' in init file"))
1638 (setq package-alist nil)
1639 (package-load-all-descriptors)
1640 (package-read-all-archive-contents)
1641 (setq package--initialized t)
1642 (unless no-activate
1643 (package-activate-all))
1644 ;; This uses `package--mapc' so it must be called after
1645 ;; `package--initialized' is t.
1646 (package--build-compatibility-table))
1648 ;;;###autoload
1649 (progn ;; Make the function usable without loading `package.el'.
1650 (defun package-activate-all ()
1651 "Activate all installed packages.
1652 The variable `package-load-list' controls which packages to load."
1653 (setq package--activated t)
1654 (let* ((elc (concat package-quickstart-file "c"))
1655 (qs (if (file-readable-p elc) elc
1656 (if (file-readable-p package-quickstart-file)
1657 package-quickstart-file))))
1658 (if qs
1659 ;; Skip load-source-file-function which would slow us down by a factor
1660 ;; 2 when loading the .el file (this assumes we were careful to
1661 ;; save this file so it doesn't need any decoding).
1662 (let ((load-source-file-function nil))
1663 (unless (boundp 'package-activated-list)
1664 (setq package-activated-list nil))
1665 (load qs nil 'nomessage))
1666 (require 'package)
1667 (package--activate-all)))))
1669 (defun package--activate-all ()
1670 (dolist (elt (package--alist))
1671 (condition-case err
1672 (package-activate (car elt))
1673 ;; Don't let failure of activation of a package arbitrarily stop
1674 ;; activation of further packages.
1675 (error (message "%s" (error-message-string err))))))
1677 ;;;; Populating `package-archive-contents' from archives
1678 ;; This subsection populates the variables listed above from the
1679 ;; actual archives, instead of from a local cache.
1681 (defvar package--downloads-in-progress nil
1682 "List of in-progress asynchronous downloads.")
1684 (declare-function epg-import-keys-from-file "epg" (context keys))
1686 ;;;###autoload
1687 (defun package-import-keyring (&optional file)
1688 "Import keys from FILE."
1689 (interactive "fFile: ")
1690 (setq file (expand-file-name file))
1691 (let ((context (epg-make-context 'OpenPGP)))
1692 (when package-gnupghome-dir
1693 (with-file-modes 448
1694 (make-directory package-gnupghome-dir t))
1695 (setf (epg-context-home-directory context) package-gnupghome-dir))
1696 (message "Importing %s..." (file-name-nondirectory file))
1697 (epg-import-keys-from-file context file)
1698 (message "Importing %s...done" (file-name-nondirectory file))))
1700 (defvar package--post-download-archives-hook nil
1701 "Hook run after the archive contents are downloaded.
1702 Don't run this hook directly. It is meant to be run as part of
1703 `package--update-downloads-in-progress'.")
1704 (put 'package--post-download-archives-hook 'risky-local-variable t)
1706 (defun package--update-downloads-in-progress (entry)
1707 "Remove ENTRY from `package--downloads-in-progress'.
1708 Once it's empty, run `package--post-download-archives-hook'."
1709 ;; Keep track of the downloading progress.
1710 (setq package--downloads-in-progress
1711 (remove entry package--downloads-in-progress))
1712 ;; If this was the last download, run the hook.
1713 (unless package--downloads-in-progress
1714 (package-read-all-archive-contents)
1715 (package--build-compatibility-table)
1716 ;; We message before running the hook, so the hook can give
1717 ;; messages as well.
1718 (message "Package refresh done")
1719 (run-hooks 'package--post-download-archives-hook)))
1721 (defun package--download-one-archive (archive file &optional async)
1722 "Retrieve an archive file FILE from ARCHIVE, and cache it.
1723 ARCHIVE should be a cons cell of the form (NAME . LOCATION),
1724 similar to an entry in `package-alist'. Save the cached copy to
1725 \"archives/NAME/FILE\" in `package-user-dir'."
1726 (package--with-response-buffer (cdr archive) :file file
1727 :async async
1728 :error-form (package--update-downloads-in-progress archive)
1729 (let* ((location (cdr archive))
1730 (name (car archive))
1731 (content (buffer-string))
1732 (dir (expand-file-name (concat "archives/" name) package-user-dir))
1733 (local-file (expand-file-name file dir)))
1734 (when (listp (read content))
1735 (make-directory dir t)
1736 (if (or (not (package-check-signature))
1737 (member name package-unsigned-archives))
1738 ;; If we don't care about the signature, save the file and
1739 ;; we're done.
1740 (progn
1741 (cl-assert (not enable-multibyte-characters))
1742 (let ((coding-system-for-write 'binary))
1743 (write-region content nil local-file nil 'silent))
1744 (package--update-downloads-in-progress archive))
1745 ;; If we care, check it (perhaps async) and *then* write the file.
1746 (package--check-signature
1747 location file content async
1748 ;; This function will be called after signature checking.
1749 (lambda (&optional good-sigs)
1750 (cl-assert (not enable-multibyte-characters))
1751 (let ((coding-system-for-write 'binary))
1752 (write-region content nil local-file nil 'silent))
1753 ;; Write out good signatures into archive-contents.signed file.
1754 (when good-sigs
1755 (write-region (mapconcat #'epg-signature-to-string good-sigs "\n")
1756 nil (concat local-file ".signed") nil 'silent)))
1757 (lambda () (package--update-downloads-in-progress archive))))))))
1759 (defun package--download-and-read-archives (&optional async)
1760 "Download descriptions of all `package-archives' and read them.
1761 Populate `package-archive-contents' with the result.
1763 If optional argument ASYNC is non-nil, perform the downloads
1764 asynchronously."
1765 ;; The downloaded archive contents will be read as part of
1766 ;; `package--update-downloads-in-progress'.
1767 (dolist (archive package-archives)
1768 (cl-pushnew archive package--downloads-in-progress
1769 :test #'equal))
1770 (dolist (archive package-archives)
1771 (condition-case-unless-debug nil
1772 (package--download-one-archive archive "archive-contents" async)
1773 (error (message "Failed to download `%s' archive."
1774 (car archive))))))
1776 ;;;###autoload
1777 (defun package-refresh-contents (&optional async)
1778 "Download descriptions of all configured ELPA packages.
1779 For each archive configured in the variable `package-archives',
1780 inform Emacs about the latest versions of all packages it offers,
1781 and make them available for download.
1782 Optional argument ASYNC specifies whether to perform the
1783 downloads in the background."
1784 (interactive)
1785 (unless (file-exists-p package-user-dir)
1786 (make-directory package-user-dir t))
1787 (let ((default-keyring (expand-file-name "package-keyring.gpg"
1788 data-directory))
1789 (inhibit-message (or inhibit-message async)))
1790 (when (and (package-check-signature) (file-exists-p default-keyring))
1791 (condition-case-unless-debug error
1792 (package-import-keyring default-keyring)
1793 (error (message "Cannot import default keyring: %S" (cdr error))))))
1794 (package--download-and-read-archives async))
1797 ;;; Dependency Management
1798 ;; Calculating the full transaction necessary for an installation,
1799 ;; keeping track of which packages were installed strictly as
1800 ;; dependencies, and determining which packages cannot be removed
1801 ;; because they are dependencies.
1803 (defun package-compute-transaction (packages requirements &optional seen)
1804 "Return a list of packages to be installed, including PACKAGES.
1805 PACKAGES should be a list of `package-desc'.
1807 REQUIREMENTS should be a list of additional requirements; each
1808 element in this list should have the form (PACKAGE VERSION-LIST),
1809 where PACKAGE is a package name and VERSION-LIST is the required
1810 version of that package.
1812 This function recursively computes the requirements of the
1813 packages in REQUIREMENTS, and returns a list of all the packages
1814 that must be installed. Packages that are already installed are
1815 not included in this list.
1817 SEEN is used internally to detect infinite recursion."
1818 ;; FIXME: We really should use backtracking to explore the whole
1819 ;; search space (e.g. if foo require bar-1.3, and bar-1.4 requires toto-1.1
1820 ;; whereas bar-1.3 requires toto-1.0 and the user has put a hold on toto-1.0:
1821 ;; the current code might fail to see that it could install foo by using the
1822 ;; older bar-1.3).
1823 (dolist (elt requirements)
1824 (let* ((next-pkg (car elt))
1825 (next-version (cadr elt))
1826 (already ()))
1827 (dolist (pkg packages)
1828 (if (eq next-pkg (package-desc-name pkg))
1829 (setq already pkg)))
1830 (when already
1831 (if (version-list-<= next-version (package-desc-version already))
1832 ;; `next-pkg' is already in `packages', but its position there
1833 ;; means it might be installed too late: remove it from there, so
1834 ;; we re-add it (along with its dependencies) at an earlier place
1835 ;; below (bug#16994).
1836 (if (memq already seen) ;Avoid inf-loop on dependency cycles.
1837 (message "Dependency cycle going through %S"
1838 (package-desc-full-name already))
1839 (setq packages (delq already packages))
1840 (setq already nil))
1841 (error "Need package `%s-%s', but only %s is being installed"
1842 next-pkg (package-version-join next-version)
1843 (package-version-join (package-desc-version already)))))
1844 (cond
1845 (already nil)
1846 ((package-installed-p next-pkg next-version) nil)
1849 ;; A package is required, but not installed. It might also be
1850 ;; blocked via `package-load-list'.
1851 (let ((pkg-descs (cdr (assq next-pkg package-archive-contents)))
1852 (found nil)
1853 (found-something nil)
1854 (problem nil))
1855 (while (and pkg-descs (not found))
1856 (let* ((pkg-desc (pop pkg-descs))
1857 (version (package-desc-version pkg-desc))
1858 (disabled (package-disabled-p next-pkg version)))
1859 (cond
1860 ((version-list-< version next-version)
1861 ;; pkg-descs is sorted by priority, not version, so
1862 ;; don't error just yet.
1863 (unless found-something
1864 (setq found-something (package-version-join version))))
1865 (disabled
1866 (unless problem
1867 (setq problem
1868 (if (stringp disabled)
1869 (format-message
1870 "Package `%s' held at version %s, but version %s required"
1871 next-pkg disabled
1872 (package-version-join next-version))
1873 (format-message "Required package `%s' is disabled"
1874 next-pkg)))))
1875 (t (setq found pkg-desc)))))
1876 (unless found
1877 (cond
1878 (problem (error "%s" problem))
1879 (found-something
1880 (error "Need package `%s-%s', but only %s is available"
1881 next-pkg (package-version-join next-version)
1882 found-something))
1883 (t (error "Package `%s-%s' is unavailable"
1884 next-pkg (package-version-join next-version)))))
1885 (setq packages
1886 (package-compute-transaction (cons found packages)
1887 (package-desc-reqs found)
1888 (cons found seen))))))))
1889 packages)
1891 (defun package--find-non-dependencies ()
1892 "Return a list of installed packages which are not dependencies.
1893 Finds all packages in `package-alist' which are not dependencies
1894 of any other packages.
1895 Used to populate `package-selected-packages'."
1896 (let ((dep-list
1897 (delete-dups
1898 (apply #'append
1899 (mapcar (lambda (p) (mapcar #'car (package-desc-reqs (cadr p))))
1900 package-alist)))))
1901 (cl-loop for p in package-alist
1902 for name = (car p)
1903 unless (memq name dep-list)
1904 collect name)))
1906 (defun package--save-selected-packages (&optional value)
1907 "Set and save `package-selected-packages' to VALUE."
1908 (when value
1909 (setq package-selected-packages value))
1910 (if after-init-time
1911 (customize-save-variable 'package-selected-packages package-selected-packages)
1912 (add-hook 'after-init-hook #'package--save-selected-packages)))
1914 (defun package--user-selected-p (pkg)
1915 "Return non-nil if PKG is a package was installed by the user.
1916 PKG is a package name.
1917 This looks into `package-selected-packages', populating it first
1918 if it is still empty."
1919 (unless (consp package-selected-packages)
1920 (package--save-selected-packages (package--find-non-dependencies)))
1921 (memq pkg package-selected-packages))
1923 (defun package--get-deps (pkgs)
1924 (let ((seen '()))
1925 (while pkgs
1926 (let ((pkg (pop pkgs)))
1927 (if (memq pkg seen)
1928 nil ;; Done already!
1929 (let ((pkg-desc (cadr (assq pkg package-alist))))
1930 (when pkg-desc
1931 (push pkg seen)
1932 (setq pkgs (append (mapcar #'car (package-desc-reqs pkg-desc))
1933 pkgs)))))))
1934 seen))
1936 (defun package--user-installed-p (package)
1937 "Return non-nil if PACKAGE is a user-installed package.
1938 PACKAGE is the package name, a symbol. Check whether the package
1939 was installed into `package-user-dir' where we assume to have
1940 control over."
1941 (let* ((pkg-desc (cadr (assq package package-alist)))
1942 (dir (package-desc-dir pkg-desc)))
1943 (file-in-directory-p dir package-user-dir)))
1945 (defun package--removable-packages ()
1946 "Return a list of names of packages no longer needed.
1947 These are packages which are neither contained in
1948 `package-selected-packages' nor a dependency of one that is."
1949 (let ((needed (package--get-deps package-selected-packages)))
1950 (cl-loop for p in (mapcar #'car package-alist)
1951 unless (or (memq p needed)
1952 ;; Do not auto-remove external packages.
1953 (not (package--user-installed-p p)))
1954 collect p)))
1956 (defun package--used-elsewhere-p (pkg-desc &optional pkg-list all)
1957 "Non-nil if PKG-DESC is a dependency of a package in PKG-LIST.
1958 Return the first package found in PKG-LIST of which PKG is a
1959 dependency. If ALL is non-nil, return all such packages instead.
1961 When not specified, PKG-LIST defaults to `package-alist'
1962 with PKG-DESC entry removed."
1963 (unless (string= (package-desc-status pkg-desc) "obsolete")
1964 (let* ((pkg (package-desc-name pkg-desc))
1965 (alist (or pkg-list
1966 (remove (assq pkg package-alist)
1967 package-alist))))
1968 (if all
1969 (cl-loop for p in alist
1970 if (assq pkg (package-desc-reqs (cadr p)))
1971 collect (cadr p))
1972 (cl-loop for p in alist thereis
1973 (and (assq pkg (package-desc-reqs (cadr p)))
1974 (cadr p)))))))
1976 (defun package--sort-deps-in-alist (package only)
1977 "Return a list of dependencies for PACKAGE sorted by dependency.
1978 PACKAGE is included as the first element of the returned list.
1979 ONLY is an alist associating package names to package objects.
1980 Only these packages will be in the return value and their cdrs are
1981 destructively set to nil in ONLY."
1982 (let ((out))
1983 (dolist (dep (package-desc-reqs package))
1984 (when-let* ((cell (assq (car dep) only))
1985 (dep-package (cdr-safe cell)))
1986 (setcdr cell nil)
1987 (setq out (append (package--sort-deps-in-alist dep-package only)
1988 out))))
1989 (cons package out)))
1991 (defun package--sort-by-dependence (package-list)
1992 "Return PACKAGE-LIST sorted by dependence.
1993 That is, any element of the returned list is guaranteed to not
1994 directly depend on any elements that come before it.
1996 PACKAGE-LIST is a list of `package-desc' objects.
1997 Indirect dependencies are guaranteed to be returned in order only
1998 if all the in-between dependencies are also in PACKAGE-LIST."
1999 (let ((alist (mapcar (lambda (p) (cons (package-desc-name p) p)) package-list))
2000 out-list)
2001 (dolist (cell alist out-list)
2002 ;; `package--sort-deps-in-alist' destructively changes alist, so
2003 ;; some cells might already be empty. We check this here.
2004 (when-let* ((pkg-desc (cdr cell)))
2005 (setcdr cell nil)
2006 (setq out-list
2007 (append (package--sort-deps-in-alist pkg-desc alist)
2008 out-list))))))
2011 ;;; Installation Functions
2012 ;; As opposed to the previous section (which listed some underlying
2013 ;; functions necessary for installation), this one contains the actual
2014 ;; functions that install packages. The package itself can be
2015 ;; installed in a variety of ways (archives, buffer, file), but
2016 ;; requirements (dependencies) are always satisfied by looking in
2017 ;; `package-archive-contents'.
2019 (defun package-archive-base (desc)
2020 "Return the package described by DESC."
2021 (cdr (assoc (package-desc-archive desc) package-archives)))
2023 (defun package-install-from-archive (pkg-desc)
2024 "Download and install a tar package defined by PKG-DESC."
2025 ;; This won't happen, unless the archive is doing something wrong.
2026 (when (eq (package-desc-kind pkg-desc) 'dir)
2027 (error "Can't install directory package from archive"))
2028 (let* ((location (package-archive-base pkg-desc))
2029 (file (concat (package-desc-full-name pkg-desc)
2030 (package-desc-suffix pkg-desc))))
2031 (package--with-response-buffer location :file file
2032 (if (or (not (package-check-signature))
2033 (member (package-desc-archive pkg-desc)
2034 package-unsigned-archives))
2035 ;; If we don't care about the signature, unpack and we're
2036 ;; done.
2037 (let ((save-silently t))
2038 (package-unpack pkg-desc))
2039 ;; If we care, check it and *then* write the file.
2040 (let ((content (buffer-string)))
2041 (package--check-signature
2042 location file content nil
2043 ;; This function will be called after signature checking.
2044 (lambda (&optional good-sigs)
2045 ;; Signature checked, unpack now.
2046 (with-temp-buffer ;FIXME: Just use the previous current-buffer.
2047 (set-buffer-multibyte nil)
2048 (cl-assert (not (multibyte-string-p content)))
2049 (insert content)
2050 (let ((save-silently t))
2051 (package-unpack pkg-desc)))
2052 ;; Here the package has been installed successfully, mark it as
2053 ;; signed if appropriate.
2054 (when good-sigs
2055 ;; Write out good signatures into NAME-VERSION.signed file.
2056 (write-region (mapconcat #'epg-signature-to-string good-sigs "\n")
2058 (expand-file-name
2059 (concat (package-desc-full-name pkg-desc) ".signed")
2060 package-user-dir)
2061 nil 'silent)
2062 ;; Update the old pkg-desc which will be shown on the description buffer.
2063 (setf (package-desc-signed pkg-desc) t)
2064 ;; Update the new (activated) pkg-desc as well.
2065 (when-let* ((pkg-descs (cdr (assq (package-desc-name pkg-desc)
2066 package-alist))))
2067 (setf (package-desc-signed (car pkg-descs)) t))))))))))
2069 (defun package-installed-p (package &optional min-version)
2070 "Return non-nil if PACKAGE, of MIN-VERSION or newer, is installed.
2071 If PACKAGE is a symbol, it is the package name and MIN-VERSION
2072 should be a version list.
2074 If PACKAGE is a `package-desc' object, MIN-VERSION is ignored."
2075 (cond
2076 ((package-desc-p package)
2077 (let ((dir (package-desc-dir package)))
2078 (and (stringp dir)
2079 (file-exists-p dir))))
2080 ((and (not package--initialized)
2081 (null min-version)
2082 package-activated-list)
2083 ;; We used the quickstart: make it possible to use package-installed-p
2084 ;; even before package is fully initialized.
2085 (memq package package-activated-list))
2088 (let ((pkg-descs (cdr (assq package (package--alist)))))
2089 (and pkg-descs
2090 (version-list-<= min-version
2091 (package-desc-version (car pkg-descs)))))
2092 ;; Also check built-in packages.
2093 (package-built-in-p package min-version)))))
2095 (defun package-download-transaction (packages)
2096 "Download and install all the packages in PACKAGES.
2097 PACKAGES should be a list of `package-desc'.
2098 This function assumes that all package requirements in
2099 PACKAGES are satisfied, i.e. that PACKAGES is computed
2100 using `package-compute-transaction'."
2101 (mapc #'package-install-from-archive packages))
2103 (defun package--archives-initialize ()
2104 "Make sure the list of installed and remote packages are initialized."
2105 (unless package--initialized
2106 (package-initialize t))
2107 (unless package-archive-contents
2108 (package-refresh-contents)))
2110 ;;;###autoload
2111 (defun package-install (pkg &optional dont-select)
2112 "Install the package PKG.
2113 PKG can be a `package-desc' or a symbol naming one of the
2114 available packages in an archive in `package-archives'. When
2115 called interactively, prompt for the package name.
2117 Mark the installed package as selected by adding it to
2118 `package-selected-packages'.
2120 When called from Lisp and optional argument DONT-SELECT is
2121 non-nil, install the package but do not add it to
2122 `package-selected-packages'.
2124 If PKG is a `package-desc' and it is already installed, don't try
2125 to install it but still mark it as selected."
2126 (interactive
2127 (progn
2128 ;; Initialize the package system to get the list of package
2129 ;; symbols for completion.
2130 (package--archives-initialize)
2131 (list (intern (completing-read
2132 "Install package: "
2133 (delq nil
2134 (mapcar (lambda (elt)
2135 (unless (package-installed-p (car elt))
2136 (symbol-name (car elt))))
2137 package-archive-contents))
2138 nil t))
2139 nil)))
2140 (package--archives-initialize)
2141 (add-hook 'post-command-hook #'package-menu--post-refresh)
2142 (let ((name (if (package-desc-p pkg)
2143 (package-desc-name pkg)
2144 pkg)))
2145 (unless (or dont-select (package--user-selected-p name))
2146 (package--save-selected-packages
2147 (cons name package-selected-packages)))
2148 (if-let* ((transaction
2149 (if (package-desc-p pkg)
2150 (unless (package-installed-p pkg)
2151 (package-compute-transaction (list pkg)
2152 (package-desc-reqs pkg)))
2153 (package-compute-transaction () (list (list pkg))))))
2154 (progn
2155 (package-download-transaction transaction)
2156 (package--quickstart-maybe-refresh)
2157 (message "Package `%s' installed." name))
2158 (message "`%s' is already installed" name))))
2160 (defun package-strip-rcs-id (str)
2161 "Strip RCS version ID from the version string STR.
2162 If the result looks like a dotted numeric version, return it.
2163 Otherwise return nil."
2164 (when str
2165 (when (string-match "\\`[ \t]*[$]Revision:[ \t]+" str)
2166 (setq str (substring str (match-end 0))))
2167 (let ((l (version-to-list str)))
2168 ;; Don't return `str' but (package-version-join (version-to-list str))
2169 ;; to make sure we use a "canonical name"!
2170 (if l (package-version-join l)))))
2172 (declare-function lm-homepage "lisp-mnt" (&optional file))
2174 ;;;###autoload
2175 (defun package-install-from-buffer ()
2176 "Install a package from the current buffer.
2177 The current buffer is assumed to be a single .el or .tar file or
2178 a directory. These must follow the packaging guidelines (see
2179 info node `(elisp)Packaging').
2181 Specially, if current buffer is a directory, the -pkg.el
2182 description file is not mandatory, in which case the information
2183 is derived from the main .el file in the directory.
2185 Downloads and installs required packages as needed."
2186 (interactive)
2187 (let* ((pkg-desc
2188 (cond
2189 ((derived-mode-p 'dired-mode)
2190 ;; This is the only way a package-desc object with a `dir'
2191 ;; desc-kind can be created. Such packages can't be
2192 ;; uploaded or installed from archives, they can only be
2193 ;; installed from local buffers or directories.
2194 (package-dir-info))
2195 ((derived-mode-p 'tar-mode)
2196 (package-tar-file-info))
2198 (save-excursion
2199 (package-buffer-info)))))
2200 (name (package-desc-name pkg-desc)))
2201 ;; Download and install the dependencies.
2202 (let* ((requires (package-desc-reqs pkg-desc))
2203 (transaction (package-compute-transaction nil requires)))
2204 (package-download-transaction transaction))
2205 ;; Install the package itself.
2206 (package-unpack pkg-desc)
2207 (unless (package--user-selected-p name)
2208 (package--save-selected-packages
2209 (cons name package-selected-packages)))
2210 (package--quickstart-maybe-refresh)
2211 pkg-desc))
2213 ;;;###autoload
2214 (defun package-install-file (file)
2215 "Install a package from FILE.
2216 The file can either be a tar file, an Emacs Lisp file, or a
2217 directory."
2218 (interactive "fPackage file name: ")
2219 (with-temp-buffer
2220 (if (file-directory-p file)
2221 (progn
2222 (setq default-directory file)
2223 (dired-mode))
2224 (insert-file-contents-literally file)
2225 (when (string-match "\\.tar\\'" file) (tar-mode)))
2226 (package-install-from-buffer)))
2228 ;;;###autoload
2229 (defun package-install-selected-packages (&optional noconfirm)
2230 "Ensure packages in `package-selected-packages' are installed.
2231 If some packages are not installed, propose to install them.
2232 If optional argument NOCONFIRM is non-nil, don't ask for
2233 confirmation to install packages."
2234 (interactive)
2235 (package--archives-initialize)
2236 ;; We don't need to populate `package-selected-packages' before
2237 ;; using here, because the outcome is the same either way (nothing
2238 ;; gets installed).
2239 (if (not package-selected-packages)
2240 (message "`package-selected-packages' is empty, nothing to install")
2241 (let* ((not-installed (seq-remove #'package-installed-p package-selected-packages))
2242 (available (seq-filter (lambda (p) (assq p package-archive-contents)) not-installed))
2243 (difference (- (length not-installed) (length available))))
2244 (cond
2245 (available
2246 (when (or noconfirm
2247 (y-or-n-p
2248 (format "Packages to install: %d (%s), proceed? "
2249 (length available)
2250 (mapconcat #'symbol-name available " "))))
2251 (mapc (lambda (p) (package-install p 'dont-select)) available)))
2252 ((> difference 0)
2253 (message "Packages that are not available: %d (the rest is already installed), maybe you need to `M-x package-refresh-contents'"
2254 difference))
2256 (message "All your packages are already installed"))))))
2259 ;;; Package Deletion
2261 (defun package--newest-p (pkg)
2262 "Return non-nil if PKG is the newest package with its name."
2263 (equal (cadr (assq (package-desc-name pkg) package-alist))
2264 pkg))
2266 (declare-function comp-el-to-eln-filename "comp.c")
2267 (defun package--delete-directory (dir)
2268 "Delete DIR recursively.
2269 Clean-up the corresponding .eln files if Emacs is native
2270 compiled."
2271 (when (featurep 'native-compile)
2272 (cl-loop
2273 for file in (directory-files-recursively dir "\\.el\\'")
2274 do (comp-clean-up-stale-eln (comp-el-to-eln-filename file))))
2275 (delete-directory dir t))
2277 (defun package-delete (pkg-desc &optional force nosave)
2278 "Delete package PKG-DESC.
2280 Argument PKG-DESC is a full description of package as vector.
2281 Interactively, prompt the user for the package name and version.
2283 When package is used elsewhere as dependency of another package,
2284 refuse deleting it and return an error.
2285 If prefix argument FORCE is non-nil, package will be deleted even
2286 if it is used elsewhere.
2287 If NOSAVE is non-nil, the package is not removed from
2288 `package-selected-packages'."
2289 (interactive
2290 (progn
2291 (let* ((package-table
2292 (mapcar
2293 (lambda (p) (cons (package-desc-full-name p) p))
2294 (delq nil
2295 (mapcar (lambda (p) (unless (package-built-in-p p) p))
2296 (apply #'append (mapcar #'cdr (package--alist)))))))
2297 (package-name (completing-read "Delete package: "
2298 (mapcar #'car package-table)
2299 nil t)))
2300 (list (cdr (assoc package-name package-table))
2301 current-prefix-arg nil))))
2302 (let ((dir (package-desc-dir pkg-desc))
2303 (name (package-desc-name pkg-desc))
2304 pkg-used-elsewhere-by)
2305 ;; If the user is trying to delete this package, they definitely
2306 ;; don't want it marked as selected, so we remove it from
2307 ;; `package-selected-packages' even if it can't be deleted.
2308 (when (and (null nosave)
2309 (package--user-selected-p name)
2310 ;; Don't deselect if this is an older version of an
2311 ;; upgraded package.
2312 (package--newest-p pkg-desc))
2313 (package--save-selected-packages (remove name package-selected-packages)))
2314 (cond ((not (string-prefix-p (file-name-as-directory
2315 (expand-file-name package-user-dir))
2316 (expand-file-name dir)))
2317 ;; Don't delete "system" packages.
2318 (error "Package `%s' is a system package, not deleting"
2319 (package-desc-full-name pkg-desc)))
2320 ((and (null force)
2321 (setq pkg-used-elsewhere-by
2322 (package--used-elsewhere-p pkg-desc)))
2323 ;; Don't delete packages used as dependency elsewhere.
2324 (error "Package `%s' is used by `%s' as dependency, not deleting"
2325 (package-desc-full-name pkg-desc)
2326 (package-desc-name pkg-used-elsewhere-by)))
2328 (add-hook 'post-command-hook #'package-menu--post-refresh)
2329 (package--delete-directory dir)
2330 ;; Remove NAME-VERSION.signed and NAME-readme.txt files.
2332 ;; NAME-readme.txt files are no longer created, but they
2333 ;; may be left around from an earlier install.
2334 (dolist (suffix '(".signed" "readme.txt"))
2335 (let* ((version (package-version-join (package-desc-version pkg-desc)))
2336 (file (concat (if (string= suffix ".signed")
2338 (substring dir 0 (- (length version))))
2339 suffix)))
2340 (when (file-exists-p file)
2341 (delete-file file))))
2342 ;; Update package-alist.
2343 (let ((pkgs (assq name package-alist)))
2344 (delete pkg-desc pkgs)
2345 (unless (cdr pkgs)
2346 (setq package-alist (delq pkgs package-alist))))
2347 (package--quickstart-maybe-refresh)
2348 (message "Package `%s' deleted."
2349 (package-desc-full-name pkg-desc))))))
2351 ;;;###autoload
2352 (defun package-reinstall (pkg)
2353 "Reinstall package PKG.
2354 PKG should be either a symbol, the package name, or a `package-desc'
2355 object."
2356 (interactive (list (intern (completing-read
2357 "Reinstall package: "
2358 (mapcar #'symbol-name
2359 (mapcar #'car package-alist))))))
2360 (package-delete
2361 (if (package-desc-p pkg) pkg (cadr (assq pkg package-alist)))
2362 'force 'nosave)
2363 (package-install pkg 'dont-select))
2365 ;;;###autoload
2366 (defun package-autoremove ()
2367 "Remove packages that are no longer needed.
2369 Packages that are no more needed by other packages in
2370 `package-selected-packages' and their dependencies
2371 will be deleted."
2372 (interactive)
2373 ;; If `package-selected-packages' is nil, it would make no sense to
2374 ;; try to populate it here, because then `package-autoremove' will
2375 ;; do absolutely nothing.
2376 (when (or package-selected-packages
2377 (yes-or-no-p
2378 (format-message
2379 "`package-selected-packages' is empty! Really remove ALL packages? ")))
2380 (let ((removable (package--removable-packages)))
2381 (if removable
2382 (when (y-or-n-p
2383 (format "Packages to delete: %d (%s), proceed? "
2384 (length removable)
2385 (mapconcat #'symbol-name removable " ")))
2386 (mapc (lambda (p)
2387 (package-delete (cadr (assq p package-alist)) t))
2388 removable))
2389 (message "Nothing to autoremove")))))
2392 ;;;; Package description buffer.
2394 ;;;###autoload
2395 (defun describe-package (package)
2396 "Display the full documentation of PACKAGE (a symbol)."
2397 (interactive
2398 (let* ((guess (or (function-called-at-point)
2399 (symbol-at-point))))
2400 (require 'finder-inf nil t)
2401 ;; Load the package list if necessary (but don't activate them).
2402 (unless package--initialized
2403 (package-initialize t))
2404 (let ((packages (append (mapcar #'car package-alist)
2405 (mapcar #'car package-archive-contents)
2406 (mapcar #'car package--builtins))))
2407 (unless (memq guess packages)
2408 (setq guess nil))
2409 (setq packages (mapcar #'symbol-name packages))
2410 (let ((val
2411 (completing-read (format-prompt "Describe package" guess)
2412 packages nil t nil nil (when guess
2413 (symbol-name guess)))))
2414 (list (and (> (length val) 0) (intern val)))))))
2415 (if (not (or (package-desc-p package) (and package (symbolp package))))
2416 (message "No package specified")
2417 (help-setup-xref (list #'describe-package package)
2418 (called-interactively-p 'interactive))
2419 (with-help-window (help-buffer)
2420 (with-current-buffer standard-output
2421 (describe-package-1 package)))))
2423 (defface package-help-section-name
2424 '((t :inherit (bold font-lock-function-name-face)))
2425 "Face used on section names in package description buffers."
2426 :version "25.1")
2428 (defun package--print-help-section (name &rest strings)
2429 "Print \"NAME: \", right aligned to the 13th column.
2430 If more STRINGS are provided, insert them followed by a newline.
2431 Otherwise no newline is inserted."
2432 (declare (indent 1))
2433 (insert (make-string (max 0 (- 11 (string-width name))) ?\s)
2434 (propertize (concat name ": ") 'font-lock-face 'package-help-section-name))
2435 (when strings
2436 (apply #'insert strings)
2437 (insert "\n")))
2439 (declare-function lm-commentary "lisp-mnt" (&optional file))
2441 (defun package--get-description (desc)
2442 "Return a string containing the long description of the package DESC.
2443 The description is read from the installed package files."
2444 ;; Installed packages have nil for kind, so we look for README
2445 ;; first, then fall back to the Commentary header.
2447 ;; We don’t include README.md here, because that is often the home
2448 ;; page on a site like github, and not suitable as the package long
2449 ;; description.
2450 (let ((files '("README-elpa" "README-elpa.md" "README" "README.rst" "README.org"))
2451 file
2452 (srcdir (package-desc-dir desc))
2453 result)
2454 (while (and files
2455 (not result))
2456 (setq file (pop files))
2457 (when (file-readable-p (expand-file-name file srcdir))
2458 ;; Found a README.
2459 (with-temp-buffer
2460 (insert-file-contents (expand-file-name file srcdir))
2461 (setq result (buffer-string)))))
2464 result
2466 ;; Look for Commentary header.
2467 (lm-commentary (expand-file-name
2468 (format "%s.el" (package-desc-name desc)) srcdir))
2469 "")))
2471 (defun describe-package-1 (pkg)
2472 "Insert the package description for PKG.
2473 Helper function for `describe-package'."
2474 (require 'lisp-mnt)
2475 (let* ((desc (or
2476 (if (package-desc-p pkg) pkg)
2477 (cadr (assq pkg package-alist))
2478 (let ((built-in (assq pkg package--builtins)))
2479 (if built-in
2480 (package--from-builtin built-in)
2481 (cadr (assq pkg package-archive-contents))))))
2482 (name (if desc (package-desc-name desc) pkg))
2483 (pkg-dir (if desc (package-desc-dir desc)))
2484 (reqs (if desc (package-desc-reqs desc)))
2485 (required-by (if desc (package--used-elsewhere-p desc nil 'all)))
2486 (version (if desc (package-desc-version desc)))
2487 (archive (if desc (package-desc-archive desc)))
2488 (extras (and desc (package-desc-extras desc)))
2489 (homepage (cdr (assoc :url extras)))
2490 (commit (cdr (assoc :commit extras)))
2491 (keywords (if desc (package-desc--keywords desc)))
2492 (built-in (eq pkg-dir 'builtin))
2493 (installable (and archive (not built-in)))
2494 (status (if desc (package-desc-status desc) "orphan"))
2495 (incompatible-reason (package--incompatible-p desc))
2496 (signed (if desc (package-desc-signed desc)))
2497 (maintainer (cdr (assoc :maintainer extras)))
2498 (authors (cdr (assoc :authors extras))))
2499 (when (string= status "avail-obso")
2500 (setq status "available obsolete"))
2501 (when incompatible-reason
2502 (setq status "incompatible"))
2503 (princ (format "Package %S is %s.\n\n" name status))
2505 ;; TODO: Remove the string decorations and reformat the strings
2506 ;; for future l10n.
2507 (package--print-help-section "Status")
2508 (cond (built-in
2509 (insert (propertize (capitalize status)
2510 'font-lock-face 'package-status-built-in)
2511 "."))
2512 (pkg-dir
2513 (insert (propertize (if (member status '("unsigned" "dependency"))
2514 "Installed"
2515 (capitalize status))
2516 'font-lock-face 'package-status-built-in))
2517 (insert (substitute-command-keys " in `"))
2518 (let ((dir (abbreviate-file-name
2519 (file-name-as-directory
2520 (if (file-in-directory-p pkg-dir package-user-dir)
2521 (file-relative-name pkg-dir package-user-dir)
2522 pkg-dir)))))
2523 (help-insert-xref-button dir 'help-package-def pkg-dir))
2524 (if (and (package-built-in-p name)
2525 (not (package-built-in-p name version)))
2526 (insert (substitute-command-keys
2527 "',\n shadowing a ")
2528 (propertize "built-in package"
2529 'font-lock-face 'package-status-built-in))
2530 (insert (substitute-command-keys "'")))
2531 (if signed
2532 (insert ".")
2533 (insert " (unsigned)."))
2534 (when (and (package-desc-p desc)
2535 (not required-by)
2536 (member status '("unsigned" "installed")))
2537 (insert " ")
2538 (package-make-button "Delete"
2539 'action #'package-delete-button-action
2540 'package-desc desc)))
2541 (incompatible-reason
2542 (insert (propertize "Incompatible" 'font-lock-face font-lock-warning-face)
2543 " because it depends on ")
2544 (if (stringp incompatible-reason)
2545 (insert "Emacs " incompatible-reason ".")
2546 (insert "uninstallable packages.")))
2547 (installable
2548 (insert (capitalize status))
2549 (insert " from " (format "%s" archive))
2550 (insert " -- ")
2551 (package-make-button
2552 "Install"
2553 'action 'package-install-button-action
2554 'package-desc desc))
2555 (t (insert (capitalize status) ".")))
2556 (insert "\n")
2557 (unless (and pkg-dir (not archive)) ; Installed pkgs don't have archive.
2558 (package--print-help-section "Archive"
2559 (or archive "n/a")))
2560 (and version
2561 (package--print-help-section "Version"
2562 (package-version-join version)))
2563 (when commit
2564 (package--print-help-section "Commit" commit))
2565 (when desc
2566 (package--print-help-section "Summary"
2567 (package-desc-summary desc)))
2569 (setq reqs (if desc (package-desc-reqs desc)))
2570 (when reqs
2571 (package--print-help-section "Requires")
2572 (let ((first t))
2573 (dolist (req reqs)
2574 (let* ((name (car req))
2575 (vers (cadr req))
2576 (text (format "%s-%s" (symbol-name name)
2577 (package-version-join vers)))
2578 (reason (if (and (listp incompatible-reason)
2579 (assq name incompatible-reason))
2580 " (not available)" "")))
2581 (cond (first (setq first nil))
2582 ((>= (+ 2 (current-column) (length text) (length reason))
2583 (window-width))
2584 (insert ",\n "))
2585 (t (insert ", ")))
2586 (help-insert-xref-button text 'help-package name)
2587 (insert reason)))
2588 (insert "\n")))
2589 (when required-by
2590 (package--print-help-section "Required by")
2591 (let ((first t))
2592 (dolist (pkg required-by)
2593 (let ((text (package-desc-full-name pkg)))
2594 (cond (first (setq first nil))
2595 ((>= (+ 2 (current-column) (length text))
2596 (window-width))
2597 (insert ",\n "))
2598 (t (insert ", ")))
2599 (help-insert-xref-button text 'help-package
2600 (package-desc-name pkg))))
2601 (insert "\n")))
2602 (when homepage
2603 ;; Prefer https for the homepage of packages on gnu.org.
2604 (if (string-match-p "^http://\\(elpa\\|www\\)\\.gnu\\.org/" homepage)
2605 (let ((gnu (cdr (assoc "gnu" package-archives))))
2606 (and gnu (string-match-p "^https" gnu)
2607 (setq homepage
2608 (replace-regexp-in-string "^http" "https" homepage)))))
2609 (package--print-help-section "Homepage")
2610 (help-insert-xref-button homepage 'help-url homepage)
2611 (insert "\n"))
2612 (when keywords
2613 (package--print-help-section "Keywords")
2614 (dolist (k keywords)
2615 (package-make-button
2617 'package-keyword k
2618 'action 'package-keyword-button-action)
2619 (insert " "))
2620 (insert "\n"))
2621 (when maintainer
2622 (package--print-help-section "Maintainer")
2623 (package--print-email-button maintainer))
2624 (when authors
2625 (package--print-help-section
2626 (if (= (length authors) 1)
2627 "Author"
2628 "Authors"))
2629 (package--print-email-button (pop authors))
2630 ;; If there's more than one author, indent the rest correctly.
2631 (dolist (name authors)
2632 (insert (make-string 13 ?\s))
2633 (package--print-email-button name)))
2634 (let* ((all-pkgs (append (cdr (assq name package-alist))
2635 (cdr (assq name package-archive-contents))
2636 (let ((bi (assq name package--builtins)))
2637 (if bi (list (package--from-builtin bi))))))
2638 (other-pkgs (delete desc all-pkgs)))
2639 (when other-pkgs
2640 (package--print-help-section "Other versions"
2641 (mapconcat (lambda (opkg)
2642 (let* ((ov (package-desc-version opkg))
2643 (dir (package-desc-dir opkg))
2644 (from (or (package-desc-archive opkg)
2645 (if (stringp dir) "installed" dir))))
2646 (if (not ov) (format "%s" from)
2647 (format "%s (%s)"
2648 (make-text-button (package-version-join ov) nil
2649 'font-lock-face 'link
2650 'follow-link t
2651 'action
2652 (lambda (_button)
2653 (describe-package opkg)))
2654 from))))
2655 other-pkgs ", ")
2656 ".")))
2658 (insert "\n")
2660 (let ((start-of-description (point)))
2661 (if built-in
2662 ;; For built-in packages, get the description from the
2663 ;; Commentary header.
2664 (insert (or (lm-commentary (locate-file (format "%s.el" name)
2665 load-path
2666 load-file-rep-suffixes))
2667 ""))
2669 (if (package-installed-p desc)
2670 ;; For installed packages, get the description from the
2671 ;; installed files.
2672 (insert (package--get-description desc))
2674 ;; For non-built-in, non-installed packages, get description from
2675 ;; the archive.
2676 (let* ((basename (format "%s-readme.txt" name))
2677 readme-string)
2679 (package--with-response-buffer (package-archive-base desc)
2680 :file basename :noerror t
2681 (save-excursion
2682 (goto-char (point-max))
2683 (unless (bolp)
2684 (insert ?\n)))
2685 (cl-assert (not enable-multibyte-characters))
2686 (setq readme-string
2687 ;; The readme.txt files are defined to contain utf-8 text.
2688 (decode-coding-region (point-min) (point-max) 'utf-8 t))
2690 (insert (or readme-string
2691 "This package does not provide a description.")))))
2692 ;; Make URLs in the description into links.
2693 (goto-char start-of-description)
2694 (browse-url-add-buttons))))
2696 (defun package-install-button-action (button)
2697 "Run `package-install' on the package BUTTON points to.
2698 Used for the `action' property of buttons in the buffer created by
2699 `describe-package'."
2700 (let ((pkg-desc (button-get button 'package-desc)))
2701 (when (y-or-n-p (format-message "Install package `%s'? "
2702 (package-desc-full-name pkg-desc)))
2703 (package-install pkg-desc nil)
2704 (describe-package (package-desc-name pkg-desc)))))
2706 (defun package-delete-button-action (button)
2707 "Run `package-delete' on the package BUTTON points to.
2708 Used for the `action' property of buttons in the buffer created by
2709 `describe-package'."
2710 (let ((pkg-desc (button-get button 'package-desc)))
2711 (when (y-or-n-p (format-message "Delete package `%s'? "
2712 (package-desc-full-name pkg-desc)))
2713 (package-delete pkg-desc)
2714 (describe-package (package-desc-name pkg-desc)))))
2716 (defun package-keyword-button-action (button)
2717 "Show filtered \"*Packages*\" buffer for BUTTON.
2718 The buffer is filtered by the `package-keyword' property of BUTTON.
2719 Used for the `action' property of buttons in the buffer created by
2720 `describe-package'."
2721 (let ((pkg-keyword (button-get button 'package-keyword)))
2722 (package-show-package-list t (list pkg-keyword))))
2724 (defun package-make-button (text &rest properties)
2725 "Insert button labeled TEXT with button PROPERTIES at point.
2726 PROPERTIES are passed to `insert-text-button', for which this
2727 function is a convenience wrapper used by `describe-package-1'."
2728 (let ((button-text (if (display-graphic-p) text (concat "[" text "]")))
2729 (button-face (if (display-graphic-p)
2730 (progn
2731 (require 'cus-edit) ; for the custom-button face
2732 'custom-button)
2733 'link)))
2734 (apply #'insert-text-button button-text 'face button-face 'follow-link t
2735 properties)))
2737 (defun package--print-email-button (recipient)
2738 "Insert a button whose action will send an email to RECIPIENT.
2739 NAME should have the form (FULLNAME . EMAIL) where FULLNAME is
2740 either a full name or nil, and EMAIL is a valid email address."
2741 (when (car recipient)
2742 (insert (car recipient)))
2743 (when (and (car recipient) (cdr recipient))
2744 (insert " "))
2745 (when (cdr recipient)
2746 (insert "<")
2747 (insert-text-button (cdr recipient)
2748 'follow-link t
2749 'action (lambda (_)
2750 (compose-mail
2751 (format "%s <%s>" (car recipient) (cdr recipient)))))
2752 (insert ">"))
2753 (insert "\n"))
2756 ;;;; Package menu mode.
2758 (defvar package-menu-mode-map
2759 (let ((map (make-sparse-keymap)))
2760 (set-keymap-parent map tabulated-list-mode-map)
2761 (define-key map "\C-m" 'package-menu-describe-package)
2762 (define-key map "u" 'package-menu-mark-unmark)
2763 (define-key map "\177" 'package-menu-backup-unmark)
2764 (define-key map "d" 'package-menu-mark-delete)
2765 (define-key map "i" 'package-menu-mark-install)
2766 (define-key map "U" 'package-menu-mark-upgrades)
2767 (define-key map "r" 'revert-buffer)
2768 (define-key map "~" 'package-menu-mark-obsolete-for-deletion)
2769 (define-key map "w" 'package-browse-url)
2770 (define-key map "x" 'package-menu-execute)
2771 (define-key map "h" 'package-menu-quick-help)
2772 (define-key map "H" #'package-menu-hide-package)
2773 (define-key map "?" 'package-menu-describe-package)
2774 (define-key map "(" #'package-menu-toggle-hiding)
2775 (define-key map (kbd "/ /") 'package-menu-clear-filter)
2776 (define-key map (kbd "/ a") 'package-menu-filter-by-archive)
2777 (define-key map (kbd "/ d") 'package-menu-filter-by-description)
2778 (define-key map (kbd "/ k") 'package-menu-filter-by-keyword)
2779 (define-key map (kbd "/ N") 'package-menu-filter-by-name-or-description)
2780 (define-key map (kbd "/ n") 'package-menu-filter-by-name)
2781 (define-key map (kbd "/ s") 'package-menu-filter-by-status)
2782 (define-key map (kbd "/ v") 'package-menu-filter-by-version)
2783 (define-key map (kbd "/ m") 'package-menu-filter-marked)
2784 (define-key map (kbd "/ u") 'package-menu-filter-upgradable)
2785 map)
2786 "Local keymap for `package-menu-mode' buffers.")
2788 (easy-menu-define package-menu-mode-menu package-menu-mode-map
2789 "Menu for `package-menu-mode'."
2790 '("Package"
2791 ["Describe Package" package-menu-describe-package :help "Display information about this package"]
2792 ["Open Package Homepage" package-browse-url
2793 :help "Open the homepage of this package"]
2794 ["Help" package-menu-quick-help :help "Show short key binding help for package-menu-mode"]
2795 "--"
2796 ["Refresh Package List" revert-buffer
2797 :help "Redownload the package archive(s)"
2798 :active (not package--downloads-in-progress)]
2799 ["Execute Marked Actions" package-menu-execute :help "Perform all the marked actions"]
2801 "--"
2802 ["Mark All Available Upgrades" package-menu-mark-upgrades
2803 :help "Mark packages that have a newer version for upgrading"
2804 :active (not package--downloads-in-progress)]
2805 ["Mark All Obsolete for Deletion" package-menu-mark-obsolete-for-deletion :help "Mark all obsolete packages for deletion"]
2806 ["Mark for Install" package-menu-mark-install :help "Mark a package for installation and move to the next line"]
2807 ["Mark for Deletion" package-menu-mark-delete :help "Mark a package for deletion and move to the next line"]
2808 ["Unmark" package-menu-mark-unmark :help "Clear any marks on a package and move to the next line"]
2810 "--"
2811 ("Filter Packages"
2812 ["Filter by Archive" package-menu-filter-by-archive :help "Filter packages by archive"]
2813 ["Filter by Description" package-menu-filter-by-description :help "Filter packages by description"]
2814 ["Filter by Keyword" package-menu-filter-by-keyword :help "Filter packages by keyword"]
2815 ["Filter by Name" package-menu-filter-by-name :help "Filter packages by name"]
2816 ["Filter by Name or Description" package-menu-filter-by-name-or-description
2817 :help "Filter packages by name or description"]
2818 ["Filter by Status" package-menu-filter-by-status :help "Filter packages by status"]
2819 ["Filter by Version" package-menu-filter-by-version :help "Filter packages by version"]
2820 ["Filter Marked" package-menu-filter-marked :help "Filter packages marked for upgrade"]
2821 ["Clear Filter" package-menu-clear-filter :help "Clear package list filter"])
2823 ["Hide by Regexp" package-menu-hide-package :help "Hide all packages matching a regexp"]
2824 ["Display Older Versions" package-menu-toggle-hiding
2825 :style toggle :selected (not package-menu--hide-packages)
2826 :help "Display package even if a newer version is already installed"]
2828 "--"
2829 ["Quit" quit-window :help "Quit package selection"]
2830 ["Customize" (customize-group 'package)]))
2832 (defvar package-menu--new-package-list nil
2833 "List of newly-available packages since `list-packages' was last called.")
2835 (defvar package-menu--transaction-status nil
2836 "Mode-line status of ongoing package transaction.")
2838 (define-derived-mode package-menu-mode tabulated-list-mode "Package Menu"
2839 "Major mode for browsing a list of packages.
2840 Letters do not insert themselves; instead, they are commands.
2841 \\<package-menu-mode-map>
2842 \\{package-menu-mode-map}"
2843 :interactive nil
2844 (setq mode-line-process '((package--downloads-in-progress ":Loading")
2845 (package-menu--transaction-status
2846 package-menu--transaction-status)))
2847 (setq tabulated-list-format
2848 `[("Package" ,package-name-column-width package-menu--name-predicate)
2849 ("Version" ,package-version-column-width package-menu--version-predicate)
2850 ("Status" ,package-status-column-width package-menu--status-predicate)
2851 ,@(if (cdr package-archives)
2852 `(("Archive" ,package-archive-column-width package-menu--archive-predicate)))
2853 ("Description" 0 package-menu--description-predicate)])
2854 (setq tabulated-list-padding 2)
2855 (setq tabulated-list-sort-key (cons "Status" nil))
2856 (add-hook 'tabulated-list-revert-hook #'package-menu--refresh nil t)
2857 (tabulated-list-init-header)
2858 (setq revert-buffer-function 'package-menu--refresh-contents)
2859 (setf imenu-prev-index-position-function
2860 #'package--imenu-prev-index-position-function)
2861 (setf imenu-extract-index-name-function
2862 #'package--imenu-extract-index-name-function))
2864 (defmacro package--push (pkg-desc status listname)
2865 "Convenience macro for `package-menu--generate'.
2866 If the alist stored in the symbol LISTNAME lacks an entry for a
2867 package PKG-DESC, add one. The alist is keyed with PKG-DESC."
2868 (declare (obsolete nil "27.1"))
2869 `(unless (assoc ,pkg-desc ,listname)
2870 ;; FIXME: Should we move status into pkg-desc?
2871 (push (cons ,pkg-desc ,status) ,listname)))
2873 (defvar package-list-unversioned nil
2874 "If non-nil, include packages that don't have a version in `list-packages'.")
2876 (defvar package-list-unsigned nil
2877 "If non-nil, mention in the list which packages were installed w/o signature.")
2879 (defvar package--emacs-version-list (version-to-list emacs-version)
2880 "The value of variable `emacs-version' as a list.")
2882 (defun package--ensure-package-menu-mode ()
2883 "Signal a user-error if major mode is not `package-menu-mode'."
2884 (unless (derived-mode-p 'package-menu-mode)
2885 (user-error "The current buffer is not a Package Menu")))
2887 (defun package--incompatible-p (pkg &optional shallow)
2888 "Return non-nil if PKG has no chance of being installable.
2889 PKG is a `package-desc' object.
2891 If SHALLOW is non-nil, this only checks if PKG depends on a
2892 higher `emacs-version' than the one being used. Otherwise, also
2893 checks the viability of dependencies, according to
2894 `package--compatibility-table'.
2896 If PKG requires an incompatible Emacs version, the return value
2897 is this version (as a string).
2898 If PKG requires incompatible packages, the return value is a list
2899 of these dependencies, similar to the list returned by
2900 `package-desc-reqs'."
2901 (let* ((reqs (package-desc-reqs pkg))
2902 (version (cadr (assq 'emacs reqs))))
2903 (if (and version (version-list-< package--emacs-version-list version))
2904 (package-version-join version)
2905 (unless shallow
2906 (let (out)
2907 (dolist (dep (package-desc-reqs pkg) out)
2908 (let ((dep-name (car dep)))
2909 (unless (eq 'emacs dep-name)
2910 (let ((cv (gethash dep-name package--compatibility-table)))
2911 (when (version-list-< (or cv '(0)) (or (cadr dep) '(0)))
2912 (push dep out)))))))))))
2914 (defun package-desc-status (pkg-desc)
2915 "Return the status of `package-desc' object PKG-DESC."
2916 (let* ((name (package-desc-name pkg-desc))
2917 (dir (package-desc-dir pkg-desc))
2918 (lle (assq name package-load-list))
2919 (held (cadr lle))
2920 (version (package-desc-version pkg-desc))
2921 (signed (or (not package-list-unsigned)
2922 (package-desc-signed pkg-desc))))
2923 (cond
2924 ((eq dir 'builtin) "built-in")
2925 ((and lle (null held)) "disabled")
2926 ((stringp held)
2927 (let ((hv (if (stringp held) (version-to-list held))))
2928 (cond
2929 ((version-list-= version hv) "held")
2930 ((version-list-< version hv) "obsolete")
2931 (t "disabled"))))
2932 (dir ;One of the installed packages.
2933 (cond
2934 ((not (file-exists-p dir)) "deleted")
2935 ;; Not inside `package-user-dir'.
2936 ((not (file-in-directory-p dir package-user-dir)) "external")
2937 ((eq pkg-desc (cadr (assq name package-alist)))
2938 (if (not signed) "unsigned"
2939 (if (package--user-selected-p name)
2940 "installed" "dependency")))
2941 (t "obsolete")))
2942 ((package--incompatible-p pkg-desc) "incompat")
2944 (let* ((ins (cadr (assq name package-alist)))
2945 (ins-v (if ins (package-desc-version ins))))
2946 (cond
2947 ;; Installed obsolete packages are handled in the `dir'
2948 ;; clause above. Here we handle available obsolete, which
2949 ;; are displayed depending on `package-menu--hide-packages'.
2950 ((and ins (version-list-<= version ins-v)) "avail-obso")
2952 (if (memq name package-menu--new-package-list)
2953 "new" "available"))))))))
2955 (defvar package-menu--hide-packages t
2956 "Whether available obsolete packages should be hidden.
2957 Can be toggled with \\<package-menu-mode-map> \\[package-menu-toggle-hiding].
2958 Installed obsolete packages are always displayed.")
2960 (defun package-menu-toggle-hiding ()
2961 "In Package Menu, toggle visibility of obsolete available packages.
2963 Also hide packages whose name matches a regexp in user option
2964 `package-hidden-regexps' (a list). To add regexps to this list,
2965 use `package-menu-hide-package'."
2966 (interactive nil package-menu-mode)
2967 (package--ensure-package-menu-mode)
2968 (setq package-menu--hide-packages
2969 (not package-menu--hide-packages))
2970 (if package-menu--hide-packages
2971 (message "Hiding obsolete or unwanted packages")
2972 (message "Displaying all packages"))
2973 (revert-buffer nil 'no-confirm))
2975 (defun package--remove-hidden (pkg-list)
2976 "Filter PKG-LIST according to `package-archive-priorities'.
2977 PKG-LIST must be a list of `package-desc' objects, all with the
2978 same name, sorted by decreasing `package-desc-priority-version'.
2979 Return a list of packages tied for the highest priority according
2980 to their archives."
2981 (when pkg-list
2982 ;; Variable toggled with `package-menu-toggle-hiding'.
2983 (if (not package-menu--hide-packages)
2984 pkg-list
2985 (let ((installed (cadr (assq (package-desc-name (car pkg-list))
2986 package-alist))))
2987 (when installed
2988 (setq pkg-list
2989 (let ((ins-version (package-desc-version installed)))
2990 (cl-remove-if (lambda (p) (version-list-< (package-desc-version p)
2991 ins-version))
2992 pkg-list))))
2993 (let ((filtered-by-priority
2994 (cond
2995 ((not package-menu-hide-low-priority)
2996 pkg-list)
2997 ((eq package-menu-hide-low-priority 'archive)
2998 (let (max-priority out)
2999 (while pkg-list
3000 (let ((p (pop pkg-list)))
3001 (let ((priority (package-desc-priority p)))
3002 (if (and max-priority (< priority max-priority))
3003 (setq pkg-list nil)
3004 (push p out)
3005 (setq max-priority priority)))))
3006 (nreverse out)))
3007 (pkg-list
3008 (list (car pkg-list))))))
3009 (if (not installed)
3010 filtered-by-priority
3011 (let ((ins-version (package-desc-version installed)))
3012 (cl-remove-if (lambda (p) (version-list-= (package-desc-version p)
3013 ins-version))
3014 filtered-by-priority))))))))
3016 (defcustom package-hidden-regexps nil
3017 "List of regexps matching the name of packages to hide.
3018 If the name of a package matches any of these regexps it is
3019 omitted from the package menu. To toggle this, type \\[package-menu-toggle-hiding].
3021 Values can be interactively added to this list by typing
3022 \\[package-menu-hide-package] on a package."
3023 :version "25.1"
3024 :type '(repeat (regexp :tag "Hide packages with name matching")))
3026 (defun package-menu--refresh (&optional packages keywords)
3027 "Re-populate the `tabulated-list-entries'.
3028 PACKAGES should be nil or t, which means to display all known packages.
3029 KEYWORDS should be nil or a list of keywords."
3030 ;; Construct list of (PKG-DESC . STATUS).
3031 (unless packages (setq packages t))
3032 (let ((hidden-names (mapconcat #'identity package-hidden-regexps "\\|"))
3033 info-list)
3034 ;; Installed packages:
3035 (dolist (elt package-alist)
3036 (let ((name (car elt)))
3037 (when (or (eq packages t) (memq name packages))
3038 (dolist (pkg (cdr elt))
3039 (when (package--has-keyword-p pkg keywords)
3040 (push pkg info-list))))))
3042 ;; Built-in packages:
3043 (dolist (elt package--builtins)
3044 (let ((pkg (package--from-builtin elt))
3045 (name (car elt)))
3046 (when (not (eq name 'emacs)) ; Hide the `emacs' package.
3047 (when (and (package--has-keyword-p pkg keywords)
3048 (or package-list-unversioned
3049 (package--bi-desc-version (cdr elt)))
3050 (or (eq packages t) (memq name packages)))
3051 (push pkg info-list)))))
3053 ;; Available and disabled packages:
3054 (unless (equal package--old-archive-priorities package-archive-priorities)
3055 (package-read-all-archive-contents))
3056 (dolist (elt package-archive-contents)
3057 (let ((name (car elt)))
3058 ;; To be displayed it must be in PACKAGES;
3059 (when (and (or (eq packages t) (memq name packages))
3060 ;; and we must either not be hiding anything,
3061 (or (not package-menu--hide-packages)
3062 (not package-hidden-regexps)
3063 ;; or just not hiding this specific package.
3064 (not (string-match hidden-names (symbol-name name)))))
3065 ;; Hide available-obsolete or low-priority packages.
3066 (dolist (pkg (package--remove-hidden (cdr elt)))
3067 (when (package--has-keyword-p pkg keywords)
3068 (push pkg info-list))))))
3070 ;; Print the result.
3071 (tabulated-list-init-header)
3072 (setq tabulated-list-entries
3073 (mapcar #'package-menu--print-info-simple info-list))))
3075 (defun package-all-keywords ()
3076 "Collect all package keywords."
3077 (let ((key-list))
3078 (package--mapc (lambda (desc)
3079 (setq key-list (append (package-desc--keywords desc)
3080 key-list))))
3081 key-list))
3083 (defun package--mapc (function &optional packages)
3084 "Call FUNCTION for all known PACKAGES.
3085 PACKAGES can be nil or t, which means to display all known
3086 packages, or a list of packages.
3088 Built-in packages are converted with `package--from-builtin'."
3089 (unless packages (setq packages t))
3090 (let (name)
3091 ;; Installed packages:
3092 (dolist (elt package-alist)
3093 (setq name (car elt))
3094 (when (or (eq packages t) (memq name packages))
3095 (mapc function (cdr elt))))
3097 ;; Built-in packages:
3098 (dolist (elt package--builtins)
3099 (setq name (car elt))
3100 (when (and (not (eq name 'emacs)) ; Hide the `emacs' package.
3101 (or package-list-unversioned
3102 (package--bi-desc-version (cdr elt)))
3103 (or (eq packages t) (memq name packages)))
3104 (funcall function (package--from-builtin elt))))
3106 ;; Available and disabled packages:
3107 (dolist (elt package-archive-contents)
3108 (setq name (car elt))
3109 (when (or (eq packages t) (memq name packages))
3110 (dolist (pkg (cdr elt))
3111 ;; Hide obsolete packages.
3112 (unless (package-installed-p (package-desc-name pkg)
3113 (package-desc-version pkg))
3114 (funcall function pkg)))))))
3116 (defun package--has-keyword-p (desc &optional keywords)
3117 "Test if package DESC has any of the given KEYWORDS.
3118 When none are given, the package matches."
3119 (if keywords
3120 (let ((desc-keywords (and desc (package-desc--keywords desc)))
3121 found)
3122 (while (and (not found) keywords)
3123 (let ((k (pop keywords)))
3124 (setq found
3125 (or (string= k (concat "arc:" (package-desc-archive desc)))
3126 (string= k (concat "status:" (package-desc-status desc)))
3127 (member k desc-keywords)))))
3128 found)
3131 (defun package-menu--display (remember-pos suffix)
3132 "Display the Package Menu.
3133 If REMEMBER-POS is non-nil, keep point on the same entry.
3135 If SUFFIX is non-nil, append that to \"Package\" for the first
3136 column in the header line."
3137 (setf (car (aref tabulated-list-format 0))
3138 (if suffix
3139 (concat "Package[" suffix "]")
3140 "Package"))
3141 (tabulated-list-init-header)
3142 (tabulated-list-print remember-pos))
3144 (defun package-menu--generate (remember-pos &optional packages keywords)
3145 "Populate and display the Package Menu.
3146 If REMEMBER-POS is non-nil, keep point on the same entry.
3147 PACKAGES should be t, which means to display all known packages,
3148 or a list of package names (symbols) to display.
3150 With KEYWORDS given, only packages with those keywords are
3151 shown."
3152 (package-menu--refresh packages keywords)
3153 (package-menu--display remember-pos
3154 (when keywords
3155 (let ((filters (mapconcat #'identity keywords ",")))
3156 (concat "Package[" filters "]")))))
3158 (defun package-menu--print-info (pkg)
3159 "Return a package entry suitable for `tabulated-list-entries'.
3160 PKG has the form (PKG-DESC . STATUS).
3161 Return (PKG-DESC [NAME VERSION STATUS DOC])."
3162 (package-menu--print-info-simple (car pkg)))
3163 (make-obsolete 'package-menu--print-info
3164 'package-menu--print-info-simple "25.1")
3167 ;;; Package menu faces
3169 (defface package-name
3170 '((t :inherit link))
3171 "Face used on package names in the package menu."
3172 :version "25.1")
3174 (defface package-description
3175 '((t :inherit default))
3176 "Face used on package description summaries in the package menu."
3177 :version "25.1")
3179 ;; Shame this hyphenates "built-in", when "font-lock-builtin-face" doesn't.
3180 (defface package-status-built-in
3181 '((t :inherit font-lock-builtin-face))
3182 "Face used on the status and version of built-in packages."
3183 :version "25.1")
3185 (defface package-status-external
3186 '((t :inherit package-status-built-in))
3187 "Face used on the status and version of external packages."
3188 :version "25.1")
3190 (defface package-status-available
3191 '((t :inherit default))
3192 "Face used on the status and version of available packages."
3193 :version "25.1")
3195 (defface package-status-new
3196 '((t :inherit (bold package-status-available)))
3197 "Face used on the status and version of new packages."
3198 :version "25.1")
3200 (defface package-status-held
3201 '((t :inherit font-lock-constant-face))
3202 "Face used on the status and version of held packages."
3203 :version "25.1")
3205 (defface package-status-disabled
3206 '((t :inherit font-lock-warning-face))
3207 "Face used on the status and version of disabled packages."
3208 :version "25.1")
3210 (defface package-status-installed
3211 '((t :inherit font-lock-comment-face))
3212 "Face used on the status and version of installed packages."
3213 :version "25.1")
3215 (defface package-status-dependency
3216 '((t :inherit package-status-installed))
3217 "Face used on the status and version of dependency packages."
3218 :version "25.1")
3220 (defface package-status-unsigned
3221 '((t :inherit font-lock-warning-face))
3222 "Face used on the status and version of unsigned packages."
3223 :version "25.1")
3225 (defface package-status-incompat
3226 '((t :inherit error))
3227 "Face used on the status and version of incompat packages."
3228 :version "25.1")
3230 (defface package-status-avail-obso
3231 '((t :inherit package-status-incompat))
3232 "Face used on the status and version of avail-obso packages."
3233 :version "25.1")
3236 ;;; Package menu printing
3238 (defun package-menu--print-info-simple (pkg)
3239 "Return a package entry suitable for `tabulated-list-entries'.
3240 PKG is a `package-desc' object.
3241 Return (PKG-DESC [NAME VERSION STATUS DOC])."
3242 (let* ((status (package-desc-status pkg))
3243 (face (pcase status
3244 ("built-in" 'package-status-built-in)
3245 ("external" 'package-status-external)
3246 ("available" 'package-status-available)
3247 ("avail-obso" 'package-status-avail-obso)
3248 ("new" 'package-status-new)
3249 ("held" 'package-status-held)
3250 ("disabled" 'package-status-disabled)
3251 ("installed" 'package-status-installed)
3252 ("dependency" 'package-status-dependency)
3253 ("unsigned" 'package-status-unsigned)
3254 ("incompat" 'package-status-incompat)
3255 (_ 'font-lock-warning-face)))) ; obsolete.
3256 (list pkg
3257 `[(,(symbol-name (package-desc-name pkg))
3258 face package-name
3259 font-lock-face package-name
3260 follow-link t
3261 package-desc ,pkg
3262 action package-menu-describe-package)
3263 ,(propertize (package-version-join
3264 (package-desc-version pkg))
3265 'font-lock-face face)
3266 ,(propertize status 'font-lock-face face)
3267 ,@(if (cdr package-archives)
3268 (list (propertize (or (package-desc-archive pkg) "")
3269 'font-lock-face face)))
3270 ,(propertize (package-desc-summary pkg)
3271 'font-lock-face 'package-description)])))
3273 (defvar package-menu--old-archive-contents nil
3274 "`package-archive-contents' before the latest refresh.")
3276 (defun package-menu--refresh-contents (&optional _arg _noconfirm)
3277 "In Package Menu, download the Emacs Lisp package archive.
3278 Fetch the contents of each archive specified in
3279 `package-archives', and then refresh the package menu.
3281 `package-menu-mode' sets `revert-buffer-function' to this
3282 function. The args ARG and NOCONFIRM, passed from
3283 `revert-buffer', are ignored."
3284 (package--ensure-package-menu-mode)
3285 (setq package-menu--old-archive-contents package-archive-contents)
3286 (setq package-menu--new-package-list nil)
3287 (package-refresh-contents package-menu-async))
3288 (define-obsolete-function-alias 'package-menu-refresh 'revert-buffer "27.1")
3290 (defun package-menu-hide-package ()
3291 "Hide in Package Menu packages that match a regexp.
3292 Prompt for the regexp to match against package names.
3293 The default regexp will hide only the package whose name is at point.
3295 The regexp is added to the list in the user option
3296 `package-hidden-regexps' and saved for future sessions.
3298 To unhide a package, type
3299 `\\[customize-variable] RET package-hidden-regexps'.
3301 Type \\[package-menu-toggle-hiding] to toggle package hiding."
3302 (declare (interactive-only "change `package-hidden-regexps' instead."))
3303 (interactive nil package-menu-mode)
3304 (package--ensure-package-menu-mode)
3305 (let* ((name (when (derived-mode-p 'package-menu-mode)
3306 (concat "\\`" (regexp-quote (symbol-name (package-desc-name
3307 (tabulated-list-get-id))))
3308 "\\'")))
3309 (re (read-string "Hide packages matching regexp: " name)))
3310 ;; Test if it is valid.
3311 (string-match re "")
3312 (push re package-hidden-regexps)
3313 (customize-save-variable 'package-hidden-regexps package-hidden-regexps)
3314 (package-menu--post-refresh)
3315 (let ((hidden
3316 (cl-remove-if-not (lambda (e) (string-match re (symbol-name (car e))))
3317 package-archive-contents)))
3318 (message "Packages to hide: %d. Type `%s' to toggle or `%s' to customize"
3319 (length hidden)
3320 (substitute-command-keys "\\[package-menu-toggle-hiding]")
3321 (substitute-command-keys "\\[customize-variable] RET package-hidden-regexps")))))
3324 (defun package-menu-describe-package (&optional button)
3325 "Describe the current package.
3326 If optional arg BUTTON is non-nil, describe its associated package."
3327 (interactive nil package-menu-mode)
3328 (let ((pkg-desc (if button (button-get button 'package-desc)
3329 (tabulated-list-get-id))))
3330 (if pkg-desc
3331 (describe-package pkg-desc)
3332 (user-error "No package here"))))
3334 ;; fixme numeric argument
3335 (defun package-menu-mark-delete (&optional _num)
3336 "Mark a package for deletion and move to the next line."
3337 (interactive "p" package-menu-mode)
3338 (package--ensure-package-menu-mode)
3339 (if (member (package-menu-get-status)
3340 '("installed" "dependency" "obsolete" "unsigned"))
3341 (tabulated-list-put-tag "D" t)
3342 (forward-line)))
3344 (defun package-menu-mark-install (&optional _num)
3345 "Mark a package for installation and move to the next line."
3346 (interactive "p" package-menu-mode)
3347 (package--ensure-package-menu-mode)
3348 (if (member (package-menu-get-status) '("available" "avail-obso" "new" "dependency"))
3349 (tabulated-list-put-tag "I" t)
3350 (forward-line)))
3352 (defun package-menu-mark-unmark (&optional _num)
3353 "Clear any marks on a package and move to the next line."
3354 (interactive "p" package-menu-mode)
3355 (package--ensure-package-menu-mode)
3356 (tabulated-list-put-tag " " t))
3358 (defun package-menu-backup-unmark ()
3359 "Back up one line and clear any marks on that package."
3360 (interactive nil package-menu-mode)
3361 (package--ensure-package-menu-mode)
3362 (forward-line -1)
3363 (tabulated-list-put-tag " "))
3365 (defun package-menu-mark-obsolete-for-deletion ()
3366 "Mark all obsolete packages for deletion."
3367 (interactive nil package-menu-mode)
3368 (package--ensure-package-menu-mode)
3369 (save-excursion
3370 (goto-char (point-min))
3371 (while (not (eobp))
3372 (if (equal (package-menu-get-status) "obsolete")
3373 (tabulated-list-put-tag "D" t)
3374 (forward-line 1)))))
3376 (defvar package--quick-help-keys
3377 '((("mark for installation," . 9)
3378 ("mark for deletion," . 9) "unmark," ("execute marked actions" . 1))
3379 ("next," "previous")
3380 ("Hide-package," "(-toggle-hidden")
3381 ("g-refresh-contents," "/-filter," "help")))
3383 (defun package--prettify-quick-help-key (desc)
3384 "Prettify DESC to be displayed as a help menu."
3385 (if (listp desc)
3386 (if (listp (cdr desc))
3387 (mapconcat #'package--prettify-quick-help-key desc " ")
3388 (let ((place (cdr desc))
3389 (out (copy-sequence (car desc))))
3390 (add-text-properties place (1+ place)
3391 '(face (bold font-lock-warning-face))
3392 out)
3393 out))
3394 (package--prettify-quick-help-key (cons desc 0))))
3396 (defun package-menu-quick-help ()
3397 "Show short key binding help for `package-menu-mode'.
3398 The full list of keys can be viewed with \\[describe-mode]."
3399 (interactive nil package-menu-mode)
3400 (package--ensure-package-menu-mode)
3401 (message (mapconcat #'package--prettify-quick-help-key
3402 package--quick-help-keys "\n")))
3404 (define-obsolete-function-alias
3405 'package-menu-view-commentary 'package-menu-describe-package "24.1")
3407 (defun package-menu-get-status ()
3408 "Return status text of package at point in Package Menu."
3409 (package--ensure-package-menu-mode)
3410 (let* ((id (tabulated-list-get-id))
3411 (entry (and id (assoc id tabulated-list-entries))))
3412 (if entry
3413 (aref (cadr entry) 2)
3414 "")))
3416 (defun package-archive-priority (archive)
3417 "Return the priority of ARCHIVE.
3419 The archive priorities are specified in
3420 `package-archive-priorities'. If not given there, the priority
3421 defaults to 0."
3422 (or (cdr (assoc archive package-archive-priorities))
3425 (defun package-desc-priority-version (pkg-desc)
3426 "Return the version PKG-DESC with the archive priority prepended.
3428 This allows for easy comparison of package versions from
3429 different archives if archive priorities are meant to be taken in
3430 consideration."
3431 (cons (package-desc-priority pkg-desc)
3432 (package-desc-version pkg-desc)))
3434 (defun package-menu--find-upgrades ()
3435 "In Package Menu, return an alist of packages that can be upgraded.
3436 The alist has the same form as `package-alist', namely a list
3437 of (PKG . DESCS), but where DESCS is the `package-desc' object
3438 corresponding to the newer version."
3439 (let (installed available upgrades)
3440 ;; Build list of installed/available packages in this buffer.
3441 (dolist (entry tabulated-list-entries)
3442 ;; ENTRY is (PKG-DESC [NAME VERSION STATUS DOC])
3443 (let ((pkg-desc (car entry))
3444 (status (aref (cadr entry) 2)))
3445 (cond ((member status '("installed" "dependency" "unsigned"))
3446 (push pkg-desc installed))
3447 ((member status '("available" "new"))
3448 (setq available (package--append-to-alist pkg-desc available))))))
3449 ;; Loop through list of installed packages, finding upgrades.
3450 (dolist (pkg-desc installed)
3451 (let* ((name (package-desc-name pkg-desc))
3452 (avail-pkg (cadr (assq name available))))
3453 (and avail-pkg
3454 (version-list-< (package-desc-priority-version pkg-desc)
3455 (package-desc-priority-version avail-pkg))
3456 (push (cons name avail-pkg) upgrades))))
3457 upgrades))
3459 (defvar package-menu--mark-upgrades-pending nil
3460 "Whether mark-upgrades is waiting for a refresh to finish.")
3462 (defun package-menu--mark-upgrades-1 ()
3463 "Mark all upgradable packages in the Package Menu.
3464 Implementation of `package-menu-mark-upgrades'."
3465 (setq package-menu--mark-upgrades-pending nil)
3466 (let ((upgrades (package-menu--find-upgrades)))
3467 (if (null upgrades)
3468 (message "No packages to upgrade")
3469 (widen)
3470 (save-excursion
3471 (goto-char (point-min))
3472 (while (not (eobp))
3473 (let* ((pkg-desc (tabulated-list-get-id))
3474 (upgrade (cdr (assq (package-desc-name pkg-desc) upgrades))))
3475 (cond ((null upgrade)
3476 (forward-line 1))
3477 ((equal pkg-desc upgrade)
3478 (package-menu-mark-install))
3480 (package-menu-mark-delete))))))
3481 (message "Packages marked for upgrading: %d"
3482 (length upgrades)))))
3485 (defun package-menu-mark-upgrades ()
3486 "Mark all upgradable packages in the Package Menu.
3487 For each installed package with a newer version available, place
3488 an (I)nstall flag on the available version and a (D)elete flag on
3489 the installed version. A subsequent \\[package-menu-execute]
3490 call will upgrade the package.
3492 If there's an async refresh operation in progress, the flags will
3493 be placed as part of `package-menu--post-refresh' instead of
3494 immediately."
3495 (interactive nil package-menu-mode)
3496 (package--ensure-package-menu-mode)
3497 (if (not package--downloads-in-progress)
3498 (package-menu--mark-upgrades-1)
3499 (setq package-menu--mark-upgrades-pending t)
3500 (message "Waiting for refresh to finish...")))
3502 (defun package-menu--list-to-prompt (packages)
3503 "Return a string listing PACKAGES that's usable in a prompt.
3504 PACKAGES is a list of `package-desc' objects.
3505 Formats the returned string to be usable in a minibuffer
3506 prompt (see `package-menu--prompt-transaction-p')."
3507 ;; The case where `package' is empty is handled in
3508 ;; `package-menu--prompt-transaction-p' below.
3509 (format "%d (%s)"
3510 (length packages)
3511 (mapconcat #'package-desc-full-name packages " ")))
3514 (defun package-menu--prompt-transaction-p (delete install upgrade)
3515 "Prompt the user about DELETE, INSTALL, and UPGRADE.
3516 DELETE, INSTALL, and UPGRADE are lists of `package-desc' objects.
3517 Either may be nil, but not all."
3518 (y-or-n-p
3519 (concat
3520 (when delete
3521 (format "Packages to delete: %s. " (package-menu--list-to-prompt delete)))
3522 (when install
3523 (format "Packages to install: %s. " (package-menu--list-to-prompt install)))
3524 (when upgrade
3525 (format "Packages to upgrade: %s. " (package-menu--list-to-prompt upgrade)))
3526 "Proceed? ")))
3529 (defun package-menu--partition-transaction (install delete)
3530 "Return an alist describing an INSTALL DELETE transaction.
3531 Alist contains three entries, upgrade, delete, and install, each
3532 with a list of package names.
3534 The upgrade entry contains any `package-desc' objects in INSTALL
3535 whose name coincides with an object in DELETE. The delete and
3536 the install entries are the same as DELETE and INSTALL with such
3537 objects removed."
3538 (let* ((upg (cl-intersection install delete :key #'package-desc-name))
3539 (ins (cl-set-difference install upg :key #'package-desc-name))
3540 (del (cl-set-difference delete upg :key #'package-desc-name)))
3541 `((delete . ,del) (install . ,ins) (upgrade . ,upg))))
3543 (defun package-menu--perform-transaction (install-list delete-list)
3544 "Install packages in INSTALL-LIST and delete DELETE-LIST."
3545 (if install-list
3546 (let ((status-format (format ":Installing %%d/%d"
3547 (length install-list)))
3548 (i 0)
3549 (package-menu--transaction-status))
3550 (dolist (pkg install-list)
3551 (setq package-menu--transaction-status
3552 (format status-format (cl-incf i)))
3553 (force-mode-line-update)
3554 (redisplay 'force)
3555 ;; Don't mark as selected, `package-menu-execute' already
3556 ;; does that.
3557 (package-install pkg 'dont-select))))
3558 (let ((package-menu--transaction-status ":Deleting"))
3559 (force-mode-line-update)
3560 (redisplay 'force)
3561 (dolist (elt (package--sort-by-dependence delete-list))
3562 (condition-case-unless-debug err
3563 (let ((inhibit-message (or inhibit-message package-menu-async)))
3564 (package-delete elt nil 'nosave))
3565 (error (message "Error trying to delete `%s': %S"
3566 (package-desc-full-name elt)
3567 err))))))
3569 (defun package--update-selected-packages (add remove)
3570 "Update the `package-selected-packages' list according to ADD and REMOVE.
3571 ADD and REMOVE must be disjoint lists of package names (or
3572 `package-desc' objects) to be added and removed to the selected
3573 packages list, respectively."
3574 (dolist (p add)
3575 (cl-pushnew (if (package-desc-p p) (package-desc-name p) p)
3576 package-selected-packages))
3577 (dolist (p remove)
3578 (setq package-selected-packages
3579 (remove (if (package-desc-p p) (package-desc-name p) p)
3580 package-selected-packages)))
3581 (when (or add remove)
3582 (package--save-selected-packages package-selected-packages)))
3584 (defun package-menu-execute (&optional noquery)
3585 "Perform marked Package Menu actions.
3586 Packages marked for installation are downloaded and installed;
3587 packages marked for deletion are removed.
3588 Optional argument NOQUERY non-nil means do not ask the user to confirm."
3589 (interactive nil package-menu-mode)
3590 (package--ensure-package-menu-mode)
3591 (let (install-list delete-list cmd pkg-desc)
3592 (save-excursion
3593 (goto-char (point-min))
3594 (while (not (eobp))
3595 (setq cmd (char-after))
3596 (unless (eq cmd ?\s)
3597 ;; This is the key PKG-DESC.
3598 (setq pkg-desc (tabulated-list-get-id))
3599 (cond ((eq cmd ?D)
3600 (push pkg-desc delete-list))
3601 ((eq cmd ?I)
3602 (push pkg-desc install-list))))
3603 (forward-line)))
3604 (unless (or delete-list install-list)
3605 (user-error "No operations specified"))
3606 (let-alist (package-menu--partition-transaction install-list delete-list)
3607 (when (or noquery
3608 (package-menu--prompt-transaction-p .delete .install .upgrade))
3609 (let ((message-template
3610 (concat "[ "
3611 (when .delete
3612 (format "Delete %d " (length .delete)))
3613 (when .install
3614 (format "Install %d " (length .install)))
3615 (when .upgrade
3616 (format "Upgrade %d " (length .upgrade)))
3617 "]")))
3618 (message "Operation %s started" message-template)
3619 ;; Packages being upgraded are not marked as selected.
3620 (package--update-selected-packages .install .delete)
3621 (package-menu--perform-transaction install-list delete-list)
3622 (when package-selected-packages
3623 (if-let* ((removable (package--removable-packages)))
3624 (message "Operation finished. Packages that are no longer needed: %d. Type `%s' to remove them"
3625 (length removable)
3626 (substitute-command-keys "\\[package-autoremove]"))
3627 (message "Operation %s finished" message-template))))))))
3629 (defun package-menu--version-predicate (A B)
3630 "Predicate to sort \"*Packages*\" buffer by the version column.
3631 This is used for `tabulated-list-format' in `package-menu-mode'."
3632 (let ((vA (or (version-to-list (aref (cadr A) 1)) '(0)))
3633 (vB (or (version-to-list (aref (cadr B) 1)) '(0))))
3634 (if (version-list-= vA vB)
3635 (package-menu--name-predicate A B)
3636 (version-list-< vA vB))))
3638 (defun package-menu--status-predicate (A B)
3639 "Predicate to sort \"*Packages*\" buffer by the status column.
3640 This is used for `tabulated-list-format' in `package-menu-mode'."
3641 (let ((sA (aref (cadr A) 2))
3642 (sB (aref (cadr B) 2)))
3643 (cond ((string= sA sB)
3644 (package-menu--name-predicate A B))
3645 ((string= sA "new") t)
3646 ((string= sB "new") nil)
3647 ((string-prefix-p "avail" sA)
3648 (if (string-prefix-p "avail" sB)
3649 (package-menu--name-predicate A B)
3651 ((string-prefix-p "avail" sB) nil)
3652 ((string= sA "installed") t)
3653 ((string= sB "installed") nil)
3654 ((string= sA "dependency") t)
3655 ((string= sB "dependency") nil)
3656 ((string= sA "unsigned") t)
3657 ((string= sB "unsigned") nil)
3658 ((string= sA "held") t)
3659 ((string= sB "held") nil)
3660 ((string= sA "external") t)
3661 ((string= sB "external") nil)
3662 ((string= sA "built-in") t)
3663 ((string= sB "built-in") nil)
3664 ((string= sA "obsolete") t)
3665 ((string= sB "obsolete") nil)
3666 ((string= sA "incompat") t)
3667 ((string= sB "incompat") nil)
3668 (t (string< sA sB)))))
3670 (defun package-menu--description-predicate (A B)
3671 "Predicate to sort \"*Packages*\" buffer by the description column.
3672 This is used for `tabulated-list-format' in `package-menu-mode'."
3673 (let ((dA (aref (cadr A) (if (cdr package-archives) 4 3)))
3674 (dB (aref (cadr B) (if (cdr package-archives) 4 3))))
3675 (if (string= dA dB)
3676 (package-menu--name-predicate A B)
3677 (string< dA dB))))
3679 (defun package-menu--name-predicate (A B)
3680 "Predicate to sort \"*Packages*\" buffer by the name column.
3681 This is used for `tabulated-list-format' in `package-menu-mode'."
3682 (string< (symbol-name (package-desc-name (car A)))
3683 (symbol-name (package-desc-name (car B)))))
3685 (defun package-menu--archive-predicate (A B)
3686 "Predicate to sort \"*Packages*\" buffer by the archive column.
3687 This is used for `tabulated-list-format' in `package-menu-mode'."
3688 (let ((a (or (package-desc-archive (car A)) ""))
3689 (b (or (package-desc-archive (car B)) "")))
3690 (if (string= a b)
3691 (package-menu--name-predicate A B)
3692 (string< a b))))
3694 (defun package-menu--populate-new-package-list ()
3695 "Decide which packages are new in `package-archive-contents'.
3696 Store this list in `package-menu--new-package-list'."
3697 ;; Find which packages are new.
3698 (when package-menu--old-archive-contents
3699 (dolist (elt package-archive-contents)
3700 (unless (assq (car elt) package-menu--old-archive-contents)
3701 (push (car elt) package-menu--new-package-list)))
3702 (setq package-menu--old-archive-contents nil)))
3704 (defun package-menu--find-and-notify-upgrades ()
3705 "Notify the user of upgradable packages."
3706 (when-let* ((upgrades (package-menu--find-upgrades)))
3707 (message "Packages that can be upgraded: %d; type `%s' to mark for upgrading."
3708 (length upgrades)
3709 (substitute-command-keys "\\[package-menu-mark-upgrades]"))))
3712 (defun package-menu--post-refresh ()
3713 "Revert \"*Packages*\" buffer and check for new packages and upgrades.
3714 Do nothing if there's no *Packages* buffer.
3716 This function is called after `package-refresh-contents' and it
3717 is added to `post-command-hook' by any function which alters the
3718 package database (`package-install' and `package-delete'). When
3719 run, it removes itself from `post-command-hook'."
3720 (remove-hook 'post-command-hook #'package-menu--post-refresh)
3721 (let ((buf (get-buffer "*Packages*")))
3722 (when (buffer-live-p buf)
3723 (with-current-buffer buf
3724 (package-menu--populate-new-package-list)
3725 (run-hooks 'tabulated-list-revert-hook)
3726 (tabulated-list-print 'remember 'update)))))
3728 (defun package-menu--mark-or-notify-upgrades ()
3729 "If there's a *Packages* buffer, check for upgrades and possibly mark them.
3730 Do nothing if there's no *Packages* buffer. If there are
3731 upgrades, mark them if `package-menu--mark-upgrades-pending' is
3732 non-nil, otherwise just notify the user that there are upgrades.
3733 This function is called after `package-refresh-contents'."
3734 (let ((buf (get-buffer "*Packages*")))
3735 (when (buffer-live-p buf)
3736 (with-current-buffer buf
3737 (if package-menu--mark-upgrades-pending
3738 (package-menu--mark-upgrades-1)
3739 (package-menu--find-and-notify-upgrades))))))
3741 ;;;###autoload
3742 (defun list-packages (&optional no-fetch)
3743 "Display a list of packages.
3744 This first fetches the updated list of packages before
3745 displaying, unless a prefix argument NO-FETCH is specified.
3746 The list is displayed in a buffer named `*Packages*', and
3747 includes the package's version, availability status, and a
3748 short description."
3749 (interactive "P")
3750 (require 'finder-inf nil t)
3751 ;; Initialize the package system if necessary.
3752 (unless package--initialized
3753 (package-initialize t))
3754 ;; Integrate the package-menu with updating the archives.
3755 (add-hook 'package--post-download-archives-hook
3756 #'package-menu--post-refresh)
3757 (add-hook 'package--post-download-archives-hook
3758 #'package-menu--mark-or-notify-upgrades 'append)
3760 ;; Generate the Package Menu.
3761 (let ((buf (get-buffer-create "*Packages*")))
3762 (with-current-buffer buf
3763 ;; Since some packages have their descriptions include non-ASCII
3764 ;; characters...
3765 (setq buffer-file-coding-system 'utf-8)
3766 (package-menu-mode)
3768 ;; Fetch the remote list of packages.
3769 (unless no-fetch (package-menu--refresh-contents))
3771 ;; If we're not async, this would be redundant.
3772 (when package-menu-async
3773 (package-menu--generate nil t)))
3774 ;; The package menu buffer has keybindings. If the user types
3775 ;; `M-x list-packages', that suggests it should become current.
3776 (pop-to-buffer-same-window buf)))
3778 ;;;###autoload
3779 (defalias 'package-list-packages 'list-packages)
3781 ;; Used in finder.el
3782 (defun package-show-package-list (&optional packages keywords)
3783 "Display PACKAGES in a *Packages* buffer.
3784 This is similar to `list-packages', but it does not fetch the
3785 updated list of packages, and it only displays packages with
3786 names in PACKAGES (which should be a list of symbols).
3788 When KEYWORDS are given, only packages with those KEYWORDS are
3789 shown."
3790 (interactive)
3791 (require 'finder-inf nil t)
3792 (let* ((buf (get-buffer-create "*Packages*"))
3793 (win (get-buffer-window buf)))
3794 (with-current-buffer buf
3795 (package-menu-mode)
3796 (package-menu--generate nil packages keywords))
3797 (if win
3798 (select-window win)
3799 (switch-to-buffer buf))))
3801 (defun package-menu--filter-by (predicate suffix)
3802 "Filter \"*Packages*\" buffer by PREDICATE and add SUFFIX to header.
3803 PREDICATE is a function which will be called with one argument, a
3804 `package-desc' object, and returns t if that object should be
3805 listed in the Package Menu.
3807 SUFFIX is passed on to `package-menu--display' and is added to
3808 the header line of the first column."
3809 ;; Update `tabulated-list-entries' so that it contains all
3810 ;; packages before searching.
3811 (package-menu--refresh t nil)
3812 (let (found-entries)
3813 (dolist (entry tabulated-list-entries)
3814 (when (funcall predicate (car entry))
3815 (push entry found-entries)))
3816 (if found-entries
3817 (progn
3818 (setq tabulated-list-entries found-entries)
3819 (package-menu--display t suffix))
3820 (user-error "No packages found"))))
3822 (defun package-menu-filter-by-archive (archive)
3823 "Filter the \"*Packages*\" buffer by ARCHIVE.
3824 Display only packages from package archive ARCHIVE.
3826 When called interactively, prompt for ARCHIVE, which can be a
3827 comma-separated string. If ARCHIVE is empty, show all packages.
3829 When called from Lisp, ARCHIVE can be a string or a list of
3830 strings. If ARCHIVE is nil or the empty string, show all
3831 packages."
3832 (interactive (list (completing-read-multiple
3833 "Filter by archive (comma separated): "
3834 (mapcar #'car package-archives)))
3835 package-menu-mode)
3836 (package--ensure-package-menu-mode)
3837 (let ((re (if (listp archive)
3838 (regexp-opt archive)
3839 archive)))
3840 (package-menu--filter-by (lambda (pkg-desc)
3841 (let ((pkg-archive (package-desc-archive pkg-desc)))
3842 (and pkg-archive
3843 (string-match-p re pkg-archive))))
3844 (concat "archive:" (if (listp archive)
3845 (string-join archive ",")
3846 archive)))))
3848 (defun package-menu-filter-by-description (description)
3849 "Filter the \"*Packages*\" buffer by DESCRIPTION regexp.
3850 Display only packages with a description that matches regexp
3851 DESCRIPTION.
3853 When called interactively, prompt for DESCRIPTION.
3855 If DESCRIPTION is nil or the empty string, show all packages."
3856 (interactive (list (read-regexp "Filter by description (regexp)"))
3857 package-menu-mode)
3858 (package--ensure-package-menu-mode)
3859 (if (or (not description) (string-empty-p description))
3860 (package-menu--generate t t)
3861 (package-menu--filter-by (lambda (pkg-desc)
3862 (string-match description
3863 (package-desc-summary pkg-desc)))
3864 (format "desc:%s" description))))
3866 (defun package-menu-filter-by-keyword (keyword)
3867 "Filter the \"*Packages*\" buffer by KEYWORD.
3868 Display only packages with specified KEYWORD.
3870 When called interactively, prompt for KEYWORD, which can be a
3871 comma-separated string. If KEYWORD is empty, show all packages.
3873 When called from Lisp, KEYWORD can be a string or a list of
3874 strings. If KEYWORD is nil or the empty string, show all
3875 packages."
3876 (interactive (list (completing-read-multiple
3877 "Keywords (comma separated): "
3878 (package-all-keywords)))
3879 package-menu-mode)
3880 (package--ensure-package-menu-mode)
3881 (when (stringp keyword)
3882 (setq keyword (list keyword)))
3883 (if (not keyword)
3884 (package-menu--generate t t)
3885 (package-menu--filter-by (lambda (pkg-desc)
3886 (package--has-keyword-p pkg-desc keyword))
3887 (concat "keyword:" (string-join keyword ",")))))
3889 (define-obsolete-function-alias
3890 'package-menu-filter #'package-menu-filter-by-keyword "27.1")
3892 (defun package-menu-filter-by-name-or-description (name-or-description)
3893 "Filter the \"*Packages*\" buffer by NAME-OR-DESCRIPTION regexp.
3894 Display only packages with a name-or-description that matches regexp
3895 NAME-OR-DESCRIPTION.
3897 When called interactively, prompt for NAME-OR-DESCRIPTION.
3899 If NAME-OR-DESCRIPTION is nil or the empty string, show all
3900 packages."
3901 (interactive (list (read-regexp "Filter by name or description (regexp)"))
3902 package-menu-mode)
3903 (package--ensure-package-menu-mode)
3904 (if (or (not name-or-description) (string-empty-p name-or-description))
3905 (package-menu--generate t t)
3906 (package-menu--filter-by (lambda (pkg-desc)
3907 (or (string-match name-or-description
3908 (package-desc-summary pkg-desc))
3909 (string-match name-or-description
3910 (symbol-name
3911 (package-desc-name pkg-desc)))))
3912 (format "name-or-desc:%s" name-or-description))))
3914 (defun package-menu-filter-by-name (name)
3915 "Filter the \"*Packages*\" buffer by NAME regexp.
3916 Display only packages with name that matches regexp NAME.
3918 When called interactively, prompt for NAME.
3920 If NAME is nil or the empty string, show all packages."
3921 (interactive (list (read-regexp "Filter by name (regexp)"))
3922 package-menu-mode)
3923 (package--ensure-package-menu-mode)
3924 (if (or (not name) (string-empty-p name))
3925 (package-menu--generate t t)
3926 (package-menu--filter-by (lambda (pkg-desc)
3927 (string-match-p name (symbol-name
3928 (package-desc-name pkg-desc))))
3929 (format "name:%s" name))))
3931 (defun package-menu-filter-by-status (status)
3932 "Filter the \"*Packages*\" buffer by STATUS.
3933 Display only packages with specified STATUS.
3935 When called interactively, prompt for STATUS, which can be a
3936 comma-separated string. If STATUS is empty, show all packages.
3938 When called from Lisp, STATUS can be a string or a list of
3939 strings. If STATUS is nil or the empty string, show all
3940 packages."
3941 (interactive (list (completing-read "Filter by status: "
3942 '("avail-obso"
3943 "available"
3944 "built-in"
3945 "dependency"
3946 "disabled"
3947 "external"
3948 "held"
3949 "incompat"
3950 "installed"
3951 "new"
3952 "unsigned")))
3953 package-menu-mode)
3954 (package--ensure-package-menu-mode)
3955 (if (or (not status) (string-empty-p status))
3956 (package-menu--generate t t)
3957 (package-menu--filter-by (lambda (pkg-desc)
3958 (string-match-p status (package-desc-status pkg-desc)))
3959 (format "status:%s" status))))
3961 (defun package-menu-filter-by-version (version predicate)
3962 "Filter the \"*Packages*\" buffer by VERSION and PREDICATE.
3963 Display only packages with a matching version.
3965 When called interactively, prompt for one of the qualifiers `<',
3966 `>' or `=', and a package version. Show only packages that has a
3967 lower (`<'), equal (`=') or higher (`>') version than the
3968 specified one.
3970 When called from Lisp, VERSION should be a version string and
3971 PREDICATE should be the symbol `=', `<' or `>'.
3973 If VERSION is nil or the empty string, show all packages."
3974 (interactive (let ((choice (intern
3975 (char-to-string
3976 (read-char-choice
3977 "Filter by version? [Type =, <, > or q] "
3978 '(?< ?> ?= ?q))))))
3979 (if (eq choice 'q)
3980 '(quit nil)
3981 (list (read-from-minibuffer
3982 (concat "Filter by version ("
3983 (pcase choice
3984 ('= "= equal to")
3985 ('< "< less than")
3986 ('> "> greater than"))
3987 "): "))
3988 choice)))
3989 package-menu-mode)
3990 (package--ensure-package-menu-mode)
3991 (unless (equal predicate 'quit)
3992 (if (or (not version) (string-empty-p version))
3993 (package-menu--generate t t)
3994 (package-menu--filter-by
3995 (let ((fun (pcase predicate
3996 ('= #'version-list-=)
3997 ('< #'version-list-<)
3998 ('> (lambda (a b) (not (version-list-<= a b))))
3999 (_ (error "Unknown predicate: %s" predicate))))
4000 (ver (version-to-list version)))
4001 (lambda (pkg-desc)
4002 (funcall fun (package-desc-version pkg-desc) ver)))
4003 (format "versions:%s%s" predicate version)))))
4005 (defun package-menu-filter-marked ()
4006 "Filter \"*Packages*\" buffer by non-empty upgrade mark.
4007 Unlike other filters, this leaves the marks intact."
4008 (interactive nil package-menu-mode)
4009 (package--ensure-package-menu-mode)
4010 (widen)
4011 (let (found-entries mark pkg-id entry marks)
4012 (save-excursion
4013 (goto-char (point-min))
4014 (while (not (eobp))
4015 (setq mark (char-after))
4016 (unless (eq mark ?\s)
4017 (setq pkg-id (tabulated-list-get-id))
4018 (setq entry (package-menu--print-info-simple pkg-id))
4019 (push entry found-entries)
4020 ;; remember the mark
4021 (push (cons pkg-id mark) marks))
4022 (forward-line))
4023 (if found-entries
4024 (progn
4025 (setq tabulated-list-entries found-entries)
4026 (package-menu--display t nil)
4027 ;; redo the marks, but we must remember the marks!!
4028 (goto-char (point-min))
4029 (while (not (eobp))
4030 (setq mark (cdr (assq (tabulated-list-get-id) marks)))
4031 (tabulated-list-put-tag (char-to-string mark) t)))
4032 (user-error "No packages found")))))
4034 (defun package-menu-filter-upgradable ()
4035 "Filter \"*Packages*\" buffer to show only upgradable packages."
4036 (interactive nil package-menu-mode)
4037 (let ((pkgs (mapcar #'car (package-menu--find-upgrades))))
4038 (package-menu--filter-by
4039 (lambda (pkg)
4040 (memql (package-desc-name pkg) pkgs))
4041 "upgradable")))
4043 (defun package-menu-clear-filter ()
4044 "Clear any filter currently applied to the \"*Packages*\" buffer."
4045 (interactive nil package-menu-mode)
4046 (package--ensure-package-menu-mode)
4047 (package-menu--generate t t))
4049 (defun package-list-packages-no-fetch ()
4050 "Display a list of packages.
4051 Does not fetch the updated list of packages before displaying.
4052 The list is displayed in a buffer named `*Packages*'."
4053 (interactive)
4054 (list-packages t))
4056 ;;;###autoload
4057 (defun package-get-version ()
4058 "Return the version number of the package in which this is used.
4059 Assumes it is used from an Elisp file placed inside the top-level directory
4060 of an installed ELPA package.
4061 The return value is a string (or nil in case we can't find it)."
4062 ;; In a sense, this is a lie, but it does just what we want: precompute
4063 ;; the version at compile time and hardcodes it into the .elc file!
4064 (declare (pure t))
4065 ;; Hack alert!
4066 (let ((file (or (macroexp-file-name) buffer-file-name)))
4067 (cond
4068 ((null file) nil)
4069 ;; Packages are normally installed into directories named "<pkg>-<vers>",
4070 ;; so get the version number from there.
4071 ((string-match "/[^/]+-\\([0-9]\\(?:[0-9.]\\|pre\\|beta\\|alpha\\|snapshot\\)+\\)/[^/]+\\'" file)
4072 (match-string 1 file))
4073 ;; For packages run straight from the an elpa.git clone, there's no
4074 ;; "-<vers>" in the directory name, so we have to fetch the version
4075 ;; the hard way.
4077 (let* ((pkgdir (file-name-directory file))
4078 (pkgname (file-name-nondirectory (directory-file-name pkgdir)))
4079 (mainfile (expand-file-name (concat pkgname ".el") pkgdir)))
4080 (when (file-readable-p mainfile)
4081 (require 'lisp-mnt)
4082 (with-temp-buffer
4083 (insert-file-contents mainfile)
4084 (or (lm-header "package-version")
4085 (lm-header "version")))))))))
4088 ;;;; Quickstart: precompute activation actions for faster start up.
4090 ;; Activating packages via `package-initialize' is costly: for N installed
4091 ;; packages, it needs to read all N <pkg>-pkg.el files first to decide
4092 ;; which packages to activate, and then again N <pkg>-autoloads.el files.
4093 ;; To speed this up, we precompute a mega-autoloads file which is the
4094 ;; concatenation of all those <pkg>-autoloads.el, so we can activate
4095 ;; all packages by loading this one file (and hence without initializing
4096 ;; package.el).
4098 ;; Other than speeding things up, this also offers a bootstrap feature:
4099 ;; it lets us activate packages according to `package-load-list' and
4100 ;; `package-user-dir' even before those vars are set.
4102 (defcustom package-quickstart nil
4103 "Precompute activation actions to speed up startup.
4104 This requires the use of `package-quickstart-refresh' every time the
4105 activations need to be changed, such as when `package-load-list' is modified."
4106 :type 'boolean
4107 :version "27.1")
4109 ;;;###autoload
4110 (defcustom package-quickstart-file
4111 (locate-user-emacs-file "package-quickstart.el")
4112 "Location of the file used to speed up activation of packages at startup."
4113 :type 'file
4114 :initialize #'custom-initialize-delay
4115 :version "27.1")
4117 (defun package--quickstart-maybe-refresh ()
4118 (if package-quickstart
4119 ;; FIXME: Delay refresh in case we're installing/deleting
4120 ;; several packages!
4121 (package-quickstart-refresh)
4122 (delete-file (concat package-quickstart-file "c"))
4123 (delete-file package-quickstart-file)))
4125 (defun package-quickstart-refresh ()
4126 "(Re)Generate the `package-quickstart-file'."
4127 (interactive)
4128 (package-initialize 'no-activate)
4129 (require 'info)
4130 (let ((package--quickstart-pkgs ())
4131 ;; Pretend we haven't activated anything yet!
4132 (package-activated-list ())
4133 ;; Make sure we can load this file without load-source-file-function.
4134 (coding-system-for-write 'emacs-internal)
4135 (Info-directory-list '("")))
4136 (dolist (elt package-alist)
4137 (condition-case err
4138 (package-activate (car elt))
4139 ;; Don't let failure of activation of a package arbitrarily stop
4140 ;; activation of further packages.
4141 (error (message "%s" (error-message-string err)))))
4142 (setq package--quickstart-pkgs (nreverse package--quickstart-pkgs))
4143 (with-temp-file package-quickstart-file
4144 (emacs-lisp-mode) ;For `syntax-ppss'.
4145 (insert ";;; Quickstart file to activate all packages at startup -*- lexical-binding:t -*-\n")
4146 (insert ";; ¡¡ This file is autogenerated by `package-quickstart-refresh', DO NOT EDIT !!\n\n")
4147 (dolist (pkg package--quickstart-pkgs)
4148 (let* ((file
4149 ;; Prefer uncompiled files (and don't accept .so files).
4150 (let ((load-suffixes '(".el" ".elc")))
4151 (locate-library (package--autoloads-file-name pkg))))
4152 (pfile (prin1-to-string file)))
4153 (insert "(let ((load-true-file-name " pfile ")\
4154 (load-file-name " pfile "))\n")
4155 (insert-file-contents file)
4156 ;; Fixup the special #$ reader form and throw away comments.
4157 (while (re-search-forward "#\\$\\|^;\\(.*\n\\)" nil 'move)
4158 (unless (nth 8 (syntax-ppss))
4159 (replace-match (if (match-end 1) "" pfile) t t)))
4160 (unless (bolp) (insert "\n"))
4161 (insert ")\n")))
4162 (pp `(setq package-activated-list
4163 (append ',(mapcar #'package-desc-name package--quickstart-pkgs)
4164 package-activated-list))
4165 (current-buffer))
4166 (let ((info-dirs (butlast Info-directory-list)))
4167 (when info-dirs
4168 (pp `(progn (require 'info)
4169 (info-initialize)
4170 (setq Info-directory-list
4171 (append ',info-dirs Info-directory-list)))
4172 (current-buffer))))
4173 ;; Use `\s' instead of a space character, so this code chunk is not
4174 ;; mistaken for an actual file-local section of package.el.
4175 (insert "\f
4176 ;; Local\sVariables:
4177 ;; version-control: never
4178 ;; no-update-autoloads: t
4179 ;; End:
4181 ;; FIXME: Do it asynchronously in an Emacs subprocess, and
4182 ;; don't show the byte-compiler warnings.
4183 (byte-compile-file package-quickstart-file)))
4185 (defun package--imenu-prev-index-position-function ()
4186 "Move point to previous line in package-menu buffer.
4187 This function is used as a value for
4188 `imenu-prev-index-position-function'."
4189 (unless (bobp)
4190 (forward-line -1)))
4192 (defun package--imenu-extract-index-name-function ()
4193 "Return imenu name for line at point.
4194 This function is used as a value for
4195 `imenu-extract-index-name-function'. Point should be at the
4196 beginning of the line."
4197 (let ((package-desc (tabulated-list-get-id)))
4198 (format "%s (%s): %s"
4199 (package-desc-name package-desc)
4200 (package-version-join (package-desc-version package-desc))
4201 (package-desc-summary package-desc))))
4203 (defun package-browse-url (desc &optional secondary)
4204 "Open the home page of the package under point in a browser.
4205 `browse-url' is used to determine the browser to be used.
4206 If SECONDARY (interactively, the prefix), use the secondary browser."
4207 (interactive (list (tabulated-list-get-id)
4208 current-prefix-arg)
4209 package-menu-mode)
4210 (unless desc
4211 (user-error "No package here"))
4212 (let ((url (cdr (assoc :url (package-desc-extras desc)))))
4213 (unless url
4214 (user-error "No home page for %s" (package-desc-name desc)))
4215 (if secondary
4216 (funcall browse-url-secondary-browser-function url)
4217 (browse-url url))))
4219 ;;;; Introspection
4221 (defun package-get-descriptor (pkg-name)
4222 "Return the `package-desc' of PKG-NAME."
4223 (unless package--initialized (package-initialize 'no-activate))
4224 (or (package--get-activatable-pkg pkg-name)
4225 (cadr (assq pkg-name package-alist))
4226 (cadr (assq pkg-name package-archive-contents))))
4228 (provide 'package)
4230 ;;; package.el ends here