1 ;;; package.el --- Simple package system for Emacs
3 ;; Copyright (C) 2007-2013 Free Software Foundation, Inc.
5 ;; Author: Tom Tromey <tromey@redhat.com>
6 ;; Created: 10 Mar 2007
9 ;; Package-Requires: ((tabulated-list "1.0"))
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
28 ;; 2 Apr 2007 - now using ChangeLog file
29 ;; 15 Mar 2007 - updated documentation
30 ;; 14 Mar 2007 - Changed how obsolete packages are handled
31 ;; 13 Mar 2007 - Wrote package-install-from-buffer
32 ;; 12 Mar 2007 - Wrote package-menu mode
36 ;; The idea behind package.el is to be able to download packages and
37 ;; install them. Packages are versioned and have versioned
38 ;; dependencies. Furthermore, this supports built-in packages which
39 ;; may or may not be newer than user-specified packages. This makes
40 ;; it possible to upgrade Emacs and automatically disable packages
41 ;; which have moved from external to core. (Note though that we don't
42 ;; currently register any of these, so this feature does not actually
45 ;; A package is described by its name and version. The distribution
46 ;; format is either a tar file or a single .el file.
48 ;; A tar file should be named "NAME-VERSION.tar". The tar file must
49 ;; unpack into a directory named after the package and version:
50 ;; "NAME-VERSION". It must contain a file named "PACKAGE-pkg.el"
51 ;; which consists of a call to define-package. It may also contain a
52 ;; "dir" file and the info files it references.
54 ;; A .el file is named "NAME-VERSION.el" in the remote archive, but is
55 ;; installed as simply "NAME.el" in a directory named "NAME-VERSION".
57 ;; The downloader downloads all dependent packages. By default,
58 ;; packages come from the official GNU sources, but others may be
59 ;; added by customizing the `package-archives' alist. Packages get
60 ;; byte-compiled at install time.
62 ;; At activation time we will set up the load-path and the info path,
63 ;; and we will load the package's autoloads. If a package's
64 ;; dependencies are not available, we will not activate that package.
66 ;; Conceptually a package has multiple state transitions:
68 ;; * Download. Fetching the package from ELPA.
69 ;; * Install. Untar the package, or write the .el file, into
70 ;; ~/.emacs.d/elpa/ directory.
71 ;; * Byte compile. Currently this phase is done during install,
72 ;; but we may change this.
73 ;; * Activate. Evaluate the autoloads for the package to make it
74 ;; available to the user.
75 ;; * Load. Actually load the package and run some code from it.
77 ;; Other external functions you may want to use:
80 ;; Enters a mode similar to buffer-menu which lets you manage
81 ;; packages. You can choose packages for install (mark with "i",
82 ;; then "x" to execute) or deletion (not implemented yet), and you
83 ;; can see what packages are available. This will automatically
84 ;; fetch the latest list of packages from ELPA.
86 ;; M-x package-install-from-buffer
87 ;; Install a package consisting of a single .el file that appears
88 ;; in the current buffer. This only works for packages which
89 ;; define a Version header properly; package.el also supports the
90 ;; extension headers Package-Version (in case Version is an RCS id
91 ;; or similar), and Package-Requires (if the package requires other
94 ;; M-x package-install-file
95 ;; Install a package from the indicated file. The package can be
96 ;; either a tar file or a .el file. A tar file must contain an
97 ;; appropriately-named "-pkg.el" file; a .el file must be properly
98 ;; formatted as with package-install-from-buffer.
101 ;;; (sorted by sort-lines):
103 ;; Jim Blandy <jimb@red-bean.com>
104 ;; Karl Fogel <kfogel@red-bean.com>
105 ;; Kevin Ryde <user42@zip.com.au>
107 ;; Michael Olson <mwolson@member.fsf.org>
108 ;; Sebastian Tennant <sebyte@smolny.plus.com>
109 ;; Stefan Monnier <monnier@iro.umontreal.ca>
110 ;; Vinicius Jose Latorre <viniciusjl@ig.com.br>
111 ;; Phil Hagelberg <phil@hagelb.org>
115 ;; - a trust mechanism, since compiling a package can run arbitrary code.
116 ;; For example, download package signatures and check that they match.
117 ;; - putting info dirs at the start of the info path means
118 ;; users see a weird ordering of categories. OTOH we want to
119 ;; override later entries. maybe emacs needs to enforce
120 ;; the standard layout?
121 ;; - put bytecode in a separate directory tree
122 ;; - perhaps give users a way to recompile their bytecode
123 ;; or do it automatically when emacs changes
124 ;; - give users a way to know whether a package is installed ok
125 ;; - give users a way to view a package's documentation when it
126 ;; only appears in the .el
127 ;; - use/extend checkdoc so people can tell if their package will work
128 ;; - "installed" instead of a blank in the status column
129 ;; - tramp needs its files to be compiled in a certain order.
130 ;; how to handle this? fix tramp?
131 ;; - on emacs 21 we don't kill the -autoloads.el buffer. what about 22?
132 ;; - maybe we need separate .elc directories for various emacs versions
133 ;; and also emacs-vs-xemacs. That way conditional compilation can
134 ;; work. But would this break anything?
135 ;; - should store the package's keywords in archive-contents, then
136 ;; let the users filter the package-menu by keyword. See
137 ;; finder-by-keyword. (We could also let people view the
138 ;; Commentary, but it isn't clear how useful this is.)
139 ;; - William Xu suggests being able to open a package file without
141 ;; - Interface with desktop.el so that restarting after an install
143 ;; - Implement M-x package-upgrade, to upgrade any/all existing packages
144 ;; - Use hierarchical layout. PKG/etc PKG/lisp PKG/info
145 ;; ... except maybe lisp?
146 ;; - It may be nice to have a macro that expands to the package's
147 ;; private data dir, aka ".../etc". Or, maybe data-directory
148 ;; needs to be a list (though this would be less nice)
149 ;; a few packages want this, eg sokoban
150 ;; - package menu needs:
151 ;; ability to know which packages are built-in & thus not deletable
152 ;; it can sometimes print odd results, like 0.3 available but 0.4 active
154 ;; - Allow multiple versions on the server...?
156 ;; - Don't install a package which will invalidate dependencies overall
157 ;; - Allow something like (or (>= emacs 21.0) (>= xemacs 21.5))
158 ;; [ currently thinking, why bother.. KISS ]
159 ;; - Allow optional package dependencies
160 ;; then if we require 'bbdb', bbdb-specific lisp in lisp/bbdb
161 ;; and just don't compile to add to load path ...?
162 ;; - Have a list of archive URLs? [ maybe there's no point ]
163 ;; - David Kastrup pointed out on the xemacs list that for GPL it
164 ;; is friendlier to ship the source tree. We could "support" that
165 ;; by just having a "src" subdir in the package. This isn't ideal
166 ;; but it probably is not worth trying to support random source
167 ;; tree layouts, build schemes, etc.
168 ;; - Our treatment of the info path is somewhat bogus
169 ;; - perhaps have an "unstable" tree in ELPA as well as a stable one
173 (eval-when-compile (require 'cl-lib
))
175 (require 'tabulated-list
)
177 (defgroup package nil
178 "Manager for Emacs Lisp packages."
183 (defcustom package-enable-at-startup t
184 "Whether to activate installed packages when Emacs starts.
185 If non-nil, packages are activated after reading the init file
186 and before `after-init-hook'. Activation is not done if
187 `user-init-file' is nil (e.g. Emacs was started with \"-q\").
189 Even if the value is nil, you can type \\[package-initialize] to
190 activate the package system at any time."
195 (defcustom package-load-list
'(all)
196 "List of packages for `package-initialize' to load.
197 Each element in this list should be a list (NAME VERSION), or the
198 symbol `all'. The symbol `all' says to load the latest installed
199 versions of all packages not specified by other elements.
201 For an element (NAME VERSION), NAME is a package name (a symbol).
202 VERSION should be t, a string, or nil.
203 If VERSION is t, all versions are loaded, though obsolete ones
204 will be put in `package-obsolete-alist' and not activated.
205 If VERSION is a string, only that version is ever loaded.
206 Any other version, even if newer, is silently ignored.
207 Hence, the package is \"held\" at that version.
208 If VERSION is nil, the package is not loaded (it is \"disabled\")."
209 :type
'(repeat symbol
)
214 (defvar Info-directory-list
)
215 (declare-function info-initialize
"info" ())
216 (declare-function url-http-parse-response
"url-http" ())
217 (declare-function lm-header
"lisp-mnt" (header))
218 (declare-function lm-commentary
"lisp-mnt" (&optional file
))
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.
225 Each element has the form (ID . LOCATION).
226 ID is an archive name, as a string.
227 LOCATION specifies the base location for the archive.
228 If it starts with \"http:\", it is treated as a HTTP URL;
229 otherwise it should be an absolute directory name.
230 (Other types of URL are currently not supported.)
232 Only add locations that you trust, since fetching and installing
233 a package can run arbitrary code."
234 :type
'(alist :key-type
(string :tag
"Archive name")
235 :value-type
(string :tag
"URL or directory name"))
240 (defcustom package-pinned-packages nil
241 "An alist of packages that are pinned to a specific archive
243 Each element has the form (SYM . ID).
244 SYM is a package, as a symbol.
245 ID is an archive name, as a string. This should correspond to an
246 entry in `package-archives'.
248 If the archive of name ID does not contain the package SYM, no
249 other location will be considered, which will make the
250 package unavailable."
251 :type
'(alist :key-type
(symbol :tag
"Package")
252 :value-type
(string :tag
"Archive name"))
257 (defconst package-archive-version
1
258 "Version number of the package archive understood by this file.
259 Lower version numbers than this will probably be understood as well.")
261 (defconst package-el-version
"1.0.1"
262 "Version of package.el.")
264 ;; We don't prime the cache since it tends to get out of date.
265 (defvar package-archive-contents nil
266 "Cache of the contents of the Emacs Lisp Package Archive.
267 This is an alist mapping package names (symbols) to
268 `package--desc' structures.")
269 (put 'package-archive-contents
'risky-local-variable t
)
271 (defcustom package-user-dir
(locate-user-emacs-file "elpa")
272 "Directory containing the user's Emacs Lisp packages.
273 The directory name should be absolute.
274 Apart from this directory, Emacs also looks for system-wide
275 packages in `package-directory-list'."
281 (defcustom package-directory-list
282 ;; Defaults are subdirs named "elpa" in the site-lisp dirs.
284 (dolist (f load-path
)
286 (equal (file-name-nondirectory f
) "site-lisp")
287 (push (expand-file-name "elpa" f
) result
)))
289 "List of additional directories containing Emacs Lisp packages.
290 Each directory name should be absolute.
292 These directories contain packages intended for system-wide; in
293 contrast, `package-user-dir' contains packages for personal use."
294 :type
'(repeat directory
)
299 (defvar package--default-summary
"No description available.")
301 (cl-defstruct (package-desc
302 ;; Rename the default constructor from `make-package-desc'.
303 (:constructor package-desc-create
)
304 ;; Has the same interface as the old `define-package',
305 ;; which is still used in the "foo-pkg.el" files. Extra
306 ;; options can be supported by adding additional keys.
308 package-desc-from-define
309 (name-string version-string
&optional summary requirements
312 (name (intern name-string
))
313 (version (version-to-list version-string
))
314 (reqs (mapcar #'(lambda (elt)
316 (version-to-list (cadr elt
))))
317 (if (eq 'quote
(car requirements
))
320 "Structure containing information about an individual package.
324 `name' Name of the package, as a symbol.
326 `version' Version of the package, as a version list.
328 `summary' Short description of the package, typically taken from
329 the first line of the file.
331 `reqs' Requirements of the package. A list of (PACKAGE
332 VERSION-LIST) naming the dependent package and the minimum
335 `kind' The distribution format of the package. Currently, it is
336 either `single' or `tar'.
338 `archive' The name of the archive (as a string) whence this
341 `dir' The directory where the package is installed (if installed)."
344 (summary package--default-summary
)
351 (defsubst package-desc-full-name
(pkg-desc)
353 (package-desc-name pkg-desc
)
354 (package-version-join (package-desc-version pkg-desc
))))
356 ;; Package descriptor format used in finder-inf.el and package--builtins.
357 (cl-defstruct (package--bi-desc
358 (:constructor package-make-builtin
(version summary
))
364 ;; The value is precomputed in finder-inf.el, but don't load that
365 ;; until it's needed (i.e. when `package-initialize' is called).
366 (defvar package--builtins nil
367 "Alist of built-in packages.
368 The actual value is initialized by loading the library
369 `finder-inf'; this is not done until it is needed, e.g. by the
370 function `package-built-in-p'.
372 Each element has the form (PKG . PACKAGE-BI-DESC), where PKG is a package
373 name (a symbol) and DESC is a `package--bi-desc' structure.")
374 (put 'package--builtins
'risky-local-variable t
)
376 (defvar package-alist nil
377 "Alist of all packages available for activation.
378 Each element has the form (PKG . DESC), where PKG is a package
379 name (a symbol) and DESC is a `package-desc' structure.
381 This variable is set automatically by `package-load-descriptor',
382 called via `package-initialize'. To change which packages are
383 loaded and/or activated, customize `package-load-list'.")
384 (put 'package-alist
'risky-local-variable t
)
386 (defvar package-activated-list nil
387 "List of the names of currently activated packages.")
388 (put 'package-activated-list
'risky-local-variable t
)
390 (defvar package-obsolete-alist nil
391 "Representation of obsolete packages.
392 Like `package-alist', but maps package name to a second alist.
393 The inner alist is keyed by version.
395 Each element of the list is (NAME . VERSION-ALIST), where each
396 entry in VERSION-ALIST is (VERSION-LIST . PACKAGE-DESC).")
397 (put 'package-obsolete-alist
'risky-local-variable t
)
399 (defun package-version-join (vlist)
400 "Return the version string corresponding to the list VLIST.
401 This is, approximately, the inverse of `version-to-list'.
402 \(Actually, it returns only one of the possible inverses, since
403 `version-to-list' is a many-to-one operation.)"
406 (let ((str-list (list "." (int-to-string (car vlist
)))))
407 (dolist (num (cdr vlist
))
410 (push (int-to-string num
) str-list
)
413 (error "Invalid version list `%s'" vlist
))
415 ;; pre, or beta, or alpha
416 (cond ((equal "." (car str-list
))
418 ((not (string-match "[0-9]+" (car str-list
)))
419 (error "Invalid version list `%s'" vlist
)))
420 (push (cond ((= num -
1) "pre")
422 ((= num -
3) "alpha"))
424 (if (equal "." (car str-list
))
426 (apply 'concat
(nreverse str-list
)))))
428 (defun package-strip-version (dirname)
429 "Strip the version from a combined package name and version.
430 E.g., if given \"quux-23.0\", will return \"quux\""
431 (if (string-match (concat "\\`" package-subdirectory-regexp
"\\'") dirname
)
432 (match-string 1 dirname
)))
434 (defun package-load-descriptor (pkg-dir)
435 "Load the description file in directory PKG-DIR."
436 (let ((pkg-file (expand-file-name (package--description-file pkg-dir
)
438 (when (file-exists-p pkg-file
)
440 (insert-file-contents pkg-file
)
442 (goto-char (point-min))
443 (let ((pkg-desc (package-process-define-package
444 (read (current-buffer)) pkg-file
)))
445 (setf (package-desc-dir pkg-desc
) pkg-dir
))))))
447 (defun package-load-all-descriptors ()
448 "Load descriptors for installed Emacs Lisp packages.
449 This looks for package subdirectories in `package-user-dir' and
450 `package-directory-list'. The variable `package-load-list'
451 controls which package subdirectories may be loaded.
453 In each valid package subdirectory, this function loads the
454 description file containing a call to `define-package', which
455 updates `package-alist' and `package-obsolete-alist'."
456 (dolist (dir (cons package-user-dir package-directory-list
))
457 (when (file-directory-p dir
)
458 (dolist (subdir (directory-files dir
))
459 (let ((pkg-dir (expand-file-name subdir dir
)))
460 (when (file-directory-p pkg-dir
)
461 (package-load-descriptor pkg-dir
)))))))
463 (defun package-disabled-p (pkg-name version
)
464 "Return whether PKG-NAME at VERSION can be activated.
465 The decision is made according to `package-load-list'.
466 Return nil if the package can be activated.
467 Return t if the package is completely disabled.
468 Return the max version (as a string) if the package is held at a lower version."
469 (let ((force (assq pkg-name package-load-list
)))
470 (cond ((null force
) (not (memq 'all package-load-list
)))
471 ((null (setq force
(cadr force
))) t
) ; disabled
473 ((stringp force
) ; held
474 (unless (version-list-= version
(version-to-list force
))
476 (t (error "Invalid element in `package-load-list'")))))
478 (defun package-activate-1 (pkg-desc)
479 (let* ((name (package-desc-name pkg-desc
))
480 (pkg-dir (package-desc-dir pkg-desc
)))
482 (error "Internal error: unable to find directory for `%s'"
483 (package-desc-full-name pkg-desc
)))
485 (when (file-exists-p (expand-file-name "dir" pkg-dir
))
486 ;; FIXME: not the friendliest, but simple.
489 (push pkg-dir Info-directory-list
))
490 ;; Add to load path, add autoloads, and activate the package.
491 (push pkg-dir load-path
)
492 (load (expand-file-name (format "%s-autoloads" name
) pkg-dir
) nil t
)
493 (push name package-activated-list
)
497 (defun package-built-in-p (package &optional min-version
)
498 "Return true if PACKAGE is built-in to Emacs.
499 Optional arg MIN-VERSION, if non-nil, should be a version list
500 specifying the minimum acceptable version."
501 (require 'finder-inf nil t
) ; For `package--builtins'.
502 (if (eq package
'emacs
)
503 (version-list-<= min-version
(version-to-list emacs-version
))
504 (let ((elt (assq package package--builtins
)))
505 (and elt
(version-list-<= min-version
506 (package--bi-desc-version (cdr elt
)))))))
508 (defun package--from-builtin (bi-desc)
509 (package-desc-create :name
(pop bi-desc
)
510 :version
(package--bi-desc-version bi-desc
)
511 :summary
(package--bi-desc-summary bi-desc
)))
513 ;; This function goes ahead and activates a newer version of a package
514 ;; if an older one was already activated. This is not ideal; we'd at
515 ;; least need to check to see if the package has actually been loaded,
516 ;; and not merely activated.
517 (defun package-activate (package min-version
)
518 "Activate package PACKAGE, of version MIN-VERSION or newer.
519 MIN-VERSION should be a version list.
520 If PACKAGE has any dependencies, recursively activate them.
521 Return nil if the package could not be activated."
522 (let ((pkg-vec (cdr (assq package package-alist
)))
523 available-version found
)
524 ;; Check if PACKAGE is available in `package-alist'.
526 (setq available-version
(package-desc-version pkg-vec
)
527 found
(version-list-<= min-version available-version
)))
529 ;; If no such package is found, maybe it's built-in.
531 (package-built-in-p package min-version
))
532 ;; If the package is already activated, just return t.
533 ((memq package package-activated-list
)
535 ;; If it's disabled, then just skip it.
536 ((package-disabled-p package available-version
) nil
)
537 ;; Otherwise, proceed with activation.
539 (let ((fail (catch 'dep-failure
540 ;; Activate its dependencies recursively.
541 (dolist (req (package-desc-reqs pkg-vec
))
542 (unless (package-activate (car req
) (cadr req
))
543 (throw 'dep-failure req
))))))
545 (warn "Unable to activate package `%s'.
546 Required package `%s-%s' is unavailable"
547 package
(car fail
) (package-version-join (cadr fail
)))
548 ;; If all goes well, activate the package itself.
549 (package-activate-1 pkg-vec
)))))))
551 (defun package-mark-obsolete (package pkg-vec
)
552 "Put package on the obsolete list, if not already there."
553 (let ((elt (assq package package-obsolete-alist
)))
555 ;; If this obsolete version does not exist in the list, update
557 (unless (assoc (package-desc-version pkg-vec
) (cdr elt
))
558 (setcdr elt
(cons (cons (package-desc-version pkg-vec
) pkg-vec
)
560 ;; Make a new association.
561 (push (cons package
(list (cons (package-desc-version pkg-vec
)
563 package-obsolete-alist
))))
565 (defun define-package (name-string version-string
566 &optional docstring requirements
567 &rest _extra-properties
)
568 "Define a new package.
569 NAME-STRING is the name of the package, as a string.
570 VERSION-STRING is the version of the package, as a string.
571 DOCSTRING is a short description of the package, a string.
572 REQUIREMENTS is a list of dependencies on other packages.
573 Each requirement is of the form (OTHER-PACKAGE OTHER-VERSION),
574 where OTHER-VERSION is a string.
576 EXTRA-PROPERTIES is currently unused."
577 ;; FIXME: Placeholder! Should we keep it?
578 (error "Don't call me!"))
580 (defun package-process-define-package (exp origin
)
581 (unless (eq (car-safe exp
) 'define-package
)
582 (error "Can't find define-package in %s" origin
))
583 (let* ((new-pkg-desc (apply #'package-desc-from-define
(cdr exp
)))
584 (name (package-desc-name new-pkg-desc
))
585 (version (package-desc-version new-pkg-desc
))
586 (old-pkg (assq name package-alist
)))
588 ;; If there's no old package, just add this to `package-alist'.
590 (push (cons name new-pkg-desc
) package-alist
))
591 ((version-list-< (package-desc-version (cdr old-pkg
)) version
)
592 ;; Remove the old package and declare it obsolete.
593 (package-mark-obsolete name
(cdr old-pkg
))
594 (setq package-alist
(cons (cons name new-pkg-desc
)
595 (delq old-pkg package-alist
))))
596 ;; You can have two packages with the same version, e.g. one in
597 ;; the system package directory and one in your private
598 ;; directory. We just let the first one win.
599 ((not (version-list-= (package-desc-version (cdr old-pkg
)) version
))
600 ;; The package is born obsolete.
601 (package-mark-obsolete name new-pkg-desc
)))
605 (defun package-autoload-ensure-default-file (file)
606 "Make sure that the autoload file FILE exists and if not create it."
607 (unless (file-exists-p file
)
609 (concat ";;; " (file-name-nondirectory file
)
610 " --- automatically extracted autoloads\n"
613 "(add-to-list 'load-path (or (file-name-directory #$) (car load-path)))\n"
614 "\f\n;; Local Variables:\n"
615 ";; version-control: never\n"
616 ";; no-byte-compile: t\n"
617 ";; no-update-autoloads: t\n"
619 ";;; " (file-name-nondirectory file
)
624 (defun package-generate-autoloads (name pkg-dir
)
625 (require 'autoload
) ;Load before we let-bind generated-autoload-file!
626 (let* ((auto-name (format "%s-autoloads.el" name
))
627 ;;(ignore-name (concat name "-pkg.el"))
628 (generated-autoload-file (expand-file-name auto-name pkg-dir
))
629 (version-control 'never
))
630 (package-autoload-ensure-default-file generated-autoload-file
)
631 (update-directory-autoloads pkg-dir
)
632 (let ((buf (find-buffer-visiting generated-autoload-file
)))
633 (when buf
(kill-buffer buf
)))
636 (defvar tar-parse-info
)
637 (declare-function tar-untar-buffer
"tar-mode" ())
638 (declare-function tar-header-name
"tar-mode" (tar-header) t
)
639 (declare-function tar-header-link-type
"tar-mode" (tar-header) t
)
641 (defun package-untar-buffer (dir)
642 "Untar the current buffer.
643 This uses `tar-untar-buffer' from Tar mode. All files should
644 untar into a directory named DIR; otherwise, signal an error."
647 ;; Make sure everything extracts into DIR.
648 (let ((regexp (concat "\\`" (regexp-quote (expand-file-name dir
)) "/"))
649 (case-fold-search (memq system-type
'(windows-nt ms-dos cygwin
))))
650 (dolist (tar-data tar-parse-info
)
651 (let ((name (expand-file-name (tar-header-name tar-data
))))
652 (or (string-match regexp name
)
653 ;; Tarballs created by some utilities don't list
654 ;; directories with a trailing slash (Bug#13136).
655 (and (string-equal dir name
)
656 (eq (tar-header-link-type tar-data
) 5))
657 (error "Package does not untar cleanly into directory %s/" dir
)))))
660 (defun package-unpack (package version
)
661 (let* ((name (symbol-name package
))
662 (dirname (concat name
"-" version
))
663 (pkg-dir (expand-file-name dirname package-user-dir
)))
664 (make-directory package-user-dir t
)
665 ;; FIXME: should we delete PKG-DIR if it exists?
666 (let* ((default-directory (file-name-as-directory package-user-dir
)))
667 (package-untar-buffer dirname
)
668 (package--make-autoloads-and-compile package pkg-dir
))))
670 (defun package--make-autoloads-and-compile (name pkg-dir
)
671 "Generate autoloads and do byte-compilation for package named NAME.
672 PKG-DIR is the name of the package directory."
673 (let ((auto-name (package-generate-autoloads name pkg-dir
))
674 (load-path (cons pkg-dir load-path
)))
675 ;; We must load the autoloads file before byte compiling, in
676 ;; case there are magic cookies to set up non-trivial paths.
677 (load auto-name nil t
)
678 ;; FIXME: Compilation should be done as a separate, optional, step.
679 ;; E.g. for multi-package installs, we should first install all packages
680 ;; and then compile them.
681 (byte-recompile-directory pkg-dir
0 t
)))
683 (defun package--write-file-no-coding (file-name)
684 (let ((buffer-file-coding-system 'no-conversion
))
685 (write-region (point-min) (point-max) file-name
)))
687 (defun package-unpack-single (name version desc requires
)
688 "Install the contents of the current buffer as a package."
689 ;; Special case "package". FIXME: Should this still be supported?
690 (if (eq name
'package
)
691 (package--write-file-no-coding
692 (expand-file-name (format "%s.el" name
) package-user-dir
))
693 (let* ((pkg-dir (expand-file-name (format "%s-%s" name
694 (package-version-join
695 (version-to-list version
)))
697 (el-file (expand-file-name (format "%s.el" name
) pkg-dir
))
698 (pkg-file (expand-file-name (package--description-file pkg-dir
)
700 (make-directory pkg-dir t
)
701 (package--write-file-no-coding el-file
)
702 (let ((print-level nil
)
708 (list 'define-package
712 (when requires
;Don't bother quoting nil.
714 ;; Turn version lists into string form.
718 (package-version-join (cadr elt
))))
724 (package--make-autoloads-and-compile name pkg-dir
))))
726 (defmacro package--with-work-buffer
(location file
&rest body
)
727 "Run BODY in a buffer containing the contents of FILE at LOCATION.
728 LOCATION is the base location of a package archive, and should be
729 one of the URLs (or file names) specified in `package-archives'.
730 FILE is the name of a file relative to that base location.
732 This macro retrieves FILE from LOCATION into a temporary buffer,
733 and evaluates BODY while that buffer is current. This work
734 buffer is killed afterwards. Return the last value in BODY."
735 `(let* ((http (string-match "\\`https?:" ,location
))
738 (url-retrieve-synchronously (concat ,location
,file
))
739 (generate-new-buffer "*package work buffer*"))))
741 (with-current-buffer buffer
743 (progn (package-handle-response)
744 (re-search-forward "^$" nil
'move
)
746 (delete-region (point-min) (point)))
747 (unless (file-name-absolute-p ,location
)
748 (error "Archive location %s is not an absolute file name"
750 (insert-file-contents (expand-file-name ,file
,location
)))
752 (kill-buffer buffer
))))
754 (defun package-handle-response ()
755 "Handle the response from a `url-retrieve-synchronously' call.
756 Parse the HTTP response and throw if an error occurred.
757 The url package seems to require extra processing for this.
758 This should be called in a `save-excursion', in the download buffer.
759 It will move point to somewhere in the headers."
760 ;; We assume HTTP here.
762 (let ((response (url-http-parse-response)))
763 (when (or (< response
200) (>= response
300))
764 (error "Error during download request:%s"
765 (buffer-substring-no-properties (point) (progn
769 (defun package-download-single (name version desc requires
)
770 "Download and install a single-file package."
771 (let ((location (package-archive-base name
))
772 (file (concat (symbol-name name
) "-" version
".el")))
773 (package--with-work-buffer location file
774 (package-unpack-single name version desc requires
))))
776 (defun package-download-tar (name version
)
777 "Download and install a tar package."
778 (let ((location (package-archive-base name
))
779 (file (concat (symbol-name name
) "-" version
".tar")))
780 (package--with-work-buffer location file
781 (package-unpack name version
))))
783 (defvar package--initialized nil
)
785 (defun package-installed-p (package &optional min-version
)
786 "Return true if PACKAGE, of MIN-VERSION or newer, is installed.
787 MIN-VERSION should be a version list."
788 (unless package--initialized
(error "package.el is not yet initialized!"))
789 (let ((pkg-desc (assq package package-alist
)))
791 (version-list-<= min-version
792 (package-desc-version (cdr pkg-desc
)))
793 ;; Also check built-in packages.
794 (package-built-in-p package min-version
))))
796 (defun package-compute-transaction (package-list requirements
)
797 "Return a list of packages to be installed, including PACKAGE-LIST.
798 PACKAGE-LIST should be a list of package names (symbols).
800 REQUIREMENTS should be a list of additional requirements; each
801 element in this list should have the form (PACKAGE VERSION-LIST),
802 where PACKAGE is a package name and VERSION-LIST is the required
803 version of that package.
805 This function recursively computes the requirements of the
806 packages in REQUIREMENTS, and returns a list of all the packages
807 that must be installed. Packages that are already installed are
808 not included in this list."
809 (dolist (elt requirements
)
810 (let* ((next-pkg (car elt
))
811 (next-version (cadr elt
)))
812 (unless (package-installed-p next-pkg next-version
)
813 ;; A package is required, but not installed. It might also be
814 ;; blocked via `package-load-list'.
815 (let ((pkg-desc (cdr (assq next-pkg package-archive-contents
)))
816 (disabled (package-disabled-p next-pkg next-version
)))
818 (if (stringp disabled
)
819 (error "Package `%s' held at version %s, \
820 but version %s required"
821 (symbol-name next-pkg
) disabled
822 (package-version-join next-version
))
823 (error "Required package '%s' is disabled"
824 (symbol-name next-pkg
))))
826 (error "Package `%s-%s' is unavailable"
827 (symbol-name next-pkg
)
828 (package-version-join next-version
)))
829 (unless (version-list-<= next-version
830 (package-desc-version pkg-desc
))
832 "Need package `%s-%s', but only %s is available"
833 (symbol-name next-pkg
) (package-version-join next-version
)
834 (package-version-join (package-desc-version pkg-desc
))))
835 ;; Move to front, so it gets installed early enough (bug#14082).
836 (setq package-list
(cons next-pkg
(delq next-pkg package-list
)))
838 (package-compute-transaction package-list
843 (defun package-read-from-string (str)
844 "Read a Lisp expression from STR.
845 Signal an error if the entire string was not used."
846 (let* ((read-data (read-from-string str
))
849 ;; The call to `ignore' suppresses a compiler warning.
850 (progn (ignore (read-from-string
851 (substring str
(cdr read-data
))))
855 (error "Can't read whole string")
858 (defun package--read-archive-file (file)
859 "Re-read archive file FILE, if it exists.
860 Will return the data from the file, or nil if the file does not exist.
861 Will throw an error if the archive version is too new."
862 (let ((filename (expand-file-name file package-user-dir
)))
863 (when (file-exists-p filename
)
865 (insert-file-contents-literally filename
)
866 (let ((contents (read (current-buffer))))
867 (if (> (car contents
) package-archive-version
)
868 (error "Package archive version %d is higher than %d"
869 (car contents
) package-archive-version
))
872 (defun package-read-all-archive-contents ()
873 "Re-read `archive-contents', if it exists.
874 If successful, set `package-archive-contents'."
875 (setq package-archive-contents nil
)
876 (dolist (archive package-archives
)
877 (package-read-archive-contents (car archive
))))
879 (defun package-read-archive-contents (archive)
880 "Re-read archive contents for ARCHIVE.
881 If successful, set the variable `package-archive-contents'.
882 If the archive version is too new, signal an error."
883 ;; Version 1 of 'archive-contents' is identical to our internal
885 (let* ((dir (concat "archives/" archive
))
886 (contents-file (concat dir
"/archive-contents"))
888 (when (setq contents
(package--read-archive-file contents-file
))
889 (dolist (package contents
)
890 (package--add-to-archive-contents package archive
)))))
892 ;; Package descriptor objects used inside the "archive-contents" file.
893 ;; Changing this defstruct implies changing the format of the
894 ;; "archive-contents" files.
895 (cl-defstruct (package--ac-desc
896 (:constructor package-make-ac-desc
(version reqs summary kind
))
899 version reqs summary kind
)
901 (defun package--add-to-archive-contents (package archive
)
902 "Add the PACKAGE from the given ARCHIVE if necessary.
903 PACKAGE should have the form (NAME . PACKAGE--AC-DESC).
904 Also, add the originating archive to the `package-desc' structure."
905 (let* ((name (car package
))
909 :version
(package--ac-desc-version (cdr package
))
910 :reqs
(package--ac-desc-reqs (cdr package
))
911 :summary
(package--ac-desc-summary (cdr package
))
912 :kind
(package--ac-desc-kind (cdr package
))
914 (entry (cons name pkg-desc
))
915 (existing-package (assq name package-archive-contents
))
916 (pinned-to-archive (assoc name package-pinned-packages
)))
917 (cond ((and pinned-to-archive
918 ;; If pinned to another archive, skip entirely.
919 (not (equal (cdr pinned-to-archive
) archive
)))
921 ((not existing-package
)
922 (push entry package-archive-contents
))
923 ((version-list-< (package-desc-version (cdr existing-package
))
924 (package-desc-version pkg-desc
))
925 ;; Replace the entry with this one.
926 (setq package-archive-contents
928 (delq existing-package
929 package-archive-contents
)))))))
931 (defun package-download-transaction (package-list)
932 "Download and install all the packages in PACKAGE-LIST.
933 PACKAGE-LIST should be a list of package names (symbols).
934 This function assumes that all package requirements in
935 PACKAGE-LIST are satisfied, i.e. that PACKAGE-LIST is computed
936 using `package-compute-transaction'."
937 ;; FIXME: make package-list a list of pkg-desc.
938 (dolist (elt package-list
)
939 (let* ((desc (cdr (assq elt package-archive-contents
)))
940 ;; As an exception, if package is "held" in
941 ;; `package-load-list', download the held version.
942 (hold (cadr (assq elt package-load-list
)))
943 (v-string (or (and (stringp hold
) hold
)
944 (package-version-join (package-desc-version desc
))))
945 (kind (package-desc-kind desc
)))
948 (package-download-tar elt v-string
))
950 (package-download-single elt v-string
951 (package-desc-summary desc
)
952 (package-desc-reqs desc
)))
954 (error "Unknown package kind: %s" (symbol-name kind
))))
955 ;; If package A depends on package B, then A may `require' B
956 ;; during byte compilation. So we need to activate B before
958 (package-activate elt
(version-to-list v-string
)))))
961 (defun package-install (pkg-desc)
962 "Install the package PKG-DESC.
963 PKG-DESC should be one of the available packages in an
964 archive in `package-archives'. Interactively, prompt for its name."
967 ;; Initialize the package system to get the list of package
968 ;; symbols for completion.
969 (unless package--initialized
970 (package-initialize t
))
971 (unless package-archive-contents
972 (package-refresh-contents))
973 (let* ((name (intern (completing-read
975 (mapcar (lambda (elt)
976 (cons (symbol-name (car elt
))
978 package-archive-contents
)
980 (pkg-desc (cdr (assq name package-archive-contents
))))
982 (error "Package `%s' is not available for installation"
985 (package-download-transaction
986 ;; FIXME: Use (list pkg-desc) instead of just the name.
987 (package-compute-transaction (list (package-desc-name pkg-desc
))
988 (package-desc-reqs pkg-desc
))))
990 (defun package-strip-rcs-id (str)
991 "Strip RCS version ID from the version string STR.
992 If the result looks like a dotted numeric version, return it.
993 Otherwise return nil."
995 (when (string-match "\\`[ \t]*[$]Revision:[ \t]+" str
)
996 (setq str
(substring str
(match-end 0))))
998 (if (version-to-list str
)
1002 (defun package-buffer-info ()
1003 "Return a `package-desc' describing the package in the current buffer.
1005 If the buffer does not contain a conforming package, signal an
1006 error. If there is a package, narrow the buffer to the file's
1008 (goto-char (point-min))
1009 (unless (re-search-forward "^;;; \\([^ ]*\\)\\.el ---[ \t]*\\(.*?\\)[ \t]*\\(-\\*-.*-\\*-[ \t]*\\)?$" nil t
)
1010 (error "Packages lacks a file header"))
1011 (let ((file-name (match-string-no-properties 1))
1012 (desc (match-string-no-properties 2))
1013 (start (line-beginning-position)))
1014 (unless (search-forward (concat ";;; " file-name
".el ends here"))
1015 (error "Package lacks a terminating comment"))
1016 ;; Try to include a trailing newline.
1018 (narrow-to-region start
(point))
1020 ;; Use some headers we've invented to drive the process.
1021 (let* ((requires-str (lm-header "package-requires"))
1022 ;; Prefer Package-Version; if defined, the package author
1023 ;; probably wants us to use it. Otherwise try Version.
1025 (or (package-strip-rcs-id (lm-header "package-version"))
1026 (package-strip-rcs-id (lm-header "version")))))
1029 "Package lacks a \"Version\" or \"Package-Version\" header"))
1030 (package-desc-from-define
1031 file-name pkg-version desc
1032 (if requires-str
(package-read-from-string requires-str
))
1035 (defun package-tar-file-info (file)
1036 "Find package information for a tar file.
1037 FILE is the name of the tar file to examine.
1038 The return result is a vector like `package-buffer-info'."
1039 (let* ((default-directory (file-name-directory file
))
1040 (file (file-name-nondirectory file
))
1042 (if (string-match "\\.tar\\'" file
)
1043 (substring file
0 (match-beginning 0))
1044 (error "Invalid package name `%s'" file
)))
1045 (desc-file (package--description-file dir-name
))
1046 ;; Extract the package descriptor.
1047 (pkg-def-contents (shell-command-to-string
1048 ;; Requires GNU tar.
1049 (concat "tar -xOf " file
" "
1050 dir-name
"/" desc-file
)))
1051 (pkg-def-parsed (package-read-from-string pkg-def-contents
)))
1052 (unless (eq (car pkg-def-parsed
) 'define-package
)
1053 (error "Can't find define-package in %s" desc-file
))
1055 (apply #'package-desc-from-define
(append (cdr pkg-def-parsed
)
1057 (unless (equal dir-name
(package-desc-full-name pkg-desc
))
1058 ;; FIXME: Shouldn't this just be a message/warning?
1059 (error "Package has inconsistent name"))
1064 (defun package-install-from-buffer (pkg-desc)
1065 "Install a package from the current buffer.
1066 When called interactively, the current buffer is assumed to be a
1067 single .el file that follows the packaging guidelines; see info
1068 node `(elisp)Packaging'.
1070 When called from Lisp, PKG-DESC is a `package-desc' describing the
1072 (interactive (list (package-buffer-info)))
1075 (let* ((name (package-desc-name pkg-desc
))
1076 (requires (package-desc-reqs pkg-desc
))
1077 (desc (package-desc-summary pkg-desc
))
1078 (pkg-version (package-desc-version pkg-desc
)))
1079 ;; Download and install the dependencies.
1080 (let ((transaction (package-compute-transaction nil requires
)))
1081 (package-download-transaction transaction
))
1082 ;; Install the package itself.
1083 (pcase (package-desc-kind pkg-desc
)
1084 (`single
(package-unpack-single name pkg-version desc requires
))
1085 (`tar
(package-unpack name pkg-version
))
1086 (type (error "Unknown type: %S" type
)))
1087 ;; Try to activate it.
1088 (package-initialize)))))
1091 (defun package-install-file (file)
1092 "Install a package from a file.
1093 The file can either be a tar file or an Emacs Lisp file."
1094 (interactive "fPackage file name: ")
1096 (insert-file-contents-literally file
)
1098 ((string-match "\\.el\\'" file
)
1099 (package-install-from-buffer (package-buffer-info)))
1100 ((string-match "\\.tar\\'" file
)
1101 (package-install-from-buffer (package-tar-file-info file
)))
1102 (t (error "Unrecognized extension `%s'" (file-name-extension file
))))))
1104 (defun package-delete (pkg-desc)
1105 (let ((dir (package-desc-dir pkg-desc
)))
1106 (if (string-equal (file-name-directory dir
)
1107 (file-name-as-directory
1108 (expand-file-name package-user-dir
)))
1110 (delete-directory dir t t
)
1111 (message "Package `%s' deleted." (package-desc-full-name pkg-desc
)))
1112 ;; Don't delete "system" packages
1113 (error "Package `%s' is a system package, not deleting"
1114 (package-desc-full-name pkg-desc
)))))
1116 (defun package-archive-base (name)
1117 "Return the archive containing the package NAME."
1118 (let ((desc (cdr (assq (intern-soft name
) package-archive-contents
))))
1119 (cdr (assoc (package-desc-archive desc
) package-archives
))))
1121 (defun package--download-one-archive (archive file
)
1122 "Retrieve an archive file FILE from ARCHIVE, and cache it.
1123 ARCHIVE should be a cons cell of the form (NAME . LOCATION),
1124 similar to an entry in `package-alist'. Save the cached copy to
1125 \"archives/NAME/archive-contents\" in `package-user-dir'."
1126 (let* ((dir (expand-file-name "archives" package-user-dir
))
1127 (dir (expand-file-name (car archive
) dir
)))
1128 (package--with-work-buffer (cdr archive
) file
1129 ;; Read the retrieved buffer to make sure it is valid (e.g. it
1130 ;; may fetch a URL redirect page).
1131 (when (listp (read buffer
))
1132 (make-directory dir t
)
1133 (setq buffer-file-name
(expand-file-name file dir
))
1134 (let ((version-control 'never
))
1138 (defun package-refresh-contents ()
1139 "Download the ELPA archive description if needed.
1140 This informs Emacs about the latest versions of all packages, and
1141 makes them available for download."
1143 (unless (file-exists-p package-user-dir
)
1144 (make-directory package-user-dir t
))
1145 (dolist (archive package-archives
)
1146 (condition-case-unless-debug nil
1147 (package--download-one-archive archive
"archive-contents")
1148 (error (message "Failed to download `%s' archive."
1150 (package-read-all-archive-contents))
1153 (defun package-initialize (&optional no-activate
)
1154 "Load Emacs Lisp packages, and activate them.
1155 The variable `package-load-list' controls which packages to load.
1156 If optional arg NO-ACTIVATE is non-nil, don't activate packages."
1158 (setq package-alist nil
1159 package-obsolete-alist nil
)
1160 (package-load-all-descriptors)
1161 (package-read-all-archive-contents)
1163 (dolist (elt package-alist
)
1164 (package-activate (car elt
) (package-desc-version (cdr elt
)))))
1165 (setq package--initialized t
))
1168 ;;;; Package description buffer.
1171 (defun describe-package (package)
1172 "Display the full documentation of PACKAGE (a symbol)."
1174 (let* ((guess (function-called-at-point))
1176 (require 'finder-inf nil t
)
1177 ;; Load the package list if necessary (but don't activate them).
1178 (unless package--initialized
1179 (package-initialize t
))
1180 (setq packages
(append (mapcar 'car package-alist
)
1181 (mapcar 'car package-archive-contents
)
1182 (mapcar 'car package--builtins
)))
1183 (unless (memq guess packages
)
1185 (setq packages
(mapcar 'symbol-name packages
))
1187 (completing-read (if guess
1188 (format "Describe package (default %s): "
1190 "Describe package: ")
1191 packages nil t nil nil guess
))
1192 (list (if (equal val
"") guess
(intern val
)))))
1193 (if (not (and package
(symbolp package
)))
1194 (message "No package specified")
1195 (help-setup-xref (list #'describe-package package
)
1196 (called-interactively-p 'interactive
))
1197 (with-help-window (help-buffer)
1198 (with-current-buffer standard-output
1199 (describe-package-1 package
)))))
1201 (defun describe-package-1 (package)
1203 (let ((package-name (symbol-name package
))
1204 (built-in (assq package package--builtins
))
1205 desc pkg-dir reqs version installable archive
)
1209 ;; Loaded packages are in `package-alist'.
1210 ((setq desc
(cdr (assq package package-alist
)))
1211 (setq version
(package-version-join (package-desc-version desc
)))
1212 (if (setq pkg-dir
(package-desc-dir desc
))
1213 (insert "an installed package.\n\n")
1214 ;; This normally does not happen.
1215 (insert "a deleted package.\n\n")))
1216 ;; Available packages are in `package-archive-contents'.
1217 ((setq desc
(cdr (assq package package-archive-contents
)))
1218 (setq version
(package-version-join (package-desc-version desc
))
1219 archive
(package-desc-archive desc
)
1222 (insert "a built-in package.\n\n")
1223 (insert "an uninstalled package.\n\n")))
1225 (setq desc
(package--from-builtin built-in
)
1226 version
(package-version-join (package-desc-version desc
)))
1227 (insert "a built-in package.\n\n"))
1229 (insert "an orphan package.\n\n")))
1231 (insert " " (propertize "Status" 'font-lock-face
'bold
) ": ")
1233 (insert (propertize "Installed"
1234 'font-lock-face
'font-lock-comment-face
))
1236 ;; Todo: Add button for uninstalling.
1237 (help-insert-xref-button (file-name-as-directory pkg-dir
)
1238 'help-package-def pkg-dir
)
1240 (insert "',\n shadowing a "
1241 (propertize "built-in package"
1242 'font-lock-face
'font-lock-builtin-face
)
1247 (insert (propertize "Built-in."
1248 'font-lock-face
'font-lock-builtin-face
)
1249 " Alternate version available")
1250 (insert "Available"))
1251 (insert " from " archive
)
1253 (let ((button-text (if (display-graphic-p) "Install" "[Install]"))
1254 (button-face (if (display-graphic-p)
1255 '(:box
(:line-width
2 :color
"dark grey")
1256 :background
"light grey"
1257 :foreground
"black")
1259 (insert-text-button button-text
'face button-face
'follow-link t
1261 'action
'package-install-button-action
)))
1263 (insert (propertize "Built-in."
1264 'font-lock-face
'font-lock-builtin-face
)))
1265 (t (insert "Deleted.")))
1267 (and version
(> (length version
) 0)
1269 (propertize "Version" 'font-lock-face
'bold
) ": " version
"\n"))
1271 (setq reqs
(if desc
(package-desc-reqs desc
)))
1273 (insert " " (propertize "Requires" 'font-lock-face
'bold
) ": ")
1277 (setq name
(car req
)
1279 text
(format "%s-%s" (symbol-name name
)
1280 (package-version-join vers
)))
1281 (cond (first (setq first nil
))
1282 ((>= (+ 2 (current-column) (length text
))
1286 (help-insert-xref-button text
'help-package name
))
1288 (insert " " (propertize "Summary" 'font-lock-face
'bold
)
1289 ": " (if desc
(package-desc-summary desc
)) "\n\n")
1292 ;; For built-in packages, insert the commentary.
1293 (let ((fn (locate-file (concat package-name
".el") load-path
1294 load-file-rep-suffixes
))
1296 (insert (or (lm-commentary fn
) ""))
1299 (when (re-search-forward "^;;; Commentary:\n" nil t
)
1301 (while (re-search-forward "^\\(;+ ?\\)" nil t
)
1302 (replace-match ""))))
1303 (let ((readme (expand-file-name (concat package-name
"-readme.txt")
1306 ;; For elpa packages, try downloading the commentary. If that
1307 ;; fails, try an existing readme file in `package-user-dir'.
1308 (cond ((condition-case nil
1309 (package--with-work-buffer (package-archive-base package
)
1310 (concat package-name
"-readme.txt")
1311 (setq buffer-file-name
1312 (expand-file-name readme package-user-dir
))
1313 (let ((version-control 'never
))
1315 (setq readme-string
(buffer-string))
1318 (insert readme-string
))
1319 ((file-readable-p readme
)
1320 (insert-file-contents readme
)
1321 (goto-char (point-max))))))))
1323 (defun package-install-button-action (button)
1324 (let ((pkg-desc (button-get button
'package-desc
)))
1325 (when (y-or-n-p (format "Install package `%s'? "
1326 (package-desc-full-name pkg-desc
)))
1327 (package-install pkg-desc
)
1328 (revert-buffer nil t
)
1329 (goto-char (point-min)))))
1332 ;;;; Package menu mode.
1334 (defvar package-menu-mode-map
1335 (let ((map (make-sparse-keymap))
1336 (menu-map (make-sparse-keymap "Package")))
1337 (set-keymap-parent map tabulated-list-mode-map
)
1338 (define-key map
"\C-m" 'package-menu-describe-package
)
1339 (define-key map
"u" 'package-menu-mark-unmark
)
1340 (define-key map
"\177" 'package-menu-backup-unmark
)
1341 (define-key map
"d" 'package-menu-mark-delete
)
1342 (define-key map
"i" 'package-menu-mark-install
)
1343 (define-key map
"U" 'package-menu-mark-upgrades
)
1344 (define-key map
"r" 'package-menu-refresh
)
1345 (define-key map
"~" 'package-menu-mark-obsolete-for-deletion
)
1346 (define-key map
"x" 'package-menu-execute
)
1347 (define-key map
"h" 'package-menu-quick-help
)
1348 (define-key map
"?" 'package-menu-describe-package
)
1349 (define-key map
[menu-bar package-menu
] (cons "Package" menu-map
))
1350 (define-key menu-map
[mq]
1351 '(menu-item "Quit" quit-window
1352 :help "Quit package selection"))
1353 (define-key menu-map [s1] '("--"))
1354 (define-key menu-map [mn]
1355 '(menu-item "Next" next-line
1357 (define-key menu-map [mp]
1358 '(menu-item "Previous" previous-line
1359 :help "Previous Line"))
1360 (define-key menu-map [s2] '("--"))
1361 (define-key menu-map [mu]
1362 '(menu-item "Unmark" package-menu-mark-unmark
1363 :help "Clear any marks on a package and move to the next line"))
1364 (define-key menu-map [munm]
1365 '(menu-item "Unmark Backwards" package-menu-backup-unmark
1366 :help "Back up one line and clear any marks on that package"))
1367 (define-key menu-map [md]
1368 '(menu-item "Mark for Deletion" package-menu-mark-delete
1369 :help "Mark a package for deletion and move to the next line"))
1370 (define-key menu-map [mi]
1371 '(menu-item "Mark for Install" package-menu-mark-install
1372 :help "Mark a package for installation and move to the next line"))
1373 (define-key menu-map [mupgrades]
1374 '(menu-item "Mark Upgradable Packages" package-menu-mark-upgrades
1375 :help "Mark packages that have a newer version for upgrading"))
1376 (define-key menu-map [s3] '("--"))
1377 (define-key menu-map [mg]
1378 '(menu-item "Update Package List" revert-buffer
1379 :help "Update the list of packages"))
1380 (define-key menu-map [mr]
1381 '(menu-item "Refresh Package List" package-menu-refresh
1382 :help "Download the ELPA archive"))
1383 (define-key menu-map [s4] '("--"))
1384 (define-key menu-map [mt]
1385 '(menu-item "Mark Obsolete Packages" package-menu-mark-obsolete-for-deletion
1386 :help "Mark all obsolete packages for deletion"))
1387 (define-key menu-map [mx]
1388 '(menu-item "Execute Actions" package-menu-execute
1389 :help "Perform all the marked actions"))
1390 (define-key menu-map [s5] '("--"))
1391 (define-key menu-map [mh]
1392 '(menu-item "Help" package-menu-quick-help
1393 :help "Show short key binding help for package-menu-mode"))
1394 (define-key menu-map [mc]
1395 '(menu-item "View Commentary" package-menu-view-commentary
1396 :help "Display information about this package"))
1398 "Local keymap for `package-menu-mode' buffers.")
1400 (defvar package-menu--new-package-list nil
1401 "List of newly-available packages since `list-packages' was last called.")
1403 (define-derived-mode package-menu-mode tabulated-list-mode "Package Menu"
1404 "Major mode for browsing a list of packages.
1405 Letters do not insert themselves; instead, they are commands.
1406 \\<package-menu-mode-map>
1407 \\{package-menu-mode-map}"
1408 (setq tabulated-list-format [("Package" 18 package-menu--name-predicate)
1410 ("Status" 10 package-menu--status-predicate)
1411 ("Description" 0 nil)])
1412 (setq tabulated-list-padding 2)
1413 (setq tabulated-list-sort-key (cons "Status" nil))
1414 (tabulated-list-init-header))
1416 (defmacro package--push (pkg-desc status listname)
1417 "Convenience macro for `package-menu--generate'.
1418 If the alist stored in the symbol LISTNAME lacks an entry for a
1419 package PKG-DESC, add one. The alist is keyed with PKG-DESC."
1420 `(unless (assoc ,pkg-desc ,listname)
1421 ;; FIXME: Should we move status into pkg-desc?
1422 (push (cons ,pkg-desc ,status) ,listname)))
1424 (defun package-menu--generate (remember-pos packages)
1425 "Populate the Package Menu.
1426 If REMEMBER-POS is non-nil, keep point on the same entry.
1427 PACKAGES should be t, which means to display all known packages,
1428 or a list of package names (symbols) to display."
1429 ;; Construct list of (PKG-DESC . STATUS).
1430 (let (info-list name)
1431 ;; Installed packages:
1432 (dolist (elt package-alist)
1433 (setq name (car elt))
1434 (when (or (eq packages t) (memq name packages))
1435 (package--push (cdr elt)
1436 (if (stringp (cadr (assq name package-load-list)))
1440 ;; Built-in packages:
1441 (dolist (elt package--builtins)
1442 (setq name (car elt))
1443 (when (and (not (eq name 'emacs)) ; Hide the `emacs' package.
1444 (or (eq packages t) (memq name packages)))
1445 (package--push (package--from-builtin elt) "built-in" info-list)))
1447 ;; Available and disabled packages:
1448 (dolist (elt package-archive-contents)
1449 (setq name (car elt))
1450 (when (or (eq packages t) (memq name packages))
1451 (let ((hold (assq name package-load-list)))
1452 (package--push (cdr elt)
1454 ((and hold (null (cadr hold))) "disabled")
1455 ((memq name package-menu--new-package-list) "new")
1459 ;; Obsolete packages:
1460 (dolist (elt package-obsolete-alist)
1461 (dolist (inner-elt (cdr elt))
1462 (when (or (eq packages t) (memq (car elt) packages))
1463 (package--push (cdr inner-elt) "obsolete" info-list))))
1465 ;; Print the result.
1466 (setq tabulated-list-entries (mapcar 'package-menu--print-info info-list))
1467 (tabulated-list-print remember-pos)))
1469 (defun package-menu--print-info (pkg)
1470 "Return a package entry suitable for `tabulated-list-entries'.
1471 PKG has the form (PKG-DESC . STATUS).
1472 Return (PKG-DESC [NAME VERSION STATUS DOC])."
1473 (let* ((pkg-desc (car pkg))
1476 (`"built-in" 'font-lock-builtin-face)
1477 (`"available" 'default)
1479 (`"held" 'font-lock-constant-face)
1480 (`"disabled" 'font-lock-warning-face)
1481 (`"installed" 'font-lock-comment-face)
1482 (_ 'font-lock-warning-face)))) ; obsolete.
1484 (vector (list (symbol-name (package-desc-name pkg-desc))
1487 'package-desc pkg-desc
1488 'action 'package-menu-describe-package)
1489 (propertize (package-version-join
1490 (package-desc-version pkg-desc))
1491 'font-lock-face face)
1492 (propertize status 'font-lock-face face)
1493 (propertize (package-desc-summary pkg-desc)
1494 'font-lock-face face)))))
1496 (defun package-menu-refresh ()
1497 "Download the Emacs Lisp package archive.
1498 This fetches the contents of each archive specified in
1499 `package-archives', and then refreshes the package menu."
1501 (unless (derived-mode-p 'package-menu-mode)
1502 (error "The current buffer is not a Package Menu"))
1503 (package-refresh-contents)
1504 (package-menu--generate t t))
1506 (defun package-menu-describe-package (&optional button)
1507 "Describe the current package.
1508 If optional arg BUTTON is non-nil, describe its associated package."
1510 (let ((pkg-desc (if button (button-get button 'package-desc)
1511 (car (tabulated-list-get-id)))))
1513 ;; FIXME: We could actually describe this particular pkg-desc.
1514 (describe-package (package-desc-name pkg-desc)))))
1516 ;; fixme numeric argument
1517 (defun package-menu-mark-delete (&optional _num)
1518 "Mark a package for deletion and move to the next line."
1520 (if (member (package-menu-get-status) '("installed" "obsolete"))
1521 (tabulated-list-put-tag "D" t)
1524 (defun package-menu-mark-install (&optional _num)
1525 "Mark a package for installation and move to the next line."
1527 (if (member (package-menu-get-status) '("available" "new"))
1528 (tabulated-list-put-tag "I" t)
1531 (defun package-menu-mark-unmark (&optional _num)
1532 "Clear any marks on a package and move to the next line."
1534 (tabulated-list-put-tag " " t))
1536 (defun package-menu-backup-unmark ()
1537 "Back up one line and clear any marks on that package."
1540 (tabulated-list-put-tag " "))
1542 (defun package-menu-mark-obsolete-for-deletion ()
1543 "Mark all obsolete packages for deletion."
1546 (goto-char (point-min))
1548 (if (equal (package-menu-get-status) "obsolete")
1549 (tabulated-list-put-tag "D" t)
1550 (forward-line 1)))))
1552 (defun package-menu-quick-help ()
1553 "Show short key binding help for package-menu-mode."
1555 (message "n-ext, i-nstall, d-elete, u-nmark, x-ecute, r-efresh, h-elp"))
1557 (define-obsolete-function-alias
1558 'package-menu-view-commentary 'package-menu-describe-package "24.1")
1560 (defun package-menu-get-status ()
1561 (let* ((id (tabulated-list-get-id))
1562 (entry (and id (assq id tabulated-list-entries))))
1564 (aref (cadr entry) 2)
1567 (defun package-menu--find-upgrades ()
1568 (let (installed available upgrades)
1569 ;; Build list of installed/available packages in this buffer.
1570 (dolist (entry tabulated-list-entries)
1571 ;; ENTRY is (PKG-DESC [NAME VERSION STATUS DOC])
1572 (let ((pkg-desc (car entry))
1573 (status (aref (cadr entry) 2)))
1574 (cond ((equal status "installed")
1575 (push pkg-desc installed))
1576 ((member status '("available" "new"))
1577 (push (cons (package-desc-name pkg-desc) pkg-desc)
1579 ;; Loop through list of installed packages, finding upgrades.
1580 (dolist (pkg-desc installed)
1581 (let ((avail-pkg (assq (package-desc-name pkg-desc) available)))
1583 (version-list-< (package-desc-version pkg-desc)
1584 (package-desc-version (cdr avail-pkg)))
1585 (push avail-pkg upgrades))))
1588 (defun package-menu-mark-upgrades ()
1589 "Mark all upgradable packages in the Package Menu.
1590 For each installed package with a newer version available, place
1591 an (I)nstall flag on the available version and a (D)elete flag on
1592 the installed version. A subsequent \\[package-menu-execute]
1593 call will upgrade the package."
1595 (unless (derived-mode-p 'package-menu-mode)
1596 (error "The current buffer is not a Package Menu"))
1597 (let ((upgrades (package-menu--find-upgrades)))
1599 (message "No packages to upgrade.")
1602 (goto-char (point-min))
1604 (let* ((pkg-desc (tabulated-list-get-id))
1605 (upgrade (cdr (assq (package-desc-name pkg-desc) upgrades))))
1606 (cond ((null upgrade)
1608 ((equal pkg-desc upgrade)
1609 (package-menu-mark-install))
1611 (package-menu-mark-delete))))))
1612 (message "%d package%s marked for upgrading."
1614 (if (= (length upgrades) 1) "" "s")))))
1616 (defun package-menu-execute (&optional noquery)
1617 "Perform marked Package Menu actions.
1618 Packages marked for installation are downloaded and installed;
1619 packages marked for deletion are removed.
1620 Optional argument NOQUERY non-nil means do not ask the user to confirm."
1622 (unless (derived-mode-p 'package-menu-mode)
1623 (error "The current buffer is not in Package Menu mode"))
1624 (let (install-list delete-list cmd pkg-desc)
1626 (goto-char (point-min))
1628 (setq cmd (char-after))
1629 (unless (eq cmd ?\s)
1630 ;; This is the key PKG-DESC.
1631 (setq pkg-desc (tabulated-list-get-id))
1633 (push pkg-desc delete-list))
1635 (push pkg-desc install-list))))
1641 (if (= (length install-list) 1)
1642 (format "Install package `%s'? "
1643 (package-desc-full-name (car install-list)))
1644 (format "Install these %d packages (%s)? "
1645 (length install-list)
1646 (mapconcat #'package-desc-full-name
1647 install-list ", ")))))
1648 (mapc 'package-install install-list)))
1649 ;; Delete packages, prompting if necessary.
1654 (if (= (length delete-list) 1)
1655 (format "Delete package `%s'? "
1656 (package-desc-full-name (car delete-list)))
1657 (format "Delete these %d packages (%s)? "
1658 (length delete-list)
1659 (mapconcat #'package-desc-full-name
1660 delete-list ", ")))))
1661 (dolist (elt delete-list)
1662 (condition-case-unless-debug err
1663 (package-delete elt)
1664 (error (message (cadr err)))))
1666 ;; If we deleted anything, regenerate `package-alist'. This is done
1667 ;; automatically if we installed a package.
1668 (and delete-list (null install-list)
1669 (package-initialize))
1670 (if (or delete-list install-list)
1671 (package-menu--generate t t)
1672 (message "No operations specified."))))
1674 (defun package-menu--version-predicate (A B)
1675 (let ((vA (or (aref (cadr A) 1) '(0)))
1676 (vB (or (aref (cadr B) 1) '(0))))
1677 (if (version-list-= vA vB)
1678 (package-menu--name-predicate A B)
1679 (version-list-< vA vB))))
1681 (defun package-menu--status-predicate (A B)
1682 (let ((sA (aref (cadr A) 2))
1683 (sB (aref (cadr B) 2)))
1684 (cond ((string= sA sB)
1685 (package-menu--name-predicate A B))
1686 ((string= sA "new") t)
1687 ((string= sB "new") nil)
1688 ((string= sA "available") t)
1689 ((string= sB "available") nil)
1690 ((string= sA "installed") t)
1691 ((string= sB "installed") nil)
1692 ((string= sA "held") t)
1693 ((string= sB "held") nil)
1694 ((string= sA "built-in") t)
1695 ((string= sB "built-in") nil)
1696 ((string= sA "obsolete") t)
1697 ((string= sB "obsolete") nil)
1698 (t (string< sA sB)))))
1700 (defun package-menu--description-predicate (A B)
1701 (let ((dA (aref (cadr A) 3))
1702 (dB (aref (cadr B) 3)))
1704 (package-menu--name-predicate A B)
1707 (defun package-menu--name-predicate (A B)
1708 (string< (symbol-name (package-desc-name (car A)))
1709 (symbol-name (package-desc-name (car B)))))
1712 (defun list-packages (&optional no-fetch)
1713 "Display a list of packages.
1714 This first fetches the updated list of packages before
1715 displaying, unless a prefix argument NO-FETCH is specified.
1716 The list is displayed in a buffer named `*Packages*'."
1718 (require 'finder-inf nil t)
1719 ;; Initialize the package system if necessary.
1720 (unless package--initialized
1721 (package-initialize t))
1722 (let (old-archives new-packages)
1724 ;; Read the locally-cached archive-contents.
1725 (package-read-all-archive-contents)
1726 (setq old-archives package-archive-contents)
1727 ;; Fetch the remote list of packages.
1728 (package-refresh-contents)
1729 ;; Find which packages are new.
1730 (dolist (elt package-archive-contents)
1731 (unless (assq (car elt) old-archives)
1732 (push (car elt) new-packages))))
1734 ;; Generate the Package Menu.
1735 (let ((buf (get-buffer-create "*Packages*")))
1736 (with-current-buffer buf
1738 (set (make-local-variable 'package-menu--new-package-list)
1740 (package-menu--generate nil t))
1741 ;; The package menu buffer has keybindings. If the user types
1742 ;; `M-x list-packages', that suggests it should become current.
1743 (switch-to-buffer buf))
1745 (let ((upgrades (package-menu--find-upgrades)))
1747 (message "%d package%s can be upgraded; type `%s' to mark %s for upgrading."
1749 (if (= (length upgrades) 1) "" "s")
1750 (substitute-command-keys "\\[package-menu-mark-upgrades]")
1751 (if (= (length upgrades) 1) "it" "them"))))))
1754 (defalias 'package-list-packages 'list-packages)
1756 ;; Used in finder.el
1757 (defun package-show-package-list (packages)
1758 "Display PACKAGES in a *Packages* buffer.
1759 This is similar to `list-packages', but it does not fetch the
1760 updated list of packages, and it only displays packages with
1761 names in PACKAGES (which should be a list of symbols)."
1762 (require 'finder-inf nil t)
1763 (let ((buf (get-buffer-create "*Packages*")))
1764 (with-current-buffer buf
1766 (package-menu--generate nil packages))
1767 (switch-to-buffer buf)))
1769 (defun package-list-packages-no-fetch ()
1770 "Display a list of packages.
1771 Does not fetch the updated list of packages before displaying.
1772 The list is displayed in a buffer named `*Packages*'."
1778 ;;; package.el ends here