1 ;;; package.el --- Simple package system for Emacs -*- lexical-binding:t -*-
3 ;; Copyright (C) 2007-2013 Free Software Foundation, Inc.
5 ;; Author: Tom Tromey <tromey@redhat.com>
6 ;; Daniel Hackney <dan@haxney.org>
7 ;; Created: 10 Mar 2007
10 ;; Package-Requires: ((tabulated-list "1.0"))
12 ;; This file is part of GNU Emacs.
14 ;; GNU Emacs is free software: you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation, either version 3 of the License, or
17 ;; (at your option) any later version.
19 ;; GNU Emacs is distributed in the hope that it will be useful,
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 ;; GNU General Public License for more details.
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
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
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
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:
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
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.
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>
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>
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
142 ;; - Interface with desktop.el so that restarting after an install
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 ;; - Our treatment of the info path is somewhat bogus
166 (eval-when-compile (require 'cl-lib))
168 (require 'tabulated-list)
170 (defgroup package nil
171 "Manager for Emacs Lisp packages."
176 (defcustom package-enable-at-startup t
177 "Whether to activate installed packages when Emacs starts.
178 If non-nil, packages are activated after reading the init file
179 and before `after-init-hook'. Activation is not done if
180 `user-init-file' is nil (e.g. Emacs was started with \"-q\").
182 Even if the value is nil, you can type \\[package-initialize] to
183 activate the package system at any time."
188 (defcustom package-load-list '(all)
189 "List of packages for `package-initialize' to load.
190 Each element in this list should be a list (NAME VERSION), or the
191 symbol `all'. The symbol `all' says to load the latest installed
192 versions of all packages not specified by other elements.
194 For an element (NAME VERSION), NAME is a package name (a symbol).
195 VERSION should be t, a string, or nil.
196 If VERSION is t, the most recent version is activated.
197 If VERSION is a string, only that version is ever loaded.
198 Any other version, even if newer, is silently ignored.
199 Hence, the package is \"held\" at that version.
200 If VERSION is nil, the package is not loaded (it is \"disabled\")."
201 :type '(repeat symbol)
206 (defvar Info-directory-list)
207 (declare-function info-initialize "info" ())
208 (declare-function url-http-parse-response "url-http" ())
209 (declare-function lm-header "lisp-mnt" (header))
210 (declare-function lm-commentary "lisp-mnt" (&optional file))
211 (defvar url-http-end-of-headers)
213 (defcustom package-archives '(("gnu" . "http://elpa.gnu.org/packages/"))
214 "An alist of archives from which to fetch.
215 The default value points to the GNU Emacs package repository.
217 Each element has the form (ID . LOCATION).
218 ID is an archive name, as a string.
219 LOCATION specifies the base location for the archive.
220 If it starts with \"http:\", it is treated as a HTTP URL;
221 otherwise it should be an absolute directory name.
222 (Other types of URL are currently not supported.)
224 Only add locations that you trust, since fetching and installing
225 a package can run arbitrary code."
226 :type '(alist :key-type (string :tag "Archive name")
227 :value-type (string :tag "URL or directory name"))
232 (defcustom package-pinned-packages nil
233 "An alist of packages that are pinned to a specific archive
235 Each element has the form (SYM . ID).
236 SYM is a package, as a symbol.
237 ID is an archive name. This should correspond to an
238 entry in `package-archives'.
240 If the archive of name ID does not contain the package SYM, no
241 other location will be considered, which will make the
242 package unavailable."
243 :type '(alist :key-type (symbol :tag "Package")
244 :value-type (string :tag "Archive name"))
249 (defconst package-archive-version 1
250 "Version number of the package archive understood by this file.
251 Lower version numbers than this will probably be understood as well.")
253 ;; We don't prime the cache since it tends to get out of date.
254 (defvar package-archive-contents nil
255 "Cache of the contents of the Emacs Lisp Package Archive.
256 This is an alist mapping package names (symbols) to
257 non-empty lists of `package-desc' structures.")
258 (put 'package-archive-contents 'risky-local-variable t)
260 (defcustom package-user-dir (locate-user-emacs-file "elpa")
261 "Directory containing the user's Emacs Lisp packages.
262 The directory name should be absolute.
263 Apart from this directory, Emacs also looks for system-wide
264 packages in `package-directory-list'."
270 (defcustom package-directory-list
271 ;; Defaults are subdirs named "elpa" in the site-lisp dirs.
273 (dolist (f load-path)
275 (equal (file-name-nondirectory f) "site-lisp")
276 (push (expand-file-name "elpa" f) result)))
278 "List of additional directories containing Emacs Lisp packages.
279 Each directory name should be absolute.
281 These directories contain packages intended for system-wide; in
282 contrast, `package-user-dir' contains packages for personal use."
283 :type '(repeat directory)
288 (defvar package--default-summary "No description available.")
290 (cl-defstruct (package-desc
291 ;; Rename the default constructor from `make-package-desc'.
292 (:constructor package-desc-create)
293 ;; Has the same interface as the old `define-package',
294 ;; which is still used in the "foo-pkg.el" files. Extra
295 ;; options can be supported by adding additional keys.
297 package-desc-from-define
298 (name-string version-string &optional summary requirements
301 (name (intern name-string))
302 (version (version-to-list version-string))
303 (reqs (mapcar #'(lambda (elt)
305 (version-to-list (cadr elt))))
306 (if (eq 'quote (car requirements))
309 (kind (plist-get rest-plist :kind))
310 (archive (plist-get rest-plist :archive))
312 (cl-remf rest-plist :kind)
313 (cl-remf rest-plist :archive)
315 (let ((value (cadr rest-plist)))
317 (push (cons (car rest-plist) value)
319 (setq rest-plist (cddr rest-plist)))
321 "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),
342 `builtin' if it is built-in, or nil otherwise.
344 `extras' Optional alist of additional keyword-value pairs."
347 (summary package--default-summary)
355 (defun package-desc-full-name (pkg-desc)
357 (package-desc-name pkg-desc)
358 (package-version-join (package-desc-version pkg-desc))))
360 (defun package-desc-suffix (pkg-desc)
361 (pcase (package-desc-kind pkg-desc)
364 (kind (error "Unknown package kind: %s" kind))))
366 ;; Package descriptor format used in finder-inf.el and package--builtins.
367 (cl-defstruct (package--bi-desc
368 (:constructor package-make-builtin (version summary))
374 (defvar package--builtins nil
375 "Alist of built-in packages.
376 The actual value is initialized by loading the library
377 `finder-inf'; this is not done until it is needed, e.g. by the
378 function `package-built-in-p'.
380 Each element has the form (PKG . PACKAGE-BI-DESC), where PKG is a package
381 name (a symbol) and DESC is a `package--bi-desc' structure.")
382 (put 'package--builtins 'risky-local-variable t)
384 (defvar package-alist nil
385 "Alist of all packages available for activation.
386 Each element has the form (PKG . DESCS), where PKG is a package
387 name (a symbol) and DESCS is a non-empty list of `package-desc' structure,
388 sorted by decreasing versions.
390 This variable is set automatically by `package-load-descriptor',
391 called via `package-initialize'. To change which packages are
392 loaded and/or activated, customize `package-load-list'.")
393 (put 'package-alist 'risky-local-variable t)
395 (defvar package-activated-list nil
396 ;; FIXME: This should implicitly include all builtin packages.
397 "List of the names of currently activated packages.")
398 (put 'package-activated-list 'risky-local-variable t)
400 (defun package-version-join (vlist)
401 "Return the version string corresponding to the list VLIST.
402 This is, approximately, the inverse of `version-to-list'.
403 \(Actually, it returns only one of the possible inverses, since
404 `version-to-list' is a many-to-one operation.)"
407 (let ((str-list (list "." (int-to-string (car vlist)))))
408 (dolist (num (cdr vlist))
411 (push (int-to-string num) str-list)
414 (error "Invalid version list `%s'" vlist))
416 ;; pre, or beta, or alpha
417 (cond ((equal "." (car str-list))
419 ((not (string-match "[0-9]+" (car str-list)))
420 (error "Invalid version list `%s'" vlist)))
421 (push (cond ((= num -1) "pre")
423 ((= num -3) "alpha"))
425 (if (equal "." (car str-list))
427 (apply 'concat (nreverse str-list)))))
429 (defun package-load-descriptor (pkg-dir)
430 "Load the description file in directory PKG-DIR."
431 (let ((pkg-file (expand-file-name (package--description-file pkg-dir)
433 (when (file-exists-p pkg-file)
435 (insert-file-contents pkg-file)
436 (goto-char (point-min))
437 (let ((pkg-desc (package-process-define-package
438 (read (current-buffer)) pkg-file)))
439 (setf (package-desc-dir pkg-desc) pkg-dir)
442 (defun package-load-all-descriptors ()
443 "Load descriptors for installed Emacs Lisp packages.
444 This looks for package subdirectories in `package-user-dir' and
445 `package-directory-list'. The variable `package-load-list'
446 controls which package subdirectories may be loaded.
448 In each valid package subdirectory, this function loads the
449 description file containing a call to `define-package', which
450 updates `package-alist'."
451 (dolist (dir (cons package-user-dir package-directory-list))
452 (when (file-directory-p dir)
453 (dolist (subdir (directory-files dir))
454 (let ((pkg-dir (expand-file-name subdir dir)))
455 (when (file-directory-p pkg-dir)
456 (package-load-descriptor pkg-dir)))))))
458 (defun package-disabled-p (pkg-name version)
459 "Return whether PKG-NAME at VERSION can be activated.
460 The decision is made according to `package-load-list'.
461 Return nil if the package can be activated.
462 Return t if the package is completely disabled.
463 Return the max version (as a string) if the package is held at a lower version."
464 (let ((force (assq pkg-name package-load-list)))
465 (cond ((null force) (not (memq 'all package-load-list)))
466 ((null (setq force (cadr force))) t) ; disabled
468 ((stringp force) ; held
469 (unless (version-list-= version (version-to-list force))
471 (t (error "Invalid element in `package-load-list'")))))
473 (defun package-activate-1 (pkg-desc)
474 (let* ((name (package-desc-name pkg-desc))
475 (pkg-dir (package-desc-dir pkg-desc))
476 (pkg-dir-dir (file-name-as-directory pkg-dir)))
478 (error "Internal error: unable to find directory for `%s'"
479 (package-desc-full-name pkg-desc)))
480 ;; Add to load path, add autoloads, and activate the package.
481 (let ((old-lp load-path))
483 (load (expand-file-name (format "%s-autoloads" name) pkg-dir) nil t))
484 (when (and (eq old-lp load-path)
485 (not (or (member pkg-dir load-path)
486 (member pkg-dir-dir load-path))))
487 ;; Old packages don't add themselves to the `load-path', so we have to
489 (push pkg-dir load-path)))
491 (when (file-exists-p (expand-file-name "dir" pkg-dir))
492 ;; FIXME: not the friendliest, but simple.
495 (push pkg-dir Info-directory-list))
496 (push name package-activated-list)
500 (defun package-built-in-p (package &optional min-version)
501 "Return true if PACKAGE is built-in to Emacs.
502 Optional arg MIN-VERSION, if non-nil, should be a version list
503 specifying the minimum acceptable version."
504 (let ((bi (assq package package--builtin-versions)))
506 (bi (version-list-<= min-version (cdr bi)))
509 (require 'finder-inf nil t) ; For `package--builtins'.
510 (assq package package--builtins)))))
512 (defun package--from-builtin (bi-desc)
513 (package-desc-create :name (pop bi-desc)
514 :version (package--bi-desc-version bi-desc)
515 :summary (package--bi-desc-summary bi-desc)
518 ;; This function goes ahead and activates a newer version of a package
519 ;; if an older one was already activated. This is not ideal; we'd at
520 ;; least need to check to see if the package has actually been loaded,
521 ;; and not merely activated.
522 (defun package-activate (package &optional force)
523 "Activate package PACKAGE.
524 If FORCE is true, (re-)activate it if it's already activated."
525 (let ((pkg-descs (cdr (assq package package-alist))))
526 ;; Check if PACKAGE is available in `package-alist'.
529 (let ((available-version (package-desc-version (car pkg-descs))))
530 (or (package-disabled-p package available-version)
531 ;; Prefer a builtin package.
532 (package-built-in-p package available-version))))
533 (setq pkg-descs (cdr pkg-descs)))
535 ;; If no such package is found, maybe it's built-in.
537 (package-built-in-p package))
538 ;; If the package is already activated, just return t.
539 ((and (memq package package-activated-list) (not force))
541 ;; Otherwise, proceed with activation.
543 (let* ((pkg-vec (car pkg-descs))
544 (fail (catch 'dep-failure
545 ;; Activate its dependencies recursively.
546 (dolist (req (package-desc-reqs pkg-vec))
547 (unless (package-activate (car req) (cadr req))
548 (throw 'dep-failure req))))))
550 (warn "Unable to activate package `%s'.
551 Required package `%s-%s' is unavailable"
552 package (car fail) (package-version-join (cadr fail)))
553 ;; If all goes well, activate the package itself.
554 (package-activate-1 pkg-vec)))))))
556 (defun define-package (_name-string _version-string
557 &optional _docstring _requirements
558 &rest _extra-properties)
559 "Define a new package.
560 NAME-STRING is the name of the package, as a string.
561 VERSION-STRING is the version of the package, as a string.
562 DOCSTRING is a short description of the package, a string.
563 REQUIREMENTS is a list of dependencies on other packages.
564 Each requirement is of the form (OTHER-PACKAGE OTHER-VERSION),
565 where OTHER-VERSION is a string.
567 EXTRA-PROPERTIES is currently unused."
568 ;; FIXME: Placeholder! Should we keep it?
569 (error "Don't call me!"))
571 (defun package-process-define-package (exp origin)
572 (unless (eq (car-safe exp) 'define-package)
573 (error "Can't find define-package in %s" origin))
574 (let* ((new-pkg-desc (apply #'package-desc-from-define (cdr exp)))
575 (name (package-desc-name new-pkg-desc))
576 (version (package-desc-version new-pkg-desc))
577 (old-pkgs (assq name package-alist)))
579 ;; If there's no old package, just add this to `package-alist'.
580 (push (list name new-pkg-desc) package-alist)
581 ;; If there is, insert the new package at the right place in the list.
583 (if (and (cdr old-pkgs)
584 (version-list-< version
585 (package-desc-version (cadr old-pkgs))))
586 (setq old-pkgs (cdr old-pkgs))
587 (push new-pkg-desc (cdr old-pkgs))
591 ;; From Emacs 22, but changed so it adds to load-path.
592 (defun package-autoload-ensure-default-file (file)
593 "Make sure that the autoload file FILE exists and if not create it."
594 (unless (file-exists-p file)
596 (concat ";;; " (file-name-nondirectory file)
597 " --- automatically extracted autoloads\n"
600 "(add-to-list 'load-path (or (file-name-directory #$) (car load-path)))\n"
601 "
\f\n;; Local Variables:\n"
602 ";; version-control: never\n"
603 ";; no-byte-compile: t\n"
604 ";; no-update-autoloads: t\n"
606 ";;; " (file-name-nondirectory file)
611 (defvar generated-autoload-file)
612 (defvar version-control)
614 (defun package-generate-autoloads (name pkg-dir)
615 (let* ((auto-name (format "%s-autoloads.el" name))
616 ;;(ignore-name (concat name "-pkg.el"))
617 (generated-autoload-file (expand-file-name auto-name pkg-dir))
618 (version-control 'never))
619 (package-autoload-ensure-default-file generated-autoload-file)
620 (update-directory-autoloads pkg-dir)
621 (let ((buf (find-buffer-visiting generated-autoload-file)))
622 (when buf (kill-buffer buf)))
625 (defvar tar-parse-info)
626 (declare-function tar-untar-buffer "tar-mode" ())
627 (declare-function tar-header-name "tar-mode" (tar-header) t)
628 (declare-function tar-header-link-type "tar-mode" (tar-header) t)
630 (defun package-untar-buffer (dir)
631 "Untar the current buffer.
632 This uses `tar-untar-buffer' from Tar mode. All files should
633 untar into a directory named DIR; otherwise, signal an error."
636 ;; Make sure everything extracts into DIR.
637 (let ((regexp (concat "\\`" (regexp-quote (expand-file-name dir)) "/"))
638 (case-fold-search (memq system-type '(windows-nt ms-dos cygwin))))
639 (dolist (tar-data tar-parse-info)
640 (let ((name (expand-file-name (tar-header-name tar-data))))
641 (or (string-match regexp name)
642 ;; Tarballs created by some utilities don't list
643 ;; directories with a trailing slash (Bug#13136).
644 (and (string-equal dir name)
645 (eq (tar-header-link-type tar-data) 5))
646 (error "Package does not untar cleanly into directory %s/" dir)))))
649 (defun package-generate-description-file (pkg-desc pkg-dir)
650 "Create the foo-pkg.el file for single-file packages."
651 (let* ((name (package-desc-name pkg-desc))
652 (pkg-file (expand-file-name (package--description-file pkg-dir)
654 (let ((print-level nil)
661 (list 'define-package
663 (package-version-join (package-desc-version pkg-desc))
664 (package-desc-summary pkg-desc)
665 (let ((requires (package-desc-reqs pkg-desc)))
667 ;; Turn version lists into string form.
671 (package-version-join (cadr elt))))
673 (package--alist-to-plist
674 (package-desc-extras pkg-desc))))
679 (defun package--alist-to-plist (alist)
680 (apply #'nconc (mapcar (lambda (pair) (list (car pair) (cdr pair))) alist)))
682 (defun package-unpack (pkg-desc)
683 "Install the contents of the current buffer as a package."
684 (let* ((name (package-desc-name pkg-desc))
685 (dirname (package-desc-full-name pkg-desc))
686 (pkg-dir (expand-file-name dirname package-user-dir)))
687 (pcase (package-desc-kind pkg-desc)
689 (make-directory package-user-dir t)
690 ;; FIXME: should we delete PKG-DIR if it exists?
691 (let* ((default-directory (file-name-as-directory package-user-dir)))
692 (package-untar-buffer dirname)))
694 (let ((el-file (expand-file-name (format "%s.el" name) pkg-dir)))
695 (make-directory pkg-dir t)
696 (package--write-file-no-coding el-file)))
697 (kind (error "Unknown package kind: %S" kind)))
698 (package--make-autoloads-and-stuff pkg-desc pkg-dir)
699 ;; Update package-alist.
700 (let ((new-desc (package-load-descriptor pkg-dir)))
701 ;; FIXME: Check that `new-desc' matches `desc'!
702 ;; FIXME: Compilation should be done as a separate, optional, step.
703 ;; E.g. for multi-package installs, we should first install all packages
704 ;; and then compile them.
705 (package--compile new-desc))
706 ;; Try to activate it.
707 (package-activate name 'force)
710 (defun package--make-autoloads-and-stuff (pkg-desc pkg-dir)
711 "Generate autoloads, description file, etc.. for PKG-DESC installed at PKG-DIR."
712 (package-generate-autoloads (package-desc-name pkg-desc) pkg-dir)
713 (let ((desc-file (package--description-file pkg-dir)))
714 (unless (file-exists-p desc-file)
715 (package-generate-description-file pkg-desc pkg-dir)))
716 ;; FIXME: Create foo.info and dir file from foo.texi?
719 (defun package--compile (pkg-desc)
720 "Byte-compile installed package PKG-DESC."
721 (package-activate-1 pkg-desc)
722 (byte-recompile-directory (package-desc-dir pkg-desc) 0 t))
724 (defun package--write-file-no-coding (file-name)
725 (let ((buffer-file-coding-system 'no-conversion))
726 (write-region (point-min) (point-max) file-name)))
728 (defmacro package--with-work-buffer (location file &rest body)
729 "Run BODY in a buffer containing the contents of FILE at LOCATION.
730 LOCATION is the base location of a package archive, and should be
731 one of the URLs (or file names) specified in `package-archives'.
732 FILE is the name of a file relative to that base location.
734 This macro retrieves FILE from LOCATION into a temporary buffer,
735 and evaluates BODY while that buffer is current. This work
736 buffer is killed afterwards. Return the last value in BODY."
737 (declare (indent 2) (debug t))
738 `(let* ((http (string-match "\\`https?:" ,location))
741 (url-retrieve-synchronously (concat ,location ,file))
742 (generate-new-buffer "*package work buffer*"))))
744 (with-current-buffer buffer
746 (progn (package-handle-response)
747 (re-search-forward "^$" nil 'move)
749 (delete-region (point-min) (point)))
750 (unless (file-name-absolute-p ,location)
751 (error "Archive location %s is not an absolute file name"
753 (insert-file-contents (expand-file-name ,file ,location)))
755 (kill-buffer buffer))))
757 (defun package-handle-response ()
758 "Handle the response from a `url-retrieve-synchronously' call.
759 Parse the HTTP response and throw if an error occurred.
760 The url package seems to require extra processing for this.
761 This should be called in a `save-excursion', in the download buffer.
762 It will move point to somewhere in the headers."
763 ;; We assume HTTP here.
765 (let ((response (url-http-parse-response)))
766 (when (or (< response 200) (>= response 300))
767 (error "Error during download request:%s"
768 (buffer-substring-no-properties (point) (line-end-position))))))
770 (defun package-install-from-archive (pkg-desc)
771 "Download and install a tar package."
772 (let ((location (package-archive-base pkg-desc))
773 (file (concat (package-desc-full-name pkg-desc)
774 (package-desc-suffix pkg-desc))))
775 (package--with-work-buffer location file
776 (package-unpack pkg-desc))))
778 (defvar package--initialized nil)
780 (defun package-installed-p (package &optional min-version)
781 "Return true if PACKAGE, of MIN-VERSION or newer, is installed.
782 MIN-VERSION should be a version list."
783 (unless package--initialized (error "package.el is not yet initialized!"))
785 (let ((pkg-descs (cdr (assq package package-alist))))
787 (version-list-<= min-version
788 (package-desc-version (car pkg-descs)))))
789 ;; Also check built-in packages.
790 (package-built-in-p package min-version)))
792 (defun package-compute-transaction (packages requirements)
793 "Return a list of packages to be installed, including PACKAGES.
794 PACKAGES should be a list of `package-desc'.
796 REQUIREMENTS should be a list of additional requirements; each
797 element in this list should have the form (PACKAGE VERSION-LIST),
798 where PACKAGE is a package name and VERSION-LIST is the required
799 version of that package.
801 This function recursively computes the requirements of the
802 packages in REQUIREMENTS, and returns a list of all the packages
803 that must be installed. Packages that are already installed are
804 not included in this list."
805 ;; FIXME: We really should use backtracking to explore the whole
806 ;; search space (e.g. if foo require bar-1.3, and bar-1.4 requires toto-1.1
807 ;; whereas bar-1.3 requires toto-1.0 and the user has put a hold on toto-1.0:
808 ;; the current code might fail to see that it could install foo by using the
810 (dolist (elt requirements)
811 (let* ((next-pkg (car elt))
812 (next-version (cadr elt))
814 (dolist (pkg packages)
815 (if (eq next-pkg (package-desc-name pkg))
819 (if (version-list-< next-version (package-desc-version already))
820 ;; Move to front, so it gets installed early enough (bug#14082).
821 (setq packages (cons already (delq already packages)))
822 (error "Need package `%s-%s', but only %s is available"
823 next-pkg (package-version-join next-version)
824 (package-version-join (package-desc-version already)))))
826 ((package-installed-p next-pkg next-version) nil)
829 ;; A package is required, but not installed. It might also be
830 ;; blocked via `package-load-list'.
831 (let ((pkg-descs (cdr (assq next-pkg package-archive-contents)))
834 (while (and pkg-descs (not found))
835 (let* ((pkg-desc (pop pkg-descs))
836 (version (package-desc-version pkg-desc))
837 (disabled (package-disabled-p next-pkg version)))
839 ((version-list-< version next-version)
841 "Need package `%s-%s', but only %s is available"
842 next-pkg (package-version-join next-version)
843 (package-version-join version)))
847 (if (stringp disabled)
848 (format "Package `%s' held at version %s, \
849 but version %s required"
851 (package-version-join next-version))
852 (format "Required package '%s' is disabled"
854 (t (setq found pkg-desc)))))
858 (error "Package `%s-%s' is unavailable"
859 next-pkg (package-version-join next-version))))
861 (package-compute-transaction (cons found packages)
862 (package-desc-reqs found))))))))
865 (defun package-read-from-string (str)
866 "Read a Lisp expression from STR.
867 Signal an error if the entire string was not used."
868 (let* ((read-data (read-from-string str))
871 ;; The call to `ignore' suppresses a compiler warning.
872 (progn (ignore (read-from-string
873 (substring str (cdr read-data))))
877 (error "Can't read whole string")
880 (defun package--read-archive-file (file)
881 "Re-read archive file FILE, if it exists.
882 Will return the data from the file, or nil if the file does not exist.
883 Will throw an error if the archive version is too new."
884 (let ((filename (expand-file-name file package-user-dir)))
885 (when (file-exists-p filename)
887 (insert-file-contents-literally filename)
888 (let ((contents (read (current-buffer))))
889 (if (> (car contents) package-archive-version)
890 (error "Package archive version %d is higher than %d"
891 (car contents) package-archive-version))
894 (defun package-read-all-archive-contents ()
895 "Re-read `archive-contents', if it exists.
896 If successful, set `package-archive-contents'."
897 (setq package-archive-contents nil)
898 (dolist (archive package-archives)
899 (package-read-archive-contents (car archive))))
901 (defun package-read-archive-contents (archive)
902 "Re-read archive contents for ARCHIVE.
903 If successful, set the variable `package-archive-contents'.
904 If the archive version is too new, signal an error."
905 ;; Version 1 of 'archive-contents' is identical to our internal
907 (let* ((contents-file (format "archives/%s/archive-contents" archive))
908 (contents (package--read-archive-file contents-file)))
910 (dolist (package contents)
911 (package--add-to-archive-contents package archive)))))
913 ;; Package descriptor objects used inside the "archive-contents" file.
914 ;; Changing this defstruct implies changing the format of the
915 ;; "archive-contents" files.
916 (cl-defstruct (package--ac-desc
917 (:constructor package-make-ac-desc (version reqs summary kind extras))
920 version reqs summary kind extras)
922 (defun package--add-to-archive-contents (package archive)
923 "Add the PACKAGE from the given ARCHIVE if necessary.
924 PACKAGE should have the form (NAME . PACKAGE--AC-DESC).
925 Also, add the originating archive to the `package-desc' structure."
926 (let* ((name (car package))
927 (version (package--ac-desc-version (cdr package)))
932 :reqs (package--ac-desc-reqs (cdr package))
933 :summary (package--ac-desc-summary (cdr package))
934 :kind (package--ac-desc-kind (cdr package))
936 :extras (and (> (length (cdr package)) 4)
937 ;; Older archive-contents files have only 4
939 (package--ac-desc-extras (cdr package)))))
940 (existing-packages (assq name package-archive-contents))
941 (pinned-to-archive (assoc name package-pinned-packages)))
943 ;; Skip entirely if pinned to another archive or already installed.
944 ((or (and pinned-to-archive
945 (not (equal (cdr pinned-to-archive) archive)))
946 (let ((bi (assq name package--builtin-versions)))
947 (and bi (version-list-= version (cdr bi))))
948 (let ((ins (cdr (assq name package-alist))))
949 (and ins (version-list-= version
950 (package-desc-version (car ins))))))
952 ((not existing-packages)
953 (push (list name pkg-desc) package-archive-contents))
956 (if (and (cdr existing-packages)
958 version (package-desc-version (cadr existing-packages))))
959 (setq existing-packages (cdr existing-packages))
960 (push pkg-desc (cdr existing-packages))
963 (defun package-download-transaction (packages)
964 "Download and install all the packages in PACKAGES.
965 PACKAGES should be a list of package-desc.
966 This function assumes that all package requirements in
967 PACKAGES are satisfied, i.e. that PACKAGES is computed
968 using `package-compute-transaction'."
969 (mapc #'package-install-from-archive packages))
972 (defun package-install (pkg)
973 "Install the package PKG.
974 PKG can be a package-desc or the package name of one the available packages
975 in an archive in `package-archives'. Interactively, prompt for its name."
978 ;; Initialize the package system to get the list of package
979 ;; symbols for completion.
980 (unless package--initialized
981 (package-initialize t))
982 (unless package-archive-contents
983 (package-refresh-contents))
984 (list (intern (completing-read
986 (mapcar (lambda (elt) (symbol-name (car elt)))
987 package-archive-contents)
989 (package-download-transaction
990 (if (package-desc-p pkg)
991 (package-compute-transaction (list pkg)
992 (package-desc-reqs pkg))
993 (package-compute-transaction ()
994 (list (list pkg))))))
996 (defun package-strip-rcs-id (str)
997 "Strip RCS version ID from the version string STR.
998 If the result looks like a dotted numeric version, return it.
999 Otherwise return nil."
1001 (when (string-match "\\`[ \t]*[$]Revision:[ \t]+" str)
1002 (setq str (substring str (match-end 0))))
1004 (if (version-to-list str)
1008 (defun package-buffer-info ()
1009 "Return a `package-desc' describing the package in the current buffer.
1011 If the buffer does not contain a conforming package, signal an
1012 error. If there is a package, narrow the buffer to the file's
1014 (goto-char (point-min))
1015 (unless (re-search-forward "^;;; \\([^ ]*\\)\\.el ---[ \t]*\\(.*?\\)[ \t]*\\(-\\*-.*-\\*-[ \t]*\\)?$" nil t)
1016 (error "Package lacks a file header"))
1017 (let ((file-name (match-string-no-properties 1))
1018 (desc (match-string-no-properties 2))
1019 (start (line-beginning-position)))
1020 (unless (search-forward (concat ";;; " file-name ".el ends here"))
1021 (error "Package lacks a terminating comment"))
1022 ;; Try to include a trailing newline.
1024 (narrow-to-region start (point))
1026 ;; Use some headers we've invented to drive the process.
1027 (let* ((requires-str (lm-header "package-requires"))
1028 ;; Prefer Package-Version; if defined, the package author
1029 ;; probably wants us to use it. Otherwise try Version.
1031 (or (package-strip-rcs-id (lm-header "package-version"))
1032 (package-strip-rcs-id (lm-header "version"))))
1033 (homepage (lm-homepage)))
1036 "Package lacks a \"Version\" or \"Package-Version\" header"))
1037 (package-desc-from-define
1038 file-name pkg-version desc
1039 (if requires-str (package-read-from-string requires-str))
1041 :homepage homepage))))
1043 (declare-function tar-get-file-descriptor "tar-mode" (file))
1044 (declare-function tar--extract "tar-mode" (descriptor))
1046 (defun package-tar-file-info ()
1047 "Find package information for a tar file.
1048 The return result is a `package-desc'."
1049 (cl-assert (derived-mode-p 'tar-mode))
1050 (let* ((dir-name (file-name-directory
1051 (tar-header-name (car tar-parse-info))))
1052 (desc-file (package--description-file dir-name))
1053 (tar-desc (tar-get-file-descriptor (concat dir-name desc-file))))
1055 (error "No package descriptor file found"))
1056 (with-current-buffer (tar--extract tar-desc)
1057 (goto-char (point-min))
1059 (let* ((pkg-def-parsed (read (current-buffer)))
1061 (if (not (eq (car pkg-def-parsed) 'define-package))
1062 (error "Can't find define-package in %s"
1063 (tar-header-name tar-desc))
1064 (apply #'package-desc-from-define
1065 (append (cdr pkg-def-parsed))))))
1066 (setf (package-desc-kind pkg-desc) 'tar)
1068 (kill-buffer (current-buffer))))))
1072 (defun package-install-from-buffer ()
1073 "Install a package from the current buffer.
1074 The current buffer is assumed to be a single .el or .tar file that follows the
1075 packaging guidelines; see info node `(elisp)Packaging'.
1076 Downloads and installs required packages as needed."
1078 (let ((pkg-desc (if (derived-mode-p 'tar-mode)
1079 (package-tar-file-info)
1080 (package-buffer-info))))
1081 ;; Download and install the dependencies.
1082 (let* ((requires (package-desc-reqs pkg-desc))
1083 (transaction (package-compute-transaction nil requires)))
1084 (package-download-transaction transaction))
1085 ;; Install the package itself.
1086 (package-unpack pkg-desc)
1090 (defun package-install-file (file)
1091 "Install a package from a file.
1092 The file can either be a tar file or an Emacs Lisp file."
1093 (interactive "fPackage file name: ")
1095 (insert-file-contents-literally file)
1096 (when (string-match "\\.tar\\'" file) (tar-mode))
1097 (package-install-from-buffer)))
1099 (defun package-delete (pkg-desc)
1100 (let ((dir (package-desc-dir pkg-desc)))
1101 (if (not (string-prefix-p (file-name-as-directory
1102 (expand-file-name package-user-dir))
1103 (expand-file-name dir)))
1104 ;; Don't delete "system" packages.
1105 (error "Package `%s' is a system package, not deleting"
1106 (package-desc-full-name pkg-desc))
1107 (delete-directory dir t t)
1108 ;; Update package-alist.
1109 (let* ((name (package-desc-name pkg-desc)))
1110 (delete pkg-desc (assq name package-alist)))
1111 (message "Package `%s' deleted." (package-desc-full-name pkg-desc)))))
1113 (defun package-archive-base (desc)
1114 "Return the archive containing the package NAME."
1115 (cdr (assoc (package-desc-archive desc) package-archives)))
1117 (defun package--download-one-archive (archive file)
1118 "Retrieve an archive file FILE from ARCHIVE, and cache it.
1119 ARCHIVE should be a cons cell of the form (NAME . LOCATION),
1120 similar to an entry in `package-alist'. Save the cached copy to
1121 \"archives/NAME/archive-contents\" in `package-user-dir'."
1122 (let* ((dir (expand-file-name (format "archives/%s" (car archive))
1124 (package--with-work-buffer (cdr archive) file
1125 ;; Read the retrieved buffer to make sure it is valid (e.g. it
1126 ;; may fetch a URL redirect page).
1127 (when (listp (read buffer))
1128 (make-directory dir t)
1129 (setq buffer-file-name (expand-file-name file dir))
1130 (let ((version-control 'never))
1134 (defun package-refresh-contents ()
1135 "Download the ELPA archive description if needed.
1136 This informs Emacs about the latest versions of all packages, and
1137 makes them available for download."
1139 ;; FIXME: Do it asynchronously.
1140 (unless (file-exists-p package-user-dir)
1141 (make-directory package-user-dir t))
1142 (dolist (archive package-archives)
1143 (condition-case-unless-debug nil
1144 (package--download-one-archive archive "archive-contents")
1145 (error (message "Failed to download `%s' archive."
1147 (package-read-all-archive-contents))
1150 (defun package-initialize (&optional no-activate)
1151 "Load Emacs Lisp packages, and activate them.
1152 The variable `package-load-list' controls which packages to load.
1153 If optional arg NO-ACTIVATE is non-nil, don't activate packages."
1155 (setq package-alist nil)
1156 (package-load-all-descriptors)
1157 (package-read-all-archive-contents)
1159 (dolist (elt package-alist)
1160 (package-activate (car elt))))
1161 (setq package--initialized t))
1164 ;;;; Package description buffer.
1167 (defun describe-package (package)
1168 "Display the full documentation of PACKAGE (a symbol)."
1170 (let* ((guess (function-called-at-point)))
1171 (require 'finder-inf nil t)
1172 ;; Load the package list if necessary (but don't activate them).
1173 (unless package--initialized
1174 (package-initialize t))
1175 (let ((packages (append (mapcar 'car package-alist)
1176 (mapcar 'car package-archive-contents)
1177 (mapcar 'car package--builtins))))
1178 (unless (memq guess packages)
1180 (setq packages (mapcar 'symbol-name packages))
1182 (completing-read (if guess
1183 (format "Describe package (default %s): "
1185 "Describe package: ")
1186 packages nil t nil nil guess)))
1187 (list (intern val))))))
1188 (if (not (or (package-desc-p package) (and package (symbolp package))))
1189 (message "No package specified")
1190 (help-setup-xref (list #'describe-package package)
1191 (called-interactively-p 'interactive))
1192 (with-help-window (help-buffer)
1193 (with-current-buffer standard-output
1194 (describe-package-1 package)))))
1196 (defun describe-package-1 (pkg)
1199 (if (package-desc-p pkg) pkg)
1200 (cadr (assq pkg package-alist))
1201 (let ((built-in (assq pkg package--builtins)))
1203 (package--from-builtin built-in)
1204 (cadr (assq pkg package-archive-contents))))))
1205 (name (if desc (package-desc-name desc) pkg))
1206 (pkg-dir (if desc (package-desc-dir desc)))
1207 (reqs (if desc (package-desc-reqs desc)))
1208 (version (if desc (package-desc-version desc)))
1209 (archive (if desc (package-desc-archive desc)))
1210 (homepage (if desc (cdr (assoc :homepage
1211 (package-desc-extras desc)))))
1212 (built-in (eq pkg-dir 'builtin))
1213 (installable (and archive (not built-in)))
1214 (status (if desc (package-desc-status desc) "orphan")))
1217 (princ (if (memq (aref status 0) '(?a ?e ?i ?o ?u)) "an " "a "))
1219 (princ " package.\n\n")
1221 (insert " " (propertize "Status" 'font-lock-face 'bold) ": ")
1223 (insert (propertize (capitalize status)
1224 'font-lock-face 'font-lock-builtin-face)
1227 (insert (propertize (capitalize status) ;FIXME: Why comment-face?
1228 'font-lock-face 'font-lock-comment-face))
1230 ;; Todo: Add button for uninstalling.
1231 (help-insert-xref-button (abbreviate-file-name
1232 (file-name-as-directory pkg-dir))
1233 'help-package-def pkg-dir)
1234 (if (and (package-built-in-p name)
1235 (not (package-built-in-p name version)))
1236 (insert "',\n shadowing a "
1237 (propertize "built-in package"
1238 'font-lock-face 'font-lock-builtin-face)
1242 (insert (capitalize status))
1243 (insert " from " (format "%s" archive))
1245 (let ((button-text (if (display-graphic-p) "Install" "[Install]"))
1246 (button-face (if (display-graphic-p)
1247 '(:box (:line-width 2 :color "dark grey")
1248 :background "light grey"
1249 :foreground "black")
1251 (insert-text-button button-text 'face button-face 'follow-link t
1253 'action 'package-install-button-action)))
1254 (t (insert (capitalize status) ".")))
1258 (propertize "Version" 'font-lock-face 'bold) ": "
1259 (package-version-join version) "\n"))
1261 (setq reqs (if desc (package-desc-reqs desc)))
1263 (insert " " (propertize "Requires" 'font-lock-face 'bold) ": ")
1267 (setq name (car req)
1269 text (format "%s-%s" (symbol-name name)
1270 (package-version-join vers)))
1271 (cond (first (setq first nil))
1272 ((>= (+ 2 (current-column) (length text))
1276 (help-insert-xref-button text 'help-package name))
1278 (insert " " (propertize "Summary" 'font-lock-face 'bold)
1279 ": " (if desc (package-desc-summary desc)) "\n")
1281 (insert " " (propertize "Homepage" 'font-lock-face 'bold) ": ")
1282 (help-insert-xref-button homepage 'help-url homepage)
1284 (let* ((all-pkgs (append (cdr (assq name package-alist))
1285 (cdr (assq name package-archive-contents))
1286 (let ((bi (assq name package--builtins)))
1287 (if bi (list (package--from-builtin bi))))))
1288 (other-pkgs (delete desc all-pkgs)))
1290 (insert " " (propertize "Other versions" 'font-lock-face 'bold) ": "
1293 (let* ((ov (package-desc-version opkg))
1294 (dir (package-desc-dir opkg))
1295 (from (or (package-desc-archive opkg)
1296 (if (stringp dir) "installed" dir))))
1297 (if (not ov) (format "%s" from)
1299 (make-text-button (package-version-join ov) nil
1304 (describe-package opkg)))
1312 ;; For built-in packages, insert the commentary.
1313 (let ((fn (locate-file (format "%s.el" name) load-path
1314 load-file-rep-suffixes))
1316 (insert (or (lm-commentary fn) ""))
1319 (when (re-search-forward "^;;; Commentary:\n" nil t)
1321 (while (re-search-forward "^\\(;+ ?\\)" nil t)
1322 (replace-match ""))))
1323 (let ((readme (expand-file-name (format "%s-readme.txt" name)
1326 ;; For elpa packages, try downloading the commentary. If that
1327 ;; fails, try an existing readme file in `package-user-dir'.
1328 (cond ((condition-case nil
1329 (package--with-work-buffer
1330 (package-archive-base desc)
1331 (format "%s-readme.txt" name)
1332 (setq buffer-file-name
1333 (expand-file-name readme package-user-dir))
1334 (let ((version-control 'never))
1336 (setq readme-string (buffer-string))
1339 (insert readme-string))
1340 ((file-readable-p readme)
1341 (insert-file-contents readme)
1342 (goto-char (point-max))))))))
1344 (defun package-install-button-action (button)
1345 (let ((pkg-desc (button-get button 'package-desc)))
1346 (when (y-or-n-p (format "Install package `%s'? "
1347 (package-desc-full-name pkg-desc)))
1348 (package-install pkg-desc)
1349 (revert-buffer nil t)
1350 (goto-char (point-min)))))
1353 ;;;; Package menu mode.
1355 (defvar package-menu-mode-map
1356 (let ((map (make-sparse-keymap))
1357 (menu-map (make-sparse-keymap "Package")))
1358 (set-keymap-parent map tabulated-list-mode-map)
1359 (define-key map "\C-m" 'package-menu-describe-package)
1360 (define-key map "u" 'package-menu-mark-unmark)
1361 (define-key map "\177" 'package-menu-backup-unmark)
1362 (define-key map "d" 'package-menu-mark-delete)
1363 (define-key map "i" 'package-menu-mark-install)
1364 (define-key map "U" 'package-menu-mark-upgrades)
1365 (define-key map "r" 'package-menu-refresh)
1366 (define-key map "~" 'package-menu-mark-obsolete-for-deletion)
1367 (define-key map "x" 'package-menu-execute)
1368 (define-key map "h" 'package-menu-quick-help)
1369 (define-key map "?" 'package-menu-describe-package)
1370 (define-key map [menu-bar package-menu] (cons "Package" menu-map))
1371 (define-key menu-map [mq]
1372 '(menu-item "Quit" quit-window
1373 :help "Quit package selection"))
1374 (define-key menu-map [s1] '("--"))
1375 (define-key menu-map [mn]
1376 '(menu-item "Next" next-line
1378 (define-key menu-map [mp]
1379 '(menu-item "Previous" previous-line
1380 :help "Previous Line"))
1381 (define-key menu-map [s2] '("--"))
1382 (define-key menu-map [mu]
1383 '(menu-item "Unmark" package-menu-mark-unmark
1384 :help "Clear any marks on a package and move to the next line"))
1385 (define-key menu-map [munm]
1386 '(menu-item "Unmark Backwards" package-menu-backup-unmark
1387 :help "Back up one line and clear any marks on that package"))
1388 (define-key menu-map [md]
1389 '(menu-item "Mark for Deletion" package-menu-mark-delete
1390 :help "Mark a package for deletion and move to the next line"))
1391 (define-key menu-map [mi]
1392 '(menu-item "Mark for Install" package-menu-mark-install
1393 :help "Mark a package for installation and move to the next line"))
1394 (define-key menu-map [mupgrades]
1395 '(menu-item "Mark Upgradable Packages" package-menu-mark-upgrades
1396 :help "Mark packages that have a newer version for upgrading"))
1397 (define-key menu-map [s3] '("--"))
1398 (define-key menu-map [mg]
1399 '(menu-item "Update Package List" revert-buffer
1400 :help "Update the list of packages"))
1401 (define-key menu-map [mr]
1402 '(menu-item "Refresh Package List" package-menu-refresh
1403 :help "Download the ELPA archive"))
1404 (define-key menu-map [s4] '("--"))
1405 (define-key menu-map [mt]
1406 '(menu-item "Mark Obsolete Packages" package-menu-mark-obsolete-for-deletion
1407 :help "Mark all obsolete packages for deletion"))
1408 (define-key menu-map [mx]
1409 '(menu-item "Execute Actions" package-menu-execute
1410 :help "Perform all the marked actions"))
1411 (define-key menu-map [s5] '("--"))
1412 (define-key menu-map [mh]
1413 '(menu-item "Help" package-menu-quick-help
1414 :help "Show short key binding help for package-menu-mode"))
1415 (define-key menu-map [mc]
1416 '(menu-item "View Commentary" package-menu-view-commentary
1417 :help "Display information about this package"))
1419 "Local keymap for `package-menu-mode' buffers.")
1421 (defvar package-menu--new-package-list nil
1422 "List of newly-available packages since `list-packages' was last called.")
1424 (define-derived-mode package-menu-mode tabulated-list-mode "Package Menu"
1425 "Major mode for browsing a list of packages.
1426 Letters do not insert themselves; instead, they are commands.
1427 \\<package-menu-mode-map>
1428 \\{package-menu-mode-map}"
1429 (setq tabulated-list-format [("Package" 18 package-menu--name-predicate)
1431 ("Status" 10 package-menu--status-predicate)
1432 ("Description" 0 nil)])
1433 (setq tabulated-list-padding 2)
1434 (setq tabulated-list-sort-key (cons "Status" nil))
1435 (add-hook 'tabulated-list-revert-hook 'package-menu--refresh nil t)
1436 (tabulated-list-init-header))
1438 (defmacro package--push (pkg-desc status listname)
1439 "Convenience macro for `package-menu--generate'.
1440 If the alist stored in the symbol LISTNAME lacks an entry for a
1441 package PKG-DESC, add one. The alist is keyed with PKG-DESC."
1442 `(unless (assoc ,pkg-desc ,listname)
1443 ;; FIXME: Should we move status into pkg-desc?
1444 (push (cons ,pkg-desc ,status) ,listname)))
1446 (defvar package-list-unversioned nil
1447 "If non-nil include packages that don't have a version in `list-package'.")
1449 (defun package-desc-status (pkg-desc)
1450 (let* ((name (package-desc-name pkg-desc))
1451 (dir (package-desc-dir pkg-desc))
1452 (lle (assq name package-load-list))
1454 (version (package-desc-version pkg-desc)))
1456 ((eq dir 'builtin) "built-in")
1457 ((and lle (null held)) "disabled")
1459 (let ((hv (if (stringp held) (version-to-list held))))
1461 ((version-list-= version hv) "held")
1462 ((version-list-< version hv) "obsolete")
1464 ((package-built-in-p name version) "obsolete")
1465 (dir ;One of the installed packages.
1467 ((not (file-exists-p (package-desc-dir pkg-desc))) "deleted")
1468 ((eq pkg-desc (cadr (assq name package-alist))) "installed")
1471 (let* ((ins (cadr (assq name package-alist)))
1472 (ins-v (if ins (package-desc-version ins))))
1474 ((or (null ins) (version-list-< ins-v version))
1475 (if (memq name package-menu--new-package-list)
1477 ((version-list-< version ins-v) "obsolete")
1478 ((version-list-= version ins-v) "installed")))))))
1480 (defun package-menu--refresh (&optional packages)
1481 "Re-populate the `tabulated-list-entries'.
1482 PACKAGES should be nil or t, which means to display all known packages."
1483 ;; Construct list of (PKG-DESC . STATUS).
1484 (unless packages (setq packages t))
1485 (let (info-list name)
1486 ;; Installed packages:
1487 (dolist (elt package-alist)
1488 (setq name (car elt))
1489 (when (or (eq packages t) (memq name packages))
1490 (dolist (pkg (cdr elt))
1491 (package--push pkg (package-desc-status pkg) info-list))))
1493 ;; Built-in packages:
1494 (dolist (elt package--builtins)
1495 (setq name (car elt))
1496 (when (and (not (eq name 'emacs)) ; Hide the `emacs' package.
1497 (or package-list-unversioned
1498 (package--bi-desc-version (cdr elt)))
1499 (or (eq packages t) (memq name packages)))
1500 (package--push (package--from-builtin elt) "built-in" info-list)))
1502 ;; Available and disabled packages:
1503 (dolist (elt package-archive-contents)
1504 (setq name (car elt))
1505 (when (or (eq packages t) (memq name packages))
1506 (dolist (pkg (cdr elt))
1507 ;; Hide obsolete packages.
1508 (unless (package-installed-p (package-desc-name pkg)
1509 (package-desc-version pkg))
1510 (package--push pkg (package-desc-status pkg) info-list)))))
1512 ;; Print the result.
1513 (setq tabulated-list-entries
1514 (mapcar #'package-menu--print-info info-list))))
1516 (defun package-menu--generate (remember-pos packages)
1517 "Populate the Package Menu.
1518 If REMEMBER-POS is non-nil, keep point on the same entry.
1519 PACKAGES should be t, which means to display all known packages,
1520 or a list of package names (symbols) to display."
1521 (package-menu--refresh packages)
1522 (tabulated-list-print remember-pos))
1524 (defun package-menu--print-info (pkg)
1525 "Return a package entry suitable for `tabulated-list-entries'.
1526 PKG has the form (PKG-DESC . STATUS).
1527 Return (PKG-DESC [NAME VERSION STATUS DOC])."
1528 (let* ((pkg-desc (car pkg))
1531 (`"built-in" 'font-lock-builtin-face)
1532 (`"available" 'default)
1534 (`"held" 'font-lock-constant-face)
1535 (`"disabled" 'font-lock-warning-face)
1536 (`"installed" 'font-lock-comment-face)
1537 (_ 'font-lock-warning-face)))) ; obsolete.
1539 (vector (list (symbol-name (package-desc-name pkg-desc))
1542 'package-desc pkg-desc
1543 'action 'package-menu-describe-package)
1544 (propertize (package-version-join
1545 (package-desc-version pkg-desc))
1546 'font-lock-face face)
1547 (propertize status 'font-lock-face face)
1548 (propertize (package-desc-summary pkg-desc)
1549 'font-lock-face face)))))
1551 (defun package-menu-refresh ()
1552 "Download the Emacs Lisp package archive.
1553 This fetches the contents of each archive specified in
1554 `package-archives', and then refreshes the package menu."
1556 (unless (derived-mode-p 'package-menu-mode)
1557 (user-error "The current buffer is not a Package Menu"))
1558 (package-refresh-contents)
1559 (package-menu--generate t t))
1561 (defun package-menu-describe-package (&optional button)
1562 "Describe the current package.
1563 If optional arg BUTTON is non-nil, describe its associated package."
1565 (let ((pkg-desc (if button (button-get button 'package-desc)
1566 (tabulated-list-get-id))))
1568 (describe-package pkg-desc)
1569 (user-error "No package here"))))
1571 ;; fixme numeric argument
1572 (defun package-menu-mark-delete (&optional _num)
1573 "Mark a package for deletion and move to the next line."
1575 (if (member (package-menu-get-status) '("installed" "obsolete"))
1576 (tabulated-list-put-tag "D" t)
1579 (defun package-menu-mark-install (&optional _num)
1580 "Mark a package for installation and move to the next line."
1582 (if (member (package-menu-get-status) '("available" "new"))
1583 (tabulated-list-put-tag "I" t)
1586 (defun package-menu-mark-unmark (&optional _num)
1587 "Clear any marks on a package and move to the next line."
1589 (tabulated-list-put-tag " " t))
1591 (defun package-menu-backup-unmark ()
1592 "Back up one line and clear any marks on that package."
1595 (tabulated-list-put-tag " "))
1597 (defun package-menu-mark-obsolete-for-deletion ()
1598 "Mark all obsolete packages for deletion."
1601 (goto-char (point-min))
1603 (if (equal (package-menu-get-status) "obsolete")
1604 (tabulated-list-put-tag "D" t)
1605 (forward-line 1)))))
1607 (defun package-menu-quick-help ()
1608 "Show short key binding help for package-menu-mode."
1610 (message "n-ext, i-nstall, d-elete, u-nmark, x-ecute, r-efresh, h-elp"))
1612 (define-obsolete-function-alias
1613 'package-menu-view-commentary 'package-menu-describe-package "24.1")
1615 (defun package-menu-get-status ()
1616 (let* ((id (tabulated-list-get-id))
1617 (entry (and id (assq id tabulated-list-entries))))
1619 (aref (cadr entry) 2)
1622 (defun package-menu--find-upgrades ()
1623 (let (installed available upgrades)
1624 ;; Build list of installed/available packages in this buffer.
1625 (dolist (entry tabulated-list-entries)
1626 ;; ENTRY is (PKG-DESC [NAME VERSION STATUS DOC])
1627 (let ((pkg-desc (car entry))
1628 (status (aref (cadr entry) 2)))
1629 (cond ((equal status "installed")
1630 (push pkg-desc installed))
1631 ((member status '("available" "new"))
1632 (push (cons (package-desc-name pkg-desc) pkg-desc)
1634 ;; Loop through list of installed packages, finding upgrades.
1635 (dolist (pkg-desc installed)
1636 (let ((avail-pkg (assq (package-desc-name pkg-desc) available)))
1638 (version-list-< (package-desc-version pkg-desc)
1639 (package-desc-version (cdr avail-pkg)))
1640 (push avail-pkg upgrades))))
1643 (defun package-menu-mark-upgrades ()
1644 "Mark all upgradable packages in the Package Menu.
1645 For each installed package with a newer version available, place
1646 an (I)nstall flag on the available version and a (D)elete flag on
1647 the installed version. A subsequent \\[package-menu-execute]
1648 call will upgrade the package."
1650 (unless (derived-mode-p 'package-menu-mode)
1651 (error "The current buffer is not a Package Menu"))
1652 (let ((upgrades (package-menu--find-upgrades)))
1654 (message "No packages to upgrade.")
1657 (goto-char (point-min))
1659 (let* ((pkg-desc (tabulated-list-get-id))
1660 (upgrade (cdr (assq (package-desc-name pkg-desc) upgrades))))
1661 (cond ((null upgrade)
1663 ((equal pkg-desc upgrade)
1664 (package-menu-mark-install))
1666 (package-menu-mark-delete))))))
1667 (message "%d package%s marked for upgrading."
1669 (if (= (length upgrades) 1) "" "s")))))
1671 (defun package-menu-execute (&optional noquery)
1672 "Perform marked Package Menu actions.
1673 Packages marked for installation are downloaded and installed;
1674 packages marked for deletion are removed.
1675 Optional argument NOQUERY non-nil means do not ask the user to confirm."
1677 (unless (derived-mode-p 'package-menu-mode)
1678 (error "The current buffer is not in Package Menu mode"))
1679 (let (install-list delete-list cmd pkg-desc)
1681 (goto-char (point-min))
1683 (setq cmd (char-after))
1684 (unless (eq cmd ?\s)
1685 ;; This is the key PKG-DESC.
1686 (setq pkg-desc (tabulated-list-get-id))
1688 (push pkg-desc delete-list))
1690 (push pkg-desc install-list))))
1696 (if (= (length install-list) 1)
1697 (format "Install package `%s'? "
1698 (package-desc-full-name (car install-list)))
1699 (format "Install these %d packages (%s)? "
1700 (length install-list)
1701 (mapconcat #'package-desc-full-name
1702 install-list ", ")))))
1703 (mapc 'package-install install-list)))
1704 ;; Delete packages, prompting if necessary.
1709 (if (= (length delete-list) 1)
1710 (format "Delete package `%s'? "
1711 (package-desc-full-name (car delete-list)))
1712 (format "Delete these %d packages (%s)? "
1713 (length delete-list)
1714 (mapconcat #'package-desc-full-name
1715 delete-list ", ")))))
1716 (dolist (elt delete-list)
1717 (condition-case-unless-debug err
1718 (package-delete elt)
1719 (error (message (cadr err)))))
1721 (if (or delete-list install-list)
1722 (package-menu--generate t t)
1723 (message "No operations specified."))))
1725 (defun package-menu--version-predicate (A B)
1726 (let ((vA (or (aref (cadr A) 1) '(0)))
1727 (vB (or (aref (cadr B) 1) '(0))))
1728 (if (version-list-= vA vB)
1729 (package-menu--name-predicate A B)
1730 (version-list-< vA vB))))
1732 (defun package-menu--status-predicate (A B)
1733 (let ((sA (aref (cadr A) 2))
1734 (sB (aref (cadr B) 2)))
1735 (cond ((string= sA sB)
1736 (package-menu--name-predicate A B))
1737 ((string= sA "new") t)
1738 ((string= sB "new") nil)
1739 ((string= sA "available") t)
1740 ((string= sB "available") nil)
1741 ((string= sA "installed") t)
1742 ((string= sB "installed") nil)
1743 ((string= sA "held") t)
1744 ((string= sB "held") nil)
1745 ((string= sA "built-in") t)
1746 ((string= sB "built-in") nil)
1747 ((string= sA "obsolete") t)
1748 ((string= sB "obsolete") nil)
1749 (t (string< sA sB)))))
1751 (defun package-menu--description-predicate (A B)
1752 (let ((dA (aref (cadr A) 3))
1753 (dB (aref (cadr B) 3)))
1755 (package-menu--name-predicate A B)
1758 (defun package-menu--name-predicate (A B)
1759 (string< (symbol-name (package-desc-name (car A)))
1760 (symbol-name (package-desc-name (car B)))))
1763 (defun list-packages (&optional no-fetch)
1764 "Display a list of packages.
1765 This first fetches the updated list of packages before
1766 displaying, unless a prefix argument NO-FETCH is specified.
1767 The list is displayed in a buffer named `*Packages*'."
1769 (require 'finder-inf nil t)
1770 ;; Initialize the package system if necessary.
1771 (unless package--initialized
1772 (package-initialize t))
1773 (let (old-archives new-packages)
1775 ;; Read the locally-cached archive-contents.
1776 (package-read-all-archive-contents)
1777 (setq old-archives package-archive-contents)
1778 ;; Fetch the remote list of packages.
1779 (package-refresh-contents)
1780 ;; Find which packages are new.
1781 (dolist (elt package-archive-contents)
1782 (unless (assq (car elt) old-archives)
1783 (push (car elt) new-packages))))
1785 ;; Generate the Package Menu.
1786 (let ((buf (get-buffer-create "*Packages*")))
1787 (with-current-buffer buf
1789 (set (make-local-variable 'package-menu--new-package-list)
1791 (package-menu--generate nil t))
1792 ;; The package menu buffer has keybindings. If the user types
1793 ;; `M-x list-packages', that suggests it should become current.
1794 (switch-to-buffer buf))
1796 (let ((upgrades (package-menu--find-upgrades)))
1798 (message "%d package%s can be upgraded; type `%s' to mark %s for upgrading."
1800 (if (= (length upgrades) 1) "" "s")
1801 (substitute-command-keys "\\[package-menu-mark-upgrades]")
1802 (if (= (length upgrades) 1) "it" "them"))))))
1805 (defalias 'package-list-packages 'list-packages)
1807 ;; Used in finder.el
1808 (defun package-show-package-list (packages)
1809 "Display PACKAGES in a *Packages* buffer.
1810 This is similar to `list-packages', but it does not fetch the
1811 updated list of packages, and it only displays packages with
1812 names in PACKAGES (which should be a list of symbols)."
1813 (require 'finder-inf nil t)
1814 (let ((buf (get-buffer-create "*Packages*")))
1815 (with-current-buffer buf
1817 (package-menu--generate nil packages))
1818 (switch-to-buffer buf)))
1820 (defun package-list-packages-no-fetch ()
1821 "Display a list of packages.
1822 Does not fetch the updated list of packages before displaying.
1823 The list is displayed in a buffer named `*Packages*'."
1829 ;;; package.el ends here