message.el (message-read-from-minibuffer): Make abbrev expansion work.
[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'.")
bc44bef7 312(put 'package-archive-contents '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))
823 (version (aref (cdr package) 0))
824 (entry (cons (car package)
825 (vconcat (cdr package) (vector archive))))
826 (existing-package (cdr (assq name package-archive-contents))))
827 (when (or (not existing-package)
148cef8e 828 (version-list-< (aref existing-package 0) version))
bc44bef7
PH
829 (add-to-list 'package-archive-contents entry))))
830
96ae4c8f
CY
831(defun package-download-transaction (package-list)
832 "Download and install all the packages in PACKAGE-LIST.
833PACKAGE-LIST should be a list of package names (symbols).
834This function assumes that all package requirements in
835PACKAGE-LIST are satisfied, i.e. that PACKAGE-LIST is computed
836using `package-compute-transaction'."
837 (dolist (elt package-list)
44198b6e
CY
838 (let* ((desc (cdr (assq elt package-archive-contents)))
839 ;; As an exception, if package is "held" in
840 ;; `package-load-list', download the held version.
841 (hold (cadr (assq elt package-load-list)))
842 (v-string (or (and (stringp hold) hold)
843 (package-version-join (package-desc-vers desc))))
844 (kind (package-desc-kind desc)))
845 (cond
846 ((eq kind 'tar)
847 (package-download-tar elt v-string))
848 ((eq kind 'single)
849 (package-download-single elt v-string
850 (package-desc-doc desc)
851 (package-desc-reqs desc)))
852 (t
853 (error "Unknown package kind: %s" (symbol-name kind)))))))
854
7254299e
CY
855(defvar package--initialized nil)
856
44198b6e
CY
857;;;###autoload
858(defun package-install (name)
859 "Install the package named NAME.
7254299e
CY
860NAME should be the name of one of the available packages in an
861archive in `package-archives'. Interactively, prompt for NAME."
44198b6e 862 (interactive
7254299e
CY
863 (progn
864 ;; Initialize the package system to get the list of package
865 ;; symbols for completion.
866 (unless package--initialized
867 (package-initialize t))
868 (list (intern (completing-read
869 "Install package: "
870 (mapcar (lambda (elt)
871 (cons (symbol-name (car elt))
872 nil))
873 package-archive-contents)
874 nil t)))))
44198b6e
CY
875 (let ((pkg-desc (assq name package-archive-contents)))
876 (unless pkg-desc
015eea59 877 (error "Package `%s' is not available for installation"
44198b6e 878 (symbol-name name)))
bc44bef7
PH
879 (package-download-transaction
880 (package-compute-transaction (list name)
881 (package-desc-reqs (cdr pkg-desc)))))
44198b6e
CY
882 ;; Try to activate it.
883 (package-initialize))
884
ffbf300e
CY
885(defun package-strip-rcs-id (str)
886 "Strip RCS version ID from the version string STR.
44198b6e
CY
887If the result looks like a dotted numeric version, return it.
888Otherwise return nil."
ffbf300e
CY
889 (when str
890 (when (string-match "\\`[ \t]*[$]Revision:[ \t]+" str)
891 (setq str (substring str (match-end 0))))
892 (condition-case nil
893 (if (version-to-list str)
894 str)
895 (error nil))))
44198b6e
CY
896
897(defun package-buffer-info ()
187d3296
CY
898 "Return a vector describing the package in the current buffer.
899The vector has the form
900
901 [FILENAME REQUIRES DESCRIPTION VERSION COMMENTARY]
902
903FILENAME is the file name, a string, sans the \".el\" extension.
7ede3b65
CY
904REQUIRES is a list of requirements, each requirement having the
905 form (NAME VER); NAME is a string and VER is a version list.
187d3296 906DESCRIPTION is the package description, a string.
44198b6e
CY
907VERSION is the version, a string.
908COMMENTARY is the commentary section, a string, or nil if none.
187d3296
CY
909
910If the buffer does not contain a conforming package, signal an
911error. If there is a package, narrow the buffer to the file's
912boundaries."
44198b6e 913 (goto-char (point-min))
187d3296
CY
914 (unless (re-search-forward "^;;; \\([^ ]*\\)\\.el --- \\(.*\\)$" nil t)
915 (error "Packages lacks a file header"))
916 (let ((file-name (match-string-no-properties 1))
917 (desc (match-string-no-properties 2))
918 (start (line-beginning-position)))
919 (unless (search-forward (concat ";;; " file-name ".el ends here"))
920 (error "Package lacks a terminating comment"))
921 ;; Try to include a trailing newline.
922 (forward-line)
923 (narrow-to-region start (point))
924 (require 'lisp-mnt)
925 ;; Use some headers we've invented to drive the process.
926 (let* ((requires-str (lm-header "package-requires"))
927 (requires (if requires-str
928 (package-read-from-string requires-str)))
929 ;; Prefer Package-Version; if defined, the package author
930 ;; probably wants us to use it. Otherwise try Version.
931 (pkg-version
932 (or (package-strip-rcs-id (lm-header "package-version"))
933 (package-strip-rcs-id (lm-header "version"))))
934 (commentary (lm-commentary)))
935 (unless pkg-version
936 (error
937 "Package lacks a \"Version\" or \"Package-Version\" header"))
938 ;; Turn string version numbers into list form.
939 (setq requires
940 (mapcar
941 (lambda (elt)
942 (list (car elt)
943 (version-to-list (car (cdr elt)))))
944 requires))
945 (vector file-name requires desc pkg-version commentary))))
44198b6e
CY
946
947(defun package-tar-file-info (file)
948 "Find package information for a tar file.
949FILE is the name of the tar file to examine.
950The return result is a vector like `package-buffer-info'."
b511b994
MA
951 (let ((default-directory (file-name-directory file))
952 (file (file-name-nondirectory file)))
4525ce3e
CY
953 (unless (string-match (concat "\\`" package-subdirectory-regexp "\\.tar\\'")
954 file)
b511b994
MA
955 (error "Invalid package name `%s'" file))
956 (let* ((pkg-name (match-string-no-properties 1 file))
957 (pkg-version (match-string-no-properties 2 file))
958 ;; Extract the package descriptor.
959 (pkg-def-contents (shell-command-to-string
960 ;; Requires GNU tar.
961 (concat "tar -xOf " file " "
962
963 pkg-name "-" pkg-version "/"
964 pkg-name "-pkg.el")))
965 (pkg-def-parsed (package-read-from-string pkg-def-contents)))
966 (unless (eq (car pkg-def-parsed) 'define-package)
967 (error "No `define-package' sexp is present in `%s-pkg.el'" pkg-name))
968 (let ((name-str (nth 1 pkg-def-parsed))
969 (version-string (nth 2 pkg-def-parsed))
970 (docstring (nth 3 pkg-def-parsed))
971 (requires (nth 4 pkg-def-parsed))
972 (readme (shell-command-to-string
973 ;; Requires GNU tar.
974 (concat "tar -xOf " file " "
975 pkg-name "-" pkg-version "/README"))))
976 (unless (equal pkg-version version-string)
977 (error "Package has inconsistent versions"))
978 (unless (equal pkg-name name-str)
979 (error "Package has inconsistent names"))
980 ;; Kind of a hack.
981 (if (string-match ": Not found in archive" readme)
982 (setq readme nil))
983 ;; Turn string version numbers into list form.
984 (if (eq (car requires) 'quote)
985 (setq requires (car (cdr requires))))
986 (setq requires
987 (mapcar (lambda (elt)
988 (list (car elt)
989 (version-to-list (cadr elt))))
990 requires))
991 (vector pkg-name requires docstring version-string readme)))))
44198b6e 992
187d3296
CY
993;;;###autoload
994(defun package-install-from-buffer (pkg-info type)
995 "Install a package from the current buffer.
996When called interactively, the current buffer is assumed to be a
997single .el file that follows the packaging guidelines; see info
998node `(elisp)Packaging'.
999
1000When called from Lisp, PKG-INFO is a vector describing the
1001information, of the type returned by `package-buffer-info'; and
1002TYPE is the package type (either `single' or `tar')."
1003 (interactive (list (package-buffer-info) 'single))
44198b6e
CY
1004 (save-excursion
1005 (save-restriction
1006 (let* ((file-name (aref pkg-info 0))
187d3296 1007 (requires (aref pkg-info 1))
44198b6e
CY
1008 (desc (if (string= (aref pkg-info 2) "")
1009 "No description available."
1010 (aref pkg-info 2)))
1011 (pkg-version (aref pkg-info 3)))
1012 ;; Download and install the dependencies.
1013 (let ((transaction (package-compute-transaction nil requires)))
1014 (package-download-transaction transaction))
1015 ;; Install the package itself.
1016 (cond
1017 ((eq type 'single)
1018 (package-unpack-single file-name pkg-version desc requires))
1019 ((eq type 'tar)
1020 (package-unpack (intern file-name) pkg-version))
1021 (t
1022 (error "Unknown type: %s" (symbol-name type))))
1023 ;; Try to activate it.
1024 (package-initialize)))))
1025
44198b6e
CY
1026;;;###autoload
1027(defun package-install-file (file)
1028 "Install a package from a file.
1029The file can either be a tar file or an Emacs Lisp file."
1030 (interactive "fPackage file name: ")
1031 (with-temp-buffer
1032 (insert-file-contents-literally file)
1033 (cond
187d3296
CY
1034 ((string-match "\\.el$" file)
1035 (package-install-from-buffer (package-buffer-info) 'single))
44198b6e 1036 ((string-match "\\.tar$" file)
187d3296 1037 (package-install-from-buffer (package-tar-file-info file) 'tar))
44198b6e
CY
1038 (t (error "Unrecognized extension `%s'" (file-name-extension file))))))
1039
1040(defun package-delete (name version)
015eea59
CY
1041 (let ((dir (package--dir name version)))
1042 (if (string-equal (file-name-directory dir)
1043 (file-name-as-directory
1044 (expand-file-name package-user-dir)))
1045 (progn
1046 (delete-directory dir t t)
1047 (message "Package `%s-%s' deleted." name version))
1048 ;; Don't delete "system" packages
1049 (error "Package `%s-%s' is a system package, not deleting"
1050 name version))))
44198b6e 1051
f561e49a 1052(defun package-archive-base (name)
bc44bef7
PH
1053 "Return the archive containing the package NAME."
1054 (let ((desc (cdr (assq (intern-soft name) package-archive-contents))))
1055 (cdr (assoc (aref desc (- (length desc) 1)) package-archives))))
1056
1057(defun package--download-one-archive (archive file)
f561e49a
CY
1058 "Retrieve an archive file FILE from ARCHIVE, and cache it.
1059ARCHIVE should be a cons cell of the form (NAME . LOCATION),
1060similar to an entry in `package-alist'. Save the cached copy to
1061\"archives/NAME/archive-contents\" in `package-user-dir'."
1062 (let* ((dir (expand-file-name "archives" package-user-dir))
1063 (dir (expand-file-name (car archive) dir)))
1064 (package--with-work-buffer (cdr archive) file
ebf662f4
CY
1065 ;; Read the retrieved buffer to make sure it is valid (e.g. it
1066 ;; may fetch a URL redirect page).
1067 (when (listp (read buffer))
1068 (make-directory dir t)
1069 (setq buffer-file-name (expand-file-name file dir))
1070 (let ((version-control 'never))
f561e49a 1071 (save-buffer))))))
44198b6e
CY
1072
1073(defun package-refresh-contents ()
1074 "Download the ELPA archive description if needed.
ebf662f4
CY
1075This informs Emacs about the latest versions of all packages, and
1076makes them available for download."
44198b6e
CY
1077 (interactive)
1078 (unless (file-exists-p package-user-dir)
1079 (make-directory package-user-dir t))
bc44bef7 1080 (dolist (archive package-archives)
515de2e3 1081 (condition-case-no-debug nil
cb6c4991 1082 (package--download-one-archive archive "archive-contents")
187d3296 1083 (error (message "Failed to download `%s' archive."
cb6c4991 1084 (car archive)))))
bc44bef7 1085 (package-read-all-archive-contents))
44198b6e
CY
1086
1087;;;###autoload
4b99edf2 1088(defun package-initialize (&optional no-activate)
44198b6e 1089 "Load Emacs Lisp packages, and activate them.
4b99edf2
CY
1090The variable `package-load-list' controls which packages to load.
1091If optional arg NO-ACTIVATE is non-nil, don't activate packages."
44198b6e 1092 (interactive)
015eea59
CY
1093 (setq package-alist nil
1094 package-obsolete-alist nil)
44198b6e 1095 (package-load-all-descriptors)
bc44bef7 1096 (package-read-all-archive-contents)
4b99edf2
CY
1097 (unless no-activate
1098 (dolist (elt package-alist)
1099 (package-activate (car elt) (package-desc-vers (cdr elt)))))
1100 (setq package--initialized t))
44198b6e
CY
1101
1102\f
cced7584 1103;;;; Package description buffer.
44198b6e 1104
cced7584
CY
1105;;;###autoload
1106(defun describe-package (package)
1107 "Display the full documentation of PACKAGE (a symbol)."
1108 (interactive
8a500a91
CY
1109 (let* ((guess (function-called-at-point))
1110 packages val)
4b99edf2
CY
1111 (require 'finder-inf nil t)
1112 ;; Load the package list if necessary (but don't activate them).
1113 (unless package--initialized
1114 (package-initialize t))
8a500a91 1115 (setq packages (append (mapcar 'car package-alist)
4b99edf2
CY
1116 (mapcar 'car package-archive-contents)
1117 (mapcar 'car package--builtins)))
cced7584
CY
1118 (unless (memq guess packages)
1119 (setq guess nil))
1120 (setq packages (mapcar 'symbol-name packages))
1121 (setq val
1122 (completing-read (if guess
1123 (format "Describe package (default %s): "
1124 guess)
1125 "Describe package: ")
1126 packages nil t nil nil guess))
cb6c4991 1127 (list (if (equal val "") guess (intern val)))))
4b99edf2
CY
1128 (if (or (null package) (not (symbolp package)))
1129 (message "No package specified")
cced7584
CY
1130 (help-setup-xref (list #'describe-package package)
1131 (called-interactively-p 'interactive))
1132 (with-help-window (help-buffer)
1133 (with-current-buffer standard-output
1134 (describe-package-1 package)))))
1135
1136(defun describe-package-1 (package)
96ae4c8f 1137 (require 'lisp-mnt)
cb6c4991
CY
1138 (let ((package-name (symbol-name package))
1139 (built-in (assq package package--builtins))
1140 desc pkg-dir reqs version installable)
cced7584
CY
1141 (prin1 package)
1142 (princ " is ")
4b99edf2
CY
1143 (cond
1144 ;; Loaded packages are in `package-alist'.
1145 ((setq desc (cdr (assq package package-alist)))
1146 (setq version (package-version-join (package-desc-vers desc)))
1147 (if (setq pkg-dir (package--dir package-name version))
1148 (insert "an installed package.\n\n")
1149 ;; This normally does not happen.
1150 (insert "a deleted package.\n\n")))
1151 ;; Available packages are in `package-archive-contents'.
1152 ((setq desc (cdr (assq package package-archive-contents)))
1153 (setq version (package-version-join (package-desc-vers desc))
8adb4c33 1154 installable t)
4b99edf2
CY
1155 (if built-in
1156 (insert "a built-in package.\n\n")
1157 (insert "an uninstalled package.\n\n")))
1158 (built-in
1159 (setq desc (cdr built-in)
1160 version (package-version-join (package-desc-vers desc)))
1161 (insert "a built-in package.\n\n"))
1162 (t
1163 (insert "an orphan package.\n\n")))
cb6c4991 1164
96ae4c8f 1165 (insert " " (propertize "Status" 'font-lock-face 'bold) ": ")
cb6c4991 1166 (cond (pkg-dir
96ae4c8f
CY
1167 (insert (propertize "Installed"
1168 'font-lock-face 'font-lock-comment-face))
cb6c4991
CY
1169 (insert " in `")
1170 ;; Todo: Add button for uninstalling.
1171 (help-insert-xref-button (file-name-as-directory pkg-dir)
1172 'help-package-def pkg-dir)
4b99edf2
CY
1173 (if built-in
1174 (insert "',\n shadowing a "
1175 (propertize "built-in package"
1176 'font-lock-face 'font-lock-builtin-face)
1177 ".")
1178 (insert "'.")))
cb6c4991 1179 (installable
4b99edf2
CY
1180 (if built-in
1181 (insert (propertize "Built-in." 'font-lock-face 'font-lock-builtin-face)
1182 " Alternate version available -- ")
1183 (insert "Available -- "))
1184 (let ((button-text (if (display-graphic-p) "Install" "[Install]"))
cb6c4991
CY
1185 (button-face (if (display-graphic-p)
1186 '(:box (:line-width 2 :color "dark grey")
1187 :background "light grey"
1188 :foreground "black")
1189 'link)))
4b99edf2 1190 (insert-text-button button-text 'face button-face 'follow-link t
cb6c4991
CY
1191 'package-symbol package
1192 'action 'package-install-button-action)))
1193 (built-in
4b99edf2 1194 (insert (propertize "Built-in." 'font-lock-face 'font-lock-builtin-face)))
cb6c4991
CY
1195 (t (insert "Deleted.")))
1196 (insert "\n")
4b99edf2 1197 (and version (> (length version) 0)
96ae4c8f
CY
1198 (insert " "
1199 (propertize "Version" 'font-lock-face 'bold) ": " version "\n"))
4b99edf2
CY
1200
1201 (setq reqs (if desc (package-desc-reqs desc)))
8adb4c33 1202 (when reqs
96ae4c8f 1203 (insert " " (propertize "Requires" 'font-lock-face 'bold) ": ")
8adb4c33
CY
1204 (let ((first t)
1205 name vers text)
1206 (dolist (req reqs)
1207 (setq name (car req)
1208 vers (cadr req)
1209 text (format "%s-%s" (symbol-name name)
1210 (package-version-join vers)))
1211 (cond (first (setq first nil))
1212 ((>= (+ 2 (current-column) (length text))
1213 (window-width))
1214 (insert ",\n "))
1215 (t (insert ", ")))
1216 (help-insert-xref-button text 'help-package name))
1217 (insert "\n")))
96ae4c8f 1218 (insert " " (propertize "Summary" 'font-lock-face 'bold)
4b99edf2 1219 ": " (if desc (package-desc-doc desc)) "\n\n")
cb6c4991 1220
4b99edf2 1221 (if built-in
96ae4c8f
CY
1222 ;; For built-in packages, insert the commentary.
1223 (let ((fn (locate-file (concat package-name ".el") load-path
1224 load-file-rep-suffixes))
1225 (opoint (point)))
1226 (insert (or (lm-commentary fn) ""))
1227 (save-excursion
1228 (goto-char opoint)
1229 (when (re-search-forward "^;;; Commentary:\n" nil t)
1230 (replace-match ""))
1231 (while (re-search-forward "^\\(;+ ?\\)" nil t)
1232 (replace-match ""))))
1233 (let ((readme (expand-file-name (concat package-name "-readme.txt")
f561e49a
CY
1234 package-user-dir))
1235 readme-string)
96ae4c8f
CY
1236 ;; For elpa packages, try downloading the commentary. If that
1237 ;; fails, try an existing readme file in `package-user-dir'.
f561e49a
CY
1238 (cond ((condition-case nil
1239 (package--with-work-buffer (package-archive-base package)
1240 (concat package-name "-readme.txt")
1241 (setq buffer-file-name
1242 (expand-file-name readme package-user-dir))
1243 (let ((version-control 'never))
1244 (save-buffer))
1245 (setq readme-string (buffer-string))
1246 t)
1247 (error nil))
1248 (insert readme-string))
96ae4c8f
CY
1249 ((file-readable-p readme)
1250 (insert-file-contents readme)
1251 (goto-char (point-max))))))))
cb6c4991
CY
1252
1253(defun package-install-button-action (button)
1254 (let ((package (button-get button 'package-symbol)))
1255 (when (y-or-n-p (format "Install package `%s'? " package))
1256 (package-install package)
1257 (revert-buffer nil t)
1258 (goto-char (point-min)))))
cced7584
CY
1259
1260\f
44198b6e
CY
1261;;;; Package menu mode.
1262
54ea2a0d 1263(defvar package-menu-mode-map
e91a96fe 1264 (let ((map (make-sparse-keymap))
64eba874 1265 (menu-map (make-sparse-keymap "Package")))
e91a96fe 1266 (set-keymap-parent map tabulated-list-mode-map)
8adb4c33 1267 (define-key map "\C-m" 'package-menu-describe-package)
54ea2a0d
JB
1268 (define-key map "u" 'package-menu-mark-unmark)
1269 (define-key map "\177" 'package-menu-backup-unmark)
1270 (define-key map "d" 'package-menu-mark-delete)
1271 (define-key map "i" 'package-menu-mark-install)
54ea2a0d
JB
1272 (define-key map "r" 'package-menu-refresh)
1273 (define-key map "~" 'package-menu-mark-obsolete-for-deletion)
1274 (define-key map "x" 'package-menu-execute)
1275 (define-key map "h" 'package-menu-quick-help)
cb6c4991 1276 (define-key map "?" 'package-menu-describe-package)
64eba874
DN
1277 (define-key map [menu-bar package-menu] (cons "Package" menu-map))
1278 (define-key menu-map [mq]
1279 '(menu-item "Quit" quit-window
1280 :help "Quit package selection"))
1281 (define-key menu-map [s1] '("--"))
1282 (define-key menu-map [mn]
1283 '(menu-item "Next" next-line
1284 :help "Next Line"))
1285 (define-key menu-map [mp]
1286 '(menu-item "Previous" previous-line
1287 :help "Previous Line"))
1288 (define-key menu-map [s2] '("--"))
1289 (define-key menu-map [mu]
1290 '(menu-item "Unmark" package-menu-mark-unmark
1291 :help "Clear any marks on a package and move to the next line"))
1292 (define-key menu-map [munm]
1293 '(menu-item "Unmark backwards" package-menu-backup-unmark
1294 :help "Back up one line and clear any marks on that package"))
1295 (define-key menu-map [md]
1296 '(menu-item "Mark for deletion" package-menu-mark-delete
1297 :help "Mark a package for deletion and move to the next line"))
1298 (define-key menu-map [mi]
1299 '(menu-item "Mark for install" package-menu-mark-install
1300 :help "Mark a package for installation and move to the next line"))
1301 (define-key menu-map [s3] '("--"))
1302 (define-key menu-map [mg]
e687c2cd 1303 '(menu-item "Update package list" revert-buffer
64eba874
DN
1304 :help "Update the list of packages"))
1305 (define-key menu-map [mr]
1306 '(menu-item "Refresh package list" package-menu-refresh
1307 :help "Download the ELPA archive"))
1308 (define-key menu-map [s4] '("--"))
1309 (define-key menu-map [mt]
1310 '(menu-item "Mark obsolete packages" package-menu-mark-obsolete-for-deletion
1311 :help "Mark all obsolete packages for deletion"))
1312 (define-key menu-map [mx]
1313 '(menu-item "Execute actions" package-menu-execute
1314 :help "Perform all the marked actions"))
1315 (define-key menu-map [s5] '("--"))
1316 (define-key menu-map [mh]
1317 '(menu-item "Help" package-menu-quick-help
1318 :help "Show short key binding help for package-menu-mode"))
1319 (define-key menu-map [mc]
1320 '(menu-item "View Commentary" package-menu-view-commentary
1321 :help "Display information about this package"))
54ea2a0d 1322 map)
44198b6e
CY
1323 "Local keymap for `package-menu-mode' buffers.")
1324
e91a96fe 1325(define-derived-mode package-menu-mode tabulated-list-mode "Package Menu"
44198b6e
CY
1326 "Major mode for browsing a list of packages.
1327Letters do not insert themselves; instead, they are commands.
1328\\<package-menu-mode-map>
1329\\{package-menu-mode-map}"
e91a96fe
CY
1330 (setq tabulated-list-format [("Package" 18 package-menu--name-predicate)
1331 ("Version" 12 nil)
1332 ("Status" 10 package-menu--status-predicate)
1333 ("Description" 0 nil)])
1334 (setq tabulated-list-padding 2)
1335 (setq tabulated-list-sort-key (cons "Status" nil))
1336 (tabulated-list-init-header))
1337
1338(defmacro package--push (package desc status listname)
1339 "Convenience macro for `package-menu--generate'.
1340If the alist stored in the symbol LISTNAME lacks an entry for a
1341package PACKAGE with descriptor DESC, add one. The alist is
7ede3b65
CY
1342keyed with cons cells (PACKAGE . VERSION-LIST), where PACKAGE is
1343a symbol and VERSION-LIST is a version list."
e91a96fe
CY
1344 `(let* ((version (package-desc-vers ,desc))
1345 (key (cons ,package version)))
1346 (unless (assoc key ,listname)
1347 (push (list key ,status (package-desc-doc ,desc)) ,listname))))
1348
512e3ae1 1349(defun package-menu--generate (remember-pos packages)
e91a96fe 1350 "Populate the Package Menu.
512e3ae1
CY
1351If REMEMBER-POS is non-nil, keep point on the same entry.
1352PACKAGES should be t, which means to display all known packages,
1353or a list of package names (symbols) to display."
e91a96fe
CY
1354 ;; Construct list of ((PACKAGE . VERSION) STATUS DESCRIPTION).
1355 (let (info-list name builtin)
1356 ;; Installed packages:
1357 (dolist (elt package-alist)
1358 (setq name (car elt))
512e3ae1
CY
1359 (when (or (eq packages t) (memq name packages))
1360 (package--push name (cdr elt)
1361 (if (stringp (cadr (assq name package-load-list)))
1362 "held" "installed")
1363 info-list)))
e91a96fe
CY
1364
1365 ;; Built-in packages:
1366 (dolist (elt package--builtins)
1367 (setq name (car elt))
512e3ae1
CY
1368 (when (and (not (eq name 'emacs)) ; Hide the `emacs' package.
1369 (or (eq packages t) (memq name packages)))
e91a96fe
CY
1370 (package--push name (cdr elt) "built-in" info-list)))
1371
1372 ;; Available and disabled packages:
1373 (dolist (elt package-archive-contents)
1374 (setq name (car elt))
512e3ae1
CY
1375 (when (or (eq packages t) (memq name packages))
1376 (let ((hold (assq name package-load-list)))
1377 (package--push name (cdr elt)
1378 (if (and hold (null (cadr hold)))
1379 "disabled"
1380 "available")
1381 info-list))))
e91a96fe
CY
1382
1383 ;; Obsolete packages:
1384 (dolist (elt package-obsolete-alist)
1385 (dolist (inner-elt (cdr elt))
512e3ae1
CY
1386 (when (or (eq packages t) (memq (car elt) packages))
1387 (package--push (car elt) (cdr inner-elt) "obsolete" info-list))))
e91a96fe
CY
1388
1389 ;; Print the result.
1390 (setq tabulated-list-entries (mapcar 'package-menu--print-info info-list))
1391 (tabulated-list-print remember-pos)))
1392
1393(defun package-menu--print-info (pkg)
1394 "Return a package entry suitable for `tabulated-list-entries'.
1395PKG has the form ((PACKAGE . VERSION) STATUS DOC).
1396Return (KEY [NAME VERSION STATUS DOC]), where KEY is the
1397identifier (NAME . VERSION-LIST)."
1398 (let* ((package (caar pkg))
1399 (version (cdr (car pkg)))
1400 (status (nth 1 pkg))
1401 (doc (or (nth 2 pkg) ""))
1402 (face (cond
1403 ((string= status "built-in") 'font-lock-builtin-face)
1404 ((string= status "available") 'default)
1405 ((string= status "held") 'font-lock-constant-face)
1406 ((string= status "disabled") 'font-lock-warning-face)
1407 ((string= status "installed") 'font-lock-comment-face)
1408 (t 'font-lock-warning-face)))) ; obsolete.
1409 (list (cons package version)
1410 (vector (list (symbol-name package)
1411 'face 'link
1412 'follow-link t
1413 'package-symbol package
1414 'action 'package-menu-describe-package)
1415 (propertize (package-version-join version)
1416 'font-lock-face face)
1417 (propertize status 'font-lock-face face)
1418 (propertize doc 'font-lock-face face)))))
44198b6e
CY
1419
1420(defun package-menu-refresh ()
ebf662f4
CY
1421 "Download the Emacs Lisp package archive.
1422This fetches the contents of each archive specified in
1423`package-archives', and then refreshes the package menu."
44198b6e 1424 (interactive)
187d3296
CY
1425 (unless (eq major-mode 'package-menu-mode)
1426 (error "The current buffer is not a Package Menu"))
44198b6e 1427 (package-refresh-contents)
512e3ae1 1428 (package-menu--generate t t))
44198b6e 1429
e91a96fe
CY
1430(defun package-menu-describe-package (&optional button)
1431 "Describe the current package.
1432If optional arg BUTTON is non-nil, describe its associated package."
44198b6e 1433 (interactive)
e91a96fe
CY
1434 (let ((package (if button (button-get button 'package-symbol)
1435 (car (tabulated-list-get-id)))))
1436 (if package
1437 (describe-package package))))
44198b6e
CY
1438
1439;; fixme numeric argument
1440(defun package-menu-mark-delete (num)
1441 "Mark a package for deletion and move to the next line."
1442 (interactive "p")
fb87e0fb 1443 (if (member (package-menu-get-status) '("installed" "obsolete"))
e91a96fe 1444 (tabulated-list-put-tag "D" t)
015eea59 1445 (forward-line)))
44198b6e
CY
1446
1447(defun package-menu-mark-install (num)
1448 "Mark a package for installation and move to the next line."
1449 (interactive "p")
015eea59 1450 (if (string-equal (package-menu-get-status) "available")
e91a96fe 1451 (tabulated-list-put-tag "I" t)
015eea59 1452 (forward-line)))
44198b6e
CY
1453
1454(defun package-menu-mark-unmark (num)
1455 "Clear any marks on a package and move to the next line."
1456 (interactive "p")
e91a96fe 1457 (tabulated-list-put-tag " " t))
44198b6e
CY
1458
1459(defun package-menu-backup-unmark ()
1460 "Back up one line and clear any marks on that package."
1461 (interactive)
1462 (forward-line -1)
e91a96fe 1463 (tabulated-list-put-tag " "))
44198b6e
CY
1464
1465(defun package-menu-mark-obsolete-for-deletion ()
1466 "Mark all obsolete packages for deletion."
1467 (interactive)
1468 (save-excursion
1469 (goto-char (point-min))
1470 (forward-line 2)
1471 (while (not (eobp))
1472 (if (looking-at ".*\\s obsolete\\s ")
e91a96fe 1473 (tabulated-list-put-tag "D" t)
44198b6e
CY
1474 (forward-line 1)))))
1475
1476(defun package-menu-quick-help ()
1477 "Show short key binding help for package-menu-mode."
1478 (interactive)
1479 (message "n-ext, i-nstall, d-elete, u-nmark, x-ecute, r-efresh, h-elp"))
1480
cb6c4991
CY
1481(define-obsolete-function-alias
1482 'package-menu-view-commentary 'package-menu-describe-package "24.1")
44198b6e 1483
44198b6e
CY
1484(defun package-menu-get-status ()
1485 (save-excursion
1486 (if (looking-at ". [^ \t]*[ \t]*[^ \t]*[ \t]*\\([^ \t]*\\)")
1487 (match-string 1)
1488 "")))
1489
1490(defun package-menu-execute ()
015eea59
CY
1491 "Perform marked Package Menu actions.
1492Packages marked for installation are downloaded and installed;
1493packages marked for deletion are removed."
44198b6e 1494 (interactive)
e91a96fe
CY
1495 (unless (eq major-mode 'package-menu-mode)
1496 (error "The current buffer is not in Package Menu mode"))
1497 (let (install-list delete-list cmd id)
015eea59
CY
1498 (save-excursion
1499 (goto-char (point-min))
1500 (while (not (eobp))
1501 (setq cmd (char-after))
e91a96fe
CY
1502 (unless (eq cmd ?\s)
1503 ;; This is the key (PACKAGE . VERSION-LIST).
1504 (setq id (tabulated-list-get-id))
1505 (cond ((eq cmd ?D)
1506 (push (cons (symbol-name (car id))
1507 (package-version-join (cdr id)))
1508 delete-list))
1509 ((eq cmd ?I)
1510 (push (car id) install-list))))
015eea59
CY
1511 (forward-line)))
1512 ;; Delete packages, prompting if necessary.
1513 (when delete-list
1514 (if (yes-or-no-p
1515 (if (= (length delete-list) 1)
1516 (format "Delete package `%s-%s'? "
1517 (caar delete-list)
1518 (cdr (car delete-list)))
1519 (format "Delete these %d packages (%s)? "
1520 (length delete-list)
1521 (mapconcat (lambda (elt)
1522 (concat (car elt) "-" (cdr elt)))
1523 delete-list
1524 ", "))))
1525 (dolist (elt delete-list)
515de2e3 1526 (condition-case-no-debug err
015eea59
CY
1527 (package-delete (car elt) (cdr elt))
1528 (error (message (cadr err)))))
1529 (error "Aborted")))
1530 (when install-list
1531 (if (yes-or-no-p
1532 (if (= (length install-list) 1)
1533 (format "Install package `%s'? " (car install-list))
1534 (format "Install these %d packages (%s)? "
1535 (length install-list)
e91a96fe
CY
1536 (mapconcat 'symbol-name install-list ", "))))
1537 (mapc 'package-install install-list)))
015eea59
CY
1538 ;; If we deleted anything, regenerate `package-alist'. This is done
1539 ;; automatically if we installed a package.
1540 (and delete-list (null install-list)
1541 (package-initialize))
1542 (if (or delete-list install-list)
512e3ae1 1543 (package-menu--generate t t)
015eea59 1544 (message "No operations specified."))))
44198b6e 1545
e91a96fe
CY
1546(defun package-menu--version-predicate (A B)
1547 (let ((vA (or (aref (cadr A) 1) '(0)))
1548 (vB (or (aref (cadr B) 1) '(0))))
1549 (if (version-list-= vA vB)
1550 (package-menu--name-predicate A B)
1551 (version-list-< vA vB))))
1552
1553(defun package-menu--status-predicate (A B)
1554 (let ((sA (aref (cadr A) 2))
1555 (sB (aref (cadr B) 2)))
1556 (cond ((string= sA sB)
1557 (package-menu--name-predicate A B))
1558 ((string= sA "available") t)
1559 ((string= sB "available") nil)
1560 ((string= sA "installed") t)
1561 ((string= sB "installed") nil)
1562 ((string= sA "held") t)
1563 ((string= sB "held") nil)
1564 ((string= sA "built-in") t)
1565 ((string= sB "built-in") nil)
1566 ((string= sA "obsolete") t)
1567 ((string= sB "obsolete") nil)
1568 (t (string< sA sB)))))
1569
1570(defun package-menu--description-predicate (A B)
1571 (let ((dA (aref (cadr A) 3))
1572 (dB (aref (cadr B) 3)))
1573 (if (string= dA dB)
1574 (package-menu--name-predicate A B)
1575 (string< dA dB))))
1576
1577(defun package-menu--name-predicate (A B)
1578 (string< (symbol-name (caar A))
1579 (symbol-name (caar B))))
44198b6e
CY
1580
1581;;;###autoload
e91a96fe 1582(defun list-packages (&optional no-fetch)
44198b6e 1583 "Display a list of packages.
e91a96fe
CY
1584This first fetches the updated list of packages before
1585displaying, unless a prefix argument NO-FETCH is specified.
44198b6e 1586The list is displayed in a buffer named `*Packages*'."
e91a96fe
CY
1587 (interactive "P")
1588 (require 'finder-inf nil t)
8a500a91 1589 ;; Initialize the package system if necessary.
4b99edf2
CY
1590 (unless package--initialized
1591 (package-initialize t))
e91a96fe
CY
1592 (unless no-fetch
1593 (package-refresh-contents))
1594 (let ((buf (get-buffer-create "*Packages*")))
1595 (with-current-buffer buf
1596 (package-menu-mode)
512e3ae1 1597 (package-menu--generate nil t))
e91a96fe
CY
1598 ;; The package menu buffer has keybindings. If the user types
1599 ;; `M-x list-packages', that suggests it should become current.
1600 (switch-to-buffer buf)))
44198b6e 1601
cb8759ca 1602;;;###autoload
96ae4c8f 1603(defalias 'package-list-packages 'list-packages)
cb8759ca 1604
512e3ae1
CY
1605;; Used in finder.el
1606(defun package-show-package-list (packages)
1607 "Display PACKAGES in a *Packages* buffer.
1608This is similar to `list-packages', but it does not fetch the
1609updated list of packages, and it only displays packages with
1610names in PACKAGES (which should be a list of symbols)."
1611 (require 'finder-inf nil t)
1612 (let ((buf (get-buffer-create "*Packages*")))
1613 (with-current-buffer buf
1614 (package-menu-mode)
1615 (package-menu--generate nil packages))
1616 (switch-to-buffer buf)))
1617
44198b6e
CY
1618(defun package-list-packages-no-fetch ()
1619 "Display a list of packages.
1620Does not fetch the updated list of packages before displaying.
1621The list is displayed in a buffer named `*Packages*'."
1622 (interactive)
e91a96fe 1623 (list-packages t))
44198b6e 1624
44198b6e
CY
1625(provide 'package)
1626
1627;;; package.el ends here