* lisp/emacs-lisp/pcase.el: Use PAT rather than UPAT in docstring
[emacs.git] / lisp / emacs-lisp / package.el
blobb96518df502b9ef92d7a46a0035b29e038a56a89
1 ;;; package.el --- Simple package system for Emacs -*- lexical-binding:t -*-
3 ;; Copyright (C) 2007-2015 Free Software Foundation, Inc.
5 ;; Author: Tom Tromey <tromey@redhat.com>
6 ;; Daniel Hackney <dan@haxney.org>
7 ;; Created: 10 Mar 2007
8 ;; Version: 1.0.1
9 ;; Keywords: tools
10 ;; Package-Requires: ((tabulated-list "1.0"))
12 ;; This file is part of GNU Emacs.
14 ;; GNU Emacs is free software: you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation, either version 3 of the License, or
17 ;; (at your option) any later version.
19 ;; GNU Emacs is distributed in the hope that it will be useful,
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 ;; GNU General Public License for more details.
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
27 ;;; Change Log:
29 ;; 2 Apr 2007 - now using ChangeLog file
30 ;; 15 Mar 2007 - updated documentation
31 ;; 14 Mar 2007 - Changed how obsolete packages are handled
32 ;; 13 Mar 2007 - Wrote package-install-from-buffer
33 ;; 12 Mar 2007 - Wrote package-menu mode
35 ;;; Commentary:
37 ;; The idea behind package.el is to be able to download packages and
38 ;; install them. Packages are versioned and have versioned
39 ;; dependencies. Furthermore, this supports built-in packages which
40 ;; may or may not be newer than user-specified packages. This makes
41 ;; it possible to upgrade Emacs and automatically disable packages
42 ;; which have moved from external to core. (Note though that we don't
43 ;; currently register any of these, so this feature does not actually
44 ;; work.)
46 ;; A package is described by its name and version. The distribution
47 ;; format is either a tar file or a single .el file.
49 ;; A tar file should be named "NAME-VERSION.tar". The tar file must
50 ;; unpack into a directory named after the package and version:
51 ;; "NAME-VERSION". It must contain a file named "PACKAGE-pkg.el"
52 ;; which consists of a call to define-package. It may also contain a
53 ;; "dir" file and the info files it references.
55 ;; A .el file is named "NAME-VERSION.el" in the remote archive, but is
56 ;; installed as simply "NAME.el" in a directory named "NAME-VERSION".
58 ;; The downloader downloads all dependent packages. By default,
59 ;; packages come from the official GNU sources, but others may be
60 ;; added by customizing the `package-archives' alist. Packages get
61 ;; byte-compiled at install time.
63 ;; At activation time we will set up the load-path and the info path,
64 ;; and we will load the package's autoloads. If a package's
65 ;; dependencies are not available, we will not activate that package.
67 ;; Conceptually a package has multiple state transitions:
69 ;; * Download. Fetching the package from ELPA.
70 ;; * Install. Untar the package, or write the .el file, into
71 ;; ~/.emacs.d/elpa/ directory.
72 ;; * Byte compile. Currently this phase is done during install,
73 ;; but we may change this.
74 ;; * Activate. Evaluate the autoloads for the package to make it
75 ;; available to the user.
76 ;; * Load. Actually load the package and run some code from it.
78 ;; Other external functions you may want to use:
80 ;; M-x list-packages
81 ;; Enters a mode similar to buffer-menu which lets you manage
82 ;; packages. You can choose packages for install (mark with "i",
83 ;; then "x" to execute) or deletion (not implemented yet), and you
84 ;; can see what packages are available. This will automatically
85 ;; fetch the latest list of packages from ELPA.
87 ;; M-x package-install-from-buffer
88 ;; Install a package consisting of a single .el file that appears
89 ;; in the current buffer. This only works for packages which
90 ;; define a Version header properly; package.el also supports the
91 ;; extension headers Package-Version (in case Version is an RCS id
92 ;; or similar), and Package-Requires (if the package requires other
93 ;; packages).
95 ;; M-x package-install-file
96 ;; Install a package from the indicated file. The package can be
97 ;; either a tar file or a .el file. A tar file must contain an
98 ;; appropriately-named "-pkg.el" file; a .el file must be properly
99 ;; formatted as with package-install-from-buffer.
101 ;;; Thanks:
102 ;;; (sorted by sort-lines):
104 ;; Jim Blandy <jimb@red-bean.com>
105 ;; Karl Fogel <kfogel@red-bean.com>
106 ;; Kevin Ryde <user42@zip.com.au>
107 ;; Lawrence Mitchell
108 ;; Michael Olson <mwolson@member.fsf.org>
109 ;; Sebastian Tennant <sebyte@smolny.plus.com>
110 ;; Stefan Monnier <monnier@iro.umontreal.ca>
111 ;; Vinicius Jose Latorre <viniciusjl@ig.com.br>
112 ;; Phil Hagelberg <phil@hagelb.org>
114 ;;; ToDo:
116 ;; - putting info dirs at the start of the info path means
117 ;; users see a weird ordering of categories. OTOH we want to
118 ;; override later entries. maybe emacs needs to enforce
119 ;; the standard layout?
120 ;; - put bytecode in a separate directory tree
121 ;; - perhaps give users a way to recompile their bytecode
122 ;; or do it automatically when emacs changes
123 ;; - give users a way to know whether a package is installed ok
124 ;; - give users a way to view a package's documentation when it
125 ;; only appears in the .el
126 ;; - use/extend checkdoc so people can tell if their package will work
127 ;; - "installed" instead of a blank in the status column
128 ;; - tramp needs its files to be compiled in a certain order.
129 ;; how to handle this? fix tramp?
130 ;; - on emacs 21 we don't kill the -autoloads.el buffer. what about 22?
131 ;; - maybe we need separate .elc directories for various emacs versions
132 ;; and also emacs-vs-xemacs. That way conditional compilation can
133 ;; work. But would this break anything?
134 ;; - should store the package's keywords in archive-contents, then
135 ;; let the users filter the package-menu by keyword. See
136 ;; finder-by-keyword. (We could also let people view the
137 ;; Commentary, but it isn't clear how useful this is.)
138 ;; - William Xu suggests being able to open a package file without
139 ;; installing it
140 ;; - Interface with desktop.el so that restarting after an install
141 ;; works properly
142 ;; - Use hierarchical layout. PKG/etc PKG/lisp PKG/info
143 ;; ... except maybe lisp?
144 ;; - It may be nice to have a macro that expands to the package's
145 ;; private data dir, aka ".../etc". Or, maybe data-directory
146 ;; needs to be a list (though this would be less nice)
147 ;; a few packages want this, eg sokoban
148 ;; - package menu needs:
149 ;; ability to know which packages are built-in & thus not deletable
150 ;; it can sometimes print odd results, like 0.3 available but 0.4 active
151 ;; why is that?
152 ;; - Allow multiple versions on the server...?
153 ;; [ why bother? ]
154 ;; - Don't install a package which will invalidate dependencies overall
155 ;; - Allow something like (or (>= emacs 21.0) (>= xemacs 21.5))
156 ;; [ currently thinking, why bother.. KISS ]
157 ;; - Allow optional package dependencies
158 ;; then if we require 'bbdb', bbdb-specific lisp in lisp/bbdb
159 ;; and just don't compile to add to load path ...?
160 ;; - Our treatment of the info path is somewhat bogus
162 ;;; Code:
164 (eval-when-compile (require 'subr-x))
165 (eval-when-compile (require 'cl-lib))
166 (eval-when-compile (require 'epg)) ;For setf accessors.
168 (require 'tabulated-list)
169 (require 'macroexp)
171 (defgroup package nil
172 "Manager for Emacs Lisp packages."
173 :group 'applications
174 :version "24.1")
177 ;;; Customization options
178 ;;;###autoload
179 (defcustom package-enable-at-startup t
180 "Whether to activate installed packages when Emacs starts.
181 If non-nil, packages are activated after reading the init file
182 and before `after-init-hook'. Activation is not done if
183 `user-init-file' is nil (e.g. Emacs was started with \"-q\").
185 Even if the value is nil, you can type \\[package-initialize] to
186 activate the package system at any time."
187 :type 'boolean
188 :version "24.1")
190 (defcustom package-load-list '(all)
191 "List of packages for `package-initialize' to load.
192 Each element in this list should be a list (NAME VERSION), or the
193 symbol `all'. The symbol `all' says to load the latest installed
194 versions of all packages not specified by other elements.
196 For an element (NAME VERSION), NAME is a package name (a symbol).
197 VERSION should be t, a string, or nil.
198 If VERSION is t, the most recent version is activated.
199 If VERSION is a string, only that version is ever loaded.
200 Any other version, even if newer, is silently ignored.
201 Hence, the package is \"held\" at that version.
202 If VERSION is nil, the package is not loaded (it is \"disabled\")."
203 :type '(repeat symbol)
204 :risky t
205 :version "24.1")
207 (defcustom package-archives '(("gnu" . "http://elpa.gnu.org/packages/"))
208 "An alist of archives from which to fetch.
209 The default value points to the GNU Emacs package repository.
211 Each element has the form (ID . LOCATION).
212 ID is an archive name, as a string.
213 LOCATION specifies the base location for the archive.
214 If it starts with \"http:\", it is treated as a HTTP URL;
215 otherwise it should be an absolute directory name.
216 (Other types of URL are currently not supported.)
218 Only add locations that you trust, since fetching and installing
219 a package can run arbitrary code."
220 :type '(alist :key-type (string :tag "Archive name")
221 :value-type (string :tag "URL or directory name"))
222 :risky t
223 :version "24.1")
225 (defcustom package-menu-hide-low-priority 'archive
226 "If non-nil, hide low priority packages from the packages menu.
227 A package is considered low priority if there's another version
228 of it available such that:
229 (a) the archive of the other package is higher priority than
230 this one, as per `package-archive-priorities';
232 (b) they both have the same archive priority but the other
233 package has a higher version number.
235 This variable has three possible values:
236 nil: no packages are hidden;
237 archive: only criteria (a) is used;
238 t: both criteria are used.
240 This variable has no effect if `package-menu--hide-obsolete' is
241 nil, so it can be toggled with \\<package-menu-mode-map> \\[package-menu-hide-obsolete]."
242 :type '(choice (const :tag "Don't hide anything" nil)
243 (const :tag "Hide per package-archive-priorities"
244 archive)
245 (const :tag "Hide per archive and version number" t))
246 :version "25.1")
248 (defcustom package-archive-priorities nil
249 "An alist of priorities for packages.
251 Each element has the form (ARCHIVE-ID . PRIORITY).
253 When installing packages, the package with the highest version
254 number from the archive with the highest priority is
255 selected. When higher versions are available from archives with
256 lower priorities, the user has to select those manually.
258 Archives not in this list have the priority 0.
260 See also `package-menu-hide-low-priority'."
261 :type '(alist :key-type (string :tag "Archive name")
262 :value-type (integer :tag "Priority (default is 0)"))
263 :risky t
264 :version "25.1")
266 (defcustom package-pinned-packages nil
267 "An alist of packages that are pinned to specific archives.
268 This can be useful if you have multiple package archives enabled,
269 and want to control which archive a given package gets installed from.
271 Each element of the alist has the form (PACKAGE . ARCHIVE), where:
272 PACKAGE is a symbol representing a package
273 ARCHIVE is a string representing an archive (it should be the car of
274 an element in `package-archives', e.g. \"gnu\").
276 Adding an entry to this variable means that only ARCHIVE will be
277 considered as a source for PACKAGE. If other archives provide PACKAGE,
278 they are ignored (for this package). If ARCHIVE does not contain PACKAGE,
279 the package will be unavailable."
280 :type '(alist :key-type (symbol :tag "Package")
281 :value-type (string :tag "Archive name"))
282 ;; I don't really see why this is risky...
283 ;; I suppose it could prevent you receiving updates for a package,
284 ;; via an entry (PACKAGE . NON-EXISTING). Which could be an issue
285 ;; if PACKAGE has a known vulnerability that is fixed in newer versions.
286 :risky t
287 :version "24.4")
289 (defcustom package-user-dir (locate-user-emacs-file "elpa")
290 "Directory containing the user's Emacs Lisp packages.
291 The directory name should be absolute.
292 Apart from this directory, Emacs also looks for system-wide
293 packages in `package-directory-list'."
294 :type 'directory
295 :risky t
296 :version "24.1")
298 (defcustom package-directory-list
299 ;; Defaults are subdirs named "elpa" in the site-lisp dirs.
300 (let (result)
301 (dolist (f load-path)
302 (and (stringp f)
303 (equal (file-name-nondirectory f) "site-lisp")
304 (push (expand-file-name "elpa" f) result)))
305 (nreverse result))
306 "List of additional directories containing Emacs Lisp packages.
307 Each directory name should be absolute.
309 These directories contain packages intended for system-wide; in
310 contrast, `package-user-dir' contains packages for personal use."
311 :type '(repeat directory)
312 :risky t
313 :version "24.1")
315 (defvar epg-gpg-program)
317 (defcustom package-check-signature
318 (if (progn (require 'epg-config) (executable-find epg-gpg-program))
319 'allow-unsigned)
320 "Non-nil means to check package signatures when installing.
321 The value `allow-unsigned' means to still install a package even if
322 it is unsigned.
324 This also applies to the \"archive-contents\" file that lists the
325 contents of the archive."
326 :type '(choice (const nil :tag "Never")
327 (const allow-unsigned :tag "Allow unsigned")
328 (const t :tag "Check always"))
329 :risky t
330 :version "24.4")
332 (defcustom package-unsigned-archives nil
333 "List of archives where we do not check for package signatures."
334 :type '(repeat (string :tag "Archive name"))
335 :risky t
336 :version "24.4")
338 (defcustom package-selected-packages nil
339 "Store here packages installed explicitly by user.
340 This variable is fed automatically by Emacs when installing a new package.
341 This variable is used by `package-autoremove' to decide
342 which packages are no longer needed.
343 You can use it to (re)install packages on other machines
344 by running `package-install-selected-packages'.
346 To check if a package is contained in this list here, use
347 `package--user-selected-p', as it may populate the variable with
348 a sane initial value."
349 :type '(repeat symbol))
351 (defcustom package-menu-async t
352 "If non-nil, package-menu will use async operations when possible.
353 Currently, only the refreshing of archive contents supports
354 asynchronous operations. Package transactions are still done
355 synchronously."
356 :type 'boolean
357 :version "25.1")
360 ;;; `package-desc' object definition
361 ;; This is the struct used internally to represent packages.
362 ;; Functions that deal with packages should generally take this object
363 ;; as an argument. In some situations (e.g. commands that query the
364 ;; user) it makes sense to take the package name as a symbol instead,
365 ;; but keep in mind there could be multiple `package-desc's with the
366 ;; same name.
367 (defvar package--default-summary "No description available.")
369 (cl-defstruct (package-desc
370 ;; Rename the default constructor from `make-package-desc'.
371 (:constructor package-desc-create)
372 ;; Has the same interface as the old `define-package',
373 ;; which is still used in the "foo-pkg.el" files. Extra
374 ;; options can be supported by adding additional keys.
375 (:constructor
376 package-desc-from-define
377 (name-string version-string &optional summary requirements
378 &rest rest-plist
379 &aux
380 (name (intern name-string))
381 (version (version-to-list version-string))
382 (reqs (mapcar #'(lambda (elt)
383 (list (car elt)
384 (version-to-list (cadr elt))))
385 (if (eq 'quote (car requirements))
386 (nth 1 requirements)
387 requirements)))
388 (kind (plist-get rest-plist :kind))
389 (archive (plist-get rest-plist :archive))
390 (extras (let (alist)
391 (while rest-plist
392 (unless (memq (car rest-plist) '(:kind :archive))
393 (let ((value (cadr rest-plist)))
394 (when value
395 (push (cons (car rest-plist)
396 (if (eq (car-safe value) 'quote)
397 (cadr value)
398 value))
399 alist))))
400 (setq rest-plist (cddr rest-plist)))
401 alist)))))
402 "Structure containing information about an individual package.
403 Slots:
405 `name' Name of the package, as a symbol.
407 `version' Version of the package, as a version list.
409 `summary' Short description of the package, typically taken from
410 the first line of the file.
412 `reqs' Requirements of the package. A list of (PACKAGE
413 VERSION-LIST) naming the dependent package and the minimum
414 required version.
416 `kind' The distribution format of the package. Currently, it is
417 either `single' or `tar'.
419 `archive' The name of the archive (as a string) whence this
420 package came.
422 `dir' The directory where the package is installed (if installed),
423 `builtin' if it is built-in, or nil otherwise.
425 `extras' Optional alist of additional keyword-value pairs.
427 `signed' Flag to indicate that the package is signed by provider."
428 name
429 version
430 (summary package--default-summary)
431 reqs
432 kind
433 archive
435 extras
436 signed)
438 (defun package--from-builtin (bi-desc)
439 (package-desc-create :name (pop bi-desc)
440 :version (package--bi-desc-version bi-desc)
441 :summary (package--bi-desc-summary bi-desc)
442 :dir 'builtin))
444 ;; Pseudo fields.
445 (defun package-version-join (vlist)
446 "Return the version string corresponding to the list VLIST.
447 This is, approximately, the inverse of `version-to-list'.
448 \(Actually, it returns only one of the possible inverses, since
449 `version-to-list' is a many-to-one operation.)"
450 (if (null vlist)
452 (let ((str-list (list "." (int-to-string (car vlist)))))
453 (dolist (num (cdr vlist))
454 (cond
455 ((>= num 0)
456 (push (int-to-string num) str-list)
457 (push "." str-list))
458 ((< num -4)
459 (error "Invalid version list `%s'" vlist))
461 ;; pre, or beta, or alpha
462 (cond ((equal "." (car str-list))
463 (pop str-list))
464 ((not (string-match "[0-9]+" (car str-list)))
465 (error "Invalid version list `%s'" vlist)))
466 (push (cond ((= num -1) "pre")
467 ((= num -2) "beta")
468 ((= num -3) "alpha")
469 ((= num -4) "snapshot"))
470 str-list))))
471 (if (equal "." (car str-list))
472 (pop str-list))
473 (apply 'concat (nreverse str-list)))))
475 (defun package-desc-full-name (pkg-desc)
476 (format "%s-%s"
477 (package-desc-name pkg-desc)
478 (package-version-join (package-desc-version pkg-desc))))
480 (defun package-desc-suffix (pkg-desc)
481 (pcase (package-desc-kind pkg-desc)
482 (`single ".el")
483 (`tar ".tar")
484 (`dir "")
485 (kind (error "Unknown package kind: %s" kind))))
487 (defun package-desc--keywords (pkg-desc)
488 (let ((keywords (cdr (assoc :keywords (package-desc-extras pkg-desc)))))
489 (if (eq (car-safe keywords) 'quote)
490 (nth 1 keywords)
491 keywords)))
493 (defun package-desc-priority (p)
494 "Return the priority of the archive of package-desc object P."
495 (package-archive-priority (package-desc-archive p)))
497 ;; Package descriptor format used in finder-inf.el and package--builtins.
498 (cl-defstruct (package--bi-desc
499 (:constructor package-make-builtin (version summary))
500 (:type vector))
501 version
502 reqs
503 summary)
506 ;;; Installed packages
507 ;; The following variables store information about packages present in
508 ;; the system. The most important of these is `package-alist'. The
509 ;; command `package-initialize' is also closely related to this
510 ;; section, but it is left for a later section because it also affects
511 ;; other stuff.
512 (defvar package--builtins nil
513 "Alist of built-in packages.
514 The actual value is initialized by loading the library
515 `finder-inf'; this is not done until it is needed, e.g. by the
516 function `package-built-in-p'.
518 Each element has the form (PKG . PACKAGE-BI-DESC), where PKG is a package
519 name (a symbol) and DESC is a `package--bi-desc' structure.")
520 (put 'package--builtins 'risky-local-variable t)
522 (defvar package-alist nil
523 "Alist of all packages available for activation.
524 Each element has the form (PKG . DESCS), where PKG is a package
525 name (a symbol) and DESCS is a non-empty list of `package-desc' structure,
526 sorted by decreasing versions.
528 This variable is set automatically by `package-load-descriptor',
529 called via `package-initialize'. To change which packages are
530 loaded and/or activated, customize `package-load-list'.")
531 (put 'package-alist 'risky-local-variable t)
533 (defvar package-activated-list nil
534 ;; FIXME: This should implicitly include all builtin packages.
535 "List of the names of currently activated packages.")
536 (put 'package-activated-list 'risky-local-variable t)
538 ;;;; Populating `package-alist'.
539 ;; The following functions are called on each installed package by
540 ;; `package-load-all-descriptors', which ultimately populates the
541 ;; `package-alist' variable.
542 (defun package-process-define-package (exp)
543 (when (eq (car-safe exp) 'define-package)
544 (let* ((new-pkg-desc (apply #'package-desc-from-define (cdr exp)))
545 (name (package-desc-name new-pkg-desc))
546 (version (package-desc-version new-pkg-desc))
547 (old-pkgs (assq name package-alist)))
548 (if (null old-pkgs)
549 ;; If there's no old package, just add this to `package-alist'.
550 (push (list name new-pkg-desc) package-alist)
551 ;; If there is, insert the new package at the right place in the list.
552 (while
553 (if (and (cdr old-pkgs)
554 (version-list-< version
555 (package-desc-version (cadr old-pkgs))))
556 (setq old-pkgs (cdr old-pkgs))
557 (push new-pkg-desc (cdr old-pkgs))
558 nil)))
559 new-pkg-desc)))
561 (defun package-load-descriptor (pkg-dir)
562 "Load the description file in directory PKG-DIR."
563 (let ((pkg-file (expand-file-name (package--description-file pkg-dir)
564 pkg-dir))
565 (signed-file (concat pkg-dir ".signed")))
566 (when (file-exists-p pkg-file)
567 (with-temp-buffer
568 (insert-file-contents pkg-file)
569 (goto-char (point-min))
570 (let ((pkg-desc (or (package-process-define-package
571 (read (current-buffer)))
572 (error "Can't find define-package in %s" pkg-file))))
573 (setf (package-desc-dir pkg-desc) pkg-dir)
574 (if (file-exists-p signed-file)
575 (setf (package-desc-signed pkg-desc) t))
576 pkg-desc)))))
578 (defun package-load-all-descriptors ()
579 "Load descriptors for installed Emacs Lisp packages.
580 This looks for package subdirectories in `package-user-dir' and
581 `package-directory-list'. The variable `package-load-list'
582 controls which package subdirectories may be loaded.
584 In each valid package subdirectory, this function loads the
585 description file containing a call to `define-package', which
586 updates `package-alist'."
587 (dolist (dir (cons package-user-dir package-directory-list))
588 (when (file-directory-p dir)
589 (dolist (subdir (directory-files dir))
590 (let ((pkg-dir (expand-file-name subdir dir)))
591 (when (file-directory-p pkg-dir)
592 (package-load-descriptor pkg-dir)))))))
594 (defun define-package (_name-string _version-string
595 &optional _docstring _requirements
596 &rest _extra-properties)
597 "Define a new package.
598 NAME-STRING is the name of the package, as a string.
599 VERSION-STRING is the version of the package, as a string.
600 DOCSTRING is a short description of the package, a string.
601 REQUIREMENTS is a list of dependencies on other packages.
602 Each requirement is of the form (OTHER-PACKAGE OTHER-VERSION),
603 where OTHER-VERSION is a string.
605 EXTRA-PROPERTIES is currently unused."
606 ;; FIXME: Placeholder! Should we keep it?
607 (error "Don't call me!"))
610 ;;; Package activation
611 ;; Section for functions used by `package-activate', which see.
612 (defun package-disabled-p (pkg-name version)
613 "Return whether PKG-NAME at VERSION can be activated.
614 The decision is made according to `package-load-list'.
615 Return nil if the package can be activated.
616 Return t if the package is completely disabled.
617 Return the max version (as a string) if the package is held at a lower version."
618 (let ((force (assq pkg-name package-load-list)))
619 (cond ((null force) (not (memq 'all package-load-list)))
620 ((null (setq force (cadr force))) t) ; disabled
621 ((eq force t) nil)
622 ((stringp force) ; held
623 (unless (version-list-= version (version-to-list force))
624 force))
625 (t (error "Invalid element in `package-load-list'")))))
627 (defun package-built-in-p (package &optional min-version)
628 "Return true if PACKAGE is built-in to Emacs.
629 Optional arg MIN-VERSION, if non-nil, should be a version list
630 specifying the minimum acceptable version."
631 (if (package-desc-p package) ;; was built-in and then was converted
632 (eq 'builtin (package-desc-dir package))
633 (let ((bi (assq package package--builtin-versions)))
634 (cond
635 (bi (version-list-<= min-version (cdr bi)))
636 ((remove 0 min-version) nil)
638 (require 'finder-inf nil t) ; For `package--builtins'.
639 (assq package package--builtins))))))
641 (defvar Info-directory-list)
642 (declare-function info-initialize "info" ())
644 (defun package-activate-1 (pkg-desc &optional reload)
645 "Activate package given by PKG-DESC, even if it was already active.
646 If RELOAD is non-nil, also `load' any files inside the package which
647 correspond to previously loaded files (those returned by
648 `package--list-loaded-files')."
649 (let* ((name (package-desc-name pkg-desc))
650 (pkg-dir (package-desc-dir pkg-desc))
651 (pkg-dir-dir (file-name-as-directory pkg-dir)))
652 (unless pkg-dir
653 (error "Internal error: unable to find directory for `%s'"
654 (package-desc-full-name pkg-desc)))
655 ;; Add to load path, add autoloads, and activate the package.
656 (let* ((old-lp load-path)
657 (autoloads-file (expand-file-name
658 (format "%s-autoloads" name) pkg-dir))
659 (loaded-files-list (and reload (package--list-loaded-files pkg-dir))))
660 (with-demoted-errors "Error in package-activate-1: %s"
661 (load autoloads-file nil t))
662 (when (and (eq old-lp load-path)
663 (not (or (member pkg-dir load-path)
664 (member pkg-dir-dir load-path))))
665 ;; Old packages don't add themselves to the `load-path', so we have to
666 ;; do it ourselves.
667 (push pkg-dir load-path))
668 ;; Call `load' on all files in `pkg-dir' already present in
669 ;; `load-history'. This is done so that macros in these files are updated
670 ;; to their new definitions. If another package is being installed which
671 ;; depends on this new definition, not doing this update would cause
672 ;; compilation errors and break the installation.
673 (with-demoted-errors "Error in package-activate-1: %s"
674 (mapc (lambda (feature) (load feature nil t))
675 ;; Skip autoloads file since we already evaluated it above.
676 (remove (file-truename autoloads-file) loaded-files-list))))
677 ;; Add info node.
678 (when (file-exists-p (expand-file-name "dir" pkg-dir))
679 ;; FIXME: not the friendliest, but simple.
680 (require 'info)
681 (info-initialize)
682 (push pkg-dir Info-directory-list))
683 (push name package-activated-list)
684 ;; Don't return nil.
687 (declare-function find-library-name "find-func" (library))
689 (defun package--list-loaded-files (dir)
690 "Recursively list all files in DIR which correspond to loaded features.
691 Returns the `file-name-sans-extension' of each file, relative to
692 DIR, sorted by most recently loaded last."
693 (let* ((history (delq nil
694 (mapcar (lambda (x)
695 (let ((f (car x)))
696 (and f (file-name-sans-extension f))))
697 load-history)))
698 (dir (file-truename dir))
699 ;; List all files that have already been loaded.
700 (list-of-conflicts
701 (delq
703 (mapcar
704 (lambda (x) (let* ((file (file-relative-name x dir))
705 ;; Previously loaded file, if any.
706 (previous
707 (ignore-errors
708 (file-name-sans-extension
709 (file-truename (find-library-name file)))))
710 (pos (when previous (member previous history))))
711 ;; Return (RELATIVE-FILENAME . HISTORY-POSITION)
712 (when pos
713 (cons (file-name-sans-extension file) (length pos)))))
714 (directory-files-recursively dir "\\`[^\\.].*\\.el\\'")))))
715 ;; Turn the list of (FILENAME . POS) back into a list of features. Files in
716 ;; subdirectories are returned relative to DIR (so not actually features).
717 (let ((default-directory (file-name-as-directory dir)))
718 (mapcar (lambda (x) (file-truename (car x)))
719 (sort list-of-conflicts
720 ;; Sort the files by ascending HISTORY-POSITION.
721 (lambda (x y) (< (cdr x) (cdr y))))))))
723 ;;;; `package-activate'
724 ;; This function activates a newer version of a package if an older
725 ;; one was already activated. It also loads a features of this
726 ;; package which were already loaded.
727 (defun package-activate (package &optional force)
728 "Activate package PACKAGE.
729 If FORCE is true, (re-)activate it if it's already activated.
730 Newer versions are always activated, regardless of FORCE."
731 (let ((pkg-descs (cdr (assq package package-alist))))
732 ;; Check if PACKAGE is available in `package-alist'.
733 (while
734 (when pkg-descs
735 (let ((available-version (package-desc-version (car pkg-descs))))
736 (or (package-disabled-p package available-version)
737 ;; Prefer a builtin package.
738 (package-built-in-p package available-version))))
739 (setq pkg-descs (cdr pkg-descs)))
740 (cond
741 ;; If no such package is found, maybe it's built-in.
742 ((null pkg-descs)
743 (package-built-in-p package))
744 ;; If the package is already activated, just return t.
745 ((and (memq package package-activated-list) (not force))
747 ;; Otherwise, proceed with activation.
749 (let* ((pkg-vec (car pkg-descs))
750 (fail (catch 'dep-failure
751 ;; Activate its dependencies recursively.
752 (dolist (req (package-desc-reqs pkg-vec))
753 (unless (package-activate (car req))
754 (throw 'dep-failure req))))))
755 (if fail
756 (warn "Unable to activate package `%s'.
757 Required package `%s-%s' is unavailable"
758 package (car fail) (package-version-join (cadr fail)))
759 ;; If all goes well, activate the package itself.
760 (package-activate-1 pkg-vec force)))))))
763 ;;; Installation -- Local operations
764 ;; This section contains a variety of features regarding installing a
765 ;; package to/from disk. This includes autoload generation,
766 ;; unpacking, compiling, as well as defining a package from the
767 ;; current buffer.
769 ;;;; Unpacking
770 (defvar tar-parse-info)
771 (declare-function tar-untar-buffer "tar-mode" ())
772 (declare-function tar-header-name "tar-mode" (tar-header) t)
773 (declare-function tar-header-link-type "tar-mode" (tar-header) t)
775 (defun package-untar-buffer (dir)
776 "Untar the current buffer.
777 This uses `tar-untar-buffer' from Tar mode. All files should
778 untar into a directory named DIR; otherwise, signal an error."
779 (require 'tar-mode)
780 (tar-mode)
781 ;; Make sure everything extracts into DIR.
782 (let ((regexp (concat "\\`" (regexp-quote (expand-file-name dir)) "/"))
783 (case-fold-search (memq system-type '(windows-nt ms-dos cygwin))))
784 (dolist (tar-data tar-parse-info)
785 (let ((name (expand-file-name (tar-header-name tar-data))))
786 (or (string-match regexp name)
787 ;; Tarballs created by some utilities don't list
788 ;; directories with a trailing slash (Bug#13136).
789 (and (string-equal dir name)
790 (eq (tar-header-link-type tar-data) 5))
791 (error "Package does not untar cleanly into directory %s/" dir)))))
792 (tar-untar-buffer))
794 (defun package--alist-to-plist-args (alist)
795 (mapcar 'macroexp-quote
796 (apply #'nconc
797 (mapcar (lambda (pair) (list (car pair) (cdr pair))) alist))))
798 (defun package-unpack (pkg-desc)
799 "Install the contents of the current buffer as a package."
800 (let* ((name (package-desc-name pkg-desc))
801 (dirname (package-desc-full-name pkg-desc))
802 (pkg-dir (expand-file-name dirname package-user-dir)))
803 (pcase (package-desc-kind pkg-desc)
804 (`dir
805 (make-directory pkg-dir t)
806 (let ((file-list
807 (directory-files
808 default-directory 'full "\\`[^.].*\\.el\\'" 'nosort)))
809 (dolist (source-file file-list)
810 (let ((target-el-file
811 (expand-file-name (file-name-nondirectory source-file) pkg-dir)))
812 (copy-file source-file target-el-file t)))
813 ;; Now that the files have been installed, this package is
814 ;; indistinguishable from a `tar' or a `single'. Let's make
815 ;; things simple by ensuring we're one of them.
816 (setf (package-desc-kind pkg-desc)
817 (if (> (length file-list) 1) 'tar 'single))))
818 (`tar
819 (make-directory package-user-dir t)
820 ;; FIXME: should we delete PKG-DIR if it exists?
821 (let* ((default-directory (file-name-as-directory package-user-dir)))
822 (package-untar-buffer dirname)))
823 (`single
824 (let ((el-file (expand-file-name (format "%s.el" name) pkg-dir)))
825 (make-directory pkg-dir t)
826 (package--write-file-no-coding el-file)))
827 (kind (error "Unknown package kind: %S" kind)))
828 (package--make-autoloads-and-stuff pkg-desc pkg-dir)
829 ;; Update package-alist.
830 (let ((new-desc (package-load-descriptor pkg-dir)))
831 ;; FIXME: Check that `new-desc' matches `desc'!
832 ;; FIXME: Compilation should be done as a separate, optional, step.
833 ;; E.g. for multi-package installs, we should first install all packages
834 ;; and then compile them.
835 (package--compile new-desc))
836 ;; Try to activate it.
837 (package-activate name 'force)
838 pkg-dir))
840 (defun package-generate-description-file (pkg-desc pkg-file)
841 "Create the foo-pkg.el file for single-file packages."
842 (let* ((name (package-desc-name pkg-desc)))
843 (let ((print-level nil)
844 (print-quoted t)
845 (print-length nil))
846 (write-region
847 (concat
848 ";;; -*- no-byte-compile: t -*-\n"
849 (prin1-to-string
850 (nconc
851 (list 'define-package
852 (symbol-name name)
853 (package-version-join (package-desc-version pkg-desc))
854 (package-desc-summary pkg-desc)
855 (let ((requires (package-desc-reqs pkg-desc)))
856 (list 'quote
857 ;; Turn version lists into string form.
858 (mapcar
859 (lambda (elt)
860 (list (car elt)
861 (package-version-join (cadr elt))))
862 requires))))
863 (package--alist-to-plist-args
864 (package-desc-extras pkg-desc))))
865 "\n")
866 nil pkg-file nil 'silent))))
868 ;;;; Autoload
869 ;; From Emacs 22, but changed so it adds to load-path.
870 (defun package-autoload-ensure-default-file (file)
871 "Make sure that the autoload file FILE exists and if not create it."
872 (unless (file-exists-p file)
873 (write-region
874 (concat ";;; " (file-name-nondirectory file)
875 " --- automatically extracted autoloads\n"
876 ";;\n"
877 ";;; Code:\n"
878 "(add-to-list 'load-path (or (file-name-directory #$) (car load-path)))\n"
879 "\f\n;; Local Variables:\n"
880 ";; version-control: never\n"
881 ";; no-byte-compile: t\n"
882 ";; no-update-autoloads: t\n"
883 ";; End:\n"
884 ";;; " (file-name-nondirectory file)
885 " ends here\n")
886 nil file nil 'silent))
887 file)
889 (defvar generated-autoload-file)
890 (defvar version-control)
892 (defun package-generate-autoloads (name pkg-dir)
893 (let* ((auto-name (format "%s-autoloads.el" name))
894 ;;(ignore-name (concat name "-pkg.el"))
895 (generated-autoload-file (expand-file-name auto-name pkg-dir))
896 ;; Silence `autoload-generate-file-autoloads'.
897 (noninteractive inhibit-message)
898 (backup-inhibited t)
899 (version-control 'never))
900 (package-autoload-ensure-default-file generated-autoload-file)
901 (update-directory-autoloads pkg-dir)
902 (let ((buf (find-buffer-visiting generated-autoload-file)))
903 (when buf (kill-buffer buf)))
904 auto-name))
906 (defun package--make-autoloads-and-stuff (pkg-desc pkg-dir)
907 "Generate autoloads, description file, etc.. for PKG-DESC installed at PKG-DIR."
908 (package-generate-autoloads (package-desc-name pkg-desc) pkg-dir)
909 (let ((desc-file (expand-file-name (package--description-file pkg-dir)
910 pkg-dir)))
911 (unless (file-exists-p desc-file)
912 (package-generate-description-file pkg-desc desc-file)))
913 ;; FIXME: Create foo.info and dir file from foo.texi?
916 ;;;; Compilation
917 (defvar warning-minimum-level)
918 (defun package--compile (pkg-desc)
919 "Byte-compile installed package PKG-DESC."
920 (let ((warning-minimum-level :error)
921 (save-silently inhibit-message))
922 (package-activate-1 pkg-desc)
923 (byte-recompile-directory (package-desc-dir pkg-desc) 0 t)))
925 ;;;; Inferring package from current buffer
926 (defun package-read-from-string (str)
927 "Read a Lisp expression from STR.
928 Signal an error if the entire string was not used."
929 (let* ((read-data (read-from-string str))
930 (more-left
931 (condition-case nil
932 ;; The call to `ignore' suppresses a compiler warning.
933 (progn (ignore (read-from-string
934 (substring str (cdr read-data))))
936 (end-of-file nil))))
937 (if more-left
938 (error "Can't read whole string")
939 (car read-data))))
941 (defun package--prepare-dependencies (deps)
942 "Turn DEPS into an acceptable list of dependencies.
944 Any parts missing a version string get a default version string
945 of \"0\" (meaning any version) and an appropriate level of lists
946 is wrapped around any parts requiring it."
947 (cond
948 ((not (listp deps))
949 (error "Invalid requirement specifier: %S" deps))
950 (t (mapcar (lambda (dep)
951 (cond
952 ((symbolp dep) `(,dep "0"))
953 ((stringp dep)
954 (error "Invalid requirement specifier: %S" dep))
955 ((and (listp dep) (null (cdr dep)))
956 (list (car dep) "0"))
957 (t dep)))
958 deps))))
960 (declare-function lm-header "lisp-mnt" (header))
961 (declare-function lm-homepage "lisp-mnt" (&optional file))
963 (defun package-buffer-info ()
964 "Return a `package-desc' describing the package in the current buffer.
966 If the buffer does not contain a conforming package, signal an
967 error. If there is a package, narrow the buffer to the file's
968 boundaries."
969 (goto-char (point-min))
970 (unless (re-search-forward "^;;; \\([^ ]*\\)\\.el ---[ \t]*\\(.*?\\)[ \t]*\\(-\\*-.*-\\*-[ \t]*\\)?$" nil t)
971 (error "Package lacks a file header"))
972 (let ((file-name (match-string-no-properties 1))
973 (desc (match-string-no-properties 2))
974 (start (line-beginning-position)))
975 (unless (search-forward (concat ";;; " file-name ".el ends here"))
976 (error "Package lacks a terminating comment"))
977 ;; Try to include a trailing newline.
978 (forward-line)
979 (narrow-to-region start (point))
980 (require 'lisp-mnt)
981 ;; Use some headers we've invented to drive the process.
982 (let* ((requires-str (lm-header "package-requires"))
983 ;; Prefer Package-Version; if defined, the package author
984 ;; probably wants us to use it. Otherwise try Version.
985 (pkg-version
986 (or (package-strip-rcs-id (lm-header "package-version"))
987 (package-strip-rcs-id (lm-header "version"))))
988 (homepage (lm-homepage)))
989 (unless pkg-version
990 (error
991 "Package lacks a \"Version\" or \"Package-Version\" header"))
992 (package-desc-from-define
993 file-name pkg-version desc
994 (if requires-str
995 (package--prepare-dependencies
996 (package-read-from-string requires-str)))
997 :kind 'single
998 :url homepage))))
1000 (defun package--read-pkg-desc (kind)
1001 "Read a `define-package' form in current buffer.
1002 Return the pkg-desc, with desc-kind set to KIND."
1003 (goto-char (point-min))
1004 (unwind-protect
1005 (let* ((pkg-def-parsed (read (current-buffer)))
1006 (pkg-desc
1007 (when (eq (car pkg-def-parsed) 'define-package)
1008 (apply #'package-desc-from-define
1009 (append (cdr pkg-def-parsed))))))
1010 (when pkg-desc
1011 (setf (package-desc-kind pkg-desc) kind)
1012 pkg-desc))))
1014 (declare-function tar-get-file-descriptor "tar-mode" (file))
1015 (declare-function tar--extract "tar-mode" (descriptor))
1017 (defun package-tar-file-info ()
1018 "Find package information for a tar file.
1019 The return result is a `package-desc'."
1020 (cl-assert (derived-mode-p 'tar-mode))
1021 (let* ((dir-name (file-name-directory
1022 (tar-header-name (car tar-parse-info))))
1023 (desc-file (package--description-file dir-name))
1024 (tar-desc (tar-get-file-descriptor (concat dir-name desc-file))))
1025 (unless tar-desc
1026 (error "No package descriptor file found"))
1027 (with-current-buffer (tar--extract tar-desc)
1028 (unwind-protect
1029 (or (package--read-pkg-desc 'tar)
1030 (error "Can't find define-package in %s"
1031 (tar-header-name tar-desc)))
1032 (kill-buffer (current-buffer))))))
1034 (defun package-dir-info ()
1035 "Find package information for a directory.
1036 The return result is a `package-desc'."
1037 (cl-assert (derived-mode-p 'dired-mode))
1038 (let* ((desc-file (package--description-file default-directory)))
1039 (if (file-readable-p desc-file)
1040 (with-temp-buffer
1041 (insert-file-contents desc-file)
1042 (package--read-pkg-desc 'dir))
1043 (let ((files (directory-files default-directory t "\\.el\\'" t))
1044 info)
1045 (while files
1046 (with-temp-buffer
1047 (insert-file-contents (pop files))
1048 ;; When we find the file with the data,
1049 (when (setq info (ignore-errors (package-buffer-info)))
1050 ;; stop looping,
1051 (setq files nil)
1052 ;; set the 'dir kind,
1053 (setf (package-desc-kind info) 'dir))))
1054 ;; and return the info.
1055 info))))
1058 ;;; Communicating with Archives
1059 ;; Set of low-level functions for communicating with archives and
1060 ;; signature checking.
1061 (defun package--write-file-no-coding (file-name)
1062 (let ((buffer-file-coding-system 'no-conversion))
1063 (write-region (point-min) (point-max) file-name nil 'silent)))
1065 (declare-function url-http-file-exists-p "url-http" (url))
1067 (defun package--archive-file-exists-p (location file)
1068 (let ((http (string-match "\\`https?:" location)))
1069 (if http
1070 (progn
1071 (require 'url-http)
1072 (url-http-file-exists-p (concat location file)))
1073 (file-exists-p (expand-file-name file location)))))
1075 (declare-function epg-make-context "epg"
1076 (&optional protocol armor textmode include-certs
1077 cipher-algorithm
1078 digest-algorithm
1079 compress-algorithm))
1080 (declare-function epg-verify-string "epg" (context signature
1081 &optional signed-text))
1082 (declare-function epg-context-result-for "epg" (context name))
1083 (declare-function epg-signature-status "epg" (signature) t)
1084 (declare-function epg-signature-to-string "epg" (signature))
1086 (defun package--display-verify-error (context sig-file)
1087 (unless (equal (epg-context-error-output context) "")
1088 (with-output-to-temp-buffer "*Error*"
1089 (with-current-buffer standard-output
1090 (if (epg-context-result-for context 'verify)
1091 (insert (format "Failed to verify signature %s:\n" sig-file)
1092 (mapconcat #'epg-signature-to-string
1093 (epg-context-result-for context 'verify)
1094 "\n"))
1095 (insert (format "Error while verifying signature %s:\n" sig-file)))
1096 (insert "\nCommand output:\n" (epg-context-error-output context))))))
1098 (defmacro package--with-work-buffer (location file &rest body)
1099 "Run BODY in a buffer containing the contents of FILE at LOCATION.
1100 LOCATION is the base location of a package archive, and should be
1101 one of the URLs (or file names) specified in `package-archives'.
1102 FILE is the name of a file relative to that base location.
1104 This macro retrieves FILE from LOCATION into a temporary buffer,
1105 and evaluates BODY while that buffer is current. This work
1106 buffer is killed afterwards. Return the last value in BODY."
1107 (declare (indent 2) (debug t))
1108 `(with-temp-buffer
1109 (if (string-match-p "\\`https?:" ,location)
1110 (url-insert-file-contents (concat ,location ,file))
1111 (unless (file-name-absolute-p ,location)
1112 (error "Archive location %s is not an absolute file name"
1113 ,location))
1114 (insert-file-contents (expand-file-name ,file ,location)))
1115 ,@body))
1117 (defmacro package--with-work-buffer-async (location file async &rest body)
1118 "Run BODY in a buffer containing the contents of FILE at LOCATION.
1119 If ASYNC is non-nil, and if it is possible, run BODY
1120 asynchronously. If an error is encountered and ASYNC is a
1121 function, call it with no arguments (instead of executing BODY),
1122 otherwise propagate the error. For description of the other
1123 arguments see `package--with-work-buffer'."
1124 (declare (indent 3) (debug t))
1125 (macroexp-let2* macroexp-copyable-p
1126 ((async-1 async)
1127 (file-1 file)
1128 (location-1 location))
1129 `(if (or (not ,async-1)
1130 (not (string-match-p "\\`https?:" ,location-1)))
1131 (package--with-work-buffer ,location-1 ,file-1 ,@body)
1132 (url-retrieve (concat ,location-1 ,file-1)
1133 (lambda (status)
1134 (if (eq (car status) :error)
1135 (if (functionp ,async-1)
1136 (funcall ,async-1)
1137 (signal (cdar status) (cddr status)))
1138 (goto-char (point-min))
1139 (unless (search-forward "\n\n" nil 'noerror)
1140 (error "Invalid url response in buffer %s"
1141 (current-buffer)))
1142 (delete-region (point-min) (point))
1143 ,@body)
1144 (kill-buffer (current-buffer)))
1146 'silent))))
1148 (defun package--check-signature-content (content string &optional sig-file)
1149 "Check signature CONTENT against STRING.
1150 SIG-FILE is the name of the signature file, used when signaling
1151 errors."
1152 (let* ((context (epg-make-context 'OpenPGP))
1153 (homedir (expand-file-name "gnupg" package-user-dir)))
1154 (setf (epg-context-home-directory context) homedir)
1155 (condition-case error
1156 (epg-verify-string context content string)
1157 (error (package--display-verify-error context sig-file)
1158 (signal (car error) (cdr error))))
1159 (let (good-signatures had-fatal-error)
1160 ;; The .sig file may contain multiple signatures. Success if one
1161 ;; of the signatures is good.
1162 (dolist (sig (epg-context-result-for context 'verify))
1163 (if (eq (epg-signature-status sig) 'good)
1164 (push sig good-signatures)
1165 ;; If package-check-signature is allow-unsigned, don't
1166 ;; signal error when we can't verify signature because of
1167 ;; missing public key. Other errors are still treated as
1168 ;; fatal (bug#17625).
1169 (unless (and (eq package-check-signature 'allow-unsigned)
1170 (eq (epg-signature-status sig) 'no-pubkey))
1171 (setq had-fatal-error t))))
1172 (when (and (null good-signatures) had-fatal-error)
1173 (package--display-verify-error context sig-file)
1174 (error "Failed to verify signature %s" sig-file))
1175 good-signatures)))
1177 (defun package--check-signature (location file &optional string async callback)
1178 "Check signature of the current buffer.
1179 Download the signature file from LOCATION by appending \".sig\"
1180 to FILE.
1181 GnuPG keyring is located under \"gnupg\" in `package-user-dir'.
1182 STRING is the string to verify, it defaults to `buffer-string'.
1183 If ASYNC is non-nil, the download of the signature file is
1184 done asynchronously.
1186 If the signature is verified and CALLBACK was provided, CALLBACK
1187 is `funcall'ed with the list of good signatures as argument (the
1188 list can be empty). If the signatures file is not found,
1189 CALLBACK is called with no arguments."
1190 (let ((sig-file (concat file ".sig"))
1191 (string (or string (buffer-string))))
1192 (condition-case nil
1193 (package--with-work-buffer-async
1194 location sig-file (when async (or callback t))
1195 (let ((sig (package--check-signature-content
1196 (buffer-string) string sig-file)))
1197 (when callback (funcall callback sig))
1198 sig))
1199 (file-error (funcall callback)))))
1202 ;;; Packages on Archives
1203 ;; The following variables store information about packages available
1204 ;; from archives. The most important of these is
1205 ;; `package-archive-contents' which is initially populated by the
1206 ;; function `package-read-all-archive-contents' from a cache on disk.
1207 ;; The `package-initialize' command is also closely related to this
1208 ;; section, but it has its own section.
1209 (defconst package-archive-version 1
1210 "Version number of the package archive understood by this file.
1211 Lower version numbers than this will probably be understood as well.")
1213 ;; We don't prime the cache since it tends to get out of date.
1214 (defvar package-archive-contents nil
1215 "Cache of the contents of the Emacs Lisp Package Archive.
1216 This is an alist mapping package names (symbols) to
1217 non-empty lists of `package-desc' structures.")
1218 (put 'package-archive-contents 'risky-local-variable t)
1220 (defvar package--compatibility-table nil
1221 "Hash table connecting package names to their compatibility.
1222 Each key is a symbol, the name of a package.
1224 The value is either nil, representing an incompatible package, or
1225 a version list, representing the highest compatible version of
1226 that package which is available.
1228 A package is considered incompatible if it requires an Emacs
1229 version higher than the one being used. To check for package
1230 \(in)compatibility, don't read this table directly, use
1231 `package--incompatible-p' which also checks dependencies.")
1233 (defun package--build-compatibility-table ()
1234 "Build `package--compatibility-table' with `package--mapc'."
1235 ;; Initialize the list of built-ins.
1236 (require 'finder-inf nil t)
1237 ;; Build compat table.
1238 (setq package--compatibility-table (make-hash-table :test 'eq))
1239 (package--mapc #'package--add-to-compatibility-table))
1241 (defun package--add-to-compatibility-table (pkg)
1242 "If PKG is compatible (without dependencies), add to the compatibility table.
1243 PKG is a package-desc object.
1244 Only adds if its version is higher than what's already stored in
1245 the table."
1246 (unless (package--incompatible-p pkg 'shallow)
1247 (let* ((name (package-desc-name pkg))
1248 (version (or (package-desc-version pkg) '(0)))
1249 (table-version (gethash name package--compatibility-table)))
1250 (when (or (not table-version)
1251 (version-list-< table-version version))
1252 (puthash name version package--compatibility-table)))))
1254 ;; Package descriptor objects used inside the "archive-contents" file.
1255 ;; Changing this defstruct implies changing the format of the
1256 ;; "archive-contents" files.
1257 (cl-defstruct (package--ac-desc
1258 (:constructor package-make-ac-desc (version reqs summary kind extras))
1259 (:copier nil)
1260 (:type vector))
1261 version reqs summary kind extras)
1263 (defun package--append-to-alist (pkg-desc alist)
1264 "Append an entry for PKG-DESC to the start of ALIST and return it.
1265 This entry takes the form (`package-desc-name' PKG-DESC).
1267 If ALIST already has an entry with this name, destructively add
1268 PKG-DESC to the cdr of this entry instead, sorted by version
1269 number."
1270 (let* ((name (package-desc-name pkg-desc))
1271 (priority-version (package-desc-priority-version pkg-desc))
1272 (existing-packages (assq name alist)))
1273 (if (not existing-packages)
1274 (cons (list name pkg-desc)
1275 alist)
1276 (while (if (and (cdr existing-packages)
1277 (version-list-< priority-version
1278 (package-desc-priority-version
1279 (cadr existing-packages))))
1280 (setq existing-packages (cdr existing-packages))
1281 (push pkg-desc (cdr existing-packages))
1282 nil))
1283 alist)))
1285 (defun package--add-to-archive-contents (package archive)
1286 "Add the PACKAGE from the given ARCHIVE if necessary.
1287 PACKAGE should have the form (NAME . PACKAGE--AC-DESC).
1288 Also, add the originating archive to the `package-desc' structure."
1289 (let* ((name (car package))
1290 (version (package--ac-desc-version (cdr package)))
1291 (pkg-desc
1292 (package-desc-create
1293 :name name
1294 :version version
1295 :reqs (package--ac-desc-reqs (cdr package))
1296 :summary (package--ac-desc-summary (cdr package))
1297 :kind (package--ac-desc-kind (cdr package))
1298 :archive archive
1299 :extras (and (> (length (cdr package)) 4)
1300 ;; Older archive-contents files have only 4
1301 ;; elements here.
1302 (package--ac-desc-extras (cdr package)))))
1303 (pinned-to-archive (assoc name package-pinned-packages)))
1304 ;; Skip entirely if pinned to another archive.
1305 (when (not (and pinned-to-archive
1306 (not (equal (cdr pinned-to-archive) archive))))
1307 (setq package-archive-contents
1308 (package--append-to-alist pkg-desc package-archive-contents)))))
1310 (defun package--read-archive-file (file)
1311 "Re-read archive file FILE, if it exists.
1312 Will return the data from the file, or nil if the file does not exist.
1313 Will throw an error if the archive version is too new."
1314 (let ((filename (expand-file-name file package-user-dir)))
1315 (when (file-exists-p filename)
1316 (with-temp-buffer
1317 (let ((coding-system-for-read 'utf-8))
1318 (insert-file-contents filename))
1319 (let ((contents (read (current-buffer))))
1320 (if (> (car contents) package-archive-version)
1321 (error "Package archive version %d is higher than %d"
1322 (car contents) package-archive-version))
1323 (cdr contents))))))
1325 (defun package-read-archive-contents (archive)
1326 "Re-read archive contents for ARCHIVE.
1327 If successful, set the variable `package-archive-contents'.
1328 If the archive version is too new, signal an error."
1329 ;; Version 1 of 'archive-contents' is identical to our internal
1330 ;; representation.
1331 (let* ((contents-file (format "archives/%s/archive-contents" archive))
1332 (contents (package--read-archive-file contents-file)))
1333 (when contents
1334 (dolist (package contents)
1335 (package--add-to-archive-contents package archive)))))
1337 (defun package-read-all-archive-contents ()
1338 "Re-read `archive-contents', if it exists.
1339 If successful, set `package-archive-contents'."
1340 (setq package-archive-contents nil)
1341 (dolist (archive package-archives)
1342 (package-read-archive-contents (car archive))))
1344 ;;;; Package Initialize
1345 ;; A bit of a milestone. This brings together some of the above
1346 ;; sections and populates all relevant lists of packages from contents
1347 ;; available on disk.
1348 (defvar package--initialized nil)
1350 (defvar package--init-file-ensured nil
1351 "Whether we know the init file has package-initialize.")
1353 ;;;###autoload
1354 (defun package-initialize (&optional no-activate)
1355 "Load Emacs Lisp packages, and activate them.
1356 The variable `package-load-list' controls which packages to load.
1357 If optional arg NO-ACTIVATE is non-nil, don't activate packages.
1358 If `user-init-file' does not mention `(package-initialize)', add
1359 it to the file."
1360 (interactive)
1361 (setq package-alist nil)
1362 (if (equal user-init-file load-file-name)
1363 ;; If `package-initialize' is being called as part of loading
1364 ;; the init file, it's obvious we don't need to ensure-init.
1365 (setq package--init-file-ensured t)
1366 (package--ensure-init-file))
1367 (package-load-all-descriptors)
1368 (package-read-all-archive-contents)
1369 (unless no-activate
1370 (dolist (elt package-alist)
1371 (package-activate (car elt))))
1372 (setq package--initialized t)
1373 ;; This uses `package--mapc' so it must be called after
1374 ;; `package--initialized' is t.
1375 (package--build-compatibility-table))
1378 ;;;; Populating `package-archive-contents' from archives
1379 ;; This subsection populates the variables listed above from the
1380 ;; actual archives, instead of from a local cache.
1381 (defvar package--downloads-in-progress nil
1382 "List of in-progress asynchronous downloads.")
1384 (declare-function epg-check-configuration "epg-config"
1385 (config &optional minimum-version))
1386 (declare-function epg-configuration "epg-config" ())
1387 (declare-function epg-import-keys-from-file "epg" (context keys))
1389 ;;;###autoload
1390 (defun package-import-keyring (&optional file)
1391 "Import keys from FILE."
1392 (interactive "fFile: ")
1393 (setq file (expand-file-name file))
1394 (let ((context (epg-make-context 'OpenPGP))
1395 (homedir (expand-file-name "gnupg" package-user-dir)))
1396 (with-file-modes 448
1397 (make-directory homedir t))
1398 (setf (epg-context-home-directory context) homedir)
1399 (message "Importing %s..." (file-name-nondirectory file))
1400 (epg-import-keys-from-file context file)
1401 (message "Importing %s...done" (file-name-nondirectory file))))
1403 (defvar package--post-download-archives-hook nil
1404 "Hook run after the archive contents are downloaded.
1405 Don't run this hook directly. It is meant to be run as part of
1406 `package--update-downloads-in-progress'.")
1407 (put 'package--post-download-archives-hook 'risky-local-variable t)
1409 (defun package--update-downloads-in-progress (entry)
1410 "Remove ENTRY from `package--downloads-in-progress'.
1411 Once it's empty, run `package--post-download-archives-hook'."
1412 ;; Keep track of the downloading progress.
1413 (setq package--downloads-in-progress
1414 (remove entry package--downloads-in-progress))
1415 ;; If this was the last download, run the hook.
1416 (unless package--downloads-in-progress
1417 (package-read-all-archive-contents)
1418 (package--build-compatibility-table)
1419 ;; We message before running the hook, so the hook can give
1420 ;; messages as well.
1421 (message "Package refresh done")
1422 (run-hooks 'package--post-download-archives-hook)))
1424 (defun package--download-one-archive (archive file &optional async)
1425 "Retrieve an archive file FILE from ARCHIVE, and cache it.
1426 ARCHIVE should be a cons cell of the form (NAME . LOCATION),
1427 similar to an entry in `package-alist'. Save the cached copy to
1428 \"archives/NAME/FILE\" in `package-user-dir'."
1429 (package--with-work-buffer-async (cdr archive) file async
1430 (let* ((location (cdr archive))
1431 (name (car archive))
1432 (content (buffer-string))
1433 (dir (expand-file-name (format "archives/%s" name) package-user-dir))
1434 (local-file (expand-file-name file dir)))
1435 (when (listp (read-from-string content))
1436 (make-directory dir t)
1437 (if (or (not package-check-signature)
1438 (member archive package-unsigned-archives))
1439 ;; If we don't care about the signature, save the file and
1440 ;; we're done.
1441 (progn (write-region content nil local-file nil 'silent)
1442 (package--update-downloads-in-progress archive))
1443 ;; If we care, check it (perhaps async) and *then* write the file.
1444 (package--check-signature
1445 location file content async
1446 ;; This function will be called after signature checking.
1447 (lambda (&optional good-sigs)
1448 (unless (or good-sigs (eq package-check-signature 'allow-unsigned))
1449 ;; Even if the sig fails, this download is done, so
1450 ;; remove it from the in-progress list.
1451 (package--update-downloads-in-progress archive)
1452 (error "Unsigned archive `%s'" name))
1453 ;; Write out the archives file.
1454 (write-region content nil local-file nil 'silent)
1455 ;; Write out good signatures into archive-contents.signed file.
1456 (when good-sigs
1457 (write-region (mapconcat #'epg-signature-to-string good-sigs "\n")
1458 nil (concat local-file ".signed") nil 'silent))
1459 (package--update-downloads-in-progress archive))))))))
1461 (defun package--download-and-read-archives (&optional async)
1462 "Download descriptions of all `package-archives' and read them.
1463 This populates `package-archive-contents'. If ASYNC is non-nil,
1464 perform the downloads asynchronously."
1465 ;; The downloaded archive contents will be read as part of
1466 ;; `package--update-downloads-in-progress'.
1467 (dolist (archive package-archives)
1468 (cl-pushnew archive package--downloads-in-progress
1469 :test #'equal))
1470 (dolist (archive package-archives)
1471 (condition-case-unless-debug nil
1472 (package--download-one-archive
1473 archive "archive-contents"
1474 ;; Called if the async download fails
1475 (when async
1476 (lambda () (package--update-downloads-in-progress archive))))
1477 (error (message "Failed to download `%s' archive."
1478 (car archive))))))
1480 ;;;###autoload
1481 (defun package-refresh-contents (&optional async)
1482 "Download descriptions of all configured ELPA packages.
1483 For each archive configured in the variable `package-archives',
1484 inform Emacs about the latest versions of all packages it offers,
1485 and make them available for download.
1486 Optional argument ASYNC specifies whether to perform the
1487 downloads in the background."
1488 (interactive)
1489 (unless (file-exists-p package-user-dir)
1490 (make-directory package-user-dir t))
1491 (let ((default-keyring (expand-file-name "package-keyring.gpg"
1492 data-directory))
1493 (inhibit-message async))
1494 (when (and package-check-signature (file-exists-p default-keyring))
1495 (condition-case-unless-debug error
1496 (progn
1497 (epg-check-configuration (epg-configuration))
1498 (package-import-keyring default-keyring))
1499 (error (message "Cannot import default keyring: %S" (cdr error))))))
1500 (package--download-and-read-archives async))
1503 ;;; Dependency Management
1504 ;; Calculating the full transaction necessary for an installation,
1505 ;; keeping track of which packages were installed strictly as
1506 ;; dependencies, and determining which packages cannot be removed
1507 ;; because they are dependencies.
1508 (defun package-compute-transaction (packages requirements &optional seen)
1509 "Return a list of packages to be installed, including PACKAGES.
1510 PACKAGES should be a list of `package-desc'.
1512 REQUIREMENTS should be a list of additional requirements; each
1513 element in this list should have the form (PACKAGE VERSION-LIST),
1514 where PACKAGE is a package name and VERSION-LIST is the required
1515 version of that package.
1517 This function recursively computes the requirements of the
1518 packages in REQUIREMENTS, and returns a list of all the packages
1519 that must be installed. Packages that are already installed are
1520 not included in this list.
1522 SEEN is used internally to detect infinite recursion."
1523 ;; FIXME: We really should use backtracking to explore the whole
1524 ;; search space (e.g. if foo require bar-1.3, and bar-1.4 requires toto-1.1
1525 ;; whereas bar-1.3 requires toto-1.0 and the user has put a hold on toto-1.0:
1526 ;; the current code might fail to see that it could install foo by using the
1527 ;; older bar-1.3).
1528 (dolist (elt requirements)
1529 (let* ((next-pkg (car elt))
1530 (next-version (cadr elt))
1531 (already ()))
1532 (dolist (pkg packages)
1533 (if (eq next-pkg (package-desc-name pkg))
1534 (setq already pkg)))
1535 (when already
1536 (if (version-list-<= next-version (package-desc-version already))
1537 ;; `next-pkg' is already in `packages', but its position there
1538 ;; means it might be installed too late: remove it from there, so
1539 ;; we re-add it (along with its dependencies) at an earlier place
1540 ;; below (bug#16994).
1541 (if (memq already seen) ;Avoid inf-loop on dependency cycles.
1542 (message "Dependency cycle going through %S"
1543 (package-desc-full-name already))
1544 (setq packages (delq already packages))
1545 (setq already nil))
1546 (error "Need package `%s-%s', but only %s is being installed"
1547 next-pkg (package-version-join next-version)
1548 (package-version-join (package-desc-version already)))))
1549 (cond
1550 (already nil)
1551 ((package-installed-p next-pkg next-version) nil)
1554 ;; A package is required, but not installed. It might also be
1555 ;; blocked via `package-load-list'.
1556 (let ((pkg-descs (cdr (assq next-pkg package-archive-contents)))
1557 (found nil)
1558 (problem nil))
1559 (while (and pkg-descs (not found))
1560 (let* ((pkg-desc (pop pkg-descs))
1561 (version (package-desc-version pkg-desc))
1562 (disabled (package-disabled-p next-pkg version)))
1563 (cond
1564 ((version-list-< version next-version)
1565 (error
1566 "Need package `%s-%s', but only %s is available"
1567 next-pkg (package-version-join next-version)
1568 (package-version-join version)))
1569 (disabled
1570 (unless problem
1571 (setq problem
1572 (if (stringp disabled)
1573 (format "Package `%s' held at version %s, \
1574 but version %s required"
1575 next-pkg disabled
1576 (package-version-join next-version))
1577 (format "Required package '%s' is disabled"
1578 next-pkg)))))
1579 (t (setq found pkg-desc)))))
1580 (unless found
1581 (if problem
1582 (error "%s" problem)
1583 (error "Package `%s-%s' is unavailable"
1584 next-pkg (package-version-join next-version))))
1585 (setq packages
1586 (package-compute-transaction (cons found packages)
1587 (package-desc-reqs found)
1588 (cons found seen))))))))
1589 packages)
1591 (defun package--find-non-dependencies ()
1592 "Return a list of installed packages which are not dependencies.
1593 Finds all packages in `package-alist' which are not dependencies
1594 of any other packages.
1595 Used to populate `package-selected-packages'."
1596 (let ((dep-list
1597 (delete-dups
1598 (apply #'append
1599 (mapcar (lambda (p) (mapcar #'car (package-desc-reqs (cadr p))))
1600 package-alist)))))
1601 (cl-loop for p in package-alist
1602 for name = (car p)
1603 unless (memq name dep-list)
1604 collect name)))
1606 (defun package--save-selected-packages (value)
1607 "Set and save `package-selected-packages' to VALUE."
1608 (let ((save-silently inhibit-message))
1609 (customize-save-variable
1610 'package-selected-packages
1611 (setq package-selected-packages value))))
1613 (defun package--user-selected-p (pkg)
1614 "Return non-nil if PKG is a package was installed by the user.
1615 PKG is a package name.
1616 This looks into `package-selected-packages', populating it first
1617 if it is still empty."
1618 (unless (consp package-selected-packages)
1619 (package--save-selected-packages (package--find-non-dependencies)))
1620 (memq pkg package-selected-packages))
1622 (defun package--get-deps (pkg &optional only)
1623 (let* ((pkg-desc (cadr (assq pkg package-alist)))
1624 (direct-deps (cl-loop for p in (package-desc-reqs pkg-desc)
1625 for name = (car p)
1626 when (assq name package-alist)
1627 collect name))
1628 (indirect-deps (unless (eq only 'direct)
1629 (delete-dups
1630 (cl-loop for p in direct-deps
1631 append (package--get-deps p))))))
1632 (cl-case only
1633 (direct direct-deps)
1634 (separate (list direct-deps indirect-deps))
1635 (indirect indirect-deps)
1636 (t (delete-dups (append direct-deps indirect-deps))))))
1638 (defun package--removable-packages ()
1639 "Return a list of names of packages no longer needed.
1640 These are packages which are neither contained in
1641 `package-selected-packages' nor a dependency of one that is."
1642 (let ((needed (cl-loop for p in package-selected-packages
1643 if (assq p package-alist)
1644 ;; `p' and its dependencies are needed.
1645 append (cons p (package--get-deps p)))))
1646 (cl-loop for p in (mapcar #'car package-alist)
1647 unless (memq p needed)
1648 collect p)))
1650 (defun package--used-elsewhere-p (pkg-desc &optional pkg-list all)
1651 "Non-nil if PKG-DESC is a dependency of a package in PKG-LIST.
1652 Return the first package found in PKG-LIST of which PKG is a
1653 dependency. If ALL is non-nil, return all such packages instead.
1655 When not specified, PKG-LIST defaults to `package-alist'
1656 with PKG-DESC entry removed."
1657 (unless (string= (package-desc-status pkg-desc) "obsolete")
1658 (let* ((pkg (package-desc-name pkg-desc))
1659 (alist (or pkg-list
1660 (remove (assq pkg package-alist)
1661 package-alist))))
1662 (if all
1663 (cl-loop for p in alist
1664 if (assq pkg (package-desc-reqs (cadr p)))
1665 collect (cadr p))
1666 (cl-loop for p in alist thereis
1667 (and (assq pkg (package-desc-reqs (cadr p)))
1668 (cadr p)))))))
1670 (defun package--sort-deps-in-alist (package only)
1671 "Return a list of dependencies for PACKAGE sorted by dependency.
1672 PACKAGE is included as the first element of the returned list.
1673 ONLY is an alist associating package names to package objects.
1674 Only these packages will be in the return value an their cdrs are
1675 destructively set to nil in ONLY."
1676 (let ((out))
1677 (dolist (dep (package-desc-reqs package))
1678 (when-let ((cell (assq (car dep) only))
1679 (dep-package (cdr-safe cell)))
1680 (setcdr cell nil)
1681 (setq out (append (package--sort-deps-in-alist dep-package only)
1682 out))))
1683 (cons package out)))
1685 (defun package--sort-by-dependence (package-list)
1686 "Return PACKAGE-LIST sorted by dependence.
1687 That is, any element of the returned list is guaranteed to not
1688 directly depend on any elements that come before it.
1690 PACKAGE-LIST is a list of package-desc objects.
1691 Indirect dependencies are guaranteed to be returned in order only
1692 if all the in-between dependencies are also in PACKAGE-LIST."
1693 (let ((alist (mapcar (lambda (p) (cons (package-desc-name p) p)) package-list))
1694 out-list)
1695 (dolist (cell alist out-list)
1696 ;; `package--sort-deps-in-alist' destructively changes alist, so
1697 ;; some cells might already be empty. We check this here.
1698 (when-let ((pkg-desc (cdr cell)))
1699 (setcdr cell nil)
1700 (setq out-list
1701 (append (package--sort-deps-in-alist pkg-desc alist)
1702 out-list))))))
1705 ;;; Installation Functions
1706 ;; As opposed to the previous section (which listed some underlying
1707 ;; functions necessary for installation), this one contains the actual
1708 ;; functions that install packages. The package itself can be
1709 ;; installed in a variety of ways (archives, buffer, file), but
1710 ;; requirements (dependencies) are always satisfied by looking in
1711 ;; `package-archive-contents'.
1712 (defun package-archive-base (desc)
1713 "Return the archive containing the package NAME."
1714 (cdr (assoc (package-desc-archive desc) package-archives)))
1716 (defun package-install-from-archive (pkg-desc)
1717 "Download and install a tar package."
1718 ;; This won't happen, unless the archive is doing something wrong.
1719 (when (eq (package-desc-kind pkg-desc) 'dir)
1720 (error "Can't install directory package from archive"))
1721 (let* ((location (package-archive-base pkg-desc))
1722 (file (concat (package-desc-full-name pkg-desc)
1723 (package-desc-suffix pkg-desc))))
1724 (package--with-work-buffer location file
1725 (if (or (not package-check-signature)
1726 (member (package-desc-archive pkg-desc)
1727 package-unsigned-archives))
1728 ;; If we don't care about the signature, unpack and we're
1729 ;; done.
1730 (let ((save-silently t))
1731 (package-unpack pkg-desc))
1732 ;; If we care, check it and *then* write the file.
1733 (let ((content (buffer-string)))
1734 (package--check-signature
1735 location file content nil
1736 ;; This function will be called after signature checking.
1737 (lambda (&optional good-sigs)
1738 (unless (or good-sigs (eq package-check-signature 'allow-unsigned))
1739 ;; Even if the sig fails, this download is done, so
1740 ;; remove it from the in-progress list.
1741 (error "Unsigned package: `%s'"
1742 (package-desc-name pkg-desc)))
1743 ;; Signature checked, unpack now.
1744 (with-temp-buffer (insert content)
1745 (let ((save-silently t))
1746 (package-unpack pkg-desc)))
1747 ;; Here the package has been installed successfully, mark it as
1748 ;; signed if appropriate.
1749 (when good-sigs
1750 ;; Write out good signatures into NAME-VERSION.signed file.
1751 (write-region (mapconcat #'epg-signature-to-string good-sigs "\n")
1753 (expand-file-name
1754 (concat (package-desc-full-name pkg-desc) ".signed")
1755 package-user-dir)
1756 nil 'silent)
1757 ;; Update the old pkg-desc which will be shown on the description buffer.
1758 (setf (package-desc-signed pkg-desc) t)
1759 ;; Update the new (activated) pkg-desc as well.
1760 (when-let ((pkg-descs (cdr (assq (package-desc-name pkg-desc) package-alist))))
1761 (setf (package-desc-signed (car pkg-descs)) t))))))))))
1763 (defun package-installed-p (package &optional min-version)
1764 "Return true if PACKAGE, of MIN-VERSION or newer, is installed.
1765 If PACKAGE is a symbol, it is the package name and MIN-VERSION
1766 should be a version list.
1768 If PACKAGE is a package-desc object, MIN-VERSION is ignored."
1769 (unless package--initialized (error "package.el is not yet initialized!"))
1770 (if (package-desc-p package)
1771 (let ((dir (package-desc-dir package)))
1772 (and (stringp dir)
1773 (file-exists-p dir)))
1775 (let ((pkg-descs (cdr (assq package package-alist))))
1776 (and pkg-descs
1777 (version-list-<= min-version
1778 (package-desc-version (car pkg-descs)))))
1779 ;; Also check built-in packages.
1780 (package-built-in-p package min-version))))
1782 (defun package-download-transaction (packages)
1783 "Download and install all the packages in PACKAGES.
1784 PACKAGES should be a list of package-desc.
1785 This function assumes that all package requirements in
1786 PACKAGES are satisfied, i.e. that PACKAGES is computed
1787 using `package-compute-transaction'."
1788 (mapc #'package-install-from-archive packages))
1790 (defun package--ensure-init-file ()
1791 "Ensure that the user's init file has `package-initialize'.
1792 `package-initialize' doesn't have to be called, as long as it is
1793 present somewhere in the file, even as a comment. If it is not,
1794 add a call to it along with some explanatory comments."
1795 ;; Don't mess with the init-file from "emacs -Q".
1796 (when (and (stringp user-init-file)
1797 (not package--init-file-ensured)
1798 (file-readable-p user-init-file)
1799 (file-writable-p user-init-file))
1800 (let* ((buffer (find-buffer-visiting user-init-file))
1801 (contains-init
1802 (if buffer
1803 (with-current-buffer buffer
1804 (save-excursion
1805 (save-restriction
1806 (widen)
1807 (goto-char (point-min))
1808 (search-forward "(package-initialize)" nil 'noerror))))
1809 ;; Don't visit the file if we don't have to.
1810 (with-temp-buffer
1811 (insert-file-contents user-init-file)
1812 (goto-char (point-min))
1813 (search-forward "(package-initialize)" nil 'noerror)))))
1814 (unless contains-init
1815 (with-current-buffer (or buffer
1816 (let ((delay-mode-hooks t))
1817 (find-file-noselect user-init-file)))
1818 (save-excursion
1819 (save-restriction
1820 (widen)
1821 (goto-char (point-min))
1822 (while (and (looking-at-p "[[:blank:]]*\\(;\\|$\\)")
1823 (not (eobp)))
1824 (forward-line 1))
1825 (insert
1826 "\n"
1827 ";; Added by Package.el. This must come before configurations of\n"
1828 ";; installed packages. Don't delete this line. If you don't want it,\n"
1829 ";; just comment it out by adding a semicolon to the start of the line.\n"
1830 ";; You may delete these explanatory comments.\n"
1831 "(package-initialize)\n")
1832 (unless (looking-at-p "$")
1833 (insert "\n"))
1834 (let ((file-precious-flag t))
1835 (save-buffer))
1836 (unless buffer
1837 (kill-buffer (current-buffer)))))))))
1838 (setq package--init-file-ensured t))
1840 ;;;###autoload
1841 (defun package-install (pkg &optional dont-select)
1842 "Install the package PKG.
1843 PKG can be a package-desc or the package name of one the available packages
1844 in an archive in `package-archives'. Interactively, prompt for its name.
1846 If called interactively or if DONT-SELECT nil, add PKG to
1847 `package-selected-packages'.
1849 If PKG is a package-desc and it is already installed, don't try
1850 to install it but still mark it as selected."
1851 (interactive
1852 (progn
1853 ;; Initialize the package system to get the list of package
1854 ;; symbols for completion.
1855 (unless package--initialized
1856 (package-initialize t))
1857 (unless package-archive-contents
1858 (package-refresh-contents))
1859 (list (intern (completing-read
1860 "Install package: "
1861 (delq nil
1862 (mapcar (lambda (elt)
1863 (unless (package-installed-p (car elt))
1864 (symbol-name (car elt))))
1865 package-archive-contents))
1866 nil t))
1867 nil)))
1868 (let ((name (if (package-desc-p pkg)
1869 (package-desc-name pkg)
1870 pkg)))
1871 (unless (or dont-select (package--user-selected-p name))
1872 (package--save-selected-packages
1873 (cons name package-selected-packages))))
1874 (if-let ((transaction
1875 (if (package-desc-p pkg)
1876 (unless (package-installed-p pkg)
1877 (package-compute-transaction (list pkg)
1878 (package-desc-reqs pkg)))
1879 (package-compute-transaction () (list (list pkg))))))
1880 (package-download-transaction transaction)
1881 (message "`%s' is already installed" (package-desc-full-name pkg))))
1883 (defun package-strip-rcs-id (str)
1884 "Strip RCS version ID from the version string STR.
1885 If the result looks like a dotted numeric version, return it.
1886 Otherwise return nil."
1887 (when str
1888 (when (string-match "\\`[ \t]*[$]Revision:[ \t]+" str)
1889 (setq str (substring str (match-end 0))))
1890 (condition-case nil
1891 (if (version-to-list str)
1892 str)
1893 (error nil))))
1895 (declare-function lm-homepage "lisp-mnt" (&optional file))
1897 ;;;###autoload
1898 (defun package-install-from-buffer ()
1899 "Install a package from the current buffer.
1900 The current buffer is assumed to be a single .el or .tar file or
1901 a directory. These must follow the packaging guidelines (see
1902 info node `(elisp)Packaging').
1904 Specially, if current buffer is a directory, the -pkg.el
1905 description file is not mandatory, in which case the information
1906 is derived from the main .el file in the directory.
1908 Downloads and installs required packages as needed."
1909 (interactive)
1910 (let* ((pkg-desc
1911 (cond
1912 ((derived-mode-p 'dired-mode)
1913 ;; This is the only way a package-desc object with a `dir'
1914 ;; desc-kind can be created. Such packages can't be
1915 ;; uploaded or installed from archives, they can only be
1916 ;; installed from local buffers or directories.
1917 (package-dir-info))
1918 ((derived-mode-p 'tar-mode)
1919 (package-tar-file-info))
1921 (package-buffer-info))))
1922 (name (package-desc-name pkg-desc)))
1923 ;; Download and install the dependencies.
1924 (let* ((requires (package-desc-reqs pkg-desc))
1925 (transaction (package-compute-transaction nil requires)))
1926 (package-download-transaction transaction))
1927 ;; Install the package itself.
1928 (package-unpack pkg-desc)
1929 (unless (package--user-selected-p name)
1930 (package--save-selected-packages
1931 (cons name package-selected-packages)))
1932 pkg-desc))
1934 ;;;###autoload
1935 (defun package-install-file (file)
1936 "Install a package from a file.
1937 The file can either be a tar file or an Emacs Lisp file."
1938 (interactive "fPackage file name: ")
1939 (with-temp-buffer
1940 (if (file-directory-p file)
1941 (progn
1942 (setq default-directory file)
1943 (dired-mode))
1944 (insert-file-contents-literally file)
1945 (when (string-match "\\.tar\\'" file) (tar-mode)))
1946 (package-install-from-buffer)))
1948 ;;;###autoload
1949 (defun package-install-selected-packages ()
1950 "Ensure packages in `package-selected-packages' are installed.
1951 If some packages are not installed propose to install them."
1952 (interactive)
1953 ;; We don't need to populate `package-selected-packages' before
1954 ;; using here, because the outcome is the same either way (nothing
1955 ;; gets installed).
1956 (if (not package-selected-packages)
1957 (message "`package-selected-packages' is empty, nothing to install")
1958 (cl-loop for p in package-selected-packages
1959 unless (package-installed-p p)
1960 collect p into lst
1961 finally
1962 (if lst
1963 (when (y-or-n-p
1964 (format "%s packages will be installed:\n%s, proceed?"
1965 (length lst)
1966 (mapconcat #'symbol-name lst ", ")))
1967 (mapc #'package-install lst))
1968 (message "All your packages are already installed")))))
1971 ;;; Package Deletion
1972 (defun package--newest-p (pkg)
1973 "Return t if PKG is the newest package with its name."
1974 (equal (cadr (assq (package-desc-name pkg) package-alist))
1975 pkg))
1977 (defun package-delete (pkg-desc &optional force nosave)
1978 "Delete package PKG-DESC.
1980 Argument PKG-DESC is a full description of package as vector.
1981 When package is used elsewhere as dependency of another package,
1982 refuse deleting it and return an error.
1983 If FORCE is non-nil package will be deleted even if it is used
1984 elsewhere.
1985 If NOSAVE is non-nil, the package is not removed from
1986 `package-selected-packages'."
1987 (let ((dir (package-desc-dir pkg-desc))
1988 (name (package-desc-name pkg-desc))
1989 pkg-used-elsewhere-by)
1990 ;; If the user is trying to delete this package, they definitely
1991 ;; don't want it marked as selected, so we remove it from
1992 ;; `package-selected-packages' even if it can't be deleted.
1993 (when (and (null nosave)
1994 (package--user-selected-p name)
1995 ;; Don't deselect if this is an older version of an
1996 ;; upgraded package.
1997 (package--newest-p pkg-desc))
1998 (package--save-selected-packages (remove name package-selected-packages)))
1999 (cond ((not (string-prefix-p (file-name-as-directory
2000 (expand-file-name package-user-dir))
2001 (expand-file-name dir)))
2002 ;; Don't delete "system" packages.
2003 (error "Package `%s' is a system package, not deleting"
2004 (package-desc-full-name pkg-desc)))
2005 ((and (null force)
2006 (setq pkg-used-elsewhere-by
2007 (package--used-elsewhere-p pkg-desc)))
2008 ;; Don't delete packages used as dependency elsewhere.
2009 (error "Package `%s' is used by `%s' as dependency, not deleting"
2010 (package-desc-full-name pkg-desc)
2011 (package-desc-name pkg-used-elsewhere-by)))
2013 (delete-directory dir t t)
2014 ;; Remove NAME-VERSION.signed file.
2015 (let ((signed-file (concat dir ".signed")))
2016 (if (file-exists-p signed-file)
2017 (delete-file signed-file)))
2018 ;; Update package-alist.
2019 (let ((pkgs (assq name package-alist)))
2020 (delete pkg-desc pkgs)
2021 (unless (cdr pkgs)
2022 (setq package-alist (delq pkgs package-alist))))
2023 (message "Package `%s' deleted." (package-desc-full-name pkg-desc))))))
2025 ;;;###autoload
2026 (defun package-reinstall (pkg)
2027 "Reinstall package PKG.
2028 PKG should be either a symbol, the package name, or a package-desc
2029 object."
2030 (interactive (list (intern (completing-read
2031 "Reinstall package: "
2032 (mapcar #'symbol-name
2033 (mapcar #'car package-alist))))))
2034 (package-delete
2035 (if (package-desc-p pkg) pkg (cadr (assq pkg package-alist)))
2036 'force 'nosave)
2037 (package-install pkg 'dont-select))
2039 ;;;###autoload
2040 (defun package-autoremove ()
2041 "Remove packages that are no more needed.
2043 Packages that are no more needed by other packages in
2044 `package-selected-packages' and their dependencies
2045 will be deleted."
2046 (interactive)
2047 ;; If `package-selected-packages' is nil, it would make no sense to
2048 ;; try to populate it here, because then `package-autoremove' will
2049 ;; do absolutely nothing.
2050 (when (or package-selected-packages
2051 (yes-or-no-p
2052 "`package-selected-packages' is empty! Really remove ALL packages? "))
2053 (let ((removable (package--removable-packages)))
2054 (if removable
2055 (when (y-or-n-p
2056 (format "%s packages will be deleted:\n%s, proceed? "
2057 (length removable)
2058 (mapconcat #'symbol-name removable ", ")))
2059 (mapc (lambda (p)
2060 (package-delete (cadr (assq p package-alist)) t))
2061 removable))
2062 (message "Nothing to autoremove")))))
2065 ;;;; Package description buffer.
2067 ;;;###autoload
2068 (defun describe-package (package)
2069 "Display the full documentation of PACKAGE (a symbol)."
2070 (interactive
2071 (let* ((guess (function-called-at-point)))
2072 (require 'finder-inf nil t)
2073 ;; Load the package list if necessary (but don't activate them).
2074 (unless package--initialized
2075 (package-initialize t))
2076 (let ((packages (append (mapcar 'car package-alist)
2077 (mapcar 'car package-archive-contents)
2078 (mapcar 'car package--builtins))))
2079 (unless (memq guess packages)
2080 (setq guess nil))
2081 (setq packages (mapcar 'symbol-name packages))
2082 (let ((val
2083 (completing-read (if guess
2084 (format "Describe package (default %s): "
2085 guess)
2086 "Describe package: ")
2087 packages nil t nil nil guess)))
2088 (list (intern val))))))
2089 (if (not (or (package-desc-p package) (and package (symbolp package))))
2090 (message "No package specified")
2091 (help-setup-xref (list #'describe-package package)
2092 (called-interactively-p 'interactive))
2093 (with-help-window (help-buffer)
2094 (with-current-buffer standard-output
2095 (describe-package-1 package)))))
2097 (declare-function lm-commentary "lisp-mnt" (&optional file))
2099 (defun describe-package-1 (pkg)
2100 (require 'lisp-mnt)
2101 (let* ((desc (or
2102 (if (package-desc-p pkg) pkg)
2103 (cadr (assq pkg package-alist))
2104 (let ((built-in (assq pkg package--builtins)))
2105 (if built-in
2106 (package--from-builtin built-in)
2107 (cadr (assq pkg package-archive-contents))))))
2108 (name (if desc (package-desc-name desc) pkg))
2109 (pkg-dir (if desc (package-desc-dir desc)))
2110 (reqs (if desc (package-desc-reqs desc)))
2111 (required-by (if desc (package--used-elsewhere-p desc nil 'all)))
2112 (version (if desc (package-desc-version desc)))
2113 (archive (if desc (package-desc-archive desc)))
2114 (extras (and desc (package-desc-extras desc)))
2115 (homepage (cdr (assoc :url extras)))
2116 (keywords (if desc (package-desc--keywords desc)))
2117 (built-in (eq pkg-dir 'builtin))
2118 (installable (and archive (not built-in)))
2119 (status (if desc (package-desc-status desc) "orphan"))
2120 (incompatible-reason (package--incompatible-p desc))
2121 (signed (if desc (package-desc-signed desc))))
2122 (when incompatible-reason
2123 (setq status "incompatible"))
2124 (prin1 name)
2125 (princ " is ")
2126 (princ (if (memq (aref status 0) '(?a ?e ?i ?o ?u)) "an " "a "))
2127 (princ status)
2128 (princ " package.\n\n")
2130 (insert " " (propertize "Status" 'font-lock-face 'bold) ": ")
2131 (cond (built-in
2132 (insert (propertize (capitalize status)
2133 'font-lock-face 'font-lock-builtin-face)
2134 "."))
2135 (pkg-dir
2136 (insert (propertize (if (member status '("unsigned" "dependency"))
2137 "Installed"
2138 (capitalize status)) ;FIXME: Why comment-face?
2139 'font-lock-face 'font-lock-comment-face))
2140 (insert " in `")
2141 ;; Todo: Add button for uninstalling.
2142 (help-insert-xref-button (abbreviate-file-name
2143 (file-name-as-directory pkg-dir))
2144 'help-package-def pkg-dir)
2145 (if (and (package-built-in-p name)
2146 (not (package-built-in-p name version)))
2147 (insert "',\n shadowing a "
2148 (propertize "built-in package"
2149 'font-lock-face 'font-lock-builtin-face))
2150 (insert "'"))
2151 (if signed
2152 (insert ".")
2153 (insert " (unsigned)."))
2154 (when (and (package-desc-p desc)
2155 (not required-by)
2156 (package-installed-p desc))
2157 (insert " ")
2158 (package-make-button "Delete"
2159 'action #'package-delete-button-action
2160 'package-desc desc)))
2161 (incompatible-reason
2162 (insert (propertize "Incompatible" 'face font-lock-warning-face)
2163 " because it depends on ")
2164 (if (stringp incompatible-reason)
2165 (insert "Emacs " incompatible-reason ".")
2166 (insert "uninstallable packages.")))
2167 (installable
2168 (insert (capitalize status))
2169 (insert " from " (format "%s" archive))
2170 (insert " -- ")
2171 (package-make-button
2172 "Install"
2173 'action 'package-install-button-action
2174 'package-desc desc))
2175 (t (insert (capitalize status) ".")))
2176 (insert "\n")
2177 (insert " " (propertize "Archive" 'font-lock-face 'bold)
2178 ": " (or archive "n/a") "\n")
2179 (and version
2180 (insert " "
2181 (propertize "Version" 'font-lock-face 'bold) ": "
2182 (package-version-join version) "\n"))
2184 (setq reqs (if desc (package-desc-reqs desc)))
2185 (when reqs
2186 (insert " " (propertize "Requires" 'font-lock-face 'bold) ": ")
2187 (let ((first t))
2188 (dolist (req reqs)
2189 (let* ((name (car req))
2190 (vers (cadr req))
2191 (text (format "%s-%s" (symbol-name name)
2192 (package-version-join vers)))
2193 (reason (if (and (listp incompatible-reason)
2194 (assq name incompatible-reason))
2195 " (not available)" "")))
2196 (cond (first (setq first nil))
2197 ((>= (+ 2 (current-column) (length text) (length reason))
2198 (window-width))
2199 (insert ",\n "))
2200 (t (insert ", ")))
2201 (help-insert-xref-button text 'help-package name)
2202 (insert reason)))
2203 (insert "\n")))
2204 (when required-by
2205 (insert (propertize "Required by" 'font-lock-face 'bold) ": ")
2206 (let ((first t))
2207 (dolist (pkg required-by)
2208 (let ((text (package-desc-full-name pkg)))
2209 (cond (first (setq first nil))
2210 ((>= (+ 2 (current-column) (length text))
2211 (window-width))
2212 (insert ",\n "))
2213 (t (insert ", ")))
2214 (help-insert-xref-button text 'help-package
2215 (package-desc-name pkg))))
2216 (insert "\n")))
2217 (insert " " (propertize "Summary" 'font-lock-face 'bold)
2218 ": " (if desc (package-desc-summary desc)) "\n")
2219 (when homepage
2220 (insert " " (propertize "Homepage" 'font-lock-face 'bold) ": ")
2221 (help-insert-xref-button homepage 'help-url homepage)
2222 (insert "\n"))
2223 (when keywords
2224 (insert " " (propertize "Keywords" 'font-lock-face 'bold) ": ")
2225 (dolist (k keywords)
2226 (package-make-button
2228 'package-keyword k
2229 'action 'package-keyword-button-action)
2230 (insert " "))
2231 (insert "\n"))
2232 (let* ((all-pkgs (append (cdr (assq name package-alist))
2233 (cdr (assq name package-archive-contents))
2234 (let ((bi (assq name package--builtins)))
2235 (if bi (list (package--from-builtin bi))))))
2236 (other-pkgs (delete desc all-pkgs)))
2237 (when other-pkgs
2238 (insert " " (propertize "Other versions" 'font-lock-face 'bold) ": "
2239 (mapconcat
2240 (lambda (opkg)
2241 (let* ((ov (package-desc-version opkg))
2242 (dir (package-desc-dir opkg))
2243 (from (or (package-desc-archive opkg)
2244 (if (stringp dir) "installed" dir))))
2245 (if (not ov) (format "%s" from)
2246 (format "%s (%s)"
2247 (make-text-button (package-version-join ov) nil
2248 'face 'link
2249 'follow-link t
2250 'action
2251 (lambda (_button)
2252 (describe-package opkg)))
2253 from))))
2254 other-pkgs ", ")
2255 ".\n")))
2257 (insert "\n")
2259 (if built-in
2260 ;; For built-in packages, insert the commentary.
2261 (let ((fn (locate-file (format "%s.el" name) load-path
2262 load-file-rep-suffixes))
2263 (opoint (point)))
2264 (insert (or (lm-commentary fn) ""))
2265 (save-excursion
2266 (goto-char opoint)
2267 (when (re-search-forward "^;;; Commentary:\n" nil t)
2268 (replace-match ""))
2269 (while (re-search-forward "^\\(;+ ?\\)" nil t)
2270 (replace-match ""))))
2271 (let ((readme (expand-file-name (format "%s-readme.txt" name)
2272 package-user-dir))
2273 readme-string)
2274 ;; For elpa packages, try downloading the commentary. If that
2275 ;; fails, try an existing readme file in `package-user-dir'.
2276 (cond ((condition-case nil
2277 (save-excursion
2278 (package--with-work-buffer
2279 (package-archive-base desc)
2280 (format "%s-readme.txt" name)
2281 (save-excursion
2282 (goto-char (point-max))
2283 (unless (bolp)
2284 (insert ?\n)))
2285 (write-region nil nil
2286 (expand-file-name readme package-user-dir)
2287 nil 'silent)
2288 (setq readme-string (buffer-string))
2290 (error nil))
2291 (insert readme-string))
2292 ((file-readable-p readme)
2293 (insert-file-contents readme)
2294 (goto-char (point-max))))))))
2296 (defun package-install-button-action (button)
2297 (let ((pkg-desc (button-get button 'package-desc)))
2298 (when (y-or-n-p (format "Install package `%s'? "
2299 (package-desc-full-name pkg-desc)))
2300 (package-install pkg-desc nil)
2301 (revert-buffer nil t)
2302 (goto-char (point-min)))))
2304 (defun package-delete-button-action (button)
2305 (let ((pkg-desc (button-get button 'package-desc)))
2306 (when (y-or-n-p (format "Delete package `%s'? "
2307 (package-desc-full-name pkg-desc)))
2308 (package-delete pkg-desc)
2309 (revert-buffer nil t)
2310 (goto-char (point-min)))))
2312 (defun package-keyword-button-action (button)
2313 (let ((pkg-keyword (button-get button 'package-keyword)))
2314 (package-show-package-list t (list pkg-keyword))))
2316 (defun package-make-button (text &rest props)
2317 (let ((button-text (if (display-graphic-p) text (concat "[" text "]")))
2318 (button-face (if (display-graphic-p)
2319 '(:box (:line-width 2 :color "dark grey")
2320 :background "light grey"
2321 :foreground "black")
2322 'link)))
2323 (apply 'insert-text-button button-text 'face button-face 'follow-link t
2324 props)))
2327 ;;;; Package menu mode.
2329 (defvar package-menu-mode-map
2330 (let ((map (make-sparse-keymap))
2331 (menu-map (make-sparse-keymap "Package")))
2332 (set-keymap-parent map tabulated-list-mode-map)
2333 (define-key map "\C-m" 'package-menu-describe-package)
2334 (define-key map "u" 'package-menu-mark-unmark)
2335 (define-key map "\177" 'package-menu-backup-unmark)
2336 (define-key map "d" 'package-menu-mark-delete)
2337 (define-key map "i" 'package-menu-mark-install)
2338 (define-key map "U" 'package-menu-mark-upgrades)
2339 (define-key map "r" 'package-menu-refresh)
2340 (define-key map "f" 'package-menu-filter)
2341 (define-key map "~" 'package-menu-mark-obsolete-for-deletion)
2342 (define-key map "x" 'package-menu-execute)
2343 (define-key map "h" 'package-menu-quick-help)
2344 (define-key map "?" 'package-menu-describe-package)
2345 (define-key map "(" #'package-menu-hide-obsolete)
2346 (define-key map [menu-bar package-menu] (cons "Package" menu-map))
2347 (define-key menu-map [mq]
2348 '(menu-item "Quit" quit-window
2349 :help "Quit package selection"))
2350 (define-key menu-map [s1] '("--"))
2351 (define-key menu-map [mn]
2352 '(menu-item "Next" next-line
2353 :help "Next Line"))
2354 (define-key menu-map [mp]
2355 '(menu-item "Previous" previous-line
2356 :help "Previous Line"))
2357 (define-key menu-map [s2] '("--"))
2358 (define-key menu-map [mu]
2359 '(menu-item "Unmark" package-menu-mark-unmark
2360 :help "Clear any marks on a package and move to the next line"))
2361 (define-key menu-map [munm]
2362 '(menu-item "Unmark Backwards" package-menu-backup-unmark
2363 :help "Back up one line and clear any marks on that package"))
2364 (define-key menu-map [md]
2365 '(menu-item "Mark for Deletion" package-menu-mark-delete
2366 :help "Mark a package for deletion and move to the next line"))
2367 (define-key menu-map [mi]
2368 '(menu-item "Mark for Install" package-menu-mark-install
2369 :help "Mark a package for installation and move to the next line"))
2370 (define-key menu-map [mupgrades]
2371 '(menu-item "Mark Upgradable Packages" package-menu-mark-upgrades
2372 :help "Mark packages that have a newer version for upgrading"))
2373 (define-key menu-map [s3] '("--"))
2374 (define-key menu-map [mf]
2375 '(menu-item "Filter Package List..." package-menu-filter
2376 :help "Filter package selection (q to go back)"))
2377 (define-key menu-map [mg]
2378 '(menu-item "Update Package List" revert-buffer
2379 :help "Update the list of packages"))
2380 (define-key menu-map [mr]
2381 '(menu-item "Refresh Package List" package-menu-refresh
2382 :help "Download the ELPA archive"))
2383 (define-key menu-map [s4] '("--"))
2384 (define-key menu-map [mt]
2385 '(menu-item "Mark Obsolete Packages" package-menu-mark-obsolete-for-deletion
2386 :help "Mark all obsolete packages for deletion"))
2387 (define-key menu-map [mx]
2388 '(menu-item "Execute Actions" package-menu-execute
2389 :help "Perform all the marked actions"))
2390 (define-key menu-map [s5] '("--"))
2391 (define-key menu-map [mh]
2392 '(menu-item "Help" package-menu-quick-help
2393 :help "Show short key binding help for package-menu-mode"))
2394 (define-key menu-map [mc]
2395 '(menu-item "Describe Package" package-menu-describe-package
2396 :help "Display information about this package"))
2397 map)
2398 "Local keymap for `package-menu-mode' buffers.")
2400 (defvar package-menu--new-package-list nil
2401 "List of newly-available packages since `list-packages' was last called.")
2403 (defvar package-menu--transaction-status nil
2404 "Mode-line status of ongoing package transaction.")
2406 (define-derived-mode package-menu-mode tabulated-list-mode "Package Menu"
2407 "Major mode for browsing a list of packages.
2408 Letters do not insert themselves; instead, they are commands.
2409 \\<package-menu-mode-map>
2410 \\{package-menu-mode-map}"
2411 (setq mode-line-process '((package--downloads-in-progress ":Loading")
2412 (package-menu--transaction-status
2413 package-menu--transaction-status)))
2414 (setq tabulated-list-format
2415 `[("Package" 18 package-menu--name-predicate)
2416 ("Version" 13 nil)
2417 ("Status" 10 package-menu--status-predicate)
2418 ,@(if (cdr package-archives)
2419 '(("Archive" 10 package-menu--archive-predicate)))
2420 ("Description" 0 nil)])
2421 (setq tabulated-list-padding 2)
2422 (setq tabulated-list-sort-key (cons "Status" nil))
2423 (add-hook 'tabulated-list-revert-hook 'package-menu--refresh nil t)
2424 (tabulated-list-init-header))
2426 (defmacro package--push (pkg-desc status listname)
2427 "Convenience macro for `package-menu--generate'.
2428 If the alist stored in the symbol LISTNAME lacks an entry for a
2429 package PKG-DESC, add one. The alist is keyed with PKG-DESC."
2430 `(unless (assoc ,pkg-desc ,listname)
2431 ;; FIXME: Should we move status into pkg-desc?
2432 (push (cons ,pkg-desc ,status) ,listname)))
2434 (defvar package-list-unversioned nil
2435 "If non-nil include packages that don't have a version in `list-package'.")
2437 (defvar package-list-unsigned nil
2438 "If non-nil, mention in the list which packages were installed w/o signature.")
2440 (defvar package--emacs-version-list (version-to-list emacs-version)
2441 "`emacs-version', as a list.")
2443 (defun package--incompatible-p (pkg &optional shallow)
2444 "Return non-nil if PKG has no chance of being installable.
2445 PKG is a package-desc object.
2447 If SHALLOW is non-nil, this only checks if PKG depends on a
2448 higher `emacs-version' than the one being used. Otherwise, also
2449 checks the viability of dependencies, according to
2450 `package--compatibility-table'.
2452 If PKG requires an incompatible Emacs version, the return value
2453 is this version (as a string).
2454 If PKG requires incompatible packages, the return value is a list
2455 of these dependencies, similar to the list returned by
2456 `package-desc-reqs'."
2457 (let* ((reqs (package-desc-reqs pkg))
2458 (version (cadr (assq 'emacs reqs))))
2459 (if (and version (version-list-< package--emacs-version-list version))
2460 (package-version-join version)
2461 (unless shallow
2462 (let (out)
2463 (dolist (dep (package-desc-reqs pkg) out)
2464 (let ((dep-name (car dep)))
2465 (unless (eq 'emacs dep-name)
2466 (let ((cv (gethash dep-name package--compatibility-table)))
2467 (when (version-list-< (or cv '(0)) (or (cadr dep) '(0)))
2468 (push dep out)))))))))))
2470 (defun package-desc-status (pkg-desc)
2471 (let* ((name (package-desc-name pkg-desc))
2472 (dir (package-desc-dir pkg-desc))
2473 (lle (assq name package-load-list))
2474 (held (cadr lle))
2475 (version (package-desc-version pkg-desc))
2476 (signed (or (not package-list-unsigned)
2477 (package-desc-signed pkg-desc))))
2478 (cond
2479 ((eq dir 'builtin) "built-in")
2480 ((and lle (null held)) "disabled")
2481 ((stringp held)
2482 (let ((hv (if (stringp held) (version-to-list held))))
2483 (cond
2484 ((version-list-= version hv) "held")
2485 ((version-list-< version hv) "obsolete")
2486 (t "disabled"))))
2487 (dir ;One of the installed packages.
2488 (cond
2489 ((not (file-exists-p dir)) "deleted")
2490 ;; Not inside `package-user-dir'.
2491 ((not (file-in-directory-p dir package-user-dir)) "external")
2492 ((eq pkg-desc (cadr (assq name package-alist)))
2493 (if (not signed) "unsigned"
2494 (if (package--user-selected-p name)
2495 "installed" "dependency")))
2496 (t "obsolete")))
2497 ((package--incompatible-p pkg-desc) "incompat")
2499 (let* ((ins (cadr (assq name package-alist)))
2500 (ins-v (if ins (package-desc-version ins))))
2501 (cond
2502 ;; Installed obsolete packages are handled in the `dir'
2503 ;; clause above. Here we handle available obsolete, which
2504 ;; are displayed depending on `package-menu--hide-obsolete'.
2505 ((and ins (version-list-<= version ins-v)) "avail-obso")
2507 (if (memq name package-menu--new-package-list)
2508 "new" "available"))))))))
2510 (defvar package-menu--hide-obsolete t
2511 "Whether available obsolete packages should be hidden.
2512 Can be toggled with \\<package-menu-mode-map> \\[package-menu-hide-obsolete].
2513 Installed obsolete packages are always displayed.")
2515 (defun package-menu-hide-obsolete ()
2516 "Toggle visibility of obsolete available packages."
2517 (interactive)
2518 (unless (derived-mode-p 'package-menu-mode)
2519 (user-error "The current buffer is not a Package Menu"))
2520 (setq package-menu--hide-obsolete
2521 (not package-menu--hide-obsolete))
2522 (message "%s available-obsolete packages" (if package-menu--hide-obsolete
2523 "Hiding" "Displaying"))
2524 (revert-buffer nil 'no-confirm))
2526 (defun package--remove-hidden (pkg-list)
2527 "Filter PKG-LIST according to `package-archive-priorities'.
2528 PKG-LIST must be a list of package-desc objects, all with the
2529 same name, sorted by decreasing `package-desc-priority-version'.
2530 Return a list of packages tied for the highest priority according
2531 to their archives."
2532 (when pkg-list
2533 ;; Variable toggled with `package-menu-hide-obsolete'.
2534 (if (not package-menu--hide-obsolete)
2535 pkg-list
2536 (let ((installed (cadr (assq (package-desc-name (car pkg-list))
2537 package-alist))))
2538 (when installed
2539 (setq pkg-list
2540 (let ((ins-version (package-desc-version installed)))
2541 (cl-remove-if (lambda (p) (version-list-< (package-desc-version p)
2542 ins-version))
2543 pkg-list))))
2544 (let ((filtered-by-priority
2545 (cond
2546 ((not package-menu-hide-low-priority)
2547 pkg-list)
2548 ((eq package-menu-hide-low-priority 'archive)
2549 (let* ((max-priority most-negative-fixnum)
2550 (out))
2551 (while pkg-list
2552 (let ((p (pop pkg-list)))
2553 (if (>= (package-desc-priority p) max-priority)
2554 (push p out)
2555 (setq pkg-list nil))))
2556 (nreverse out)))
2557 (pkg-list
2558 (list (car pkg-list))))))
2559 (if (not installed)
2560 filtered-by-priority
2561 (let ((ins-version (package-desc-version installed)))
2562 (cl-remove-if (lambda (p) (version-list-= (package-desc-version p)
2563 ins-version))
2564 filtered-by-priority))))))))
2566 (defun package-menu--refresh (&optional packages keywords)
2567 "Re-populate the `tabulated-list-entries'.
2568 PACKAGES should be nil or t, which means to display all known packages.
2569 KEYWORDS should be nil or a list of keywords."
2570 ;; Construct list of (PKG-DESC . STATUS).
2571 (unless packages (setq packages t))
2572 (let (info-list)
2573 ;; Installed packages:
2574 (dolist (elt package-alist)
2575 (let ((name (car elt)))
2576 (when (or (eq packages t) (memq name packages))
2577 (dolist (pkg (cdr elt))
2578 (when (package--has-keyword-p pkg keywords)
2579 (push pkg info-list))))))
2581 ;; Built-in packages:
2582 (dolist (elt package--builtins)
2583 (let ((pkg (package--from-builtin elt))
2584 (name (car elt)))
2585 (when (not (eq name 'emacs)) ; Hide the `emacs' package.
2586 (when (and (package--has-keyword-p pkg keywords)
2587 (or package-list-unversioned
2588 (package--bi-desc-version (cdr elt)))
2589 (or (eq packages t) (memq name packages)))
2590 (push pkg info-list)))))
2592 ;; Available and disabled packages:
2593 (dolist (elt package-archive-contents)
2594 (let ((name (car elt)))
2595 (when (or (eq packages t) (memq name packages))
2596 ;; Hide available-obsolete or low-priority packages.
2597 (dolist (pkg (package--remove-hidden (cdr elt)))
2598 (when (package--has-keyword-p pkg keywords)
2599 (push pkg info-list))))))
2601 ;; Print the result.
2602 (setq tabulated-list-entries
2603 (mapcar #'package-menu--print-info-simple info-list))))
2605 (defun package-all-keywords ()
2606 "Collect all package keywords"
2607 (let ((key-list))
2608 (package--mapc (lambda (desc)
2609 (setq key-list (append (package-desc--keywords desc)
2610 key-list))))
2611 key-list))
2613 (defun package--mapc (function &optional packages)
2614 "Call FUNCTION for all known PACKAGES.
2615 PACKAGES can be nil or t, which means to display all known
2616 packages, or a list of packages.
2618 Built-in packages are converted with `package--from-builtin'."
2619 (unless packages (setq packages t))
2620 (let (name)
2621 ;; Installed packages:
2622 (dolist (elt package-alist)
2623 (setq name (car elt))
2624 (when (or (eq packages t) (memq name packages))
2625 (mapc function (cdr elt))))
2627 ;; Built-in packages:
2628 (dolist (elt package--builtins)
2629 (setq name (car elt))
2630 (when (and (not (eq name 'emacs)) ; Hide the `emacs' package.
2631 (or package-list-unversioned
2632 (package--bi-desc-version (cdr elt)))
2633 (or (eq packages t) (memq name packages)))
2634 (funcall function (package--from-builtin elt))))
2636 ;; Available and disabled packages:
2637 (dolist (elt package-archive-contents)
2638 (setq name (car elt))
2639 (when (or (eq packages t) (memq name packages))
2640 (dolist (pkg (cdr elt))
2641 ;; Hide obsolete packages.
2642 (unless (package-installed-p (package-desc-name pkg)
2643 (package-desc-version pkg))
2644 (funcall function pkg)))))))
2646 (defun package--has-keyword-p (desc &optional keywords)
2647 "Test if package DESC has any of the given KEYWORDS.
2648 When none are given, the package matches."
2649 (if keywords
2650 (let ((desc-keywords (and desc (package-desc--keywords desc)))
2651 found)
2652 (while (and (not found) keywords)
2653 (let ((k (pop keywords)))
2654 (setq found
2655 (or (string= k (concat "arc:" (package-desc-archive desc)))
2656 (string= k (concat "status:" (package-desc-status desc)))
2657 (member k desc-keywords)))))
2658 found)
2661 (defun package-menu--generate (remember-pos packages &optional keywords)
2662 "Populate the Package Menu.
2663 If REMEMBER-POS is non-nil, keep point on the same entry.
2664 PACKAGES should be t, which means to display all known packages,
2665 or a list of package names (symbols) to display.
2667 With KEYWORDS given, only packages with those keywords are
2668 shown."
2669 (package-menu--refresh packages keywords)
2670 (setf (car (aref tabulated-list-format 0))
2671 (if keywords
2672 (let ((filters (mapconcat 'identity keywords ",")))
2673 (concat "Package[" filters "]"))
2674 "Package"))
2675 (if keywords
2676 (define-key package-menu-mode-map "q" 'package-show-package-list)
2677 (define-key package-menu-mode-map "q" 'quit-window))
2678 (tabulated-list-init-header)
2679 (tabulated-list-print remember-pos))
2681 (defun package-menu--print-info (pkg)
2682 "Return a package entry suitable for `tabulated-list-entries'.
2683 PKG has the form (PKG-DESC . STATUS).
2684 Return (PKG-DESC [NAME VERSION STATUS DOC])."
2685 (package-menu--print-info-simple (car pkg)))
2686 (make-obsolete 'package-menu--print-info
2687 'package-menu--print-info-simple "25.1")
2689 (defun package-menu--print-info-simple (pkg)
2690 "Return a package entry suitable for `tabulated-list-entries'.
2691 PKG is a package-desc object.
2692 Return (PKG-DESC [NAME VERSION STATUS DOC])."
2693 (let* ((status (package-desc-status pkg))
2694 (face (pcase status
2695 (`"built-in" 'font-lock-builtin-face)
2696 (`"external" 'font-lock-builtin-face)
2697 (`"available" 'default)
2698 (`"avail-obso" 'font-lock-comment-face)
2699 (`"new" 'bold)
2700 (`"held" 'font-lock-constant-face)
2701 (`"disabled" 'font-lock-warning-face)
2702 (`"installed" 'font-lock-comment-face)
2703 (`"dependency" 'font-lock-comment-face)
2704 (`"unsigned" 'font-lock-warning-face)
2705 (`"incompat" 'font-lock-comment-face)
2706 (_ 'font-lock-warning-face)))) ; obsolete.
2707 (list pkg
2708 `[(,(symbol-name (package-desc-name pkg))
2709 face link
2710 follow-link t
2711 package-desc ,pkg
2712 action package-menu-describe-package)
2713 ,(propertize (package-version-join
2714 (package-desc-version pkg))
2715 'font-lock-face face)
2716 ,(propertize status 'font-lock-face face)
2717 ,@(if (cdr package-archives)
2718 (list (propertize (or (package-desc-archive pkg) "")
2719 'font-lock-face face)))
2720 ,(package-desc-summary pkg)])))
2722 (defvar package-menu--old-archive-contents nil
2723 "`package-archive-contents' before the latest refresh.")
2725 (defun package-menu-refresh ()
2726 "Download the Emacs Lisp package archive.
2727 This fetches the contents of each archive specified in
2728 `package-archives', and then refreshes the package menu."
2729 (interactive)
2730 (unless (derived-mode-p 'package-menu-mode)
2731 (user-error "The current buffer is not a Package Menu"))
2732 (setq package-menu--old-archive-contents package-archive-contents)
2733 (setq package-menu--new-package-list nil)
2734 (package-refresh-contents package-menu-async))
2736 (defun package-menu-describe-package (&optional button)
2737 "Describe the current package.
2738 If optional arg BUTTON is non-nil, describe its associated package."
2739 (interactive)
2740 (let ((pkg-desc (if button (button-get button 'package-desc)
2741 (tabulated-list-get-id))))
2742 (if pkg-desc
2743 (describe-package pkg-desc)
2744 (user-error "No package here"))))
2746 ;; fixme numeric argument
2747 (defun package-menu-mark-delete (&optional _num)
2748 "Mark a package for deletion and move to the next line."
2749 (interactive "p")
2750 (if (member (package-menu-get-status)
2751 '("installed" "dependency" "obsolete" "unsigned"))
2752 (tabulated-list-put-tag "D" t)
2753 (forward-line)))
2755 (defun package-menu-mark-install (&optional _num)
2756 "Mark a package for installation and move to the next line."
2757 (interactive "p")
2758 (if (member (package-menu-get-status) '("available" "avail-obso" "new" "dependency"))
2759 (tabulated-list-put-tag "I" t)
2760 (forward-line)))
2762 (defun package-menu-mark-unmark (&optional _num)
2763 "Clear any marks on a package and move to the next line."
2764 (interactive "p")
2765 (tabulated-list-put-tag " " t))
2767 (defun package-menu-backup-unmark ()
2768 "Back up one line and clear any marks on that package."
2769 (interactive)
2770 (forward-line -1)
2771 (tabulated-list-put-tag " "))
2773 (defun package-menu-mark-obsolete-for-deletion ()
2774 "Mark all obsolete packages for deletion."
2775 (interactive)
2776 (save-excursion
2777 (goto-char (point-min))
2778 (while (not (eobp))
2779 (if (equal (package-menu-get-status) "obsolete")
2780 (tabulated-list-put-tag "D" t)
2781 (forward-line 1)))))
2783 (defvar package--quick-help-keys
2784 '(("install," "delete," "unmark," ("execute" . 1))
2785 ("next," "previous")
2786 ("refresh-contents," "g-redisplay," "filter," "(-toggle-obsolete" "help")))
2788 (defun package--prettify-quick-help-key (desc)
2789 "Prettify DESC to be displayed as a help menu."
2790 (if (listp desc)
2791 (if (listp (cdr desc))
2792 (mapconcat #'package--prettify-quick-help-key desc " ")
2793 (let ((place (cdr desc))
2794 (out (car desc)))
2795 ;; (setq out (propertize out 'face 'paradox-comment-face))
2796 (add-text-properties place (1+ place)
2797 '(face (bold font-lock-function-name-face))
2798 out)
2799 out))
2800 (package--prettify-quick-help-key (cons desc 0))))
2802 (defun package-menu-quick-help ()
2803 "Show short key binding help for `package-menu-mode'.
2804 The full list of keys can be viewed with \\[describe-mode]."
2805 (interactive)
2806 (message (mapconcat #'package--prettify-quick-help-key
2807 package--quick-help-keys "\n")))
2809 (define-obsolete-function-alias
2810 'package-menu-view-commentary 'package-menu-describe-package "24.1")
2812 (defun package-menu-get-status ()
2813 (let* ((id (tabulated-list-get-id))
2814 (entry (and id (assq id tabulated-list-entries))))
2815 (if entry
2816 (aref (cadr entry) 2)
2817 "")))
2819 (defun package-archive-priority (archive)
2820 "Return the priority of ARCHIVE.
2822 The archive priorities are specified in
2823 `package-archive-priorities'. If not given there, the priority
2824 defaults to 0."
2825 (or (cdr (assoc archive package-archive-priorities))
2828 (defun package-desc-priority-version (pkg-desc)
2829 "Return the version PKG-DESC with the archive priority prepended.
2831 This allows for easy comparison of package versions from
2832 different archives if archive priorities are meant to be taken in
2833 consideration."
2834 (cons (package-desc-priority pkg-desc)
2835 (package-desc-version pkg-desc)))
2837 (defun package-menu--find-upgrades ()
2838 (let (installed available upgrades)
2839 ;; Build list of installed/available packages in this buffer.
2840 (dolist (entry tabulated-list-entries)
2841 ;; ENTRY is (PKG-DESC [NAME VERSION STATUS DOC])
2842 (let ((pkg-desc (car entry))
2843 (status (aref (cadr entry) 2)))
2844 (cond ((member status '("installed" "dependency" "unsigned"))
2845 (push pkg-desc installed))
2846 ((member status '("available" "new"))
2847 (setq available (package--append-to-alist pkg-desc available))))))
2848 ;; Loop through list of installed packages, finding upgrades.
2849 (dolist (pkg-desc installed)
2850 (let* ((name (package-desc-name pkg-desc))
2851 (avail-pkg (cadr (assq name available))))
2852 (and avail-pkg
2853 (version-list-< (package-desc-priority-version pkg-desc)
2854 (package-desc-priority-version avail-pkg))
2855 (push (cons name avail-pkg) upgrades))))
2856 upgrades))
2858 (defun package-menu-mark-upgrades ()
2859 "Mark all upgradable packages in the Package Menu.
2860 For each installed package with a newer version available, place
2861 an (I)nstall flag on the available version and a (D)elete flag on
2862 the installed version. A subsequent \\[package-menu-execute]
2863 call will upgrade the package."
2864 (interactive)
2865 (unless (derived-mode-p 'package-menu-mode)
2866 (error "The current buffer is not a Package Menu"))
2867 (let ((upgrades (package-menu--find-upgrades)))
2868 (if (null upgrades)
2869 (message "No packages to upgrade.")
2870 (widen)
2871 (save-excursion
2872 (goto-char (point-min))
2873 (while (not (eobp))
2874 (let* ((pkg-desc (tabulated-list-get-id))
2875 (upgrade (cdr (assq (package-desc-name pkg-desc) upgrades))))
2876 (cond ((null upgrade)
2877 (forward-line 1))
2878 ((equal pkg-desc upgrade)
2879 (package-menu-mark-install))
2881 (package-menu-mark-delete))))))
2882 (message "%d package%s marked for upgrading."
2883 (length upgrades)
2884 (if (= (length upgrades) 1) "" "s")))))
2886 (defun package-menu--list-to-prompt (packages)
2887 "Return a string listing PACKAGES that's usable in a prompt.
2888 PACKAGES is a list of `package-desc' objects.
2889 Formats the returned string to be usable in a minibuffer
2890 prompt (see `package-menu--prompt-transaction-p')."
2891 (cond
2892 ;; None
2893 ((not packages) "")
2894 ;; More than 1
2895 ((cdr packages)
2896 (format "these %d packages (%s)"
2897 (length packages)
2898 (mapconcat #'package-desc-full-name packages ", ")))
2899 ;; Exactly 1
2900 (t (format "package `%s'"
2901 (package-desc-full-name (car packages))))))
2903 (defun package-menu--prompt-transaction-p (delete install upgrade)
2904 "Prompt the user about DELETE, INSTALL, and UPGRADE.
2905 DELETE, INSTALL, and UPGRADE are lists of `package-desc' objects.
2906 Either may be nil, but not all."
2907 (y-or-n-p
2908 (concat
2909 (when delete "Delete ")
2910 (package-menu--list-to-prompt delete)
2911 (when (and delete install)
2912 (if upgrade "; " "; and "))
2913 (when install "Install ")
2914 (package-menu--list-to-prompt install)
2915 (when (and upgrade (or install delete)) "; and ")
2916 (when upgrade "Upgrade ")
2917 (package-menu--list-to-prompt upgrade)
2918 "? ")))
2920 (defun package-menu--partition-transaction (install delete)
2921 "Return an alist describing an INSTALL DELETE transaction.
2922 Alist contains three entries, upgrade, delete, and install, each
2923 with a list of package names.
2925 The upgrade entry contains any `package-desc' objects in INSTALL
2926 whose name coincides with an object in DELETE. The delete and
2927 the install entries are the same as DELETE and INSTALL with such
2928 objects removed."
2929 (let* ((upg (cl-intersection install delete :key #'package-desc-name))
2930 (ins (cl-set-difference install upg :key #'package-desc-name))
2931 (del (cl-set-difference delete upg :key #'package-desc-name)))
2932 `((delete . ,del) (install . ,ins) (upgrade . ,upg))))
2934 (defun package-menu--perform-transaction (install-list delete-list)
2935 "Install packages in INSTALL-LIST and delete DELETE-LIST."
2936 (if install-list
2937 (let ((status-format (format ":Installing %%d/%d"
2938 (length install-list)))
2939 (i 0)
2940 (package-menu--transaction-status))
2941 (dolist (pkg install-list)
2942 (setq package-menu--transaction-status
2943 (format status-format (cl-incf i)))
2944 (force-mode-line-update)
2945 (redisplay 'force)
2946 ;; Don't mark as selected, `package-menu-execute' already
2947 ;; does that.
2948 (package-install pkg 'dont-select)))
2949 ;; Once there are no more packages to install, proceed to
2950 ;; deletion.
2951 (let ((package-menu--transaction-status ":Deleting"))
2952 (force-mode-line-update)
2953 (redisplay 'force)
2954 (dolist (elt (package--sort-by-dependence delete-list))
2955 (condition-case-unless-debug err
2956 (let ((inhibit-message t))
2957 (package-delete elt nil 'nosave))
2958 (error (message (cadr err))))))))
2960 (defun package--update-selected-packages (add remove)
2961 "Update the `package-selected-packages' list according to ADD and REMOVE.
2962 ADD and REMOVE must be disjoint lists of package names (or
2963 `package-desc' objects) to be added and removed to the selected
2964 packages list, respectively."
2965 (dolist (p add)
2966 (cl-pushnew (if (package-desc-p p) (package-desc-name p) p)
2967 package-selected-packages))
2968 (dolist (p remove)
2969 (setq package-selected-packages
2970 (remove (if (package-desc-p p) (package-desc-name p) p)
2971 package-selected-packages)))
2972 (when (or add remove)
2973 (package--save-selected-packages package-selected-packages)))
2975 (defun package-menu-execute (&optional noquery)
2976 "Perform marked Package Menu actions.
2977 Packages marked for installation are downloaded and installed;
2978 packages marked for deletion are removed.
2979 Optional argument NOQUERY non-nil means do not ask the user to confirm."
2980 (interactive)
2981 (unless (derived-mode-p 'package-menu-mode)
2982 (error "The current buffer is not in Package Menu mode"))
2983 (let (install-list delete-list cmd pkg-desc)
2984 (save-excursion
2985 (goto-char (point-min))
2986 (while (not (eobp))
2987 (setq cmd (char-after))
2988 (unless (eq cmd ?\s)
2989 ;; This is the key PKG-DESC.
2990 (setq pkg-desc (tabulated-list-get-id))
2991 (cond ((eq cmd ?D)
2992 (push pkg-desc delete-list))
2993 ((eq cmd ?I)
2994 (push pkg-desc install-list))))
2995 (forward-line)))
2996 (unless (or delete-list install-list)
2997 (user-error "No operations specified"))
2998 (let-alist (package-menu--partition-transaction install-list delete-list)
2999 (when (or noquery
3000 (package-menu--prompt-transaction-p .delete .install .upgrade))
3001 (let ((message-template
3002 (concat "Package menu: Operation %s ["
3003 (when .delete (format "Delet__ %s" (length .delete)))
3004 (when (and .delete .install) "; ")
3005 (when .install (format "Install__ %s" (length .install)))
3006 (when (and .upgrade (or .install .delete)) "; ")
3007 (when .upgrade (format "Upgrad__ %s" (length .upgrade)))
3008 "]")))
3009 (message (replace-regexp-in-string "__" "ing" message-template) "started")
3010 ;; Packages being upgraded are not marked as selected.
3011 (package--update-selected-packages .install .delete)
3012 (package-menu--perform-transaction install-list delete-list)
3013 (when package-selected-packages
3014 (if-let ((removable (package--removable-packages)))
3015 (message "Package menu: Operation finished. %d packages %s"
3016 (length removable)
3017 "are no longer needed, type `M-x package-autoremove' to remove them")
3018 (message (replace-regexp-in-string "__" "ed" message-template)
3019 "finished"))))
3020 ;; This calls `package-menu--generate'.
3021 (package-menu--post-refresh)))))
3023 (defun package-menu--version-predicate (A B)
3024 (let ((vA (or (aref (cadr A) 1) '(0)))
3025 (vB (or (aref (cadr B) 1) '(0))))
3026 (if (version-list-= vA vB)
3027 (package-menu--name-predicate A B)
3028 (version-list-< vA vB))))
3030 (defun package-menu--status-predicate (A B)
3031 (let ((sA (aref (cadr A) 2))
3032 (sB (aref (cadr B) 2)))
3033 (cond ((string= sA sB)
3034 (package-menu--name-predicate A B))
3035 ((string= sA "new") t)
3036 ((string= sB "new") nil)
3037 ((string-prefix-p "avail" sA)
3038 (if (string-prefix-p "avail" sB)
3039 (package-menu--name-predicate A B)
3041 ((string-prefix-p "avail" sB) nil)
3042 ((string= sA "installed") t)
3043 ((string= sB "installed") nil)
3044 ((string= sA "dependency") t)
3045 ((string= sB "dependency") nil)
3046 ((string= sA "unsigned") t)
3047 ((string= sB "unsigned") nil)
3048 ((string= sA "held") t)
3049 ((string= sB "held") nil)
3050 ((string= sA "external") t)
3051 ((string= sB "external") nil)
3052 ((string= sA "built-in") t)
3053 ((string= sB "built-in") nil)
3054 ((string= sA "obsolete") t)
3055 ((string= sB "obsolete") nil)
3056 ((string= sA "incompat") t)
3057 ((string= sB "incompat") nil)
3058 (t (string< sA sB)))))
3060 (defun package-menu--description-predicate (A B)
3061 (let ((dA (aref (cadr A) 3))
3062 (dB (aref (cadr B) 3)))
3063 (if (string= dA dB)
3064 (package-menu--name-predicate A B)
3065 (string< dA dB))))
3067 (defun package-menu--name-predicate (A B)
3068 (string< (symbol-name (package-desc-name (car A)))
3069 (symbol-name (package-desc-name (car B)))))
3071 (defun package-menu--archive-predicate (A B)
3072 (string< (or (package-desc-archive (car A)) "")
3073 (or (package-desc-archive (car B)) "")))
3075 (defun package-menu--populate-new-package-list ()
3076 "Decide which packages are new in `package-archives-contents'.
3077 Store this list in `package-menu--new-package-list'."
3078 ;; Find which packages are new.
3079 (when package-menu--old-archive-contents
3080 (dolist (elt package-archive-contents)
3081 (unless (assq (car elt) package-menu--old-archive-contents)
3082 (push (car elt) package-menu--new-package-list)))
3083 (setq package-menu--old-archive-contents nil)))
3085 (defun package-menu--find-and-notify-upgrades ()
3086 "Notify the user of upgradable packages."
3087 (when-let ((upgrades (package-menu--find-upgrades)))
3088 (message "%d package%s can be upgraded; type `%s' to mark %s for upgrading."
3089 (length upgrades)
3090 (if (= (length upgrades) 1) "" "s")
3091 (substitute-command-keys "\\[package-menu-mark-upgrades]")
3092 (if (= (length upgrades) 1) "it" "them"))))
3094 (defun package-menu--post-refresh ()
3095 "Check for new packages, revert the *Packages* buffer, and check for upgrades.
3096 This function is called after `package-refresh-contents' and
3097 after `package-menu--perform-transaction'."
3098 (package-menu--populate-new-package-list)
3099 (let ((buf (get-buffer "*Packages*")))
3100 (when (buffer-live-p buf)
3101 (with-current-buffer buf
3102 (revert-buffer nil 'noconfirm))))
3103 (package-menu--find-and-notify-upgrades))
3105 ;;;###autoload
3106 (defun list-packages (&optional no-fetch)
3107 "Display a list of packages.
3108 This first fetches the updated list of packages before
3109 displaying, unless a prefix argument NO-FETCH is specified.
3110 The list is displayed in a buffer named `*Packages*'."
3111 (interactive "P")
3112 (require 'finder-inf nil t)
3113 ;; Initialize the package system if necessary.
3114 (unless package--initialized
3115 (package-initialize t))
3116 ;; Integrate the package-menu with updating the archives.
3117 (add-hook 'package--post-download-archives-hook
3118 #'package-menu--post-refresh)
3120 ;; Generate the Package Menu.
3121 (let ((buf (get-buffer-create "*Packages*")))
3122 (with-current-buffer buf
3123 (package-menu-mode)
3125 ;; Fetch the remote list of packages.
3126 (unless no-fetch (package-menu-refresh))
3128 ;; If we're not async, this would be redundant.
3129 (when package-menu-async
3130 (package-menu--generate nil t)))
3131 ;; The package menu buffer has keybindings. If the user types
3132 ;; `M-x list-packages', that suggests it should become current.
3133 (switch-to-buffer buf)))
3135 ;;;###autoload
3136 (defalias 'package-list-packages 'list-packages)
3138 ;; Used in finder.el
3139 (defun package-show-package-list (&optional packages keywords)
3140 "Display PACKAGES in a *Packages* buffer.
3141 This is similar to `list-packages', but it does not fetch the
3142 updated list of packages, and it only displays packages with
3143 names in PACKAGES (which should be a list of symbols).
3145 When KEYWORDS are given, only packages with those KEYWORDS are
3146 shown."
3147 (interactive)
3148 (require 'finder-inf nil t)
3149 (let* ((buf (get-buffer-create "*Packages*"))
3150 (win (get-buffer-window buf)))
3151 (with-current-buffer buf
3152 (package-menu-mode)
3153 (package-menu--generate nil packages keywords))
3154 (if win
3155 (select-window win)
3156 (switch-to-buffer buf))))
3158 ;; package-menu--generate rebinds "q" on the fly, so we have to
3159 ;; hard-code the binding in the doc-string here.
3160 (defun package-menu-filter (keyword)
3161 "Filter the *Packages* buffer.
3162 Show only those items that relate to the specified KEYWORD.
3163 KEYWORD can be a string or a list of strings. If it is a list, a
3164 package will be displayed if it matches any of the keywords.
3165 Interactively, it is a list of strings separated by commas.
3167 To restore the full package list, type `q'."
3168 (interactive
3169 (list (completing-read-multiple
3170 "Keywords (comma separated): " (package-all-keywords))))
3171 (package-show-package-list t (if (stringp keyword)
3172 (list keyword)
3173 keyword)))
3175 (defun package-list-packages-no-fetch ()
3176 "Display a list of packages.
3177 Does not fetch the updated list of packages before displaying.
3178 The list is displayed in a buffer named `*Packages*'."
3179 (interactive)
3180 (list-packages t))
3182 (provide 'package)
3184 ;;; package.el ends here