Add tool bar style text-image-horiz (text to the left of the image).
[bpt/emacs.git] / lisp / emacs-lisp / package.el
CommitLineData
44198b6e
CY
1;;; package.el --- Simple package system for Emacs
2
3;; Copyright (C) 2007, 2008, 2009, 2010 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;; This code supports a single package repository, ELPA. All packages
47;; must be registered there.
48
49;; A package is described by its name and version. The distribution
50;; format is either a tar file or a single .el file.
51
52;; A tar file should be named "NAME-VERSION.tar". The tar file must
53;; unpack into a directory named after the package and version:
54;; "NAME-VERSION". It must contain a file named "PACKAGE-pkg.el"
55;; which consists of a call to define-package. It may also contain a
56;; "dir" file and the info files it references.
57
58;; A .el file will be named "NAME-VERSION.el" in ELPA, but will be
59;; installed as simply "NAME.el" in a directory named "NAME-VERSION".
60
61;; The downloader will download all dependent packages. It will also
62;; byte-compile the package's lisp at install time.
63
64;; At activation time we will set up the load-path and the info path,
65;; and we will load the package's autoloads. If a package's
66;; dependencies are not available, we will not activate that package.
67
68;; Conceptually a package has multiple state transitions:
69;;
70;; * Download. Fetching the package from ELPA.
71;; * Install. Untar the package, or write the .el file, into
72;; ~/.emacs.d/elpa/ directory.
73;; * Byte compile. Currently this phase is done during install,
74;; but we may change this.
75;; * Activate. Evaluate the autoloads for the package to make it
76;; available to the user.
77;; * Load. Actually load the package and run some code from it.
78
79;; Other external functions you may want to use:
80;;
81;; M-x package-list-packages
82;; Enters a mode similar to buffer-menu which lets you manage
83;; packages. You can choose packages for install (mark with "i",
84;; then "x" to execute) or deletion (not implemented yet), and you
85;; can see what packages are available. This will automatically
86;; fetch the latest list of packages from ELPA.
87;;
88;; M-x package-list-packages-no-fetch
89;; Like package-list-packages, but does not automatically fetch the
90;; new list of packages.
91;;
92;; M-x package-install-from-buffer
93;; Install a package consisting of a single .el file that appears
94;; in the current buffer. This only works for packages which
95;; define a Version header properly; package.el also supports the
96;; extension headers Package-Version (in case Version is an RCS id
97;; or similar), and Package-Requires (if the package requires other
98;; packages).
99;;
100;; M-x package-install-file
101;; Install a package from the indicated file. The package can be
102;; either a tar file or a .el file. A tar file must contain an
103;; appropriately-named "-pkg.el" file; a .el file must be properly
104;; formatted as with package-install-from-buffer.
105
106;;; Thanks:
107;;; (sorted by sort-lines):
108
109;; Jim Blandy <jimb@red-bean.com>
110;; Karl Fogel <kfogel@red-bean.com>
111;; Kevin Ryde <user42@zip.com.au>
112;; Lawrence Mitchell
113;; Michael Olson <mwolson@member.fsf.org>
114;; Sebastian Tennant <sebyte@smolny.plus.com>
115;; Stefan Monnier <monnier@iro.umontreal.ca>
116;; Vinicius Jose Latorre <viniciusjl@ig.com.br>
117;; Phil Hagelberg <phil@hagelb.org>
118
119;;; ToDo:
120
121;; - putting info dirs at the start of the info path means
122;; users see a weird ordering of categories. OTOH we want to
123;; override later entries. maybe emacs needs to enforce
124;; the standard layout?
125;; - put bytecode in a separate directory tree
126;; - perhaps give users a way to recompile their bytecode
127;; or do it automatically when emacs changes
128;; - give users a way to know whether a package is installed ok
129;; - give users a way to view a package's documentation when it
130;; only appears in the .el
131;; - use/extend checkdoc so people can tell if their package will work
132;; - "installed" instead of a blank in the status column
133;; - tramp needs its files to be compiled in a certain order.
134;; how to handle this? fix tramp?
135;; - on emacs 21 we don't kill the -autoloads.el buffer. what about 22?
136;; - maybe we need separate .elc directories for various emacs versions
137;; and also emacs-vs-xemacs. That way conditional compilation can
138;; work. But would this break anything?
139;; - should store the package's keywords in archive-contents, then
140;; let the users filter the package-menu by keyword. See
141;; finder-by-keyword. (We could also let people view the
142;; Commentary, but it isn't clear how useful this is.)
143;; - William Xu suggests being able to open a package file without
144;; installing it
145;; - Interface with desktop.el so that restarting after an install
146;; works properly
147;; - Implement M-x package-upgrade, to upgrade any/all existing packages
148;; - Use hierarchical layout. PKG/etc PKG/lisp PKG/info
149;; ... except maybe lisp?
150;; - It may be nice to have a macro that expands to the package's
151;; private data dir, aka ".../etc". Or, maybe data-directory
152;; needs to be a list (though this would be less nice)
153;; a few packages want this, eg sokoban
154;; - package menu needs:
155;; ability to know which packages are built-in & thus not deletable
156;; it can sometimes print odd results, like 0.3 available but 0.4 active
157;; why is that?
158;; - Allow multiple versions on the server...?
159;; [ why bother? ]
160;; - Don't install a package which will invalidate dependencies overall
161;; - Allow something like (or (>= emacs 21.0) (>= xemacs 21.5))
162;; [ currently thinking, why bother.. KISS ]
163;; - Allow optional package dependencies
164;; then if we require 'bbdb', bbdb-specific lisp in lisp/bbdb
165;; and just don't compile to add to load path ...?
166;; - Have a list of archive URLs? [ maybe there's no point ]
167;; - David Kastrup pointed out on the xemacs list that for GPL it
168;; is friendlier to ship the source tree. We could "support" that
169;; by just having a "src" subdir in the package. This isn't ideal
170;; but it probably is not worth trying to support random source
171;; tree layouts, build schemes, etc.
172;; - Our treatment of the info path is somewhat bogus
173;; - perhaps have an "unstable" tree in ELPA as well as a stable one
174
175;;; Code:
176
177(defgroup package nil
178 "Manager for Emacs Lisp packages."
179 :group 'applications
180 :version "24.1")
181
182;;;###autoload
183(defcustom package-enable-at-startup t
184 "Whether to activate installed packages when Emacs starts.
185If non-nil, packages are activated after reading the init file
186and before `after-init-hook'. Activation is not done if
187`user-init-file' is nil (e.g. Emacs was started with \"-q\").
188
189Even if the value is nil, you can type \\[package-initialize] to
190activate the package system at any time."
191 :type 'boolean
192 :group 'package
193 :version "24.1")
194
195(defcustom package-load-list '(all)
196 "List of packages for `package-initialize' to load.
197Each element in this list should be a list (NAME VERSION), or the
198symbol `all'. The symbol `all' says to load the latest installed
199versions of all packages not specified by other elements.
200
201For an element (NAME VERSION), NAME is a package name (a symbol).
202VERSION should be t, a string, or nil.
203If VERSION is t, all versions are loaded, though obsolete ones
204 will be put in `package-obsolete-alist' and not activated.
205If VERSION is a string, only that version is ever loaded.
206 Any other version, even if newer, is silently ignored.
207 Hence, the package is \"held\" at that version.
208If VERSION is nil, the package is not loaded (it is \"disabled\")."
209 :type '(repeat symbol)
210 :group 'package
211 :version "24.1")
212
213(defvar Info-directory-list)
44198b6e
CY
214(declare-function info-initialize "info" ())
215(declare-function url-http-parse-response "url-http" ())
216(declare-function lm-header "lisp-mnt" (header))
217(declare-function lm-commentary "lisp-mnt" (&optional file))
218(declare-function dired-delete-file "dired" (file &optional recursive trash))
219
220(defconst package-archive-base "http://elpa.gnu.org/packages/"
221 "Base URL for the Emacs Lisp Package Archive (ELPA).
222Ordinarily you should not need to change this.
223Note that some code in package.el assumes that this is an http: URL.")
224
225(defconst package-archive-version 1
226 "Version number of the package archive understood by this file.
227Lower version numbers than this will probably be understood as well.")
228
229(defconst package-el-version "1.0"
230 "Version of package.el.")
231
232;; We don't prime the cache since it tends to get out of date.
233(defvar package-archive-contents nil
234 "Cache of the contents of the Emacs Lisp Package Archive.
235This is an alist mapping package names (symbols) to package
236descriptor vectors. These are like the vectors for `package-alist'
237but have an extra entry which is 'tar for tar packages and
238'single for single-file packages.")
239
240(defcustom package-user-dir (locate-user-emacs-file "elpa")
241 "Directory containing the user's Emacs Lisp packages.
242The directory name should be absolute.
243Apart from this directory, Emacs also looks for system-wide
244packages in `package-directory-list'."
245 :type 'directory
246 :group 'package
247 :version "24.1")
248
249(defcustom package-directory-list
250 ;; Defaults are subdirs named "elpa" in the site-lisp dirs.
251 (let (result)
252 (dolist (f load-path)
253 (if (equal (file-name-nondirectory f) "site-lisp")
254 (push (expand-file-name "elpa" f) result)))
255 (nreverse result))
256 "List of additional directories containing Emacs Lisp packages.
257Each directory name should be absolute.
258
259These directories contain packages intended for system-wide; in
260contrast, `package-user-dir' contains packages for personal use."
261 :type '(repeat directory)
262 :group 'package
263 :version "24.1")
264
265(defun package-version-split (string)
266 "Split a package string into a version list."
267 (mapcar 'string-to-int (split-string string "[.]")))
268
269(defconst package--builtins-base
270 ;; We use package-version split here to make sure to pick up the
271 ;; minor version.
272 `((emacs . [,(package-version-split emacs-version) nil
273 "GNU Emacs"])
274 (package . [,(package-version-split package-el-version)
275 nil "Simple package system for GNU Emacs"]))
276 "Packages which are always built-in.")
277
278(defvar package--builtins
279 (delq nil
280 (append
281 package--builtins-base
282 (if (>= emacs-major-version 22)
283 ;; FIXME: emacs 22 includes tramp, rcirc, maybe
284 ;; other things...
285 '((erc . [(5 2) nil "An Emacs Internet Relay Chat client"])
286 ;; The external URL is version 1.15, so make sure the
287 ;; built-in one looks newer.
288 (url . [(1 16) nil "URL handling libary"])))
289 (if (>= emacs-major-version 23)
290 '(;; Strangely, nxml-version is missing in Emacs 23.
291 ;; We pick the merge date as the version.
292 (nxml . [(20071123) nil "Major mode for editing XML documents."])
293 (bubbles . [(0 5) nil "Puzzle game for Emacs."])))))
294 "Alist of all built-in packages.
295Maps the package name to a vector [VERSION REQS DOCSTRING].")
296
297(defvar package-alist package--builtins
298 "Alist of all packages available for activation.
299This maps the package name to a vector [VERSION REQS DOCSTRING].
300
301The value is generated by `package-load-descriptor', usually
302called via `package-initialize'. For user customizations of
303which packages to load/activate, see `package-load-list'.")
304
305(defvar package-activated-list
306 (mapcar #'car package-alist)
307 "List of the names of currently activated packages.")
308
309(defvar package-obsolete-alist nil
310 "Representation of obsolete packages.
311Like `package-alist', but maps package name to a second alist.
312The inner alist is keyed by version.")
313
314(defconst package-subdirectory-regexp
315 "^\\([^.].*\\)-\\([0-9]+\\(?:[.][0-9]+\\)*\\)$"
316 "Regular expression matching the name of a package subdirectory.
317The first subexpression is the package name.
318The second subexpression is the version string.")
319
320(defun package-version-join (l)
321 "Turn a list of version numbers into a version string."
322 (mapconcat 'int-to-string l "."))
323
324(defun package--version-first-nonzero (l)
325 (while (and l (= (car l) 0))
326 (setq l (cdr l)))
327 (if l (car l) 0))
328
329(defun package-version-compare (v1 v2 fun)
330 "Compare two version lists according to FUN.
331FUN can be <, <=, =, >, >=, or /=."
332 (while (and v1 v2 (= (car v1) (car v2)))
333 (setq v1 (cdr v1)
334 v2 (cdr v2)))
335 (if v1
336 (if v2
337 ;; Both not null; we know the cars are not =.
338 (funcall fun (car v1) (car v2))
339 ;; V1 not null, V2 null.
340 (funcall fun (package--version-first-nonzero v1) 0))
341 (if v2
342 ;; V1 null, V2 not null.
343 (funcall fun 0 (package--version-first-nonzero v2))
344 ;; Both null.
345 (funcall fun 0 0))))
346
347(defun package--test-version-compare ()
348 "Test suite for `package-version-compare'."
349 (unless (and (package-version-compare '(0) '(0) '=)
350 (not (package-version-compare '(1) '(0) '=))
351 (package-version-compare '(1 0 1) '(1) '>=)
352 (package-version-compare '(1 0 1) '(1) '>)
353 (not (package-version-compare '(0 9 1) '(1 0 2) '>=)))
354 (error "Failed"))
355 t)
356
357(defun package-strip-version (dirname)
358 "Strip the version from a combined package name and version.
359E.g., if given \"quux-23.0\", will return \"quux\""
360 (if (string-match package-subdirectory-regexp dirname)
361 (match-string 1 dirname)))
362
363(defun package-load-descriptor (dir package)
364 "Load the description file for a package.
365DIR is the directory in which to find the package subdirectory,
366and PACKAGE is the name of the package subdirectory.
367Return nil if the package could not be found."
368 (let ((pkg-dir (expand-file-name package dir)))
369 (if (file-directory-p pkg-dir)
370 (load (expand-file-name (concat (package-strip-version package)
371 "-pkg")
372 pkg-dir)
373 nil t))))
374
375(defun package-load-all-descriptors ()
376 "Load descriptors for installed Emacs Lisp packages.
377This looks for package subdirectories in `package-user-dir' and
378`package-directory-list'. The variable `package-load-list'
379controls which package subdirectories may be loaded.
380
381In each valid package subdirectory, this function loads the
382description file containing a call to `define-package', which
383updates `package-alist' and `package-obsolete-alist'."
384 (let ((all (memq 'all package-load-list))
385 name version force)
386 (dolist (dir (cons package-user-dir package-directory-list))
387 (when (file-directory-p dir)
388 (dolist (subdir (directory-files dir))
389 (when (and (file-directory-p (expand-file-name subdir dir))
390 (string-match package-subdirectory-regexp subdir))
391 (setq name (intern (match-string 1 subdir))
392 version (match-string 2 subdir)
393 force (assq name package-load-list))
394 (when (cond
395 ((null force)
396 all) ; not in package-load-list
397 ((null (setq force (cadr force)))
398 nil) ; disabled
399 ((eq force t)
400 t)
401 ((stringp force) ; held
402 (package-version-compare (package-version-split version)
403 (package-version-split force)
404 '=))
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
cced7584
CY
425(defun package--dir (name version-string)
426 (let* ((subdir (concat name "-" version-string))
44198b6e 427 (dir-list (cons package-user-dir package-directory-list))
cced7584 428 pkg-dir)
44198b6e 429 (while dir-list
cced7584
CY
430 (let ((subdir-full (expand-file-name subdir (car dir-list))))
431 (if (file-directory-p subdir-full)
432 (setq pkg-dir subdir-full
433 dir-list nil)
44198b6e 434 (setq dir-list (cdr dir-list)))))
cced7584
CY
435 pkg-dir))
436
437(defun package-activate-1 (package pkg-vec)
438 (let* ((name (symbol-name package))
439 (version-str (package-version-join (package-desc-vers pkg-vec)))
440 (pkg-dir (package--dir name version-str)))
44198b6e
CY
441 (unless pkg-dir
442 (error "Internal error: could not find directory for %s-%s"
cced7584
CY
443 name version-str))
444 ;; Add info node.
44198b6e
CY
445 (if (file-exists-p (expand-file-name "dir" pkg-dir))
446 (progn
447 ;; FIXME: not the friendliest, but simple.
448 (require 'info)
449 (info-initialize)
450 (setq Info-directory-list (cons pkg-dir Info-directory-list))))
cced7584 451 ;; Add to load path, add autoloads, and activate the package.
44198b6e 452 (setq load-path (cons pkg-dir load-path))
cced7584 453 (load (expand-file-name (concat name "-autoloads") pkg-dir) nil t)
44198b6e
CY
454 (setq package-activated-list (cons package package-activated-list))
455 ;; Don't return nil.
456 t))
457
458(defun package--built-in (package version)
459 "Return true if the package is built-in to Emacs."
460 (let ((elt (assq package package--builtins)))
461 (and elt
462 (package-version-compare (package-desc-vers (cdr elt)) version '=))))
463
464;; FIXME: return a reason instead?
465(defun package-activate (package version)
466 "Activate a package, and recursively activate its dependencies.
467Return nil if the package could not be activated."
468 ;; Assume the user knows what he is doing -- go ahead and activate a
469 ;; newer version of a package if an older one has already been
470 ;; activated. This is not ideal; we'd at least need to check to see
471 ;; if the package has actually been loaded, and not merely
472 ;; activated. However, don't try to activate 'emacs', as that makes
473 ;; no sense.
474 (unless (eq package 'emacs)
475 (let* ((pkg-desc (assq package package-alist))
476 (this-version (package-desc-vers (cdr pkg-desc)))
477 (req-list (package-desc-reqs (cdr pkg-desc)))
cced7584 478 ;; If the package was never activated, do it now.
44198b6e
CY
479 (keep-going (or (not (memq package package-activated-list))
480 (package-version-compare this-version version '>))))
481 (while (and req-list keep-going)
482 (let* ((req (car req-list))
483 (req-name (car req))
484 (req-version (cadr req)))
485 (or (package-activate req-name req-version)
486 (setq keep-going nil)))
487 (setq req-list (cdr req-list)))
488 (if keep-going
489 (package-activate-1 package (cdr pkg-desc))
490 ;; We get here if a dependency failed to activate -- but we
491 ;; can also get here if the requested package was already
492 ;; activated. Return non-nil in the latter case.
493 (and (memq package package-activated-list)
494 (package-version-compare this-version version '>=))))))
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 (setq package-obsolete-alist
507 (cons (cons package (list (cons (package-desc-vers pkg-vec)
508 pkg-vec)))
509 package-obsolete-alist)))))
510
511;; (define-package "emacs" "21.4.1" "GNU Emacs core package.")
512;; (define-package "erc" "5.1" "ERC - irc client" '((emacs "21.0")))
513(defun define-package (name-str version-string
514 &optional docstring requirements)
515 "Define a new package.
516NAME is the name of the package, a string.
517VERSION-STRING is the version of the package, a dotted sequence
518of integers.
519DOCSTRING is the optional description.
520REQUIREMENTS is a list of requirements on other packages.
521Each requirement is of the form (OTHER-PACKAGE \"VERSION\")."
522 (let* ((name (intern name-str))
523 (pkg-desc (assq name package-alist))
524 (new-version (package-version-split version-string))
525 (new-pkg-desc
526 (cons name
527 (vector new-version
528 (mapcar
529 (lambda (elt)
530 (list (car elt)
531 (package-version-split (car (cdr elt)))))
532 requirements)
533 docstring))))
534 ;; Only redefine a package if the redefinition is newer.
535 (if (or (not pkg-desc)
536 (package-version-compare new-version
537 (package-desc-vers (cdr pkg-desc))
538 '>))
539 (progn
540 (when pkg-desc
541 ;; Remove old package and declare it obsolete.
542 (setq package-alist (delq pkg-desc package-alist))
543 (package-mark-obsolete (car pkg-desc) (cdr pkg-desc)))
544 ;; Add package to the alist.
545 (setq package-alist (cons new-pkg-desc package-alist)))
546 ;; You can have two packages with the same version, for instance
547 ;; one in the system package directory and one in your private
548 ;; directory. We just let the first one win.
549 (unless (package-version-compare new-version
550 (package-desc-vers (cdr pkg-desc))
551 '=)
552 ;; The package is born obsolete.
553 (package-mark-obsolete (car new-pkg-desc) (cdr new-pkg-desc))))))
554
555;; From Emacs 22.
556(defun package-autoload-ensure-default-file (file)
557 "Make sure that the autoload file FILE exists and if not create it."
558 (unless (file-exists-p file)
559 (write-region
560 (concat ";;; " (file-name-nondirectory file)
561 " --- automatically extracted autoloads\n"
562 ";;\n"
563 ";;; Code:\n\n"
564 "\f\n;; Local Variables:\n"
565 ";; version-control: never\n"
566 ";; no-byte-compile: t\n"
567 ";; no-update-autoloads: t\n"
568 ";; End:\n"
569 ";;; " (file-name-nondirectory file)
570 " ends here\n")
571 nil file))
572 file)
573
574(defun package-generate-autoloads (name pkg-dir)
575 (let* ((auto-name (concat name "-autoloads.el"))
576 (ignore-name (concat name "-pkg.el"))
577 (generated-autoload-file (expand-file-name auto-name pkg-dir))
578 (version-control 'never))
579 (require 'autoload)
580 (unless (fboundp 'autoload-ensure-default-file)
581 (package-autoload-ensure-default-file generated-autoload-file))
582 (update-directory-autoloads pkg-dir)))
583
584(defun package-untar-buffer ()
585 "Untar the current buffer.
586This uses `tar-untar-buffer' if it is available.
587Otherwise it uses an external `tar' program.
588`default-directory' should be set by the caller."
589 (require 'tar-mode)
590 (if (fboundp 'tar-untar-buffer)
591 (progn
592 ;; tar-mode messes with narrowing, so we just let it have the
593 ;; whole buffer to play with.
594 (delete-region (point-min) (point))
595 (tar-mode)
596 (tar-untar-buffer))
597 ;; FIXME: check the result.
598 (call-process-region (point) (point-max) "tar" nil '(nil nil) nil
599 "xf" "-")))
600
601(defun package-unpack (name version)
602 (let ((pkg-dir (expand-file-name (concat (symbol-name name) "-" version)
603 package-user-dir)))
604 ;; Be careful!!
605 (make-directory package-user-dir t)
606 (if (file-directory-p pkg-dir)
607 (mapc (lambda (file) nil) ; 'delete-file -- FIXME: when we're
608 ; more confident
609 (directory-files pkg-dir t "^[^.]")))
610 (let* ((default-directory (file-name-as-directory package-user-dir)))
611 (package-untar-buffer)
612 (package-generate-autoloads (symbol-name name) pkg-dir)
613 (let ((load-path (cons pkg-dir load-path)))
614 (byte-recompile-directory pkg-dir 0 t)))))
615
616(defun package-unpack-single (file-name version desc requires)
617 "Install the contents of the current buffer as a package."
618 ;; Special case "package".
619 (if (string= file-name "package")
620 (write-region (point-min) (point-max)
621 (expand-file-name (concat file-name ".el")
622 package-user-dir)
623 nil nil nil nil)
624 (let* ((pkg-dir (expand-file-name (concat file-name "-" version)
625 package-user-dir))
626 (el-file (expand-file-name (concat file-name ".el") pkg-dir))
627 (pkg-file (expand-file-name (concat file-name "-pkg.el") pkg-dir)))
628 (make-directory pkg-dir t)
629 (write-region (point-min) (point-max) el-file nil nil nil 'excl)
630 (let ((print-level nil)
631 (print-length nil))
632 (write-region
633 (concat
634 (prin1-to-string
635 (list 'define-package
636 file-name
637 version
638 desc
639 (list 'quote
640 ;; Turn version lists into string form.
641 (mapcar
642 (lambda (elt)
643 (list (car elt)
644 (package-version-join (car (cdr elt)))))
645 requires))))
646 "\n")
647 nil
648 pkg-file
649 nil nil nil 'excl))
650 (package-generate-autoloads file-name pkg-dir)
651 (let ((load-path (cons pkg-dir load-path)))
652 (byte-recompile-directory pkg-dir 0 t)))))
653
654(defun package-handle-response ()
655 "Handle the response from the server.
656Parse the HTTP response and throw if an error occurred.
657The url package seems to require extra processing for this.
658This should be called in a `save-excursion', in the download buffer.
659It will move point to somewhere in the headers."
660 ;; We assume HTTP here.
661 (require 'url-http)
662 (let ((response (url-http-parse-response)))
663 (when (or (< response 200) (>= response 300))
664 (display-buffer (current-buffer))
665 (error "Error during download request:%s"
666 (buffer-substring-no-properties (point) (progn
667 (end-of-line)
668 (point)))))))
669
670(defun package-download-single (name version desc requires)
671 "Download and install a single-file package."
672 (let ((buffer (url-retrieve-synchronously
673 (concat package-archive-base
674 (symbol-name name) "-" version ".el"))))
675 (with-current-buffer buffer
676 (package-handle-response)
677 (re-search-forward "^$" nil 'move)
678 (forward-char)
679 (delete-region (point-min) (point))
680 (package-unpack-single (symbol-name name) version desc requires)
681 (kill-buffer buffer))))
682
683(defun package-download-tar (name version)
684 "Download and install a tar package."
685 (let ((tar-buffer (url-retrieve-synchronously
686 (concat package-archive-base
687 (symbol-name name) "-" version ".tar"))))
688 (with-current-buffer tar-buffer
689 (package-handle-response)
690 (re-search-forward "^$" nil 'move)
691 (forward-char)
692 (package-unpack name version)
693 (kill-buffer tar-buffer))))
694
695(defun package-installed-p (package version)
696 (let ((pkg-desc (assq package package-alist)))
697 (and pkg-desc
698 (package-version-compare version
699 (package-desc-vers (cdr pkg-desc))
700 '>=))))
701
702(defun package-compute-transaction (result requirements)
703 (dolist (elt requirements)
704 (let* ((next-pkg (car elt))
705 (next-version (cadr elt)))
706 (unless (package-installed-p next-pkg next-version)
707 ;; A package is required, but not installed. It might also be
708 ;; blocked via `package-load-list'.
709 (let ((pkg-desc (assq next-pkg package-archive-contents))
710 hold)
711 (when (setq hold (assq next-pkg package-load-list))
712 (setq hold (cadr hold))
713 (cond ((eq hold nil)
714 (error "Required package '%s' is disabled"
715 (symbol-name next-pkg)))
716 ((null (stringp hold))
717 (error "Invalid element in `package-load-list'"))
718 ((package-version-compare next-version
719 (package-version-split hold)
720 '>)
721 (error "Package '%s' held at version %s, \
722but version %s required"
723 (symbol-name next-pkg) hold
724 (package-version-join next-version)))))
725 (unless pkg-desc
726 (error "Package '%s' is not available for installation"
727 (symbol-name next-pkg)))
728 (unless (package-version-compare (package-desc-vers (cdr pkg-desc))
729 next-version
730 '>=)
731 (error
732 "Need package '%s' with version %s, but only %s is available"
733 (symbol-name next-pkg) (package-version-join next-version)
734 (package-version-join (package-desc-vers (cdr pkg-desc)))))
735 ;; Only add to the transaction if we don't already have it.
736 (unless (memq next-pkg result)
737 (setq result (cons next-pkg result)))
738 (setq result
739 (package-compute-transaction result
740 (package-desc-reqs
741 (cdr pkg-desc))))))))
742 result)
743
744(defun package-read-from-string (str)
745 "Read a Lisp expression from STR.
746Signal an error if the entire string was not used."
747 (let* ((read-data (read-from-string str))
748 (more-left
749 (condition-case nil
750 ;; The call to `ignore' suppresses a compiler warning.
751 (progn (ignore (read-from-string
752 (substring str (cdr read-data))))
753 t)
754 (end-of-file nil))))
755 (if more-left
756 (error "Can't read whole string")
757 (car read-data))))
758
759(defun package--read-archive-file (file)
760 "Re-read archive file FILE, if it exists.
761Will return the data from the file, or nil if the file does not exist.
762Will throw an error if the archive version is too new."
763 (let ((filename (expand-file-name file package-user-dir)))
764 (if (file-exists-p filename)
765 (with-temp-buffer
766 (insert-file-contents-literally filename)
767 (let ((contents (package-read-from-string
768 (buffer-substring-no-properties (point-min)
769 (point-max)))))
770 (if (> (car contents) package-archive-version)
771 (error "Package archive version %d is greater than %d - upgrade package.el"
772 (car contents) package-archive-version))
773 (cdr contents))))))
774
775(defun package-read-archive-contents ()
776 "Re-read `archive-contents' and `builtin-packages', if they exist.
777Set `package-archive-contents' and `package--builtins' if successful.
778Throw an error if the archive version is too new."
779 (let ((archive-contents (package--read-archive-file "archive-contents"))
780 (builtins (package--read-archive-file "builtin-packages")))
781 (if archive-contents
782 ;; Version 1 of 'archive-contents' is identical to our
783 ;; internal representation.
784 (setq package-archive-contents archive-contents))
785 (if builtins
786 ;; Version 1 of 'builtin-packages' is a list where the car is
787 ;; a split emacs version and the cdr is an alist suitable for
788 ;; package--builtins.
789 (let ((our-version (package-version-split emacs-version))
790 (result package--builtins-base))
791 (setq package--builtins
792 (dolist (elt builtins result)
793 (if (package-version-compare our-version (car elt) '>=)
794 (setq result (append (cdr elt) result)))))))))
795
796(defun package-download-transaction (transaction)
797 "Download and install all the packages in the given transaction."
798 (dolist (elt transaction)
799 (let* ((desc (cdr (assq elt package-archive-contents)))
800 ;; As an exception, if package is "held" in
801 ;; `package-load-list', download the held version.
802 (hold (cadr (assq elt package-load-list)))
803 (v-string (or (and (stringp hold) hold)
804 (package-version-join (package-desc-vers desc))))
805 (kind (package-desc-kind desc)))
806 (cond
807 ((eq kind 'tar)
808 (package-download-tar elt v-string))
809 ((eq kind 'single)
810 (package-download-single elt v-string
811 (package-desc-doc desc)
812 (package-desc-reqs desc)))
813 (t
814 (error "Unknown package kind: %s" (symbol-name kind)))))))
815
816;;;###autoload
817(defun package-install (name)
818 "Install the package named NAME.
819Interactively, prompt for the package name.
820The package is found on the archive site, see `package-archive-base'."
821 (interactive
822 (list (progn
823 ;; Make sure we're using the most recent download of the
824 ;; archive. Maybe we should be updating the archive first?
825 (package-read-archive-contents)
826 (intern (completing-read "Install package: "
827 (mapcar (lambda (elt)
828 (cons (symbol-name (car elt))
829 nil))
830 package-archive-contents)
831 nil t)))))
832 (let ((pkg-desc (assq name package-archive-contents)))
833 (unless pkg-desc
834 (error "Package '%s' not available for installation"
835 (symbol-name name)))
836 (let ((transaction
837 (package-compute-transaction (list name)
838 (package-desc-reqs (cdr pkg-desc)))))
839 (package-download-transaction transaction)))
840 ;; Try to activate it.
841 (package-initialize))
842
843(defun package-strip-rcs-id (v-str)
844 "Strip RCS version ID from the version string.
845If the result looks like a dotted numeric version, return it.
846Otherwise return nil."
847 (if v-str
848 (if (string-match "^[ \t]*[$]Revision:[ \t]\([0-9.]+\)[ \t]*[$]$" v-str)
849 (match-string 1 v-str)
850 (if (string-match "^[0-9.]*$" v-str)
851 v-str))))
852
853(defun package-buffer-info ()
854 "Return a vector of information about the package in the current buffer.
855The vector looks like [FILENAME REQUIRES DESCRIPTION VERSION COMMENTARY]
856FILENAME is the file name, a string. It does not have the \".el\" extension.
857REQUIRES is a requires list, or nil.
858DESCRIPTION is the package description (a string).
859VERSION is the version, a string.
860COMMENTARY is the commentary section, a string, or nil if none.
861Throws an exception if the buffer does not contain a conforming package.
862If there is a package, narrows the buffer to the file's boundaries.
863May narrow buffer or move point even on failure."
864 (goto-char (point-min))
865 (if (re-search-forward "^;;; \\([^ ]*\\)\\.el --- \\(.*\\)$" nil t)
866 (let ((file-name (match-string 1))
867 (desc (match-string 2))
868 (start (progn (beginning-of-line) (point))))
869 (if (search-forward (concat ";;; " file-name ".el ends here"))
870 (progn
871 ;; Try to include a trailing newline.
872 (forward-line)
873 (narrow-to-region start (point))
874 (require 'lisp-mnt)
875 ;; Use some headers we've invented to drive the process.
876 (let* ((requires-str (lm-header "package-requires"))
877 (requires (if requires-str
878 (package-read-from-string requires-str)))
879 ;; Prefer Package-Version, because if it is
880 ;; defined the package author probably wants us
881 ;; to use it. Otherwise try Version.
882 (pkg-version
883 (or (package-strip-rcs-id (lm-header "package-version"))
884 (package-strip-rcs-id (lm-header "version"))))
885 (commentary (lm-commentary)))
886 (unless pkg-version
887 (error
888 "Package does not define a usable \"Version\" or \"Package-Version\" header"))
889 ;; Turn string version numbers into list form.
890 (setq requires
891 (mapcar
892 (lambda (elt)
893 (list (car elt)
894 (package-version-split (car (cdr elt)))))
895 requires))
896 (set-text-properties 0 (length file-name) nil file-name)
897 (set-text-properties 0 (length pkg-version) nil pkg-version)
898 (set-text-properties 0 (length desc) nil desc)
899 (vector file-name requires desc pkg-version commentary)))
900 (error "Package missing a terminating comment")))
901 (error "No starting comment for package")))
902
903(defun package-tar-file-info (file)
904 "Find package information for a tar file.
905FILE is the name of the tar file to examine.
906The return result is a vector like `package-buffer-info'."
907 (unless (string-match "^\\(.+\\)-\\([0-9.]+\\)\\.tar$" file)
908 (error "`%s' doesn't have a package-ish name" file))
909 (let* ((pkg-name (file-name-nondirectory (match-string-no-properties 1 file)))
910 (pkg-version (match-string-no-properties 2 file))
911 ;; Extract the package descriptor.
912 (pkg-def-contents (shell-command-to-string
913 ;; Requires GNU tar.
914 (concat "tar -xOf " file " "
915 pkg-name "-" pkg-version "/"
916 pkg-name "-pkg.el")))
917 (pkg-def-parsed (package-read-from-string pkg-def-contents)))
918 (unless (eq (car pkg-def-parsed) 'define-package)
919 (error "%s-pkg.el doesn't contain `define-package' sexp" pkg-name))
920 (let ((name-str (nth 1 pkg-def-parsed))
921 (version-string (nth 2 pkg-def-parsed))
922 (docstring (nth 3 pkg-def-parsed))
923 (requires (nth 4 pkg-def-parsed))
924
925 (readme (shell-command-to-string
926 ;; Requires GNU tar.
927 (concat "tar -xOf " file " "
928 pkg-name "-" pkg-version "/README"))))
929 (unless (equal pkg-version version-string)
930 (error "Inconsistent versions!"))
931 (unless (equal pkg-name name-str)
932 (error "Inconsistent names!"))
933 ;; Kind of a hack.
934 (if (string-match ": Not found in archive" readme)
935 (setq readme nil))
936 ;; Turn string version numbers into list form.
937 (if (eq (car requires) 'quote)
938 (setq requires (car (cdr requires))))
939 (setq requires
940 (mapcar
941 (lambda (elt)
942 (list (car elt)
943 (package-version-split (car (cdr elt)))))
944 requires))
945 (vector pkg-name requires docstring version-string readme))))
946
947(defun package-install-buffer-internal (pkg-info type)
948 (save-excursion
949 (save-restriction
950 (let* ((file-name (aref pkg-info 0))
951 (requires (aref pkg-info 1))
952 (desc (if (string= (aref pkg-info 2) "")
953 "No description available."
954 (aref pkg-info 2)))
955 (pkg-version (aref pkg-info 3)))
956 ;; Download and install the dependencies.
957 (let ((transaction (package-compute-transaction nil requires)))
958 (package-download-transaction transaction))
959 ;; Install the package itself.
960 (cond
961 ((eq type 'single)
962 (package-unpack-single file-name pkg-version desc requires))
963 ((eq type 'tar)
964 (package-unpack (intern file-name) pkg-version))
965 (t
966 (error "Unknown type: %s" (symbol-name type))))
967 ;; Try to activate it.
968 (package-initialize)))))
969
970;;;###autoload
971(defun package-install-from-buffer ()
972 "Install a package from the current buffer.
973The package is assumed to be a single .el file which
974follows the elisp comment guidelines; see
975info node `(elisp)Library Headers'."
976 (interactive)
977 (package-install-buffer-internal (package-buffer-info) 'single))
978
979;;;###autoload
980(defun package-install-file (file)
981 "Install a package from a file.
982The file can either be a tar file or an Emacs Lisp file."
983 (interactive "fPackage file name: ")
984 (with-temp-buffer
985 (insert-file-contents-literally file)
986 (cond
987 ((string-match "\\.el$" file) (package-install-from-buffer))
988 ((string-match "\\.tar$" file)
989 (package-install-buffer-internal (package-tar-file-info file) 'tar))
990 (t (error "Unrecognized extension `%s'" (file-name-extension file))))))
991
992(defun package-delete (name version)
993 (require 'dired) ; for dired-delete-file
994 (dired-delete-file (expand-file-name (concat name "-" version)
995 package-user-dir)
996 ;; FIXME: query user?
997 'always))
998
999(defun package--download-one-archive (file)
1000 "Download a single archive file and cache it locally."
1001 (let ((buffer (url-retrieve-synchronously
1002 (concat package-archive-base file))))
1003 (with-current-buffer buffer
1004 (package-handle-response)
1005 (re-search-forward "^$" nil 'move)
1006 (forward-char)
1007 (delete-region (point-min) (point))
1008 (setq buffer-file-name (concat (file-name-as-directory package-user-dir)
1009 file))
1010 (let ((version-control 'never))
1011 (save-buffer))
1012 (kill-buffer buffer))))
1013
1014(defun package-refresh-contents ()
1015 "Download the ELPA archive description if needed.
1016Invoking this will ensure that Emacs knows about the latest versions
1017of all packages. This will let Emacs make them available for
1018download."
1019 (interactive)
1020 (unless (file-exists-p package-user-dir)
1021 (make-directory package-user-dir t))
1022 (package--download-one-archive "archive-contents")
1023 (package--download-one-archive "builtin-packages")
1024 (package-read-archive-contents))
1025
1026;;;###autoload
1027(defun package-initialize ()
1028 "Load Emacs Lisp packages, and activate them.
1029The variable `package-load-list' controls which packages to load."
1030 (interactive)
1031 (setq package-obsolete-alist nil)
1032 (package-load-all-descriptors)
1033 (package-read-archive-contents)
1034 ;; Try to activate all our packages.
1035 (mapc (lambda (elt)
1036 (package-activate (car elt) (package-desc-vers (cdr elt))))
1037 package-alist))
1038
1039\f
cced7584 1040;;;; Package description buffer.
44198b6e 1041
cced7584
CY
1042;;;###autoload
1043(defun describe-package (package)
1044 "Display the full documentation of PACKAGE (a symbol)."
1045 (interactive
1046 (let* ((packages (append (mapcar 'car package-alist)
1047 (mapcar 'car package-archive-contents)))
1048 (guess (function-called-at-point))
1049 val)
1050 (unless (memq guess packages)
1051 (setq guess nil))
1052 (setq packages (mapcar 'symbol-name packages))
1053 (setq val
1054 (completing-read (if guess
1055 (format "Describe package (default %s): "
1056 guess)
1057 "Describe package: ")
1058 packages nil t nil nil guess))
1059 (list (if (equal val "")
1060 guess
1061 (intern val)))))
1062 (if (or (null package) (null (symbolp package)))
1063 (message "You did not specify a package")
1064 (help-setup-xref (list #'describe-package package)
1065 (called-interactively-p 'interactive))
1066 (with-help-window (help-buffer)
1067 (with-current-buffer standard-output
1068 (describe-package-1 package)))))
1069
1070(defun describe-package-1 (package)
1071 (let ((desc (cdr (assq package package-alist)))
8adb4c33 1072 reqs version installable)
cced7584
CY
1073 (prin1 package)
1074 (princ " is ")
1075 (cond
1076 (desc
1077 ;; This package is loaded (i.e. in `package-alist').
1078 (let (pkg-dir)
1079 (setq version (package-version-join (package-desc-vers desc)))
1080 (if (assq package package--builtins)
1081 (princ "a built-in package.\n\n")
1082 (setq pkg-dir (package--dir (symbol-name package) version))
1083 (if pkg-dir
1084 (progn
1085 (insert "a package installed in `")
1086 (help-insert-xref-button (file-name-as-directory pkg-dir)
1087 'help-package-def pkg-dir)
1088 (insert "'.\n\n"))
1089 ;; This normally does not happen.
1090 (insert "a deleted package.\n\n")
1091 (setq version nil)))))
1092 (t
1093 ;; An uninstalled package.
8adb4c33
CY
1094 (setq desc (cdr (assq package package-archive-contents))
1095 version (package-version-join (package-desc-vers desc))
1096 installable t)
1097 (insert "an installable package.\n\n")))
cced7584
CY
1098 (if version
1099 (insert " Version: " version "\n"))
8adb4c33
CY
1100 (setq reqs (package-desc-reqs desc))
1101 (when reqs
1102 (insert " Requires: ")
1103 (let ((first t)
1104 name vers text)
1105 (dolist (req reqs)
1106 (setq name (car req)
1107 vers (cadr req)
1108 text (format "%s-%s" (symbol-name name)
1109 (package-version-join vers)))
1110 (cond (first (setq first nil))
1111 ((>= (+ 2 (current-column) (length text))
1112 (window-width))
1113 (insert ",\n "))
1114 (t (insert ", ")))
1115 (help-insert-xref-button text 'help-package name))
1116 (insert "\n")))
1117 (insert " Description: " (package-desc-doc desc) "\n")
1118 ;; Todo: button for uninstalling a package.
1119 (when installable
1120 (let ((button-text (if (display-graphic-p)
1121 "Install"
1122 "[Install]"))
1123 (button-face (if (display-graphic-p)
1124 '(:box (:line-width 2 :color "dark grey")
1125 :background "light grey"
1126 :foreground "black")
1127 'link)))
1128 (insert "\n")
1129 (insert-text-button button-text
1130 'face button-face
1131 'follow-link t
1132 'package-symbol package
1133 'action (lambda (button)
1134 (package-install
1135 (button-get button 'package-symbol))
1136 (revert-buffer nil t)
1137 (goto-char (point-min))))
1138 (insert "\n")))))
cced7584
CY
1139
1140\f
44198b6e
CY
1141;;;; Package menu mode.
1142
54ea2a0d 1143(defvar package-menu-mode-map
64eba874
DN
1144 (let ((map (make-keymap))
1145 (menu-map (make-sparse-keymap "Package")))
54ea2a0d 1146 (suppress-keymap map)
8adb4c33 1147 (define-key map "\C-m" 'package-menu-describe-package)
54ea2a0d
JB
1148 (define-key map "q" 'quit-window)
1149 (define-key map "n" 'next-line)
1150 (define-key map "p" 'previous-line)
1151 (define-key map "u" 'package-menu-mark-unmark)
1152 (define-key map "\177" 'package-menu-backup-unmark)
1153 (define-key map "d" 'package-menu-mark-delete)
1154 (define-key map "i" 'package-menu-mark-install)
1155 (define-key map "g" 'package-menu-revert)
1156 (define-key map "r" 'package-menu-refresh)
1157 (define-key map "~" 'package-menu-mark-obsolete-for-deletion)
1158 (define-key map "x" 'package-menu-execute)
1159 (define-key map "h" 'package-menu-quick-help)
1160 (define-key map "?" 'package-menu-view-commentary)
64eba874
DN
1161 (define-key map [menu-bar package-menu] (cons "Package" menu-map))
1162 (define-key menu-map [mq]
1163 '(menu-item "Quit" quit-window
1164 :help "Quit package selection"))
1165 (define-key menu-map [s1] '("--"))
1166 (define-key menu-map [mn]
1167 '(menu-item "Next" next-line
1168 :help "Next Line"))
1169 (define-key menu-map [mp]
1170 '(menu-item "Previous" previous-line
1171 :help "Previous Line"))
1172 (define-key menu-map [s2] '("--"))
1173 (define-key menu-map [mu]
1174 '(menu-item "Unmark" package-menu-mark-unmark
1175 :help "Clear any marks on a package and move to the next line"))
1176 (define-key menu-map [munm]
1177 '(menu-item "Unmark backwards" package-menu-backup-unmark
1178 :help "Back up one line and clear any marks on that package"))
1179 (define-key menu-map [md]
1180 '(menu-item "Mark for deletion" package-menu-mark-delete
1181 :help "Mark a package for deletion and move to the next line"))
1182 (define-key menu-map [mi]
1183 '(menu-item "Mark for install" package-menu-mark-install
1184 :help "Mark a package for installation and move to the next line"))
1185 (define-key menu-map [s3] '("--"))
1186 (define-key menu-map [mg]
1187 '(menu-item "Update package list" package-menu-revert
1188 :help "Update the list of packages"))
1189 (define-key menu-map [mr]
1190 '(menu-item "Refresh package list" package-menu-refresh
1191 :help "Download the ELPA archive"))
1192 (define-key menu-map [s4] '("--"))
1193 (define-key menu-map [mt]
1194 '(menu-item "Mark obsolete packages" package-menu-mark-obsolete-for-deletion
1195 :help "Mark all obsolete packages for deletion"))
1196 (define-key menu-map [mx]
1197 '(menu-item "Execute actions" package-menu-execute
1198 :help "Perform all the marked actions"))
1199 (define-key menu-map [s5] '("--"))
1200 (define-key menu-map [mh]
1201 '(menu-item "Help" package-menu-quick-help
1202 :help "Show short key binding help for package-menu-mode"))
1203 (define-key menu-map [mc]
1204 '(menu-item "View Commentary" package-menu-view-commentary
1205 :help "Display information about this package"))
54ea2a0d 1206 map)
44198b6e
CY
1207 "Local keymap for `package-menu-mode' buffers.")
1208
44198b6e
CY
1209(defvar package-menu-sort-button-map
1210 (let ((map (make-sparse-keymap)))
1211 (define-key map [header-line mouse-1] 'package-menu-sort-by-column)
1212 (define-key map [follow-link] 'mouse-face)
1213 map)
1214 "Local keymap for package menu sort buttons.")
1215
1216(put 'package-menu-mode 'mode-class 'special)
1217
1218(defun package-menu-mode ()
1219 "Major mode for browsing a list of packages.
1220Letters do not insert themselves; instead, they are commands.
1221\\<package-menu-mode-map>
1222\\{package-menu-mode-map}"
1223 (kill-all-local-variables)
1224 (use-local-map package-menu-mode-map)
1225 (setq major-mode 'package-menu-mode)
1226 (setq mode-name "Package Menu")
1227 (setq truncate-lines t)
1228 (setq buffer-read-only t)
1229 ;; Support Emacs 21.
1230 (if (fboundp 'run-mode-hooks)
1231 (run-mode-hooks 'package-menu-mode-hook)
1232 (run-hooks 'package-menu-mode-hook)))
1233
1234(defun package-menu-refresh ()
1235 "Download the ELPA archive.
1236This fetches the file describing the current contents of
1237the Emacs Lisp Package Archive, and then refreshes the
1238package menu. This lets you see what new packages are
1239available for download."
1240 (interactive)
1241 (package-refresh-contents)
1242 (package-list-packages-internal))
1243
1244(defun package-menu-revert ()
1245 "Update the list of packages."
1246 (interactive)
1247 (package-list-packages-internal))
1248
8adb4c33
CY
1249(defun package-menu-describe-package ()
1250 "Describe the package in the current line."
1251 (interactive)
1252 (let ((name (package-menu-get-package)))
1253 (if name
1254 (describe-package (intern name))
1255 (message "No package on this line"))))
1256
44198b6e
CY
1257(defun package-menu-mark-internal (what)
1258 (unless (eobp)
1259 (let ((buffer-read-only nil))
1260 (beginning-of-line)
1261 (delete-char 1)
1262 (insert what)
1263 (forward-line))))
1264
1265;; fixme numeric argument
1266(defun package-menu-mark-delete (num)
1267 "Mark a package for deletion and move to the next line."
1268 (interactive "p")
1269 (package-menu-mark-internal "D"))
1270
1271(defun package-menu-mark-install (num)
1272 "Mark a package for installation and move to the next line."
1273 (interactive "p")
1274 (package-menu-mark-internal "I"))
1275
1276(defun package-menu-mark-unmark (num)
1277 "Clear any marks on a package and move to the next line."
1278 (interactive "p")
1279 (package-menu-mark-internal " "))
1280
1281(defun package-menu-backup-unmark ()
1282 "Back up one line and clear any marks on that package."
1283 (interactive)
1284 (forward-line -1)
1285 (package-menu-mark-internal " ")
1286 (forward-line -1))
1287
1288(defun package-menu-mark-obsolete-for-deletion ()
1289 "Mark all obsolete packages for deletion."
1290 (interactive)
1291 (save-excursion
1292 (goto-char (point-min))
1293 (forward-line 2)
1294 (while (not (eobp))
1295 (if (looking-at ".*\\s obsolete\\s ")
1296 (package-menu-mark-internal "D")
1297 (forward-line 1)))))
1298
1299(defun package-menu-quick-help ()
1300 "Show short key binding help for package-menu-mode."
1301 (interactive)
1302 (message "n-ext, i-nstall, d-elete, u-nmark, x-ecute, r-efresh, h-elp"))
1303
1304(defun package-menu-view-commentary ()
1305 "Display information about this package.
1306For single-file packages, shows the commentary section from the header.
1307For larger packages, shows the README file."
1308 (interactive)
1309 (let* (start-point ok
1310 (pkg-name (package-menu-get-package))
1311 (buffer (url-retrieve-synchronously (concat package-archive-base
1312 pkg-name
1313 "-readme.txt"))))
1314 (with-current-buffer buffer
1315 ;; FIXME: it would be nice to work with any URL type.
1316 (setq start-point url-http-end-of-headers)
1317 (setq ok (eq (url-http-parse-response) 200)))
1318 (let ((new-buffer (get-buffer-create "*Package Info*")))
1319 (with-current-buffer new-buffer
1320 (let ((buffer-read-only nil))
1321 (erase-buffer)
1322 (insert "Package information for " pkg-name "\n\n")
1323 (if ok
1324 (insert-buffer-substring buffer start-point)
1325 (insert "This package does not have a README file or commentary comment.\n"))
1326 (goto-char (point-min))
1327 (view-mode)))
1328 (display-buffer new-buffer t))))
1329
1330;; Return the name of the package on the current line.
1331(defun package-menu-get-package ()
1332 (save-excursion
1333 (beginning-of-line)
1334 (if (looking-at ". \\([^ \t]*\\)")
8adb4c33 1335 (match-string-no-properties 1))))
44198b6e
CY
1336
1337;; Return the version of the package on the current line.
1338(defun package-menu-get-version ()
1339 (save-excursion
1340 (beginning-of-line)
1341 (if (looking-at ". [^ \t]*[ \t]*\\([0-9.]*\\)")
1342 (match-string 1))))
1343
1344(defun package-menu-get-status ()
1345 (save-excursion
1346 (if (looking-at ". [^ \t]*[ \t]*[^ \t]*[ \t]*\\([^ \t]*\\)")
1347 (match-string 1)
1348 "")))
1349
1350(defun package-menu-execute ()
1351 "Perform all the marked actions.
1352Packages marked for installation will be downloaded and
1353installed. Packages marked for deletion will be removed.
1354Note that after installing packages you will want to restart
1355Emacs."
1356 (interactive)
1357 (goto-char (point-min))
1358 (forward-line 2)
1359 (while (not (eobp))
1360 (let ((cmd (char-after))
1361 (pkg-name (package-menu-get-package))
1362 (pkg-vers (package-menu-get-version))
1363 (pkg-status (package-menu-get-status)))
1364 (cond
1365 ((eq cmd ?D)
1366 (when (and (string= pkg-status "installed")
1367 (string= pkg-name "package"))
1368 ;; FIXME: actually, we could be tricky and remove all info.
1369 ;; But that is drastic and the user can do that instead.
1370 (error "Can't delete most recent version of `package'"))
1371 ;; Ask for confirmation here? Maybe if package status is ""?
1372 ;; Or if any lisp from package is actually loaded?
1373 (message "Deleting %s-%s..." pkg-name pkg-vers)
1374 (package-delete pkg-name pkg-vers)
1375 (message "Deleting %s-%s... done" pkg-name pkg-vers))
1376 ((eq cmd ?I)
1377 (package-install (intern pkg-name)))))
1378 (forward-line))
1379 (package-menu-revert))
1380
1381(defun package-print-package (package version key desc)
1382 (let ((face
1383 (cond ((eq package 'emacs) 'font-lock-builtin-face)
1384 ((string= key "available") 'default)
1385 ((string= key "held") 'font-lock-constant-face)
1386 ((string= key "disabled") 'font-lock-warning-face)
1387 ((string= key "installed") 'font-lock-comment-face)
1388 (t ; obsolete, but also the default.
1389 'font-lock-warning-face))))
1390 (insert (propertize " " 'font-lock-face face))
8adb4c33
CY
1391 (insert-text-button (symbol-name package)
1392 'face 'link
1393 'follow-link t
1394 'package-symbol package
1395 'action (lambda (button)
1396 (describe-package
1397 (button-get button 'package-symbol))))
44198b6e
CY
1398 (indent-to 20 1)
1399 (insert (propertize (package-version-join version) 'font-lock-face face))
8adb4c33 1400 (indent-to 32 1)
44198b6e
CY
1401 (insert (propertize key 'font-lock-face face))
1402 ;; FIXME: this 'when' is bogus...
1403 (when desc
8adb4c33 1404 (indent-to 43 1)
44198b6e
CY
1405 (insert (propertize desc 'font-lock-face face)))
1406 (insert "\n")))
1407
1408(defun package-list-maybe-add (package version status description result)
1409 (unless (assoc (cons package version) result)
1410 (setq result (cons (list (cons package version) status description)
1411 result)))
1412 result)
1413
1414;; This decides how we should sort; nil means by package name.
1415(defvar package-menu-sort-key nil)
1416
1417(defun package-list-packages-internal ()
1418 (package-initialize) ; FIXME: do this here?
1419 (with-current-buffer (get-buffer-create "*Packages*")
1420 (setq buffer-read-only nil)
1421 (erase-buffer)
1422 (let ((info-list)
1423 name desc hold)
1424 ;; List installed packages
1425 (dolist (elt package-alist)
1426 (setq name (car elt)
1427 desc (cdr elt)
1428 hold (assq name package-load-list))
1429 (setq info-list
1430 (package-list-maybe-add name (package-desc-vers desc)
1431 ;; FIXME: it turns out to be
1432 ;; tricky to see if this package
1433 ;; is presently activated.
1434 (if (stringp (cadr hold))
1435 "held"
1436 "installed")
1437 (package-desc-doc desc)
1438 info-list)))
1439 ;; List available packages
1440 (dolist (elt package-archive-contents)
1441 (setq name (car elt)
1442 desc (cdr elt)
1443 hold (assq name package-load-list))
1444 (unless (and hold (stringp (cadr hold))
1445 (package-installed-p
1446 name (package-version-split (cadr hold))))
1447 (setq info-list
1448 (package-list-maybe-add name
1449 (package-desc-vers desc)
1450 (if (and hold (null (cadr hold)))
1451 "disabled"
1452 "available")
1453 (package-desc-doc (cdr elt))
1454 info-list))))
1455 ;; List obsolete packages
1456 (mapc (lambda (elt)
1457 (mapc (lambda (inner-elt)
1458 (setq info-list
1459 (package-list-maybe-add (car elt)
1460 (package-desc-vers
1461 (cdr inner-elt))
1462 "obsolete"
1463 (package-desc-doc
1464 (cdr inner-elt))
1465 info-list)))
1466 (cdr elt)))
1467 package-obsolete-alist)
1468 (let ((selector (cond
1469 ((string= package-menu-sort-key "Version")
1470 ;; FIXME this doesn't work.
1471 #'(lambda (e) (cdr (car e))))
1472 ((string= package-menu-sort-key "Status")
1473 #'(lambda (e) (car (cdr e))))
1474 ((string= package-menu-sort-key "Description")
1475 #'(lambda (e) (car (cdr (cdr e)))))
1476 (t ; "Package" is default.
1477 #'(lambda (e) (symbol-name (car (car e))))))))
1478 (setq info-list
1479 (sort info-list
1480 (lambda (left right)
1481 (let ((vleft (funcall selector left))
1482 (vright (funcall selector right)))
1483 (string< vleft vright))))))
1484 (mapc (lambda (elt)
1485 (package-print-package (car (car elt))
1486 (cdr (car elt))
1487 (car (cdr elt))
1488 (car (cdr (cdr elt)))))
1489 info-list))
1490 (goto-char (point-min))
1491 (current-buffer)))
1492
1493(defun package-menu-sort-by-column (&optional e)
1494 "Sort the package menu by the last column clicked on."
1495 (interactive (list last-input-event))
1496 (if e (mouse-select-window e))
1497 (let* ((pos (event-start e))
1498 (obj (posn-object pos))
1499 (col (if obj
1500 (get-text-property (cdr obj) 'column-name (car obj))
1501 (get-text-property (posn-point pos) 'column-name))))
1502 (setq package-menu-sort-key col))
1503 (package-list-packages-internal))
1504
1505(defun package--list-packages ()
1506 "Display a list of packages.
1507Helper function that does all the work for the user-facing functions."
1508 (with-current-buffer (package-list-packages-internal)
1509 (package-menu-mode)
1510 ;; Set up the header line.
1511 (setq header-line-format
1512 (mapconcat
1513 (lambda (pair)
1514 (let ((column (car pair))
1515 (name (cdr pair)))
1516 (concat
1517 ;; Insert a space that aligns the button properly.
1518 (propertize " " 'display (list 'space :align-to column)
1519 'face 'fixed-pitch)
1520 ;; Set up the column button.
1521 (if (string= name "Version")
1522 name
1523 (propertize name
1524 'column-name name
1525 'help-echo "mouse-1: sort by column"
1526 'mouse-face 'highlight
1527 'keymap package-menu-sort-button-map)))))
1528 ;; We take a trick from buff-menu and have a dummy leading
1529 ;; space to align the header line with the beginning of the
1530 ;; text. This doesn't really work properly on Emacs 21,
1531 ;; but it is close enough.
1532 '((0 . "")
1533 (2 . "Package")
1534 (20 . "Version")
1535 (30 . "Status")
1536 (41 . "Description"))
1537 ""))
1538
1539 ;; It's okay to use pop-to-buffer here. The package menu buffer
1540 ;; has keybindings, and the user just typed 'M-x
1541 ;; package-list-packages', suggesting that they might want to use
1542 ;; them.
1543 (pop-to-buffer (current-buffer))))
1544
1545;;;###autoload
1546(defun package-list-packages ()
1547 "Display a list of packages.
1548Fetches the updated list of packages before displaying.
1549The list is displayed in a buffer named `*Packages*'."
1550 (interactive)
1551 (package-refresh-contents)
1552 (package--list-packages))
1553
1554(defun package-list-packages-no-fetch ()
1555 "Display a list of packages.
1556Does not fetch the updated list of packages before displaying.
1557The list is displayed in a buffer named `*Packages*'."
1558 (interactive)
1559 (package--list-packages))
1560
44198b6e
CY
1561(provide 'package)
1562
1563;;; package.el ends here