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