(package--append-to-alist): Updated docstring due to new name.
[emacs.git] / lisp / emacs-lisp / package.el
blob0f094b556ba93f69aab98ccaa7da47121a7eb156
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 "Append an entry for PKG-DESC to the start of ALIST and return it.
1161 This entry takes the form (`package-desc-name' PKG-DESC).
1163 If ALIST already has an entry with this name, destructively add
1164 PKG-DESC to the cdr of this entry instead, sorted by version
1165 number."
1166 (let* ((name (package-desc-name pkg-desc))
1167 (priority-version (package-desc-priority-version pkg-desc))
1168 (existing-packages (assq name alist)))
1169 (if (not existing-packages)
1170 (cons (list name pkg-desc)
1171 alist)
1172 (while (if (and (cdr existing-packages)
1173 (version-list-< priority-version
1174 (package-desc-priority-version
1175 (cadr existing-packages))))
1176 (setq existing-packages (cdr existing-packages))
1177 (push pkg-desc (cdr existing-packages))
1178 nil))
1179 alist)))
1181 (defun package-download-transaction (packages)
1182 "Download and install all the packages in PACKAGES.
1183 PACKAGES should be a list of package-desc.
1184 This function assumes that all package requirements in
1185 PACKAGES are satisfied, i.e. that PACKAGES is computed
1186 using `package-compute-transaction'."
1187 (mapc #'package-install-from-archive packages))
1189 ;;;###autoload
1190 (defun package-install (pkg)
1191 "Install the package PKG.
1192 PKG can be a package-desc or the package name of one the available packages
1193 in an archive in `package-archives'. Interactively, prompt for its name."
1194 (interactive
1195 (progn
1196 ;; Initialize the package system to get the list of package
1197 ;; symbols for completion.
1198 (unless package--initialized
1199 (package-initialize t))
1200 (unless package-archive-contents
1201 (package-refresh-contents))
1202 (list (intern (completing-read
1203 "Install package: "
1204 (delq nil
1205 (mapcar (lambda (elt)
1206 (unless (package-installed-p (car elt))
1207 (symbol-name (car elt))))
1208 package-archive-contents))
1209 nil t)))))
1210 (package-download-transaction
1211 (if (package-desc-p pkg)
1212 (package-compute-transaction (list pkg)
1213 (package-desc-reqs pkg))
1214 (package-compute-transaction ()
1215 (list (list pkg))))))
1217 (defun package-strip-rcs-id (str)
1218 "Strip RCS version ID from the version string STR.
1219 If the result looks like a dotted numeric version, return it.
1220 Otherwise return nil."
1221 (when str
1222 (when (string-match "\\`[ \t]*[$]Revision:[ \t]+" str)
1223 (setq str (substring str (match-end 0))))
1224 (condition-case nil
1225 (if (version-to-list str)
1226 str)
1227 (error nil))))
1229 (declare-function lm-homepage "lisp-mnt" (&optional file))
1231 (defun package--prepare-dependencies (deps)
1232 "Turn DEPS into an acceptable list of dependencies.
1234 Any parts missing a version string get a default version string
1235 of \"0\" (meaning any version) and an appropriate level of lists
1236 is wrapped around any parts requiring it."
1237 (cond
1238 ((not (listp deps))
1239 (error "Invalid requirement specifier: %S" deps))
1240 (t (mapcar (lambda (dep)
1241 (cond
1242 ((symbolp dep) `(,dep "0"))
1243 ((stringp dep)
1244 (error "Invalid requirement specifier: %S" dep))
1245 ((and (listp dep) (null (cdr dep)))
1246 (list (car dep) "0"))
1247 (t dep)))
1248 deps))))
1250 (defun package-buffer-info ()
1251 "Return a `package-desc' describing the package in the current buffer.
1253 If the buffer does not contain a conforming package, signal an
1254 error. If there is a package, narrow the buffer to the file's
1255 boundaries."
1256 (goto-char (point-min))
1257 (unless (re-search-forward "^;;; \\([^ ]*\\)\\.el ---[ \t]*\\(.*?\\)[ \t]*\\(-\\*-.*-\\*-[ \t]*\\)?$" nil t)
1258 (error "Package lacks a file header"))
1259 (let ((file-name (match-string-no-properties 1))
1260 (desc (match-string-no-properties 2))
1261 (start (line-beginning-position)))
1262 (unless (search-forward (concat ";;; " file-name ".el ends here"))
1263 (error "Package lacks a terminating comment"))
1264 ;; Try to include a trailing newline.
1265 (forward-line)
1266 (narrow-to-region start (point))
1267 (require 'lisp-mnt)
1268 ;; Use some headers we've invented to drive the process.
1269 (let* ((requires-str (lm-header "package-requires"))
1270 ;; Prefer Package-Version; if defined, the package author
1271 ;; probably wants us to use it. Otherwise try Version.
1272 (pkg-version
1273 (or (package-strip-rcs-id (lm-header "package-version"))
1274 (package-strip-rcs-id (lm-header "version"))))
1275 (homepage (lm-homepage)))
1276 (unless pkg-version
1277 (error
1278 "Package lacks a \"Version\" or \"Package-Version\" header"))
1279 (package-desc-from-define
1280 file-name pkg-version desc
1281 (if requires-str
1282 (package--prepare-dependencies
1283 (package-read-from-string requires-str)))
1284 :kind 'single
1285 :url homepage))))
1287 (declare-function tar-get-file-descriptor "tar-mode" (file))
1288 (declare-function tar--extract "tar-mode" (descriptor))
1290 (defun package-tar-file-info ()
1291 "Find package information for a tar file.
1292 The return result is a `package-desc'."
1293 (cl-assert (derived-mode-p 'tar-mode))
1294 (let* ((dir-name (file-name-directory
1295 (tar-header-name (car tar-parse-info))))
1296 (desc-file (package--description-file dir-name))
1297 (tar-desc (tar-get-file-descriptor (concat dir-name desc-file))))
1298 (unless tar-desc
1299 (error "No package descriptor file found"))
1300 (with-current-buffer (tar--extract tar-desc)
1301 (unwind-protect
1302 (package--read-pkg-desc 'tar)
1303 (kill-buffer (current-buffer))))))
1305 (defun package-dir-info ()
1306 "Find package information for a directory.
1307 The return result is a `package-desc'."
1308 (cl-assert (derived-mode-p 'dired-mode))
1309 (let* ((desc-file (package--description-file default-directory)))
1310 (if (file-readable-p desc-file)
1311 (with-temp-buffer
1312 (insert-file-contents desc-file)
1313 (package--read-pkg-desc 'dir))
1314 (let ((files (directory-files default-directory t "\\.el\\'" t))
1315 info)
1316 (while files
1317 (with-temp-buffer
1318 (insert-file-contents (pop files))
1319 (if (setq info (ignore-errors (package-buffer-info)))
1320 (setq files nil)
1321 (setf (package-desc-kind info) 'dir))))))))
1323 (defun package--read-pkg-desc (kind)
1324 "Read a `define-package' form in current buffer.
1325 Return the pkg-desc, with desc-kind set to KIND."
1326 (goto-char (point-min))
1327 (unwind-protect
1328 (let* ((pkg-def-parsed (read (current-buffer)))
1329 (pkg-desc
1330 (if (not (eq (car pkg-def-parsed) 'define-package))
1331 (error "Can't find define-package in %s"
1332 (tar-header-name tar-desc))
1333 (apply #'package-desc-from-define
1334 (append (cdr pkg-def-parsed))))))
1335 (setf (package-desc-kind pkg-desc) kind)
1336 pkg-desc)))
1339 ;;;###autoload
1340 (defun package-install-from-buffer ()
1341 "Install a package from the current buffer.
1342 The current buffer is assumed to be a single .el or .tar file or
1343 a directory. These must follow the packaging guidelines (see
1344 info node `(elisp)Packaging').
1346 Specially, if current buffer is a directory, the -pkg.el
1347 description file is not mandatory, in which case the information
1348 is derived from the main .el file in the directory.
1350 Downloads and installs required packages as needed."
1351 (interactive)
1352 (let ((pkg-desc
1353 (cond
1354 ((derived-mode-p 'dired-mode)
1355 ;; This is the only way a package-desc object with a `dir'
1356 ;; desc-kind can be created. Such packages can't be
1357 ;; uploaded or installed from archives, they can only be
1358 ;; installed from local buffers or directories.
1359 (package-dir-info))
1360 ((derived-mode-p 'tar-mode)
1361 (package-tar-file-info))
1363 (package-buffer-info)))))
1364 ;; Download and install the dependencies.
1365 (let* ((requires (package-desc-reqs pkg-desc))
1366 (transaction (package-compute-transaction nil requires)))
1367 (package-download-transaction transaction))
1368 ;; Install the package itself.
1369 (package-unpack pkg-desc)
1370 pkg-desc))
1372 ;;;###autoload
1373 (defun package-install-file (file)
1374 "Install a package from a file.
1375 The file can either be a tar file or an Emacs Lisp file."
1376 (interactive "fPackage file name: ")
1377 (with-temp-buffer
1378 (if (file-directory-p file)
1379 (progn
1380 (setq default-directory file)
1381 (dired-mode))
1382 (insert-file-contents-literally file)
1383 (when (string-match "\\.tar\\'" file) (tar-mode)))
1384 (package-install-from-buffer)))
1386 (defun package-delete (pkg-desc)
1387 (let ((dir (package-desc-dir pkg-desc)))
1388 (if (not (string-prefix-p (file-name-as-directory
1389 (expand-file-name package-user-dir))
1390 (expand-file-name dir)))
1391 ;; Don't delete "system" packages.
1392 (error "Package `%s' is a system package, not deleting"
1393 (package-desc-full-name pkg-desc))
1394 (delete-directory dir t t)
1395 ;; Remove NAME-VERSION.signed file.
1396 (let ((signed-file (concat dir ".signed")))
1397 (if (file-exists-p signed-file)
1398 (delete-file signed-file)))
1399 ;; Update package-alist.
1400 (let* ((name (package-desc-name pkg-desc))
1401 (pkgs (assq name package-alist)))
1402 (delete pkg-desc pkgs)
1403 (unless (cdr pkgs)
1404 (setq package-alist (delq pkgs package-alist))))
1405 (message "Package `%s' deleted." (package-desc-full-name pkg-desc)))))
1407 (defun package-archive-base (desc)
1408 "Return the archive containing the package NAME."
1409 (cdr (assoc (package-desc-archive desc) package-archives)))
1411 (defun package-archive-priority (archive)
1412 "Return the priority of ARCHIVE.
1414 The archive priorities are specified in
1415 `package-archive-priorities'. If not given there, the priority
1416 defaults to 0."
1417 (or (cdr (assoc archive package-archive-priorities))
1420 (defun package-desc-priority-version (pkg-desc)
1421 "Return the version PKG-DESC with the archive priority prepended.
1423 This allows for easy comparison of package versions from
1424 different archives if archive priorities are meant to be taken in
1425 consideration."
1426 (cons (package-archive-priority
1427 (package-desc-archive pkg-desc))
1428 (package-desc-version pkg-desc)))
1430 (defun package--download-one-archive (archive file)
1431 "Retrieve an archive file FILE from ARCHIVE, and cache it.
1432 ARCHIVE should be a cons cell of the form (NAME . LOCATION),
1433 similar to an entry in `package-alist'. Save the cached copy to
1434 \"archives/NAME/archive-contents\" in `package-user-dir'."
1435 (let ((dir (expand-file-name (format "archives/%s" (car archive))
1436 package-user-dir))
1437 (sig-file (concat file ".sig"))
1438 good-signatures)
1439 (package--with-work-buffer (cdr archive) file
1440 ;; Check signature of archive-contents, if desired.
1441 (if (and package-check-signature
1442 (not (member archive package-unsigned-archives)))
1443 (if (package--archive-file-exists-p (cdr archive) sig-file)
1444 (setq good-signatures (package--check-signature (cdr archive)
1445 file))
1446 (unless (eq package-check-signature 'allow-unsigned)
1447 (error "Unsigned archive `%s'"
1448 (car archive)))))
1449 ;; Read the retrieved buffer to make sure it is valid (e.g. it
1450 ;; may fetch a URL redirect page).
1451 (when (listp (read (current-buffer)))
1452 (make-directory dir t)
1453 (write-region nil nil (expand-file-name file dir) nil 'silent)))
1454 (when good-signatures
1455 ;; Write out good signatures into archive-contents.signed file.
1456 (write-region (mapconcat #'epg-signature-to-string good-signatures "\n")
1458 (expand-file-name (concat file ".signed") dir)
1459 nil 'silent))))
1461 (declare-function epg-check-configuration "epg-config"
1462 (config &optional minimum-version))
1463 (declare-function epg-configuration "epg-config" ())
1464 (declare-function epg-import-keys-from-file "epg" (context keys))
1466 ;;;###autoload
1467 (defun package-import-keyring (&optional file)
1468 "Import keys from FILE."
1469 (interactive "fFile: ")
1470 (setq file (expand-file-name file))
1471 (let ((context (epg-make-context 'OpenPGP))
1472 (homedir (expand-file-name "gnupg" package-user-dir)))
1473 (with-file-modes 448
1474 (make-directory homedir t))
1475 (setf (epg-context-home-directory context) homedir)
1476 (message "Importing %s..." (file-name-nondirectory file))
1477 (epg-import-keys-from-file context file)
1478 (message "Importing %s...done" (file-name-nondirectory file))))
1480 ;;;###autoload
1481 (defun package-refresh-contents ()
1482 "Download the ELPA archive description if needed.
1483 This informs Emacs about the latest versions of all packages, and
1484 makes them available for download."
1485 (interactive)
1486 ;; FIXME: Do it asynchronously.
1487 (unless (file-exists-p package-user-dir)
1488 (make-directory package-user-dir t))
1489 (let ((default-keyring (expand-file-name "package-keyring.gpg"
1490 data-directory)))
1491 (when (and package-check-signature (file-exists-p default-keyring))
1492 (condition-case-unless-debug error
1493 (progn
1494 (epg-check-configuration (epg-configuration))
1495 (package-import-keyring default-keyring))
1496 (error (message "Cannot import default keyring: %S" (cdr error))))))
1497 (dolist (archive package-archives)
1498 (condition-case-unless-debug nil
1499 (package--download-one-archive archive "archive-contents")
1500 (error (message "Failed to download `%s' archive."
1501 (car archive)))))
1502 (package-read-all-archive-contents))
1504 ;;;###autoload
1505 (defun package-initialize (&optional no-activate)
1506 "Load Emacs Lisp packages, and activate them.
1507 The variable `package-load-list' controls which packages to load.
1508 If optional arg NO-ACTIVATE is non-nil, don't activate packages."
1509 (interactive)
1510 (setq package-alist nil)
1511 (package-load-all-descriptors)
1512 (package-read-all-archive-contents)
1513 (unless no-activate
1514 (dolist (elt package-alist)
1515 (package-activate (car elt))))
1516 (setq package--initialized t))
1519 ;;;; Package description buffer.
1521 ;;;###autoload
1522 (defun describe-package (package)
1523 "Display the full documentation of PACKAGE (a symbol)."
1524 (interactive
1525 (let* ((guess (function-called-at-point)))
1526 (require 'finder-inf nil t)
1527 ;; Load the package list if necessary (but don't activate them).
1528 (unless package--initialized
1529 (package-initialize t))
1530 (let ((packages (append (mapcar 'car package-alist)
1531 (mapcar 'car package-archive-contents)
1532 (mapcar 'car package--builtins))))
1533 (unless (memq guess packages)
1534 (setq guess nil))
1535 (setq packages (mapcar 'symbol-name packages))
1536 (let ((val
1537 (completing-read (if guess
1538 (format "Describe package (default %s): "
1539 guess)
1540 "Describe package: ")
1541 packages nil t nil nil guess)))
1542 (list (intern val))))))
1543 (if (not (or (package-desc-p package) (and package (symbolp package))))
1544 (message "No package specified")
1545 (help-setup-xref (list #'describe-package package)
1546 (called-interactively-p 'interactive))
1547 (with-help-window (help-buffer)
1548 (with-current-buffer standard-output
1549 (describe-package-1 package)))))
1551 (defun describe-package-1 (pkg)
1552 (require 'lisp-mnt)
1553 (let* ((desc (or
1554 (if (package-desc-p pkg) pkg)
1555 (cadr (assq pkg package-alist))
1556 (let ((built-in (assq pkg package--builtins)))
1557 (if built-in
1558 (package--from-builtin built-in)
1559 (cadr (assq pkg package-archive-contents))))))
1560 (name (if desc (package-desc-name desc) pkg))
1561 (pkg-dir (if desc (package-desc-dir desc)))
1562 (reqs (if desc (package-desc-reqs desc)))
1563 (version (if desc (package-desc-version desc)))
1564 (archive (if desc (package-desc-archive desc)))
1565 (extras (and desc (package-desc-extras desc)))
1566 (homepage (cdr (assoc :url extras)))
1567 (keywords (if desc (package-desc--keywords desc)))
1568 (built-in (eq pkg-dir 'builtin))
1569 (installable (and archive (not built-in)))
1570 (status (if desc (package-desc-status desc) "orphan"))
1571 (signed (if desc (package-desc-signed desc))))
1572 (prin1 name)
1573 (princ " is ")
1574 (princ (if (memq (aref status 0) '(?a ?e ?i ?o ?u)) "an " "a "))
1575 (princ status)
1576 (princ " package.\n\n")
1578 (insert " " (propertize "Status" 'font-lock-face 'bold) ": ")
1579 (cond (built-in
1580 (insert (propertize (capitalize status)
1581 'font-lock-face 'font-lock-builtin-face)
1582 "."))
1583 (pkg-dir
1584 (insert (propertize (if (equal status "unsigned")
1585 "Installed"
1586 (capitalize status)) ;FIXME: Why comment-face?
1587 'font-lock-face 'font-lock-comment-face))
1588 (insert " in `")
1589 ;; Todo: Add button for uninstalling.
1590 (help-insert-xref-button (abbreviate-file-name
1591 (file-name-as-directory pkg-dir))
1592 'help-package-def pkg-dir)
1593 (if (and (package-built-in-p name)
1594 (not (package-built-in-p name version)))
1595 (insert "',\n shadowing a "
1596 (propertize "built-in package"
1597 'font-lock-face 'font-lock-builtin-face))
1598 (insert "'"))
1599 (if signed
1600 (insert ".")
1601 (insert " (unsigned).")))
1602 (installable
1603 (insert (capitalize status))
1604 (insert " from " (format "%s" archive))
1605 (insert " -- ")
1606 (package-make-button
1607 "Install"
1608 'action 'package-install-button-action
1609 'package-desc desc))
1610 (t (insert (capitalize status) ".")))
1611 (insert "\n")
1612 (insert " " (propertize "Archive" 'font-lock-face 'bold)
1613 ": " (or archive "n/a") "\n")
1614 (and version
1615 (insert " "
1616 (propertize "Version" 'font-lock-face 'bold) ": "
1617 (package-version-join version) "\n"))
1619 (setq reqs (if desc (package-desc-reqs desc)))
1620 (when reqs
1621 (insert " " (propertize "Requires" 'font-lock-face 'bold) ": ")
1622 (let ((first t)
1623 name vers text)
1624 (dolist (req reqs)
1625 (setq name (car req)
1626 vers (cadr req)
1627 text (format "%s-%s" (symbol-name name)
1628 (package-version-join vers)))
1629 (cond (first (setq first nil))
1630 ((>= (+ 2 (current-column) (length text))
1631 (window-width))
1632 (insert ",\n "))
1633 (t (insert ", ")))
1634 (help-insert-xref-button text 'help-package name))
1635 (insert "\n")))
1636 (insert " " (propertize "Summary" 'font-lock-face 'bold)
1637 ": " (if desc (package-desc-summary desc)) "\n")
1638 (when homepage
1639 (insert " " (propertize "Homepage" 'font-lock-face 'bold) ": ")
1640 (help-insert-xref-button homepage 'help-url homepage)
1641 (insert "\n"))
1642 (when keywords
1643 (insert " " (propertize "Keywords" 'font-lock-face 'bold) ": ")
1644 (dolist (k keywords)
1645 (package-make-button
1647 'package-keyword k
1648 'action 'package-keyword-button-action)
1649 (insert " "))
1650 (insert "\n"))
1651 (let* ((all-pkgs (append (cdr (assq name package-alist))
1652 (cdr (assq name package-archive-contents))
1653 (let ((bi (assq name package--builtins)))
1654 (if bi (list (package--from-builtin bi))))))
1655 (other-pkgs (delete desc all-pkgs)))
1656 (when other-pkgs
1657 (insert " " (propertize "Other versions" 'font-lock-face 'bold) ": "
1658 (mapconcat
1659 (lambda (opkg)
1660 (let* ((ov (package-desc-version opkg))
1661 (dir (package-desc-dir opkg))
1662 (from (or (package-desc-archive opkg)
1663 (if (stringp dir) "installed" dir))))
1664 (if (not ov) (format "%s" from)
1665 (format "%s (%s)"
1666 (make-text-button (package-version-join ov) nil
1667 'face 'link
1668 'follow-link t
1669 'action
1670 (lambda (_button)
1671 (describe-package opkg)))
1672 from))))
1673 other-pkgs ", ")
1674 ".\n")))
1676 (insert "\n")
1678 (if built-in
1679 ;; For built-in packages, insert the commentary.
1680 (let ((fn (locate-file (format "%s.el" name) load-path
1681 load-file-rep-suffixes))
1682 (opoint (point)))
1683 (insert (or (lm-commentary fn) ""))
1684 (save-excursion
1685 (goto-char opoint)
1686 (when (re-search-forward "^;;; Commentary:\n" nil t)
1687 (replace-match ""))
1688 (while (re-search-forward "^\\(;+ ?\\)" nil t)
1689 (replace-match ""))))
1690 (let ((readme (expand-file-name (format "%s-readme.txt" name)
1691 package-user-dir))
1692 readme-string)
1693 ;; For elpa packages, try downloading the commentary. If that
1694 ;; fails, try an existing readme file in `package-user-dir'.
1695 (cond ((condition-case nil
1696 (save-excursion
1697 (package--with-work-buffer
1698 (package-archive-base desc)
1699 (format "%s-readme.txt" name)
1700 (save-excursion
1701 (goto-char (point-max))
1702 (unless (bolp)
1703 (insert ?\n)))
1704 (write-region nil nil
1705 (expand-file-name readme package-user-dir)
1706 nil 'silent)
1707 (setq readme-string (buffer-string))
1709 (error nil))
1710 (insert readme-string))
1711 ((file-readable-p readme)
1712 (insert-file-contents readme)
1713 (goto-char (point-max))))))))
1715 (defun package-install-button-action (button)
1716 (let ((pkg-desc (button-get button 'package-desc)))
1717 (when (y-or-n-p (format "Install package `%s'? "
1718 (package-desc-full-name pkg-desc)))
1719 (package-install pkg-desc)
1720 (revert-buffer nil t)
1721 (goto-char (point-min)))))
1723 (defun package-keyword-button-action (button)
1724 (let ((pkg-keyword (button-get button 'package-keyword)))
1725 (package-show-package-list t (list pkg-keyword))))
1727 (defun package-make-button (text &rest props)
1728 (let ((button-text (if (display-graphic-p) text (concat "[" text "]")))
1729 (button-face (if (display-graphic-p)
1730 '(:box (:line-width 2 :color "dark grey")
1731 :background "light grey"
1732 :foreground "black")
1733 'link)))
1734 (apply 'insert-text-button button-text 'face button-face 'follow-link t
1735 props)))
1738 ;;;; Package menu mode.
1740 (defvar package-menu-mode-map
1741 (let ((map (make-sparse-keymap))
1742 (menu-map (make-sparse-keymap "Package")))
1743 (set-keymap-parent map tabulated-list-mode-map)
1744 (define-key map "\C-m" 'package-menu-describe-package)
1745 (define-key map "u" 'package-menu-mark-unmark)
1746 (define-key map "\177" 'package-menu-backup-unmark)
1747 (define-key map "d" 'package-menu-mark-delete)
1748 (define-key map "i" 'package-menu-mark-install)
1749 (define-key map "U" 'package-menu-mark-upgrades)
1750 (define-key map "r" 'package-menu-refresh)
1751 (define-key map "f" 'package-menu-filter)
1752 (define-key map "~" 'package-menu-mark-obsolete-for-deletion)
1753 (define-key map "x" 'package-menu-execute)
1754 (define-key map "h" 'package-menu-quick-help)
1755 (define-key map "?" 'package-menu-describe-package)
1756 (define-key map [menu-bar package-menu] (cons "Package" menu-map))
1757 (define-key menu-map [mq]
1758 '(menu-item "Quit" quit-window
1759 :help "Quit package selection"))
1760 (define-key menu-map [s1] '("--"))
1761 (define-key menu-map [mn]
1762 '(menu-item "Next" next-line
1763 :help "Next Line"))
1764 (define-key menu-map [mp]
1765 '(menu-item "Previous" previous-line
1766 :help "Previous Line"))
1767 (define-key menu-map [s2] '("--"))
1768 (define-key menu-map [mu]
1769 '(menu-item "Unmark" package-menu-mark-unmark
1770 :help "Clear any marks on a package and move to the next line"))
1771 (define-key menu-map [munm]
1772 '(menu-item "Unmark Backwards" package-menu-backup-unmark
1773 :help "Back up one line and clear any marks on that package"))
1774 (define-key menu-map [md]
1775 '(menu-item "Mark for Deletion" package-menu-mark-delete
1776 :help "Mark a package for deletion and move to the next line"))
1777 (define-key menu-map [mi]
1778 '(menu-item "Mark for Install" package-menu-mark-install
1779 :help "Mark a package for installation and move to the next line"))
1780 (define-key menu-map [mupgrades]
1781 '(menu-item "Mark Upgradable Packages" package-menu-mark-upgrades
1782 :help "Mark packages that have a newer version for upgrading"))
1783 (define-key menu-map [s3] '("--"))
1784 (define-key menu-map [mf]
1785 '(menu-item "Filter Package List..." package-menu-filter
1786 :help "Filter package selection (q to go back)"))
1787 (define-key menu-map [mg]
1788 '(menu-item "Update Package List" revert-buffer
1789 :help "Update the list of packages"))
1790 (define-key menu-map [mr]
1791 '(menu-item "Refresh Package List" package-menu-refresh
1792 :help "Download the ELPA archive"))
1793 (define-key menu-map [s4] '("--"))
1794 (define-key menu-map [mt]
1795 '(menu-item "Mark Obsolete Packages" package-menu-mark-obsolete-for-deletion
1796 :help "Mark all obsolete packages for deletion"))
1797 (define-key menu-map [mx]
1798 '(menu-item "Execute Actions" package-menu-execute
1799 :help "Perform all the marked actions"))
1800 (define-key menu-map [s5] '("--"))
1801 (define-key menu-map [mh]
1802 '(menu-item "Help" package-menu-quick-help
1803 :help "Show short key binding help for package-menu-mode"))
1804 (define-key menu-map [mc]
1805 '(menu-item "Describe Package" package-menu-describe-package
1806 :help "Display information about this package"))
1807 map)
1808 "Local keymap for `package-menu-mode' buffers.")
1810 (defvar package-menu--new-package-list nil
1811 "List of newly-available packages since `list-packages' was last called.")
1813 (define-derived-mode package-menu-mode tabulated-list-mode "Package Menu"
1814 "Major mode for browsing a list of packages.
1815 Letters do not insert themselves; instead, they are commands.
1816 \\<package-menu-mode-map>
1817 \\{package-menu-mode-map}"
1818 (setq tabulated-list-format
1819 `[("Package" 18 package-menu--name-predicate)
1820 ("Version" 13 nil)
1821 ("Status" 10 package-menu--status-predicate)
1822 ,@(if (cdr package-archives)
1823 '(("Archive" 10 package-menu--archive-predicate)))
1824 ("Description" 0 nil)])
1825 (setq tabulated-list-padding 2)
1826 (setq tabulated-list-sort-key (cons "Status" nil))
1827 (add-hook 'tabulated-list-revert-hook 'package-menu--refresh nil t)
1828 (tabulated-list-init-header))
1830 (defmacro package--push (pkg-desc status listname)
1831 "Convenience macro for `package-menu--generate'.
1832 If the alist stored in the symbol LISTNAME lacks an entry for a
1833 package PKG-DESC, add one. The alist is keyed with PKG-DESC."
1834 `(unless (assoc ,pkg-desc ,listname)
1835 ;; FIXME: Should we move status into pkg-desc?
1836 (push (cons ,pkg-desc ,status) ,listname)))
1838 (defvar package-list-unversioned nil
1839 "If non-nil include packages that don't have a version in `list-package'.")
1841 (defvar package-list-unsigned nil
1842 "If non-nil, mention in the list which packages were installed w/o signature.")
1844 (defun package-desc-status (pkg-desc)
1845 (let* ((name (package-desc-name pkg-desc))
1846 (dir (package-desc-dir pkg-desc))
1847 (lle (assq name package-load-list))
1848 (held (cadr lle))
1849 (version (package-desc-version pkg-desc))
1850 (signed (package-desc-signed pkg-desc)))
1851 (cond
1852 ((eq dir 'builtin) "built-in")
1853 ((and lle (null held)) "disabled")
1854 ((stringp held)
1855 (let ((hv (if (stringp held) (version-to-list held))))
1856 (cond
1857 ((version-list-= version hv) "held")
1858 ((version-list-< version hv) "obsolete")
1859 (t "disabled"))))
1860 ((package-built-in-p name version) "obsolete")
1861 (dir ;One of the installed packages.
1862 (cond
1863 ((not (file-exists-p (package-desc-dir pkg-desc))) "deleted")
1864 ((eq pkg-desc (cadr (assq name package-alist)))
1865 (if (or (not package-list-unsigned) signed) "installed" "unsigned"))
1866 (t "obsolete")))
1868 (let* ((ins (cadr (assq name package-alist)))
1869 (ins-v (if ins (package-desc-version ins))))
1870 (cond
1871 ((or (null ins) (version-list-< ins-v version))
1872 (if (memq name package-menu--new-package-list)
1873 "new" "available"))
1874 ((version-list-< version ins-v) "obsolete")
1875 ((version-list-= version ins-v)
1876 (if (or (not package-list-unsigned) signed)
1877 "installed" "unsigned"))))))))
1879 (defun package-menu--refresh (&optional packages keywords)
1880 "Re-populate the `tabulated-list-entries'.
1881 PACKAGES should be nil or t, which means to display all known packages.
1882 KEYWORDS should be nil or a list of keywords."
1883 ;; Construct list of (PKG-DESC . STATUS).
1884 (unless packages (setq packages t))
1885 (let (info-list name)
1886 ;; Installed packages:
1887 (dolist (elt package-alist)
1888 (setq name (car elt))
1889 (when (or (eq packages t) (memq name packages))
1890 (dolist (pkg (cdr elt))
1891 (when (package--has-keyword-p pkg keywords)
1892 (package--push pkg (package-desc-status pkg) info-list)))))
1894 ;; Built-in packages:
1895 (dolist (elt package--builtins)
1896 (setq name (car elt))
1897 (when (and (not (eq name 'emacs)) ; Hide the `emacs' package.
1898 (package--has-keyword-p (package--from-builtin elt) keywords)
1899 (or package-list-unversioned
1900 (package--bi-desc-version (cdr elt)))
1901 (or (eq packages t) (memq name packages)))
1902 (package--push (package--from-builtin elt) "built-in" info-list)))
1904 ;; Available and disabled packages:
1905 (dolist (elt package-archive-contents)
1906 (setq name (car elt))
1907 (when (or (eq packages t) (memq name packages))
1908 (dolist (pkg (cdr elt))
1909 ;; Hide obsolete packages.
1910 (when (and (not (package-installed-p (package-desc-name pkg)
1911 (package-desc-version pkg)))
1912 (package--has-keyword-p pkg keywords))
1913 (package--push pkg (package-desc-status pkg) info-list)))))
1915 ;; Print the result.
1916 (setq tabulated-list-entries
1917 (mapcar #'package-menu--print-info info-list))))
1919 (defun package-all-keywords ()
1920 "Collect all package keywords"
1921 (let (keywords)
1922 (package--mapc (lambda (desc)
1923 (let* ((desc-keywords (and desc (package-desc--keywords desc))))
1924 (setq keywords (append keywords desc-keywords)))))
1925 keywords))
1927 (defun package--mapc (function &optional packages)
1928 "Call FUNCTION for all known PACKAGES.
1929 PACKAGES can be nil or t, which means to display all known
1930 packages, or a list of packages.
1932 Built-in packages are converted with `package--from-builtin'."
1933 (unless packages (setq packages t))
1934 (let (name)
1935 ;; Installed packages:
1936 (dolist (elt package-alist)
1937 (setq name (car elt))
1938 (when (or (eq packages t) (memq name packages))
1939 (mapc function (cdr elt))))
1941 ;; Built-in packages:
1942 (dolist (elt package--builtins)
1943 (setq name (car elt))
1944 (when (and (not (eq name 'emacs)) ; Hide the `emacs' package.
1945 (or package-list-unversioned
1946 (package--bi-desc-version (cdr elt)))
1947 (or (eq packages t) (memq name packages)))
1948 (funcall function (package--from-builtin elt))))
1950 ;; Available and disabled packages:
1951 (dolist (elt package-archive-contents)
1952 (setq name (car elt))
1953 (when (or (eq packages t) (memq name packages))
1954 (dolist (pkg (cdr elt))
1955 ;; Hide obsolete packages.
1956 (unless (package-installed-p (package-desc-name pkg)
1957 (package-desc-version pkg))
1958 (funcall function pkg)))))))
1960 (defun package--has-keyword-p (desc &optional keywords)
1961 "Test if package DESC has any of the given KEYWORDS.
1962 When none are given, the package matches."
1963 (if keywords
1964 (let* ((desc-keywords (and desc (package-desc--keywords desc)))
1965 found)
1966 (dolist (k keywords)
1967 (when (and (not found)
1968 (member k desc-keywords))
1969 (setq found t)))
1970 found)
1973 (defun package-menu--generate (remember-pos packages &optional keywords)
1974 "Populate the Package Menu.
1975 If REMEMBER-POS is non-nil, keep point on the same entry.
1976 PACKAGES should be t, which means to display all known packages,
1977 or a list of package names (symbols) to display.
1979 With KEYWORDS given, only packages with those keywords are
1980 shown."
1981 (package-menu--refresh packages keywords)
1982 (setf (car (aref tabulated-list-format 0))
1983 (if keywords
1984 (let ((filters (mapconcat 'identity keywords ",")))
1985 (concat "Package[" filters "]"))
1986 "Package"))
1987 (if keywords
1988 (define-key package-menu-mode-map "q" 'package-show-package-list)
1989 (define-key package-menu-mode-map "q" 'quit-window))
1990 (tabulated-list-init-header)
1991 (tabulated-list-print remember-pos))
1993 (defun package-menu--print-info (pkg)
1994 "Return a package entry suitable for `tabulated-list-entries'.
1995 PKG has the form (PKG-DESC . STATUS).
1996 Return (PKG-DESC [NAME VERSION STATUS DOC])."
1997 (let* ((pkg-desc (car pkg))
1998 (status (cdr pkg))
1999 (face (pcase status
2000 (`"built-in" 'font-lock-builtin-face)
2001 (`"available" 'default)
2002 (`"new" 'bold)
2003 (`"held" 'font-lock-constant-face)
2004 (`"disabled" 'font-lock-warning-face)
2005 (`"installed" 'font-lock-comment-face)
2006 (`"unsigned" 'font-lock-warning-face)
2007 (_ 'font-lock-warning-face)))) ; obsolete.
2008 (list pkg-desc
2009 `[,(list (symbol-name (package-desc-name pkg-desc))
2010 'face 'link
2011 'follow-link t
2012 'package-desc pkg-desc
2013 'action 'package-menu-describe-package)
2014 ,(propertize (package-version-join
2015 (package-desc-version pkg-desc))
2016 'font-lock-face face)
2017 ,(propertize status 'font-lock-face face)
2018 ,@(if (cdr package-archives)
2019 (list (propertize (or (package-desc-archive pkg-desc) "")
2020 'font-lock-face face)))
2021 ,(propertize (package-desc-summary pkg-desc)
2022 'font-lock-face face)])))
2024 (defun package-menu-refresh ()
2025 "Download the Emacs Lisp package archive.
2026 This fetches the contents of each archive specified in
2027 `package-archives', and then refreshes the package menu."
2028 (interactive)
2029 (unless (derived-mode-p 'package-menu-mode)
2030 (user-error "The current buffer is not a Package Menu"))
2031 (package-refresh-contents)
2032 (package-menu--generate t t))
2034 (defun package-menu-describe-package (&optional button)
2035 "Describe the current package.
2036 If optional arg BUTTON is non-nil, describe its associated package."
2037 (interactive)
2038 (let ((pkg-desc (if button (button-get button 'package-desc)
2039 (tabulated-list-get-id))))
2040 (if pkg-desc
2041 (describe-package pkg-desc)
2042 (user-error "No package here"))))
2044 ;; fixme numeric argument
2045 (defun package-menu-mark-delete (&optional _num)
2046 "Mark a package for deletion and move to the next line."
2047 (interactive "p")
2048 (if (member (package-menu-get-status) '("installed" "obsolete" "unsigned"))
2049 (tabulated-list-put-tag "D" t)
2050 (forward-line)))
2052 (defun package-menu-mark-install (&optional _num)
2053 "Mark a package for installation and move to the next line."
2054 (interactive "p")
2055 (if (member (package-menu-get-status) '("available" "new"))
2056 (tabulated-list-put-tag "I" t)
2057 (forward-line)))
2059 (defun package-menu-mark-unmark (&optional _num)
2060 "Clear any marks on a package and move to the next line."
2061 (interactive "p")
2062 (tabulated-list-put-tag " " t))
2064 (defun package-menu-backup-unmark ()
2065 "Back up one line and clear any marks on that package."
2066 (interactive)
2067 (forward-line -1)
2068 (tabulated-list-put-tag " "))
2070 (defun package-menu-mark-obsolete-for-deletion ()
2071 "Mark all obsolete packages for deletion."
2072 (interactive)
2073 (save-excursion
2074 (goto-char (point-min))
2075 (while (not (eobp))
2076 (if (equal (package-menu-get-status) "obsolete")
2077 (tabulated-list-put-tag "D" t)
2078 (forward-line 1)))))
2080 (defun package-menu-quick-help ()
2081 "Show short key binding help for package-menu-mode."
2082 (interactive)
2083 (message "n-ext, i-nstall, d-elete, u-nmark, x-ecute, r-efresh, h-elp"))
2085 (define-obsolete-function-alias
2086 'package-menu-view-commentary 'package-menu-describe-package "24.1")
2088 (defun package-menu-get-status ()
2089 (let* ((id (tabulated-list-get-id))
2090 (entry (and id (assq id tabulated-list-entries))))
2091 (if entry
2092 (aref (cadr entry) 2)
2093 "")))
2095 (defun package-menu--find-upgrades ()
2096 (let (installed available upgrades)
2097 ;; Build list of installed/available packages in this buffer.
2098 (dolist (entry tabulated-list-entries)
2099 ;; ENTRY is (PKG-DESC [NAME VERSION STATUS DOC])
2100 (let ((pkg-desc (car entry))
2101 (status (aref (cadr entry) 2)))
2102 (cond ((member status '("installed" "unsigned"))
2103 (push pkg-desc installed))
2104 ((member status '("available" "new"))
2105 (setq available (package--append-to-alist pkg-desc available))))))
2106 ;; Loop through list of installed packages, finding upgrades.
2107 (dolist (pkg-desc installed)
2108 (let* ((name (package-desc-name pkg-desc))
2109 (avail-pkg (cadr (assq name available))))
2110 (and avail-pkg
2111 (version-list-< (package-desc-priority-version pkg-desc)
2112 (package-desc-priority-version avail-pkg))
2113 (push (cons name avail-pkg) upgrades))))
2114 upgrades))
2116 (defun package-menu-mark-upgrades ()
2117 "Mark all upgradable packages in the Package Menu.
2118 For each installed package with a newer version available, place
2119 an (I)nstall flag on the available version and a (D)elete flag on
2120 the installed version. A subsequent \\[package-menu-execute]
2121 call will upgrade the package."
2122 (interactive)
2123 (unless (derived-mode-p 'package-menu-mode)
2124 (error "The current buffer is not a Package Menu"))
2125 (let ((upgrades (package-menu--find-upgrades)))
2126 (if (null upgrades)
2127 (message "No packages to upgrade.")
2128 (widen)
2129 (save-excursion
2130 (goto-char (point-min))
2131 (while (not (eobp))
2132 (let* ((pkg-desc (tabulated-list-get-id))
2133 (upgrade (cdr (assq (package-desc-name pkg-desc) upgrades))))
2134 (cond ((null upgrade)
2135 (forward-line 1))
2136 ((equal pkg-desc upgrade)
2137 (package-menu-mark-install))
2139 (package-menu-mark-delete))))))
2140 (message "%d package%s marked for upgrading."
2141 (length upgrades)
2142 (if (= (length upgrades) 1) "" "s")))))
2144 (defun package-menu-execute (&optional noquery)
2145 "Perform marked Package Menu actions.
2146 Packages marked for installation are downloaded and installed;
2147 packages marked for deletion are removed.
2148 Optional argument NOQUERY non-nil means do not ask the user to confirm."
2149 (interactive)
2150 (unless (derived-mode-p 'package-menu-mode)
2151 (error "The current buffer is not in Package Menu mode"))
2152 (let (install-list delete-list cmd pkg-desc)
2153 (save-excursion
2154 (goto-char (point-min))
2155 (while (not (eobp))
2156 (setq cmd (char-after))
2157 (unless (eq cmd ?\s)
2158 ;; This is the key PKG-DESC.
2159 (setq pkg-desc (tabulated-list-get-id))
2160 (cond ((eq cmd ?D)
2161 (push pkg-desc delete-list))
2162 ((eq cmd ?I)
2163 (push pkg-desc install-list))))
2164 (forward-line)))
2165 (when install-list
2166 (if (or
2167 noquery
2168 (yes-or-no-p
2169 (if (= (length install-list) 1)
2170 (format "Install package `%s'? "
2171 (package-desc-full-name (car install-list)))
2172 (format "Install these %d packages (%s)? "
2173 (length install-list)
2174 (mapconcat #'package-desc-full-name
2175 install-list ", ")))))
2176 (mapc 'package-install install-list)))
2177 ;; Delete packages, prompting if necessary.
2178 (when delete-list
2179 (if (or
2180 noquery
2181 (yes-or-no-p
2182 (if (= (length delete-list) 1)
2183 (format "Delete package `%s'? "
2184 (package-desc-full-name (car delete-list)))
2185 (format "Delete these %d packages (%s)? "
2186 (length delete-list)
2187 (mapconcat #'package-desc-full-name
2188 delete-list ", ")))))
2189 (dolist (elt delete-list)
2190 (condition-case-unless-debug err
2191 (package-delete elt)
2192 (error (message (cadr err)))))
2193 (error "Aborted")))
2194 (if (or delete-list install-list)
2195 (package-menu--generate t t)
2196 (message "No operations specified."))))
2198 (defun package-menu--version-predicate (A B)
2199 (let ((vA (or (aref (cadr A) 1) '(0)))
2200 (vB (or (aref (cadr B) 1) '(0))))
2201 (if (version-list-= vA vB)
2202 (package-menu--name-predicate A B)
2203 (version-list-< vA vB))))
2205 (defun package-menu--status-predicate (A B)
2206 (let ((sA (aref (cadr A) 2))
2207 (sB (aref (cadr B) 2)))
2208 (cond ((string= sA sB)
2209 (package-menu--name-predicate A B))
2210 ((string= sA "new") t)
2211 ((string= sB "new") nil)
2212 ((string= sA "available") t)
2213 ((string= sB "available") nil)
2214 ((string= sA "installed") t)
2215 ((string= sB "installed") nil)
2216 ((string= sA "unsigned") t)
2217 ((string= sB "unsigned") nil)
2218 ((string= sA "held") t)
2219 ((string= sB "held") nil)
2220 ((string= sA "built-in") t)
2221 ((string= sB "built-in") nil)
2222 ((string= sA "obsolete") t)
2223 ((string= sB "obsolete") nil)
2224 (t (string< sA sB)))))
2226 (defun package-menu--description-predicate (A B)
2227 (let ((dA (aref (cadr A) 3))
2228 (dB (aref (cadr B) 3)))
2229 (if (string= dA dB)
2230 (package-menu--name-predicate A B)
2231 (string< dA dB))))
2233 (defun package-menu--name-predicate (A B)
2234 (string< (symbol-name (package-desc-name (car A)))
2235 (symbol-name (package-desc-name (car B)))))
2237 (defun package-menu--archive-predicate (A B)
2238 (string< (or (package-desc-archive (car A)) "")
2239 (or (package-desc-archive (car B)) "")))
2241 ;;;###autoload
2242 (defun list-packages (&optional no-fetch)
2243 "Display a list of packages.
2244 This first fetches the updated list of packages before
2245 displaying, unless a prefix argument NO-FETCH is specified.
2246 The list is displayed in a buffer named `*Packages*'."
2247 (interactive "P")
2248 (require 'finder-inf nil t)
2249 ;; Initialize the package system if necessary.
2250 (unless package--initialized
2251 (package-initialize t))
2252 (let (old-archives new-packages)
2253 (unless no-fetch
2254 ;; Read the locally-cached archive-contents.
2255 (package-read-all-archive-contents)
2256 (setq old-archives package-archive-contents)
2257 ;; Fetch the remote list of packages.
2258 (package-refresh-contents)
2259 ;; Find which packages are new.
2260 (dolist (elt package-archive-contents)
2261 (unless (assq (car elt) old-archives)
2262 (push (car elt) new-packages))))
2264 ;; Generate the Package Menu.
2265 (let ((buf (get-buffer-create "*Packages*")))
2266 (with-current-buffer buf
2267 (package-menu-mode)
2268 (set (make-local-variable 'package-menu--new-package-list)
2269 new-packages)
2270 (package-menu--generate nil t))
2271 ;; The package menu buffer has keybindings. If the user types
2272 ;; `M-x list-packages', that suggests it should become current.
2273 (switch-to-buffer buf))
2275 (let ((upgrades (package-menu--find-upgrades)))
2276 (if upgrades
2277 (message "%d package%s can be upgraded; type `%s' to mark %s for upgrading."
2278 (length upgrades)
2279 (if (= (length upgrades) 1) "" "s")
2280 (substitute-command-keys "\\[package-menu-mark-upgrades]")
2281 (if (= (length upgrades) 1) "it" "them"))))))
2283 ;;;###autoload
2284 (defalias 'package-list-packages 'list-packages)
2286 ;; Used in finder.el
2287 (defun package-show-package-list (&optional packages keywords)
2288 "Display PACKAGES in a *Packages* buffer.
2289 This is similar to `list-packages', but it does not fetch the
2290 updated list of packages, and it only displays packages with
2291 names in PACKAGES (which should be a list of symbols).
2293 When KEYWORDS are given, only packages with those KEYWORDS are
2294 shown."
2295 (interactive)
2296 (require 'finder-inf nil t)
2297 (let* ((buf (get-buffer-create "*Packages*"))
2298 (win (get-buffer-window buf)))
2299 (with-current-buffer buf
2300 (package-menu-mode)
2301 (package-menu--generate nil packages keywords))
2302 (if win
2303 (select-window win)
2304 (switch-to-buffer buf))))
2306 ;; package-menu--generate rebinds "q" on the fly, so we have to
2307 ;; hard-code the binding in the doc-string here.
2308 (defun package-menu-filter (keyword)
2309 "Filter the *Packages* buffer.
2310 Show only those items that relate to the specified KEYWORD.
2311 To restore the full package list, type `q'."
2312 (interactive (list (completing-read "Keyword: " (package-all-keywords))))
2313 (package-show-package-list t (list keyword)))
2315 (defun package-list-packages-no-fetch ()
2316 "Display a list of packages.
2317 Does not fetch the updated list of packages before displaying.
2318 The list is displayed in a buffer named `*Packages*'."
2319 (interactive)
2320 (list-packages t))
2322 (provide 'package)
2324 ;;; package.el ends here