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