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