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