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