lisp/emacs-lisp/package.el (tar-get-file-descriptor, tar--extract): Declare.
[emacs.git] / lisp / emacs-lisp / package.el
blobe5833703ad53bc2b0ecf29d6ac6445594c1470b2
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
7 ;; Version: 1.0.1
8 ;; Keywords: tools
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/>.
26 ;;; Change Log:
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
34 ;;; Commentary:
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
43 ;; work.)
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:
79 ;; M-x list-packages
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
92 ;; packages).
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.
100 ;;; Thanks:
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>
106 ;; Lawrence Mitchell
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>
113 ;;; ToDo:
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
140 ;; installing it
141 ;; - Interface with desktop.el so that restarting after an install
142 ;; works properly
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
152 ;; why is that?
153 ;; - Allow multiple versions on the server...?
154 ;; [ why bother? ]
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 ;; - Our treatment of the info path is somewhat bogus
163 ;;; Code:
165 (eval-when-compile (require 'cl-lib))
167 (require 'tabulated-list)
169 (defgroup package nil
170 "Manager for Emacs Lisp packages."
171 :group 'applications
172 :version "24.1")
174 ;;;###autoload
175 (defcustom package-enable-at-startup t
176 "Whether to activate installed packages when Emacs starts.
177 If non-nil, packages are activated after reading the init file
178 and before `after-init-hook'. Activation is not done if
179 `user-init-file' is nil (e.g. Emacs was started with \"-q\").
181 Even if the value is nil, you can type \\[package-initialize] to
182 activate the package system at any time."
183 :type 'boolean
184 :group 'package
185 :version "24.1")
187 (defcustom package-load-list '(all)
188 "List of packages for `package-initialize' to load.
189 Each element in this list should be a list (NAME VERSION), or the
190 symbol `all'. The symbol `all' says to load the latest installed
191 versions of all packages not specified by other elements.
193 For an element (NAME VERSION), NAME is a package name (a symbol).
194 VERSION should be t, a string, or nil.
195 If VERSION is t, the most recent version is activated.
196 If VERSION is a string, only that version is ever loaded.
197 Any other version, even if newer, is silently ignored.
198 Hence, the package is \"held\" at that version.
199 If VERSION is nil, the package is not loaded (it is \"disabled\")."
200 :type '(repeat symbol)
201 :risky t
202 :group 'package
203 :version "24.1")
205 (defvar Info-directory-list)
206 (declare-function info-initialize "info" ())
207 (declare-function url-http-parse-response "url-http" ())
208 (declare-function lm-header "lisp-mnt" (header))
209 (declare-function lm-commentary "lisp-mnt" (&optional file))
210 (defvar url-http-end-of-headers)
212 (defcustom package-archives '(("gnu" . "http://elpa.gnu.org/packages/"))
213 "An alist of archives from which to fetch.
214 The default value points to the GNU Emacs package repository.
216 Each element has the form (ID . LOCATION).
217 ID is an archive name, as a string.
218 LOCATION specifies the base location for the archive.
219 If it starts with \"http:\", it is treated as a HTTP URL;
220 otherwise it should be an absolute directory name.
221 (Other types of URL are currently not supported.)
223 Only add locations that you trust, since fetching and installing
224 a package can run arbitrary code."
225 :type '(alist :key-type (string :tag "Archive name")
226 :value-type (string :tag "URL or directory name"))
227 :risky t
228 :group 'package
229 :version "24.1")
231 (defcustom package-pinned-packages nil
232 "An alist of packages that are pinned to a specific archive
234 Each element has the form (SYM . ID).
235 SYM is a package, as a symbol.
236 ID is an archive name, as a string. This should correspond to an
237 entry in `package-archives'.
239 If the archive of name ID does not contain the package SYM, no
240 other location will be considered, which will make the
241 package unavailable."
242 :type '(alist :key-type (symbol :tag "Package")
243 :value-type (string :tag "Archive name"))
244 :risky t
245 :group 'package
246 :version "24.4")
248 (defconst package-archive-version 1
249 "Version number of the package archive understood by this file.
250 Lower version numbers than this will probably be understood as well.")
252 (defconst package-el-version "1.0.1"
253 "Version of package.el.")
255 ;; We don't prime the cache since it tends to get out of date.
256 (defvar package-archive-contents nil
257 "Cache of the contents of the Emacs Lisp Package Archive.
258 This is an alist mapping package names (symbols) to
259 `package-desc' structures.")
260 (put 'package-archive-contents 'risky-local-variable t)
262 (defcustom package-user-dir (locate-user-emacs-file "elpa")
263 "Directory containing the user's Emacs Lisp packages.
264 The directory name should be absolute.
265 Apart from this directory, Emacs also looks for system-wide
266 packages in `package-directory-list'."
267 :type 'directory
268 :risky t
269 :group 'package
270 :version "24.1")
272 (defcustom package-directory-list
273 ;; Defaults are subdirs named "elpa" in the site-lisp dirs.
274 (let (result)
275 (dolist (f load-path)
276 (and (stringp f)
277 (equal (file-name-nondirectory f) "site-lisp")
278 (push (expand-file-name "elpa" f) result)))
279 (nreverse result))
280 "List of additional directories containing Emacs Lisp packages.
281 Each directory name should be absolute.
283 These directories contain packages intended for system-wide; in
284 contrast, `package-user-dir' contains packages for personal use."
285 :type '(repeat directory)
286 :risky t
287 :group 'package
288 :version "24.1")
290 (defvar package--default-summary "No description available.")
292 (cl-defstruct (package-desc
293 ;; Rename the default constructor from `make-package-desc'.
294 (:constructor package-desc-create)
295 ;; Has the same interface as the old `define-package',
296 ;; which is still used in the "foo-pkg.el" files. Extra
297 ;; options can be supported by adding additional keys.
298 (:constructor
299 package-desc-from-define
300 (name-string version-string &optional summary requirements
301 &key kind archive
302 &aux
303 (name (intern name-string))
304 (version (version-to-list version-string))
305 (reqs (mapcar #'(lambda (elt)
306 (list (car elt)
307 (version-to-list (cadr elt))))
308 (if (eq 'quote (car requirements))
309 (nth 1 requirements)
310 requirements))))))
311 "Structure containing information about an individual package.
313 Slots:
315 `name' Name of the package, as a symbol.
317 `version' Version of the package, as a version list.
319 `summary' Short description of the package, typically taken from
320 the first line of the file.
322 `reqs' Requirements of the package. A list of (PACKAGE
323 VERSION-LIST) naming the dependent package and the minimum
324 required version.
326 `kind' The distribution format of the package. Currently, it is
327 either `single' or `tar'.
329 `archive' The name of the archive (as a string) whence this
330 package came.
332 `dir' The directory where the package is installed (if installed)."
333 name
334 version
335 (summary package--default-summary)
336 reqs
337 kind
338 archive
339 dir)
341 ;; Pseudo fields.
342 (defun package-desc-full-name (pkg-desc)
343 (format "%s-%s"
344 (package-desc-name pkg-desc)
345 (package-version-join (package-desc-version pkg-desc))))
347 (defun package-desc-suffix (pkg-desc)
348 (pcase (package-desc-kind pkg-desc)
349 (`single ".el")
350 (`tar ".tar")
351 (kind (error "Unknown package kind: %s" kind))))
353 ;; Package descriptor format used in finder-inf.el and package--builtins.
354 (cl-defstruct (package--bi-desc
355 (:constructor package-make-builtin (version summary))
356 (:type vector))
357 version
358 reqs
359 summary)
361 (defvar package--builtins nil
362 "Alist of built-in packages.
363 The actual value is initialized by loading the library
364 `finder-inf'; this is not done until it is needed, e.g. by the
365 function `package-built-in-p'.
367 Each element has the form (PKG . PACKAGE-BI-DESC), where PKG is a package
368 name (a symbol) and DESC is a `package--bi-desc' structure.")
369 (put 'package--builtins 'risky-local-variable t)
371 (defvar package-alist nil
372 "Alist of all packages available for activation.
373 Each element has the form (PKG . DESCS), where PKG is a package
374 name (a symbol) and DESCS is a non-empty list of `package-desc' structure,
375 sorted by decreasing versions.
377 This variable is set automatically by `package-load-descriptor',
378 called via `package-initialize'. To change which packages are
379 loaded and/or activated, customize `package-load-list'.")
380 (put 'package-alist 'risky-local-variable t)
382 (defvar package-activated-list nil
383 ;; FIXME: This should implicitly include all builtin packages.
384 "List of the names of currently activated packages.")
385 (put 'package-activated-list 'risky-local-variable t)
387 (defun package-version-join (vlist)
388 "Return the version string corresponding to the list VLIST.
389 This is, approximately, the inverse of `version-to-list'.
390 \(Actually, it returns only one of the possible inverses, since
391 `version-to-list' is a many-to-one operation.)"
392 (if (null vlist)
394 (let ((str-list (list "." (int-to-string (car vlist)))))
395 (dolist (num (cdr vlist))
396 (cond
397 ((>= num 0)
398 (push (int-to-string num) str-list)
399 (push "." str-list))
400 ((< num -3)
401 (error "Invalid version list `%s'" vlist))
403 ;; pre, or beta, or alpha
404 (cond ((equal "." (car str-list))
405 (pop str-list))
406 ((not (string-match "[0-9]+" (car str-list)))
407 (error "Invalid version list `%s'" vlist)))
408 (push (cond ((= num -1) "pre")
409 ((= num -2) "beta")
410 ((= num -3) "alpha"))
411 str-list))))
412 (if (equal "." (car str-list))
413 (pop str-list))
414 (apply 'concat (nreverse str-list)))))
416 (defun package-load-descriptor (pkg-dir)
417 "Load the description file in directory PKG-DIR."
418 (let ((pkg-file (expand-file-name (package--description-file pkg-dir)
419 pkg-dir)))
420 (when (file-exists-p pkg-file)
421 (with-temp-buffer
422 (insert-file-contents pkg-file)
423 (goto-char (point-min))
424 (let ((pkg-desc (package-process-define-package
425 (read (current-buffer)) pkg-file)))
426 (setf (package-desc-dir pkg-desc) pkg-dir)
427 pkg-desc)))))
429 (defun package-load-all-descriptors ()
430 "Load descriptors for installed Emacs Lisp packages.
431 This looks for package subdirectories in `package-user-dir' and
432 `package-directory-list'. The variable `package-load-list'
433 controls which package subdirectories may be loaded.
435 In each valid package subdirectory, this function loads the
436 description file containing a call to `define-package', which
437 updates `package-alist'."
438 (dolist (dir (cons package-user-dir package-directory-list))
439 (when (file-directory-p dir)
440 (dolist (subdir (directory-files dir))
441 (let ((pkg-dir (expand-file-name subdir dir)))
442 (when (file-directory-p pkg-dir)
443 (package-load-descriptor pkg-dir)))))))
445 (defun package-disabled-p (pkg-name version)
446 "Return whether PKG-NAME at VERSION can be activated.
447 The decision is made according to `package-load-list'.
448 Return nil if the package can be activated.
449 Return t if the package is completely disabled.
450 Return the max version (as a string) if the package is held at a lower version."
451 (let ((force (assq pkg-name package-load-list)))
452 (cond ((null force) (not (memq 'all package-load-list)))
453 ((null (setq force (cadr force))) t) ; disabled
454 ((eq force t) nil)
455 ((stringp force) ; held
456 (unless (version-list-= version (version-to-list force))
457 force))
458 (t (error "Invalid element in `package-load-list'")))))
460 (defun package-activate-1 (pkg-desc)
461 (let* ((name (package-desc-name pkg-desc))
462 (pkg-dir (package-desc-dir pkg-desc)))
463 (unless pkg-dir
464 (error "Internal error: unable to find directory for `%s'"
465 (package-desc-full-name pkg-desc)))
466 ;; Add info node.
467 (when (file-exists-p (expand-file-name "dir" pkg-dir))
468 ;; FIXME: not the friendliest, but simple.
469 (require 'info)
470 (info-initialize)
471 (push pkg-dir Info-directory-list))
472 ;; Add to load path, add autoloads, and activate the package.
473 (push pkg-dir load-path)
474 (load (expand-file-name (format "%s-autoloads" name) pkg-dir) nil t)
475 (push name package-activated-list)
476 ;; Don't return nil.
479 (defun package-built-in-p (package &optional min-version)
480 "Return true if PACKAGE is built-in to Emacs.
481 Optional arg MIN-VERSION, if non-nil, should be a version list
482 specifying the minimum acceptable version."
483 (let ((bi (assq package package--builtin-versions)))
484 (cond
485 (bi (version-list-<= min-version (cdr bi)))
486 (min-version nil)
488 (require 'finder-inf nil t) ; For `package--builtins'.
489 (assq package package--builtins)))))
491 (defun package--from-builtin (bi-desc)
492 (package-desc-create :name (pop bi-desc)
493 :version (package--bi-desc-version bi-desc)
494 :summary (package--bi-desc-summary bi-desc)))
496 ;; This function goes ahead and activates a newer version of a package
497 ;; if an older one was already activated. This is not ideal; we'd at
498 ;; least need to check to see if the package has actually been loaded,
499 ;; and not merely activated.
500 (defun package-activate (package &optional force)
501 "Activate package PACKAGE.
502 If FORCE is true, (re-)activate it if it's already activated."
503 (let ((pkg-descs (cdr (assq package package-alist))))
504 ;; Check if PACKAGE is available in `package-alist'.
505 (while
506 (when pkg-descs
507 (let ((available-version (package-desc-version (car pkg-descs))))
508 (or (package-disabled-p package available-version)
509 ;; Prefer a builtin package.
510 (package-built-in-p package available-version))))
511 (setq pkg-descs (cdr pkg-descs)))
512 (cond
513 ;; If no such package is found, maybe it's built-in.
514 ((null pkg-descs)
515 (package-built-in-p package))
516 ;; If the package is already activated, just return t.
517 ((and (memq package package-activated-list) (not force))
519 ;; Otherwise, proceed with activation.
521 (let* ((pkg-vec (car pkg-descs))
522 (fail (catch 'dep-failure
523 ;; Activate its dependencies recursively.
524 (dolist (req (package-desc-reqs pkg-vec))
525 (unless (package-activate (car req) (cadr req))
526 (throw 'dep-failure req))))))
527 (if fail
528 (warn "Unable to activate package `%s'.
529 Required package `%s-%s' is unavailable"
530 package (car fail) (package-version-join (cadr fail)))
531 ;; If all goes well, activate the package itself.
532 (package-activate-1 pkg-vec)))))))
534 (defun define-package (_name-string _version-string
535 &optional _docstring _requirements
536 &rest _extra-properties)
537 "Define a new package.
538 NAME-STRING is the name of the package, as a string.
539 VERSION-STRING is the version of the package, as a string.
540 DOCSTRING is a short description of the package, a string.
541 REQUIREMENTS is a list of dependencies on other packages.
542 Each requirement is of the form (OTHER-PACKAGE OTHER-VERSION),
543 where OTHER-VERSION is a string.
545 EXTRA-PROPERTIES is currently unused."
546 ;; FIXME: Placeholder! Should we keep it?
547 (error "Don't call me!"))
549 (defun package-process-define-package (exp origin)
550 (unless (eq (car-safe exp) 'define-package)
551 (error "Can't find define-package in %s" origin))
552 (let* ((new-pkg-desc (apply #'package-desc-from-define (cdr exp)))
553 (name (package-desc-name new-pkg-desc))
554 (version (package-desc-version new-pkg-desc))
555 (old-pkgs (assq name package-alist)))
556 (if (null old-pkgs)
557 ;; If there's no old package, just add this to `package-alist'.
558 (push (list name new-pkg-desc) package-alist)
559 ;; If there is, insert the new package at the right place in the list.
560 (while old-pkgs
561 (cond
562 ((null (cdr old-pkgs)) (push new-pkg-desc (cdr old-pkgs)))
563 ((version-list-< (package-desc-version (cadr old-pkgs)) version)
564 (push new-pkg-desc (cdr old-pkgs))
565 (setq old-pkgs nil)))
566 (setq old-pkgs (cdr old-pkgs))))
567 new-pkg-desc))
569 ;; From Emacs 22, but changed so it adds to load-path.
570 (defun package-autoload-ensure-default-file (file)
571 "Make sure that the autoload file FILE exists and if not create it."
572 (unless (file-exists-p file)
573 (write-region
574 (concat ";;; " (file-name-nondirectory file)
575 " --- automatically extracted autoloads\n"
576 ";;\n"
577 ";;; Code:\n"
578 "(add-to-list 'load-path (or (file-name-directory #$) (car load-path)))\n"
579 "\f\n;; Local Variables:\n"
580 ";; version-control: never\n"
581 ";; no-byte-compile: t\n"
582 ";; no-update-autoloads: t\n"
583 ";; End:\n"
584 ";;; " (file-name-nondirectory file)
585 " ends here\n")
586 nil file))
587 file)
589 (defun package-generate-autoloads (name pkg-dir)
590 (require 'autoload) ;Load before we let-bind generated-autoload-file!
591 (let* ((auto-name (format "%s-autoloads.el" name))
592 ;;(ignore-name (concat name "-pkg.el"))
593 (generated-autoload-file (expand-file-name auto-name pkg-dir))
594 (version-control 'never))
595 (package-autoload-ensure-default-file generated-autoload-file)
596 (update-directory-autoloads pkg-dir)
597 (let ((buf (find-buffer-visiting generated-autoload-file)))
598 (when buf (kill-buffer buf)))
599 auto-name))
601 (defvar tar-parse-info)
602 (declare-function tar-untar-buffer "tar-mode" ())
603 (declare-function tar-header-name "tar-mode" (tar-header) t)
604 (declare-function tar-header-link-type "tar-mode" (tar-header) t)
606 (defun package-untar-buffer (dir)
607 "Untar the current buffer.
608 This uses `tar-untar-buffer' from Tar mode. All files should
609 untar into a directory named DIR; otherwise, signal an error."
610 (require 'tar-mode)
611 (tar-mode)
612 ;; Make sure everything extracts into DIR.
613 (let ((regexp (concat "\\`" (regexp-quote (expand-file-name dir)) "/"))
614 (case-fold-search (memq system-type '(windows-nt ms-dos cygwin))))
615 (dolist (tar-data tar-parse-info)
616 (let ((name (expand-file-name (tar-header-name tar-data))))
617 (or (string-match regexp name)
618 ;; Tarballs created by some utilities don't list
619 ;; directories with a trailing slash (Bug#13136).
620 (and (string-equal dir name)
621 (eq (tar-header-link-type tar-data) 5))
622 (error "Package does not untar cleanly into directory %s/" dir)))))
623 (tar-untar-buffer))
625 (defun package-generate-description-file (pkg-desc pkg-dir)
626 "Create the foo-pkg.el file for single-file packages."
627 (let* ((name (package-desc-name pkg-desc))
628 (pkg-file (expand-file-name (package--description-file pkg-dir)
629 pkg-dir)))
630 (let ((print-level nil)
631 (print-quoted t)
632 (print-length nil))
633 (write-region
634 (concat
635 (prin1-to-string
636 (list 'define-package
637 (symbol-name name)
638 (package-version-join (package-desc-version pkg-desc))
639 (package-desc-summary pkg-desc)
640 (let ((requires (package-desc-reqs pkg-desc)))
641 (list 'quote
642 ;; Turn version lists into string form.
643 (mapcar
644 (lambda (elt)
645 (list (car elt)
646 (package-version-join (cadr elt))))
647 requires)))))
648 "\n")
650 pkg-file))))
652 (defun package-unpack (pkg-desc)
653 "Install the contents of the current buffer as a package."
654 (let* ((name (package-desc-name pkg-desc))
655 (dirname (package-desc-full-name pkg-desc))
656 (pkg-dir (expand-file-name dirname package-user-dir)))
657 (pcase (package-desc-kind pkg-desc)
658 (`tar
659 (make-directory package-user-dir t)
660 ;; FIXME: should we delete PKG-DIR if it exists?
661 (let* ((default-directory (file-name-as-directory package-user-dir)))
662 (package-untar-buffer dirname)))
663 (`single
664 (let ((el-file (expand-file-name (format "%s.el" name) pkg-dir)))
665 (make-directory pkg-dir t)
666 (package--write-file-no-coding el-file)))
667 (kind (error "Unknown package kind: %S" kind)))
668 (package--make-autoloads-and-stuff pkg-desc pkg-dir)
669 ;; Update package-alist.
670 (let ((new-desc (package-load-descriptor pkg-dir)))
671 ;; FIXME: Check that `new-desc' matches `desc'!
672 ;; FIXME: Compilation should be done as a separate, optional, step.
673 ;; E.g. for multi-package installs, we should first install all packages
674 ;; and then compile them.
675 (package--compile new-desc))
676 ;; Try to activate it.
677 (package-activate name 'force)
678 pkg-dir))
680 (defun package--make-autoloads-and-stuff (pkg-desc pkg-dir)
681 "Generate autoloads, description file, etc.. for PKG-DESC installed at PKG-DIR."
682 (package-generate-autoloads (package-desc-name pkg-desc) pkg-dir)
683 (let ((desc-file (package--description-file pkg-dir)))
684 (unless (file-exists-p desc-file)
685 (package-generate-description-file pkg-desc pkg-dir)))
686 ;; FIXME: Create foo.info and dir file from foo.texi?
689 (defun package--compile (pkg-desc)
690 "Byte-compile installed package PKG-DESC."
691 (package-activate-1 pkg-desc)
692 (byte-recompile-directory (package-desc-dir pkg-desc) 0 t))
694 (defun package--write-file-no-coding (file-name)
695 (let ((buffer-file-coding-system 'no-conversion))
696 (write-region (point-min) (point-max) file-name)))
698 (defmacro package--with-work-buffer (location file &rest body)
699 "Run BODY in a buffer containing the contents of FILE at LOCATION.
700 LOCATION is the base location of a package archive, and should be
701 one of the URLs (or file names) specified in `package-archives'.
702 FILE is the name of a file relative to that base location.
704 This macro retrieves FILE from LOCATION into a temporary buffer,
705 and evaluates BODY while that buffer is current. This work
706 buffer is killed afterwards. Return the last value in BODY."
707 (declare (indent 2) (debug t))
708 `(let* ((http (string-match "\\`https?:" ,location))
709 (buffer
710 (if http
711 (url-retrieve-synchronously (concat ,location ,file))
712 (generate-new-buffer "*package work buffer*"))))
713 (prog1
714 (with-current-buffer buffer
715 (if http
716 (progn (package-handle-response)
717 (re-search-forward "^$" nil 'move)
718 (forward-char)
719 (delete-region (point-min) (point)))
720 (unless (file-name-absolute-p ,location)
721 (error "Archive location %s is not an absolute file name"
722 ,location))
723 (insert-file-contents (expand-file-name ,file ,location)))
724 ,@body)
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.
734 (require 'url-http)
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) (line-end-position))))))
740 (defun package-install-from-archive (pkg-desc)
741 "Download and install a tar package."
742 (let ((location (package-archive-base pkg-desc))
743 (file (concat (package-desc-full-name pkg-desc)
744 (package-desc-suffix pkg-desc))))
745 (package--with-work-buffer location file
746 (package-unpack pkg-desc))))
748 (defvar package--initialized nil)
750 (defun package-installed-p (package &optional min-version)
751 "Return true if PACKAGE, of MIN-VERSION or newer, is installed.
752 MIN-VERSION should be a version list."
753 (unless package--initialized (error "package.el is not yet initialized!"))
755 (let ((pkg-descs (cdr (assq package package-alist))))
756 (and pkg-descs
757 (version-list-<= min-version
758 (package-desc-version (car pkg-descs)))))
759 ;; Also check built-in packages.
760 (package-built-in-p package min-version)))
762 (defun package-compute-transaction (package-list requirements)
763 "Return a list of packages to be installed, including PACKAGE-LIST.
764 PACKAGE-LIST should be a list of package names (symbols).
766 REQUIREMENTS should be a list of additional requirements; each
767 element in this list should have the form (PACKAGE VERSION-LIST),
768 where PACKAGE is a package name and VERSION-LIST is the required
769 version of that package.
771 This function recursively computes the requirements of the
772 packages in REQUIREMENTS, and returns a list of all the packages
773 that must be installed. Packages that are already installed are
774 not included in this list."
775 (dolist (elt requirements)
776 (let* ((next-pkg (car elt))
777 (next-version (cadr elt)))
778 (unless (package-installed-p next-pkg next-version)
779 ;; A package is required, but not installed. It might also be
780 ;; blocked via `package-load-list'.
781 (let ((pkg-desc (cdr (assq next-pkg package-archive-contents)))
782 ;; FIXME: package-disabled-p needs to use a <= test!
783 (disabled (package-disabled-p next-pkg next-version)))
784 (when disabled
785 (if (stringp disabled)
786 (error "Package `%s' held at version %s, \
787 but version %s required"
788 (symbol-name next-pkg) disabled
789 (package-version-join next-version))
790 (error "Required package '%s' is disabled"
791 (symbol-name next-pkg))))
792 (unless pkg-desc
793 (error "Package `%s-%s' is unavailable"
794 (symbol-name next-pkg)
795 (package-version-join next-version)))
796 (unless (version-list-<= next-version
797 (package-desc-version pkg-desc))
798 (error
799 "Need package `%s-%s', but only %s is available"
800 (symbol-name next-pkg) (package-version-join next-version)
801 (package-version-join (package-desc-version pkg-desc))))
802 ;; Move to front, so it gets installed early enough (bug#14082).
803 (setq package-list (cons next-pkg (delq next-pkg package-list)))
804 (setq package-list
805 (package-compute-transaction package-list
806 (package-desc-reqs
807 pkg-desc)))))))
808 package-list)
810 (defun package-read-from-string (str)
811 "Read a Lisp expression from STR.
812 Signal an error if the entire string was not used."
813 (let* ((read-data (read-from-string str))
814 (more-left
815 (condition-case nil
816 ;; The call to `ignore' suppresses a compiler warning.
817 (progn (ignore (read-from-string
818 (substring str (cdr read-data))))
820 (end-of-file nil))))
821 (if more-left
822 (error "Can't read whole string")
823 (car read-data))))
825 (defun package--read-archive-file (file)
826 "Re-read archive file FILE, if it exists.
827 Will return the data from the file, or nil if the file does not exist.
828 Will throw an error if the archive version is too new."
829 (let ((filename (expand-file-name file package-user-dir)))
830 (when (file-exists-p filename)
831 (with-temp-buffer
832 (insert-file-contents-literally filename)
833 (let ((contents (read (current-buffer))))
834 (if (> (car contents) package-archive-version)
835 (error "Package archive version %d is higher than %d"
836 (car contents) package-archive-version))
837 (cdr contents))))))
839 (defun package-read-all-archive-contents ()
840 "Re-read `archive-contents', if it exists.
841 If successful, set `package-archive-contents'."
842 (setq package-archive-contents nil)
843 (dolist (archive package-archives)
844 (package-read-archive-contents (car archive))))
846 (defun package-read-archive-contents (archive)
847 "Re-read archive contents for ARCHIVE.
848 If successful, set the variable `package-archive-contents'.
849 If the archive version is too new, signal an error."
850 ;; Version 1 of 'archive-contents' is identical to our internal
851 ;; representation.
852 (let* ((contents-file (format "archives/%s/archive-contents" archive))
853 (contents (package--read-archive-file contents-file)))
854 (when contents
855 (dolist (package contents)
856 (package--add-to-archive-contents package archive)))))
858 ;; Package descriptor objects used inside the "archive-contents" file.
859 ;; Changing this defstruct implies changing the format of the
860 ;; "archive-contents" files.
861 (cl-defstruct (package--ac-desc
862 (:constructor package-make-ac-desc (version reqs summary kind))
863 (:copier nil)
864 (:type vector))
865 version reqs summary kind)
867 (defun package--add-to-archive-contents (package archive)
868 "Add the PACKAGE from the given ARCHIVE if necessary.
869 PACKAGE should have the form (NAME . PACKAGE--AC-DESC).
870 Also, add the originating archive to the `package-desc' structure."
871 (let* ((name (car package))
872 (version (package--ac-desc-version (cdr package)))
873 (pkg-desc
874 (package-desc-create
875 :name name
876 :version version
877 :reqs (package--ac-desc-reqs (cdr package))
878 :summary (package--ac-desc-summary (cdr package))
879 :kind (package--ac-desc-kind (cdr package))
880 :archive archive))
881 (entry (cons name pkg-desc))
882 (existing-package (assq name package-archive-contents))
883 (pinned-to-archive (assoc name package-pinned-packages)))
884 (cond
885 ;; Skip entirely if pinned to another archive or if no more recent
886 ;; than what we already have installed.
887 ((or (and pinned-to-archive
888 (not (equal (cdr pinned-to-archive) archive)))
889 (let ((bi (assq name package--builtin-versions)))
890 (and bi (version-list-<= version (cdr bi))))
891 (let ((ins (cdr (assq name package-alist))))
892 (and ins (version-list-<= version
893 (package-desc-version (car ins))))))
894 nil)
895 ((not existing-package)
896 (push entry package-archive-contents))
897 ((version-list-< (package-desc-version (cdr existing-package))
898 version)
899 ;; Replace the entry with this one.
900 (setq package-archive-contents
901 (cons entry
902 (delq existing-package
903 package-archive-contents)))))))
905 (defun package-download-transaction (package-list)
906 "Download and install all the packages in PACKAGE-LIST.
907 PACKAGE-LIST should be a list of package names (symbols).
908 This function assumes that all package requirements in
909 PACKAGE-LIST are satisfied, i.e. that PACKAGE-LIST is computed
910 using `package-compute-transaction'."
911 ;; FIXME: make package-list a list of pkg-desc.
912 (dolist (elt package-list)
913 (let ((desc (cdr (assq elt package-archive-contents))))
914 (package-install-from-archive desc))))
916 ;;;###autoload
917 (defun package-install (pkg-desc)
918 "Install the package PKG-DESC.
919 PKG-DESC should be one of the available packages in an
920 archive in `package-archives'. Interactively, prompt for its name."
921 (interactive
922 (progn
923 ;; Initialize the package system to get the list of package
924 ;; symbols for completion.
925 (unless package--initialized
926 (package-initialize t))
927 (unless package-archive-contents
928 (package-refresh-contents))
929 (let* ((name (intern (completing-read
930 "Install package: "
931 (mapcar (lambda (elt)
932 (cons (symbol-name (car elt))
933 nil))
934 package-archive-contents)
935 nil t)))
936 (pkg-desc (cdr (assq name package-archive-contents))))
937 (unless pkg-desc
938 (error "Package `%s' is not available for installation"
939 name))
940 (list pkg-desc))))
941 (package-download-transaction
942 ;; FIXME: Use (list pkg-desc) instead of just the name.
943 (package-compute-transaction (list (package-desc-name pkg-desc))
944 (package-desc-reqs pkg-desc))))
946 (defun package-strip-rcs-id (str)
947 "Strip RCS version ID from the version string STR.
948 If the result looks like a dotted numeric version, return it.
949 Otherwise return nil."
950 (when str
951 (when (string-match "\\`[ \t]*[$]Revision:[ \t]+" str)
952 (setq str (substring str (match-end 0))))
953 (condition-case nil
954 (if (version-to-list str)
955 str)
956 (error nil))))
958 (defun package-buffer-info ()
959 "Return a `package-desc' describing the package in the current buffer.
961 If the buffer does not contain a conforming package, signal an
962 error. If there is a package, narrow the buffer to the file's
963 boundaries."
964 (goto-char (point-min))
965 (unless (re-search-forward "^;;; \\([^ ]*\\)\\.el ---[ \t]*\\(.*?\\)[ \t]*\\(-\\*-.*-\\*-[ \t]*\\)?$" nil t)
966 (error "Packages lacks a file header"))
967 (let ((file-name (match-string-no-properties 1))
968 (desc (match-string-no-properties 2))
969 (start (line-beginning-position)))
970 (unless (search-forward (concat ";;; " file-name ".el ends here"))
971 (error "Package lacks a terminating comment"))
972 ;; Try to include a trailing newline.
973 (forward-line)
974 (narrow-to-region start (point))
975 (require 'lisp-mnt)
976 ;; Use some headers we've invented to drive the process.
977 (let* ((requires-str (lm-header "package-requires"))
978 ;; Prefer Package-Version; if defined, the package author
979 ;; probably wants us to use it. Otherwise try Version.
980 (pkg-version
981 (or (package-strip-rcs-id (lm-header "package-version"))
982 (package-strip-rcs-id (lm-header "version")))))
983 (unless pkg-version
984 (error
985 "Package lacks a \"Version\" or \"Package-Version\" header"))
986 (package-desc-from-define
987 file-name pkg-version desc
988 (if requires-str (package-read-from-string requires-str))
989 :kind 'single))))
991 (declare-function tar-get-file-descriptor "tar-mode" (file))
992 (declare-function tar--extract "tar-mode" (descriptor))
994 (defun package-tar-file-info ()
995 "Find package information for a tar file.
996 The return result is a `package-desc'."
997 (cl-assert (derived-mode-p 'tar-mode))
998 (let* ((dir-name (file-name-directory
999 (tar-header-name (car tar-parse-info))))
1000 (desc-file (package--description-file dir-name))
1001 (tar-desc (tar-get-file-descriptor (concat dir-name desc-file))))
1002 (unless tar-desc
1003 (error "No package descriptor file found"))
1004 (with-current-buffer (tar--extract tar-desc)
1005 (goto-char (point-min))
1006 (unwind-protect
1007 (let* ((pkg-def-parsed (read (current-buffer)))
1008 (pkg-desc
1009 (if (not (eq (car pkg-def-parsed) 'define-package))
1010 (error "Can't find define-package in %s"
1011 (tar-header-name tar-desc))
1012 (apply #'package-desc-from-define
1013 (append (cdr pkg-def-parsed))))))
1014 (setf (package-desc-kind pkg-desc) 'tar)
1015 pkg-desc)
1016 (kill-buffer (current-buffer))))))
1019 ;;;###autoload
1020 (defun package-install-from-buffer ()
1021 "Install a package from the current buffer.
1022 The current buffer is assumed to be a single .el or .tar file that follows the
1023 packaging guidelines; see info node `(elisp)Packaging'.
1024 Downloads and installs required packages as needed."
1025 (interactive)
1026 (let ((pkg-desc (if (derived-mode-p 'tar-mode)
1027 (package-tar-file-info)
1028 (package-buffer-info))))
1029 ;; Download and install the dependencies.
1030 (let* ((requires (package-desc-reqs pkg-desc))
1031 (transaction (package-compute-transaction nil requires)))
1032 (package-download-transaction transaction))
1033 ;; Install the package itself.
1034 (package-unpack pkg-desc)
1035 pkg-desc))
1037 ;;;###autoload
1038 (defun package-install-file (file)
1039 "Install a package from a file.
1040 The file can either be a tar file or an Emacs Lisp file."
1041 (interactive "fPackage file name: ")
1042 (with-temp-buffer
1043 (insert-file-contents-literally file)
1044 (when (string-match "\\.tar\\'" file) (tar-mode))
1045 (package-install-from-buffer)))
1047 (defun package-delete (pkg-desc)
1048 (let ((dir (package-desc-dir pkg-desc)))
1049 (if (string-equal (file-name-directory dir)
1050 (file-name-as-directory
1051 (expand-file-name package-user-dir)))
1052 (progn
1053 (delete-directory dir t t)
1054 (message "Package `%s' deleted." (package-desc-full-name pkg-desc)))
1055 ;; Don't delete "system" packages
1056 (error "Package `%s' is a system package, not deleting"
1057 (package-desc-full-name pkg-desc)))))
1059 (defun package-archive-base (desc)
1060 "Return the archive containing the package NAME."
1061 (cdr (assoc (package-desc-archive desc) package-archives)))
1063 (defun package--download-one-archive (archive file)
1064 "Retrieve an archive file FILE from ARCHIVE, and cache it.
1065 ARCHIVE should be a cons cell of the form (NAME . LOCATION),
1066 similar to an entry in `package-alist'. Save the cached copy to
1067 \"archives/NAME/archive-contents\" in `package-user-dir'."
1068 (let* ((dir (expand-file-name (format "archives/%s" (car archive))
1069 package-user-dir)))
1070 (package--with-work-buffer (cdr archive) file
1071 ;; Read the retrieved buffer to make sure it is valid (e.g. it
1072 ;; may fetch a URL redirect page).
1073 (when (listp (read buffer))
1074 (make-directory dir t)
1075 (setq buffer-file-name (expand-file-name file dir))
1076 (let ((version-control 'never))
1077 (save-buffer))))))
1079 ;;;###autoload
1080 (defun package-refresh-contents ()
1081 "Download the ELPA archive description if needed.
1082 This informs Emacs about the latest versions of all packages, and
1083 makes them available for download."
1084 (interactive)
1085 (unless (file-exists-p package-user-dir)
1086 (make-directory package-user-dir t))
1087 (dolist (archive package-archives)
1088 (condition-case-unless-debug nil
1089 (package--download-one-archive archive "archive-contents")
1090 (error (message "Failed to download `%s' archive."
1091 (car archive)))))
1092 (package-read-all-archive-contents))
1094 ;;;###autoload
1095 (defun package-initialize (&optional no-activate)
1096 "Load Emacs Lisp packages, and activate them.
1097 The variable `package-load-list' controls which packages to load.
1098 If optional arg NO-ACTIVATE is non-nil, don't activate packages."
1099 (interactive)
1100 (setq package-alist nil)
1101 (package-load-all-descriptors)
1102 (package-read-all-archive-contents)
1103 (unless no-activate
1104 (dolist (elt package-alist)
1105 (package-activate (car elt))))
1106 (setq package--initialized t))
1109 ;;;; Package description buffer.
1111 ;;;###autoload
1112 (defun describe-package (package)
1113 "Display the full documentation of PACKAGE (a symbol)."
1114 (interactive
1115 (let* ((guess (function-called-at-point))
1116 packages val)
1117 (require 'finder-inf nil t)
1118 ;; Load the package list if necessary (but don't activate them).
1119 (unless package--initialized
1120 (package-initialize t))
1121 (setq packages (append (mapcar 'car package-alist)
1122 (mapcar 'car package-archive-contents)
1123 (mapcar 'car package--builtins)))
1124 (unless (memq guess packages)
1125 (setq guess nil))
1126 (setq packages (mapcar 'symbol-name packages))
1127 (setq val
1128 (completing-read (if guess
1129 (format "Describe package (default %s): "
1130 guess)
1131 "Describe package: ")
1132 packages nil t nil nil guess))
1133 (list (if (equal val "") guess (intern val)))))
1134 (if (not (and package (symbolp package)))
1135 (message "No package specified")
1136 (help-setup-xref (list #'describe-package package)
1137 (called-interactively-p 'interactive))
1138 (with-help-window (help-buffer)
1139 (with-current-buffer standard-output
1140 (describe-package-1 package)))))
1142 (defun describe-package-1 (package)
1143 (require 'lisp-mnt)
1144 (let ((package-name (symbol-name package))
1145 (built-in (assq package package--builtins))
1146 desc pkg-dir reqs version installable archive)
1147 (prin1 package)
1148 (princ " is ")
1149 (cond
1150 ;; Loaded packages are in `package-alist'.
1151 ((setq desc (cadr (assq package package-alist)))
1152 (setq version (package-version-join (package-desc-version desc)))
1153 (if (setq pkg-dir (package-desc-dir desc))
1154 (insert "an installed package.\n\n")
1155 ;; This normally does not happen.
1156 (insert "a deleted package.\n\n")))
1157 ;; Available packages are in `package-archive-contents'.
1158 ((setq desc (cdr (assq package package-archive-contents)))
1159 (setq version (package-version-join (package-desc-version desc))
1160 archive (package-desc-archive desc)
1161 installable t)
1162 (if built-in
1163 (insert "a built-in package.\n\n")
1164 (insert "an uninstalled package.\n\n")))
1165 (built-in
1166 (setq desc (package--from-builtin built-in)
1167 version (package-version-join (package-desc-version desc)))
1168 (insert "a built-in package.\n\n"))
1170 (insert "an orphan package.\n\n")))
1172 (insert " " (propertize "Status" 'font-lock-face 'bold) ": ")
1173 (cond (pkg-dir
1174 (insert (propertize "Installed"
1175 'font-lock-face 'font-lock-comment-face))
1176 (insert " in `")
1177 ;; Todo: Add button for uninstalling.
1178 (help-insert-xref-button (file-name-as-directory pkg-dir)
1179 'help-package-def pkg-dir)
1180 (if built-in
1181 (insert "',\n shadowing a "
1182 (propertize "built-in package"
1183 'font-lock-face 'font-lock-builtin-face)
1184 ".")
1185 (insert "'.")))
1186 (installable
1187 (if built-in
1188 (insert (propertize "Built-in."
1189 'font-lock-face 'font-lock-builtin-face)
1190 " Alternate version available")
1191 (insert "Available"))
1192 (insert " from " archive)
1193 (insert " -- ")
1194 (let ((button-text (if (display-graphic-p) "Install" "[Install]"))
1195 (button-face (if (display-graphic-p)
1196 '(:box (:line-width 2 :color "dark grey")
1197 :background "light grey"
1198 :foreground "black")
1199 'link)))
1200 (insert-text-button button-text 'face button-face 'follow-link t
1201 'package-desc desc
1202 'action 'package-install-button-action)))
1203 (built-in
1204 (insert (propertize "Built-in."
1205 'font-lock-face 'font-lock-builtin-face)))
1206 (t (insert "Deleted.")))
1207 (insert "\n")
1208 (and version (> (length version) 0)
1209 (insert " "
1210 (propertize "Version" 'font-lock-face 'bold) ": " version "\n"))
1212 (setq reqs (if desc (package-desc-reqs desc)))
1213 (when reqs
1214 (insert " " (propertize "Requires" 'font-lock-face 'bold) ": ")
1215 (let ((first t)
1216 name vers text)
1217 (dolist (req reqs)
1218 (setq name (car req)
1219 vers (cadr req)
1220 text (format "%s-%s" (symbol-name name)
1221 (package-version-join vers)))
1222 (cond (first (setq first nil))
1223 ((>= (+ 2 (current-column) (length text))
1224 (window-width))
1225 (insert ",\n "))
1226 (t (insert ", ")))
1227 (help-insert-xref-button text 'help-package name))
1228 (insert "\n")))
1229 (insert " " (propertize "Summary" 'font-lock-face 'bold)
1230 ": " (if desc (package-desc-summary desc)) "\n\n")
1232 (if built-in
1233 ;; For built-in packages, insert the commentary.
1234 (let ((fn (locate-file (concat package-name ".el") load-path
1235 load-file-rep-suffixes))
1236 (opoint (point)))
1237 (insert (or (lm-commentary fn) ""))
1238 (save-excursion
1239 (goto-char opoint)
1240 (when (re-search-forward "^;;; Commentary:\n" nil t)
1241 (replace-match ""))
1242 (while (re-search-forward "^\\(;+ ?\\)" nil t)
1243 (replace-match ""))))
1244 (let ((readme (expand-file-name (concat package-name "-readme.txt")
1245 package-user-dir))
1246 readme-string)
1247 ;; For elpa packages, try downloading the commentary. If that
1248 ;; fails, try an existing readme file in `package-user-dir'.
1249 (cond ((condition-case nil
1250 (package--with-work-buffer (package-archive-base desc)
1251 (concat package-name "-readme.txt")
1252 (setq buffer-file-name
1253 (expand-file-name readme package-user-dir))
1254 (let ((version-control 'never))
1255 (save-buffer))
1256 (setq readme-string (buffer-string))
1258 (error nil))
1259 (insert readme-string))
1260 ((file-readable-p readme)
1261 (insert-file-contents readme)
1262 (goto-char (point-max))))))))
1264 (defun package-install-button-action (button)
1265 (let ((pkg-desc (button-get button 'package-desc)))
1266 (when (y-or-n-p (format "Install package `%s'? "
1267 (package-desc-full-name pkg-desc)))
1268 (package-install pkg-desc)
1269 (revert-buffer nil t)
1270 (goto-char (point-min)))))
1273 ;;;; Package menu mode.
1275 (defvar package-menu-mode-map
1276 (let ((map (make-sparse-keymap))
1277 (menu-map (make-sparse-keymap "Package")))
1278 (set-keymap-parent map tabulated-list-mode-map)
1279 (define-key map "\C-m" 'package-menu-describe-package)
1280 (define-key map "u" 'package-menu-mark-unmark)
1281 (define-key map "\177" 'package-menu-backup-unmark)
1282 (define-key map "d" 'package-menu-mark-delete)
1283 (define-key map "i" 'package-menu-mark-install)
1284 (define-key map "U" 'package-menu-mark-upgrades)
1285 (define-key map "r" 'package-menu-refresh)
1286 (define-key map "~" 'package-menu-mark-obsolete-for-deletion)
1287 (define-key map "x" 'package-menu-execute)
1288 (define-key map "h" 'package-menu-quick-help)
1289 (define-key map "?" 'package-menu-describe-package)
1290 (define-key map [menu-bar package-menu] (cons "Package" menu-map))
1291 (define-key menu-map [mq]
1292 '(menu-item "Quit" quit-window
1293 :help "Quit package selection"))
1294 (define-key menu-map [s1] '("--"))
1295 (define-key menu-map [mn]
1296 '(menu-item "Next" next-line
1297 :help "Next Line"))
1298 (define-key menu-map [mp]
1299 '(menu-item "Previous" previous-line
1300 :help "Previous Line"))
1301 (define-key menu-map [s2] '("--"))
1302 (define-key menu-map [mu]
1303 '(menu-item "Unmark" package-menu-mark-unmark
1304 :help "Clear any marks on a package and move to the next line"))
1305 (define-key menu-map [munm]
1306 '(menu-item "Unmark Backwards" package-menu-backup-unmark
1307 :help "Back up one line and clear any marks on that package"))
1308 (define-key menu-map [md]
1309 '(menu-item "Mark for Deletion" package-menu-mark-delete
1310 :help "Mark a package for deletion and move to the next line"))
1311 (define-key menu-map [mi]
1312 '(menu-item "Mark for Install" package-menu-mark-install
1313 :help "Mark a package for installation and move to the next line"))
1314 (define-key menu-map [mupgrades]
1315 '(menu-item "Mark Upgradable Packages" package-menu-mark-upgrades
1316 :help "Mark packages that have a newer version for upgrading"))
1317 (define-key menu-map [s3] '("--"))
1318 (define-key menu-map [mg]
1319 '(menu-item "Update Package List" revert-buffer
1320 :help "Update the list of packages"))
1321 (define-key menu-map [mr]
1322 '(menu-item "Refresh Package List" package-menu-refresh
1323 :help "Download the ELPA archive"))
1324 (define-key menu-map [s4] '("--"))
1325 (define-key menu-map [mt]
1326 '(menu-item "Mark Obsolete Packages" package-menu-mark-obsolete-for-deletion
1327 :help "Mark all obsolete packages for deletion"))
1328 (define-key menu-map [mx]
1329 '(menu-item "Execute Actions" package-menu-execute
1330 :help "Perform all the marked actions"))
1331 (define-key menu-map [s5] '("--"))
1332 (define-key menu-map [mh]
1333 '(menu-item "Help" package-menu-quick-help
1334 :help "Show short key binding help for package-menu-mode"))
1335 (define-key menu-map [mc]
1336 '(menu-item "View Commentary" package-menu-view-commentary
1337 :help "Display information about this package"))
1338 map)
1339 "Local keymap for `package-menu-mode' buffers.")
1341 (defvar package-menu--new-package-list nil
1342 "List of newly-available packages since `list-packages' was last called.")
1344 (define-derived-mode package-menu-mode tabulated-list-mode "Package Menu"
1345 "Major mode for browsing a list of packages.
1346 Letters do not insert themselves; instead, they are commands.
1347 \\<package-menu-mode-map>
1348 \\{package-menu-mode-map}"
1349 (setq tabulated-list-format [("Package" 18 package-menu--name-predicate)
1350 ("Version" 12 nil)
1351 ("Status" 10 package-menu--status-predicate)
1352 ("Description" 0 nil)])
1353 (setq tabulated-list-padding 2)
1354 (setq tabulated-list-sort-key (cons "Status" nil))
1355 (tabulated-list-init-header))
1357 (defmacro package--push (pkg-desc status listname)
1358 "Convenience macro for `package-menu--generate'.
1359 If the alist stored in the symbol LISTNAME lacks an entry for a
1360 package PKG-DESC, add one. The alist is keyed with PKG-DESC."
1361 `(unless (assoc ,pkg-desc ,listname)
1362 ;; FIXME: Should we move status into pkg-desc?
1363 (push (cons ,pkg-desc ,status) ,listname)))
1365 (defvar package-list-unversioned nil
1366 "If non-nil include packages that don't have a version in `list-package'.")
1368 (defun package-menu--generate (remember-pos packages)
1369 "Populate the Package Menu.
1370 If REMEMBER-POS is non-nil, keep point on the same entry.
1371 PACKAGES should be t, which means to display all known packages,
1372 or a list of package names (symbols) to display."
1373 ;; Construct list of (PKG-DESC . STATUS).
1374 (let (info-list name)
1375 ;; Installed packages:
1376 (dolist (elt package-alist)
1377 (setq name (car elt))
1378 (when (or (eq packages t) (memq name packages))
1379 (let* ((lle (assq name package-load-list))
1380 (held (cadr lle))
1381 (hv (if (stringp held) (version-to-list held))))
1382 (dolist (pkg (cdr elt))
1383 (let ((version (package-desc-version pkg)))
1384 (package--push pkg
1385 (cond
1386 ((and lle (null held)) "disabled")
1388 (cond
1389 ((version-list-= version hv) "held")
1390 ((version-list-< version hv) "obsolete")
1391 (t "disabled")))
1392 ((package-built-in-p name version) "obsolete")
1393 ((eq pkg (cadr elt)) "installed")
1394 (t "obsolete"))
1395 info-list))))))
1397 ;; Built-in packages:
1398 (dolist (elt package--builtins)
1399 (setq name (car elt))
1400 (when (and (not (eq name 'emacs)) ; Hide the `emacs' package.
1401 (or package-list-unversioned
1402 (package--bi-desc-version (cdr elt)))
1403 (or (eq packages t) (memq name packages)))
1404 (package--push (package--from-builtin elt) "built-in" info-list)))
1406 ;; Available and disabled packages:
1407 (dolist (elt package-archive-contents)
1408 (setq name (car elt))
1409 (when (or (eq packages t) (memq name packages))
1410 (let ((hold (assq name package-load-list)))
1411 (package--push (cdr elt)
1412 (cond
1413 ((and hold (null (cadr hold))) "disabled")
1414 ((memq name package-menu--new-package-list) "new")
1415 (t "available"))
1416 info-list))))
1418 ;; Print the result.
1419 (setq tabulated-list-entries (mapcar 'package-menu--print-info info-list))
1420 (tabulated-list-print remember-pos)))
1422 (defun package-menu--print-info (pkg)
1423 "Return a package entry suitable for `tabulated-list-entries'.
1424 PKG has the form (PKG-DESC . STATUS).
1425 Return (PKG-DESC [NAME VERSION STATUS DOC])."
1426 (let* ((pkg-desc (car pkg))
1427 (status (cdr pkg))
1428 (face (pcase status
1429 (`"built-in" 'font-lock-builtin-face)
1430 (`"available" 'default)
1431 (`"new" 'bold)
1432 (`"held" 'font-lock-constant-face)
1433 (`"disabled" 'font-lock-warning-face)
1434 (`"installed" 'font-lock-comment-face)
1435 (_ 'font-lock-warning-face)))) ; obsolete.
1436 (list pkg-desc
1437 (vector (list (symbol-name (package-desc-name pkg-desc))
1438 'face 'link
1439 'follow-link t
1440 'package-desc pkg-desc
1441 'action 'package-menu-describe-package)
1442 (propertize (package-version-join
1443 (package-desc-version pkg-desc))
1444 'font-lock-face face)
1445 (propertize status 'font-lock-face face)
1446 (propertize (package-desc-summary pkg-desc)
1447 'font-lock-face face)))))
1449 (defun package-menu-refresh ()
1450 "Download the Emacs Lisp package archive.
1451 This fetches the contents of each archive specified in
1452 `package-archives', and then refreshes the package menu."
1453 (interactive)
1454 (unless (derived-mode-p 'package-menu-mode)
1455 (error "The current buffer is not a Package Menu"))
1456 (package-refresh-contents)
1457 (package-menu--generate t t))
1459 (defun package-menu-describe-package (&optional button)
1460 "Describe the current package.
1461 If optional arg BUTTON is non-nil, describe its associated package."
1462 (interactive)
1463 (let ((pkg-desc (if button (button-get button 'package-desc)
1464 (tabulated-list-get-id))))
1465 (if pkg-desc
1466 ;; FIXME: We could actually describe this particular pkg-desc.
1467 (describe-package (package-desc-name pkg-desc)))))
1469 ;; fixme numeric argument
1470 (defun package-menu-mark-delete (&optional _num)
1471 "Mark a package for deletion and move to the next line."
1472 (interactive "p")
1473 (if (member (package-menu-get-status) '("installed" "obsolete"))
1474 (tabulated-list-put-tag "D" t)
1475 (forward-line)))
1477 (defun package-menu-mark-install (&optional _num)
1478 "Mark a package for installation and move to the next line."
1479 (interactive "p")
1480 (if (member (package-menu-get-status) '("available" "new"))
1481 (tabulated-list-put-tag "I" t)
1482 (forward-line)))
1484 (defun package-menu-mark-unmark (&optional _num)
1485 "Clear any marks on a package and move to the next line."
1486 (interactive "p")
1487 (tabulated-list-put-tag " " t))
1489 (defun package-menu-backup-unmark ()
1490 "Back up one line and clear any marks on that package."
1491 (interactive)
1492 (forward-line -1)
1493 (tabulated-list-put-tag " "))
1495 (defun package-menu-mark-obsolete-for-deletion ()
1496 "Mark all obsolete packages for deletion."
1497 (interactive)
1498 (save-excursion
1499 (goto-char (point-min))
1500 (while (not (eobp))
1501 (if (equal (package-menu-get-status) "obsolete")
1502 (tabulated-list-put-tag "D" t)
1503 (forward-line 1)))))
1505 (defun package-menu-quick-help ()
1506 "Show short key binding help for package-menu-mode."
1507 (interactive)
1508 (message "n-ext, i-nstall, d-elete, u-nmark, x-ecute, r-efresh, h-elp"))
1510 (define-obsolete-function-alias
1511 'package-menu-view-commentary 'package-menu-describe-package "24.1")
1513 (defun package-menu-get-status ()
1514 (let* ((id (tabulated-list-get-id))
1515 (entry (and id (assq id tabulated-list-entries))))
1516 (if entry
1517 (aref (cadr entry) 2)
1518 "")))
1520 (defun package-menu--find-upgrades ()
1521 (let (installed available upgrades)
1522 ;; Build list of installed/available packages in this buffer.
1523 (dolist (entry tabulated-list-entries)
1524 ;; ENTRY is (PKG-DESC [NAME VERSION STATUS DOC])
1525 (let ((pkg-desc (car entry))
1526 (status (aref (cadr entry) 2)))
1527 (cond ((equal status "installed")
1528 (push pkg-desc installed))
1529 ((member status '("available" "new"))
1530 (push (cons (package-desc-name pkg-desc) pkg-desc)
1531 available)))))
1532 ;; Loop through list of installed packages, finding upgrades.
1533 (dolist (pkg-desc installed)
1534 (let ((avail-pkg (assq (package-desc-name pkg-desc) available)))
1535 (and avail-pkg
1536 (version-list-< (package-desc-version pkg-desc)
1537 (package-desc-version (cdr avail-pkg)))
1538 (push avail-pkg upgrades))))
1539 upgrades))
1541 (defun package-menu-mark-upgrades ()
1542 "Mark all upgradable packages in the Package Menu.
1543 For each installed package with a newer version available, place
1544 an (I)nstall flag on the available version and a (D)elete flag on
1545 the installed version. A subsequent \\[package-menu-execute]
1546 call will upgrade the package."
1547 (interactive)
1548 (unless (derived-mode-p 'package-menu-mode)
1549 (error "The current buffer is not a Package Menu"))
1550 (let ((upgrades (package-menu--find-upgrades)))
1551 (if (null upgrades)
1552 (message "No packages to upgrade.")
1553 (widen)
1554 (save-excursion
1555 (goto-char (point-min))
1556 (while (not (eobp))
1557 (let* ((pkg-desc (tabulated-list-get-id))
1558 (upgrade (cdr (assq (package-desc-name pkg-desc) upgrades))))
1559 (cond ((null upgrade)
1560 (forward-line 1))
1561 ((equal pkg-desc upgrade)
1562 (package-menu-mark-install))
1564 (package-menu-mark-delete))))))
1565 (message "%d package%s marked for upgrading."
1566 (length upgrades)
1567 (if (= (length upgrades) 1) "" "s")))))
1569 (defun package-menu-execute (&optional noquery)
1570 "Perform marked Package Menu actions.
1571 Packages marked for installation are downloaded and installed;
1572 packages marked for deletion are removed.
1573 Optional argument NOQUERY non-nil means do not ask the user to confirm."
1574 (interactive)
1575 (unless (derived-mode-p 'package-menu-mode)
1576 (error "The current buffer is not in Package Menu mode"))
1577 (let (install-list delete-list cmd pkg-desc)
1578 (save-excursion
1579 (goto-char (point-min))
1580 (while (not (eobp))
1581 (setq cmd (char-after))
1582 (unless (eq cmd ?\s)
1583 ;; This is the key PKG-DESC.
1584 (setq pkg-desc (tabulated-list-get-id))
1585 (cond ((eq cmd ?D)
1586 (push pkg-desc delete-list))
1587 ((eq cmd ?I)
1588 (push pkg-desc install-list))))
1589 (forward-line)))
1590 (when install-list
1591 (if (or
1592 noquery
1593 (yes-or-no-p
1594 (if (= (length install-list) 1)
1595 (format "Install package `%s'? "
1596 (package-desc-full-name (car install-list)))
1597 (format "Install these %d packages (%s)? "
1598 (length install-list)
1599 (mapconcat #'package-desc-full-name
1600 install-list ", ")))))
1601 (mapc 'package-install install-list)))
1602 ;; Delete packages, prompting if necessary.
1603 (when delete-list
1604 (if (or
1605 noquery
1606 (yes-or-no-p
1607 (if (= (length delete-list) 1)
1608 (format "Delete package `%s'? "
1609 (package-desc-full-name (car delete-list)))
1610 (format "Delete these %d packages (%s)? "
1611 (length delete-list)
1612 (mapconcat #'package-desc-full-name
1613 delete-list ", ")))))
1614 (dolist (elt delete-list)
1615 (condition-case-unless-debug err
1616 (package-delete elt)
1617 (error (message (cadr err)))))
1618 (error "Aborted")))
1619 ;; If we deleted anything, regenerate `package-alist'. This is done
1620 ;; automatically if we installed a package.
1621 (and delete-list (null install-list)
1622 (package-initialize))
1623 (if (or delete-list install-list)
1624 (package-menu--generate t t)
1625 (message "No operations specified."))))
1627 (defun package-menu--version-predicate (A B)
1628 (let ((vA (or (aref (cadr A) 1) '(0)))
1629 (vB (or (aref (cadr B) 1) '(0))))
1630 (if (version-list-= vA vB)
1631 (package-menu--name-predicate A B)
1632 (version-list-< vA vB))))
1634 (defun package-menu--status-predicate (A B)
1635 (let ((sA (aref (cadr A) 2))
1636 (sB (aref (cadr B) 2)))
1637 (cond ((string= sA sB)
1638 (package-menu--name-predicate A B))
1639 ((string= sA "new") t)
1640 ((string= sB "new") nil)
1641 ((string= sA "available") t)
1642 ((string= sB "available") nil)
1643 ((string= sA "installed") t)
1644 ((string= sB "installed") nil)
1645 ((string= sA "held") t)
1646 ((string= sB "held") nil)
1647 ((string= sA "built-in") t)
1648 ((string= sB "built-in") nil)
1649 ((string= sA "obsolete") t)
1650 ((string= sB "obsolete") nil)
1651 (t (string< sA sB)))))
1653 (defun package-menu--description-predicate (A B)
1654 (let ((dA (aref (cadr A) 3))
1655 (dB (aref (cadr B) 3)))
1656 (if (string= dA dB)
1657 (package-menu--name-predicate A B)
1658 (string< dA dB))))
1660 (defun package-menu--name-predicate (A B)
1661 (string< (symbol-name (package-desc-name (car A)))
1662 (symbol-name (package-desc-name (car B)))))
1664 ;;;###autoload
1665 (defun list-packages (&optional no-fetch)
1666 "Display a list of packages.
1667 This first fetches the updated list of packages before
1668 displaying, unless a prefix argument NO-FETCH is specified.
1669 The list is displayed in a buffer named `*Packages*'."
1670 (interactive "P")
1671 (require 'finder-inf nil t)
1672 ;; Initialize the package system if necessary.
1673 (unless package--initialized
1674 (package-initialize t))
1675 (let (old-archives new-packages)
1676 (unless no-fetch
1677 ;; Read the locally-cached archive-contents.
1678 (package-read-all-archive-contents)
1679 (setq old-archives package-archive-contents)
1680 ;; Fetch the remote list of packages.
1681 (package-refresh-contents)
1682 ;; Find which packages are new.
1683 (dolist (elt package-archive-contents)
1684 (unless (assq (car elt) old-archives)
1685 (push (car elt) new-packages))))
1687 ;; Generate the Package Menu.
1688 (let ((buf (get-buffer-create "*Packages*")))
1689 (with-current-buffer buf
1690 (package-menu-mode)
1691 (set (make-local-variable 'package-menu--new-package-list)
1692 new-packages)
1693 (package-menu--generate nil t))
1694 ;; The package menu buffer has keybindings. If the user types
1695 ;; `M-x list-packages', that suggests it should become current.
1696 (switch-to-buffer buf))
1698 (let ((upgrades (package-menu--find-upgrades)))
1699 (if upgrades
1700 (message "%d package%s can be upgraded; type `%s' to mark %s for upgrading."
1701 (length upgrades)
1702 (if (= (length upgrades) 1) "" "s")
1703 (substitute-command-keys "\\[package-menu-mark-upgrades]")
1704 (if (= (length upgrades) 1) "it" "them"))))))
1706 ;;;###autoload
1707 (defalias 'package-list-packages 'list-packages)
1709 ;; Used in finder.el
1710 (defun package-show-package-list (packages)
1711 "Display PACKAGES in a *Packages* buffer.
1712 This is similar to `list-packages', but it does not fetch the
1713 updated list of packages, and it only displays packages with
1714 names in PACKAGES (which should be a list of symbols)."
1715 (require 'finder-inf nil t)
1716 (let ((buf (get-buffer-create "*Packages*")))
1717 (with-current-buffer buf
1718 (package-menu-mode)
1719 (package-menu--generate nil packages))
1720 (switch-to-buffer buf)))
1722 (defun package-list-packages-no-fetch ()
1723 "Display a list of packages.
1724 Does not fetch the updated list of packages before displaying.
1725 The list is displayed in a buffer named `*Packages*'."
1726 (interactive)
1727 (list-packages t))
1729 (provide 'package)
1731 ;;; package.el ends here