]> code.delx.au - gnu-emacs/blob - lisp/emacs-lisp/package.el
* lisp/emacs-lisp/package.el (package-desc-from-define): Ignore unknown keys.
[gnu-emacs] / lisp / emacs-lisp / package.el
1 ;;; package.el --- Simple package system for Emacs -*- lexical-binding:t -*-
2
3 ;; Copyright (C) 2007-2013 Free Software Foundation, Inc.
4
5 ;; Author: Tom Tromey <tromey@redhat.com>
6 ;; Daniel Hackney <dan@haxney.org>
7 ;; Created: 10 Mar 2007
8 ;; Version: 1.0.1
9 ;; Keywords: tools
10 ;; Package-Requires: ((tabulated-list "1.0"))
11
12 ;; This file is part of GNU Emacs.
13
14 ;; GNU Emacs is free software: you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation, either version 3 of the License, or
17 ;; (at your option) any later version.
18
19 ;; GNU Emacs is distributed in the hope that it will be useful,
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 ;; GNU General Public License for more details.
23
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
26
27 ;;; Change Log:
28
29 ;; 2 Apr 2007 - now using ChangeLog file
30 ;; 15 Mar 2007 - updated documentation
31 ;; 14 Mar 2007 - Changed how obsolete packages are handled
32 ;; 13 Mar 2007 - Wrote package-install-from-buffer
33 ;; 12 Mar 2007 - Wrote package-menu mode
34
35 ;;; Commentary:
36
37 ;; The idea behind package.el is to be able to download packages and
38 ;; install them. Packages are versioned and have versioned
39 ;; dependencies. Furthermore, this supports built-in packages which
40 ;; may or may not be newer than user-specified packages. This makes
41 ;; it possible to upgrade Emacs and automatically disable packages
42 ;; which have moved from external to core. (Note though that we don't
43 ;; currently register any of these, so this feature does not actually
44 ;; work.)
45
46 ;; A package is described by its name and version. The distribution
47 ;; format is either a tar file or a single .el file.
48
49 ;; A tar file should be named "NAME-VERSION.tar". The tar file must
50 ;; unpack into a directory named after the package and version:
51 ;; "NAME-VERSION". It must contain a file named "PACKAGE-pkg.el"
52 ;; which consists of a call to define-package. It may also contain a
53 ;; "dir" file and the info files it references.
54
55 ;; A .el file is named "NAME-VERSION.el" in the remote archive, but is
56 ;; installed as simply "NAME.el" in a directory named "NAME-VERSION".
57
58 ;; The downloader downloads all dependent packages. By default,
59 ;; packages come from the official GNU sources, but others may be
60 ;; added by customizing the `package-archives' alist. Packages get
61 ;; byte-compiled at install time.
62
63 ;; At activation time we will set up the load-path and the info path,
64 ;; and we will load the package's autoloads. If a package's
65 ;; dependencies are not available, we will not activate that package.
66
67 ;; Conceptually a package has multiple state transitions:
68 ;;
69 ;; * Download. Fetching the package from ELPA.
70 ;; * Install. Untar the package, or write the .el file, into
71 ;; ~/.emacs.d/elpa/ directory.
72 ;; * Byte compile. Currently this phase is done during install,
73 ;; but we may change this.
74 ;; * Activate. Evaluate the autoloads for the package to make it
75 ;; available to the user.
76 ;; * Load. Actually load the package and run some code from it.
77
78 ;; Other external functions you may want to use:
79 ;;
80 ;; M-x list-packages
81 ;; Enters a mode similar to buffer-menu which lets you manage
82 ;; packages. You can choose packages for install (mark with "i",
83 ;; then "x" to execute) or deletion (not implemented yet), and you
84 ;; can see what packages are available. This will automatically
85 ;; fetch the latest list of packages from ELPA.
86 ;;
87 ;; M-x package-install-from-buffer
88 ;; Install a package consisting of a single .el file that appears
89 ;; in the current buffer. This only works for packages which
90 ;; define a Version header properly; package.el also supports the
91 ;; extension headers Package-Version (in case Version is an RCS id
92 ;; or similar), and Package-Requires (if the package requires other
93 ;; packages).
94 ;;
95 ;; M-x package-install-file
96 ;; Install a package from the indicated file. The package can be
97 ;; either a tar file or a .el file. A tar file must contain an
98 ;; appropriately-named "-pkg.el" file; a .el file must be properly
99 ;; formatted as with package-install-from-buffer.
100
101 ;;; Thanks:
102 ;;; (sorted by sort-lines):
103
104 ;; Jim Blandy <jimb@red-bean.com>
105 ;; Karl Fogel <kfogel@red-bean.com>
106 ;; Kevin Ryde <user42@zip.com.au>
107 ;; Lawrence Mitchell
108 ;; Michael Olson <mwolson@member.fsf.org>
109 ;; Sebastian Tennant <sebyte@smolny.plus.com>
110 ;; Stefan Monnier <monnier@iro.umontreal.ca>
111 ;; Vinicius Jose Latorre <viniciusjl@ig.com.br>
112 ;; Phil Hagelberg <phil@hagelb.org>
113
114 ;;; ToDo:
115
116 ;; - a trust mechanism, since compiling a package can run arbitrary code.
117 ;; For example, download package signatures and check that they match.
118 ;; - putting info dirs at the start of the info path means
119 ;; users see a weird ordering of categories. OTOH we want to
120 ;; override later entries. maybe emacs needs to enforce
121 ;; the standard layout?
122 ;; - put bytecode in a separate directory tree
123 ;; - perhaps give users a way to recompile their bytecode
124 ;; or do it automatically when emacs changes
125 ;; - give users a way to know whether a package is installed ok
126 ;; - give users a way to view a package's documentation when it
127 ;; only appears in the .el
128 ;; - use/extend checkdoc so people can tell if their package will work
129 ;; - "installed" instead of a blank in the status column
130 ;; - tramp needs its files to be compiled in a certain order.
131 ;; how to handle this? fix tramp?
132 ;; - on emacs 21 we don't kill the -autoloads.el buffer. what about 22?
133 ;; - maybe we need separate .elc directories for various emacs versions
134 ;; and also emacs-vs-xemacs. That way conditional compilation can
135 ;; work. But would this break anything?
136 ;; - should store the package's keywords in archive-contents, then
137 ;; let the users filter the package-menu by keyword. See
138 ;; finder-by-keyword. (We could also let people view the
139 ;; Commentary, but it isn't clear how useful this is.)
140 ;; - William Xu suggests being able to open a package file without
141 ;; installing it
142 ;; - Interface with desktop.el so that restarting after an install
143 ;; works properly
144 ;; - Use hierarchical layout. PKG/etc PKG/lisp PKG/info
145 ;; ... except maybe lisp?
146 ;; - It may be nice to have a macro that expands to the package's
147 ;; private data dir, aka ".../etc". Or, maybe data-directory
148 ;; needs to be a list (though this would be less nice)
149 ;; a few packages want this, eg sokoban
150 ;; - package menu needs:
151 ;; ability to know which packages are built-in & thus not deletable
152 ;; it can sometimes print odd results, like 0.3 available but 0.4 active
153 ;; why is that?
154 ;; - Allow multiple versions on the server...?
155 ;; [ why bother? ]
156 ;; - Don't install a package which will invalidate dependencies overall
157 ;; - Allow something like (or (>= emacs 21.0) (>= xemacs 21.5))
158 ;; [ currently thinking, why bother.. KISS ]
159 ;; - Allow optional package dependencies
160 ;; then if we require 'bbdb', bbdb-specific lisp in lisp/bbdb
161 ;; and just don't compile to add to load path ...?
162 ;; - Our treatment of the info path is somewhat bogus
163
164 ;;; Code:
165
166 (eval-when-compile (require 'cl-lib))
167
168 (require 'tabulated-list)
169
170 (defgroup package nil
171 "Manager for Emacs Lisp packages."
172 :group 'applications
173 :version "24.1")
174
175 ;;;###autoload
176 (defcustom package-enable-at-startup t
177 "Whether to activate installed packages when Emacs starts.
178 If non-nil, packages are activated after reading the init file
179 and before `after-init-hook'. Activation is not done if
180 `user-init-file' is nil (e.g. Emacs was started with \"-q\").
181
182 Even if the value is nil, you can type \\[package-initialize] to
183 activate the package system at any time."
184 :type 'boolean
185 :group 'package
186 :version "24.1")
187
188 (defcustom package-load-list '(all)
189 "List of packages for `package-initialize' to load.
190 Each element in this list should be a list (NAME VERSION), or the
191 symbol `all'. The symbol `all' says to load the latest installed
192 versions of all packages not specified by other elements.
193
194 For an element (NAME VERSION), NAME is a package name (a symbol).
195 VERSION should be t, a string, or nil.
196 If VERSION is t, the most recent version is activated.
197 If VERSION is a string, only that version is ever loaded.
198 Any other version, even if newer, is silently ignored.
199 Hence, the package is \"held\" at that version.
200 If VERSION is nil, the package is not loaded (it is \"disabled\")."
201 :type '(repeat symbol)
202 :risky t
203 :group 'package
204 :version "24.1")
205
206 (defvar Info-directory-list)
207 (declare-function info-initialize "info" ())
208 (declare-function url-http-parse-response "url-http" ())
209 (declare-function lm-header "lisp-mnt" (header))
210 (declare-function lm-commentary "lisp-mnt" (&optional file))
211 (defvar url-http-end-of-headers)
212
213 (defcustom package-archives '(("gnu" . "http://elpa.gnu.org/packages/"))
214 "An alist of archives from which to fetch.
215 The default value points to the GNU Emacs package repository.
216
217 Each element has the form (ID . LOCATION).
218 ID is an archive name, as a string.
219 LOCATION specifies the base location for the archive.
220 If it starts with \"http:\", it is treated as a HTTP URL;
221 otherwise it should be an absolute directory name.
222 (Other types of URL are currently not supported.)
223
224 Only add locations that you trust, since fetching and installing
225 a package can run arbitrary code."
226 :type '(alist :key-type (string :tag "Archive name")
227 :value-type (string :tag "URL or directory name"))
228 :risky t
229 :group 'package
230 :version "24.1")
231
232 (defcustom package-pinned-packages nil
233 "An alist of packages that are pinned to a specific archive
234
235 Each element has the form (SYM . ID).
236 SYM is a package, as a symbol.
237 ID is an archive name. This should correspond to an
238 entry in `package-archives'.
239
240 If the archive of name ID does not contain the package SYM, no
241 other location will be considered, which will make the
242 package unavailable."
243 :type '(alist :key-type (symbol :tag "Package")
244 :value-type (string :tag "Archive name"))
245 :risky t
246 :group 'package
247 :version "24.4")
248
249 (defconst package-archive-version 1
250 "Version number of the package archive understood by this file.
251 Lower version numbers than this will probably be understood as well.")
252
253 ;; We don't prime the cache since it tends to get out of date.
254 (defvar package-archive-contents nil
255 "Cache of the contents of the Emacs Lisp Package Archive.
256 This is an alist mapping package names (symbols) to
257 non-empty lists of `package-desc' structures.")
258 (put 'package-archive-contents 'risky-local-variable t)
259
260 (defcustom package-user-dir (locate-user-emacs-file "elpa")
261 "Directory containing the user's Emacs Lisp packages.
262 The directory name should be absolute.
263 Apart from this directory, Emacs also looks for system-wide
264 packages in `package-directory-list'."
265 :type 'directory
266 :risky t
267 :group 'package
268 :version "24.1")
269
270 (defcustom package-directory-list
271 ;; Defaults are subdirs named "elpa" in the site-lisp dirs.
272 (let (result)
273 (dolist (f load-path)
274 (and (stringp f)
275 (equal (file-name-nondirectory f) "site-lisp")
276 (push (expand-file-name "elpa" f) result)))
277 (nreverse result))
278 "List of additional directories containing Emacs Lisp packages.
279 Each directory name should be absolute.
280
281 These directories contain packages intended for system-wide; in
282 contrast, `package-user-dir' contains packages for personal use."
283 :type '(repeat directory)
284 :risky t
285 :group 'package
286 :version "24.1")
287
288 (defvar package--default-summary "No description available.")
289
290 (cl-defstruct (package-desc
291 ;; Rename the default constructor from `make-package-desc'.
292 (:constructor package-desc-create)
293 ;; Has the same interface as the old `define-package',
294 ;; which is still used in the "foo-pkg.el" files. Extra
295 ;; options can be supported by adding additional keys.
296 (:constructor
297 package-desc-from-define
298 (name-string version-string &optional summary requirements
299 &key kind archive &allow-other-keys
300 &aux
301 (name (intern name-string))
302 (version (version-to-list version-string))
303 (reqs (mapcar #'(lambda (elt)
304 (list (car elt)
305 (version-to-list (cadr elt))))
306 (if (eq 'quote (car requirements))
307 (nth 1 requirements)
308 requirements))))))
309 "Structure containing information about an individual package.
310 Slots:
311
312 `name' Name of the package, as a symbol.
313
314 `version' Version of the package, as a version list.
315
316 `summary' Short description of the package, typically taken from
317 the first line of the file.
318
319 `reqs' Requirements of the package. A list of (PACKAGE
320 VERSION-LIST) naming the dependent package and the minimum
321 required version.
322
323 `kind' The distribution format of the package. Currently, it is
324 either `single' or `tar'.
325
326 `archive' The name of the archive (as a string) whence this
327 package came.
328
329 `dir' The directory where the package is installed (if installed),
330 `builtin' if it is built-in, or nil otherwise."
331 name
332 version
333 (summary package--default-summary)
334 reqs
335 kind
336 archive
337 dir)
338
339 ;; Pseudo fields.
340 (defun package-desc-full-name (pkg-desc)
341 (format "%s-%s"
342 (package-desc-name pkg-desc)
343 (package-version-join (package-desc-version pkg-desc))))
344
345 (defun package-desc-suffix (pkg-desc)
346 (pcase (package-desc-kind pkg-desc)
347 (`single ".el")
348 (`tar ".tar")
349 (kind (error "Unknown package kind: %s" kind))))
350
351 ;; Package descriptor format used in finder-inf.el and package--builtins.
352 (cl-defstruct (package--bi-desc
353 (:constructor package-make-builtin (version summary))
354 (:type vector))
355 version
356 reqs
357 summary)
358
359 (defvar package--builtins nil
360 "Alist of built-in packages.
361 The actual value is initialized by loading the library
362 `finder-inf'; this is not done until it is needed, e.g. by the
363 function `package-built-in-p'.
364
365 Each element has the form (PKG . PACKAGE-BI-DESC), where PKG is a package
366 name (a symbol) and DESC is a `package--bi-desc' structure.")
367 (put 'package--builtins 'risky-local-variable t)
368
369 (defvar package-alist nil
370 "Alist of all packages available for activation.
371 Each element has the form (PKG . DESCS), where PKG is a package
372 name (a symbol) and DESCS is a non-empty list of `package-desc' structure,
373 sorted by decreasing versions.
374
375 This variable is set automatically by `package-load-descriptor',
376 called via `package-initialize'. To change which packages are
377 loaded and/or activated, customize `package-load-list'.")
378 (put 'package-alist 'risky-local-variable t)
379
380 (defvar package-activated-list nil
381 ;; FIXME: This should implicitly include all builtin packages.
382 "List of the names of currently activated packages.")
383 (put 'package-activated-list 'risky-local-variable t)
384
385 (defun package-version-join (vlist)
386 "Return the version string corresponding to the list VLIST.
387 This is, approximately, the inverse of `version-to-list'.
388 \(Actually, it returns only one of the possible inverses, since
389 `version-to-list' is a many-to-one operation.)"
390 (if (null vlist)
391 ""
392 (let ((str-list (list "." (int-to-string (car vlist)))))
393 (dolist (num (cdr vlist))
394 (cond
395 ((>= num 0)
396 (push (int-to-string num) str-list)
397 (push "." str-list))
398 ((< num -3)
399 (error "Invalid version list `%s'" vlist))
400 (t
401 ;; pre, or beta, or alpha
402 (cond ((equal "." (car str-list))
403 (pop str-list))
404 ((not (string-match "[0-9]+" (car str-list)))
405 (error "Invalid version list `%s'" vlist)))
406 (push (cond ((= num -1) "pre")
407 ((= num -2) "beta")
408 ((= num -3) "alpha"))
409 str-list))))
410 (if (equal "." (car str-list))
411 (pop str-list))
412 (apply 'concat (nreverse str-list)))))
413
414 (defun package-load-descriptor (pkg-dir)
415 "Load the description file in directory PKG-DIR."
416 (let ((pkg-file (expand-file-name (package--description-file pkg-dir)
417 pkg-dir)))
418 (when (file-exists-p pkg-file)
419 (with-temp-buffer
420 (insert-file-contents pkg-file)
421 (goto-char (point-min))
422 (let ((pkg-desc (package-process-define-package
423 (read (current-buffer)) pkg-file)))
424 (setf (package-desc-dir pkg-desc) pkg-dir)
425 pkg-desc)))))
426
427 (defun package-load-all-descriptors ()
428 "Load descriptors for installed Emacs Lisp packages.
429 This looks for package subdirectories in `package-user-dir' and
430 `package-directory-list'. The variable `package-load-list'
431 controls which package subdirectories may be loaded.
432
433 In each valid package subdirectory, this function loads the
434 description file containing a call to `define-package', which
435 updates `package-alist'."
436 (dolist (dir (cons package-user-dir package-directory-list))
437 (when (file-directory-p dir)
438 (dolist (subdir (directory-files dir))
439 (let ((pkg-dir (expand-file-name subdir dir)))
440 (when (file-directory-p pkg-dir)
441 (package-load-descriptor pkg-dir)))))))
442
443 (defun package-disabled-p (pkg-name version)
444 "Return whether PKG-NAME at VERSION can be activated.
445 The decision is made according to `package-load-list'.
446 Return nil if the package can be activated.
447 Return t if the package is completely disabled.
448 Return the max version (as a string) if the package is held at a lower version."
449 (let ((force (assq pkg-name package-load-list)))
450 (cond ((null force) (not (memq 'all package-load-list)))
451 ((null (setq force (cadr force))) t) ; disabled
452 ((eq force t) nil)
453 ((stringp force) ; held
454 (unless (version-list-= version (version-to-list force))
455 force))
456 (t (error "Invalid element in `package-load-list'")))))
457
458 (defun package-activate-1 (pkg-desc)
459 (let* ((name (package-desc-name pkg-desc))
460 (pkg-dir (package-desc-dir pkg-desc)))
461 (unless pkg-dir
462 (error "Internal error: unable to find directory for `%s'"
463 (package-desc-full-name pkg-desc)))
464 ;; Add info node.
465 (when (file-exists-p (expand-file-name "dir" pkg-dir))
466 ;; FIXME: not the friendliest, but simple.
467 (require 'info)
468 (info-initialize)
469 (push pkg-dir Info-directory-list))
470 ;; Add to load path, add autoloads, and activate the package.
471 (push pkg-dir load-path)
472 (load (expand-file-name (format "%s-autoloads" name) pkg-dir) nil t)
473 (push name package-activated-list)
474 ;; Don't return nil.
475 t))
476
477 (defun package-built-in-p (package &optional min-version)
478 "Return true if PACKAGE is built-in to Emacs.
479 Optional arg MIN-VERSION, if non-nil, should be a version list
480 specifying the minimum acceptable version."
481 (let ((bi (assq package package--builtin-versions)))
482 (cond
483 (bi (version-list-<= min-version (cdr bi)))
484 (min-version nil)
485 (t
486 (require 'finder-inf nil t) ; For `package--builtins'.
487 (assq package package--builtins)))))
488
489 (defun package--from-builtin (bi-desc)
490 (package-desc-create :name (pop bi-desc)
491 :version (package--bi-desc-version bi-desc)
492 :summary (package--bi-desc-summary bi-desc)
493 :dir 'builtin))
494
495 ;; This function goes ahead and activates a newer version of a package
496 ;; if an older one was already activated. This is not ideal; we'd at
497 ;; least need to check to see if the package has actually been loaded,
498 ;; and not merely activated.
499 (defun package-activate (package &optional force)
500 "Activate package PACKAGE.
501 If FORCE is true, (re-)activate it if it's already activated."
502 (let ((pkg-descs (cdr (assq package package-alist))))
503 ;; Check if PACKAGE is available in `package-alist'.
504 (while
505 (when pkg-descs
506 (let ((available-version (package-desc-version (car pkg-descs))))
507 (or (package-disabled-p package available-version)
508 ;; Prefer a builtin package.
509 (package-built-in-p package available-version))))
510 (setq pkg-descs (cdr pkg-descs)))
511 (cond
512 ;; If no such package is found, maybe it's built-in.
513 ((null pkg-descs)
514 (package-built-in-p package))
515 ;; If the package is already activated, just return t.
516 ((and (memq package package-activated-list) (not force))
517 t)
518 ;; Otherwise, proceed with activation.
519 (t
520 (let* ((pkg-vec (car pkg-descs))
521 (fail (catch 'dep-failure
522 ;; Activate its dependencies recursively.
523 (dolist (req (package-desc-reqs pkg-vec))
524 (unless (package-activate (car req) (cadr req))
525 (throw 'dep-failure req))))))
526 (if fail
527 (warn "Unable to activate package `%s'.
528 Required package `%s-%s' is unavailable"
529 package (car fail) (package-version-join (cadr fail)))
530 ;; If all goes well, activate the package itself.
531 (package-activate-1 pkg-vec)))))))
532
533 (defun define-package (_name-string _version-string
534 &optional _docstring _requirements
535 &rest _extra-properties)
536 "Define a new package.
537 NAME-STRING is the name of the package, as a string.
538 VERSION-STRING is the version of the package, as a string.
539 DOCSTRING is a short description of the package, a string.
540 REQUIREMENTS is a list of dependencies on other packages.
541 Each requirement is of the form (OTHER-PACKAGE OTHER-VERSION),
542 where OTHER-VERSION is a string.
543
544 EXTRA-PROPERTIES is currently unused."
545 ;; FIXME: Placeholder! Should we keep it?
546 (error "Don't call me!"))
547
548 (defun package-process-define-package (exp origin)
549 (unless (eq (car-safe exp) 'define-package)
550 (error "Can't find define-package in %s" origin))
551 (let* ((new-pkg-desc (apply #'package-desc-from-define (cdr exp)))
552 (name (package-desc-name new-pkg-desc))
553 (version (package-desc-version new-pkg-desc))
554 (old-pkgs (assq name package-alist)))
555 (if (null old-pkgs)
556 ;; If there's no old package, just add this to `package-alist'.
557 (push (list name new-pkg-desc) package-alist)
558 ;; If there is, insert the new package at the right place in the list.
559 (while
560 (if (and (cdr old-pkgs)
561 (version-list-< version
562 (package-desc-version (cadr old-pkgs))))
563 (setq old-pkgs (cdr old-pkgs))
564 (push new-pkg-desc (cdr old-pkgs))
565 nil)))
566 new-pkg-desc))
567
568 ;; From Emacs 22, but changed so it adds to load-path.
569 (defun package-autoload-ensure-default-file (file)
570 "Make sure that the autoload file FILE exists and if not create it."
571 (unless (file-exists-p file)
572 (write-region
573 (concat ";;; " (file-name-nondirectory file)
574 " --- automatically extracted autoloads\n"
575 ";;\n"
576 ";;; Code:\n"
577 "(add-to-list 'load-path (or (file-name-directory #$) (car load-path)))\n"
578 "\f\n;; Local Variables:\n"
579 ";; version-control: never\n"
580 ";; no-byte-compile: t\n"
581 ";; no-update-autoloads: t\n"
582 ";; End:\n"
583 ";;; " (file-name-nondirectory file)
584 " ends here\n")
585 nil file))
586 file)
587
588 (defvar generated-autoload-file)
589 (defvar version-control)
590
591 (defun package-generate-autoloads (name pkg-dir)
592 (require 'autoload) ;Load before we let-bind generated-autoload-file!
593 (let* ((auto-name (format "%s-autoloads.el" name))
594 ;;(ignore-name (concat name "-pkg.el"))
595 (generated-autoload-file (expand-file-name auto-name pkg-dir))
596 (version-control 'never))
597 (package-autoload-ensure-default-file generated-autoload-file)
598 (update-directory-autoloads pkg-dir)
599 (let ((buf (find-buffer-visiting generated-autoload-file)))
600 (when buf (kill-buffer buf)))
601 auto-name))
602
603 (defvar tar-parse-info)
604 (declare-function tar-untar-buffer "tar-mode" ())
605 (declare-function tar-header-name "tar-mode" (tar-header) t)
606 (declare-function tar-header-link-type "tar-mode" (tar-header) t)
607
608 (defun package-untar-buffer (dir)
609 "Untar the current buffer.
610 This uses `tar-untar-buffer' from Tar mode. All files should
611 untar into a directory named DIR; otherwise, signal an error."
612 (require 'tar-mode)
613 (tar-mode)
614 ;; Make sure everything extracts into DIR.
615 (let ((regexp (concat "\\`" (regexp-quote (expand-file-name dir)) "/"))
616 (case-fold-search (memq system-type '(windows-nt ms-dos cygwin))))
617 (dolist (tar-data tar-parse-info)
618 (let ((name (expand-file-name (tar-header-name tar-data))))
619 (or (string-match regexp name)
620 ;; Tarballs created by some utilities don't list
621 ;; directories with a trailing slash (Bug#13136).
622 (and (string-equal dir name)
623 (eq (tar-header-link-type tar-data) 5))
624 (error "Package does not untar cleanly into directory %s/" dir)))))
625 (tar-untar-buffer))
626
627 (defun package-generate-description-file (pkg-desc pkg-dir)
628 "Create the foo-pkg.el file for single-file packages."
629 (let* ((name (package-desc-name pkg-desc))
630 (pkg-file (expand-file-name (package--description-file pkg-dir)
631 pkg-dir)))
632 (let ((print-level nil)
633 (print-quoted t)
634 (print-length nil))
635 (write-region
636 (concat
637 (prin1-to-string
638 (list 'define-package
639 (symbol-name name)
640 (package-version-join (package-desc-version pkg-desc))
641 (package-desc-summary pkg-desc)
642 (let ((requires (package-desc-reqs pkg-desc)))
643 (list 'quote
644 ;; Turn version lists into string form.
645 (mapcar
646 (lambda (elt)
647 (list (car elt)
648 (package-version-join (cadr elt))))
649 requires)))))
650 "\n")
651 nil
652 pkg-file))))
653
654 (defun package-unpack (pkg-desc)
655 "Install the contents of the current buffer as a package."
656 (let* ((name (package-desc-name pkg-desc))
657 (dirname (package-desc-full-name pkg-desc))
658 (pkg-dir (expand-file-name dirname package-user-dir)))
659 (pcase (package-desc-kind pkg-desc)
660 (`tar
661 (make-directory package-user-dir t)
662 ;; FIXME: should we delete PKG-DIR if it exists?
663 (let* ((default-directory (file-name-as-directory package-user-dir)))
664 (package-untar-buffer dirname)))
665 (`single
666 (let ((el-file (expand-file-name (format "%s.el" name) pkg-dir)))
667 (make-directory pkg-dir t)
668 (package--write-file-no-coding el-file)))
669 (kind (error "Unknown package kind: %S" kind)))
670 (package--make-autoloads-and-stuff pkg-desc pkg-dir)
671 ;; Update package-alist.
672 (let ((new-desc (package-load-descriptor pkg-dir)))
673 ;; FIXME: Check that `new-desc' matches `desc'!
674 ;; FIXME: Compilation should be done as a separate, optional, step.
675 ;; E.g. for multi-package installs, we should first install all packages
676 ;; and then compile them.
677 (package--compile new-desc))
678 ;; Try to activate it.
679 (package-activate name 'force)
680 pkg-dir))
681
682 (defun package--make-autoloads-and-stuff (pkg-desc pkg-dir)
683 "Generate autoloads, description file, etc.. for PKG-DESC installed at PKG-DIR."
684 (package-generate-autoloads (package-desc-name pkg-desc) pkg-dir)
685 (let ((desc-file (package--description-file pkg-dir)))
686 (unless (file-exists-p desc-file)
687 (package-generate-description-file pkg-desc pkg-dir)))
688 ;; FIXME: Create foo.info and dir file from foo.texi?
689 )
690
691 (defun package--compile (pkg-desc)
692 "Byte-compile installed package PKG-DESC."
693 (package-activate-1 pkg-desc)
694 (byte-recompile-directory (package-desc-dir pkg-desc) 0 t))
695
696 (defun package--write-file-no-coding (file-name)
697 (let ((buffer-file-coding-system 'no-conversion))
698 (write-region (point-min) (point-max) file-name)))
699
700 (defmacro package--with-work-buffer (location file &rest body)
701 "Run BODY in a buffer containing the contents of FILE at LOCATION.
702 LOCATION is the base location of a package archive, and should be
703 one of the URLs (or file names) specified in `package-archives'.
704 FILE is the name of a file relative to that base location.
705
706 This macro retrieves FILE from LOCATION into a temporary buffer,
707 and evaluates BODY while that buffer is current. This work
708 buffer is killed afterwards. Return the last value in BODY."
709 (declare (indent 2) (debug t))
710 `(let* ((http (string-match "\\`https?:" ,location))
711 (buffer
712 (if http
713 (url-retrieve-synchronously (concat ,location ,file))
714 (generate-new-buffer "*package work buffer*"))))
715 (prog1
716 (with-current-buffer buffer
717 (if http
718 (progn (package-handle-response)
719 (re-search-forward "^$" nil 'move)
720 (forward-char)
721 (delete-region (point-min) (point)))
722 (unless (file-name-absolute-p ,location)
723 (error "Archive location %s is not an absolute file name"
724 ,location))
725 (insert-file-contents (expand-file-name ,file ,location)))
726 ,@body)
727 (kill-buffer buffer))))
728
729 (defun package-handle-response ()
730 "Handle the response from a `url-retrieve-synchronously' call.
731 Parse the HTTP response and throw if an error occurred.
732 The url package seems to require extra processing for this.
733 This should be called in a `save-excursion', in the download buffer.
734 It will move point to somewhere in the headers."
735 ;; We assume HTTP here.
736 (require 'url-http)
737 (let ((response (url-http-parse-response)))
738 (when (or (< response 200) (>= response 300))
739 (error "Error during download request:%s"
740 (buffer-substring-no-properties (point) (line-end-position))))))
741
742 (defun package-install-from-archive (pkg-desc)
743 "Download and install a tar package."
744 (let ((location (package-archive-base pkg-desc))
745 (file (concat (package-desc-full-name pkg-desc)
746 (package-desc-suffix pkg-desc))))
747 (package--with-work-buffer location file
748 (package-unpack pkg-desc))))
749
750 (defvar package--initialized nil)
751
752 (defun package-installed-p (package &optional min-version)
753 "Return true if PACKAGE, of MIN-VERSION or newer, is installed.
754 MIN-VERSION should be a version list."
755 (unless package--initialized (error "package.el is not yet initialized!"))
756 (or
757 (let ((pkg-descs (cdr (assq package package-alist))))
758 (and pkg-descs
759 (version-list-<= min-version
760 (package-desc-version (car pkg-descs)))))
761 ;; Also check built-in packages.
762 (package-built-in-p package min-version)))
763
764 (defun package-compute-transaction (packages requirements)
765 "Return a list of packages to be installed, including PACKAGES.
766 PACKAGES should be a list of `package-desc'.
767
768 REQUIREMENTS should be a list of additional requirements; each
769 element in this list should have the form (PACKAGE VERSION-LIST),
770 where PACKAGE is a package name and VERSION-LIST is the required
771 version of that package.
772
773 This function recursively computes the requirements of the
774 packages in REQUIREMENTS, and returns a list of all the packages
775 that must be installed. Packages that are already installed are
776 not included in this list."
777 ;; FIXME: We really should use backtracking to explore the whole
778 ;; search space (e.g. if foo require bar-1.3, and bar-1.4 requires toto-1.1
779 ;; whereas bar-1.3 requires toto-1.0 and the user has put a hold on toto-1.0:
780 ;; the current code might fail to see that it could install foo by using the
781 ;; older bar-1.3).
782 (dolist (elt requirements)
783 (let* ((next-pkg (car elt))
784 (next-version (cadr elt))
785 (already ()))
786 (dolist (pkg packages)
787 (if (eq next-pkg (package-desc-name pkg))
788 (setq already pkg)))
789 (cond
790 (already
791 (if (version-list-< next-version (package-desc-version already))
792 ;; Move to front, so it gets installed early enough (bug#14082).
793 (setq packages (cons already (delq already packages)))
794 (error "Need package `%s-%s', but only %s is available"
795 next-pkg (package-version-join next-version)
796 (package-version-join (package-desc-version already)))))
797
798 ((package-installed-p next-pkg next-version) nil)
799
800 (t
801 ;; A package is required, but not installed. It might also be
802 ;; blocked via `package-load-list'.
803 (let ((pkg-descs (cdr (assq next-pkg package-archive-contents)))
804 (found nil)
805 (problem nil))
806 (while (and pkg-descs (not found))
807 (let* ((pkg-desc (pop pkg-descs))
808 (version (package-desc-version pkg-desc))
809 (disabled (package-disabled-p next-pkg version)))
810 (cond
811 ((version-list-< version next-version)
812 (error
813 "Need package `%s-%s', but only %s is available"
814 next-pkg (package-version-join next-version)
815 (package-version-join version)))
816 (disabled
817 (unless problem
818 (setq problem
819 (if (stringp disabled)
820 (format "Package `%s' held at version %s, \
821 but version %s required"
822 next-pkg disabled
823 (package-version-join next-version))
824 (format "Required package '%s' is disabled"
825 next-pkg)))))
826 (t (setq found pkg-desc)))))
827 (unless found
828 (if problem
829 (error problem)
830 (error "Package `%s-%s' is unavailable"
831 next-pkg (package-version-join next-version))))
832 (setq packages
833 (package-compute-transaction (cons found packages)
834 (package-desc-reqs found))))))))
835 packages)
836
837 (defun package-read-from-string (str)
838 "Read a Lisp expression from STR.
839 Signal an error if the entire string was not used."
840 (let* ((read-data (read-from-string str))
841 (more-left
842 (condition-case nil
843 ;; The call to `ignore' suppresses a compiler warning.
844 (progn (ignore (read-from-string
845 (substring str (cdr read-data))))
846 t)
847 (end-of-file nil))))
848 (if more-left
849 (error "Can't read whole string")
850 (car read-data))))
851
852 (defun package--read-archive-file (file)
853 "Re-read archive file FILE, if it exists.
854 Will return the data from the file, or nil if the file does not exist.
855 Will throw an error if the archive version is too new."
856 (let ((filename (expand-file-name file package-user-dir)))
857 (when (file-exists-p filename)
858 (with-temp-buffer
859 (insert-file-contents-literally filename)
860 (let ((contents (read (current-buffer))))
861 (if (> (car contents) package-archive-version)
862 (error "Package archive version %d is higher than %d"
863 (car contents) package-archive-version))
864 (cdr contents))))))
865
866 (defun package-read-all-archive-contents ()
867 "Re-read `archive-contents', if it exists.
868 If successful, set `package-archive-contents'."
869 (setq package-archive-contents nil)
870 (dolist (archive package-archives)
871 (package-read-archive-contents (car archive))))
872
873 (defun package-read-archive-contents (archive)
874 "Re-read archive contents for ARCHIVE.
875 If successful, set the variable `package-archive-contents'.
876 If the archive version is too new, signal an error."
877 ;; Version 1 of 'archive-contents' is identical to our internal
878 ;; representation.
879 (let* ((contents-file (format "archives/%s/archive-contents" archive))
880 (contents (package--read-archive-file contents-file)))
881 (when contents
882 (dolist (package contents)
883 (package--add-to-archive-contents package archive)))))
884
885 ;; Package descriptor objects used inside the "archive-contents" file.
886 ;; Changing this defstruct implies changing the format of the
887 ;; "archive-contents" files.
888 (cl-defstruct (package--ac-desc
889 (:constructor package-make-ac-desc (version reqs summary kind))
890 (:copier nil)
891 (:type vector))
892 version reqs summary kind)
893
894 (defun package--add-to-archive-contents (package archive)
895 "Add the PACKAGE from the given ARCHIVE if necessary.
896 PACKAGE should have the form (NAME . PACKAGE--AC-DESC).
897 Also, add the originating archive to the `package-desc' structure."
898 (let* ((name (car package))
899 (version (package--ac-desc-version (cdr package)))
900 (pkg-desc
901 (package-desc-create
902 :name name
903 :version version
904 :reqs (package--ac-desc-reqs (cdr package))
905 :summary (package--ac-desc-summary (cdr package))
906 :kind (package--ac-desc-kind (cdr package))
907 :archive archive))
908 (existing-packages (assq name package-archive-contents))
909 (pinned-to-archive (assoc name package-pinned-packages)))
910 (cond
911 ;; Skip entirely if pinned to another archive or already installed.
912 ((or (and pinned-to-archive
913 (not (equal (cdr pinned-to-archive) archive)))
914 (let ((bi (assq name package--builtin-versions)))
915 (and bi (version-list-= version (cdr bi))))
916 (let ((ins (cdr (assq name package-alist))))
917 (and ins (version-list-= version
918 (package-desc-version (car ins))))))
919 nil)
920 ((not existing-packages)
921 (push (list name pkg-desc) package-archive-contents))
922 (t
923 (while
924 (if (and (cdr existing-packages)
925 (version-list-<
926 version (package-desc-version (cadr existing-packages))))
927 (setq existing-packages (cdr existing-packages))
928 (push pkg-desc (cdr existing-packages))
929 nil))))))
930
931 (defun package-download-transaction (packages)
932 "Download and install all the packages in PACKAGES.
933 PACKAGES should be a list of package-desc.
934 This function assumes that all package requirements in
935 PACKAGES are satisfied, i.e. that PACKAGES is computed
936 using `package-compute-transaction'."
937 (mapc #'package-install-from-archive packages))
938
939 ;;;###autoload
940 (defun package-install (pkg)
941 "Install the package PKG.
942 PKG can be a package-desc or the package name of one the available packages
943 in an archive in `package-archives'. Interactively, prompt for its name."
944 (interactive
945 (progn
946 ;; Initialize the package system to get the list of package
947 ;; symbols for completion.
948 (unless package--initialized
949 (package-initialize t))
950 (unless package-archive-contents
951 (package-refresh-contents))
952 (list (intern (completing-read
953 "Install package: "
954 (mapcar (lambda (elt) (symbol-name (car elt)))
955 package-archive-contents)
956 nil t)))))
957 (package-download-transaction
958 (if (package-desc-p pkg)
959 (package-compute-transaction (list pkg)
960 (package-desc-reqs pkg))
961 (package-compute-transaction ()
962 (list (list pkg))))))
963
964 (defun package-strip-rcs-id (str)
965 "Strip RCS version ID from the version string STR.
966 If the result looks like a dotted numeric version, return it.
967 Otherwise return nil."
968 (when str
969 (when (string-match "\\`[ \t]*[$]Revision:[ \t]+" str)
970 (setq str (substring str (match-end 0))))
971 (condition-case nil
972 (if (version-to-list str)
973 str)
974 (error nil))))
975
976 (defun package-buffer-info ()
977 "Return a `package-desc' describing the package in the current buffer.
978
979 If the buffer does not contain a conforming package, signal an
980 error. If there is a package, narrow the buffer to the file's
981 boundaries."
982 (goto-char (point-min))
983 (unless (re-search-forward "^;;; \\([^ ]*\\)\\.el ---[ \t]*\\(.*?\\)[ \t]*\\(-\\*-.*-\\*-[ \t]*\\)?$" nil t)
984 (error "Packages lacks a file header"))
985 (let ((file-name (match-string-no-properties 1))
986 (desc (match-string-no-properties 2))
987 (start (line-beginning-position)))
988 (unless (search-forward (concat ";;; " file-name ".el ends here"))
989 (error "Package lacks a terminating comment"))
990 ;; Try to include a trailing newline.
991 (forward-line)
992 (narrow-to-region start (point))
993 (require 'lisp-mnt)
994 ;; Use some headers we've invented to drive the process.
995 (let* ((requires-str (lm-header "package-requires"))
996 ;; Prefer Package-Version; if defined, the package author
997 ;; probably wants us to use it. Otherwise try Version.
998 (pkg-version
999 (or (package-strip-rcs-id (lm-header "package-version"))
1000 (package-strip-rcs-id (lm-header "version")))))
1001 (unless pkg-version
1002 (error
1003 "Package lacks a \"Version\" or \"Package-Version\" header"))
1004 (package-desc-from-define
1005 file-name pkg-version desc
1006 (if requires-str (package-read-from-string requires-str))
1007 :kind 'single))))
1008
1009 (declare-function tar-get-file-descriptor "tar-mode" (file))
1010 (declare-function tar--extract "tar-mode" (descriptor))
1011
1012 (defun package-tar-file-info ()
1013 "Find package information for a tar file.
1014 The return result is a `package-desc'."
1015 (cl-assert (derived-mode-p 'tar-mode))
1016 (let* ((dir-name (file-name-directory
1017 (tar-header-name (car tar-parse-info))))
1018 (desc-file (package--description-file dir-name))
1019 (tar-desc (tar-get-file-descriptor (concat dir-name desc-file))))
1020 (unless tar-desc
1021 (error "No package descriptor file found"))
1022 (with-current-buffer (tar--extract tar-desc)
1023 (goto-char (point-min))
1024 (unwind-protect
1025 (let* ((pkg-def-parsed (read (current-buffer)))
1026 (pkg-desc
1027 (if (not (eq (car pkg-def-parsed) 'define-package))
1028 (error "Can't find define-package in %s"
1029 (tar-header-name tar-desc))
1030 (apply #'package-desc-from-define
1031 (append (cdr pkg-def-parsed))))))
1032 (setf (package-desc-kind pkg-desc) 'tar)
1033 pkg-desc)
1034 (kill-buffer (current-buffer))))))
1035
1036
1037 ;;;###autoload
1038 (defun package-install-from-buffer ()
1039 "Install a package from the current buffer.
1040 The current buffer is assumed to be a single .el or .tar file that follows the
1041 packaging guidelines; see info node `(elisp)Packaging'.
1042 Downloads and installs required packages as needed."
1043 (interactive)
1044 (let ((pkg-desc (if (derived-mode-p 'tar-mode)
1045 (package-tar-file-info)
1046 (package-buffer-info))))
1047 ;; Download and install the dependencies.
1048 (let* ((requires (package-desc-reqs pkg-desc))
1049 (transaction (package-compute-transaction nil requires)))
1050 (package-download-transaction transaction))
1051 ;; Install the package itself.
1052 (package-unpack pkg-desc)
1053 pkg-desc))
1054
1055 ;;;###autoload
1056 (defun package-install-file (file)
1057 "Install a package from a file.
1058 The file can either be a tar file or an Emacs Lisp file."
1059 (interactive "fPackage file name: ")
1060 (with-temp-buffer
1061 (insert-file-contents-literally file)
1062 (when (string-match "\\.tar\\'" file) (tar-mode))
1063 (package-install-from-buffer)))
1064
1065 (defun package-delete (pkg-desc)
1066 (let ((dir (package-desc-dir pkg-desc)))
1067 (if (not (string-prefix-p (file-name-as-directory
1068 (expand-file-name package-user-dir))
1069 (expand-file-name dir)))
1070 ;; Don't delete "system" packages.
1071 (error "Package `%s' is a system package, not deleting"
1072 (package-desc-full-name pkg-desc))
1073 (delete-directory dir t t)
1074 ;; Update package-alist.
1075 (let* ((name (package-desc-name pkg-desc)))
1076 (delete pkg-desc (assq name package-alist)))
1077 (message "Package `%s' deleted." (package-desc-full-name pkg-desc)))))
1078
1079 (defun package-archive-base (desc)
1080 "Return the archive containing the package NAME."
1081 (cdr (assoc (package-desc-archive desc) package-archives)))
1082
1083 (defun package--download-one-archive (archive file)
1084 "Retrieve an archive file FILE from ARCHIVE, and cache it.
1085 ARCHIVE should be a cons cell of the form (NAME . LOCATION),
1086 similar to an entry in `package-alist'. Save the cached copy to
1087 \"archives/NAME/archive-contents\" in `package-user-dir'."
1088 (let* ((dir (expand-file-name (format "archives/%s" (car archive))
1089 package-user-dir)))
1090 (package--with-work-buffer (cdr archive) file
1091 ;; Read the retrieved buffer to make sure it is valid (e.g. it
1092 ;; may fetch a URL redirect page).
1093 (when (listp (read buffer))
1094 (make-directory dir t)
1095 (setq buffer-file-name (expand-file-name file dir))
1096 (let ((version-control 'never))
1097 (save-buffer))))))
1098
1099 ;;;###autoload
1100 (defun package-refresh-contents ()
1101 "Download the ELPA archive description if needed.
1102 This informs Emacs about the latest versions of all packages, and
1103 makes them available for download."
1104 (interactive)
1105 ;; FIXME: Do it asynchronously.
1106 (unless (file-exists-p package-user-dir)
1107 (make-directory package-user-dir t))
1108 (dolist (archive package-archives)
1109 (condition-case-unless-debug nil
1110 (package--download-one-archive archive "archive-contents")
1111 (error (message "Failed to download `%s' archive."
1112 (car archive)))))
1113 (package-read-all-archive-contents))
1114
1115 ;;;###autoload
1116 (defun package-initialize (&optional no-activate)
1117 "Load Emacs Lisp packages, and activate them.
1118 The variable `package-load-list' controls which packages to load.
1119 If optional arg NO-ACTIVATE is non-nil, don't activate packages."
1120 (interactive)
1121 (setq package-alist nil)
1122 (package-load-all-descriptors)
1123 (package-read-all-archive-contents)
1124 (unless no-activate
1125 (dolist (elt package-alist)
1126 (package-activate (car elt))))
1127 (setq package--initialized t))
1128
1129 \f
1130 ;;;; Package description buffer.
1131
1132 ;;;###autoload
1133 (defun describe-package (package)
1134 "Display the full documentation of PACKAGE (a symbol)."
1135 (interactive
1136 (let* ((guess (function-called-at-point)))
1137 (require 'finder-inf nil t)
1138 ;; Load the package list if necessary (but don't activate them).
1139 (unless package--initialized
1140 (package-initialize t))
1141 (let ((packages (append (mapcar 'car package-alist)
1142 (mapcar 'car package-archive-contents)
1143 (mapcar 'car package--builtins))))
1144 (unless (memq guess packages)
1145 (setq guess nil))
1146 (setq packages (mapcar 'symbol-name packages))
1147 (let ((val
1148 (completing-read (if guess
1149 (format "Describe package (default %s): "
1150 guess)
1151 "Describe package: ")
1152 packages nil t nil nil guess)))
1153 (list (intern val))))))
1154 (if (not (or (package-desc-p package) (and package (symbolp package))))
1155 (message "No package specified")
1156 (help-setup-xref (list #'describe-package package)
1157 (called-interactively-p 'interactive))
1158 (with-help-window (help-buffer)
1159 (with-current-buffer standard-output
1160 (describe-package-1 package)))))
1161
1162 (defun describe-package-1 (pkg)
1163 (require 'lisp-mnt)
1164 (let* ((desc (or
1165 (if (package-desc-p pkg) pkg)
1166 (cadr (assq pkg package-alist))
1167 (let ((built-in (assq pkg package--builtins)))
1168 (if built-in
1169 (package--from-builtin built-in)
1170 (cadr (assq pkg package-archive-contents))))))
1171 (name (if desc (package-desc-name desc) pkg))
1172 (pkg-dir (if desc (package-desc-dir desc)))
1173 (reqs (if desc (package-desc-reqs desc)))
1174 (version (if desc (package-desc-version desc)))
1175 (archive (if desc (package-desc-archive desc)))
1176 (built-in (eq pkg-dir 'builtin))
1177 (installable (and archive (not built-in)))
1178 (status (if desc (package-desc-status desc) "orphan")))
1179 (prin1 name)
1180 (princ " is ")
1181 (princ (if (memq (aref status 0) '(?a ?e ?i ?o ?u)) "an " "a "))
1182 (princ status)
1183 (princ " package.\n\n")
1184
1185 (insert " " (propertize "Status" 'font-lock-face 'bold) ": ")
1186 (cond (built-in
1187 (insert (propertize (capitalize status)
1188 'font-lock-face 'font-lock-builtin-face)
1189 "."))
1190 (pkg-dir
1191 (insert (propertize (capitalize status) ;FIXME: Why comment-face?
1192 'font-lock-face 'font-lock-comment-face))
1193 (insert " in `")
1194 ;; Todo: Add button for uninstalling.
1195 (help-insert-xref-button (abbreviate-file-name
1196 (file-name-as-directory pkg-dir))
1197 'help-package-def pkg-dir)
1198 (if (and (package-built-in-p name)
1199 (not (package-built-in-p name version)))
1200 (insert "',\n shadowing a "
1201 (propertize "built-in package"
1202 'font-lock-face 'font-lock-builtin-face)
1203 ".")
1204 (insert "'.")))
1205 (installable
1206 (insert (capitalize status))
1207 (insert " from " (format "%s" archive))
1208 (insert " -- ")
1209 (let ((button-text (if (display-graphic-p) "Install" "[Install]"))
1210 (button-face (if (display-graphic-p)
1211 '(:box (:line-width 2 :color "dark grey")
1212 :background "light grey"
1213 :foreground "black")
1214 'link)))
1215 (insert-text-button button-text 'face button-face 'follow-link t
1216 'package-desc desc
1217 'action 'package-install-button-action)))
1218 (t (insert (capitalize status) ".")))
1219 (insert "\n")
1220 (and version
1221 (insert " "
1222 (propertize "Version" 'font-lock-face 'bold) ": "
1223 (package-version-join version) "\n"))
1224
1225 (setq reqs (if desc (package-desc-reqs desc)))
1226 (when reqs
1227 (insert " " (propertize "Requires" 'font-lock-face 'bold) ": ")
1228 (let ((first t)
1229 name vers text)
1230 (dolist (req reqs)
1231 (setq name (car req)
1232 vers (cadr req)
1233 text (format "%s-%s" (symbol-name name)
1234 (package-version-join vers)))
1235 (cond (first (setq first nil))
1236 ((>= (+ 2 (current-column) (length text))
1237 (window-width))
1238 (insert ",\n "))
1239 (t (insert ", ")))
1240 (help-insert-xref-button text 'help-package name))
1241 (insert "\n")))
1242 (insert " " (propertize "Summary" 'font-lock-face 'bold)
1243 ": " (if desc (package-desc-summary desc)) "\n")
1244
1245 (let* ((all-pkgs (append (cdr (assq name package-alist))
1246 (cdr (assq name package-archive-contents))
1247 (let ((bi (assq name package--builtins)))
1248 (if bi (list (package--from-builtin bi))))))
1249 (other-pkgs (delete desc all-pkgs)))
1250 (when other-pkgs
1251 (insert " " (propertize "Other versions" 'font-lock-face 'bold) ": "
1252 (mapconcat
1253 (lambda (opkg)
1254 (let* ((ov (package-desc-version opkg))
1255 (dir (package-desc-dir opkg))
1256 (from (or (package-desc-archive opkg)
1257 (if (stringp dir) "installed" dir))))
1258 (if (not ov) (format "%s" from)
1259 (format "%s (%s)"
1260 (make-text-button (package-version-join ov) nil
1261 'face 'link
1262 'follow-link t
1263 'action
1264 (lambda (_button)
1265 (describe-package opkg)))
1266 from))))
1267 other-pkgs ", ")
1268 ".\n")))
1269
1270 (insert "\n")
1271
1272 (if built-in
1273 ;; For built-in packages, insert the commentary.
1274 (let ((fn (locate-file (format "%s.el" name) load-path
1275 load-file-rep-suffixes))
1276 (opoint (point)))
1277 (insert (or (lm-commentary fn) ""))
1278 (save-excursion
1279 (goto-char opoint)
1280 (when (re-search-forward "^;;; Commentary:\n" nil t)
1281 (replace-match ""))
1282 (while (re-search-forward "^\\(;+ ?\\)" nil t)
1283 (replace-match ""))))
1284 (let ((readme (expand-file-name (format "%s-readme.txt" name)
1285 package-user-dir))
1286 readme-string)
1287 ;; For elpa packages, try downloading the commentary. If that
1288 ;; fails, try an existing readme file in `package-user-dir'.
1289 (cond ((condition-case nil
1290 (package--with-work-buffer
1291 (package-archive-base desc)
1292 (format "%s-readme.txt" name)
1293 (setq buffer-file-name
1294 (expand-file-name readme package-user-dir))
1295 (let ((version-control 'never))
1296 (save-buffer))
1297 (setq readme-string (buffer-string))
1298 t)
1299 (error nil))
1300 (insert readme-string))
1301 ((file-readable-p readme)
1302 (insert-file-contents readme)
1303 (goto-char (point-max))))))))
1304
1305 (defun package-install-button-action (button)
1306 (let ((pkg-desc (button-get button 'package-desc)))
1307 (when (y-or-n-p (format "Install package `%s'? "
1308 (package-desc-full-name pkg-desc)))
1309 (package-install pkg-desc)
1310 (revert-buffer nil t)
1311 (goto-char (point-min)))))
1312
1313 \f
1314 ;;;; Package menu mode.
1315
1316 (defvar package-menu-mode-map
1317 (let ((map (make-sparse-keymap))
1318 (menu-map (make-sparse-keymap "Package")))
1319 (set-keymap-parent map tabulated-list-mode-map)
1320 (define-key map "\C-m" 'package-menu-describe-package)
1321 (define-key map "u" 'package-menu-mark-unmark)
1322 (define-key map "\177" 'package-menu-backup-unmark)
1323 (define-key map "d" 'package-menu-mark-delete)
1324 (define-key map "i" 'package-menu-mark-install)
1325 (define-key map "U" 'package-menu-mark-upgrades)
1326 (define-key map "r" 'package-menu-refresh)
1327 (define-key map "~" 'package-menu-mark-obsolete-for-deletion)
1328 (define-key map "x" 'package-menu-execute)
1329 (define-key map "h" 'package-menu-quick-help)
1330 (define-key map "?" 'package-menu-describe-package)
1331 (define-key map [menu-bar package-menu] (cons "Package" menu-map))
1332 (define-key menu-map [mq]
1333 '(menu-item "Quit" quit-window
1334 :help "Quit package selection"))
1335 (define-key menu-map [s1] '("--"))
1336 (define-key menu-map [mn]
1337 '(menu-item "Next" next-line
1338 :help "Next Line"))
1339 (define-key menu-map [mp]
1340 '(menu-item "Previous" previous-line
1341 :help "Previous Line"))
1342 (define-key menu-map [s2] '("--"))
1343 (define-key menu-map [mu]
1344 '(menu-item "Unmark" package-menu-mark-unmark
1345 :help "Clear any marks on a package and move to the next line"))
1346 (define-key menu-map [munm]
1347 '(menu-item "Unmark Backwards" package-menu-backup-unmark
1348 :help "Back up one line and clear any marks on that package"))
1349 (define-key menu-map [md]
1350 '(menu-item "Mark for Deletion" package-menu-mark-delete
1351 :help "Mark a package for deletion and move to the next line"))
1352 (define-key menu-map [mi]
1353 '(menu-item "Mark for Install" package-menu-mark-install
1354 :help "Mark a package for installation and move to the next line"))
1355 (define-key menu-map [mupgrades]
1356 '(menu-item "Mark Upgradable Packages" package-menu-mark-upgrades
1357 :help "Mark packages that have a newer version for upgrading"))
1358 (define-key menu-map [s3] '("--"))
1359 (define-key menu-map [mg]
1360 '(menu-item "Update Package List" revert-buffer
1361 :help "Update the list of packages"))
1362 (define-key menu-map [mr]
1363 '(menu-item "Refresh Package List" package-menu-refresh
1364 :help "Download the ELPA archive"))
1365 (define-key menu-map [s4] '("--"))
1366 (define-key menu-map [mt]
1367 '(menu-item "Mark Obsolete Packages" package-menu-mark-obsolete-for-deletion
1368 :help "Mark all obsolete packages for deletion"))
1369 (define-key menu-map [mx]
1370 '(menu-item "Execute Actions" package-menu-execute
1371 :help "Perform all the marked actions"))
1372 (define-key menu-map [s5] '("--"))
1373 (define-key menu-map [mh]
1374 '(menu-item "Help" package-menu-quick-help
1375 :help "Show short key binding help for package-menu-mode"))
1376 (define-key menu-map [mc]
1377 '(menu-item "View Commentary" package-menu-view-commentary
1378 :help "Display information about this package"))
1379 map)
1380 "Local keymap for `package-menu-mode' buffers.")
1381
1382 (defvar package-menu--new-package-list nil
1383 "List of newly-available packages since `list-packages' was last called.")
1384
1385 (define-derived-mode package-menu-mode tabulated-list-mode "Package Menu"
1386 "Major mode for browsing a list of packages.
1387 Letters do not insert themselves; instead, they are commands.
1388 \\<package-menu-mode-map>
1389 \\{package-menu-mode-map}"
1390 (setq tabulated-list-format [("Package" 18 package-menu--name-predicate)
1391 ("Version" 12 nil)
1392 ("Status" 10 package-menu--status-predicate)
1393 ("Description" 0 nil)])
1394 (setq tabulated-list-padding 2)
1395 (setq tabulated-list-sort-key (cons "Status" nil))
1396 (add-hook 'tabulated-list-revert-hook 'package-menu--refresh nil t)
1397 (tabulated-list-init-header))
1398
1399 (defmacro package--push (pkg-desc status listname)
1400 "Convenience macro for `package-menu--generate'.
1401 If the alist stored in the symbol LISTNAME lacks an entry for a
1402 package PKG-DESC, add one. The alist is keyed with PKG-DESC."
1403 `(unless (assoc ,pkg-desc ,listname)
1404 ;; FIXME: Should we move status into pkg-desc?
1405 (push (cons ,pkg-desc ,status) ,listname)))
1406
1407 (defvar package-list-unversioned nil
1408 "If non-nil include packages that don't have a version in `list-package'.")
1409
1410 (defun package-desc-status (pkg-desc)
1411 (let* ((name (package-desc-name pkg-desc))
1412 (dir (package-desc-dir pkg-desc))
1413 (lle (assq name package-load-list))
1414 (held (cadr lle))
1415 (version (package-desc-version pkg-desc)))
1416 (cond
1417 ((eq dir 'builtin) "built-in")
1418 ((and lle (null held)) "disabled")
1419 ((stringp held)
1420 (let ((hv (if (stringp held) (version-to-list held))))
1421 (cond
1422 ((version-list-= version hv) "held")
1423 ((version-list-< version hv) "obsolete")
1424 (t "disabled"))))
1425 ((package-built-in-p name version) "obsolete")
1426 (dir ;One of the installed packages.
1427 (cond
1428 ((not (file-exists-p (package-desc-dir pkg-desc))) "deleted")
1429 ((eq pkg-desc (cadr (assq name package-alist))) "installed")
1430 (t "obsolete")))
1431 (t
1432 (let* ((ins (cadr (assq name package-alist)))
1433 (ins-v (if ins (package-desc-version ins))))
1434 (cond
1435 ((or (null ins) (version-list-< ins-v version))
1436 (if (memq name package-menu--new-package-list)
1437 "new" "available"))
1438 ((version-list-< version ins-v) "obsolete")
1439 ((version-list-= version ins-v) "installed")))))))
1440
1441 (defun package-menu--refresh (&optional packages)
1442 "Re-populate the `tabulated-list-entries'.
1443 PACKAGES should be nil or t, which means to display all known packages."
1444 ;; Construct list of (PKG-DESC . STATUS).
1445 (unless packages (setq packages t))
1446 (let (info-list name)
1447 ;; Installed packages:
1448 (dolist (elt package-alist)
1449 (setq name (car elt))
1450 (when (or (eq packages t) (memq name packages))
1451 (dolist (pkg (cdr elt))
1452 (package--push pkg (package-desc-status pkg) info-list))))
1453
1454 ;; Built-in packages:
1455 (dolist (elt package--builtins)
1456 (setq name (car elt))
1457 (when (and (not (eq name 'emacs)) ; Hide the `emacs' package.
1458 (or package-list-unversioned
1459 (package--bi-desc-version (cdr elt)))
1460 (or (eq packages t) (memq name packages)))
1461 (package--push (package--from-builtin elt) "built-in" info-list)))
1462
1463 ;; Available and disabled packages:
1464 (dolist (elt package-archive-contents)
1465 (setq name (car elt))
1466 (when (or (eq packages t) (memq name packages))
1467 (dolist (pkg (cdr elt))
1468 ;; Hide obsolete packages.
1469 (unless (package-installed-p (package-desc-name pkg)
1470 (package-desc-version pkg))
1471 (package--push pkg (package-desc-status pkg) info-list)))))
1472
1473 ;; Print the result.
1474 (setq tabulated-list-entries
1475 (mapcar #'package-menu--print-info info-list))))
1476
1477 (defun package-menu--generate (remember-pos packages)
1478 "Populate the Package Menu.
1479 If REMEMBER-POS is non-nil, keep point on the same entry.
1480 PACKAGES should be t, which means to display all known packages,
1481 or a list of package names (symbols) to display."
1482 (package-menu--refresh packages)
1483 (tabulated-list-print remember-pos))
1484
1485 (defun package-menu--print-info (pkg)
1486 "Return a package entry suitable for `tabulated-list-entries'.
1487 PKG has the form (PKG-DESC . STATUS).
1488 Return (PKG-DESC [NAME VERSION STATUS DOC])."
1489 (let* ((pkg-desc (car pkg))
1490 (status (cdr pkg))
1491 (face (pcase status
1492 (`"built-in" 'font-lock-builtin-face)
1493 (`"available" 'default)
1494 (`"new" 'bold)
1495 (`"held" 'font-lock-constant-face)
1496 (`"disabled" 'font-lock-warning-face)
1497 (`"installed" 'font-lock-comment-face)
1498 (_ 'font-lock-warning-face)))) ; obsolete.
1499 (list pkg-desc
1500 (vector (list (symbol-name (package-desc-name pkg-desc))
1501 'face 'link
1502 'follow-link t
1503 'package-desc pkg-desc
1504 'action 'package-menu-describe-package)
1505 (propertize (package-version-join
1506 (package-desc-version pkg-desc))
1507 'font-lock-face face)
1508 (propertize status 'font-lock-face face)
1509 (propertize (package-desc-summary pkg-desc)
1510 'font-lock-face face)))))
1511
1512 (defun package-menu-refresh ()
1513 "Download the Emacs Lisp package archive.
1514 This fetches the contents of each archive specified in
1515 `package-archives', and then refreshes the package menu."
1516 (interactive)
1517 (unless (derived-mode-p 'package-menu-mode)
1518 (error "The current buffer is not a Package Menu"))
1519 (package-refresh-contents)
1520 (package-menu--generate t t))
1521
1522 (defun package-menu-describe-package (&optional button)
1523 "Describe the current package.
1524 If optional arg BUTTON is non-nil, describe its associated package."
1525 (interactive)
1526 (let ((pkg-desc (if button (button-get button 'package-desc)
1527 (tabulated-list-get-id))))
1528 (if pkg-desc
1529 (describe-package pkg-desc)
1530 (error "No package here"))))
1531
1532 ;; fixme numeric argument
1533 (defun package-menu-mark-delete (&optional _num)
1534 "Mark a package for deletion and move to the next line."
1535 (interactive "p")
1536 (if (member (package-menu-get-status) '("installed" "obsolete"))
1537 (tabulated-list-put-tag "D" t)
1538 (forward-line)))
1539
1540 (defun package-menu-mark-install (&optional _num)
1541 "Mark a package for installation and move to the next line."
1542 (interactive "p")
1543 (if (member (package-menu-get-status) '("available" "new"))
1544 (tabulated-list-put-tag "I" t)
1545 (forward-line)))
1546
1547 (defun package-menu-mark-unmark (&optional _num)
1548 "Clear any marks on a package and move to the next line."
1549 (interactive "p")
1550 (tabulated-list-put-tag " " t))
1551
1552 (defun package-menu-backup-unmark ()
1553 "Back up one line and clear any marks on that package."
1554 (interactive)
1555 (forward-line -1)
1556 (tabulated-list-put-tag " "))
1557
1558 (defun package-menu-mark-obsolete-for-deletion ()
1559 "Mark all obsolete packages for deletion."
1560 (interactive)
1561 (save-excursion
1562 (goto-char (point-min))
1563 (while (not (eobp))
1564 (if (equal (package-menu-get-status) "obsolete")
1565 (tabulated-list-put-tag "D" t)
1566 (forward-line 1)))))
1567
1568 (defun package-menu-quick-help ()
1569 "Show short key binding help for package-menu-mode."
1570 (interactive)
1571 (message "n-ext, i-nstall, d-elete, u-nmark, x-ecute, r-efresh, h-elp"))
1572
1573 (define-obsolete-function-alias
1574 'package-menu-view-commentary 'package-menu-describe-package "24.1")
1575
1576 (defun package-menu-get-status ()
1577 (let* ((id (tabulated-list-get-id))
1578 (entry (and id (assq id tabulated-list-entries))))
1579 (if entry
1580 (aref (cadr entry) 2)
1581 "")))
1582
1583 (defun package-menu--find-upgrades ()
1584 (let (installed available upgrades)
1585 ;; Build list of installed/available packages in this buffer.
1586 (dolist (entry tabulated-list-entries)
1587 ;; ENTRY is (PKG-DESC [NAME VERSION STATUS DOC])
1588 (let ((pkg-desc (car entry))
1589 (status (aref (cadr entry) 2)))
1590 (cond ((equal status "installed")
1591 (push pkg-desc installed))
1592 ((member status '("available" "new"))
1593 (push (cons (package-desc-name pkg-desc) pkg-desc)
1594 available)))))
1595 ;; Loop through list of installed packages, finding upgrades.
1596 (dolist (pkg-desc installed)
1597 (let ((avail-pkg (assq (package-desc-name pkg-desc) available)))
1598 (and avail-pkg
1599 (version-list-< (package-desc-version pkg-desc)
1600 (package-desc-version (cdr avail-pkg)))
1601 (push avail-pkg upgrades))))
1602 upgrades))
1603
1604 (defun package-menu-mark-upgrades ()
1605 "Mark all upgradable packages in the Package Menu.
1606 For each installed package with a newer version available, place
1607 an (I)nstall flag on the available version and a (D)elete flag on
1608 the installed version. A subsequent \\[package-menu-execute]
1609 call will upgrade the package."
1610 (interactive)
1611 (unless (derived-mode-p 'package-menu-mode)
1612 (error "The current buffer is not a Package Menu"))
1613 (let ((upgrades (package-menu--find-upgrades)))
1614 (if (null upgrades)
1615 (message "No packages to upgrade.")
1616 (widen)
1617 (save-excursion
1618 (goto-char (point-min))
1619 (while (not (eobp))
1620 (let* ((pkg-desc (tabulated-list-get-id))
1621 (upgrade (cdr (assq (package-desc-name pkg-desc) upgrades))))
1622 (cond ((null upgrade)
1623 (forward-line 1))
1624 ((equal pkg-desc upgrade)
1625 (package-menu-mark-install))
1626 (t
1627 (package-menu-mark-delete))))))
1628 (message "%d package%s marked for upgrading."
1629 (length upgrades)
1630 (if (= (length upgrades) 1) "" "s")))))
1631
1632 (defun package-menu-execute (&optional noquery)
1633 "Perform marked Package Menu actions.
1634 Packages marked for installation are downloaded and installed;
1635 packages marked for deletion are removed.
1636 Optional argument NOQUERY non-nil means do not ask the user to confirm."
1637 (interactive)
1638 (unless (derived-mode-p 'package-menu-mode)
1639 (error "The current buffer is not in Package Menu mode"))
1640 (let (install-list delete-list cmd pkg-desc)
1641 (save-excursion
1642 (goto-char (point-min))
1643 (while (not (eobp))
1644 (setq cmd (char-after))
1645 (unless (eq cmd ?\s)
1646 ;; This is the key PKG-DESC.
1647 (setq pkg-desc (tabulated-list-get-id))
1648 (cond ((eq cmd ?D)
1649 (push pkg-desc delete-list))
1650 ((eq cmd ?I)
1651 (push pkg-desc install-list))))
1652 (forward-line)))
1653 (when install-list
1654 (if (or
1655 noquery
1656 (yes-or-no-p
1657 (if (= (length install-list) 1)
1658 (format "Install package `%s'? "
1659 (package-desc-full-name (car install-list)))
1660 (format "Install these %d packages (%s)? "
1661 (length install-list)
1662 (mapconcat #'package-desc-full-name
1663 install-list ", ")))))
1664 (mapc 'package-install install-list)))
1665 ;; Delete packages, prompting if necessary.
1666 (when delete-list
1667 (if (or
1668 noquery
1669 (yes-or-no-p
1670 (if (= (length delete-list) 1)
1671 (format "Delete package `%s'? "
1672 (package-desc-full-name (car delete-list)))
1673 (format "Delete these %d packages (%s)? "
1674 (length delete-list)
1675 (mapconcat #'package-desc-full-name
1676 delete-list ", ")))))
1677 (dolist (elt delete-list)
1678 (condition-case-unless-debug err
1679 (package-delete elt)
1680 (error (message (cadr err)))))
1681 (error "Aborted")))
1682 (if (or delete-list install-list)
1683 (package-menu--generate t t)
1684 (message "No operations specified."))))
1685
1686 (defun package-menu--version-predicate (A B)
1687 (let ((vA (or (aref (cadr A) 1) '(0)))
1688 (vB (or (aref (cadr B) 1) '(0))))
1689 (if (version-list-= vA vB)
1690 (package-menu--name-predicate A B)
1691 (version-list-< vA vB))))
1692
1693 (defun package-menu--status-predicate (A B)
1694 (let ((sA (aref (cadr A) 2))
1695 (sB (aref (cadr B) 2)))
1696 (cond ((string= sA sB)
1697 (package-menu--name-predicate A B))
1698 ((string= sA "new") t)
1699 ((string= sB "new") nil)
1700 ((string= sA "available") t)
1701 ((string= sB "available") nil)
1702 ((string= sA "installed") t)
1703 ((string= sB "installed") nil)
1704 ((string= sA "held") t)
1705 ((string= sB "held") nil)
1706 ((string= sA "built-in") t)
1707 ((string= sB "built-in") nil)
1708 ((string= sA "obsolete") t)
1709 ((string= sB "obsolete") nil)
1710 (t (string< sA sB)))))
1711
1712 (defun package-menu--description-predicate (A B)
1713 (let ((dA (aref (cadr A) 3))
1714 (dB (aref (cadr B) 3)))
1715 (if (string= dA dB)
1716 (package-menu--name-predicate A B)
1717 (string< dA dB))))
1718
1719 (defun package-menu--name-predicate (A B)
1720 (string< (symbol-name (package-desc-name (car A)))
1721 (symbol-name (package-desc-name (car B)))))
1722
1723 ;;;###autoload
1724 (defun list-packages (&optional no-fetch)
1725 "Display a list of packages.
1726 This first fetches the updated list of packages before
1727 displaying, unless a prefix argument NO-FETCH is specified.
1728 The list is displayed in a buffer named `*Packages*'."
1729 (interactive "P")
1730 (require 'finder-inf nil t)
1731 ;; Initialize the package system if necessary.
1732 (unless package--initialized
1733 (package-initialize t))
1734 (let (old-archives new-packages)
1735 (unless no-fetch
1736 ;; Read the locally-cached archive-contents.
1737 (package-read-all-archive-contents)
1738 (setq old-archives package-archive-contents)
1739 ;; Fetch the remote list of packages.
1740 (package-refresh-contents)
1741 ;; Find which packages are new.
1742 (dolist (elt package-archive-contents)
1743 (unless (assq (car elt) old-archives)
1744 (push (car elt) new-packages))))
1745
1746 ;; Generate the Package Menu.
1747 (let ((buf (get-buffer-create "*Packages*")))
1748 (with-current-buffer buf
1749 (package-menu-mode)
1750 (set (make-local-variable 'package-menu--new-package-list)
1751 new-packages)
1752 (package-menu--generate nil t))
1753 ;; The package menu buffer has keybindings. If the user types
1754 ;; `M-x list-packages', that suggests it should become current.
1755 (switch-to-buffer buf))
1756
1757 (let ((upgrades (package-menu--find-upgrades)))
1758 (if upgrades
1759 (message "%d package%s can be upgraded; type `%s' to mark %s for upgrading."
1760 (length upgrades)
1761 (if (= (length upgrades) 1) "" "s")
1762 (substitute-command-keys "\\[package-menu-mark-upgrades]")
1763 (if (= (length upgrades) 1) "it" "them"))))))
1764
1765 ;;;###autoload
1766 (defalias 'package-list-packages 'list-packages)
1767
1768 ;; Used in finder.el
1769 (defun package-show-package-list (packages)
1770 "Display PACKAGES in a *Packages* buffer.
1771 This is similar to `list-packages', but it does not fetch the
1772 updated list of packages, and it only displays packages with
1773 names in PACKAGES (which should be a list of symbols)."
1774 (require 'finder-inf nil t)
1775 (let ((buf (get-buffer-create "*Packages*")))
1776 (with-current-buffer buf
1777 (package-menu-mode)
1778 (package-menu--generate nil packages))
1779 (switch-to-buffer buf)))
1780
1781 (defun package-list-packages-no-fetch ()
1782 "Display a list of packages.
1783 Does not fetch the updated list of packages before displaying.
1784 The list is displayed in a buffer named `*Packages*'."
1785 (interactive)
1786 (list-packages t))
1787
1788 (provide 'package)
1789
1790 ;;; package.el ends here