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