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