package.el: Fix some version tags
[emacs.git] / lisp / emacs-lisp / package.el
blob27ebd3cabb1c8dfbbc082ac5b4c35a66297dff1c
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 'packagea
294 :version "24.4")
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.4")
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 &optional seen)
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.
885 SEEN is used internally to detect infinite recursion."
886 ;; FIXME: We really should use backtracking to explore the whole
887 ;; search space (e.g. if foo require bar-1.3, and bar-1.4 requires toto-1.1
888 ;; whereas bar-1.3 requires toto-1.0 and the user has put a hold on toto-1.0:
889 ;; the current code might fail to see that it could install foo by using the
890 ;; older bar-1.3).
891 (dolist (elt requirements)
892 (let* ((next-pkg (car elt))
893 (next-version (cadr elt))
894 (already ()))
895 (dolist (pkg packages)
896 (if (eq next-pkg (package-desc-name pkg))
897 (setq already pkg)))
898 (when already
899 (if (version-list-<= next-version (package-desc-version already))
900 ;; `next-pkg' is already in `packages', but its position there
901 ;; means it might be installed too late: remove it from there, so
902 ;; we re-add it (along with its dependencies) at an earlier place
903 ;; below (bug#16994).
904 (if (memq already seen) ;Avoid inf-loop on dependency cycles.
905 (message "Dependency cycle going through %S"
906 (package-desc-full-name already))
907 (setq packages (delq already packages))
908 (setq already nil))
909 (error "Need package `%s-%s', but only %s is being installed"
910 next-pkg (package-version-join next-version)
911 (package-version-join (package-desc-version already)))))
912 (cond
913 (already nil)
914 ((package-installed-p next-pkg next-version) nil)
917 ;; A package is required, but not installed. It might also be
918 ;; blocked via `package-load-list'.
919 (let ((pkg-descs (cdr (assq next-pkg package-archive-contents)))
920 (found nil)
921 (problem nil))
922 (while (and pkg-descs (not found))
923 (let* ((pkg-desc (pop pkg-descs))
924 (version (package-desc-version pkg-desc))
925 (disabled (package-disabled-p next-pkg version)))
926 (cond
927 ((version-list-< version next-version)
928 (error
929 "Need package `%s-%s', but only %s is available"
930 next-pkg (package-version-join next-version)
931 (package-version-join version)))
932 (disabled
933 (unless problem
934 (setq problem
935 (if (stringp disabled)
936 (format "Package `%s' held at version %s, \
937 but version %s required"
938 next-pkg disabled
939 (package-version-join next-version))
940 (format "Required package '%s' is disabled"
941 next-pkg)))))
942 (t (setq found pkg-desc)))))
943 (unless found
944 (if problem
945 (error "%s" problem)
946 (error "Package `%s-%s' is unavailable"
947 next-pkg (package-version-join next-version))))
948 (setq packages
949 (package-compute-transaction (cons found packages)
950 (package-desc-reqs found)
951 (cons found seen))))))))
952 packages)
954 (defun package-read-from-string (str)
955 "Read a Lisp expression from STR.
956 Signal an error if the entire string was not used."
957 (let* ((read-data (read-from-string str))
958 (more-left
959 (condition-case nil
960 ;; The call to `ignore' suppresses a compiler warning.
961 (progn (ignore (read-from-string
962 (substring str (cdr read-data))))
964 (end-of-file nil))))
965 (if more-left
966 (error "Can't read whole string")
967 (car read-data))))
969 (defun package--read-archive-file (file)
970 "Re-read archive file FILE, if it exists.
971 Will return the data from the file, or nil if the file does not exist.
972 Will throw an error if the archive version is too new."
973 (let ((filename (expand-file-name file package-user-dir)))
974 (when (file-exists-p filename)
975 (with-temp-buffer
976 (insert-file-contents-literally filename)
977 (let ((contents (read (current-buffer))))
978 (if (> (car contents) package-archive-version)
979 (error "Package archive version %d is higher than %d"
980 (car contents) package-archive-version))
981 (cdr contents))))))
983 (defun package-read-all-archive-contents ()
984 "Re-read `archive-contents', if it exists.
985 If successful, set `package-archive-contents'."
986 (setq package-archive-contents nil)
987 (dolist (archive package-archives)
988 (package-read-archive-contents (car archive))))
990 (defun package-read-archive-contents (archive)
991 "Re-read archive contents for ARCHIVE.
992 If successful, set the variable `package-archive-contents'.
993 If the archive version is too new, signal an error."
994 ;; Version 1 of 'archive-contents' is identical to our internal
995 ;; representation.
996 (let* ((contents-file (format "archives/%s/archive-contents" archive))
997 (contents (package--read-archive-file contents-file)))
998 (when contents
999 (dolist (package contents)
1000 (package--add-to-archive-contents package archive)))))
1002 ;; Package descriptor objects used inside the "archive-contents" file.
1003 ;; Changing this defstruct implies changing the format of the
1004 ;; "archive-contents" files.
1005 (cl-defstruct (package--ac-desc
1006 (:constructor package-make-ac-desc (version reqs summary kind extras))
1007 (:copier nil)
1008 (:type vector))
1009 version reqs summary kind extras)
1011 (defun package--add-to-archive-contents (package archive)
1012 "Add the PACKAGE from the given ARCHIVE if necessary.
1013 PACKAGE should have the form (NAME . PACKAGE--AC-DESC).
1014 Also, add the originating archive to the `package-desc' structure."
1015 (let* ((name (car package))
1016 (version (package--ac-desc-version (cdr package)))
1017 (pkg-desc
1018 (package-desc-create
1019 :name name
1020 :version version
1021 :reqs (package--ac-desc-reqs (cdr package))
1022 :summary (package--ac-desc-summary (cdr package))
1023 :kind (package--ac-desc-kind (cdr package))
1024 :archive archive
1025 :extras (and (> (length (cdr package)) 4)
1026 ;; Older archive-contents files have only 4
1027 ;; elements here.
1028 (package--ac-desc-extras (cdr package)))))
1029 (existing-packages (assq name package-archive-contents))
1030 (pinned-to-archive (assoc name package-pinned-packages)))
1031 (cond
1032 ;; Skip entirely if pinned to another archive.
1033 ((and pinned-to-archive
1034 (not (equal (cdr pinned-to-archive) archive)))
1035 nil)
1036 ((not existing-packages)
1037 (push (list name pkg-desc) package-archive-contents))
1039 (while
1040 (if (and (cdr existing-packages)
1041 (version-list-<
1042 version (package-desc-version (cadr existing-packages))))
1043 (setq existing-packages (cdr existing-packages))
1044 (push pkg-desc (cdr existing-packages))
1045 nil))))))
1047 (defun package-download-transaction (packages)
1048 "Download and install all the packages in PACKAGES.
1049 PACKAGES should be a list of package-desc.
1050 This function assumes that all package requirements in
1051 PACKAGES are satisfied, i.e. that PACKAGES is computed
1052 using `package-compute-transaction'."
1053 (mapc #'package-install-from-archive packages))
1055 ;;;###autoload
1056 (defun package-install (pkg)
1057 "Install the package PKG.
1058 PKG can be a package-desc or the package name of one the available packages
1059 in an archive in `package-archives'. Interactively, prompt for its name."
1060 (interactive
1061 (progn
1062 ;; Initialize the package system to get the list of package
1063 ;; symbols for completion.
1064 (unless package--initialized
1065 (package-initialize t))
1066 (unless package-archive-contents
1067 (package-refresh-contents))
1068 (list (intern (completing-read
1069 "Install package: "
1070 (delq nil
1071 (mapcar (lambda (elt)
1072 (unless (package-installed-p (car elt))
1073 (symbol-name (car elt))))
1074 package-archive-contents))
1075 nil t)))))
1076 (package-download-transaction
1077 (if (package-desc-p pkg)
1078 (package-compute-transaction (list pkg)
1079 (package-desc-reqs pkg))
1080 (package-compute-transaction ()
1081 (list (list pkg))))))
1083 (defun package-strip-rcs-id (str)
1084 "Strip RCS version ID from the version string STR.
1085 If the result looks like a dotted numeric version, return it.
1086 Otherwise return nil."
1087 (when str
1088 (when (string-match "\\`[ \t]*[$]Revision:[ \t]+" str)
1089 (setq str (substring str (match-end 0))))
1090 (condition-case nil
1091 (if (version-to-list str)
1092 str)
1093 (error nil))))
1095 (declare-function lm-homepage "lisp-mnt" (&optional file))
1097 (defun package--prepare-dependencies (deps)
1098 "Turn DEPS into an acceptable list of dependencies.
1100 Any parts missing a version string get a default version string
1101 of \"0\" (meaning any version) and an appropriate level of lists
1102 is wrapped around any parts requiring it."
1103 (cond
1104 ((not (listp deps))
1105 (error "Invalid requirement specifier: %S" deps))
1106 (t (mapcar (lambda (dep)
1107 (cond
1108 ((symbolp dep) `(,dep "0"))
1109 ((stringp dep)
1110 (error "Invalid requirement specifier: %S" dep))
1111 ((and (listp dep) (null (cdr dep)))
1112 (list (car dep) "0"))
1113 (t dep)))
1114 deps))))
1116 (defun package-buffer-info ()
1117 "Return a `package-desc' describing the package in the current buffer.
1119 If the buffer does not contain a conforming package, signal an
1120 error. If there is a package, narrow the buffer to the file's
1121 boundaries."
1122 (goto-char (point-min))
1123 (unless (re-search-forward "^;;; \\([^ ]*\\)\\.el ---[ \t]*\\(.*?\\)[ \t]*\\(-\\*-.*-\\*-[ \t]*\\)?$" nil t)
1124 (error "Package lacks a file header"))
1125 (let ((file-name (match-string-no-properties 1))
1126 (desc (match-string-no-properties 2))
1127 (start (line-beginning-position)))
1128 (unless (search-forward (concat ";;; " file-name ".el ends here"))
1129 (error "Package lacks a terminating comment"))
1130 ;; Try to include a trailing newline.
1131 (forward-line)
1132 (narrow-to-region start (point))
1133 (require 'lisp-mnt)
1134 ;; Use some headers we've invented to drive the process.
1135 (let* ((requires-str (lm-header "package-requires"))
1136 ;; Prefer Package-Version; if defined, the package author
1137 ;; probably wants us to use it. Otherwise try Version.
1138 (pkg-version
1139 (or (package-strip-rcs-id (lm-header "package-version"))
1140 (package-strip-rcs-id (lm-header "version"))))
1141 (homepage (lm-homepage)))
1142 (unless pkg-version
1143 (error
1144 "Package lacks a \"Version\" or \"Package-Version\" header"))
1145 (package-desc-from-define
1146 file-name pkg-version desc
1147 (if requires-str
1148 (package--prepare-dependencies
1149 (package-read-from-string requires-str)))
1150 :kind 'single
1151 :url homepage))))
1153 (declare-function tar-get-file-descriptor "tar-mode" (file))
1154 (declare-function tar--extract "tar-mode" (descriptor))
1156 (defun package-tar-file-info ()
1157 "Find package information for a tar file.
1158 The return result is a `package-desc'."
1159 (cl-assert (derived-mode-p 'tar-mode))
1160 (let* ((dir-name (file-name-directory
1161 (tar-header-name (car tar-parse-info))))
1162 (desc-file (package--description-file dir-name))
1163 (tar-desc (tar-get-file-descriptor (concat dir-name desc-file))))
1164 (unless tar-desc
1165 (error "No package descriptor file found"))
1166 (with-current-buffer (tar--extract tar-desc)
1167 (goto-char (point-min))
1168 (unwind-protect
1169 (let* ((pkg-def-parsed (read (current-buffer)))
1170 (pkg-desc
1171 (if (not (eq (car pkg-def-parsed) 'define-package))
1172 (error "Can't find define-package in %s"
1173 (tar-header-name tar-desc))
1174 (apply #'package-desc-from-define
1175 (append (cdr pkg-def-parsed))))))
1176 (setf (package-desc-kind pkg-desc) 'tar)
1177 pkg-desc)
1178 (kill-buffer (current-buffer))))))
1181 ;;;###autoload
1182 (defun package-install-from-buffer ()
1183 "Install a package from the current buffer.
1184 The current buffer is assumed to be a single .el or .tar file that follows the
1185 packaging guidelines; see info node `(elisp)Packaging'.
1186 Downloads and installs required packages as needed."
1187 (interactive)
1188 (let ((pkg-desc (if (derived-mode-p 'tar-mode)
1189 (package-tar-file-info)
1190 (package-buffer-info))))
1191 ;; Download and install the dependencies.
1192 (let* ((requires (package-desc-reqs pkg-desc))
1193 (transaction (package-compute-transaction nil requires)))
1194 (package-download-transaction transaction))
1195 ;; Install the package itself.
1196 (package-unpack pkg-desc)
1197 pkg-desc))
1199 ;;;###autoload
1200 (defun package-install-file (file)
1201 "Install a package from a file.
1202 The file can either be a tar file or an Emacs Lisp file."
1203 (interactive "fPackage file name: ")
1204 (with-temp-buffer
1205 (insert-file-contents-literally file)
1206 (when (string-match "\\.tar\\'" file) (tar-mode))
1207 (package-install-from-buffer)))
1209 (defun package-delete (pkg-desc)
1210 (let ((dir (package-desc-dir pkg-desc)))
1211 (if (not (string-prefix-p (file-name-as-directory
1212 (expand-file-name package-user-dir))
1213 (expand-file-name dir)))
1214 ;; Don't delete "system" packages.
1215 (error "Package `%s' is a system package, not deleting"
1216 (package-desc-full-name pkg-desc))
1217 (delete-directory dir t t)
1218 ;; Remove NAME-VERSION.signed file.
1219 (let ((signed-file (concat dir ".signed")))
1220 (if (file-exists-p signed-file)
1221 (delete-file signed-file)))
1222 ;; Update package-alist.
1223 (let* ((name (package-desc-name pkg-desc))
1224 (pkgs (assq name package-alist)))
1225 (delete pkg-desc pkgs)
1226 (unless (cdr pkgs)
1227 (setq package-alist (delq pkgs package-alist))))
1228 (message "Package `%s' deleted." (package-desc-full-name pkg-desc)))))
1230 (defun package-archive-base (desc)
1231 "Return the archive containing the package NAME."
1232 (cdr (assoc (package-desc-archive desc) package-archives)))
1234 (defun package--download-one-archive (archive file)
1235 "Retrieve an archive file FILE from ARCHIVE, and cache it.
1236 ARCHIVE should be a cons cell of the form (NAME . LOCATION),
1237 similar to an entry in `package-alist'. Save the cached copy to
1238 \"archives/NAME/archive-contents\" in `package-user-dir'."
1239 (let ((dir (expand-file-name (format "archives/%s" (car archive))
1240 package-user-dir))
1241 (sig-file (concat file ".sig"))
1242 good-signatures)
1243 (package--with-work-buffer (cdr archive) file
1244 ;; Check signature of archive-contents, if desired.
1245 (if (and package-check-signature
1246 (not (member archive package-unsigned-archives)))
1247 (if (package--archive-file-exists-p (cdr archive) sig-file)
1248 (setq good-signatures (package--check-signature (cdr archive)
1249 file))
1250 (unless (eq package-check-signature 'allow-unsigned)
1251 (error "Unsigned archive `%s'"
1252 (car archive)))))
1253 ;; Read the retrieved buffer to make sure it is valid (e.g. it
1254 ;; may fetch a URL redirect page).
1255 (when (listp (read (current-buffer)))
1256 (make-directory dir t)
1257 (write-region nil nil (expand-file-name file dir) nil 'silent)))
1258 (when good-signatures
1259 ;; Write out good signatures into archive-contents.signed file.
1260 (write-region (mapconcat #'epg-signature-to-string good-signatures "\n")
1262 (expand-file-name (concat file ".signed") dir)
1263 nil 'silent))))
1265 (declare-function epg-check-configuration "epg-config"
1266 (config &optional minimum-version))
1267 (declare-function epg-configuration "epg-config" ())
1268 (declare-function epg-import-keys-from-file "epg" (context keys))
1270 ;;;###autoload
1271 (defun package-import-keyring (&optional file)
1272 "Import keys from FILE."
1273 (interactive "fFile: ")
1274 (setq file (expand-file-name file))
1275 (let ((context (epg-make-context 'OpenPGP))
1276 (homedir (expand-file-name "gnupg" package-user-dir)))
1277 (make-directory homedir t)
1278 (epg-context-set-home-directory context homedir)
1279 (message "Importing %s..." (file-name-nondirectory file))
1280 (epg-import-keys-from-file context file)
1281 (message "Importing %s...done" (file-name-nondirectory file))))
1283 ;;;###autoload
1284 (defun package-refresh-contents ()
1285 "Download the ELPA archive description if needed.
1286 This informs Emacs about the latest versions of all packages, and
1287 makes them available for download."
1288 (interactive)
1289 ;; FIXME: Do it asynchronously.
1290 (unless (file-exists-p package-user-dir)
1291 (make-directory package-user-dir t))
1292 (let ((default-keyring (expand-file-name "package-keyring.gpg"
1293 data-directory)))
1294 (if (file-exists-p default-keyring)
1295 (condition-case-unless-debug error
1296 (progn
1297 (epg-check-configuration (epg-configuration))
1298 (package-import-keyring default-keyring))
1299 (error (message "Cannot import default keyring: %S" (cdr error))))))
1300 (dolist (archive package-archives)
1301 (condition-case-unless-debug nil
1302 (package--download-one-archive archive "archive-contents")
1303 (error (message "Failed to download `%s' archive."
1304 (car archive)))))
1305 (package-read-all-archive-contents))
1307 ;;;###autoload
1308 (defun package-initialize (&optional no-activate)
1309 "Load Emacs Lisp packages, and activate them.
1310 The variable `package-load-list' controls which packages to load.
1311 If optional arg NO-ACTIVATE is non-nil, don't activate packages."
1312 (interactive)
1313 (setq package-alist nil)
1314 (package-load-all-descriptors)
1315 (package-read-all-archive-contents)
1316 (unless no-activate
1317 (dolist (elt package-alist)
1318 (package-activate (car elt))))
1319 (setq package--initialized t))
1322 ;;;; Package description buffer.
1324 ;;;###autoload
1325 (defun describe-package (package)
1326 "Display the full documentation of PACKAGE (a symbol)."
1327 (interactive
1328 (let* ((guess (function-called-at-point)))
1329 (require 'finder-inf nil t)
1330 ;; Load the package list if necessary (but don't activate them).
1331 (unless package--initialized
1332 (package-initialize t))
1333 (let ((packages (append (mapcar 'car package-alist)
1334 (mapcar 'car package-archive-contents)
1335 (mapcar 'car package--builtins))))
1336 (unless (memq guess packages)
1337 (setq guess nil))
1338 (setq packages (mapcar 'symbol-name packages))
1339 (let ((val
1340 (completing-read (if guess
1341 (format "Describe package (default %s): "
1342 guess)
1343 "Describe package: ")
1344 packages nil t nil nil guess)))
1345 (list (intern val))))))
1346 (if (not (or (package-desc-p package) (and package (symbolp package))))
1347 (message "No package specified")
1348 (help-setup-xref (list #'describe-package package)
1349 (called-interactively-p 'interactive))
1350 (with-help-window (help-buffer)
1351 (with-current-buffer standard-output
1352 (describe-package-1 package)))))
1354 (defun describe-package-1 (pkg)
1355 (require 'lisp-mnt)
1356 (let* ((desc (or
1357 (if (package-desc-p pkg) pkg)
1358 (cadr (assq pkg package-alist))
1359 (let ((built-in (assq pkg package--builtins)))
1360 (if built-in
1361 (package--from-builtin built-in)
1362 (cadr (assq pkg package-archive-contents))))))
1363 (name (if desc (package-desc-name desc) pkg))
1364 (pkg-dir (if desc (package-desc-dir desc)))
1365 (reqs (if desc (package-desc-reqs desc)))
1366 (version (if desc (package-desc-version desc)))
1367 (archive (if desc (package-desc-archive desc)))
1368 (extras (and desc (package-desc-extras desc)))
1369 (homepage (cdr (assoc :url extras)))
1370 (keywords (if desc (package-desc--keywords desc)))
1371 (built-in (eq pkg-dir 'builtin))
1372 (installable (and archive (not built-in)))
1373 (status (if desc (package-desc-status desc) "orphan"))
1374 (signed (if desc (package-desc-signed desc))))
1375 (prin1 name)
1376 (princ " is ")
1377 (princ (if (memq (aref status 0) '(?a ?e ?i ?o ?u)) "an " "a "))
1378 (princ status)
1379 (princ " package.\n\n")
1381 (insert " " (propertize "Status" 'font-lock-face 'bold) ": ")
1382 (cond (built-in
1383 (insert (propertize (capitalize status)
1384 'font-lock-face 'font-lock-builtin-face)
1385 "."))
1386 (pkg-dir
1387 (insert (propertize (if (equal status "unsigned")
1388 "Installed"
1389 (capitalize status)) ;FIXME: Why comment-face?
1390 'font-lock-face 'font-lock-comment-face))
1391 (insert " in `")
1392 ;; Todo: Add button for uninstalling.
1393 (help-insert-xref-button (abbreviate-file-name
1394 (file-name-as-directory pkg-dir))
1395 'help-package-def pkg-dir)
1396 (if (and (package-built-in-p name)
1397 (not (package-built-in-p name version)))
1398 (insert "',\n shadowing a "
1399 (propertize "built-in package"
1400 'font-lock-face 'font-lock-builtin-face))
1401 (insert "'"))
1402 (if signed
1403 (insert ".")
1404 (insert " (unsigned).")))
1405 (installable
1406 (insert (capitalize status))
1407 (insert " from " (format "%s" archive))
1408 (insert " -- ")
1409 (package-make-button
1410 "Install"
1411 'action 'package-install-button-action
1412 'package-desc desc))
1413 (t (insert (capitalize status) ".")))
1414 (insert "\n")
1415 (insert " " (propertize "Archive" 'font-lock-face 'bold)
1416 ": " (or archive "n/a") "\n")
1417 (and version
1418 (insert " "
1419 (propertize "Version" 'font-lock-face 'bold) ": "
1420 (package-version-join version) "\n"))
1422 (setq reqs (if desc (package-desc-reqs desc)))
1423 (when reqs
1424 (insert " " (propertize "Requires" 'font-lock-face 'bold) ": ")
1425 (let ((first t)
1426 name vers text)
1427 (dolist (req reqs)
1428 (setq name (car req)
1429 vers (cadr req)
1430 text (format "%s-%s" (symbol-name name)
1431 (package-version-join vers)))
1432 (cond (first (setq first nil))
1433 ((>= (+ 2 (current-column) (length text))
1434 (window-width))
1435 (insert ",\n "))
1436 (t (insert ", ")))
1437 (help-insert-xref-button text 'help-package name))
1438 (insert "\n")))
1439 (insert " " (propertize "Summary" 'font-lock-face 'bold)
1440 ": " (if desc (package-desc-summary desc)) "\n")
1441 (when homepage
1442 (insert " " (propertize "Homepage" 'font-lock-face 'bold) ": ")
1443 (help-insert-xref-button homepage 'help-url homepage)
1444 (insert "\n"))
1445 (when keywords
1446 (insert " " (propertize "Keywords" 'font-lock-face 'bold) ": ")
1447 (dolist (k keywords)
1448 (package-make-button
1450 'package-keyword k
1451 'action 'package-keyword-button-action)
1452 (insert " "))
1453 (insert "\n"))
1454 (let* ((all-pkgs (append (cdr (assq name package-alist))
1455 (cdr (assq name package-archive-contents))
1456 (let ((bi (assq name package--builtins)))
1457 (if bi (list (package--from-builtin bi))))))
1458 (other-pkgs (delete desc all-pkgs)))
1459 (when other-pkgs
1460 (insert " " (propertize "Other versions" 'font-lock-face 'bold) ": "
1461 (mapconcat
1462 (lambda (opkg)
1463 (let* ((ov (package-desc-version opkg))
1464 (dir (package-desc-dir opkg))
1465 (from (or (package-desc-archive opkg)
1466 (if (stringp dir) "installed" dir))))
1467 (if (not ov) (format "%s" from)
1468 (format "%s (%s)"
1469 (make-text-button (package-version-join ov) nil
1470 'face 'link
1471 'follow-link t
1472 'action
1473 (lambda (_button)
1474 (describe-package opkg)))
1475 from))))
1476 other-pkgs ", ")
1477 ".\n")))
1479 (insert "\n")
1481 (if built-in
1482 ;; For built-in packages, insert the commentary.
1483 (let ((fn (locate-file (format "%s.el" name) load-path
1484 load-file-rep-suffixes))
1485 (opoint (point)))
1486 (insert (or (lm-commentary fn) ""))
1487 (save-excursion
1488 (goto-char opoint)
1489 (when (re-search-forward "^;;; Commentary:\n" nil t)
1490 (replace-match ""))
1491 (while (re-search-forward "^\\(;+ ?\\)" nil t)
1492 (replace-match ""))))
1493 (let ((readme (expand-file-name (format "%s-readme.txt" name)
1494 package-user-dir))
1495 readme-string)
1496 ;; For elpa packages, try downloading the commentary. If that
1497 ;; fails, try an existing readme file in `package-user-dir'.
1498 (cond ((condition-case nil
1499 (save-excursion
1500 (package--with-work-buffer
1501 (package-archive-base desc)
1502 (format "%s-readme.txt" name)
1503 (save-excursion
1504 (goto-char (point-max))
1505 (unless (bolp)
1506 (insert ?\n)))
1507 (write-region nil nil
1508 (expand-file-name readme package-user-dir)
1509 nil 'silent)
1510 (setq readme-string (buffer-string))
1512 (error nil))
1513 (insert readme-string))
1514 ((file-readable-p readme)
1515 (insert-file-contents readme)
1516 (goto-char (point-max))))))))
1518 (defun package-install-button-action (button)
1519 (let ((pkg-desc (button-get button 'package-desc)))
1520 (when (y-or-n-p (format "Install package `%s'? "
1521 (package-desc-full-name pkg-desc)))
1522 (package-install pkg-desc)
1523 (revert-buffer nil t)
1524 (goto-char (point-min)))))
1526 (defun package-keyword-button-action (button)
1527 (let ((pkg-keyword (button-get button 'package-keyword)))
1528 (package-show-package-list t (list pkg-keyword))))
1530 (defun package-make-button (text &rest props)
1531 (let ((button-text (if (display-graphic-p) text (concat "[" text "]")))
1532 (button-face (if (display-graphic-p)
1533 '(:box (:line-width 2 :color "dark grey")
1534 :background "light grey"
1535 :foreground "black")
1536 'link)))
1537 (apply 'insert-text-button button-text 'face button-face 'follow-link t
1538 props)))
1541 ;;;; Package menu mode.
1543 (defvar package-menu-mode-map
1544 (let ((map (make-sparse-keymap))
1545 (menu-map (make-sparse-keymap "Package")))
1546 (set-keymap-parent map tabulated-list-mode-map)
1547 (define-key map "\C-m" 'package-menu-describe-package)
1548 (define-key map "u" 'package-menu-mark-unmark)
1549 (define-key map "\177" 'package-menu-backup-unmark)
1550 (define-key map "d" 'package-menu-mark-delete)
1551 (define-key map "i" 'package-menu-mark-install)
1552 (define-key map "U" 'package-menu-mark-upgrades)
1553 (define-key map "r" 'package-menu-refresh)
1554 (define-key map "f" 'package-menu-filter)
1555 (define-key map "~" 'package-menu-mark-obsolete-for-deletion)
1556 (define-key map "x" 'package-menu-execute)
1557 (define-key map "h" 'package-menu-quick-help)
1558 (define-key map "?" 'package-menu-describe-package)
1559 (define-key map [menu-bar package-menu] (cons "Package" menu-map))
1560 (define-key menu-map [mq]
1561 '(menu-item "Quit" quit-window
1562 :help "Quit package selection"))
1563 (define-key menu-map [s1] '("--"))
1564 (define-key menu-map [mn]
1565 '(menu-item "Next" next-line
1566 :help "Next Line"))
1567 (define-key menu-map [mp]
1568 '(menu-item "Previous" previous-line
1569 :help "Previous Line"))
1570 (define-key menu-map [s2] '("--"))
1571 (define-key menu-map [mu]
1572 '(menu-item "Unmark" package-menu-mark-unmark
1573 :help "Clear any marks on a package and move to the next line"))
1574 (define-key menu-map [munm]
1575 '(menu-item "Unmark Backwards" package-menu-backup-unmark
1576 :help "Back up one line and clear any marks on that package"))
1577 (define-key menu-map [md]
1578 '(menu-item "Mark for Deletion" package-menu-mark-delete
1579 :help "Mark a package for deletion and move to the next line"))
1580 (define-key menu-map [mi]
1581 '(menu-item "Mark for Install" package-menu-mark-install
1582 :help "Mark a package for installation and move to the next line"))
1583 (define-key menu-map [mupgrades]
1584 '(menu-item "Mark Upgradable Packages" package-menu-mark-upgrades
1585 :help "Mark packages that have a newer version for upgrading"))
1586 (define-key menu-map [s3] '("--"))
1587 (define-key menu-map [mf]
1588 '(menu-item "Filter Package List..." package-menu-filter
1589 :help "Filter package selection (q to go back)"))
1590 (define-key menu-map [mg]
1591 '(menu-item "Update Package List" revert-buffer
1592 :help "Update the list of packages"))
1593 (define-key menu-map [mr]
1594 '(menu-item "Refresh Package List" package-menu-refresh
1595 :help "Download the ELPA archive"))
1596 (define-key menu-map [s4] '("--"))
1597 (define-key menu-map [mt]
1598 '(menu-item "Mark Obsolete Packages" package-menu-mark-obsolete-for-deletion
1599 :help "Mark all obsolete packages for deletion"))
1600 (define-key menu-map [mx]
1601 '(menu-item "Execute Actions" package-menu-execute
1602 :help "Perform all the marked actions"))
1603 (define-key menu-map [s5] '("--"))
1604 (define-key menu-map [mh]
1605 '(menu-item "Help" package-menu-quick-help
1606 :help "Show short key binding help for package-menu-mode"))
1607 (define-key menu-map [mc]
1608 '(menu-item "Describe Package" package-menu-describe-package
1609 :help "Display information about this package"))
1610 map)
1611 "Local keymap for `package-menu-mode' buffers.")
1613 (defvar package-menu--new-package-list nil
1614 "List of newly-available packages since `list-packages' was last called.")
1616 (define-derived-mode package-menu-mode tabulated-list-mode "Package Menu"
1617 "Major mode for browsing a list of packages.
1618 Letters do not insert themselves; instead, they are commands.
1619 \\<package-menu-mode-map>
1620 \\{package-menu-mode-map}"
1621 (setq tabulated-list-format
1622 `[("Package" 18 package-menu--name-predicate)
1623 ("Version" 12 nil)
1624 ("Status" 10 package-menu--status-predicate)
1625 ,@(if (cdr package-archives)
1626 '(("Archive" 10 package-menu--archive-predicate)))
1627 ("Description" 0 nil)])
1628 (setq tabulated-list-padding 2)
1629 (setq tabulated-list-sort-key (cons "Status" nil))
1630 (add-hook 'tabulated-list-revert-hook 'package-menu--refresh nil t)
1631 (tabulated-list-init-header))
1633 (defmacro package--push (pkg-desc status listname)
1634 "Convenience macro for `package-menu--generate'.
1635 If the alist stored in the symbol LISTNAME lacks an entry for a
1636 package PKG-DESC, add one. The alist is keyed with PKG-DESC."
1637 `(unless (assoc ,pkg-desc ,listname)
1638 ;; FIXME: Should we move status into pkg-desc?
1639 (push (cons ,pkg-desc ,status) ,listname)))
1641 (defvar package-list-unversioned nil
1642 "If non-nil include packages that don't have a version in `list-package'.")
1644 (defun package-desc-status (pkg-desc)
1645 (let* ((name (package-desc-name pkg-desc))
1646 (dir (package-desc-dir pkg-desc))
1647 (lle (assq name package-load-list))
1648 (held (cadr lle))
1649 (version (package-desc-version pkg-desc))
1650 (signed (package-desc-signed pkg-desc)))
1651 (cond
1652 ((eq dir 'builtin) "built-in")
1653 ((and lle (null held)) "disabled")
1654 ((stringp held)
1655 (let ((hv (if (stringp held) (version-to-list held))))
1656 (cond
1657 ((version-list-= version hv) "held")
1658 ((version-list-< version hv) "obsolete")
1659 (t "disabled"))))
1660 ((package-built-in-p name version) "obsolete")
1661 (dir ;One of the installed packages.
1662 (cond
1663 ((not (file-exists-p (package-desc-dir pkg-desc))) "deleted")
1664 ((eq pkg-desc (cadr (assq name package-alist))) (if signed
1665 "installed"
1666 "unsigned"))
1667 (t "obsolete")))
1669 (let* ((ins (cadr (assq name package-alist)))
1670 (ins-v (if ins (package-desc-version ins))))
1671 (cond
1672 ((or (null ins) (version-list-< ins-v version))
1673 (if (memq name package-menu--new-package-list)
1674 "new" "available"))
1675 ((version-list-< version ins-v) "obsolete")
1676 ((version-list-= version ins-v) (if signed
1677 "installed"
1678 "unsigned"))))))))
1680 (defun package-menu--refresh (&optional packages keywords)
1681 "Re-populate the `tabulated-list-entries'.
1682 PACKAGES should be nil or t, which means to display all known packages.
1683 KEYWORDS should be nil or a list of keywords."
1684 ;; Construct list of (PKG-DESC . STATUS).
1685 (unless packages (setq packages t))
1686 (let (info-list name)
1687 ;; Installed packages:
1688 (dolist (elt package-alist)
1689 (setq name (car elt))
1690 (when (or (eq packages t) (memq name packages))
1691 (dolist (pkg (cdr elt))
1692 (when (package--has-keyword-p pkg keywords)
1693 (package--push pkg (package-desc-status pkg) info-list)))))
1695 ;; Built-in packages:
1696 (dolist (elt package--builtins)
1697 (setq name (car elt))
1698 (when (and (not (eq name 'emacs)) ; Hide the `emacs' package.
1699 (package--has-keyword-p (package--from-builtin elt) keywords)
1700 (or package-list-unversioned
1701 (package--bi-desc-version (cdr elt)))
1702 (or (eq packages t) (memq name packages)))
1703 (package--push (package--from-builtin elt) "built-in" info-list)))
1705 ;; Available and disabled packages:
1706 (dolist (elt package-archive-contents)
1707 (setq name (car elt))
1708 (when (or (eq packages t) (memq name packages))
1709 (dolist (pkg (cdr elt))
1710 ;; Hide obsolete packages.
1711 (when (and (not (package-installed-p (package-desc-name pkg)
1712 (package-desc-version pkg)))
1713 (package--has-keyword-p pkg keywords))
1714 (package--push pkg (package-desc-status pkg) info-list)))))
1716 ;; Print the result.
1717 (setq tabulated-list-entries
1718 (mapcar #'package-menu--print-info info-list))))
1720 (defun package-all-keywords ()
1721 "Collect all package keywords"
1722 (let (keywords)
1723 (package--mapc (lambda (desc)
1724 (let* ((desc-keywords (and desc (package-desc--keywords desc))))
1725 (setq keywords (append keywords desc-keywords)))))
1726 keywords))
1728 (defun package--mapc (function &optional packages)
1729 "Call FUNCTION for all known PACKAGES.
1730 PACKAGES can be nil or t, which means to display all known
1731 packages, or a list of packages.
1733 Built-in packages are converted with `package--from-builtin'."
1734 (unless packages (setq packages t))
1735 (let (name)
1736 ;; Installed packages:
1737 (dolist (elt package-alist)
1738 (setq name (car elt))
1739 (when (or (eq packages t) (memq name packages))
1740 (mapc function (cdr elt))))
1742 ;; Built-in packages:
1743 (dolist (elt package--builtins)
1744 (setq name (car elt))
1745 (when (and (not (eq name 'emacs)) ; Hide the `emacs' package.
1746 (or package-list-unversioned
1747 (package--bi-desc-version (cdr elt)))
1748 (or (eq packages t) (memq name packages)))
1749 (funcall function (package--from-builtin elt))))
1751 ;; Available and disabled packages:
1752 (dolist (elt package-archive-contents)
1753 (setq name (car elt))
1754 (when (or (eq packages t) (memq name packages))
1755 (dolist (pkg (cdr elt))
1756 ;; Hide obsolete packages.
1757 (unless (package-installed-p (package-desc-name pkg)
1758 (package-desc-version pkg))
1759 (funcall function pkg)))))))
1761 (defun package--has-keyword-p (desc &optional keywords)
1762 "Test if package DESC has any of the given KEYWORDS.
1763 When none are given, the package matches."
1764 (if keywords
1765 (let* ((desc-keywords (and desc (package-desc--keywords desc)))
1766 found)
1767 (dolist (k keywords)
1768 (when (and (not found)
1769 (member k desc-keywords))
1770 (setq found t)))
1771 found)
1774 (defun package-menu--generate (remember-pos packages &optional keywords)
1775 "Populate the Package Menu.
1776 If REMEMBER-POS is non-nil, keep point on the same entry.
1777 PACKAGES should be t, which means to display all known packages,
1778 or a list of package names (symbols) to display.
1780 With KEYWORDS given, only packages with those keywords are
1781 shown."
1782 (package-menu--refresh packages keywords)
1783 (setf (car (aref tabulated-list-format 0))
1784 (if keywords
1785 (let ((filters (mapconcat 'identity keywords ",")))
1786 (concat "Package[" filters "]"))
1787 "Package"))
1788 (if keywords
1789 (define-key package-menu-mode-map "q" 'package-show-package-list)
1790 (define-key package-menu-mode-map "q" 'quit-window))
1791 (tabulated-list-init-header)
1792 (tabulated-list-print remember-pos))
1794 (defun package-menu--print-info (pkg)
1795 "Return a package entry suitable for `tabulated-list-entries'.
1796 PKG has the form (PKG-DESC . STATUS).
1797 Return (PKG-DESC [NAME VERSION STATUS DOC])."
1798 (let* ((pkg-desc (car pkg))
1799 (status (cdr pkg))
1800 (face (pcase status
1801 (`"built-in" 'font-lock-builtin-face)
1802 (`"available" 'default)
1803 (`"new" 'bold)
1804 (`"held" 'font-lock-constant-face)
1805 (`"disabled" 'font-lock-warning-face)
1806 (`"installed" 'font-lock-comment-face)
1807 (`"unsigned" 'font-lock-warning-face)
1808 (_ 'font-lock-warning-face)))) ; obsolete.
1809 (list pkg-desc
1810 `[,(list (symbol-name (package-desc-name pkg-desc))
1811 'face 'link
1812 'follow-link t
1813 'package-desc pkg-desc
1814 'action 'package-menu-describe-package)
1815 ,(propertize (package-version-join
1816 (package-desc-version pkg-desc))
1817 'font-lock-face face)
1818 ,(propertize status 'font-lock-face face)
1819 ,@(if (cdr package-archives)
1820 (list (propertize (or (package-desc-archive pkg-desc) "")
1821 'font-lock-face face)))
1822 ,(propertize (package-desc-summary pkg-desc)
1823 'font-lock-face face)])))
1825 (defun package-menu-refresh ()
1826 "Download the Emacs Lisp package archive.
1827 This fetches the contents of each archive specified in
1828 `package-archives', and then refreshes the package menu."
1829 (interactive)
1830 (unless (derived-mode-p 'package-menu-mode)
1831 (user-error "The current buffer is not a Package Menu"))
1832 (package-refresh-contents)
1833 (package-menu--generate t t))
1835 (defun package-menu-describe-package (&optional button)
1836 "Describe the current package.
1837 If optional arg BUTTON is non-nil, describe its associated package."
1838 (interactive)
1839 (let ((pkg-desc (if button (button-get button 'package-desc)
1840 (tabulated-list-get-id))))
1841 (if pkg-desc
1842 (describe-package pkg-desc)
1843 (user-error "No package here"))))
1845 ;; fixme numeric argument
1846 (defun package-menu-mark-delete (&optional _num)
1847 "Mark a package for deletion and move to the next line."
1848 (interactive "p")
1849 (if (member (package-menu-get-status) '("installed" "obsolete" "unsigned"))
1850 (tabulated-list-put-tag "D" t)
1851 (forward-line)))
1853 (defun package-menu-mark-install (&optional _num)
1854 "Mark a package for installation and move to the next line."
1855 (interactive "p")
1856 (if (member (package-menu-get-status) '("available" "new"))
1857 (tabulated-list-put-tag "I" t)
1858 (forward-line)))
1860 (defun package-menu-mark-unmark (&optional _num)
1861 "Clear any marks on a package and move to the next line."
1862 (interactive "p")
1863 (tabulated-list-put-tag " " t))
1865 (defun package-menu-backup-unmark ()
1866 "Back up one line and clear any marks on that package."
1867 (interactive)
1868 (forward-line -1)
1869 (tabulated-list-put-tag " "))
1871 (defun package-menu-mark-obsolete-for-deletion ()
1872 "Mark all obsolete packages for deletion."
1873 (interactive)
1874 (save-excursion
1875 (goto-char (point-min))
1876 (while (not (eobp))
1877 (if (equal (package-menu-get-status) "obsolete")
1878 (tabulated-list-put-tag "D" t)
1879 (forward-line 1)))))
1881 (defun package-menu-quick-help ()
1882 "Show short key binding help for package-menu-mode."
1883 (interactive)
1884 (message "n-ext, i-nstall, d-elete, u-nmark, x-ecute, r-efresh, h-elp"))
1886 (define-obsolete-function-alias
1887 'package-menu-view-commentary 'package-menu-describe-package "24.1")
1889 (defun package-menu-get-status ()
1890 (let* ((id (tabulated-list-get-id))
1891 (entry (and id (assq id tabulated-list-entries))))
1892 (if entry
1893 (aref (cadr entry) 2)
1894 "")))
1896 (defun package-menu--find-upgrades ()
1897 (let (installed available upgrades)
1898 ;; Build list of installed/available packages in this buffer.
1899 (dolist (entry tabulated-list-entries)
1900 ;; ENTRY is (PKG-DESC [NAME VERSION STATUS DOC])
1901 (let ((pkg-desc (car entry))
1902 (status (aref (cadr entry) 2)))
1903 (cond ((member status '("installed" "unsigned"))
1904 (push pkg-desc installed))
1905 ((member status '("available" "new"))
1906 (push (cons (package-desc-name pkg-desc) pkg-desc)
1907 available)))))
1908 ;; Loop through list of installed packages, finding upgrades.
1909 (dolist (pkg-desc installed)
1910 (let ((avail-pkg (assq (package-desc-name pkg-desc) available)))
1911 (and avail-pkg
1912 (version-list-< (package-desc-version pkg-desc)
1913 (package-desc-version (cdr avail-pkg)))
1914 (push avail-pkg upgrades))))
1915 upgrades))
1917 (defun package-menu-mark-upgrades ()
1918 "Mark all upgradable packages in the Package Menu.
1919 For each installed package with a newer version available, place
1920 an (I)nstall flag on the available version and a (D)elete flag on
1921 the installed version. A subsequent \\[package-menu-execute]
1922 call will upgrade the package."
1923 (interactive)
1924 (unless (derived-mode-p 'package-menu-mode)
1925 (error "The current buffer is not a Package Menu"))
1926 (let ((upgrades (package-menu--find-upgrades)))
1927 (if (null upgrades)
1928 (message "No packages to upgrade.")
1929 (widen)
1930 (save-excursion
1931 (goto-char (point-min))
1932 (while (not (eobp))
1933 (let* ((pkg-desc (tabulated-list-get-id))
1934 (upgrade (cdr (assq (package-desc-name pkg-desc) upgrades))))
1935 (cond ((null upgrade)
1936 (forward-line 1))
1937 ((equal pkg-desc upgrade)
1938 (package-menu-mark-install))
1940 (package-menu-mark-delete))))))
1941 (message "%d package%s marked for upgrading."
1942 (length upgrades)
1943 (if (= (length upgrades) 1) "" "s")))))
1945 (defun package-menu-execute (&optional noquery)
1946 "Perform marked Package Menu actions.
1947 Packages marked for installation are downloaded and installed;
1948 packages marked for deletion are removed.
1949 Optional argument NOQUERY non-nil means do not ask the user to confirm."
1950 (interactive)
1951 (unless (derived-mode-p 'package-menu-mode)
1952 (error "The current buffer is not in Package Menu mode"))
1953 (let (install-list delete-list cmd pkg-desc)
1954 (save-excursion
1955 (goto-char (point-min))
1956 (while (not (eobp))
1957 (setq cmd (char-after))
1958 (unless (eq cmd ?\s)
1959 ;; This is the key PKG-DESC.
1960 (setq pkg-desc (tabulated-list-get-id))
1961 (cond ((eq cmd ?D)
1962 (push pkg-desc delete-list))
1963 ((eq cmd ?I)
1964 (push pkg-desc install-list))))
1965 (forward-line)))
1966 (when install-list
1967 (if (or
1968 noquery
1969 (yes-or-no-p
1970 (if (= (length install-list) 1)
1971 (format "Install package `%s'? "
1972 (package-desc-full-name (car install-list)))
1973 (format "Install these %d packages (%s)? "
1974 (length install-list)
1975 (mapconcat #'package-desc-full-name
1976 install-list ", ")))))
1977 (mapc 'package-install install-list)))
1978 ;; Delete packages, prompting if necessary.
1979 (when delete-list
1980 (if (or
1981 noquery
1982 (yes-or-no-p
1983 (if (= (length delete-list) 1)
1984 (format "Delete package `%s'? "
1985 (package-desc-full-name (car delete-list)))
1986 (format "Delete these %d packages (%s)? "
1987 (length delete-list)
1988 (mapconcat #'package-desc-full-name
1989 delete-list ", ")))))
1990 (dolist (elt delete-list)
1991 (condition-case-unless-debug err
1992 (package-delete elt)
1993 (error (message (cadr err)))))
1994 (error "Aborted")))
1995 (if (or delete-list install-list)
1996 (package-menu--generate t t)
1997 (message "No operations specified."))))
1999 (defun package-menu--version-predicate (A B)
2000 (let ((vA (or (aref (cadr A) 1) '(0)))
2001 (vB (or (aref (cadr B) 1) '(0))))
2002 (if (version-list-= vA vB)
2003 (package-menu--name-predicate A B)
2004 (version-list-< vA vB))))
2006 (defun package-menu--status-predicate (A B)
2007 (let ((sA (aref (cadr A) 2))
2008 (sB (aref (cadr B) 2)))
2009 (cond ((string= sA sB)
2010 (package-menu--name-predicate A B))
2011 ((string= sA "new") t)
2012 ((string= sB "new") nil)
2013 ((string= sA "available") t)
2014 ((string= sB "available") nil)
2015 ((string= sA "installed") t)
2016 ((string= sB "installed") nil)
2017 ((string= sA "unsigned") t)
2018 ((string= sB "unsigned") nil)
2019 ((string= sA "held") t)
2020 ((string= sB "held") nil)
2021 ((string= sA "built-in") t)
2022 ((string= sB "built-in") nil)
2023 ((string= sA "obsolete") t)
2024 ((string= sB "obsolete") nil)
2025 (t (string< sA sB)))))
2027 (defun package-menu--description-predicate (A B)
2028 (let ((dA (aref (cadr A) 3))
2029 (dB (aref (cadr B) 3)))
2030 (if (string= dA dB)
2031 (package-menu--name-predicate A B)
2032 (string< dA dB))))
2034 (defun package-menu--name-predicate (A B)
2035 (string< (symbol-name (package-desc-name (car A)))
2036 (symbol-name (package-desc-name (car B)))))
2038 (defun package-menu--archive-predicate (A B)
2039 (string< (or (package-desc-archive (car A)) "")
2040 (or (package-desc-archive (car B)) "")))
2042 ;;;###autoload
2043 (defun list-packages (&optional no-fetch)
2044 "Display a list of packages.
2045 This first fetches the updated list of packages before
2046 displaying, unless a prefix argument NO-FETCH is specified.
2047 The list is displayed in a buffer named `*Packages*'."
2048 (interactive "P")
2049 (require 'finder-inf nil t)
2050 ;; Initialize the package system if necessary.
2051 (unless package--initialized
2052 (package-initialize t))
2053 (let (old-archives new-packages)
2054 (unless no-fetch
2055 ;; Read the locally-cached archive-contents.
2056 (package-read-all-archive-contents)
2057 (setq old-archives package-archive-contents)
2058 ;; Fetch the remote list of packages.
2059 (package-refresh-contents)
2060 ;; Find which packages are new.
2061 (dolist (elt package-archive-contents)
2062 (unless (assq (car elt) old-archives)
2063 (push (car elt) new-packages))))
2065 ;; Generate the Package Menu.
2066 (let ((buf (get-buffer-create "*Packages*")))
2067 (with-current-buffer buf
2068 (package-menu-mode)
2069 (set (make-local-variable 'package-menu--new-package-list)
2070 new-packages)
2071 (package-menu--generate nil t))
2072 ;; The package menu buffer has keybindings. If the user types
2073 ;; `M-x list-packages', that suggests it should become current.
2074 (switch-to-buffer buf))
2076 (let ((upgrades (package-menu--find-upgrades)))
2077 (if upgrades
2078 (message "%d package%s can be upgraded; type `%s' to mark %s for upgrading."
2079 (length upgrades)
2080 (if (= (length upgrades) 1) "" "s")
2081 (substitute-command-keys "\\[package-menu-mark-upgrades]")
2082 (if (= (length upgrades) 1) "it" "them"))))))
2084 ;;;###autoload
2085 (defalias 'package-list-packages 'list-packages)
2087 ;; Used in finder.el
2088 (defun package-show-package-list (&optional packages keywords)
2089 "Display PACKAGES in a *Packages* buffer.
2090 This is similar to `list-packages', but it does not fetch the
2091 updated list of packages, and it only displays packages with
2092 names in PACKAGES (which should be a list of symbols).
2094 When KEYWORDS are given, only packages with those KEYWORDS are
2095 shown."
2096 (interactive)
2097 (require 'finder-inf nil t)
2098 (let* ((buf (get-buffer-create "*Packages*"))
2099 (win (get-buffer-window buf)))
2100 (with-current-buffer buf
2101 (package-menu-mode)
2102 (package-menu--generate nil packages keywords))
2103 (if win
2104 (select-window win)
2105 (switch-to-buffer buf))))
2107 ;; package-menu--generate rebinds "q" on the fly, so we have to
2108 ;; hard-code the binding in the doc-string here.
2109 (defun package-menu-filter (keyword)
2110 "Filter the *Packages* buffer.
2111 Show only those items that relate to the specified KEYWORD.
2112 To restore the full package list, type `q'."
2113 (interactive (list (completing-read "Keyword: " (package-all-keywords))))
2114 (package-show-package-list t (list keyword)))
2116 (defun package-list-packages-no-fetch ()
2117 "Display a list of packages.
2118 Does not fetch the updated list of packages before displaying.
2119 The list is displayed in a buffer named `*Packages*'."
2120 (interactive)
2121 (list-packages t))
2123 (provide 'package)
2125 ;;; package.el ends here