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