* lisp/emacs-lisp/package.el: Hide lower-priority packages in menu
[emacs.git] / lisp / emacs-lisp / package.el
blob999e857346b3b7a1c3d26bca68f1ffb7a1846a2f
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 :group 'package
189 :version "24.1")
191 (defcustom package-load-list '(all)
192 "List of packages for `package-initialize' to load.
193 Each element in this list should be a list (NAME VERSION), or the
194 symbol `all'. The symbol `all' says to load the latest installed
195 versions of all packages not specified by other elements.
197 For an element (NAME VERSION), NAME is a package name (a symbol).
198 VERSION should be t, a string, or nil.
199 If VERSION is t, the most recent version is activated.
200 If VERSION is a string, only that version is ever loaded.
201 Any other version, even if newer, is silently ignored.
202 Hence, the package is \"held\" at that version.
203 If VERSION is nil, the package is not loaded (it is \"disabled\")."
204 :type '(repeat symbol)
205 :risky t
206 :group 'package
207 :version "24.1")
209 (defcustom package-archives '(("gnu" . "http://elpa.gnu.org/packages/"))
210 "An alist of archives from which to fetch.
211 The default value points to the GNU Emacs package repository.
213 Each element has the form (ID . LOCATION).
214 ID is an archive name, as a string.
215 LOCATION specifies the base location for the archive.
216 If it starts with \"http:\", it is treated as a HTTP URL;
217 otherwise it should be an absolute directory name.
218 (Other types of URL are currently not supported.)
220 Only add locations that you trust, since fetching and installing
221 a package can run arbitrary code."
222 :type '(alist :key-type (string :tag "Archive name")
223 :value-type (string :tag "URL or directory name"))
224 :risky t
225 :group 'package
226 :version "24.1")
228 (defcustom package-menu-hide-low-priority 'archive
229 "If non-nil, hide low priority packages from the packages menu.
230 A package is considered low priority if there's another version
231 of it available such that:
232 (a) the archive of the other package is higher priority than
233 this one, as per `package-archive-priorities';
235 (b) they both have the same archive priority but the other
236 package has a higher version number.
238 This variable has three possible values:
239 nil: no packages are hidden;
240 archive: only criteria (a) is used;
241 t: both criteria are used.
243 This variable has no effect if `package-menu--hide-obsolete' is
244 nil, so it can be toggled with \\<package-menu-mode-map> \\[package-menu-hide-obsolete]."
245 :type '(choice (const :tag "Don't hide anything" nil)
246 (const :tag "Hide per package-archive-priorities"
247 archive)
248 (const :tag "Hide per archive and version number" t))
249 :group 'package
250 :version "25.1")
252 (defcustom package-archive-priorities nil
253 "An alist of priorities for packages.
255 Each element has the form (ARCHIVE-ID . PRIORITY).
257 When installing packages, the package with the highest version
258 number from the archive with the highest priority is
259 selected. When higher versions are available from archives with
260 lower priorities, the user has to select those manually.
262 Archives not in this list have the priority 0.
264 See also `package-menu-hide-low-priority'."
265 :type '(alist :key-type (string :tag "Archive name")
266 :value-type (integer :tag "Priority (default is 0)"))
267 :risky t
268 :group 'package
269 :version "25.1")
271 (defcustom package-pinned-packages nil
272 "An alist of packages that are pinned to specific archives.
273 This can be useful if you have multiple package archives enabled,
274 and want to control which archive a given package gets installed from.
276 Each element of the alist has the form (PACKAGE . ARCHIVE), where:
277 PACKAGE is a symbol representing a package
278 ARCHIVE is a string representing an archive (it should be the car of
279 an element in `package-archives', e.g. \"gnu\").
281 Adding an entry to this variable means that only ARCHIVE will be
282 considered as a source for PACKAGE. If other archives provide PACKAGE,
283 they are ignored (for this package). If ARCHIVE does not contain PACKAGE,
284 the package will be unavailable."
285 :type '(alist :key-type (symbol :tag "Package")
286 :value-type (string :tag "Archive name"))
287 ;; I don't really see why this is risky...
288 ;; I suppose it could prevent you receiving updates for a package,
289 ;; via an entry (PACKAGE . NON-EXISTING). Which could be an issue
290 ;; if PACKAGE has a known vulnerability that is fixed in newer versions.
291 :risky t
292 :group 'package
293 :version "24.4")
295 (defcustom package-user-dir (locate-user-emacs-file "elpa")
296 "Directory containing the user's Emacs Lisp packages.
297 The directory name should be absolute.
298 Apart from this directory, Emacs also looks for system-wide
299 packages in `package-directory-list'."
300 :type 'directory
301 :risky t
302 :group 'package
303 :version "24.1")
305 (defcustom package-directory-list
306 ;; Defaults are subdirs named "elpa" in the site-lisp dirs.
307 (let (result)
308 (dolist (f load-path)
309 (and (stringp f)
310 (equal (file-name-nondirectory f) "site-lisp")
311 (push (expand-file-name "elpa" f) result)))
312 (nreverse result))
313 "List of additional directories containing Emacs Lisp packages.
314 Each directory name should be absolute.
316 These directories contain packages intended for system-wide; in
317 contrast, `package-user-dir' contains packages for personal use."
318 :type '(repeat directory)
319 :risky t
320 :group 'package
321 :version "24.1")
323 (defvar epg-gpg-program)
325 (defcustom package-check-signature
326 (if (progn (require 'epg-config) (executable-find epg-gpg-program))
327 'allow-unsigned)
328 "Non-nil means to check package signatures when installing.
329 The value `allow-unsigned' means to still install a package even if
330 it is unsigned.
332 This also applies to the \"archive-contents\" file that lists the
333 contents of the archive."
334 :type '(choice (const nil :tag "Never")
335 (const allow-unsigned :tag "Allow unsigned")
336 (const t :tag "Check always"))
337 :risky t
338 :group 'package
339 :version "24.4")
341 (defcustom package-unsigned-archives nil
342 "List of archives where we do not check for package signatures."
343 :type '(repeat (string :tag "Archive name"))
344 :risky t
345 :group 'package
346 :version "24.4")
348 (defcustom package-selected-packages nil
349 "Store here packages installed explicitly by user.
350 This variable is fed automatically by Emacs when installing a new package.
351 This variable is used by `package-autoremove' to decide
352 which packages are no longer needed.
353 You can use it to (re)install packages on other machines
354 by running `package-user-selected-packages-install'.
356 To check if a package is contained in this list here, use
357 `package--user-selected-p', as it may populate the variable with
358 a sane initial value."
359 :group 'package
360 :type '(repeat symbol))
363 ;;; `package-desc' object definition
364 ;; This is the struct used internally to represent packages.
365 ;; Functions that deal with packages should generally take this object
366 ;; as an argument. In some situations (e.g. commands that query the
367 ;; user) it makes sense to take the package name as a symbol instead,
368 ;; but keep in mind there could be multiple `package-desc's with the
369 ;; same name.
370 (defvar package--default-summary "No description available.")
372 (cl-defstruct (package-desc
373 ;; Rename the default constructor from `make-package-desc'.
374 (:constructor package-desc-create)
375 ;; Has the same interface as the old `define-package',
376 ;; which is still used in the "foo-pkg.el" files. Extra
377 ;; options can be supported by adding additional keys.
378 (:constructor
379 package-desc-from-define
380 (name-string version-string &optional summary requirements
381 &rest rest-plist
382 &aux
383 (name (intern name-string))
384 (version (version-to-list version-string))
385 (reqs (mapcar #'(lambda (elt)
386 (list (car elt)
387 (version-to-list (cadr elt))))
388 (if (eq 'quote (car requirements))
389 (nth 1 requirements)
390 requirements)))
391 (kind (plist-get rest-plist :kind))
392 (archive (plist-get rest-plist :archive))
393 (extras (let (alist)
394 (while rest-plist
395 (unless (memq (car rest-plist) '(:kind :archive))
396 (let ((value (cadr rest-plist)))
397 (when value
398 (push (cons (car rest-plist)
399 (if (eq (car-safe value) 'quote)
400 (cadr value)
401 value))
402 alist))))
403 (setq rest-plist (cddr rest-plist)))
404 alist)))))
405 "Structure containing information about an individual package.
406 Slots:
408 `name' Name of the package, as a symbol.
410 `version' Version of the package, as a version list.
412 `summary' Short description of the package, typically taken from
413 the first line of the file.
415 `reqs' Requirements of the package. A list of (PACKAGE
416 VERSION-LIST) naming the dependent package and the minimum
417 required version.
419 `kind' The distribution format of the package. Currently, it is
420 either `single' or `tar'.
422 `archive' The name of the archive (as a string) whence this
423 package came.
425 `dir' The directory where the package is installed (if installed),
426 `builtin' if it is built-in, or nil otherwise.
428 `extras' Optional alist of additional keyword-value pairs.
430 `signed' Flag to indicate that the package is signed by provider."
431 name
432 version
433 (summary package--default-summary)
434 reqs
435 kind
436 archive
438 extras
439 signed)
441 (defun package--from-builtin (bi-desc)
442 (package-desc-create :name (pop bi-desc)
443 :version (package--bi-desc-version bi-desc)
444 :summary (package--bi-desc-summary bi-desc)
445 :dir 'builtin))
447 ;; Pseudo fields.
448 (defun package-version-join (vlist)
449 "Return the version string corresponding to the list VLIST.
450 This is, approximately, the inverse of `version-to-list'.
451 \(Actually, it returns only one of the possible inverses, since
452 `version-to-list' is a many-to-one operation.)"
453 (if (null vlist)
455 (let ((str-list (list "." (int-to-string (car vlist)))))
456 (dolist (num (cdr vlist))
457 (cond
458 ((>= num 0)
459 (push (int-to-string num) str-list)
460 (push "." str-list))
461 ((< num -4)
462 (error "Invalid version list `%s'" vlist))
464 ;; pre, or beta, or alpha
465 (cond ((equal "." (car str-list))
466 (pop str-list))
467 ((not (string-match "[0-9]+" (car str-list)))
468 (error "Invalid version list `%s'" vlist)))
469 (push (cond ((= num -1) "pre")
470 ((= num -2) "beta")
471 ((= num -3) "alpha")
472 ((= num -4) "snapshot"))
473 str-list))))
474 (if (equal "." (car str-list))
475 (pop str-list))
476 (apply 'concat (nreverse str-list)))))
478 (defun package-desc-full-name (pkg-desc)
479 (format "%s-%s"
480 (package-desc-name pkg-desc)
481 (package-version-join (package-desc-version pkg-desc))))
483 (defun package-desc-suffix (pkg-desc)
484 (pcase (package-desc-kind pkg-desc)
485 (`single ".el")
486 (`tar ".tar")
487 (`dir "")
488 (kind (error "Unknown package kind: %s" kind))))
490 (defun package-desc--keywords (pkg-desc)
491 (let ((keywords (cdr (assoc :keywords (package-desc-extras pkg-desc)))))
492 (if (eq (car-safe keywords) 'quote)
493 (nth 1 keywords)
494 keywords)))
496 (defun package-desc-priority (p)
497 "Return the priority of the archive of package-desc object P."
498 (package-archive-priority (package-desc-archive p)))
500 ;; Package descriptor format used in finder-inf.el and package--builtins.
501 (cl-defstruct (package--bi-desc
502 (:constructor package-make-builtin (version summary))
503 (:type vector))
504 version
505 reqs
506 summary)
509 ;;; Installed packages
510 ;; The following variables store information about packages present in
511 ;; the system. The most important of these is `package-alist'. The
512 ;; command `package-initialize' is also closely related to this
513 ;; section, but it is left for a later section because it also affects
514 ;; other stuff.
515 (defvar package--builtins nil
516 "Alist of built-in packages.
517 The actual value is initialized by loading the library
518 `finder-inf'; this is not done until it is needed, e.g. by the
519 function `package-built-in-p'.
521 Each element has the form (PKG . PACKAGE-BI-DESC), where PKG is a package
522 name (a symbol) and DESC is a `package--bi-desc' structure.")
523 (put 'package--builtins 'risky-local-variable t)
525 (defvar package-alist nil
526 "Alist of all packages available for activation.
527 Each element has the form (PKG . DESCS), where PKG is a package
528 name (a symbol) and DESCS is a non-empty list of `package-desc' structure,
529 sorted by decreasing versions.
531 This variable is set automatically by `package-load-descriptor',
532 called via `package-initialize'. To change which packages are
533 loaded and/or activated, customize `package-load-list'.")
534 (put 'package-alist 'risky-local-variable t)
536 (defvar package-activated-list nil
537 ;; FIXME: This should implicitly include all builtin packages.
538 "List of the names of currently activated packages.")
539 (put 'package-activated-list 'risky-local-variable t)
541 ;;;; Populating `package-alist'.
542 ;; The following functions are called on each installed package by
543 ;; `package-load-all-descriptors', which ultimately populates the
544 ;; `package-alist' variable.
545 (defun package-process-define-package (exp)
546 (when (eq (car-safe exp) 'define-package)
547 (let* ((new-pkg-desc (apply #'package-desc-from-define (cdr exp)))
548 (name (package-desc-name new-pkg-desc))
549 (version (package-desc-version new-pkg-desc))
550 (old-pkgs (assq name package-alist)))
551 (if (null old-pkgs)
552 ;; If there's no old package, just add this to `package-alist'.
553 (push (list name new-pkg-desc) package-alist)
554 ;; If there is, insert the new package at the right place in the list.
555 (while
556 (if (and (cdr old-pkgs)
557 (version-list-< version
558 (package-desc-version (cadr old-pkgs))))
559 (setq old-pkgs (cdr old-pkgs))
560 (push new-pkg-desc (cdr old-pkgs))
561 nil)))
562 new-pkg-desc)))
564 (defun package-load-descriptor (pkg-dir)
565 "Load the description file in directory PKG-DIR."
566 (let ((pkg-file (expand-file-name (package--description-file pkg-dir)
567 pkg-dir))
568 (signed-file (concat pkg-dir ".signed")))
569 (when (file-exists-p pkg-file)
570 (with-temp-buffer
571 (insert-file-contents pkg-file)
572 (goto-char (point-min))
573 (let ((pkg-desc (or (package-process-define-package
574 (read (current-buffer)))
575 (error "Can't find define-package in %s" pkg-file))))
576 (setf (package-desc-dir pkg-desc) pkg-dir)
577 (if (file-exists-p signed-file)
578 (setf (package-desc-signed pkg-desc) t))
579 pkg-desc)))))
581 (defun package-load-all-descriptors ()
582 "Load descriptors for installed Emacs Lisp packages.
583 This looks for package subdirectories in `package-user-dir' and
584 `package-directory-list'. The variable `package-load-list'
585 controls which package subdirectories may be loaded.
587 In each valid package subdirectory, this function loads the
588 description file containing a call to `define-package', which
589 updates `package-alist'."
590 (dolist (dir (cons package-user-dir package-directory-list))
591 (when (file-directory-p dir)
592 (dolist (subdir (directory-files dir))
593 (let ((pkg-dir (expand-file-name subdir dir)))
594 (when (file-directory-p pkg-dir)
595 (package-load-descriptor pkg-dir)))))))
597 (defun define-package (_name-string _version-string
598 &optional _docstring _requirements
599 &rest _extra-properties)
600 "Define a new package.
601 NAME-STRING is the name of the package, as a string.
602 VERSION-STRING is the version of the package, as a string.
603 DOCSTRING is a short description of the package, a string.
604 REQUIREMENTS is a list of dependencies on other packages.
605 Each requirement is of the form (OTHER-PACKAGE OTHER-VERSION),
606 where OTHER-VERSION is a string.
608 EXTRA-PROPERTIES is currently unused."
609 ;; FIXME: Placeholder! Should we keep it?
610 (error "Don't call me!"))
613 ;;; Package activation
614 ;; Section for functions used by `package-activate', which see.
615 (defun package-disabled-p (pkg-name version)
616 "Return whether PKG-NAME at VERSION can be activated.
617 The decision is made according to `package-load-list'.
618 Return nil if the package can be activated.
619 Return t if the package is completely disabled.
620 Return the max version (as a string) if the package is held at a lower version."
621 (let ((force (assq pkg-name package-load-list)))
622 (cond ((null force) (not (memq 'all package-load-list)))
623 ((null (setq force (cadr force))) t) ; disabled
624 ((eq force t) nil)
625 ((stringp force) ; held
626 (unless (version-list-= version (version-to-list force))
627 force))
628 (t (error "Invalid element in `package-load-list'")))))
630 (defun package-built-in-p (package &optional min-version)
631 "Return true if PACKAGE is built-in to Emacs.
632 Optional arg MIN-VERSION, if non-nil, should be a version list
633 specifying the minimum acceptable version."
634 (if (package-desc-p package) ;; was built-in and then was converted
635 (eq 'builtin (package-desc-dir package))
636 (let ((bi (assq package package--builtin-versions)))
637 (cond
638 (bi (version-list-<= min-version (cdr bi)))
639 ((remove 0 min-version) nil)
641 (require 'finder-inf nil t) ; For `package--builtins'.
642 (assq package package--builtins))))))
644 (defvar Info-directory-list)
645 (declare-function info-initialize "info" ())
647 (defun package-activate-1 (pkg-desc &optional reload)
648 "Activate package given by PKG-DESC, even if it was already active.
649 If RELOAD is non-nil, also `load' any files inside the package which
650 correspond to previously loaded files (those returned by
651 `package--list-loaded-files')."
652 (let* ((name (package-desc-name pkg-desc))
653 (pkg-dir (package-desc-dir pkg-desc))
654 (pkg-dir-dir (file-name-as-directory pkg-dir)))
655 (unless pkg-dir
656 (error "Internal error: unable to find directory for `%s'"
657 (package-desc-full-name pkg-desc)))
658 ;; Add to load path, add autoloads, and activate the package.
659 (let* ((old-lp load-path)
660 (autoloads-file (expand-file-name
661 (format "%s-autoloads" name) pkg-dir))
662 (loaded-files-list (and reload (package--list-loaded-files pkg-dir))))
663 (with-demoted-errors "Error in package-activate-1: %s"
664 (load autoloads-file nil t))
665 (when (and (eq old-lp load-path)
666 (not (or (member pkg-dir load-path)
667 (member pkg-dir-dir load-path))))
668 ;; Old packages don't add themselves to the `load-path', so we have to
669 ;; do it ourselves.
670 (push pkg-dir load-path))
671 ;; Call `load' on all files in `pkg-dir' already present in
672 ;; `load-history'. This is done so that macros in these files are updated
673 ;; to their new definitions. If another package is being installed which
674 ;; depends on this new definition, not doing this update would cause
675 ;; compilation errors and break the installation.
676 (with-demoted-errors "Error in package-activate-1: %s"
677 (mapc (lambda (feature) (load feature nil t))
678 ;; Skip autoloads file since we already evaluated it above.
679 (remove (file-truename autoloads-file) loaded-files-list))))
680 ;; Add info node.
681 (when (file-exists-p (expand-file-name "dir" pkg-dir))
682 ;; FIXME: not the friendliest, but simple.
683 (require 'info)
684 (info-initialize)
685 (push pkg-dir Info-directory-list))
686 (push name package-activated-list)
687 ;; Don't return nil.
690 (declare-function find-library-name "find-func" (library))
692 (defun package--list-loaded-files (dir)
693 "Recursively list all files in DIR which correspond to loaded features.
694 Returns the `file-name-sans-extension' of each file, relative to
695 DIR, sorted by most recently loaded last."
696 (let* ((history (delq nil
697 (mapcar (lambda (x)
698 (let ((f (car x)))
699 (and f (file-name-sans-extension f))))
700 load-history)))
701 (dir (file-truename dir))
702 ;; List all files that have already been loaded.
703 (list-of-conflicts
704 (delq
706 (mapcar
707 (lambda (x) (let* ((file (file-relative-name x dir))
708 ;; Previously loaded file, if any.
709 (previous
710 (ignore-errors
711 (file-name-sans-extension
712 (file-truename (find-library-name file)))))
713 (pos (when previous (member previous history))))
714 ;; Return (RELATIVE-FILENAME . HISTORY-POSITION)
715 (when pos
716 (cons (file-name-sans-extension file) (length pos)))))
717 (directory-files-recursively dir "\\`[^\\.].*\\.el\\'")))))
718 ;; Turn the list of (FILENAME . POS) back into a list of features. Files in
719 ;; subdirectories are returned relative to DIR (so not actually features).
720 (let ((default-directory (file-name-as-directory dir)))
721 (mapcar (lambda (x) (file-truename (car x)))
722 (sort list-of-conflicts
723 ;; Sort the files by ascending HISTORY-POSITION.
724 (lambda (x y) (< (cdr x) (cdr y))))))))
726 ;;;; `package-activate'
727 ;; This function activates a newer version of a package if an older
728 ;; one was already activated. It also loads a features of this
729 ;; package which were already loaded.
730 (defun package-activate (package &optional force)
731 "Activate package PACKAGE.
732 If FORCE is true, (re-)activate it if it's already activated.
733 Newer versions are always activated, regardless of FORCE."
734 (let ((pkg-descs (cdr (assq package package-alist))))
735 ;; Check if PACKAGE is available in `package-alist'.
736 (while
737 (when pkg-descs
738 (let ((available-version (package-desc-version (car pkg-descs))))
739 (or (package-disabled-p package available-version)
740 ;; Prefer a builtin package.
741 (package-built-in-p package available-version))))
742 (setq pkg-descs (cdr pkg-descs)))
743 (cond
744 ;; If no such package is found, maybe it's built-in.
745 ((null pkg-descs)
746 (package-built-in-p package))
747 ;; If the package is already activated, just return t.
748 ((and (memq package package-activated-list) (not force))
750 ;; Otherwise, proceed with activation.
752 (let* ((pkg-vec (car pkg-descs))
753 (fail (catch 'dep-failure
754 ;; Activate its dependencies recursively.
755 (dolist (req (package-desc-reqs pkg-vec))
756 (unless (package-activate (car req))
757 (throw 'dep-failure req))))))
758 (if fail
759 (warn "Unable to activate package `%s'.
760 Required package `%s-%s' is unavailable"
761 package (car fail) (package-version-join (cadr fail)))
762 ;; If all goes well, activate the package itself.
763 (package-activate-1 pkg-vec force)))))))
766 ;;; Installation -- Local operations
767 ;; This section contains a variety of features regarding installing a
768 ;; package to/from disk. This includes autoload generation,
769 ;; unpacking, compiling, as well as defining a package from the
770 ;; current buffer.
772 ;;;; Unpacking
773 (defvar tar-parse-info)
774 (declare-function tar-untar-buffer "tar-mode" ())
775 (declare-function tar-header-name "tar-mode" (tar-header) t)
776 (declare-function tar-header-link-type "tar-mode" (tar-header) t)
778 (defun package-untar-buffer (dir)
779 "Untar the current buffer.
780 This uses `tar-untar-buffer' from Tar mode. All files should
781 untar into a directory named DIR; otherwise, signal an error."
782 (require 'tar-mode)
783 (tar-mode)
784 ;; Make sure everything extracts into DIR.
785 (let ((regexp (concat "\\`" (regexp-quote (expand-file-name dir)) "/"))
786 (case-fold-search (memq system-type '(windows-nt ms-dos cygwin))))
787 (dolist (tar-data tar-parse-info)
788 (let ((name (expand-file-name (tar-header-name tar-data))))
789 (or (string-match regexp name)
790 ;; Tarballs created by some utilities don't list
791 ;; directories with a trailing slash (Bug#13136).
792 (and (string-equal dir name)
793 (eq (tar-header-link-type tar-data) 5))
794 (error "Package does not untar cleanly into directory %s/" dir)))))
795 (tar-untar-buffer))
797 (defun package--alist-to-plist-args (alist)
798 (mapcar 'macroexp-quote
799 (apply #'nconc
800 (mapcar (lambda (pair) (list (car pair) (cdr pair))) alist))))
801 (defun package-unpack (pkg-desc)
802 "Install the contents of the current buffer as a package."
803 (let* ((name (package-desc-name pkg-desc))
804 (dirname (package-desc-full-name pkg-desc))
805 (pkg-dir (expand-file-name dirname package-user-dir)))
806 (pcase (package-desc-kind pkg-desc)
807 (`dir
808 (make-directory pkg-dir t)
809 (let ((file-list
810 (directory-files
811 default-directory 'full "\\`[^.].*\\.el\\'" 'nosort)))
812 (dolist (source-file file-list)
813 (let ((target-el-file
814 (expand-file-name (file-name-nondirectory source-file) pkg-dir)))
815 (copy-file source-file target-el-file t)))
816 ;; Now that the files have been installed, this package is
817 ;; indistinguishable from a `tar' or a `single'. Let's make
818 ;; things simple by ensuring we're one of them.
819 (setf (package-desc-kind pkg-desc)
820 (if (> (length file-list) 1) 'tar 'single))))
821 (`tar
822 (make-directory package-user-dir t)
823 ;; FIXME: should we delete PKG-DIR if it exists?
824 (let* ((default-directory (file-name-as-directory package-user-dir)))
825 (package-untar-buffer dirname)))
826 (`single
827 (let ((el-file (expand-file-name (format "%s.el" name) pkg-dir)))
828 (make-directory pkg-dir t)
829 (package--write-file-no-coding el-file)))
830 (kind (error "Unknown package kind: %S" kind)))
831 (package--make-autoloads-and-stuff pkg-desc pkg-dir)
832 ;; Update package-alist.
833 (let ((new-desc (package-load-descriptor pkg-dir)))
834 ;; FIXME: Check that `new-desc' matches `desc'!
835 ;; FIXME: Compilation should be done as a separate, optional, step.
836 ;; E.g. for multi-package installs, we should first install all packages
837 ;; and then compile them.
838 (package--compile new-desc))
839 ;; Try to activate it.
840 (package-activate name 'force)
841 pkg-dir))
843 (defun package-generate-description-file (pkg-desc pkg-file)
844 "Create the foo-pkg.el file for single-file packages."
845 (let* ((name (package-desc-name pkg-desc)))
846 (let ((print-level nil)
847 (print-quoted t)
848 (print-length nil))
849 (write-region
850 (concat
851 ";;; -*- no-byte-compile: t -*-\n"
852 (prin1-to-string
853 (nconc
854 (list 'define-package
855 (symbol-name name)
856 (package-version-join (package-desc-version pkg-desc))
857 (package-desc-summary pkg-desc)
858 (let ((requires (package-desc-reqs pkg-desc)))
859 (list 'quote
860 ;; Turn version lists into string form.
861 (mapcar
862 (lambda (elt)
863 (list (car elt)
864 (package-version-join (cadr elt))))
865 requires))))
866 (package--alist-to-plist-args
867 (package-desc-extras pkg-desc))))
868 "\n")
869 nil pkg-file nil 'silent))))
871 ;;;; Autoload
872 ;; From Emacs 22, but changed so it adds to load-path.
873 (defun package-autoload-ensure-default-file (file)
874 "Make sure that the autoload file FILE exists and if not create it."
875 (unless (file-exists-p file)
876 (write-region
877 (concat ";;; " (file-name-nondirectory file)
878 " --- automatically extracted autoloads\n"
879 ";;\n"
880 ";;; Code:\n"
881 "(add-to-list 'load-path (or (file-name-directory #$) (car load-path)))\n"
882 "\f\n;; Local Variables:\n"
883 ";; version-control: never\n"
884 ";; no-byte-compile: t\n"
885 ";; no-update-autoloads: t\n"
886 ";; End:\n"
887 ";;; " (file-name-nondirectory file)
888 " ends here\n")
889 nil file nil 'silent))
890 file)
892 (defvar generated-autoload-file)
893 (defvar version-control)
895 (defun package-generate-autoloads (name pkg-dir)
896 (let* ((auto-name (format "%s-autoloads.el" name))
897 ;;(ignore-name (concat name "-pkg.el"))
898 (generated-autoload-file (expand-file-name auto-name pkg-dir))
899 ;; Silence `autoload-generate-file-autoloads'.
900 (noninteractive package--silence)
901 (backup-inhibited t)
902 (version-control 'never))
903 (package-autoload-ensure-default-file generated-autoload-file)
904 (update-directory-autoloads pkg-dir)
905 (let ((buf (find-buffer-visiting generated-autoload-file)))
906 (when buf (kill-buffer buf)))
907 auto-name))
909 (defun package--make-autoloads-and-stuff (pkg-desc pkg-dir)
910 "Generate autoloads, description file, etc.. for PKG-DESC installed at PKG-DIR."
911 (package-generate-autoloads (package-desc-name pkg-desc) pkg-dir)
912 (let ((desc-file (expand-file-name (package--description-file pkg-dir)
913 pkg-dir)))
914 (unless (file-exists-p desc-file)
915 (package-generate-description-file pkg-desc desc-file)))
916 ;; FIXME: Create foo.info and dir file from foo.texi?
919 ;;;; Compilation
920 (defun package--compile (pkg-desc)
921 "Byte-compile installed package PKG-DESC."
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" ())
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))
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"))
1141 (delete-region (point-min) (point))
1142 ,@body)
1143 (kill-buffer (current-buffer)))
1145 'silent))))
1147 (defun package--check-signature-content (content string &optional sig-file)
1148 "Check signature CONTENT against STRING.
1149 SIG-FILE is the name of the signature file, used when signaling
1150 errors."
1151 (let* ((context (epg-make-context 'OpenPGP))
1152 (homedir (expand-file-name "gnupg" package-user-dir)))
1153 (setf (epg-context-home-directory context) homedir)
1154 (condition-case error
1155 (epg-verify-string context content string)
1156 (error (package--display-verify-error context sig-file)
1157 (signal (car error) (cdr error))))
1158 (let (good-signatures had-fatal-error)
1159 ;; The .sig file may contain multiple signatures. Success if one
1160 ;; of the signatures is good.
1161 (dolist (sig (epg-context-result-for context 'verify))
1162 (if (eq (epg-signature-status sig) 'good)
1163 (push sig good-signatures)
1164 ;; If package-check-signature is allow-unsigned, don't
1165 ;; signal error when we can't verify signature because of
1166 ;; missing public key. Other errors are still treated as
1167 ;; fatal (bug#17625).
1168 (unless (and (eq package-check-signature 'allow-unsigned)
1169 (eq (epg-signature-status sig) 'no-pubkey))
1170 (setq had-fatal-error t))))
1171 (when (and (null good-signatures) had-fatal-error)
1172 (package--display-verify-error context sig-file)
1173 (error "Failed to verify signature %s" sig-file))
1174 good-signatures)))
1176 (defun package--check-signature (location file &optional string async callback)
1177 "Check signature of the current buffer.
1178 Download the signature file from LOCATION by appending \".sig\"
1179 to FILE.
1180 GnuPG keyring is located under \"gnupg\" in `package-user-dir'.
1181 STRING is the string to verify, it defaults to `buffer-string'.
1182 If ASYNC is non-nil, the download of the signature file is
1183 done asynchronously.
1185 If the signature is verified and CALLBACK was provided, CALLBACK
1186 is `funcall'ed with the list of good signatures as argument (the
1187 list can be empty). If the signatures file is not found,
1188 CALLBACK is called with no arguments."
1189 (let ((sig-file (concat file ".sig"))
1190 (string (or string (buffer-string))))
1191 (condition-case nil
1192 (package--with-work-buffer-async
1193 location sig-file (when async (or callback t))
1194 (let ((sig (package--check-signature-content
1195 (buffer-string) string sig-file)))
1196 (when callback (funcall callback sig))
1197 sig))
1198 (file-error (funcall callback)))))
1201 ;;; Packages on Archives
1202 ;; The following variables store information about packages available
1203 ;; from archives. The most important of these is
1204 ;; `package-archive-contents' which is initially populated by the
1205 ;; function `package-read-all-archive-contents' from a cache on disk.
1206 ;; The `package-initialize' command is also closely related to this
1207 ;; section, but it has its own section.
1208 (defconst package-archive-version 1
1209 "Version number of the package archive understood by this file.
1210 Lower version numbers than this will probably be understood as well.")
1212 ;; We don't prime the cache since it tends to get out of date.
1213 (defvar package-archive-contents nil
1214 "Cache of the contents of the Emacs Lisp Package Archive.
1215 This is an alist mapping package names (symbols) to
1216 non-empty lists of `package-desc' structures.")
1217 (put 'package-archive-contents 'risky-local-variable t)
1219 (defvar package--compatibility-table nil
1220 "Hash table connecting package names to their compatibility.
1221 Each key is a symbol, the name of a package.
1223 The value is either nil, representing an incompatible package, or
1224 a version list, representing the highest compatible version of
1225 that package which is available.
1227 A package is considered incompatible if it requires an Emacs
1228 version higher than the one being used. To check for package
1229 \(in)compatibility, don't read this table directly, use
1230 `package--incompatible-p' which also checks dependencies.")
1232 (defun package--build-compatibility-table ()
1233 "Build `package--compatibility-table' with `package--mapc'."
1234 ;; Initialize the list of built-ins.
1235 (require 'finder-inf nil t)
1236 ;; Build compat table.
1237 (setq package--compatibility-table (make-hash-table :test 'eq))
1238 (package--mapc #'package--add-to-compatibility-table))
1240 (defun package--add-to-compatibility-table (pkg)
1241 "If PKG is compatible (without dependencies), add to the compatibility table.
1242 PKG is a package-desc object.
1243 Only adds if its version is higher than what's already stored in
1244 the table."
1245 (unless (package--incompatible-p pkg 'shallow)
1246 (let* ((name (package-desc-name pkg))
1247 (version (or (package-desc-version pkg) '(0)))
1248 (table-version (gethash name package--compatibility-table)))
1249 (when (or (not table-version)
1250 (version-list-< table-version version))
1251 (puthash name version package--compatibility-table)))))
1253 ;; Package descriptor objects used inside the "archive-contents" file.
1254 ;; Changing this defstruct implies changing the format of the
1255 ;; "archive-contents" files.
1256 (cl-defstruct (package--ac-desc
1257 (:constructor package-make-ac-desc (version reqs summary kind extras))
1258 (:copier nil)
1259 (:type vector))
1260 version reqs summary kind extras)
1262 (defun package--append-to-alist (pkg-desc alist)
1263 "Append an entry for PKG-DESC to the start of ALIST and return it.
1264 This entry takes the form (`package-desc-name' PKG-DESC).
1266 If ALIST already has an entry with this name, destructively add
1267 PKG-DESC to the cdr of this entry instead, sorted by version
1268 number."
1269 (let* ((name (package-desc-name pkg-desc))
1270 (priority-version (package-desc-priority-version pkg-desc))
1271 (existing-packages (assq name alist)))
1272 (if (not existing-packages)
1273 (cons (list name pkg-desc)
1274 alist)
1275 (while (if (and (cdr existing-packages)
1276 (version-list-< priority-version
1277 (package-desc-priority-version
1278 (cadr existing-packages))))
1279 (setq existing-packages (cdr existing-packages))
1280 (push pkg-desc (cdr existing-packages))
1281 nil))
1282 alist)))
1284 (defun package--add-to-archive-contents (package archive)
1285 "Add the PACKAGE from the given ARCHIVE if necessary.
1286 PACKAGE should have the form (NAME . PACKAGE--AC-DESC).
1287 Also, add the originating archive to the `package-desc' structure."
1288 (let* ((name (car package))
1289 (version (package--ac-desc-version (cdr package)))
1290 (pkg-desc
1291 (package-desc-create
1292 :name name
1293 :version version
1294 :reqs (package--ac-desc-reqs (cdr package))
1295 :summary (package--ac-desc-summary (cdr package))
1296 :kind (package--ac-desc-kind (cdr package))
1297 :archive archive
1298 :extras (and (> (length (cdr package)) 4)
1299 ;; Older archive-contents files have only 4
1300 ;; elements here.
1301 (package--ac-desc-extras (cdr package)))))
1302 (pinned-to-archive (assoc name package-pinned-packages)))
1303 ;; Skip entirely if pinned to another archive.
1304 (when (not (and pinned-to-archive
1305 (not (equal (cdr pinned-to-archive) archive))))
1306 (setq package-archive-contents
1307 (package--append-to-alist pkg-desc package-archive-contents)))))
1309 (defun package--read-archive-file (file)
1310 "Re-read archive file FILE, if it exists.
1311 Will return the data from the file, or nil if the file does not exist.
1312 Will throw an error if the archive version is too new."
1313 (let ((filename (expand-file-name file package-user-dir)))
1314 (when (file-exists-p filename)
1315 (with-temp-buffer
1316 (let ((coding-system-for-read 'utf-8))
1317 (insert-file-contents filename))
1318 (let ((contents (read (current-buffer))))
1319 (if (> (car contents) package-archive-version)
1320 (error "Package archive version %d is higher than %d"
1321 (car contents) package-archive-version))
1322 (cdr contents))))))
1324 (defun package-read-archive-contents (archive)
1325 "Re-read archive contents for ARCHIVE.
1326 If successful, set the variable `package-archive-contents'.
1327 If the archive version is too new, signal an error."
1328 ;; Version 1 of 'archive-contents' is identical to our internal
1329 ;; representation.
1330 (let* ((contents-file (format "archives/%s/archive-contents" archive))
1331 (contents (package--read-archive-file contents-file)))
1332 (when contents
1333 (dolist (package contents)
1334 (package--add-to-archive-contents package archive)))))
1336 (defun package-read-all-archive-contents ()
1337 "Re-read `archive-contents', if it exists.
1338 If successful, set `package-archive-contents'."
1339 (setq package-archive-contents nil)
1340 (dolist (archive package-archives)
1341 (package-read-archive-contents (car archive))))
1343 ;;;; Package Initialize
1344 ;; A bit of a milestone. This brings together some of the above
1345 ;; sections and populates all relevant lists of packages from contents
1346 ;; available on disk.
1347 (defvar package--initialized nil)
1349 ;;;###autoload
1350 (defun package-initialize (&optional no-activate)
1351 "Load Emacs Lisp packages, and activate them.
1352 The variable `package-load-list' controls which packages to load.
1353 If optional arg NO-ACTIVATE is non-nil, don't activate packages.
1354 If `user-init-file' does not mention `(package-initialize)', add
1355 it to the file."
1356 (interactive)
1357 (setq package-alist nil)
1358 (package--ensure-init-file)
1359 (package-load-all-descriptors)
1360 (package-read-all-archive-contents)
1361 (unless no-activate
1362 (dolist (elt package-alist)
1363 (package-activate (car elt))))
1364 (setq package--initialized t)
1365 ;; This uses `package--mapc' so it must be called after
1366 ;; `package--initialized' is t.
1367 (package--build-compatibility-table))
1370 ;;;; Populating `package-archive-contents' from archives
1371 ;; This subsection populates the variables listed above from the
1372 ;; actual archives, instead of from a local cache.
1373 (defvar package--downloads-in-progress nil
1374 "List of in-progress asynchronous downloads.")
1376 (defvar package--all-keywords nil
1377 "List of known keywords.
1378 Generated by `package-all-keywords'. Reset to nil whenever the
1379 package archives are retrieved.")
1381 (declare-function epg-check-configuration "epg-config"
1382 (config &optional minimum-version))
1383 (declare-function epg-configuration "epg-config" ())
1384 (declare-function epg-import-keys-from-file "epg" (context keys))
1386 (defvar package--silence nil)
1388 (defun package--message (format &rest args)
1389 "Like `message', except sometimes don't print to minibuffer.
1390 If the variable `package--silence' is non-nil, the message is not
1391 displayed on the minibuffer."
1392 (apply #'message format args)
1393 (when package--silence
1394 (message nil)))
1396 ;;;###autoload
1397 (defun package-import-keyring (&optional file)
1398 "Import keys from FILE."
1399 (interactive "fFile: ")
1400 (setq file (expand-file-name file))
1401 (let ((context (epg-make-context 'OpenPGP))
1402 (homedir (expand-file-name "gnupg" package-user-dir)))
1403 (with-file-modes 448
1404 (make-directory homedir t))
1405 (setf (epg-context-home-directory context) homedir)
1406 (package--message "Importing %s..." (file-name-nondirectory file))
1407 (epg-import-keys-from-file context file)
1408 (package--message "Importing %s...done" (file-name-nondirectory file))))
1410 (defvar package--post-download-archives-hook nil
1411 "Hook run after the archive contents are downloaded.
1412 Don't run this hook directly. It is meant to be run as part of
1413 `package--update-downloads-in-progress'.")
1414 (put 'package--post-download-archives-hook 'risky-local-variable t)
1416 (defun package--update-downloads-in-progress (entry)
1417 "Remove ENTRY from `package--downloads-in-progress'.
1418 Once it's empty, run `package--post-download-archives-hook'."
1419 ;; Keep track of the downloading progress.
1420 (setq package--downloads-in-progress
1421 (remove entry package--downloads-in-progress))
1422 ;; If this was the last download, run the hook.
1423 (unless package--downloads-in-progress
1424 (package-read-all-archive-contents)
1425 (package--build-compatibility-table)
1426 ;; We message before running the hook, so the hook can give
1427 ;; messages as well.
1428 (message "Package refresh done")
1429 (run-hooks 'package--post-download-archives-hook)))
1431 (defun package--download-one-archive (archive file &optional async)
1432 "Retrieve an archive file FILE from ARCHIVE, and cache it.
1433 ARCHIVE should be a cons cell of the form (NAME . LOCATION),
1434 similar to an entry in `package-alist'. Save the cached copy to
1435 \"archives/NAME/FILE\" in `package-user-dir'."
1436 (package--with-work-buffer-async (cdr archive) file async
1437 (let* ((location (cdr archive))
1438 (name (car archive))
1439 (content (buffer-string))
1440 (dir (expand-file-name (format "archives/%s" name) package-user-dir))
1441 (local-file (expand-file-name file dir)))
1442 (when (listp (read-from-string content))
1443 (make-directory dir t)
1444 (if (or (not package-check-signature)
1445 (member archive package-unsigned-archives))
1446 ;; If we don't care about the signature, save the file and
1447 ;; we're done.
1448 (progn (write-region content nil local-file nil 'silent)
1449 (package--update-downloads-in-progress archive))
1450 ;; If we care, check it (perhaps async) and *then* write the file.
1451 (package--check-signature
1452 location file content async
1453 ;; This function will be called after signature checking.
1454 (lambda (&optional good-sigs)
1455 (unless (or good-sigs (eq package-check-signature 'allow-unsigned))
1456 ;; Even if the sig fails, this download is done, so
1457 ;; remove it from the in-progress list.
1458 (package--update-downloads-in-progress archive)
1459 (error "Unsigned archive `%s'" name))
1460 ;; Write out the archives file.
1461 (write-region content nil local-file nil 'silent)
1462 ;; Write out good signatures into archive-contents.signed file.
1463 (when good-sigs
1464 (write-region (mapconcat #'epg-signature-to-string good-sigs "\n")
1465 nil (concat local-file ".signed") nil 'silent))
1466 (package--update-downloads-in-progress archive))))))))
1468 (defun package--download-and-read-archives (&optional async)
1469 "Download descriptions of all `package-archives' and read them.
1470 This populates `package-archive-contents'. If ASYNC is non-nil,
1471 perform the downloads asynchronously."
1472 ;; The downloaded archive contents will be read as part of
1473 ;; `package--update-downloads-in-progress'.
1474 (setq package--downloads-in-progress
1475 (append package-archives
1476 package--downloads-in-progress))
1477 (dolist (archive package-archives)
1478 (condition-case-unless-debug nil
1479 (package--download-one-archive
1480 archive "archive-contents"
1481 ;; Called if the async download fails
1482 (when async
1483 (lambda () (package--update-downloads-in-progress archive))))
1484 (error (message "Failed to download `%s' archive."
1485 (car archive))))))
1487 ;;;###autoload
1488 (defun package-refresh-contents (&optional async)
1489 "Download descriptions of all configured ELPA packages.
1490 For each archive configured in the variable `package-archives',
1491 inform Emacs about the latest versions of all packages it offers,
1492 and make them available for download.
1493 Optional argument ASYNC specifies whether to perform the
1494 downloads in the background."
1495 (interactive)
1496 (unless (file-exists-p package-user-dir)
1497 (make-directory package-user-dir t))
1498 (setq package--all-keywords nil)
1499 (let ((default-keyring (expand-file-name "package-keyring.gpg"
1500 data-directory))
1501 (package--silence async))
1502 (when (and package-check-signature (file-exists-p default-keyring))
1503 (condition-case-unless-debug error
1504 (progn
1505 (epg-check-configuration (epg-configuration))
1506 (package-import-keyring default-keyring))
1507 (error (message "Cannot import default keyring: %S" (cdr error)))))
1508 (package--download-and-read-archives async)))
1511 ;;; Dependency Management
1512 ;; Calculating the full transaction necessary for an installation,
1513 ;; keeping track of which packages were installed strictly as
1514 ;; dependencies, and determining which packages cannot be removed
1515 ;; because they are dependencies.
1516 (defun package-compute-transaction (packages requirements &optional seen)
1517 "Return a list of packages to be installed, including PACKAGES.
1518 PACKAGES should be a list of `package-desc'.
1520 REQUIREMENTS should be a list of additional requirements; each
1521 element in this list should have the form (PACKAGE VERSION-LIST),
1522 where PACKAGE is a package name and VERSION-LIST is the required
1523 version of that package.
1525 This function recursively computes the requirements of the
1526 packages in REQUIREMENTS, and returns a list of all the packages
1527 that must be installed. Packages that are already installed are
1528 not included in this list.
1530 SEEN is used internally to detect infinite recursion."
1531 ;; FIXME: We really should use backtracking to explore the whole
1532 ;; search space (e.g. if foo require bar-1.3, and bar-1.4 requires toto-1.1
1533 ;; whereas bar-1.3 requires toto-1.0 and the user has put a hold on toto-1.0:
1534 ;; the current code might fail to see that it could install foo by using the
1535 ;; older bar-1.3).
1536 (dolist (elt requirements)
1537 (let* ((next-pkg (car elt))
1538 (next-version (cadr elt))
1539 (already ()))
1540 (dolist (pkg packages)
1541 (if (eq next-pkg (package-desc-name pkg))
1542 (setq already pkg)))
1543 (when already
1544 (if (version-list-<= next-version (package-desc-version already))
1545 ;; `next-pkg' is already in `packages', but its position there
1546 ;; means it might be installed too late: remove it from there, so
1547 ;; we re-add it (along with its dependencies) at an earlier place
1548 ;; below (bug#16994).
1549 (if (memq already seen) ;Avoid inf-loop on dependency cycles.
1550 (package--message "Dependency cycle going through %S"
1551 (package-desc-full-name already))
1552 (setq packages (delq already packages))
1553 (setq already nil))
1554 (error "Need package `%s-%s', but only %s is being installed"
1555 next-pkg (package-version-join next-version)
1556 (package-version-join (package-desc-version already)))))
1557 (cond
1558 (already nil)
1559 ((package-installed-p next-pkg next-version) nil)
1562 ;; A package is required, but not installed. It might also be
1563 ;; blocked via `package-load-list'.
1564 (let ((pkg-descs (cdr (assq next-pkg package-archive-contents)))
1565 (found nil)
1566 (problem nil))
1567 (while (and pkg-descs (not found))
1568 (let* ((pkg-desc (pop pkg-descs))
1569 (version (package-desc-version pkg-desc))
1570 (disabled (package-disabled-p next-pkg version)))
1571 (cond
1572 ((version-list-< version next-version)
1573 (error
1574 "Need package `%s-%s', but only %s is available"
1575 next-pkg (package-version-join next-version)
1576 (package-version-join version)))
1577 (disabled
1578 (unless problem
1579 (setq problem
1580 (if (stringp disabled)
1581 (format "Package `%s' held at version %s, \
1582 but version %s required"
1583 next-pkg disabled
1584 (package-version-join next-version))
1585 (format "Required package '%s' is disabled"
1586 next-pkg)))))
1587 (t (setq found pkg-desc)))))
1588 (unless found
1589 (if problem
1590 (error "%s" problem)
1591 (error "Package `%s-%s' is unavailable"
1592 next-pkg (package-version-join next-version))))
1593 (setq packages
1594 (package-compute-transaction (cons found packages)
1595 (package-desc-reqs found)
1596 (cons found seen))))))))
1597 packages)
1599 (defun package--find-non-dependencies ()
1600 "Return a list of installed packages which are not dependencies.
1601 Finds all packages in `package-alist' which are not dependencies
1602 of any other packages.
1603 Used to populate `package-selected-packages'."
1604 (let ((dep-list
1605 (delete-dups
1606 (apply #'append
1607 (mapcar (lambda (p) (mapcar #'car (package-desc-reqs (cadr p))))
1608 package-alist)))))
1609 (cl-loop for p in package-alist
1610 for name = (car p)
1611 unless (memq name dep-list)
1612 collect name)))
1614 (defun package--save-selected-packages (value)
1615 "Set and save `package-selected-packages' to VALUE."
1616 (let ((save-silently package--silence))
1617 (customize-save-variable
1618 'package-selected-packages
1619 (setq package-selected-packages value))))
1621 (defun package--user-selected-p (pkg)
1622 "Return non-nil if PKG is a package was installed by the user.
1623 PKG is a package name.
1624 This looks into `package-selected-packages', populating it first
1625 if it is still empty."
1626 (unless (consp package-selected-packages)
1627 (package--save-selected-packages (package--find-non-dependencies)))
1628 (memq pkg package-selected-packages))
1630 (defun package--get-deps (pkg &optional only)
1631 (let* ((pkg-desc (cadr (assq pkg package-alist)))
1632 (direct-deps (cl-loop for p in (package-desc-reqs pkg-desc)
1633 for name = (car p)
1634 when (assq name package-alist)
1635 collect name))
1636 (indirect-deps (unless (eq only 'direct)
1637 (delete-dups
1638 (cl-loop for p in direct-deps
1639 append (package--get-deps p))))))
1640 (cl-case only
1641 (direct direct-deps)
1642 (separate (list direct-deps indirect-deps))
1643 (indirect indirect-deps)
1644 (t (delete-dups (append direct-deps indirect-deps))))))
1646 (defun package--removable-packages ()
1647 "Return a list of names of packages no longer needed.
1648 These are packages which are neither contained in
1649 `package-selected-packages' nor a dependency of one that is."
1650 (let ((needed (cl-loop for p in package-selected-packages
1651 if (assq p package-alist)
1652 ;; `p' and its dependencies are needed.
1653 append (cons p (package--get-deps p)))))
1654 (cl-loop for p in (mapcar #'car package-alist)
1655 unless (memq p needed)
1656 collect p)))
1658 (defun package--used-elsewhere-p (pkg-desc &optional pkg-list)
1659 "Non-nil if PKG-DESC is a dependency of a package in PKG-LIST.
1660 Return the first package found in PKG-LIST of which PKG is a
1661 dependency.
1663 When not specified, PKG-LIST defaults to `package-alist'
1664 with PKG-DESC entry removed."
1665 (unless (string= (package-desc-status pkg-desc) "obsolete")
1666 (let ((pkg (package-desc-name pkg-desc)))
1667 (cl-loop with alist = (or pkg-list
1668 (remove (assq pkg package-alist)
1669 package-alist))
1670 for p in alist thereis
1671 (and (memq pkg (mapcar #'car (package-desc-reqs (cadr p))))
1672 (car p))))))
1674 (defun package--sort-deps-in-alist (package only)
1675 "Return a list of dependencies for PACKAGE sorted by dependency.
1676 PACKAGE is included as the first element of the returned list.
1677 ONLY is an alist associating package names to package objects.
1678 Only these packages will be in the return value an their cdrs are
1679 destructively set to nil in ONLY."
1680 (let ((out))
1681 (dolist (dep (package-desc-reqs package))
1682 (when-let ((cell (assq (car dep) only))
1683 (dep-package (cdr-safe cell)))
1684 (setcdr cell nil)
1685 (setq out (append (package--sort-deps-in-alist dep-package only)
1686 out))))
1687 (cons package out)))
1689 (defun package--sort-by-dependence (package-list)
1690 "Return PACKAGE-LIST sorted by dependence.
1691 That is, any element of the returned list is guaranteed to not
1692 directly depend on any elements that come before it.
1694 PACKAGE-LIST is a list of package-desc objects.
1695 Indirect dependencies are guaranteed to be returned in order only
1696 if all the in-between dependencies are also in PACKAGE-LIST."
1697 (let ((alist (mapcar (lambda (p) (cons (package-desc-name p) p)) package-list))
1698 out-list)
1699 (dolist (cell alist out-list)
1700 ;; `package--sort-deps-in-alist' destructively changes alist, so
1701 ;; some cells might already be empty. We check this here.
1702 (when-let ((pkg-desc (cdr cell)))
1703 (setcdr cell nil)
1704 (setq out-list
1705 (append (package--sort-deps-in-alist pkg-desc alist)
1706 out-list))))))
1709 ;;; Installation Functions
1710 ;; As opposed to the previous section (which listed some underlying
1711 ;; functions necessary for installation), this one contains the actual
1712 ;; functions that install packages. The package itself can be
1713 ;; installed in a variety of ways (archives, buffer, file), but
1714 ;; requirements (dependencies) are always satisfied by looking in
1715 ;; `package-archive-contents'.
1716 (defun package-archive-base (desc)
1717 "Return the archive containing the package NAME."
1718 (cdr (assoc (package-desc-archive desc) package-archives)))
1720 (defun package-install-from-archive (pkg-desc &optional async callback)
1721 "Download and install a tar package.
1722 If ASYNC is non-nil, perform the download asynchronously.
1723 If CALLBACK is non-nil, call it with no arguments once the
1724 operation is done."
1725 ;; This won't happen, unless the archive is doing something wrong.
1726 (when (eq (package-desc-kind pkg-desc) 'dir)
1727 (error "Can't install directory package from archive"))
1728 (let* ((location (package-archive-base pkg-desc))
1729 (file (concat (package-desc-full-name pkg-desc)
1730 (package-desc-suffix pkg-desc))))
1731 (package--with-work-buffer-async location file async
1732 (if (or (not package-check-signature)
1733 (member (package-desc-archive pkg-desc)
1734 package-unsigned-archives))
1735 ;; If we don't care about the signature, unpack and we're
1736 ;; done.
1737 (progn (let ((save-silently async))
1738 (package-unpack pkg-desc))
1739 (funcall callback))
1740 ;; If we care, check it and *then* write the file.
1741 (let ((content (buffer-string)))
1742 (package--check-signature
1743 location file content async
1744 ;; This function will be called after signature checking.
1745 (lambda (&optional good-sigs)
1746 (unless (or good-sigs (eq package-check-signature 'allow-unsigned))
1747 ;; Even if the sig fails, this download is done, so
1748 ;; remove it from the in-progress list.
1749 (error "Unsigned package: `%s'"
1750 (package-desc-name pkg-desc)))
1751 ;; Signature checked, unpack now.
1752 (with-temp-buffer (insert content)
1753 (let ((save-silently async))
1754 (package-unpack pkg-desc)))
1755 ;; Here the package has been installed successfully, mark it as
1756 ;; signed if appropriate.
1757 (when good-sigs
1758 ;; Write out good signatures into NAME-VERSION.signed file.
1759 (write-region (mapconcat #'epg-signature-to-string good-sigs "\n")
1761 (expand-file-name
1762 (concat (package-desc-full-name pkg-desc) ".signed")
1763 package-user-dir)
1764 nil 'silent)
1765 ;; Update the old pkg-desc which will be shown on the description buffer.
1766 (setf (package-desc-signed pkg-desc) t)
1767 ;; Update the new (activated) pkg-desc as well.
1768 (when-let ((pkg-descs (cdr (assq (package-desc-name pkg-desc) package-alist))))
1769 (setf (package-desc-signed (car pkg-descs)) t)))
1770 (when (functionp callback)
1771 (funcall callback)))))))))
1773 (defun package-installed-p (package &optional min-version)
1774 "Return true if PACKAGE, of MIN-VERSION or newer, is installed.
1775 If PACKAGE is a symbol, it is the package name and MIN-VERSION
1776 should be a version list.
1778 If PACKAGE is a package-desc object, MIN-VERSION is ignored."
1779 (unless package--initialized (error "package.el is not yet initialized!"))
1780 (if (package-desc-p package)
1781 (let ((dir (package-desc-dir package)))
1782 (and (stringp dir)
1783 (file-exists-p dir)))
1785 (let ((pkg-descs (cdr (assq package package-alist))))
1786 (and pkg-descs
1787 (version-list-<= min-version
1788 (package-desc-version (car pkg-descs)))))
1789 ;; Also check built-in packages.
1790 (package-built-in-p package min-version))))
1792 (defun package-download-transaction (packages &optional async callback)
1793 "Download and install all the packages in PACKAGES.
1794 PACKAGES should be a list of package-desc.
1795 If ASYNC is non-nil, perform the downloads asynchronously.
1796 If CALLBACK is non-nil, call it with no arguments once the
1797 entire operation is done.
1799 This function assumes that all package requirements in
1800 PACKAGES are satisfied, i.e. that PACKAGES is computed
1801 using `package-compute-transaction'."
1802 (cond
1803 (packages (package-install-from-archive
1804 (car packages)
1805 async
1806 (lambda ()
1807 (package-download-transaction (cdr packages))
1808 (when (functionp callback)
1809 (funcall callback)))))
1810 (callback (funcall callback))))
1812 (defun package--ensure-init-file ()
1813 "Ensure that the user's init file calls `package-initialize'."
1814 ;; Don't mess with the init-file from "emacs -Q".
1815 (when user-init-file
1816 (let* ((buffer (find-buffer-visiting user-init-file))
1817 (contains-init
1818 (if buffer
1819 (with-current-buffer buffer
1820 (save-excursion
1821 (save-restriction
1822 (widen)
1823 (goto-char (point-min))
1824 (search-forward "(package-initialize)" nil 'noerror))))
1825 (with-temp-buffer
1826 (insert-file-contents user-init-file)
1827 (goto-char (point-min))
1828 (search-forward "(package-initialize)" nil 'noerror)))))
1829 (unless contains-init
1830 (with-current-buffer (or buffer
1831 (let ((delay-mode-hooks t))
1832 (find-file-noselect user-init-file)))
1833 (save-excursion
1834 (save-restriction
1835 (widen)
1836 (goto-char (point-min))
1837 (insert
1838 ";; Added by Package.el. This must come before configurations of\n"
1839 ";; installed packages. Don't delete this line. If you don't want it,\n"
1840 ";; just comment it out by adding a semicolon to the start of the line.\n"
1841 ";; You may delete these explanatory comments.\n"
1842 "(package-initialize)\n")
1843 (unless (looking-at-p "$")
1844 (insert "\n"))
1845 (let ((file-precious-flag t))
1846 (save-buffer))
1847 (unless buffer
1848 (kill-buffer (current-buffer))))))))))
1850 ;;;###autoload
1851 (defun package-install (pkg &optional dont-select async callback)
1852 "Install the package PKG.
1853 PKG can be a package-desc or the package name of one the available packages
1854 in an archive in `package-archives'. Interactively, prompt for its name.
1856 If called interactively or if DONT-SELECT nil, add PKG to
1857 `package-selected-packages'.
1858 If ASYNC is non-nil, perform the downloads asynchronously.
1859 If CALLBACK is non-nil, call it with no arguments once the
1860 entire operation is done.
1862 If PKG is a package-desc and it is already installed, don't try
1863 to install it but still mark it as selected."
1864 (interactive
1865 (progn
1866 ;; Initialize the package system to get the list of package
1867 ;; symbols for completion.
1868 (unless package--initialized
1869 (package-initialize t))
1870 (unless package-archive-contents
1871 (package-refresh-contents))
1872 (list (intern (completing-read
1873 "Install package: "
1874 (delq nil
1875 (mapcar (lambda (elt)
1876 (unless (package-installed-p (car elt))
1877 (symbol-name (car elt))))
1878 package-archive-contents))
1879 nil t))
1880 nil)))
1881 (let ((name (if (package-desc-p pkg)
1882 (package-desc-name pkg)
1883 pkg)))
1884 (unless (or dont-select (package--user-selected-p name))
1885 (package--save-selected-packages
1886 (cons name package-selected-packages))))
1887 (if-let ((transaction
1888 (if (package-desc-p pkg)
1889 (unless (package-installed-p pkg)
1890 (package-compute-transaction (list pkg)
1891 (package-desc-reqs pkg)))
1892 (package-compute-transaction () (list (list pkg))))))
1893 (package-download-transaction transaction async callback)
1894 (package--message "`%s' is already installed" (package-desc-full-name pkg))))
1896 (defun package-strip-rcs-id (str)
1897 "Strip RCS version ID from the version string STR.
1898 If the result looks like a dotted numeric version, return it.
1899 Otherwise return nil."
1900 (when str
1901 (when (string-match "\\`[ \t]*[$]Revision:[ \t]+" str)
1902 (setq str (substring str (match-end 0))))
1903 (condition-case nil
1904 (if (version-to-list str)
1905 str)
1906 (error nil))))
1908 (declare-function lm-homepage "lisp-mnt" (&optional file))
1910 ;;;###autoload
1911 (defun package-install-from-buffer ()
1912 "Install a package from the current buffer.
1913 The current buffer is assumed to be a single .el or .tar file or
1914 a directory. These must follow the packaging guidelines (see
1915 info node `(elisp)Packaging').
1917 Specially, if current buffer is a directory, the -pkg.el
1918 description file is not mandatory, in which case the information
1919 is derived from the main .el file in the directory.
1921 Downloads and installs required packages as needed."
1922 (interactive)
1923 (let* ((pkg-desc
1924 (cond
1925 ((derived-mode-p 'dired-mode)
1926 ;; This is the only way a package-desc object with a `dir'
1927 ;; desc-kind can be created. Such packages can't be
1928 ;; uploaded or installed from archives, they can only be
1929 ;; installed from local buffers or directories.
1930 (package-dir-info))
1931 ((derived-mode-p 'tar-mode)
1932 (package-tar-file-info))
1934 (package-buffer-info))))
1935 (name (package-desc-name pkg-desc)))
1936 ;; Download and install the dependencies.
1937 (let* ((requires (package-desc-reqs pkg-desc))
1938 (transaction (package-compute-transaction nil requires)))
1939 (package-download-transaction transaction))
1940 ;; Install the package itself.
1941 (package-unpack pkg-desc)
1942 (unless (package--user-selected-p name)
1943 (package--save-selected-packages
1944 (cons name package-selected-packages)))
1945 pkg-desc))
1947 ;;;###autoload
1948 (defun package-install-file (file)
1949 "Install a package from a file.
1950 The file can either be a tar file or an Emacs Lisp file."
1951 (interactive "fPackage file name: ")
1952 (with-temp-buffer
1953 (if (file-directory-p file)
1954 (progn
1955 (setq default-directory file)
1956 (dired-mode))
1957 (insert-file-contents-literally file)
1958 (when (string-match "\\.tar\\'" file) (tar-mode)))
1959 (package-install-from-buffer)))
1961 ;;;###autoload
1962 (defun package-install-user-selected-packages ()
1963 "Ensure packages in `package-selected-packages' are installed.
1964 If some packages are not installed propose to install them."
1965 (interactive)
1966 ;; We don't need to populate `package-selected-packages' before
1967 ;; using here, because the outcome is the same either way (nothing
1968 ;; gets installed).
1969 (if (not package-selected-packages)
1970 (message "`package-selected-packages' is empty, nothing to install")
1971 (cl-loop for p in package-selected-packages
1972 unless (package-installed-p p)
1973 collect p into lst
1974 finally
1975 (if lst
1976 (when (y-or-n-p
1977 (format "%s packages will be installed:\n%s, proceed?"
1978 (length lst)
1979 (mapconcat #'symbol-name lst ", ")))
1980 (mapc #'package-install lst))
1981 (message "All your packages are already installed")))))
1984 ;;; Package Deletion
1985 (defun package--newest-p (pkg)
1986 "Return t if PKG is the newest package with its name."
1987 (equal (cadr (assq (package-desc-name pkg) package-alist))
1988 pkg))
1990 (defun package-delete (pkg-desc &optional force nosave)
1991 "Delete package PKG-DESC.
1993 Argument PKG-DESC is a full description of package as vector.
1994 When package is used elsewhere as dependency of another package,
1995 refuse deleting it and return an error.
1996 If FORCE is non-nil package will be deleted even if it is used
1997 elsewhere.
1998 If NOSAVE is non-nil, the package is not removed from
1999 `package-selected-packages'."
2000 (let ((dir (package-desc-dir pkg-desc))
2001 (name (package-desc-name pkg-desc))
2002 pkg-used-elsewhere-by)
2003 ;; If the user is trying to delete this package, they definitely
2004 ;; don't want it marked as selected, so we remove it from
2005 ;; `package-selected-packages' even if it can't be deleted.
2006 (when (and (null nosave)
2007 (package--user-selected-p name)
2008 ;; Don't deselect if this is an older version of an
2009 ;; upgraded package.
2010 (package--newest-p pkg-desc))
2011 (package--save-selected-packages (remove name package-selected-packages)))
2012 (cond ((not (string-prefix-p (file-name-as-directory
2013 (expand-file-name package-user-dir))
2014 (expand-file-name dir)))
2015 ;; Don't delete "system" packages.
2016 (error "Package `%s' is a system package, not deleting"
2017 (package-desc-full-name pkg-desc)))
2018 ((and (null force)
2019 (setq pkg-used-elsewhere-by
2020 (package--used-elsewhere-p pkg-desc)))
2021 ;; Don't delete packages used as dependency elsewhere.
2022 (error "Package `%s' is used by `%s' as dependency, not deleting"
2023 (package-desc-full-name pkg-desc)
2024 pkg-used-elsewhere-by))
2026 (delete-directory dir t t)
2027 ;; Remove NAME-VERSION.signed file.
2028 (let ((signed-file (concat dir ".signed")))
2029 (if (file-exists-p signed-file)
2030 (delete-file signed-file)))
2031 ;; Update package-alist.
2032 (let ((pkgs (assq name package-alist)))
2033 (delete pkg-desc pkgs)
2034 (unless (cdr pkgs)
2035 (setq package-alist (delq pkgs package-alist))))
2036 (package--message "Package `%s' deleted." (package-desc-full-name pkg-desc))))))
2038 ;;;###autoload
2039 (defun package-reinstall (pkg)
2040 "Reinstall package PKG.
2041 PKG should be either a symbol, the package name, or a package-desc
2042 object."
2043 (interactive (list (intern (completing-read
2044 "Reinstall package: "
2045 (mapcar #'symbol-name
2046 (mapcar #'car package-alist))))))
2047 (package-delete
2048 (if (package-desc-p pkg) pkg (cadr (assq pkg package-alist)))
2049 'force 'nosave)
2050 (package-install pkg 'dont-select))
2052 ;;;###autoload
2053 (defun package-autoremove ()
2054 "Remove packages that are no more needed.
2056 Packages that are no more needed by other packages in
2057 `package-selected-packages' and their dependencies
2058 will be deleted."
2059 (interactive)
2060 ;; If `package-selected-packages' is nil, it would make no sense to
2061 ;; try to populate it here, because then `package-autoremove' will
2062 ;; do absolutely nothing.
2063 (when (or package-selected-packages
2064 (yes-or-no-p
2065 "`package-selected-packages' is empty! Really remove ALL packages? "))
2066 (let ((removable (package--removable-packages)))
2067 (if removable
2068 (when (y-or-n-p
2069 (format "%s packages will be deleted:\n%s, proceed? "
2070 (length removable)
2071 (mapconcat #'symbol-name removable ", ")))
2072 (mapc (lambda (p)
2073 (package-delete (cadr (assq p package-alist)) t))
2074 removable))
2075 (message "Nothing to autoremove")))))
2078 ;;;; Package description buffer.
2080 ;;;###autoload
2081 (defun describe-package (package)
2082 "Display the full documentation of PACKAGE (a symbol)."
2083 (interactive
2084 (let* ((guess (function-called-at-point)))
2085 (require 'finder-inf nil t)
2086 ;; Load the package list if necessary (but don't activate them).
2087 (unless package--initialized
2088 (package-initialize t))
2089 (let ((packages (append (mapcar 'car package-alist)
2090 (mapcar 'car package-archive-contents)
2091 (mapcar 'car package--builtins))))
2092 (unless (memq guess packages)
2093 (setq guess nil))
2094 (setq packages (mapcar 'symbol-name packages))
2095 (let ((val
2096 (completing-read (if guess
2097 (format "Describe package (default %s): "
2098 guess)
2099 "Describe package: ")
2100 packages nil t nil nil guess)))
2101 (list (intern val))))))
2102 (if (not (or (package-desc-p package) (and package (symbolp package))))
2103 (message "No package specified")
2104 (help-setup-xref (list #'describe-package package)
2105 (called-interactively-p 'interactive))
2106 (with-help-window (help-buffer)
2107 (with-current-buffer standard-output
2108 (describe-package-1 package)))))
2110 (declare-function lm-commentary "lisp-mnt" (&optional file))
2112 (defun describe-package-1 (pkg)
2113 (require 'lisp-mnt)
2114 (let* ((desc (or
2115 (if (package-desc-p pkg) pkg)
2116 (cadr (assq pkg package-alist))
2117 (let ((built-in (assq pkg package--builtins)))
2118 (if built-in
2119 (package--from-builtin built-in)
2120 (cadr (assq pkg package-archive-contents))))))
2121 (name (if desc (package-desc-name desc) pkg))
2122 (pkg-dir (if desc (package-desc-dir desc)))
2123 (reqs (if desc (package-desc-reqs desc)))
2124 (version (if desc (package-desc-version desc)))
2125 (archive (if desc (package-desc-archive desc)))
2126 (extras (and desc (package-desc-extras desc)))
2127 (homepage (cdr (assoc :url extras)))
2128 (keywords (if desc (package-desc--keywords desc)))
2129 (built-in (eq pkg-dir 'builtin))
2130 (installable (and archive (not built-in)))
2131 (status (if desc (package-desc-status desc) "orphan"))
2132 (incompatible-reason (package--incompatible-p desc))
2133 (signed (if desc (package-desc-signed desc))))
2134 (when incompatible-reason
2135 (setq status "incompatible"))
2136 (prin1 name)
2137 (princ " is ")
2138 (princ (if (memq (aref status 0) '(?a ?e ?i ?o ?u)) "an " "a "))
2139 (princ status)
2140 (princ " package.\n\n")
2142 (insert " " (propertize "Status" 'font-lock-face 'bold) ": ")
2143 (cond (built-in
2144 (insert (propertize (capitalize status)
2145 'font-lock-face 'font-lock-builtin-face)
2146 "."))
2147 (pkg-dir
2148 (insert (propertize (if (member status '("unsigned" "dependency"))
2149 "Installed"
2150 (capitalize status)) ;FIXME: Why comment-face?
2151 'font-lock-face 'font-lock-comment-face))
2152 (insert " in `")
2153 ;; Todo: Add button for uninstalling.
2154 (help-insert-xref-button (abbreviate-file-name
2155 (file-name-as-directory pkg-dir))
2156 'help-package-def pkg-dir)
2157 (if (and (package-built-in-p name)
2158 (not (package-built-in-p name version)))
2159 (insert "',\n shadowing a "
2160 (propertize "built-in package"
2161 'font-lock-face 'font-lock-builtin-face))
2162 (insert "'"))
2163 (if signed
2164 (insert ".")
2165 (insert " (unsigned).")))
2166 (incompatible-reason
2167 (insert (propertize "Incompatible" 'face font-lock-warning-face)
2168 " because it depends on ")
2169 (if (stringp incompatible-reason)
2170 (insert "Emacs " incompatible-reason ".")
2171 (insert "uninstallable packages.")))
2172 (installable
2173 (insert (capitalize status))
2174 (insert " from " (format "%s" archive))
2175 (insert " -- ")
2176 (package-make-button
2177 "Install"
2178 'action 'package-install-button-action
2179 'package-desc desc))
2180 (t (insert (capitalize status) ".")))
2181 (insert "\n")
2182 (insert " " (propertize "Archive" 'font-lock-face 'bold)
2183 ": " (or archive "n/a") "\n")
2184 (and version
2185 (insert " "
2186 (propertize "Version" 'font-lock-face 'bold) ": "
2187 (package-version-join version) "\n"))
2189 (setq reqs (if desc (package-desc-reqs desc)))
2190 (when reqs
2191 (insert " " (propertize "Requires" 'font-lock-face 'bold) ": ")
2192 (let ((first t))
2193 (dolist (req reqs)
2194 (let* ((name (car req))
2195 (vers (cadr req))
2196 (text (format "%s-%s" (symbol-name name)
2197 (package-version-join vers)))
2198 (reason (if (and (listp incompatible-reason)
2199 (assq name incompatible-reason))
2200 " (not available)" "")))
2201 (cond (first (setq first nil))
2202 ((>= (+ 2 (current-column) (length text) (length reason))
2203 (window-width))
2204 (insert ",\n "))
2205 (t (insert ", ")))
2206 (help-insert-xref-button text 'help-package name)
2207 (insert reason)))
2208 (insert "\n")))
2209 (insert " " (propertize "Summary" 'font-lock-face 'bold)
2210 ": " (if desc (package-desc-summary desc)) "\n")
2211 (when homepage
2212 (insert " " (propertize "Homepage" 'font-lock-face 'bold) ": ")
2213 (help-insert-xref-button homepage 'help-url homepage)
2214 (insert "\n"))
2215 (when keywords
2216 (insert " " (propertize "Keywords" 'font-lock-face 'bold) ": ")
2217 (dolist (k keywords)
2218 (package-make-button
2220 'package-keyword k
2221 'action 'package-keyword-button-action)
2222 (insert " "))
2223 (insert "\n"))
2224 (let* ((all-pkgs (append (cdr (assq name package-alist))
2225 (cdr (assq name package-archive-contents))
2226 (let ((bi (assq name package--builtins)))
2227 (if bi (list (package--from-builtin bi))))))
2228 (other-pkgs (delete desc all-pkgs)))
2229 (when other-pkgs
2230 (insert " " (propertize "Other versions" 'font-lock-face 'bold) ": "
2231 (mapconcat
2232 (lambda (opkg)
2233 (let* ((ov (package-desc-version opkg))
2234 (dir (package-desc-dir opkg))
2235 (from (or (package-desc-archive opkg)
2236 (if (stringp dir) "installed" dir))))
2237 (if (not ov) (format "%s" from)
2238 (format "%s (%s)"
2239 (make-text-button (package-version-join ov) nil
2240 'face 'link
2241 'follow-link t
2242 'action
2243 (lambda (_button)
2244 (describe-package opkg)))
2245 from))))
2246 other-pkgs ", ")
2247 ".\n")))
2249 (insert "\n")
2251 (if built-in
2252 ;; For built-in packages, insert the commentary.
2253 (let ((fn (locate-file (format "%s.el" name) load-path
2254 load-file-rep-suffixes))
2255 (opoint (point)))
2256 (insert (or (lm-commentary fn) ""))
2257 (save-excursion
2258 (goto-char opoint)
2259 (when (re-search-forward "^;;; Commentary:\n" nil t)
2260 (replace-match ""))
2261 (while (re-search-forward "^\\(;+ ?\\)" nil t)
2262 (replace-match ""))))
2263 (let ((readme (expand-file-name (format "%s-readme.txt" name)
2264 package-user-dir))
2265 readme-string)
2266 ;; For elpa packages, try downloading the commentary. If that
2267 ;; fails, try an existing readme file in `package-user-dir'.
2268 (cond ((condition-case nil
2269 (save-excursion
2270 (package--with-work-buffer
2271 (package-archive-base desc)
2272 (format "%s-readme.txt" name)
2273 (save-excursion
2274 (goto-char (point-max))
2275 (unless (bolp)
2276 (insert ?\n)))
2277 (write-region nil nil
2278 (expand-file-name readme package-user-dir)
2279 nil 'silent)
2280 (setq readme-string (buffer-string))
2282 (error nil))
2283 (insert readme-string))
2284 ((file-readable-p readme)
2285 (insert-file-contents readme)
2286 (goto-char (point-max))))))))
2288 (defun package-install-button-action (button)
2289 (let ((pkg-desc (button-get button 'package-desc)))
2290 (when (y-or-n-p (format "Install package `%s'? "
2291 (package-desc-full-name pkg-desc)))
2292 (package-install pkg-desc nil)
2293 (revert-buffer nil t)
2294 (goto-char (point-min)))))
2296 (defun package-keyword-button-action (button)
2297 (let ((pkg-keyword (button-get button 'package-keyword)))
2298 (package-show-package-list t (list pkg-keyword))))
2300 (defun package-make-button (text &rest props)
2301 (let ((button-text (if (display-graphic-p) text (concat "[" text "]")))
2302 (button-face (if (display-graphic-p)
2303 '(:box (:line-width 2 :color "dark grey")
2304 :background "light grey"
2305 :foreground "black")
2306 'link)))
2307 (apply 'insert-text-button button-text 'face button-face 'follow-link t
2308 props)))
2311 ;;;; Package menu mode.
2313 (defvar package-menu-mode-map
2314 (let ((map (make-sparse-keymap))
2315 (menu-map (make-sparse-keymap "Package")))
2316 (set-keymap-parent map tabulated-list-mode-map)
2317 (define-key map "\C-m" 'package-menu-describe-package)
2318 (define-key map "u" 'package-menu-mark-unmark)
2319 (define-key map "\177" 'package-menu-backup-unmark)
2320 (define-key map "d" 'package-menu-mark-delete)
2321 (define-key map "i" 'package-menu-mark-install)
2322 (define-key map "U" 'package-menu-mark-upgrades)
2323 (define-key map "r" 'package-menu-refresh)
2324 (define-key map "f" 'package-menu-filter)
2325 (define-key map "~" 'package-menu-mark-obsolete-for-deletion)
2326 (define-key map "x" 'package-menu-execute)
2327 (define-key map "h" 'package-menu-quick-help)
2328 (define-key map "?" 'package-menu-describe-package)
2329 (define-key map "(" #'package-menu-hide-obsolete)
2330 (define-key map [menu-bar package-menu] (cons "Package" menu-map))
2331 (define-key menu-map [mq]
2332 '(menu-item "Quit" quit-window
2333 :help "Quit package selection"))
2334 (define-key menu-map [s1] '("--"))
2335 (define-key menu-map [mn]
2336 '(menu-item "Next" next-line
2337 :help "Next Line"))
2338 (define-key menu-map [mp]
2339 '(menu-item "Previous" previous-line
2340 :help "Previous Line"))
2341 (define-key menu-map [s2] '("--"))
2342 (define-key menu-map [mu]
2343 '(menu-item "Unmark" package-menu-mark-unmark
2344 :help "Clear any marks on a package and move to the next line"))
2345 (define-key menu-map [munm]
2346 '(menu-item "Unmark Backwards" package-menu-backup-unmark
2347 :help "Back up one line and clear any marks on that package"))
2348 (define-key menu-map [md]
2349 '(menu-item "Mark for Deletion" package-menu-mark-delete
2350 :help "Mark a package for deletion and move to the next line"))
2351 (define-key menu-map [mi]
2352 '(menu-item "Mark for Install" package-menu-mark-install
2353 :help "Mark a package for installation and move to the next line"))
2354 (define-key menu-map [mupgrades]
2355 '(menu-item "Mark Upgradable Packages" package-menu-mark-upgrades
2356 :help "Mark packages that have a newer version for upgrading"))
2357 (define-key menu-map [s3] '("--"))
2358 (define-key menu-map [mf]
2359 '(menu-item "Filter Package List..." package-menu-filter
2360 :help "Filter package selection (q to go back)"))
2361 (define-key menu-map [mg]
2362 '(menu-item "Update Package List" revert-buffer
2363 :help "Update the list of packages"))
2364 (define-key menu-map [mr]
2365 '(menu-item "Refresh Package List" package-menu-refresh
2366 :help "Download the ELPA archive"))
2367 (define-key menu-map [s4] '("--"))
2368 (define-key menu-map [mt]
2369 '(menu-item "Mark Obsolete Packages" package-menu-mark-obsolete-for-deletion
2370 :help "Mark all obsolete packages for deletion"))
2371 (define-key menu-map [mx]
2372 '(menu-item "Execute Actions" package-menu-execute
2373 :help "Perform all the marked actions"))
2374 (define-key menu-map [s5] '("--"))
2375 (define-key menu-map [mh]
2376 '(menu-item "Help" package-menu-quick-help
2377 :help "Show short key binding help for package-menu-mode"))
2378 (define-key menu-map [mc]
2379 '(menu-item "Describe Package" package-menu-describe-package
2380 :help "Display information about this package"))
2381 map)
2382 "Local keymap for `package-menu-mode' buffers.")
2384 (defvar package-menu--new-package-list nil
2385 "List of newly-available packages since `list-packages' was last called.")
2387 (define-derived-mode package-menu-mode tabulated-list-mode "Package Menu"
2388 "Major mode for browsing a list of packages.
2389 Letters do not insert themselves; instead, they are commands.
2390 \\<package-menu-mode-map>
2391 \\{package-menu-mode-map}"
2392 (setq mode-line-process '(package--downloads-in-progress ":Loading"))
2393 (setq tabulated-list-format
2394 `[("Package" 18 package-menu--name-predicate)
2395 ("Version" 13 nil)
2396 ("Status" 10 package-menu--status-predicate)
2397 ,@(if (cdr package-archives)
2398 '(("Archive" 10 package-menu--archive-predicate)))
2399 ("Description" 0 nil)])
2400 (setq tabulated-list-padding 2)
2401 (setq tabulated-list-sort-key (cons "Status" nil))
2402 (add-hook 'tabulated-list-revert-hook 'package-menu--refresh nil t)
2403 (tabulated-list-init-header))
2405 (defmacro package--push (pkg-desc status listname)
2406 "Convenience macro for `package-menu--generate'.
2407 If the alist stored in the symbol LISTNAME lacks an entry for a
2408 package PKG-DESC, add one. The alist is keyed with PKG-DESC."
2409 `(unless (assoc ,pkg-desc ,listname)
2410 ;; FIXME: Should we move status into pkg-desc?
2411 (push (cons ,pkg-desc ,status) ,listname)))
2413 (defvar package-list-unversioned nil
2414 "If non-nil include packages that don't have a version in `list-package'.")
2416 (defvar package-list-unsigned nil
2417 "If non-nil, mention in the list which packages were installed w/o signature.")
2419 (defvar package--emacs-version-list (version-to-list emacs-version)
2420 "`emacs-version', as a list.")
2422 (defun package--incompatible-p (pkg &optional shallow)
2423 "Return non-nil if PKG has no chance of being installable.
2424 PKG is a package-desc object.
2426 If SHALLOW is non-nil, this only checks if PKG depends on a
2427 higher `emacs-version' than the one being used. Otherwise, also
2428 checks the viability of dependencies, according to
2429 `package--compatibility-table'.
2431 If PKG requires an incompatible Emacs version, the return value
2432 is this version (as a string).
2433 If PKG requires incompatible packages, the return value is a list
2434 of these dependencies, similar to the list returned by
2435 `package-desc-reqs'."
2436 (let* ((reqs (package-desc-reqs pkg))
2437 (version (cadr (assq 'emacs reqs))))
2438 (if (and version (version-list-< package--emacs-version-list version))
2439 (package-version-join version)
2440 (unless shallow
2441 (let (out)
2442 (dolist (dep (package-desc-reqs pkg) out)
2443 (let ((dep-name (car dep)))
2444 (unless (eq 'emacs dep-name)
2445 (let ((cv (gethash dep-name package--compatibility-table)))
2446 (when (version-list-< (or cv '(0)) (or (cadr dep) '(0)))
2447 (push dep out)))))))))))
2449 (defun package-desc-status (pkg-desc)
2450 (let* ((name (package-desc-name pkg-desc))
2451 (dir (package-desc-dir pkg-desc))
2452 (lle (assq name package-load-list))
2453 (held (cadr lle))
2454 (version (package-desc-version pkg-desc))
2455 (signed (or (not package-list-unsigned)
2456 (package-desc-signed pkg-desc))))
2457 (cond
2458 ((eq dir 'builtin) "built-in")
2459 ((and lle (null held)) "disabled")
2460 ((stringp held)
2461 (let ((hv (if (stringp held) (version-to-list held))))
2462 (cond
2463 ((version-list-= version hv) "held")
2464 ((version-list-< version hv) "obsolete")
2465 (t "disabled"))))
2466 ((package-built-in-p name version) "obsolete")
2467 ((package--incompatible-p pkg-desc) "incompat")
2468 (dir ;One of the installed packages.
2469 (cond
2470 ((not (file-exists-p (package-desc-dir pkg-desc))) "deleted")
2471 ((eq pkg-desc (cadr (assq name package-alist)))
2472 (if (not signed) "unsigned"
2473 (if (package--user-selected-p name)
2474 "installed" "dependency")))
2475 (t "obsolete")))
2477 (let* ((ins (cadr (assq name package-alist)))
2478 (ins-v (if ins (package-desc-version ins))))
2479 (cond
2480 ;; Installed obsolete packages are handled in the `dir'
2481 ;; clause above. Here we handle available obsolete, which
2482 ;; are displayed depending on `package-menu--hide-obsolete'.
2483 ((and ins (version-list-<= version ins-v)) "avail-obso")
2485 (if (memq name package-menu--new-package-list)
2486 "new" "available"))))))))
2488 (defvar package-menu--hide-obsolete t
2489 "Whether avaiable obsolete packages should be hidden.
2490 Can be toggled with \\<package-menu-mode-map> \\[package-menu-hide-obsolete].
2491 Installed obsolete packages are always displayed.")
2493 (defun package-menu-hide-obsolete ()
2494 "Toggle visibility of obsolete available packages."
2495 (interactive)
2496 (unless (derived-mode-p 'package-menu-mode)
2497 (user-error "The current buffer is not a Package Menu"))
2498 (setq package-menu--hide-obsolete
2499 (not package-menu--hide-obsolete))
2500 (message "%s available-obsolete packages" (if package-menu--hide-obsolete
2501 "Hiding" "Displaying"))
2502 (revert-buffer nil 'no-confirm))
2504 (defun package--remove-hidden (pkg-list)
2505 "Filter PKG-LIST according to `package-archive-priorities'.
2506 PKG-LIST must be a list of package-desc objects sorted by
2507 decreasing version number.
2508 Return a list of packages tied for the highest priority according
2509 to their archives."
2510 (when pkg-list
2511 ;; The first is a variable toggled with
2512 ;; `package-menu-hide-obsolete', the second is a static user
2513 ;; option that defines *what* we hide.
2514 (if (and package-menu--hide-obsolete
2515 package-menu-hide-low-priority)
2516 (let ((max-priority (package-desc-priority (car pkg-list)))
2517 (out (list (pop pkg-list))))
2518 (dolist (p pkg-list (nreverse out))
2519 (let ((priority (package-desc-priority p)))
2520 (cond
2521 ((> priority max-priority)
2522 (setq max-priority priority)
2523 (setq out (list p)))
2524 ;; This assumes pkg-list is sorted by version number.
2525 ((and (= priority max-priority)
2526 (eq package-menu-hide-low-priority 'archive))
2527 (push p out))))))
2528 pkg-list)))
2530 (defun package-menu--refresh (&optional packages keywords)
2531 "Re-populate the `tabulated-list-entries'.
2532 PACKAGES should be nil or t, which means to display all known packages.
2533 KEYWORDS should be nil or a list of keywords."
2534 ;; Construct list of (PKG-DESC . STATUS).
2535 (unless packages (setq packages t))
2536 (let (info-list name)
2537 ;; Installed packages:
2538 (dolist (elt package-alist)
2539 (setq name (car elt))
2540 (when (or (eq packages t) (memq name packages))
2541 (dolist (pkg (cdr elt))
2542 (when (package--has-keyword-p pkg keywords)
2543 (package--push pkg (package-desc-status pkg) info-list)))))
2545 ;; Built-in packages:
2546 (dolist (elt package--builtins)
2547 (setq name (car elt))
2548 (when (and (not (eq name 'emacs)) ; Hide the `emacs' package.
2549 (package--has-keyword-p (package--from-builtin elt) keywords)
2550 (or package-list-unversioned
2551 (package--bi-desc-version (cdr elt)))
2552 (or (eq packages t) (memq name packages)))
2553 (package--push (package--from-builtin elt) "built-in" info-list)))
2555 ;; Available and disabled packages:
2556 (dolist (elt package-archive-contents)
2557 (setq name (car elt))
2558 (when (or (eq packages t) (memq name packages))
2559 (dolist (pkg (package--remove-hidden (cdr elt)))
2560 ;; Hide available obsolete packages.
2561 (when (and (not (and package-menu--hide-obsolete
2562 (package-installed-p (package-desc-name pkg)
2563 (package-desc-version pkg))))
2564 (package--has-keyword-p pkg keywords))
2565 (package--push pkg (package-desc-status pkg) info-list)))))
2567 ;; Print the result.
2568 (setq tabulated-list-entries
2569 (mapcar #'package-menu--print-info info-list))))
2571 (defun package-all-keywords ()
2572 "Collect all package keywords"
2573 (unless package--all-keywords
2574 (package--mapc (lambda (desc)
2575 (let* ((desc-keywords (and desc (package-desc--keywords desc))))
2576 (setq package--all-keywords (append desc-keywords package--all-keywords))))))
2577 package--all-keywords)
2579 (defun package--mapc (function &optional packages)
2580 "Call FUNCTION for all known PACKAGES.
2581 PACKAGES can be nil or t, which means to display all known
2582 packages, or a list of packages.
2584 Built-in packages are converted with `package--from-builtin'."
2585 (unless packages (setq packages t))
2586 (let (name)
2587 ;; Installed packages:
2588 (dolist (elt package-alist)
2589 (setq name (car elt))
2590 (when (or (eq packages t) (memq name packages))
2591 (mapc function (cdr elt))))
2593 ;; Built-in packages:
2594 (dolist (elt package--builtins)
2595 (setq name (car elt))
2596 (when (and (not (eq name 'emacs)) ; Hide the `emacs' package.
2597 (or package-list-unversioned
2598 (package--bi-desc-version (cdr elt)))
2599 (or (eq packages t) (memq name packages)))
2600 (funcall function (package--from-builtin elt))))
2602 ;; Available and disabled packages:
2603 (dolist (elt package-archive-contents)
2604 (setq name (car elt))
2605 (when (or (eq packages t) (memq name packages))
2606 (dolist (pkg (cdr elt))
2607 ;; Hide obsolete packages.
2608 (unless (package-installed-p (package-desc-name pkg)
2609 (package-desc-version pkg))
2610 (funcall function pkg)))))))
2612 (defun package--has-keyword-p (desc &optional keywords)
2613 "Test if package DESC has any of the given KEYWORDS.
2614 When none are given, the package matches."
2615 (if keywords
2616 (let ((desc-keywords (and desc (package-desc--keywords desc)))
2617 found)
2618 (while (and (not found) keywords)
2619 (let ((k (pop keywords)))
2620 (setq found
2621 (or (string= k (concat "arc:" (package-desc-archive desc)))
2622 (string= k (concat "status:" (package-desc-status desc)))
2623 (member k desc-keywords)))))
2624 found)
2627 (defun package-menu--generate (remember-pos packages &optional keywords)
2628 "Populate the Package Menu.
2629 If REMEMBER-POS is non-nil, keep point on the same entry.
2630 PACKAGES should be t, which means to display all known packages,
2631 or a list of package names (symbols) to display.
2633 With KEYWORDS given, only packages with those keywords are
2634 shown."
2635 (package-menu--refresh packages keywords)
2636 (setf (car (aref tabulated-list-format 0))
2637 (if keywords
2638 (let ((filters (mapconcat 'identity keywords ",")))
2639 (concat "Package[" filters "]"))
2640 "Package"))
2641 (if keywords
2642 (define-key package-menu-mode-map "q" 'package-show-package-list)
2643 (define-key package-menu-mode-map "q" 'quit-window))
2644 (tabulated-list-init-header)
2645 (tabulated-list-print remember-pos))
2647 (defun package-menu--print-info (pkg)
2648 "Return a package entry suitable for `tabulated-list-entries'.
2649 PKG has the form (PKG-DESC . STATUS).
2650 Return (PKG-DESC [NAME VERSION STATUS DOC])."
2651 (let* ((pkg-desc (car pkg))
2652 (status (cdr pkg))
2653 (face (pcase status
2654 (`"built-in" 'font-lock-builtin-face)
2655 (`"available" 'default)
2656 (`"avail-obso" 'font-lock-comment-face)
2657 (`"new" 'bold)
2658 (`"held" 'font-lock-constant-face)
2659 (`"disabled" 'font-lock-warning-face)
2660 (`"installed" 'font-lock-comment-face)
2661 (`"dependency" 'font-lock-comment-face)
2662 (`"unsigned" 'font-lock-warning-face)
2663 (`"incompat" 'font-lock-comment-face)
2664 (_ 'font-lock-warning-face)))) ; obsolete.
2665 (list pkg-desc
2666 `[,(list (symbol-name (package-desc-name pkg-desc))
2667 'face 'link
2668 'follow-link t
2669 'package-desc pkg-desc
2670 'action 'package-menu-describe-package)
2671 ,(propertize (package-version-join
2672 (package-desc-version pkg-desc))
2673 'font-lock-face face)
2674 ,(propertize status 'font-lock-face face)
2675 ,@(if (cdr package-archives)
2676 (list (propertize (or (package-desc-archive pkg-desc) "")
2677 'font-lock-face face)))
2678 ,(propertize (package-desc-summary pkg-desc)
2679 'font-lock-face face)])))
2681 (defun package-menu-refresh ()
2682 "Download the Emacs Lisp package archive.
2683 This fetches the contents of each archive specified in
2684 `package-archives', and then refreshes the package menu."
2685 (interactive)
2686 (unless (derived-mode-p 'package-menu-mode)
2687 (user-error "The current buffer is not a Package Menu"))
2688 (setq package-menu--old-archive-contents package-archive-contents)
2689 (setq package-menu--new-package-list nil)
2690 (package-refresh-contents package-menu-async))
2692 (defun package-menu-describe-package (&optional button)
2693 "Describe the current package.
2694 If optional arg BUTTON is non-nil, describe its associated package."
2695 (interactive)
2696 (let ((pkg-desc (if button (button-get button 'package-desc)
2697 (tabulated-list-get-id))))
2698 (if pkg-desc
2699 (describe-package pkg-desc)
2700 (user-error "No package here"))))
2702 ;; fixme numeric argument
2703 (defun package-menu-mark-delete (&optional _num)
2704 "Mark a package for deletion and move to the next line."
2705 (interactive "p")
2706 (if (member (package-menu-get-status)
2707 '("installed" "dependency" "obsolete" "unsigned"))
2708 (tabulated-list-put-tag "D" t)
2709 (forward-line)))
2711 (defun package-menu-mark-install (&optional _num)
2712 "Mark a package for installation and move to the next line."
2713 (interactive "p")
2714 (if (member (package-menu-get-status) '("available" "avail-obso" "new" "dependency"))
2715 (tabulated-list-put-tag "I" t)
2716 (forward-line)))
2718 (defun package-menu-mark-unmark (&optional _num)
2719 "Clear any marks on a package and move to the next line."
2720 (interactive "p")
2721 (tabulated-list-put-tag " " t))
2723 (defun package-menu-backup-unmark ()
2724 "Back up one line and clear any marks on that package."
2725 (interactive)
2726 (forward-line -1)
2727 (tabulated-list-put-tag " "))
2729 (defun package-menu-mark-obsolete-for-deletion ()
2730 "Mark all obsolete packages for deletion."
2731 (interactive)
2732 (save-excursion
2733 (goto-char (point-min))
2734 (while (not (eobp))
2735 (if (equal (package-menu-get-status) "obsolete")
2736 (tabulated-list-put-tag "D" t)
2737 (forward-line 1)))))
2739 (defvar package--quick-help-keys
2740 '(("install," "delete," "unmark," ("execute" . 1))
2741 ("next," "previous")
2742 ("refresh-contents," "g-redisplay," "filter," "(-toggle-obsolete" "help")))
2744 (defun package--prettify-quick-help-key (desc)
2745 "Prettify DESC to be displayed as a help menu."
2746 (if (listp desc)
2747 (if (listp (cdr desc))
2748 (mapconcat #'package--prettify-quick-help-key desc " ")
2749 (let ((place (cdr desc))
2750 (out (car desc)))
2751 ;; (setq out (propertize out 'face 'paradox-comment-face))
2752 (add-text-properties place (1+ place)
2753 '(face (bold font-lock-function-name-face))
2754 out)
2755 out))
2756 (package--prettify-quick-help-key (cons desc 0))))
2758 (defun package-menu-quick-help ()
2759 "Show short key binding help for `package-menu-mode'.
2760 The full list of keys can be viewed with \\[describe-mode]."
2761 (interactive)
2762 (message (mapconcat #'package--prettify-quick-help-key
2763 package--quick-help-keys "\n")))
2765 (define-obsolete-function-alias
2766 'package-menu-view-commentary 'package-menu-describe-package "24.1")
2768 (defun package-menu-get-status ()
2769 (let* ((id (tabulated-list-get-id))
2770 (entry (and id (assq id tabulated-list-entries))))
2771 (if entry
2772 (aref (cadr entry) 2)
2773 "")))
2775 (defun package-archive-priority (archive)
2776 "Return the priority of ARCHIVE.
2778 The archive priorities are specified in
2779 `package-archive-priorities'. If not given there, the priority
2780 defaults to 0."
2781 (or (cdr (assoc archive package-archive-priorities))
2784 (defun package-desc-priority-version (pkg-desc)
2785 "Return the version PKG-DESC with the archive priority prepended.
2787 This allows for easy comparison of package versions from
2788 different archives if archive priorities are meant to be taken in
2789 consideration."
2790 (cons (package-desc-priority pkg-desc)
2791 (package-desc-version pkg-desc)))
2793 (defun package-menu--find-upgrades ()
2794 (let (installed available upgrades)
2795 ;; Build list of installed/available packages in this buffer.
2796 (dolist (entry tabulated-list-entries)
2797 ;; ENTRY is (PKG-DESC [NAME VERSION STATUS DOC])
2798 (let ((pkg-desc (car entry))
2799 (status (aref (cadr entry) 2)))
2800 (cond ((member status '("installed" "dependency" "unsigned"))
2801 (push pkg-desc installed))
2802 ((member status '("available" "new"))
2803 (setq available (package--append-to-alist pkg-desc available))))))
2804 ;; Loop through list of installed packages, finding upgrades.
2805 (dolist (pkg-desc installed)
2806 (let* ((name (package-desc-name pkg-desc))
2807 (avail-pkg (cadr (assq name available))))
2808 (and avail-pkg
2809 (version-list-< (package-desc-priority-version pkg-desc)
2810 (package-desc-priority-version avail-pkg))
2811 (push (cons name avail-pkg) upgrades))))
2812 upgrades))
2814 (defun package-menu-mark-upgrades ()
2815 "Mark all upgradable packages in the Package Menu.
2816 For each installed package with a newer version available, place
2817 an (I)nstall flag on the available version and a (D)elete flag on
2818 the installed version. A subsequent \\[package-menu-execute]
2819 call will upgrade the package."
2820 (interactive)
2821 (unless (derived-mode-p 'package-menu-mode)
2822 (error "The current buffer is not a Package Menu"))
2823 (let ((upgrades (package-menu--find-upgrades)))
2824 (if (null upgrades)
2825 (message "No packages to upgrade.")
2826 (widen)
2827 (save-excursion
2828 (goto-char (point-min))
2829 (while (not (eobp))
2830 (let* ((pkg-desc (tabulated-list-get-id))
2831 (upgrade (cdr (assq (package-desc-name pkg-desc) upgrades))))
2832 (cond ((null upgrade)
2833 (forward-line 1))
2834 ((equal pkg-desc upgrade)
2835 (package-menu-mark-install))
2837 (package-menu-mark-delete))))))
2838 (message "%d package%s marked for upgrading."
2839 (length upgrades)
2840 (if (= (length upgrades) 1) "" "s")))))
2842 (defun package-menu--list-to-prompt (packages)
2843 "Return a string listing PACKAGES that's usable in a prompt.
2844 PACKAGES is a list of `package-desc' objects.
2845 Formats the returned string to be usable in a minibuffer
2846 prompt (see `package-menu--prompt-transaction-p')."
2847 (cond
2848 ;; None
2849 ((not packages) "")
2850 ;; More than 1
2851 ((cdr packages)
2852 (format "these %d packages (%s)"
2853 (length packages)
2854 (mapconcat #'package-desc-full-name packages ", ")))
2855 ;; Exactly 1
2856 (t (format "package `%s'"
2857 (package-desc-full-name (car packages))))))
2859 (defun package-menu--prompt-transaction-p (install delete)
2860 "Prompt the user about installing INSTALL and deleting DELETE.
2861 INSTALL and DELETE are lists of `package-desc'. Either may be
2862 nil, but not both."
2863 (let* ((upg (cl-intersection install delete :key #'package-desc-name))
2864 (ins (cl-set-difference install upg :key #'package-desc-name))
2865 (del (cl-set-difference delete upg :key #'package-desc-name)))
2866 (y-or-n-p
2867 (concat
2868 (when del "Delete ")
2869 (package-menu--list-to-prompt del)
2870 (when (and del ins)
2871 (if upg "; " "; and "))
2872 (when ins "Install ")
2873 (package-menu--list-to-prompt ins)
2874 (when (and upg (or ins del)) "; and ")
2875 (when upg "Upgrade ")
2876 (package-menu--list-to-prompt upg)
2877 "? "))))
2879 (defun package-menu--perform-transaction (install-list delete-list &optional async)
2880 "Install packages in INSTALL-LIST and delete DELETE-LIST.
2881 If ASYNC is non-nil, perform the installation downloads
2882 asynchronously."
2883 ;; While there are packages to install, call `package-install' on
2884 ;; the next one and defer deletion to the callback function.
2885 (if install-list
2886 (let* ((pkg (car install-list))
2887 (rest (cdr install-list))
2888 ;; Don't mark as selected if it's a new version of an
2889 ;; installed package.
2890 (dont-mark (and (not (package-installed-p pkg))
2891 (package-installed-p
2892 (package-desc-name pkg)))))
2893 (package-install
2894 pkg dont-mark async
2895 (lambda () (package-menu--perform-transaction rest delete-list async))))
2896 ;; Once there are no more packages to install, proceed to
2897 ;; deletion.
2898 (let ((package--silence async))
2899 (dolist (elt (package--sort-by-dependence delete-list))
2900 (condition-case-unless-debug err
2901 (package-delete elt)
2902 (error (message (cadr err)))))
2903 (when package-selected-packages
2904 (when-let ((removable (package--removable-packages)))
2905 (package--message "These %d packages are no longer needed, type `M-x package-autoremove' to remove them (%s)"
2906 (length removable)
2907 (mapconcat #'symbol-name removable ", ")))))
2908 (message "Transaction done")
2909 (package-menu--post-refresh)))
2911 (defun package-menu-execute (&optional noquery)
2912 "Perform marked Package Menu actions.
2913 Packages marked for installation are downloaded and installed;
2914 packages marked for deletion are removed.
2915 Optional argument NOQUERY non-nil means do not ask the user to confirm."
2916 (interactive)
2917 (unless (derived-mode-p 'package-menu-mode)
2918 (error "The current buffer is not in Package Menu mode"))
2919 (let (install-list delete-list cmd pkg-desc)
2920 (save-excursion
2921 (goto-char (point-min))
2922 (while (not (eobp))
2923 (setq cmd (char-after))
2924 (unless (eq cmd ?\s)
2925 ;; This is the key PKG-DESC.
2926 (setq pkg-desc (tabulated-list-get-id))
2927 (cond ((eq cmd ?D)
2928 (push pkg-desc delete-list))
2929 ((eq cmd ?I)
2930 (push pkg-desc install-list))))
2931 (forward-line)))
2932 (unless (or delete-list install-list)
2933 (user-error "No operations specified"))
2934 (when (or noquery
2935 (package-menu--prompt-transaction-p install-list delete-list))
2936 (message "Transaction started")
2937 ;; This calls `package-menu--generate' after everything's done.
2938 (package-menu--perform-transaction
2939 install-list delete-list package-menu-async))))
2941 (defun package-menu--version-predicate (A B)
2942 (let ((vA (or (aref (cadr A) 1) '(0)))
2943 (vB (or (aref (cadr B) 1) '(0))))
2944 (if (version-list-= vA vB)
2945 (package-menu--name-predicate A B)
2946 (version-list-< vA vB))))
2948 (defun package-menu--status-predicate (A B)
2949 (let ((sA (aref (cadr A) 2))
2950 (sB (aref (cadr B) 2)))
2951 (cond ((string= sA sB)
2952 (package-menu--name-predicate A B))
2953 ((string= sA "new") t)
2954 ((string= sB "new") nil)
2955 ((string-prefix-p "avail" sA)
2956 (if (string-prefix-p "avail" sB)
2957 (package-menu--name-predicate A B)
2959 ((string-prefix-p "avail" sB) nil)
2960 ((string= sA "installed") t)
2961 ((string= sB "installed") nil)
2962 ((string= sA "dependency") t)
2963 ((string= sB "dependency") nil)
2964 ((string= sA "unsigned") t)
2965 ((string= sB "unsigned") nil)
2966 ((string= sA "held") t)
2967 ((string= sB "held") nil)
2968 ((string= sA "built-in") t)
2969 ((string= sB "built-in") nil)
2970 ((string= sA "obsolete") t)
2971 ((string= sB "obsolete") nil)
2972 ((string= sA "incompat") t)
2973 ((string= sB "incompat") nil)
2974 (t (string< sA sB)))))
2976 (defun package-menu--description-predicate (A B)
2977 (let ((dA (aref (cadr A) 3))
2978 (dB (aref (cadr B) 3)))
2979 (if (string= dA dB)
2980 (package-menu--name-predicate A B)
2981 (string< dA dB))))
2983 (defun package-menu--name-predicate (A B)
2984 (string< (symbol-name (package-desc-name (car A)))
2985 (symbol-name (package-desc-name (car B)))))
2987 (defun package-menu--archive-predicate (A B)
2988 (string< (or (package-desc-archive (car A)) "")
2989 (or (package-desc-archive (car B)) "")))
2991 (defvar package-menu--old-archive-contents nil
2992 "`package-archive-contents' before the latest refresh.")
2994 (defun package-menu--populate-new-package-list ()
2995 "Decide which packages are new in `package-archives-contents'.
2996 Store this list in `package-menu--new-package-list'."
2997 ;; Find which packages are new.
2998 (when package-menu--old-archive-contents
2999 (dolist (elt package-archive-contents)
3000 (unless (assq (car elt) package-menu--old-archive-contents)
3001 (push (car elt) package-menu--new-package-list)))
3002 (setq package-menu--old-archive-contents nil)))
3004 (defun package-menu--find-and-notify-upgrades ()
3005 "Notify the user of upgradable packages."
3006 (when-let ((upgrades (package-menu--find-upgrades)))
3007 (message "%d package%s can be upgraded; type `%s' to mark %s for upgrading."
3008 (length upgrades)
3009 (if (= (length upgrades) 1) "" "s")
3010 (substitute-command-keys "\\[package-menu-mark-upgrades]")
3011 (if (= (length upgrades) 1) "it" "them"))))
3013 (defun package-menu--post-refresh ()
3014 "Check for new packages, revert the *Packages* buffer, and check for upgrades.
3015 This function is called after `package-refresh-contents' and
3016 after `package-menu--perform-transaction'."
3017 (package-menu--populate-new-package-list)
3018 (let ((buf (get-buffer "*Packages*")))
3019 (when (buffer-live-p buf)
3020 (with-current-buffer buf
3021 (revert-buffer nil 'noconfirm))))
3022 (package-menu--find-and-notify-upgrades))
3024 (defcustom package-menu-async t
3025 "If non-nil, package-menu will use async operations when possible.
3026 This includes refreshing archive contents as well as installing
3027 packages."
3028 :type 'boolean
3029 :version "25.1"
3030 :group 'package)
3032 ;;;###autoload
3033 (defun list-packages (&optional no-fetch)
3034 "Display a list of packages.
3035 This first fetches the updated list of packages before
3036 displaying, unless a prefix argument NO-FETCH is specified.
3037 The list is displayed in a buffer named `*Packages*'."
3038 (interactive "P")
3039 (require 'finder-inf nil t)
3040 ;; Initialize the package system if necessary.
3041 (unless package--initialized
3042 (package-initialize t))
3043 ;; Integrate the package-menu with updating the archives.
3044 (add-hook 'package--post-download-archives-hook
3045 #'package-menu--post-refresh)
3047 ;; Generate the Package Menu.
3048 (let ((buf (get-buffer-create "*Packages*")))
3049 (with-current-buffer buf
3050 (package-menu-mode)
3052 ;; Fetch the remote list of packages.
3053 (unless no-fetch (package-menu-refresh))
3055 ;; If we're not async, this would be redundant.
3056 (when package-menu-async
3057 (package-menu--generate nil t)))
3058 ;; The package menu buffer has keybindings. If the user types
3059 ;; `M-x list-packages', that suggests it should become current.
3060 (switch-to-buffer buf)))
3062 ;;;###autoload
3063 (defalias 'package-list-packages 'list-packages)
3065 ;; Used in finder.el
3066 (defun package-show-package-list (&optional packages keywords)
3067 "Display PACKAGES in a *Packages* buffer.
3068 This is similar to `list-packages', but it does not fetch the
3069 updated list of packages, and it only displays packages with
3070 names in PACKAGES (which should be a list of symbols).
3072 When KEYWORDS are given, only packages with those KEYWORDS are
3073 shown."
3074 (interactive)
3075 (require 'finder-inf nil t)
3076 (let* ((buf (get-buffer-create "*Packages*"))
3077 (win (get-buffer-window buf)))
3078 (with-current-buffer buf
3079 (package-menu-mode)
3080 (package-menu--generate nil packages keywords))
3081 (if win
3082 (select-window win)
3083 (switch-to-buffer buf))))
3085 ;; package-menu--generate rebinds "q" on the fly, so we have to
3086 ;; hard-code the binding in the doc-string here.
3087 (defun package-menu-filter (keyword)
3088 "Filter the *Packages* buffer.
3089 Show only those items that relate to the specified KEYWORD.
3090 KEYWORD can be a string or a list of strings. If it is a list, a
3091 package will be displayed if it matches any of the keywords.
3092 Interactively, it is a list of strings separated by commas.
3094 To restore the full package list, type `q'."
3095 (interactive
3096 (list (completing-read-multiple
3097 "Keywords (comma separated): " (package-all-keywords))))
3098 (package-show-package-list t (if (stringp keyword)
3099 (list keyword)
3100 keyword)))
3102 (defun package-list-packages-no-fetch ()
3103 "Display a list of packages.
3104 Does not fetch the updated list of packages before displaying.
3105 The list is displayed in a buffer named `*Packages*'."
3106 (interactive)
3107 (list-packages t))
3109 (provide 'package)
3111 ;;; package.el ends here