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