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