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