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