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