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