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
342 (summary package--default-summary)
347 ;; Package descriptor format used in finder-inf.el and package--builtins.
348 (cl-defstruct (package--bi-desc
349 (:constructor package-make-builtin (version summary))
355 ;; The value is precomputed in finder-inf.el, but don't load that
356 ;; until it's needed (i.e. when `package-initialize' is called).
357 (defvar package--builtins nil
358 "Alist of built-in packages.
359 The actual value is initialized by loading the library
360 `finder-inf'; this is not done until it is needed, e.g. by the
361 function `package-built-in-p'.
363 Each element has the form (PKG . PACKAGE-BI-DESC), where PKG is a package
364 name (a symbol) and DESC is a `package--bi-desc' structure.")
365 (put 'package--builtins 'risky-local-variable t)
367 (defvar package-alist nil
368 "Alist of all packages available for activation.
369 Each element has the form (PKG . DESC), where PKG is a package
370 name (a symbol) and DESC is a `package-desc' structure.
372 This variable is set automatically by `package-load-descriptor',
373 called via `package-initialize'. To change which packages are
374 loaded and/or activated, customize `package-load-list'.")
375 (put 'package-alist 'risky-local-variable t)
377 (defvar package-activated-list nil
378 "List of the names of currently activated packages.")
379 (put 'package-activated-list 'risky-local-variable t)
381 (defvar package-obsolete-alist nil
382 "Representation of obsolete packages.
383 Like `package-alist', but maps package name to a second alist.
384 The inner alist is keyed by version.
386 Each element of the list is (NAME . VERSION-ALIST), where each
387 entry in VERSION-ALIST is (VERSION-LIST . PACKAGE-DESC).")
388 (put 'package-obsolete-alist 'risky-local-variable t)
390 (defun package-version-join (vlist)
391 "Return the version string corresponding to the list VLIST.
392 This is, approximately, the inverse of `version-to-list'.
393 \(Actually, it returns only one of the possible inverses, since
394 `version-to-list' is a many-to-one operation.)"
397 (let ((str-list (list "." (int-to-string (car vlist)))))
398 (dolist (num (cdr vlist))
401 (push (int-to-string num) str-list)
404 (error "Invalid version list `%s'" vlist))
406 ;; pre, or beta, or alpha
407 (cond ((equal "." (car str-list))
409 ((not (string-match "[0-9]+" (car str-list)))
410 (error "Invalid version list `%s'" vlist)))
411 (push (cond ((= num -1) "pre")
413 ((= num -3) "alpha"))
415 (if (equal "." (car str-list))
417 (apply 'concat (nreverse str-list)))))
419 (defun package-strip-version (dirname)
420 "Strip the version from a combined package name and version.
421 E.g., if given \"quux-23.0\", will return \"quux\""
422 (if (string-match (concat "\\`" package-subdirectory-regexp "\\'") dirname)
423 (match-string 1 dirname)))
425 (defun package-load-descriptor (dir package)
426 "Load the description file in directory DIR for package PACKAGE.
427 Here, PACKAGE is a string of the form NAME-VERSION, where NAME is
428 the package name and VERSION is its version."
429 (let* ((pkg-dir (expand-file-name package dir))
430 (pkg-file (expand-file-name
431 (concat (package-strip-version package) "-pkg")
433 (when (and (file-directory-p pkg-dir)
434 (file-exists-p (concat pkg-file ".el")))
435 (load pkg-file nil t))))
437 (defun package-load-all-descriptors ()
438 "Load descriptors for installed Emacs Lisp packages.
439 This looks for package subdirectories in `package-user-dir' and
440 `package-directory-list'. The variable `package-load-list'
441 controls which package subdirectories may be loaded.
443 In each valid package subdirectory, this function loads the
444 description file containing a call to `define-package', which
445 updates `package-alist' and `package-obsolete-alist'."
446 (let ((regexp (concat "\\`" package-subdirectory-regexp "\\'")))
447 (dolist (dir (cons package-user-dir package-directory-list))
448 (when (file-directory-p dir)
449 (dolist (subdir (directory-files dir))
450 (when (string-match regexp subdir)
451 (package-maybe-load-descriptor (match-string 1 subdir)
452 (match-string 2 subdir)
455 (defun package-maybe-load-descriptor (name version dir)
456 "Maybe load a specific package from directory DIR.
457 NAME and VERSION are the package's name and version strings.
458 This function checks `package-load-list', before actually loading
459 the package by calling `package-load-descriptor'."
460 (let ((force (assq (intern name) package-load-list))
461 (subdir (concat name "-" version)))
462 (and (file-directory-p (expand-file-name subdir dir))
463 ;; Check `package-load-list':
465 (memq 'all package-load-list))
466 ((null (setq force (cadr force)))
470 ((stringp force) ; held
471 (version-list-= (version-to-list version)
472 (version-to-list force)))
474 (error "Invalid element in `package-load-list'")))
475 ;; Actually load the descriptor:
476 (package-load-descriptor dir subdir))))
478 (define-obsolete-function-alias 'package-desc-vers 'package-desc-version "24.4")
480 (define-obsolete-function-alias 'package-desc-doc 'package-desc-summary "24.4")
483 (defun package--dir (name version)
484 ;; FIXME: Keep this as a field in the package-desc.
485 "Return the directory where a package is installed, or nil if none.
486 NAME is a symbol and VERSION is a string."
487 (let* ((subdir (format "%s-%s" name version))
488 (dir-list (cons package-user-dir package-directory-list))
491 (let ((subdir-full (expand-file-name subdir (car dir-list))))
492 (if (file-directory-p subdir-full)
493 (setq pkg-dir subdir-full
495 (setq dir-list (cdr dir-list)))))
498 (defun package-activate-1 (pkg-desc)
499 (let* ((name (package-desc-name pkg-desc))
500 (version-str (package-version-join (package-desc-version pkg-desc)))
501 (pkg-dir (package--dir name version-str)))
503 (error "Internal error: unable to find directory for `%s-%s'"
506 (when (file-exists-p (expand-file-name "dir" pkg-dir))
507 ;; FIXME: not the friendliest, but simple.
510 (push pkg-dir Info-directory-list))
511 ;; Add to load path, add autoloads, and activate the package.
512 (push pkg-dir load-path)
513 (load (expand-file-name (format "%s-autoloads" name) pkg-dir) nil t)
514 (push name package-activated-list)
518 (defun package-built-in-p (package &optional min-version)
519 "Return true if PACKAGE is built-in to Emacs.
520 Optional arg MIN-VERSION, if non-nil, should be a version list
521 specifying the minimum acceptable version."
522 (require 'finder-inf nil t) ; For `package--builtins'.
523 (if (eq package 'emacs)
524 (version-list-<= min-version (version-to-list emacs-version))
525 (let ((elt (assq package package--builtins)))
526 (and elt (version-list-<= min-version
527 (package--bi-desc-version (cdr elt)))))))
529 (defun package--from-builtin (bi-desc)
530 (package-desc-create :name (pop bi-desc)
531 :version (package--bi-desc-version bi-desc)
532 :summary (package--bi-desc-summary bi-desc)))
534 ;; This function goes ahead and activates a newer version of a package
535 ;; if an older one was already activated. This is not ideal; we'd at
536 ;; least need to check to see if the package has actually been loaded,
537 ;; and not merely activated.
538 (defun package-activate (package min-version)
539 "Activate package PACKAGE, of version MIN-VERSION or newer.
540 MIN-VERSION should be a version list.
541 If PACKAGE has any dependencies, recursively activate them.
542 Return nil if the package could not be activated."
543 (let ((pkg-vec (cdr (assq package package-alist)))
544 available-version found)
545 ;; Check if PACKAGE is available in `package-alist'.
547 (setq available-version (package-desc-version pkg-vec)
548 found (version-list-<= min-version available-version)))
550 ;; If no such package is found, maybe it's built-in.
552 (package-built-in-p package min-version))
553 ;; If the package is already activated, just return t.
554 ((memq package package-activated-list)
556 ;; Otherwise, proceed with activation.
558 (let ((fail (catch 'dep-failure
559 ;; Activate its dependencies recursively.
560 (dolist (req (package-desc-reqs pkg-vec))
561 (unless (package-activate (car req) (cadr req))
562 (throw 'dep-failure req))))))
564 (warn "Unable to activate package `%s'.
565 Required package `%s-%s' is unavailable"
566 package (car fail) (package-version-join (cadr fail)))
567 ;; If all goes well, activate the package itself.
568 (package-activate-1 pkg-vec)))))))
570 (defun package-mark-obsolete (package pkg-vec)
571 "Put package on the obsolete list, if not already there."
572 (let ((elt (assq package package-obsolete-alist)))
574 ;; If this obsolete version does not exist in the list, update
576 (unless (assoc (package-desc-version pkg-vec) (cdr elt))
577 (setcdr elt (cons (cons (package-desc-version pkg-vec) pkg-vec)
579 ;; Make a new association.
580 (push (cons package (list (cons (package-desc-version pkg-vec)
582 package-obsolete-alist))))
584 (defun define-package (name-string version-string
585 &optional docstring requirements
586 &rest _extra-properties)
587 "Define a new package.
588 NAME-STRING is the name of the package, as a string.
589 VERSION-STRING is the version of the package, as a string.
590 DOCSTRING is a short description of the package, a string.
591 REQUIREMENTS is a list of dependencies on other packages.
592 Each requirement is of the form (OTHER-PACKAGE OTHER-VERSION),
593 where OTHER-VERSION is a string.
595 EXTRA-PROPERTIES is currently unused."
596 (let* ((name (intern name-string))
597 (version (version-to-list version-string))
598 (new-pkg-desc (cons name
599 (package-desc-from-define name-string
603 (old-pkg (assq name package-alist)))
605 ;; If there's no old package, just add this to `package-alist'.
607 (push new-pkg-desc package-alist))
608 ((version-list-< (package-desc-version (cdr old-pkg)) version)
609 ;; Remove the old package and declare it obsolete.
610 (package-mark-obsolete name (cdr old-pkg))
611 (setq package-alist (cons new-pkg-desc
612 (delq old-pkg package-alist))))
613 ;; You can have two packages with the same version, e.g. one in
614 ;; the system package directory and one in your private
615 ;; directory. We just let the first one win.
616 ((not (version-list-= (package-desc-version (cdr old-pkg)) version))
617 ;; The package is born obsolete.
618 (package-mark-obsolete name (cdr new-pkg-desc))))))
621 (defun package-autoload-ensure-default-file (file)
622 "Make sure that the autoload file FILE exists and if not create it."
623 (unless (file-exists-p file)
625 (concat ";;; " (file-name-nondirectory file)
626 " --- automatically extracted autoloads\n"
629 "(add-to-list 'load-path (or (file-name-directory #$) (car load-path)))\n"
630 "
\f\n;; Local Variables:\n"
631 ";; version-control: never\n"
632 ";; no-byte-compile: t\n"
633 ";; no-update-autoloads: t\n"
635 ";;; " (file-name-nondirectory file)
640 (defun package-generate-autoloads (name pkg-dir)
641 (require 'autoload) ;Load before we let-bind generated-autoload-file!
642 (let* ((auto-name (format "%s-autoloads.el" name))
643 ;;(ignore-name (concat name "-pkg.el"))
644 (generated-autoload-file (expand-file-name auto-name pkg-dir))
645 (version-control 'never))
646 (package-autoload-ensure-default-file generated-autoload-file)
647 (update-directory-autoloads pkg-dir)
648 (let ((buf (find-buffer-visiting generated-autoload-file)))
649 (when buf (kill-buffer buf)))
652 (defvar tar-parse-info)
653 (declare-function tar-untar-buffer "tar-mode" ())
654 (declare-function tar-header-name "tar-mode" (tar-header) t)
655 (declare-function tar-header-link-type "tar-mode" (tar-header) t)
657 (defun package-untar-buffer (dir)
658 "Untar the current buffer.
659 This uses `tar-untar-buffer' from Tar mode. All files should
660 untar into a directory named DIR; otherwise, signal an error."
663 ;; Make sure everything extracts into DIR.
664 (let ((regexp (concat "\\`" (regexp-quote (expand-file-name dir)) "/"))
665 (case-fold-search (memq system-type '(windows-nt ms-dos cygwin))))
666 (dolist (tar-data tar-parse-info)
667 (let ((name (expand-file-name (tar-header-name tar-data))))
668 (or (string-match regexp name)
669 ;; Tarballs created by some utilities don't list
670 ;; directories with a trailing slash (Bug#13136).
671 (and (string-equal dir name)
672 (eq (tar-header-link-type tar-data) 5))
673 (error "Package does not untar cleanly into directory %s/" dir)))))
676 (defun package-unpack (package version)
677 (let* ((name (symbol-name package))
678 (dirname (concat name "-" version))
679 (pkg-dir (expand-file-name dirname package-user-dir)))
680 (make-directory package-user-dir t)
681 ;; FIXME: should we delete PKG-DIR if it exists?
682 (let* ((default-directory (file-name-as-directory package-user-dir)))
683 (package-untar-buffer dirname)
684 (package--make-autoloads-and-compile package pkg-dir))))
686 (defun package--make-autoloads-and-compile (name pkg-dir)
687 "Generate autoloads and do byte-compilation for package named NAME.
688 PKG-DIR is the name of the package directory."
689 (let ((auto-name (package-generate-autoloads name pkg-dir))
690 (load-path (cons pkg-dir load-path)))
691 ;; We must load the autoloads file before byte compiling, in
692 ;; case there are magic cookies to set up non-trivial paths.
693 (load auto-name nil t)
694 ;; FIXME: Compilation should be done as a separate, optional, step.
695 ;; E.g. for multi-package installs, we should first install all packages
696 ;; and then compile them.
697 (byte-recompile-directory pkg-dir 0 t)))
699 (defun package--write-file-no-coding (file-name)
700 (let ((buffer-file-coding-system 'no-conversion))
701 (write-region (point-min) (point-max) file-name)))
703 (defun package-unpack-single (name version desc requires)
704 "Install the contents of the current buffer as a package."
705 ;; Special case "package". FIXME: Should this still be supported?
706 (if (eq name 'package)
707 (package--write-file-no-coding
708 (expand-file-name (format "%s.el" name) package-user-dir))
709 (let* ((pkg-dir (expand-file-name (format "%s-%s" name
710 (package-version-join
711 (version-to-list version)))
713 (el-file (expand-file-name (format "%s.el" name) pkg-dir))
714 (pkg-file (expand-file-name (format "%s-pkg.el" name) pkg-dir)))
715 (make-directory pkg-dir t)
716 (package--write-file-no-coding el-file)
717 (let ((print-level nil)
723 (list 'define-package
727 (when requires ;Don't bother quoting nil.
729 ;; Turn version lists into string form.
733 (package-version-join (cadr elt))))
739 (package--make-autoloads-and-compile name pkg-dir))))
741 (defmacro package--with-work-buffer (location file &rest body)
742 "Run BODY in a buffer containing the contents of FILE at LOCATION.
743 LOCATION is the base location of a package archive, and should be
744 one of the URLs (or file names) specified in `package-archives'.
745 FILE is the name of a file relative to that base location.
747 This macro retrieves FILE from LOCATION into a temporary buffer,
748 and evaluates BODY while that buffer is current. This work
749 buffer is killed afterwards. Return the last value in BODY."
750 `(let* ((http (string-match "\\`https?:" ,location))
753 (url-retrieve-synchronously (concat ,location ,file))
754 (generate-new-buffer "*package work buffer*"))))
756 (with-current-buffer buffer
758 (progn (package-handle-response)
759 (re-search-forward "^$" nil 'move)
761 (delete-region (point-min) (point)))
762 (unless (file-name-absolute-p ,location)
763 (error "Archive location %s is not an absolute file name"
765 (insert-file-contents (expand-file-name ,file ,location)))
767 (kill-buffer buffer))))
769 (defun package-handle-response ()
770 "Handle the response from a `url-retrieve-synchronously' call.
771 Parse the HTTP response and throw if an error occurred.
772 The url package seems to require extra processing for this.
773 This should be called in a `save-excursion', in the download buffer.
774 It will move point to somewhere in the headers."
775 ;; We assume HTTP here.
777 (let ((response (url-http-parse-response)))
778 (when (or (< response 200) (>= response 300))
779 (error "Error during download request:%s"
780 (buffer-substring-no-properties (point) (progn
784 (defun package-download-single (name version desc requires)
785 "Download and install a single-file package."
786 (let ((location (package-archive-base name))
787 (file (concat (symbol-name name) "-" version ".el")))
788 (package--with-work-buffer location file
789 (package-unpack-single name version desc requires))))
791 (defun package-download-tar (name version)
792 "Download and install a tar package."
793 (let ((location (package-archive-base name))
794 (file (concat (symbol-name name) "-" version ".tar")))
795 (package--with-work-buffer location file
796 (package-unpack name version))))
798 (defvar package--initialized nil)
800 (defun package-installed-p (package &optional min-version)
801 "Return true if PACKAGE, of MIN-VERSION or newer, is installed.
802 MIN-VERSION should be a version list."
803 (unless package--initialized (error "package.el is not yet initialized!"))
804 (let ((pkg-desc (assq package package-alist)))
806 (version-list-<= min-version
807 (package-desc-version (cdr pkg-desc)))
808 ;; Also check built-in packages.
809 (package-built-in-p package min-version))))
811 (defun package-compute-transaction (package-list requirements)
812 "Return a list of packages to be installed, including PACKAGE-LIST.
813 PACKAGE-LIST should be a list of package names (symbols).
815 REQUIREMENTS should be a list of additional requirements; each
816 element in this list should have the form (PACKAGE VERSION-LIST),
817 where PACKAGE is a package name and VERSION-LIST is the required
818 version of that package.
820 This function recursively computes the requirements of the
821 packages in REQUIREMENTS, and returns a list of all the packages
822 that must be installed. Packages that are already installed are
823 not included in this list."
824 (dolist (elt requirements)
825 (let* ((next-pkg (car elt))
826 (next-version (cadr elt)))
827 (unless (package-installed-p next-pkg next-version)
828 ;; A package is required, but not installed. It might also be
829 ;; blocked via `package-load-list'.
830 (let ((pkg-desc (cdr (assq next-pkg package-archive-contents)))
832 (when (setq hold (assq next-pkg package-load-list))
833 (setq hold (cadr hold))
836 (error "Required package '%s' is disabled"
837 (symbol-name next-pkg)))
838 ((null (stringp hold))
839 (error "Invalid element in `package-load-list'"))
840 ((version-list-< (version-to-list hold) next-version)
841 (error "Package `%s' held at version %s, \
842 but version %s required"
843 (symbol-name next-pkg) hold
844 (package-version-join next-version)))))
846 (error "Package `%s-%s' is unavailable"
847 (symbol-name next-pkg)
848 (package-version-join next-version)))
849 (unless (version-list-<= next-version
850 (package-desc-version pkg-desc))
852 "Need package `%s-%s', but only %s is available"
853 (symbol-name next-pkg) (package-version-join next-version)
854 (package-version-join (package-desc-version pkg-desc))))
855 ;; Move to front, so it gets installed early enough (bug#14082).
856 (setq package-list (cons next-pkg (delq next-pkg package-list)))
858 (package-compute-transaction package-list
863 (defun package-read-from-string (str)
864 "Read a Lisp expression from STR.
865 Signal an error if the entire string was not used."
866 (let* ((read-data (read-from-string str))
869 ;; The call to `ignore' suppresses a compiler warning.
870 (progn (ignore (read-from-string
871 (substring str (cdr read-data))))
875 (error "Can't read whole string")
878 (defun package--read-archive-file (file)
879 "Re-read archive file FILE, if it exists.
880 Will return the data from the file, or nil if the file does not exist.
881 Will throw an error if the archive version is too new."
882 (let ((filename (expand-file-name file package-user-dir)))
883 (when (file-exists-p filename)
885 (insert-file-contents-literally filename)
886 (let ((contents (read (current-buffer))))
887 (if (> (car contents) package-archive-version)
888 (error "Package archive version %d is higher than %d"
889 (car contents) package-archive-version))
892 (defun package-read-all-archive-contents ()
893 "Re-read `archive-contents', if it exists.
894 If successful, set `package-archive-contents'."
895 (setq package-archive-contents nil)
896 (dolist (archive package-archives)
897 (package-read-archive-contents (car archive))))
899 (defun package-read-archive-contents (archive)
900 "Re-read archive contents for ARCHIVE.
901 If successful, set the variable `package-archive-contents'.
902 If the archive version is too new, signal an error."
903 ;; Version 1 of 'archive-contents' is identical to our internal
905 (let* ((dir (concat "archives/" archive))
906 (contents-file (concat dir "/archive-contents"))
908 (when (setq contents (package--read-archive-file contents-file))
909 (dolist (package contents)
910 (package--add-to-archive-contents package archive)))))
912 ;; Package descriptor objects used inside the "archive-contents" file.
913 ;; Changing this defstruct implies changing the format of the
914 ;; "archive-contents" files.
915 (cl-defstruct (package--ac-desc
916 (:constructor package-make-ac-desc (version reqs summary kind))
919 version reqs summary kind)
921 (defun package--add-to-archive-contents (package archive)
922 "Add the PACKAGE from the given ARCHIVE if necessary.
923 PACKAGE should have the form (NAME . PACKAGE--AC-DESC).
924 Also, add the originating archive to the `package-desc' structure."
925 (let* ((name (car package))
929 :version (package--ac-desc-version (cdr package))
930 :reqs (package--ac-desc-reqs (cdr package))
931 :summary (package--ac-desc-summary (cdr package))
932 :kind (package--ac-desc-kind (cdr package))
934 (entry (cons name pkg-desc))
935 (existing-package (assq name package-archive-contents))
936 (pinned-to-archive (assoc name package-pinned-packages)))
937 (cond ((and pinned-to-archive
938 ;; If pinned to another archive, skip entirely.
939 (not (equal (cdr pinned-to-archive) archive)))
941 ((not existing-package)
942 (push entry package-archive-contents))
943 ((version-list-< (package-desc-version (cdr existing-package))
944 (package-desc-version pkg-desc))
945 ;; Replace the entry with this one.
946 (setq package-archive-contents
948 (delq existing-package
949 package-archive-contents)))))))
951 (defun package-download-transaction (package-list)
952 "Download and install all the packages in PACKAGE-LIST.
953 PACKAGE-LIST should be a list of package names (symbols).
954 This function assumes that all package requirements in
955 PACKAGE-LIST are satisfied, i.e. that PACKAGE-LIST is computed
956 using `package-compute-transaction'."
957 (dolist (elt package-list)
958 (let* ((desc (cdr (assq elt package-archive-contents)))
959 ;; As an exception, if package is "held" in
960 ;; `package-load-list', download the held version.
961 (hold (cadr (assq elt package-load-list)))
962 (v-string (or (and (stringp hold) hold)
963 (package-version-join (package-desc-version desc))))
964 (kind (package-desc-kind desc)))
967 (package-download-tar elt v-string))
969 (package-download-single elt v-string
970 (package-desc-summary desc)
971 (package-desc-reqs desc)))
973 (error "Unknown package kind: %s" (symbol-name kind))))
974 ;; If package A depends on package B, then A may `require' B
975 ;; during byte compilation. So we need to activate B before
977 (package-maybe-load-descriptor (symbol-name elt) v-string
979 (package-activate elt (version-to-list v-string)))))
982 (defun package-install (name)
983 "Install the package named NAME.
984 NAME should be the name of one of the available packages in an
985 archive in `package-archives'. Interactively, prompt for NAME."
988 ;; Initialize the package system to get the list of package
989 ;; symbols for completion.
990 (unless package--initialized
991 (package-initialize t))
992 (unless package-archive-contents
993 (package-refresh-contents))
994 (list (intern (completing-read
996 (mapcar (lambda (elt)
997 (cons (symbol-name (car elt))
999 package-archive-contents)
1001 (let ((pkg-desc (assq name package-archive-contents)))
1003 (error "Package `%s' is not available for installation"
1004 (symbol-name name)))
1005 (package-download-transaction
1006 (package-compute-transaction (list name)
1007 (package-desc-reqs (cdr pkg-desc))))))
1009 (defun package-strip-rcs-id (str)
1010 "Strip RCS version ID from the version string STR.
1011 If the result looks like a dotted numeric version, return it.
1012 Otherwise return nil."
1014 (when (string-match "\\`[ \t]*[$]Revision:[ \t]+" str)
1015 (setq str (substring str (match-end 0))))
1017 (if (version-to-list str)
1021 (defun package-buffer-info ()
1022 "Return a `package-desc' describing the package in the current buffer.
1024 If the buffer does not contain a conforming package, signal an
1025 error. If there is a package, narrow the buffer to the file's
1027 (goto-char (point-min))
1028 (unless (re-search-forward "^;;; \\([^ ]*\\)\\.el ---[ \t]*\\(.*?\\)[ \t]*\\(-\\*-.*-\\*-[ \t]*\\)?$" nil t)
1029 (error "Packages lacks a file header"))
1030 (let ((file-name (match-string-no-properties 1))
1031 (desc (match-string-no-properties 2))
1032 (start (line-beginning-position)))
1033 (unless (search-forward (concat ";;; " file-name ".el ends here"))
1034 (error "Package lacks a terminating comment"))
1035 ;; Try to include a trailing newline.
1037 (narrow-to-region start (point))
1039 ;; Use some headers we've invented to drive the process.
1040 (let* ((requires-str (lm-header "package-requires"))
1041 ;; Prefer Package-Version; if defined, the package author
1042 ;; probably wants us to use it. Otherwise try Version.
1044 (or (package-strip-rcs-id (lm-header "package-version"))
1045 (package-strip-rcs-id (lm-header "version")))))
1048 "Package lacks a \"Version\" or \"Package-Version\" header"))
1049 (package-desc-from-define
1050 file-name pkg-version desc
1051 (if requires-str (package-read-from-string requires-str))
1054 (defun package-tar-file-info (file)
1055 "Find package information for a tar file.
1056 FILE is the name of the tar file to examine.
1057 The return result is a vector like `package-buffer-info'."
1058 (let ((default-directory (file-name-directory file))
1059 (file (file-name-nondirectory file)))
1060 (unless (string-match (concat "\\`" package-subdirectory-regexp "\\.tar\\'")
1062 (error "Invalid package name `%s'" file))
1063 (let* ((pkg-name (match-string-no-properties 1 file))
1064 (pkg-version (match-string-no-properties 2 file))
1065 ;; Extract the package descriptor.
1066 (pkg-def-contents (shell-command-to-string
1067 ;; Requires GNU tar.
1068 (concat "tar -xOf " file " "
1069 pkg-name "-" pkg-version "/"
1070 pkg-name "-pkg.el")))
1071 (pkg-def-parsed (package-read-from-string pkg-def-contents)))
1072 (unless (eq (car pkg-def-parsed) 'define-package)
1073 (error "No `define-package' sexp is present in `%s-pkg.el'" pkg-name))
1075 (apply #'package-desc-from-define (append (cdr pkg-def-parsed)
1077 (unless (equal pkg-version
1078 (package-version-join (package-desc-version pkg-desc)))
1079 (error "Package has inconsistent versions"))
1080 (unless (equal pkg-name (symbol-name (package-desc-name pkg-desc)))
1081 (error "Package has inconsistent names"))
1086 (defun package-install-from-buffer (pkg-desc)
1087 "Install a package from the current buffer.
1088 When called interactively, the current buffer is assumed to be a
1089 single .el file that follows the packaging guidelines; see info
1090 node `(elisp)Packaging'.
1092 When called from Lisp, PKG-DESC is a `package-desc' describing the
1094 (interactive (list (package-buffer-info)))
1097 (let* ((name (package-desc-name pkg-desc))
1098 (requires (package-desc-reqs pkg-desc))
1099 (desc (package-desc-summary pkg-desc))
1100 (pkg-version (package-desc-version pkg-desc)))
1101 ;; Download and install the dependencies.
1102 (let ((transaction (package-compute-transaction nil requires)))
1103 (package-download-transaction transaction))
1104 ;; Install the package itself.
1105 (pcase (package-desc-kind pkg-desc)
1106 (`single (package-unpack-single name pkg-version desc requires))
1107 (`tar (package-unpack name pkg-version))
1108 (type (error "Unknown type: %S" type)))
1109 ;; Try to activate it.
1110 (package-initialize)))))
1113 (defun package-install-file (file)
1114 "Install a package from a file.
1115 The file can either be a tar file or an Emacs Lisp file."
1116 (interactive "fPackage file name: ")
1118 (insert-file-contents-literally file)
1120 ((string-match "\\.el\\'" file)
1121 (package-install-from-buffer (package-buffer-info)))
1122 ((string-match "\\.tar\\'" file)
1123 (package-install-from-buffer (package-tar-file-info file)))
1124 (t (error "Unrecognized extension `%s'" (file-name-extension file))))))
1126 (defun package-delete (name version)
1127 (let ((dir (package--dir name version)))
1128 (if (string-equal (file-name-directory dir)
1129 (file-name-as-directory
1130 (expand-file-name package-user-dir)))
1132 (delete-directory dir t t)
1133 (message "Package `%s-%s' deleted." name version))
1134 ;; Don't delete "system" packages
1135 (error "Package `%s-%s' is a system package, not deleting"
1138 (defun package-archive-base (name)
1139 "Return the archive containing the package NAME."
1140 (let ((desc (cdr (assq (intern-soft name) package-archive-contents))))
1141 (cdr (assoc (package-desc-archive desc) package-archives))))
1143 (defun package--download-one-archive (archive file)
1144 "Retrieve an archive file FILE from ARCHIVE, and cache it.
1145 ARCHIVE should be a cons cell of the form (NAME . LOCATION),
1146 similar to an entry in `package-alist'. Save the cached copy to
1147 \"archives/NAME/archive-contents\" in `package-user-dir'."
1148 (let* ((dir (expand-file-name "archives" package-user-dir))
1149 (dir (expand-file-name (car archive) dir)))
1150 (package--with-work-buffer (cdr archive) file
1151 ;; Read the retrieved buffer to make sure it is valid (e.g. it
1152 ;; may fetch a URL redirect page).
1153 (when (listp (read buffer))
1154 (make-directory dir t)
1155 (setq buffer-file-name (expand-file-name file dir))
1156 (let ((version-control 'never))
1160 (defun package-refresh-contents ()
1161 "Download the ELPA archive description if needed.
1162 This informs Emacs about the latest versions of all packages, and
1163 makes them available for download."
1165 (unless (file-exists-p package-user-dir)
1166 (make-directory package-user-dir t))
1167 (dolist (archive package-archives)
1168 (condition-case-unless-debug nil
1169 (package--download-one-archive archive "archive-contents")
1170 (error (message "Failed to download `%s' archive."
1172 (package-read-all-archive-contents))
1175 (defun package-initialize (&optional no-activate)
1176 "Load Emacs Lisp packages, and activate them.
1177 The variable `package-load-list' controls which packages to load.
1178 If optional arg NO-ACTIVATE is non-nil, don't activate packages."
1180 (setq package-alist nil
1181 package-obsolete-alist nil)
1182 (package-load-all-descriptors)
1183 (package-read-all-archive-contents)
1185 (dolist (elt package-alist)
1186 (package-activate (car elt) (package-desc-version (cdr elt)))))
1187 (setq package--initialized t))
1190 ;;;; Package description buffer.
1193 (defun describe-package (package)
1194 "Display the full documentation of PACKAGE (a symbol)."
1196 (let* ((guess (function-called-at-point))
1198 (require 'finder-inf nil t)
1199 ;; Load the package list if necessary (but don't activate them).
1200 (unless package--initialized
1201 (package-initialize t))
1202 (setq packages (append (mapcar 'car package-alist)
1203 (mapcar 'car package-archive-contents)
1204 (mapcar 'car package--builtins)))
1205 (unless (memq guess packages)
1207 (setq packages (mapcar 'symbol-name packages))
1209 (completing-read (if guess
1210 (format "Describe package (default %s): "
1212 "Describe package: ")
1213 packages nil t nil nil guess))
1214 (list (if (equal val "") guess (intern val)))))
1215 (if (or (null package) (not (symbolp package)))
1216 (message "No package specified")
1217 (help-setup-xref (list #'describe-package package)
1218 (called-interactively-p 'interactive))
1219 (with-help-window (help-buffer)
1220 (with-current-buffer standard-output
1221 (describe-package-1 package)))))
1223 (defun describe-package-1 (package)
1225 (let ((package-name (symbol-name package))
1226 (built-in (assq package package--builtins))
1227 desc pkg-dir reqs version installable archive)
1231 ;; Loaded packages are in `package-alist'.
1232 ((setq desc (cdr (assq package package-alist)))
1233 (setq version (package-version-join (package-desc-version desc)))
1234 (if (setq pkg-dir (package--dir package-name version))
1235 (insert "an installed package.\n\n")
1236 ;; This normally does not happen.
1237 (insert "a deleted package.\n\n")))
1238 ;; Available packages are in `package-archive-contents'.
1239 ((setq desc (cdr (assq package package-archive-contents)))
1240 (setq version (package-version-join (package-desc-version desc))
1241 archive (package-desc-archive desc)
1244 (insert "a built-in package.\n\n")
1245 (insert "an uninstalled package.\n\n")))
1247 (setq desc (package--from-builtin built-in)
1248 version (package-version-join (package-desc-version desc)))
1249 (insert "a built-in package.\n\n"))
1251 (insert "an orphan package.\n\n")))
1253 (insert " " (propertize "Status" 'font-lock-face 'bold) ": ")
1255 (insert (propertize "Installed"
1256 'font-lock-face 'font-lock-comment-face))
1258 ;; Todo: Add button for uninstalling.
1259 (help-insert-xref-button (file-name-as-directory pkg-dir)
1260 'help-package-def pkg-dir)
1262 (insert "',\n shadowing a "
1263 (propertize "built-in package"
1264 'font-lock-face 'font-lock-builtin-face)
1269 (insert (propertize "Built-in."
1270 'font-lock-face 'font-lock-builtin-face)
1271 " Alternate version available")
1272 (insert "Available"))
1273 (insert " from " archive)
1275 (let ((button-text (if (display-graphic-p) "Install" "[Install]"))
1276 (button-face (if (display-graphic-p)
1277 '(:box (:line-width 2 :color "dark grey")
1278 :background "light grey"
1279 :foreground "black")
1281 (insert-text-button button-text 'face button-face 'follow-link t
1282 'package-symbol package
1283 'action 'package-install-button-action)))
1285 (insert (propertize "Built-in."
1286 'font-lock-face 'font-lock-builtin-face)))
1287 (t (insert "Deleted.")))
1289 (and version (> (length version) 0)
1291 (propertize "Version" 'font-lock-face 'bold) ": " version "\n"))
1293 (setq reqs (if desc (package-desc-reqs desc)))
1295 (insert " " (propertize "Requires" 'font-lock-face 'bold) ": ")
1299 (setq name (car req)
1301 text (format "%s-%s" (symbol-name name)
1302 (package-version-join vers)))
1303 (cond (first (setq first nil))
1304 ((>= (+ 2 (current-column) (length text))
1308 (help-insert-xref-button text 'help-package name))
1310 (insert " " (propertize "Summary" 'font-lock-face 'bold)
1311 ": " (if desc (package-desc-summary desc)) "\n\n")
1314 ;; For built-in packages, insert the commentary.
1315 (let ((fn (locate-file (concat package-name ".el") load-path
1316 load-file-rep-suffixes))
1318 (insert (or (lm-commentary fn) ""))
1321 (when (re-search-forward "^;;; Commentary:\n" nil t)
1323 (while (re-search-forward "^\\(;+ ?\\)" nil t)
1324 (replace-match ""))))
1325 (let ((readme (expand-file-name (concat package-name "-readme.txt")
1328 ;; For elpa packages, try downloading the commentary. If that
1329 ;; fails, try an existing readme file in `package-user-dir'.
1330 (cond ((condition-case nil
1331 (package--with-work-buffer (package-archive-base package)
1332 (concat package-name "-readme.txt")
1333 (setq buffer-file-name
1334 (expand-file-name readme package-user-dir))
1335 (let ((version-control 'never))
1337 (setq readme-string (buffer-string))
1340 (insert readme-string))
1341 ((file-readable-p readme)
1342 (insert-file-contents readme)
1343 (goto-char (point-max))))))))
1345 (defun package-install-button-action (button)
1346 (let ((package (button-get button 'package-symbol)))
1347 (when (y-or-n-p (format "Install package `%s'? " package))
1348 (package-install package)
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 (tabulated-list-init-header))
1437 (defmacro package--push (package desc status listname)
1438 "Convenience macro for `package-menu--generate'.
1439 If the alist stored in the symbol LISTNAME lacks an entry for a
1440 package PACKAGE with descriptor DESC, add one. The alist is
1441 keyed with cons cells (PACKAGE . VERSION-LIST), where PACKAGE is
1442 a symbol and VERSION-LIST is a version list."
1443 `(let* ((version (package-desc-version ,desc))
1444 (key (cons ,package version)))
1445 (unless (assoc key ,listname)
1446 (push (list key ,status (package-desc-summary ,desc)) ,listname))))
1448 (defun package-menu--generate (remember-pos packages)
1449 "Populate the Package Menu.
1450 If REMEMBER-POS is non-nil, keep point on the same entry.
1451 PACKAGES should be t, which means to display all known packages,
1452 or a list of package names (symbols) to display."
1453 ;; Construct list of ((PACKAGE . VERSION) STATUS DESCRIPTION).
1454 (let (info-list name)
1455 ;; Installed packages:
1456 (dolist (elt package-alist)
1457 (setq name (car elt))
1458 (when (or (eq packages t) (memq name packages))
1459 (package--push name (cdr elt)
1460 (if (stringp (cadr (assq name package-load-list)))
1464 ;; Built-in packages:
1465 (dolist (elt package--builtins)
1466 (setq name (car elt))
1467 (when (and (not (eq name 'emacs)) ; Hide the `emacs' package.
1468 (or (eq packages t) (memq name packages)))
1469 (package--push name (package--from-builtin elt) "built-in" info-list)))
1471 ;; Available and disabled packages:
1472 (dolist (elt package-archive-contents)
1473 (setq name (car elt))
1474 (when (or (eq packages t) (memq name packages))
1475 (let ((hold (assq name package-load-list)))
1476 (package--push name (cdr elt)
1478 ((and hold (null (cadr hold))) "disabled")
1479 ((memq name package-menu--new-package-list) "new")
1483 ;; Obsolete packages:
1484 (dolist (elt package-obsolete-alist)
1485 (dolist (inner-elt (cdr elt))
1486 (when (or (eq packages t) (memq (car elt) packages))
1487 (package--push (car elt) (cdr inner-elt) "obsolete" info-list))))
1489 ;; Print the result.
1490 (setq tabulated-list-entries (mapcar 'package-menu--print-info info-list))
1491 (tabulated-list-print remember-pos)))
1493 (defun package-menu--print-info (pkg)
1494 "Return a package entry suitable for `tabulated-list-entries'.
1495 PKG has the form ((PACKAGE . VERSION) STATUS DOC).
1496 Return (KEY [NAME VERSION STATUS DOC]), where KEY is the
1497 identifier (NAME . VERSION-LIST)."
1498 (let* ((package (caar pkg))
1499 (version (cdr (car pkg)))
1500 (status (nth 1 pkg))
1501 (doc (or (nth 2 pkg) ""))
1503 ((string= status "built-in") 'font-lock-builtin-face)
1504 ((string= status "available") 'default)
1505 ((string= status "new") 'bold)
1506 ((string= status "held") 'font-lock-constant-face)
1507 ((string= status "disabled") 'font-lock-warning-face)
1508 ((string= status "installed") 'font-lock-comment-face)
1509 (t 'font-lock-warning-face)))) ; obsolete.
1510 (list (cons package version)
1511 (vector (list (symbol-name package)
1514 'package-symbol package
1515 'action 'package-menu-describe-package)
1516 (propertize (package-version-join version)
1517 'font-lock-face face)
1518 (propertize status 'font-lock-face face)
1519 (propertize doc 'font-lock-face face)))))
1521 (defun package-menu-refresh ()
1522 "Download the Emacs Lisp package archive.
1523 This fetches the contents of each archive specified in
1524 `package-archives', and then refreshes the package menu."
1526 (unless (derived-mode-p 'package-menu-mode)
1527 (error "The current buffer is not a Package Menu"))
1528 (package-refresh-contents)
1529 (package-menu--generate t t))
1531 (defun package-menu-describe-package (&optional button)
1532 "Describe the current package.
1533 If optional arg BUTTON is non-nil, describe its associated package."
1535 (let ((package (if button (button-get button 'package-symbol)
1536 (car (tabulated-list-get-id)))))
1538 (describe-package package))))
1540 ;; fixme numeric argument
1541 (defun package-menu-mark-delete (&optional _num)
1542 "Mark a package for deletion and move to the next line."
1544 (if (member (package-menu-get-status) '("installed" "obsolete"))
1545 (tabulated-list-put-tag "D" t)
1548 (defun package-menu-mark-install (&optional _num)
1549 "Mark a package for installation and move to the next line."
1551 (if (member (package-menu-get-status) '("available" "new"))
1552 (tabulated-list-put-tag "I" t)
1555 (defun package-menu-mark-unmark (&optional _num)
1556 "Clear any marks on a package and move to the next line."
1558 (tabulated-list-put-tag " " t))
1560 (defun package-menu-backup-unmark ()
1561 "Back up one line and clear any marks on that package."
1564 (tabulated-list-put-tag " "))
1566 (defun package-menu-mark-obsolete-for-deletion ()
1567 "Mark all obsolete packages for deletion."
1570 (goto-char (point-min))
1572 (if (equal (package-menu-get-status) "obsolete")
1573 (tabulated-list-put-tag "D" t)
1574 (forward-line 1)))))
1576 (defun package-menu-quick-help ()
1577 "Show short key binding help for package-menu-mode."
1579 (message "n-ext, i-nstall, d-elete, u-nmark, x-ecute, r-efresh, h-elp"))
1581 (define-obsolete-function-alias
1582 'package-menu-view-commentary 'package-menu-describe-package "24.1")
1584 (defun package-menu-get-status ()
1585 (let* ((pkg (tabulated-list-get-id))
1586 (entry (and pkg (assq pkg tabulated-list-entries))))
1588 (aref (cadr entry) 2)
1591 (defun package-menu--find-upgrades ()
1592 (let (installed available upgrades)
1593 ;; Build list of installed/available packages in this buffer.
1594 (dolist (entry tabulated-list-entries)
1595 ;; ENTRY is ((NAME . VERSION) [NAME VERSION STATUS DOC])
1596 (let ((pkg (car entry))
1597 (status (aref (cadr entry) 2)))
1598 (cond ((equal status "installed")
1599 (push pkg installed))
1600 ((member status '("available" "new"))
1601 (push pkg available)))))
1602 ;; Loop through list of installed packages, finding upgrades
1603 (dolist (pkg installed)
1604 (let ((avail-pkg (assq (car pkg) available)))
1606 (version-list-< (cdr pkg) (cdr avail-pkg))
1607 (push avail-pkg upgrades))))
1610 (defun package-menu-mark-upgrades ()
1611 "Mark all upgradable packages in the Package Menu.
1612 For each installed package with a newer version available, place
1613 an (I)nstall flag on the available version and a (D)elete flag on
1614 the installed version. A subsequent \\[package-menu-execute]
1615 call will upgrade the package."
1617 (unless (derived-mode-p 'package-menu-mode)
1618 (error "The current buffer is not a Package Menu"))
1619 (let ((upgrades (package-menu--find-upgrades)))
1621 (message "No packages to upgrade.")
1624 (goto-char (point-min))
1626 (let* ((pkg (tabulated-list-get-id))
1627 (upgrade (assq (car pkg) upgrades)))
1628 (cond ((null upgrade)
1630 ((equal pkg upgrade)
1631 (package-menu-mark-install))
1633 (package-menu-mark-delete))))))
1634 (message "%d package%s marked for upgrading."
1636 (if (= (length upgrades) 1) "" "s")))))
1638 (defun package-menu-execute (&optional noquery)
1639 "Perform marked Package Menu actions.
1640 Packages marked for installation are downloaded and installed;
1641 packages marked for deletion are removed.
1642 Optional argument NOQUERY non-nil means do not ask the user to confirm."
1644 (unless (derived-mode-p 'package-menu-mode)
1645 (error "The current buffer is not in Package Menu mode"))
1646 (let (install-list delete-list cmd id)
1648 (goto-char (point-min))
1650 (setq cmd (char-after))
1651 (unless (eq cmd ?\s)
1652 ;; This is the key (PACKAGE . VERSION-LIST).
1653 (setq id (tabulated-list-get-id))
1655 (push (cons (symbol-name (car id))
1656 (package-version-join (cdr id)))
1659 (push (car id) install-list))))
1665 (if (= (length install-list) 1)
1666 (format "Install package `%s'? " (car install-list))
1667 (format "Install these %d packages (%s)? "
1668 (length install-list)
1669 (mapconcat 'symbol-name install-list ", ")))))
1670 (mapc 'package-install install-list)))
1671 ;; Delete packages, prompting if necessary.
1676 (if (= (length delete-list) 1)
1677 (format "Delete package `%s-%s'? "
1679 (cdr (car delete-list)))
1680 (format "Delete these %d packages (%s)? "
1681 (length delete-list)
1682 (mapconcat (lambda (elt)
1683 (concat (car elt) "-" (cdr elt)))
1686 (dolist (elt delete-list)
1687 (condition-case-unless-debug err
1688 (package-delete (car elt) (cdr elt))
1689 (error (message (cadr err)))))
1691 ;; If we deleted anything, regenerate `package-alist'. This is done
1692 ;; automatically if we installed a package.
1693 (and delete-list (null install-list)
1694 (package-initialize))
1695 (if (or delete-list install-list)
1696 (package-menu--generate t t)
1697 (message "No operations specified."))))
1699 (defun package-menu--version-predicate (A B)
1700 (let ((vA (or (aref (cadr A) 1) '(0)))
1701 (vB (or (aref (cadr B) 1) '(0))))
1702 (if (version-list-= vA vB)
1703 (package-menu--name-predicate A B)
1704 (version-list-< vA vB))))
1706 (defun package-menu--status-predicate (A B)
1707 (let ((sA (aref (cadr A) 2))
1708 (sB (aref (cadr B) 2)))
1709 (cond ((string= sA sB)
1710 (package-menu--name-predicate A B))
1711 ((string= sA "new") t)
1712 ((string= sB "new") nil)
1713 ((string= sA "available") t)
1714 ((string= sB "available") nil)
1715 ((string= sA "installed") t)
1716 ((string= sB "installed") nil)
1717 ((string= sA "held") t)
1718 ((string= sB "held") nil)
1719 ((string= sA "built-in") t)
1720 ((string= sB "built-in") nil)
1721 ((string= sA "obsolete") t)
1722 ((string= sB "obsolete") nil)
1723 (t (string< sA sB)))))
1725 (defun package-menu--description-predicate (A B)
1726 (let ((dA (aref (cadr A) 3))
1727 (dB (aref (cadr B) 3)))
1729 (package-menu--name-predicate A B)
1732 (defun package-menu--name-predicate (A B)
1733 (string< (symbol-name (caar A))
1734 (symbol-name (caar B))))
1737 (defun list-packages (&optional no-fetch)
1738 "Display a list of packages.
1739 This first fetches the updated list of packages before
1740 displaying, unless a prefix argument NO-FETCH is specified.
1741 The list is displayed in a buffer named `*Packages*'."
1743 (require 'finder-inf nil t)
1744 ;; Initialize the package system if necessary.
1745 (unless package--initialized
1746 (package-initialize t))
1747 (let (old-archives new-packages)
1749 ;; Read the locally-cached archive-contents.
1750 (package-read-all-archive-contents)
1751 (setq old-archives package-archive-contents)
1752 ;; Fetch the remote list of packages.
1753 (package-refresh-contents)
1754 ;; Find which packages are new.
1755 (dolist (elt package-archive-contents)
1756 (unless (assq (car elt) old-archives)
1757 (push (car elt) new-packages))))
1759 ;; Generate the Package Menu.
1760 (let ((buf (get-buffer-create "*Packages*")))
1761 (with-current-buffer buf
1763 (set (make-local-variable 'package-menu--new-package-list)
1765 (package-menu--generate nil t))
1766 ;; The package menu buffer has keybindings. If the user types
1767 ;; `M-x list-packages', that suggests it should become current.
1768 (switch-to-buffer buf))
1770 (let ((upgrades (package-menu--find-upgrades)))
1772 (message "%d package%s can be upgraded; type `%s' to mark %s for upgrading."
1774 (if (= (length upgrades) 1) "" "s")
1775 (substitute-command-keys "\\[package-menu-mark-upgrades]")
1776 (if (= (length upgrades) 1) "it" "them"))))))
1779 (defalias 'package-list-packages 'list-packages)
1781 ;; Used in finder.el
1782 (defun package-show-package-list (packages)
1783 "Display PACKAGES in a *Packages* buffer.
1784 This is similar to `list-packages', but it does not fetch the
1785 updated list of packages, and it only displays packages with
1786 names in PACKAGES (which should be a list of symbols)."
1787 (require 'finder-inf nil t)
1788 (let ((buf (get-buffer-create "*Packages*")))
1789 (with-current-buffer buf
1791 (package-menu--generate nil packages))
1792 (switch-to-buffer buf)))
1794 (defun package-list-packages-no-fetch ()
1795 "Display a list of packages.
1796 Does not fetch the updated list of packages before displaying.
1797 The list is displayed in a buffer named `*Packages*'."
1803 ;;; package.el ends here