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