Fix previous calendar-in-read-only-buffer change.
[emacs.git] / lisp / emacs-lisp / package.el
blob66370c643bf6d365e8ce9fc41fd80f90b7eb8a3a
1 ;;; package.el --- Simple package system for Emacs
3 ;; Copyright (C) 2007-2012 Free Software Foundation, Inc.
5 ;; Author: Tom Tromey <tromey@redhat.com>
6 ;; Created: 10 Mar 2007
7 ;; Version: 1.0
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 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-install-from-buffer
88 ;; Install a package consisting of a single .el file that appears
89 ;; in the current buffer. This only works for packages which
90 ;; define a Version header properly; package.el also supports the
91 ;; extension headers Package-Version (in case Version is an RCS id
92 ;; or similar), and Package-Requires (if the package requires other
93 ;; packages).
95 ;; M-x package-install-file
96 ;; Install a package from the indicated file. The package can be
97 ;; either a tar file or a .el file. A tar file must contain an
98 ;; appropriately-named "-pkg.el" file; a .el file must be properly
99 ;; formatted as with package-install-from-buffer.
101 ;;; Thanks:
102 ;;; (sorted by sort-lines):
104 ;; Jim Blandy <jimb@red-bean.com>
105 ;; Karl Fogel <kfogel@red-bean.com>
106 ;; Kevin Ryde <user42@zip.com.au>
107 ;; Lawrence Mitchell
108 ;; Michael Olson <mwolson@member.fsf.org>
109 ;; Sebastian Tennant <sebyte@smolny.plus.com>
110 ;; Stefan Monnier <monnier@iro.umontreal.ca>
111 ;; Vinicius Jose Latorre <viniciusjl@ig.com.br>
112 ;; Phil Hagelberg <phil@hagelb.org>
114 ;;; ToDo:
116 ;; - a trust mechanism, since compiling a package can run arbitrary code.
117 ;; For example, download package signatures and check that they match.
118 ;; - putting info dirs at the start of the info path means
119 ;; users see a weird ordering of categories. OTOH we want to
120 ;; override later entries. maybe emacs needs to enforce
121 ;; the standard layout?
122 ;; - put bytecode in a separate directory tree
123 ;; - perhaps give users a way to recompile their bytecode
124 ;; or do it automatically when emacs changes
125 ;; - give users a way to know whether a package is installed ok
126 ;; - give users a way to view a package's documentation when it
127 ;; only appears in the .el
128 ;; - use/extend checkdoc so people can tell if their package will work
129 ;; - "installed" instead of a blank in the status column
130 ;; - tramp needs its files to be compiled in a certain order.
131 ;; how to handle this? fix tramp?
132 ;; - on emacs 21 we don't kill the -autoloads.el buffer. what about 22?
133 ;; - maybe we need separate .elc directories for various emacs versions
134 ;; and also emacs-vs-xemacs. That way conditional compilation can
135 ;; work. But would this break anything?
136 ;; - should store the package's keywords in archive-contents, then
137 ;; let the users filter the package-menu by keyword. See
138 ;; finder-by-keyword. (We could also let people view the
139 ;; Commentary, but it isn't clear how useful this is.)
140 ;; - William Xu suggests being able to open a package file without
141 ;; installing it
142 ;; - Interface with desktop.el so that restarting after an install
143 ;; works properly
144 ;; - Implement M-x package-upgrade, to upgrade any/all existing packages
145 ;; - Use hierarchical layout. PKG/etc PKG/lisp PKG/info
146 ;; ... except maybe lisp?
147 ;; - It may be nice to have a macro that expands to the package's
148 ;; private data dir, aka ".../etc". Or, maybe data-directory
149 ;; needs to be a list (though this would be less nice)
150 ;; a few packages want this, eg sokoban
151 ;; - package menu needs:
152 ;; ability to know which packages are built-in & thus not deletable
153 ;; it can sometimes print odd results, like 0.3 available but 0.4 active
154 ;; why is that?
155 ;; - Allow multiple versions on the server...?
156 ;; [ why bother? ]
157 ;; - Don't install a package which will invalidate dependencies overall
158 ;; - Allow something like (or (>= emacs 21.0) (>= xemacs 21.5))
159 ;; [ currently thinking, why bother.. KISS ]
160 ;; - Allow optional package dependencies
161 ;; then if we require 'bbdb', bbdb-specific lisp in lisp/bbdb
162 ;; and just don't compile to add to load path ...?
163 ;; - Have a list of archive URLs? [ maybe there's no point ]
164 ;; - David Kastrup pointed out on the xemacs list that for GPL it
165 ;; is friendlier to ship the source tree. We could "support" that
166 ;; by just having a "src" subdir in the package. This isn't ideal
167 ;; but it probably is not worth trying to support random source
168 ;; tree layouts, build schemes, etc.
169 ;; - Our treatment of the info path is somewhat bogus
170 ;; - perhaps have an "unstable" tree in ELPA as well as a stable one
172 ;;; Code:
174 (require 'tabulated-list)
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 (defvar url-http-end-of-headers)
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.
224 Each element has the form (ID . LOCATION).
225 ID is an archive name, as a string.
226 LOCATION specifies the base location for the archive.
227 If it starts with \"http:\", it is treated as a HTTP URL;
228 otherwise it should be an absolute directory name.
229 (Other types of URL are currently not supported.)
231 Only add locations that you trust, since fetching and installing
232 a package can run arbitrary code."
233 :type '(alist :key-type (string :tag "Archive name")
234 :value-type (string :tag "URL or directory name"))
235 :risky t
236 :group 'package
237 :version "24.1")
239 (defconst package-archive-version 1
240 "Version number of the package archive understood by this file.
241 Lower version numbers than this will probably be understood as well.")
243 (defconst package-el-version "1.0"
244 "Version of package.el.")
246 ;; We don't prime the cache since it tends to get out of date.
247 (defvar package-archive-contents nil
248 "Cache of the contents of the Emacs Lisp Package Archive.
249 This is an alist mapping package names (symbols) to package
250 descriptor vectors. These are like the vectors for `package-alist'
251 but have extra entries: one which is 'tar for tar packages and
252 'single for single-file packages, and one which is the name of
253 the archive from which it came.")
254 (put 'package-archive-contents 'risky-local-variable t)
256 (defcustom package-user-dir (locate-user-emacs-file "elpa")
257 "Directory containing the user's Emacs Lisp packages.
258 The directory name should be absolute.
259 Apart from this directory, Emacs also looks for system-wide
260 packages in `package-directory-list'."
261 :type 'directory
262 :risky t
263 :group 'package
264 :version "24.1")
266 (defcustom package-directory-list
267 ;; Defaults are subdirs named "elpa" in the site-lisp dirs.
268 (let (result)
269 (dolist (f load-path)
270 (and (stringp f)
271 (equal (file-name-nondirectory f) "site-lisp")
272 (push (expand-file-name "elpa" f) result)))
273 (nreverse result))
274 "List of additional directories containing Emacs Lisp packages.
275 Each directory name should be absolute.
277 These directories contain packages intended for system-wide; in
278 contrast, `package-user-dir' contains packages for personal use."
279 :type '(repeat directory)
280 :risky t
281 :group 'package
282 :version "24.1")
284 ;; The value is precomputed in finder-inf.el, but don't load that
285 ;; until it's needed (i.e. when `package-initialize' is called).
286 (defvar package--builtins nil
287 "Alist of built-in packages.
288 The actual value is initialized by loading the library
289 `finder-inf'; this is not done until it is needed, e.g. by the
290 function `package-built-in-p'.
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.
294 The vector DESC has the form [VERSION-LIST REQS DOCSTRING].
295 VERSION-LIST is a version list.
296 REQS is a list of packages required by the package, each
297 requirement having the form (NAME VL), where NAME is a string
298 and VL is a version list.
299 DOCSTRING is a brief description of the package.")
300 (put 'package--builtins 'risky-local-variable t)
302 (defvar package-alist nil
303 "Alist of all packages available for activation.
304 Each element has the form (PKG . DESC), where PKG is a package
305 name (a symbol) and DESC is a vector that describes the package.
307 The vector DESC has the form [VERSION-LIST REQS DOCSTRING].
308 VERSION-LIST is a version list.
309 REQS is a list of packages required by the package, each
310 requirement having the form (NAME VL) where NAME is a string
311 and VL is a version list.
312 DOCSTRING is a brief description of the package.
314 This variable is set automatically by `package-load-descriptor',
315 called via `package-initialize'. To change which packages are
316 loaded and/or activated, customize `package-load-list'.")
317 (put 'package-alist 'risky-local-variable t)
319 (defvar package-activated-list nil
320 "List of the names of currently activated packages.")
321 (put 'package-activated-list 'risky-local-variable t)
323 (defvar package-obsolete-alist nil
324 "Representation of obsolete packages.
325 Like `package-alist', but maps package name to a second alist.
326 The inner alist is keyed by version.")
327 (put 'package-obsolete-alist 'risky-local-variable t)
329 (defun package-version-join (vlist)
330 "Return the version string corresponding to the list VLIST.
331 This is, approximately, the inverse of `version-to-list'.
332 \(Actually, it returns only one of the possible inverses, since
333 `version-to-list' is a many-to-one operation.)"
334 (if (null vlist)
336 (let ((str-list (list "." (int-to-string (car vlist)))))
337 (dolist (num (cdr vlist))
338 (cond
339 ((>= num 0)
340 (push (int-to-string num) str-list)
341 (push "." str-list))
342 ((< num -3)
343 (error "Invalid version list `%s'" vlist))
345 ;; pre, or beta, or alpha
346 (cond ((equal "." (car str-list))
347 (pop str-list))
348 ((not (string-match "[0-9]+" (car str-list)))
349 (error "Invalid version list `%s'" vlist)))
350 (push (cond ((= num -1) "pre")
351 ((= num -2) "beta")
352 ((= num -3) "alpha"))
353 str-list))))
354 (if (equal "." (car str-list))
355 (pop str-list))
356 (apply 'concat (nreverse str-list)))))
358 (defun package-strip-version (dirname)
359 "Strip the version from a combined package name and version.
360 E.g., if given \"quux-23.0\", will return \"quux\""
361 (if (string-match (concat "\\`" package-subdirectory-regexp "\\'") dirname)
362 (match-string 1 dirname)))
364 (defun package-load-descriptor (dir package)
365 "Load the description file in directory DIR for package PACKAGE.
366 Here, PACKAGE is a string of the form NAME-VERSION, where NAME is
367 the package name and VERSION is its version."
368 (let* ((pkg-dir (expand-file-name package dir))
369 (pkg-file (expand-file-name
370 (concat (package-strip-version package) "-pkg")
371 pkg-dir)))
372 (when (and (file-directory-p pkg-dir)
373 (file-exists-p (concat pkg-file ".el")))
374 (load pkg-file nil t))))
376 (defun package-load-all-descriptors ()
377 "Load descriptors for installed Emacs Lisp packages.
378 This looks for package subdirectories in `package-user-dir' and
379 `package-directory-list'. The variable `package-load-list'
380 controls which package subdirectories may be loaded.
382 In each valid package subdirectory, this function loads the
383 description file containing a call to `define-package', which
384 updates `package-alist' and `package-obsolete-alist'."
385 (let ((regexp (concat "\\`" package-subdirectory-regexp "\\'")))
386 (dolist (dir (cons package-user-dir package-directory-list))
387 (when (file-directory-p dir)
388 (dolist (subdir (directory-files dir))
389 (when (string-match regexp subdir)
390 (package-maybe-load-descriptor (match-string 1 subdir)
391 (match-string 2 subdir)
392 dir)))))))
394 (defun package-maybe-load-descriptor (name version dir)
395 "Maybe load a specific package from directory DIR.
396 NAME and VERSION are the package's name and version strings.
397 This function checks `package-load-list', before actually loading
398 the package by calling `package-load-descriptor'."
399 (let ((force (assq (intern name) package-load-list))
400 (subdir (concat name "-" version)))
401 (and (file-directory-p (expand-file-name subdir dir))
402 ;; Check `package-load-list':
403 (cond ((null force)
404 (memq 'all package-load-list))
405 ((null (setq force (cadr force)))
406 nil) ; disabled
407 ((eq force t)
409 ((stringp force) ; held
410 (version-list-= (version-to-list version)
411 (version-to-list force)))
413 (error "Invalid element in `package-load-list'")))
414 ;; Actually load the descriptor:
415 (package-load-descriptor dir subdir))))
417 (defsubst package-desc-vers (desc)
418 "Extract version from a package description vector."
419 (aref desc 0))
421 (defsubst package-desc-reqs (desc)
422 "Extract requirements from a package description vector."
423 (aref desc 1))
425 (defsubst package-desc-doc (desc)
426 "Extract doc string from a package description vector."
427 (aref desc 2))
429 (defsubst package-desc-kind (desc)
430 "Extract the kind of download from an archive package description vector."
431 (aref desc 3))
433 (defun package--dir (name version)
434 "Return the directory where a package is installed, or nil if none.
435 NAME and VERSION are both strings."
436 (let* ((subdir (concat name "-" version))
437 (dir-list (cons package-user-dir package-directory-list))
438 pkg-dir)
439 (while dir-list
440 (let ((subdir-full (expand-file-name subdir (car dir-list))))
441 (if (file-directory-p subdir-full)
442 (setq pkg-dir subdir-full
443 dir-list nil)
444 (setq dir-list (cdr dir-list)))))
445 pkg-dir))
447 (defun package-activate-1 (package pkg-vec)
448 (let* ((name (symbol-name package))
449 (version-str (package-version-join (package-desc-vers pkg-vec)))
450 (pkg-dir (package--dir name version-str)))
451 (unless pkg-dir
452 (error "Internal error: unable to find directory for `%s-%s'"
453 name version-str))
454 ;; Add info node.
455 (when (file-exists-p (expand-file-name "dir" pkg-dir))
456 ;; FIXME: not the friendliest, but simple.
457 (require 'info)
458 (info-initialize)
459 (push pkg-dir Info-directory-list))
460 ;; Add to load path, add autoloads, and activate the package.
461 (push pkg-dir load-path)
462 (load (expand-file-name (concat name "-autoloads") pkg-dir) nil t)
463 (push package package-activated-list)
464 ;; Don't return nil.
467 (defun package-built-in-p (package &optional min-version)
468 "Return true if PACKAGE is built-in to Emacs.
469 Optional arg MIN-VERSION, if non-nil, should be a version list
470 specifying the minimum acceptable version."
471 (require 'finder-inf nil t) ; For `package--builtins'.
472 (if (eq package 'emacs)
473 (version-list-<= min-version (version-to-list emacs-version))
474 (let ((elt (assq package package--builtins)))
475 (and elt (version-list-<= min-version
476 (package-desc-vers (cdr elt)))))))
478 ;; This function goes ahead and activates a newer version of a package
479 ;; if an older one was already activated. This is not ideal; we'd at
480 ;; least need to check to see if the package has actually been loaded,
481 ;; and not merely activated.
482 (defun package-activate (package min-version)
483 "Activate package PACKAGE, of version MIN-VERSION or newer.
484 MIN-VERSION should be a version list.
485 If PACKAGE has any dependencies, recursively activate them.
486 Return nil if the package could not be activated."
487 (let ((pkg-vec (cdr (assq package package-alist)))
488 available-version found)
489 ;; Check if PACKAGE is available in `package-alist'.
490 (when pkg-vec
491 (setq available-version (package-desc-vers pkg-vec)
492 found (version-list-<= min-version available-version)))
493 (cond
494 ;; If no such package is found, maybe it's built-in.
495 ((null found)
496 (package-built-in-p package min-version))
497 ;; If the package is already activated, just return t.
498 ((memq package package-activated-list)
500 ;; Otherwise, proceed with activation.
502 (let ((fail (catch 'dep-failure
503 ;; Activate its dependencies recursively.
504 (dolist (req (package-desc-reqs pkg-vec))
505 (unless (package-activate (car req) (cadr req))
506 (throw 'dep-failure req))))))
507 (if fail
508 (warn "Unable to activate package `%s'.
509 Required package `%s-%s' is unavailable"
510 package (car fail) (package-version-join (cadr fail)))
511 ;; If all goes well, activate the package itself.
512 (package-activate-1 package pkg-vec)))))))
514 (defun package-mark-obsolete (package pkg-vec)
515 "Put package on the obsolete list, if not already there."
516 (let ((elt (assq package package-obsolete-alist)))
517 (if elt
518 ;; If this obsolete version does not exist in the list, update
519 ;; it the list.
520 (unless (assoc (package-desc-vers pkg-vec) (cdr elt))
521 (setcdr elt (cons (cons (package-desc-vers pkg-vec) pkg-vec)
522 (cdr elt))))
523 ;; Make a new association.
524 (push (cons package (list (cons (package-desc-vers pkg-vec)
525 pkg-vec)))
526 package-obsolete-alist))))
528 (defun define-package (name-string version-string
529 &optional docstring requirements
530 &rest _extra-properties)
531 "Define a new package.
532 NAME-STRING is the name of the package, as a string.
533 VERSION-STRING is the version of the package, as a string.
534 DOCSTRING is a short description of the package, a string.
535 REQUIREMENTS is a list of dependencies on other packages.
536 Each requirement is of the form (OTHER-PACKAGE OTHER-VERSION),
537 where OTHER-VERSION is a string.
539 EXTRA-PROPERTIES is currently unused."
540 (let* ((name (intern name-string))
541 (version (version-to-list version-string))
542 (new-pkg-desc
543 (cons name
544 (vector version
545 (mapcar
546 (lambda (elt)
547 (list (car elt)
548 (version-to-list (car (cdr elt)))))
549 requirements)
550 docstring)))
551 (old-pkg (assq name package-alist)))
552 (cond
553 ;; If there's no old package, just add this to `package-alist'.
554 ((null old-pkg)
555 (push new-pkg-desc package-alist))
556 ((version-list-< (package-desc-vers (cdr old-pkg)) version)
557 ;; Remove the old package and declare it obsolete.
558 (package-mark-obsolete name (cdr old-pkg))
559 (setq package-alist (cons new-pkg-desc
560 (delq old-pkg package-alist))))
561 ;; You can have two packages with the same version, e.g. one in
562 ;; the system package directory and one in your private
563 ;; directory. We just let the first one win.
564 ((not (version-list-= (package-desc-vers (cdr old-pkg)) version))
565 ;; The package is born obsolete.
566 (package-mark-obsolete name (cdr new-pkg-desc))))))
568 ;; From Emacs 22.
569 (defun package-autoload-ensure-default-file (file)
570 "Make sure that the autoload file FILE exists and if not create it."
571 (unless (file-exists-p file)
572 (write-region
573 (concat ";;; " (file-name-nondirectory file)
574 " --- automatically extracted autoloads\n"
575 ";;\n"
576 ";;; Code:\n\n"
577 "\f\n;; Local Variables:\n"
578 ";; version-control: never\n"
579 ";; no-byte-compile: t\n"
580 ";; no-update-autoloads: t\n"
581 ";; End:\n"
582 ";;; " (file-name-nondirectory file)
583 " ends here\n")
584 nil file))
585 file)
587 (defun package-generate-autoloads (name pkg-dir)
588 (require 'autoload) ;Load before we let-bind generated-autoload-file!
589 (let* ((auto-name (concat name "-autoloads.el"))
590 ;;(ignore-name (concat name "-pkg.el"))
591 (generated-autoload-file (expand-file-name auto-name pkg-dir))
592 (version-control 'never))
593 (unless (fboundp 'autoload-ensure-default-file)
594 (package-autoload-ensure-default-file generated-autoload-file))
595 (update-directory-autoloads pkg-dir)))
597 (defvar tar-parse-info)
598 (declare-function tar-untar-buffer "tar-mode" ())
600 (defun package-untar-buffer (dir)
601 "Untar the current buffer.
602 This uses `tar-untar-buffer' from Tar mode. All files should
603 untar into a directory named DIR; otherwise, signal an error."
604 (require 'tar-mode)
605 (tar-mode)
606 ;; Make sure everything extracts into DIR.
607 (let ((regexp (concat "\\`" (regexp-quote dir) "/")))
608 (dolist (tar-data tar-parse-info)
609 (unless (string-match regexp (aref tar-data 2))
610 (error "Package does not untar cleanly into directory %s/" dir))))
611 (tar-untar-buffer))
613 (defun package-unpack (package version)
614 (let* ((name (symbol-name package))
615 (dirname (concat name "-" version))
616 (pkg-dir (expand-file-name dirname package-user-dir)))
617 (make-directory package-user-dir t)
618 ;; FIXME: should we delete PKG-DIR if it exists?
619 (let* ((default-directory (file-name-as-directory package-user-dir)))
620 (package-untar-buffer dirname)
621 (package--make-autoloads-and-compile name pkg-dir))))
623 (defun package--make-autoloads-and-compile (name pkg-dir)
624 "Generate autoloads and do byte-compilation for package named NAME.
625 PKG-DIR is the name of the package directory."
626 (package-generate-autoloads name pkg-dir)
627 (let ((load-path (cons pkg-dir load-path)))
628 ;; We must load the autoloads file before byte compiling, in
629 ;; case there are magic cookies to set up non-trivial paths.
630 (load (expand-file-name (concat name "-autoloads") pkg-dir) nil t)
631 (byte-recompile-directory pkg-dir 0 t)))
633 (defun package--write-file-no-coding (file-name)
634 (let ((buffer-file-coding-system 'no-conversion))
635 (write-region (point-min) (point-max) file-name)))
637 (defun package-unpack-single (file-name version desc requires)
638 "Install the contents of the current buffer as a package."
639 ;; Special case "package".
640 (if (string= file-name "package")
641 (package--write-file-no-coding
642 (expand-file-name (concat file-name ".el") package-user-dir))
643 (let* ((pkg-dir (expand-file-name (concat file-name "-"
644 (package-version-join
645 (version-to-list version)))
646 package-user-dir))
647 (el-file (expand-file-name (concat file-name ".el") pkg-dir))
648 (pkg-file (expand-file-name (concat file-name "-pkg.el") pkg-dir)))
649 (make-directory pkg-dir t)
650 (package--write-file-no-coding el-file)
651 (let ((print-level nil)
652 (print-length nil))
653 (write-region
654 (concat
655 (prin1-to-string
656 (list 'define-package
657 file-name
658 version
659 desc
660 (list 'quote
661 ;; Turn version lists into string form.
662 (mapcar
663 (lambda (elt)
664 (list (car elt)
665 (package-version-join (cadr elt))))
666 requires))))
667 "\n")
669 pkg-file
670 nil nil nil 'excl))
671 (package--make-autoloads-and-compile file-name pkg-dir))))
673 (defmacro package--with-work-buffer (location file &rest body)
674 "Run BODY in a buffer containing the contents of FILE at LOCATION.
675 LOCATION is the base location of a package archive, and should be
676 one of the URLs (or file names) specified in `package-archives'.
677 FILE is the name of a file relative to that base location.
679 This macro retrieves FILE from LOCATION into a temporary buffer,
680 and evaluates BODY while that buffer is current. This work
681 buffer is killed afterwards. Return the last value in BODY."
682 `(let* ((http (string-match "\\`https?:" ,location))
683 (buffer
684 (if http
685 (url-retrieve-synchronously (concat ,location ,file))
686 (generate-new-buffer "*package work buffer*"))))
687 (prog1
688 (with-current-buffer buffer
689 (if http
690 (progn (package-handle-response)
691 (re-search-forward "^$" nil 'move)
692 (forward-char)
693 (delete-region (point-min) (point)))
694 (unless (file-name-absolute-p ,location)
695 (error "Archive location %s is not an absolute file name"
696 ,location))
697 (insert-file-contents (expand-file-name ,file ,location)))
698 ,@body)
699 (kill-buffer buffer))))
701 (defun package-handle-response ()
702 "Handle the response from a `url-retrieve-synchronously' call.
703 Parse the HTTP response and throw if an error occurred.
704 The url package seems to require extra processing for this.
705 This should be called in a `save-excursion', in the download buffer.
706 It will move point to somewhere in the headers."
707 ;; We assume HTTP here.
708 (require 'url-http)
709 (let ((response (url-http-parse-response)))
710 (when (or (< response 200) (>= response 300))
711 (error "Error during download request:%s"
712 (buffer-substring-no-properties (point) (progn
713 (end-of-line)
714 (point)))))))
716 (defun package-download-single (name version desc requires)
717 "Download and install a single-file package."
718 (let ((location (package-archive-base name))
719 (file (concat (symbol-name name) "-" version ".el")))
720 (package--with-work-buffer location file
721 (package-unpack-single (symbol-name name) version desc requires))))
723 (defun package-download-tar (name version)
724 "Download and install a tar package."
725 (let ((location (package-archive-base name))
726 (file (concat (symbol-name name) "-" version ".tar")))
727 (package--with-work-buffer location file
728 (package-unpack name version))))
730 (defun package-installed-p (package &optional min-version)
731 "Return true if PACKAGE, of MIN-VERSION or newer, is installed.
732 MIN-VERSION should be a version list."
733 (let ((pkg-desc (assq package package-alist)))
734 (if pkg-desc
735 (version-list-<= min-version
736 (package-desc-vers (cdr pkg-desc)))
737 ;; Also check built-in packages.
738 (package-built-in-p package min-version))))
740 (defun package-compute-transaction (package-list requirements)
741 "Return a list of packages to be installed, including PACKAGE-LIST.
742 PACKAGE-LIST should be a list of package names (symbols).
744 REQUIREMENTS should be a list of additional requirements; each
745 element in this list should have the form (PACKAGE VERSION-LIST),
746 where PACKAGE is a package name and VERSION-LIST is the required
747 version of that package.
749 This function recursively computes the requirements of the
750 packages in REQUIREMENTS, and returns a list of all the packages
751 that must be installed. Packages that are already installed are
752 not included in this list."
753 (dolist (elt requirements)
754 (let* ((next-pkg (car elt))
755 (next-version (cadr elt)))
756 (unless (package-installed-p next-pkg next-version)
757 ;; A package is required, but not installed. It might also be
758 ;; blocked via `package-load-list'.
759 (let ((pkg-desc (assq next-pkg package-archive-contents))
760 hold)
761 (when (setq hold (assq next-pkg package-load-list))
762 (setq hold (cadr hold))
763 (cond ((eq hold t))
764 ((eq hold nil)
765 (error "Required package '%s' is disabled"
766 (symbol-name next-pkg)))
767 ((null (stringp hold))
768 (error "Invalid element in `package-load-list'"))
769 ((version-list-< (version-to-list hold) next-version)
770 (error "Package `%s' held at version %s, \
771 but version %s required"
772 (symbol-name next-pkg) hold
773 (package-version-join next-version)))))
774 (unless pkg-desc
775 (error "Package `%s-%s' is unavailable"
776 (symbol-name next-pkg)
777 (package-version-join next-version)))
778 (unless (version-list-<= next-version
779 (package-desc-vers (cdr pkg-desc)))
780 (error
781 "Need package `%s-%s', but only %s is available"
782 (symbol-name next-pkg) (package-version-join next-version)
783 (package-version-join (package-desc-vers (cdr pkg-desc)))))
784 ;; Only add to the transaction if we don't already have it.
785 (unless (memq next-pkg package-list)
786 (push next-pkg package-list))
787 (setq package-list
788 (package-compute-transaction package-list
789 (package-desc-reqs
790 (cdr pkg-desc))))))))
791 package-list)
793 (defun package-read-from-string (str)
794 "Read a Lisp expression from STR.
795 Signal an error if the entire string was not used."
796 (let* ((read-data (read-from-string str))
797 (more-left
798 (condition-case nil
799 ;; The call to `ignore' suppresses a compiler warning.
800 (progn (ignore (read-from-string
801 (substring str (cdr read-data))))
803 (end-of-file nil))))
804 (if more-left
805 (error "Can't read whole string")
806 (car read-data))))
808 (defun package--read-archive-file (file)
809 "Re-read archive file FILE, if it exists.
810 Will return the data from the file, or nil if the file does not exist.
811 Will throw an error if the archive version is too new."
812 (let ((filename (expand-file-name file package-user-dir)))
813 (when (file-exists-p filename)
814 (with-temp-buffer
815 (insert-file-contents-literally filename)
816 (let ((contents (read (current-buffer))))
817 (if (> (car contents) package-archive-version)
818 (error "Package archive version %d is higher than %d"
819 (car contents) package-archive-version))
820 (cdr contents))))))
822 (defun package-read-all-archive-contents ()
823 "Re-read `archive-contents', if it exists.
824 If successful, set `package-archive-contents'."
825 (setq package-archive-contents nil)
826 (dolist (archive package-archives)
827 (package-read-archive-contents (car archive))))
829 (defun package-read-archive-contents (archive)
830 "Re-read archive contents for ARCHIVE.
831 If successful, set the variable `package-archive-contents'.
832 If the archive version is too new, signal an error."
833 ;; Version 1 of 'archive-contents' is identical to our internal
834 ;; representation.
835 (let* ((dir (concat "archives/" archive))
836 (contents-file (concat dir "/archive-contents"))
837 contents)
838 (when (setq contents (package--read-archive-file contents-file))
839 (dolist (package contents)
840 (package--add-to-archive-contents package archive)))))
842 (defun package--add-to-archive-contents (package archive)
843 "Add the PACKAGE from the given ARCHIVE if necessary.
844 Also, add the originating archive to the end of the package vector."
845 (let* ((name (car package))
846 (version (package-desc-vers (cdr package)))
847 (entry (cons name
848 (vconcat (cdr package) (vector archive))))
849 (existing-package (assq name package-archive-contents)))
850 (cond ((not existing-package)
851 (add-to-list 'package-archive-contents entry))
852 ((version-list-< (package-desc-vers (cdr existing-package))
853 version)
854 ;; Replace the entry with this one.
855 (setq package-archive-contents
856 (cons entry
857 (delq existing-package
858 package-archive-contents)))))))
860 (defun package-download-transaction (package-list)
861 "Download and install all the packages in PACKAGE-LIST.
862 PACKAGE-LIST should be a list of package names (symbols).
863 This function assumes that all package requirements in
864 PACKAGE-LIST are satisfied, i.e. that PACKAGE-LIST is computed
865 using `package-compute-transaction'."
866 (dolist (elt package-list)
867 (let* ((desc (cdr (assq elt package-archive-contents)))
868 ;; As an exception, if package is "held" in
869 ;; `package-load-list', download the held version.
870 (hold (cadr (assq elt package-load-list)))
871 (v-string (or (and (stringp hold) hold)
872 (package-version-join (package-desc-vers desc))))
873 (kind (package-desc-kind desc)))
874 (cond
875 ((eq kind 'tar)
876 (package-download-tar elt v-string))
877 ((eq kind 'single)
878 (package-download-single elt v-string
879 (package-desc-doc desc)
880 (package-desc-reqs desc)))
882 (error "Unknown package kind: %s" (symbol-name kind))))
883 ;; If package A depends on package B, then A may `require' B
884 ;; during byte compilation. So we need to activate B before
885 ;; unpacking A.
886 (package-maybe-load-descriptor (symbol-name elt) v-string
887 package-user-dir)
888 (package-activate elt (version-to-list v-string)))))
890 (defvar package--initialized nil)
892 ;;;###autoload
893 (defun package-install (name)
894 "Install the package named NAME.
895 NAME should be the name of one of the available packages in an
896 archive in `package-archives'. Interactively, prompt for NAME."
897 (interactive
898 (progn
899 ;; Initialize the package system to get the list of package
900 ;; symbols for completion.
901 (unless package--initialized
902 (package-initialize t))
903 (unless package-archive-contents
904 (package-refresh-contents))
905 (list (intern (completing-read
906 "Install package: "
907 (mapcar (lambda (elt)
908 (cons (symbol-name (car elt))
909 nil))
910 package-archive-contents)
911 nil t)))))
912 (let ((pkg-desc (assq name package-archive-contents)))
913 (unless pkg-desc
914 (error "Package `%s' is not available for installation"
915 (symbol-name name)))
916 (package-download-transaction
917 (package-compute-transaction (list name)
918 (package-desc-reqs (cdr pkg-desc))))))
920 (defun package-strip-rcs-id (str)
921 "Strip RCS version ID from the version string STR.
922 If the result looks like a dotted numeric version, return it.
923 Otherwise return nil."
924 (when str
925 (when (string-match "\\`[ \t]*[$]Revision:[ \t]+" str)
926 (setq str (substring str (match-end 0))))
927 (condition-case nil
928 (if (version-to-list str)
929 str)
930 (error nil))))
932 (defun package-buffer-info ()
933 "Return a vector describing the package in the current buffer.
934 The vector has the form
936 [FILENAME REQUIRES DESCRIPTION VERSION COMMENTARY]
938 FILENAME is the file name, a string, sans the \".el\" extension.
939 REQUIRES is a list of requirements, each requirement having the
940 form (NAME VER); NAME is a string and VER is a version list.
941 DESCRIPTION is the package description, a string.
942 VERSION is the version, a string.
943 COMMENTARY is the commentary section, a string, or nil if none.
945 If the buffer does not contain a conforming package, signal an
946 error. If there is a package, narrow the buffer to the file's
947 boundaries."
948 (goto-char (point-min))
949 (unless (re-search-forward "^;;; \\([^ ]*\\)\\.el ---[ \t]*\\(.*?\\)[ \t]*\\(-\\*-.*-\\*-[ \t]*\\)?$" nil t)
950 (error "Packages lacks a file header"))
951 (let ((file-name (match-string-no-properties 1))
952 (desc (match-string-no-properties 2))
953 (start (line-beginning-position)))
954 (unless (search-forward (concat ";;; " file-name ".el ends here"))
955 (error "Package lacks a terminating comment"))
956 ;; Try to include a trailing newline.
957 (forward-line)
958 (narrow-to-region start (point))
959 (require 'lisp-mnt)
960 ;; Use some headers we've invented to drive the process.
961 (let* ((requires-str (lm-header "package-requires"))
962 (requires (if requires-str
963 (package-read-from-string requires-str)))
964 ;; Prefer Package-Version; if defined, the package author
965 ;; probably wants us to use it. Otherwise try Version.
966 (pkg-version
967 (or (package-strip-rcs-id (lm-header "package-version"))
968 (package-strip-rcs-id (lm-header "version"))))
969 (commentary (lm-commentary)))
970 (unless pkg-version
971 (error
972 "Package lacks a \"Version\" or \"Package-Version\" header"))
973 ;; Turn string version numbers into list form.
974 (setq requires
975 (mapcar
976 (lambda (elt)
977 (list (car elt)
978 (version-to-list (car (cdr elt)))))
979 requires))
980 (vector file-name requires desc pkg-version commentary))))
982 (defun package-tar-file-info (file)
983 "Find package information for a tar file.
984 FILE is the name of the tar file to examine.
985 The return result is a vector like `package-buffer-info'."
986 (let ((default-directory (file-name-directory file))
987 (file (file-name-nondirectory file)))
988 (unless (string-match (concat "\\`" package-subdirectory-regexp "\\.tar\\'")
989 file)
990 (error "Invalid package name `%s'" file))
991 (let* ((pkg-name (match-string-no-properties 1 file))
992 (pkg-version (match-string-no-properties 2 file))
993 ;; Extract the package descriptor.
994 (pkg-def-contents (shell-command-to-string
995 ;; Requires GNU tar.
996 (concat "tar -xOf " file " "
998 pkg-name "-" pkg-version "/"
999 pkg-name "-pkg.el")))
1000 (pkg-def-parsed (package-read-from-string pkg-def-contents)))
1001 (unless (eq (car pkg-def-parsed) 'define-package)
1002 (error "No `define-package' sexp is present in `%s-pkg.el'" pkg-name))
1003 (let ((name-str (nth 1 pkg-def-parsed))
1004 (version-string (nth 2 pkg-def-parsed))
1005 (docstring (nth 3 pkg-def-parsed))
1006 (requires (nth 4 pkg-def-parsed))
1007 (readme (shell-command-to-string
1008 ;; Requires GNU tar.
1009 (concat "tar -xOf " file " "
1010 pkg-name "-" pkg-version "/README"))))
1011 (unless (equal pkg-version version-string)
1012 (error "Package has inconsistent versions"))
1013 (unless (equal pkg-name name-str)
1014 (error "Package has inconsistent names"))
1015 ;; Kind of a hack.
1016 (if (string-match ": Not found in archive" readme)
1017 (setq readme nil))
1018 ;; Turn string version numbers into list form.
1019 (if (eq (car requires) 'quote)
1020 (setq requires (car (cdr requires))))
1021 (setq requires
1022 (mapcar (lambda (elt)
1023 (list (car elt)
1024 (version-to-list (cadr elt))))
1025 requires))
1026 (vector pkg-name requires docstring version-string readme)))))
1028 ;;;###autoload
1029 (defun package-install-from-buffer (pkg-info type)
1030 "Install a package from the current buffer.
1031 When called interactively, the current buffer is assumed to be a
1032 single .el file that follows the packaging guidelines; see info
1033 node `(elisp)Packaging'.
1035 When called from Lisp, PKG-INFO is a vector describing the
1036 information, of the type returned by `package-buffer-info'; and
1037 TYPE is the package type (either `single' or `tar')."
1038 (interactive (list (package-buffer-info) 'single))
1039 (save-excursion
1040 (save-restriction
1041 (let* ((file-name (aref pkg-info 0))
1042 (requires (aref pkg-info 1))
1043 (desc (if (string= (aref pkg-info 2) "")
1044 "No description available."
1045 (aref pkg-info 2)))
1046 (pkg-version (aref pkg-info 3)))
1047 ;; Download and install the dependencies.
1048 (let ((transaction (package-compute-transaction nil requires)))
1049 (package-download-transaction transaction))
1050 ;; Install the package itself.
1051 (cond
1052 ((eq type 'single)
1053 (package-unpack-single file-name pkg-version desc requires))
1054 ((eq type 'tar)
1055 (package-unpack (intern file-name) pkg-version))
1057 (error "Unknown type: %s" (symbol-name type))))
1058 ;; Try to activate it.
1059 (package-initialize)))))
1061 ;;;###autoload
1062 (defun package-install-file (file)
1063 "Install a package from a file.
1064 The file can either be a tar file or an Emacs Lisp file."
1065 (interactive "fPackage file name: ")
1066 (with-temp-buffer
1067 (insert-file-contents-literally file)
1068 (cond
1069 ((string-match "\\.el$" file)
1070 (package-install-from-buffer (package-buffer-info) 'single))
1071 ((string-match "\\.tar$" file)
1072 (package-install-from-buffer (package-tar-file-info file) 'tar))
1073 (t (error "Unrecognized extension `%s'" (file-name-extension file))))))
1075 (defun package-delete (name version)
1076 (let ((dir (package--dir name version)))
1077 (if (string-equal (file-name-directory dir)
1078 (file-name-as-directory
1079 (expand-file-name package-user-dir)))
1080 (progn
1081 (delete-directory dir t t)
1082 (message "Package `%s-%s' deleted." name version))
1083 ;; Don't delete "system" packages
1084 (error "Package `%s-%s' is a system package, not deleting"
1085 name version))))
1087 (defun package-archive-base (name)
1088 "Return the archive containing the package NAME."
1089 (let ((desc (cdr (assq (intern-soft name) package-archive-contents))))
1090 (cdr (assoc (aref desc (- (length desc) 1)) package-archives))))
1092 (defun package--download-one-archive (archive file)
1093 "Retrieve an archive file FILE from ARCHIVE, and cache it.
1094 ARCHIVE should be a cons cell of the form (NAME . LOCATION),
1095 similar to an entry in `package-alist'. Save the cached copy to
1096 \"archives/NAME/archive-contents\" in `package-user-dir'."
1097 (let* ((dir (expand-file-name "archives" package-user-dir))
1098 (dir (expand-file-name (car archive) dir)))
1099 (package--with-work-buffer (cdr archive) file
1100 ;; Read the retrieved buffer to make sure it is valid (e.g. it
1101 ;; may fetch a URL redirect page).
1102 (when (listp (read buffer))
1103 (make-directory dir t)
1104 (setq buffer-file-name (expand-file-name file dir))
1105 (let ((version-control 'never))
1106 (save-buffer))))))
1108 ;;;###autoload
1109 (defun package-refresh-contents ()
1110 "Download the ELPA archive description if needed.
1111 This informs Emacs about the latest versions of all packages, and
1112 makes them available for download."
1113 (interactive)
1114 (unless (file-exists-p package-user-dir)
1115 (make-directory package-user-dir t))
1116 (dolist (archive package-archives)
1117 (condition-case-unless-debug nil
1118 (package--download-one-archive archive "archive-contents")
1119 (error (message "Failed to download `%s' archive."
1120 (car archive)))))
1121 (package-read-all-archive-contents))
1123 ;;;###autoload
1124 (defun package-initialize (&optional no-activate)
1125 "Load Emacs Lisp packages, and activate them.
1126 The variable `package-load-list' controls which packages to load.
1127 If optional arg NO-ACTIVATE is non-nil, don't activate packages."
1128 (interactive)
1129 (setq package-alist nil
1130 package-obsolete-alist nil)
1131 (package-load-all-descriptors)
1132 (package-read-all-archive-contents)
1133 (unless no-activate
1134 (dolist (elt package-alist)
1135 (package-activate (car elt) (package-desc-vers (cdr elt)))))
1136 (setq package--initialized t))
1139 ;;;; Package description buffer.
1141 ;;;###autoload
1142 (defun describe-package (package)
1143 "Display the full documentation of PACKAGE (a symbol)."
1144 (interactive
1145 (let* ((guess (function-called-at-point))
1146 packages val)
1147 (require 'finder-inf nil t)
1148 ;; Load the package list if necessary (but don't activate them).
1149 (unless package--initialized
1150 (package-initialize t))
1151 (setq packages (append (mapcar 'car package-alist)
1152 (mapcar 'car package-archive-contents)
1153 (mapcar 'car package--builtins)))
1154 (unless (memq guess packages)
1155 (setq guess nil))
1156 (setq packages (mapcar 'symbol-name packages))
1157 (setq val
1158 (completing-read (if guess
1159 (format "Describe package (default %s): "
1160 guess)
1161 "Describe package: ")
1162 packages nil t nil nil guess))
1163 (list (if (equal val "") guess (intern val)))))
1164 (if (or (null package) (not (symbolp package)))
1165 (message "No package specified")
1166 (help-setup-xref (list #'describe-package package)
1167 (called-interactively-p 'interactive))
1168 (with-help-window (help-buffer)
1169 (with-current-buffer standard-output
1170 (describe-package-1 package)))))
1172 (defun describe-package-1 (package)
1173 (require 'lisp-mnt)
1174 (let ((package-name (symbol-name package))
1175 (built-in (assq package package--builtins))
1176 desc pkg-dir reqs version installable)
1177 (prin1 package)
1178 (princ " is ")
1179 (cond
1180 ;; Loaded packages are in `package-alist'.
1181 ((setq desc (cdr (assq package package-alist)))
1182 (setq version (package-version-join (package-desc-vers desc)))
1183 (if (setq pkg-dir (package--dir package-name version))
1184 (insert "an installed package.\n\n")
1185 ;; This normally does not happen.
1186 (insert "a deleted package.\n\n")))
1187 ;; Available packages are in `package-archive-contents'.
1188 ((setq desc (cdr (assq package package-archive-contents)))
1189 (setq version (package-version-join (package-desc-vers desc))
1190 installable t)
1191 (if built-in
1192 (insert "a built-in package.\n\n")
1193 (insert "an uninstalled package.\n\n")))
1194 (built-in
1195 (setq desc (cdr built-in)
1196 version (package-version-join (package-desc-vers desc)))
1197 (insert "a built-in package.\n\n"))
1199 (insert "an orphan package.\n\n")))
1201 (insert " " (propertize "Status" 'font-lock-face 'bold) ": ")
1202 (cond (pkg-dir
1203 (insert (propertize "Installed"
1204 'font-lock-face 'font-lock-comment-face))
1205 (insert " in `")
1206 ;; Todo: Add button for uninstalling.
1207 (help-insert-xref-button (file-name-as-directory pkg-dir)
1208 'help-package-def pkg-dir)
1209 (if built-in
1210 (insert "',\n shadowing a "
1211 (propertize "built-in package"
1212 'font-lock-face 'font-lock-builtin-face)
1213 ".")
1214 (insert "'.")))
1215 (installable
1216 (if built-in
1217 (insert (propertize "Built-in." 'font-lock-face 'font-lock-builtin-face)
1218 " Alternate version available -- ")
1219 (insert "Available -- "))
1220 (let ((button-text (if (display-graphic-p) "Install" "[Install]"))
1221 (button-face (if (display-graphic-p)
1222 '(:box (:line-width 2 :color "dark grey")
1223 :background "light grey"
1224 :foreground "black")
1225 'link)))
1226 (insert-text-button button-text 'face button-face 'follow-link t
1227 'package-symbol package
1228 'action 'package-install-button-action)))
1229 (built-in
1230 (insert (propertize "Built-in." 'font-lock-face 'font-lock-builtin-face)))
1231 (t (insert "Deleted.")))
1232 (insert "\n")
1233 (and version (> (length version) 0)
1234 (insert " "
1235 (propertize "Version" 'font-lock-face 'bold) ": " version "\n"))
1237 (setq reqs (if desc (package-desc-reqs desc)))
1238 (when reqs
1239 (insert " " (propertize "Requires" 'font-lock-face 'bold) ": ")
1240 (let ((first t)
1241 name vers text)
1242 (dolist (req reqs)
1243 (setq name (car req)
1244 vers (cadr req)
1245 text (format "%s-%s" (symbol-name name)
1246 (package-version-join vers)))
1247 (cond (first (setq first nil))
1248 ((>= (+ 2 (current-column) (length text))
1249 (window-width))
1250 (insert ",\n "))
1251 (t (insert ", ")))
1252 (help-insert-xref-button text 'help-package name))
1253 (insert "\n")))
1254 (insert " " (propertize "Summary" 'font-lock-face 'bold)
1255 ": " (if desc (package-desc-doc desc)) "\n\n")
1257 (if built-in
1258 ;; For built-in packages, insert the commentary.
1259 (let ((fn (locate-file (concat package-name ".el") load-path
1260 load-file-rep-suffixes))
1261 (opoint (point)))
1262 (insert (or (lm-commentary fn) ""))
1263 (save-excursion
1264 (goto-char opoint)
1265 (when (re-search-forward "^;;; Commentary:\n" nil t)
1266 (replace-match ""))
1267 (while (re-search-forward "^\\(;+ ?\\)" nil t)
1268 (replace-match ""))))
1269 (let ((readme (expand-file-name (concat package-name "-readme.txt")
1270 package-user-dir))
1271 readme-string)
1272 ;; For elpa packages, try downloading the commentary. If that
1273 ;; fails, try an existing readme file in `package-user-dir'.
1274 (cond ((condition-case nil
1275 (package--with-work-buffer (package-archive-base package)
1276 (concat package-name "-readme.txt")
1277 (setq buffer-file-name
1278 (expand-file-name readme package-user-dir))
1279 (let ((version-control 'never))
1280 (save-buffer))
1281 (setq readme-string (buffer-string))
1283 (error nil))
1284 (insert readme-string))
1285 ((file-readable-p readme)
1286 (insert-file-contents readme)
1287 (goto-char (point-max))))))))
1289 (defun package-install-button-action (button)
1290 (let ((package (button-get button 'package-symbol)))
1291 (when (y-or-n-p (format "Install package `%s'? " package))
1292 (package-install package)
1293 (revert-buffer nil t)
1294 (goto-char (point-min)))))
1297 ;;;; Package menu mode.
1299 (defvar package-menu-mode-map
1300 (let ((map (make-sparse-keymap))
1301 (menu-map (make-sparse-keymap "Package")))
1302 (set-keymap-parent map tabulated-list-mode-map)
1303 (define-key map "\C-m" 'package-menu-describe-package)
1304 (define-key map "u" 'package-menu-mark-unmark)
1305 (define-key map "\177" 'package-menu-backup-unmark)
1306 (define-key map "d" 'package-menu-mark-delete)
1307 (define-key map "i" 'package-menu-mark-install)
1308 (define-key map "U" 'package-menu-mark-upgrades)
1309 (define-key map "r" 'package-menu-refresh)
1310 (define-key map "~" 'package-menu-mark-obsolete-for-deletion)
1311 (define-key map "x" 'package-menu-execute)
1312 (define-key map "h" 'package-menu-quick-help)
1313 (define-key map "?" 'package-menu-describe-package)
1314 (define-key map [menu-bar package-menu] (cons "Package" menu-map))
1315 (define-key menu-map [mq]
1316 '(menu-item "Quit" quit-window
1317 :help "Quit package selection"))
1318 (define-key menu-map [s1] '("--"))
1319 (define-key menu-map [mn]
1320 '(menu-item "Next" next-line
1321 :help "Next Line"))
1322 (define-key menu-map [mp]
1323 '(menu-item "Previous" previous-line
1324 :help "Previous Line"))
1325 (define-key menu-map [s2] '("--"))
1326 (define-key menu-map [mu]
1327 '(menu-item "Unmark" package-menu-mark-unmark
1328 :help "Clear any marks on a package and move to the next line"))
1329 (define-key menu-map [munm]
1330 '(menu-item "Unmark Backwards" package-menu-backup-unmark
1331 :help "Back up one line and clear any marks on that package"))
1332 (define-key menu-map [md]
1333 '(menu-item "Mark for Deletion" package-menu-mark-delete
1334 :help "Mark a package for deletion and move to the next line"))
1335 (define-key menu-map [mi]
1336 '(menu-item "Mark for Install" package-menu-mark-install
1337 :help "Mark a package for installation and move to the next line"))
1338 (define-key menu-map [mupgrades]
1339 '(menu-item "Mark Upgradable Packages" package-menu-mark-upgrades
1340 :help "Mark packages that have a newer version for upgrading"))
1341 (define-key menu-map [s3] '("--"))
1342 (define-key menu-map [mg]
1343 '(menu-item "Update Package List" revert-buffer
1344 :help "Update the list of packages"))
1345 (define-key menu-map [mr]
1346 '(menu-item "Refresh Package List" package-menu-refresh
1347 :help "Download the ELPA archive"))
1348 (define-key menu-map [s4] '("--"))
1349 (define-key menu-map [mt]
1350 '(menu-item "Mark Obsolete Packages" package-menu-mark-obsolete-for-deletion
1351 :help "Mark all obsolete packages for deletion"))
1352 (define-key menu-map [mx]
1353 '(menu-item "Execute Actions" package-menu-execute
1354 :help "Perform all the marked actions"))
1355 (define-key menu-map [s5] '("--"))
1356 (define-key menu-map [mh]
1357 '(menu-item "Help" package-menu-quick-help
1358 :help "Show short key binding help for package-menu-mode"))
1359 (define-key menu-map [mc]
1360 '(menu-item "View Commentary" package-menu-view-commentary
1361 :help "Display information about this package"))
1362 map)
1363 "Local keymap for `package-menu-mode' buffers.")
1365 (define-derived-mode package-menu-mode tabulated-list-mode "Package Menu"
1366 "Major mode for browsing a list of packages.
1367 Letters do not insert themselves; instead, they are commands.
1368 \\<package-menu-mode-map>
1369 \\{package-menu-mode-map}"
1370 (setq tabulated-list-format [("Package" 18 package-menu--name-predicate)
1371 ("Version" 12 nil)
1372 ("Status" 10 package-menu--status-predicate)
1373 ("Description" 0 nil)])
1374 (setq tabulated-list-padding 2)
1375 (setq tabulated-list-sort-key (cons "Status" nil))
1376 (tabulated-list-init-header))
1378 (defmacro package--push (package desc status listname)
1379 "Convenience macro for `package-menu--generate'.
1380 If the alist stored in the symbol LISTNAME lacks an entry for a
1381 package PACKAGE with descriptor DESC, add one. The alist is
1382 keyed with cons cells (PACKAGE . VERSION-LIST), where PACKAGE is
1383 a symbol and VERSION-LIST is a version list."
1384 `(let* ((version (package-desc-vers ,desc))
1385 (key (cons ,package version)))
1386 (unless (assoc key ,listname)
1387 (push (list key ,status (package-desc-doc ,desc)) ,listname))))
1389 (defun package-menu--generate (remember-pos packages)
1390 "Populate the Package Menu.
1391 If REMEMBER-POS is non-nil, keep point on the same entry.
1392 PACKAGES should be t, which means to display all known packages,
1393 or a list of package names (symbols) to display."
1394 ;; Construct list of ((PACKAGE . VERSION) STATUS DESCRIPTION).
1395 (let (info-list name)
1396 ;; Installed packages:
1397 (dolist (elt package-alist)
1398 (setq name (car elt))
1399 (when (or (eq packages t) (memq name packages))
1400 (package--push name (cdr elt)
1401 (if (stringp (cadr (assq name package-load-list)))
1402 "held" "installed")
1403 info-list)))
1405 ;; Built-in packages:
1406 (dolist (elt package--builtins)
1407 (setq name (car elt))
1408 (when (and (not (eq name 'emacs)) ; Hide the `emacs' package.
1409 (or (eq packages t) (memq name packages)))
1410 (package--push name (cdr elt) "built-in" info-list)))
1412 ;; Available and disabled packages:
1413 (dolist (elt package-archive-contents)
1414 (setq name (car elt))
1415 (when (or (eq packages t) (memq name packages))
1416 (let ((hold (assq name package-load-list)))
1417 (package--push name (cdr elt)
1418 (if (and hold (null (cadr hold)))
1419 "disabled"
1420 "available")
1421 info-list))))
1423 ;; Obsolete packages:
1424 (dolist (elt package-obsolete-alist)
1425 (dolist (inner-elt (cdr elt))
1426 (when (or (eq packages t) (memq (car elt) packages))
1427 (package--push (car elt) (cdr inner-elt) "obsolete" info-list))))
1429 ;; Print the result.
1430 (setq tabulated-list-entries (mapcar 'package-menu--print-info info-list))
1431 (tabulated-list-print remember-pos)))
1433 (defun package-menu--print-info (pkg)
1434 "Return a package entry suitable for `tabulated-list-entries'.
1435 PKG has the form ((PACKAGE . VERSION) STATUS DOC).
1436 Return (KEY [NAME VERSION STATUS DOC]), where KEY is the
1437 identifier (NAME . VERSION-LIST)."
1438 (let* ((package (caar pkg))
1439 (version (cdr (car pkg)))
1440 (status (nth 1 pkg))
1441 (doc (or (nth 2 pkg) ""))
1442 (face (cond
1443 ((string= status "built-in") 'font-lock-builtin-face)
1444 ((string= status "available") 'default)
1445 ((string= status "held") 'font-lock-constant-face)
1446 ((string= status "disabled") 'font-lock-warning-face)
1447 ((string= status "installed") 'font-lock-comment-face)
1448 (t 'font-lock-warning-face)))) ; obsolete.
1449 (list (cons package version)
1450 (vector (list (symbol-name package)
1451 'face 'link
1452 'follow-link t
1453 'package-symbol package
1454 'action 'package-menu-describe-package)
1455 (propertize (package-version-join version)
1456 'font-lock-face face)
1457 (propertize status 'font-lock-face face)
1458 (propertize doc 'font-lock-face face)))))
1460 (defun package-menu-refresh ()
1461 "Download the Emacs Lisp package archive.
1462 This fetches the contents of each archive specified in
1463 `package-archives', and then refreshes the package menu."
1464 (interactive)
1465 (unless (derived-mode-p 'package-menu-mode)
1466 (error "The current buffer is not a Package Menu"))
1467 (package-refresh-contents)
1468 (package-menu--generate t t))
1470 (defun package-menu-describe-package (&optional button)
1471 "Describe the current package.
1472 If optional arg BUTTON is non-nil, describe its associated package."
1473 (interactive)
1474 (let ((package (if button (button-get button 'package-symbol)
1475 (car (tabulated-list-get-id)))))
1476 (if package
1477 (describe-package package))))
1479 ;; fixme numeric argument
1480 (defun package-menu-mark-delete (&optional _num)
1481 "Mark a package for deletion and move to the next line."
1482 (interactive "p")
1483 (if (member (package-menu-get-status) '("installed" "obsolete"))
1484 (tabulated-list-put-tag "D" t)
1485 (forward-line)))
1487 (defun package-menu-mark-install (&optional _num)
1488 "Mark a package for installation and move to the next line."
1489 (interactive "p")
1490 (if (string-equal (package-menu-get-status) "available")
1491 (tabulated-list-put-tag "I" t)
1492 (forward-line)))
1494 (defun package-menu-mark-unmark (&optional _num)
1495 "Clear any marks on a package and move to the next line."
1496 (interactive "p")
1497 (tabulated-list-put-tag " " t))
1499 (defun package-menu-backup-unmark ()
1500 "Back up one line and clear any marks on that package."
1501 (interactive)
1502 (forward-line -1)
1503 (tabulated-list-put-tag " "))
1505 (defun package-menu-mark-obsolete-for-deletion ()
1506 "Mark all obsolete packages for deletion."
1507 (interactive)
1508 (save-excursion
1509 (goto-char (point-min))
1510 (while (not (eobp))
1511 (if (equal (package-menu-get-status) "obsolete")
1512 (tabulated-list-put-tag "D" t)
1513 (forward-line 1)))))
1515 (defun package-menu-quick-help ()
1516 "Show short key binding help for package-menu-mode."
1517 (interactive)
1518 (message "n-ext, i-nstall, d-elete, u-nmark, x-ecute, r-efresh, h-elp"))
1520 (define-obsolete-function-alias
1521 'package-menu-view-commentary 'package-menu-describe-package "24.1")
1523 (defun package-menu-get-status ()
1524 (let* ((pkg (tabulated-list-get-id))
1525 (entry (and pkg (assq pkg tabulated-list-entries))))
1526 (if entry
1527 (aref (cadr entry) 2)
1528 "")))
1530 (defun package-menu--find-upgrades ()
1531 (let (installed available upgrades)
1532 ;; Build list of installed/available packages in this buffer.
1533 (dolist (entry tabulated-list-entries)
1534 ;; ENTRY is ((NAME . VERSION) [NAME VERSION STATUS DOC])
1535 (let ((pkg (car entry))
1536 (status (aref (cadr entry) 2)))
1537 (cond ((equal status "installed")
1538 (push pkg installed))
1539 ((equal status "available")
1540 (push pkg available)))))
1541 ;; Loop through list of installed packages, finding upgrades
1542 (dolist (pkg installed)
1543 (let ((avail-pkg (assq (car pkg) available)))
1544 (and avail-pkg
1545 (version-list-< (cdr pkg) (cdr avail-pkg))
1546 (push avail-pkg upgrades))))
1547 upgrades))
1549 (defun package-menu-mark-upgrades ()
1550 "Mark all upgradable packages in the Package Menu.
1551 For each installed package with a newer version available, place
1552 an (I)nstall flag on the available version and a (D)elete flag on
1553 the installed version. A subsequent \\[package-menu-execute]
1554 call will upgrade the package."
1555 (interactive)
1556 (unless (derived-mode-p 'package-menu-mode)
1557 (error "The current buffer is not a Package Menu"))
1558 (let ((upgrades (package-menu--find-upgrades)))
1559 (if (null upgrades)
1560 (message "No packages to upgrade.")
1561 (widen)
1562 (save-excursion
1563 (goto-char (point-min))
1564 (while (not (eobp))
1565 (let* ((pkg (tabulated-list-get-id))
1566 (upgrade (assq (car pkg) upgrades)))
1567 (cond ((null upgrade)
1568 (forward-line 1))
1569 ((equal pkg upgrade)
1570 (package-menu-mark-install))
1572 (package-menu-mark-delete))))))
1573 (message "%d package%s marked for upgrading."
1574 (length upgrades)
1575 (if (= (length upgrades) 1) "" "s")))))
1577 (defun package-menu-execute ()
1578 "Perform marked Package Menu actions.
1579 Packages marked for installation are downloaded and installed;
1580 packages marked for deletion are removed."
1581 (interactive)
1582 (unless (derived-mode-p 'package-menu-mode)
1583 (error "The current buffer is not in Package Menu mode"))
1584 (let (install-list delete-list cmd id)
1585 (save-excursion
1586 (goto-char (point-min))
1587 (while (not (eobp))
1588 (setq cmd (char-after))
1589 (unless (eq cmd ?\s)
1590 ;; This is the key (PACKAGE . VERSION-LIST).
1591 (setq id (tabulated-list-get-id))
1592 (cond ((eq cmd ?D)
1593 (push (cons (symbol-name (car id))
1594 (package-version-join (cdr id)))
1595 delete-list))
1596 ((eq cmd ?I)
1597 (push (car id) install-list))))
1598 (forward-line)))
1599 (when install-list
1600 (if (yes-or-no-p
1601 (if (= (length install-list) 1)
1602 (format "Install package `%s'? " (car install-list))
1603 (format "Install these %d packages (%s)? "
1604 (length install-list)
1605 (mapconcat 'symbol-name install-list ", "))))
1606 (mapc 'package-install install-list)))
1607 ;; Delete packages, prompting if necessary.
1608 (when delete-list
1609 (if (yes-or-no-p
1610 (if (= (length delete-list) 1)
1611 (format "Delete package `%s-%s'? "
1612 (caar delete-list)
1613 (cdr (car delete-list)))
1614 (format "Delete these %d packages (%s)? "
1615 (length delete-list)
1616 (mapconcat (lambda (elt)
1617 (concat (car elt) "-" (cdr elt)))
1618 delete-list
1619 ", "))))
1620 (dolist (elt delete-list)
1621 (condition-case-unless-debug err
1622 (package-delete (car elt) (cdr elt))
1623 (error (message (cadr err)))))
1624 (error "Aborted")))
1625 ;; If we deleted anything, regenerate `package-alist'. This is done
1626 ;; automatically if we installed a package.
1627 (and delete-list (null install-list)
1628 (package-initialize))
1629 (if (or delete-list install-list)
1630 (package-menu--generate t t)
1631 (message "No operations specified."))))
1633 (defun package-menu--version-predicate (A B)
1634 (let ((vA (or (aref (cadr A) 1) '(0)))
1635 (vB (or (aref (cadr B) 1) '(0))))
1636 (if (version-list-= vA vB)
1637 (package-menu--name-predicate A B)
1638 (version-list-< vA vB))))
1640 (defun package-menu--status-predicate (A B)
1641 (let ((sA (aref (cadr A) 2))
1642 (sB (aref (cadr B) 2)))
1643 (cond ((string= sA sB)
1644 (package-menu--name-predicate A B))
1645 ((string= sA "available") t)
1646 ((string= sB "available") nil)
1647 ((string= sA "installed") t)
1648 ((string= sB "installed") nil)
1649 ((string= sA "held") t)
1650 ((string= sB "held") nil)
1651 ((string= sA "built-in") t)
1652 ((string= sB "built-in") nil)
1653 ((string= sA "obsolete") t)
1654 ((string= sB "obsolete") nil)
1655 (t (string< sA sB)))))
1657 (defun package-menu--description-predicate (A B)
1658 (let ((dA (aref (cadr A) 3))
1659 (dB (aref (cadr B) 3)))
1660 (if (string= dA dB)
1661 (package-menu--name-predicate A B)
1662 (string< dA dB))))
1664 (defun package-menu--name-predicate (A B)
1665 (string< (symbol-name (caar A))
1666 (symbol-name (caar B))))
1668 ;;;###autoload
1669 (defun list-packages (&optional no-fetch)
1670 "Display a list of packages.
1671 This first fetches the updated list of packages before
1672 displaying, unless a prefix argument NO-FETCH is specified.
1673 The list is displayed in a buffer named `*Packages*'."
1674 (interactive "P")
1675 (require 'finder-inf nil t)
1676 ;; Initialize the package system if necessary.
1677 (unless package--initialized
1678 (package-initialize t))
1679 (unless no-fetch
1680 (package-refresh-contents))
1681 (let ((buf (get-buffer-create "*Packages*")))
1682 (with-current-buffer buf
1683 (package-menu-mode)
1684 (package-menu--generate nil t))
1685 ;; The package menu buffer has keybindings. If the user types
1686 ;; `M-x list-packages', that suggests it should become current.
1687 (switch-to-buffer buf))
1688 (let ((upgrades (package-menu--find-upgrades)))
1689 (if upgrades
1690 (message "%d package%s can be upgraded; type `%s' to mark %s for upgrading."
1691 (length upgrades)
1692 (if (= (length upgrades) 1) "" "s")
1693 (substitute-command-keys "\\[package-menu-mark-upgrades]")
1694 (if (= (length upgrades) 1) "it" "them")))))
1696 ;;;###autoload
1697 (defalias 'package-list-packages 'list-packages)
1699 ;; Used in finder.el
1700 (defun package-show-package-list (packages)
1701 "Display PACKAGES in a *Packages* buffer.
1702 This is similar to `list-packages', but it does not fetch the
1703 updated list of packages, and it only displays packages with
1704 names in PACKAGES (which should be a list of symbols)."
1705 (require 'finder-inf nil t)
1706 (let ((buf (get-buffer-create "*Packages*")))
1707 (with-current-buffer buf
1708 (package-menu-mode)
1709 (package-menu--generate nil packages))
1710 (switch-to-buffer buf)))
1712 (defun package-list-packages-no-fetch ()
1713 "Display a list of packages.
1714 Does not fetch the updated list of packages before displaying.
1715 The list is displayed in a buffer named `*Packages*'."
1716 (interactive)
1717 (list-packages t))
1719 (provide 'package)
1721 ;;; package.el ends here