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