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