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