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 (require 'tabulated-list)
175 (defgroup package nil
176 "Manager for Emacs Lisp packages."
181 (defcustom package-enable-at-startup t
182 "Whether to activate installed packages when Emacs starts.
183 If non-nil, packages are activated after reading the init file
184 and before `after-init-hook'. Activation is not done if
185 `user-init-file' is nil (e.g. Emacs was started with \"-q\").
187 Even if the value is nil, you can type \\[package-initialize] to
188 activate the package system at any time."
193 (defcustom package-load-list '(all)
194 "List of packages for `package-initialize' to load.
195 Each element in this list should be a list (NAME VERSION), or the
196 symbol `all'. The symbol `all' says to load the latest installed
197 versions of all packages not specified by other elements.
199 For an element (NAME VERSION), NAME is a package name (a symbol).
200 VERSION should be t, a string, or nil.
201 If VERSION is t, all versions are loaded, though obsolete ones
202 will be put in `package-obsolete-alist' and not activated.
203 If VERSION is a string, only that version is ever loaded.
204 Any other version, even if newer, is silently ignored.
205 Hence, the package is \"held\" at that version.
206 If VERSION is nil, the package is not loaded (it is \"disabled\")."
207 :type '(repeat symbol)
212 (defvar Info-directory-list)
213 (declare-function info-initialize "info" ())
214 (declare-function url-http-parse-response "url-http" ())
215 (declare-function lm-header "lisp-mnt" (header))
216 (declare-function lm-commentary "lisp-mnt" (&optional file))
217 (defvar url-http-end-of-headers)
219 (defcustom package-archives '(("gnu" . "http://elpa.gnu.org/packages/"))
220 "An alist of archives from which to fetch.
221 The default value points to the GNU Emacs package repository.
223 Each element has the form (ID . LOCATION).
224 ID is an archive name, as a string.
225 LOCATION specifies the base location for the archive.
226 If it starts with \"http:\", it is treated as a HTTP URL;
227 otherwise it should be an absolute directory name.
228 (Other types of URL are currently not supported.)
230 Only add locations that you trust, since fetching and installing
231 a package can run arbitrary code."
232 :type '(alist :key-type (string :tag "Archive name")
233 :value-type (string :tag "URL or directory name"))
238 (defcustom package-pinned-packages nil
239 "An alist of packages that are pinned to a specific archive
241 Each element has the form (SYM . ID).
242 SYM is a package, as a symbol.
243 ID is an archive name, as a string. This should correspond to an
244 entry in `package-archives'.
246 If the archive of name ID does not contain the package SYM, no
247 other location will be considered, which will make the
248 package unavailable."
249 :type '(alist :key-type (symbol :tag "Package")
250 :value-type (string :tag "Archive name"))
255 (defconst package-archive-version 1
256 "Version number of the package archive understood by this file.
257 Lower version numbers than this will probably be understood as well.")
259 (defconst package-el-version "1.0.1"
260 "Version of package.el.")
262 ;; We don't prime the cache since it tends to get out of date.
263 (defvar package-archive-contents nil
264 "Cache of the contents of the Emacs Lisp Package Archive.
265 This is an alist mapping package names (symbols) to package
266 descriptor vectors. These are like the vectors for `package-alist'
267 but have extra entries: one which is 'tar for tar packages and
268 'single for single-file packages, and one which is the name of
269 the archive from which it came.")
270 (put 'package-archive-contents 'risky-local-variable t)
272 (defcustom package-user-dir (locate-user-emacs-file "elpa")
273 "Directory containing the user's Emacs Lisp packages.
274 The directory name should be absolute.
275 Apart from this directory, Emacs also looks for system-wide
276 packages in `package-directory-list'."
282 (defcustom package-directory-list
283 ;; Defaults are subdirs named "elpa" in the site-lisp dirs.
285 (dolist (f load-path)
287 (equal (file-name-nondirectory f) "site-lisp")
288 (push (expand-file-name "elpa" f) result)))
290 "List of additional directories containing Emacs Lisp packages.
291 Each directory name should be absolute.
293 These directories contain packages intended for system-wide; in
294 contrast, `package-user-dir' contains packages for personal use."
295 :type '(repeat directory)
300 ;; The value is precomputed in finder-inf.el, but don't load that
301 ;; until it's needed (i.e. when `package-initialize' is called).
302 (defvar package--builtins nil
303 "Alist of built-in packages.
304 The actual value is initialized by loading the library
305 `finder-inf'; this is not done until it is needed, e.g. by the
306 function `package-built-in-p'.
308 Each element has the form (PKG . DESC), where PKG is a package
309 name (a symbol) and DESC is a vector that describes the package.
310 The vector DESC has the form [VERSION-LIST REQS DOCSTRING].
311 VERSION-LIST is a version list.
312 REQS is a list of packages required by the package, each
313 requirement having the form (NAME VL), where NAME is a string
314 and VL is a version list.
315 DOCSTRING is a brief description of the package.")
316 (put 'package--builtins 'risky-local-variable t)
318 (defvar package-alist nil
319 "Alist of all packages available for activation.
320 Each element has the form (PKG . DESC), where PKG is a package
321 name (a symbol) and DESC is a vector that describes the package.
323 The vector DESC has the form [VERSION-LIST REQS DOCSTRING].
324 VERSION-LIST is a version list.
325 REQS is a list of packages required by the package, each
326 requirement having the form (NAME VL) where NAME is a string
327 and VL is a version list.
328 DOCSTRING is a brief description of the package.
330 This variable is set automatically by `package-load-descriptor',
331 called via `package-initialize'. To change which packages are
332 loaded and/or activated, customize `package-load-list'.")
333 (put 'package-alist 'risky-local-variable t)
335 (defvar package-activated-list nil
336 "List of the names of currently activated packages.")
337 (put 'package-activated-list 'risky-local-variable t)
339 (defvar package-obsolete-alist nil
340 "Representation of obsolete packages.
341 Like `package-alist', but maps package name to a second alist.
342 The inner alist is keyed by version.")
343 (put 'package-obsolete-alist 'risky-local-variable t)
345 (defun package-version-join (vlist)
346 "Return the version string corresponding to the list VLIST.
347 This is, approximately, the inverse of `version-to-list'.
348 \(Actually, it returns only one of the possible inverses, since
349 `version-to-list' is a many-to-one operation.)"
352 (let ((str-list (list "." (int-to-string (car vlist)))))
353 (dolist (num (cdr vlist))
356 (push (int-to-string num) str-list)
359 (error "Invalid version list `%s'" vlist))
361 ;; pre, or beta, or alpha
362 (cond ((equal "." (car str-list))
364 ((not (string-match "[0-9]+" (car str-list)))
365 (error "Invalid version list `%s'" vlist)))
366 (push (cond ((= num -1) "pre")
368 ((= num -3) "alpha"))
370 (if (equal "." (car str-list))
372 (apply 'concat (nreverse str-list)))))
374 (defun package-strip-version (dirname)
375 "Strip the version from a combined package name and version.
376 E.g., if given \"quux-23.0\", will return \"quux\""
377 (if (string-match (concat "\\`" package-subdirectory-regexp "\\'") dirname)
378 (match-string 1 dirname)))
380 (defun package-load-descriptor (dir package)
381 "Load the description file in directory DIR for package PACKAGE.
382 Here, PACKAGE is a string of the form NAME-VERSION, where NAME is
383 the package name and VERSION is its version."
384 (let* ((pkg-dir (expand-file-name package dir))
385 (pkg-file (expand-file-name
386 (concat (package-strip-version package) "-pkg")
388 (when (and (file-directory-p pkg-dir)
389 (file-exists-p (concat pkg-file ".el")))
390 (load pkg-file nil t))))
392 (defun package-load-all-descriptors ()
393 "Load descriptors for installed Emacs Lisp packages.
394 This looks for package subdirectories in `package-user-dir' and
395 `package-directory-list'. The variable `package-load-list'
396 controls which package subdirectories may be loaded.
398 In each valid package subdirectory, this function loads the
399 description file containing a call to `define-package', which
400 updates `package-alist' and `package-obsolete-alist'."
401 (let ((regexp (concat "\\`" package-subdirectory-regexp "\\'")))
402 (dolist (dir (cons package-user-dir package-directory-list))
403 (when (file-directory-p dir)
404 (dolist (subdir (directory-files dir))
405 (when (string-match regexp subdir)
406 (package-maybe-load-descriptor (match-string 1 subdir)
407 (match-string 2 subdir)
410 (defun package-maybe-load-descriptor (name version dir)
411 "Maybe load a specific package from directory DIR.
412 NAME and VERSION are the package's name and version strings.
413 This function checks `package-load-list', before actually loading
414 the package by calling `package-load-descriptor'."
415 (let ((force (assq (intern name) package-load-list))
416 (subdir (concat name "-" version)))
417 (and (file-directory-p (expand-file-name subdir dir))
418 ;; Check `package-load-list':
420 (memq 'all package-load-list))
421 ((null (setq force (cadr force)))
425 ((stringp force) ; held
426 (version-list-= (version-to-list version)
427 (version-to-list force)))
429 (error "Invalid element in `package-load-list'")))
430 ;; Actually load the descriptor:
431 (package-load-descriptor dir subdir))))
433 (defsubst package-desc-vers (desc)
434 "Extract version from a package description vector."
437 (defsubst package-desc-reqs (desc)
438 "Extract requirements from a package description vector."
441 (defsubst package-desc-doc (desc)
442 "Extract doc string from a package description vector."
445 (defsubst package-desc-kind (desc)
446 "Extract the kind of download from an archive package description vector."
449 (defun package--dir (name version)
450 "Return the directory where a package is installed, or nil if none.
451 NAME and VERSION are both strings."
452 (let* ((subdir (concat name "-" version))
453 (dir-list (cons package-user-dir package-directory-list))
456 (let ((subdir-full (expand-file-name subdir (car dir-list))))
457 (if (file-directory-p subdir-full)
458 (setq pkg-dir subdir-full
460 (setq dir-list (cdr dir-list)))))
463 (defun package-activate-1 (package pkg-vec)
464 (let* ((name (symbol-name package))
465 (version-str (package-version-join (package-desc-vers pkg-vec)))
466 (pkg-dir (package--dir name version-str)))
468 (error "Internal error: unable to find directory for `%s-%s'"
471 (when (file-exists-p (expand-file-name "dir" pkg-dir))
472 ;; FIXME: not the friendliest, but simple.
475 (push pkg-dir Info-directory-list))
476 ;; Add to load path, add autoloads, and activate the package.
477 (push pkg-dir load-path)
478 (load (expand-file-name (concat name "-autoloads") pkg-dir) nil t)
479 (push package package-activated-list)
483 (defun package-built-in-p (package &optional min-version)
484 "Return true if PACKAGE is built-in to Emacs.
485 Optional arg MIN-VERSION, if non-nil, should be a version list
486 specifying the minimum acceptable version."
487 (require 'finder-inf nil t) ; For `package--builtins'.
488 (if (eq package 'emacs)
489 (version-list-<= min-version (version-to-list emacs-version))
490 (let ((elt (assq package package--builtins)))
491 (and elt (version-list-<= min-version
492 (package-desc-vers (cdr elt)))))))
494 ;; This function goes ahead and activates a newer version of a package
495 ;; if an older one was already activated. This is not ideal; we'd at
496 ;; least need to check to see if the package has actually been loaded,
497 ;; and not merely activated.
498 (defun package-activate (package min-version)
499 "Activate package PACKAGE, of version MIN-VERSION or newer.
500 MIN-VERSION should be a version list.
501 If PACKAGE has any dependencies, recursively activate them.
502 Return nil if the package could not be activated."
503 (let ((pkg-vec (cdr (assq package package-alist)))
504 available-version found)
505 ;; Check if PACKAGE is available in `package-alist'.
507 (setq available-version (package-desc-vers pkg-vec)
508 found (version-list-<= min-version available-version)))
510 ;; If no such package is found, maybe it's built-in.
512 (package-built-in-p package min-version))
513 ;; If the package is already activated, just return t.
514 ((memq package package-activated-list)
516 ;; Otherwise, proceed with activation.
518 (let ((fail (catch 'dep-failure
519 ;; Activate its dependencies recursively.
520 (dolist (req (package-desc-reqs pkg-vec))
521 (unless (package-activate (car req) (cadr req))
522 (throw 'dep-failure req))))))
524 (warn "Unable to activate package `%s'.
525 Required package `%s-%s' is unavailable"
526 package (car fail) (package-version-join (cadr fail)))
527 ;; If all goes well, activate the package itself.
528 (package-activate-1 package pkg-vec)))))))
530 (defun package-mark-obsolete (package pkg-vec)
531 "Put package on the obsolete list, if not already there."
532 (let ((elt (assq package package-obsolete-alist)))
534 ;; If this obsolete version does not exist in the list, update
536 (unless (assoc (package-desc-vers pkg-vec) (cdr elt))
537 (setcdr elt (cons (cons (package-desc-vers pkg-vec) pkg-vec)
539 ;; Make a new association.
540 (push (cons package (list (cons (package-desc-vers pkg-vec)
542 package-obsolete-alist))))
544 (defun define-package (name-string version-string
545 &optional docstring requirements
546 &rest _extra-properties)
547 "Define a new package.
548 NAME-STRING is the name of the package, as a string.
549 VERSION-STRING is the version of the package, as a string.
550 DOCSTRING is a short description of the package, a string.
551 REQUIREMENTS is a list of dependencies on other packages.
552 Each requirement is of the form (OTHER-PACKAGE OTHER-VERSION),
553 where OTHER-VERSION is a string.
555 EXTRA-PROPERTIES is currently unused."
556 (let* ((name (intern name-string))
557 (version (version-to-list version-string))
564 (version-to-list (car (cdr elt)))))
567 (old-pkg (assq name package-alist)))
569 ;; If there's no old package, just add this to `package-alist'.
571 (push new-pkg-desc package-alist))
572 ((version-list-< (package-desc-vers (cdr old-pkg)) version)
573 ;; Remove the old package and declare it obsolete.
574 (package-mark-obsolete name (cdr old-pkg))
575 (setq package-alist (cons new-pkg-desc
576 (delq old-pkg package-alist))))
577 ;; You can have two packages with the same version, e.g. one in
578 ;; the system package directory and one in your private
579 ;; directory. We just let the first one win.
580 ((not (version-list-= (package-desc-vers (cdr old-pkg)) version))
581 ;; The package is born obsolete.
582 (package-mark-obsolete name (cdr new-pkg-desc))))))
585 (defun package-autoload-ensure-default-file (file)
586 "Make sure that the autoload file FILE exists and if not create it."
587 (unless (file-exists-p file)
589 (concat ";;; " (file-name-nondirectory file)
590 " --- automatically extracted autoloads\n"
593 "(add-to-list 'load-path (or (file-name-directory #$) (car load-path)))\n"
594 "
\f\n;; Local Variables:\n"
595 ";; version-control: never\n"
596 ";; no-byte-compile: t\n"
597 ";; no-update-autoloads: t\n"
599 ";;; " (file-name-nondirectory file)
604 (defun package-generate-autoloads (name pkg-dir)
605 (require 'autoload) ;Load before we let-bind generated-autoload-file!
606 (let* ((auto-name (concat name "-autoloads.el"))
607 ;;(ignore-name (concat name "-pkg.el"))
608 (generated-autoload-file (expand-file-name auto-name pkg-dir))
609 (version-control 'never))
610 (package-autoload-ensure-default-file generated-autoload-file)
611 (update-directory-autoloads pkg-dir)
612 (let ((buf (find-buffer-visiting generated-autoload-file)))
613 (when buf (kill-buffer buf)))))
615 (defvar tar-parse-info)
616 (declare-function tar-untar-buffer "tar-mode" ())
617 (declare-function tar-header-name "tar-mode" (tar-header) t)
618 (declare-function tar-header-link-type "tar-mode" (tar-header) t)
620 (defun package-untar-buffer (dir)
621 "Untar the current buffer.
622 This uses `tar-untar-buffer' from Tar mode. All files should
623 untar into a directory named DIR; otherwise, signal an error."
626 ;; Make sure everything extracts into DIR.
627 (let ((regexp (concat "\\`" (regexp-quote (expand-file-name dir)) "/"))
628 (case-fold-search (memq system-type '(windows-nt ms-dos cygwin))))
629 (dolist (tar-data tar-parse-info)
630 (let ((name (expand-file-name (tar-header-name tar-data))))
631 (or (string-match regexp name)
632 ;; Tarballs created by some utilities don't list
633 ;; directories with a trailing slash (Bug#13136).
634 (and (string-equal dir name)
635 (eq (tar-header-link-type tar-data) 5))
636 (error "Package does not untar cleanly into directory %s/" dir)))))
639 (defun package-unpack (package version)
640 (let* ((name (symbol-name package))
641 (dirname (concat name "-" version))
642 (pkg-dir (expand-file-name dirname package-user-dir)))
643 (make-directory package-user-dir t)
644 ;; FIXME: should we delete PKG-DIR if it exists?
645 (let* ((default-directory (file-name-as-directory package-user-dir)))
646 (package-untar-buffer dirname)
647 (package--make-autoloads-and-compile name pkg-dir))))
649 (defun package--make-autoloads-and-compile (name pkg-dir)
650 "Generate autoloads and do byte-compilation for package named NAME.
651 PKG-DIR is the name of the package directory."
652 (package-generate-autoloads name pkg-dir)
653 (let ((load-path (cons pkg-dir load-path)))
654 ;; We must load the autoloads file before byte compiling, in
655 ;; case there are magic cookies to set up non-trivial paths.
656 (load (expand-file-name (concat name "-autoloads") pkg-dir) nil t)
657 (byte-recompile-directory pkg-dir 0 t)))
659 (defun package--write-file-no-coding (file-name)
660 (let ((buffer-file-coding-system 'no-conversion))
661 (write-region (point-min) (point-max) file-name)))
663 (defun package-unpack-single (file-name version desc requires)
664 "Install the contents of the current buffer as a package."
665 ;; Special case "package".
666 (if (string= file-name "package")
667 (package--write-file-no-coding
668 (expand-file-name (concat file-name ".el") package-user-dir))
669 (let* ((pkg-dir (expand-file-name (concat file-name "-"
670 (package-version-join
671 (version-to-list version)))
673 (el-file (expand-file-name (concat file-name ".el") pkg-dir))
674 (pkg-file (expand-file-name (concat file-name "-pkg.el") pkg-dir)))
675 (make-directory pkg-dir t)
676 (package--write-file-no-coding el-file)
677 (let ((print-level nil)
682 (list 'define-package
687 ;; Turn version lists into string form.
691 (package-version-join (cadr elt))))
697 (package--make-autoloads-and-compile file-name pkg-dir))))
699 (defmacro package--with-work-buffer (location file &rest body)
700 "Run BODY in a buffer containing the contents of FILE at LOCATION.
701 LOCATION is the base location of a package archive, and should be
702 one of the URLs (or file names) specified in `package-archives'.
703 FILE is the name of a file relative to that base location.
705 This macro retrieves FILE from LOCATION into a temporary buffer,
706 and evaluates BODY while that buffer is current. This work
707 buffer is killed afterwards. Return the last value in BODY."
708 `(let* ((http (string-match "\\`https?:" ,location))
711 (url-retrieve-synchronously (concat ,location ,file))
712 (generate-new-buffer "*package work buffer*"))))
714 (with-current-buffer buffer
716 (progn (package-handle-response)
717 (re-search-forward "^$" nil 'move)
719 (delete-region (point-min) (point)))
720 (unless (file-name-absolute-p ,location)
721 (error "Archive location %s is not an absolute file name"
723 (insert-file-contents (expand-file-name ,file ,location)))
725 (kill-buffer buffer))))
727 (defun package-handle-response ()
728 "Handle the response from a `url-retrieve-synchronously' call.
729 Parse the HTTP response and throw if an error occurred.
730 The url package seems to require extra processing for this.
731 This should be called in a `save-excursion', in the download buffer.
732 It will move point to somewhere in the headers."
733 ;; We assume HTTP here.
735 (let ((response (url-http-parse-response)))
736 (when (or (< response 200) (>= response 300))
737 (error "Error during download request:%s"
738 (buffer-substring-no-properties (point) (progn
742 (defun package-download-single (name version desc requires)
743 "Download and install a single-file package."
744 (let ((location (package-archive-base name))
745 (file (concat (symbol-name name) "-" version ".el")))
746 (package--with-work-buffer location file
747 (package-unpack-single (symbol-name name) version desc requires))))
749 (defun package-download-tar (name version)
750 "Download and install a tar package."
751 (let ((location (package-archive-base name))
752 (file (concat (symbol-name name) "-" version ".tar")))
753 (package--with-work-buffer location file
754 (package-unpack name version))))
756 (defvar package--initialized nil)
758 (defun package-installed-p (package &optional min-version)
759 "Return true if PACKAGE, of MIN-VERSION or newer, is installed.
760 MIN-VERSION should be a version list."
761 (unless package--initialized (error "package.el is not yet initialized!"))
762 (let ((pkg-desc (assq package package-alist)))
764 (version-list-<= min-version
765 (package-desc-vers (cdr pkg-desc)))
766 ;; Also check built-in packages.
767 (package-built-in-p package min-version))))
769 (defun package-compute-transaction (package-list requirements)
770 "Return a list of packages to be installed, including PACKAGE-LIST.
771 PACKAGE-LIST should be a list of package names (symbols).
773 REQUIREMENTS should be a list of additional requirements; each
774 element in this list should have the form (PACKAGE VERSION-LIST),
775 where PACKAGE is a package name and VERSION-LIST is the required
776 version of that package.
778 This function recursively computes the requirements of the
779 packages in REQUIREMENTS, and returns a list of all the packages
780 that must be installed. Packages that are already installed are
781 not included in this list."
782 (dolist (elt requirements)
783 (let* ((next-pkg (car elt))
784 (next-version (cadr elt)))
785 (unless (package-installed-p next-pkg next-version)
786 ;; A package is required, but not installed. It might also be
787 ;; blocked via `package-load-list'.
788 (let ((pkg-desc (assq next-pkg package-archive-contents))
790 (when (setq hold (assq next-pkg package-load-list))
791 (setq hold (cadr hold))
794 (error "Required package '%s' is disabled"
795 (symbol-name next-pkg)))
796 ((null (stringp hold))
797 (error "Invalid element in `package-load-list'"))
798 ((version-list-< (version-to-list hold) next-version)
799 (error "Package `%s' held at version %s, \
800 but version %s required"
801 (symbol-name next-pkg) hold
802 (package-version-join next-version)))))
804 (error "Package `%s-%s' is unavailable"
805 (symbol-name next-pkg)
806 (package-version-join next-version)))
807 (unless (version-list-<= next-version
808 (package-desc-vers (cdr pkg-desc)))
810 "Need package `%s-%s', but only %s is available"
811 (symbol-name next-pkg) (package-version-join next-version)
812 (package-version-join (package-desc-vers (cdr pkg-desc)))))
813 ;; Move to front, so it gets installed early enough (bug#14082).
814 (setq package-list (cons next-pkg (delq next-pkg package-list)))
816 (package-compute-transaction package-list
818 (cdr pkg-desc))))))))
821 (defun package-read-from-string (str)
822 "Read a Lisp expression from STR.
823 Signal an error if the entire string was not used."
824 (let* ((read-data (read-from-string str))
827 ;; The call to `ignore' suppresses a compiler warning.
828 (progn (ignore (read-from-string
829 (substring str (cdr read-data))))
833 (error "Can't read whole string")
836 (defun package--read-archive-file (file)
837 "Re-read archive file FILE, if it exists.
838 Will return the data from the file, or nil if the file does not exist.
839 Will throw an error if the archive version is too new."
840 (let ((filename (expand-file-name file package-user-dir)))
841 (when (file-exists-p filename)
843 (insert-file-contents-literally filename)
844 (let ((contents (read (current-buffer))))
845 (if (> (car contents) package-archive-version)
846 (error "Package archive version %d is higher than %d"
847 (car contents) package-archive-version))
850 (defun package-read-all-archive-contents ()
851 "Re-read `archive-contents', if it exists.
852 If successful, set `package-archive-contents'."
853 (setq package-archive-contents nil)
854 (dolist (archive package-archives)
855 (package-read-archive-contents (car archive))))
857 (defun package-read-archive-contents (archive)
858 "Re-read archive contents for ARCHIVE.
859 If successful, set the variable `package-archive-contents'.
860 If the archive version is too new, signal an error."
861 ;; Version 1 of 'archive-contents' is identical to our internal
863 (let* ((dir (concat "archives/" archive))
864 (contents-file (concat dir "/archive-contents"))
866 (when (setq contents (package--read-archive-file contents-file))
867 (dolist (package contents)
868 (package--add-to-archive-contents package archive)))))
870 (defun package--add-to-archive-contents (package archive)
871 "Add the PACKAGE from the given ARCHIVE if necessary.
872 Also, add the originating archive to the end of the package vector."
873 (let* ((name (car package))
874 (version (package-desc-vers (cdr package)))
876 (vconcat (cdr package) (vector archive))))
877 (existing-package (assq name package-archive-contents))
878 (pinned-to-archive (assoc name package-pinned-packages)))
879 (cond ((and pinned-to-archive
880 ;; If pinned to another archive, skip entirely.
881 (not (equal (cdr pinned-to-archive) archive)))
883 ((not existing-package)
884 (add-to-list 'package-archive-contents entry))
885 ((version-list-< (package-desc-vers (cdr existing-package))
887 ;; Replace the entry with this one.
888 (setq package-archive-contents
890 (delq existing-package
891 package-archive-contents)))))))
893 (defun package-download-transaction (package-list)
894 "Download and install all the packages in PACKAGE-LIST.
895 PACKAGE-LIST should be a list of package names (symbols).
896 This function assumes that all package requirements in
897 PACKAGE-LIST are satisfied, i.e. that PACKAGE-LIST is computed
898 using `package-compute-transaction'."
899 (dolist (elt package-list)
900 (let* ((desc (cdr (assq elt package-archive-contents)))
901 ;; As an exception, if package is "held" in
902 ;; `package-load-list', download the held version.
903 (hold (cadr (assq elt package-load-list)))
904 (v-string (or (and (stringp hold) hold)
905 (package-version-join (package-desc-vers desc))))
906 (kind (package-desc-kind desc)))
909 (package-download-tar elt v-string))
911 (package-download-single elt v-string
912 (package-desc-doc desc)
913 (package-desc-reqs desc)))
915 (error "Unknown package kind: %s" (symbol-name kind))))
916 ;; If package A depends on package B, then A may `require' B
917 ;; during byte compilation. So we need to activate B before
919 (package-maybe-load-descriptor (symbol-name elt) v-string
921 (package-activate elt (version-to-list v-string)))))
924 (defun package-install (name)
925 "Install the package named NAME.
926 NAME should be the name of one of the available packages in an
927 archive in `package-archives'. Interactively, prompt for NAME."
930 ;; Initialize the package system to get the list of package
931 ;; symbols for completion.
932 (unless package--initialized
933 (package-initialize t))
934 (unless package-archive-contents
935 (package-refresh-contents))
936 (list (intern (completing-read
938 (mapcar (lambda (elt)
939 (cons (symbol-name (car elt))
941 package-archive-contents)
943 (let ((pkg-desc (assq name package-archive-contents)))
945 (error "Package `%s' is not available for installation"
947 (package-download-transaction
948 (package-compute-transaction (list name)
949 (package-desc-reqs (cdr pkg-desc))))))
951 (defun package-strip-rcs-id (str)
952 "Strip RCS version ID from the version string STR.
953 If the result looks like a dotted numeric version, return it.
954 Otherwise return nil."
956 (when (string-match "\\`[ \t]*[$]Revision:[ \t]+" str)
957 (setq str (substring str (match-end 0))))
959 (if (version-to-list str)
963 (defun package-buffer-info ()
964 "Return a vector describing the package in the current buffer.
965 The vector has the form
967 [FILENAME REQUIRES DESCRIPTION VERSION COMMENTARY]
969 FILENAME is the file name, a string, sans the \".el\" extension.
970 REQUIRES is a list of requirements, each requirement having the
971 form (NAME VER); NAME is a string and VER is a version list.
972 DESCRIPTION is the package description, a string.
973 VERSION is the version, a string.
974 COMMENTARY is the commentary section, a string, or nil if none.
976 If the buffer does not contain a conforming package, signal an
977 error. If there is a package, narrow the buffer to the file's
979 (goto-char (point-min))
980 (unless (re-search-forward "^;;; \\([^ ]*\\)\\.el ---[ \t]*\\(.*?\\)[ \t]*\\(-\\*-.*-\\*-[ \t]*\\)?$" nil t)
981 (error "Packages lacks a file header"))
982 (let ((file-name (match-string-no-properties 1))
983 (desc (match-string-no-properties 2))
984 (start (line-beginning-position)))
985 (unless (search-forward (concat ";;; " file-name ".el ends here"))
986 (error "Package lacks a terminating comment"))
987 ;; Try to include a trailing newline.
989 (narrow-to-region start (point))
991 ;; Use some headers we've invented to drive the process.
992 (let* ((requires-str (lm-header "package-requires"))
993 (requires (if requires-str
994 (package-read-from-string requires-str)))
995 ;; Prefer Package-Version; if defined, the package author
996 ;; probably wants us to use it. Otherwise try Version.
998 (or (package-strip-rcs-id (lm-header "package-version"))
999 (package-strip-rcs-id (lm-header "version"))))
1000 (commentary (lm-commentary)))
1003 "Package lacks a \"Version\" or \"Package-Version\" header"))
1004 ;; Turn string version numbers into list form.
1009 (version-to-list (car (cdr elt)))))
1011 (vector file-name requires desc pkg-version commentary))))
1013 (defun package-tar-file-info (file)
1014 "Find package information for a tar file.
1015 FILE is the name of the tar file to examine.
1016 The return result is a vector like `package-buffer-info'."
1017 (let ((default-directory (file-name-directory file))
1018 (file (file-name-nondirectory file)))
1019 (unless (string-match (concat "\\`" package-subdirectory-regexp "\\.tar\\'")
1021 (error "Invalid package name `%s'" file))
1022 (let* ((pkg-name (match-string-no-properties 1 file))
1023 (pkg-version (match-string-no-properties 2 file))
1024 ;; Extract the package descriptor.
1025 (pkg-def-contents (shell-command-to-string
1026 ;; Requires GNU tar.
1027 (concat "tar -xOf " file " "
1029 pkg-name "-" pkg-version "/"
1030 pkg-name "-pkg.el")))
1031 (pkg-def-parsed (package-read-from-string pkg-def-contents)))
1032 (unless (eq (car pkg-def-parsed) 'define-package)
1033 (error "No `define-package' sexp is present in `%s-pkg.el'" pkg-name))
1034 (let ((name-str (nth 1 pkg-def-parsed))
1035 (version-string (nth 2 pkg-def-parsed))
1036 (docstring (nth 3 pkg-def-parsed))
1037 (requires (nth 4 pkg-def-parsed))
1038 (readme (shell-command-to-string
1039 ;; Requires GNU tar.
1040 (concat "tar -xOf " file " "
1041 pkg-name "-" pkg-version "/README"))))
1042 (unless (equal pkg-version version-string)
1043 (error "Package has inconsistent versions"))
1044 (unless (equal pkg-name name-str)
1045 (error "Package has inconsistent names"))
1047 (if (string-match ": Not found in archive" readme)
1049 ;; Turn string version numbers into list form.
1050 (if (eq (car requires) 'quote)
1051 (setq requires (car (cdr requires))))
1053 (mapcar (lambda (elt)
1055 (version-to-list (cadr elt))))
1057 (vector pkg-name requires docstring version-string readme)))))
1060 (defun package-install-from-buffer (pkg-info type)
1061 "Install a package from the current buffer.
1062 When called interactively, the current buffer is assumed to be a
1063 single .el file that follows the packaging guidelines; see info
1064 node `(elisp)Packaging'.
1066 When called from Lisp, PKG-INFO is a vector describing the
1067 information, of the type returned by `package-buffer-info'; and
1068 TYPE is the package type (either `single' or `tar')."
1069 (interactive (list (package-buffer-info) 'single))
1072 (let* ((file-name (aref pkg-info 0))
1073 (requires (aref pkg-info 1))
1074 (desc (if (string= (aref pkg-info 2) "")
1075 "No description available."
1077 (pkg-version (aref pkg-info 3)))
1078 ;; Download and install the dependencies.
1079 (let ((transaction (package-compute-transaction nil requires)))
1080 (package-download-transaction transaction))
1081 ;; Install the package itself.
1084 (package-unpack-single file-name pkg-version desc requires))
1086 (package-unpack (intern file-name) pkg-version))
1088 (error "Unknown type: %s" (symbol-name type))))
1089 ;; Try to activate it.
1090 (package-initialize)))))
1093 (defun package-install-file (file)
1094 "Install a package from a file.
1095 The file can either be a tar file or an Emacs Lisp file."
1096 (interactive "fPackage file name: ")
1098 (insert-file-contents-literally file)
1100 ((string-match "\\.el$" file)
1101 (package-install-from-buffer (package-buffer-info) 'single))
1102 ((string-match "\\.tar$" file)
1103 (package-install-from-buffer (package-tar-file-info file) 'tar))
1104 (t (error "Unrecognized extension `%s'" (file-name-extension file))))))
1106 (defun package-delete (name version)
1107 (let ((dir (package--dir name version)))
1108 (if (string-equal (file-name-directory dir)
1109 (file-name-as-directory
1110 (expand-file-name package-user-dir)))
1112 (delete-directory dir t t)
1113 (message "Package `%s-%s' deleted." name version))
1114 ;; Don't delete "system" packages
1115 (error "Package `%s-%s' is a system package, not deleting"
1118 (defun package-archive-base (name)
1119 "Return the archive containing the package NAME."
1120 (let ((desc (cdr (assq (intern-soft name) package-archive-contents))))
1121 (cdr (assoc (aref desc (- (length desc) 1)) package-archives))))
1123 (defun package--download-one-archive (archive file)
1124 "Retrieve an archive file FILE from ARCHIVE, and cache it.
1125 ARCHIVE should be a cons cell of the form (NAME . LOCATION),
1126 similar to an entry in `package-alist'. Save the cached copy to
1127 \"archives/NAME/archive-contents\" in `package-user-dir'."
1128 (let* ((dir (expand-file-name "archives" package-user-dir))
1129 (dir (expand-file-name (car archive) dir)))
1130 (package--with-work-buffer (cdr archive) file
1131 ;; Read the retrieved buffer to make sure it is valid (e.g. it
1132 ;; may fetch a URL redirect page).
1133 (when (listp (read buffer))
1134 (make-directory dir t)
1135 (setq buffer-file-name (expand-file-name file dir))
1136 (let ((version-control 'never))
1140 (defun package-refresh-contents ()
1141 "Download the ELPA archive description if needed.
1142 This informs Emacs about the latest versions of all packages, and
1143 makes them available for download."
1145 (unless (file-exists-p package-user-dir)
1146 (make-directory package-user-dir t))
1147 (dolist (archive package-archives)
1148 (condition-case-unless-debug nil
1149 (package--download-one-archive archive "archive-contents")
1150 (error (message "Failed to download `%s' archive."
1152 (package-read-all-archive-contents))
1155 (defun package-initialize (&optional no-activate)
1156 "Load Emacs Lisp packages, and activate them.
1157 The variable `package-load-list' controls which packages to load.
1158 If optional arg NO-ACTIVATE is non-nil, don't activate packages."
1160 (setq package-alist nil
1161 package-obsolete-alist nil)
1162 (package-load-all-descriptors)
1163 (package-read-all-archive-contents)
1165 (dolist (elt package-alist)
1166 (package-activate (car elt) (package-desc-vers (cdr elt)))))
1167 (setq package--initialized t))
1170 ;;;; Package description buffer.
1173 (defun describe-package (package)
1174 "Display the full documentation of PACKAGE (a symbol)."
1176 (let* ((guess (function-called-at-point))
1178 (require 'finder-inf nil t)
1179 ;; Load the package list if necessary (but don't activate them).
1180 (unless package--initialized
1181 (package-initialize t))
1182 (setq packages (append (mapcar 'car package-alist)
1183 (mapcar 'car package-archive-contents)
1184 (mapcar 'car package--builtins)))
1185 (unless (memq guess packages)
1187 (setq packages (mapcar 'symbol-name packages))
1189 (completing-read (if guess
1190 (format "Describe package (default %s): "
1192 "Describe package: ")
1193 packages nil t nil nil guess))
1194 (list (if (equal val "") guess (intern val)))))
1195 (if (or (null package) (not (symbolp package)))
1196 (message "No package specified")
1197 (help-setup-xref (list #'describe-package package)
1198 (called-interactively-p 'interactive))
1199 (with-help-window (help-buffer)
1200 (with-current-buffer standard-output
1201 (describe-package-1 package)))))
1203 (defun describe-package-1 (package)
1205 (let ((package-name (symbol-name package))
1206 (built-in (assq package package--builtins))
1207 desc pkg-dir reqs version installable archive)
1211 ;; Loaded packages are in `package-alist'.
1212 ((setq desc (cdr (assq package package-alist)))
1213 (setq version (package-version-join (package-desc-vers desc)))
1214 (if (setq pkg-dir (package--dir package-name version))
1215 (insert "an installed package.\n\n")
1216 ;; This normally does not happen.
1217 (insert "a deleted package.\n\n")))
1218 ;; Available packages are in `package-archive-contents'.
1219 ((setq desc (cdr (assq package package-archive-contents)))
1220 (setq version (package-version-join (package-desc-vers desc))
1221 archive (aref desc (- (length desc) 1))
1224 (insert "a built-in package.\n\n")
1225 (insert "an uninstalled package.\n\n")))
1227 (setq desc (cdr built-in)
1228 version (package-version-join (package-desc-vers desc)))
1229 (insert "a built-in package.\n\n"))
1231 (insert "an orphan package.\n\n")))
1233 (insert " " (propertize "Status" 'font-lock-face 'bold) ": ")
1235 (insert (propertize "Installed"
1236 'font-lock-face 'font-lock-comment-face))
1238 ;; Todo: Add button for uninstalling.
1239 (help-insert-xref-button (file-name-as-directory pkg-dir)
1240 'help-package-def pkg-dir)
1242 (insert "',\n shadowing a "
1243 (propertize "built-in package"
1244 'font-lock-face 'font-lock-builtin-face)
1249 (insert (propertize "Built-in." 'font-lock-face 'font-lock-builtin-face)
1250 " Alternate version available")
1251 (insert "Available"))
1252 (insert " from " archive)
1254 (let ((button-text (if (display-graphic-p) "Install" "[Install]"))
1255 (button-face (if (display-graphic-p)
1256 '(:box (:line-width 2 :color "dark grey")
1257 :background "light grey"
1258 :foreground "black")
1260 (insert-text-button button-text 'face button-face 'follow-link t
1261 'package-symbol package
1262 'action 'package-install-button-action)))
1264 (insert (propertize "Built-in." 'font-lock-face 'font-lock-builtin-face)))
1265 (t (insert "Deleted.")))
1267 (and version (> (length version) 0)
1269 (propertize "Version" 'font-lock-face 'bold) ": " version "\n"))
1271 (setq reqs (if desc (package-desc-reqs desc)))
1273 (insert " " (propertize "Requires" 'font-lock-face 'bold) ": ")
1277 (setq name (car req)
1279 text (format "%s-%s" (symbol-name name)
1280 (package-version-join vers)))
1281 (cond (first (setq first nil))
1282 ((>= (+ 2 (current-column) (length text))
1286 (help-insert-xref-button text 'help-package name))
1288 (insert " " (propertize "Summary" 'font-lock-face 'bold)
1289 ": " (if desc (package-desc-doc desc)) "\n\n")
1292 ;; For built-in packages, insert the commentary.
1293 (let ((fn (locate-file (concat package-name ".el") load-path
1294 load-file-rep-suffixes))
1296 (insert (or (lm-commentary fn) ""))
1299 (when (re-search-forward "^;;; Commentary:\n" nil t)
1301 (while (re-search-forward "^\\(;+ ?\\)" nil t)
1302 (replace-match ""))))
1303 (let ((readme (expand-file-name (concat package-name "-readme.txt")
1306 ;; For elpa packages, try downloading the commentary. If that
1307 ;; fails, try an existing readme file in `package-user-dir'.
1308 (cond ((condition-case nil
1309 (package--with-work-buffer (package-archive-base package)
1310 (concat package-name "-readme.txt")
1311 (setq buffer-file-name
1312 (expand-file-name readme package-user-dir))
1313 (let ((version-control 'never))
1315 (setq readme-string (buffer-string))
1318 (insert readme-string))
1319 ((file-readable-p readme)
1320 (insert-file-contents readme)
1321 (goto-char (point-max))))))))
1323 (defun package-install-button-action (button)
1324 (let ((package (button-get button 'package-symbol)))
1325 (when (y-or-n-p (format "Install package `%s'? " package))
1326 (package-install package)
1327 (revert-buffer nil t)
1328 (goto-char (point-min)))))
1331 ;;;; Package menu mode.
1333 (defvar package-menu-mode-map
1334 (let ((map (make-sparse-keymap))
1335 (menu-map (make-sparse-keymap "Package")))
1336 (set-keymap-parent map tabulated-list-mode-map)
1337 (define-key map "\C-m" 'package-menu-describe-package)
1338 (define-key map "u" 'package-menu-mark-unmark)
1339 (define-key map "\177" 'package-menu-backup-unmark)
1340 (define-key map "d" 'package-menu-mark-delete)
1341 (define-key map "i" 'package-menu-mark-install)
1342 (define-key map "U" 'package-menu-mark-upgrades)
1343 (define-key map "r" 'package-menu-refresh)
1344 (define-key map "~" 'package-menu-mark-obsolete-for-deletion)
1345 (define-key map "x" 'package-menu-execute)
1346 (define-key map "h" 'package-menu-quick-help)
1347 (define-key map "?" 'package-menu-describe-package)
1348 (define-key map [menu-bar package-menu] (cons "Package" menu-map))
1349 (define-key menu-map [mq]
1350 '(menu-item "Quit" quit-window
1351 :help "Quit package selection"))
1352 (define-key menu-map [s1] '("--"))
1353 (define-key menu-map [mn]
1354 '(menu-item "Next" next-line
1356 (define-key menu-map [mp]
1357 '(menu-item "Previous" previous-line
1358 :help "Previous Line"))
1359 (define-key menu-map [s2] '("--"))
1360 (define-key menu-map [mu]
1361 '(menu-item "Unmark" package-menu-mark-unmark
1362 :help "Clear any marks on a package and move to the next line"))
1363 (define-key menu-map [munm]
1364 '(menu-item "Unmark Backwards" package-menu-backup-unmark
1365 :help "Back up one line and clear any marks on that package"))
1366 (define-key menu-map [md]
1367 '(menu-item "Mark for Deletion" package-menu-mark-delete
1368 :help "Mark a package for deletion and move to the next line"))
1369 (define-key menu-map [mi]
1370 '(menu-item "Mark for Install" package-menu-mark-install
1371 :help "Mark a package for installation and move to the next line"))
1372 (define-key menu-map [mupgrades]
1373 '(menu-item "Mark Upgradable Packages" package-menu-mark-upgrades
1374 :help "Mark packages that have a newer version for upgrading"))
1375 (define-key menu-map [s3] '("--"))
1376 (define-key menu-map [mg]
1377 '(menu-item "Update Package List" revert-buffer
1378 :help "Update the list of packages"))
1379 (define-key menu-map [mr]
1380 '(menu-item "Refresh Package List" package-menu-refresh
1381 :help "Download the ELPA archive"))
1382 (define-key menu-map [s4] '("--"))
1383 (define-key menu-map [mt]
1384 '(menu-item "Mark Obsolete Packages" package-menu-mark-obsolete-for-deletion
1385 :help "Mark all obsolete packages for deletion"))
1386 (define-key menu-map [mx]
1387 '(menu-item "Execute Actions" package-menu-execute
1388 :help "Perform all the marked actions"))
1389 (define-key menu-map [s5] '("--"))
1390 (define-key menu-map [mh]
1391 '(menu-item "Help" package-menu-quick-help
1392 :help "Show short key binding help for package-menu-mode"))
1393 (define-key menu-map [mc]
1394 '(menu-item "View Commentary" package-menu-view-commentary
1395 :help "Display information about this package"))
1397 "Local keymap for `package-menu-mode' buffers.")
1399 (defvar package-menu--new-package-list nil
1400 "List of newly-available packages since `list-packages' was last called.")
1402 (define-derived-mode package-menu-mode tabulated-list-mode "Package Menu"
1403 "Major mode for browsing a list of packages.
1404 Letters do not insert themselves; instead, they are commands.
1405 \\<package-menu-mode-map>
1406 \\{package-menu-mode-map}"
1407 (setq tabulated-list-format [("Package" 18 package-menu--name-predicate)
1409 ("Status" 10 package-menu--status-predicate)
1410 ("Description" 0 nil)])
1411 (setq tabulated-list-padding 2)
1412 (setq tabulated-list-sort-key (cons "Status" nil))
1413 (tabulated-list-init-header))
1415 (defmacro package--push (package desc status listname)
1416 "Convenience macro for `package-menu--generate'.
1417 If the alist stored in the symbol LISTNAME lacks an entry for a
1418 package PACKAGE with descriptor DESC, add one. The alist is
1419 keyed with cons cells (PACKAGE . VERSION-LIST), where PACKAGE is
1420 a symbol and VERSION-LIST is a version list."
1421 `(let* ((version (package-desc-vers ,desc))
1422 (key (cons ,package version)))
1423 (unless (assoc key ,listname)
1424 (push (list key ,status (package-desc-doc ,desc)) ,listname))))
1426 (defun package-menu--generate (remember-pos packages)
1427 "Populate the Package Menu.
1428 If REMEMBER-POS is non-nil, keep point on the same entry.
1429 PACKAGES should be t, which means to display all known packages,
1430 or a list of package names (symbols) to display."
1431 ;; Construct list of ((PACKAGE . VERSION) STATUS DESCRIPTION).
1432 (let (info-list name)
1433 ;; Installed packages:
1434 (dolist (elt package-alist)
1435 (setq name (car elt))
1436 (when (or (eq packages t) (memq name packages))
1437 (package--push name (cdr elt)
1438 (if (stringp (cadr (assq name package-load-list)))
1442 ;; Built-in packages:
1443 (dolist (elt package--builtins)
1444 (setq name (car elt))
1445 (when (and (not (eq name 'emacs)) ; Hide the `emacs' package.
1446 (or (eq packages t) (memq name packages)))
1447 (package--push name (cdr elt) "built-in" info-list)))
1449 ;; Available and disabled packages:
1450 (dolist (elt package-archive-contents)
1451 (setq name (car elt))
1452 (when (or (eq packages t) (memq name packages))
1453 (let ((hold (assq name package-load-list)))
1454 (package--push name (cdr elt)
1456 ((and hold (null (cadr hold))) "disabled")
1457 ((memq name package-menu--new-package-list) "new")
1461 ;; Obsolete packages:
1462 (dolist (elt package-obsolete-alist)
1463 (dolist (inner-elt (cdr elt))
1464 (when (or (eq packages t) (memq (car elt) packages))
1465 (package--push (car elt) (cdr inner-elt) "obsolete" info-list))))
1467 ;; Print the result.
1468 (setq tabulated-list-entries (mapcar 'package-menu--print-info info-list))
1469 (tabulated-list-print remember-pos)))
1471 (defun package-menu--print-info (pkg)
1472 "Return a package entry suitable for `tabulated-list-entries'.
1473 PKG has the form ((PACKAGE . VERSION) STATUS DOC).
1474 Return (KEY [NAME VERSION STATUS DOC]), where KEY is the
1475 identifier (NAME . VERSION-LIST)."
1476 (let* ((package (caar pkg))
1477 (version (cdr (car pkg)))
1478 (status (nth 1 pkg))
1479 (doc (or (nth 2 pkg) ""))
1481 ((string= status "built-in") 'font-lock-builtin-face)
1482 ((string= status "available") 'default)
1483 ((string= status "new") 'bold)
1484 ((string= status "held") 'font-lock-constant-face)
1485 ((string= status "disabled") 'font-lock-warning-face)
1486 ((string= status "installed") 'font-lock-comment-face)
1487 (t 'font-lock-warning-face)))) ; obsolete.
1488 (list (cons package version)
1489 (vector (list (symbol-name package)
1492 'package-symbol package
1493 'action 'package-menu-describe-package)
1494 (propertize (package-version-join version)
1495 'font-lock-face face)
1496 (propertize status 'font-lock-face face)
1497 (propertize doc 'font-lock-face face)))))
1499 (defun package-menu-refresh ()
1500 "Download the Emacs Lisp package archive.
1501 This fetches the contents of each archive specified in
1502 `package-archives', and then refreshes the package menu."
1504 (unless (derived-mode-p 'package-menu-mode)
1505 (error "The current buffer is not a Package Menu"))
1506 (package-refresh-contents)
1507 (package-menu--generate t t))
1509 (defun package-menu-describe-package (&optional button)
1510 "Describe the current package.
1511 If optional arg BUTTON is non-nil, describe its associated package."
1513 (let ((package (if button (button-get button 'package-symbol)
1514 (car (tabulated-list-get-id)))))
1516 (describe-package package))))
1518 ;; fixme numeric argument
1519 (defun package-menu-mark-delete (&optional _num)
1520 "Mark a package for deletion and move to the next line."
1522 (if (member (package-menu-get-status) '("installed" "obsolete"))
1523 (tabulated-list-put-tag "D" t)
1526 (defun package-menu-mark-install (&optional _num)
1527 "Mark a package for installation and move to the next line."
1529 (if (member (package-menu-get-status) '("available" "new"))
1530 (tabulated-list-put-tag "I" t)
1533 (defun package-menu-mark-unmark (&optional _num)
1534 "Clear any marks on a package and move to the next line."
1536 (tabulated-list-put-tag " " t))
1538 (defun package-menu-backup-unmark ()
1539 "Back up one line and clear any marks on that package."
1542 (tabulated-list-put-tag " "))
1544 (defun package-menu-mark-obsolete-for-deletion ()
1545 "Mark all obsolete packages for deletion."
1548 (goto-char (point-min))
1550 (if (equal (package-menu-get-status) "obsolete")
1551 (tabulated-list-put-tag "D" t)
1552 (forward-line 1)))))
1554 (defun package-menu-quick-help ()
1555 "Show short key binding help for package-menu-mode."
1557 (message "n-ext, i-nstall, d-elete, u-nmark, x-ecute, r-efresh, h-elp"))
1559 (define-obsolete-function-alias
1560 'package-menu-view-commentary 'package-menu-describe-package "24.1")
1562 (defun package-menu-get-status ()
1563 (let* ((pkg (tabulated-list-get-id))
1564 (entry (and pkg (assq pkg tabulated-list-entries))))
1566 (aref (cadr entry) 2)
1569 (defun package-menu--find-upgrades ()
1570 (let (installed available upgrades)
1571 ;; Build list of installed/available packages in this buffer.
1572 (dolist (entry tabulated-list-entries)
1573 ;; ENTRY is ((NAME . VERSION) [NAME VERSION STATUS DOC])
1574 (let ((pkg (car entry))
1575 (status (aref (cadr entry) 2)))
1576 (cond ((equal status "installed")
1577 (push pkg installed))
1578 ((member status '("available" "new"))
1579 (push pkg available)))))
1580 ;; Loop through list of installed packages, finding upgrades
1581 (dolist (pkg installed)
1582 (let ((avail-pkg (assq (car pkg) available)))
1584 (version-list-< (cdr pkg) (cdr avail-pkg))
1585 (push avail-pkg upgrades))))
1588 (defun package-menu-mark-upgrades ()
1589 "Mark all upgradable packages in the Package Menu.
1590 For each installed package with a newer version available, place
1591 an (I)nstall flag on the available version and a (D)elete flag on
1592 the installed version. A subsequent \\[package-menu-execute]
1593 call will upgrade the package."
1595 (unless (derived-mode-p 'package-menu-mode)
1596 (error "The current buffer is not a Package Menu"))
1597 (let ((upgrades (package-menu--find-upgrades)))
1599 (message "No packages to upgrade.")
1602 (goto-char (point-min))
1604 (let* ((pkg (tabulated-list-get-id))
1605 (upgrade (assq (car pkg) upgrades)))
1606 (cond ((null upgrade)
1608 ((equal pkg upgrade)
1609 (package-menu-mark-install))
1611 (package-menu-mark-delete))))))
1612 (message "%d package%s marked for upgrading."
1614 (if (= (length upgrades) 1) "" "s")))))
1616 (defun package-menu-execute (&optional noquery)
1617 "Perform marked Package Menu actions.
1618 Packages marked for installation are downloaded and installed;
1619 packages marked for deletion are removed.
1620 Optional argument NOQUERY non-nil means do not ask the user to confirm."
1622 (unless (derived-mode-p 'package-menu-mode)
1623 (error "The current buffer is not in Package Menu mode"))
1624 (let (install-list delete-list cmd id)
1626 (goto-char (point-min))
1628 (setq cmd (char-after))
1629 (unless (eq cmd ?\s)
1630 ;; This is the key (PACKAGE . VERSION-LIST).
1631 (setq id (tabulated-list-get-id))
1633 (push (cons (symbol-name (car id))
1634 (package-version-join (cdr id)))
1637 (push (car id) install-list))))
1643 (if (= (length install-list) 1)
1644 (format "Install package `%s'? " (car install-list))
1645 (format "Install these %d packages (%s)? "
1646 (length install-list)
1647 (mapconcat 'symbol-name install-list ", ")))))
1648 (mapc 'package-install install-list)))
1649 ;; Delete packages, prompting if necessary.
1654 (if (= (length delete-list) 1)
1655 (format "Delete package `%s-%s'? "
1657 (cdr (car delete-list)))
1658 (format "Delete these %d packages (%s)? "
1659 (length delete-list)
1660 (mapconcat (lambda (elt)
1661 (concat (car elt) "-" (cdr elt)))
1664 (dolist (elt delete-list)
1665 (condition-case-unless-debug err
1666 (package-delete (car elt) (cdr elt))
1667 (error (message (cadr err)))))
1669 ;; If we deleted anything, regenerate `package-alist'. This is done
1670 ;; automatically if we installed a package.
1671 (and delete-list (null install-list)
1672 (package-initialize))
1673 (if (or delete-list install-list)
1674 (package-menu--generate t t)
1675 (message "No operations specified."))))
1677 (defun package-menu--version-predicate (A B)
1678 (let ((vA (or (aref (cadr A) 1) '(0)))
1679 (vB (or (aref (cadr B) 1) '(0))))
1680 (if (version-list-= vA vB)
1681 (package-menu--name-predicate A B)
1682 (version-list-< vA vB))))
1684 (defun package-menu--status-predicate (A B)
1685 (let ((sA (aref (cadr A) 2))
1686 (sB (aref (cadr B) 2)))
1687 (cond ((string= sA sB)
1688 (package-menu--name-predicate A B))
1689 ((string= sA "new") t)
1690 ((string= sB "new") nil)
1691 ((string= sA "available") t)
1692 ((string= sB "available") nil)
1693 ((string= sA "installed") t)
1694 ((string= sB "installed") nil)
1695 ((string= sA "held") t)
1696 ((string= sB "held") nil)
1697 ((string= sA "built-in") t)
1698 ((string= sB "built-in") nil)
1699 ((string= sA "obsolete") t)
1700 ((string= sB "obsolete") nil)
1701 (t (string< sA sB)))))
1703 (defun package-menu--description-predicate (A B)
1704 (let ((dA (aref (cadr A) 3))
1705 (dB (aref (cadr B) 3)))
1707 (package-menu--name-predicate A B)
1710 (defun package-menu--name-predicate (A B)
1711 (string< (symbol-name (caar A))
1712 (symbol-name (caar B))))
1715 (defun list-packages (&optional no-fetch)
1716 "Display a list of packages.
1717 This first fetches the updated list of packages before
1718 displaying, unless a prefix argument NO-FETCH is specified.
1719 The list is displayed in a buffer named `*Packages*'."
1721 (require 'finder-inf nil t)
1722 ;; Initialize the package system if necessary.
1723 (unless package--initialized
1724 (package-initialize t))
1725 (let (old-archives new-packages)
1727 ;; Read the locally-cached archive-contents.
1728 (package-read-all-archive-contents)
1729 (setq old-archives package-archive-contents)
1730 ;; Fetch the remote list of packages.
1731 (package-refresh-contents)
1732 ;; Find which packages are new.
1733 (dolist (elt package-archive-contents)
1734 (unless (assq (car elt) old-archives)
1735 (push (car elt) new-packages))))
1737 ;; Generate the Package Menu.
1738 (let ((buf (get-buffer-create "*Packages*")))
1739 (with-current-buffer buf
1741 (set (make-local-variable 'package-menu--new-package-list)
1743 (package-menu--generate nil t))
1744 ;; The package menu buffer has keybindings. If the user types
1745 ;; `M-x list-packages', that suggests it should become current.
1746 (switch-to-buffer buf))
1748 (let ((upgrades (package-menu--find-upgrades)))
1750 (message "%d package%s can be upgraded; type `%s' to mark %s for upgrading."
1752 (if (= (length upgrades) 1) "" "s")
1753 (substitute-command-keys "\\[package-menu-mark-upgrades]")
1754 (if (= (length upgrades) 1) "it" "them"))))))
1757 (defalias 'package-list-packages 'list-packages)
1759 ;; Used in finder.el
1760 (defun package-show-package-list (packages)
1761 "Display PACKAGES in a *Packages* buffer.
1762 This is similar to `list-packages', but it does not fetch the
1763 updated list of packages, and it only displays packages with
1764 names in PACKAGES (which should be a list of symbols)."
1765 (require 'finder-inf nil t)
1766 (let ((buf (get-buffer-create "*Packages*")))
1767 (with-current-buffer buf
1769 (package-menu--generate nil packages))
1770 (switch-to-buffer buf)))
1772 (defun package-list-packages-no-fetch ()
1773 "Display a list of packages.
1774 Does not fetch the updated list of packages before displaying.
1775 The list is displayed in a buffer named `*Packages*'."
1781 ;;; package.el ends here