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