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