emacs-lisp/package.el (package-install): Rename ARG to MARK-SELECTED.
[emacs.git] / lisp / emacs-lisp / package.el
blobc322ce8cc56b4dd289ac4e71c5938c0baacb0c2a
1 ;;; package.el --- Simple package system for Emacs -*- lexical-binding:t -*-
3 ;; Copyright (C) 2007-2015 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 ;; - 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
162 ;;; Code:
164 (eval-when-compile (require 'cl-lib))
165 (eval-when-compile (require 'epg)) ;For setf accessors.
167 (require 'tabulated-list)
168 (require 'macroexp)
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-file-exists-p "url-http" (url))
209 (declare-function lm-header "lisp-mnt" (header))
210 (declare-function lm-commentary "lisp-mnt" (&optional file))
212 (defcustom package-archives '(("gnu" . "http://elpa.gnu.org/packages/"))
213 "An alist of archives from which to fetch.
214 The default value points to the GNU Emacs package repository.
216 Each element has the form (ID . LOCATION).
217 ID is an archive name, as a string.
218 LOCATION specifies the base location for the archive.
219 If it starts with \"http:\", it is treated as a HTTP URL;
220 otherwise it should be an absolute directory name.
221 (Other types of URL are currently not supported.)
223 Only add locations that you trust, since fetching and installing
224 a package can run arbitrary code."
225 :type '(alist :key-type (string :tag "Archive name")
226 :value-type (string :tag "URL or directory name"))
227 :risky t
228 :group 'package
229 :version "24.1")
231 (defcustom package-archive-priorities nil
232 "An alist of priorities for packages.
234 Each element has the form (ARCHIVE-ID . PRIORITY).
236 When installing packages, the package with the highest version
237 number from the archive with the highest priority is
238 selected. When higher versions are available from archives with
239 lower priorities, the user has to select those manually.
241 Archives not in this list have the priority 0."
242 :type '(alist :key-type (string :tag "Archive name")
243 :value-type (integer :tag "Priority (default is 0)"))
244 :risky t
245 :group 'package
246 :version "25.1")
248 (defcustom package-pinned-packages nil
249 "An alist of packages that are pinned to specific archives.
250 This can be useful if you have multiple package archives enabled,
251 and want to control which archive a given package gets installed from.
253 Each element of the alist has the form (PACKAGE . ARCHIVE), where:
254 PACKAGE is a symbol representing a package
255 ARCHIVE is a string representing an archive (it should be the car of
256 an element in `package-archives', e.g. \"gnu\").
258 Adding an entry to this variable means that only ARCHIVE will be
259 considered as a source for PACKAGE. If other archives provide PACKAGE,
260 they are ignored (for this package). If ARCHIVE does not contain PACKAGE,
261 the package will be unavailable."
262 :type '(alist :key-type (symbol :tag "Package")
263 :value-type (string :tag "Archive name"))
264 ;; I don't really see why this is risky...
265 ;; I suppose it could prevent you receiving updates for a package,
266 ;; via an entry (PACKAGE . NON-EXISTING). Which could be an issue
267 ;; if PACKAGE has a known vulnerability that is fixed in newer versions.
268 :risky t
269 :group 'package
270 :version "24.4")
272 (defconst package-archive-version 1
273 "Version number of the package archive understood by this file.
274 Lower version numbers than this will probably be understood as well.")
276 ;; We don't prime the cache since it tends to get out of date.
277 (defvar package-archive-contents nil
278 "Cache of the contents of the Emacs Lisp Package Archive.
279 This is an alist mapping package names (symbols) to
280 non-empty lists of `package-desc' structures.")
281 (put 'package-archive-contents 'risky-local-variable t)
283 (defcustom package-user-dir (locate-user-emacs-file "elpa")
284 "Directory containing the user's Emacs Lisp packages.
285 The directory name should be absolute.
286 Apart from this directory, Emacs also looks for system-wide
287 packages in `package-directory-list'."
288 :type 'directory
289 :risky t
290 :group 'package
291 :version "24.1")
293 (defcustom package-directory-list
294 ;; Defaults are subdirs named "elpa" in the site-lisp dirs.
295 (let (result)
296 (dolist (f load-path)
297 (and (stringp f)
298 (equal (file-name-nondirectory f) "site-lisp")
299 (push (expand-file-name "elpa" f) result)))
300 (nreverse result))
301 "List of additional directories containing Emacs Lisp packages.
302 Each directory name should be absolute.
304 These directories contain packages intended for system-wide; in
305 contrast, `package-user-dir' contains packages for personal use."
306 :type '(repeat directory)
307 :risky t
308 :group 'package
309 :version "24.1")
311 (defvar epg-gpg-program)
313 (defcustom package-check-signature
314 (if (progn (require 'epg-config) (executable-find epg-gpg-program))
315 'allow-unsigned)
316 "Non-nil means to check package signatures when installing.
317 The value `allow-unsigned' means to still install a package even if
318 it is unsigned.
320 This also applies to the \"archive-contents\" file that lists the
321 contents of the archive."
322 :type '(choice (const nil :tag "Never")
323 (const allow-unsigned :tag "Allow unsigned")
324 (const t :tag "Check always"))
325 :risky t
326 :group 'package
327 :version "24.4")
329 (defcustom package-unsigned-archives nil
330 "List of archives where we do not check for package signatures."
331 :type '(repeat (string :tag "Archive name"))
332 :risky t
333 :group 'package
334 :version "24.4")
336 (defcustom package-selected-packages nil
337 "Store here packages installed explicitely by user.
338 This variable will be feeded automatically by emacs,
339 when installing a new package.
340 This variable will be used by `package-autoremove' to decide
341 which packages are no more needed.
342 You can use it to (re)install packages on other machines
343 by running `package-user-selected-packages-install'."
344 :group 'package
345 :type '(repeat symbol))
347 (defvar package--default-summary "No description available.")
349 (cl-defstruct (package-desc
350 ;; Rename the default constructor from `make-package-desc'.
351 (:constructor package-desc-create)
352 ;; Has the same interface as the old `define-package',
353 ;; which is still used in the "foo-pkg.el" files. Extra
354 ;; options can be supported by adding additional keys.
355 (:constructor
356 package-desc-from-define
357 (name-string version-string &optional summary requirements
358 &rest rest-plist
359 &aux
360 (name (intern name-string))
361 (version (version-to-list version-string))
362 (reqs (mapcar #'(lambda (elt)
363 (list (car elt)
364 (version-to-list (cadr elt))))
365 (if (eq 'quote (car requirements))
366 (nth 1 requirements)
367 requirements)))
368 (kind (plist-get rest-plist :kind))
369 (archive (plist-get rest-plist :archive))
370 (extras (let (alist)
371 (while rest-plist
372 (unless (memq (car rest-plist) '(:kind :archive))
373 (let ((value (cadr rest-plist)))
374 (when value
375 (push (cons (car rest-plist)
376 (if (eq (car-safe value) 'quote)
377 (cadr value)
378 value))
379 alist))))
380 (setq rest-plist (cddr rest-plist)))
381 alist)))))
382 "Structure containing information about an individual package.
383 Slots:
385 `name' Name of the package, as a symbol.
387 `version' Version of the package, as a version list.
389 `summary' Short description of the package, typically taken from
390 the first line of the file.
392 `reqs' Requirements of the package. A list of (PACKAGE
393 VERSION-LIST) naming the dependent package and the minimum
394 required version.
396 `kind' The distribution format of the package. Currently, it is
397 either `single' or `tar'.
399 `archive' The name of the archive (as a string) whence this
400 package came.
402 `dir' The directory where the package is installed (if installed),
403 `builtin' if it is built-in, or nil otherwise.
405 `extras' Optional alist of additional keyword-value pairs.
407 `signed' Flag to indicate that the package is signed by provider."
408 name
409 version
410 (summary package--default-summary)
411 reqs
412 kind
413 archive
415 extras
416 signed)
418 ;; Pseudo fields.
419 (defun package-desc-full-name (pkg-desc)
420 (format "%s-%s"
421 (package-desc-name pkg-desc)
422 (package-version-join (package-desc-version pkg-desc))))
424 (defun package-desc-suffix (pkg-desc)
425 (pcase (package-desc-kind pkg-desc)
426 (`single ".el")
427 (`tar ".tar")
428 (`dir "")
429 (kind (error "Unknown package kind: %s" kind))))
431 (defun package-desc--keywords (pkg-desc)
432 (let ((keywords (cdr (assoc :keywords (package-desc-extras pkg-desc)))))
433 (if (eq (car-safe keywords) 'quote)
434 (nth 1 keywords)
435 keywords)))
437 ;; Package descriptor format used in finder-inf.el and package--builtins.
438 (cl-defstruct (package--bi-desc
439 (:constructor package-make-builtin (version summary))
440 (:type vector))
441 version
442 reqs
443 summary)
445 (defvar package--builtins nil
446 "Alist of built-in packages.
447 The actual value is initialized by loading the library
448 `finder-inf'; this is not done until it is needed, e.g. by the
449 function `package-built-in-p'.
451 Each element has the form (PKG . PACKAGE-BI-DESC), where PKG is a package
452 name (a symbol) and DESC is a `package--bi-desc' structure.")
453 (put 'package--builtins 'risky-local-variable t)
455 (defvar package-alist nil
456 "Alist of all packages available for activation.
457 Each element has the form (PKG . DESCS), where PKG is a package
458 name (a symbol) and DESCS is a non-empty list of `package-desc' structure,
459 sorted by decreasing versions.
461 This variable is set automatically by `package-load-descriptor',
462 called via `package-initialize'. To change which packages are
463 loaded and/or activated, customize `package-load-list'.")
464 (put 'package-alist 'risky-local-variable t)
466 (defvar package-activated-list nil
467 ;; FIXME: This should implicitly include all builtin packages.
468 "List of the names of currently activated packages.")
469 (put 'package-activated-list 'risky-local-variable t)
471 (defun package-version-join (vlist)
472 "Return the version string corresponding to the list VLIST.
473 This is, approximately, the inverse of `version-to-list'.
474 \(Actually, it returns only one of the possible inverses, since
475 `version-to-list' is a many-to-one operation.)"
476 (if (null vlist)
478 (let ((str-list (list "." (int-to-string (car vlist)))))
479 (dolist (num (cdr vlist))
480 (cond
481 ((>= num 0)
482 (push (int-to-string num) str-list)
483 (push "." str-list))
484 ((< num -4)
485 (error "Invalid version list `%s'" vlist))
487 ;; pre, or beta, or alpha
488 (cond ((equal "." (car str-list))
489 (pop str-list))
490 ((not (string-match "[0-9]+" (car str-list)))
491 (error "Invalid version list `%s'" vlist)))
492 (push (cond ((= num -1) "pre")
493 ((= num -2) "beta")
494 ((= num -3) "alpha")
495 ((= num -4) "snapshot"))
496 str-list))))
497 (if (equal "." (car str-list))
498 (pop str-list))
499 (apply 'concat (nreverse str-list)))))
501 (defun package-load-descriptor (pkg-dir)
502 "Load the description file in directory PKG-DIR."
503 (let ((pkg-file (expand-file-name (package--description-file pkg-dir)
504 pkg-dir))
505 (signed-file (concat pkg-dir ".signed")))
506 (when (file-exists-p pkg-file)
507 (with-temp-buffer
508 (insert-file-contents pkg-file)
509 (goto-char (point-min))
510 (let ((pkg-desc (package-process-define-package
511 (read (current-buffer)) pkg-file)))
512 (setf (package-desc-dir pkg-desc) pkg-dir)
513 (if (file-exists-p signed-file)
514 (setf (package-desc-signed pkg-desc) t))
515 pkg-desc)))))
517 (defun package-load-all-descriptors ()
518 "Load descriptors for installed Emacs Lisp packages.
519 This looks for package subdirectories in `package-user-dir' and
520 `package-directory-list'. The variable `package-load-list'
521 controls which package subdirectories may be loaded.
523 In each valid package subdirectory, this function loads the
524 description file containing a call to `define-package', which
525 updates `package-alist'."
526 (dolist (dir (cons package-user-dir package-directory-list))
527 (when (file-directory-p dir)
528 (dolist (subdir (directory-files dir))
529 (let ((pkg-dir (expand-file-name subdir dir)))
530 (when (file-directory-p pkg-dir)
531 (package-load-descriptor pkg-dir)))))))
533 (defun package-disabled-p (pkg-name version)
534 "Return whether PKG-NAME at VERSION can be activated.
535 The decision is made according to `package-load-list'.
536 Return nil if the package can be activated.
537 Return t if the package is completely disabled.
538 Return the max version (as a string) if the package is held at a lower version."
539 (let ((force (assq pkg-name package-load-list)))
540 (cond ((null force) (not (memq 'all package-load-list)))
541 ((null (setq force (cadr force))) t) ; disabled
542 ((eq force t) nil)
543 ((stringp force) ; held
544 (unless (version-list-= version (version-to-list force))
545 force))
546 (t (error "Invalid element in `package-load-list'")))))
548 (defun package-activate-1 (pkg-desc &optional reload)
549 "Activate package given by PKG-DESC, even if it was already active.
550 If RELOAD is non-nil, also `load' any files inside the package which
551 correspond to previously loaded files (those returned by
552 `package--list-loaded-files')."
553 (let* ((name (package-desc-name pkg-desc))
554 (pkg-dir (package-desc-dir pkg-desc))
555 (pkg-dir-dir (file-name-as-directory pkg-dir)))
556 (unless pkg-dir
557 (error "Internal error: unable to find directory for `%s'"
558 (package-desc-full-name pkg-desc)))
559 ;; Add to load path, add autoloads, and activate the package.
560 (let* ((old-lp load-path)
561 (autoloads-file (expand-file-name
562 (format "%s-autoloads" name) pkg-dir))
563 (loaded-files-list (and reload (package--list-loaded-files pkg-dir))))
564 (with-demoted-errors "Error in package-activate-1: %s"
565 (load autoloads-file nil t))
566 (when (and (eq old-lp load-path)
567 (not (or (member pkg-dir load-path)
568 (member pkg-dir-dir load-path))))
569 ;; Old packages don't add themselves to the `load-path', so we have to
570 ;; do it ourselves.
571 (push pkg-dir load-path))
572 ;; Call `load' on all files in `pkg-dir' already present in
573 ;; `load-history'. This is done so that macros in these files are updated
574 ;; to their new definitions. If another package is being installed which
575 ;; depends on this new definition, not doing this update would cause
576 ;; compilation errors and break the installation.
577 (with-demoted-errors "Error in package-activate-1: %s"
578 (mapc (lambda (feature) (load feature nil t))
579 ;; Skip autoloads file since we already evaluated it above.
580 (remove (file-truename autoloads-file) loaded-files-list))))
581 ;; Add info node.
582 (when (file-exists-p (expand-file-name "dir" pkg-dir))
583 ;; FIXME: not the friendliest, but simple.
584 (require 'info)
585 (info-initialize)
586 (push pkg-dir Info-directory-list))
587 (push name package-activated-list)
588 ;; Don't return nil.
591 (declare-function find-library-name "find-func" (library))
592 (defun package--list-loaded-files (dir)
593 "Recursively list all files in DIR which correspond to loaded features.
594 Returns the `file-name-sans-extension' of each file, relative to
595 DIR, sorted by most recently loaded last."
596 (let* ((history (delq nil
597 (mapcar (lambda (x)
598 (let ((f (car x)))
599 (and f (file-name-sans-extension f))))
600 load-history)))
601 (dir (file-truename dir))
602 ;; List all files that have already been loaded.
603 (list-of-conflicts
604 (delq
606 (mapcar
607 (lambda (x) (let* ((file (file-relative-name x dir))
608 ;; Previously loaded file, if any.
609 (previous
610 (ignore-errors
611 (file-name-sans-extension
612 (file-truename (find-library-name file)))))
613 (pos (when previous (member previous history))))
614 ;; Return (RELATIVE-FILENAME . HISTORY-POSITION)
615 (when pos
616 (cons (file-name-sans-extension file) (length pos)))))
617 (directory-files-recursively dir "\\`[^\\.].*\\.el\\'")))))
618 ;; Turn the list of (FILENAME . POS) back into a list of features. Files in
619 ;; subdirectories are returned relative to DIR (so not actually features).
620 (let ((default-directory (file-name-as-directory dir)))
621 (mapcar (lambda (x) (file-truename (car x)))
622 (sort list-of-conflicts
623 ;; Sort the files by ascending HISTORY-POSITION.
624 (lambda (x y) (< (cdr x) (cdr y))))))))
626 (defun package-built-in-p (package &optional min-version)
627 "Return true if PACKAGE is built-in to Emacs.
628 Optional arg MIN-VERSION, if non-nil, should be a version list
629 specifying the minimum acceptable version."
630 (if (package-desc-p package) ;; was built-in and then was converted
631 (eq 'builtin (package-desc-dir package))
632 (let ((bi (assq package package--builtin-versions)))
633 (cond
634 (bi (version-list-<= min-version (cdr bi)))
635 ((remove 0 min-version) nil)
637 (require 'finder-inf nil t) ; For `package--builtins'.
638 (assq package package--builtins))))))
640 (defun package--from-builtin (bi-desc)
641 (package-desc-create :name (pop bi-desc)
642 :version (package--bi-desc-version bi-desc)
643 :summary (package--bi-desc-summary bi-desc)
644 :dir 'builtin))
646 ;; This function goes ahead and activates a newer version of a package
647 ;; if an older one was already activated. This is not ideal; we'd at
648 ;; least need to check to see if the package has actually been loaded,
649 ;; and not merely activated.
650 (defun package-activate (package &optional force)
651 "Activate package PACKAGE.
652 If FORCE is true, (re-)activate it if it's already activated."
653 (let ((pkg-descs (cdr (assq package package-alist))))
654 ;; Check if PACKAGE is available in `package-alist'.
655 (while
656 (when pkg-descs
657 (let ((available-version (package-desc-version (car pkg-descs))))
658 (or (package-disabled-p package available-version)
659 ;; Prefer a builtin package.
660 (package-built-in-p package available-version))))
661 (setq pkg-descs (cdr pkg-descs)))
662 (cond
663 ;; If no such package is found, maybe it's built-in.
664 ((null pkg-descs)
665 (package-built-in-p package))
666 ;; If the package is already activated, just return t.
667 ((and (memq package package-activated-list) (not force))
669 ;; Otherwise, proceed with activation.
671 (let* ((pkg-vec (car pkg-descs))
672 (fail (catch 'dep-failure
673 ;; Activate its dependencies recursively.
674 (dolist (req (package-desc-reqs pkg-vec))
675 (unless (package-activate (car req))
676 (throw 'dep-failure req))))))
677 (if fail
678 (warn "Unable to activate package `%s'.
679 Required package `%s-%s' is unavailable"
680 package (car fail) (package-version-join (cadr fail)))
681 ;; If all goes well, activate the package itself.
682 (package-activate-1 pkg-vec force)))))))
684 (defun define-package (_name-string _version-string
685 &optional _docstring _requirements
686 &rest _extra-properties)
687 "Define a new package.
688 NAME-STRING is the name of the package, as a string.
689 VERSION-STRING is the version of the package, as a string.
690 DOCSTRING is a short description of the package, a string.
691 REQUIREMENTS is a list of dependencies on other packages.
692 Each requirement is of the form (OTHER-PACKAGE OTHER-VERSION),
693 where OTHER-VERSION is a string.
695 EXTRA-PROPERTIES is currently unused."
696 ;; FIXME: Placeholder! Should we keep it?
697 (error "Don't call me!"))
699 (defun package-process-define-package (exp origin)
700 (unless (eq (car-safe exp) 'define-package)
701 (error "Can't find define-package in %s" origin))
702 (let* ((new-pkg-desc (apply #'package-desc-from-define (cdr exp)))
703 (name (package-desc-name new-pkg-desc))
704 (version (package-desc-version new-pkg-desc))
705 (old-pkgs (assq name package-alist)))
706 (if (null old-pkgs)
707 ;; If there's no old package, just add this to `package-alist'.
708 (push (list name new-pkg-desc) package-alist)
709 ;; If there is, insert the new package at the right place in the list.
710 (while
711 (if (and (cdr old-pkgs)
712 (version-list-< version
713 (package-desc-version (cadr old-pkgs))))
714 (setq old-pkgs (cdr old-pkgs))
715 (push new-pkg-desc (cdr old-pkgs))
716 nil)))
717 new-pkg-desc))
719 ;; From Emacs 22, but changed so it adds to load-path.
720 (defun package-autoload-ensure-default-file (file)
721 "Make sure that the autoload file FILE exists and if not create it."
722 (unless (file-exists-p file)
723 (write-region
724 (concat ";;; " (file-name-nondirectory file)
725 " --- automatically extracted autoloads\n"
726 ";;\n"
727 ";;; Code:\n"
728 "(add-to-list 'load-path (or (file-name-directory #$) (car load-path)))\n"
729 "\f\n;; Local Variables:\n"
730 ";; version-control: never\n"
731 ";; no-byte-compile: t\n"
732 ";; no-update-autoloads: t\n"
733 ";; End:\n"
734 ";;; " (file-name-nondirectory file)
735 " ends here\n")
736 nil file nil 'silent))
737 file)
739 (defvar generated-autoload-file)
740 (defvar version-control)
742 (defun package-generate-autoloads (name pkg-dir)
743 (let* ((auto-name (format "%s-autoloads.el" name))
744 ;;(ignore-name (concat name "-pkg.el"))
745 (generated-autoload-file (expand-file-name auto-name pkg-dir))
746 (backup-inhibited t)
747 (version-control 'never))
748 (package-autoload-ensure-default-file generated-autoload-file)
749 (update-directory-autoloads pkg-dir)
750 (let ((buf (find-buffer-visiting generated-autoload-file)))
751 (when buf (kill-buffer buf)))
752 auto-name))
754 (defvar tar-parse-info)
755 (declare-function tar-untar-buffer "tar-mode" ())
756 (declare-function tar-header-name "tar-mode" (tar-header) t)
757 (declare-function tar-header-link-type "tar-mode" (tar-header) t)
759 (defun package-untar-buffer (dir)
760 "Untar the current buffer.
761 This uses `tar-untar-buffer' from Tar mode. All files should
762 untar into a directory named DIR; otherwise, signal an error."
763 (require 'tar-mode)
764 (tar-mode)
765 ;; Make sure everything extracts into DIR.
766 (let ((regexp (concat "\\`" (regexp-quote (expand-file-name dir)) "/"))
767 (case-fold-search (memq system-type '(windows-nt ms-dos cygwin))))
768 (dolist (tar-data tar-parse-info)
769 (let ((name (expand-file-name (tar-header-name tar-data))))
770 (or (string-match regexp name)
771 ;; Tarballs created by some utilities don't list
772 ;; directories with a trailing slash (Bug#13136).
773 (and (string-equal dir name)
774 (eq (tar-header-link-type tar-data) 5))
775 (error "Package does not untar cleanly into directory %s/" dir)))))
776 (tar-untar-buffer))
778 (defun package-generate-description-file (pkg-desc pkg-file)
779 "Create the foo-pkg.el file for single-file packages."
780 (let* ((name (package-desc-name pkg-desc)))
781 (let ((print-level nil)
782 (print-quoted t)
783 (print-length nil))
784 (write-region
785 (concat
786 ";;; -*- no-byte-compile: t -*-\n"
787 (prin1-to-string
788 (nconc
789 (list 'define-package
790 (symbol-name name)
791 (package-version-join (package-desc-version pkg-desc))
792 (package-desc-summary pkg-desc)
793 (let ((requires (package-desc-reqs pkg-desc)))
794 (list 'quote
795 ;; Turn version lists into string form.
796 (mapcar
797 (lambda (elt)
798 (list (car elt)
799 (package-version-join (cadr elt))))
800 requires))))
801 (package--alist-to-plist-args
802 (package-desc-extras pkg-desc))))
803 "\n")
804 nil pkg-file nil 'silent))))
806 (defun package--alist-to-plist-args (alist)
807 (mapcar 'macroexp-quote
808 (apply #'nconc
809 (mapcar (lambda (pair) (list (car pair) (cdr pair))) alist))))
810 (defun package-unpack (pkg-desc)
811 "Install the contents of the current buffer as a package."
812 (let* ((name (package-desc-name pkg-desc))
813 (dirname (package-desc-full-name pkg-desc))
814 (pkg-dir (expand-file-name dirname package-user-dir)))
815 (pcase (package-desc-kind pkg-desc)
816 (`dir
817 (make-directory pkg-dir t)
818 (let ((file-list
819 (directory-files
820 default-directory 'full "\\`[^.].*\\.el\\'" 'nosort)))
821 (dolist (source-file file-list)
822 (let ((target-el-file
823 (expand-file-name (file-name-nondirectory source-file) pkg-dir)))
824 (copy-file source-file target-el-file t)))
825 ;; Now that the files have been installed, this package is
826 ;; indistinguishable from a `tar' or a `single'. Let's make
827 ;; things simple by ensuring we're one of them.
828 (setf (package-desc-kind pkg-desc)
829 (if (> (length file-list) 1) 'tar 'single))))
830 (`tar
831 (make-directory package-user-dir t)
832 ;; FIXME: should we delete PKG-DIR if it exists?
833 (let* ((default-directory (file-name-as-directory package-user-dir)))
834 (package-untar-buffer dirname)))
835 (`single
836 (let ((el-file (expand-file-name (format "%s.el" name) pkg-dir)))
837 (make-directory pkg-dir t)
838 (package--write-file-no-coding el-file)))
839 (kind (error "Unknown package kind: %S" kind)))
840 (package--make-autoloads-and-stuff pkg-desc pkg-dir)
841 ;; Update package-alist.
842 (let ((new-desc (package-load-descriptor pkg-dir)))
843 ;; FIXME: Check that `new-desc' matches `desc'!
844 ;; FIXME: Compilation should be done as a separate, optional, step.
845 ;; E.g. for multi-package installs, we should first install all packages
846 ;; and then compile them.
847 (package--compile new-desc))
848 ;; Try to activate it.
849 (package-activate name 'force)
850 pkg-dir))
852 (defun package--make-autoloads-and-stuff (pkg-desc pkg-dir)
853 "Generate autoloads, description file, etc.. for PKG-DESC installed at PKG-DIR."
854 (package-generate-autoloads (package-desc-name pkg-desc) pkg-dir)
855 (let ((desc-file (expand-file-name (package--description-file pkg-dir)
856 pkg-dir)))
857 (unless (file-exists-p desc-file)
858 (package-generate-description-file pkg-desc desc-file)))
859 ;; FIXME: Create foo.info and dir file from foo.texi?
862 (defun package--compile (pkg-desc)
863 "Byte-compile installed package PKG-DESC."
864 (package-activate-1 pkg-desc)
865 (byte-recompile-directory (package-desc-dir pkg-desc) 0 t))
867 (defun package--write-file-no-coding (file-name)
868 (let ((buffer-file-coding-system 'no-conversion))
869 (write-region (point-min) (point-max) file-name nil 'silent)))
871 (defmacro package--with-work-buffer (location file &rest body)
872 "Run BODY in a buffer containing the contents of FILE at LOCATION.
873 LOCATION is the base location of a package archive, and should be
874 one of the URLs (or file names) specified in `package-archives'.
875 FILE is the name of a file relative to that base location.
877 This macro retrieves FILE from LOCATION into a temporary buffer,
878 and evaluates BODY while that buffer is current. This work
879 buffer is killed afterwards. Return the last value in BODY."
880 (declare (indent 2) (debug t))
881 `(with-temp-buffer
882 (if (string-match-p "\\`https?:" ,location)
883 (url-insert-file-contents (concat ,location ,file))
884 (unless (file-name-absolute-p ,location)
885 (error "Archive location %s is not an absolute file name"
886 ,location))
887 (insert-file-contents (expand-file-name ,file ,location)))
888 ,@body))
890 (defun package--archive-file-exists-p (location file)
891 (let ((http (string-match "\\`https?:" location)))
892 (if http
893 (progn
894 (require 'url-http)
895 (url-http-file-exists-p (concat location file)))
896 (file-exists-p (expand-file-name file location)))))
898 (declare-function epg-make-context "epg"
899 (&optional protocol armor textmode include-certs
900 cipher-algorithm
901 digest-algorithm
902 compress-algorithm))
903 (declare-function epg-verify-string "epg" (context signature
904 &optional signed-text))
905 (declare-function epg-context-result-for "epg" (context name))
906 (declare-function epg-signature-status "epg" (signature))
907 (declare-function epg-signature-to-string "epg" (signature))
909 (defun package--display-verify-error (context sig-file)
910 (unless (equal (epg-context-error-output context) "")
911 (with-output-to-temp-buffer "*Error*"
912 (with-current-buffer standard-output
913 (if (epg-context-result-for context 'verify)
914 (insert (format "Failed to verify signature %s:\n" sig-file)
915 (mapconcat #'epg-signature-to-string
916 (epg-context-result-for context 'verify)
917 "\n"))
918 (insert (format "Error while verifying signature %s:\n" sig-file)))
919 (insert "\nCommand output:\n" (epg-context-error-output context))))))
921 (defun package--check-signature (location file)
922 "Check signature of the current buffer.
923 GnuPG keyring is located under \"gnupg\" in `package-user-dir'."
924 (let* ((context (epg-make-context 'OpenPGP))
925 (homedir (expand-file-name "gnupg" package-user-dir))
926 (sig-file (concat file ".sig"))
927 (sig-content (package--with-work-buffer location sig-file
928 (buffer-string))))
929 (setf (epg-context-home-directory context) homedir)
930 (condition-case error
931 (epg-verify-string context sig-content (buffer-string))
932 (error
933 (package--display-verify-error context sig-file)
934 (signal (car error) (cdr error))))
935 (let (good-signatures had-fatal-error)
936 ;; The .sig file may contain multiple signatures. Success if one
937 ;; of the signatures is good.
938 (dolist (sig (epg-context-result-for context 'verify))
939 (if (eq (epg-signature-status sig) 'good)
940 (push sig good-signatures)
941 ;; If package-check-signature is allow-unsigned, don't
942 ;; signal error when we can't verify signature because of
943 ;; missing public key. Other errors are still treated as
944 ;; fatal (bug#17625).
945 (unless (and (eq package-check-signature 'allow-unsigned)
946 (eq (epg-signature-status sig) 'no-pubkey))
947 (setq had-fatal-error t))))
948 (when (and (null good-signatures) had-fatal-error)
949 (package--display-verify-error context sig-file)
950 (error "Failed to verify signature %s" sig-file))
951 good-signatures)))
953 (defun package-install-from-archive (pkg-desc)
954 "Download and install a tar package."
955 ;; This won't happen, unless the archive is doing something wrong.
956 (when (eq (package-desc-kind pkg-desc) 'dir)
957 (error "Can't install directory package from archive"))
958 (let* ((location (package-archive-base pkg-desc))
959 (file (concat (package-desc-full-name pkg-desc)
960 (package-desc-suffix pkg-desc)))
961 (sig-file (concat file ".sig"))
962 good-signatures pkg-descs)
963 (package--with-work-buffer location file
964 (if (and package-check-signature
965 (not (member (package-desc-archive pkg-desc)
966 package-unsigned-archives)))
967 (if (package--archive-file-exists-p location sig-file)
968 (setq good-signatures (package--check-signature location file))
969 (unless (eq package-check-signature 'allow-unsigned)
970 (error "Unsigned package: `%s'"
971 (package-desc-name pkg-desc)))))
972 (package-unpack pkg-desc))
973 ;; Here the package has been installed successfully, mark it as
974 ;; signed if appropriate.
975 (when good-signatures
976 ;; Write out good signatures into NAME-VERSION.signed file.
977 (write-region (mapconcat #'epg-signature-to-string good-signatures "\n")
979 (expand-file-name
980 (concat (package-desc-full-name pkg-desc)
981 ".signed")
982 package-user-dir)
983 nil 'silent)
984 ;; Update the old pkg-desc which will be shown on the description buffer.
985 (setf (package-desc-signed pkg-desc) t)
986 ;; Update the new (activated) pkg-desc as well.
987 (setq pkg-descs (cdr (assq (package-desc-name pkg-desc) package-alist)))
988 (if pkg-descs
989 (setf (package-desc-signed (car pkg-descs)) t)))))
991 (defvar package--initialized nil)
993 (defun package-installed-p (package &optional min-version)
994 "Return true if PACKAGE, of MIN-VERSION or newer, is installed.
995 MIN-VERSION should be a version list."
996 (unless package--initialized (error "package.el is not yet initialized!"))
998 (let ((pkg-descs (cdr (assq package package-alist))))
999 (and pkg-descs
1000 (version-list-<= min-version
1001 (package-desc-version (car pkg-descs)))))
1002 ;; Also check built-in packages.
1003 (package-built-in-p package min-version)))
1005 (defun package-compute-transaction (packages requirements &optional seen)
1006 "Return a list of packages to be installed, including PACKAGES.
1007 PACKAGES should be a list of `package-desc'.
1009 REQUIREMENTS should be a list of additional requirements; each
1010 element in this list should have the form (PACKAGE VERSION-LIST),
1011 where PACKAGE is a package name and VERSION-LIST is the required
1012 version of that package.
1014 This function recursively computes the requirements of the
1015 packages in REQUIREMENTS, and returns a list of all the packages
1016 that must be installed. Packages that are already installed are
1017 not included in this list.
1019 SEEN is used internally to detect infinite recursion."
1020 ;; FIXME: We really should use backtracking to explore the whole
1021 ;; search space (e.g. if foo require bar-1.3, and bar-1.4 requires toto-1.1
1022 ;; whereas bar-1.3 requires toto-1.0 and the user has put a hold on toto-1.0:
1023 ;; the current code might fail to see that it could install foo by using the
1024 ;; older bar-1.3).
1025 (dolist (elt requirements)
1026 (let* ((next-pkg (car elt))
1027 (next-version (cadr elt))
1028 (already ()))
1029 (dolist (pkg packages)
1030 (if (eq next-pkg (package-desc-name pkg))
1031 (setq already pkg)))
1032 (when already
1033 (if (version-list-<= next-version (package-desc-version already))
1034 ;; `next-pkg' is already in `packages', but its position there
1035 ;; means it might be installed too late: remove it from there, so
1036 ;; we re-add it (along with its dependencies) at an earlier place
1037 ;; below (bug#16994).
1038 (if (memq already seen) ;Avoid inf-loop on dependency cycles.
1039 (message "Dependency cycle going through %S"
1040 (package-desc-full-name already))
1041 (setq packages (delq already packages))
1042 (setq already nil))
1043 (error "Need package `%s-%s', but only %s is being installed"
1044 next-pkg (package-version-join next-version)
1045 (package-version-join (package-desc-version already)))))
1046 (cond
1047 (already nil)
1048 ((package-installed-p next-pkg next-version) nil)
1051 ;; A package is required, but not installed. It might also be
1052 ;; blocked via `package-load-list'.
1053 (let ((pkg-descs (cdr (assq next-pkg package-archive-contents)))
1054 (found nil)
1055 (problem nil))
1056 (while (and pkg-descs (not found))
1057 (let* ((pkg-desc (pop pkg-descs))
1058 (version (package-desc-version pkg-desc))
1059 (disabled (package-disabled-p next-pkg version)))
1060 (cond
1061 ((version-list-< version next-version)
1062 (error
1063 "Need package `%s-%s', but only %s is available"
1064 next-pkg (package-version-join next-version)
1065 (package-version-join version)))
1066 (disabled
1067 (unless problem
1068 (setq problem
1069 (if (stringp disabled)
1070 (format "Package `%s' held at version %s, \
1071 but version %s required"
1072 next-pkg disabled
1073 (package-version-join next-version))
1074 (format "Required package '%s' is disabled"
1075 next-pkg)))))
1076 (t (setq found pkg-desc)))))
1077 (unless found
1078 (if problem
1079 (error "%s" problem)
1080 (error "Package `%s-%s' is unavailable"
1081 next-pkg (package-version-join next-version))))
1082 (setq packages
1083 (package-compute-transaction (cons found packages)
1084 (package-desc-reqs found)
1085 (cons found seen))))))))
1086 packages)
1088 (defun package-read-from-string (str)
1089 "Read a Lisp expression from STR.
1090 Signal an error if the entire string was not used."
1091 (let* ((read-data (read-from-string str))
1092 (more-left
1093 (condition-case nil
1094 ;; The call to `ignore' suppresses a compiler warning.
1095 (progn (ignore (read-from-string
1096 (substring str (cdr read-data))))
1098 (end-of-file nil))))
1099 (if more-left
1100 (error "Can't read whole string")
1101 (car read-data))))
1103 (defun package--read-archive-file (file)
1104 "Re-read archive file FILE, if it exists.
1105 Will return the data from the file, or nil if the file does not exist.
1106 Will throw an error if the archive version is too new."
1107 (let ((filename (expand-file-name file package-user-dir)))
1108 (when (file-exists-p filename)
1109 (with-temp-buffer
1110 (insert-file-contents-literally filename)
1111 (let ((contents (read (current-buffer))))
1112 (if (> (car contents) package-archive-version)
1113 (error "Package archive version %d is higher than %d"
1114 (car contents) package-archive-version))
1115 (cdr contents))))))
1117 (defun package-read-all-archive-contents ()
1118 "Re-read `archive-contents', if it exists.
1119 If successful, set `package-archive-contents'."
1120 (setq package-archive-contents nil)
1121 (dolist (archive package-archives)
1122 (package-read-archive-contents (car archive))))
1124 (defun package-read-archive-contents (archive)
1125 "Re-read archive contents for ARCHIVE.
1126 If successful, set the variable `package-archive-contents'.
1127 If the archive version is too new, signal an error."
1128 ;; Version 1 of 'archive-contents' is identical to our internal
1129 ;; representation.
1130 (let* ((contents-file (format "archives/%s/archive-contents" archive))
1131 (contents (package--read-archive-file contents-file)))
1132 (when contents
1133 (dolist (package contents)
1134 (package--add-to-archive-contents package archive)))))
1136 ;; Package descriptor objects used inside the "archive-contents" file.
1137 ;; Changing this defstruct implies changing the format of the
1138 ;; "archive-contents" files.
1139 (cl-defstruct (package--ac-desc
1140 (:constructor package-make-ac-desc (version reqs summary kind extras))
1141 (:copier nil)
1142 (:type vector))
1143 version reqs summary kind extras)
1145 (defun package--add-to-archive-contents (package archive)
1146 "Add the PACKAGE from the given ARCHIVE if necessary.
1147 PACKAGE should have the form (NAME . PACKAGE--AC-DESC).
1148 Also, add the originating archive to the `package-desc' structure."
1149 (let* ((name (car package))
1150 (version (package--ac-desc-version (cdr package)))
1151 (pkg-desc
1152 (package-desc-create
1153 :name name
1154 :version version
1155 :reqs (package--ac-desc-reqs (cdr package))
1156 :summary (package--ac-desc-summary (cdr package))
1157 :kind (package--ac-desc-kind (cdr package))
1158 :archive archive
1159 :extras (and (> (length (cdr package)) 4)
1160 ;; Older archive-contents files have only 4
1161 ;; elements here.
1162 (package--ac-desc-extras (cdr package)))))
1163 (pinned-to-archive (assoc name package-pinned-packages)))
1164 ;; Skip entirely if pinned to another archive.
1165 (when (not (and pinned-to-archive
1166 (not (equal (cdr pinned-to-archive) archive))))
1167 (setq package-archive-contents
1168 (package--append-to-alist pkg-desc package-archive-contents)))))
1170 (defun package--append-to-alist (pkg-desc alist)
1171 "Append an entry for PKG-DESC to the start of ALIST and return it.
1172 This entry takes the form (`package-desc-name' PKG-DESC).
1174 If ALIST already has an entry with this name, destructively add
1175 PKG-DESC to the cdr of this entry instead, sorted by version
1176 number."
1177 (let* ((name (package-desc-name pkg-desc))
1178 (priority-version (package-desc-priority-version pkg-desc))
1179 (existing-packages (assq name alist)))
1180 (if (not existing-packages)
1181 (cons (list name pkg-desc)
1182 alist)
1183 (while (if (and (cdr existing-packages)
1184 (version-list-< priority-version
1185 (package-desc-priority-version
1186 (cadr existing-packages))))
1187 (setq existing-packages (cdr existing-packages))
1188 (push pkg-desc (cdr existing-packages))
1189 nil))
1190 alist)))
1192 (defun package-download-transaction (packages)
1193 "Download and install all the packages in PACKAGES.
1194 PACKAGES should be a list of package-desc.
1195 This function assumes that all package requirements in
1196 PACKAGES are satisfied, i.e. that PACKAGES is computed
1197 using `package-compute-transaction'."
1198 (mapc #'package-install-from-archive packages))
1200 ;;;###autoload
1201 (defun package-install (pkg &optional mark-selected)
1202 "Install the package PKG.
1203 PKG can be a package-desc or the package name of one the available packages
1204 in an archive in `package-archives'. Interactively, prompt for its name.
1206 If called interactively or if MARK-SELECTED is non-nil, add PKG
1207 to `package-selected-packages'."
1208 (interactive
1209 (progn
1210 ;; Initialize the package system to get the list of package
1211 ;; symbols for completion.
1212 (unless package--initialized
1213 (package-initialize t))
1214 (unless package-archive-contents
1215 (package-refresh-contents))
1216 (list (intern (completing-read
1217 "Install package: "
1218 (delq nil
1219 (mapcar (lambda (elt)
1220 (unless (package-installed-p (car elt))
1221 (symbol-name (car elt))))
1222 package-archive-contents))
1223 nil t))
1224 t)))
1225 (when (and mark-selected (not (memq pkg package-selected-packages)))
1226 (customize-save-variable 'package-selected-packages
1227 (cons pkg package-selected-packages)))
1228 (package-download-transaction
1229 (if (package-desc-p pkg)
1230 (package-compute-transaction (list pkg)
1231 (package-desc-reqs pkg))
1232 (package-compute-transaction ()
1233 (list (list pkg))))))
1235 ;;;###autoload
1236 (defun package-reinstall (pkg)
1237 "Reinstall package PKG."
1238 (interactive (list (intern (completing-read
1239 "Reinstall package: "
1240 (mapcar 'symbol-name
1241 (mapcar 'car package-alist))))))
1242 (package-delete (cadr (assq pkg package-alist)) t)
1243 (package-install pkg))
1245 (defun package-strip-rcs-id (str)
1246 "Strip RCS version ID from the version string STR.
1247 If the result looks like a dotted numeric version, return it.
1248 Otherwise return nil."
1249 (when str
1250 (when (string-match "\\`[ \t]*[$]Revision:[ \t]+" str)
1251 (setq str (substring str (match-end 0))))
1252 (condition-case nil
1253 (if (version-to-list str)
1254 str)
1255 (error nil))))
1257 (declare-function lm-homepage "lisp-mnt" (&optional file))
1259 (defun package--prepare-dependencies (deps)
1260 "Turn DEPS into an acceptable list of dependencies.
1262 Any parts missing a version string get a default version string
1263 of \"0\" (meaning any version) and an appropriate level of lists
1264 is wrapped around any parts requiring it."
1265 (cond
1266 ((not (listp deps))
1267 (error "Invalid requirement specifier: %S" deps))
1268 (t (mapcar (lambda (dep)
1269 (cond
1270 ((symbolp dep) `(,dep "0"))
1271 ((stringp dep)
1272 (error "Invalid requirement specifier: %S" dep))
1273 ((and (listp dep) (null (cdr dep)))
1274 (list (car dep) "0"))
1275 (t dep)))
1276 deps))))
1278 (defun package-buffer-info ()
1279 "Return a `package-desc' describing the package in the current buffer.
1281 If the buffer does not contain a conforming package, signal an
1282 error. If there is a package, narrow the buffer to the file's
1283 boundaries."
1284 (goto-char (point-min))
1285 (unless (re-search-forward "^;;; \\([^ ]*\\)\\.el ---[ \t]*\\(.*?\\)[ \t]*\\(-\\*-.*-\\*-[ \t]*\\)?$" nil t)
1286 (error "Package lacks a file header"))
1287 (let ((file-name (match-string-no-properties 1))
1288 (desc (match-string-no-properties 2))
1289 (start (line-beginning-position)))
1290 (unless (search-forward (concat ";;; " file-name ".el ends here"))
1291 (error "Package lacks a terminating comment"))
1292 ;; Try to include a trailing newline.
1293 (forward-line)
1294 (narrow-to-region start (point))
1295 (require 'lisp-mnt)
1296 ;; Use some headers we've invented to drive the process.
1297 (let* ((requires-str (lm-header "package-requires"))
1298 ;; Prefer Package-Version; if defined, the package author
1299 ;; probably wants us to use it. Otherwise try Version.
1300 (pkg-version
1301 (or (package-strip-rcs-id (lm-header "package-version"))
1302 (package-strip-rcs-id (lm-header "version"))))
1303 (homepage (lm-homepage)))
1304 (unless pkg-version
1305 (error
1306 "Package lacks a \"Version\" or \"Package-Version\" header"))
1307 (package-desc-from-define
1308 file-name pkg-version desc
1309 (if requires-str
1310 (package--prepare-dependencies
1311 (package-read-from-string requires-str)))
1312 :kind 'single
1313 :url homepage))))
1315 (declare-function tar-get-file-descriptor "tar-mode" (file))
1316 (declare-function tar--extract "tar-mode" (descriptor))
1318 (defun package-tar-file-info ()
1319 "Find package information for a tar file.
1320 The return result is a `package-desc'."
1321 (cl-assert (derived-mode-p 'tar-mode))
1322 (let* ((dir-name (file-name-directory
1323 (tar-header-name (car tar-parse-info))))
1324 (desc-file (package--description-file dir-name))
1325 (tar-desc (tar-get-file-descriptor (concat dir-name desc-file))))
1326 (unless tar-desc
1327 (error "No package descriptor file found"))
1328 (with-current-buffer (tar--extract tar-desc)
1329 (unwind-protect
1330 (package--read-pkg-desc 'tar)
1331 (kill-buffer (current-buffer))))))
1333 (defun package-dir-info ()
1334 "Find package information for a directory.
1335 The return result is a `package-desc'."
1336 (cl-assert (derived-mode-p 'dired-mode))
1337 (let* ((desc-file (package--description-file default-directory)))
1338 (if (file-readable-p desc-file)
1339 (with-temp-buffer
1340 (insert-file-contents desc-file)
1341 (package--read-pkg-desc 'dir))
1342 (let ((files (directory-files default-directory t "\\.el\\'" t))
1343 info)
1344 (while files
1345 (with-temp-buffer
1346 (insert-file-contents (pop files))
1347 ;; When we find the file with the data,
1348 (when (setq info (ignore-errors (package-buffer-info)))
1349 ;; stop looping,
1350 (setq files nil)
1351 ;; set the 'dir kind,
1352 (setf (package-desc-kind info) 'dir))))
1353 ;; and return the info.
1354 info))))
1356 (defun package--read-pkg-desc (kind)
1357 "Read a `define-package' form in current buffer.
1358 Return the pkg-desc, with desc-kind set to KIND."
1359 (goto-char (point-min))
1360 (unwind-protect
1361 (let* ((pkg-def-parsed (read (current-buffer)))
1362 (pkg-desc
1363 (if (not (eq (car pkg-def-parsed) 'define-package))
1364 (error "Can't find define-package in %s"
1365 (tar-header-name tar-desc))
1366 (apply #'package-desc-from-define
1367 (append (cdr pkg-def-parsed))))))
1368 (setf (package-desc-kind pkg-desc) kind)
1369 pkg-desc)))
1372 ;;;###autoload
1373 (defun package-install-from-buffer ()
1374 "Install a package from the current buffer.
1375 The current buffer is assumed to be a single .el or .tar file or
1376 a directory. These must follow the packaging guidelines (see
1377 info node `(elisp)Packaging').
1379 Specially, if current buffer is a directory, the -pkg.el
1380 description file is not mandatory, in which case the information
1381 is derived from the main .el file in the directory.
1383 Downloads and installs required packages as needed."
1384 (interactive)
1385 (let* ((pkg-desc
1386 (cond
1387 ((derived-mode-p 'dired-mode)
1388 ;; This is the only way a package-desc object with a `dir'
1389 ;; desc-kind can be created. Such packages can't be
1390 ;; uploaded or installed from archives, they can only be
1391 ;; installed from local buffers or directories.
1392 (package-dir-info))
1393 ((derived-mode-p 'tar-mode)
1394 (package-tar-file-info))
1396 (package-buffer-info))))
1397 (name (package-desc-name pkg-desc)))
1398 ;; Download and install the dependencies.
1399 (let* ((requires (package-desc-reqs pkg-desc))
1400 (transaction (package-compute-transaction nil requires)))
1401 (package-download-transaction transaction))
1402 ;; Install the package itself.
1403 (package-unpack pkg-desc)
1404 (unless (memq name package-selected-packages)
1405 (push name package-selected-packages)
1406 (customize-save-variable 'package-selected-packages
1407 package-selected-packages))
1408 pkg-desc))
1410 ;;;###autoload
1411 (defun package-install-file (file)
1412 "Install a package from a file.
1413 The file can either be a tar file or an Emacs Lisp file."
1414 (interactive "fPackage file name: ")
1415 (with-temp-buffer
1416 (if (file-directory-p file)
1417 (progn
1418 (setq default-directory file)
1419 (dired-mode))
1420 (insert-file-contents-literally file)
1421 (when (string-match "\\.tar\\'" file) (tar-mode)))
1422 (package-install-from-buffer)))
1424 (defun package--get-deps (pkg &optional only)
1425 (let* ((pkg-desc (cadr (assq pkg package-alist)))
1426 (direct-deps (cl-loop for p in (package-desc-reqs pkg-desc)
1427 for name = (car p)
1428 when (assq name package-alist)
1429 collect name))
1430 (indirect-deps (unless (eq only 'direct)
1431 (cl-loop for p in direct-deps
1432 for dep = (cadr (assq p package-alist))
1433 when (and dep (assq p package-alist))
1434 append (mapcar 'car
1435 (package-desc-reqs
1436 dep))))))
1437 (cl-case only
1438 (direct direct-deps)
1439 (separate (list direct-deps indirect-deps))
1440 (indirect indirect-deps)
1441 (t (append direct-deps indirect-deps)))))
1443 ;;;###autoload
1444 (defun package-user-selected-packages-install ()
1445 "Ensure packages in `package-selected-packages' are installed.
1446 If some packages are not installed propose to install them."
1447 (interactive)
1448 (cl-loop for p in package-selected-packages
1449 unless (package-installed-p p)
1450 collect p into lst
1451 finally
1452 (if lst
1453 (when (y-or-n-p
1454 (format "%s packages will be installed:\n%s, proceed?"
1455 (length lst)
1456 (mapconcat 'symbol-name lst ", ")))
1457 (mapc 'package-install lst))
1458 (message "All your packages are already installed"))))
1460 (defun package-used-elsewhere-p (pkg-desc &optional pkg-list)
1461 "Check in PKG-LIST if PKG-DESC is used elsewhere as dependency.
1463 When not specified, PKG-LIST default to `package-alist'
1464 with PKG-DESC entry removed.
1465 Returns the first package found in PKG-LIST where PKG is used as dependency."
1466 (unless (string= (package-desc-status pkg-desc) "obsolete")
1467 (let ((pkg (package-desc-name pkg-desc)))
1468 (cl-loop with alist = (or pkg-list
1469 (remove (assq pkg package-alist)
1470 package-alist))
1471 for p in alist thereis
1472 (and (memq pkg (mapcar 'car (package-desc-reqs (cadr p))))
1473 (car p))))))
1475 (defun package-delete (pkg-desc &optional force)
1476 "Delete package PKG-DESC.
1478 Argument PKG-DESC is a full description of package as vector.
1479 When package is used elsewhere as dependency of another package,
1480 refuse deleting it and return an error.
1481 If FORCE is non--nil package will be deleted even if it is used
1482 elsewhere."
1483 (let ((dir (package-desc-dir pkg-desc))
1484 (name (package-desc-name pkg-desc))
1485 pkg-used-elsewhere-by)
1486 (cond ((not (string-prefix-p (file-name-as-directory
1487 (expand-file-name package-user-dir))
1488 (expand-file-name dir)))
1489 ;; Don't delete "system" packages.
1490 (error "Package `%s' is a system package, not deleting"
1491 (package-desc-full-name pkg-desc)))
1492 ((and (null force)
1493 (setq pkg-used-elsewhere-by
1494 (package-used-elsewhere-p pkg-desc)))
1495 ;; Don't delete packages used as dependency elsewhere.
1496 (error "Package `%s' is used by `%s' as dependency, not deleting"
1497 (package-desc-full-name pkg-desc)
1498 pkg-used-elsewhere-by))
1500 (delete-directory dir t t)
1501 ;; Remove NAME-VERSION.signed file.
1502 (let ((signed-file (concat dir ".signed")))
1503 (if (file-exists-p signed-file)
1504 (delete-file signed-file)))
1505 ;; Update package-alist.
1506 (let ((pkgs (assq name package-alist)))
1507 (delete pkg-desc pkgs)
1508 (unless (cdr pkgs)
1509 (setq package-alist (delq pkgs package-alist))))
1510 (message "Package `%s' deleted." (package-desc-full-name pkg-desc))))))
1512 ;;;###autoload
1513 (defun package-autoremove ()
1514 "Remove packages that are no more needed.
1516 Packages that are no more needed by other packages in
1517 `package-selected-packages' and their dependencies
1518 will be deleted."
1519 (interactive)
1520 (let* (old-direct
1521 (needed (cl-loop for p in package-selected-packages
1522 if (assq p package-alist)
1523 append (package--get-deps p) into lst
1524 else do (push p old-direct)
1525 finally return lst)))
1526 (cl-loop for p in (mapcar 'car package-alist)
1527 unless (or (memq p needed)
1528 (memq p package-selected-packages))
1529 collect p into lst
1530 finally (if lst
1531 (when (y-or-n-p (format "%s packages will be deleted:\n%s, proceed? "
1532 (length lst)
1533 (mapconcat 'symbol-name lst ", ")))
1534 (mapc (lambda (p)
1535 (package-delete (cadr (assq p package-alist)) t))
1536 lst))
1537 (message "Nothing to autoremove")))))
1539 (defun package-archive-base (desc)
1540 "Return the archive containing the package NAME."
1541 (cdr (assoc (package-desc-archive desc) package-archives)))
1543 (defun package-archive-priority (archive)
1544 "Return the priority of ARCHIVE.
1546 The archive priorities are specified in
1547 `package-archive-priorities'. If not given there, the priority
1548 defaults to 0."
1549 (or (cdr (assoc archive package-archive-priorities))
1552 (defun package-desc-priority-version (pkg-desc)
1553 "Return the version PKG-DESC with the archive priority prepended.
1555 This allows for easy comparison of package versions from
1556 different archives if archive priorities are meant to be taken in
1557 consideration."
1558 (cons (package-archive-priority
1559 (package-desc-archive pkg-desc))
1560 (package-desc-version pkg-desc)))
1562 (defun package--download-one-archive (archive file)
1563 "Retrieve an archive file FILE from ARCHIVE, and cache it.
1564 ARCHIVE should be a cons cell of the form (NAME . LOCATION),
1565 similar to an entry in `package-alist'. Save the cached copy to
1566 \"archives/NAME/archive-contents\" in `package-user-dir'."
1567 (let ((dir (expand-file-name (format "archives/%s" (car archive))
1568 package-user-dir))
1569 (sig-file (concat file ".sig"))
1570 good-signatures)
1571 (package--with-work-buffer (cdr archive) file
1572 ;; Check signature of archive-contents, if desired.
1573 (if (and package-check-signature
1574 (not (member archive package-unsigned-archives)))
1575 (if (package--archive-file-exists-p (cdr archive) sig-file)
1576 (setq good-signatures (package--check-signature (cdr archive)
1577 file))
1578 (unless (eq package-check-signature 'allow-unsigned)
1579 (error "Unsigned archive `%s'"
1580 (car archive)))))
1581 ;; Read the retrieved buffer to make sure it is valid (e.g. it
1582 ;; may fetch a URL redirect page).
1583 (when (listp (read (current-buffer)))
1584 (make-directory dir t)
1585 (write-region nil nil (expand-file-name file dir) nil 'silent)))
1586 (when good-signatures
1587 ;; Write out good signatures into archive-contents.signed file.
1588 (write-region (mapconcat #'epg-signature-to-string good-signatures "\n")
1590 (expand-file-name (concat file ".signed") dir)
1591 nil 'silent))))
1593 (declare-function epg-check-configuration "epg-config"
1594 (config &optional minimum-version))
1595 (declare-function epg-configuration "epg-config" ())
1596 (declare-function epg-import-keys-from-file "epg" (context keys))
1598 ;;;###autoload
1599 (defun package-import-keyring (&optional file)
1600 "Import keys from FILE."
1601 (interactive "fFile: ")
1602 (setq file (expand-file-name file))
1603 (let ((context (epg-make-context 'OpenPGP))
1604 (homedir (expand-file-name "gnupg" package-user-dir)))
1605 (with-file-modes 448
1606 (make-directory homedir t))
1607 (setf (epg-context-home-directory context) homedir)
1608 (message "Importing %s..." (file-name-nondirectory file))
1609 (epg-import-keys-from-file context file)
1610 (message "Importing %s...done" (file-name-nondirectory file))))
1612 ;;;###autoload
1613 (defun package-refresh-contents ()
1614 "Download the ELPA archive description if needed.
1615 This informs Emacs about the latest versions of all packages, and
1616 makes them available for download."
1617 (interactive)
1618 ;; FIXME: Do it asynchronously.
1619 (unless (file-exists-p package-user-dir)
1620 (make-directory package-user-dir t))
1621 (let ((default-keyring (expand-file-name "package-keyring.gpg"
1622 data-directory)))
1623 (when (and package-check-signature (file-exists-p default-keyring))
1624 (condition-case-unless-debug error
1625 (progn
1626 (epg-check-configuration (epg-configuration))
1627 (package-import-keyring default-keyring))
1628 (error (message "Cannot import default keyring: %S" (cdr error))))))
1629 (dolist (archive package-archives)
1630 (condition-case-unless-debug nil
1631 (package--download-one-archive archive "archive-contents")
1632 (error (message "Failed to download `%s' archive."
1633 (car archive)))))
1634 (package-read-all-archive-contents))
1636 ;;;###autoload
1637 (defun package-initialize (&optional no-activate)
1638 "Load Emacs Lisp packages, and activate them.
1639 The variable `package-load-list' controls which packages to load.
1640 If optional arg NO-ACTIVATE is non-nil, don't activate packages."
1641 (interactive)
1642 (setq package-alist nil)
1643 (package-load-all-descriptors)
1644 (package-read-all-archive-contents)
1645 (unless no-activate
1646 (dolist (elt package-alist)
1647 (package-activate (car elt))))
1648 (setq package--initialized t))
1651 ;;;; Package description buffer.
1653 ;;;###autoload
1654 (defun describe-package (package)
1655 "Display the full documentation of PACKAGE (a symbol)."
1656 (interactive
1657 (let* ((guess (function-called-at-point)))
1658 (require 'finder-inf nil t)
1659 ;; Load the package list if necessary (but don't activate them).
1660 (unless package--initialized
1661 (package-initialize t))
1662 (let ((packages (append (mapcar 'car package-alist)
1663 (mapcar 'car package-archive-contents)
1664 (mapcar 'car package--builtins))))
1665 (unless (memq guess packages)
1666 (setq guess nil))
1667 (setq packages (mapcar 'symbol-name packages))
1668 (let ((val
1669 (completing-read (if guess
1670 (format "Describe package (default %s): "
1671 guess)
1672 "Describe package: ")
1673 packages nil t nil nil guess)))
1674 (list (intern val))))))
1675 (if (not (or (package-desc-p package) (and package (symbolp package))))
1676 (message "No package specified")
1677 (help-setup-xref (list #'describe-package package)
1678 (called-interactively-p 'interactive))
1679 (with-help-window (help-buffer)
1680 (with-current-buffer standard-output
1681 (describe-package-1 package)))))
1683 (defun describe-package-1 (pkg)
1684 (require 'lisp-mnt)
1685 (let* ((desc (or
1686 (if (package-desc-p pkg) pkg)
1687 (cadr (assq pkg package-alist))
1688 (let ((built-in (assq pkg package--builtins)))
1689 (if built-in
1690 (package--from-builtin built-in)
1691 (cadr (assq pkg package-archive-contents))))))
1692 (name (if desc (package-desc-name desc) pkg))
1693 (pkg-dir (if desc (package-desc-dir desc)))
1694 (reqs (if desc (package-desc-reqs desc)))
1695 (version (if desc (package-desc-version desc)))
1696 (archive (if desc (package-desc-archive desc)))
1697 (extras (and desc (package-desc-extras desc)))
1698 (homepage (cdr (assoc :url extras)))
1699 (keywords (if desc (package-desc--keywords desc)))
1700 (built-in (eq pkg-dir 'builtin))
1701 (installable (and archive (not built-in)))
1702 (status (if desc (package-desc-status desc) "orphan"))
1703 (signed (if desc (package-desc-signed desc))))
1704 (prin1 name)
1705 (princ " is ")
1706 (princ (if (memq (aref status 0) '(?a ?e ?i ?o ?u)) "an " "a "))
1707 (princ status)
1708 (princ " package.\n\n")
1710 (insert " " (propertize "Status" 'font-lock-face 'bold) ": ")
1711 (cond (built-in
1712 (insert (propertize (capitalize status)
1713 'font-lock-face 'font-lock-builtin-face)
1714 "."))
1715 (pkg-dir
1716 (insert (propertize (if (equal status "unsigned")
1717 "Installed"
1718 (capitalize status)) ;FIXME: Why comment-face?
1719 'font-lock-face 'font-lock-comment-face))
1720 (insert " in `")
1721 ;; Todo: Add button for uninstalling.
1722 (help-insert-xref-button (abbreviate-file-name
1723 (file-name-as-directory pkg-dir))
1724 'help-package-def pkg-dir)
1725 (if (and (package-built-in-p name)
1726 (not (package-built-in-p name version)))
1727 (insert "',\n shadowing a "
1728 (propertize "built-in package"
1729 'font-lock-face 'font-lock-builtin-face))
1730 (insert "'"))
1731 (if signed
1732 (insert ".")
1733 (insert " (unsigned).")))
1734 (installable
1735 (insert (capitalize status))
1736 (insert " from " (format "%s" archive))
1737 (insert " -- ")
1738 (package-make-button
1739 "Install"
1740 'action 'package-install-button-action
1741 'package-desc desc))
1742 (t (insert (capitalize status) ".")))
1743 (insert "\n")
1744 (insert " " (propertize "Archive" 'font-lock-face 'bold)
1745 ": " (or archive "n/a") "\n")
1746 (and version
1747 (insert " "
1748 (propertize "Version" 'font-lock-face 'bold) ": "
1749 (package-version-join version) "\n"))
1751 (setq reqs (if desc (package-desc-reqs desc)))
1752 (when reqs
1753 (insert " " (propertize "Requires" 'font-lock-face 'bold) ": ")
1754 (let ((first t)
1755 name vers text)
1756 (dolist (req reqs)
1757 (setq name (car req)
1758 vers (cadr req)
1759 text (format "%s-%s" (symbol-name name)
1760 (package-version-join vers)))
1761 (cond (first (setq first nil))
1762 ((>= (+ 2 (current-column) (length text))
1763 (window-width))
1764 (insert ",\n "))
1765 (t (insert ", ")))
1766 (help-insert-xref-button text 'help-package name))
1767 (insert "\n")))
1768 (insert " " (propertize "Summary" 'font-lock-face 'bold)
1769 ": " (if desc (package-desc-summary desc)) "\n")
1770 (when homepage
1771 (insert " " (propertize "Homepage" 'font-lock-face 'bold) ": ")
1772 (help-insert-xref-button homepage 'help-url homepage)
1773 (insert "\n"))
1774 (when keywords
1775 (insert " " (propertize "Keywords" 'font-lock-face 'bold) ": ")
1776 (dolist (k keywords)
1777 (package-make-button
1779 'package-keyword k
1780 'action 'package-keyword-button-action)
1781 (insert " "))
1782 (insert "\n"))
1783 (let* ((all-pkgs (append (cdr (assq name package-alist))
1784 (cdr (assq name package-archive-contents))
1785 (let ((bi (assq name package--builtins)))
1786 (if bi (list (package--from-builtin bi))))))
1787 (other-pkgs (delete desc all-pkgs)))
1788 (when other-pkgs
1789 (insert " " (propertize "Other versions" 'font-lock-face 'bold) ": "
1790 (mapconcat
1791 (lambda (opkg)
1792 (let* ((ov (package-desc-version opkg))
1793 (dir (package-desc-dir opkg))
1794 (from (or (package-desc-archive opkg)
1795 (if (stringp dir) "installed" dir))))
1796 (if (not ov) (format "%s" from)
1797 (format "%s (%s)"
1798 (make-text-button (package-version-join ov) nil
1799 'face 'link
1800 'follow-link t
1801 'action
1802 (lambda (_button)
1803 (describe-package opkg)))
1804 from))))
1805 other-pkgs ", ")
1806 ".\n")))
1808 (insert "\n")
1810 (if built-in
1811 ;; For built-in packages, insert the commentary.
1812 (let ((fn (locate-file (format "%s.el" name) load-path
1813 load-file-rep-suffixes))
1814 (opoint (point)))
1815 (insert (or (lm-commentary fn) ""))
1816 (save-excursion
1817 (goto-char opoint)
1818 (when (re-search-forward "^;;; Commentary:\n" nil t)
1819 (replace-match ""))
1820 (while (re-search-forward "^\\(;+ ?\\)" nil t)
1821 (replace-match ""))))
1822 (let ((readme (expand-file-name (format "%s-readme.txt" name)
1823 package-user-dir))
1824 readme-string)
1825 ;; For elpa packages, try downloading the commentary. If that
1826 ;; fails, try an existing readme file in `package-user-dir'.
1827 (cond ((condition-case nil
1828 (save-excursion
1829 (package--with-work-buffer
1830 (package-archive-base desc)
1831 (format "%s-readme.txt" name)
1832 (save-excursion
1833 (goto-char (point-max))
1834 (unless (bolp)
1835 (insert ?\n)))
1836 (write-region nil nil
1837 (expand-file-name readme package-user-dir)
1838 nil 'silent)
1839 (setq readme-string (buffer-string))
1841 (error nil))
1842 (insert readme-string))
1843 ((file-readable-p readme)
1844 (insert-file-contents readme)
1845 (goto-char (point-max))))))))
1847 (defun package-install-button-action (button)
1848 (let ((pkg-desc (button-get button 'package-desc)))
1849 (when (y-or-n-p (format "Install package `%s'? "
1850 (package-desc-full-name pkg-desc)))
1851 (package-install pkg-desc 1)
1852 (revert-buffer nil t)
1853 (goto-char (point-min)))))
1855 (defun package-keyword-button-action (button)
1856 (let ((pkg-keyword (button-get button 'package-keyword)))
1857 (package-show-package-list t (list pkg-keyword))))
1859 (defun package-make-button (text &rest props)
1860 (let ((button-text (if (display-graphic-p) text (concat "[" text "]")))
1861 (button-face (if (display-graphic-p)
1862 '(:box (:line-width 2 :color "dark grey")
1863 :background "light grey"
1864 :foreground "black")
1865 'link)))
1866 (apply 'insert-text-button button-text 'face button-face 'follow-link t
1867 props)))
1870 ;;;; Package menu mode.
1872 (defvar package-menu-mode-map
1873 (let ((map (make-sparse-keymap))
1874 (menu-map (make-sparse-keymap "Package")))
1875 (set-keymap-parent map tabulated-list-mode-map)
1876 (define-key map "\C-m" 'package-menu-describe-package)
1877 (define-key map "u" 'package-menu-mark-unmark)
1878 (define-key map "\177" 'package-menu-backup-unmark)
1879 (define-key map "d" 'package-menu-mark-delete)
1880 (define-key map "i" 'package-menu-mark-install)
1881 (define-key map "U" 'package-menu-mark-upgrades)
1882 (define-key map "r" 'package-menu-refresh)
1883 (define-key map "f" 'package-menu-filter)
1884 (define-key map "~" 'package-menu-mark-obsolete-for-deletion)
1885 (define-key map "x" 'package-menu-execute)
1886 (define-key map "h" 'package-menu-quick-help)
1887 (define-key map "?" 'package-menu-describe-package)
1888 (define-key map [menu-bar package-menu] (cons "Package" menu-map))
1889 (define-key menu-map [mq]
1890 '(menu-item "Quit" quit-window
1891 :help "Quit package selection"))
1892 (define-key menu-map [s1] '("--"))
1893 (define-key menu-map [mn]
1894 '(menu-item "Next" next-line
1895 :help "Next Line"))
1896 (define-key menu-map [mp]
1897 '(menu-item "Previous" previous-line
1898 :help "Previous Line"))
1899 (define-key menu-map [s2] '("--"))
1900 (define-key menu-map [mu]
1901 '(menu-item "Unmark" package-menu-mark-unmark
1902 :help "Clear any marks on a package and move to the next line"))
1903 (define-key menu-map [munm]
1904 '(menu-item "Unmark Backwards" package-menu-backup-unmark
1905 :help "Back up one line and clear any marks on that package"))
1906 (define-key menu-map [md]
1907 '(menu-item "Mark for Deletion" package-menu-mark-delete
1908 :help "Mark a package for deletion and move to the next line"))
1909 (define-key menu-map [mi]
1910 '(menu-item "Mark for Install" package-menu-mark-install
1911 :help "Mark a package for installation and move to the next line"))
1912 (define-key menu-map [mupgrades]
1913 '(menu-item "Mark Upgradable Packages" package-menu-mark-upgrades
1914 :help "Mark packages that have a newer version for upgrading"))
1915 (define-key menu-map [s3] '("--"))
1916 (define-key menu-map [mf]
1917 '(menu-item "Filter Package List..." package-menu-filter
1918 :help "Filter package selection (q to go back)"))
1919 (define-key menu-map [mg]
1920 '(menu-item "Update Package List" revert-buffer
1921 :help "Update the list of packages"))
1922 (define-key menu-map [mr]
1923 '(menu-item "Refresh Package List" package-menu-refresh
1924 :help "Download the ELPA archive"))
1925 (define-key menu-map [s4] '("--"))
1926 (define-key menu-map [mt]
1927 '(menu-item "Mark Obsolete Packages" package-menu-mark-obsolete-for-deletion
1928 :help "Mark all obsolete packages for deletion"))
1929 (define-key menu-map [mx]
1930 '(menu-item "Execute Actions" package-menu-execute
1931 :help "Perform all the marked actions"))
1932 (define-key menu-map [s5] '("--"))
1933 (define-key menu-map [mh]
1934 '(menu-item "Help" package-menu-quick-help
1935 :help "Show short key binding help for package-menu-mode"))
1936 (define-key menu-map [mc]
1937 '(menu-item "Describe Package" package-menu-describe-package
1938 :help "Display information about this package"))
1939 map)
1940 "Local keymap for `package-menu-mode' buffers.")
1942 (defvar package-menu--new-package-list nil
1943 "List of newly-available packages since `list-packages' was last called.")
1945 (define-derived-mode package-menu-mode tabulated-list-mode "Package Menu"
1946 "Major mode for browsing a list of packages.
1947 Letters do not insert themselves; instead, they are commands.
1948 \\<package-menu-mode-map>
1949 \\{package-menu-mode-map}"
1950 (setq tabulated-list-format
1951 `[("Package" 18 package-menu--name-predicate)
1952 ("Version" 13 nil)
1953 ("Status" 10 package-menu--status-predicate)
1954 ,@(if (cdr package-archives)
1955 '(("Archive" 10 package-menu--archive-predicate)))
1956 ("Description" 0 nil)])
1957 (setq tabulated-list-padding 2)
1958 (setq tabulated-list-sort-key (cons "Status" nil))
1959 (add-hook 'tabulated-list-revert-hook 'package-menu--refresh nil t)
1960 (tabulated-list-init-header))
1962 (defmacro package--push (pkg-desc status listname)
1963 "Convenience macro for `package-menu--generate'.
1964 If the alist stored in the symbol LISTNAME lacks an entry for a
1965 package PKG-DESC, add one. The alist is keyed with PKG-DESC."
1966 `(unless (assoc ,pkg-desc ,listname)
1967 ;; FIXME: Should we move status into pkg-desc?
1968 (push (cons ,pkg-desc ,status) ,listname)))
1970 (defvar package-list-unversioned nil
1971 "If non-nil include packages that don't have a version in `list-package'.")
1973 (defvar package-list-unsigned nil
1974 "If non-nil, mention in the list which packages were installed w/o signature.")
1976 (defun package-desc-status (pkg-desc)
1977 (let* ((name (package-desc-name pkg-desc))
1978 (dir (package-desc-dir pkg-desc))
1979 (lle (assq name package-load-list))
1980 (held (cadr lle))
1981 (version (package-desc-version pkg-desc))
1982 (signed (package-desc-signed pkg-desc)))
1983 (cond
1984 ((eq dir 'builtin) "built-in")
1985 ((and lle (null held)) "disabled")
1986 ((stringp held)
1987 (let ((hv (if (stringp held) (version-to-list held))))
1988 (cond
1989 ((version-list-= version hv) "held")
1990 ((version-list-< version hv) "obsolete")
1991 (t "disabled"))))
1992 ((package-built-in-p name version) "obsolete")
1993 (dir ;One of the installed packages.
1994 (cond
1995 ((not (file-exists-p (package-desc-dir pkg-desc))) "deleted")
1996 ((eq pkg-desc (cadr (assq name package-alist)))
1997 (if (or (not package-list-unsigned) signed) "installed" "unsigned"))
1998 (t "obsolete")))
2000 (let* ((ins (cadr (assq name package-alist)))
2001 (ins-v (if ins (package-desc-version ins))))
2002 (cond
2003 ((or (null ins) (version-list-< ins-v version))
2004 (if (memq name package-menu--new-package-list)
2005 "new" "available"))
2006 ((version-list-< version ins-v) "obsolete")
2007 ((version-list-= version ins-v)
2008 (if (or (not package-list-unsigned) signed)
2009 "installed" "unsigned"))))))))
2011 (defun package-menu--refresh (&optional packages keywords)
2012 "Re-populate the `tabulated-list-entries'.
2013 PACKAGES should be nil or t, which means to display all known packages.
2014 KEYWORDS should be nil or a list of keywords."
2015 ;; Construct list of (PKG-DESC . STATUS).
2016 (unless packages (setq packages t))
2017 (let (info-list name)
2018 ;; Installed packages:
2019 (dolist (elt package-alist)
2020 (setq name (car elt))
2021 (when (or (eq packages t) (memq name packages))
2022 (dolist (pkg (cdr elt))
2023 (when (package--has-keyword-p pkg keywords)
2024 (package--push pkg (package-desc-status pkg) info-list)))))
2026 ;; Built-in packages:
2027 (dolist (elt package--builtins)
2028 (setq name (car elt))
2029 (when (and (not (eq name 'emacs)) ; Hide the `emacs' package.
2030 (package--has-keyword-p (package--from-builtin elt) keywords)
2031 (or package-list-unversioned
2032 (package--bi-desc-version (cdr elt)))
2033 (or (eq packages t) (memq name packages)))
2034 (package--push (package--from-builtin elt) "built-in" info-list)))
2036 ;; Available and disabled packages:
2037 (dolist (elt package-archive-contents)
2038 (setq name (car elt))
2039 (when (or (eq packages t) (memq name packages))
2040 (dolist (pkg (cdr elt))
2041 ;; Hide obsolete packages.
2042 (when (and (not (package-installed-p (package-desc-name pkg)
2043 (package-desc-version pkg)))
2044 (package--has-keyword-p pkg keywords))
2045 (package--push pkg (package-desc-status pkg) info-list)))))
2047 ;; Print the result.
2048 (setq tabulated-list-entries
2049 (mapcar #'package-menu--print-info info-list))))
2051 (defun package-all-keywords ()
2052 "Collect all package keywords"
2053 (let (keywords)
2054 (package--mapc (lambda (desc)
2055 (let* ((desc-keywords (and desc (package-desc--keywords desc))))
2056 (setq keywords (append keywords desc-keywords)))))
2057 keywords))
2059 (defun package--mapc (function &optional packages)
2060 "Call FUNCTION for all known PACKAGES.
2061 PACKAGES can be nil or t, which means to display all known
2062 packages, or a list of packages.
2064 Built-in packages are converted with `package--from-builtin'."
2065 (unless packages (setq packages t))
2066 (let (name)
2067 ;; Installed packages:
2068 (dolist (elt package-alist)
2069 (setq name (car elt))
2070 (when (or (eq packages t) (memq name packages))
2071 (mapc function (cdr elt))))
2073 ;; Built-in packages:
2074 (dolist (elt package--builtins)
2075 (setq name (car elt))
2076 (when (and (not (eq name 'emacs)) ; Hide the `emacs' package.
2077 (or package-list-unversioned
2078 (package--bi-desc-version (cdr elt)))
2079 (or (eq packages t) (memq name packages)))
2080 (funcall function (package--from-builtin elt))))
2082 ;; Available and disabled packages:
2083 (dolist (elt package-archive-contents)
2084 (setq name (car elt))
2085 (when (or (eq packages t) (memq name packages))
2086 (dolist (pkg (cdr elt))
2087 ;; Hide obsolete packages.
2088 (unless (package-installed-p (package-desc-name pkg)
2089 (package-desc-version pkg))
2090 (funcall function pkg)))))))
2092 (defun package--has-keyword-p (desc &optional keywords)
2093 "Test if package DESC has any of the given KEYWORDS.
2094 When none are given, the package matches."
2095 (if keywords
2096 (let* ((desc-keywords (and desc (package-desc--keywords desc)))
2097 found)
2098 (dolist (k keywords)
2099 (when (and (not found)
2100 (member k desc-keywords))
2101 (setq found t)))
2102 found)
2105 (defun package-menu--generate (remember-pos packages &optional keywords)
2106 "Populate the Package Menu.
2107 If REMEMBER-POS is non-nil, keep point on the same entry.
2108 PACKAGES should be t, which means to display all known packages,
2109 or a list of package names (symbols) to display.
2111 With KEYWORDS given, only packages with those keywords are
2112 shown."
2113 (package-menu--refresh packages keywords)
2114 (setf (car (aref tabulated-list-format 0))
2115 (if keywords
2116 (let ((filters (mapconcat 'identity keywords ",")))
2117 (concat "Package[" filters "]"))
2118 "Package"))
2119 (if keywords
2120 (define-key package-menu-mode-map "q" 'package-show-package-list)
2121 (define-key package-menu-mode-map "q" 'quit-window))
2122 (tabulated-list-init-header)
2123 (tabulated-list-print remember-pos))
2125 (defun package-menu--print-info (pkg)
2126 "Return a package entry suitable for `tabulated-list-entries'.
2127 PKG has the form (PKG-DESC . STATUS).
2128 Return (PKG-DESC [NAME VERSION STATUS DOC])."
2129 (let* ((pkg-desc (car pkg))
2130 (status (cdr pkg))
2131 (face (pcase status
2132 (`"built-in" 'font-lock-builtin-face)
2133 (`"available" 'default)
2134 (`"new" 'bold)
2135 (`"held" 'font-lock-constant-face)
2136 (`"disabled" 'font-lock-warning-face)
2137 (`"installed" 'font-lock-comment-face)
2138 (`"unsigned" 'font-lock-warning-face)
2139 (_ 'font-lock-warning-face)))) ; obsolete.
2140 (list pkg-desc
2141 `[,(list (symbol-name (package-desc-name pkg-desc))
2142 'face 'link
2143 'follow-link t
2144 'package-desc pkg-desc
2145 'action 'package-menu-describe-package)
2146 ,(propertize (package-version-join
2147 (package-desc-version pkg-desc))
2148 'font-lock-face face)
2149 ,(propertize status 'font-lock-face face)
2150 ,@(if (cdr package-archives)
2151 (list (propertize (or (package-desc-archive pkg-desc) "")
2152 'font-lock-face face)))
2153 ,(propertize (package-desc-summary pkg-desc)
2154 'font-lock-face face)])))
2156 (defun package-menu-refresh ()
2157 "Download the Emacs Lisp package archive.
2158 This fetches the contents of each archive specified in
2159 `package-archives', and then refreshes the package menu."
2160 (interactive)
2161 (unless (derived-mode-p 'package-menu-mode)
2162 (user-error "The current buffer is not a Package Menu"))
2163 (package-refresh-contents)
2164 (package-menu--generate t t))
2166 (defun package-menu-describe-package (&optional button)
2167 "Describe the current package.
2168 If optional arg BUTTON is non-nil, describe its associated package."
2169 (interactive)
2170 (let ((pkg-desc (if button (button-get button 'package-desc)
2171 (tabulated-list-get-id))))
2172 (if pkg-desc
2173 (describe-package pkg-desc)
2174 (user-error "No package here"))))
2176 ;; fixme numeric argument
2177 (defun package-menu-mark-delete (&optional _num)
2178 "Mark a package for deletion and move to the next line."
2179 (interactive "p")
2180 (if (member (package-menu-get-status) '("installed" "obsolete" "unsigned"))
2181 (tabulated-list-put-tag "D" t)
2182 (forward-line)))
2184 (defun package-menu-mark-install (&optional _num)
2185 "Mark a package for installation and move to the next line."
2186 (interactive "p")
2187 (if (member (package-menu-get-status) '("available" "new"))
2188 (tabulated-list-put-tag "I" t)
2189 (forward-line)))
2191 (defun package-menu-mark-unmark (&optional _num)
2192 "Clear any marks on a package and move to the next line."
2193 (interactive "p")
2194 (tabulated-list-put-tag " " t))
2196 (defun package-menu-backup-unmark ()
2197 "Back up one line and clear any marks on that package."
2198 (interactive)
2199 (forward-line -1)
2200 (tabulated-list-put-tag " "))
2202 (defun package-menu-mark-obsolete-for-deletion ()
2203 "Mark all obsolete packages for deletion."
2204 (interactive)
2205 (save-excursion
2206 (goto-char (point-min))
2207 (while (not (eobp))
2208 (if (equal (package-menu-get-status) "obsolete")
2209 (tabulated-list-put-tag "D" t)
2210 (forward-line 1)))))
2212 (defun package-menu-quick-help ()
2213 "Show short key binding help for package-menu-mode."
2214 (interactive)
2215 (message "n-ext, i-nstall, d-elete, u-nmark, x-ecute, r-efresh, h-elp"))
2217 (define-obsolete-function-alias
2218 'package-menu-view-commentary 'package-menu-describe-package "24.1")
2220 (defun package-menu-get-status ()
2221 (let* ((id (tabulated-list-get-id))
2222 (entry (and id (assq id tabulated-list-entries))))
2223 (if entry
2224 (aref (cadr entry) 2)
2225 "")))
2227 (defun package-menu--find-upgrades ()
2228 (let (installed available upgrades)
2229 ;; Build list of installed/available packages in this buffer.
2230 (dolist (entry tabulated-list-entries)
2231 ;; ENTRY is (PKG-DESC [NAME VERSION STATUS DOC])
2232 (let ((pkg-desc (car entry))
2233 (status (aref (cadr entry) 2)))
2234 (cond ((member status '("installed" "unsigned"))
2235 (push pkg-desc installed))
2236 ((member status '("available" "new"))
2237 (setq available (package--append-to-alist pkg-desc available))))))
2238 ;; Loop through list of installed packages, finding upgrades.
2239 (dolist (pkg-desc installed)
2240 (let* ((name (package-desc-name pkg-desc))
2241 (avail-pkg (cadr (assq name available))))
2242 (and avail-pkg
2243 (version-list-< (package-desc-priority-version pkg-desc)
2244 (package-desc-priority-version avail-pkg))
2245 (push (cons name avail-pkg) upgrades))))
2246 upgrades))
2248 (defun package-menu-mark-upgrades ()
2249 "Mark all upgradable packages in the Package Menu.
2250 For each installed package with a newer version available, place
2251 an (I)nstall flag on the available version and a (D)elete flag on
2252 the installed version. A subsequent \\[package-menu-execute]
2253 call will upgrade the package."
2254 (interactive)
2255 (unless (derived-mode-p 'package-menu-mode)
2256 (error "The current buffer is not a Package Menu"))
2257 (let ((upgrades (package-menu--find-upgrades)))
2258 (if (null upgrades)
2259 (message "No packages to upgrade.")
2260 (widen)
2261 (save-excursion
2262 (goto-char (point-min))
2263 (while (not (eobp))
2264 (let* ((pkg-desc (tabulated-list-get-id))
2265 (upgrade (cdr (assq (package-desc-name pkg-desc) upgrades))))
2266 (cond ((null upgrade)
2267 (forward-line 1))
2268 ((equal pkg-desc upgrade)
2269 (package-menu-mark-install))
2271 (package-menu-mark-delete))))))
2272 (message "%d package%s marked for upgrading."
2273 (length upgrades)
2274 (if (= (length upgrades) 1) "" "s")))))
2276 (defun package-menu-execute (&optional noquery)
2277 "Perform marked Package Menu actions.
2278 Packages marked for installation are downloaded and installed;
2279 packages marked for deletion are removed.
2280 Optional argument NOQUERY non-nil means do not ask the user to confirm."
2281 (interactive)
2282 (unless (derived-mode-p 'package-menu-mode)
2283 (error "The current buffer is not in Package Menu mode"))
2284 (let (install-list delete-list cmd pkg-desc)
2285 (save-excursion
2286 (goto-char (point-min))
2287 (while (not (eobp))
2288 (setq cmd (char-after))
2289 (unless (eq cmd ?\s)
2290 ;; This is the key PKG-DESC.
2291 (setq pkg-desc (tabulated-list-get-id))
2292 (cond ((eq cmd ?D)
2293 (push pkg-desc delete-list))
2294 ((eq cmd ?I)
2295 (push pkg-desc install-list))))
2296 (forward-line)))
2297 (when install-list
2298 (if (or
2299 noquery
2300 (yes-or-no-p
2301 (if (= (length install-list) 1)
2302 (format "Install package `%s'? "
2303 (package-desc-full-name (car install-list)))
2304 (format "Install these %d packages (%s)? "
2305 (length install-list)
2306 (mapconcat #'package-desc-full-name
2307 install-list ", ")))))
2308 (mapc (lambda (p)
2309 (package-install p (and (null (package-installed-p p)) 1)))
2310 install-list)))
2311 ;; Delete packages, prompting if necessary.
2312 (when delete-list
2313 (if (or
2314 noquery
2315 (yes-or-no-p
2316 (if (= (length delete-list) 1)
2317 (format "Delete package `%s'? "
2318 (package-desc-full-name (car delete-list)))
2319 (format "Delete these %d packages (%s)? "
2320 (length delete-list)
2321 (mapconcat #'package-desc-full-name
2322 delete-list ", ")))))
2323 (dolist (elt delete-list)
2324 (condition-case-unless-debug err
2325 (package-delete elt)
2326 (error (message (cadr err)))))
2327 (error "Aborted")))
2328 (if (or delete-list install-list)
2329 (package-menu--generate t t)
2330 (message "No operations specified."))))
2332 (defun package-menu--version-predicate (A B)
2333 (let ((vA (or (aref (cadr A) 1) '(0)))
2334 (vB (or (aref (cadr B) 1) '(0))))
2335 (if (version-list-= vA vB)
2336 (package-menu--name-predicate A B)
2337 (version-list-< vA vB))))
2339 (defun package-menu--status-predicate (A B)
2340 (let ((sA (aref (cadr A) 2))
2341 (sB (aref (cadr B) 2)))
2342 (cond ((string= sA sB)
2343 (package-menu--name-predicate A B))
2344 ((string= sA "new") t)
2345 ((string= sB "new") nil)
2346 ((string= sA "available") t)
2347 ((string= sB "available") nil)
2348 ((string= sA "installed") t)
2349 ((string= sB "installed") nil)
2350 ((string= sA "unsigned") t)
2351 ((string= sB "unsigned") nil)
2352 ((string= sA "held") t)
2353 ((string= sB "held") nil)
2354 ((string= sA "built-in") t)
2355 ((string= sB "built-in") nil)
2356 ((string= sA "obsolete") t)
2357 ((string= sB "obsolete") nil)
2358 (t (string< sA sB)))))
2360 (defun package-menu--description-predicate (A B)
2361 (let ((dA (aref (cadr A) 3))
2362 (dB (aref (cadr B) 3)))
2363 (if (string= dA dB)
2364 (package-menu--name-predicate A B)
2365 (string< dA dB))))
2367 (defun package-menu--name-predicate (A B)
2368 (string< (symbol-name (package-desc-name (car A)))
2369 (symbol-name (package-desc-name (car B)))))
2371 (defun package-menu--archive-predicate (A B)
2372 (string< (or (package-desc-archive (car A)) "")
2373 (or (package-desc-archive (car B)) "")))
2375 ;;;###autoload
2376 (defun list-packages (&optional no-fetch)
2377 "Display a list of packages.
2378 This first fetches the updated list of packages before
2379 displaying, unless a prefix argument NO-FETCH is specified.
2380 The list is displayed in a buffer named `*Packages*'."
2381 (interactive "P")
2382 (require 'finder-inf nil t)
2383 ;; Initialize the package system if necessary.
2384 (unless package--initialized
2385 (package-initialize t))
2386 (let (old-archives new-packages)
2387 (unless no-fetch
2388 ;; Read the locally-cached archive-contents.
2389 (package-read-all-archive-contents)
2390 (setq old-archives package-archive-contents)
2391 ;; Fetch the remote list of packages.
2392 (package-refresh-contents)
2393 ;; Find which packages are new.
2394 (dolist (elt package-archive-contents)
2395 (unless (assq (car elt) old-archives)
2396 (push (car elt) new-packages))))
2398 ;; Generate the Package Menu.
2399 (let ((buf (get-buffer-create "*Packages*")))
2400 (with-current-buffer buf
2401 (package-menu-mode)
2402 (set (make-local-variable 'package-menu--new-package-list)
2403 new-packages)
2404 (package-menu--generate nil t))
2405 ;; The package menu buffer has keybindings. If the user types
2406 ;; `M-x list-packages', that suggests it should become current.
2407 (switch-to-buffer buf))
2409 (let ((upgrades (package-menu--find-upgrades)))
2410 (if upgrades
2411 (message "%d package%s can be upgraded; type `%s' to mark %s for upgrading."
2412 (length upgrades)
2413 (if (= (length upgrades) 1) "" "s")
2414 (substitute-command-keys "\\[package-menu-mark-upgrades]")
2415 (if (= (length upgrades) 1) "it" "them"))))))
2417 ;;;###autoload
2418 (defalias 'package-list-packages 'list-packages)
2420 ;; Used in finder.el
2421 (defun package-show-package-list (&optional packages keywords)
2422 "Display PACKAGES in a *Packages* buffer.
2423 This is similar to `list-packages', but it does not fetch the
2424 updated list of packages, and it only displays packages with
2425 names in PACKAGES (which should be a list of symbols).
2427 When KEYWORDS are given, only packages with those KEYWORDS are
2428 shown."
2429 (interactive)
2430 (require 'finder-inf nil t)
2431 (let* ((buf (get-buffer-create "*Packages*"))
2432 (win (get-buffer-window buf)))
2433 (with-current-buffer buf
2434 (package-menu-mode)
2435 (package-menu--generate nil packages keywords))
2436 (if win
2437 (select-window win)
2438 (switch-to-buffer buf))))
2440 ;; package-menu--generate rebinds "q" on the fly, so we have to
2441 ;; hard-code the binding in the doc-string here.
2442 (defun package-menu-filter (keyword)
2443 "Filter the *Packages* buffer.
2444 Show only those items that relate to the specified KEYWORD.
2445 To restore the full package list, type `q'."
2446 (interactive (list (completing-read "Keyword: " (package-all-keywords))))
2447 (package-show-package-list t (list keyword)))
2449 (defun package-list-packages-no-fetch ()
2450 "Display a list of packages.
2451 Does not fetch the updated list of packages before displaying.
2452 The list is displayed in a buffer named `*Packages*'."
2453 (interactive)
2454 (list-packages t))
2456 (provide 'package)
2458 ;;; package.el ends here