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