package.el (package--append-to-alist): Renamed from `add-to-alist'
[emacs.git] / lisp / emacs-lisp / package.el
blob07ddddddb99f9767c8893ded95ed34d7f3f2b0de
1 ;;; package.el --- Simple package system for Emacs -*- lexical-binding:t -*-
3 ;; Copyright (C) 2007-2015 Free Software Foundation, Inc.
5 ;; Author: Tom Tromey <tromey@redhat.com>
6 ;; Daniel Hackney <dan@haxney.org>
7 ;; Created: 10 Mar 2007
8 ;; Version: 1.0.1
9 ;; Keywords: tools
10 ;; Package-Requires: ((tabulated-list "1.0"))
12 ;; This file is part of GNU Emacs.
14 ;; GNU Emacs is free software: you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation, either version 3 of the License, or
17 ;; (at your option) any later version.
19 ;; GNU Emacs is distributed in the hope that it will be useful,
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 ;; GNU General Public License for more details.
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
27 ;;; Change Log:
29 ;; 2 Apr 2007 - now using ChangeLog file
30 ;; 15 Mar 2007 - updated documentation
31 ;; 14 Mar 2007 - Changed how obsolete packages are handled
32 ;; 13 Mar 2007 - Wrote package-install-from-buffer
33 ;; 12 Mar 2007 - Wrote package-menu mode
35 ;;; Commentary:
37 ;; The idea behind package.el is to be able to download packages and
38 ;; install them. Packages are versioned and have versioned
39 ;; dependencies. Furthermore, this supports built-in packages which
40 ;; may or may not be newer than user-specified packages. This makes
41 ;; it possible to upgrade Emacs and automatically disable packages
42 ;; which have moved from external to core. (Note though that we don't
43 ;; currently register any of these, so this feature does not actually
44 ;; work.)
46 ;; A package is described by its name and version. The distribution
47 ;; format is either a tar file or a single .el file.
49 ;; A tar file should be named "NAME-VERSION.tar". The tar file must
50 ;; unpack into a directory named after the package and version:
51 ;; "NAME-VERSION". It must contain a file named "PACKAGE-pkg.el"
52 ;; which consists of a call to define-package. It may also contain a
53 ;; "dir" file and the info files it references.
55 ;; A .el file is named "NAME-VERSION.el" in the remote archive, but is
56 ;; installed as simply "NAME.el" in a directory named "NAME-VERSION".
58 ;; The downloader downloads all dependent packages. By default,
59 ;; packages come from the official GNU sources, but others may be
60 ;; added by customizing the `package-archives' alist. Packages get
61 ;; byte-compiled at install time.
63 ;; At activation time we will set up the load-path and the info path,
64 ;; and we will load the package's autoloads. If a package's
65 ;; dependencies are not available, we will not activate that package.
67 ;; Conceptually a package has multiple state transitions:
69 ;; * Download. Fetching the package from ELPA.
70 ;; * Install. Untar the package, or write the .el file, into
71 ;; ~/.emacs.d/elpa/ directory.
72 ;; * Byte compile. Currently this phase is done during install,
73 ;; but we may change this.
74 ;; * Activate. Evaluate the autoloads for the package to make it
75 ;; available to the user.
76 ;; * Load. Actually load the package and run some code from it.
78 ;; Other external functions you may want to use:
80 ;; M-x list-packages
81 ;; Enters a mode similar to buffer-menu which lets you manage
82 ;; packages. You can choose packages for install (mark with "i",
83 ;; then "x" to execute) or deletion (not implemented yet), and you
84 ;; can see what packages are available. This will automatically
85 ;; fetch the latest list of packages from ELPA.
87 ;; M-x package-install-from-buffer
88 ;; Install a package consisting of a single .el file that appears
89 ;; in the current buffer. This only works for packages which
90 ;; define a Version header properly; package.el also supports the
91 ;; extension headers Package-Version (in case Version is an RCS id
92 ;; or similar), and Package-Requires (if the package requires other
93 ;; packages).
95 ;; M-x package-install-file
96 ;; Install a package from the indicated file. The package can be
97 ;; either a tar file or a .el file. A tar file must contain an
98 ;; appropriately-named "-pkg.el" file; a .el file must be properly
99 ;; formatted as with package-install-from-buffer.
101 ;;; Thanks:
102 ;;; (sorted by sort-lines):
104 ;; Jim Blandy <jimb@red-bean.com>
105 ;; Karl Fogel <kfogel@red-bean.com>
106 ;; Kevin Ryde <user42@zip.com.au>
107 ;; Lawrence Mitchell
108 ;; Michael Olson <mwolson@member.fsf.org>
109 ;; Sebastian Tennant <sebyte@smolny.plus.com>
110 ;; Stefan Monnier <monnier@iro.umontreal.ca>
111 ;; Vinicius Jose Latorre <viniciusjl@ig.com.br>
112 ;; Phil Hagelberg <phil@hagelb.org>
114 ;;; ToDo:
116 ;; - putting info dirs at the start of the info path means
117 ;; users see a weird ordering of categories. OTOH we want to
118 ;; override later entries. maybe emacs needs to enforce
119 ;; the standard layout?
120 ;; - put bytecode in a separate directory tree
121 ;; - perhaps give users a way to recompile their bytecode
122 ;; or do it automatically when emacs changes
123 ;; - give users a way to know whether a package is installed ok
124 ;; - give users a way to view a package's documentation when it
125 ;; only appears in the .el
126 ;; - use/extend checkdoc so people can tell if their package will work
127 ;; - "installed" instead of a blank in the status column
128 ;; - tramp needs its files to be compiled in a certain order.
129 ;; how to handle this? fix tramp?
130 ;; - on emacs 21 we don't kill the -autoloads.el buffer. what about 22?
131 ;; - maybe we need separate .elc directories for various emacs versions
132 ;; and also emacs-vs-xemacs. That way conditional compilation can
133 ;; work. But would this break anything?
134 ;; - should store the package's keywords in archive-contents, then
135 ;; let the users filter the package-menu by keyword. See
136 ;; finder-by-keyword. (We could also let people view the
137 ;; Commentary, but it isn't clear how useful this is.)
138 ;; - William Xu suggests being able to open a package file without
139 ;; installing it
140 ;; - Interface with desktop.el so that restarting after an install
141 ;; works properly
142 ;; - Use hierarchical layout. PKG/etc PKG/lisp PKG/info
143 ;; ... except maybe lisp?
144 ;; - It may be nice to have a macro that expands to the package's
145 ;; private data dir, aka ".../etc". Or, maybe data-directory
146 ;; needs to be a list (though this would be less nice)
147 ;; a few packages want this, eg sokoban
148 ;; - package menu needs:
149 ;; ability to know which packages are built-in & thus not deletable
150 ;; it can sometimes print odd results, like 0.3 available but 0.4 active
151 ;; why is that?
152 ;; - Allow multiple versions on the server...?
153 ;; [ why bother? ]
154 ;; - Don't install a package which will invalidate dependencies overall
155 ;; - Allow something like (or (>= emacs 21.0) (>= xemacs 21.5))
156 ;; [ currently thinking, why bother.. KISS ]
157 ;; - Allow optional package dependencies
158 ;; then if we require 'bbdb', bbdb-specific lisp in lisp/bbdb
159 ;; and just don't compile to add to load path ...?
160 ;; - Our treatment of the info path is somewhat bogus
162 ;;; Code:
164 (eval-when-compile (require 'cl-lib))
165 (eval-when-compile (require 'epg)) ;For setf accessors.
167 (require 'tabulated-list)
168 (require 'macroexp)
170 (defgroup package nil
171 "Manager for Emacs Lisp packages."
172 :group 'applications
173 :version "24.1")
175 ;;;###autoload
176 (defcustom package-enable-at-startup t
177 "Whether to activate installed packages when Emacs starts.
178 If non-nil, packages are activated after reading the init file
179 and before `after-init-hook'. Activation is not done if
180 `user-init-file' is nil (e.g. Emacs was started with \"-q\").
182 Even if the value is nil, you can type \\[package-initialize] to
183 activate the package system at any time."
184 :type 'boolean
185 :group 'package
186 :version "24.1")
188 (defcustom package-load-list '(all)
189 "List of packages for `package-initialize' to load.
190 Each element in this list should be a list (NAME VERSION), or the
191 symbol `all'. The symbol `all' says to load the latest installed
192 versions of all packages not specified by other elements.
194 For an element (NAME VERSION), NAME is a package name (a symbol).
195 VERSION should be t, a string, or nil.
196 If VERSION is t, the most recent version is activated.
197 If VERSION is a string, only that version is ever loaded.
198 Any other version, even if newer, is silently ignored.
199 Hence, the package is \"held\" at that version.
200 If VERSION is nil, the package is not loaded (it is \"disabled\")."
201 :type '(repeat symbol)
202 :risky t
203 :group 'package
204 :version "24.1")
206 (defvar Info-directory-list)
207 (declare-function info-initialize "info" ())
208 (declare-function url-http-file-exists-p "url-http" (url))
209 (declare-function lm-header "lisp-mnt" (header))
210 (declare-function lm-commentary "lisp-mnt" (&optional file))
212 (defcustom package-archives '(("gnu" . "http://elpa.gnu.org/packages/"))
213 "An alist of archives from which to fetch.
214 The default value points to the GNU Emacs package repository.
216 Each element has the form (ID . LOCATION).
217 ID is an archive name, as a string.
218 LOCATION specifies the base location for the archive.
219 If it starts with \"http:\", it is treated as a HTTP URL;
220 otherwise it should be an absolute directory name.
221 (Other types of URL are currently not supported.)
223 Only add locations that you trust, since fetching and installing
224 a package can run arbitrary code."
225 :type '(alist :key-type (string :tag "Archive name")
226 :value-type (string :tag "URL or directory name"))
227 :risky t
228 :group 'package
229 :version "24.1")
231 (defcustom package-archive-priorities nil
232 "An alist of priorities for packages.
234 Each element has the form (ARCHIVE-ID . PRIORITY).
236 When installing packages, the package with the highest version
237 number from the archive with the highest priority is
238 selected. When higher versions are available from archives with
239 lower priorities, the user has to select those manually.
241 Archives not in this list have the priority 0."
242 :type '(alist :key-type (string :tag "Archive name")
243 :value-type (integer :tag "Priority (default is 0)"))
244 :risky t
245 :group 'package
246 :version "25.1")
248 (defcustom package-pinned-packages nil
249 "An alist of packages that are pinned to specific archives.
250 This can be useful if you have multiple package archives enabled,
251 and want to control which archive a given package gets installed from.
253 Each element of the alist has the form (PACKAGE . ARCHIVE), where:
254 PACKAGE is a symbol representing a package
255 ARCHIVE is a string representing an archive (it should be the car of
256 an element in `package-archives', e.g. \"gnu\").
258 Adding an entry to this variable means that only ARCHIVE will be
259 considered as a source for PACKAGE. If other archives provide PACKAGE,
260 they are ignored (for this package). If ARCHIVE does not contain PACKAGE,
261 the package will be unavailable."
262 :type '(alist :key-type (symbol :tag "Package")
263 :value-type (string :tag "Archive name"))
264 ;; I don't really see why this is risky...
265 ;; I suppose it could prevent you receiving updates for a package,
266 ;; via an entry (PACKAGE . NON-EXISTING). Which could be an issue
267 ;; if PACKAGE has a known vulnerability that is fixed in newer versions.
268 :risky t
269 :group 'package
270 :version "24.4")
272 (defconst package-archive-version 1
273 "Version number of the package archive understood by this file.
274 Lower version numbers than this will probably be understood as well.")
276 ;; We don't prime the cache since it tends to get out of date.
277 (defvar package-archive-contents nil
278 "Cache of the contents of the Emacs Lisp Package Archive.
279 This is an alist mapping package names (symbols) to
280 non-empty lists of `package-desc' structures.")
281 (put 'package-archive-contents 'risky-local-variable t)
283 (defcustom package-user-dir (locate-user-emacs-file "elpa")
284 "Directory containing the user's Emacs Lisp packages.
285 The directory name should be absolute.
286 Apart from this directory, Emacs also looks for system-wide
287 packages in `package-directory-list'."
288 :type 'directory
289 :risky t
290 :group 'package
291 :version "24.1")
293 (defcustom package-directory-list
294 ;; Defaults are subdirs named "elpa" in the site-lisp dirs.
295 (let (result)
296 (dolist (f load-path)
297 (and (stringp f)
298 (equal (file-name-nondirectory f) "site-lisp")
299 (push (expand-file-name "elpa" f) result)))
300 (nreverse result))
301 "List of additional directories containing Emacs Lisp packages.
302 Each directory name should be absolute.
304 These directories contain packages intended for system-wide; in
305 contrast, `package-user-dir' contains packages for personal use."
306 :type '(repeat directory)
307 :risky t
308 :group 'package
309 :version "24.1")
311 (defvar epg-gpg-program)
313 (defcustom package-check-signature
314 (if (progn (require 'epg-config) (executable-find epg-gpg-program))
315 'allow-unsigned)
316 "Non-nil means to check package signatures when installing.
317 The value `allow-unsigned' means to still install a package even if
318 it is unsigned.
320 This also applies to the \"archive-contents\" file that lists the
321 contents of the archive."
322 :type '(choice (const nil :tag "Never")
323 (const allow-unsigned :tag "Allow unsigned")
324 (const t :tag "Check always"))
325 :risky t
326 :group 'package
327 :version "24.4")
329 (defcustom package-unsigned-archives nil
330 "List of archives where we do not check for package signatures."
331 :type '(repeat (string :tag "Archive name"))
332 :risky t
333 :group 'package
334 :version "24.4")
336 (defvar package--default-summary "No description available.")
338 (cl-defstruct (package-desc
339 ;; Rename the default constructor from `make-package-desc'.
340 (:constructor package-desc-create)
341 ;; Has the same interface as the old `define-package',
342 ;; which is still used in the "foo-pkg.el" files. Extra
343 ;; options can be supported by adding additional keys.
344 (:constructor
345 package-desc-from-define
346 (name-string version-string &optional summary requirements
347 &rest rest-plist
348 &aux
349 (name (intern name-string))
350 (version (version-to-list version-string))
351 (reqs (mapcar #'(lambda (elt)
352 (list (car elt)
353 (version-to-list (cadr elt))))
354 (if (eq 'quote (car requirements))
355 (nth 1 requirements)
356 requirements)))
357 (kind (plist-get rest-plist :kind))
358 (archive (plist-get rest-plist :archive))
359 (extras (let (alist)
360 (while rest-plist
361 (unless (memq (car rest-plist) '(:kind :archive))
362 (let ((value (cadr rest-plist)))
363 (when value
364 (push (cons (car rest-plist)
365 (if (eq (car-safe value) 'quote)
366 (cadr value)
367 value))
368 alist))))
369 (setq rest-plist (cddr rest-plist)))
370 alist)))))
371 "Structure containing information about an individual package.
372 Slots:
374 `name' Name of the package, as a symbol.
376 `version' Version of the package, as a version list.
378 `summary' Short description of the package, typically taken from
379 the first line of the file.
381 `reqs' Requirements of the package. A list of (PACKAGE
382 VERSION-LIST) naming the dependent package and the minimum
383 required version.
385 `kind' The distribution format of the package. Currently, it is
386 either `single' or `tar'.
388 `archive' The name of the archive (as a string) whence this
389 package came.
391 `dir' The directory where the package is installed (if installed),
392 `builtin' if it is built-in, or nil otherwise.
394 `extras' Optional alist of additional keyword-value pairs.
396 `signed' Flag to indicate that the package is signed by provider."
397 name
398 version
399 (summary package--default-summary)
400 reqs
401 kind
402 archive
404 extras
405 signed)
407 ;; Pseudo fields.
408 (defun package-desc-full-name (pkg-desc)
409 (format "%s-%s"
410 (package-desc-name pkg-desc)
411 (package-version-join (package-desc-version pkg-desc))))
413 (defun package-desc-suffix (pkg-desc)
414 (pcase (package-desc-kind pkg-desc)
415 (`single ".el")
416 (`tar ".tar")
417 (`dir "")
418 (kind (error "Unknown package kind: %s" kind))))
420 (defun package-desc--keywords (pkg-desc)
421 (let ((keywords (cdr (assoc :keywords (package-desc-extras pkg-desc)))))
422 (if (eq (car-safe keywords) 'quote)
423 (nth 1 keywords)
424 keywords)))
426 ;; Package descriptor format used in finder-inf.el and package--builtins.
427 (cl-defstruct (package--bi-desc
428 (:constructor package-make-builtin (version summary))
429 (:type vector))
430 version
431 reqs
432 summary)
434 (defvar package--builtins nil
435 "Alist of built-in packages.
436 The actual value is initialized by loading the library
437 `finder-inf'; this is not done until it is needed, e.g. by the
438 function `package-built-in-p'.
440 Each element has the form (PKG . PACKAGE-BI-DESC), where PKG is a package
441 name (a symbol) and DESC is a `package--bi-desc' structure.")
442 (put 'package--builtins 'risky-local-variable t)
444 (defvar package-alist nil
445 "Alist of all packages available for activation.
446 Each element has the form (PKG . DESCS), where PKG is a package
447 name (a symbol) and DESCS is a non-empty list of `package-desc' structure,
448 sorted by decreasing versions.
450 This variable is set automatically by `package-load-descriptor',
451 called via `package-initialize'. To change which packages are
452 loaded and/or activated, customize `package-load-list'.")
453 (put 'package-alist 'risky-local-variable t)
455 (defvar package-activated-list nil
456 ;; FIXME: This should implicitly include all builtin packages.
457 "List of the names of currently activated packages.")
458 (put 'package-activated-list 'risky-local-variable t)
460 (defun package-version-join (vlist)
461 "Return the version string corresponding to the list VLIST.
462 This is, approximately, the inverse of `version-to-list'.
463 \(Actually, it returns only one of the possible inverses, since
464 `version-to-list' is a many-to-one operation.)"
465 (if (null vlist)
467 (let ((str-list (list "." (int-to-string (car vlist)))))
468 (dolist (num (cdr vlist))
469 (cond
470 ((>= num 0)
471 (push (int-to-string num) str-list)
472 (push "." str-list))
473 ((< num -4)
474 (error "Invalid version list `%s'" vlist))
476 ;; pre, or beta, or alpha
477 (cond ((equal "." (car str-list))
478 (pop str-list))
479 ((not (string-match "[0-9]+" (car str-list)))
480 (error "Invalid version list `%s'" vlist)))
481 (push (cond ((= num -1) "pre")
482 ((= num -2) "beta")
483 ((= num -3) "alpha")
484 ((= num -4) "snapshot"))
485 str-list))))
486 (if (equal "." (car str-list))
487 (pop str-list))
488 (apply 'concat (nreverse str-list)))))
490 (defun package-load-descriptor (pkg-dir)
491 "Load the description file in directory PKG-DIR."
492 (let ((pkg-file (expand-file-name (package--description-file pkg-dir)
493 pkg-dir))
494 (signed-file (concat pkg-dir ".signed")))
495 (when (file-exists-p pkg-file)
496 (with-temp-buffer
497 (insert-file-contents pkg-file)
498 (goto-char (point-min))
499 (let ((pkg-desc (package-process-define-package
500 (read (current-buffer)) pkg-file)))
501 (setf (package-desc-dir pkg-desc) pkg-dir)
502 (if (file-exists-p signed-file)
503 (setf (package-desc-signed pkg-desc) t))
504 pkg-desc)))))
506 (defun package-load-all-descriptors ()
507 "Load descriptors for installed Emacs Lisp packages.
508 This looks for package subdirectories in `package-user-dir' and
509 `package-directory-list'. The variable `package-load-list'
510 controls which package subdirectories may be loaded.
512 In each valid package subdirectory, this function loads the
513 description file containing a call to `define-package', which
514 updates `package-alist'."
515 (dolist (dir (cons package-user-dir package-directory-list))
516 (when (file-directory-p dir)
517 (dolist (subdir (directory-files dir))
518 (let ((pkg-dir (expand-file-name subdir dir)))
519 (when (file-directory-p pkg-dir)
520 (package-load-descriptor pkg-dir)))))))
522 (defun package-disabled-p (pkg-name version)
523 "Return whether PKG-NAME at VERSION can be activated.
524 The decision is made according to `package-load-list'.
525 Return nil if the package can be activated.
526 Return t if the package is completely disabled.
527 Return the max version (as a string) if the package is held at a lower version."
528 (let ((force (assq pkg-name package-load-list)))
529 (cond ((null force) (not (memq 'all package-load-list)))
530 ((null (setq force (cadr force))) t) ; disabled
531 ((eq force t) nil)
532 ((stringp force) ; held
533 (unless (version-list-= version (version-to-list force))
534 force))
535 (t (error "Invalid element in `package-load-list'")))))
537 (defun package-activate-1 (pkg-desc &optional reload)
538 "Activate package given by PKG-DESC, even if it was already active.
539 If RELOAD is non-nil, also `load' any files inside the package which
540 correspond to previously loaded files (those returned by
541 `package--list-loaded-files')."
542 (let* ((name (package-desc-name pkg-desc))
543 (pkg-dir (package-desc-dir pkg-desc))
544 (pkg-dir-dir (file-name-as-directory pkg-dir)))
545 (unless pkg-dir
546 (error "Internal error: unable to find directory for `%s'"
547 (package-desc-full-name pkg-desc)))
548 ;; Add to load path, add autoloads, and activate the package.
549 (let* ((old-lp load-path)
550 (autoloads-file (expand-file-name
551 (format "%s-autoloads" name) pkg-dir))
552 (loaded-files-list (and reload (package--list-loaded-files pkg-dir))))
553 (with-demoted-errors "Error in package-activate-1: %s"
554 (load autoloads-file nil t))
555 (when (and (eq old-lp load-path)
556 (not (or (member pkg-dir load-path)
557 (member pkg-dir-dir load-path))))
558 ;; Old packages don't add themselves to the `load-path', so we have to
559 ;; do it ourselves.
560 (push pkg-dir load-path))
561 ;; Call `load' on all files in `pkg-dir' already present in
562 ;; `load-history'. This is done so that macros in these files are updated
563 ;; to their new definitions. If another package is being installed which
564 ;; depends on this new definition, not doing this update would cause
565 ;; compilation errors and break the installation.
566 (with-demoted-errors "Error in package-activate-1: %s"
567 (mapc (lambda (feature) (load feature nil t))
568 ;; Skip autoloads file since we already evaluated it above.
569 (remove (file-truename autoloads-file) loaded-files-list))))
570 ;; Add info node.
571 (when (file-exists-p (expand-file-name "dir" pkg-dir))
572 ;; FIXME: not the friendliest, but simple.
573 (require 'info)
574 (info-initialize)
575 (push pkg-dir Info-directory-list))
576 (push name package-activated-list)
577 ;; Don't return nil.
580 (declare-function find-library-name "find-func" (library))
581 (defun package--list-loaded-files (dir)
582 "Recursively list all files in DIR which correspond to loaded features.
583 Returns the `file-name-sans-extension' of each file, relative to
584 DIR, sorted by most recently loaded last."
585 (let* ((history (delq nil
586 (mapcar (lambda (x)
587 (let ((f (car x)))
588 (and f (file-name-sans-extension f))))
589 load-history)))
590 (dir (file-truename dir))
591 ;; List all files that have already been loaded.
592 (list-of-conflicts
593 (delq
595 (mapcar
596 (lambda (x) (let* ((file (file-relative-name x dir))
597 ;; Previously loaded file, if any.
598 (previous
599 (ignore-errors
600 (file-name-sans-extension
601 (file-truename (find-library-name file)))))
602 (pos (when previous (member previous history))))
603 ;; Return (RELATIVE-FILENAME . HISTORY-POSITION)
604 (when pos
605 (cons (file-name-sans-extension file) (length pos)))))
606 (directory-files-recursively dir "\\`[^\\.].*\\.el\\'")))))
607 ;; Turn the list of (FILENAME . POS) back into a list of features. Files in
608 ;; subdirectories are returned relative to DIR (so not actually features).
609 (let ((default-directory (file-name-as-directory dir)))
610 (mapcar (lambda (x) (file-truename (car x)))
611 (sort list-of-conflicts
612 ;; Sort the files by ascending HISTORY-POSITION.
613 (lambda (x y) (< (cdr x) (cdr y))))))))
615 (defun package-built-in-p (package &optional min-version)
616 "Return true if PACKAGE is built-in to Emacs.
617 Optional arg MIN-VERSION, if non-nil, should be a version list
618 specifying the minimum acceptable version."
619 (if (package-desc-p package) ;; was built-in and then was converted
620 (eq 'builtin (package-desc-dir package))
621 (let ((bi (assq package package--builtin-versions)))
622 (cond
623 (bi (version-list-<= min-version (cdr bi)))
624 ((remove 0 min-version) nil)
626 (require 'finder-inf nil t) ; For `package--builtins'.
627 (assq package package--builtins))))))
629 (defun package--from-builtin (bi-desc)
630 (package-desc-create :name (pop bi-desc)
631 :version (package--bi-desc-version bi-desc)
632 :summary (package--bi-desc-summary bi-desc)
633 :dir 'builtin))
635 ;; This function goes ahead and activates a newer version of a package
636 ;; if an older one was already activated. This is not ideal; we'd at
637 ;; least need to check to see if the package has actually been loaded,
638 ;; and not merely activated.
639 (defun package-activate (package &optional force)
640 "Activate package PACKAGE.
641 If FORCE is true, (re-)activate it if it's already activated."
642 (let ((pkg-descs (cdr (assq package package-alist))))
643 ;; Check if PACKAGE is available in `package-alist'.
644 (while
645 (when pkg-descs
646 (let ((available-version (package-desc-version (car pkg-descs))))
647 (or (package-disabled-p package available-version)
648 ;; Prefer a builtin package.
649 (package-built-in-p package available-version))))
650 (setq pkg-descs (cdr pkg-descs)))
651 (cond
652 ;; If no such package is found, maybe it's built-in.
653 ((null pkg-descs)
654 (package-built-in-p package))
655 ;; If the package is already activated, just return t.
656 ((and (memq package package-activated-list) (not force))
658 ;; Otherwise, proceed with activation.
660 (let* ((pkg-vec (car pkg-descs))
661 (fail (catch 'dep-failure
662 ;; Activate its dependencies recursively.
663 (dolist (req (package-desc-reqs pkg-vec))
664 (unless (package-activate (car req))
665 (throw 'dep-failure req))))))
666 (if fail
667 (warn "Unable to activate package `%s'.
668 Required package `%s-%s' is unavailable"
669 package (car fail) (package-version-join (cadr fail)))
670 ;; If all goes well, activate the package itself.
671 (package-activate-1 pkg-vec force)))))))
673 (defun define-package (_name-string _version-string
674 &optional _docstring _requirements
675 &rest _extra-properties)
676 "Define a new package.
677 NAME-STRING is the name of the package, as a string.
678 VERSION-STRING is the version of the package, as a string.
679 DOCSTRING is a short description of the package, a string.
680 REQUIREMENTS is a list of dependencies on other packages.
681 Each requirement is of the form (OTHER-PACKAGE OTHER-VERSION),
682 where OTHER-VERSION is a string.
684 EXTRA-PROPERTIES is currently unused."
685 ;; FIXME: Placeholder! Should we keep it?
686 (error "Don't call me!"))
688 (defun package-process-define-package (exp origin)
689 (unless (eq (car-safe exp) 'define-package)
690 (error "Can't find define-package in %s" origin))
691 (let* ((new-pkg-desc (apply #'package-desc-from-define (cdr exp)))
692 (name (package-desc-name new-pkg-desc))
693 (version (package-desc-version new-pkg-desc))
694 (old-pkgs (assq name package-alist)))
695 (if (null old-pkgs)
696 ;; If there's no old package, just add this to `package-alist'.
697 (push (list name new-pkg-desc) package-alist)
698 ;; If there is, insert the new package at the right place in the list.
699 (while
700 (if (and (cdr old-pkgs)
701 (version-list-< version
702 (package-desc-version (cadr old-pkgs))))
703 (setq old-pkgs (cdr old-pkgs))
704 (push new-pkg-desc (cdr old-pkgs))
705 nil)))
706 new-pkg-desc))
708 ;; From Emacs 22, but changed so it adds to load-path.
709 (defun package-autoload-ensure-default-file (file)
710 "Make sure that the autoload file FILE exists and if not create it."
711 (unless (file-exists-p file)
712 (write-region
713 (concat ";;; " (file-name-nondirectory file)
714 " --- automatically extracted autoloads\n"
715 ";;\n"
716 ";;; Code:\n"
717 "(add-to-list 'load-path (or (file-name-directory #$) (car load-path)))\n"
718 "\f\n;; Local Variables:\n"
719 ";; version-control: never\n"
720 ";; no-byte-compile: t\n"
721 ";; no-update-autoloads: t\n"
722 ";; End:\n"
723 ";;; " (file-name-nondirectory file)
724 " ends here\n")
725 nil file nil 'silent))
726 file)
728 (defvar generated-autoload-file)
729 (defvar version-control)
731 (defun package-generate-autoloads (name pkg-dir)
732 (let* ((auto-name (format "%s-autoloads.el" name))
733 ;;(ignore-name (concat name "-pkg.el"))
734 (generated-autoload-file (expand-file-name auto-name pkg-dir))
735 (backup-inhibited t)
736 (version-control 'never))
737 (package-autoload-ensure-default-file generated-autoload-file)
738 (update-directory-autoloads pkg-dir)
739 (let ((buf (find-buffer-visiting generated-autoload-file)))
740 (when buf (kill-buffer buf)))
741 auto-name))
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)
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))
767 (defun package-generate-description-file (pkg-desc pkg-file)
768 "Create the foo-pkg.el file for single-file packages."
769 (let* ((name (package-desc-name pkg-desc)))
770 (let ((print-level nil)
771 (print-quoted t)
772 (print-length nil))
773 (write-region
774 (concat
775 ";;; -*- no-byte-compile: t -*-\n"
776 (prin1-to-string
777 (nconc
778 (list 'define-package
779 (symbol-name name)
780 (package-version-join (package-desc-version pkg-desc))
781 (package-desc-summary pkg-desc)
782 (let ((requires (package-desc-reqs pkg-desc)))
783 (list 'quote
784 ;; Turn version lists into string form.
785 (mapcar
786 (lambda (elt)
787 (list (car elt)
788 (package-version-join (cadr elt))))
789 requires))))
790 (package--alist-to-plist-args
791 (package-desc-extras pkg-desc))))
792 "\n")
793 nil pkg-file nil 'silent))))
795 (defun package--alist-to-plist-args (alist)
796 (mapcar 'macroexp-quote
797 (apply #'nconc
798 (mapcar (lambda (pair) (list (car pair) (cdr pair))) alist))))
799 (defun package-unpack (pkg-desc)
800 "Install the contents of the current buffer as a package."
801 (let* ((name (package-desc-name pkg-desc))
802 (dirname (package-desc-full-name pkg-desc))
803 (pkg-dir (expand-file-name dirname package-user-dir)))
804 (pcase (package-desc-kind pkg-desc)
805 (`dir
806 (make-directory pkg-dir t)
807 (let ((file-list
808 (directory-files
809 default-directory 'full "\\`[^.].*\\.el\\'" 'nosort)))
810 (dolist (source-file file-list)
811 (let ((target-el-file
812 (expand-file-name (file-name-nondirectory source-file) pkg-dir)))
813 (copy-file source-file target-el-file t)))
814 ;; Now that the files have been installed, this package is
815 ;; indistinguishable from a `tar' or a `single'. Let's make
816 ;; things simple by ensuring we're one of them.
817 (setf (package-desc-kind pkg-desc)
818 (if (> (length file-list) 1) 'tar 'single))))
819 (`tar
820 (make-directory package-user-dir t)
821 ;; FIXME: should we delete PKG-DIR if it exists?
822 (let* ((default-directory (file-name-as-directory package-user-dir)))
823 (package-untar-buffer dirname)))
824 (`single
825 (let ((el-file (expand-file-name (format "%s.el" name) pkg-dir)))
826 (make-directory pkg-dir t)
827 (package--write-file-no-coding el-file)))
828 (kind (error "Unknown package kind: %S" kind)))
829 (package--make-autoloads-and-stuff pkg-desc pkg-dir)
830 ;; Update package-alist.
831 (let ((new-desc (package-load-descriptor pkg-dir)))
832 ;; FIXME: Check that `new-desc' matches `desc'!
833 ;; FIXME: Compilation should be done as a separate, optional, step.
834 ;; E.g. for multi-package installs, we should first install all packages
835 ;; and then compile them.
836 (package--compile new-desc))
837 ;; Try to activate it.
838 (package-activate name 'force)
839 pkg-dir))
841 (defun package--make-autoloads-and-stuff (pkg-desc pkg-dir)
842 "Generate autoloads, description file, etc.. for PKG-DESC installed at PKG-DIR."
843 (package-generate-autoloads (package-desc-name pkg-desc) pkg-dir)
844 (let ((desc-file (expand-file-name (package--description-file pkg-dir)
845 pkg-dir)))
846 (unless (file-exists-p desc-file)
847 (package-generate-description-file pkg-desc desc-file)))
848 ;; FIXME: Create foo.info and dir file from foo.texi?
851 (defun package--compile (pkg-desc)
852 "Byte-compile installed package PKG-DESC."
853 (package-activate-1 pkg-desc)
854 (byte-recompile-directory (package-desc-dir pkg-desc) 0 t))
856 (defun package--write-file-no-coding (file-name)
857 (let ((buffer-file-coding-system 'no-conversion))
858 (write-region (point-min) (point-max) file-name nil 'silent)))
860 (defmacro package--with-work-buffer (location file &rest body)
861 "Run BODY in a buffer containing the contents of FILE at LOCATION.
862 LOCATION is the base location of a package archive, and should be
863 one of the URLs (or file names) specified in `package-archives'.
864 FILE is the name of a file relative to that base location.
866 This macro retrieves FILE from LOCATION into a temporary buffer,
867 and evaluates BODY while that buffer is current. This work
868 buffer is killed afterwards. Return the last value in BODY."
869 (declare (indent 2) (debug t))
870 `(with-temp-buffer
871 (if (string-match-p "\\`https?:" ,location)
872 (url-insert-file-contents (concat ,location ,file))
873 (unless (file-name-absolute-p ,location)
874 (error "Archive location %s is not an absolute file name"
875 ,location))
876 (insert-file-contents (expand-file-name ,file ,location)))
877 ,@body))
879 (defun package--archive-file-exists-p (location file)
880 (let ((http (string-match "\\`https?:" location)))
881 (if http
882 (progn
883 (require 'url-http)
884 (url-http-file-exists-p (concat location file)))
885 (file-exists-p (expand-file-name file location)))))
887 (declare-function epg-make-context "epg"
888 (&optional protocol armor textmode include-certs
889 cipher-algorithm
890 digest-algorithm
891 compress-algorithm))
892 (declare-function epg-verify-string "epg" (context signature
893 &optional signed-text))
894 (declare-function epg-context-result-for "epg" (context name))
895 (declare-function epg-signature-status "epg" (signature))
896 (declare-function epg-signature-to-string "epg" (signature))
898 (defun package--display-verify-error (context sig-file)
899 (unless (equal (epg-context-error-output context) "")
900 (with-output-to-temp-buffer "*Error*"
901 (with-current-buffer standard-output
902 (if (epg-context-result-for context 'verify)
903 (insert (format "Failed to verify signature %s:\n" sig-file)
904 (mapconcat #'epg-signature-to-string
905 (epg-context-result-for context 'verify)
906 "\n"))
907 (insert (format "Error while verifying signature %s:\n" sig-file)))
908 (insert "\nCommand output:\n" (epg-context-error-output context))))))
910 (defun package--check-signature (location file)
911 "Check signature of the current buffer.
912 GnuPG keyring is located under \"gnupg\" in `package-user-dir'."
913 (let* ((context (epg-make-context 'OpenPGP))
914 (homedir (expand-file-name "gnupg" package-user-dir))
915 (sig-file (concat file ".sig"))
916 (sig-content (package--with-work-buffer location sig-file
917 (buffer-string))))
918 (setf (epg-context-home-directory context) homedir)
919 (condition-case error
920 (epg-verify-string context sig-content (buffer-string))
921 (error
922 (package--display-verify-error context sig-file)
923 (signal (car error) (cdr error))))
924 (let (good-signatures had-fatal-error)
925 ;; The .sig file may contain multiple signatures. Success if one
926 ;; of the signatures is good.
927 (dolist (sig (epg-context-result-for context 'verify))
928 (if (eq (epg-signature-status sig) 'good)
929 (push sig good-signatures)
930 ;; If package-check-signature is allow-unsigned, don't
931 ;; signal error when we can't verify signature because of
932 ;; missing public key. Other errors are still treated as
933 ;; fatal (bug#17625).
934 (unless (and (eq package-check-signature 'allow-unsigned)
935 (eq (epg-signature-status sig) 'no-pubkey))
936 (setq had-fatal-error t))))
937 (when (and (null good-signatures) had-fatal-error)
938 (package--display-verify-error context sig-file)
939 (error "Failed to verify signature %s" sig-file))
940 good-signatures)))
942 (defun package-install-from-archive (pkg-desc)
943 "Download and install a tar package."
944 ;; This won't happen, unless the archive is doing something wrong.
945 (when (eq (package-desc-kind pkg-desc) 'dir)
946 (error "Can't install directory package from archive"))
947 (let* ((location (package-archive-base pkg-desc))
948 (file (concat (package-desc-full-name pkg-desc)
949 (package-desc-suffix pkg-desc)))
950 (sig-file (concat file ".sig"))
951 good-signatures pkg-descs)
952 (package--with-work-buffer location file
953 (if (and package-check-signature
954 (not (member (package-desc-archive pkg-desc)
955 package-unsigned-archives)))
956 (if (package--archive-file-exists-p location sig-file)
957 (setq good-signatures (package--check-signature location file))
958 (unless (eq package-check-signature 'allow-unsigned)
959 (error "Unsigned package: `%s'"
960 (package-desc-name pkg-desc)))))
961 (package-unpack pkg-desc))
962 ;; Here the package has been installed successfully, mark it as
963 ;; signed if appropriate.
964 (when good-signatures
965 ;; Write out good signatures into NAME-VERSION.signed file.
966 (write-region (mapconcat #'epg-signature-to-string good-signatures "\n")
968 (expand-file-name
969 (concat (package-desc-full-name pkg-desc)
970 ".signed")
971 package-user-dir)
972 nil 'silent)
973 ;; Update the old pkg-desc which will be shown on the description buffer.
974 (setf (package-desc-signed pkg-desc) t)
975 ;; Update the new (activated) pkg-desc as well.
976 (setq pkg-descs (cdr (assq (package-desc-name pkg-desc) package-alist)))
977 (if pkg-descs
978 (setf (package-desc-signed (car pkg-descs)) t)))))
980 (defvar package--initialized nil)
982 (defun package-installed-p (package &optional min-version)
983 "Return true if PACKAGE, of MIN-VERSION or newer, is installed.
984 MIN-VERSION should be a version list."
985 (unless package--initialized (error "package.el is not yet initialized!"))
987 (let ((pkg-descs (cdr (assq package package-alist))))
988 (and pkg-descs
989 (version-list-<= min-version
990 (package-desc-version (car pkg-descs)))))
991 ;; Also check built-in packages.
992 (package-built-in-p package min-version)))
994 (defun package-compute-transaction (packages requirements &optional seen)
995 "Return a list of packages to be installed, including PACKAGES.
996 PACKAGES should be a list of `package-desc'.
998 REQUIREMENTS should be a list of additional requirements; each
999 element in this list should have the form (PACKAGE VERSION-LIST),
1000 where PACKAGE is a package name and VERSION-LIST is the required
1001 version of that package.
1003 This function recursively computes the requirements of the
1004 packages in REQUIREMENTS, and returns a list of all the packages
1005 that must be installed. Packages that are already installed are
1006 not included in this list.
1008 SEEN is used internally to detect infinite recursion."
1009 ;; FIXME: We really should use backtracking to explore the whole
1010 ;; search space (e.g. if foo require bar-1.3, and bar-1.4 requires toto-1.1
1011 ;; whereas bar-1.3 requires toto-1.0 and the user has put a hold on toto-1.0:
1012 ;; the current code might fail to see that it could install foo by using the
1013 ;; older bar-1.3).
1014 (dolist (elt requirements)
1015 (let* ((next-pkg (car elt))
1016 (next-version (cadr elt))
1017 (already ()))
1018 (dolist (pkg packages)
1019 (if (eq next-pkg (package-desc-name pkg))
1020 (setq already pkg)))
1021 (when already
1022 (if (version-list-<= next-version (package-desc-version already))
1023 ;; `next-pkg' is already in `packages', but its position there
1024 ;; means it might be installed too late: remove it from there, so
1025 ;; we re-add it (along with its dependencies) at an earlier place
1026 ;; below (bug#16994).
1027 (if (memq already seen) ;Avoid inf-loop on dependency cycles.
1028 (message "Dependency cycle going through %S"
1029 (package-desc-full-name already))
1030 (setq packages (delq already packages))
1031 (setq already nil))
1032 (error "Need package `%s-%s', but only %s is being installed"
1033 next-pkg (package-version-join next-version)
1034 (package-version-join (package-desc-version already)))))
1035 (cond
1036 (already nil)
1037 ((package-installed-p next-pkg next-version) nil)
1040 ;; A package is required, but not installed. It might also be
1041 ;; blocked via `package-load-list'.
1042 (let ((pkg-descs (cdr (assq next-pkg package-archive-contents)))
1043 (found nil)
1044 (problem nil))
1045 (while (and pkg-descs (not found))
1046 (let* ((pkg-desc (pop pkg-descs))
1047 (version (package-desc-version pkg-desc))
1048 (disabled (package-disabled-p next-pkg version)))
1049 (cond
1050 ((version-list-< version next-version)
1051 (error
1052 "Need package `%s-%s', but only %s is available"
1053 next-pkg (package-version-join next-version)
1054 (package-version-join version)))
1055 (disabled
1056 (unless problem
1057 (setq problem
1058 (if (stringp disabled)
1059 (format "Package `%s' held at version %s, \
1060 but version %s required"
1061 next-pkg disabled
1062 (package-version-join next-version))
1063 (format "Required package '%s' is disabled"
1064 next-pkg)))))
1065 (t (setq found pkg-desc)))))
1066 (unless found
1067 (if problem
1068 (error "%s" problem)
1069 (error "Package `%s-%s' is unavailable"
1070 next-pkg (package-version-join next-version))))
1071 (setq packages
1072 (package-compute-transaction (cons found packages)
1073 (package-desc-reqs found)
1074 (cons found seen))))))))
1075 packages)
1077 (defun package-read-from-string (str)
1078 "Read a Lisp expression from STR.
1079 Signal an error if the entire string was not used."
1080 (let* ((read-data (read-from-string str))
1081 (more-left
1082 (condition-case nil
1083 ;; The call to `ignore' suppresses a compiler warning.
1084 (progn (ignore (read-from-string
1085 (substring str (cdr read-data))))
1087 (end-of-file nil))))
1088 (if more-left
1089 (error "Can't read whole string")
1090 (car read-data))))
1092 (defun package--read-archive-file (file)
1093 "Re-read archive file FILE, if it exists.
1094 Will return the data from the file, or nil if the file does not exist.
1095 Will throw an error if the archive version is too new."
1096 (let ((filename (expand-file-name file package-user-dir)))
1097 (when (file-exists-p filename)
1098 (with-temp-buffer
1099 (insert-file-contents-literally filename)
1100 (let ((contents (read (current-buffer))))
1101 (if (> (car contents) package-archive-version)
1102 (error "Package archive version %d is higher than %d"
1103 (car contents) package-archive-version))
1104 (cdr contents))))))
1106 (defun package-read-all-archive-contents ()
1107 "Re-read `archive-contents', if it exists.
1108 If successful, set `package-archive-contents'."
1109 (setq package-archive-contents nil)
1110 (dolist (archive package-archives)
1111 (package-read-archive-contents (car archive))))
1113 (defun package-read-archive-contents (archive)
1114 "Re-read archive contents for ARCHIVE.
1115 If successful, set the variable `package-archive-contents'.
1116 If the archive version is too new, signal an error."
1117 ;; Version 1 of 'archive-contents' is identical to our internal
1118 ;; representation.
1119 (let* ((contents-file (format "archives/%s/archive-contents" archive))
1120 (contents (package--read-archive-file contents-file)))
1121 (when contents
1122 (dolist (package contents)
1123 (package--add-to-archive-contents package archive)))))
1125 ;; Package descriptor objects used inside the "archive-contents" file.
1126 ;; Changing this defstruct implies changing the format of the
1127 ;; "archive-contents" files.
1128 (cl-defstruct (package--ac-desc
1129 (:constructor package-make-ac-desc (version reqs summary kind extras))
1130 (:copier nil)
1131 (:type vector))
1132 version reqs summary kind extras)
1134 (defun package--add-to-archive-contents (package archive)
1135 "Add the PACKAGE from the given ARCHIVE if necessary.
1136 PACKAGE should have the form (NAME . PACKAGE--AC-DESC).
1137 Also, add the originating archive to the `package-desc' structure."
1138 (let* ((name (car package))
1139 (version (package--ac-desc-version (cdr package)))
1140 (pkg-desc
1141 (package-desc-create
1142 :name name
1143 :version version
1144 :reqs (package--ac-desc-reqs (cdr package))
1145 :summary (package--ac-desc-summary (cdr package))
1146 :kind (package--ac-desc-kind (cdr package))
1147 :archive archive
1148 :extras (and (> (length (cdr package)) 4)
1149 ;; Older archive-contents files have only 4
1150 ;; elements here.
1151 (package--ac-desc-extras (cdr package)))))
1152 (pinned-to-archive (assoc name package-pinned-packages)))
1153 ;; Skip entirely if pinned to another archive.
1154 (when (not (and pinned-to-archive
1155 (not (equal (cdr pinned-to-archive) archive))))
1156 (setq package-archive-contents
1157 (package--append-to-alist pkg-desc package-archive-contents)))))
1159 (defun package--append-to-alist (pkg-desc alist)
1160 "Add PKG-DESC to ALIST.
1162 Packages are grouped by name. The package descriptions are sorted
1163 by version number."
1164 (let* ((name (package-desc-name pkg-desc))
1165 (priority-version (package-desc-priority-version pkg-desc))
1166 (existing-packages (assq name alist)))
1167 (if (not existing-packages)
1168 (cons (list name pkg-desc)
1169 alist)
1170 (while (if (and (cdr existing-packages)
1171 (version-list-< priority-version
1172 (package-desc-priority-version
1173 (cadr existing-packages))))
1174 (setq existing-packages (cdr existing-packages))
1175 (push pkg-desc (cdr existing-packages))
1176 nil))
1177 alist)))
1179 (defun package-download-transaction (packages)
1180 "Download and install all the packages in PACKAGES.
1181 PACKAGES should be a list of package-desc.
1182 This function assumes that all package requirements in
1183 PACKAGES are satisfied, i.e. that PACKAGES is computed
1184 using `package-compute-transaction'."
1185 (mapc #'package-install-from-archive packages))
1187 ;;;###autoload
1188 (defun package-install (pkg)
1189 "Install the package PKG.
1190 PKG can be a package-desc or the package name of one the available packages
1191 in an archive in `package-archives'. Interactively, prompt for its name."
1192 (interactive
1193 (progn
1194 ;; Initialize the package system to get the list of package
1195 ;; symbols for completion.
1196 (unless package--initialized
1197 (package-initialize t))
1198 (unless package-archive-contents
1199 (package-refresh-contents))
1200 (list (intern (completing-read
1201 "Install package: "
1202 (delq nil
1203 (mapcar (lambda (elt)
1204 (unless (package-installed-p (car elt))
1205 (symbol-name (car elt))))
1206 package-archive-contents))
1207 nil t)))))
1208 (package-download-transaction
1209 (if (package-desc-p pkg)
1210 (package-compute-transaction (list pkg)
1211 (package-desc-reqs pkg))
1212 (package-compute-transaction ()
1213 (list (list pkg))))))
1215 (defun package-strip-rcs-id (str)
1216 "Strip RCS version ID from the version string STR.
1217 If the result looks like a dotted numeric version, return it.
1218 Otherwise return nil."
1219 (when str
1220 (when (string-match "\\`[ \t]*[$]Revision:[ \t]+" str)
1221 (setq str (substring str (match-end 0))))
1222 (condition-case nil
1223 (if (version-to-list str)
1224 str)
1225 (error nil))))
1227 (declare-function lm-homepage "lisp-mnt" (&optional file))
1229 (defun package--prepare-dependencies (deps)
1230 "Turn DEPS into an acceptable list of dependencies.
1232 Any parts missing a version string get a default version string
1233 of \"0\" (meaning any version) and an appropriate level of lists
1234 is wrapped around any parts requiring it."
1235 (cond
1236 ((not (listp deps))
1237 (error "Invalid requirement specifier: %S" deps))
1238 (t (mapcar (lambda (dep)
1239 (cond
1240 ((symbolp dep) `(,dep "0"))
1241 ((stringp dep)
1242 (error "Invalid requirement specifier: %S" dep))
1243 ((and (listp dep) (null (cdr dep)))
1244 (list (car dep) "0"))
1245 (t dep)))
1246 deps))))
1248 (defun package-buffer-info ()
1249 "Return a `package-desc' describing the package in the current buffer.
1251 If the buffer does not contain a conforming package, signal an
1252 error. If there is a package, narrow the buffer to the file's
1253 boundaries."
1254 (goto-char (point-min))
1255 (unless (re-search-forward "^;;; \\([^ ]*\\)\\.el ---[ \t]*\\(.*?\\)[ \t]*\\(-\\*-.*-\\*-[ \t]*\\)?$" nil t)
1256 (error "Package lacks a file header"))
1257 (let ((file-name (match-string-no-properties 1))
1258 (desc (match-string-no-properties 2))
1259 (start (line-beginning-position)))
1260 (unless (search-forward (concat ";;; " file-name ".el ends here"))
1261 (error "Package lacks a terminating comment"))
1262 ;; Try to include a trailing newline.
1263 (forward-line)
1264 (narrow-to-region start (point))
1265 (require 'lisp-mnt)
1266 ;; Use some headers we've invented to drive the process.
1267 (let* ((requires-str (lm-header "package-requires"))
1268 ;; Prefer Package-Version; if defined, the package author
1269 ;; probably wants us to use it. Otherwise try Version.
1270 (pkg-version
1271 (or (package-strip-rcs-id (lm-header "package-version"))
1272 (package-strip-rcs-id (lm-header "version"))))
1273 (homepage (lm-homepage)))
1274 (unless pkg-version
1275 (error
1276 "Package lacks a \"Version\" or \"Package-Version\" header"))
1277 (package-desc-from-define
1278 file-name pkg-version desc
1279 (if requires-str
1280 (package--prepare-dependencies
1281 (package-read-from-string requires-str)))
1282 :kind 'single
1283 :url homepage))))
1285 (declare-function tar-get-file-descriptor "tar-mode" (file))
1286 (declare-function tar--extract "tar-mode" (descriptor))
1288 (defun package-tar-file-info ()
1289 "Find package information for a tar file.
1290 The return result is a `package-desc'."
1291 (cl-assert (derived-mode-p 'tar-mode))
1292 (let* ((dir-name (file-name-directory
1293 (tar-header-name (car tar-parse-info))))
1294 (desc-file (package--description-file dir-name))
1295 (tar-desc (tar-get-file-descriptor (concat dir-name desc-file))))
1296 (unless tar-desc
1297 (error "No package descriptor file found"))
1298 (with-current-buffer (tar--extract tar-desc)
1299 (unwind-protect
1300 (package--read-pkg-desc 'tar)
1301 (kill-buffer (current-buffer))))))
1303 (defun package-dir-info ()
1304 "Find package information for a directory.
1305 The return result is a `package-desc'."
1306 (cl-assert (derived-mode-p 'dired-mode))
1307 (let* ((desc-file (package--description-file default-directory)))
1308 (if (file-readable-p desc-file)
1309 (with-temp-buffer
1310 (insert-file-contents desc-file)
1311 (package--read-pkg-desc 'dir))
1312 (let ((files (directory-files default-directory t "\\.el\\'" t))
1313 info)
1314 (while files
1315 (with-temp-buffer
1316 (insert-file-contents (pop files))
1317 (if (setq info (ignore-errors (package-buffer-info)))
1318 (setq files nil)
1319 (setf (package-desc-kind info) 'dir))))))))
1321 (defun package--read-pkg-desc (kind)
1322 "Read a `define-package' form in current buffer.
1323 Return the pkg-desc, with desc-kind set to KIND."
1324 (goto-char (point-min))
1325 (unwind-protect
1326 (let* ((pkg-def-parsed (read (current-buffer)))
1327 (pkg-desc
1328 (if (not (eq (car pkg-def-parsed) 'define-package))
1329 (error "Can't find define-package in %s"
1330 (tar-header-name tar-desc))
1331 (apply #'package-desc-from-define
1332 (append (cdr pkg-def-parsed))))))
1333 (setf (package-desc-kind pkg-desc) kind)
1334 pkg-desc)))
1337 ;;;###autoload
1338 (defun package-install-from-buffer ()
1339 "Install a package from the current buffer.
1340 The current buffer is assumed to be a single .el or .tar file or
1341 a directory. These must follow the packaging guidelines (see
1342 info node `(elisp)Packaging').
1344 Specially, if current buffer is a directory, the -pkg.el
1345 description file is not mandatory, in which case the information
1346 is derived from the main .el file in the directory.
1348 Downloads and installs required packages as needed."
1349 (interactive)
1350 (let ((pkg-desc
1351 (cond
1352 ((derived-mode-p 'dired-mode)
1353 ;; This is the only way a package-desc object with a `dir'
1354 ;; desc-kind can be created. Such packages can't be
1355 ;; uploaded or installed from archives, they can only be
1356 ;; installed from local buffers or directories.
1357 (package-dir-info))
1358 ((derived-mode-p 'tar-mode)
1359 (package-tar-file-info))
1361 (package-buffer-info)))))
1362 ;; Download and install the dependencies.
1363 (let* ((requires (package-desc-reqs pkg-desc))
1364 (transaction (package-compute-transaction nil requires)))
1365 (package-download-transaction transaction))
1366 ;; Install the package itself.
1367 (package-unpack pkg-desc)
1368 pkg-desc))
1370 ;;;###autoload
1371 (defun package-install-file (file)
1372 "Install a package from a file.
1373 The file can either be a tar file or an Emacs Lisp file."
1374 (interactive "fPackage file name: ")
1375 (with-temp-buffer
1376 (if (file-directory-p file)
1377 (progn
1378 (setq default-directory file)
1379 (dired-mode))
1380 (insert-file-contents-literally file)
1381 (when (string-match "\\.tar\\'" file) (tar-mode)))
1382 (package-install-from-buffer)))
1384 (defun package-delete (pkg-desc)
1385 (let ((dir (package-desc-dir pkg-desc)))
1386 (if (not (string-prefix-p (file-name-as-directory
1387 (expand-file-name package-user-dir))
1388 (expand-file-name dir)))
1389 ;; Don't delete "system" packages.
1390 (error "Package `%s' is a system package, not deleting"
1391 (package-desc-full-name pkg-desc))
1392 (delete-directory dir t t)
1393 ;; Remove NAME-VERSION.signed file.
1394 (let ((signed-file (concat dir ".signed")))
1395 (if (file-exists-p signed-file)
1396 (delete-file signed-file)))
1397 ;; Update package-alist.
1398 (let* ((name (package-desc-name pkg-desc))
1399 (pkgs (assq name package-alist)))
1400 (delete pkg-desc pkgs)
1401 (unless (cdr pkgs)
1402 (setq package-alist (delq pkgs package-alist))))
1403 (message "Package `%s' deleted." (package-desc-full-name pkg-desc)))))
1405 (defun package-archive-base (desc)
1406 "Return the archive containing the package NAME."
1407 (cdr (assoc (package-desc-archive desc) package-archives)))
1409 (defun package-archive-priority (archive)
1410 "Return the priority of ARCHIVE.
1412 The archive priorities are specified in
1413 `package-archive-priorities'. If not given there, the priority
1414 defaults to 0."
1415 (or (cdr (assoc archive package-archive-priorities))
1418 (defun package-desc-priority-version (pkg-desc)
1419 "Return the version PKG-DESC with the archive priority prepended.
1421 This allows for easy comparison of package versions from
1422 different archives if archive priorities are meant to be taken in
1423 consideration."
1424 (cons (package-archive-priority
1425 (package-desc-archive pkg-desc))
1426 (package-desc-version pkg-desc)))
1428 (defun package--download-one-archive (archive file)
1429 "Retrieve an archive file FILE from ARCHIVE, and cache it.
1430 ARCHIVE should be a cons cell of the form (NAME . LOCATION),
1431 similar to an entry in `package-alist'. Save the cached copy to
1432 \"archives/NAME/archive-contents\" in `package-user-dir'."
1433 (let ((dir (expand-file-name (format "archives/%s" (car archive))
1434 package-user-dir))
1435 (sig-file (concat file ".sig"))
1436 good-signatures)
1437 (package--with-work-buffer (cdr archive) file
1438 ;; Check signature of archive-contents, if desired.
1439 (if (and package-check-signature
1440 (not (member archive package-unsigned-archives)))
1441 (if (package--archive-file-exists-p (cdr archive) sig-file)
1442 (setq good-signatures (package--check-signature (cdr archive)
1443 file))
1444 (unless (eq package-check-signature 'allow-unsigned)
1445 (error "Unsigned archive `%s'"
1446 (car archive)))))
1447 ;; Read the retrieved buffer to make sure it is valid (e.g. it
1448 ;; may fetch a URL redirect page).
1449 (when (listp (read (current-buffer)))
1450 (make-directory dir t)
1451 (write-region nil nil (expand-file-name file dir) nil 'silent)))
1452 (when good-signatures
1453 ;; Write out good signatures into archive-contents.signed file.
1454 (write-region (mapconcat #'epg-signature-to-string good-signatures "\n")
1456 (expand-file-name (concat file ".signed") dir)
1457 nil 'silent))))
1459 (declare-function epg-check-configuration "epg-config"
1460 (config &optional minimum-version))
1461 (declare-function epg-configuration "epg-config" ())
1462 (declare-function epg-import-keys-from-file "epg" (context keys))
1464 ;;;###autoload
1465 (defun package-import-keyring (&optional file)
1466 "Import keys from FILE."
1467 (interactive "fFile: ")
1468 (setq file (expand-file-name file))
1469 (let ((context (epg-make-context 'OpenPGP))
1470 (homedir (expand-file-name "gnupg" package-user-dir)))
1471 (with-file-modes 448
1472 (make-directory homedir t))
1473 (setf (epg-context-home-directory context) homedir)
1474 (message "Importing %s..." (file-name-nondirectory file))
1475 (epg-import-keys-from-file context file)
1476 (message "Importing %s...done" (file-name-nondirectory file))))
1478 ;;;###autoload
1479 (defun package-refresh-contents ()
1480 "Download the ELPA archive description if needed.
1481 This informs Emacs about the latest versions of all packages, and
1482 makes them available for download."
1483 (interactive)
1484 ;; FIXME: Do it asynchronously.
1485 (unless (file-exists-p package-user-dir)
1486 (make-directory package-user-dir t))
1487 (let ((default-keyring (expand-file-name "package-keyring.gpg"
1488 data-directory)))
1489 (when (and package-check-signature (file-exists-p default-keyring))
1490 (condition-case-unless-debug error
1491 (progn
1492 (epg-check-configuration (epg-configuration))
1493 (package-import-keyring default-keyring))
1494 (error (message "Cannot import default keyring: %S" (cdr error))))))
1495 (dolist (archive package-archives)
1496 (condition-case-unless-debug nil
1497 (package--download-one-archive archive "archive-contents")
1498 (error (message "Failed to download `%s' archive."
1499 (car archive)))))
1500 (package-read-all-archive-contents))
1502 ;;;###autoload
1503 (defun package-initialize (&optional no-activate)
1504 "Load Emacs Lisp packages, and activate them.
1505 The variable `package-load-list' controls which packages to load.
1506 If optional arg NO-ACTIVATE is non-nil, don't activate packages."
1507 (interactive)
1508 (setq package-alist nil)
1509 (package-load-all-descriptors)
1510 (package-read-all-archive-contents)
1511 (unless no-activate
1512 (dolist (elt package-alist)
1513 (package-activate (car elt))))
1514 (setq package--initialized t))
1517 ;;;; Package description buffer.
1519 ;;;###autoload
1520 (defun describe-package (package)
1521 "Display the full documentation of PACKAGE (a symbol)."
1522 (interactive
1523 (let* ((guess (function-called-at-point)))
1524 (require 'finder-inf nil t)
1525 ;; Load the package list if necessary (but don't activate them).
1526 (unless package--initialized
1527 (package-initialize t))
1528 (let ((packages (append (mapcar 'car package-alist)
1529 (mapcar 'car package-archive-contents)
1530 (mapcar 'car package--builtins))))
1531 (unless (memq guess packages)
1532 (setq guess nil))
1533 (setq packages (mapcar 'symbol-name packages))
1534 (let ((val
1535 (completing-read (if guess
1536 (format "Describe package (default %s): "
1537 guess)
1538 "Describe package: ")
1539 packages nil t nil nil guess)))
1540 (list (intern val))))))
1541 (if (not (or (package-desc-p package) (and package (symbolp package))))
1542 (message "No package specified")
1543 (help-setup-xref (list #'describe-package package)
1544 (called-interactively-p 'interactive))
1545 (with-help-window (help-buffer)
1546 (with-current-buffer standard-output
1547 (describe-package-1 package)))))
1549 (defun describe-package-1 (pkg)
1550 (require 'lisp-mnt)
1551 (let* ((desc (or
1552 (if (package-desc-p pkg) pkg)
1553 (cadr (assq pkg package-alist))
1554 (let ((built-in (assq pkg package--builtins)))
1555 (if built-in
1556 (package--from-builtin built-in)
1557 (cadr (assq pkg package-archive-contents))))))
1558 (name (if desc (package-desc-name desc) pkg))
1559 (pkg-dir (if desc (package-desc-dir desc)))
1560 (reqs (if desc (package-desc-reqs desc)))
1561 (version (if desc (package-desc-version desc)))
1562 (archive (if desc (package-desc-archive desc)))
1563 (extras (and desc (package-desc-extras desc)))
1564 (homepage (cdr (assoc :url extras)))
1565 (keywords (if desc (package-desc--keywords desc)))
1566 (built-in (eq pkg-dir 'builtin))
1567 (installable (and archive (not built-in)))
1568 (status (if desc (package-desc-status desc) "orphan"))
1569 (signed (if desc (package-desc-signed desc))))
1570 (prin1 name)
1571 (princ " is ")
1572 (princ (if (memq (aref status 0) '(?a ?e ?i ?o ?u)) "an " "a "))
1573 (princ status)
1574 (princ " package.\n\n")
1576 (insert " " (propertize "Status" 'font-lock-face 'bold) ": ")
1577 (cond (built-in
1578 (insert (propertize (capitalize status)
1579 'font-lock-face 'font-lock-builtin-face)
1580 "."))
1581 (pkg-dir
1582 (insert (propertize (if (equal status "unsigned")
1583 "Installed"
1584 (capitalize status)) ;FIXME: Why comment-face?
1585 'font-lock-face 'font-lock-comment-face))
1586 (insert " in `")
1587 ;; Todo: Add button for uninstalling.
1588 (help-insert-xref-button (abbreviate-file-name
1589 (file-name-as-directory pkg-dir))
1590 'help-package-def pkg-dir)
1591 (if (and (package-built-in-p name)
1592 (not (package-built-in-p name version)))
1593 (insert "',\n shadowing a "
1594 (propertize "built-in package"
1595 'font-lock-face 'font-lock-builtin-face))
1596 (insert "'"))
1597 (if signed
1598 (insert ".")
1599 (insert " (unsigned).")))
1600 (installable
1601 (insert (capitalize status))
1602 (insert " from " (format "%s" archive))
1603 (insert " -- ")
1604 (package-make-button
1605 "Install"
1606 'action 'package-install-button-action
1607 'package-desc desc))
1608 (t (insert (capitalize status) ".")))
1609 (insert "\n")
1610 (insert " " (propertize "Archive" 'font-lock-face 'bold)
1611 ": " (or archive "n/a") "\n")
1612 (and version
1613 (insert " "
1614 (propertize "Version" 'font-lock-face 'bold) ": "
1615 (package-version-join version) "\n"))
1617 (setq reqs (if desc (package-desc-reqs desc)))
1618 (when reqs
1619 (insert " " (propertize "Requires" 'font-lock-face 'bold) ": ")
1620 (let ((first t)
1621 name vers text)
1622 (dolist (req reqs)
1623 (setq name (car req)
1624 vers (cadr req)
1625 text (format "%s-%s" (symbol-name name)
1626 (package-version-join vers)))
1627 (cond (first (setq first nil))
1628 ((>= (+ 2 (current-column) (length text))
1629 (window-width))
1630 (insert ",\n "))
1631 (t (insert ", ")))
1632 (help-insert-xref-button text 'help-package name))
1633 (insert "\n")))
1634 (insert " " (propertize "Summary" 'font-lock-face 'bold)
1635 ": " (if desc (package-desc-summary desc)) "\n")
1636 (when homepage
1637 (insert " " (propertize "Homepage" 'font-lock-face 'bold) ": ")
1638 (help-insert-xref-button homepage 'help-url homepage)
1639 (insert "\n"))
1640 (when keywords
1641 (insert " " (propertize "Keywords" 'font-lock-face 'bold) ": ")
1642 (dolist (k keywords)
1643 (package-make-button
1645 'package-keyword k
1646 'action 'package-keyword-button-action)
1647 (insert " "))
1648 (insert "\n"))
1649 (let* ((all-pkgs (append (cdr (assq name package-alist))
1650 (cdr (assq name package-archive-contents))
1651 (let ((bi (assq name package--builtins)))
1652 (if bi (list (package--from-builtin bi))))))
1653 (other-pkgs (delete desc all-pkgs)))
1654 (when other-pkgs
1655 (insert " " (propertize "Other versions" 'font-lock-face 'bold) ": "
1656 (mapconcat
1657 (lambda (opkg)
1658 (let* ((ov (package-desc-version opkg))
1659 (dir (package-desc-dir opkg))
1660 (from (or (package-desc-archive opkg)
1661 (if (stringp dir) "installed" dir))))
1662 (if (not ov) (format "%s" from)
1663 (format "%s (%s)"
1664 (make-text-button (package-version-join ov) nil
1665 'face 'link
1666 'follow-link t
1667 'action
1668 (lambda (_button)
1669 (describe-package opkg)))
1670 from))))
1671 other-pkgs ", ")
1672 ".\n")))
1674 (insert "\n")
1676 (if built-in
1677 ;; For built-in packages, insert the commentary.
1678 (let ((fn (locate-file (format "%s.el" name) load-path
1679 load-file-rep-suffixes))
1680 (opoint (point)))
1681 (insert (or (lm-commentary fn) ""))
1682 (save-excursion
1683 (goto-char opoint)
1684 (when (re-search-forward "^;;; Commentary:\n" nil t)
1685 (replace-match ""))
1686 (while (re-search-forward "^\\(;+ ?\\)" nil t)
1687 (replace-match ""))))
1688 (let ((readme (expand-file-name (format "%s-readme.txt" name)
1689 package-user-dir))
1690 readme-string)
1691 ;; For elpa packages, try downloading the commentary. If that
1692 ;; fails, try an existing readme file in `package-user-dir'.
1693 (cond ((condition-case nil
1694 (save-excursion
1695 (package--with-work-buffer
1696 (package-archive-base desc)
1697 (format "%s-readme.txt" name)
1698 (save-excursion
1699 (goto-char (point-max))
1700 (unless (bolp)
1701 (insert ?\n)))
1702 (write-region nil nil
1703 (expand-file-name readme package-user-dir)
1704 nil 'silent)
1705 (setq readme-string (buffer-string))
1707 (error nil))
1708 (insert readme-string))
1709 ((file-readable-p readme)
1710 (insert-file-contents readme)
1711 (goto-char (point-max))))))))
1713 (defun package-install-button-action (button)
1714 (let ((pkg-desc (button-get button 'package-desc)))
1715 (when (y-or-n-p (format "Install package `%s'? "
1716 (package-desc-full-name pkg-desc)))
1717 (package-install pkg-desc)
1718 (revert-buffer nil t)
1719 (goto-char (point-min)))))
1721 (defun package-keyword-button-action (button)
1722 (let ((pkg-keyword (button-get button 'package-keyword)))
1723 (package-show-package-list t (list pkg-keyword))))
1725 (defun package-make-button (text &rest props)
1726 (let ((button-text (if (display-graphic-p) text (concat "[" text "]")))
1727 (button-face (if (display-graphic-p)
1728 '(:box (:line-width 2 :color "dark grey")
1729 :background "light grey"
1730 :foreground "black")
1731 'link)))
1732 (apply 'insert-text-button button-text 'face button-face 'follow-link t
1733 props)))
1736 ;;;; Package menu mode.
1738 (defvar package-menu-mode-map
1739 (let ((map (make-sparse-keymap))
1740 (menu-map (make-sparse-keymap "Package")))
1741 (set-keymap-parent map tabulated-list-mode-map)
1742 (define-key map "\C-m" 'package-menu-describe-package)
1743 (define-key map "u" 'package-menu-mark-unmark)
1744 (define-key map "\177" 'package-menu-backup-unmark)
1745 (define-key map "d" 'package-menu-mark-delete)
1746 (define-key map "i" 'package-menu-mark-install)
1747 (define-key map "U" 'package-menu-mark-upgrades)
1748 (define-key map "r" 'package-menu-refresh)
1749 (define-key map "f" 'package-menu-filter)
1750 (define-key map "~" 'package-menu-mark-obsolete-for-deletion)
1751 (define-key map "x" 'package-menu-execute)
1752 (define-key map "h" 'package-menu-quick-help)
1753 (define-key map "?" 'package-menu-describe-package)
1754 (define-key map [menu-bar package-menu] (cons "Package" menu-map))
1755 (define-key menu-map [mq]
1756 '(menu-item "Quit" quit-window
1757 :help "Quit package selection"))
1758 (define-key menu-map [s1] '("--"))
1759 (define-key menu-map [mn]
1760 '(menu-item "Next" next-line
1761 :help "Next Line"))
1762 (define-key menu-map [mp]
1763 '(menu-item "Previous" previous-line
1764 :help "Previous Line"))
1765 (define-key menu-map [s2] '("--"))
1766 (define-key menu-map [mu]
1767 '(menu-item "Unmark" package-menu-mark-unmark
1768 :help "Clear any marks on a package and move to the next line"))
1769 (define-key menu-map [munm]
1770 '(menu-item "Unmark Backwards" package-menu-backup-unmark
1771 :help "Back up one line and clear any marks on that package"))
1772 (define-key menu-map [md]
1773 '(menu-item "Mark for Deletion" package-menu-mark-delete
1774 :help "Mark a package for deletion and move to the next line"))
1775 (define-key menu-map [mi]
1776 '(menu-item "Mark for Install" package-menu-mark-install
1777 :help "Mark a package for installation and move to the next line"))
1778 (define-key menu-map [mupgrades]
1779 '(menu-item "Mark Upgradable Packages" package-menu-mark-upgrades
1780 :help "Mark packages that have a newer version for upgrading"))
1781 (define-key menu-map [s3] '("--"))
1782 (define-key menu-map [mf]
1783 '(menu-item "Filter Package List..." package-menu-filter
1784 :help "Filter package selection (q to go back)"))
1785 (define-key menu-map [mg]
1786 '(menu-item "Update Package List" revert-buffer
1787 :help "Update the list of packages"))
1788 (define-key menu-map [mr]
1789 '(menu-item "Refresh Package List" package-menu-refresh
1790 :help "Download the ELPA archive"))
1791 (define-key menu-map [s4] '("--"))
1792 (define-key menu-map [mt]
1793 '(menu-item "Mark Obsolete Packages" package-menu-mark-obsolete-for-deletion
1794 :help "Mark all obsolete packages for deletion"))
1795 (define-key menu-map [mx]
1796 '(menu-item "Execute Actions" package-menu-execute
1797 :help "Perform all the marked actions"))
1798 (define-key menu-map [s5] '("--"))
1799 (define-key menu-map [mh]
1800 '(menu-item "Help" package-menu-quick-help
1801 :help "Show short key binding help for package-menu-mode"))
1802 (define-key menu-map [mc]
1803 '(menu-item "Describe Package" package-menu-describe-package
1804 :help "Display information about this package"))
1805 map)
1806 "Local keymap for `package-menu-mode' buffers.")
1808 (defvar package-menu--new-package-list nil
1809 "List of newly-available packages since `list-packages' was last called.")
1811 (define-derived-mode package-menu-mode tabulated-list-mode "Package Menu"
1812 "Major mode for browsing a list of packages.
1813 Letters do not insert themselves; instead, they are commands.
1814 \\<package-menu-mode-map>
1815 \\{package-menu-mode-map}"
1816 (setq tabulated-list-format
1817 `[("Package" 18 package-menu--name-predicate)
1818 ("Version" 13 nil)
1819 ("Status" 10 package-menu--status-predicate)
1820 ,@(if (cdr package-archives)
1821 '(("Archive" 10 package-menu--archive-predicate)))
1822 ("Description" 0 nil)])
1823 (setq tabulated-list-padding 2)
1824 (setq tabulated-list-sort-key (cons "Status" nil))
1825 (add-hook 'tabulated-list-revert-hook 'package-menu--refresh nil t)
1826 (tabulated-list-init-header))
1828 (defmacro package--push (pkg-desc status listname)
1829 "Convenience macro for `package-menu--generate'.
1830 If the alist stored in the symbol LISTNAME lacks an entry for a
1831 package PKG-DESC, add one. The alist is keyed with PKG-DESC."
1832 `(unless (assoc ,pkg-desc ,listname)
1833 ;; FIXME: Should we move status into pkg-desc?
1834 (push (cons ,pkg-desc ,status) ,listname)))
1836 (defvar package-list-unversioned nil
1837 "If non-nil include packages that don't have a version in `list-package'.")
1839 (defvar package-list-unsigned nil
1840 "If non-nil, mention in the list which packages were installed w/o signature.")
1842 (defun package-desc-status (pkg-desc)
1843 (let* ((name (package-desc-name pkg-desc))
1844 (dir (package-desc-dir pkg-desc))
1845 (lle (assq name package-load-list))
1846 (held (cadr lle))
1847 (version (package-desc-version pkg-desc))
1848 (signed (package-desc-signed pkg-desc)))
1849 (cond
1850 ((eq dir 'builtin) "built-in")
1851 ((and lle (null held)) "disabled")
1852 ((stringp held)
1853 (let ((hv (if (stringp held) (version-to-list held))))
1854 (cond
1855 ((version-list-= version hv) "held")
1856 ((version-list-< version hv) "obsolete")
1857 (t "disabled"))))
1858 ((package-built-in-p name version) "obsolete")
1859 (dir ;One of the installed packages.
1860 (cond
1861 ((not (file-exists-p (package-desc-dir pkg-desc))) "deleted")
1862 ((eq pkg-desc (cadr (assq name package-alist)))
1863 (if (or (not package-list-unsigned) signed) "installed" "unsigned"))
1864 (t "obsolete")))
1866 (let* ((ins (cadr (assq name package-alist)))
1867 (ins-v (if ins (package-desc-version ins))))
1868 (cond
1869 ((or (null ins) (version-list-< ins-v version))
1870 (if (memq name package-menu--new-package-list)
1871 "new" "available"))
1872 ((version-list-< version ins-v) "obsolete")
1873 ((version-list-= version ins-v)
1874 (if (or (not package-list-unsigned) signed)
1875 "installed" "unsigned"))))))))
1877 (defun package-menu--refresh (&optional packages keywords)
1878 "Re-populate the `tabulated-list-entries'.
1879 PACKAGES should be nil or t, which means to display all known packages.
1880 KEYWORDS should be nil or a list of keywords."
1881 ;; Construct list of (PKG-DESC . STATUS).
1882 (unless packages (setq packages t))
1883 (let (info-list name)
1884 ;; Installed packages:
1885 (dolist (elt package-alist)
1886 (setq name (car elt))
1887 (when (or (eq packages t) (memq name packages))
1888 (dolist (pkg (cdr elt))
1889 (when (package--has-keyword-p pkg keywords)
1890 (package--push pkg (package-desc-status pkg) info-list)))))
1892 ;; Built-in packages:
1893 (dolist (elt package--builtins)
1894 (setq name (car elt))
1895 (when (and (not (eq name 'emacs)) ; Hide the `emacs' package.
1896 (package--has-keyword-p (package--from-builtin elt) keywords)
1897 (or package-list-unversioned
1898 (package--bi-desc-version (cdr elt)))
1899 (or (eq packages t) (memq name packages)))
1900 (package--push (package--from-builtin elt) "built-in" info-list)))
1902 ;; Available and disabled packages:
1903 (dolist (elt package-archive-contents)
1904 (setq name (car elt))
1905 (when (or (eq packages t) (memq name packages))
1906 (dolist (pkg (cdr elt))
1907 ;; Hide obsolete packages.
1908 (when (and (not (package-installed-p (package-desc-name pkg)
1909 (package-desc-version pkg)))
1910 (package--has-keyword-p pkg keywords))
1911 (package--push pkg (package-desc-status pkg) info-list)))))
1913 ;; Print the result.
1914 (setq tabulated-list-entries
1915 (mapcar #'package-menu--print-info info-list))))
1917 (defun package-all-keywords ()
1918 "Collect all package keywords"
1919 (let (keywords)
1920 (package--mapc (lambda (desc)
1921 (let* ((desc-keywords (and desc (package-desc--keywords desc))))
1922 (setq keywords (append keywords desc-keywords)))))
1923 keywords))
1925 (defun package--mapc (function &optional packages)
1926 "Call FUNCTION for all known PACKAGES.
1927 PACKAGES can be nil or t, which means to display all known
1928 packages, or a list of packages.
1930 Built-in packages are converted with `package--from-builtin'."
1931 (unless packages (setq packages t))
1932 (let (name)
1933 ;; Installed packages:
1934 (dolist (elt package-alist)
1935 (setq name (car elt))
1936 (when (or (eq packages t) (memq name packages))
1937 (mapc function (cdr elt))))
1939 ;; Built-in packages:
1940 (dolist (elt package--builtins)
1941 (setq name (car elt))
1942 (when (and (not (eq name 'emacs)) ; Hide the `emacs' package.
1943 (or package-list-unversioned
1944 (package--bi-desc-version (cdr elt)))
1945 (or (eq packages t) (memq name packages)))
1946 (funcall function (package--from-builtin elt))))
1948 ;; Available and disabled packages:
1949 (dolist (elt package-archive-contents)
1950 (setq name (car elt))
1951 (when (or (eq packages t) (memq name packages))
1952 (dolist (pkg (cdr elt))
1953 ;; Hide obsolete packages.
1954 (unless (package-installed-p (package-desc-name pkg)
1955 (package-desc-version pkg))
1956 (funcall function pkg)))))))
1958 (defun package--has-keyword-p (desc &optional keywords)
1959 "Test if package DESC has any of the given KEYWORDS.
1960 When none are given, the package matches."
1961 (if keywords
1962 (let* ((desc-keywords (and desc (package-desc--keywords desc)))
1963 found)
1964 (dolist (k keywords)
1965 (when (and (not found)
1966 (member k desc-keywords))
1967 (setq found t)))
1968 found)
1971 (defun package-menu--generate (remember-pos packages &optional keywords)
1972 "Populate the Package Menu.
1973 If REMEMBER-POS is non-nil, keep point on the same entry.
1974 PACKAGES should be t, which means to display all known packages,
1975 or a list of package names (symbols) to display.
1977 With KEYWORDS given, only packages with those keywords are
1978 shown."
1979 (package-menu--refresh packages keywords)
1980 (setf (car (aref tabulated-list-format 0))
1981 (if keywords
1982 (let ((filters (mapconcat 'identity keywords ",")))
1983 (concat "Package[" filters "]"))
1984 "Package"))
1985 (if keywords
1986 (define-key package-menu-mode-map "q" 'package-show-package-list)
1987 (define-key package-menu-mode-map "q" 'quit-window))
1988 (tabulated-list-init-header)
1989 (tabulated-list-print remember-pos))
1991 (defun package-menu--print-info (pkg)
1992 "Return a package entry suitable for `tabulated-list-entries'.
1993 PKG has the form (PKG-DESC . STATUS).
1994 Return (PKG-DESC [NAME VERSION STATUS DOC])."
1995 (let* ((pkg-desc (car pkg))
1996 (status (cdr pkg))
1997 (face (pcase status
1998 (`"built-in" 'font-lock-builtin-face)
1999 (`"available" 'default)
2000 (`"new" 'bold)
2001 (`"held" 'font-lock-constant-face)
2002 (`"disabled" 'font-lock-warning-face)
2003 (`"installed" 'font-lock-comment-face)
2004 (`"unsigned" 'font-lock-warning-face)
2005 (_ 'font-lock-warning-face)))) ; obsolete.
2006 (list pkg-desc
2007 `[,(list (symbol-name (package-desc-name pkg-desc))
2008 'face 'link
2009 'follow-link t
2010 'package-desc pkg-desc
2011 'action 'package-menu-describe-package)
2012 ,(propertize (package-version-join
2013 (package-desc-version pkg-desc))
2014 'font-lock-face face)
2015 ,(propertize status 'font-lock-face face)
2016 ,@(if (cdr package-archives)
2017 (list (propertize (or (package-desc-archive pkg-desc) "")
2018 'font-lock-face face)))
2019 ,(propertize (package-desc-summary pkg-desc)
2020 'font-lock-face face)])))
2022 (defun package-menu-refresh ()
2023 "Download the Emacs Lisp package archive.
2024 This fetches the contents of each archive specified in
2025 `package-archives', and then refreshes the package menu."
2026 (interactive)
2027 (unless (derived-mode-p 'package-menu-mode)
2028 (user-error "The current buffer is not a Package Menu"))
2029 (package-refresh-contents)
2030 (package-menu--generate t t))
2032 (defun package-menu-describe-package (&optional button)
2033 "Describe the current package.
2034 If optional arg BUTTON is non-nil, describe its associated package."
2035 (interactive)
2036 (let ((pkg-desc (if button (button-get button 'package-desc)
2037 (tabulated-list-get-id))))
2038 (if pkg-desc
2039 (describe-package pkg-desc)
2040 (user-error "No package here"))))
2042 ;; fixme numeric argument
2043 (defun package-menu-mark-delete (&optional _num)
2044 "Mark a package for deletion and move to the next line."
2045 (interactive "p")
2046 (if (member (package-menu-get-status) '("installed" "obsolete" "unsigned"))
2047 (tabulated-list-put-tag "D" t)
2048 (forward-line)))
2050 (defun package-menu-mark-install (&optional _num)
2051 "Mark a package for installation and move to the next line."
2052 (interactive "p")
2053 (if (member (package-menu-get-status) '("available" "new"))
2054 (tabulated-list-put-tag "I" t)
2055 (forward-line)))
2057 (defun package-menu-mark-unmark (&optional _num)
2058 "Clear any marks on a package and move to the next line."
2059 (interactive "p")
2060 (tabulated-list-put-tag " " t))
2062 (defun package-menu-backup-unmark ()
2063 "Back up one line and clear any marks on that package."
2064 (interactive)
2065 (forward-line -1)
2066 (tabulated-list-put-tag " "))
2068 (defun package-menu-mark-obsolete-for-deletion ()
2069 "Mark all obsolete packages for deletion."
2070 (interactive)
2071 (save-excursion
2072 (goto-char (point-min))
2073 (while (not (eobp))
2074 (if (equal (package-menu-get-status) "obsolete")
2075 (tabulated-list-put-tag "D" t)
2076 (forward-line 1)))))
2078 (defun package-menu-quick-help ()
2079 "Show short key binding help for package-menu-mode."
2080 (interactive)
2081 (message "n-ext, i-nstall, d-elete, u-nmark, x-ecute, r-efresh, h-elp"))
2083 (define-obsolete-function-alias
2084 'package-menu-view-commentary 'package-menu-describe-package "24.1")
2086 (defun package-menu-get-status ()
2087 (let* ((id (tabulated-list-get-id))
2088 (entry (and id (assq id tabulated-list-entries))))
2089 (if entry
2090 (aref (cadr entry) 2)
2091 "")))
2093 (defun package-menu--find-upgrades ()
2094 (let (installed available upgrades)
2095 ;; Build list of installed/available packages in this buffer.
2096 (dolist (entry tabulated-list-entries)
2097 ;; ENTRY is (PKG-DESC [NAME VERSION STATUS DOC])
2098 (let ((pkg-desc (car entry))
2099 (status (aref (cadr entry) 2)))
2100 (cond ((member status '("installed" "unsigned"))
2101 (push pkg-desc installed))
2102 ((member status '("available" "new"))
2103 (setq available (package--append-to-alist pkg-desc available))))))
2104 ;; Loop through list of installed packages, finding upgrades.
2105 (dolist (pkg-desc installed)
2106 (let* ((name (package-desc-name pkg-desc))
2107 (avail-pkg (cadr (assq name available))))
2108 (and avail-pkg
2109 (version-list-< (package-desc-priority-version pkg-desc)
2110 (package-desc-priority-version avail-pkg))
2111 (push (cons name avail-pkg) upgrades))))
2112 upgrades))
2114 (defun package-menu-mark-upgrades ()
2115 "Mark all upgradable packages in the Package Menu.
2116 For each installed package with a newer version available, place
2117 an (I)nstall flag on the available version and a (D)elete flag on
2118 the installed version. A subsequent \\[package-menu-execute]
2119 call will upgrade the package."
2120 (interactive)
2121 (unless (derived-mode-p 'package-menu-mode)
2122 (error "The current buffer is not a Package Menu"))
2123 (let ((upgrades (package-menu--find-upgrades)))
2124 (if (null upgrades)
2125 (message "No packages to upgrade.")
2126 (widen)
2127 (save-excursion
2128 (goto-char (point-min))
2129 (while (not (eobp))
2130 (let* ((pkg-desc (tabulated-list-get-id))
2131 (upgrade (cdr (assq (package-desc-name pkg-desc) upgrades))))
2132 (cond ((null upgrade)
2133 (forward-line 1))
2134 ((equal pkg-desc upgrade)
2135 (package-menu-mark-install))
2137 (package-menu-mark-delete))))))
2138 (message "%d package%s marked for upgrading."
2139 (length upgrades)
2140 (if (= (length upgrades) 1) "" "s")))))
2142 (defun package-menu-execute (&optional noquery)
2143 "Perform marked Package Menu actions.
2144 Packages marked for installation are downloaded and installed;
2145 packages marked for deletion are removed.
2146 Optional argument NOQUERY non-nil means do not ask the user to confirm."
2147 (interactive)
2148 (unless (derived-mode-p 'package-menu-mode)
2149 (error "The current buffer is not in Package Menu mode"))
2150 (let (install-list delete-list cmd pkg-desc)
2151 (save-excursion
2152 (goto-char (point-min))
2153 (while (not (eobp))
2154 (setq cmd (char-after))
2155 (unless (eq cmd ?\s)
2156 ;; This is the key PKG-DESC.
2157 (setq pkg-desc (tabulated-list-get-id))
2158 (cond ((eq cmd ?D)
2159 (push pkg-desc delete-list))
2160 ((eq cmd ?I)
2161 (push pkg-desc install-list))))
2162 (forward-line)))
2163 (when install-list
2164 (if (or
2165 noquery
2166 (yes-or-no-p
2167 (if (= (length install-list) 1)
2168 (format "Install package `%s'? "
2169 (package-desc-full-name (car install-list)))
2170 (format "Install these %d packages (%s)? "
2171 (length install-list)
2172 (mapconcat #'package-desc-full-name
2173 install-list ", ")))))
2174 (mapc 'package-install install-list)))
2175 ;; Delete packages, prompting if necessary.
2176 (when delete-list
2177 (if (or
2178 noquery
2179 (yes-or-no-p
2180 (if (= (length delete-list) 1)
2181 (format "Delete package `%s'? "
2182 (package-desc-full-name (car delete-list)))
2183 (format "Delete these %d packages (%s)? "
2184 (length delete-list)
2185 (mapconcat #'package-desc-full-name
2186 delete-list ", ")))))
2187 (dolist (elt delete-list)
2188 (condition-case-unless-debug err
2189 (package-delete elt)
2190 (error (message (cadr err)))))
2191 (error "Aborted")))
2192 (if (or delete-list install-list)
2193 (package-menu--generate t t)
2194 (message "No operations specified."))))
2196 (defun package-menu--version-predicate (A B)
2197 (let ((vA (or (aref (cadr A) 1) '(0)))
2198 (vB (or (aref (cadr B) 1) '(0))))
2199 (if (version-list-= vA vB)
2200 (package-menu--name-predicate A B)
2201 (version-list-< vA vB))))
2203 (defun package-menu--status-predicate (A B)
2204 (let ((sA (aref (cadr A) 2))
2205 (sB (aref (cadr B) 2)))
2206 (cond ((string= sA sB)
2207 (package-menu--name-predicate A B))
2208 ((string= sA "new") t)
2209 ((string= sB "new") nil)
2210 ((string= sA "available") t)
2211 ((string= sB "available") nil)
2212 ((string= sA "installed") t)
2213 ((string= sB "installed") nil)
2214 ((string= sA "unsigned") t)
2215 ((string= sB "unsigned") nil)
2216 ((string= sA "held") t)
2217 ((string= sB "held") nil)
2218 ((string= sA "built-in") t)
2219 ((string= sB "built-in") nil)
2220 ((string= sA "obsolete") t)
2221 ((string= sB "obsolete") nil)
2222 (t (string< sA sB)))))
2224 (defun package-menu--description-predicate (A B)
2225 (let ((dA (aref (cadr A) 3))
2226 (dB (aref (cadr B) 3)))
2227 (if (string= dA dB)
2228 (package-menu--name-predicate A B)
2229 (string< dA dB))))
2231 (defun package-menu--name-predicate (A B)
2232 (string< (symbol-name (package-desc-name (car A)))
2233 (symbol-name (package-desc-name (car B)))))
2235 (defun package-menu--archive-predicate (A B)
2236 (string< (or (package-desc-archive (car A)) "")
2237 (or (package-desc-archive (car B)) "")))
2239 ;;;###autoload
2240 (defun list-packages (&optional no-fetch)
2241 "Display a list of packages.
2242 This first fetches the updated list of packages before
2243 displaying, unless a prefix argument NO-FETCH is specified.
2244 The list is displayed in a buffer named `*Packages*'."
2245 (interactive "P")
2246 (require 'finder-inf nil t)
2247 ;; Initialize the package system if necessary.
2248 (unless package--initialized
2249 (package-initialize t))
2250 (let (old-archives new-packages)
2251 (unless no-fetch
2252 ;; Read the locally-cached archive-contents.
2253 (package-read-all-archive-contents)
2254 (setq old-archives package-archive-contents)
2255 ;; Fetch the remote list of packages.
2256 (package-refresh-contents)
2257 ;; Find which packages are new.
2258 (dolist (elt package-archive-contents)
2259 (unless (assq (car elt) old-archives)
2260 (push (car elt) new-packages))))
2262 ;; Generate the Package Menu.
2263 (let ((buf (get-buffer-create "*Packages*")))
2264 (with-current-buffer buf
2265 (package-menu-mode)
2266 (set (make-local-variable 'package-menu--new-package-list)
2267 new-packages)
2268 (package-menu--generate nil t))
2269 ;; The package menu buffer has keybindings. If the user types
2270 ;; `M-x list-packages', that suggests it should become current.
2271 (switch-to-buffer buf))
2273 (let ((upgrades (package-menu--find-upgrades)))
2274 (if upgrades
2275 (message "%d package%s can be upgraded; type `%s' to mark %s for upgrading."
2276 (length upgrades)
2277 (if (= (length upgrades) 1) "" "s")
2278 (substitute-command-keys "\\[package-menu-mark-upgrades]")
2279 (if (= (length upgrades) 1) "it" "them"))))))
2281 ;;;###autoload
2282 (defalias 'package-list-packages 'list-packages)
2284 ;; Used in finder.el
2285 (defun package-show-package-list (&optional packages keywords)
2286 "Display PACKAGES in a *Packages* buffer.
2287 This is similar to `list-packages', but it does not fetch the
2288 updated list of packages, and it only displays packages with
2289 names in PACKAGES (which should be a list of symbols).
2291 When KEYWORDS are given, only packages with those KEYWORDS are
2292 shown."
2293 (interactive)
2294 (require 'finder-inf nil t)
2295 (let* ((buf (get-buffer-create "*Packages*"))
2296 (win (get-buffer-window buf)))
2297 (with-current-buffer buf
2298 (package-menu-mode)
2299 (package-menu--generate nil packages keywords))
2300 (if win
2301 (select-window win)
2302 (switch-to-buffer buf))))
2304 ;; package-menu--generate rebinds "q" on the fly, so we have to
2305 ;; hard-code the binding in the doc-string here.
2306 (defun package-menu-filter (keyword)
2307 "Filter the *Packages* buffer.
2308 Show only those items that relate to the specified KEYWORD.
2309 To restore the full package list, type `q'."
2310 (interactive (list (completing-read "Keyword: " (package-all-keywords))))
2311 (package-show-package-list t (list keyword)))
2313 (defun package-list-packages-no-fetch ()
2314 "Display a list of packages.
2315 Does not fetch the updated list of packages before displaying.
2316 The list is displayed in a buffer named `*Packages*'."
2317 (interactive)
2318 (list-packages t))
2320 (provide 'package)
2322 ;;; package.el ends here