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
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
27 ;; 2 Apr 2007 - now using ChangeLog file
28 ;; 15 Mar 2007 - updated documentation
29 ;; 14 Mar 2007 - Changed how obsolete packages are handled
30 ;; 13 Mar 2007 - Wrote package-install-from-buffer
31 ;; 12 Mar 2007 - Wrote package-menu mode
35 ;; The idea behind package.el is to be able to download packages and
36 ;; install them. Packages are versioned and have versioned
37 ;; dependencies. Furthermore, this supports built-in packages which
38 ;; may or may not be newer than user-specified packages. This makes
39 ;; it possible to upgrade Emacs and automatically disable packages
40 ;; which have moved from external to core. (Note though that we don't
41 ;; currently register any of these, so this feature does not actually
44 ;; A package is described by its name and version. The distribution
45 ;; format is either a tar file or a single .el file.
47 ;; A tar file should be named "NAME-VERSION.tar". The tar file must
48 ;; unpack into a directory named after the package and version:
49 ;; "NAME-VERSION". It must contain a file named "PACKAGE-pkg.el"
50 ;; which consists of a call to define-package. It may also contain a
51 ;; "dir" file and the info files it references.
53 ;; A .el file is named "NAME-VERSION.el" in the remote archive, but is
54 ;; installed as simply "NAME.el" in a directory named "NAME-VERSION".
56 ;; The downloader downloads all dependent packages. By default,
57 ;; packages come from the official GNU sources, but others may be
58 ;; added by customizing the `package-archives' alist. Packages get
59 ;; byte-compiled at install time.
61 ;; At activation time we will set up the load-path and the info path,
62 ;; and we will load the package's autoloads. If a package's
63 ;; dependencies are not available, we will not activate that package.
65 ;; Conceptually a package has multiple state transitions:
67 ;; * Download. Fetching the package from ELPA.
68 ;; * Install. Untar the package, or write the .el file, into
69 ;; ~/.emacs.d/elpa/ directory.
70 ;; * Byte compile. Currently this phase is done during install,
71 ;; but we may change this.
72 ;; * Activate. Evaluate the autoloads for the package to make it
73 ;; available to the user.
74 ;; * Load. Actually load the package and run some code from it.
76 ;; Other external functions you may want to use:
79 ;; Enters a mode similar to buffer-menu which lets you manage
80 ;; packages. You can choose packages for install (mark with "i",
81 ;; then "x" to execute) or deletion (not implemented yet), and you
82 ;; can see what packages are available. This will automatically
83 ;; fetch the latest list of packages from ELPA.
85 ;; M-x package-install-from-buffer
86 ;; Install a package consisting of a single .el file that appears
87 ;; in the current buffer. This only works for packages which
88 ;; define a Version header properly; package.el also supports the
89 ;; extension headers Package-Version (in case Version is an RCS id
90 ;; or similar), and Package-Requires (if the package requires other
93 ;; M-x package-install-file
94 ;; Install a package from the indicated file. The package can be
95 ;; either a tar file or a .el file. A tar file must contain an
96 ;; appropriately-named "-pkg.el" file; a .el file must be properly
97 ;; formatted as with package-install-from-buffer.
100 ;;; (sorted by sort-lines):
102 ;; Jim Blandy <jimb@red-bean.com>
103 ;; Karl Fogel <kfogel@red-bean.com>
104 ;; Kevin Ryde <user42@zip.com.au>
106 ;; Michael Olson <mwolson@member.fsf.org>
107 ;; Sebastian Tennant <sebyte@smolny.plus.com>
108 ;; Stefan Monnier <monnier@iro.umontreal.ca>
109 ;; Vinicius Jose Latorre <viniciusjl@ig.com.br>
110 ;; Phil Hagelberg <phil@hagelb.org>
114 ;; - a trust mechanism, since compiling a package can run arbitrary code.
115 ;; For example, download package signatures and check that they match.
116 ;; - putting info dirs at the start of the info path means
117 ;; users see a weird ordering of categories. OTOH we want to
118 ;; override later entries. maybe emacs needs to enforce
119 ;; the standard layout?
120 ;; - put bytecode in a separate directory tree
121 ;; - perhaps give users a way to recompile their bytecode
122 ;; or do it automatically when emacs changes
123 ;; - give users a way to know whether a package is installed ok
124 ;; - give users a way to view a package's documentation when it
125 ;; only appears in the .el
126 ;; - use/extend checkdoc so people can tell if their package will work
127 ;; - "installed" instead of a blank in the status column
128 ;; - tramp needs its files to be compiled in a certain order.
129 ;; how to handle this? fix tramp?
130 ;; - on emacs 21 we don't kill the -autoloads.el buffer. what about 22?
131 ;; - maybe we need separate .elc directories for various emacs versions
132 ;; and also emacs-vs-xemacs. That way conditional compilation can
133 ;; work. But would this break anything?
134 ;; - should store the package's keywords in archive-contents, then
135 ;; let the users filter the package-menu by keyword. See
136 ;; finder-by-keyword. (We could also let people view the
137 ;; Commentary, but it isn't clear how useful this is.)
138 ;; - William Xu suggests being able to open a package file without
140 ;; - Interface with desktop.el so that restarting after an install
142 ;; - Implement M-x package-upgrade, to upgrade any/all existing packages
143 ;; - Use hierarchical layout. PKG/etc PKG/lisp PKG/info
144 ;; ... except maybe lisp?
145 ;; - It may be nice to have a macro that expands to the package's
146 ;; private data dir, aka ".../etc". Or, maybe data-directory
147 ;; needs to be a list (though this would be less nice)
148 ;; a few packages want this, eg sokoban
149 ;; - package menu needs:
150 ;; ability to know which packages are built-in & thus not deletable
151 ;; it can sometimes print odd results, like 0.3 available but 0.4 active
153 ;; - Allow multiple versions on the server...?
155 ;; - Don't install a package which will invalidate dependencies overall
156 ;; - Allow something like (or (>= emacs 21.0) (>= xemacs 21.5))
157 ;; [ currently thinking, why bother.. KISS ]
158 ;; - Allow optional package dependencies
159 ;; then if we require 'bbdb', bbdb-specific lisp in lisp/bbdb
160 ;; and just don't compile to add to load path ...?
161 ;; - Have a list of archive URLs? [ maybe there's no point ]
162 ;; - David Kastrup pointed out on the xemacs list that for GPL it
163 ;; is friendlier to ship the source tree. We could "support" that
164 ;; by just having a "src" subdir in the package. This isn't ideal
165 ;; but it probably is not worth trying to support random source
166 ;; tree layouts, build schemes, etc.
167 ;; - Our treatment of the info path is somewhat bogus
168 ;; - perhaps have an "unstable" tree in ELPA as well as a stable one
172 (require 'tabulated-list
)
174 (defgroup package nil
175 "Manager for Emacs Lisp packages."
180 (defcustom package-enable-at-startup t
181 "Whether to activate installed packages when Emacs starts.
182 If non-nil, packages are activated after reading the init file
183 and before `after-init-hook'. Activation is not done if
184 `user-init-file' is nil (e.g. Emacs was started with \"-q\").
186 Even if the value is nil, you can type \\[package-initialize] to
187 activate the package system at any time."
192 (defcustom package-load-list
'(all)
193 "List of packages for `package-initialize' to load.
194 Each element in this list should be a list (NAME VERSION), or the
195 symbol `all'. The symbol `all' says to load the latest installed
196 versions of all packages not specified by other elements.
198 For an element (NAME VERSION), NAME is a package name (a symbol).
199 VERSION should be t, a string, or nil.
200 If VERSION is t, all versions are loaded, though obsolete ones
201 will be put in `package-obsolete-alist' and not activated.
202 If VERSION is a string, only that version is ever loaded.
203 Any other version, even if newer, is silently ignored.
204 Hence, the package is \"held\" at that version.
205 If VERSION is nil, the package is not loaded (it is \"disabled\")."
206 :type
'(repeat symbol
)
211 (defvar Info-directory-list
)
212 (declare-function info-initialize
"info" ())
213 (declare-function url-http-parse-response
"url-http" ())
214 (declare-function lm-header
"lisp-mnt" (header))
215 (declare-function lm-commentary
"lisp-mnt" (&optional file
))
216 (defvar url-http-end-of-headers
)
218 (defcustom package-archives
'(("gnu" .
"http://elpa.gnu.org/packages/"))
219 "An alist of archives from which to fetch.
220 The default value points to the GNU Emacs package repository.
222 Each element has the form (ID . LOCATION).
223 ID is an archive name, as a string.
224 LOCATION specifies the base location for the archive.
225 If it starts with \"http:\", it is treated as a HTTP URL;
226 otherwise it should be an absolute directory name.
227 (Other types of URL are currently not supported.)
229 Only add locations that you trust, since fetching and installing
230 a package can run arbitrary code."
231 :type
'(alist :key-type
(string :tag
"Archive name")
232 :value-type
(string :tag
"URL or directory name"))
237 (defconst package-archive-version
1
238 "Version number of the package archive understood by this file.
239 Lower version numbers than this will probably be understood as well.")
241 (defconst package-el-version
"1.0"
242 "Version of package.el.")
244 ;; We don't prime the cache since it tends to get out of date.
245 (defvar package-archive-contents nil
246 "Cache of the contents of the Emacs Lisp Package Archive.
247 This is an alist mapping package names (symbols) to package
248 descriptor vectors. These are like the vectors for `package-alist'
249 but have extra entries: one which is 'tar for tar packages and
250 'single for single-file packages, and one which is the name of
251 the archive from which it came.")
252 (put 'package-archive-contents
'risky-local-variable t
)
254 (defcustom package-user-dir
(locate-user-emacs-file "elpa")
255 "Directory containing the user's Emacs Lisp packages.
256 The directory name should be absolute.
257 Apart from this directory, Emacs also looks for system-wide
258 packages in `package-directory-list'."
264 (defcustom package-directory-list
265 ;; Defaults are subdirs named "elpa" in the site-lisp dirs.
267 (dolist (f load-path
)
269 (equal (file-name-nondirectory f
) "site-lisp")
270 (push (expand-file-name "elpa" f
) result
)))
272 "List of additional directories containing Emacs Lisp packages.
273 Each directory name should be absolute.
275 These directories contain packages intended for system-wide; in
276 contrast, `package-user-dir' contains packages for personal use."
277 :type
'(repeat directory
)
282 ;; The value is precomputed in finder-inf.el, but don't load that
283 ;; until it's needed (i.e. when `package-initialize' is called).
284 (defvar package--builtins nil
285 "Alist of built-in packages.
286 The actual value is initialized by loading the library
287 `finder-inf'; this is not done until it is needed, e.g. by the
288 function `package-built-in-p'.
290 Each element has the form (PKG . DESC), where PKG is a package
291 name (a symbol) and DESC is a vector that describes the package.
292 The vector DESC has the form [VERSION-LIST REQS DOCSTRING].
293 VERSION-LIST is a version list.
294 REQS is a list of packages required by the package, each
295 requirement having the form (NAME VL), where NAME is a string
296 and VL is a version list.
297 DOCSTRING is a brief description of the package.")
298 (put 'package--builtins
'risky-local-variable t
)
300 (defvar package-alist nil
301 "Alist of all packages available for activation.
302 Each element has the form (PKG . DESC), where PKG is a package
303 name (a symbol) and DESC is a vector that describes the package.
305 The vector DESC has the form [VERSION-LIST REQS DOCSTRING].
306 VERSION-LIST is a version list.
307 REQS is a list of packages required by the package, each
308 requirement having the form (NAME VL) where NAME is a string
309 and VL is a version list.
310 DOCSTRING is a brief description of the package.
312 This variable is set automatically by `package-load-descriptor',
313 called via `package-initialize'. To change which packages are
314 loaded and/or activated, customize `package-load-list'.")
315 (put 'package-alist
'risky-local-variable t
)
317 (defvar package-activated-list nil
318 "List of the names of currently activated packages.")
319 (put 'package-activated-list
'risky-local-variable t
)
321 (defvar package-obsolete-alist nil
322 "Representation of obsolete packages.
323 Like `package-alist', but maps package name to a second alist.
324 The inner alist is keyed by version.")
325 (put 'package-obsolete-alist
'risky-local-variable t
)
327 (defun package-version-join (vlist)
328 "Return the version string corresponding to the list VLIST.
329 This is, approximately, the inverse of `version-to-list'.
330 \(Actually, it returns only one of the possible inverses, since
331 `version-to-list' is a many-to-one operation.)"
334 (let ((str-list (list "." (int-to-string (car vlist
)))))
335 (dolist (num (cdr vlist
))
338 (push (int-to-string num
) str-list
)
341 (error "Invalid version list `%s'" vlist
))
343 ;; pre, or beta, or alpha
344 (cond ((equal "." (car str-list
))
346 ((not (string-match "[0-9]+" (car str-list
)))
347 (error "Invalid version list `%s'" vlist
)))
348 (push (cond ((= num -
1) "pre")
350 ((= num -
3) "alpha"))
352 (if (equal "." (car str-list
))
354 (apply 'concat
(nreverse str-list
)))))
356 (defun package-strip-version (dirname)
357 "Strip the version from a combined package name and version.
358 E.g., if given \"quux-23.0\", will return \"quux\""
359 (if (string-match (concat "\\`" package-subdirectory-regexp
"\\'") dirname
)
360 (match-string 1 dirname
)))
362 (defun package-load-descriptor (dir package
)
363 "Load the description file in directory DIR for package PACKAGE.
364 Here, PACKAGE is a string of the form NAME-VERSION, where NAME is
365 the package name and VERSION is its version."
366 (let* ((pkg-dir (expand-file-name package dir
))
367 (pkg-file (expand-file-name
368 (concat (package-strip-version package
) "-pkg")
370 (when (and (file-directory-p pkg-dir
)
371 (file-exists-p (concat pkg-file
".el")))
372 (load pkg-file nil t
))))
374 (defun package-load-all-descriptors ()
375 "Load descriptors for installed Emacs Lisp packages.
376 This looks for package subdirectories in `package-user-dir' and
377 `package-directory-list'. The variable `package-load-list'
378 controls which package subdirectories may be loaded.
380 In each valid package subdirectory, this function loads the
381 description file containing a call to `define-package', which
382 updates `package-alist' and `package-obsolete-alist'."
383 (let ((regexp (concat "\\`" package-subdirectory-regexp
"\\'")))
384 (dolist (dir (cons package-user-dir package-directory-list
))
385 (when (file-directory-p dir
)
386 (dolist (subdir (directory-files dir
))
387 (when (string-match regexp subdir
)
388 (package-maybe-load-descriptor (match-string 1 subdir
)
389 (match-string 2 subdir
)
392 (defun package-maybe-load-descriptor (name version dir
)
393 "Maybe load a specific package from directory DIR.
394 NAME and VERSION are the package's name and version strings.
395 This function checks `package-load-list', before actually loading
396 the package by calling `package-load-descriptor'."
397 (let ((force (assq (intern name
) package-load-list
))
398 (subdir (concat name
"-" version
)))
399 (and (file-directory-p (expand-file-name subdir dir
))
400 ;; Check `package-load-list':
402 (memq 'all package-load-list
))
403 ((null (setq force
(cadr force
)))
407 ((stringp force
) ; held
408 (version-list-= (version-to-list version
)
409 (version-to-list force
)))
411 (error "Invalid element in `package-load-list'")))
412 ;; Actually load the descriptor:
413 (package-load-descriptor dir subdir
))))
415 (defsubst package-desc-vers
(desc)
416 "Extract version from a package description vector."
419 (defsubst package-desc-reqs
(desc)
420 "Extract requirements from a package description vector."
423 (defsubst package-desc-doc
(desc)
424 "Extract doc string from a package description vector."
427 (defsubst package-desc-kind
(desc)
428 "Extract the kind of download from an archive package description vector."
431 (defun package--dir (name version
)
432 "Return the directory where a package is installed, or nil if none.
433 NAME and VERSION are both strings."
434 (let* ((subdir (concat name
"-" version
))
435 (dir-list (cons package-user-dir package-directory-list
))
438 (let ((subdir-full (expand-file-name subdir
(car dir-list
))))
439 (if (file-directory-p subdir-full
)
440 (setq pkg-dir subdir-full
442 (setq dir-list
(cdr dir-list
)))))
445 (defun package-activate-1 (package pkg-vec
)
446 (let* ((name (symbol-name package
))
447 (version-str (package-version-join (package-desc-vers pkg-vec
)))
448 (pkg-dir (package--dir name version-str
)))
450 (error "Internal error: unable to find directory for `%s-%s'"
453 (when (file-exists-p (expand-file-name "dir" pkg-dir
))
454 ;; FIXME: not the friendliest, but simple.
457 (push pkg-dir Info-directory-list
))
458 ;; Add to load path, add autoloads, and activate the package.
459 (push pkg-dir load-path
)
460 (load (expand-file-name (concat name
"-autoloads") pkg-dir
) nil t
)
461 (push package package-activated-list
)
465 (defun package-built-in-p (package &optional min-version
)
466 "Return true if PACKAGE is built-in to Emacs.
467 Optional arg MIN-VERSION, if non-nil, should be a version list
468 specifying the minimum acceptable version."
469 (require 'finder-inf nil t
) ; For `package--builtins'.
470 (if (eq package
'emacs
)
471 (version-list-<= min-version
(version-to-list emacs-version
))
472 (let ((elt (assq package package--builtins
)))
473 (and elt
(version-list-<= min-version
474 (package-desc-vers (cdr elt
)))))))
476 ;; This function goes ahead and activates a newer version of a package
477 ;; if an older one was already activated. This is not ideal; we'd at
478 ;; least need to check to see if the package has actually been loaded,
479 ;; and not merely activated.
480 (defun package-activate (package min-version
)
481 "Activate package PACKAGE, of version MIN-VERSION or newer.
482 MIN-VERSION should be a version list.
483 If PACKAGE has any dependencies, recursively activate them.
484 Return nil if the package could not be activated."
485 (let ((pkg-vec (cdr (assq package package-alist
)))
486 available-version found
)
487 ;; Check if PACKAGE is available in `package-alist'.
489 (setq available-version
(package-desc-vers pkg-vec
)
490 found
(version-list-<= min-version available-version
)))
492 ;; If no such package is found, maybe it's built-in.
494 (package-built-in-p package min-version
))
495 ;; If the package is already activated, just return t.
496 ((memq package package-activated-list
)
498 ;; Otherwise, proceed with activation.
500 (let ((fail (catch 'dep-failure
501 ;; Activate its dependencies recursively.
502 (dolist (req (package-desc-reqs pkg-vec
))
503 (unless (package-activate (car req
) (cadr req
))
504 (throw 'dep-failure req
))))))
506 (warn "Unable to activate package `%s'.
507 Required package `%s-%s' is unavailable"
508 package
(car fail
) (package-version-join (cadr fail
)))
509 ;; If all goes well, activate the package itself.
510 (package-activate-1 package pkg-vec
)))))))
512 (defun package-mark-obsolete (package pkg-vec
)
513 "Put package on the obsolete list, if not already there."
514 (let ((elt (assq package package-obsolete-alist
)))
516 ;; If this obsolete version does not exist in the list, update
518 (unless (assoc (package-desc-vers pkg-vec
) (cdr elt
))
519 (setcdr elt
(cons (cons (package-desc-vers pkg-vec
) pkg-vec
)
521 ;; Make a new association.
522 (push (cons package
(list (cons (package-desc-vers pkg-vec
)
524 package-obsolete-alist
))))
526 (defun define-package (name-string version-string
527 &optional docstring requirements
528 &rest _extra-properties
)
529 "Define a new package.
530 NAME-STRING is the name of the package, as a string.
531 VERSION-STRING is the version of the package, as a string.
532 DOCSTRING is a short description of the package, a string.
533 REQUIREMENTS is a list of dependencies on other packages.
534 Each requirement is of the form (OTHER-PACKAGE OTHER-VERSION),
535 where OTHER-VERSION is a string.
537 EXTRA-PROPERTIES is currently unused."
538 (let* ((name (intern name-string
))
539 (version (version-to-list version-string
))
546 (version-to-list (car (cdr elt
)))))
549 (old-pkg (assq name package-alist
)))
551 ;; If there's no old package, just add this to `package-alist'.
553 (push new-pkg-desc package-alist
))
554 ((version-list-< (package-desc-vers (cdr old-pkg
)) version
)
555 ;; Remove the old package and declare it obsolete.
556 (package-mark-obsolete name
(cdr old-pkg
))
557 (setq package-alist
(cons new-pkg-desc
558 (delq old-pkg package-alist
))))
559 ;; You can have two packages with the same version, e.g. one in
560 ;; the system package directory and one in your private
561 ;; directory. We just let the first one win.
562 ((not (version-list-= (package-desc-vers (cdr old-pkg
)) version
))
563 ;; The package is born obsolete.
564 (package-mark-obsolete name
(cdr new-pkg-desc
))))))
567 (defun package-autoload-ensure-default-file (file)
568 "Make sure that the autoload file FILE exists and if not create it."
569 (unless (file-exists-p file
)
571 (concat ";;; " (file-name-nondirectory file
)
572 " --- automatically extracted autoloads\n"
575 "\f\n;; Local Variables:\n"
576 ";; version-control: never\n"
577 ";; no-byte-compile: t\n"
578 ";; no-update-autoloads: t\n"
580 ";;; " (file-name-nondirectory file
)
585 (defun package-generate-autoloads (name pkg-dir
)
586 (require 'autoload
) ;Load before we let-bind generated-autoload-file!
587 (let* ((auto-name (concat name
"-autoloads.el"))
588 ;;(ignore-name (concat name "-pkg.el"))
589 (generated-autoload-file (expand-file-name auto-name pkg-dir
))
590 (version-control 'never
))
591 (unless (fboundp 'autoload-ensure-default-file
)
592 (package-autoload-ensure-default-file generated-autoload-file
))
593 (update-directory-autoloads pkg-dir
)
594 (let ((buf (find-buffer-visiting generated-autoload-file
)))
595 (when buf
(kill-buffer buf
)))))
597 (defvar tar-parse-info
)
598 (declare-function tar-untar-buffer
"tar-mode" ())
599 (declare-function tar-header-name
"tar-mode" (tar-header))
600 (declare-function tar-header-link-type
"tar-mode" (tar-header))
602 (defun package-untar-buffer (dir)
603 "Untar the current buffer.
604 This uses `tar-untar-buffer' from Tar mode. All files should
605 untar into a directory named DIR; otherwise, signal an error."
608 ;; Make sure everything extracts into DIR.
609 (let ((regexp (concat "\\`" (regexp-quote (expand-file-name dir
)) "/"))
610 (case-fold-search (memq system-type
'(windows-nt ms-dos cygwin
))))
611 (dolist (tar-data tar-parse-info
)
612 (let ((name (expand-file-name (tar-header-name tar-data
))))
613 (or (string-match regexp name
)
614 ;; Tarballs created by some utilities don't list
615 ;; directories with a trailing slash (Bug#13136).
616 (and (string-equal dir name
)
617 (eq (tar-header-link-type tar-data
) 5))
618 (error "Package does not untar cleanly into directory %s/" dir
)))))
621 (defun package-unpack (package version
)
622 (let* ((name (symbol-name package
))
623 (dirname (concat name
"-" version
))
624 (pkg-dir (expand-file-name dirname package-user-dir
)))
625 (make-directory package-user-dir t
)
626 ;; FIXME: should we delete PKG-DIR if it exists?
627 (let* ((default-directory (file-name-as-directory package-user-dir
)))
628 (package-untar-buffer dirname
)
629 (package--make-autoloads-and-compile name pkg-dir
))))
631 (defun package--make-autoloads-and-compile (name pkg-dir
)
632 "Generate autoloads and do byte-compilation for package named NAME.
633 PKG-DIR is the name of the package directory."
634 (package-generate-autoloads name pkg-dir
)
635 (let ((load-path (cons pkg-dir load-path
)))
636 ;; We must load the autoloads file before byte compiling, in
637 ;; case there are magic cookies to set up non-trivial paths.
638 (load (expand-file-name (concat name
"-autoloads") pkg-dir
) nil t
)
639 (byte-recompile-directory pkg-dir
0 t
)))
641 (defun package--write-file-no-coding (file-name)
642 (let ((buffer-file-coding-system 'no-conversion
))
643 (write-region (point-min) (point-max) file-name
)))
645 (defun package-unpack-single (file-name version desc requires
)
646 "Install the contents of the current buffer as a package."
647 ;; Special case "package".
648 (if (string= file-name
"package")
649 (package--write-file-no-coding
650 (expand-file-name (concat file-name
".el") package-user-dir
))
651 (let* ((pkg-dir (expand-file-name (concat file-name
"-"
652 (package-version-join
653 (version-to-list version
)))
655 (el-file (expand-file-name (concat file-name
".el") pkg-dir
))
656 (pkg-file (expand-file-name (concat file-name
"-pkg.el") pkg-dir
)))
657 (make-directory pkg-dir t
)
658 (package--write-file-no-coding el-file
)
659 (let ((print-level nil
)
664 (list 'define-package
669 ;; Turn version lists into string form.
673 (package-version-join (cadr elt
))))
679 (package--make-autoloads-and-compile file-name pkg-dir
))))
681 (defmacro package--with-work-buffer
(location file
&rest body
)
682 "Run BODY in a buffer containing the contents of FILE at LOCATION.
683 LOCATION is the base location of a package archive, and should be
684 one of the URLs (or file names) specified in `package-archives'.
685 FILE is the name of a file relative to that base location.
687 This macro retrieves FILE from LOCATION into a temporary buffer,
688 and evaluates BODY while that buffer is current. This work
689 buffer is killed afterwards. Return the last value in BODY."
690 `(let* ((http (string-match "\\`https?:" ,location
))
693 (url-retrieve-synchronously (concat ,location
,file
))
694 (generate-new-buffer "*package work buffer*"))))
696 (with-current-buffer buffer
698 (progn (package-handle-response)
699 (re-search-forward "^$" nil
'move
)
701 (delete-region (point-min) (point)))
702 (unless (file-name-absolute-p ,location
)
703 (error "Archive location %s is not an absolute file name"
705 (insert-file-contents (expand-file-name ,file
,location
)))
707 (kill-buffer buffer
))))
709 (defun package-handle-response ()
710 "Handle the response from a `url-retrieve-synchronously' call.
711 Parse the HTTP response and throw if an error occurred.
712 The url package seems to require extra processing for this.
713 This should be called in a `save-excursion', in the download buffer.
714 It will move point to somewhere in the headers."
715 ;; We assume HTTP here.
717 (let ((response (url-http-parse-response)))
718 (when (or (< response
200) (>= response
300))
719 (error "Error during download request:%s"
720 (buffer-substring-no-properties (point) (progn
724 (defun package-download-single (name version desc requires
)
725 "Download and install a single-file package."
726 (let ((location (package-archive-base name
))
727 (file (concat (symbol-name name
) "-" version
".el")))
728 (package--with-work-buffer location file
729 (package-unpack-single (symbol-name name
) version desc requires
))))
731 (defun package-download-tar (name version
)
732 "Download and install a tar package."
733 (let ((location (package-archive-base name
))
734 (file (concat (symbol-name name
) "-" version
".tar")))
735 (package--with-work-buffer location file
736 (package-unpack name version
))))
738 (defvar package--initialized nil
)
740 (defun package-installed-p (package &optional min-version
)
741 "Return true if PACKAGE, of MIN-VERSION or newer, is installed.
742 MIN-VERSION should be a version list."
743 (unless package--initialized
(error "package.el is not yet initialized!"))
744 (let ((pkg-desc (assq package package-alist
)))
746 (version-list-<= min-version
747 (package-desc-vers (cdr pkg-desc
)))
748 ;; Also check built-in packages.
749 (package-built-in-p package min-version
))))
751 (defun package-compute-transaction (package-list requirements
)
752 "Return a list of packages to be installed, including PACKAGE-LIST.
753 PACKAGE-LIST should be a list of package names (symbols).
755 REQUIREMENTS should be a list of additional requirements; each
756 element in this list should have the form (PACKAGE VERSION-LIST),
757 where PACKAGE is a package name and VERSION-LIST is the required
758 version of that package.
760 This function recursively computes the requirements of the
761 packages in REQUIREMENTS, and returns a list of all the packages
762 that must be installed. Packages that are already installed are
763 not included in this list."
764 (dolist (elt requirements
)
765 (let* ((next-pkg (car elt
))
766 (next-version (cadr elt
)))
767 (unless (package-installed-p next-pkg next-version
)
768 ;; A package is required, but not installed. It might also be
769 ;; blocked via `package-load-list'.
770 (let ((pkg-desc (assq next-pkg package-archive-contents
))
772 (when (setq hold
(assq next-pkg package-load-list
))
773 (setq hold
(cadr hold
))
776 (error "Required package '%s' is disabled"
777 (symbol-name next-pkg
)))
778 ((null (stringp hold
))
779 (error "Invalid element in `package-load-list'"))
780 ((version-list-< (version-to-list hold
) next-version
)
781 (error "Package `%s' held at version %s, \
782 but version %s required"
783 (symbol-name next-pkg
) hold
784 (package-version-join next-version
)))))
786 (error "Package `%s-%s' is unavailable"
787 (symbol-name next-pkg
)
788 (package-version-join next-version
)))
789 (unless (version-list-<= next-version
790 (package-desc-vers (cdr pkg-desc
)))
792 "Need package `%s-%s', but only %s is available"
793 (symbol-name next-pkg
) (package-version-join next-version
)
794 (package-version-join (package-desc-vers (cdr pkg-desc
)))))
795 ;; Only add to the transaction if we don't already have it.
796 (unless (memq next-pkg package-list
)
797 (push next-pkg package-list
))
799 (package-compute-transaction package-list
801 (cdr pkg-desc
))))))))
804 (defun package-read-from-string (str)
805 "Read a Lisp expression from STR.
806 Signal an error if the entire string was not used."
807 (let* ((read-data (read-from-string str
))
810 ;; The call to `ignore' suppresses a compiler warning.
811 (progn (ignore (read-from-string
812 (substring str
(cdr read-data
))))
816 (error "Can't read whole string")
819 (defun package--read-archive-file (file)
820 "Re-read archive file FILE, if it exists.
821 Will return the data from the file, or nil if the file does not exist.
822 Will throw an error if the archive version is too new."
823 (let ((filename (expand-file-name file package-user-dir
)))
824 (when (file-exists-p filename
)
826 (insert-file-contents-literally filename
)
827 (let ((contents (read (current-buffer))))
828 (if (> (car contents
) package-archive-version
)
829 (error "Package archive version %d is higher than %d"
830 (car contents
) package-archive-version
))
833 (defun package-read-all-archive-contents ()
834 "Re-read `archive-contents', if it exists.
835 If successful, set `package-archive-contents'."
836 (setq package-archive-contents nil
)
837 (dolist (archive package-archives
)
838 (package-read-archive-contents (car archive
))))
840 (defun package-read-archive-contents (archive)
841 "Re-read archive contents for ARCHIVE.
842 If successful, set the variable `package-archive-contents'.
843 If the archive version is too new, signal an error."
844 ;; Version 1 of 'archive-contents' is identical to our internal
846 (let* ((dir (concat "archives/" archive
))
847 (contents-file (concat dir
"/archive-contents"))
849 (when (setq contents
(package--read-archive-file contents-file
))
850 (dolist (package contents
)
851 (package--add-to-archive-contents package archive
)))))
853 (defun package--add-to-archive-contents (package archive
)
854 "Add the PACKAGE from the given ARCHIVE if necessary.
855 Also, add the originating archive to the end of the package vector."
856 (let* ((name (car package
))
857 (version (package-desc-vers (cdr package
)))
859 (vconcat (cdr package
) (vector archive
))))
860 (existing-package (assq name package-archive-contents
)))
861 (cond ((not existing-package
)
862 (add-to-list 'package-archive-contents entry
))
863 ((version-list-< (package-desc-vers (cdr existing-package
))
865 ;; Replace the entry with this one.
866 (setq package-archive-contents
868 (delq existing-package
869 package-archive-contents
)))))))
871 (defun package-download-transaction (package-list)
872 "Download and install all the packages in PACKAGE-LIST.
873 PACKAGE-LIST should be a list of package names (symbols).
874 This function assumes that all package requirements in
875 PACKAGE-LIST are satisfied, i.e. that PACKAGE-LIST is computed
876 using `package-compute-transaction'."
877 (dolist (elt package-list
)
878 (let* ((desc (cdr (assq elt package-archive-contents
)))
879 ;; As an exception, if package is "held" in
880 ;; `package-load-list', download the held version.
881 (hold (cadr (assq elt package-load-list
)))
882 (v-string (or (and (stringp hold
) hold
)
883 (package-version-join (package-desc-vers desc
))))
884 (kind (package-desc-kind desc
)))
887 (package-download-tar elt v-string
))
889 (package-download-single elt v-string
890 (package-desc-doc desc
)
891 (package-desc-reqs desc
)))
893 (error "Unknown package kind: %s" (symbol-name kind
))))
894 ;; If package A depends on package B, then A may `require' B
895 ;; during byte compilation. So we need to activate B before
897 (package-maybe-load-descriptor (symbol-name elt
) v-string
899 (package-activate elt
(version-to-list v-string
)))))
902 (defun package-install (name)
903 "Install the package named NAME.
904 NAME should be the name of one of the available packages in an
905 archive in `package-archives'. Interactively, prompt for NAME."
908 ;; Initialize the package system to get the list of package
909 ;; symbols for completion.
910 (unless package--initialized
911 (package-initialize t
))
912 (unless package-archive-contents
913 (package-refresh-contents))
914 (list (intern (completing-read
916 (mapcar (lambda (elt)
917 (cons (symbol-name (car elt
))
919 package-archive-contents
)
921 (let ((pkg-desc (assq name package-archive-contents
)))
923 (error "Package `%s' is not available for installation"
925 (package-download-transaction
926 (package-compute-transaction (list name
)
927 (package-desc-reqs (cdr pkg-desc
))))))
929 (defun package-strip-rcs-id (str)
930 "Strip RCS version ID from the version string STR.
931 If the result looks like a dotted numeric version, return it.
932 Otherwise return nil."
934 (when (string-match "\\`[ \t]*[$]Revision:[ \t]+" str
)
935 (setq str
(substring str
(match-end 0))))
937 (if (version-to-list str
)
941 (defun package-buffer-info ()
942 "Return a vector describing the package in the current buffer.
943 The vector has the form
945 [FILENAME REQUIRES DESCRIPTION VERSION COMMENTARY]
947 FILENAME is the file name, a string, sans the \".el\" extension.
948 REQUIRES is a list of requirements, each requirement having the
949 form (NAME VER); NAME is a string and VER is a version list.
950 DESCRIPTION is the package description, a string.
951 VERSION is the version, a string.
952 COMMENTARY is the commentary section, a string, or nil if none.
954 If the buffer does not contain a conforming package, signal an
955 error. If there is a package, narrow the buffer to the file's
957 (goto-char (point-min))
958 (unless (re-search-forward "^;;; \\([^ ]*\\)\\.el ---[ \t]*\\(.*?\\)[ \t]*\\(-\\*-.*-\\*-[ \t]*\\)?$" nil t
)
959 (error "Packages lacks a file header"))
960 (let ((file-name (match-string-no-properties 1))
961 (desc (match-string-no-properties 2))
962 (start (line-beginning-position)))
963 (unless (search-forward (concat ";;; " file-name
".el ends here"))
964 (error "Package lacks a terminating comment"))
965 ;; Try to include a trailing newline.
967 (narrow-to-region start
(point))
969 ;; Use some headers we've invented to drive the process.
970 (let* ((requires-str (lm-header "package-requires"))
971 (requires (if requires-str
972 (package-read-from-string requires-str
)))
973 ;; Prefer Package-Version; if defined, the package author
974 ;; probably wants us to use it. Otherwise try Version.
976 (or (package-strip-rcs-id (lm-header "package-version"))
977 (package-strip-rcs-id (lm-header "version"))))
978 (commentary (lm-commentary)))
981 "Package lacks a \"Version\" or \"Package-Version\" header"))
982 ;; Turn string version numbers into list form.
987 (version-to-list (car (cdr elt
)))))
989 (vector file-name requires desc pkg-version commentary
))))
991 (defun package-tar-file-info (file)
992 "Find package information for a tar file.
993 FILE is the name of the tar file to examine.
994 The return result is a vector like `package-buffer-info'."
995 (let ((default-directory (file-name-directory file
))
996 (file (file-name-nondirectory file
)))
997 (unless (string-match (concat "\\`" package-subdirectory-regexp
"\\.tar\\'")
999 (error "Invalid package name `%s'" file
))
1000 (let* ((pkg-name (match-string-no-properties 1 file
))
1001 (pkg-version (match-string-no-properties 2 file
))
1002 ;; Extract the package descriptor.
1003 (pkg-def-contents (shell-command-to-string
1004 ;; Requires GNU tar.
1005 (concat "tar -xOf " file
" "
1007 pkg-name
"-" pkg-version
"/"
1008 pkg-name
"-pkg.el")))
1009 (pkg-def-parsed (package-read-from-string pkg-def-contents
)))
1010 (unless (eq (car pkg-def-parsed
) 'define-package
)
1011 (error "No `define-package' sexp is present in `%s-pkg.el'" pkg-name
))
1012 (let ((name-str (nth 1 pkg-def-parsed
))
1013 (version-string (nth 2 pkg-def-parsed
))
1014 (docstring (nth 3 pkg-def-parsed
))
1015 (requires (nth 4 pkg-def-parsed
))
1016 (readme (shell-command-to-string
1017 ;; Requires GNU tar.
1018 (concat "tar -xOf " file
" "
1019 pkg-name
"-" pkg-version
"/README"))))
1020 (unless (equal pkg-version version-string
)
1021 (error "Package has inconsistent versions"))
1022 (unless (equal pkg-name name-str
)
1023 (error "Package has inconsistent names"))
1025 (if (string-match ": Not found in archive" readme
)
1027 ;; Turn string version numbers into list form.
1028 (if (eq (car requires
) 'quote
)
1029 (setq requires
(car (cdr requires
))))
1031 (mapcar (lambda (elt)
1033 (version-to-list (cadr elt
))))
1035 (vector pkg-name requires docstring version-string readme
)))))
1038 (defun package-install-from-buffer (pkg-info type
)
1039 "Install a package from the current buffer.
1040 When called interactively, the current buffer is assumed to be a
1041 single .el file that follows the packaging guidelines; see info
1042 node `(elisp)Packaging'.
1044 When called from Lisp, PKG-INFO is a vector describing the
1045 information, of the type returned by `package-buffer-info'; and
1046 TYPE is the package type (either `single' or `tar')."
1047 (interactive (list (package-buffer-info) 'single
))
1050 (let* ((file-name (aref pkg-info
0))
1051 (requires (aref pkg-info
1))
1052 (desc (if (string= (aref pkg-info
2) "")
1053 "No description available."
1055 (pkg-version (aref pkg-info
3)))
1056 ;; Download and install the dependencies.
1057 (let ((transaction (package-compute-transaction nil requires
)))
1058 (package-download-transaction transaction
))
1059 ;; Install the package itself.
1062 (package-unpack-single file-name pkg-version desc requires
))
1064 (package-unpack (intern file-name
) pkg-version
))
1066 (error "Unknown type: %s" (symbol-name type
))))
1067 ;; Try to activate it.
1068 (package-initialize)))))
1071 (defun package-install-file (file)
1072 "Install a package from a file.
1073 The file can either be a tar file or an Emacs Lisp file."
1074 (interactive "fPackage file name: ")
1076 (insert-file-contents-literally file
)
1078 ((string-match "\\.el$" file
)
1079 (package-install-from-buffer (package-buffer-info) 'single
))
1080 ((string-match "\\.tar$" file
)
1081 (package-install-from-buffer (package-tar-file-info file
) 'tar
))
1082 (t (error "Unrecognized extension `%s'" (file-name-extension file
))))))
1084 (defun package-delete (name version
)
1085 (let ((dir (package--dir name version
)))
1086 (if (string-equal (file-name-directory dir
)
1087 (file-name-as-directory
1088 (expand-file-name package-user-dir
)))
1090 (delete-directory dir t t
)
1091 (message "Package `%s-%s' deleted." name version
))
1092 ;; Don't delete "system" packages
1093 (error "Package `%s-%s' is a system package, not deleting"
1096 (defun package-archive-base (name)
1097 "Return the archive containing the package NAME."
1098 (let ((desc (cdr (assq (intern-soft name
) package-archive-contents
))))
1099 (cdr (assoc (aref desc
(- (length desc
) 1)) package-archives
))))
1101 (defun package--download-one-archive (archive file
)
1102 "Retrieve an archive file FILE from ARCHIVE, and cache it.
1103 ARCHIVE should be a cons cell of the form (NAME . LOCATION),
1104 similar to an entry in `package-alist'. Save the cached copy to
1105 \"archives/NAME/archive-contents\" in `package-user-dir'."
1106 (let* ((dir (expand-file-name "archives" package-user-dir
))
1107 (dir (expand-file-name (car archive
) dir
)))
1108 (package--with-work-buffer (cdr archive
) file
1109 ;; Read the retrieved buffer to make sure it is valid (e.g. it
1110 ;; may fetch a URL redirect page).
1111 (when (listp (read buffer
))
1112 (make-directory dir t
)
1113 (setq buffer-file-name
(expand-file-name file dir
))
1114 (let ((version-control 'never
))
1118 (defun package-refresh-contents ()
1119 "Download the ELPA archive description if needed.
1120 This informs Emacs about the latest versions of all packages, and
1121 makes them available for download."
1123 (unless (file-exists-p package-user-dir
)
1124 (make-directory package-user-dir t
))
1125 (dolist (archive package-archives
)
1126 (condition-case-unless-debug nil
1127 (package--download-one-archive archive
"archive-contents")
1128 (error (message "Failed to download `%s' archive."
1130 (package-read-all-archive-contents))
1133 (defun package-initialize (&optional no-activate
)
1134 "Load Emacs Lisp packages, and activate them.
1135 The variable `package-load-list' controls which packages to load.
1136 If optional arg NO-ACTIVATE is non-nil, don't activate packages."
1138 (setq package-alist nil
1139 package-obsolete-alist nil
)
1140 (package-load-all-descriptors)
1141 (package-read-all-archive-contents)
1143 (dolist (elt package-alist
)
1144 (package-activate (car elt
) (package-desc-vers (cdr elt
)))))
1145 (setq package--initialized t
))
1148 ;;;; Package description buffer.
1151 (defun describe-package (package)
1152 "Display the full documentation of PACKAGE (a symbol)."
1154 (let* ((guess (function-called-at-point))
1156 (require 'finder-inf nil t
)
1157 ;; Load the package list if necessary (but don't activate them).
1158 (unless package--initialized
1159 (package-initialize t
))
1160 (setq packages
(append (mapcar 'car package-alist
)
1161 (mapcar 'car package-archive-contents
)
1162 (mapcar 'car package--builtins
)))
1163 (unless (memq guess packages
)
1165 (setq packages
(mapcar 'symbol-name packages
))
1167 (completing-read (if guess
1168 (format "Describe package (default %s): "
1170 "Describe package: ")
1171 packages nil t nil nil guess
))
1172 (list (if (equal val
"") guess
(intern val
)))))
1173 (if (or (null package
) (not (symbolp package
)))
1174 (message "No package specified")
1175 (help-setup-xref (list #'describe-package package
)
1176 (called-interactively-p 'interactive
))
1177 (with-help-window (help-buffer)
1178 (with-current-buffer standard-output
1179 (describe-package-1 package
)))))
1181 (defun describe-package-1 (package)
1183 (let ((package-name (symbol-name package
))
1184 (built-in (assq package package--builtins
))
1185 desc pkg-dir reqs version installable archive
)
1189 ;; Loaded packages are in `package-alist'.
1190 ((setq desc
(cdr (assq package package-alist
)))
1191 (setq version
(package-version-join (package-desc-vers desc
)))
1192 (if (setq pkg-dir
(package--dir package-name version
))
1193 (insert "an installed package.\n\n")
1194 ;; This normally does not happen.
1195 (insert "a deleted package.\n\n")))
1196 ;; Available packages are in `package-archive-contents'.
1197 ((setq desc
(cdr (assq package package-archive-contents
)))
1198 (setq version
(package-version-join (package-desc-vers desc
))
1199 archive
(aref desc
(- (length desc
) 1))
1202 (insert "a built-in package.\n\n")
1203 (insert "an uninstalled package.\n\n")))
1205 (setq desc
(cdr built-in
)
1206 version
(package-version-join (package-desc-vers desc
)))
1207 (insert "a built-in package.\n\n"))
1209 (insert "an orphan package.\n\n")))
1211 (insert " " (propertize "Status" 'font-lock-face
'bold
) ": ")
1213 (insert (propertize "Installed"
1214 'font-lock-face
'font-lock-comment-face
))
1216 ;; Todo: Add button for uninstalling.
1217 (help-insert-xref-button (file-name-as-directory pkg-dir
)
1218 'help-package-def pkg-dir
)
1220 (insert "',\n shadowing a "
1221 (propertize "built-in package"
1222 'font-lock-face
'font-lock-builtin-face
)
1227 (insert (propertize "Built-in." 'font-lock-face
'font-lock-builtin-face
)
1228 " Alternate version available")
1229 (insert "Available"))
1230 (insert " from " archive
)
1232 (let ((button-text (if (display-graphic-p) "Install" "[Install]"))
1233 (button-face (if (display-graphic-p)
1234 '(:box
(:line-width
2 :color
"dark grey")
1235 :background
"light grey"
1236 :foreground
"black")
1238 (insert-text-button button-text
'face button-face
'follow-link t
1239 'package-symbol package
1240 'action
'package-install-button-action
)))
1242 (insert (propertize "Built-in." 'font-lock-face
'font-lock-builtin-face
)))
1243 (t (insert "Deleted.")))
1245 (and version
(> (length version
) 0)
1247 (propertize "Version" 'font-lock-face
'bold
) ": " version
"\n"))
1249 (setq reqs
(if desc
(package-desc-reqs desc
)))
1251 (insert " " (propertize "Requires" 'font-lock-face
'bold
) ": ")
1255 (setq name
(car req
)
1257 text
(format "%s-%s" (symbol-name name
)
1258 (package-version-join vers
)))
1259 (cond (first (setq first nil
))
1260 ((>= (+ 2 (current-column) (length text
))
1264 (help-insert-xref-button text
'help-package name
))
1266 (insert " " (propertize "Summary" 'font-lock-face
'bold
)
1267 ": " (if desc
(package-desc-doc desc
)) "\n\n")
1270 ;; For built-in packages, insert the commentary.
1271 (let ((fn (locate-file (concat package-name
".el") load-path
1272 load-file-rep-suffixes
))
1274 (insert (or (lm-commentary fn
) ""))
1277 (when (re-search-forward "^;;; Commentary:\n" nil t
)
1279 (while (re-search-forward "^\\(;+ ?\\)" nil t
)
1280 (replace-match ""))))
1281 (let ((readme (expand-file-name (concat package-name
"-readme.txt")
1284 ;; For elpa packages, try downloading the commentary. If that
1285 ;; fails, try an existing readme file in `package-user-dir'.
1286 (cond ((condition-case nil
1287 (package--with-work-buffer (package-archive-base package
)
1288 (concat package-name
"-readme.txt")
1289 (setq buffer-file-name
1290 (expand-file-name readme package-user-dir
))
1291 (let ((version-control 'never
))
1293 (setq readme-string
(buffer-string))
1296 (insert readme-string
))
1297 ((file-readable-p readme
)
1298 (insert-file-contents readme
)
1299 (goto-char (point-max))))))))
1301 (defun package-install-button-action (button)
1302 (let ((package (button-get button
'package-symbol
)))
1303 (when (y-or-n-p (format "Install package `%s'? " package
))
1304 (package-install package
)
1305 (revert-buffer nil t
)
1306 (goto-char (point-min)))))
1309 ;;;; Package menu mode.
1311 (defvar package-menu-mode-map
1312 (let ((map (make-sparse-keymap))
1313 (menu-map (make-sparse-keymap "Package")))
1314 (set-keymap-parent map tabulated-list-mode-map
)
1315 (define-key map
"\C-m" 'package-menu-describe-package
)
1316 (define-key map
"u" 'package-menu-mark-unmark
)
1317 (define-key map
"\177" 'package-menu-backup-unmark
)
1318 (define-key map
"d" 'package-menu-mark-delete
)
1319 (define-key map
"i" 'package-menu-mark-install
)
1320 (define-key map
"U" 'package-menu-mark-upgrades
)
1321 (define-key map
"r" 'package-menu-refresh
)
1322 (define-key map
"~" 'package-menu-mark-obsolete-for-deletion
)
1323 (define-key map
"x" 'package-menu-execute
)
1324 (define-key map
"h" 'package-menu-quick-help
)
1325 (define-key map
"?" 'package-menu-describe-package
)
1326 (define-key map
[menu-bar package-menu
] (cons "Package" menu-map
))
1327 (define-key menu-map
[mq]
1328 '(menu-item "Quit" quit-window
1329 :help "Quit package selection"))
1330 (define-key menu-map [s1] '("--"))
1331 (define-key menu-map [mn]
1332 '(menu-item "Next" next-line
1334 (define-key menu-map [mp]
1335 '(menu-item "Previous" previous-line
1336 :help "Previous Line"))
1337 (define-key menu-map [s2] '("--"))
1338 (define-key menu-map [mu]
1339 '(menu-item "Unmark" package-menu-mark-unmark
1340 :help "Clear any marks on a package and move to the next line"))
1341 (define-key menu-map [munm]
1342 '(menu-item "Unmark Backwards" package-menu-backup-unmark
1343 :help "Back up one line and clear any marks on that package"))
1344 (define-key menu-map [md]
1345 '(menu-item "Mark for Deletion" package-menu-mark-delete
1346 :help "Mark a package for deletion and move to the next line"))
1347 (define-key menu-map [mi]
1348 '(menu-item "Mark for Install" package-menu-mark-install
1349 :help "Mark a package for installation and move to the next line"))
1350 (define-key menu-map [mupgrades]
1351 '(menu-item "Mark Upgradable Packages" package-menu-mark-upgrades
1352 :help "Mark packages that have a newer version for upgrading"))
1353 (define-key menu-map [s3] '("--"))
1354 (define-key menu-map [mg]
1355 '(menu-item "Update Package List" revert-buffer
1356 :help "Update the list of packages"))
1357 (define-key menu-map [mr]
1358 '(menu-item "Refresh Package List" package-menu-refresh
1359 :help "Download the ELPA archive"))
1360 (define-key menu-map [s4] '("--"))
1361 (define-key menu-map [mt]
1362 '(menu-item "Mark Obsolete Packages" package-menu-mark-obsolete-for-deletion
1363 :help "Mark all obsolete packages for deletion"))
1364 (define-key menu-map [mx]
1365 '(menu-item "Execute Actions" package-menu-execute
1366 :help "Perform all the marked actions"))
1367 (define-key menu-map [s5] '("--"))
1368 (define-key menu-map [mh]
1369 '(menu-item "Help" package-menu-quick-help
1370 :help "Show short key binding help for package-menu-mode"))
1371 (define-key menu-map [mc]
1372 '(menu-item "View Commentary" package-menu-view-commentary
1373 :help "Display information about this package"))
1375 "Local keymap for `package-menu-mode' buffers.")
1377 (defvar package-menu--new-package-list nil
1378 "List of newly-available packages since `list-packages' was last called.")
1380 (define-derived-mode package-menu-mode tabulated-list-mode "Package Menu"
1381 "Major mode for browsing a list of packages.
1382 Letters do not insert themselves; instead, they are commands.
1383 \\<package-menu-mode-map>
1384 \\{package-menu-mode-map}"
1385 (setq tabulated-list-format [("Package" 18 package-menu--name-predicate)
1387 ("Status" 10 package-menu--status-predicate)
1388 ("Description" 0 nil)])
1389 (setq tabulated-list-padding 2)
1390 (setq tabulated-list-sort-key (cons "Status" nil))
1391 (tabulated-list-init-header))
1393 (defmacro package--push (package desc status listname)
1394 "Convenience macro for `package-menu--generate'.
1395 If the alist stored in the symbol LISTNAME lacks an entry for a
1396 package PACKAGE with descriptor DESC, add one. The alist is
1397 keyed with cons cells (PACKAGE . VERSION-LIST), where PACKAGE is
1398 a symbol and VERSION-LIST is a version list."
1399 `(let* ((version (package-desc-vers ,desc))
1400 (key (cons ,package version)))
1401 (unless (assoc key ,listname)
1402 (push (list key ,status (package-desc-doc ,desc)) ,listname))))
1404 (defun package-menu--generate (remember-pos packages)
1405 "Populate the Package Menu.
1406 If REMEMBER-POS is non-nil, keep point on the same entry.
1407 PACKAGES should be t, which means to display all known packages,
1408 or a list of package names (symbols) to display."
1409 ;; Construct list of ((PACKAGE . VERSION) STATUS DESCRIPTION).
1410 (let (info-list name)
1411 ;; Installed packages:
1412 (dolist (elt package-alist)
1413 (setq name (car elt))
1414 (when (or (eq packages t) (memq name packages))
1415 (package--push name (cdr elt)
1416 (if (stringp (cadr (assq name package-load-list)))
1420 ;; Built-in packages:
1421 (dolist (elt package--builtins)
1422 (setq name (car elt))
1423 (when (and (not (eq name 'emacs)) ; Hide the `emacs' package.
1424 (or (eq packages t) (memq name packages)))
1425 (package--push name (cdr elt) "built-in" info-list)))
1427 ;; Available and disabled packages:
1428 (dolist (elt package-archive-contents)
1429 (setq name (car elt))
1430 (when (or (eq packages t) (memq name packages))
1431 (let ((hold (assq name package-load-list)))
1432 (package--push name (cdr elt)
1434 ((and hold (null (cadr hold))) "disabled")
1435 ((memq name package-menu--new-package-list) "new")
1439 ;; Obsolete packages:
1440 (dolist (elt package-obsolete-alist)
1441 (dolist (inner-elt (cdr elt))
1442 (when (or (eq packages t) (memq (car elt) packages))
1443 (package--push (car elt) (cdr inner-elt) "obsolete" info-list))))
1445 ;; Print the result.
1446 (setq tabulated-list-entries (mapcar 'package-menu--print-info info-list))
1447 (tabulated-list-print remember-pos)))
1449 (defun package-menu--print-info (pkg)
1450 "Return a package entry suitable for `tabulated-list-entries'.
1451 PKG has the form ((PACKAGE . VERSION) STATUS DOC).
1452 Return (KEY [NAME VERSION STATUS DOC]), where KEY is the
1453 identifier (NAME . VERSION-LIST)."
1454 (let* ((package (caar pkg))
1455 (version (cdr (car pkg)))
1456 (status (nth 1 pkg))
1457 (doc (or (nth 2 pkg) ""))
1459 ((string= status "built-in") 'font-lock-builtin-face)
1460 ((string= status "available") 'default)
1461 ((string= status "new") 'bold)
1462 ((string= status "held") 'font-lock-constant-face)
1463 ((string= status "disabled") 'font-lock-warning-face)
1464 ((string= status "installed") 'font-lock-comment-face)
1465 (t 'font-lock-warning-face)))) ; obsolete.
1466 (list (cons package version)
1467 (vector (list (symbol-name package)
1470 'package-symbol package
1471 'action 'package-menu-describe-package)
1472 (propertize (package-version-join version)
1473 'font-lock-face face)
1474 (propertize status 'font-lock-face face)
1475 (propertize doc 'font-lock-face face)))))
1477 (defun package-menu-refresh ()
1478 "Download the Emacs Lisp package archive.
1479 This fetches the contents of each archive specified in
1480 `package-archives', and then refreshes the package menu."
1482 (unless (derived-mode-p 'package-menu-mode)
1483 (error "The current buffer is not a Package Menu"))
1484 (package-refresh-contents)
1485 (package-menu--generate t t))
1487 (defun package-menu-describe-package (&optional button)
1488 "Describe the current package.
1489 If optional arg BUTTON is non-nil, describe its associated package."
1491 (let ((package (if button (button-get button 'package-symbol)
1492 (car (tabulated-list-get-id)))))
1494 (describe-package package))))
1496 ;; fixme numeric argument
1497 (defun package-menu-mark-delete (&optional _num)
1498 "Mark a package for deletion and move to the next line."
1500 (if (member (package-menu-get-status) '("installed" "obsolete"))
1501 (tabulated-list-put-tag "D" t)
1504 (defun package-menu-mark-install (&optional _num)
1505 "Mark a package for installation and move to the next line."
1507 (if (member (package-menu-get-status) '("available" "new"))
1508 (tabulated-list-put-tag "I" t)
1511 (defun package-menu-mark-unmark (&optional _num)
1512 "Clear any marks on a package and move to the next line."
1514 (tabulated-list-put-tag " " t))
1516 (defun package-menu-backup-unmark ()
1517 "Back up one line and clear any marks on that package."
1520 (tabulated-list-put-tag " "))
1522 (defun package-menu-mark-obsolete-for-deletion ()
1523 "Mark all obsolete packages for deletion."
1526 (goto-char (point-min))
1528 (if (equal (package-menu-get-status) "obsolete")
1529 (tabulated-list-put-tag "D" t)
1530 (forward-line 1)))))
1532 (defun package-menu-quick-help ()
1533 "Show short key binding help for package-menu-mode."
1535 (message "n-ext, i-nstall, d-elete, u-nmark, x-ecute, r-efresh, h-elp"))
1537 (define-obsolete-function-alias
1538 'package-menu-view-commentary 'package-menu-describe-package "24.1")
1540 (defun package-menu-get-status ()
1541 (let* ((pkg (tabulated-list-get-id))
1542 (entry (and pkg (assq pkg tabulated-list-entries))))
1544 (aref (cadr entry) 2)
1547 (defun package-menu--find-upgrades ()
1548 (let (installed available upgrades)
1549 ;; Build list of installed/available packages in this buffer.
1550 (dolist (entry tabulated-list-entries)
1551 ;; ENTRY is ((NAME . VERSION) [NAME VERSION STATUS DOC])
1552 (let ((pkg (car entry))
1553 (status (aref (cadr entry) 2)))
1554 (cond ((equal status "installed")
1555 (push pkg installed))
1556 ((member status '("available" "new"))
1557 (push pkg available)))))
1558 ;; Loop through list of installed packages, finding upgrades
1559 (dolist (pkg installed)
1560 (let ((avail-pkg (assq (car pkg) available)))
1562 (version-list-< (cdr pkg) (cdr avail-pkg))
1563 (push avail-pkg upgrades))))
1566 (defun package-menu-mark-upgrades ()
1567 "Mark all upgradable packages in the Package Menu.
1568 For each installed package with a newer version available, place
1569 an (I)nstall flag on the available version and a (D)elete flag on
1570 the installed version. A subsequent \\[package-menu-execute]
1571 call will upgrade the package."
1573 (unless (derived-mode-p 'package-menu-mode)
1574 (error "The current buffer is not a Package Menu"))
1575 (let ((upgrades (package-menu--find-upgrades)))
1577 (message "No packages to upgrade.")
1580 (goto-char (point-min))
1582 (let* ((pkg (tabulated-list-get-id))
1583 (upgrade (assq (car pkg) upgrades)))
1584 (cond ((null upgrade)
1586 ((equal pkg upgrade)
1587 (package-menu-mark-install))
1589 (package-menu-mark-delete))))))
1590 (message "%d package%s marked for upgrading."
1592 (if (= (length upgrades) 1) "" "s")))))
1594 (defun package-menu-execute (&optional noquery)
1595 "Perform marked Package Menu actions.
1596 Packages marked for installation are downloaded and installed;
1597 packages marked for deletion are removed.
1598 Optional argument NOQUERY non-nil means do not ask the user to confirm."
1600 (unless (derived-mode-p 'package-menu-mode)
1601 (error "The current buffer is not in Package Menu mode"))
1602 (let (install-list delete-list cmd id)
1604 (goto-char (point-min))
1606 (setq cmd (char-after))
1607 (unless (eq cmd ?\s)
1608 ;; This is the key (PACKAGE . VERSION-LIST).
1609 (setq id (tabulated-list-get-id))
1611 (push (cons (symbol-name (car id))
1612 (package-version-join (cdr id)))
1615 (push (car id) install-list))))
1621 (if (= (length install-list) 1)
1622 (format "Install package `%s'? " (car install-list))
1623 (format "Install these %d packages (%s)? "
1624 (length install-list)
1625 (mapconcat 'symbol-name install-list ", ")))))
1626 (mapc 'package-install install-list)))
1627 ;; Delete packages, prompting if necessary.
1632 (if (= (length delete-list) 1)
1633 (format "Delete package `%s-%s'? "
1635 (cdr (car delete-list)))
1636 (format "Delete these %d packages (%s)? "
1637 (length delete-list)
1638 (mapconcat (lambda (elt)
1639 (concat (car elt) "-" (cdr elt)))
1642 (dolist (elt delete-list)
1643 (condition-case-unless-debug err
1644 (package-delete (car elt) (cdr elt))
1645 (error (message (cadr err)))))
1647 ;; If we deleted anything, regenerate `package-alist'. This is done
1648 ;; automatically if we installed a package.
1649 (and delete-list (null install-list)
1650 (package-initialize))
1651 (if (or delete-list install-list)
1652 (package-menu--generate t t)
1653 (message "No operations specified."))))
1655 (defun package-menu--version-predicate (A B)
1656 (let ((vA (or (aref (cadr A) 1) '(0)))
1657 (vB (or (aref (cadr B) 1) '(0))))
1658 (if (version-list-= vA vB)
1659 (package-menu--name-predicate A B)
1660 (version-list-< vA vB))))
1662 (defun package-menu--status-predicate (A B)
1663 (let ((sA (aref (cadr A) 2))
1664 (sB (aref (cadr B) 2)))
1665 (cond ((string= sA sB)
1666 (package-menu--name-predicate A B))
1667 ((string= sA "new") t)
1668 ((string= sB "new") nil)
1669 ((string= sA "available") t)
1670 ((string= sB "available") nil)
1671 ((string= sA "installed") t)
1672 ((string= sB "installed") nil)
1673 ((string= sA "held") t)
1674 ((string= sB "held") nil)
1675 ((string= sA "built-in") t)
1676 ((string= sB "built-in") nil)
1677 ((string= sA "obsolete") t)
1678 ((string= sB "obsolete") nil)
1679 (t (string< sA sB)))))
1681 (defun package-menu--description-predicate (A B)
1682 (let ((dA (aref (cadr A) 3))
1683 (dB (aref (cadr B) 3)))
1685 (package-menu--name-predicate A B)
1688 (defun package-menu--name-predicate (A B)
1689 (string< (symbol-name (caar A))
1690 (symbol-name (caar B))))
1693 (defun list-packages (&optional no-fetch)
1694 "Display a list of packages.
1695 This first fetches the updated list of packages before
1696 displaying, unless a prefix argument NO-FETCH is specified.
1697 The list is displayed in a buffer named `*Packages*'."
1699 (require 'finder-inf nil t)
1700 ;; Initialize the package system if necessary.
1701 (unless package--initialized
1702 (package-initialize t))
1703 (let (old-archives new-packages)
1705 ;; Read the locally-cached archive-contents.
1706 (package-read-all-archive-contents)
1707 (setq old-archives package-archive-contents)
1708 ;; Fetch the remote list of packages.
1709 (package-refresh-contents)
1710 ;; Find which packages are new.
1711 (dolist (elt package-archive-contents)
1712 (unless (assq (car elt) old-archives)
1713 (push (car elt) new-packages))))
1715 ;; Generate the Package Menu.
1716 (let ((buf (get-buffer-create "*Packages*")))
1717 (with-current-buffer buf
1719 (set (make-local-variable 'package-menu--new-package-list)
1721 (package-menu--generate nil t))
1722 ;; The package menu buffer has keybindings. If the user types
1723 ;; `M-x list-packages', that suggests it should become current.
1724 (switch-to-buffer buf))
1726 (let ((upgrades (package-menu--find-upgrades)))
1728 (message "%d package%s can be upgraded; type `%s' to mark %s for upgrading."
1730 (if (= (length upgrades) 1) "" "s")
1731 (substitute-command-keys "\\[package-menu-mark-upgrades]")
1732 (if (= (length upgrades) 1) "it" "them"))))))
1735 (defalias 'package-list-packages 'list-packages)
1737 ;; Used in finder.el
1738 (defun package-show-package-list (packages)
1739 "Display PACKAGES in a *Packages* buffer.
1740 This is similar to `list-packages', but it does not fetch the
1741 updated list of packages, and it only displays packages with
1742 names in PACKAGES (which should be a list of symbols)."
1743 (require 'finder-inf nil t)
1744 (let ((buf (get-buffer-create "*Packages*")))
1745 (with-current-buffer buf
1747 (package-menu--generate nil packages))
1748 (switch-to-buffer buf)))
1750 (defun package-list-packages-no-fetch ()
1751 "Display a list of packages.
1752 Does not fetch the updated list of packages before displaying.
1753 The list is displayed in a buffer named `*Packages*'."
1759 ;;; package.el ends here