5fdc870dec354387ba29cfa6c1e17211044d81ba
[bpt/emacs.git] / lisp / emacs-lisp / package.el
1 ;;; package.el --- Simple package system for Emacs
2
3 ;; Copyright (C) 2007-2013 Free Software Foundation, Inc.
4
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"))
10
11 ;; This file is part of GNU Emacs.
12
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.
17
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.
22
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/>.
25
26 ;;; Change Log:
27
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
33
34 ;;; Commentary:
35
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.)
44
45 ;; A package is described by its name and version. The distribution
46 ;; format is either a tar file or a single .el file.
47
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.
53
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".
56
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.
61
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.
65
66 ;; Conceptually a package has multiple state transitions:
67 ;;
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.
76
77 ;; Other external functions you may want to use:
78 ;;
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.
85 ;;
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).
93 ;;
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.
99
100 ;;; Thanks:
101 ;;; (sorted by sort-lines):
102
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>
112
113 ;;; ToDo:
114
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
162
163 ;;; Code:
164
165 (eval-when-compile (require 'cl-lib))
166
167 (require 'tabulated-list)
168
169 (defgroup package nil
170 "Manager for Emacs Lisp packages."
171 :group 'applications
172 :version "24.1")
173
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\").
180
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")
186
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.
192
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")
204
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)
211
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.
215
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.)
222
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")
230
231 (defcustom package-pinned-packages nil
232 "An alist of packages that are pinned to a specific archive
233
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'.
238
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")
247
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.")
251
252 (defconst package-el-version "1.0.1"
253 "Version of package.el.")
254
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)
261
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")
271
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.
282
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")
289
290 (defvar package--default-summary "No description available.")
291
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.
312
313 Slots:
314
315 `name' Name of the package, as a symbol.
316
317 `version' Version of the package, as a version list.
318
319 `summary' Short description of the package, typically taken from
320 the first line of the file.
321
322 `reqs' Requirements of the package. A list of (PACKAGE
323 VERSION-LIST) naming the dependent package and the minimum
324 required version.
325
326 `kind' The distribution format of the package. Currently, it is
327 either `single' or `tar'.
328
329 `archive' The name of the archive (as a string) whence this
330 package came.
331
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)
340
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))))
346
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))))
352
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)
360
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'.
366
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)
370
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.
376
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)
381
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)
386
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)
393 ""
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))
402 (t
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)))))
415
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)))))
428
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.
434
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)))))))
444
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'")))))
459
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.
477 t))
478
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)
487 (t
488 (require 'finder-inf nil t) ; For `package--builtins'.
489 (assq package package--builtins)))))
490
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)))
495
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))
518 t)
519 ;; Otherwise, proceed with activation.
520 (t
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)))))))
533
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.
544
545 EXTRA-PROPERTIES is currently unused."
546 ;; FIXME: Placeholder! Should we keep it?
547 (error "Don't call me!"))
548
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))
568
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)
588
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))
600
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)
605
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))
624
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")
649 nil
650 pkg-file))))
651
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))
679
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?
687 )
688
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))
693
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)))
697
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.
703
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))))
726
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))))))
739
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))))
747
748 (defvar package--initialized nil)
749
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!"))
754 (or
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)))
761
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).
765
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.
770
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)
809
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))))
819 t)
820 (end-of-file nil))))
821 (if more-left
822 (error "Can't read whole string")
823 (car read-data))))
824
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))))))
838
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))))
845
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)))))
857
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)
866
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)))))))
904
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))))
915
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))))
945
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))))
957
958 (defun package-buffer-info ()
959 "Return a `package-desc' describing the package in the current buffer.
960
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))))
990
991 (defun package-tar-file-info ()
992 "Find package information for a tar file.
993 The return result is a `package-desc'."
994 (cl-assert (derived-mode-p 'tar-mode))
995 (let* ((dir-name (file-name-directory
996 (tar-header-name (car tar-parse-info))))
997 (desc-file (package--description-file dir-name))
998 (tar-desc (tar-get-file-descriptor (concat dir-name desc-file))))
999 (unless tar-desc
1000 (error "No package descriptor file found"))
1001 (with-current-buffer (tar--extract tar-desc)
1002 (goto-char (point-min))
1003 (unwind-protect
1004 (let* ((pkg-def-parsed (read (current-buffer)))
1005 (pkg-desc
1006 (if (not (eq (car pkg-def-parsed) 'define-package))
1007 (error "Can't find define-package in %s"
1008 (tar-header-name tar-desc))
1009 (apply #'package-desc-from-define
1010 (append (cdr pkg-def-parsed))))))
1011 (setf (package-desc-kind pkg-desc) 'tar)
1012 pkg-desc)
1013 (kill-buffer (current-buffer))))))
1014
1015
1016 ;;;###autoload
1017 (defun package-install-from-buffer ()
1018 "Install a package from the current buffer.
1019 The current buffer is assumed to be a single .el or .tar file that follows the
1020 packaging guidelines; see info node `(elisp)Packaging'.
1021 Downloads and installs required packages as needed."
1022 (interactive)
1023 (let ((pkg-desc (if (derived-mode-p 'tar-mode)
1024 (package-tar-file-info)
1025 (package-buffer-info))))
1026 ;; Download and install the dependencies.
1027 (let* ((requires (package-desc-reqs pkg-desc))
1028 (transaction (package-compute-transaction nil requires)))
1029 (package-download-transaction transaction))
1030 ;; Install the package itself.
1031 (package-unpack pkg-desc)
1032 pkg-desc))
1033
1034 ;;;###autoload
1035 (defun package-install-file (file)
1036 "Install a package from a file.
1037 The file can either be a tar file or an Emacs Lisp file."
1038 (interactive "fPackage file name: ")
1039 (with-temp-buffer
1040 (insert-file-contents-literally file)
1041 (when (string-match "\\.tar\\'" file) (tar-mode))
1042 (package-install-from-buffer)))
1043
1044 (defun package-delete (pkg-desc)
1045 (let ((dir (package-desc-dir pkg-desc)))
1046 (if (string-equal (file-name-directory dir)
1047 (file-name-as-directory
1048 (expand-file-name package-user-dir)))
1049 (progn
1050 (delete-directory dir t t)
1051 (message "Package `%s' deleted." (package-desc-full-name pkg-desc)))
1052 ;; Don't delete "system" packages
1053 (error "Package `%s' is a system package, not deleting"
1054 (package-desc-full-name pkg-desc)))))
1055
1056 (defun package-archive-base (desc)
1057 "Return the archive containing the package NAME."
1058 (cdr (assoc (package-desc-archive desc) package-archives)))
1059
1060 (defun package--download-one-archive (archive file)
1061 "Retrieve an archive file FILE from ARCHIVE, and cache it.
1062 ARCHIVE should be a cons cell of the form (NAME . LOCATION),
1063 similar to an entry in `package-alist'. Save the cached copy to
1064 \"archives/NAME/archive-contents\" in `package-user-dir'."
1065 (let* ((dir (expand-file-name (format "archives/%s" (car archive))
1066 package-user-dir)))
1067 (package--with-work-buffer (cdr archive) file
1068 ;; Read the retrieved buffer to make sure it is valid (e.g. it
1069 ;; may fetch a URL redirect page).
1070 (when (listp (read buffer))
1071 (make-directory dir t)
1072 (setq buffer-file-name (expand-file-name file dir))
1073 (let ((version-control 'never))
1074 (save-buffer))))))
1075
1076 ;;;###autoload
1077 (defun package-refresh-contents ()
1078 "Download the ELPA archive description if needed.
1079 This informs Emacs about the latest versions of all packages, and
1080 makes them available for download."
1081 (interactive)
1082 (unless (file-exists-p package-user-dir)
1083 (make-directory package-user-dir t))
1084 (dolist (archive package-archives)
1085 (condition-case-unless-debug nil
1086 (package--download-one-archive archive "archive-contents")
1087 (error (message "Failed to download `%s' archive."
1088 (car archive)))))
1089 (package-read-all-archive-contents))
1090
1091 ;;;###autoload
1092 (defun package-initialize (&optional no-activate)
1093 "Load Emacs Lisp packages, and activate them.
1094 The variable `package-load-list' controls which packages to load.
1095 If optional arg NO-ACTIVATE is non-nil, don't activate packages."
1096 (interactive)
1097 (setq package-alist nil)
1098 (package-load-all-descriptors)
1099 (package-read-all-archive-contents)
1100 (unless no-activate
1101 (dolist (elt package-alist)
1102 (package-activate (car elt))))
1103 (setq package--initialized t))
1104
1105 \f
1106 ;;;; Package description buffer.
1107
1108 ;;;###autoload
1109 (defun describe-package (package)
1110 "Display the full documentation of PACKAGE (a symbol)."
1111 (interactive
1112 (let* ((guess (function-called-at-point))
1113 packages val)
1114 (require 'finder-inf nil t)
1115 ;; Load the package list if necessary (but don't activate them).
1116 (unless package--initialized
1117 (package-initialize t))
1118 (setq packages (append (mapcar 'car package-alist)
1119 (mapcar 'car package-archive-contents)
1120 (mapcar 'car package--builtins)))
1121 (unless (memq guess packages)
1122 (setq guess nil))
1123 (setq packages (mapcar 'symbol-name packages))
1124 (setq val
1125 (completing-read (if guess
1126 (format "Describe package (default %s): "
1127 guess)
1128 "Describe package: ")
1129 packages nil t nil nil guess))
1130 (list (if (equal val "") guess (intern val)))))
1131 (if (not (and package (symbolp package)))
1132 (message "No package specified")
1133 (help-setup-xref (list #'describe-package package)
1134 (called-interactively-p 'interactive))
1135 (with-help-window (help-buffer)
1136 (with-current-buffer standard-output
1137 (describe-package-1 package)))))
1138
1139 (defun describe-package-1 (package)
1140 (require 'lisp-mnt)
1141 (let ((package-name (symbol-name package))
1142 (built-in (assq package package--builtins))
1143 desc pkg-dir reqs version installable archive)
1144 (prin1 package)
1145 (princ " is ")
1146 (cond
1147 ;; Loaded packages are in `package-alist'.
1148 ((setq desc (cadr (assq package package-alist)))
1149 (setq version (package-version-join (package-desc-version desc)))
1150 (if (setq pkg-dir (package-desc-dir desc))
1151 (insert "an installed package.\n\n")
1152 ;; This normally does not happen.
1153 (insert "a deleted package.\n\n")))
1154 ;; Available packages are in `package-archive-contents'.
1155 ((setq desc (cdr (assq package package-archive-contents)))
1156 (setq version (package-version-join (package-desc-version desc))
1157 archive (package-desc-archive desc)
1158 installable t)
1159 (if built-in
1160 (insert "a built-in package.\n\n")
1161 (insert "an uninstalled package.\n\n")))
1162 (built-in
1163 (setq desc (package--from-builtin built-in)
1164 version (package-version-join (package-desc-version desc)))
1165 (insert "a built-in package.\n\n"))
1166 (t
1167 (insert "an orphan package.\n\n")))
1168
1169 (insert " " (propertize "Status" 'font-lock-face 'bold) ": ")
1170 (cond (pkg-dir
1171 (insert (propertize "Installed"
1172 'font-lock-face 'font-lock-comment-face))
1173 (insert " in `")
1174 ;; Todo: Add button for uninstalling.
1175 (help-insert-xref-button (file-name-as-directory pkg-dir)
1176 'help-package-def pkg-dir)
1177 (if built-in
1178 (insert "',\n shadowing a "
1179 (propertize "built-in package"
1180 'font-lock-face 'font-lock-builtin-face)
1181 ".")
1182 (insert "'.")))
1183 (installable
1184 (if built-in
1185 (insert (propertize "Built-in."
1186 'font-lock-face 'font-lock-builtin-face)
1187 " Alternate version available")
1188 (insert "Available"))
1189 (insert " from " archive)
1190 (insert " -- ")
1191 (let ((button-text (if (display-graphic-p) "Install" "[Install]"))
1192 (button-face (if (display-graphic-p)
1193 '(:box (:line-width 2 :color "dark grey")
1194 :background "light grey"
1195 :foreground "black")
1196 'link)))
1197 (insert-text-button button-text 'face button-face 'follow-link t
1198 'package-desc desc
1199 'action 'package-install-button-action)))
1200 (built-in
1201 (insert (propertize "Built-in."
1202 'font-lock-face 'font-lock-builtin-face)))
1203 (t (insert "Deleted.")))
1204 (insert "\n")
1205 (and version (> (length version) 0)
1206 (insert " "
1207 (propertize "Version" 'font-lock-face 'bold) ": " version "\n"))
1208
1209 (setq reqs (if desc (package-desc-reqs desc)))
1210 (when reqs
1211 (insert " " (propertize "Requires" 'font-lock-face 'bold) ": ")
1212 (let ((first t)
1213 name vers text)
1214 (dolist (req reqs)
1215 (setq name (car req)
1216 vers (cadr req)
1217 text (format "%s-%s" (symbol-name name)
1218 (package-version-join vers)))
1219 (cond (first (setq first nil))
1220 ((>= (+ 2 (current-column) (length text))
1221 (window-width))
1222 (insert ",\n "))
1223 (t (insert ", ")))
1224 (help-insert-xref-button text 'help-package name))
1225 (insert "\n")))
1226 (insert " " (propertize "Summary" 'font-lock-face 'bold)
1227 ": " (if desc (package-desc-summary desc)) "\n\n")
1228
1229 (if built-in
1230 ;; For built-in packages, insert the commentary.
1231 (let ((fn (locate-file (concat package-name ".el") load-path
1232 load-file-rep-suffixes))
1233 (opoint (point)))
1234 (insert (or (lm-commentary fn) ""))
1235 (save-excursion
1236 (goto-char opoint)
1237 (when (re-search-forward "^;;; Commentary:\n" nil t)
1238 (replace-match ""))
1239 (while (re-search-forward "^\\(;+ ?\\)" nil t)
1240 (replace-match ""))))
1241 (let ((readme (expand-file-name (concat package-name "-readme.txt")
1242 package-user-dir))
1243 readme-string)
1244 ;; For elpa packages, try downloading the commentary. If that
1245 ;; fails, try an existing readme file in `package-user-dir'.
1246 (cond ((condition-case nil
1247 (package--with-work-buffer (package-archive-base desc)
1248 (concat package-name "-readme.txt")
1249 (setq buffer-file-name
1250 (expand-file-name readme package-user-dir))
1251 (let ((version-control 'never))
1252 (save-buffer))
1253 (setq readme-string (buffer-string))
1254 t)
1255 (error nil))
1256 (insert readme-string))
1257 ((file-readable-p readme)
1258 (insert-file-contents readme)
1259 (goto-char (point-max))))))))
1260
1261 (defun package-install-button-action (button)
1262 (let ((pkg-desc (button-get button 'package-desc)))
1263 (when (y-or-n-p (format "Install package `%s'? "
1264 (package-desc-full-name pkg-desc)))
1265 (package-install pkg-desc)
1266 (revert-buffer nil t)
1267 (goto-char (point-min)))))
1268
1269 \f
1270 ;;;; Package menu mode.
1271
1272 (defvar package-menu-mode-map
1273 (let ((map (make-sparse-keymap))
1274 (menu-map (make-sparse-keymap "Package")))
1275 (set-keymap-parent map tabulated-list-mode-map)
1276 (define-key map "\C-m" 'package-menu-describe-package)
1277 (define-key map "u" 'package-menu-mark-unmark)
1278 (define-key map "\177" 'package-menu-backup-unmark)
1279 (define-key map "d" 'package-menu-mark-delete)
1280 (define-key map "i" 'package-menu-mark-install)
1281 (define-key map "U" 'package-menu-mark-upgrades)
1282 (define-key map "r" 'package-menu-refresh)
1283 (define-key map "~" 'package-menu-mark-obsolete-for-deletion)
1284 (define-key map "x" 'package-menu-execute)
1285 (define-key map "h" 'package-menu-quick-help)
1286 (define-key map "?" 'package-menu-describe-package)
1287 (define-key map [menu-bar package-menu] (cons "Package" menu-map))
1288 (define-key menu-map [mq]
1289 '(menu-item "Quit" quit-window
1290 :help "Quit package selection"))
1291 (define-key menu-map [s1] '("--"))
1292 (define-key menu-map [mn]
1293 '(menu-item "Next" next-line
1294 :help "Next Line"))
1295 (define-key menu-map [mp]
1296 '(menu-item "Previous" previous-line
1297 :help "Previous Line"))
1298 (define-key menu-map [s2] '("--"))
1299 (define-key menu-map [mu]
1300 '(menu-item "Unmark" package-menu-mark-unmark
1301 :help "Clear any marks on a package and move to the next line"))
1302 (define-key menu-map [munm]
1303 '(menu-item "Unmark Backwards" package-menu-backup-unmark
1304 :help "Back up one line and clear any marks on that package"))
1305 (define-key menu-map [md]
1306 '(menu-item "Mark for Deletion" package-menu-mark-delete
1307 :help "Mark a package for deletion and move to the next line"))
1308 (define-key menu-map [mi]
1309 '(menu-item "Mark for Install" package-menu-mark-install
1310 :help "Mark a package for installation and move to the next line"))
1311 (define-key menu-map [mupgrades]
1312 '(menu-item "Mark Upgradable Packages" package-menu-mark-upgrades
1313 :help "Mark packages that have a newer version for upgrading"))
1314 (define-key menu-map [s3] '("--"))
1315 (define-key menu-map [mg]
1316 '(menu-item "Update Package List" revert-buffer
1317 :help "Update the list of packages"))
1318 (define-key menu-map [mr]
1319 '(menu-item "Refresh Package List" package-menu-refresh
1320 :help "Download the ELPA archive"))
1321 (define-key menu-map [s4] '("--"))
1322 (define-key menu-map [mt]
1323 '(menu-item "Mark Obsolete Packages" package-menu-mark-obsolete-for-deletion
1324 :help "Mark all obsolete packages for deletion"))
1325 (define-key menu-map [mx]
1326 '(menu-item "Execute Actions" package-menu-execute
1327 :help "Perform all the marked actions"))
1328 (define-key menu-map [s5] '("--"))
1329 (define-key menu-map [mh]
1330 '(menu-item "Help" package-menu-quick-help
1331 :help "Show short key binding help for package-menu-mode"))
1332 (define-key menu-map [mc]
1333 '(menu-item "View Commentary" package-menu-view-commentary
1334 :help "Display information about this package"))
1335 map)
1336 "Local keymap for `package-menu-mode' buffers.")
1337
1338 (defvar package-menu--new-package-list nil
1339 "List of newly-available packages since `list-packages' was last called.")
1340
1341 (define-derived-mode package-menu-mode tabulated-list-mode "Package Menu"
1342 "Major mode for browsing a list of packages.
1343 Letters do not insert themselves; instead, they are commands.
1344 \\<package-menu-mode-map>
1345 \\{package-menu-mode-map}"
1346 (setq tabulated-list-format [("Package" 18 package-menu--name-predicate)
1347 ("Version" 12 nil)
1348 ("Status" 10 package-menu--status-predicate)
1349 ("Description" 0 nil)])
1350 (setq tabulated-list-padding 2)
1351 (setq tabulated-list-sort-key (cons "Status" nil))
1352 (tabulated-list-init-header))
1353
1354 (defmacro package--push (pkg-desc status listname)
1355 "Convenience macro for `package-menu--generate'.
1356 If the alist stored in the symbol LISTNAME lacks an entry for a
1357 package PKG-DESC, add one. The alist is keyed with PKG-DESC."
1358 `(unless (assoc ,pkg-desc ,listname)
1359 ;; FIXME: Should we move status into pkg-desc?
1360 (push (cons ,pkg-desc ,status) ,listname)))
1361
1362 (defvar package-list-unversioned nil
1363 "If non-nil include packages that don't have a version in `list-package'.")
1364
1365 (defun package-menu--generate (remember-pos packages)
1366 "Populate the Package Menu.
1367 If REMEMBER-POS is non-nil, keep point on the same entry.
1368 PACKAGES should be t, which means to display all known packages,
1369 or a list of package names (symbols) to display."
1370 ;; Construct list of (PKG-DESC . STATUS).
1371 (let (info-list name)
1372 ;; Installed packages:
1373 (dolist (elt package-alist)
1374 (setq name (car elt))
1375 (when (or (eq packages t) (memq name packages))
1376 (let* ((lle (assq name package-load-list))
1377 (held (cadr lle))
1378 (hv (if (stringp held) (version-to-list held))))
1379 (dolist (pkg (cdr elt))
1380 (let ((version (package-desc-version pkg)))
1381 (package--push pkg
1382 (cond
1383 ((and lle (null held)) "disabled")
1384 (hv
1385 (cond
1386 ((version-list-= version hv) "held")
1387 ((version-list-< version hv) "obsolete")
1388 (t "disabled")))
1389 ((package-built-in-p name version) "obsolete")
1390 ((eq pkg (cadr elt)) "installed")
1391 (t "obsolete"))
1392 info-list))))))
1393
1394 ;; Built-in packages:
1395 (dolist (elt package--builtins)
1396 (setq name (car elt))
1397 (when (and (not (eq name 'emacs)) ; Hide the `emacs' package.
1398 (or package-list-unversioned
1399 (package--bi-desc-version (cdr elt)))
1400 (or (eq packages t) (memq name packages)))
1401 (package--push (package--from-builtin elt) "built-in" info-list)))
1402
1403 ;; Available and disabled packages:
1404 (dolist (elt package-archive-contents)
1405 (setq name (car elt))
1406 (when (or (eq packages t) (memq name packages))
1407 (let ((hold (assq name package-load-list)))
1408 (package--push (cdr elt)
1409 (cond
1410 ((and hold (null (cadr hold))) "disabled")
1411 ((memq name package-menu--new-package-list) "new")
1412 (t "available"))
1413 info-list))))
1414
1415 ;; Print the result.
1416 (setq tabulated-list-entries (mapcar 'package-menu--print-info info-list))
1417 (tabulated-list-print remember-pos)))
1418
1419 (defun package-menu--print-info (pkg)
1420 "Return a package entry suitable for `tabulated-list-entries'.
1421 PKG has the form (PKG-DESC . STATUS).
1422 Return (PKG-DESC [NAME VERSION STATUS DOC])."
1423 (let* ((pkg-desc (car pkg))
1424 (status (cdr pkg))
1425 (face (pcase status
1426 (`"built-in" 'font-lock-builtin-face)
1427 (`"available" 'default)
1428 (`"new" 'bold)
1429 (`"held" 'font-lock-constant-face)
1430 (`"disabled" 'font-lock-warning-face)
1431 (`"installed" 'font-lock-comment-face)
1432 (_ 'font-lock-warning-face)))) ; obsolete.
1433 (list pkg-desc
1434 (vector (list (symbol-name (package-desc-name pkg-desc))
1435 'face 'link
1436 'follow-link t
1437 'package-desc pkg-desc
1438 'action 'package-menu-describe-package)
1439 (propertize (package-version-join
1440 (package-desc-version pkg-desc))
1441 'font-lock-face face)
1442 (propertize status 'font-lock-face face)
1443 (propertize (package-desc-summary pkg-desc)
1444 'font-lock-face face)))))
1445
1446 (defun package-menu-refresh ()
1447 "Download the Emacs Lisp package archive.
1448 This fetches the contents of each archive specified in
1449 `package-archives', and then refreshes the package menu."
1450 (interactive)
1451 (unless (derived-mode-p 'package-menu-mode)
1452 (error "The current buffer is not a Package Menu"))
1453 (package-refresh-contents)
1454 (package-menu--generate t t))
1455
1456 (defun package-menu-describe-package (&optional button)
1457 "Describe the current package.
1458 If optional arg BUTTON is non-nil, describe its associated package."
1459 (interactive)
1460 (let ((pkg-desc (if button (button-get button 'package-desc)
1461 (tabulated-list-get-id))))
1462 (if pkg-desc
1463 ;; FIXME: We could actually describe this particular pkg-desc.
1464 (describe-package (package-desc-name pkg-desc)))))
1465
1466 ;; fixme numeric argument
1467 (defun package-menu-mark-delete (&optional _num)
1468 "Mark a package for deletion and move to the next line."
1469 (interactive "p")
1470 (if (member (package-menu-get-status) '("installed" "obsolete"))
1471 (tabulated-list-put-tag "D" t)
1472 (forward-line)))
1473
1474 (defun package-menu-mark-install (&optional _num)
1475 "Mark a package for installation and move to the next line."
1476 (interactive "p")
1477 (if (member (package-menu-get-status) '("available" "new"))
1478 (tabulated-list-put-tag "I" t)
1479 (forward-line)))
1480
1481 (defun package-menu-mark-unmark (&optional _num)
1482 "Clear any marks on a package and move to the next line."
1483 (interactive "p")
1484 (tabulated-list-put-tag " " t))
1485
1486 (defun package-menu-backup-unmark ()
1487 "Back up one line and clear any marks on that package."
1488 (interactive)
1489 (forward-line -1)
1490 (tabulated-list-put-tag " "))
1491
1492 (defun package-menu-mark-obsolete-for-deletion ()
1493 "Mark all obsolete packages for deletion."
1494 (interactive)
1495 (save-excursion
1496 (goto-char (point-min))
1497 (while (not (eobp))
1498 (if (equal (package-menu-get-status) "obsolete")
1499 (tabulated-list-put-tag "D" t)
1500 (forward-line 1)))))
1501
1502 (defun package-menu-quick-help ()
1503 "Show short key binding help for package-menu-mode."
1504 (interactive)
1505 (message "n-ext, i-nstall, d-elete, u-nmark, x-ecute, r-efresh, h-elp"))
1506
1507 (define-obsolete-function-alias
1508 'package-menu-view-commentary 'package-menu-describe-package "24.1")
1509
1510 (defun package-menu-get-status ()
1511 (let* ((id (tabulated-list-get-id))
1512 (entry (and id (assq id tabulated-list-entries))))
1513 (if entry
1514 (aref (cadr entry) 2)
1515 "")))
1516
1517 (defun package-menu--find-upgrades ()
1518 (let (installed available upgrades)
1519 ;; Build list of installed/available packages in this buffer.
1520 (dolist (entry tabulated-list-entries)
1521 ;; ENTRY is (PKG-DESC [NAME VERSION STATUS DOC])
1522 (let ((pkg-desc (car entry))
1523 (status (aref (cadr entry) 2)))
1524 (cond ((equal status "installed")
1525 (push pkg-desc installed))
1526 ((member status '("available" "new"))
1527 (push (cons (package-desc-name pkg-desc) pkg-desc)
1528 available)))))
1529 ;; Loop through list of installed packages, finding upgrades.
1530 (dolist (pkg-desc installed)
1531 (let ((avail-pkg (assq (package-desc-name pkg-desc) available)))
1532 (and avail-pkg
1533 (version-list-< (package-desc-version pkg-desc)
1534 (package-desc-version (cdr avail-pkg)))
1535 (push avail-pkg upgrades))))
1536 upgrades))
1537
1538 (defun package-menu-mark-upgrades ()
1539 "Mark all upgradable packages in the Package Menu.
1540 For each installed package with a newer version available, place
1541 an (I)nstall flag on the available version and a (D)elete flag on
1542 the installed version. A subsequent \\[package-menu-execute]
1543 call will upgrade the package."
1544 (interactive)
1545 (unless (derived-mode-p 'package-menu-mode)
1546 (error "The current buffer is not a Package Menu"))
1547 (let ((upgrades (package-menu--find-upgrades)))
1548 (if (null upgrades)
1549 (message "No packages to upgrade.")
1550 (widen)
1551 (save-excursion
1552 (goto-char (point-min))
1553 (while (not (eobp))
1554 (let* ((pkg-desc (tabulated-list-get-id))
1555 (upgrade (cdr (assq (package-desc-name pkg-desc) upgrades))))
1556 (cond ((null upgrade)
1557 (forward-line 1))
1558 ((equal pkg-desc upgrade)
1559 (package-menu-mark-install))
1560 (t
1561 (package-menu-mark-delete))))))
1562 (message "%d package%s marked for upgrading."
1563 (length upgrades)
1564 (if (= (length upgrades) 1) "" "s")))))
1565
1566 (defun package-menu-execute (&optional noquery)
1567 "Perform marked Package Menu actions.
1568 Packages marked for installation are downloaded and installed;
1569 packages marked for deletion are removed.
1570 Optional argument NOQUERY non-nil means do not ask the user to confirm."
1571 (interactive)
1572 (unless (derived-mode-p 'package-menu-mode)
1573 (error "The current buffer is not in Package Menu mode"))
1574 (let (install-list delete-list cmd pkg-desc)
1575 (save-excursion
1576 (goto-char (point-min))
1577 (while (not (eobp))
1578 (setq cmd (char-after))
1579 (unless (eq cmd ?\s)
1580 ;; This is the key PKG-DESC.
1581 (setq pkg-desc (tabulated-list-get-id))
1582 (cond ((eq cmd ?D)
1583 (push pkg-desc delete-list))
1584 ((eq cmd ?I)
1585 (push pkg-desc install-list))))
1586 (forward-line)))
1587 (when install-list
1588 (if (or
1589 noquery
1590 (yes-or-no-p
1591 (if (= (length install-list) 1)
1592 (format "Install package `%s'? "
1593 (package-desc-full-name (car install-list)))
1594 (format "Install these %d packages (%s)? "
1595 (length install-list)
1596 (mapconcat #'package-desc-full-name
1597 install-list ", ")))))
1598 (mapc 'package-install install-list)))
1599 ;; Delete packages, prompting if necessary.
1600 (when delete-list
1601 (if (or
1602 noquery
1603 (yes-or-no-p
1604 (if (= (length delete-list) 1)
1605 (format "Delete package `%s'? "
1606 (package-desc-full-name (car delete-list)))
1607 (format "Delete these %d packages (%s)? "
1608 (length delete-list)
1609 (mapconcat #'package-desc-full-name
1610 delete-list ", ")))))
1611 (dolist (elt delete-list)
1612 (condition-case-unless-debug err
1613 (package-delete elt)
1614 (error (message (cadr err)))))
1615 (error "Aborted")))
1616 ;; If we deleted anything, regenerate `package-alist'. This is done
1617 ;; automatically if we installed a package.
1618 (and delete-list (null install-list)
1619 (package-initialize))
1620 (if (or delete-list install-list)
1621 (package-menu--generate t t)
1622 (message "No operations specified."))))
1623
1624 (defun package-menu--version-predicate (A B)
1625 (let ((vA (or (aref (cadr A) 1) '(0)))
1626 (vB (or (aref (cadr B) 1) '(0))))
1627 (if (version-list-= vA vB)
1628 (package-menu--name-predicate A B)
1629 (version-list-< vA vB))))
1630
1631 (defun package-menu--status-predicate (A B)
1632 (let ((sA (aref (cadr A) 2))
1633 (sB (aref (cadr B) 2)))
1634 (cond ((string= sA sB)
1635 (package-menu--name-predicate A B))
1636 ((string= sA "new") t)
1637 ((string= sB "new") nil)
1638 ((string= sA "available") t)
1639 ((string= sB "available") nil)
1640 ((string= sA "installed") t)
1641 ((string= sB "installed") nil)
1642 ((string= sA "held") t)
1643 ((string= sB "held") nil)
1644 ((string= sA "built-in") t)
1645 ((string= sB "built-in") nil)
1646 ((string= sA "obsolete") t)
1647 ((string= sB "obsolete") nil)
1648 (t (string< sA sB)))))
1649
1650 (defun package-menu--description-predicate (A B)
1651 (let ((dA (aref (cadr A) 3))
1652 (dB (aref (cadr B) 3)))
1653 (if (string= dA dB)
1654 (package-menu--name-predicate A B)
1655 (string< dA dB))))
1656
1657 (defun package-menu--name-predicate (A B)
1658 (string< (symbol-name (package-desc-name (car A)))
1659 (symbol-name (package-desc-name (car B)))))
1660
1661 ;;;###autoload
1662 (defun list-packages (&optional no-fetch)
1663 "Display a list of packages.
1664 This first fetches the updated list of packages before
1665 displaying, unless a prefix argument NO-FETCH is specified.
1666 The list is displayed in a buffer named `*Packages*'."
1667 (interactive "P")
1668 (require 'finder-inf nil t)
1669 ;; Initialize the package system if necessary.
1670 (unless package--initialized
1671 (package-initialize t))
1672 (let (old-archives new-packages)
1673 (unless no-fetch
1674 ;; Read the locally-cached archive-contents.
1675 (package-read-all-archive-contents)
1676 (setq old-archives package-archive-contents)
1677 ;; Fetch the remote list of packages.
1678 (package-refresh-contents)
1679 ;; Find which packages are new.
1680 (dolist (elt package-archive-contents)
1681 (unless (assq (car elt) old-archives)
1682 (push (car elt) new-packages))))
1683
1684 ;; Generate the Package Menu.
1685 (let ((buf (get-buffer-create "*Packages*")))
1686 (with-current-buffer buf
1687 (package-menu-mode)
1688 (set (make-local-variable 'package-menu--new-package-list)
1689 new-packages)
1690 (package-menu--generate nil t))
1691 ;; The package menu buffer has keybindings. If the user types
1692 ;; `M-x list-packages', that suggests it should become current.
1693 (switch-to-buffer buf))
1694
1695 (let ((upgrades (package-menu--find-upgrades)))
1696 (if upgrades
1697 (message "%d package%s can be upgraded; type `%s' to mark %s for upgrading."
1698 (length upgrades)
1699 (if (= (length upgrades) 1) "" "s")
1700 (substitute-command-keys "\\[package-menu-mark-upgrades]")
1701 (if (= (length upgrades) 1) "it" "them"))))))
1702
1703 ;;;###autoload
1704 (defalias 'package-list-packages 'list-packages)
1705
1706 ;; Used in finder.el
1707 (defun package-show-package-list (packages)
1708 "Display PACKAGES in a *Packages* buffer.
1709 This is similar to `list-packages', but it does not fetch the
1710 updated list of packages, and it only displays packages with
1711 names in PACKAGES (which should be a list of symbols)."
1712 (require 'finder-inf nil t)
1713 (let ((buf (get-buffer-create "*Packages*")))
1714 (with-current-buffer buf
1715 (package-menu-mode)
1716 (package-menu--generate nil packages))
1717 (switch-to-buffer buf)))
1718
1719 (defun package-list-packages-no-fetch ()
1720 "Display a list of packages.
1721 Does not fetch the updated list of packages before displaying.
1722 The list is displayed in a buffer named `*Packages*'."
1723 (interactive)
1724 (list-packages t))
1725
1726 (provide 'package)
1727
1728 ;;; package.el ends here