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