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