* etc/NEWS: Document the disappearance of binding caches.
[bpt/emacs.git] / lisp / emacs-lisp / package.el
CommitLineData
44198b6e
CY
1;;; package.el --- Simple package system for Emacs
2
73b0cd50 3;; Copyright (C) 2007-2011 Free Software Foundation, Inc.
44198b6e
CY
4
5;; Author: Tom Tromey <tromey@redhat.com>
6;; Created: 10 Mar 2007
7;; Version: 0.9
8;; Keywords: tools
9
10;; This file is part of GNU Emacs.
11
12;; GNU Emacs is free software; you can redistribute it and/or modify
13;; it under the terms of the GNU General Public License as published by
14;; the Free Software Foundation; either version 3, or (at your option)
15;; any later version.
16
17;; GNU Emacs is distributed in the hope that it will be useful,
18;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20;; GNU General Public License for more details.
21
22;; You should have received a copy of the GNU General Public License
23;; along with GNU Emacs; see the file COPYING. If not, write to the
24;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25;; Boston, MA 02110-1301, USA.
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
142;; - Implement M-x package-upgrade, to upgrade any/all existing packages
143;; - Use hierarchical layout. PKG/etc PKG/lisp PKG/info
144;; ... except maybe lisp?
145;; - It may be nice to have a macro that expands to the package's
146;; private data dir, aka ".../etc". Or, maybe data-directory
147;; needs to be a list (though this would be less nice)
148;; a few packages want this, eg sokoban
149;; - package menu needs:
150;; ability to know which packages are built-in & thus not deletable
151;; it can sometimes print odd results, like 0.3 available but 0.4 active
152;; why is that?
153;; - Allow multiple versions on the server...?
154;; [ why bother? ]
155;; - Don't install a package which will invalidate dependencies overall
156;; - Allow something like (or (>= emacs 21.0) (>= xemacs 21.5))
157;; [ currently thinking, why bother.. KISS ]
158;; - Allow optional package dependencies
159;; then if we require 'bbdb', bbdb-specific lisp in lisp/bbdb
160;; and just don't compile to add to load path ...?
161;; - Have a list of archive URLs? [ maybe there's no point ]
162;; - David Kastrup pointed out on the xemacs list that for GPL it
163;; is friendlier to ship the source tree. We could "support" that
164;; by just having a "src" subdir in the package. This isn't ideal
165;; but it probably is not worth trying to support random source
166;; tree layouts, build schemes, etc.
167;; - Our treatment of the info path is somewhat bogus
168;; - perhaps have an "unstable" tree in ELPA as well as a stable one
169
170;;; Code:
171
e91a96fe
CY
172(require 'tabulated-list)
173
44198b6e
CY
174(defgroup package nil
175 "Manager for Emacs Lisp packages."
176 :group 'applications
177 :version "24.1")
178
179;;;###autoload
180(defcustom package-enable-at-startup t
181 "Whether to activate installed packages when Emacs starts.
182If non-nil, packages are activated after reading the init file
183and before `after-init-hook'. Activation is not done if
184`user-init-file' is nil (e.g. Emacs was started with \"-q\").
185
186Even if the value is nil, you can type \\[package-initialize] to
187activate the package system at any time."
188 :type 'boolean
189 :group 'package
190 :version "24.1")
191
192(defcustom package-load-list '(all)
193 "List of packages for `package-initialize' to load.
194Each element in this list should be a list (NAME VERSION), or the
195symbol `all'. The symbol `all' says to load the latest installed
196versions of all packages not specified by other elements.
197
198For an element (NAME VERSION), NAME is a package name (a symbol).
199VERSION should be t, a string, or nil.
200If VERSION is t, all versions are loaded, though obsolete ones
201 will be put in `package-obsolete-alist' and not activated.
202If VERSION is a string, only that version is ever loaded.
203 Any other version, even if newer, is silently ignored.
204 Hence, the package is \"held\" at that version.
205If VERSION is nil, the package is not loaded (it is \"disabled\")."
206 :type '(repeat symbol)
bc44bef7 207 :risky t
44198b6e
CY
208 :group 'package
209 :version "24.1")
210
211(defvar Info-directory-list)
44198b6e
CY
212(declare-function info-initialize "info" ())
213(declare-function url-http-parse-response "url-http" ())
214(declare-function lm-header "lisp-mnt" (header))
215(declare-function lm-commentary "lisp-mnt" (&optional file))
cb6c4991 216(defvar url-http-end-of-headers)
44198b6e 217
bc44bef7
PH
218(defcustom package-archives '(("gnu" . "http://elpa.gnu.org/packages/"))
219 "An alist of archives from which to fetch.
220The default value points to the GNU Emacs package repository.
f561e49a
CY
221
222Each element has the form (ID . LOCATION).
223 ID is an archive name, as a string.
224 LOCATION specifies the base location for the archive.
225 If it starts with \"http:\", it is treated as a HTTP URL;
226 otherwise it should be an absolute directory name.
227 (Other types of URL are currently not supported.)"
bc44bef7 228 :type '(alist :key-type (string :tag "Archive name")
f561e49a 229 :value-type (string :tag "URL or directory name"))
bc44bef7
PH
230 :risky t
231 :group 'package
232 :version "24.1")
44198b6e
CY
233
234(defconst package-archive-version 1
235 "Version number of the package archive understood by this file.
236Lower version numbers than this will probably be understood as well.")
237
238(defconst package-el-version "1.0"
239 "Version of package.el.")
240
241;; We don't prime the cache since it tends to get out of date.
242(defvar package-archive-contents nil
243 "Cache of the contents of the Emacs Lisp Package Archive.
244This is an alist mapping package names (symbols) to package
245descriptor vectors. These are like the vectors for `package-alist'
bc44bef7
PH
246but have extra entries: one which is 'tar for tar packages and
247'single for single-file packages, and one which is the name of
248the archive from which it came.")
249(put 'package-archive-contents 'risky-local-variable t)
44198b6e
CY
250
251(defcustom package-user-dir (locate-user-emacs-file "elpa")
252 "Directory containing the user's Emacs Lisp packages.
253The directory name should be absolute.
254Apart from this directory, Emacs also looks for system-wide
255packages in `package-directory-list'."
256 :type 'directory
bc44bef7 257 :risky t
44198b6e
CY
258 :group 'package
259 :version "24.1")
260
261(defcustom package-directory-list
262 ;; Defaults are subdirs named "elpa" in the site-lisp dirs.
263 (let (result)
264 (dolist (f load-path)
0be01d2c
CY
265 (and (stringp f)
266 (equal (file-name-nondirectory f) "site-lisp")
267 (push (expand-file-name "elpa" f) result)))
44198b6e
CY
268 (nreverse result))
269 "List of additional directories containing Emacs Lisp packages.
270Each directory name should be absolute.
271
272These directories contain packages intended for system-wide; in
273contrast, `package-user-dir' contains packages for personal use."
274 :type '(repeat directory)
bc44bef7 275 :risky t
44198b6e
CY
276 :group 'package
277 :version "24.1")
278
96ae4c8f
CY
279;; The value is precomputed in finder-inf.el, but don't load that
280;; until it's needed (i.e. when `package-intialize' is called).
281(defvar package--builtins nil
282 "Alist of built-in packages.
015eea59
CY
283The actual value is initialized by loading the library
284`finder-inf'; this is not done until it is needed, e.g. by the
285function `package-built-in-p'.
286
96ae4c8f
CY
287Each element has the form (PKG . DESC), where PKG is a package
288name (a symbol) and DESC is a vector that describes the package.
7ede3b65
CY
289The vector DESC has the form [VERSION-LIST REQS DOCSTRING].
290 VERSION-LIST is a version list.
291 REQS is a list of packages required by the package, each
292 requirement having the form (NAME VL), where NAME is a string
293 and VL is a version list.
96ae4c8f 294 DOCSTRING is a brief description of the package.")
bc44bef7 295(put 'package--builtins 'risky-local-variable t)
44198b6e 296
96ae4c8f 297(defvar package-alist nil
44198b6e 298 "Alist of all packages available for activation.
96ae4c8f
CY
299Each element has the form (PKG . DESC), where PKG is a package
300name (a symbol) and DESC is a vector that describes the package.
44198b6e 301
7ede3b65
CY
302The vector DESC has the form [VERSION-LIST REQS DOCSTRING].
303 VERSION-LIST is a version list.
304 REQS is a list of packages required by the package, each
305 requirement having the form (NAME VL) where NAME is a string
306 and VL is a version list.
96ae4c8f
CY
307 DOCSTRING is a brief description of the package.
308
309This variable is set automatically by `package-load-descriptor',
310called via `package-initialize'. To change which packages are
311loaded and/or activated, customize `package-load-list'.")
25322144 312(put 'package-alist 'risky-local-variable t)
44198b6e 313
96ae4c8f 314(defvar package-activated-list nil
44198b6e 315 "List of the names of currently activated packages.")
bc44bef7 316(put 'package-activated-list 'risky-local-variable t)
44198b6e
CY
317
318(defvar package-obsolete-alist nil
319 "Representation of obsolete packages.
320Like `package-alist', but maps package name to a second alist.
321The inner alist is keyed by version.")
bc44bef7 322(put 'package-obsolete-alist 'risky-local-variable t)
44198b6e 323
ba08b241
CY
324(defun package-version-join (vlist)
325 "Return the version string corresponding to the list VLIST.
326This is, approximately, the inverse of `version-to-list'.
327\(Actually, it returns only one of the possible inverses, since
328`version-to-list' is a many-to-one operation.)"
329 (if (null vlist)
330 ""
331 (let ((str-list (list "." (int-to-string (car vlist)))))
332 (dolist (num (cdr vlist))
333 (cond
334 ((>= num 0)
335 (push (int-to-string num) str-list)
336 (push "." str-list))
337 ((< num -3)
338 (error "Invalid version list `%s'" vlist))
339 (t
340 ;; pre, or beta, or alpha
341 (cond ((equal "." (car str-list))
342 (pop str-list))
343 ((not (string-match "[0-9]+" (car str-list)))
344 (error "Invalid version list `%s'" vlist)))
345 (push (cond ((= num -1) "pre")
346 ((= num -2) "beta")
347 ((= num -3) "alpha"))
348 str-list))))
349 (if (equal "." (car str-list))
350 (pop str-list))
351 (apply 'concat (nreverse str-list)))))
44198b6e 352
44198b6e
CY
353(defun package-strip-version (dirname)
354 "Strip the version from a combined package name and version.
355E.g., if given \"quux-23.0\", will return \"quux\""
4525ce3e 356 (if (string-match (concat "\\`" package-subdirectory-regexp "\\'") dirname)
44198b6e
CY
357 (match-string 1 dirname)))
358
359(defun package-load-descriptor (dir package)
4b99edf2 360 "Load the description file in directory DIR for package PACKAGE.
7ede3b65
CY
361Here, PACKAGE is a string of the form NAME-VERSION, where NAME is
362the package name and VERSION is its version."
bc44bef7
PH
363 (let* ((pkg-dir (expand-file-name package dir))
364 (pkg-file (expand-file-name
365 (concat (package-strip-version package) "-pkg")
366 pkg-dir)))
367 (when (and (file-directory-p pkg-dir)
368 (file-exists-p (concat pkg-file ".el")))
369 (load pkg-file nil t))))
44198b6e
CY
370
371(defun package-load-all-descriptors ()
372 "Load descriptors for installed Emacs Lisp packages.
373This looks for package subdirectories in `package-user-dir' and
374`package-directory-list'. The variable `package-load-list'
375controls which package subdirectories may be loaded.
376
377In each valid package subdirectory, this function loads the
378description file containing a call to `define-package', which
379updates `package-alist' and `package-obsolete-alist'."
380 (let ((all (memq 'all package-load-list))
4525ce3e 381 (regexp (concat "\\`" package-subdirectory-regexp "\\'"))
44198b6e
CY
382 name version force)
383 (dolist (dir (cons package-user-dir package-directory-list))
384 (when (file-directory-p dir)
385 (dolist (subdir (directory-files dir))
386 (when (and (file-directory-p (expand-file-name subdir dir))
4525ce3e 387 (string-match regexp subdir))
44198b6e
CY
388 (setq name (intern (match-string 1 subdir))
389 version (match-string 2 subdir)
390 force (assq name package-load-list))
391 (when (cond
392 ((null force)
393 all) ; not in package-load-list
394 ((null (setq force (cadr force)))
395 nil) ; disabled
396 ((eq force t)
397 t)
398 ((stringp force) ; held
148cef8e
CY
399 (version-list-= (version-to-list version)
400 (version-to-list force)))
44198b6e
CY
401 (t
402 (error "Invalid element in `package-load-list'")))
403 (package-load-descriptor dir subdir))))))))
404
405(defsubst package-desc-vers (desc)
406 "Extract version from a package description vector."
407 (aref desc 0))
408
409(defsubst package-desc-reqs (desc)
410 "Extract requirements from a package description vector."
411 (aref desc 1))
412
413(defsubst package-desc-doc (desc)
414 "Extract doc string from a package description vector."
415 (aref desc 2))
416
417(defsubst package-desc-kind (desc)
418 "Extract the kind of download from an archive package description vector."
419 (aref desc 3))
420
015eea59
CY
421(defun package--dir (name version)
422 "Return the directory where a package is installed, or nil if none.
423NAME and VERSION are both strings."
424 (let* ((subdir (concat name "-" version))
44198b6e 425 (dir-list (cons package-user-dir package-directory-list))
cced7584 426 pkg-dir)
44198b6e 427 (while dir-list
cced7584
CY
428 (let ((subdir-full (expand-file-name subdir (car dir-list))))
429 (if (file-directory-p subdir-full)
430 (setq pkg-dir subdir-full
431 dir-list nil)
44198b6e 432 (setq dir-list (cdr dir-list)))))
cced7584
CY
433 pkg-dir))
434
435(defun package-activate-1 (package pkg-vec)
436 (let* ((name (symbol-name package))
437 (version-str (package-version-join (package-desc-vers pkg-vec)))
438 (pkg-dir (package--dir name version-str)))
44198b6e 439 (unless pkg-dir
015eea59 440 (error "Internal error: unable to find directory for `%s-%s'"
cced7584
CY
441 name version-str))
442 ;; Add info node.
ebf662f4
CY
443 (when (file-exists-p (expand-file-name "dir" pkg-dir))
444 ;; FIXME: not the friendliest, but simple.
445 (require 'info)
446 (info-initialize)
447 (push pkg-dir Info-directory-list))
cced7584 448 ;; Add to load path, add autoloads, and activate the package.
ebf662f4 449 (push pkg-dir load-path)
cced7584 450 (load (expand-file-name (concat name "-autoloads") pkg-dir) nil t)
ebf662f4 451 (push package package-activated-list)
44198b6e
CY
452 ;; Don't return nil.
453 t))
454
7ede3b65
CY
455(defun package-built-in-p (package &optional min-version)
456 "Return true if PACKAGE is built-in to Emacs.
457Optional arg MIN-VERSION, if non-nil, should be a version list
458specifying the minimum acceptable version."
4b99edf2 459 (require 'finder-inf nil t) ; For `package--builtins'.
44198b6e 460 (let ((elt (assq package package--builtins)))
512e3ae1 461 (and elt (version-list-<= min-version (package-desc-vers (cdr elt))))))
44198b6e 462
4b99edf2
CY
463;; This function goes ahead and activates a newer version of a package
464;; if an older one was already activated. This is not ideal; we'd at
465;; least need to check to see if the package has actually been loaded,
466;; and not merely activated.
7ede3b65
CY
467(defun package-activate (package min-version)
468 "Activate package PACKAGE, of version MIN-VERSION or newer.
469MIN-VERSION should be a version list.
4b99edf2 470If PACKAGE has any dependencies, recursively activate them.
44198b6e 471Return nil if the package could not be activated."
4b99edf2
CY
472 (let ((pkg-vec (cdr (assq package package-alist)))
473 available-version found)
474 ;; Check if PACKAGE is available in `package-alist'.
475 (when pkg-vec
476 (setq available-version (package-desc-vers pkg-vec)
7ede3b65 477 found (version-list-<= min-version available-version)))
4b99edf2
CY
478 (cond
479 ;; If no such package is found, maybe it's built-in.
480 ((null found)
7ede3b65 481 (package-built-in-p package min-version))
4b99edf2
CY
482 ;; If the package is already activated, just return t.
483 ((memq package package-activated-list)
484 t)
485 ;; Otherwise, proceed with activation.
486 (t
487 (let ((fail (catch 'dep-failure
488 ;; Activate its dependencies recursively.
489 (dolist (req (package-desc-reqs pkg-vec))
490 (unless (package-activate (car req) (cadr req))
491 (throw 'dep-failure req))))))
492 (if fail
493 (warn "Unable to activate package `%s'.
015eea59 494Required package `%s-%s' is unavailable"
4b99edf2
CY
495 package (car fail) (package-version-join (cadr fail)))
496 ;; If all goes well, activate the package itself.
497 (package-activate-1 package pkg-vec)))))))
44198b6e
CY
498
499(defun package-mark-obsolete (package pkg-vec)
500 "Put package on the obsolete list, if not already there."
501 (let ((elt (assq package package-obsolete-alist)))
502 (if elt
503 ;; If this obsolete version does not exist in the list, update
504 ;; it the list.
505 (unless (assoc (package-desc-vers pkg-vec) (cdr elt))
506 (setcdr elt (cons (cons (package-desc-vers pkg-vec) pkg-vec)
507 (cdr elt))))
508 ;; Make a new association.
ebf662f4
CY
509 (push (cons package (list (cons (package-desc-vers pkg-vec)
510 pkg-vec)))
511 package-obsolete-alist))))
44198b6e 512
4b99edf2 513(defun define-package (name-string version-string
187d3296
CY
514 &optional docstring requirements
515 &rest extra-properties)
44198b6e 516 "Define a new package.
4b99edf2 517NAME-STRING is the name of the package, as a string.
7ede3b65 518VERSION-STRING is the version of the package, as a string.
4b99edf2
CY
519DOCSTRING is a short description of the package, a string.
520REQUIREMENTS is a list of dependencies on other packages.
7ede3b65
CY
521 Each requirement is of the form (OTHER-PACKAGE OTHER-VERSION),
522 where OTHER-VERSION is a string.
187d3296
CY
523
524EXTRA-PROPERTIES is currently unused."
4b99edf2
CY
525 (let* ((name (intern name-string))
526 (version (version-to-list version-string))
44198b6e
CY
527 (new-pkg-desc
528 (cons name
4b99edf2 529 (vector version
44198b6e
CY
530 (mapcar
531 (lambda (elt)
532 (list (car elt)
148cef8e 533 (version-to-list (car (cdr elt)))))
44198b6e 534 requirements)
4b99edf2
CY
535 docstring)))
536 (old-pkg (assq name package-alist)))
537 (cond
538 ;; If there's no old package, just add this to `package-alist'.
539 ((null old-pkg)
540 (push new-pkg-desc package-alist))
541 ((version-list-< (package-desc-vers (cdr old-pkg)) version)
542 ;; Remove the old package and declare it obsolete.
543 (package-mark-obsolete name (cdr old-pkg))
544 (setq package-alist (cons new-pkg-desc
545 (delq old-pkg package-alist))))
546 ;; You can have two packages with the same version, e.g. one in
547 ;; the system package directory and one in your private
548 ;; directory. We just let the first one win.
549 ((not (version-list-= (package-desc-vers (cdr old-pkg)) version))
550 ;; The package is born obsolete.
551 (package-mark-obsolete name (cdr new-pkg-desc))))))
44198b6e
CY
552
553;; From Emacs 22.
554(defun package-autoload-ensure-default-file (file)
555 "Make sure that the autoload file FILE exists and if not create it."
556 (unless (file-exists-p file)
557 (write-region
558 (concat ";;; " (file-name-nondirectory file)
559 " --- automatically extracted autoloads\n"
560 ";;\n"
561 ";;; Code:\n\n"
562 "\f\n;; Local Variables:\n"
563 ";; version-control: never\n"
564 ";; no-byte-compile: t\n"
565 ";; no-update-autoloads: t\n"
566 ";; End:\n"
567 ";;; " (file-name-nondirectory file)
568 " ends here\n")
569 nil file))
570 file)
571
572(defun package-generate-autoloads (name pkg-dir)
9a70f03d 573 (require 'autoload) ;Load before we let-bind generated-autoload-file!
44198b6e
CY
574 (let* ((auto-name (concat name "-autoloads.el"))
575 (ignore-name (concat name "-pkg.el"))
576 (generated-autoload-file (expand-file-name auto-name pkg-dir))
577 (version-control 'never))
44198b6e
CY
578 (unless (fboundp 'autoload-ensure-default-file)
579 (package-autoload-ensure-default-file generated-autoload-file))
580 (update-directory-autoloads pkg-dir)))
581
4525ce3e
CY
582(defvar tar-parse-info)
583(declare-function tar-untar-buffer "tar-mode" ())
584
585(defun package-untar-buffer (dir)
44198b6e 586 "Untar the current buffer.
4525ce3e
CY
587This uses `tar-untar-buffer' from Tar mode. All files should
588untar into a directory named DIR; otherwise, signal an error."
44198b6e 589 (require 'tar-mode)
4525ce3e
CY
590 (tar-mode)
591 ;; Make sure everything extracts into DIR.
592 (let ((regexp (concat "\\`" (regexp-quote dir) "/")))
593 (dolist (tar-data tar-parse-info)
594 (unless (string-match regexp (aref tar-data 2))
595 (error "Package does not untar cleanly into directory %s/" dir))))
596 (tar-untar-buffer))
44198b6e
CY
597
598(defun package-unpack (name version)
4525ce3e
CY
599 (let* ((dirname (concat (symbol-name name) "-" version))
600 (pkg-dir (expand-file-name dirname package-user-dir)))
44198b6e 601 (make-directory package-user-dir t)
015eea59 602 ;; FIXME: should we delete PKG-DIR if it exists?
44198b6e 603 (let* ((default-directory (file-name-as-directory package-user-dir)))
4525ce3e 604 (package-untar-buffer dirname)
44198b6e
CY
605 (package-generate-autoloads (symbol-name name) pkg-dir)
606 (let ((load-path (cons pkg-dir load-path)))
607 (byte-recompile-directory pkg-dir 0 t)))))
608
c6affbde 609(defun package--write-file-no-coding (file-name)
bc44bef7 610 (let ((buffer-file-coding-system 'no-conversion))
c6affbde 611 (write-region (point-min) (point-max) file-name)))
bc44bef7 612
44198b6e
CY
613(defun package-unpack-single (file-name version desc requires)
614 "Install the contents of the current buffer as a package."
615 ;; Special case "package".
616 (if (string= file-name "package")
bc44bef7 617 (package--write-file-no-coding
c6affbde 618 (expand-file-name (concat file-name ".el") package-user-dir))
ba08b241
CY
619 (let* ((pkg-dir (expand-file-name (concat file-name "-"
620 (package-version-join
621 (version-to-list version)))
44198b6e
CY
622 package-user-dir))
623 (el-file (expand-file-name (concat file-name ".el") pkg-dir))
624 (pkg-file (expand-file-name (concat file-name "-pkg.el") pkg-dir)))
625 (make-directory pkg-dir t)
c6affbde 626 (package--write-file-no-coding el-file)
44198b6e
CY
627 (let ((print-level nil)
628 (print-length nil))
629 (write-region
630 (concat
631 (prin1-to-string
632 (list 'define-package
633 file-name
634 version
635 desc
636 (list 'quote
637 ;; Turn version lists into string form.
638 (mapcar
639 (lambda (elt)
640 (list (car elt)
015eea59 641 (package-version-join (cadr elt))))
44198b6e
CY
642 requires))))
643 "\n")
644 nil
645 pkg-file
646 nil nil nil 'excl))
647 (package-generate-autoloads file-name pkg-dir)
648 (let ((load-path (cons pkg-dir load-path)))
649 (byte-recompile-directory pkg-dir 0 t)))))
650
f561e49a
CY
651(defmacro package--with-work-buffer (location file &rest body)
652 "Run BODY in a buffer containing the contents of FILE at LOCATION.
653LOCATION is the base location of a package archive, and should be
654one of the URLs (or file names) specified in `package-archives'.
655FILE is the name of a file relative to that base location.
656
657This macro retrieves FILE from LOCATION into a temporary buffer,
658and evaluates BODY while that buffer is current. This work
659buffer is killed afterwards. Return the last value in BODY."
da91b5f2 660 `(let* ((http (string-match "\\`https?:" ,location))
f561e49a
CY
661 (buffer
662 (if http
663 (url-retrieve-synchronously (concat ,location ,file))
664 (generate-new-buffer "*package work buffer*"))))
665 (prog1
666 (with-current-buffer buffer
667 (if http
668 (progn (package-handle-response)
669 (re-search-forward "^$" nil 'move)
670 (forward-char)
671 (delete-region (point-min) (point)))
672 (unless (file-name-absolute-p ,location)
673 (error "Archive location %s is not an absolute file name"
674 ,location))
675 (insert-file-contents (expand-file-name ,file ,location)))
676 ,@body)
677 (kill-buffer buffer))))
678
44198b6e 679(defun package-handle-response ()
f561e49a 680 "Handle the response from a `url-retrieve-synchronously' call.
44198b6e
CY
681Parse the HTTP response and throw if an error occurred.
682The url package seems to require extra processing for this.
683This should be called in a `save-excursion', in the download buffer.
684It will move point to somewhere in the headers."
685 ;; We assume HTTP here.
686 (require 'url-http)
687 (let ((response (url-http-parse-response)))
688 (when (or (< response 200) (>= response 300))
44198b6e
CY
689 (error "Error during download request:%s"
690 (buffer-substring-no-properties (point) (progn
691 (end-of-line)
692 (point)))))))
693
694(defun package-download-single (name version desc requires)
695 "Download and install a single-file package."
f561e49a
CY
696 (let ((location (package-archive-base name))
697 (file (concat (symbol-name name) "-" version ".el")))
698 (package--with-work-buffer location file
699 (package-unpack-single (symbol-name name) version desc requires))))
44198b6e
CY
700
701(defun package-download-tar (name version)
702 "Download and install a tar package."
f561e49a
CY
703 (let ((location (package-archive-base name))
704 (file (concat (symbol-name name) "-" version ".tar")))
705 (package--with-work-buffer location file
706 (package-unpack name version))))
44198b6e 707
bc44bef7 708(defun package-installed-p (package &optional min-version)
7ede3b65
CY
709 "Return true if PACKAGE, of MIN-VERSION or newer, is installed.
710MIN-VERSION should be a version list."
44198b6e 711 (let ((pkg-desc (assq package package-alist)))
4b99edf2
CY
712 (if pkg-desc
713 (version-list-<= min-version
714 (package-desc-vers (cdr pkg-desc)))
715 ;; Also check built-in packages.
716 (package-built-in-p package min-version))))
44198b6e 717
96ae4c8f
CY
718(defun package-compute-transaction (package-list requirements)
719 "Return a list of packages to be installed, including PACKAGE-LIST.
720PACKAGE-LIST should be a list of package names (symbols).
721
722REQUIREMENTS should be a list of additional requirements; each
7ede3b65
CY
723element in this list should have the form (PACKAGE VERSION-LIST),
724where PACKAGE is a package name and VERSION-LIST is the required
725version of that package.
96ae4c8f
CY
726
727This function recursively computes the requirements of the
728packages in REQUIREMENTS, and returns a list of all the packages
729that must be installed. Packages that are already installed are
730not included in this list."
44198b6e
CY
731 (dolist (elt requirements)
732 (let* ((next-pkg (car elt))
733 (next-version (cadr elt)))
734 (unless (package-installed-p next-pkg next-version)
735 ;; A package is required, but not installed. It might also be
736 ;; blocked via `package-load-list'.
737 (let ((pkg-desc (assq next-pkg package-archive-contents))
738 hold)
739 (when (setq hold (assq next-pkg package-load-list))
740 (setq hold (cadr hold))
741 (cond ((eq hold nil)
742 (error "Required package '%s' is disabled"
743 (symbol-name next-pkg)))
744 ((null (stringp hold))
745 (error "Invalid element in `package-load-list'"))
148cef8e 746 ((version-list-< (version-to-list hold) next-version)
015eea59 747 (error "Package `%s' held at version %s, \
44198b6e
CY
748but version %s required"
749 (symbol-name next-pkg) hold
750 (package-version-join next-version)))))
751 (unless pkg-desc
015eea59 752 (error "Package `%s-%s' is unavailable"
4b99edf2
CY
753 (symbol-name next-pkg)
754 (package-version-join next-version)))
148cef8e
CY
755 (unless (version-list-<= next-version
756 (package-desc-vers (cdr pkg-desc)))
44198b6e 757 (error
015eea59 758 "Need package `%s-%s', but only %s is available"
44198b6e
CY
759 (symbol-name next-pkg) (package-version-join next-version)
760 (package-version-join (package-desc-vers (cdr pkg-desc)))))
761 ;; Only add to the transaction if we don't already have it.
96ae4c8f 762 (unless (memq next-pkg package-list)
ebf662f4 763 (push next-pkg package-list))
96ae4c8f
CY
764 (setq package-list
765 (package-compute-transaction package-list
44198b6e
CY
766 (package-desc-reqs
767 (cdr pkg-desc))))))))
96ae4c8f 768 package-list)
44198b6e
CY
769
770(defun package-read-from-string (str)
771 "Read a Lisp expression from STR.
772Signal an error if the entire string was not used."
773 (let* ((read-data (read-from-string str))
187d3296
CY
774 (more-left
775 (condition-case nil
776 ;; The call to `ignore' suppresses a compiler warning.
777 (progn (ignore (read-from-string
778 (substring str (cdr read-data))))
779 t)
780 (end-of-file nil))))
44198b6e
CY
781 (if more-left
782 (error "Can't read whole string")
783 (car read-data))))
784
785(defun package--read-archive-file (file)
786 "Re-read archive file FILE, if it exists.
787Will return the data from the file, or nil if the file does not exist.
788Will throw an error if the archive version is too new."
789 (let ((filename (expand-file-name file package-user-dir)))
187d3296
CY
790 (when (file-exists-p filename)
791 (with-temp-buffer
792 (insert-file-contents-literally filename)
793 (let ((contents (read (current-buffer))))
794 (if (> (car contents) package-archive-version)
795 (error "Package archive version %d is higher than %d"
796 (car contents) package-archive-version))
797 (cdr contents))))))
44198b6e 798
bc44bef7 799(defun package-read-all-archive-contents ()
96ae4c8f
CY
800 "Re-read `archive-contents', if it exists.
801If successful, set `package-archive-contents'."
fbe3be3f 802 (setq package-archive-contents nil)
bc44bef7 803 (dolist (archive package-archives)
96ae4c8f 804 (package-read-archive-contents (car archive))))
44198b6e 805
bc44bef7 806(defun package-read-archive-contents (archive)
187d3296
CY
807 "Re-read archive contents for ARCHIVE.
808If successful, set the variable `package-archive-contents'.
bc44bef7 809If the archive version is too new, signal an error."
187d3296
CY
810 ;; Version 1 of 'archive-contents' is identical to our internal
811 ;; representation.
812 (let* ((dir (concat "archives/" archive))
813 (contents-file (concat dir "/archive-contents"))
814 contents)
815 (when (setq contents (package--read-archive-file contents-file))
816 (dolist (package contents)
817 (package--add-to-archive-contents package archive)))))
bc44bef7
PH
818
819(defun package--add-to-archive-contents (package archive)
820 "Add the PACKAGE from the given ARCHIVE if necessary.
821Also, add the originating archive to the end of the package vector."
822 (let* ((name (car package))
25322144
CY
823 (version (package-desc-vers (cdr package)))
824 (entry (cons name
bc44bef7 825 (vconcat (cdr package) (vector archive))))
25322144
CY
826 (existing-package (assq name package-archive-contents)))
827 (cond ((not existing-package)
828 (add-to-list 'package-archive-contents entry))
829 ((version-list-< (package-desc-vers (cdr existing-package))
830 version)
831 ;; Replace the entry with this one.
832 (setq package-archive-contents
833 (cons entry
834 (delq existing-package
835 package-archive-contents)))))))
bc44bef7 836
96ae4c8f
CY
837(defun package-download-transaction (package-list)
838 "Download and install all the packages in PACKAGE-LIST.
839PACKAGE-LIST should be a list of package names (symbols).
840This function assumes that all package requirements in
841PACKAGE-LIST are satisfied, i.e. that PACKAGE-LIST is computed
842using `package-compute-transaction'."
843 (dolist (elt package-list)
44198b6e
CY
844 (let* ((desc (cdr (assq elt package-archive-contents)))
845 ;; As an exception, if package is "held" in
846 ;; `package-load-list', download the held version.
847 (hold (cadr (assq elt package-load-list)))
848 (v-string (or (and (stringp hold) hold)
849 (package-version-join (package-desc-vers desc))))
850 (kind (package-desc-kind desc)))
851 (cond
852 ((eq kind 'tar)
853 (package-download-tar elt v-string))
854 ((eq kind 'single)
855 (package-download-single elt v-string
856 (package-desc-doc desc)
857 (package-desc-reqs desc)))
858 (t
859 (error "Unknown package kind: %s" (symbol-name kind)))))))
860
7254299e
CY
861(defvar package--initialized nil)
862
44198b6e
CY
863;;;###autoload
864(defun package-install (name)
865 "Install the package named NAME.
7254299e
CY
866NAME should be the name of one of the available packages in an
867archive in `package-archives'. Interactively, prompt for NAME."
44198b6e 868 (interactive
7254299e
CY
869 (progn
870 ;; Initialize the package system to get the list of package
871 ;; symbols for completion.
872 (unless package--initialized
873 (package-initialize t))
874 (list (intern (completing-read
875 "Install package: "
876 (mapcar (lambda (elt)
877 (cons (symbol-name (car elt))
878 nil))
879 package-archive-contents)
880 nil t)))))
44198b6e
CY
881 (let ((pkg-desc (assq name package-archive-contents)))
882 (unless pkg-desc
015eea59 883 (error "Package `%s' is not available for installation"
44198b6e 884 (symbol-name name)))
bc44bef7
PH
885 (package-download-transaction
886 (package-compute-transaction (list name)
887 (package-desc-reqs (cdr pkg-desc)))))
44198b6e
CY
888 ;; Try to activate it.
889 (package-initialize))
890
ffbf300e
CY
891(defun package-strip-rcs-id (str)
892 "Strip RCS version ID from the version string STR.
44198b6e
CY
893If the result looks like a dotted numeric version, return it.
894Otherwise return nil."
ffbf300e
CY
895 (when str
896 (when (string-match "\\`[ \t]*[$]Revision:[ \t]+" str)
897 (setq str (substring str (match-end 0))))
898 (condition-case nil
899 (if (version-to-list str)
900 str)
901 (error nil))))
44198b6e
CY
902
903(defun package-buffer-info ()
187d3296
CY
904 "Return a vector describing the package in the current buffer.
905The vector has the form
906
907 [FILENAME REQUIRES DESCRIPTION VERSION COMMENTARY]
908
909FILENAME is the file name, a string, sans the \".el\" extension.
7ede3b65
CY
910REQUIRES is a list of requirements, each requirement having the
911 form (NAME VER); NAME is a string and VER is a version list.
187d3296 912DESCRIPTION is the package description, a string.
44198b6e
CY
913VERSION is the version, a string.
914COMMENTARY is the commentary section, a string, or nil if none.
187d3296
CY
915
916If the buffer does not contain a conforming package, signal an
917error. If there is a package, narrow the buffer to the file's
918boundaries."
44198b6e 919 (goto-char (point-min))
187d3296
CY
920 (unless (re-search-forward "^;;; \\([^ ]*\\)\\.el --- \\(.*\\)$" nil t)
921 (error "Packages lacks a file header"))
922 (let ((file-name (match-string-no-properties 1))
923 (desc (match-string-no-properties 2))
924 (start (line-beginning-position)))
925 (unless (search-forward (concat ";;; " file-name ".el ends here"))
926 (error "Package lacks a terminating comment"))
927 ;; Try to include a trailing newline.
928 (forward-line)
929 (narrow-to-region start (point))
930 (require 'lisp-mnt)
931 ;; Use some headers we've invented to drive the process.
932 (let* ((requires-str (lm-header "package-requires"))
933 (requires (if requires-str
934 (package-read-from-string requires-str)))
935 ;; Prefer Package-Version; if defined, the package author
936 ;; probably wants us to use it. Otherwise try Version.
937 (pkg-version
938 (or (package-strip-rcs-id (lm-header "package-version"))
939 (package-strip-rcs-id (lm-header "version"))))
940 (commentary (lm-commentary)))
941 (unless pkg-version
942 (error
943 "Package lacks a \"Version\" or \"Package-Version\" header"))
944 ;; Turn string version numbers into list form.
945 (setq requires
946 (mapcar
947 (lambda (elt)
948 (list (car elt)
949 (version-to-list (car (cdr elt)))))
950 requires))
951 (vector file-name requires desc pkg-version commentary))))
44198b6e
CY
952
953(defun package-tar-file-info (file)
954 "Find package information for a tar file.
955FILE is the name of the tar file to examine.
956The return result is a vector like `package-buffer-info'."
b511b994
MA
957 (let ((default-directory (file-name-directory file))
958 (file (file-name-nondirectory file)))
4525ce3e
CY
959 (unless (string-match (concat "\\`" package-subdirectory-regexp "\\.tar\\'")
960 file)
b511b994
MA
961 (error "Invalid package name `%s'" file))
962 (let* ((pkg-name (match-string-no-properties 1 file))
963 (pkg-version (match-string-no-properties 2 file))
964 ;; Extract the package descriptor.
965 (pkg-def-contents (shell-command-to-string
966 ;; Requires GNU tar.
967 (concat "tar -xOf " file " "
968
969 pkg-name "-" pkg-version "/"
970 pkg-name "-pkg.el")))
971 (pkg-def-parsed (package-read-from-string pkg-def-contents)))
972 (unless (eq (car pkg-def-parsed) 'define-package)
973 (error "No `define-package' sexp is present in `%s-pkg.el'" pkg-name))
974 (let ((name-str (nth 1 pkg-def-parsed))
975 (version-string (nth 2 pkg-def-parsed))
976 (docstring (nth 3 pkg-def-parsed))
977 (requires (nth 4 pkg-def-parsed))
978 (readme (shell-command-to-string
979 ;; Requires GNU tar.
980 (concat "tar -xOf " file " "
981 pkg-name "-" pkg-version "/README"))))
982 (unless (equal pkg-version version-string)
983 (error "Package has inconsistent versions"))
984 (unless (equal pkg-name name-str)
985 (error "Package has inconsistent names"))
986 ;; Kind of a hack.
987 (if (string-match ": Not found in archive" readme)
988 (setq readme nil))
989 ;; Turn string version numbers into list form.
990 (if (eq (car requires) 'quote)
991 (setq requires (car (cdr requires))))
992 (setq requires
993 (mapcar (lambda (elt)
994 (list (car elt)
995 (version-to-list (cadr elt))))
996 requires))
997 (vector pkg-name requires docstring version-string readme)))))
44198b6e 998
187d3296
CY
999;;;###autoload
1000(defun package-install-from-buffer (pkg-info type)
1001 "Install a package from the current buffer.
1002When called interactively, the current buffer is assumed to be a
1003single .el file that follows the packaging guidelines; see info
1004node `(elisp)Packaging'.
1005
1006When called from Lisp, PKG-INFO is a vector describing the
1007information, of the type returned by `package-buffer-info'; and
1008TYPE is the package type (either `single' or `tar')."
1009 (interactive (list (package-buffer-info) 'single))
44198b6e
CY
1010 (save-excursion
1011 (save-restriction
1012 (let* ((file-name (aref pkg-info 0))
187d3296 1013 (requires (aref pkg-info 1))
44198b6e
CY
1014 (desc (if (string= (aref pkg-info 2) "")
1015 "No description available."
1016 (aref pkg-info 2)))
1017 (pkg-version (aref pkg-info 3)))
1018 ;; Download and install the dependencies.
1019 (let ((transaction (package-compute-transaction nil requires)))
1020 (package-download-transaction transaction))
1021 ;; Install the package itself.
1022 (cond
1023 ((eq type 'single)
1024 (package-unpack-single file-name pkg-version desc requires))
1025 ((eq type 'tar)
1026 (package-unpack (intern file-name) pkg-version))
1027 (t
1028 (error "Unknown type: %s" (symbol-name type))))
1029 ;; Try to activate it.
1030 (package-initialize)))))
1031
44198b6e
CY
1032;;;###autoload
1033(defun package-install-file (file)
1034 "Install a package from a file.
1035The file can either be a tar file or an Emacs Lisp file."
1036 (interactive "fPackage file name: ")
1037 (with-temp-buffer
1038 (insert-file-contents-literally file)
1039 (cond
187d3296
CY
1040 ((string-match "\\.el$" file)
1041 (package-install-from-buffer (package-buffer-info) 'single))
44198b6e 1042 ((string-match "\\.tar$" file)
187d3296 1043 (package-install-from-buffer (package-tar-file-info file) 'tar))
44198b6e
CY
1044 (t (error "Unrecognized extension `%s'" (file-name-extension file))))))
1045
1046(defun package-delete (name version)
015eea59
CY
1047 (let ((dir (package--dir name version)))
1048 (if (string-equal (file-name-directory dir)
1049 (file-name-as-directory
1050 (expand-file-name package-user-dir)))
1051 (progn
1052 (delete-directory dir t t)
1053 (message "Package `%s-%s' deleted." name version))
1054 ;; Don't delete "system" packages
1055 (error "Package `%s-%s' is a system package, not deleting"
1056 name version))))
44198b6e 1057
f561e49a 1058(defun package-archive-base (name)
bc44bef7
PH
1059 "Return the archive containing the package NAME."
1060 (let ((desc (cdr (assq (intern-soft name) package-archive-contents))))
1061 (cdr (assoc (aref desc (- (length desc) 1)) package-archives))))
1062
1063(defun package--download-one-archive (archive file)
f561e49a
CY
1064 "Retrieve an archive file FILE from ARCHIVE, and cache it.
1065ARCHIVE should be a cons cell of the form (NAME . LOCATION),
1066similar to an entry in `package-alist'. Save the cached copy to
1067\"archives/NAME/archive-contents\" in `package-user-dir'."
1068 (let* ((dir (expand-file-name "archives" package-user-dir))
1069 (dir (expand-file-name (car archive) dir)))
1070 (package--with-work-buffer (cdr archive) file
ebf662f4
CY
1071 ;; Read the retrieved buffer to make sure it is valid (e.g. it
1072 ;; may fetch a URL redirect page).
1073 (when (listp (read buffer))
1074 (make-directory dir t)
1075 (setq buffer-file-name (expand-file-name file dir))
1076 (let ((version-control 'never))
f561e49a 1077 (save-buffer))))))
44198b6e
CY
1078
1079(defun package-refresh-contents ()
1080 "Download the ELPA archive description if needed.
ebf662f4
CY
1081This informs Emacs about the latest versions of all packages, and
1082makes them available for download."
44198b6e
CY
1083 (interactive)
1084 (unless (file-exists-p package-user-dir)
1085 (make-directory package-user-dir t))
bc44bef7 1086 (dolist (archive package-archives)
515de2e3 1087 (condition-case-no-debug nil
cb6c4991 1088 (package--download-one-archive archive "archive-contents")
187d3296 1089 (error (message "Failed to download `%s' archive."
cb6c4991 1090 (car archive)))))
bc44bef7 1091 (package-read-all-archive-contents))
44198b6e
CY
1092
1093;;;###autoload
4b99edf2 1094(defun package-initialize (&optional no-activate)
44198b6e 1095 "Load Emacs Lisp packages, and activate them.
4b99edf2
CY
1096The variable `package-load-list' controls which packages to load.
1097If optional arg NO-ACTIVATE is non-nil, don't activate packages."
44198b6e 1098 (interactive)
015eea59
CY
1099 (setq package-alist nil
1100 package-obsolete-alist nil)
44198b6e 1101 (package-load-all-descriptors)
bc44bef7 1102 (package-read-all-archive-contents)
4b99edf2
CY
1103 (unless no-activate
1104 (dolist (elt package-alist)
1105 (package-activate (car elt) (package-desc-vers (cdr elt)))))
1106 (setq package--initialized t))
44198b6e
CY
1107
1108\f
cced7584 1109;;;; Package description buffer.
44198b6e 1110
cced7584
CY
1111;;;###autoload
1112(defun describe-package (package)
1113 "Display the full documentation of PACKAGE (a symbol)."
1114 (interactive
8a500a91
CY
1115 (let* ((guess (function-called-at-point))
1116 packages val)
4b99edf2
CY
1117 (require 'finder-inf nil t)
1118 ;; Load the package list if necessary (but don't activate them).
1119 (unless package--initialized
1120 (package-initialize t))
8a500a91 1121 (setq packages (append (mapcar 'car package-alist)
4b99edf2
CY
1122 (mapcar 'car package-archive-contents)
1123 (mapcar 'car package--builtins)))
cced7584
CY
1124 (unless (memq guess packages)
1125 (setq guess nil))
1126 (setq packages (mapcar 'symbol-name packages))
1127 (setq val
1128 (completing-read (if guess
1129 (format "Describe package (default %s): "
1130 guess)
1131 "Describe package: ")
1132 packages nil t nil nil guess))
cb6c4991 1133 (list (if (equal val "") guess (intern val)))))
4b99edf2
CY
1134 (if (or (null package) (not (symbolp package)))
1135 (message "No package specified")
cced7584
CY
1136 (help-setup-xref (list #'describe-package package)
1137 (called-interactively-p 'interactive))
1138 (with-help-window (help-buffer)
1139 (with-current-buffer standard-output
1140 (describe-package-1 package)))))
1141
1142(defun describe-package-1 (package)
96ae4c8f 1143 (require 'lisp-mnt)
cb6c4991
CY
1144 (let ((package-name (symbol-name package))
1145 (built-in (assq package package--builtins))
1146 desc pkg-dir reqs version installable)
cced7584
CY
1147 (prin1 package)
1148 (princ " is ")
4b99edf2
CY
1149 (cond
1150 ;; Loaded packages are in `package-alist'.
1151 ((setq desc (cdr (assq package package-alist)))
1152 (setq version (package-version-join (package-desc-vers desc)))
1153 (if (setq pkg-dir (package--dir package-name version))
1154 (insert "an installed package.\n\n")
1155 ;; This normally does not happen.
1156 (insert "a deleted package.\n\n")))
1157 ;; Available packages are in `package-archive-contents'.
1158 ((setq desc (cdr (assq package package-archive-contents)))
1159 (setq version (package-version-join (package-desc-vers desc))
8adb4c33 1160 installable t)
4b99edf2
CY
1161 (if built-in
1162 (insert "a built-in package.\n\n")
1163 (insert "an uninstalled package.\n\n")))
1164 (built-in
1165 (setq desc (cdr built-in)
1166 version (package-version-join (package-desc-vers desc)))
1167 (insert "a built-in package.\n\n"))
1168 (t
1169 (insert "an orphan package.\n\n")))
cb6c4991 1170
96ae4c8f 1171 (insert " " (propertize "Status" 'font-lock-face 'bold) ": ")
cb6c4991 1172 (cond (pkg-dir
96ae4c8f
CY
1173 (insert (propertize "Installed"
1174 'font-lock-face 'font-lock-comment-face))
cb6c4991
CY
1175 (insert " in `")
1176 ;; Todo: Add button for uninstalling.
1177 (help-insert-xref-button (file-name-as-directory pkg-dir)
1178 'help-package-def pkg-dir)
4b99edf2
CY
1179 (if built-in
1180 (insert "',\n shadowing a "
1181 (propertize "built-in package"
1182 'font-lock-face 'font-lock-builtin-face)
1183 ".")
1184 (insert "'.")))
cb6c4991 1185 (installable
4b99edf2
CY
1186 (if built-in
1187 (insert (propertize "Built-in." 'font-lock-face 'font-lock-builtin-face)
1188 " Alternate version available -- ")
1189 (insert "Available -- "))
1190 (let ((button-text (if (display-graphic-p) "Install" "[Install]"))
cb6c4991
CY
1191 (button-face (if (display-graphic-p)
1192 '(:box (:line-width 2 :color "dark grey")
1193 :background "light grey"
1194 :foreground "black")
1195 'link)))
4b99edf2 1196 (insert-text-button button-text 'face button-face 'follow-link t
cb6c4991
CY
1197 'package-symbol package
1198 'action 'package-install-button-action)))
1199 (built-in
4b99edf2 1200 (insert (propertize "Built-in." 'font-lock-face 'font-lock-builtin-face)))
cb6c4991
CY
1201 (t (insert "Deleted.")))
1202 (insert "\n")
4b99edf2 1203 (and version (> (length version) 0)
96ae4c8f
CY
1204 (insert " "
1205 (propertize "Version" 'font-lock-face 'bold) ": " version "\n"))
4b99edf2
CY
1206
1207 (setq reqs (if desc (package-desc-reqs desc)))
8adb4c33 1208 (when reqs
96ae4c8f 1209 (insert " " (propertize "Requires" 'font-lock-face 'bold) ": ")
8adb4c33
CY
1210 (let ((first t)
1211 name vers text)
1212 (dolist (req reqs)
1213 (setq name (car req)
1214 vers (cadr req)
1215 text (format "%s-%s" (symbol-name name)
1216 (package-version-join vers)))
1217 (cond (first (setq first nil))
1218 ((>= (+ 2 (current-column) (length text))
1219 (window-width))
1220 (insert ",\n "))
1221 (t (insert ", ")))
1222 (help-insert-xref-button text 'help-package name))
1223 (insert "\n")))
96ae4c8f 1224 (insert " " (propertize "Summary" 'font-lock-face 'bold)
4b99edf2 1225 ": " (if desc (package-desc-doc desc)) "\n\n")
cb6c4991 1226
4b99edf2 1227 (if built-in
96ae4c8f
CY
1228 ;; For built-in packages, insert the commentary.
1229 (let ((fn (locate-file (concat package-name ".el") load-path
1230 load-file-rep-suffixes))
1231 (opoint (point)))
1232 (insert (or (lm-commentary fn) ""))
1233 (save-excursion
1234 (goto-char opoint)
1235 (when (re-search-forward "^;;; Commentary:\n" nil t)
1236 (replace-match ""))
1237 (while (re-search-forward "^\\(;+ ?\\)" nil t)
1238 (replace-match ""))))
1239 (let ((readme (expand-file-name (concat package-name "-readme.txt")
f561e49a
CY
1240 package-user-dir))
1241 readme-string)
96ae4c8f
CY
1242 ;; For elpa packages, try downloading the commentary. If that
1243 ;; fails, try an existing readme file in `package-user-dir'.
f561e49a
CY
1244 (cond ((condition-case nil
1245 (package--with-work-buffer (package-archive-base package)
1246 (concat package-name "-readme.txt")
1247 (setq buffer-file-name
1248 (expand-file-name readme package-user-dir))
1249 (let ((version-control 'never))
1250 (save-buffer))
1251 (setq readme-string (buffer-string))
1252 t)
1253 (error nil))
1254 (insert readme-string))
96ae4c8f
CY
1255 ((file-readable-p readme)
1256 (insert-file-contents readme)
1257 (goto-char (point-max))))))))
cb6c4991
CY
1258
1259(defun package-install-button-action (button)
1260 (let ((package (button-get button 'package-symbol)))
1261 (when (y-or-n-p (format "Install package `%s'? " package))
1262 (package-install package)
1263 (revert-buffer nil t)
1264 (goto-char (point-min)))))
cced7584
CY
1265
1266\f
44198b6e
CY
1267;;;; Package menu mode.
1268
54ea2a0d 1269(defvar package-menu-mode-map
e91a96fe 1270 (let ((map (make-sparse-keymap))
64eba874 1271 (menu-map (make-sparse-keymap "Package")))
e91a96fe 1272 (set-keymap-parent map tabulated-list-mode-map)
8adb4c33 1273 (define-key map "\C-m" 'package-menu-describe-package)
54ea2a0d
JB
1274 (define-key map "u" 'package-menu-mark-unmark)
1275 (define-key map "\177" 'package-menu-backup-unmark)
1276 (define-key map "d" 'package-menu-mark-delete)
1277 (define-key map "i" 'package-menu-mark-install)
25322144 1278 (define-key map "U" 'package-menu-mark-upgrades)
54ea2a0d
JB
1279 (define-key map "r" 'package-menu-refresh)
1280 (define-key map "~" 'package-menu-mark-obsolete-for-deletion)
1281 (define-key map "x" 'package-menu-execute)
1282 (define-key map "h" 'package-menu-quick-help)
cb6c4991 1283 (define-key map "?" 'package-menu-describe-package)
64eba874
DN
1284 (define-key map [menu-bar package-menu] (cons "Package" menu-map))
1285 (define-key menu-map [mq]
1286 '(menu-item "Quit" quit-window
1287 :help "Quit package selection"))
1288 (define-key menu-map [s1] '("--"))
1289 (define-key menu-map [mn]
1290 '(menu-item "Next" next-line
1291 :help "Next Line"))
1292 (define-key menu-map [mp]
1293 '(menu-item "Previous" previous-line
1294 :help "Previous Line"))
1295 (define-key menu-map [s2] '("--"))
1296 (define-key menu-map [mu]
1297 '(menu-item "Unmark" package-menu-mark-unmark
1298 :help "Clear any marks on a package and move to the next line"))
1299 (define-key menu-map [munm]
7cc6e154 1300 '(menu-item "Unmark Backwards" package-menu-backup-unmark
64eba874
DN
1301 :help "Back up one line and clear any marks on that package"))
1302 (define-key menu-map [md]
7cc6e154 1303 '(menu-item "Mark for Deletion" package-menu-mark-delete
64eba874
DN
1304 :help "Mark a package for deletion and move to the next line"))
1305 (define-key menu-map [mi]
7cc6e154 1306 '(menu-item "Mark for Install" package-menu-mark-install
64eba874 1307 :help "Mark a package for installation and move to the next line"))
d770725a 1308 (define-key menu-map [mupgrades]
7cc6e154 1309 '(menu-item "Mark Upgradable Packages" package-menu-mark-upgrades
d770725a 1310 :help "Mark packages that have a newer version for upgrading"))
64eba874
DN
1311 (define-key menu-map [s3] '("--"))
1312 (define-key menu-map [mg]
7cc6e154 1313 '(menu-item "Update Package List" revert-buffer
64eba874
DN
1314 :help "Update the list of packages"))
1315 (define-key menu-map [mr]
7cc6e154 1316 '(menu-item "Refresh Package List" package-menu-refresh
64eba874
DN
1317 :help "Download the ELPA archive"))
1318 (define-key menu-map [s4] '("--"))
1319 (define-key menu-map [mt]
7cc6e154 1320 '(menu-item "Mark Obsolete Packages" package-menu-mark-obsolete-for-deletion
64eba874
DN
1321 :help "Mark all obsolete packages for deletion"))
1322 (define-key menu-map [mx]
7cc6e154 1323 '(menu-item "Execute Actions" package-menu-execute
64eba874
DN
1324 :help "Perform all the marked actions"))
1325 (define-key menu-map [s5] '("--"))
1326 (define-key menu-map [mh]
1327 '(menu-item "Help" package-menu-quick-help
1328 :help "Show short key binding help for package-menu-mode"))
1329 (define-key menu-map [mc]
1330 '(menu-item "View Commentary" package-menu-view-commentary
1331 :help "Display information about this package"))
54ea2a0d 1332 map)
44198b6e
CY
1333 "Local keymap for `package-menu-mode' buffers.")
1334
e91a96fe 1335(define-derived-mode package-menu-mode tabulated-list-mode "Package Menu"
44198b6e
CY
1336 "Major mode for browsing a list of packages.
1337Letters do not insert themselves; instead, they are commands.
1338\\<package-menu-mode-map>
1339\\{package-menu-mode-map}"
e91a96fe
CY
1340 (setq tabulated-list-format [("Package" 18 package-menu--name-predicate)
1341 ("Version" 12 nil)
1342 ("Status" 10 package-menu--status-predicate)
1343 ("Description" 0 nil)])
1344 (setq tabulated-list-padding 2)
1345 (setq tabulated-list-sort-key (cons "Status" nil))
1346 (tabulated-list-init-header))
1347
1348(defmacro package--push (package desc status listname)
1349 "Convenience macro for `package-menu--generate'.
1350If the alist stored in the symbol LISTNAME lacks an entry for a
1351package PACKAGE with descriptor DESC, add one. The alist is
7ede3b65
CY
1352keyed with cons cells (PACKAGE . VERSION-LIST), where PACKAGE is
1353a symbol and VERSION-LIST is a version list."
e91a96fe
CY
1354 `(let* ((version (package-desc-vers ,desc))
1355 (key (cons ,package version)))
1356 (unless (assoc key ,listname)
1357 (push (list key ,status (package-desc-doc ,desc)) ,listname))))
1358
512e3ae1 1359(defun package-menu--generate (remember-pos packages)
e91a96fe 1360 "Populate the Package Menu.
512e3ae1
CY
1361If REMEMBER-POS is non-nil, keep point on the same entry.
1362PACKAGES should be t, which means to display all known packages,
1363or a list of package names (symbols) to display."
e91a96fe
CY
1364 ;; Construct list of ((PACKAGE . VERSION) STATUS DESCRIPTION).
1365 (let (info-list name builtin)
1366 ;; Installed packages:
1367 (dolist (elt package-alist)
1368 (setq name (car elt))
512e3ae1
CY
1369 (when (or (eq packages t) (memq name packages))
1370 (package--push name (cdr elt)
1371 (if (stringp (cadr (assq name package-load-list)))
1372 "held" "installed")
1373 info-list)))
e91a96fe
CY
1374
1375 ;; Built-in packages:
1376 (dolist (elt package--builtins)
1377 (setq name (car elt))
512e3ae1
CY
1378 (when (and (not (eq name 'emacs)) ; Hide the `emacs' package.
1379 (or (eq packages t) (memq name packages)))
e91a96fe
CY
1380 (package--push name (cdr elt) "built-in" info-list)))
1381
1382 ;; Available and disabled packages:
1383 (dolist (elt package-archive-contents)
1384 (setq name (car elt))
512e3ae1
CY
1385 (when (or (eq packages t) (memq name packages))
1386 (let ((hold (assq name package-load-list)))
1387 (package--push name (cdr elt)
1388 (if (and hold (null (cadr hold)))
1389 "disabled"
1390 "available")
1391 info-list))))
e91a96fe
CY
1392
1393 ;; Obsolete packages:
1394 (dolist (elt package-obsolete-alist)
1395 (dolist (inner-elt (cdr elt))
512e3ae1
CY
1396 (when (or (eq packages t) (memq (car elt) packages))
1397 (package--push (car elt) (cdr inner-elt) "obsolete" info-list))))
e91a96fe
CY
1398
1399 ;; Print the result.
1400 (setq tabulated-list-entries (mapcar 'package-menu--print-info info-list))
1401 (tabulated-list-print remember-pos)))
1402
1403(defun package-menu--print-info (pkg)
1404 "Return a package entry suitable for `tabulated-list-entries'.
1405PKG has the form ((PACKAGE . VERSION) STATUS DOC).
1406Return (KEY [NAME VERSION STATUS DOC]), where KEY is the
1407identifier (NAME . VERSION-LIST)."
1408 (let* ((package (caar pkg))
1409 (version (cdr (car pkg)))
1410 (status (nth 1 pkg))
1411 (doc (or (nth 2 pkg) ""))
1412 (face (cond
1413 ((string= status "built-in") 'font-lock-builtin-face)
1414 ((string= status "available") 'default)
1415 ((string= status "held") 'font-lock-constant-face)
1416 ((string= status "disabled") 'font-lock-warning-face)
1417 ((string= status "installed") 'font-lock-comment-face)
1418 (t 'font-lock-warning-face)))) ; obsolete.
1419 (list (cons package version)
1420 (vector (list (symbol-name package)
1421 'face 'link
1422 'follow-link t
1423 'package-symbol package
1424 'action 'package-menu-describe-package)
1425 (propertize (package-version-join version)
1426 'font-lock-face face)
1427 (propertize status 'font-lock-face face)
1428 (propertize doc 'font-lock-face face)))))
44198b6e
CY
1429
1430(defun package-menu-refresh ()
ebf662f4
CY
1431 "Download the Emacs Lisp package archive.
1432This fetches the contents of each archive specified in
1433`package-archives', and then refreshes the package menu."
44198b6e 1434 (interactive)
25322144 1435 (unless (derived-mode-p 'package-menu-mode)
187d3296 1436 (error "The current buffer is not a Package Menu"))
44198b6e 1437 (package-refresh-contents)
512e3ae1 1438 (package-menu--generate t t))
44198b6e 1439
e91a96fe
CY
1440(defun package-menu-describe-package (&optional button)
1441 "Describe the current package.
1442If optional arg BUTTON is non-nil, describe its associated package."
44198b6e 1443 (interactive)
e91a96fe
CY
1444 (let ((package (if button (button-get button 'package-symbol)
1445 (car (tabulated-list-get-id)))))
1446 (if package
1447 (describe-package package))))
44198b6e
CY
1448
1449;; fixme numeric argument
25322144 1450(defun package-menu-mark-delete (&optional num)
44198b6e
CY
1451 "Mark a package for deletion and move to the next line."
1452 (interactive "p")
fb87e0fb 1453 (if (member (package-menu-get-status) '("installed" "obsolete"))
e91a96fe 1454 (tabulated-list-put-tag "D" t)
015eea59 1455 (forward-line)))
44198b6e 1456
25322144 1457(defun package-menu-mark-install (&optional num)
44198b6e
CY
1458 "Mark a package for installation and move to the next line."
1459 (interactive "p")
015eea59 1460 (if (string-equal (package-menu-get-status) "available")
e91a96fe 1461 (tabulated-list-put-tag "I" t)
015eea59 1462 (forward-line)))
44198b6e 1463
25322144 1464(defun package-menu-mark-unmark (&optional num)
44198b6e
CY
1465 "Clear any marks on a package and move to the next line."
1466 (interactive "p")
e91a96fe 1467 (tabulated-list-put-tag " " t))
44198b6e
CY
1468
1469(defun package-menu-backup-unmark ()
1470 "Back up one line and clear any marks on that package."
1471 (interactive)
1472 (forward-line -1)
e91a96fe 1473 (tabulated-list-put-tag " "))
44198b6e
CY
1474
1475(defun package-menu-mark-obsolete-for-deletion ()
1476 "Mark all obsolete packages for deletion."
1477 (interactive)
1478 (save-excursion
1479 (goto-char (point-min))
44198b6e 1480 (while (not (eobp))
25322144 1481 (if (equal (package-menu-get-status) "obsolete")
e91a96fe 1482 (tabulated-list-put-tag "D" t)
44198b6e
CY
1483 (forward-line 1)))))
1484
1485(defun package-menu-quick-help ()
1486 "Show short key binding help for package-menu-mode."
1487 (interactive)
1488 (message "n-ext, i-nstall, d-elete, u-nmark, x-ecute, r-efresh, h-elp"))
1489
cb6c4991
CY
1490(define-obsolete-function-alias
1491 'package-menu-view-commentary 'package-menu-describe-package "24.1")
44198b6e 1492
44198b6e 1493(defun package-menu-get-status ()
25322144
CY
1494 (let* ((pkg (tabulated-list-get-id))
1495 (entry (and pkg (assq pkg tabulated-list-entries))))
1496 (if entry
1497 (aref (cadr entry) 2)
44198b6e
CY
1498 "")))
1499
25322144
CY
1500(defun package-menu--find-upgrades ()
1501 (let (installed available upgrades)
1502 ;; Build list of installed/available packages in this buffer.
1503 (dolist (entry tabulated-list-entries)
1504 ;; ENTRY is ((NAME . VERSION) [NAME VERSION STATUS DOC])
1505 (let ((pkg (car entry))
1506 (status (aref (cadr entry) 2))
1507 old)
1508 (cond ((equal status "installed")
1509 (push pkg installed))
1510 ((equal status "available")
1511 (push pkg available)))))
1512 ;; Loop through list of installed packages, finding upgrades
1513 (dolist (pkg installed)
1514 (let ((avail-pkg (assq (car pkg) available)))
1515 (and avail-pkg
1516 (version-list-< (cdr pkg) (cdr avail-pkg))
1517 (push avail-pkg upgrades))))
1518 upgrades))
1519
1520(defun package-menu-mark-upgrades ()
1521 "Mark all upgradable packages in the Package Menu.
1522For each installed package with a newer version available, place
1523an (I)nstall flag on the available version and a (D)elete flag on
1524the installed version. A subsequent \\[package-menu-execute]
1525call will upgrade the package."
1526 (interactive)
1527 (unless (derived-mode-p 'package-menu-mode)
1528 (error "The current buffer is not a Package Menu"))
1529 (let ((upgrades (package-menu--find-upgrades)))
1530 (if (null upgrades)
1531 (message "No packages to upgrade.")
1532 (widen)
1533 (save-excursion
1534 (goto-char (point-min))
1535 (while (not (eobp))
1536 (let* ((pkg (tabulated-list-get-id))
1537 (upgrade (assq (car pkg) upgrades)))
1538 (cond ((null upgrade)
1539 (forward-line 1))
1540 ((equal pkg upgrade)
1541 (package-menu-mark-install))
1542 (t
1543 (package-menu-mark-delete))))))
1544 (message "%d package%s marked for upgrading."
1545 (length upgrades)
1546 (if (= (length upgrades) 1) "" "s")))))
1547
44198b6e 1548(defun package-menu-execute ()
015eea59
CY
1549 "Perform marked Package Menu actions.
1550Packages marked for installation are downloaded and installed;
1551packages marked for deletion are removed."
44198b6e 1552 (interactive)
25322144 1553 (unless (derived-mode-p 'package-menu-mode)
e91a96fe
CY
1554 (error "The current buffer is not in Package Menu mode"))
1555 (let (install-list delete-list cmd id)
015eea59
CY
1556 (save-excursion
1557 (goto-char (point-min))
1558 (while (not (eobp))
1559 (setq cmd (char-after))
e91a96fe
CY
1560 (unless (eq cmd ?\s)
1561 ;; This is the key (PACKAGE . VERSION-LIST).
1562 (setq id (tabulated-list-get-id))
1563 (cond ((eq cmd ?D)
1564 (push (cons (symbol-name (car id))
1565 (package-version-join (cdr id)))
1566 delete-list))
1567 ((eq cmd ?I)
1568 (push (car id) install-list))))
015eea59 1569 (forward-line)))
25322144
CY
1570 (when install-list
1571 (if (yes-or-no-p
1572 (if (= (length install-list) 1)
1573 (format "Install package `%s'? " (car install-list))
1574 (format "Install these %d packages (%s)? "
1575 (length install-list)
1576 (mapconcat 'symbol-name install-list ", "))))
1577 (mapc 'package-install install-list)))
015eea59
CY
1578 ;; Delete packages, prompting if necessary.
1579 (when delete-list
1580 (if (yes-or-no-p
1581 (if (= (length delete-list) 1)
1582 (format "Delete package `%s-%s'? "
1583 (caar delete-list)
1584 (cdr (car delete-list)))
1585 (format "Delete these %d packages (%s)? "
1586 (length delete-list)
1587 (mapconcat (lambda (elt)
1588 (concat (car elt) "-" (cdr elt)))
1589 delete-list
1590 ", "))))
1591 (dolist (elt delete-list)
515de2e3 1592 (condition-case-no-debug err
015eea59
CY
1593 (package-delete (car elt) (cdr elt))
1594 (error (message (cadr err)))))
1595 (error "Aborted")))
015eea59
CY
1596 ;; If we deleted anything, regenerate `package-alist'. This is done
1597 ;; automatically if we installed a package.
1598 (and delete-list (null install-list)
1599 (package-initialize))
1600 (if (or delete-list install-list)
512e3ae1 1601 (package-menu--generate t t)
015eea59 1602 (message "No operations specified."))))
44198b6e 1603
e91a96fe
CY
1604(defun package-menu--version-predicate (A B)
1605 (let ((vA (or (aref (cadr A) 1) '(0)))
1606 (vB (or (aref (cadr B) 1) '(0))))
1607 (if (version-list-= vA vB)
1608 (package-menu--name-predicate A B)
1609 (version-list-< vA vB))))
1610
1611(defun package-menu--status-predicate (A B)
1612 (let ((sA (aref (cadr A) 2))
1613 (sB (aref (cadr B) 2)))
1614 (cond ((string= sA sB)
1615 (package-menu--name-predicate A B))
1616 ((string= sA "available") t)
1617 ((string= sB "available") nil)
1618 ((string= sA "installed") t)
1619 ((string= sB "installed") nil)
1620 ((string= sA "held") t)
1621 ((string= sB "held") nil)
1622 ((string= sA "built-in") t)
1623 ((string= sB "built-in") nil)
1624 ((string= sA "obsolete") t)
1625 ((string= sB "obsolete") nil)
1626 (t (string< sA sB)))))
1627
1628(defun package-menu--description-predicate (A B)
1629 (let ((dA (aref (cadr A) 3))
1630 (dB (aref (cadr B) 3)))
1631 (if (string= dA dB)
1632 (package-menu--name-predicate A B)
1633 (string< dA dB))))
1634
1635(defun package-menu--name-predicate (A B)
1636 (string< (symbol-name (caar A))
1637 (symbol-name (caar B))))
44198b6e
CY
1638
1639;;;###autoload
e91a96fe 1640(defun list-packages (&optional no-fetch)
44198b6e 1641 "Display a list of packages.
e91a96fe
CY
1642This first fetches the updated list of packages before
1643displaying, unless a prefix argument NO-FETCH is specified.
44198b6e 1644The list is displayed in a buffer named `*Packages*'."
e91a96fe
CY
1645 (interactive "P")
1646 (require 'finder-inf nil t)
8a500a91 1647 ;; Initialize the package system if necessary.
4b99edf2
CY
1648 (unless package--initialized
1649 (package-initialize t))
e91a96fe
CY
1650 (unless no-fetch
1651 (package-refresh-contents))
1652 (let ((buf (get-buffer-create "*Packages*")))
1653 (with-current-buffer buf
1654 (package-menu-mode)
512e3ae1 1655 (package-menu--generate nil t))
e91a96fe
CY
1656 ;; The package menu buffer has keybindings. If the user types
1657 ;; `M-x list-packages', that suggests it should become current.
25322144
CY
1658 (switch-to-buffer buf))
1659 (let ((upgrades (package-menu--find-upgrades)))
1660 (if upgrades
502f9ebd 1661 (message "%d package%s can be upgraded; type `%s' to mark %s for upgrading."
25322144
CY
1662 (length upgrades)
1663 (if (= (length upgrades) 1) "" "s")
502f9ebd
CY
1664 (substitute-command-keys "\\[package-menu-mark-upgrades]")
1665 (if (= (length upgrades) 1) "it" "them")))))
44198b6e 1666
cb8759ca 1667;;;###autoload
96ae4c8f 1668(defalias 'package-list-packages 'list-packages)
cb8759ca 1669
512e3ae1
CY
1670;; Used in finder.el
1671(defun package-show-package-list (packages)
1672 "Display PACKAGES in a *Packages* buffer.
1673This is similar to `list-packages', but it does not fetch the
1674updated list of packages, and it only displays packages with
1675names in PACKAGES (which should be a list of symbols)."
1676 (require 'finder-inf nil t)
1677 (let ((buf (get-buffer-create "*Packages*")))
1678 (with-current-buffer buf
1679 (package-menu-mode)
1680 (package-menu--generate nil packages))
1681 (switch-to-buffer buf)))
1682
44198b6e
CY
1683(defun package-list-packages-no-fetch ()
1684 "Display a list of packages.
1685Does not fetch the updated list of packages before displaying.
1686The list is displayed in a buffer named `*Packages*'."
1687 (interactive)
e91a96fe 1688 (list-packages t))
44198b6e 1689
44198b6e
CY
1690(provide 'package)
1691
1692;;; package.el ends here