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