* emacs-lisp/package.el (package--read-pkg-desc): New function.
[emacs.git] / lisp / emacs-lisp / package.el
blobd5906675596c0f92249706c31afa90b8f19a440d
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 'integer
243 :risky t
244 :group 'package
245 :version "25.1")
247 (defcustom package-pinned-packages nil
248 "An alist of packages that are pinned to specific archives.
249 This can be useful if you have multiple package archives enabled,
250 and want to control which archive a given package gets installed from.
252 Each element of the alist has the form (PACKAGE . ARCHIVE), where:
253 PACKAGE is a symbol representing a package
254 ARCHIVE is a string representing an archive (it should be the car of
255 an element in `package-archives', e.g. \"gnu\").
257 Adding an entry to this variable means that only ARCHIVE will be
258 considered as a source for PACKAGE. If other archives provide PACKAGE,
259 they are ignored (for this package). If ARCHIVE does not contain PACKAGE,
260 the package will be unavailable."
261 :type '(alist :key-type (symbol :tag "Package")
262 :value-type (string :tag "Archive name"))
263 ;; I don't really see why this is risky...
264 ;; I suppose it could prevent you receiving updates for a package,
265 ;; via an entry (PACKAGE . NON-EXISTING). Which could be an issue
266 ;; if PACKAGE has a known vulnerability that is fixed in newer versions.
267 :risky t
268 :group 'package
269 :version "24.4")
271 (defconst package-archive-version 1
272 "Version number of the package archive understood by this file.
273 Lower version numbers than this will probably be understood as well.")
275 ;; We don't prime the cache since it tends to get out of date.
276 (defvar package-archive-contents nil
277 "Cache of the contents of the Emacs Lisp Package Archive.
278 This is an alist mapping package names (symbols) to
279 non-empty lists of `package-desc' structures.")
280 (put 'package-archive-contents 'risky-local-variable t)
282 (defcustom package-user-dir (locate-user-emacs-file "elpa")
283 "Directory containing the user's Emacs Lisp packages.
284 The directory name should be absolute.
285 Apart from this directory, Emacs also looks for system-wide
286 packages in `package-directory-list'."
287 :type 'directory
288 :risky t
289 :group 'package
290 :version "24.1")
292 (defcustom package-directory-list
293 ;; Defaults are subdirs named "elpa" in the site-lisp dirs.
294 (let (result)
295 (dolist (f load-path)
296 (and (stringp f)
297 (equal (file-name-nondirectory f) "site-lisp")
298 (push (expand-file-name "elpa" f) result)))
299 (nreverse result))
300 "List of additional directories containing Emacs Lisp packages.
301 Each directory name should be absolute.
303 These directories contain packages intended for system-wide; in
304 contrast, `package-user-dir' contains packages for personal use."
305 :type '(repeat directory)
306 :risky t
307 :group 'package
308 :version "24.1")
310 (defvar epg-gpg-program)
312 (defcustom package-check-signature
313 (if (progn (require 'epg-config) (executable-find epg-gpg-program))
314 'allow-unsigned)
315 "Non-nil means to check package signatures when installing.
316 The value `allow-unsigned' means to still install a package even if
317 it is unsigned.
319 This also applies to the \"archive-contents\" file that lists the
320 contents of the archive."
321 :type '(choice (const nil :tag "Never")
322 (const allow-unsigned :tag "Allow unsigned")
323 (const t :tag "Check always"))
324 :risky t
325 :group 'package
326 :version "24.4")
328 (defcustom package-unsigned-archives nil
329 "List of archives where we do not check for package signatures."
330 :type '(repeat (string :tag "Archive name"))
331 :risky t
332 :group 'package
333 :version "24.4")
335 (defvar package--default-summary "No description available.")
337 (cl-defstruct (package-desc
338 ;; Rename the default constructor from `make-package-desc'.
339 (:constructor package-desc-create)
340 ;; Has the same interface as the old `define-package',
341 ;; which is still used in the "foo-pkg.el" files. Extra
342 ;; options can be supported by adding additional keys.
343 (:constructor
344 package-desc-from-define
345 (name-string version-string &optional summary requirements
346 &rest rest-plist
347 &aux
348 (name (intern name-string))
349 (version (version-to-list version-string))
350 (reqs (mapcar #'(lambda (elt)
351 (list (car elt)
352 (version-to-list (cadr elt))))
353 (if (eq 'quote (car requirements))
354 (nth 1 requirements)
355 requirements)))
356 (kind (plist-get rest-plist :kind))
357 (archive (plist-get rest-plist :archive))
358 (extras (let (alist)
359 (while rest-plist
360 (unless (memq (car rest-plist) '(:kind :archive))
361 (let ((value (cadr rest-plist)))
362 (when value
363 (push (cons (car rest-plist)
364 (if (eq (car-safe value) 'quote)
365 (cadr value)
366 value))
367 alist))))
368 (setq rest-plist (cddr rest-plist)))
369 alist)))))
370 "Structure containing information about an individual package.
371 Slots:
373 `name' Name of the package, as a symbol.
375 `version' Version of the package, as a version list.
377 `summary' Short description of the package, typically taken from
378 the first line of the file.
380 `reqs' Requirements of the package. A list of (PACKAGE
381 VERSION-LIST) naming the dependent package and the minimum
382 required version.
384 `kind' The distribution format of the package. Currently, it is
385 either `single' or `tar'.
387 `archive' The name of the archive (as a string) whence this
388 package came.
390 `dir' The directory where the package is installed (if installed),
391 `builtin' if it is built-in, or nil otherwise.
393 `extras' Optional alist of additional keyword-value pairs.
395 `signed' Flag to indicate that the package is signed by provider."
396 name
397 version
398 (summary package--default-summary)
399 reqs
400 kind
401 archive
403 extras
404 signed)
406 ;; Pseudo fields.
407 (defun package-desc-full-name (pkg-desc)
408 (format "%s-%s"
409 (package-desc-name pkg-desc)
410 (package-version-join (package-desc-version pkg-desc))))
412 (defun package-desc-suffix (pkg-desc)
413 (pcase (package-desc-kind pkg-desc)
414 (`single ".el")
415 (`tar ".tar")
416 (kind (error "Unknown package kind: %s" kind))))
418 (defun package-desc--keywords (pkg-desc)
419 (let ((keywords (cdr (assoc :keywords (package-desc-extras pkg-desc)))))
420 (if (eq (car-safe keywords) 'quote)
421 (nth 1 keywords)
422 keywords)))
424 ;; Package descriptor format used in finder-inf.el and package--builtins.
425 (cl-defstruct (package--bi-desc
426 (:constructor package-make-builtin (version summary))
427 (:type vector))
428 version
429 reqs
430 summary)
432 (defvar package--builtins nil
433 "Alist of built-in packages.
434 The actual value is initialized by loading the library
435 `finder-inf'; this is not done until it is needed, e.g. by the
436 function `package-built-in-p'.
438 Each element has the form (PKG . PACKAGE-BI-DESC), where PKG is a package
439 name (a symbol) and DESC is a `package--bi-desc' structure.")
440 (put 'package--builtins 'risky-local-variable t)
442 (defvar package-alist nil
443 "Alist of all packages available for activation.
444 Each element has the form (PKG . DESCS), where PKG is a package
445 name (a symbol) and DESCS is a non-empty list of `package-desc' structure,
446 sorted by decreasing versions.
448 This variable is set automatically by `package-load-descriptor',
449 called via `package-initialize'. To change which packages are
450 loaded and/or activated, customize `package-load-list'.")
451 (put 'package-alist 'risky-local-variable t)
453 (defvar package-activated-list nil
454 ;; FIXME: This should implicitly include all builtin packages.
455 "List of the names of currently activated packages.")
456 (put 'package-activated-list 'risky-local-variable t)
458 (defun package-version-join (vlist)
459 "Return the version string corresponding to the list VLIST.
460 This is, approximately, the inverse of `version-to-list'.
461 \(Actually, it returns only one of the possible inverses, since
462 `version-to-list' is a many-to-one operation.)"
463 (if (null vlist)
465 (let ((str-list (list "." (int-to-string (car vlist)))))
466 (dolist (num (cdr vlist))
467 (cond
468 ((>= num 0)
469 (push (int-to-string num) str-list)
470 (push "." str-list))
471 ((< num -4)
472 (error "Invalid version list `%s'" vlist))
474 ;; pre, or beta, or alpha
475 (cond ((equal "." (car str-list))
476 (pop str-list))
477 ((not (string-match "[0-9]+" (car str-list)))
478 (error "Invalid version list `%s'" vlist)))
479 (push (cond ((= num -1) "pre")
480 ((= num -2) "beta")
481 ((= num -3) "alpha")
482 ((= num -4) "snapshot"))
483 str-list))))
484 (if (equal "." (car str-list))
485 (pop str-list))
486 (apply 'concat (nreverse str-list)))))
488 (defun package-load-descriptor (pkg-dir)
489 "Load the description file in directory PKG-DIR."
490 (let ((pkg-file (expand-file-name (package--description-file pkg-dir)
491 pkg-dir))
492 (signed-file (concat pkg-dir ".signed")))
493 (when (file-exists-p pkg-file)
494 (with-temp-buffer
495 (insert-file-contents pkg-file)
496 (goto-char (point-min))
497 (let ((pkg-desc (package-process-define-package
498 (read (current-buffer)) pkg-file)))
499 (setf (package-desc-dir pkg-desc) pkg-dir)
500 (if (file-exists-p signed-file)
501 (setf (package-desc-signed pkg-desc) t))
502 pkg-desc)))))
504 (defun package-load-all-descriptors ()
505 "Load descriptors for installed Emacs Lisp packages.
506 This looks for package subdirectories in `package-user-dir' and
507 `package-directory-list'. The variable `package-load-list'
508 controls which package subdirectories may be loaded.
510 In each valid package subdirectory, this function loads the
511 description file containing a call to `define-package', which
512 updates `package-alist'."
513 (dolist (dir (cons package-user-dir package-directory-list))
514 (when (file-directory-p dir)
515 (dolist (subdir (directory-files dir))
516 (let ((pkg-dir (expand-file-name subdir dir)))
517 (when (file-directory-p pkg-dir)
518 (package-load-descriptor pkg-dir)))))))
520 (defun package-disabled-p (pkg-name version)
521 "Return whether PKG-NAME at VERSION can be activated.
522 The decision is made according to `package-load-list'.
523 Return nil if the package can be activated.
524 Return t if the package is completely disabled.
525 Return the max version (as a string) if the package is held at a lower version."
526 (let ((force (assq pkg-name package-load-list)))
527 (cond ((null force) (not (memq 'all package-load-list)))
528 ((null (setq force (cadr force))) t) ; disabled
529 ((eq force t) nil)
530 ((stringp force) ; held
531 (unless (version-list-= version (version-to-list force))
532 force))
533 (t (error "Invalid element in `package-load-list'")))))
535 (defun package-activate-1 (pkg-desc &optional reload)
536 "Activate package given by PKG-DESC, even if it was already active.
537 If RELOAD is non-nil, also `load' any files inside the package which
538 correspond to previously loaded files (those returned by
539 `package--list-loaded-files')."
540 (let* ((name (package-desc-name pkg-desc))
541 (pkg-dir (package-desc-dir pkg-desc))
542 (pkg-dir-dir (file-name-as-directory pkg-dir)))
543 (unless pkg-dir
544 (error "Internal error: unable to find directory for `%s'"
545 (package-desc-full-name pkg-desc)))
546 ;; Add to load path, add autoloads, and activate the package.
547 (let* ((old-lp load-path)
548 (autoloads-file (expand-file-name
549 (format "%s-autoloads" name) pkg-dir))
550 (loaded-files-list (and reload (package--list-loaded-files pkg-dir))))
551 (with-demoted-errors "Error in package-activate-1: %s"
552 (load autoloads-file nil t))
553 (when (and (eq old-lp load-path)
554 (not (or (member pkg-dir load-path)
555 (member pkg-dir-dir load-path))))
556 ;; Old packages don't add themselves to the `load-path', so we have to
557 ;; do it ourselves.
558 (push pkg-dir load-path))
559 ;; Call `load' on all files in `pkg-dir' already present in
560 ;; `load-history'. This is done so that macros in these files are updated
561 ;; to their new definitions. If another package is being installed which
562 ;; depends on this new definition, not doing this update would cause
563 ;; compilation errors and break the installation.
564 (with-demoted-errors "Error in package-activate-1: %s"
565 (mapc (lambda (feature) (load feature nil t))
566 ;; Skip autoloads file since we already evaluated it above.
567 (remove (file-truename autoloads-file) loaded-files-list))))
568 ;; Add info node.
569 (when (file-exists-p (expand-file-name "dir" pkg-dir))
570 ;; FIXME: not the friendliest, but simple.
571 (require 'info)
572 (info-initialize)
573 (push pkg-dir Info-directory-list))
574 (push name package-activated-list)
575 ;; Don't return nil.
578 (declare-function find-library-name "find-func" (library))
579 (defun package--list-loaded-files (dir)
580 "Recursively list all files in DIR which correspond to loaded features.
581 Returns the `file-name-sans-extension' of each file, relative to
582 DIR, sorted by most recently loaded last."
583 (let* ((history (delq nil
584 (mapcar (lambda (x)
585 (let ((f (car x)))
586 (and f (file-name-sans-extension f))))
587 load-history)))
588 (dir (file-truename dir))
589 ;; List all files that have already been loaded.
590 (list-of-conflicts
591 (delq
593 (mapcar
594 (lambda (x) (let* ((file (file-relative-name x dir))
595 ;; Previously loaded file, if any.
596 (previous
597 (ignore-errors
598 (file-name-sans-extension
599 (file-truename (find-library-name file)))))
600 (pos (when previous (member previous history))))
601 ;; Return (RELATIVE-FILENAME . HISTORY-POSITION)
602 (when pos
603 (cons (file-name-sans-extension file) (length pos)))))
604 (directory-files-recursively dir "\\`[^\\.].*\\.el\\'")))))
605 ;; Turn the list of (FILENAME . POS) back into a list of features. Files in
606 ;; subdirectories are returned relative to DIR (so not actually features).
607 (let ((default-directory (file-name-as-directory dir)))
608 (mapcar (lambda (x) (file-truename (car x)))
609 (sort list-of-conflicts
610 ;; Sort the files by ascending HISTORY-POSITION.
611 (lambda (x y) (< (cdr x) (cdr y))))))))
613 (defun package-built-in-p (package &optional min-version)
614 "Return true if PACKAGE is built-in to Emacs.
615 Optional arg MIN-VERSION, if non-nil, should be a version list
616 specifying the minimum acceptable version."
617 (if (package-desc-p package) ;; was built-in and then was converted
618 (eq 'builtin (package-desc-dir package))
619 (let ((bi (assq package package--builtin-versions)))
620 (cond
621 (bi (version-list-<= min-version (cdr bi)))
622 ((remove 0 min-version) nil)
624 (require 'finder-inf nil t) ; For `package--builtins'.
625 (assq package package--builtins))))))
627 (defun package--from-builtin (bi-desc)
628 (package-desc-create :name (pop bi-desc)
629 :version (package--bi-desc-version bi-desc)
630 :summary (package--bi-desc-summary bi-desc)
631 :dir 'builtin))
633 ;; This function goes ahead and activates a newer version of a package
634 ;; if an older one was already activated. This is not ideal; we'd at
635 ;; least need to check to see if the package has actually been loaded,
636 ;; and not merely activated.
637 (defun package-activate (package &optional force)
638 "Activate package PACKAGE.
639 If FORCE is true, (re-)activate it if it's already activated."
640 (let ((pkg-descs (cdr (assq package package-alist))))
641 ;; Check if PACKAGE is available in `package-alist'.
642 (while
643 (when pkg-descs
644 (let ((available-version (package-desc-version (car pkg-descs))))
645 (or (package-disabled-p package available-version)
646 ;; Prefer a builtin package.
647 (package-built-in-p package available-version))))
648 (setq pkg-descs (cdr pkg-descs)))
649 (cond
650 ;; If no such package is found, maybe it's built-in.
651 ((null pkg-descs)
652 (package-built-in-p package))
653 ;; If the package is already activated, just return t.
654 ((and (memq package package-activated-list) (not force))
656 ;; Otherwise, proceed with activation.
658 (let* ((pkg-vec (car pkg-descs))
659 (fail (catch 'dep-failure
660 ;; Activate its dependencies recursively.
661 (dolist (req (package-desc-reqs pkg-vec))
662 (unless (package-activate (car req))
663 (throw 'dep-failure req))))))
664 (if fail
665 (warn "Unable to activate package `%s'.
666 Required package `%s-%s' is unavailable"
667 package (car fail) (package-version-join (cadr fail)))
668 ;; If all goes well, activate the package itself.
669 (package-activate-1 pkg-vec force)))))))
671 (defun define-package (_name-string _version-string
672 &optional _docstring _requirements
673 &rest _extra-properties)
674 "Define a new package.
675 NAME-STRING is the name of the package, as a string.
676 VERSION-STRING is the version of the package, as a string.
677 DOCSTRING is a short description of the package, a string.
678 REQUIREMENTS is a list of dependencies on other packages.
679 Each requirement is of the form (OTHER-PACKAGE OTHER-VERSION),
680 where OTHER-VERSION is a string.
682 EXTRA-PROPERTIES is currently unused."
683 ;; FIXME: Placeholder! Should we keep it?
684 (error "Don't call me!"))
686 (defun package-process-define-package (exp origin)
687 (unless (eq (car-safe exp) 'define-package)
688 (error "Can't find define-package in %s" origin))
689 (let* ((new-pkg-desc (apply #'package-desc-from-define (cdr exp)))
690 (name (package-desc-name new-pkg-desc))
691 (version (package-desc-version new-pkg-desc))
692 (old-pkgs (assq name package-alist)))
693 (if (null old-pkgs)
694 ;; If there's no old package, just add this to `package-alist'.
695 (push (list name new-pkg-desc) package-alist)
696 ;; If there is, insert the new package at the right place in the list.
697 (while
698 (if (and (cdr old-pkgs)
699 (version-list-< version
700 (package-desc-version (cadr old-pkgs))))
701 (setq old-pkgs (cdr old-pkgs))
702 (push new-pkg-desc (cdr old-pkgs))
703 nil)))
704 new-pkg-desc))
706 ;; From Emacs 22, but changed so it adds to load-path.
707 (defun package-autoload-ensure-default-file (file)
708 "Make sure that the autoload file FILE exists and if not create it."
709 (unless (file-exists-p file)
710 (write-region
711 (concat ";;; " (file-name-nondirectory file)
712 " --- automatically extracted autoloads\n"
713 ";;\n"
714 ";;; Code:\n"
715 "(add-to-list 'load-path (or (file-name-directory #$) (car load-path)))\n"
716 "\f\n;; Local Variables:\n"
717 ";; version-control: never\n"
718 ";; no-byte-compile: t\n"
719 ";; no-update-autoloads: t\n"
720 ";; End:\n"
721 ";;; " (file-name-nondirectory file)
722 " ends here\n")
723 nil file nil 'silent))
724 file)
726 (defvar generated-autoload-file)
727 (defvar version-control)
729 (defun package-generate-autoloads (name pkg-dir)
730 (let* ((auto-name (format "%s-autoloads.el" name))
731 ;;(ignore-name (concat name "-pkg.el"))
732 (generated-autoload-file (expand-file-name auto-name pkg-dir))
733 (backup-inhibited t)
734 (version-control 'never))
735 (package-autoload-ensure-default-file generated-autoload-file)
736 (update-directory-autoloads pkg-dir)
737 (let ((buf (find-buffer-visiting generated-autoload-file)))
738 (when buf (kill-buffer buf)))
739 auto-name))
741 (defvar tar-parse-info)
742 (declare-function tar-untar-buffer "tar-mode" ())
743 (declare-function tar-header-name "tar-mode" (tar-header) t)
744 (declare-function tar-header-link-type "tar-mode" (tar-header) t)
746 (defun package-untar-buffer (dir)
747 "Untar the current buffer.
748 This uses `tar-untar-buffer' from Tar mode. All files should
749 untar into a directory named DIR; otherwise, signal an error."
750 (require 'tar-mode)
751 (tar-mode)
752 ;; Make sure everything extracts into DIR.
753 (let ((regexp (concat "\\`" (regexp-quote (expand-file-name dir)) "/"))
754 (case-fold-search (memq system-type '(windows-nt ms-dos cygwin))))
755 (dolist (tar-data tar-parse-info)
756 (let ((name (expand-file-name (tar-header-name tar-data))))
757 (or (string-match regexp name)
758 ;; Tarballs created by some utilities don't list
759 ;; directories with a trailing slash (Bug#13136).
760 (and (string-equal dir name)
761 (eq (tar-header-link-type tar-data) 5))
762 (error "Package does not untar cleanly into directory %s/" dir)))))
763 (tar-untar-buffer))
765 (defun package-generate-description-file (pkg-desc pkg-file)
766 "Create the foo-pkg.el file for single-file packages."
767 (let* ((name (package-desc-name pkg-desc)))
768 (let ((print-level nil)
769 (print-quoted t)
770 (print-length nil))
771 (write-region
772 (concat
773 ";;; -*- no-byte-compile: t -*-\n"
774 (prin1-to-string
775 (nconc
776 (list 'define-package
777 (symbol-name name)
778 (package-version-join (package-desc-version pkg-desc))
779 (package-desc-summary pkg-desc)
780 (let ((requires (package-desc-reqs pkg-desc)))
781 (list 'quote
782 ;; Turn version lists into string form.
783 (mapcar
784 (lambda (elt)
785 (list (car elt)
786 (package-version-join (cadr elt))))
787 requires))))
788 (package--alist-to-plist-args
789 (package-desc-extras pkg-desc))))
790 "\n")
791 nil pkg-file nil 'silent))))
793 (defun package--alist-to-plist-args (alist)
794 (mapcar 'macroexp-quote
795 (apply #'nconc
796 (mapcar (lambda (pair) (list (car pair) (cdr pair))) alist))))
797 (defun package-unpack (pkg-desc)
798 "Install the contents of the current buffer as a package."
799 (let* ((name (package-desc-name pkg-desc))
800 (dirname (package-desc-full-name pkg-desc))
801 (pkg-dir (expand-file-name dirname package-user-dir)))
802 (pcase (package-desc-kind pkg-desc)
803 (`tar
804 (make-directory package-user-dir t)
805 ;; FIXME: should we delete PKG-DIR if it exists?
806 (let* ((default-directory (file-name-as-directory package-user-dir)))
807 (package-untar-buffer dirname)))
808 (`single
809 (let ((el-file (expand-file-name (format "%s.el" name) pkg-dir)))
810 (make-directory pkg-dir t)
811 (package--write-file-no-coding el-file)))
812 (kind (error "Unknown package kind: %S" kind)))
813 (package--make-autoloads-and-stuff pkg-desc pkg-dir)
814 ;; Update package-alist.
815 (let ((new-desc (package-load-descriptor pkg-dir)))
816 ;; FIXME: Check that `new-desc' matches `desc'!
817 ;; FIXME: Compilation should be done as a separate, optional, step.
818 ;; E.g. for multi-package installs, we should first install all packages
819 ;; and then compile them.
820 (package--compile new-desc))
821 ;; Try to activate it.
822 (package-activate name 'force)
823 pkg-dir))
825 (defun package--make-autoloads-and-stuff (pkg-desc pkg-dir)
826 "Generate autoloads, description file, etc.. for PKG-DESC installed at PKG-DIR."
827 (package-generate-autoloads (package-desc-name pkg-desc) pkg-dir)
828 (let ((desc-file (expand-file-name (package--description-file pkg-dir)
829 pkg-dir)))
830 (unless (file-exists-p desc-file)
831 (package-generate-description-file pkg-desc desc-file)))
832 ;; FIXME: Create foo.info and dir file from foo.texi?
835 (defun package--compile (pkg-desc)
836 "Byte-compile installed package PKG-DESC."
837 (package-activate-1 pkg-desc)
838 (byte-recompile-directory (package-desc-dir pkg-desc) 0 t))
840 (defun package--write-file-no-coding (file-name)
841 (let ((buffer-file-coding-system 'no-conversion))
842 (write-region (point-min) (point-max) file-name nil 'silent)))
844 (defmacro package--with-work-buffer (location file &rest body)
845 "Run BODY in a buffer containing the contents of FILE at LOCATION.
846 LOCATION is the base location of a package archive, and should be
847 one of the URLs (or file names) specified in `package-archives'.
848 FILE is the name of a file relative to that base location.
850 This macro retrieves FILE from LOCATION into a temporary buffer,
851 and evaluates BODY while that buffer is current. This work
852 buffer is killed afterwards. Return the last value in BODY."
853 (declare (indent 2) (debug t))
854 `(with-temp-buffer
855 (if (string-match-p "\\`https?:" ,location)
856 (url-insert-file-contents (concat ,location ,file))
857 (unless (file-name-absolute-p ,location)
858 (error "Archive location %s is not an absolute file name"
859 ,location))
860 (insert-file-contents (expand-file-name ,file ,location)))
861 ,@body))
863 (defun package--archive-file-exists-p (location file)
864 (let ((http (string-match "\\`https?:" location)))
865 (if http
866 (progn
867 (require 'url-http)
868 (url-http-file-exists-p (concat location file)))
869 (file-exists-p (expand-file-name file location)))))
871 (declare-function epg-make-context "epg"
872 (&optional protocol armor textmode include-certs
873 cipher-algorithm
874 digest-algorithm
875 compress-algorithm))
876 (declare-function epg-verify-string "epg" (context signature
877 &optional signed-text))
878 (declare-function epg-context-result-for "epg" (context name))
879 (declare-function epg-signature-status "epg" (signature))
880 (declare-function epg-signature-to-string "epg" (signature))
882 (defun package--display-verify-error (context sig-file)
883 (unless (equal (epg-context-error-output context) "")
884 (with-output-to-temp-buffer "*Error*"
885 (with-current-buffer standard-output
886 (if (epg-context-result-for context 'verify)
887 (insert (format "Failed to verify signature %s:\n" sig-file)
888 (mapconcat #'epg-signature-to-string
889 (epg-context-result-for context 'verify)
890 "\n"))
891 (insert (format "Error while verifying signature %s:\n" sig-file)))
892 (insert "\nCommand output:\n" (epg-context-error-output context))))))
894 (defun package--check-signature (location file)
895 "Check signature of the current buffer.
896 GnuPG keyring is located under \"gnupg\" in `package-user-dir'."
897 (let* ((context (epg-make-context 'OpenPGP))
898 (homedir (expand-file-name "gnupg" package-user-dir))
899 (sig-file (concat file ".sig"))
900 (sig-content (package--with-work-buffer location sig-file
901 (buffer-string))))
902 (setf (epg-context-home-directory context) homedir)
903 (condition-case error
904 (epg-verify-string context sig-content (buffer-string))
905 (error
906 (package--display-verify-error context sig-file)
907 (signal (car error) (cdr error))))
908 (let (good-signatures had-fatal-error)
909 ;; The .sig file may contain multiple signatures. Success if one
910 ;; of the signatures is good.
911 (dolist (sig (epg-context-result-for context 'verify))
912 (if (eq (epg-signature-status sig) 'good)
913 (push sig good-signatures)
914 ;; If package-check-signature is allow-unsigned, don't
915 ;; signal error when we can't verify signature because of
916 ;; missing public key. Other errors are still treated as
917 ;; fatal (bug#17625).
918 (unless (and (eq package-check-signature 'allow-unsigned)
919 (eq (epg-signature-status sig) 'no-pubkey))
920 (setq had-fatal-error t))))
921 (when (and (null good-signatures) had-fatal-error)
922 (package--display-verify-error context sig-file)
923 (error "Failed to verify signature %s" sig-file))
924 good-signatures)))
926 (defun package-install-from-archive (pkg-desc)
927 "Download and install a tar package."
928 (let* ((location (package-archive-base pkg-desc))
929 (file (concat (package-desc-full-name pkg-desc)
930 (package-desc-suffix pkg-desc)))
931 (sig-file (concat file ".sig"))
932 good-signatures pkg-descs)
933 (package--with-work-buffer location file
934 (if (and package-check-signature
935 (not (member (package-desc-archive pkg-desc)
936 package-unsigned-archives)))
937 (if (package--archive-file-exists-p location sig-file)
938 (setq good-signatures (package--check-signature location file))
939 (unless (eq package-check-signature 'allow-unsigned)
940 (error "Unsigned package: `%s'"
941 (package-desc-name pkg-desc)))))
942 (package-unpack pkg-desc))
943 ;; Here the package has been installed successfully, mark it as
944 ;; signed if appropriate.
945 (when good-signatures
946 ;; Write out good signatures into NAME-VERSION.signed file.
947 (write-region (mapconcat #'epg-signature-to-string good-signatures "\n")
949 (expand-file-name
950 (concat (package-desc-full-name pkg-desc)
951 ".signed")
952 package-user-dir)
953 nil 'silent)
954 ;; Update the old pkg-desc which will be shown on the description buffer.
955 (setf (package-desc-signed pkg-desc) t)
956 ;; Update the new (activated) pkg-desc as well.
957 (setq pkg-descs (cdr (assq (package-desc-name pkg-desc) package-alist)))
958 (if pkg-descs
959 (setf (package-desc-signed (car pkg-descs)) t)))))
961 (defvar package--initialized nil)
963 (defun package-installed-p (package &optional min-version)
964 "Return true if PACKAGE, of MIN-VERSION or newer, is installed.
965 MIN-VERSION should be a version list."
966 (unless package--initialized (error "package.el is not yet initialized!"))
968 (let ((pkg-descs (cdr (assq package package-alist))))
969 (and pkg-descs
970 (version-list-<= min-version
971 (package-desc-version (car pkg-descs)))))
972 ;; Also check built-in packages.
973 (package-built-in-p package min-version)))
975 (defun package-compute-transaction (packages requirements &optional seen)
976 "Return a list of packages to be installed, including PACKAGES.
977 PACKAGES should be a list of `package-desc'.
979 REQUIREMENTS should be a list of additional requirements; each
980 element in this list should have the form (PACKAGE VERSION-LIST),
981 where PACKAGE is a package name and VERSION-LIST is the required
982 version of that package.
984 This function recursively computes the requirements of the
985 packages in REQUIREMENTS, and returns a list of all the packages
986 that must be installed. Packages that are already installed are
987 not included in this list.
989 SEEN is used internally to detect infinite recursion."
990 ;; FIXME: We really should use backtracking to explore the whole
991 ;; search space (e.g. if foo require bar-1.3, and bar-1.4 requires toto-1.1
992 ;; whereas bar-1.3 requires toto-1.0 and the user has put a hold on toto-1.0:
993 ;; the current code might fail to see that it could install foo by using the
994 ;; older bar-1.3).
995 (dolist (elt requirements)
996 (let* ((next-pkg (car elt))
997 (next-version (cadr elt))
998 (already ()))
999 (dolist (pkg packages)
1000 (if (eq next-pkg (package-desc-name pkg))
1001 (setq already pkg)))
1002 (when already
1003 (if (version-list-<= next-version (package-desc-version already))
1004 ;; `next-pkg' is already in `packages', but its position there
1005 ;; means it might be installed too late: remove it from there, so
1006 ;; we re-add it (along with its dependencies) at an earlier place
1007 ;; below (bug#16994).
1008 (if (memq already seen) ;Avoid inf-loop on dependency cycles.
1009 (message "Dependency cycle going through %S"
1010 (package-desc-full-name already))
1011 (setq packages (delq already packages))
1012 (setq already nil))
1013 (error "Need package `%s-%s', but only %s is being installed"
1014 next-pkg (package-version-join next-version)
1015 (package-version-join (package-desc-version already)))))
1016 (cond
1017 (already nil)
1018 ((package-installed-p next-pkg next-version) nil)
1021 ;; A package is required, but not installed. It might also be
1022 ;; blocked via `package-load-list'.
1023 (let ((pkg-descs (cdr (assq next-pkg package-archive-contents)))
1024 (found nil)
1025 (problem nil))
1026 (while (and pkg-descs (not found))
1027 (let* ((pkg-desc (pop pkg-descs))
1028 (version (package-desc-version pkg-desc))
1029 (disabled (package-disabled-p next-pkg version)))
1030 (cond
1031 ((version-list-< version next-version)
1032 (error
1033 "Need package `%s-%s', but only %s is available"
1034 next-pkg (package-version-join next-version)
1035 (package-version-join version)))
1036 (disabled
1037 (unless problem
1038 (setq problem
1039 (if (stringp disabled)
1040 (format "Package `%s' held at version %s, \
1041 but version %s required"
1042 next-pkg disabled
1043 (package-version-join next-version))
1044 (format "Required package '%s' is disabled"
1045 next-pkg)))))
1046 (t (setq found pkg-desc)))))
1047 (unless found
1048 (if problem
1049 (error "%s" problem)
1050 (error "Package `%s-%s' is unavailable"
1051 next-pkg (package-version-join next-version))))
1052 (setq packages
1053 (package-compute-transaction (cons found packages)
1054 (package-desc-reqs found)
1055 (cons found seen))))))))
1056 packages)
1058 (defun package-read-from-string (str)
1059 "Read a Lisp expression from STR.
1060 Signal an error if the entire string was not used."
1061 (let* ((read-data (read-from-string str))
1062 (more-left
1063 (condition-case nil
1064 ;; The call to `ignore' suppresses a compiler warning.
1065 (progn (ignore (read-from-string
1066 (substring str (cdr read-data))))
1068 (end-of-file nil))))
1069 (if more-left
1070 (error "Can't read whole string")
1071 (car read-data))))
1073 (defun package--read-archive-file (file)
1074 "Re-read archive file FILE, if it exists.
1075 Will return the data from the file, or nil if the file does not exist.
1076 Will throw an error if the archive version is too new."
1077 (let ((filename (expand-file-name file package-user-dir)))
1078 (when (file-exists-p filename)
1079 (with-temp-buffer
1080 (insert-file-contents-literally filename)
1081 (let ((contents (read (current-buffer))))
1082 (if (> (car contents) package-archive-version)
1083 (error "Package archive version %d is higher than %d"
1084 (car contents) package-archive-version))
1085 (cdr contents))))))
1087 (defun package-read-all-archive-contents ()
1088 "Re-read `archive-contents', if it exists.
1089 If successful, set `package-archive-contents'."
1090 (setq package-archive-contents nil)
1091 (dolist (archive package-archives)
1092 (package-read-archive-contents (car archive))))
1094 (defun package-read-archive-contents (archive)
1095 "Re-read archive contents for ARCHIVE.
1096 If successful, set the variable `package-archive-contents'.
1097 If the archive version is too new, signal an error."
1098 ;; Version 1 of 'archive-contents' is identical to our internal
1099 ;; representation.
1100 (let* ((contents-file (format "archives/%s/archive-contents" archive))
1101 (contents (package--read-archive-file contents-file)))
1102 (when contents
1103 (dolist (package contents)
1104 (package--add-to-archive-contents package archive)))))
1106 ;; Package descriptor objects used inside the "archive-contents" file.
1107 ;; Changing this defstruct implies changing the format of the
1108 ;; "archive-contents" files.
1109 (cl-defstruct (package--ac-desc
1110 (:constructor package-make-ac-desc (version reqs summary kind extras))
1111 (:copier nil)
1112 (:type vector))
1113 version reqs summary kind extras)
1115 (defun package--add-to-archive-contents (package archive)
1116 "Add the PACKAGE from the given ARCHIVE if necessary.
1117 PACKAGE should have the form (NAME . PACKAGE--AC-DESC).
1118 Also, add the originating archive to the `package-desc' structure."
1119 (let* ((name (car package))
1120 (version (package--ac-desc-version (cdr package)))
1121 (pkg-desc
1122 (package-desc-create
1123 :name name
1124 :version version
1125 :reqs (package--ac-desc-reqs (cdr package))
1126 :summary (package--ac-desc-summary (cdr package))
1127 :kind (package--ac-desc-kind (cdr package))
1128 :archive archive
1129 :extras (and (> (length (cdr package)) 4)
1130 ;; Older archive-contents files have only 4
1131 ;; elements here.
1132 (package--ac-desc-extras (cdr package)))))
1133 (pinned-to-archive (assoc name package-pinned-packages)))
1134 ;; Skip entirely if pinned to another archive.
1135 (when (not (and pinned-to-archive
1136 (not (equal (cdr pinned-to-archive) archive))))
1137 (setq package-archive-contents
1138 (package--add-to-alist pkg-desc package-archive-contents)))))
1140 (defun package--add-to-alist (pkg-desc alist)
1141 "Add PKG-DESC to ALIST.
1143 Packages are grouped by name. The package descriptions are sorted
1144 by version number."
1145 (let* ((name (package-desc-name pkg-desc))
1146 (priority-version (package-desc-priority-version pkg-desc))
1147 (existing-packages (assq name alist)))
1148 (if (not existing-packages)
1149 (cons (list name pkg-desc)
1150 alist)
1151 (while (if (and (cdr existing-packages)
1152 (version-list-< priority-version
1153 (package-desc-priority-version
1154 (cadr existing-packages))))
1155 (setq existing-packages (cdr existing-packages))
1156 (push pkg-desc (cdr existing-packages))
1157 nil))
1158 alist)))
1160 (defun package-download-transaction (packages)
1161 "Download and install all the packages in PACKAGES.
1162 PACKAGES should be a list of package-desc.
1163 This function assumes that all package requirements in
1164 PACKAGES are satisfied, i.e. that PACKAGES is computed
1165 using `package-compute-transaction'."
1166 (mapc #'package-install-from-archive packages))
1168 ;;;###autoload
1169 (defun package-install (pkg)
1170 "Install the package PKG.
1171 PKG can be a package-desc or the package name of one the available packages
1172 in an archive in `package-archives'. Interactively, prompt for its name."
1173 (interactive
1174 (progn
1175 ;; Initialize the package system to get the list of package
1176 ;; symbols for completion.
1177 (unless package--initialized
1178 (package-initialize t))
1179 (unless package-archive-contents
1180 (package-refresh-contents))
1181 (list (intern (completing-read
1182 "Install package: "
1183 (delq nil
1184 (mapcar (lambda (elt)
1185 (unless (package-installed-p (car elt))
1186 (symbol-name (car elt))))
1187 package-archive-contents))
1188 nil t)))))
1189 (package-download-transaction
1190 (if (package-desc-p pkg)
1191 (package-compute-transaction (list pkg)
1192 (package-desc-reqs pkg))
1193 (package-compute-transaction ()
1194 (list (list pkg))))))
1196 (defun package-strip-rcs-id (str)
1197 "Strip RCS version ID from the version string STR.
1198 If the result looks like a dotted numeric version, return it.
1199 Otherwise return nil."
1200 (when str
1201 (when (string-match "\\`[ \t]*[$]Revision:[ \t]+" str)
1202 (setq str (substring str (match-end 0))))
1203 (condition-case nil
1204 (if (version-to-list str)
1205 str)
1206 (error nil))))
1208 (declare-function lm-homepage "lisp-mnt" (&optional file))
1210 (defun package--prepare-dependencies (deps)
1211 "Turn DEPS into an acceptable list of dependencies.
1213 Any parts missing a version string get a default version string
1214 of \"0\" (meaning any version) and an appropriate level of lists
1215 is wrapped around any parts requiring it."
1216 (cond
1217 ((not (listp deps))
1218 (error "Invalid requirement specifier: %S" deps))
1219 (t (mapcar (lambda (dep)
1220 (cond
1221 ((symbolp dep) `(,dep "0"))
1222 ((stringp dep)
1223 (error "Invalid requirement specifier: %S" dep))
1224 ((and (listp dep) (null (cdr dep)))
1225 (list (car dep) "0"))
1226 (t dep)))
1227 deps))))
1229 (defun package-buffer-info ()
1230 "Return a `package-desc' describing the package in the current buffer.
1232 If the buffer does not contain a conforming package, signal an
1233 error. If there is a package, narrow the buffer to the file's
1234 boundaries."
1235 (goto-char (point-min))
1236 (unless (re-search-forward "^;;; \\([^ ]*\\)\\.el ---[ \t]*\\(.*?\\)[ \t]*\\(-\\*-.*-\\*-[ \t]*\\)?$" nil t)
1237 (error "Package lacks a file header"))
1238 (let ((file-name (match-string-no-properties 1))
1239 (desc (match-string-no-properties 2))
1240 (start (line-beginning-position)))
1241 (unless (search-forward (concat ";;; " file-name ".el ends here"))
1242 (error "Package lacks a terminating comment"))
1243 ;; Try to include a trailing newline.
1244 (forward-line)
1245 (narrow-to-region start (point))
1246 (require 'lisp-mnt)
1247 ;; Use some headers we've invented to drive the process.
1248 (let* ((requires-str (lm-header "package-requires"))
1249 ;; Prefer Package-Version; if defined, the package author
1250 ;; probably wants us to use it. Otherwise try Version.
1251 (pkg-version
1252 (or (package-strip-rcs-id (lm-header "package-version"))
1253 (package-strip-rcs-id (lm-header "version"))))
1254 (homepage (lm-homepage)))
1255 (unless pkg-version
1256 (error
1257 "Package lacks a \"Version\" or \"Package-Version\" header"))
1258 (package-desc-from-define
1259 file-name pkg-version desc
1260 (if requires-str
1261 (package--prepare-dependencies
1262 (package-read-from-string requires-str)))
1263 :kind 'single
1264 :url homepage))))
1266 (declare-function tar-get-file-descriptor "tar-mode" (file))
1267 (declare-function tar--extract "tar-mode" (descriptor))
1269 (defun package-tar-file-info ()
1270 "Find package information for a tar file.
1271 The return result is a `package-desc'."
1272 (cl-assert (derived-mode-p 'tar-mode))
1273 (let* ((dir-name (file-name-directory
1274 (tar-header-name (car tar-parse-info))))
1275 (desc-file (package--description-file dir-name))
1276 (tar-desc (tar-get-file-descriptor (concat dir-name desc-file))))
1277 (unless tar-desc
1278 (error "No package descriptor file found"))
1279 (with-current-buffer (tar--extract tar-desc)
1280 (unwind-protect
1281 (package--read-pkg-desc 'tar)
1282 (kill-buffer (current-buffer))))))
1284 (defun package--read-pkg-desc (kind)
1285 "Read a `define-package' form in current buffer.
1286 Return the pkg-desc, with desc-kind set to KIND."
1287 (goto-char (point-min))
1288 (unwind-protect
1289 (let* ((pkg-def-parsed (read (current-buffer)))
1290 (pkg-desc
1291 (if (not (eq (car pkg-def-parsed) 'define-package))
1292 (error "Can't find define-package in %s"
1293 (tar-header-name tar-desc))
1294 (apply #'package-desc-from-define
1295 (append (cdr pkg-def-parsed))))))
1296 (setf (package-desc-kind pkg-desc) kind)
1297 pkg-desc)))
1300 ;;;###autoload
1301 (defun package-install-from-buffer ()
1302 "Install a package from the current buffer.
1303 The current buffer is assumed to be a single .el or .tar file that follows the
1304 packaging guidelines; see info node `(elisp)Packaging'.
1305 Downloads and installs required packages as needed."
1306 (interactive)
1307 (let ((pkg-desc (if (derived-mode-p 'tar-mode)
1308 (package-tar-file-info)
1309 (package-buffer-info))))
1310 ;; Download and install the dependencies.
1311 (let* ((requires (package-desc-reqs pkg-desc))
1312 (transaction (package-compute-transaction nil requires)))
1313 (package-download-transaction transaction))
1314 ;; Install the package itself.
1315 (package-unpack pkg-desc)
1316 pkg-desc))
1318 ;;;###autoload
1319 (defun package-install-file (file)
1320 "Install a package from a file.
1321 The file can either be a tar file or an Emacs Lisp file."
1322 (interactive "fPackage file name: ")
1323 (with-temp-buffer
1324 (insert-file-contents-literally file)
1325 (when (string-match "\\.tar\\'" file) (tar-mode))
1326 (package-install-from-buffer)))
1328 (defun package-delete (pkg-desc)
1329 (let ((dir (package-desc-dir pkg-desc)))
1330 (if (not (string-prefix-p (file-name-as-directory
1331 (expand-file-name package-user-dir))
1332 (expand-file-name dir)))
1333 ;; Don't delete "system" packages.
1334 (error "Package `%s' is a system package, not deleting"
1335 (package-desc-full-name pkg-desc))
1336 (delete-directory dir t t)
1337 ;; Remove NAME-VERSION.signed file.
1338 (let ((signed-file (concat dir ".signed")))
1339 (if (file-exists-p signed-file)
1340 (delete-file signed-file)))
1341 ;; Update package-alist.
1342 (let* ((name (package-desc-name pkg-desc))
1343 (pkgs (assq name package-alist)))
1344 (delete pkg-desc pkgs)
1345 (unless (cdr pkgs)
1346 (setq package-alist (delq pkgs package-alist))))
1347 (message "Package `%s' deleted." (package-desc-full-name pkg-desc)))))
1349 (defun package-archive-base (desc)
1350 "Return the archive containing the package NAME."
1351 (cdr (assoc (package-desc-archive desc) package-archives)))
1353 (defun package-archive-priority (archive)
1354 "Return the priority of ARCHIVE.
1356 The archive priorities are specified in
1357 `package-archive-priorities'. If not given there, the priority
1358 defaults to 0."
1359 (or (cdr (assoc archive package-archive-priorities))
1362 (defun package-desc-priority-version (pkg-desc)
1363 "Return the version PKG-DESC with the archive priority prepended.
1365 This allows for easy comparison of package versions from
1366 different archives if archive priorities are meant to be taken in
1367 consideration."
1368 (cons (package-archive-priority
1369 (package-desc-archive pkg-desc))
1370 (package-desc-version pkg-desc)))
1372 (defun package--download-one-archive (archive file)
1373 "Retrieve an archive file FILE from ARCHIVE, and cache it.
1374 ARCHIVE should be a cons cell of the form (NAME . LOCATION),
1375 similar to an entry in `package-alist'. Save the cached copy to
1376 \"archives/NAME/archive-contents\" in `package-user-dir'."
1377 (let ((dir (expand-file-name (format "archives/%s" (car archive))
1378 package-user-dir))
1379 (sig-file (concat file ".sig"))
1380 good-signatures)
1381 (package--with-work-buffer (cdr archive) file
1382 ;; Check signature of archive-contents, if desired.
1383 (if (and package-check-signature
1384 (not (member archive package-unsigned-archives)))
1385 (if (package--archive-file-exists-p (cdr archive) sig-file)
1386 (setq good-signatures (package--check-signature (cdr archive)
1387 file))
1388 (unless (eq package-check-signature 'allow-unsigned)
1389 (error "Unsigned archive `%s'"
1390 (car archive)))))
1391 ;; Read the retrieved buffer to make sure it is valid (e.g. it
1392 ;; may fetch a URL redirect page).
1393 (when (listp (read (current-buffer)))
1394 (make-directory dir t)
1395 (write-region nil nil (expand-file-name file dir) nil 'silent)))
1396 (when good-signatures
1397 ;; Write out good signatures into archive-contents.signed file.
1398 (write-region (mapconcat #'epg-signature-to-string good-signatures "\n")
1400 (expand-file-name (concat file ".signed") dir)
1401 nil 'silent))))
1403 (declare-function epg-check-configuration "epg-config"
1404 (config &optional minimum-version))
1405 (declare-function epg-configuration "epg-config" ())
1406 (declare-function epg-import-keys-from-file "epg" (context keys))
1408 ;;;###autoload
1409 (defun package-import-keyring (&optional file)
1410 "Import keys from FILE."
1411 (interactive "fFile: ")
1412 (setq file (expand-file-name file))
1413 (let ((context (epg-make-context 'OpenPGP))
1414 (homedir (expand-file-name "gnupg" package-user-dir)))
1415 (with-file-modes 448
1416 (make-directory homedir t))
1417 (setf (epg-context-home-directory context) homedir)
1418 (message "Importing %s..." (file-name-nondirectory file))
1419 (epg-import-keys-from-file context file)
1420 (message "Importing %s...done" (file-name-nondirectory file))))
1422 ;;;###autoload
1423 (defun package-refresh-contents ()
1424 "Download the ELPA archive description if needed.
1425 This informs Emacs about the latest versions of all packages, and
1426 makes them available for download."
1427 (interactive)
1428 ;; FIXME: Do it asynchronously.
1429 (unless (file-exists-p package-user-dir)
1430 (make-directory package-user-dir t))
1431 (let ((default-keyring (expand-file-name "package-keyring.gpg"
1432 data-directory)))
1433 (when (and package-check-signature (file-exists-p default-keyring))
1434 (condition-case-unless-debug error
1435 (progn
1436 (epg-check-configuration (epg-configuration))
1437 (package-import-keyring default-keyring))
1438 (error (message "Cannot import default keyring: %S" (cdr error))))))
1439 (dolist (archive package-archives)
1440 (condition-case-unless-debug nil
1441 (package--download-one-archive archive "archive-contents")
1442 (error (message "Failed to download `%s' archive."
1443 (car archive)))))
1444 (package-read-all-archive-contents))
1446 ;;;###autoload
1447 (defun package-initialize (&optional no-activate)
1448 "Load Emacs Lisp packages, and activate them.
1449 The variable `package-load-list' controls which packages to load.
1450 If optional arg NO-ACTIVATE is non-nil, don't activate packages."
1451 (interactive)
1452 (setq package-alist nil)
1453 (package-load-all-descriptors)
1454 (package-read-all-archive-contents)
1455 (unless no-activate
1456 (dolist (elt package-alist)
1457 (package-activate (car elt))))
1458 (setq package--initialized t))
1461 ;;;; Package description buffer.
1463 ;;;###autoload
1464 (defun describe-package (package)
1465 "Display the full documentation of PACKAGE (a symbol)."
1466 (interactive
1467 (let* ((guess (function-called-at-point)))
1468 (require 'finder-inf nil t)
1469 ;; Load the package list if necessary (but don't activate them).
1470 (unless package--initialized
1471 (package-initialize t))
1472 (let ((packages (append (mapcar 'car package-alist)
1473 (mapcar 'car package-archive-contents)
1474 (mapcar 'car package--builtins))))
1475 (unless (memq guess packages)
1476 (setq guess nil))
1477 (setq packages (mapcar 'symbol-name packages))
1478 (let ((val
1479 (completing-read (if guess
1480 (format "Describe package (default %s): "
1481 guess)
1482 "Describe package: ")
1483 packages nil t nil nil guess)))
1484 (list (intern val))))))
1485 (if (not (or (package-desc-p package) (and package (symbolp package))))
1486 (message "No package specified")
1487 (help-setup-xref (list #'describe-package package)
1488 (called-interactively-p 'interactive))
1489 (with-help-window (help-buffer)
1490 (with-current-buffer standard-output
1491 (describe-package-1 package)))))
1493 (defun describe-package-1 (pkg)
1494 (require 'lisp-mnt)
1495 (let* ((desc (or
1496 (if (package-desc-p pkg) pkg)
1497 (cadr (assq pkg package-alist))
1498 (let ((built-in (assq pkg package--builtins)))
1499 (if built-in
1500 (package--from-builtin built-in)
1501 (cadr (assq pkg package-archive-contents))))))
1502 (name (if desc (package-desc-name desc) pkg))
1503 (pkg-dir (if desc (package-desc-dir desc)))
1504 (reqs (if desc (package-desc-reqs desc)))
1505 (version (if desc (package-desc-version desc)))
1506 (archive (if desc (package-desc-archive desc)))
1507 (extras (and desc (package-desc-extras desc)))
1508 (homepage (cdr (assoc :url extras)))
1509 (keywords (if desc (package-desc--keywords desc)))
1510 (built-in (eq pkg-dir 'builtin))
1511 (installable (and archive (not built-in)))
1512 (status (if desc (package-desc-status desc) "orphan"))
1513 (signed (if desc (package-desc-signed desc))))
1514 (prin1 name)
1515 (princ " is ")
1516 (princ (if (memq (aref status 0) '(?a ?e ?i ?o ?u)) "an " "a "))
1517 (princ status)
1518 (princ " package.\n\n")
1520 (insert " " (propertize "Status" 'font-lock-face 'bold) ": ")
1521 (cond (built-in
1522 (insert (propertize (capitalize status)
1523 'font-lock-face 'font-lock-builtin-face)
1524 "."))
1525 (pkg-dir
1526 (insert (propertize (if (equal status "unsigned")
1527 "Installed"
1528 (capitalize status)) ;FIXME: Why comment-face?
1529 'font-lock-face 'font-lock-comment-face))
1530 (insert " in `")
1531 ;; Todo: Add button for uninstalling.
1532 (help-insert-xref-button (abbreviate-file-name
1533 (file-name-as-directory pkg-dir))
1534 'help-package-def pkg-dir)
1535 (if (and (package-built-in-p name)
1536 (not (package-built-in-p name version)))
1537 (insert "',\n shadowing a "
1538 (propertize "built-in package"
1539 'font-lock-face 'font-lock-builtin-face))
1540 (insert "'"))
1541 (if signed
1542 (insert ".")
1543 (insert " (unsigned).")))
1544 (installable
1545 (insert (capitalize status))
1546 (insert " from " (format "%s" archive))
1547 (insert " -- ")
1548 (package-make-button
1549 "Install"
1550 'action 'package-install-button-action
1551 'package-desc desc))
1552 (t (insert (capitalize status) ".")))
1553 (insert "\n")
1554 (insert " " (propertize "Archive" 'font-lock-face 'bold)
1555 ": " (or archive "n/a") "\n")
1556 (and version
1557 (insert " "
1558 (propertize "Version" 'font-lock-face 'bold) ": "
1559 (package-version-join version) "\n"))
1561 (setq reqs (if desc (package-desc-reqs desc)))
1562 (when reqs
1563 (insert " " (propertize "Requires" 'font-lock-face 'bold) ": ")
1564 (let ((first t)
1565 name vers text)
1566 (dolist (req reqs)
1567 (setq name (car req)
1568 vers (cadr req)
1569 text (format "%s-%s" (symbol-name name)
1570 (package-version-join vers)))
1571 (cond (first (setq first nil))
1572 ((>= (+ 2 (current-column) (length text))
1573 (window-width))
1574 (insert ",\n "))
1575 (t (insert ", ")))
1576 (help-insert-xref-button text 'help-package name))
1577 (insert "\n")))
1578 (insert " " (propertize "Summary" 'font-lock-face 'bold)
1579 ": " (if desc (package-desc-summary desc)) "\n")
1580 (when homepage
1581 (insert " " (propertize "Homepage" 'font-lock-face 'bold) ": ")
1582 (help-insert-xref-button homepage 'help-url homepage)
1583 (insert "\n"))
1584 (when keywords
1585 (insert " " (propertize "Keywords" 'font-lock-face 'bold) ": ")
1586 (dolist (k keywords)
1587 (package-make-button
1589 'package-keyword k
1590 'action 'package-keyword-button-action)
1591 (insert " "))
1592 (insert "\n"))
1593 (let* ((all-pkgs (append (cdr (assq name package-alist))
1594 (cdr (assq name package-archive-contents))
1595 (let ((bi (assq name package--builtins)))
1596 (if bi (list (package--from-builtin bi))))))
1597 (other-pkgs (delete desc all-pkgs)))
1598 (when other-pkgs
1599 (insert " " (propertize "Other versions" 'font-lock-face 'bold) ": "
1600 (mapconcat
1601 (lambda (opkg)
1602 (let* ((ov (package-desc-version opkg))
1603 (dir (package-desc-dir opkg))
1604 (from (or (package-desc-archive opkg)
1605 (if (stringp dir) "installed" dir))))
1606 (if (not ov) (format "%s" from)
1607 (format "%s (%s)"
1608 (make-text-button (package-version-join ov) nil
1609 'face 'link
1610 'follow-link t
1611 'action
1612 (lambda (_button)
1613 (describe-package opkg)))
1614 from))))
1615 other-pkgs ", ")
1616 ".\n")))
1618 (insert "\n")
1620 (if built-in
1621 ;; For built-in packages, insert the commentary.
1622 (let ((fn (locate-file (format "%s.el" name) load-path
1623 load-file-rep-suffixes))
1624 (opoint (point)))
1625 (insert (or (lm-commentary fn) ""))
1626 (save-excursion
1627 (goto-char opoint)
1628 (when (re-search-forward "^;;; Commentary:\n" nil t)
1629 (replace-match ""))
1630 (while (re-search-forward "^\\(;+ ?\\)" nil t)
1631 (replace-match ""))))
1632 (let ((readme (expand-file-name (format "%s-readme.txt" name)
1633 package-user-dir))
1634 readme-string)
1635 ;; For elpa packages, try downloading the commentary. If that
1636 ;; fails, try an existing readme file in `package-user-dir'.
1637 (cond ((condition-case nil
1638 (save-excursion
1639 (package--with-work-buffer
1640 (package-archive-base desc)
1641 (format "%s-readme.txt" name)
1642 (save-excursion
1643 (goto-char (point-max))
1644 (unless (bolp)
1645 (insert ?\n)))
1646 (write-region nil nil
1647 (expand-file-name readme package-user-dir)
1648 nil 'silent)
1649 (setq readme-string (buffer-string))
1651 (error nil))
1652 (insert readme-string))
1653 ((file-readable-p readme)
1654 (insert-file-contents readme)
1655 (goto-char (point-max))))))))
1657 (defun package-install-button-action (button)
1658 (let ((pkg-desc (button-get button 'package-desc)))
1659 (when (y-or-n-p (format "Install package `%s'? "
1660 (package-desc-full-name pkg-desc)))
1661 (package-install pkg-desc)
1662 (revert-buffer nil t)
1663 (goto-char (point-min)))))
1665 (defun package-keyword-button-action (button)
1666 (let ((pkg-keyword (button-get button 'package-keyword)))
1667 (package-show-package-list t (list pkg-keyword))))
1669 (defun package-make-button (text &rest props)
1670 (let ((button-text (if (display-graphic-p) text (concat "[" text "]")))
1671 (button-face (if (display-graphic-p)
1672 '(:box (:line-width 2 :color "dark grey")
1673 :background "light grey"
1674 :foreground "black")
1675 'link)))
1676 (apply 'insert-text-button button-text 'face button-face 'follow-link t
1677 props)))
1680 ;;;; Package menu mode.
1682 (defvar package-menu-mode-map
1683 (let ((map (make-sparse-keymap))
1684 (menu-map (make-sparse-keymap "Package")))
1685 (set-keymap-parent map tabulated-list-mode-map)
1686 (define-key map "\C-m" 'package-menu-describe-package)
1687 (define-key map "u" 'package-menu-mark-unmark)
1688 (define-key map "\177" 'package-menu-backup-unmark)
1689 (define-key map "d" 'package-menu-mark-delete)
1690 (define-key map "i" 'package-menu-mark-install)
1691 (define-key map "U" 'package-menu-mark-upgrades)
1692 (define-key map "r" 'package-menu-refresh)
1693 (define-key map "f" 'package-menu-filter)
1694 (define-key map "~" 'package-menu-mark-obsolete-for-deletion)
1695 (define-key map "x" 'package-menu-execute)
1696 (define-key map "h" 'package-menu-quick-help)
1697 (define-key map "?" 'package-menu-describe-package)
1698 (define-key map [menu-bar package-menu] (cons "Package" menu-map))
1699 (define-key menu-map [mq]
1700 '(menu-item "Quit" quit-window
1701 :help "Quit package selection"))
1702 (define-key menu-map [s1] '("--"))
1703 (define-key menu-map [mn]
1704 '(menu-item "Next" next-line
1705 :help "Next Line"))
1706 (define-key menu-map [mp]
1707 '(menu-item "Previous" previous-line
1708 :help "Previous Line"))
1709 (define-key menu-map [s2] '("--"))
1710 (define-key menu-map [mu]
1711 '(menu-item "Unmark" package-menu-mark-unmark
1712 :help "Clear any marks on a package and move to the next line"))
1713 (define-key menu-map [munm]
1714 '(menu-item "Unmark Backwards" package-menu-backup-unmark
1715 :help "Back up one line and clear any marks on that package"))
1716 (define-key menu-map [md]
1717 '(menu-item "Mark for Deletion" package-menu-mark-delete
1718 :help "Mark a package for deletion and move to the next line"))
1719 (define-key menu-map [mi]
1720 '(menu-item "Mark for Install" package-menu-mark-install
1721 :help "Mark a package for installation and move to the next line"))
1722 (define-key menu-map [mupgrades]
1723 '(menu-item "Mark Upgradable Packages" package-menu-mark-upgrades
1724 :help "Mark packages that have a newer version for upgrading"))
1725 (define-key menu-map [s3] '("--"))
1726 (define-key menu-map [mf]
1727 '(menu-item "Filter Package List..." package-menu-filter
1728 :help "Filter package selection (q to go back)"))
1729 (define-key menu-map [mg]
1730 '(menu-item "Update Package List" revert-buffer
1731 :help "Update the list of packages"))
1732 (define-key menu-map [mr]
1733 '(menu-item "Refresh Package List" package-menu-refresh
1734 :help "Download the ELPA archive"))
1735 (define-key menu-map [s4] '("--"))
1736 (define-key menu-map [mt]
1737 '(menu-item "Mark Obsolete Packages" package-menu-mark-obsolete-for-deletion
1738 :help "Mark all obsolete packages for deletion"))
1739 (define-key menu-map [mx]
1740 '(menu-item "Execute Actions" package-menu-execute
1741 :help "Perform all the marked actions"))
1742 (define-key menu-map [s5] '("--"))
1743 (define-key menu-map [mh]
1744 '(menu-item "Help" package-menu-quick-help
1745 :help "Show short key binding help for package-menu-mode"))
1746 (define-key menu-map [mc]
1747 '(menu-item "Describe Package" package-menu-describe-package
1748 :help "Display information about this package"))
1749 map)
1750 "Local keymap for `package-menu-mode' buffers.")
1752 (defvar package-menu--new-package-list nil
1753 "List of newly-available packages since `list-packages' was last called.")
1755 (define-derived-mode package-menu-mode tabulated-list-mode "Package Menu"
1756 "Major mode for browsing a list of packages.
1757 Letters do not insert themselves; instead, they are commands.
1758 \\<package-menu-mode-map>
1759 \\{package-menu-mode-map}"
1760 (setq tabulated-list-format
1761 `[("Package" 18 package-menu--name-predicate)
1762 ("Version" 13 nil)
1763 ("Status" 10 package-menu--status-predicate)
1764 ,@(if (cdr package-archives)
1765 '(("Archive" 10 package-menu--archive-predicate)))
1766 ("Description" 0 nil)])
1767 (setq tabulated-list-padding 2)
1768 (setq tabulated-list-sort-key (cons "Status" nil))
1769 (add-hook 'tabulated-list-revert-hook 'package-menu--refresh nil t)
1770 (tabulated-list-init-header))
1772 (defmacro package--push (pkg-desc status listname)
1773 "Convenience macro for `package-menu--generate'.
1774 If the alist stored in the symbol LISTNAME lacks an entry for a
1775 package PKG-DESC, add one. The alist is keyed with PKG-DESC."
1776 `(unless (assoc ,pkg-desc ,listname)
1777 ;; FIXME: Should we move status into pkg-desc?
1778 (push (cons ,pkg-desc ,status) ,listname)))
1780 (defvar package-list-unversioned nil
1781 "If non-nil include packages that don't have a version in `list-package'.")
1783 (defvar package-list-unsigned nil
1784 "If non-nil, mention in the list which packages were installed w/o signature.")
1786 (defun package-desc-status (pkg-desc)
1787 (let* ((name (package-desc-name pkg-desc))
1788 (dir (package-desc-dir pkg-desc))
1789 (lle (assq name package-load-list))
1790 (held (cadr lle))
1791 (version (package-desc-version pkg-desc))
1792 (signed (package-desc-signed pkg-desc)))
1793 (cond
1794 ((eq dir 'builtin) "built-in")
1795 ((and lle (null held)) "disabled")
1796 ((stringp held)
1797 (let ((hv (if (stringp held) (version-to-list held))))
1798 (cond
1799 ((version-list-= version hv) "held")
1800 ((version-list-< version hv) "obsolete")
1801 (t "disabled"))))
1802 ((package-built-in-p name version) "obsolete")
1803 (dir ;One of the installed packages.
1804 (cond
1805 ((not (file-exists-p (package-desc-dir pkg-desc))) "deleted")
1806 ((eq pkg-desc (cadr (assq name package-alist)))
1807 (if (or (not package-list-unsigned) signed) "installed" "unsigned"))
1808 (t "obsolete")))
1810 (let* ((ins (cadr (assq name package-alist)))
1811 (ins-v (if ins (package-desc-version ins))))
1812 (cond
1813 ((or (null ins) (version-list-< ins-v version))
1814 (if (memq name package-menu--new-package-list)
1815 "new" "available"))
1816 ((version-list-< version ins-v) "obsolete")
1817 ((version-list-= version ins-v)
1818 (if (or (not package-list-unsigned) signed)
1819 "installed" "unsigned"))))))))
1821 (defun package-menu--refresh (&optional packages keywords)
1822 "Re-populate the `tabulated-list-entries'.
1823 PACKAGES should be nil or t, which means to display all known packages.
1824 KEYWORDS should be nil or a list of keywords."
1825 ;; Construct list of (PKG-DESC . STATUS).
1826 (unless packages (setq packages t))
1827 (let (info-list name)
1828 ;; Installed packages:
1829 (dolist (elt package-alist)
1830 (setq name (car elt))
1831 (when (or (eq packages t) (memq name packages))
1832 (dolist (pkg (cdr elt))
1833 (when (package--has-keyword-p pkg keywords)
1834 (package--push pkg (package-desc-status pkg) info-list)))))
1836 ;; Built-in packages:
1837 (dolist (elt package--builtins)
1838 (setq name (car elt))
1839 (when (and (not (eq name 'emacs)) ; Hide the `emacs' package.
1840 (package--has-keyword-p (package--from-builtin elt) keywords)
1841 (or package-list-unversioned
1842 (package--bi-desc-version (cdr elt)))
1843 (or (eq packages t) (memq name packages)))
1844 (package--push (package--from-builtin elt) "built-in" info-list)))
1846 ;; Available and disabled packages:
1847 (dolist (elt package-archive-contents)
1848 (setq name (car elt))
1849 (when (or (eq packages t) (memq name packages))
1850 (dolist (pkg (cdr elt))
1851 ;; Hide obsolete packages.
1852 (when (and (not (package-installed-p (package-desc-name pkg)
1853 (package-desc-version pkg)))
1854 (package--has-keyword-p pkg keywords))
1855 (package--push pkg (package-desc-status pkg) info-list)))))
1857 ;; Print the result.
1858 (setq tabulated-list-entries
1859 (mapcar #'package-menu--print-info info-list))))
1861 (defun package-all-keywords ()
1862 "Collect all package keywords"
1863 (let (keywords)
1864 (package--mapc (lambda (desc)
1865 (let* ((desc-keywords (and desc (package-desc--keywords desc))))
1866 (setq keywords (append keywords desc-keywords)))))
1867 keywords))
1869 (defun package--mapc (function &optional packages)
1870 "Call FUNCTION for all known PACKAGES.
1871 PACKAGES can be nil or t, which means to display all known
1872 packages, or a list of packages.
1874 Built-in packages are converted with `package--from-builtin'."
1875 (unless packages (setq packages t))
1876 (let (name)
1877 ;; Installed packages:
1878 (dolist (elt package-alist)
1879 (setq name (car elt))
1880 (when (or (eq packages t) (memq name packages))
1881 (mapc function (cdr elt))))
1883 ;; Built-in packages:
1884 (dolist (elt package--builtins)
1885 (setq name (car elt))
1886 (when (and (not (eq name 'emacs)) ; Hide the `emacs' package.
1887 (or package-list-unversioned
1888 (package--bi-desc-version (cdr elt)))
1889 (or (eq packages t) (memq name packages)))
1890 (funcall function (package--from-builtin elt))))
1892 ;; Available and disabled packages:
1893 (dolist (elt package-archive-contents)
1894 (setq name (car elt))
1895 (when (or (eq packages t) (memq name packages))
1896 (dolist (pkg (cdr elt))
1897 ;; Hide obsolete packages.
1898 (unless (package-installed-p (package-desc-name pkg)
1899 (package-desc-version pkg))
1900 (funcall function pkg)))))))
1902 (defun package--has-keyword-p (desc &optional keywords)
1903 "Test if package DESC has any of the given KEYWORDS.
1904 When none are given, the package matches."
1905 (if keywords
1906 (let* ((desc-keywords (and desc (package-desc--keywords desc)))
1907 found)
1908 (dolist (k keywords)
1909 (when (and (not found)
1910 (member k desc-keywords))
1911 (setq found t)))
1912 found)
1915 (defun package-menu--generate (remember-pos packages &optional keywords)
1916 "Populate the Package Menu.
1917 If REMEMBER-POS is non-nil, keep point on the same entry.
1918 PACKAGES should be t, which means to display all known packages,
1919 or a list of package names (symbols) to display.
1921 With KEYWORDS given, only packages with those keywords are
1922 shown."
1923 (package-menu--refresh packages keywords)
1924 (setf (car (aref tabulated-list-format 0))
1925 (if keywords
1926 (let ((filters (mapconcat 'identity keywords ",")))
1927 (concat "Package[" filters "]"))
1928 "Package"))
1929 (if keywords
1930 (define-key package-menu-mode-map "q" 'package-show-package-list)
1931 (define-key package-menu-mode-map "q" 'quit-window))
1932 (tabulated-list-init-header)
1933 (tabulated-list-print remember-pos))
1935 (defun package-menu--print-info (pkg)
1936 "Return a package entry suitable for `tabulated-list-entries'.
1937 PKG has the form (PKG-DESC . STATUS).
1938 Return (PKG-DESC [NAME VERSION STATUS DOC])."
1939 (let* ((pkg-desc (car pkg))
1940 (status (cdr pkg))
1941 (face (pcase status
1942 (`"built-in" 'font-lock-builtin-face)
1943 (`"available" 'default)
1944 (`"new" 'bold)
1945 (`"held" 'font-lock-constant-face)
1946 (`"disabled" 'font-lock-warning-face)
1947 (`"installed" 'font-lock-comment-face)
1948 (`"unsigned" 'font-lock-warning-face)
1949 (_ 'font-lock-warning-face)))) ; obsolete.
1950 (list pkg-desc
1951 `[,(list (symbol-name (package-desc-name pkg-desc))
1952 'face 'link
1953 'follow-link t
1954 'package-desc pkg-desc
1955 'action 'package-menu-describe-package)
1956 ,(propertize (package-version-join
1957 (package-desc-version pkg-desc))
1958 'font-lock-face face)
1959 ,(propertize status 'font-lock-face face)
1960 ,@(if (cdr package-archives)
1961 (list (propertize (or (package-desc-archive pkg-desc) "")
1962 'font-lock-face face)))
1963 ,(propertize (package-desc-summary pkg-desc)
1964 'font-lock-face face)])))
1966 (defun package-menu-refresh ()
1967 "Download the Emacs Lisp package archive.
1968 This fetches the contents of each archive specified in
1969 `package-archives', and then refreshes the package menu."
1970 (interactive)
1971 (unless (derived-mode-p 'package-menu-mode)
1972 (user-error "The current buffer is not a Package Menu"))
1973 (package-refresh-contents)
1974 (package-menu--generate t t))
1976 (defun package-menu-describe-package (&optional button)
1977 "Describe the current package.
1978 If optional arg BUTTON is non-nil, describe its associated package."
1979 (interactive)
1980 (let ((pkg-desc (if button (button-get button 'package-desc)
1981 (tabulated-list-get-id))))
1982 (if pkg-desc
1983 (describe-package pkg-desc)
1984 (user-error "No package here"))))
1986 ;; fixme numeric argument
1987 (defun package-menu-mark-delete (&optional _num)
1988 "Mark a package for deletion and move to the next line."
1989 (interactive "p")
1990 (if (member (package-menu-get-status) '("installed" "obsolete" "unsigned"))
1991 (tabulated-list-put-tag "D" t)
1992 (forward-line)))
1994 (defun package-menu-mark-install (&optional _num)
1995 "Mark a package for installation and move to the next line."
1996 (interactive "p")
1997 (if (member (package-menu-get-status) '("available" "new"))
1998 (tabulated-list-put-tag "I" t)
1999 (forward-line)))
2001 (defun package-menu-mark-unmark (&optional _num)
2002 "Clear any marks on a package and move to the next line."
2003 (interactive "p")
2004 (tabulated-list-put-tag " " t))
2006 (defun package-menu-backup-unmark ()
2007 "Back up one line and clear any marks on that package."
2008 (interactive)
2009 (forward-line -1)
2010 (tabulated-list-put-tag " "))
2012 (defun package-menu-mark-obsolete-for-deletion ()
2013 "Mark all obsolete packages for deletion."
2014 (interactive)
2015 (save-excursion
2016 (goto-char (point-min))
2017 (while (not (eobp))
2018 (if (equal (package-menu-get-status) "obsolete")
2019 (tabulated-list-put-tag "D" t)
2020 (forward-line 1)))))
2022 (defun package-menu-quick-help ()
2023 "Show short key binding help for package-menu-mode."
2024 (interactive)
2025 (message "n-ext, i-nstall, d-elete, u-nmark, x-ecute, r-efresh, h-elp"))
2027 (define-obsolete-function-alias
2028 'package-menu-view-commentary 'package-menu-describe-package "24.1")
2030 (defun package-menu-get-status ()
2031 (let* ((id (tabulated-list-get-id))
2032 (entry (and id (assq id tabulated-list-entries))))
2033 (if entry
2034 (aref (cadr entry) 2)
2035 "")))
2037 (defun package-menu--find-upgrades ()
2038 (let (installed available upgrades)
2039 ;; Build list of installed/available packages in this buffer.
2040 (dolist (entry tabulated-list-entries)
2041 ;; ENTRY is (PKG-DESC [NAME VERSION STATUS DOC])
2042 (let ((pkg-desc (car entry))
2043 (status (aref (cadr entry) 2)))
2044 (cond ((member status '("installed" "unsigned"))
2045 (push pkg-desc installed))
2046 ((member status '("available" "new"))
2047 (setq available (package--add-to-alist pkg-desc available))))))
2048 ;; Loop through list of installed packages, finding upgrades.
2049 (dolist (pkg-desc installed)
2050 (let* ((name (package-desc-name pkg-desc))
2051 (avail-pkg (cadr (assq name available))))
2052 (and avail-pkg
2053 (version-list-< (package-desc-priority-version pkg-desc)
2054 (package-desc-priority-version avail-pkg))
2055 (push (cons name avail-pkg) upgrades))))
2056 upgrades))
2058 (defun package-menu-mark-upgrades ()
2059 "Mark all upgradable packages in the Package Menu.
2060 For each installed package with a newer version available, place
2061 an (I)nstall flag on the available version and a (D)elete flag on
2062 the installed version. A subsequent \\[package-menu-execute]
2063 call will upgrade the package."
2064 (interactive)
2065 (unless (derived-mode-p 'package-menu-mode)
2066 (error "The current buffer is not a Package Menu"))
2067 (let ((upgrades (package-menu--find-upgrades)))
2068 (if (null upgrades)
2069 (message "No packages to upgrade.")
2070 (widen)
2071 (save-excursion
2072 (goto-char (point-min))
2073 (while (not (eobp))
2074 (let* ((pkg-desc (tabulated-list-get-id))
2075 (upgrade (cdr (assq (package-desc-name pkg-desc) upgrades))))
2076 (cond ((null upgrade)
2077 (forward-line 1))
2078 ((equal pkg-desc upgrade)
2079 (package-menu-mark-install))
2081 (package-menu-mark-delete))))))
2082 (message "%d package%s marked for upgrading."
2083 (length upgrades)
2084 (if (= (length upgrades) 1) "" "s")))))
2086 (defun package-menu-execute (&optional noquery)
2087 "Perform marked Package Menu actions.
2088 Packages marked for installation are downloaded and installed;
2089 packages marked for deletion are removed.
2090 Optional argument NOQUERY non-nil means do not ask the user to confirm."
2091 (interactive)
2092 (unless (derived-mode-p 'package-menu-mode)
2093 (error "The current buffer is not in Package Menu mode"))
2094 (let (install-list delete-list cmd pkg-desc)
2095 (save-excursion
2096 (goto-char (point-min))
2097 (while (not (eobp))
2098 (setq cmd (char-after))
2099 (unless (eq cmd ?\s)
2100 ;; This is the key PKG-DESC.
2101 (setq pkg-desc (tabulated-list-get-id))
2102 (cond ((eq cmd ?D)
2103 (push pkg-desc delete-list))
2104 ((eq cmd ?I)
2105 (push pkg-desc install-list))))
2106 (forward-line)))
2107 (when install-list
2108 (if (or
2109 noquery
2110 (yes-or-no-p
2111 (if (= (length install-list) 1)
2112 (format "Install package `%s'? "
2113 (package-desc-full-name (car install-list)))
2114 (format "Install these %d packages (%s)? "
2115 (length install-list)
2116 (mapconcat #'package-desc-full-name
2117 install-list ", ")))))
2118 (mapc 'package-install install-list)))
2119 ;; Delete packages, prompting if necessary.
2120 (when delete-list
2121 (if (or
2122 noquery
2123 (yes-or-no-p
2124 (if (= (length delete-list) 1)
2125 (format "Delete package `%s'? "
2126 (package-desc-full-name (car delete-list)))
2127 (format "Delete these %d packages (%s)? "
2128 (length delete-list)
2129 (mapconcat #'package-desc-full-name
2130 delete-list ", ")))))
2131 (dolist (elt delete-list)
2132 (condition-case-unless-debug err
2133 (package-delete elt)
2134 (error (message (cadr err)))))
2135 (error "Aborted")))
2136 (if (or delete-list install-list)
2137 (package-menu--generate t t)
2138 (message "No operations specified."))))
2140 (defun package-menu--version-predicate (A B)
2141 (let ((vA (or (aref (cadr A) 1) '(0)))
2142 (vB (or (aref (cadr B) 1) '(0))))
2143 (if (version-list-= vA vB)
2144 (package-menu--name-predicate A B)
2145 (version-list-< vA vB))))
2147 (defun package-menu--status-predicate (A B)
2148 (let ((sA (aref (cadr A) 2))
2149 (sB (aref (cadr B) 2)))
2150 (cond ((string= sA sB)
2151 (package-menu--name-predicate A B))
2152 ((string= sA "new") t)
2153 ((string= sB "new") nil)
2154 ((string= sA "available") t)
2155 ((string= sB "available") nil)
2156 ((string= sA "installed") t)
2157 ((string= sB "installed") nil)
2158 ((string= sA "unsigned") t)
2159 ((string= sB "unsigned") nil)
2160 ((string= sA "held") t)
2161 ((string= sB "held") nil)
2162 ((string= sA "built-in") t)
2163 ((string= sB "built-in") nil)
2164 ((string= sA "obsolete") t)
2165 ((string= sB "obsolete") nil)
2166 (t (string< sA sB)))))
2168 (defun package-menu--description-predicate (A B)
2169 (let ((dA (aref (cadr A) 3))
2170 (dB (aref (cadr B) 3)))
2171 (if (string= dA dB)
2172 (package-menu--name-predicate A B)
2173 (string< dA dB))))
2175 (defun package-menu--name-predicate (A B)
2176 (string< (symbol-name (package-desc-name (car A)))
2177 (symbol-name (package-desc-name (car B)))))
2179 (defun package-menu--archive-predicate (A B)
2180 (string< (or (package-desc-archive (car A)) "")
2181 (or (package-desc-archive (car B)) "")))
2183 ;;;###autoload
2184 (defun list-packages (&optional no-fetch)
2185 "Display a list of packages.
2186 This first fetches the updated list of packages before
2187 displaying, unless a prefix argument NO-FETCH is specified.
2188 The list is displayed in a buffer named `*Packages*'."
2189 (interactive "P")
2190 (require 'finder-inf nil t)
2191 ;; Initialize the package system if necessary.
2192 (unless package--initialized
2193 (package-initialize t))
2194 (let (old-archives new-packages)
2195 (unless no-fetch
2196 ;; Read the locally-cached archive-contents.
2197 (package-read-all-archive-contents)
2198 (setq old-archives package-archive-contents)
2199 ;; Fetch the remote list of packages.
2200 (package-refresh-contents)
2201 ;; Find which packages are new.
2202 (dolist (elt package-archive-contents)
2203 (unless (assq (car elt) old-archives)
2204 (push (car elt) new-packages))))
2206 ;; Generate the Package Menu.
2207 (let ((buf (get-buffer-create "*Packages*")))
2208 (with-current-buffer buf
2209 (package-menu-mode)
2210 (set (make-local-variable 'package-menu--new-package-list)
2211 new-packages)
2212 (package-menu--generate nil t))
2213 ;; The package menu buffer has keybindings. If the user types
2214 ;; `M-x list-packages', that suggests it should become current.
2215 (switch-to-buffer buf))
2217 (let ((upgrades (package-menu--find-upgrades)))
2218 (if upgrades
2219 (message "%d package%s can be upgraded; type `%s' to mark %s for upgrading."
2220 (length upgrades)
2221 (if (= (length upgrades) 1) "" "s")
2222 (substitute-command-keys "\\[package-menu-mark-upgrades]")
2223 (if (= (length upgrades) 1) "it" "them"))))))
2225 ;;;###autoload
2226 (defalias 'package-list-packages 'list-packages)
2228 ;; Used in finder.el
2229 (defun package-show-package-list (&optional packages keywords)
2230 "Display PACKAGES in a *Packages* buffer.
2231 This is similar to `list-packages', but it does not fetch the
2232 updated list of packages, and it only displays packages with
2233 names in PACKAGES (which should be a list of symbols).
2235 When KEYWORDS are given, only packages with those KEYWORDS are
2236 shown."
2237 (interactive)
2238 (require 'finder-inf nil t)
2239 (let* ((buf (get-buffer-create "*Packages*"))
2240 (win (get-buffer-window buf)))
2241 (with-current-buffer buf
2242 (package-menu-mode)
2243 (package-menu--generate nil packages keywords))
2244 (if win
2245 (select-window win)
2246 (switch-to-buffer buf))))
2248 ;; package-menu--generate rebinds "q" on the fly, so we have to
2249 ;; hard-code the binding in the doc-string here.
2250 (defun package-menu-filter (keyword)
2251 "Filter the *Packages* buffer.
2252 Show only those items that relate to the specified KEYWORD.
2253 To restore the full package list, type `q'."
2254 (interactive (list (completing-read "Keyword: " (package-all-keywords))))
2255 (package-show-package-list t (list keyword)))
2257 (defun package-list-packages-no-fetch ()
2258 "Display a list of packages.
2259 Does not fetch the updated list of packages before displaying.
2260 The list is displayed in a buffer named `*Packages*'."
2261 (interactive)
2262 (list-packages t))
2264 (provide 'package)
2266 ;;; package.el ends here