Merge from emacs-24; up to 2014-05-29T17:16:00Z!dmantipov@yandex.ru
[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
ab84733c 294 :version "24.4")
acbadd00
DU
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
ab84733c 301 :version "24.4")
acbadd00 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 649 (generated-autoload-file (expand-file-name auto-name pkg-dir))
c63982dd 650 (backup-inhibited t)
44198b6e 651 (version-control 'never))
4fac34ce 652 (package-autoload-ensure-default-file generated-autoload-file)
cd44022c
DH
653 (update-directory-autoloads pkg-dir)
654 (let ((buf (find-buffer-visiting generated-autoload-file)))
f56be016
SM
655 (when buf (kill-buffer buf)))
656 auto-name))
44198b6e 657
4525ce3e
CY
658(defvar tar-parse-info)
659(declare-function tar-untar-buffer "tar-mode" ())
f20def1f
GM
660(declare-function tar-header-name "tar-mode" (tar-header) t)
661(declare-function tar-header-link-type "tar-mode" (tar-header) t)
4525ce3e
CY
662
663(defun package-untar-buffer (dir)
44198b6e 664 "Untar the current buffer.
4525ce3e
CY
665This uses `tar-untar-buffer' from Tar mode. All files should
666untar into a directory named DIR; otherwise, signal an error."
44198b6e 667 (require 'tar-mode)
4525ce3e
CY
668 (tar-mode)
669 ;; Make sure everything extracts into DIR.
3c94d7a6
CY
670 (let ((regexp (concat "\\`" (regexp-quote (expand-file-name dir)) "/"))
671 (case-fold-search (memq system-type '(windows-nt ms-dos cygwin))))
4525ce3e 672 (dolist (tar-data tar-parse-info)
3c94d7a6
CY
673 (let ((name (expand-file-name (tar-header-name tar-data))))
674 (or (string-match regexp name)
675 ;; Tarballs created by some utilities don't list
676 ;; directories with a trailing slash (Bug#13136).
677 (and (string-equal dir name)
678 (eq (tar-header-link-type tar-data) 5))
679 (error "Package does not untar cleanly into directory %s/" dir)))))
4525ce3e 680 (tar-untar-buffer))
44198b6e 681
fd846ab4
SM
682(defun package-generate-description-file (pkg-desc pkg-dir)
683 "Create the foo-pkg.el file for single-file packages."
684 (let* ((name (package-desc-name pkg-desc))
685 (pkg-file (expand-file-name (package--description-file pkg-dir)
686 pkg-dir)))
687 (let ((print-level nil)
688 (print-quoted t)
689 (print-length nil))
690 (write-region
691 (concat
770a6a91 692 ";;; -*- no-byte-compile: t -*-\n"
fd846ab4 693 (prin1-to-string
056453c6
DG
694 (nconc
695 (list 'define-package
696 (symbol-name name)
697 (package-version-join (package-desc-version pkg-desc))
698 (package-desc-summary pkg-desc)
699 (let ((requires (package-desc-reqs pkg-desc)))
700 (list 'quote
701 ;; Turn version lists into string form.
702 (mapcar
703 (lambda (elt)
704 (list (car elt)
705 (package-version-join (cadr elt))))
706 requires))))
e50a0b6e
TTN
707 (let ((alist (package-desc-extras pkg-desc))
708 flat)
709 (while alist
710 (let* ((pair (pop alist))
711 (key (car pair))
712 (val (cdr pair)))
713 ;; Don't bother ‘quote’ing ‘key’; it is always a keyword.
714 (push key flat)
715 (push (if (and (not (consp val))
716 (or (keywordp val)
717 (not (symbolp val))
718 (memq val '(nil t))))
719 val
720 `',val)
721 flat)))
722 (nreverse flat))))
fd846ab4 723 "\n")
29afcbab 724 nil pkg-file nil 'silent))))
fd846ab4
SM
725
726(defun package-unpack (pkg-desc)
727 "Install the contents of the current buffer as a package."
728 (let* ((name (package-desc-name pkg-desc))
729 (dirname (package-desc-full-name pkg-desc))
4525ce3e 730 (pkg-dir (expand-file-name dirname package-user-dir)))
fd846ab4
SM
731 (pcase (package-desc-kind pkg-desc)
732 (`tar
733 (make-directory package-user-dir t)
734 ;; FIXME: should we delete PKG-DIR if it exists?
735 (let* ((default-directory (file-name-as-directory package-user-dir)))
736 (package-untar-buffer dirname)))
737 (`single
738 (let ((el-file (expand-file-name (format "%s.el" name) pkg-dir)))
739 (make-directory pkg-dir t)
740 (package--write-file-no-coding el-file)))
741 (kind (error "Unknown package kind: %S" kind)))
742 (package--make-autoloads-and-stuff pkg-desc pkg-dir)
743 ;; Update package-alist.
744 (let ((new-desc (package-load-descriptor pkg-dir)))
745 ;; FIXME: Check that `new-desc' matches `desc'!
746 ;; FIXME: Compilation should be done as a separate, optional, step.
747 ;; E.g. for multi-package installs, we should first install all packages
748 ;; and then compile them.
749 (package--compile new-desc))
750 ;; Try to activate it.
a7d2d465 751 (package-activate name 'force)
fd846ab4
SM
752 pkg-dir))
753
754(defun package--make-autoloads-and-stuff (pkg-desc pkg-dir)
755 "Generate autoloads, description file, etc.. for PKG-DESC installed at PKG-DIR."
756 (package-generate-autoloads (package-desc-name pkg-desc) pkg-dir)
757 (let ((desc-file (package--description-file pkg-dir)))
758 (unless (file-exists-p desc-file)
759 (package-generate-description-file pkg-desc pkg-dir)))
760 ;; FIXME: Create foo.info and dir file from foo.texi?
761 )
762
763(defun package--compile (pkg-desc)
764 "Byte-compile installed package PKG-DESC."
765 (package-activate-1 pkg-desc)
766 (byte-recompile-directory (package-desc-dir pkg-desc) 0 t))
44198b6e 767
c6affbde 768(defun package--write-file-no-coding (file-name)
bc44bef7 769 (let ((buffer-file-coding-system 'no-conversion))
29afcbab 770 (write-region (point-min) (point-max) file-name nil 'silent)))
bc44bef7 771
f561e49a
CY
772(defmacro package--with-work-buffer (location file &rest body)
773 "Run BODY in a buffer containing the contents of FILE at LOCATION.
774LOCATION is the base location of a package archive, and should be
775one of the URLs (or file names) specified in `package-archives'.
776FILE is the name of a file relative to that base location.
777
778This macro retrieves FILE from LOCATION into a temporary buffer,
779and evaluates BODY while that buffer is current. This work
780buffer is killed afterwards. Return the last value in BODY."
fd846ab4 781 (declare (indent 2) (debug t))
196716cf
JB
782 `(with-temp-buffer
783 (if (string-match-p "\\`https?:" ,location)
784 (url-insert-file-contents (concat ,location ,file))
785 (unless (file-name-absolute-p ,location)
786 (error "Archive location %s is not an absolute file name"
787 ,location))
788 (insert-file-contents (expand-file-name ,file ,location)))
789 ,@body))
44198b6e 790
acbadd00
DU
791(defun package--archive-file-exists-p (location file)
792 (let ((http (string-match "\\`https?:" location)))
793 (if http
794 (progn
795 (require 'url-http)
796 (url-http-file-exists-p (concat location file)))
797 (file-exists-p (expand-file-name file location)))))
798
799(declare-function epg-make-context "epg"
800 (&optional protocol armor textmode include-certs
801 cipher-algorithm
802 digest-algorithm
803 compress-algorithm))
804(declare-function epg-context-set-home-directory "epg" (context directory))
805(declare-function epg-verify-string "epg" (context signature
806 &optional signed-text))
807(declare-function epg-context-result-for "epg" (context name))
808(declare-function epg-signature-status "epg" (signature))
809(declare-function epg-signature-to-string "epg" (signature))
810
811(defun package--check-signature (location file)
812 "Check signature of the current buffer.
813GnuPG keyring is located under \"gnupg\" in `package-user-dir'."
ec6c7de2
SM
814 (let* ((context (epg-make-context 'OpenPGP))
815 (homedir (expand-file-name "gnupg" package-user-dir))
816 (sig-file (concat file ".sig"))
817 (sig-content (package--with-work-buffer location sig-file
818 (buffer-string))))
acbadd00
DU
819 (epg-context-set-home-directory context homedir)
820 (epg-verify-string context sig-content (buffer-string))
821 ;; The .sig file may contain multiple signatures. Success if one
822 ;; of the signatures is good.
ec6c7de2
SM
823 (let ((good-signatures
824 (delq nil (mapcar (lambda (sig)
825 (if (eq (epg-signature-status sig) 'good)
826 sig))
827 (epg-context-result-for context 'verify)))))
828 (if (null good-signatures)
829 (error "Failed to verify signature %s: %S"
830 sig-file
831 (mapcar #'epg-signature-to-string
832 (epg-context-result-for context 'verify)))
833 good-signatures))))
acbadd00 834
fd846ab4 835(defun package-install-from-archive (pkg-desc)
44198b6e 836 "Download and install a tar package."
acbadd00
DU
837 (let* ((location (package-archive-base pkg-desc))
838 (file (concat (package-desc-full-name pkg-desc)
839 (package-desc-suffix pkg-desc)))
840 (sig-file (concat file ".sig"))
841 good-signatures pkg-descs)
f561e49a 842 (package--with-work-buffer location file
acbadd00
DU
843 (if (and package-check-signature
844 (not (member (package-desc-archive pkg-desc)
845 package-unsigned-archives)))
846 (if (package--archive-file-exists-p location sig-file)
847 (setq good-signatures (package--check-signature location file))
848 (unless (eq package-check-signature 'allow-unsigned)
849 (error "Unsigned package: `%s'"
850 (package-desc-name pkg-desc)))))
851 (package-unpack pkg-desc))
852 ;; Here the package has been installed successfully, mark it as
853 ;; signed if appropriate.
854 (when good-signatures
855 ;; Write out good signatures into NAME-VERSION.signed file.
856 (write-region (mapconcat #'epg-signature-to-string good-signatures "\n")
857 nil
858 (expand-file-name
859 (concat (package-desc-full-name pkg-desc)
860 ".signed")
29afcbab
SM
861 package-user-dir)
862 nil 'silent)
acbadd00
DU
863 ;; Update the old pkg-desc which will be shown on the description buffer.
864 (setf (package-desc-signed pkg-desc) t)
865 ;; Update the new (activated) pkg-desc as well.
866 (setq pkg-descs (cdr (assq (package-desc-name pkg-desc) package-alist)))
867 (if pkg-descs
868 (setf (package-desc-signed (car pkg-descs)) t)))))
44198b6e 869
1ead7dbd
SM
870(defvar package--initialized nil)
871
bc44bef7 872(defun package-installed-p (package &optional min-version)
7ede3b65
CY
873 "Return true if PACKAGE, of MIN-VERSION or newer, is installed.
874MIN-VERSION should be a version list."
a7723be6 875 (unless package--initialized (error "package.el is not yet initialized!"))
2167f25d
BG
876 (or
877 (let ((pkg-descs (cdr (assq package package-alist))))
878 (and pkg-descs
879 (version-list-<= min-version
880 (package-desc-version (car pkg-descs)))))
881 ;; Also check built-in packages.
882 (package-built-in-p package min-version)))
44198b6e 883
5e87fcb1 884(defun package-compute-transaction (packages requirements &optional seen)
12059709
SM
885 "Return a list of packages to be installed, including PACKAGES.
886PACKAGES should be a list of `package-desc'.
96ae4c8f
CY
887
888REQUIREMENTS should be a list of additional requirements; each
7ede3b65
CY
889element in this list should have the form (PACKAGE VERSION-LIST),
890where PACKAGE is a package name and VERSION-LIST is the required
891version of that package.
96ae4c8f
CY
892
893This function recursively computes the requirements of the
894packages in REQUIREMENTS, and returns a list of all the packages
895that must be installed. Packages that are already installed are
5e87fcb1
SM
896not included in this list.
897
898SEEN is used internally to detect infinite recursion."
12059709
SM
899 ;; FIXME: We really should use backtracking to explore the whole
900 ;; search space (e.g. if foo require bar-1.3, and bar-1.4 requires toto-1.1
901 ;; whereas bar-1.3 requires toto-1.0 and the user has put a hold on toto-1.0:
902 ;; the current code might fail to see that it could install foo by using the
903 ;; older bar-1.3).
44198b6e
CY
904 (dolist (elt requirements)
905 (let* ((next-pkg (car elt))
12059709
SM
906 (next-version (cadr elt))
907 (already ()))
908 (dolist (pkg packages)
909 (if (eq next-pkg (package-desc-name pkg))
910 (setq already pkg)))
5e87fcb1 911 (when already
dcf78667 912 (if (version-list-<= next-version (package-desc-version already))
5e87fcb1
SM
913 ;; `next-pkg' is already in `packages', but its position there
914 ;; means it might be installed too late: remove it from there, so
915 ;; we re-add it (along with its dependencies) at an earlier place
916 ;; below (bug#16994).
917 (if (memq already seen) ;Avoid inf-loop on dependency cycles.
918 (message "Dependency cycle going through %S"
919 (package-desc-full-name already))
920 (setq packages (delq already packages))
921 (setq already nil))
dcf78667 922 (error "Need package `%s-%s', but only %s is being installed"
12059709
SM
923 next-pkg (package-version-join next-version)
924 (package-version-join (package-desc-version already)))))
5e87fcb1
SM
925 (cond
926 (already nil)
12059709
SM
927 ((package-installed-p next-pkg next-version) nil)
928
929 (t
44198b6e
CY
930 ;; A package is required, but not installed. It might also be
931 ;; blocked via `package-load-list'.
12059709
SM
932 (let ((pkg-descs (cdr (assq next-pkg package-archive-contents)))
933 (found nil)
934 (problem nil))
935 (while (and pkg-descs (not found))
936 (let* ((pkg-desc (pop pkg-descs))
937 (version (package-desc-version pkg-desc))
938 (disabled (package-disabled-p next-pkg version)))
939 (cond
940 ((version-list-< version next-version)
941 (error
942 "Need package `%s-%s', but only %s is available"
943 next-pkg (package-version-join next-version)
944 (package-version-join version)))
945 (disabled
946 (unless problem
947 (setq problem
948 (if (stringp disabled)
949 (format "Package `%s' held at version %s, \
44198b6e 950but version %s required"
12059709
SM
951 next-pkg disabled
952 (package-version-join next-version))
953 (format "Required package '%s' is disabled"
954 next-pkg)))))
955 (t (setq found pkg-desc)))))
956 (unless found
957 (if problem
5e87fcb1 958 (error "%s" problem)
12059709
SM
959 (error "Package `%s-%s' is unavailable"
960 next-pkg (package-version-join next-version))))
961 (setq packages
962 (package-compute-transaction (cons found packages)
5e87fcb1
SM
963 (package-desc-reqs found)
964 (cons found seen))))))))
12059709 965 packages)
44198b6e
CY
966
967(defun package-read-from-string (str)
968 "Read a Lisp expression from STR.
969Signal an error if the entire string was not used."
970 (let* ((read-data (read-from-string str))
187d3296
CY
971 (more-left
972 (condition-case nil
973 ;; The call to `ignore' suppresses a compiler warning.
974 (progn (ignore (read-from-string
975 (substring str (cdr read-data))))
976 t)
977 (end-of-file nil))))
44198b6e
CY
978 (if more-left
979 (error "Can't read whole string")
980 (car read-data))))
981
982(defun package--read-archive-file (file)
983 "Re-read archive file FILE, if it exists.
984Will return the data from the file, or nil if the file does not exist.
985Will throw an error if the archive version is too new."
986 (let ((filename (expand-file-name file package-user-dir)))
187d3296
CY
987 (when (file-exists-p filename)
988 (with-temp-buffer
989 (insert-file-contents-literally filename)
990 (let ((contents (read (current-buffer))))
991 (if (> (car contents) package-archive-version)
992 (error "Package archive version %d is higher than %d"
993 (car contents) package-archive-version))
994 (cdr contents))))))
44198b6e 995
bc44bef7 996(defun package-read-all-archive-contents ()
96ae4c8f
CY
997 "Re-read `archive-contents', if it exists.
998If successful, set `package-archive-contents'."
fbe3be3f 999 (setq package-archive-contents nil)
bc44bef7 1000 (dolist (archive package-archives)
96ae4c8f 1001 (package-read-archive-contents (car archive))))
44198b6e 1002
bc44bef7 1003(defun package-read-archive-contents (archive)
187d3296
CY
1004 "Re-read archive contents for ARCHIVE.
1005If successful, set the variable `package-archive-contents'.
bc44bef7 1006If the archive version is too new, signal an error."
187d3296
CY
1007 ;; Version 1 of 'archive-contents' is identical to our internal
1008 ;; representation.
66bd25ab
SM
1009 (let* ((contents-file (format "archives/%s/archive-contents" archive))
1010 (contents (package--read-archive-file contents-file)))
1011 (when contents
187d3296
CY
1012 (dolist (package contents)
1013 (package--add-to-archive-contents package archive)))))
bc44bef7 1014
f56be016
SM
1015;; Package descriptor objects used inside the "archive-contents" file.
1016;; Changing this defstruct implies changing the format of the
1017;; "archive-contents" files.
1018(cl-defstruct (package--ac-desc
056453c6 1019 (:constructor package-make-ac-desc (version reqs summary kind extras))
f56be016
SM
1020 (:copier nil)
1021 (:type vector))
056453c6 1022 version reqs summary kind extras)
f56be016 1023
bc44bef7
PH
1024(defun package--add-to-archive-contents (package archive)
1025 "Add the PACKAGE from the given ARCHIVE if necessary.
f56be016
SM
1026PACKAGE should have the form (NAME . PACKAGE--AC-DESC).
1027Also, add the originating archive to the `package-desc' structure."
1028 (let* ((name (car package))
66bd25ab 1029 (version (package--ac-desc-version (cdr package)))
f56be016
SM
1030 (pkg-desc
1031 (package-desc-create
1032 :name name
66bd25ab 1033 :version version
f56be016
SM
1034 :reqs (package--ac-desc-reqs (cdr package))
1035 :summary (package--ac-desc-summary (cdr package))
1036 :kind (package--ac-desc-kind (cdr package))
056453c6
DG
1037 :archive archive
1038 :extras (and (> (length (cdr package)) 4)
1039 ;; Older archive-contents files have only 4
1040 ;; elements here.
1041 (package--ac-desc-extras (cdr package)))))
12059709 1042 (existing-packages (assq name package-archive-contents))
397703b4 1043 (pinned-to-archive (assoc name package-pinned-packages)))
66bd25ab 1044 (cond
3e2377ce
DG
1045 ;; Skip entirely if pinned to another archive.
1046 ((and pinned-to-archive
1047 (not (equal (cdr pinned-to-archive) archive)))
66bd25ab 1048 nil)
12059709
SM
1049 ((not existing-packages)
1050 (push (list name pkg-desc) package-archive-contents))
1051 (t
1052 (while
1053 (if (and (cdr existing-packages)
1054 (version-list-<
1055 version (package-desc-version (cadr existing-packages))))
1056 (setq existing-packages (cdr existing-packages))
a49ff80c
SM
1057 (push pkg-desc (cdr existing-packages))
1058 nil))))))
12059709
SM
1059
1060(defun package-download-transaction (packages)
1061 "Download and install all the packages in PACKAGES.
1062PACKAGES should be a list of package-desc.
96ae4c8f 1063This function assumes that all package requirements in
12059709 1064PACKAGES are satisfied, i.e. that PACKAGES is computed
96ae4c8f 1065using `package-compute-transaction'."
12059709 1066 (mapc #'package-install-from-archive packages))
44198b6e
CY
1067
1068;;;###autoload
5b165ade
SM
1069(defun package-install (pkg)
1070 "Install the package PKG.
1071PKG can be a package-desc or the package name of one the available packages
1072in an archive in `package-archives'. Interactively, prompt for its name."
44198b6e 1073 (interactive
7254299e
CY
1074 (progn
1075 ;; Initialize the package system to get the list of package
1076 ;; symbols for completion.
1077 (unless package--initialized
1078 (package-initialize t))
70550acf
PH
1079 (unless package-archive-contents
1080 (package-refresh-contents))
5b165ade 1081 (list (intern (completing-read
12059709 1082 "Install package: "
3e2377ce
DG
1083 (delq nil
1084 (mapcar (lambda (elt)
1085 (unless (package-installed-p (car elt))
1086 (symbol-name (car elt))))
1087 package-archive-contents))
5b165ade 1088 nil t)))))
1b8dff23 1089 (package-download-transaction
12059709
SM
1090 (if (package-desc-p pkg)
1091 (package-compute-transaction (list pkg)
1092 (package-desc-reqs pkg))
1093 (package-compute-transaction ()
1094 (list (list pkg))))))
44198b6e 1095
ffbf300e
CY
1096(defun package-strip-rcs-id (str)
1097 "Strip RCS version ID from the version string STR.
44198b6e
CY
1098If the result looks like a dotted numeric version, return it.
1099Otherwise return nil."
ffbf300e
CY
1100 (when str
1101 (when (string-match "\\`[ \t]*[$]Revision:[ \t]+" str)
1102 (setq str (substring str (match-end 0))))
1103 (condition-case nil
1104 (if (version-to-list str)
1105 str)
1106 (error nil))))
44198b6e 1107
f53d3815
GM
1108(declare-function lm-homepage "lisp-mnt" (&optional file))
1109
f77d7d17
TW
1110(defun package--prepare-dependencies (deps)
1111 "Turn DEPS into an acceptable list of dependencies.
1112
1113Any parts missing a version string get a default version string
1114of \"0\" (meaning any version) and an appropriate level of lists
1115is wrapped around any parts requiring it."
1116 (cond
1117 ((not (listp deps))
1118 (error "Invalid requirement specifier: %S" deps))
1119 (t (mapcar (lambda (dep)
1120 (cond
1121 ((symbolp dep) `(,dep "0"))
1122 ((stringp dep)
1123 (error "Invalid requirement specifier: %S" dep))
0d71dfb3
TW
1124 ((and (listp dep) (null (cdr dep)))
1125 (list (car dep) "0"))
f77d7d17
TW
1126 (t dep)))
1127 deps))))
1128
44198b6e 1129(defun package-buffer-info ()
f56be016 1130 "Return a `package-desc' describing the package in the current buffer.
187d3296
CY
1131
1132If the buffer does not contain a conforming package, signal an
1133error. If there is a package, narrow the buffer to the file's
1134boundaries."
44198b6e 1135 (goto-char (point-min))
f677562b 1136 (unless (re-search-forward "^;;; \\([^ ]*\\)\\.el ---[ \t]*\\(.*?\\)[ \t]*\\(-\\*-.*-\\*-[ \t]*\\)?$" nil t)
d96ad422 1137 (error "Package lacks a file header"))
187d3296
CY
1138 (let ((file-name (match-string-no-properties 1))
1139 (desc (match-string-no-properties 2))
1140 (start (line-beginning-position)))
1141 (unless (search-forward (concat ";;; " file-name ".el ends here"))
1142 (error "Package lacks a terminating comment"))
1143 ;; Try to include a trailing newline.
1144 (forward-line)
1145 (narrow-to-region start (point))
1146 (require 'lisp-mnt)
1147 ;; Use some headers we've invented to drive the process.
1148 (let* ((requires-str (lm-header "package-requires"))
187d3296
CY
1149 ;; Prefer Package-Version; if defined, the package author
1150 ;; probably wants us to use it. Otherwise try Version.
1151 (pkg-version
1152 (or (package-strip-rcs-id (lm-header "package-version"))
056453c6
DG
1153 (package-strip-rcs-id (lm-header "version"))))
1154 (homepage (lm-homepage)))
187d3296
CY
1155 (unless pkg-version
1156 (error
1157 "Package lacks a \"Version\" or \"Package-Version\" header"))
f56be016
SM
1158 (package-desc-from-define
1159 file-name pkg-version desc
f77d7d17
TW
1160 (if requires-str
1161 (package--prepare-dependencies
1162 (package-read-from-string requires-str)))
056453c6 1163 :kind 'single
494e898b 1164 :url homepage))))
44198b6e 1165
9e277302
JB
1166(declare-function tar-get-file-descriptor "tar-mode" (file))
1167(declare-function tar--extract "tar-mode" (descriptor))
1168
fd846ab4 1169(defun package-tar-file-info ()
44198b6e 1170 "Find package information for a tar file.
fd846ab4
SM
1171The return result is a `package-desc'."
1172 (cl-assert (derived-mode-p 'tar-mode))
1173 (let* ((dir-name (file-name-directory
1174 (tar-header-name (car tar-parse-info))))
1b8dff23 1175 (desc-file (package--description-file dir-name))
fd846ab4
SM
1176 (tar-desc (tar-get-file-descriptor (concat dir-name desc-file))))
1177 (unless tar-desc
1178 (error "No package descriptor file found"))
1179 (with-current-buffer (tar--extract tar-desc)
1180 (goto-char (point-min))
1181 (unwind-protect
1182 (let* ((pkg-def-parsed (read (current-buffer)))
1183 (pkg-desc
1184 (if (not (eq (car pkg-def-parsed) 'define-package))
1185 (error "Can't find define-package in %s"
1186 (tar-header-name tar-desc))
1187 (apply #'package-desc-from-define
1188 (append (cdr pkg-def-parsed))))))
1189 (setf (package-desc-kind pkg-desc) 'tar)
1190 pkg-desc)
1191 (kill-buffer (current-buffer))))))
f56be016 1192
44198b6e 1193
187d3296 1194;;;###autoload
fd846ab4 1195(defun package-install-from-buffer ()
187d3296 1196 "Install a package from the current buffer.
fd846ab4
SM
1197The current buffer is assumed to be a single .el or .tar file that follows the
1198packaging guidelines; see info node `(elisp)Packaging'.
1199Downloads and installs required packages as needed."
1200 (interactive)
1201 (let ((pkg-desc (if (derived-mode-p 'tar-mode)
1202 (package-tar-file-info)
1203 (package-buffer-info))))
1204 ;; Download and install the dependencies.
1205 (let* ((requires (package-desc-reqs pkg-desc))
1206 (transaction (package-compute-transaction nil requires)))
1207 (package-download-transaction transaction))
1208 ;; Install the package itself.
1209 (package-unpack pkg-desc)
1210 pkg-desc))
44198b6e 1211
44198b6e
CY
1212;;;###autoload
1213(defun package-install-file (file)
1214 "Install a package from a file.
1215The file can either be a tar file or an Emacs Lisp file."
1216 (interactive "fPackage file name: ")
1217 (with-temp-buffer
1218 (insert-file-contents-literally file)
fd846ab4
SM
1219 (when (string-match "\\.tar\\'" file) (tar-mode))
1220 (package-install-from-buffer)))
44198b6e 1221
1b8dff23
SM
1222(defun package-delete (pkg-desc)
1223 (let ((dir (package-desc-dir pkg-desc)))
12059709
SM
1224 (if (not (string-prefix-p (file-name-as-directory
1225 (expand-file-name package-user-dir))
1226 (expand-file-name dir)))
1227 ;; Don't delete "system" packages.
1228 (error "Package `%s' is a system package, not deleting"
1229 (package-desc-full-name pkg-desc))
1230 (delete-directory dir t t)
acbadd00
DU
1231 ;; Remove NAME-VERSION.signed file.
1232 (let ((signed-file (concat dir ".signed")))
1233 (if (file-exists-p signed-file)
1234 (delete-file signed-file)))
12059709 1235 ;; Update package-alist.
ec6c7de2
SM
1236 (let* ((name (package-desc-name pkg-desc))
1237 (pkgs (assq name package-alist)))
1238 (delete pkg-desc pkgs)
1239 (unless (cdr pkgs)
1240 (setq package-alist (delq pkgs package-alist))))
12059709 1241 (message "Package `%s' deleted." (package-desc-full-name pkg-desc)))))
44198b6e 1242
fd846ab4 1243(defun package-archive-base (desc)
bc44bef7 1244 "Return the archive containing the package NAME."
fd846ab4 1245 (cdr (assoc (package-desc-archive desc) package-archives)))
bc44bef7
PH
1246
1247(defun package--download-one-archive (archive file)
f561e49a
CY
1248 "Retrieve an archive file FILE from ARCHIVE, and cache it.
1249ARCHIVE should be a cons cell of the form (NAME . LOCATION),
1250similar to an entry in `package-alist'. Save the cached copy to
1251\"archives/NAME/archive-contents\" in `package-user-dir'."
acbadd00
DU
1252 (let ((dir (expand-file-name (format "archives/%s" (car archive))
1253 package-user-dir))
1254 (sig-file (concat file ".sig"))
1255 good-signatures)
f561e49a 1256 (package--with-work-buffer (cdr archive) file
acbadd00
DU
1257 ;; Check signature of archive-contents, if desired.
1258 (if (and package-check-signature
1259 (not (member archive package-unsigned-archives)))
1260 (if (package--archive-file-exists-p (cdr archive) sig-file)
1261 (setq good-signatures (package--check-signature (cdr archive)
1262 file))
1263 (unless (eq package-check-signature 'allow-unsigned)
1264 (error "Unsigned archive `%s'"
1265 (car archive)))))
ebf662f4
CY
1266 ;; Read the retrieved buffer to make sure it is valid (e.g. it
1267 ;; may fetch a URL redirect page).
196716cf 1268 (when (listp (read (current-buffer)))
ebf662f4 1269 (make-directory dir t)
b0df8b95 1270 (write-region nil nil (expand-file-name file dir) nil 'silent)))
acbadd00
DU
1271 (when good-signatures
1272 ;; Write out good signatures into archive-contents.signed file.
1273 (write-region (mapconcat #'epg-signature-to-string good-signatures "\n")
1274 nil
29afcbab
SM
1275 (expand-file-name (concat file ".signed") dir)
1276 nil 'silent))))
acbadd00
DU
1277
1278(declare-function epg-check-configuration "epg-config"
1279 (config &optional minimum-version))
1280(declare-function epg-configuration "epg-config" ())
1281(declare-function epg-import-keys-from-file "epg" (context keys))
1282
1283;;;###autoload
1284(defun package-import-keyring (&optional file)
1285 "Import keys from FILE."
1286 (interactive "fFile: ")
1287 (setq file (expand-file-name file))
1288 (let ((context (epg-make-context 'OpenPGP))
1289 (homedir (expand-file-name "gnupg" package-user-dir)))
1290 (make-directory homedir t)
1291 (epg-context-set-home-directory context homedir)
1292 (message "Importing %s..." (file-name-nondirectory file))
1293 (epg-import-keys-from-file context file)
1294 (message "Importing %s...done" (file-name-nondirectory file))))
44198b6e 1295
38bb2ca8 1296;;;###autoload
44198b6e
CY
1297(defun package-refresh-contents ()
1298 "Download the ELPA archive description if needed.
ebf662f4
CY
1299This informs Emacs about the latest versions of all packages, and
1300makes them available for download."
44198b6e 1301 (interactive)
5b165ade 1302 ;; FIXME: Do it asynchronously.
44198b6e
CY
1303 (unless (file-exists-p package-user-dir)
1304 (make-directory package-user-dir t))
acbadd00
DU
1305 (let ((default-keyring (expand-file-name "package-keyring.gpg"
1306 data-directory)))
1307 (if (file-exists-p default-keyring)
1308 (condition-case-unless-debug error
1309 (progn
1310 (epg-check-configuration (epg-configuration))
1311 (package-import-keyring default-keyring))
1312 (error (message "Cannot import default keyring: %S" (cdr error))))))
bc44bef7 1313 (dolist (archive package-archives)
1be3ca5a 1314 (condition-case-unless-debug nil
cb6c4991 1315 (package--download-one-archive archive "archive-contents")
187d3296 1316 (error (message "Failed to download `%s' archive."
cb6c4991 1317 (car archive)))))
bc44bef7 1318 (package-read-all-archive-contents))
44198b6e
CY
1319
1320;;;###autoload
4b99edf2 1321(defun package-initialize (&optional no-activate)
44198b6e 1322 "Load Emacs Lisp packages, and activate them.
4b99edf2
CY
1323The variable `package-load-list' controls which packages to load.
1324If optional arg NO-ACTIVATE is non-nil, don't activate packages."
44198b6e 1325 (interactive)
a7d2d465 1326 (setq package-alist nil)
44198b6e 1327 (package-load-all-descriptors)
bc44bef7 1328 (package-read-all-archive-contents)
4b99edf2
CY
1329 (unless no-activate
1330 (dolist (elt package-alist)
a7d2d465 1331 (package-activate (car elt))))
4b99edf2 1332 (setq package--initialized t))
44198b6e
CY
1333
1334\f
cced7584 1335;;;; Package description buffer.
44198b6e 1336
cced7584
CY
1337;;;###autoload
1338(defun describe-package (package)
1339 "Display the full documentation of PACKAGE (a symbol)."
1340 (interactive
12059709 1341 (let* ((guess (function-called-at-point)))
4b99edf2
CY
1342 (require 'finder-inf nil t)
1343 ;; Load the package list if necessary (but don't activate them).
1344 (unless package--initialized
1345 (package-initialize t))
12059709
SM
1346 (let ((packages (append (mapcar 'car package-alist)
1347 (mapcar 'car package-archive-contents)
1348 (mapcar 'car package--builtins))))
1349 (unless (memq guess packages)
1350 (setq guess nil))
1351 (setq packages (mapcar 'symbol-name packages))
1352 (let ((val
1353 (completing-read (if guess
1354 (format "Describe package (default %s): "
1355 guess)
1356 "Describe package: ")
1357 packages nil t nil nil guess)))
1358 (list (intern val))))))
1359 (if (not (or (package-desc-p package) (and package (symbolp package))))
4b99edf2 1360 (message "No package specified")
cced7584
CY
1361 (help-setup-xref (list #'describe-package package)
1362 (called-interactively-p 'interactive))
1363 (with-help-window (help-buffer)
1364 (with-current-buffer standard-output
1365 (describe-package-1 package)))))
1366
12059709 1367(defun describe-package-1 (pkg)
96ae4c8f 1368 (require 'lisp-mnt)
12059709
SM
1369 (let* ((desc (or
1370 (if (package-desc-p pkg) pkg)
1371 (cadr (assq pkg package-alist))
1372 (let ((built-in (assq pkg package--builtins)))
1373 (if built-in
1374 (package--from-builtin built-in)
1375 (cadr (assq pkg package-archive-contents))))))
1376 (name (if desc (package-desc-name desc) pkg))
1377 (pkg-dir (if desc (package-desc-dir desc)))
1378 (reqs (if desc (package-desc-reqs desc)))
1379 (version (if desc (package-desc-version desc)))
1380 (archive (if desc (package-desc-archive desc)))
a81fc7ba
TZ
1381 (extras (and desc (package-desc-extras desc)))
1382 (homepage (cdr (assoc :url extras)))
2dbf4ffd 1383 (keywords (if desc (package-desc--keywords desc)))
12059709
SM
1384 (built-in (eq pkg-dir 'builtin))
1385 (installable (and archive (not built-in)))
acbadd00
DU
1386 (status (if desc (package-desc-status desc) "orphan"))
1387 (signed (if desc (package-desc-signed desc))))
12059709 1388 (prin1 name)
cced7584 1389 (princ " is ")
12059709
SM
1390 (princ (if (memq (aref status 0) '(?a ?e ?i ?o ?u)) "an " "a "))
1391 (princ status)
1392 (princ " package.\n\n")
cb6c4991 1393
96ae4c8f 1394 (insert " " (propertize "Status" 'font-lock-face 'bold) ": ")
12059709
SM
1395 (cond (built-in
1396 (insert (propertize (capitalize status)
1397 'font-lock-face 'font-lock-builtin-face)
1398 "."))
1399 (pkg-dir
acbadd00
DU
1400 (insert (propertize (if (equal status "unsigned")
1401 "Installed"
1402 (capitalize status)) ;FIXME: Why comment-face?
96ae4c8f 1403 'font-lock-face 'font-lock-comment-face))
cb6c4991
CY
1404 (insert " in `")
1405 ;; Todo: Add button for uninstalling.
12059709
SM
1406 (help-insert-xref-button (abbreviate-file-name
1407 (file-name-as-directory pkg-dir))
cb6c4991 1408 'help-package-def pkg-dir)
12059709
SM
1409 (if (and (package-built-in-p name)
1410 (not (package-built-in-p name version)))
4b99edf2
CY
1411 (insert "',\n shadowing a "
1412 (propertize "built-in package"
acbadd00
DU
1413 'font-lock-face 'font-lock-builtin-face))
1414 (insert "'"))
1415 (if signed
1416 (insert ".")
1417 (insert " (unsigned).")))
cb6c4991 1418 (installable
12059709
SM
1419 (insert (capitalize status))
1420 (insert " from " (format "%s" archive))
55b056ba 1421 (insert " -- ")
a81fc7ba
TZ
1422 (package-make-button
1423 "Install"
1424 'action 'package-install-button-action
1425 'package-desc desc))
12059709 1426 (t (insert (capitalize status) ".")))
cb6c4991 1427 (insert "\n")
86fd16b6
TZ
1428 (insert " " (propertize "Archive" 'font-lock-face 'bold)
1429 ": " (or archive "n/a") "\n")
12059709 1430 (and version
96ae4c8f 1431 (insert " "
12059709
SM
1432 (propertize "Version" 'font-lock-face 'bold) ": "
1433 (package-version-join version) "\n"))
4b99edf2
CY
1434
1435 (setq reqs (if desc (package-desc-reqs desc)))
8adb4c33 1436 (when reqs
96ae4c8f 1437 (insert " " (propertize "Requires" 'font-lock-face 'bold) ": ")
8adb4c33
CY
1438 (let ((first t)
1439 name vers text)
1440 (dolist (req reqs)
1441 (setq name (car req)
1442 vers (cadr req)
1443 text (format "%s-%s" (symbol-name name)
1444 (package-version-join vers)))
1445 (cond (first (setq first nil))
1446 ((>= (+ 2 (current-column) (length text))
1447 (window-width))
1448 (insert ",\n "))
1449 (t (insert ", ")))
1450 (help-insert-xref-button text 'help-package name))
1451 (insert "\n")))
96ae4c8f 1452 (insert " " (propertize "Summary" 'font-lock-face 'bold)
12059709 1453 ": " (if desc (package-desc-summary desc)) "\n")
056453c6
DG
1454 (when homepage
1455 (insert " " (propertize "Homepage" 'font-lock-face 'bold) ": ")
1456 (help-insert-xref-button homepage 'help-url homepage)
1457 (insert "\n"))
a81fc7ba
TZ
1458 (when keywords
1459 (insert " " (propertize "Keywords" 'font-lock-face 'bold) ": ")
1460 (dolist (k keywords)
1461 (package-make-button
1462 k
1463 'package-keyword k
1464 'action 'package-keyword-button-action)
1465 (insert " "))
1466 (insert "\n"))
12059709
SM
1467 (let* ((all-pkgs (append (cdr (assq name package-alist))
1468 (cdr (assq name package-archive-contents))
1469 (let ((bi (assq name package--builtins)))
1470 (if bi (list (package--from-builtin bi))))))
1471 (other-pkgs (delete desc all-pkgs)))
1472 (when other-pkgs
1473 (insert " " (propertize "Other versions" 'font-lock-face 'bold) ": "
1474 (mapconcat
1475 (lambda (opkg)
1476 (let* ((ov (package-desc-version opkg))
1477 (dir (package-desc-dir opkg))
1478 (from (or (package-desc-archive opkg)
1479 (if (stringp dir) "installed" dir))))
1480 (if (not ov) (format "%s" from)
1481 (format "%s (%s)"
1482 (make-text-button (package-version-join ov) nil
1483 'face 'link
1484 'follow-link t
1485 'action
1486 (lambda (_button)
1487 (describe-package opkg)))
1488 from))))
1489 other-pkgs ", ")
1490 ".\n")))
1491
1492 (insert "\n")
cb6c4991 1493
4b99edf2 1494 (if built-in
96ae4c8f 1495 ;; For built-in packages, insert the commentary.
12059709 1496 (let ((fn (locate-file (format "%s.el" name) load-path
96ae4c8f
CY
1497 load-file-rep-suffixes))
1498 (opoint (point)))
1499 (insert (or (lm-commentary fn) ""))
1500 (save-excursion
1501 (goto-char opoint)
1502 (when (re-search-forward "^;;; Commentary:\n" nil t)
1503 (replace-match ""))
1504 (while (re-search-forward "^\\(;+ ?\\)" nil t)
1505 (replace-match ""))))
12059709 1506 (let ((readme (expand-file-name (format "%s-readme.txt" name)
f561e49a
CY
1507 package-user-dir))
1508 readme-string)
96ae4c8f
CY
1509 ;; For elpa packages, try downloading the commentary. If that
1510 ;; fails, try an existing readme file in `package-user-dir'.
f561e49a 1511 (cond ((condition-case nil
7e3561ee
SM
1512 (save-excursion
1513 (package--with-work-buffer
1514 (package-archive-base desc)
1515 (format "%s-readme.txt" name)
b0df8b95
DG
1516 (save-excursion
1517 (goto-char (point-max))
1518 (unless (bolp)
1519 (insert ?\n)))
1520 (write-region nil nil
1521 (expand-file-name readme package-user-dir)
1522 nil 'silent)
7e3561ee
SM
1523 (setq readme-string (buffer-string))
1524 t))
f561e49a 1525 (error nil))
196716cf 1526 (insert readme-string))
96ae4c8f
CY
1527 ((file-readable-p readme)
1528 (insert-file-contents readme)
1529 (goto-char (point-max))))))))
cb6c4991
CY
1530
1531(defun package-install-button-action (button)
1b8dff23
SM
1532 (let ((pkg-desc (button-get button 'package-desc)))
1533 (when (y-or-n-p (format "Install package `%s'? "
1534 (package-desc-full-name pkg-desc)))
1535 (package-install pkg-desc)
cb6c4991
CY
1536 (revert-buffer nil t)
1537 (goto-char (point-min)))))
cced7584 1538
a81fc7ba
TZ
1539(defun package-keyword-button-action (button)
1540 (let ((pkg-keyword (button-get button 'package-keyword)))
5ae811dd 1541 (package-show-package-list t (list pkg-keyword))))
a81fc7ba
TZ
1542
1543(defun package-make-button (text &rest props)
1544 (let ((button-text (if (display-graphic-p) text (concat "[" text "]")))
1545 (button-face (if (display-graphic-p)
1546 '(:box (:line-width 2 :color "dark grey")
1547 :background "light grey"
1548 :foreground "black")
1549 'link)))
1550 (apply 'insert-text-button button-text 'face button-face 'follow-link t
1551 props)))
1552
cced7584 1553\f
44198b6e
CY
1554;;;; Package menu mode.
1555
54ea2a0d 1556(defvar package-menu-mode-map
e91a96fe 1557 (let ((map (make-sparse-keymap))
64eba874 1558 (menu-map (make-sparse-keymap "Package")))
e91a96fe 1559 (set-keymap-parent map tabulated-list-mode-map)
8adb4c33 1560 (define-key map "\C-m" 'package-menu-describe-package)
54ea2a0d
JB
1561 (define-key map "u" 'package-menu-mark-unmark)
1562 (define-key map "\177" 'package-menu-backup-unmark)
1563 (define-key map "d" 'package-menu-mark-delete)
1564 (define-key map "i" 'package-menu-mark-install)
25322144 1565 (define-key map "U" 'package-menu-mark-upgrades)
54ea2a0d 1566 (define-key map "r" 'package-menu-refresh)
a215930c 1567 (define-key map "f" 'package-menu-filter)
54ea2a0d
JB
1568 (define-key map "~" 'package-menu-mark-obsolete-for-deletion)
1569 (define-key map "x" 'package-menu-execute)
1570 (define-key map "h" 'package-menu-quick-help)
cb6c4991 1571 (define-key map "?" 'package-menu-describe-package)
64eba874
DN
1572 (define-key map [menu-bar package-menu] (cons "Package" menu-map))
1573 (define-key menu-map [mq]
1574 '(menu-item "Quit" quit-window
1575 :help "Quit package selection"))
1576 (define-key menu-map [s1] '("--"))
1577 (define-key menu-map [mn]
1578 '(menu-item "Next" next-line
1579 :help "Next Line"))
1580 (define-key menu-map [mp]
1581 '(menu-item "Previous" previous-line
1582 :help "Previous Line"))
1583 (define-key menu-map [s2] '("--"))
1584 (define-key menu-map [mu]
1585 '(menu-item "Unmark" package-menu-mark-unmark
1586 :help "Clear any marks on a package and move to the next line"))
1587 (define-key menu-map [munm]
7cc6e154 1588 '(menu-item "Unmark Backwards" package-menu-backup-unmark
64eba874
DN
1589 :help "Back up one line and clear any marks on that package"))
1590 (define-key menu-map [md]
7cc6e154 1591 '(menu-item "Mark for Deletion" package-menu-mark-delete
64eba874
DN
1592 :help "Mark a package for deletion and move to the next line"))
1593 (define-key menu-map [mi]
7cc6e154 1594 '(menu-item "Mark for Install" package-menu-mark-install
64eba874 1595 :help "Mark a package for installation and move to the next line"))
d770725a 1596 (define-key menu-map [mupgrades]
7cc6e154 1597 '(menu-item "Mark Upgradable Packages" package-menu-mark-upgrades
d770725a 1598 :help "Mark packages that have a newer version for upgrading"))
64eba874 1599 (define-key menu-map [s3] '("--"))
a215930c
GM
1600 (define-key menu-map [mf]
1601 '(menu-item "Filter Package List..." package-menu-filter
1602 :help "Filter package selection (q to go back)"))
64eba874 1603 (define-key menu-map [mg]
7cc6e154 1604 '(menu-item "Update Package List" revert-buffer
64eba874
DN
1605 :help "Update the list of packages"))
1606 (define-key menu-map [mr]
7cc6e154 1607 '(menu-item "Refresh Package List" package-menu-refresh
64eba874
DN
1608 :help "Download the ELPA archive"))
1609 (define-key menu-map [s4] '("--"))
1610 (define-key menu-map [mt]
7cc6e154 1611 '(menu-item "Mark Obsolete Packages" package-menu-mark-obsolete-for-deletion
64eba874
DN
1612 :help "Mark all obsolete packages for deletion"))
1613 (define-key menu-map [mx]
7cc6e154 1614 '(menu-item "Execute Actions" package-menu-execute
64eba874
DN
1615 :help "Perform all the marked actions"))
1616 (define-key menu-map [s5] '("--"))
1617 (define-key menu-map [mh]
1618 '(menu-item "Help" package-menu-quick-help
1619 :help "Show short key binding help for package-menu-mode"))
1620 (define-key menu-map [mc]
88103345 1621 '(menu-item "Describe Package" package-menu-describe-package
64eba874 1622 :help "Display information about this package"))
54ea2a0d 1623 map)
44198b6e
CY
1624 "Local keymap for `package-menu-mode' buffers.")
1625
60057926
CY
1626(defvar package-menu--new-package-list nil
1627 "List of newly-available packages since `list-packages' was last called.")
1628
e91a96fe 1629(define-derived-mode package-menu-mode tabulated-list-mode "Package Menu"
44198b6e
CY
1630 "Major mode for browsing a list of packages.
1631Letters do not insert themselves; instead, they are commands.
1632\\<package-menu-mode-map>
1633\\{package-menu-mode-map}"
29afcbab
SM
1634 (setq tabulated-list-format
1635 `[("Package" 18 package-menu--name-predicate)
1636 ("Version" 12 nil)
1637 ("Status" 10 package-menu--status-predicate)
1638 ,@(if (cdr package-archives)
1639 '(("Archive" 10 package-menu--archive-predicate)))
1640 ("Description" 0 nil)])
e91a96fe
CY
1641 (setq tabulated-list-padding 2)
1642 (setq tabulated-list-sort-key (cons "Status" nil))
6874724a 1643 (add-hook 'tabulated-list-revert-hook 'package-menu--refresh nil t)
e91a96fe
CY
1644 (tabulated-list-init-header))
1645
1b8dff23 1646(defmacro package--push (pkg-desc status listname)
e91a96fe
CY
1647 "Convenience macro for `package-menu--generate'.
1648If the alist stored in the symbol LISTNAME lacks an entry for a
1b8dff23
SM
1649package PKG-DESC, add one. The alist is keyed with PKG-DESC."
1650 `(unless (assoc ,pkg-desc ,listname)
1651 ;; FIXME: Should we move status into pkg-desc?
1652 (push (cons ,pkg-desc ,status) ,listname)))
e91a96fe 1653
66bd25ab
SM
1654(defvar package-list-unversioned nil
1655 "If non-nil include packages that don't have a version in `list-package'.")
1656
12059709
SM
1657(defun package-desc-status (pkg-desc)
1658 (let* ((name (package-desc-name pkg-desc))
1659 (dir (package-desc-dir pkg-desc))
1660 (lle (assq name package-load-list))
1661 (held (cadr lle))
acbadd00
DU
1662 (version (package-desc-version pkg-desc))
1663 (signed (package-desc-signed pkg-desc)))
12059709
SM
1664 (cond
1665 ((eq dir 'builtin) "built-in")
1666 ((and lle (null held)) "disabled")
1667 ((stringp held)
1668 (let ((hv (if (stringp held) (version-to-list held))))
1669 (cond
1670 ((version-list-= version hv) "held")
1671 ((version-list-< version hv) "obsolete")
1672 (t "disabled"))))
1673 ((package-built-in-p name version) "obsolete")
1674 (dir ;One of the installed packages.
1675 (cond
1676 ((not (file-exists-p (package-desc-dir pkg-desc))) "deleted")
acbadd00
DU
1677 ((eq pkg-desc (cadr (assq name package-alist))) (if signed
1678 "installed"
1679 "unsigned"))
12059709
SM
1680 (t "obsolete")))
1681 (t
1682 (let* ((ins (cadr (assq name package-alist)))
1683 (ins-v (if ins (package-desc-version ins))))
1684 (cond
1685 ((or (null ins) (version-list-< ins-v version))
1686 (if (memq name package-menu--new-package-list)
1687 "new" "available"))
1688 ((version-list-< version ins-v) "obsolete")
acbadd00
DU
1689 ((version-list-= version ins-v) (if signed
1690 "installed"
1691 "unsigned"))))))))
12059709 1692
5ae811dd 1693(defun package-menu--refresh (&optional packages keywords)
12059709 1694 "Re-populate the `tabulated-list-entries'.
5ae811dd
TZ
1695PACKAGES should be nil or t, which means to display all known packages.
1696KEYWORDS should be nil or a list of keywords."
1b8dff23 1697 ;; Construct list of (PKG-DESC . STATUS).
12059709 1698 (unless packages (setq packages t))
4d6769e1 1699 (let (info-list name)
e91a96fe
CY
1700 ;; Installed packages:
1701 (dolist (elt package-alist)
1702 (setq name (car elt))
512e3ae1 1703 (when (or (eq packages t) (memq name packages))
12059709 1704 (dolist (pkg (cdr elt))
5ae811dd
TZ
1705 (when (package--has-keyword-p pkg keywords)
1706 (package--push pkg (package-desc-status pkg) info-list)))))
e91a96fe
CY
1707
1708 ;; Built-in packages:
1709 (dolist (elt package--builtins)
1710 (setq name (car elt))
512e3ae1 1711 (when (and (not (eq name 'emacs)) ; Hide the `emacs' package.
5ae811dd 1712 (package--has-keyword-p (package--from-builtin elt) keywords)
66bd25ab
SM
1713 (or package-list-unversioned
1714 (package--bi-desc-version (cdr elt)))
512e3ae1 1715 (or (eq packages t) (memq name packages)))
1b8dff23 1716 (package--push (package--from-builtin elt) "built-in" info-list)))
e91a96fe
CY
1717
1718 ;; Available and disabled packages:
1719 (dolist (elt package-archive-contents)
1720 (setq name (car elt))
512e3ae1 1721 (when (or (eq packages t) (memq name packages))
12059709
SM
1722 (dolist (pkg (cdr elt))
1723 ;; Hide obsolete packages.
5ae811dd
TZ
1724 (when (and (not (package-installed-p (package-desc-name pkg)
1725 (package-desc-version pkg)))
1726 (package--has-keyword-p pkg keywords))
12059709 1727 (package--push pkg (package-desc-status pkg) info-list)))))
e91a96fe 1728
e91a96fe 1729 ;; Print the result.
12059709
SM
1730 (setq tabulated-list-entries
1731 (mapcar #'package-menu--print-info info-list))))
1732
5ae811dd
TZ
1733(defun package-all-keywords ()
1734 "Collect all package keywords"
1735 (let (keywords)
1736 (package--mapc (lambda (desc)
29afcbab 1737 (let* ((desc-keywords (and desc (package-desc--keywords desc))))
5ae811dd
TZ
1738 (setq keywords (append keywords desc-keywords)))))
1739 keywords))
1740
1741(defun package--mapc (function &optional packages)
1742 "Call FUNCTION for all known PACKAGES.
1743PACKAGES can be nil or t, which means to display all known
1744packages, or a list of packages.
1745
1746Built-in packages are converted with `package--from-builtin'."
1747 (unless packages (setq packages t))
1748 (let (name)
1749 ;; Installed packages:
1750 (dolist (elt package-alist)
1751 (setq name (car elt))
1752 (when (or (eq packages t) (memq name packages))
1753 (mapc function (cdr elt))))
1754
1755 ;; Built-in packages:
1756 (dolist (elt package--builtins)
1757 (setq name (car elt))
1758 (when (and (not (eq name 'emacs)) ; Hide the `emacs' package.
1759 (or package-list-unversioned
1760 (package--bi-desc-version (cdr elt)))
1761 (or (eq packages t) (memq name packages)))
1762 (funcall function (package--from-builtin elt))))
1763
1764 ;; Available and disabled packages:
1765 (dolist (elt package-archive-contents)
1766 (setq name (car elt))
1767 (when (or (eq packages t) (memq name packages))
1768 (dolist (pkg (cdr elt))
1769 ;; Hide obsolete packages.
1770 (unless (package-installed-p (package-desc-name pkg)
1771 (package-desc-version pkg))
1772 (funcall function pkg)))))))
1773
1774(defun package--has-keyword-p (desc &optional keywords)
1775 "Test if package DESC has any of the given KEYWORDS.
1776When none are given, the package matches."
1777 (if keywords
29afcbab 1778 (let* ((desc-keywords (and desc (package-desc--keywords desc)))
5ae811dd
TZ
1779 found)
1780 (dolist (k keywords)
1781 (when (and (not found)
1782 (member k desc-keywords))
1783 (setq found t)))
1784 found)
1785 t))
1786
1787(defun package-menu--generate (remember-pos packages &optional keywords)
12059709
SM
1788 "Populate the Package Menu.
1789 If REMEMBER-POS is non-nil, keep point on the same entry.
1790PACKAGES should be t, which means to display all known packages,
5ae811dd
TZ
1791or a list of package names (symbols) to display.
1792
1793With KEYWORDS given, only packages with those keywords are
1794shown."
1795 (package-menu--refresh packages keywords)
1796 (setf (car (aref tabulated-list-format 0))
1797 (if keywords
1798 (let ((filters (mapconcat 'identity keywords ",")))
1799 (concat "Package[" filters "]"))
1800 "Package"))
1801 (if keywords
1802 (define-key package-menu-mode-map "q" 'package-show-package-list)
1803 (define-key package-menu-mode-map "q" 'quit-window))
1804 (tabulated-list-init-header)
12059709 1805 (tabulated-list-print remember-pos))
e91a96fe
CY
1806
1807(defun package-menu--print-info (pkg)
1808 "Return a package entry suitable for `tabulated-list-entries'.
1b8dff23
SM
1809PKG has the form (PKG-DESC . STATUS).
1810Return (PKG-DESC [NAME VERSION STATUS DOC])."
1811 (let* ((pkg-desc (car pkg))
1812 (status (cdr pkg))
1813 (face (pcase status
29afcbab
SM
1814 (`"built-in" 'font-lock-builtin-face)
1815 (`"available" 'default)
1816 (`"new" 'bold)
1817 (`"held" 'font-lock-constant-face)
1818 (`"disabled" 'font-lock-warning-face)
1819 (`"installed" 'font-lock-comment-face)
1820 (`"unsigned" 'font-lock-warning-face)
1821 (_ 'font-lock-warning-face)))) ; obsolete.
1b8dff23 1822 (list pkg-desc
29afcbab
SM
1823 `[,(list (symbol-name (package-desc-name pkg-desc))
1824 'face 'link
1825 'follow-link t
1826 'package-desc pkg-desc
1827 'action 'package-menu-describe-package)
1828 ,(propertize (package-version-join
1829 (package-desc-version pkg-desc))
1830 'font-lock-face face)
1831 ,(propertize status 'font-lock-face face)
1832 ,@(if (cdr package-archives)
1833 (list (propertize (or (package-desc-archive pkg-desc) "")
1834 'font-lock-face face)))
1835 ,(propertize (package-desc-summary pkg-desc)
1836 'font-lock-face face)])))
44198b6e
CY
1837
1838(defun package-menu-refresh ()
ebf662f4
CY
1839 "Download the Emacs Lisp package archive.
1840This fetches the contents of each archive specified in
1841`package-archives', and then refreshes the package menu."
44198b6e 1842 (interactive)
25322144 1843 (unless (derived-mode-p 'package-menu-mode)
96dbf5a8 1844 (user-error "The current buffer is not a Package Menu"))
44198b6e 1845 (package-refresh-contents)
512e3ae1 1846 (package-menu--generate t t))
44198b6e 1847
e91a96fe
CY
1848(defun package-menu-describe-package (&optional button)
1849 "Describe the current package.
1850If optional arg BUTTON is non-nil, describe its associated package."
44198b6e 1851 (interactive)
1b8dff23 1852 (let ((pkg-desc (if button (button-get button 'package-desc)
66bd25ab 1853 (tabulated-list-get-id))))
1b8dff23 1854 (if pkg-desc
12059709 1855 (describe-package pkg-desc)
96dbf5a8 1856 (user-error "No package here"))))
44198b6e
CY
1857
1858;; fixme numeric argument
4d6769e1 1859(defun package-menu-mark-delete (&optional _num)
44198b6e
CY
1860 "Mark a package for deletion and move to the next line."
1861 (interactive "p")
acbadd00 1862 (if (member (package-menu-get-status) '("installed" "obsolete" "unsigned"))
e91a96fe 1863 (tabulated-list-put-tag "D" t)
015eea59 1864 (forward-line)))
44198b6e 1865
4d6769e1 1866(defun package-menu-mark-install (&optional _num)
44198b6e
CY
1867 "Mark a package for installation and move to the next line."
1868 (interactive "p")
60057926 1869 (if (member (package-menu-get-status) '("available" "new"))
e91a96fe 1870 (tabulated-list-put-tag "I" t)
015eea59 1871 (forward-line)))
44198b6e 1872
4d6769e1 1873(defun package-menu-mark-unmark (&optional _num)
44198b6e
CY
1874 "Clear any marks on a package and move to the next line."
1875 (interactive "p")
e91a96fe 1876 (tabulated-list-put-tag " " t))
44198b6e
CY
1877
1878(defun package-menu-backup-unmark ()
1879 "Back up one line and clear any marks on that package."
1880 (interactive)
1881 (forward-line -1)
e91a96fe 1882 (tabulated-list-put-tag " "))
44198b6e
CY
1883
1884(defun package-menu-mark-obsolete-for-deletion ()
1885 "Mark all obsolete packages for deletion."
1886 (interactive)
1887 (save-excursion
1888 (goto-char (point-min))
44198b6e 1889 (while (not (eobp))
25322144 1890 (if (equal (package-menu-get-status) "obsolete")
e91a96fe 1891 (tabulated-list-put-tag "D" t)
44198b6e
CY
1892 (forward-line 1)))))
1893
1894(defun package-menu-quick-help ()
1895 "Show short key binding help for package-menu-mode."
1896 (interactive)
1897 (message "n-ext, i-nstall, d-elete, u-nmark, x-ecute, r-efresh, h-elp"))
1898
cb6c4991
CY
1899(define-obsolete-function-alias
1900 'package-menu-view-commentary 'package-menu-describe-package "24.1")
44198b6e 1901
44198b6e 1902(defun package-menu-get-status ()
1b8dff23
SM
1903 (let* ((id (tabulated-list-get-id))
1904 (entry (and id (assq id tabulated-list-entries))))
25322144
CY
1905 (if entry
1906 (aref (cadr entry) 2)
44198b6e
CY
1907 "")))
1908
25322144
CY
1909(defun package-menu--find-upgrades ()
1910 (let (installed available upgrades)
1911 ;; Build list of installed/available packages in this buffer.
1912 (dolist (entry tabulated-list-entries)
1b8dff23
SM
1913 ;; ENTRY is (PKG-DESC [NAME VERSION STATUS DOC])
1914 (let ((pkg-desc (car entry))
4d6769e1 1915 (status (aref (cadr entry) 2)))
acbadd00 1916 (cond ((member status '("installed" "unsigned"))
1b8dff23 1917 (push pkg-desc installed))
60057926 1918 ((member status '("available" "new"))
1b8dff23
SM
1919 (push (cons (package-desc-name pkg-desc) pkg-desc)
1920 available)))))
1921 ;; Loop through list of installed packages, finding upgrades.
1922 (dolist (pkg-desc installed)
1923 (let ((avail-pkg (assq (package-desc-name pkg-desc) available)))
25322144 1924 (and avail-pkg
1b8dff23
SM
1925 (version-list-< (package-desc-version pkg-desc)
1926 (package-desc-version (cdr avail-pkg)))
25322144
CY
1927 (push avail-pkg upgrades))))
1928 upgrades))
1929
1930(defun package-menu-mark-upgrades ()
1931 "Mark all upgradable packages in the Package Menu.
1932For each installed package with a newer version available, place
1933an (I)nstall flag on the available version and a (D)elete flag on
1934the installed version. A subsequent \\[package-menu-execute]
1935call will upgrade the package."
1936 (interactive)
1937 (unless (derived-mode-p 'package-menu-mode)
1938 (error "The current buffer is not a Package Menu"))
1939 (let ((upgrades (package-menu--find-upgrades)))
1940 (if (null upgrades)
1941 (message "No packages to upgrade.")
1942 (widen)
1943 (save-excursion
1944 (goto-char (point-min))
1945 (while (not (eobp))
1b8dff23
SM
1946 (let* ((pkg-desc (tabulated-list-get-id))
1947 (upgrade (cdr (assq (package-desc-name pkg-desc) upgrades))))
25322144
CY
1948 (cond ((null upgrade)
1949 (forward-line 1))
1b8dff23 1950 ((equal pkg-desc upgrade)
25322144
CY
1951 (package-menu-mark-install))
1952 (t
1953 (package-menu-mark-delete))))))
1954 (message "%d package%s marked for upgrading."
1955 (length upgrades)
1956 (if (= (length upgrades) 1) "" "s")))))
1957
a7da0114 1958(defun package-menu-execute (&optional noquery)
015eea59
CY
1959 "Perform marked Package Menu actions.
1960Packages marked for installation are downloaded and installed;
eeb468da
GM
1961packages marked for deletion are removed.
1962Optional argument NOQUERY non-nil means do not ask the user to confirm."
44198b6e 1963 (interactive)
25322144 1964 (unless (derived-mode-p 'package-menu-mode)
e91a96fe 1965 (error "The current buffer is not in Package Menu mode"))
1b8dff23 1966 (let (install-list delete-list cmd pkg-desc)
015eea59
CY
1967 (save-excursion
1968 (goto-char (point-min))
1969 (while (not (eobp))
1970 (setq cmd (char-after))
e91a96fe 1971 (unless (eq cmd ?\s)
1b8dff23
SM
1972 ;; This is the key PKG-DESC.
1973 (setq pkg-desc (tabulated-list-get-id))
e91a96fe 1974 (cond ((eq cmd ?D)
1b8dff23 1975 (push pkg-desc delete-list))
e91a96fe 1976 ((eq cmd ?I)
1b8dff23 1977 (push pkg-desc install-list))))
015eea59 1978 (forward-line)))
25322144 1979 (when install-list
a7da0114
YB
1980 (if (or
1981 noquery
1982 (yes-or-no-p
1b8dff23
SM
1983 (if (= (length install-list) 1)
1984 (format "Install package `%s'? "
1985 (package-desc-full-name (car install-list)))
1986 (format "Install these %d packages (%s)? "
1987 (length install-list)
1988 (mapconcat #'package-desc-full-name
1989 install-list ", ")))))
25322144 1990 (mapc 'package-install install-list)))
015eea59
CY
1991 ;; Delete packages, prompting if necessary.
1992 (when delete-list
a7da0114
YB
1993 (if (or
1994 noquery
1995 (yes-or-no-p
015eea59 1996 (if (= (length delete-list) 1)
1b8dff23
SM
1997 (format "Delete package `%s'? "
1998 (package-desc-full-name (car delete-list)))
015eea59
CY
1999 (format "Delete these %d packages (%s)? "
2000 (length delete-list)
1b8dff23
SM
2001 (mapconcat #'package-desc-full-name
2002 delete-list ", ")))))
015eea59 2003 (dolist (elt delete-list)
1be3ca5a 2004 (condition-case-unless-debug err
1b8dff23 2005 (package-delete elt)
015eea59
CY
2006 (error (message (cadr err)))))
2007 (error "Aborted")))
015eea59 2008 (if (or delete-list install-list)
512e3ae1 2009 (package-menu--generate t t)
015eea59 2010 (message "No operations specified."))))
44198b6e 2011
e91a96fe
CY
2012(defun package-menu--version-predicate (A B)
2013 (let ((vA (or (aref (cadr A) 1) '(0)))
2014 (vB (or (aref (cadr B) 1) '(0))))
2015 (if (version-list-= vA vB)
2016 (package-menu--name-predicate A B)
2017 (version-list-< vA vB))))
2018
2019(defun package-menu--status-predicate (A B)
2020 (let ((sA (aref (cadr A) 2))
2021 (sB (aref (cadr B) 2)))
2022 (cond ((string= sA sB)
2023 (package-menu--name-predicate A B))
60057926
CY
2024 ((string= sA "new") t)
2025 ((string= sB "new") nil)
2026 ((string= sA "available") t)
e91a96fe 2027 ((string= sB "available") nil)
60057926 2028 ((string= sA "installed") t)
e91a96fe 2029 ((string= sB "installed") nil)
acbadd00
DU
2030 ((string= sA "unsigned") t)
2031 ((string= sB "unsigned") nil)
60057926 2032 ((string= sA "held") t)
e91a96fe 2033 ((string= sB "held") nil)
60057926 2034 ((string= sA "built-in") t)
e91a96fe 2035 ((string= sB "built-in") nil)
60057926
CY
2036 ((string= sA "obsolete") t)
2037 ((string= sB "obsolete") nil)
e91a96fe
CY
2038 (t (string< sA sB)))))
2039
2040(defun package-menu--description-predicate (A B)
2041 (let ((dA (aref (cadr A) 3))
2042 (dB (aref (cadr B) 3)))
2043 (if (string= dA dB)
2044 (package-menu--name-predicate A B)
2045 (string< dA dB))))
2046
2047(defun package-menu--name-predicate (A B)
1b8dff23
SM
2048 (string< (symbol-name (package-desc-name (car A)))
2049 (symbol-name (package-desc-name (car B)))))
44198b6e 2050
7397c587
TZ
2051(defun package-menu--archive-predicate (A B)
2052 (string< (or (package-desc-archive (car A)) "")
2053 (or (package-desc-archive (car B)) "")))
2054
44198b6e 2055;;;###autoload
e91a96fe 2056(defun list-packages (&optional no-fetch)
44198b6e 2057 "Display a list of packages.
e91a96fe
CY
2058This first fetches the updated list of packages before
2059displaying, unless a prefix argument NO-FETCH is specified.
44198b6e 2060The list is displayed in a buffer named `*Packages*'."
e91a96fe
CY
2061 (interactive "P")
2062 (require 'finder-inf nil t)
8a500a91 2063 ;; Initialize the package system if necessary.
4b99edf2
CY
2064 (unless package--initialized
2065 (package-initialize t))
60057926
CY
2066 (let (old-archives new-packages)
2067 (unless no-fetch
2068 ;; Read the locally-cached archive-contents.
2069 (package-read-all-archive-contents)
2070 (setq old-archives package-archive-contents)
2071 ;; Fetch the remote list of packages.
2072 (package-refresh-contents)
2073 ;; Find which packages are new.
2074 (dolist (elt package-archive-contents)
2075 (unless (assq (car elt) old-archives)
2076 (push (car elt) new-packages))))
2077
2078 ;; Generate the Package Menu.
2079 (let ((buf (get-buffer-create "*Packages*")))
2080 (with-current-buffer buf
2081 (package-menu-mode)
2082 (set (make-local-variable 'package-menu--new-package-list)
2083 new-packages)
2084 (package-menu--generate nil t))
2085 ;; The package menu buffer has keybindings. If the user types
2086 ;; `M-x list-packages', that suggests it should become current.
2087 (switch-to-buffer buf))
2088
2089 (let ((upgrades (package-menu--find-upgrades)))
2090 (if upgrades
2091 (message "%d package%s can be upgraded; type `%s' to mark %s for upgrading."
2092 (length upgrades)
2093 (if (= (length upgrades) 1) "" "s")
2094 (substitute-command-keys "\\[package-menu-mark-upgrades]")
2095 (if (= (length upgrades) 1) "it" "them"))))))
44198b6e 2096
cb8759ca 2097;;;###autoload
96ae4c8f 2098(defalias 'package-list-packages 'list-packages)
cb8759ca 2099
512e3ae1 2100;; Used in finder.el
5ae811dd 2101(defun package-show-package-list (&optional packages keywords)
512e3ae1
CY
2102 "Display PACKAGES in a *Packages* buffer.
2103This is similar to `list-packages', but it does not fetch the
2104updated list of packages, and it only displays packages with
5ae811dd
TZ
2105names in PACKAGES (which should be a list of symbols).
2106
2107When KEYWORDS are given, only packages with those KEYWORDS are
2108shown."
2109 (interactive)
512e3ae1 2110 (require 'finder-inf nil t)
2614c1af
DG
2111 (let* ((buf (get-buffer-create "*Packages*"))
2112 (win (get-buffer-window buf)))
512e3ae1
CY
2113 (with-current-buffer buf
2114 (package-menu-mode)
5ae811dd 2115 (package-menu--generate nil packages keywords))
2614c1af
DG
2116 (if win
2117 (select-window win)
2118 (switch-to-buffer buf))))
512e3ae1 2119
a215930c
GM
2120;; package-menu--generate rebinds "q" on the fly, so we have to
2121;; hard-code the binding in the doc-string here.
2122(defun package-menu-filter (keyword)
2123 "Filter the *Packages* buffer.
2124Show only those items that relate to the specified KEYWORD.
2125To restore the full package list, type `q'."
5ae811dd
TZ
2126 (interactive (list (completing-read "Keyword: " (package-all-keywords))))
2127 (package-show-package-list t (list keyword)))
2128
44198b6e
CY
2129(defun package-list-packages-no-fetch ()
2130 "Display a list of packages.
2131Does not fetch the updated list of packages before displaying.
2132The list is displayed in a buffer named `*Packages*'."
2133 (interactive)
e91a96fe 2134 (list-packages t))
44198b6e 2135
44198b6e
CY
2136(provide 'package)
2137
2138;;; package.el ends here