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