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