emacs-lisp/package.el (package-menu-mark-install): Mark dependency.
[emacs.git] / lisp / emacs-lisp / package.el
blobf11790693b987300172473d5022e7d64d0b2f825
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 package)))
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 PKG shoul be either a symbol, the package name, or a package-desc
1270 object."
1271 (interactive (list (intern (completing-read
1272 "Reinstall package: "
1273 (mapcar #'symbol-name
1274 (mapcar #'car package-alist))))))
1275 (package-delete
1276 (if (package-desc-p pkg) pkg (cadr (assq pkg package-alist)))
1277 'force 'nosave)
1278 (package-install pkg))
1280 (defun package-strip-rcs-id (str)
1281 "Strip RCS version ID from the version string STR.
1282 If the result looks like a dotted numeric version, return it.
1283 Otherwise return nil."
1284 (when str
1285 (when (string-match "\\`[ \t]*[$]Revision:[ \t]+" str)
1286 (setq str (substring str (match-end 0))))
1287 (condition-case nil
1288 (if (version-to-list str)
1289 str)
1290 (error nil))))
1292 (declare-function lm-homepage "lisp-mnt" (&optional file))
1294 (defun package--prepare-dependencies (deps)
1295 "Turn DEPS into an acceptable list of dependencies.
1297 Any parts missing a version string get a default version string
1298 of \"0\" (meaning any version) and an appropriate level of lists
1299 is wrapped around any parts requiring it."
1300 (cond
1301 ((not (listp deps))
1302 (error "Invalid requirement specifier: %S" deps))
1303 (t (mapcar (lambda (dep)
1304 (cond
1305 ((symbolp dep) `(,dep "0"))
1306 ((stringp dep)
1307 (error "Invalid requirement specifier: %S" dep))
1308 ((and (listp dep) (null (cdr dep)))
1309 (list (car dep) "0"))
1310 (t dep)))
1311 deps))))
1313 (defun package-buffer-info ()
1314 "Return a `package-desc' describing the package in the current buffer.
1316 If the buffer does not contain a conforming package, signal an
1317 error. If there is a package, narrow the buffer to the file's
1318 boundaries."
1319 (goto-char (point-min))
1320 (unless (re-search-forward "^;;; \\([^ ]*\\)\\.el ---[ \t]*\\(.*?\\)[ \t]*\\(-\\*-.*-\\*-[ \t]*\\)?$" nil t)
1321 (error "Package lacks a file header"))
1322 (let ((file-name (match-string-no-properties 1))
1323 (desc (match-string-no-properties 2))
1324 (start (line-beginning-position)))
1325 (unless (search-forward (concat ";;; " file-name ".el ends here"))
1326 (error "Package lacks a terminating comment"))
1327 ;; Try to include a trailing newline.
1328 (forward-line)
1329 (narrow-to-region start (point))
1330 (require 'lisp-mnt)
1331 ;; Use some headers we've invented to drive the process.
1332 (let* ((requires-str (lm-header "package-requires"))
1333 ;; Prefer Package-Version; if defined, the package author
1334 ;; probably wants us to use it. Otherwise try Version.
1335 (pkg-version
1336 (or (package-strip-rcs-id (lm-header "package-version"))
1337 (package-strip-rcs-id (lm-header "version"))))
1338 (homepage (lm-homepage)))
1339 (unless pkg-version
1340 (error
1341 "Package lacks a \"Version\" or \"Package-Version\" header"))
1342 (package-desc-from-define
1343 file-name pkg-version desc
1344 (if requires-str
1345 (package--prepare-dependencies
1346 (package-read-from-string requires-str)))
1347 :kind 'single
1348 :url homepage))))
1350 (declare-function tar-get-file-descriptor "tar-mode" (file))
1351 (declare-function tar--extract "tar-mode" (descriptor))
1353 (defun package-tar-file-info ()
1354 "Find package information for a tar file.
1355 The return result is a `package-desc'."
1356 (cl-assert (derived-mode-p 'tar-mode))
1357 (let* ((dir-name (file-name-directory
1358 (tar-header-name (car tar-parse-info))))
1359 (desc-file (package--description-file dir-name))
1360 (tar-desc (tar-get-file-descriptor (concat dir-name desc-file))))
1361 (unless tar-desc
1362 (error "No package descriptor file found"))
1363 (with-current-buffer (tar--extract tar-desc)
1364 (unwind-protect
1365 (or (package--read-pkg-desc 'tar)
1366 (error "Can't find define-package in %s"
1367 (tar-header-name tar-desc)))
1368 (kill-buffer (current-buffer))))))
1370 (defun package-dir-info ()
1371 "Find package information for a directory.
1372 The return result is a `package-desc'."
1373 (cl-assert (derived-mode-p 'dired-mode))
1374 (let* ((desc-file (package--description-file default-directory)))
1375 (if (file-readable-p desc-file)
1376 (with-temp-buffer
1377 (insert-file-contents desc-file)
1378 (package--read-pkg-desc 'dir))
1379 (let ((files (directory-files default-directory t "\\.el\\'" t))
1380 info)
1381 (while files
1382 (with-temp-buffer
1383 (insert-file-contents (pop files))
1384 ;; When we find the file with the data,
1385 (when (setq info (ignore-errors (package-buffer-info)))
1386 ;; stop looping,
1387 (setq files nil)
1388 ;; set the 'dir kind,
1389 (setf (package-desc-kind info) 'dir))))
1390 ;; and return the info.
1391 info))))
1393 (defun package--read-pkg-desc (kind)
1394 "Read a `define-package' form in current buffer.
1395 Return the pkg-desc, with desc-kind set to KIND."
1396 (goto-char (point-min))
1397 (unwind-protect
1398 (let* ((pkg-def-parsed (read (current-buffer)))
1399 (pkg-desc
1400 (when (eq (car pkg-def-parsed) 'define-package)
1401 (apply #'package-desc-from-define
1402 (append (cdr pkg-def-parsed))))))
1403 (when pkg-desc
1404 (setf (package-desc-kind pkg-desc) kind)
1405 pkg-desc))))
1408 ;;;###autoload
1409 (defun package-install-from-buffer ()
1410 "Install a package from the current buffer.
1411 The current buffer is assumed to be a single .el or .tar file or
1412 a directory. These must follow the packaging guidelines (see
1413 info node `(elisp)Packaging').
1415 Specially, if current buffer is a directory, the -pkg.el
1416 description file is not mandatory, in which case the information
1417 is derived from the main .el file in the directory.
1419 Downloads and installs required packages as needed."
1420 (interactive)
1421 (let* ((pkg-desc
1422 (cond
1423 ((derived-mode-p 'dired-mode)
1424 ;; This is the only way a package-desc object with a `dir'
1425 ;; desc-kind can be created. Such packages can't be
1426 ;; uploaded or installed from archives, they can only be
1427 ;; installed from local buffers or directories.
1428 (package-dir-info))
1429 ((derived-mode-p 'tar-mode)
1430 (package-tar-file-info))
1432 (package-buffer-info))))
1433 (name (package-desc-name pkg-desc)))
1434 ;; Download and install the dependencies.
1435 (let* ((requires (package-desc-reqs pkg-desc))
1436 (transaction (package-compute-transaction nil requires)))
1437 (package-download-transaction transaction))
1438 ;; Install the package itself.
1439 (package-unpack pkg-desc)
1440 (unless (package--user-selected-p name)
1441 (customize-save-variable 'package-selected-packages
1442 (cons name package-selected-packages)))
1443 pkg-desc))
1445 ;;;###autoload
1446 (defun package-install-file (file)
1447 "Install a package from a file.
1448 The file can either be a tar file or an Emacs Lisp file."
1449 (interactive "fPackage file name: ")
1450 (with-temp-buffer
1451 (if (file-directory-p file)
1452 (progn
1453 (setq default-directory file)
1454 (dired-mode))
1455 (insert-file-contents-literally file)
1456 (when (string-match "\\.tar\\'" file) (tar-mode)))
1457 (package-install-from-buffer)))
1459 (defun package--get-deps (pkg &optional only)
1460 (let* ((pkg-desc (cadr (assq pkg package-alist)))
1461 (direct-deps (cl-loop for p in (package-desc-reqs pkg-desc)
1462 for name = (car p)
1463 when (assq name package-alist)
1464 collect name))
1465 (indirect-deps (unless (eq only 'direct)
1466 (delete-dups
1467 (cl-loop for p in direct-deps
1468 append (package--get-deps p))))))
1469 (cl-case only
1470 (direct direct-deps)
1471 (separate (list direct-deps indirect-deps))
1472 (indirect indirect-deps)
1473 (t (delete-dups (append direct-deps indirect-deps))))))
1475 ;;;###autoload
1476 (defun package-install-user-selected-packages ()
1477 "Ensure packages in `package-selected-packages' are installed.
1478 If some packages are not installed propose to install them."
1479 (interactive)
1480 ;; We don't need to populate `package-selected-packages' before
1481 ;; using here, because the outcome is the same either way (nothing
1482 ;; gets installed).
1483 (if (not package-selected-packages)
1484 (message "`package-selected-packages' is empty, nothing to install")
1485 (cl-loop for p in package-selected-packages
1486 unless (package-installed-p p)
1487 collect p into lst
1488 finally
1489 (if lst
1490 (when (y-or-n-p
1491 (format "%s packages will be installed:\n%s, proceed?"
1492 (length lst)
1493 (mapconcat #'symbol-name lst ", ")))
1494 (mapc #'package-install lst))
1495 (message "All your packages are already installed")))))
1497 (defun package--used-elsewhere-p (pkg-desc &optional pkg-list)
1498 "Non-nil if PKG-DESC is a dependency of a package in PKG-LIST.
1499 Return the first package found in PKG-LIST of which PKG is a
1500 dependency.
1502 When not specified, PKG-LIST defaults to `package-alist'
1503 with PKG-DESC entry removed."
1504 (unless (string= (package-desc-status pkg-desc) "obsolete")
1505 (let ((pkg (package-desc-name pkg-desc)))
1506 (cl-loop with alist = (or pkg-list
1507 (remove (assq pkg package-alist)
1508 package-alist))
1509 for p in alist thereis
1510 (and (memq pkg (mapcar #'car (package-desc-reqs (cadr p))))
1511 (car p))))))
1513 (defun package-delete (pkg-desc &optional force nosave)
1514 "Delete package PKG-DESC.
1516 Argument PKG-DESC is a full description of package as vector.
1517 When package is used elsewhere as dependency of another package,
1518 refuse deleting it and return an error.
1519 If FORCE is non-nil package will be deleted even if it is used
1520 elsewhere.
1521 If NOSAVE is non-nil, the package is not removed from
1522 `package-selected-packages'."
1523 (let ((dir (package-desc-dir pkg-desc))
1524 (name (package-desc-name pkg-desc))
1525 pkg-used-elsewhere-by)
1526 ;; If the user is trying to delete this package, they definitely
1527 ;; don't want it marked as selected, so we remove it from
1528 ;; `package-selected-packages' even if it can't be deleted.
1529 (when (and (null nosave)
1530 (package--user-selected-p name))
1531 (customize-save-variable
1532 'package-selected-packages (remove name package-selected-packages)))
1533 (cond ((not (string-prefix-p (file-name-as-directory
1534 (expand-file-name package-user-dir))
1535 (expand-file-name dir)))
1536 ;; Don't delete "system" packages.
1537 (error "Package `%s' is a system package, not deleting"
1538 (package-desc-full-name pkg-desc)))
1539 ((and (null force)
1540 (setq pkg-used-elsewhere-by
1541 (package--used-elsewhere-p pkg-desc)))
1542 ;; Don't delete packages used as dependency elsewhere.
1543 (error "Package `%s' is used by `%s' as dependency, not deleting"
1544 (package-desc-full-name pkg-desc)
1545 pkg-used-elsewhere-by))
1547 (delete-directory dir t t)
1548 ;; Remove NAME-VERSION.signed file.
1549 (let ((signed-file (concat dir ".signed")))
1550 (if (file-exists-p signed-file)
1551 (delete-file signed-file)))
1552 ;; Update package-alist.
1553 (let ((pkgs (assq name package-alist)))
1554 (delete pkg-desc pkgs)
1555 (unless (cdr pkgs)
1556 (setq package-alist (delq pkgs package-alist))))
1557 (message "Package `%s' deleted." (package-desc-full-name pkg-desc))))))
1559 (defun package--removable-packages ()
1560 "Return a list of names of packages no longer needed.
1561 These are packages which are neither contained in
1562 `package-selected-packages' nor a dependency of one that is."
1563 (let ((needed (cl-loop for p in package-selected-packages
1564 if (assq p package-alist)
1565 ;; `p' and its dependencies are needed.
1566 append (cons p (package--get-deps p)))))
1567 (cl-loop for p in (mapcar #'car package-alist)
1568 unless (memq p needed)
1569 collect p)))
1571 ;;;###autoload
1572 (defun package-autoremove ()
1573 "Remove packages that are no more needed.
1575 Packages that are no more needed by other packages in
1576 `package-selected-packages' and their dependencies
1577 will be deleted."
1578 (interactive)
1579 ;; If `package-selected-packages' is nil, it would make no sense to
1580 ;; try to populate it here, because then `package-autoremove' will
1581 ;; do absolutely nothing.
1582 (when (or package-selected-packages
1583 (yes-or-no-p
1584 "`package-selected-packages' is empty! Really remove ALL packages? "))
1585 (let ((removable (package--removable-packages)))
1586 (if removable
1587 (when (y-or-n-p
1588 (format "%s packages will be deleted:\n%s, proceed? "
1589 (length removable)
1590 (mapconcat #'symbol-name removable ", ")))
1591 (mapc (lambda (p)
1592 (package-delete (cadr (assq p package-alist)) t))
1593 removable)
1594 (message "Nothing to autoremove"))))))
1596 (defun package-archive-base (desc)
1597 "Return the archive containing the package NAME."
1598 (cdr (assoc (package-desc-archive desc) package-archives)))
1600 (defun package-archive-priority (archive)
1601 "Return the priority of ARCHIVE.
1603 The archive priorities are specified in
1604 `package-archive-priorities'. If not given there, the priority
1605 defaults to 0."
1606 (or (cdr (assoc archive package-archive-priorities))
1609 (defun package-desc-priority-version (pkg-desc)
1610 "Return the version PKG-DESC with the archive priority prepended.
1612 This allows for easy comparison of package versions from
1613 different archives if archive priorities are meant to be taken in
1614 consideration."
1615 (cons (package-archive-priority
1616 (package-desc-archive pkg-desc))
1617 (package-desc-version pkg-desc)))
1619 (defun package--download-one-archive (archive file)
1620 "Retrieve an archive file FILE from ARCHIVE, and cache it.
1621 ARCHIVE should be a cons cell of the form (NAME . LOCATION),
1622 similar to an entry in `package-alist'. Save the cached copy to
1623 \"archives/NAME/archive-contents\" in `package-user-dir'."
1624 (let ((dir (expand-file-name (format "archives/%s" (car archive))
1625 package-user-dir))
1626 (sig-file (concat file ".sig"))
1627 good-signatures)
1628 (package--with-work-buffer (cdr archive) file
1629 ;; Check signature of archive-contents, if desired.
1630 (if (and package-check-signature
1631 (not (member archive package-unsigned-archives)))
1632 (if (package--archive-file-exists-p (cdr archive) sig-file)
1633 (setq good-signatures (package--check-signature (cdr archive)
1634 file))
1635 (unless (eq package-check-signature 'allow-unsigned)
1636 (error "Unsigned archive `%s'"
1637 (car archive)))))
1638 ;; Read the retrieved buffer to make sure it is valid (e.g. it
1639 ;; may fetch a URL redirect page).
1640 (when (listp (read (current-buffer)))
1641 (make-directory dir t)
1642 (write-region nil nil (expand-file-name file dir) nil 'silent)))
1643 (when good-signatures
1644 ;; Write out good signatures into archive-contents.signed file.
1645 (write-region (mapconcat #'epg-signature-to-string good-signatures "\n")
1647 (expand-file-name (concat file ".signed") dir)
1648 nil 'silent))))
1650 (declare-function epg-check-configuration "epg-config"
1651 (config &optional minimum-version))
1652 (declare-function epg-configuration "epg-config" ())
1653 (declare-function epg-import-keys-from-file "epg" (context keys))
1655 ;;;###autoload
1656 (defun package-import-keyring (&optional file)
1657 "Import keys from FILE."
1658 (interactive "fFile: ")
1659 (setq file (expand-file-name file))
1660 (let ((context (epg-make-context 'OpenPGP))
1661 (homedir (expand-file-name "gnupg" package-user-dir)))
1662 (with-file-modes 448
1663 (make-directory homedir t))
1664 (setf (epg-context-home-directory context) homedir)
1665 (message "Importing %s..." (file-name-nondirectory file))
1666 (epg-import-keys-from-file context file)
1667 (message "Importing %s...done" (file-name-nondirectory file))))
1669 ;;;###autoload
1670 (defun package-refresh-contents ()
1671 "Download the ELPA archive description if needed.
1672 This informs Emacs about the latest versions of all packages, and
1673 makes them available for download."
1674 (interactive)
1675 ;; FIXME: Do it asynchronously.
1676 (unless (file-exists-p package-user-dir)
1677 (make-directory package-user-dir t))
1678 (let ((default-keyring (expand-file-name "package-keyring.gpg"
1679 data-directory)))
1680 (when (and package-check-signature (file-exists-p default-keyring))
1681 (condition-case-unless-debug error
1682 (progn
1683 (epg-check-configuration (epg-configuration))
1684 (package-import-keyring default-keyring))
1685 (error (message "Cannot import default keyring: %S" (cdr error))))))
1686 (dolist (archive package-archives)
1687 (condition-case-unless-debug nil
1688 (package--download-one-archive archive "archive-contents")
1689 (error (message "Failed to download `%s' archive."
1690 (car archive)))))
1691 (package-read-all-archive-contents))
1693 (defun package--find-non-dependencies ()
1694 "Return a list of installed packages which are not dependencies.
1695 Finds all packages in `package-alist' which are not dependencies
1696 of any other packages.
1697 Used to populate `package-selected-packages'."
1698 (let ((dep-list
1699 (delete-dups
1700 (apply #'append
1701 (mapcar (lambda (p) (mapcar #'car (package-desc-reqs (cadr p))))
1702 package-alist)))))
1703 (cl-loop for p in package-alist
1704 for name = (car p)
1705 unless (memq name dep-list)
1706 collect name)))
1708 ;;;###autoload
1709 (defun package-initialize (&optional no-activate)
1710 "Load Emacs Lisp packages, and activate them.
1711 The variable `package-load-list' controls which packages to load.
1712 If optional arg NO-ACTIVATE is non-nil, don't activate packages."
1713 (interactive)
1714 (setq package-alist nil)
1715 (package-load-all-descriptors)
1716 (package-read-all-archive-contents)
1717 (unless no-activate
1718 (dolist (elt package-alist)
1719 (package-activate (car elt))))
1720 (setq package--initialized t))
1723 ;;;; Package description buffer.
1725 ;;;###autoload
1726 (defun describe-package (package)
1727 "Display the full documentation of PACKAGE (a symbol)."
1728 (interactive
1729 (let* ((guess (function-called-at-point)))
1730 (require 'finder-inf nil t)
1731 ;; Load the package list if necessary (but don't activate them).
1732 (unless package--initialized
1733 (package-initialize t))
1734 (let ((packages (append (mapcar 'car package-alist)
1735 (mapcar 'car package-archive-contents)
1736 (mapcar 'car package--builtins))))
1737 (unless (memq guess packages)
1738 (setq guess nil))
1739 (setq packages (mapcar 'symbol-name packages))
1740 (let ((val
1741 (completing-read (if guess
1742 (format "Describe package (default %s): "
1743 guess)
1744 "Describe package: ")
1745 packages nil t nil nil guess)))
1746 (list (intern val))))))
1747 (if (not (or (package-desc-p package) (and package (symbolp package))))
1748 (message "No package specified")
1749 (help-setup-xref (list #'describe-package package)
1750 (called-interactively-p 'interactive))
1751 (with-help-window (help-buffer)
1752 (with-current-buffer standard-output
1753 (describe-package-1 package)))))
1755 (defun describe-package-1 (pkg)
1756 (require 'lisp-mnt)
1757 (let* ((desc (or
1758 (if (package-desc-p pkg) pkg)
1759 (cadr (assq pkg package-alist))
1760 (let ((built-in (assq pkg package--builtins)))
1761 (if built-in
1762 (package--from-builtin built-in)
1763 (cadr (assq pkg package-archive-contents))))))
1764 (name (if desc (package-desc-name desc) pkg))
1765 (pkg-dir (if desc (package-desc-dir desc)))
1766 (reqs (if desc (package-desc-reqs desc)))
1767 (version (if desc (package-desc-version desc)))
1768 (archive (if desc (package-desc-archive desc)))
1769 (extras (and desc (package-desc-extras desc)))
1770 (homepage (cdr (assoc :url extras)))
1771 (keywords (if desc (package-desc--keywords desc)))
1772 (built-in (eq pkg-dir 'builtin))
1773 (installable (and archive (not built-in)))
1774 (status (if desc (package-desc-status desc) "orphan"))
1775 (signed (if desc (package-desc-signed desc))))
1776 (prin1 name)
1777 (princ " is ")
1778 (princ (if (memq (aref status 0) '(?a ?e ?i ?o ?u)) "an " "a "))
1779 (princ status)
1780 (princ " package.\n\n")
1782 (insert " " (propertize "Status" 'font-lock-face 'bold) ": ")
1783 (cond (built-in
1784 (insert (propertize (capitalize status)
1785 'font-lock-face 'font-lock-builtin-face)
1786 "."))
1787 (pkg-dir
1788 (insert (propertize (if (member status '("unsigned" "dependency"))
1789 "Installed"
1790 (capitalize status)) ;FIXME: Why comment-face?
1791 'font-lock-face 'font-lock-comment-face))
1792 (insert " in `")
1793 ;; Todo: Add button for uninstalling.
1794 (help-insert-xref-button (abbreviate-file-name
1795 (file-name-as-directory pkg-dir))
1796 'help-package-def pkg-dir)
1797 (if (and (package-built-in-p name)
1798 (not (package-built-in-p name version)))
1799 (insert "',\n shadowing a "
1800 (propertize "built-in package"
1801 'font-lock-face 'font-lock-builtin-face))
1802 (insert "'"))
1803 (if signed
1804 (insert ".")
1805 (insert " (unsigned).")))
1806 (installable
1807 (insert (capitalize status))
1808 (insert " from " (format "%s" archive))
1809 (insert " -- ")
1810 (package-make-button
1811 "Install"
1812 'action 'package-install-button-action
1813 'package-desc desc))
1814 (t (insert (capitalize status) ".")))
1815 (insert "\n")
1816 (insert " " (propertize "Archive" 'font-lock-face 'bold)
1817 ": " (or archive "n/a") "\n")
1818 (and version
1819 (insert " "
1820 (propertize "Version" 'font-lock-face 'bold) ": "
1821 (package-version-join version) "\n"))
1823 (setq reqs (if desc (package-desc-reqs desc)))
1824 (when reqs
1825 (insert " " (propertize "Requires" 'font-lock-face 'bold) ": ")
1826 (let ((first t)
1827 name vers text)
1828 (dolist (req reqs)
1829 (setq name (car req)
1830 vers (cadr req)
1831 text (format "%s-%s" (symbol-name name)
1832 (package-version-join vers)))
1833 (cond (first (setq first nil))
1834 ((>= (+ 2 (current-column) (length text))
1835 (window-width))
1836 (insert ",\n "))
1837 (t (insert ", ")))
1838 (help-insert-xref-button text 'help-package name))
1839 (insert "\n")))
1840 (insert " " (propertize "Summary" 'font-lock-face 'bold)
1841 ": " (if desc (package-desc-summary desc)) "\n")
1842 (when homepage
1843 (insert " " (propertize "Homepage" 'font-lock-face 'bold) ": ")
1844 (help-insert-xref-button homepage 'help-url homepage)
1845 (insert "\n"))
1846 (when keywords
1847 (insert " " (propertize "Keywords" 'font-lock-face 'bold) ": ")
1848 (dolist (k keywords)
1849 (package-make-button
1851 'package-keyword k
1852 'action 'package-keyword-button-action)
1853 (insert " "))
1854 (insert "\n"))
1855 (let* ((all-pkgs (append (cdr (assq name package-alist))
1856 (cdr (assq name package-archive-contents))
1857 (let ((bi (assq name package--builtins)))
1858 (if bi (list (package--from-builtin bi))))))
1859 (other-pkgs (delete desc all-pkgs)))
1860 (when other-pkgs
1861 (insert " " (propertize "Other versions" 'font-lock-face 'bold) ": "
1862 (mapconcat
1863 (lambda (opkg)
1864 (let* ((ov (package-desc-version opkg))
1865 (dir (package-desc-dir opkg))
1866 (from (or (package-desc-archive opkg)
1867 (if (stringp dir) "installed" dir))))
1868 (if (not ov) (format "%s" from)
1869 (format "%s (%s)"
1870 (make-text-button (package-version-join ov) nil
1871 'face 'link
1872 'follow-link t
1873 'action
1874 (lambda (_button)
1875 (describe-package opkg)))
1876 from))))
1877 other-pkgs ", ")
1878 ".\n")))
1880 (insert "\n")
1882 (if built-in
1883 ;; For built-in packages, insert the commentary.
1884 (let ((fn (locate-file (format "%s.el" name) load-path
1885 load-file-rep-suffixes))
1886 (opoint (point)))
1887 (insert (or (lm-commentary fn) ""))
1888 (save-excursion
1889 (goto-char opoint)
1890 (when (re-search-forward "^;;; Commentary:\n" nil t)
1891 (replace-match ""))
1892 (while (re-search-forward "^\\(;+ ?\\)" nil t)
1893 (replace-match ""))))
1894 (let ((readme (expand-file-name (format "%s-readme.txt" name)
1895 package-user-dir))
1896 readme-string)
1897 ;; For elpa packages, try downloading the commentary. If that
1898 ;; fails, try an existing readme file in `package-user-dir'.
1899 (cond ((condition-case nil
1900 (save-excursion
1901 (package--with-work-buffer
1902 (package-archive-base desc)
1903 (format "%s-readme.txt" name)
1904 (save-excursion
1905 (goto-char (point-max))
1906 (unless (bolp)
1907 (insert ?\n)))
1908 (write-region nil nil
1909 (expand-file-name readme package-user-dir)
1910 nil 'silent)
1911 (setq readme-string (buffer-string))
1913 (error nil))
1914 (insert readme-string))
1915 ((file-readable-p readme)
1916 (insert-file-contents readme)
1917 (goto-char (point-max))))))))
1919 (defun package-install-button-action (button)
1920 (let ((pkg-desc (button-get button 'package-desc)))
1921 (when (y-or-n-p (format "Install package `%s'? "
1922 (package-desc-full-name pkg-desc)))
1923 (package-install pkg-desc 1)
1924 (revert-buffer nil t)
1925 (goto-char (point-min)))))
1927 (defun package-keyword-button-action (button)
1928 (let ((pkg-keyword (button-get button 'package-keyword)))
1929 (package-show-package-list t (list pkg-keyword))))
1931 (defun package-make-button (text &rest props)
1932 (let ((button-text (if (display-graphic-p) text (concat "[" text "]")))
1933 (button-face (if (display-graphic-p)
1934 '(:box (:line-width 2 :color "dark grey")
1935 :background "light grey"
1936 :foreground "black")
1937 'link)))
1938 (apply 'insert-text-button button-text 'face button-face 'follow-link t
1939 props)))
1942 ;;;; Package menu mode.
1944 (defvar package-menu-mode-map
1945 (let ((map (make-sparse-keymap))
1946 (menu-map (make-sparse-keymap "Package")))
1947 (set-keymap-parent map tabulated-list-mode-map)
1948 (define-key map "\C-m" 'package-menu-describe-package)
1949 (define-key map "u" 'package-menu-mark-unmark)
1950 (define-key map "\177" 'package-menu-backup-unmark)
1951 (define-key map "d" 'package-menu-mark-delete)
1952 (define-key map "i" 'package-menu-mark-install)
1953 (define-key map "U" 'package-menu-mark-upgrades)
1954 (define-key map "r" 'package-menu-refresh)
1955 (define-key map "f" 'package-menu-filter)
1956 (define-key map "~" 'package-menu-mark-obsolete-for-deletion)
1957 (define-key map "x" 'package-menu-execute)
1958 (define-key map "h" 'package-menu-quick-help)
1959 (define-key map "?" 'package-menu-describe-package)
1960 (define-key map [menu-bar package-menu] (cons "Package" menu-map))
1961 (define-key menu-map [mq]
1962 '(menu-item "Quit" quit-window
1963 :help "Quit package selection"))
1964 (define-key menu-map [s1] '("--"))
1965 (define-key menu-map [mn]
1966 '(menu-item "Next" next-line
1967 :help "Next Line"))
1968 (define-key menu-map [mp]
1969 '(menu-item "Previous" previous-line
1970 :help "Previous Line"))
1971 (define-key menu-map [s2] '("--"))
1972 (define-key menu-map [mu]
1973 '(menu-item "Unmark" package-menu-mark-unmark
1974 :help "Clear any marks on a package and move to the next line"))
1975 (define-key menu-map [munm]
1976 '(menu-item "Unmark Backwards" package-menu-backup-unmark
1977 :help "Back up one line and clear any marks on that package"))
1978 (define-key menu-map [md]
1979 '(menu-item "Mark for Deletion" package-menu-mark-delete
1980 :help "Mark a package for deletion and move to the next line"))
1981 (define-key menu-map [mi]
1982 '(menu-item "Mark for Install" package-menu-mark-install
1983 :help "Mark a package for installation and move to the next line"))
1984 (define-key menu-map [mupgrades]
1985 '(menu-item "Mark Upgradable Packages" package-menu-mark-upgrades
1986 :help "Mark packages that have a newer version for upgrading"))
1987 (define-key menu-map [s3] '("--"))
1988 (define-key menu-map [mf]
1989 '(menu-item "Filter Package List..." package-menu-filter
1990 :help "Filter package selection (q to go back)"))
1991 (define-key menu-map [mg]
1992 '(menu-item "Update Package List" revert-buffer
1993 :help "Update the list of packages"))
1994 (define-key menu-map [mr]
1995 '(menu-item "Refresh Package List" package-menu-refresh
1996 :help "Download the ELPA archive"))
1997 (define-key menu-map [s4] '("--"))
1998 (define-key menu-map [mt]
1999 '(menu-item "Mark Obsolete Packages" package-menu-mark-obsolete-for-deletion
2000 :help "Mark all obsolete packages for deletion"))
2001 (define-key menu-map [mx]
2002 '(menu-item "Execute Actions" package-menu-execute
2003 :help "Perform all the marked actions"))
2004 (define-key menu-map [s5] '("--"))
2005 (define-key menu-map [mh]
2006 '(menu-item "Help" package-menu-quick-help
2007 :help "Show short key binding help for package-menu-mode"))
2008 (define-key menu-map [mc]
2009 '(menu-item "Describe Package" package-menu-describe-package
2010 :help "Display information about this package"))
2011 map)
2012 "Local keymap for `package-menu-mode' buffers.")
2014 (defvar package-menu--new-package-list nil
2015 "List of newly-available packages since `list-packages' was last called.")
2017 (define-derived-mode package-menu-mode tabulated-list-mode "Package Menu"
2018 "Major mode for browsing a list of packages.
2019 Letters do not insert themselves; instead, they are commands.
2020 \\<package-menu-mode-map>
2021 \\{package-menu-mode-map}"
2022 (setq tabulated-list-format
2023 `[("Package" 18 package-menu--name-predicate)
2024 ("Version" 13 nil)
2025 ("Status" 10 package-menu--status-predicate)
2026 ,@(if (cdr package-archives)
2027 '(("Archive" 10 package-menu--archive-predicate)))
2028 ("Description" 0 nil)])
2029 (setq tabulated-list-padding 2)
2030 (setq tabulated-list-sort-key (cons "Status" nil))
2031 (add-hook 'tabulated-list-revert-hook 'package-menu--refresh nil t)
2032 (tabulated-list-init-header))
2034 (defmacro package--push (pkg-desc status listname)
2035 "Convenience macro for `package-menu--generate'.
2036 If the alist stored in the symbol LISTNAME lacks an entry for a
2037 package PKG-DESC, add one. The alist is keyed with PKG-DESC."
2038 `(unless (assoc ,pkg-desc ,listname)
2039 ;; FIXME: Should we move status into pkg-desc?
2040 (push (cons ,pkg-desc ,status) ,listname)))
2042 (defvar package-list-unversioned nil
2043 "If non-nil include packages that don't have a version in `list-package'.")
2045 (defvar package-list-unsigned nil
2046 "If non-nil, mention in the list which packages were installed w/o signature.")
2048 (defun package-desc-status (pkg-desc)
2049 (let* ((name (package-desc-name pkg-desc))
2050 (dir (package-desc-dir pkg-desc))
2051 (lle (assq name package-load-list))
2052 (held (cadr lle))
2053 (version (package-desc-version pkg-desc))
2054 (signed (or (not package-list-unsigned)
2055 (package-desc-signed pkg-desc))))
2056 (cond
2057 ((eq dir 'builtin) "built-in")
2058 ((and lle (null held)) "disabled")
2059 ((stringp held)
2060 (let ((hv (if (stringp held) (version-to-list held))))
2061 (cond
2062 ((version-list-= version hv) "held")
2063 ((version-list-< version hv) "obsolete")
2064 (t "disabled"))))
2065 ((package-built-in-p name version) "obsolete")
2066 (dir ;One of the installed packages.
2067 (cond
2068 ((not (file-exists-p (package-desc-dir pkg-desc))) "deleted")
2069 ((eq pkg-desc (cadr (assq name package-alist)))
2070 (if (not signed) "unsigned"
2071 (if (package--user-selected-p name)
2072 "installed" "dependency")))
2073 (t "obsolete")))
2075 (let* ((ins (cadr (assq name package-alist)))
2076 (ins-v (if ins (package-desc-version ins))))
2077 (cond
2078 ((or (null ins) (version-list-< ins-v version))
2079 (if (memq name package-menu--new-package-list)
2080 "new" "available"))
2081 ((version-list-< version ins-v) "obsolete")
2082 ((version-list-= version ins-v)
2083 (if (not signed) "unsigned"
2084 (if (package--user-selected-p name)
2085 "installed" "dependency")))))))))
2087 (defun package-menu--refresh (&optional packages keywords)
2088 "Re-populate the `tabulated-list-entries'.
2089 PACKAGES should be nil or t, which means to display all known packages.
2090 KEYWORDS should be nil or a list of keywords."
2091 ;; Construct list of (PKG-DESC . STATUS).
2092 (unless packages (setq packages t))
2093 (let (info-list name)
2094 ;; Installed packages:
2095 (dolist (elt package-alist)
2096 (setq name (car elt))
2097 (when (or (eq packages t) (memq name packages))
2098 (dolist (pkg (cdr elt))
2099 (when (package--has-keyword-p pkg keywords)
2100 (package--push pkg (package-desc-status pkg) info-list)))))
2102 ;; Built-in packages:
2103 (dolist (elt package--builtins)
2104 (setq name (car elt))
2105 (when (and (not (eq name 'emacs)) ; Hide the `emacs' package.
2106 (package--has-keyword-p (package--from-builtin elt) keywords)
2107 (or package-list-unversioned
2108 (package--bi-desc-version (cdr elt)))
2109 (or (eq packages t) (memq name packages)))
2110 (package--push (package--from-builtin elt) "built-in" info-list)))
2112 ;; Available and disabled packages:
2113 (dolist (elt package-archive-contents)
2114 (setq name (car elt))
2115 (when (or (eq packages t) (memq name packages))
2116 (dolist (pkg (cdr elt))
2117 ;; Hide obsolete packages.
2118 (when (and (not (package-installed-p (package-desc-name pkg)
2119 (package-desc-version pkg)))
2120 (package--has-keyword-p pkg keywords))
2121 (package--push pkg (package-desc-status pkg) info-list)))))
2123 ;; Print the result.
2124 (setq tabulated-list-entries
2125 (mapcar #'package-menu--print-info info-list))))
2127 (defun package-all-keywords ()
2128 "Collect all package keywords"
2129 (let (keywords)
2130 (package--mapc (lambda (desc)
2131 (let* ((desc-keywords (and desc (package-desc--keywords desc))))
2132 (setq keywords (append keywords desc-keywords)))))
2133 keywords))
2135 (defun package--mapc (function &optional packages)
2136 "Call FUNCTION for all known PACKAGES.
2137 PACKAGES can be nil or t, which means to display all known
2138 packages, or a list of packages.
2140 Built-in packages are converted with `package--from-builtin'."
2141 (unless packages (setq packages t))
2142 (let (name)
2143 ;; Installed packages:
2144 (dolist (elt package-alist)
2145 (setq name (car elt))
2146 (when (or (eq packages t) (memq name packages))
2147 (mapc function (cdr elt))))
2149 ;; Built-in packages:
2150 (dolist (elt package--builtins)
2151 (setq name (car elt))
2152 (when (and (not (eq name 'emacs)) ; Hide the `emacs' package.
2153 (or package-list-unversioned
2154 (package--bi-desc-version (cdr elt)))
2155 (or (eq packages t) (memq name packages)))
2156 (funcall function (package--from-builtin elt))))
2158 ;; Available and disabled packages:
2159 (dolist (elt package-archive-contents)
2160 (setq name (car elt))
2161 (when (or (eq packages t) (memq name packages))
2162 (dolist (pkg (cdr elt))
2163 ;; Hide obsolete packages.
2164 (unless (package-installed-p (package-desc-name pkg)
2165 (package-desc-version pkg))
2166 (funcall function pkg)))))))
2168 (defun package--has-keyword-p (desc &optional keywords)
2169 "Test if package DESC has any of the given KEYWORDS.
2170 When none are given, the package matches."
2171 (if keywords
2172 (let* ((desc-keywords (and desc (package-desc--keywords desc)))
2173 found)
2174 (dolist (k keywords)
2175 (when (and (not found)
2176 (member k desc-keywords))
2177 (setq found t)))
2178 found)
2181 (defun package-menu--generate (remember-pos packages &optional keywords)
2182 "Populate the Package Menu.
2183 If REMEMBER-POS is non-nil, keep point on the same entry.
2184 PACKAGES should be t, which means to display all known packages,
2185 or a list of package names (symbols) to display.
2187 With KEYWORDS given, only packages with those keywords are
2188 shown."
2189 (package-menu--refresh packages keywords)
2190 (setf (car (aref tabulated-list-format 0))
2191 (if keywords
2192 (let ((filters (mapconcat 'identity keywords ",")))
2193 (concat "Package[" filters "]"))
2194 "Package"))
2195 (if keywords
2196 (define-key package-menu-mode-map "q" 'package-show-package-list)
2197 (define-key package-menu-mode-map "q" 'quit-window))
2198 (tabulated-list-init-header)
2199 (tabulated-list-print remember-pos))
2201 (defun package-menu--print-info (pkg)
2202 "Return a package entry suitable for `tabulated-list-entries'.
2203 PKG has the form (PKG-DESC . STATUS).
2204 Return (PKG-DESC [NAME VERSION STATUS DOC])."
2205 (let* ((pkg-desc (car pkg))
2206 (status (cdr pkg))
2207 (face (pcase status
2208 (`"built-in" 'font-lock-builtin-face)
2209 (`"available" 'default)
2210 (`"new" 'bold)
2211 (`"held" 'font-lock-constant-face)
2212 (`"disabled" 'font-lock-warning-face)
2213 (`"installed" 'font-lock-comment-face)
2214 (`"dependency" 'font-lock-comment-face)
2215 (`"unsigned" 'font-lock-warning-face)
2216 (_ 'font-lock-warning-face)))) ; obsolete.
2217 (list pkg-desc
2218 `[,(list (symbol-name (package-desc-name pkg-desc))
2219 'face 'link
2220 'follow-link t
2221 'package-desc pkg-desc
2222 'action 'package-menu-describe-package)
2223 ,(propertize (package-version-join
2224 (package-desc-version pkg-desc))
2225 'font-lock-face face)
2226 ,(propertize status 'font-lock-face face)
2227 ,@(if (cdr package-archives)
2228 (list (propertize (or (package-desc-archive pkg-desc) "")
2229 'font-lock-face face)))
2230 ,(propertize (package-desc-summary pkg-desc)
2231 'font-lock-face face)])))
2233 (defun package-menu-refresh ()
2234 "Download the Emacs Lisp package archive.
2235 This fetches the contents of each archive specified in
2236 `package-archives', and then refreshes the package menu."
2237 (interactive)
2238 (unless (derived-mode-p 'package-menu-mode)
2239 (user-error "The current buffer is not a Package Menu"))
2240 (package-refresh-contents)
2241 (package-menu--generate t t))
2243 (defun package-menu-describe-package (&optional button)
2244 "Describe the current package.
2245 If optional arg BUTTON is non-nil, describe its associated package."
2246 (interactive)
2247 (let ((pkg-desc (if button (button-get button 'package-desc)
2248 (tabulated-list-get-id))))
2249 (if pkg-desc
2250 (describe-package pkg-desc)
2251 (user-error "No package here"))))
2253 ;; fixme numeric argument
2254 (defun package-menu-mark-delete (&optional _num)
2255 "Mark a package for deletion and move to the next line."
2256 (interactive "p")
2257 (if (member (package-menu-get-status)
2258 '("installed" "dependency" "obsolete" "unsigned"))
2259 (tabulated-list-put-tag "D" t)
2260 (forward-line)))
2262 (defun package-menu-mark-install (&optional _num)
2263 "Mark a package for installation and move to the next line."
2264 (interactive "p")
2265 (if (member (package-menu-get-status) '("available" "new" "dependency"))
2266 (tabulated-list-put-tag "I" t)
2267 (forward-line)))
2269 (defun package-menu-mark-unmark (&optional _num)
2270 "Clear any marks on a package and move to the next line."
2271 (interactive "p")
2272 (tabulated-list-put-tag " " t))
2274 (defun package-menu-backup-unmark ()
2275 "Back up one line and clear any marks on that package."
2276 (interactive)
2277 (forward-line -1)
2278 (tabulated-list-put-tag " "))
2280 (defun package-menu-mark-obsolete-for-deletion ()
2281 "Mark all obsolete packages for deletion."
2282 (interactive)
2283 (save-excursion
2284 (goto-char (point-min))
2285 (while (not (eobp))
2286 (if (equal (package-menu-get-status) "obsolete")
2287 (tabulated-list-put-tag "D" t)
2288 (forward-line 1)))))
2290 (defun package-menu-quick-help ()
2291 "Show short key binding help for package-menu-mode."
2292 (interactive)
2293 (message "n-ext, i-nstall, d-elete, u-nmark, x-ecute, r-efresh, h-elp"))
2295 (define-obsolete-function-alias
2296 'package-menu-view-commentary 'package-menu-describe-package "24.1")
2298 (defun package-menu-get-status ()
2299 (let* ((id (tabulated-list-get-id))
2300 (entry (and id (assq id tabulated-list-entries))))
2301 (if entry
2302 (aref (cadr entry) 2)
2303 "")))
2305 (defun package-menu--find-upgrades ()
2306 (let (installed available upgrades)
2307 ;; Build list of installed/available packages in this buffer.
2308 (dolist (entry tabulated-list-entries)
2309 ;; ENTRY is (PKG-DESC [NAME VERSION STATUS DOC])
2310 (let ((pkg-desc (car entry))
2311 (status (aref (cadr entry) 2)))
2312 (cond ((member status '("installed" "dependency" "unsigned"))
2313 (push pkg-desc installed))
2314 ((member status '("available" "new"))
2315 (setq available (package--append-to-alist pkg-desc available))))))
2316 ;; Loop through list of installed packages, finding upgrades.
2317 (dolist (pkg-desc installed)
2318 (let* ((name (package-desc-name pkg-desc))
2319 (avail-pkg (cadr (assq name available))))
2320 (and avail-pkg
2321 (version-list-< (package-desc-priority-version pkg-desc)
2322 (package-desc-priority-version avail-pkg))
2323 (push (cons name avail-pkg) upgrades))))
2324 upgrades))
2326 (defun package-menu-mark-upgrades ()
2327 "Mark all upgradable packages in the Package Menu.
2328 For each installed package with a newer version available, place
2329 an (I)nstall flag on the available version and a (D)elete flag on
2330 the installed version. A subsequent \\[package-menu-execute]
2331 call will upgrade the package."
2332 (interactive)
2333 (unless (derived-mode-p 'package-menu-mode)
2334 (error "The current buffer is not a Package Menu"))
2335 (let ((upgrades (package-menu--find-upgrades)))
2336 (if (null upgrades)
2337 (message "No packages to upgrade.")
2338 (widen)
2339 (save-excursion
2340 (goto-char (point-min))
2341 (while (not (eobp))
2342 (let* ((pkg-desc (tabulated-list-get-id))
2343 (upgrade (cdr (assq (package-desc-name pkg-desc) upgrades))))
2344 (cond ((null upgrade)
2345 (forward-line 1))
2346 ((equal pkg-desc upgrade)
2347 (package-menu-mark-install))
2349 (package-menu-mark-delete))))))
2350 (message "%d package%s marked for upgrading."
2351 (length upgrades)
2352 (if (= (length upgrades) 1) "" "s")))))
2354 (defun package--sort-deps-in-alist (package only)
2355 "Return a list of dependencies for PACKAGE sorted by dependency.
2356 PACKAGE is included as the first element of the returned list.
2357 ONLY is an alist associating package names to package objects.
2358 Only these packages will be in the return value an their cdrs are
2359 destructively set to nil in ONLY."
2360 (let ((out))
2361 (dolist (dep (package-desc-reqs package))
2362 (when-let ((cell (assq (car dep) only))
2363 (dep-package (cdr-safe cell)))
2364 (setcdr cell nil)
2365 (setq out (append (package--sort-deps-in-alist dep-package only)
2366 out))))
2367 (cons package out)))
2369 (defun package--sort-by-dependence (package-list)
2370 "Return PACKAGE-LIST sorted by dependence.
2371 That is, any element of the returned list is guaranteed to not
2372 directly depend on any elements that come before it.
2374 PACKAGE-LIST is a list of package-desc objects.
2375 Indirect dependencies are guaranteed to be returned in order only
2376 if all the in-between dependencies are also in PACKAGE-LIST."
2377 (let ((alist (mapcar (lambda (p) (cons (package-desc-name p) p)) package-list))
2378 out-list)
2379 (dolist (cell alist out-list)
2380 ;; `package--sort-deps-in-alist' destructively changes alist, so
2381 ;; some cells might already be empty. We check this here.
2382 (when-let ((pkg-desc (cdr cell)))
2383 (setcdr cell nil)
2384 (setq out-list
2385 (append (package--sort-deps-in-alist pkg-desc alist)
2386 out-list))))))
2388 (defun package-menu-execute (&optional noquery)
2389 "Perform marked Package Menu actions.
2390 Packages marked for installation are downloaded and installed;
2391 packages marked for deletion are removed.
2392 Optional argument NOQUERY non-nil means do not ask the user to confirm."
2393 (interactive)
2394 (unless (derived-mode-p 'package-menu-mode)
2395 (error "The current buffer is not in Package Menu mode"))
2396 (let (install-list delete-list cmd pkg-desc)
2397 (save-excursion
2398 (goto-char (point-min))
2399 (while (not (eobp))
2400 (setq cmd (char-after))
2401 (unless (eq cmd ?\s)
2402 ;; This is the key PKG-DESC.
2403 (setq pkg-desc (tabulated-list-get-id))
2404 (cond ((eq cmd ?D)
2405 (push pkg-desc delete-list))
2406 ((eq cmd ?I)
2407 (push pkg-desc install-list))))
2408 (forward-line)))
2409 (when install-list
2410 (if (or
2411 noquery
2412 (yes-or-no-p
2413 (if (= (length install-list) 1)
2414 (format "Install package `%s'? "
2415 (package-desc-full-name (car install-list)))
2416 (format "Install these %d packages (%s)? "
2417 (length install-list)
2418 (mapconcat #'package-desc-full-name
2419 install-list ", ")))))
2420 (mapc (lambda (p)
2421 ;; Mark as selected if it's the exact version of a
2422 ;; package that's already installed, or if it's not
2423 ;; installed at all. Don't mark if it's a new
2424 ;; version of an installed package.
2425 (package-install p (or (package-installed-p p)
2426 (not (package-installed-p
2427 (package-desc-name p))))))
2428 install-list)))
2429 ;; Delete packages, prompting if necessary.
2430 (when delete-list
2431 (if (or
2432 noquery
2433 (yes-or-no-p
2434 (if (= (length delete-list) 1)
2435 (format "Delete package `%s'? "
2436 (package-desc-full-name (car delete-list)))
2437 (format "Delete these %d packages (%s)? "
2438 (length delete-list)
2439 (mapconcat #'package-desc-full-name
2440 delete-list ", ")))))
2441 (dolist (elt (package--sort-by-dependence delete-list))
2442 (condition-case-unless-debug err
2443 (package-delete elt)
2444 (error (message (cadr err)))))
2445 (error "Aborted")))
2446 (if (not (or delete-list install-list))
2447 (message "No operations specified.")
2448 (when package-selected-packages
2449 (let ((removable (package--removable-packages)))
2450 (when (and removable
2451 (y-or-n-p
2452 (format "These %d packages are no longer needed, delete them (%s)? "
2453 (length removable)
2454 (mapconcat #'symbol-name removable ", "))))
2455 ;; We know these are removable, so we can use force instead of sorting them.
2456 (mapc (lambda (p) (package-delete (cadr (assq p package-alist)) 'force 'nosave))
2457 removable))))
2458 (package-menu--generate t t))))
2460 (defun package-menu--version-predicate (A B)
2461 (let ((vA (or (aref (cadr A) 1) '(0)))
2462 (vB (or (aref (cadr B) 1) '(0))))
2463 (if (version-list-= vA vB)
2464 (package-menu--name-predicate A B)
2465 (version-list-< vA vB))))
2467 (defun package-menu--status-predicate (A B)
2468 (let ((sA (aref (cadr A) 2))
2469 (sB (aref (cadr B) 2)))
2470 (cond ((string= sA sB)
2471 (package-menu--name-predicate A B))
2472 ((string= sA "new") t)
2473 ((string= sB "new") nil)
2474 ((string= sA "available") t)
2475 ((string= sB "available") nil)
2476 ((string= sA "installed") t)
2477 ((string= sB "installed") nil)
2478 ((string= sA "dependency") t)
2479 ((string= sB "dependency") nil)
2480 ((string= sA "unsigned") t)
2481 ((string= sB "unsigned") nil)
2482 ((string= sA "held") t)
2483 ((string= sB "held") nil)
2484 ((string= sA "built-in") t)
2485 ((string= sB "built-in") nil)
2486 ((string= sA "obsolete") t)
2487 ((string= sB "obsolete") nil)
2488 (t (string< sA sB)))))
2490 (defun package-menu--description-predicate (A B)
2491 (let ((dA (aref (cadr A) 3))
2492 (dB (aref (cadr B) 3)))
2493 (if (string= dA dB)
2494 (package-menu--name-predicate A B)
2495 (string< dA dB))))
2497 (defun package-menu--name-predicate (A B)
2498 (string< (symbol-name (package-desc-name (car A)))
2499 (symbol-name (package-desc-name (car B)))))
2501 (defun package-menu--archive-predicate (A B)
2502 (string< (or (package-desc-archive (car A)) "")
2503 (or (package-desc-archive (car B)) "")))
2505 ;;;###autoload
2506 (defun list-packages (&optional no-fetch)
2507 "Display a list of packages.
2508 This first fetches the updated list of packages before
2509 displaying, unless a prefix argument NO-FETCH is specified.
2510 The list is displayed in a buffer named `*Packages*'."
2511 (interactive "P")
2512 (require 'finder-inf nil t)
2513 ;; Initialize the package system if necessary.
2514 (unless package--initialized
2515 (package-initialize t))
2516 (let (old-archives new-packages)
2517 (unless no-fetch
2518 ;; Read the locally-cached archive-contents.
2519 (package-read-all-archive-contents)
2520 (setq old-archives package-archive-contents)
2521 ;; Fetch the remote list of packages.
2522 (package-refresh-contents)
2523 ;; Find which packages are new.
2524 (dolist (elt package-archive-contents)
2525 (unless (assq (car elt) old-archives)
2526 (push (car elt) new-packages))))
2528 ;; Generate the Package Menu.
2529 (let ((buf (get-buffer-create "*Packages*")))
2530 (with-current-buffer buf
2531 (package-menu-mode)
2532 (set (make-local-variable 'package-menu--new-package-list)
2533 new-packages)
2534 (package-menu--generate nil t))
2535 ;; The package menu buffer has keybindings. If the user types
2536 ;; `M-x list-packages', that suggests it should become current.
2537 (switch-to-buffer buf))
2539 (let ((upgrades (package-menu--find-upgrades)))
2540 (if upgrades
2541 (message "%d package%s can be upgraded; type `%s' to mark %s for upgrading."
2542 (length upgrades)
2543 (if (= (length upgrades) 1) "" "s")
2544 (substitute-command-keys "\\[package-menu-mark-upgrades]")
2545 (if (= (length upgrades) 1) "it" "them"))))))
2547 ;;;###autoload
2548 (defalias 'package-list-packages 'list-packages)
2550 ;; Used in finder.el
2551 (defun package-show-package-list (&optional packages keywords)
2552 "Display PACKAGES in a *Packages* buffer.
2553 This is similar to `list-packages', but it does not fetch the
2554 updated list of packages, and it only displays packages with
2555 names in PACKAGES (which should be a list of symbols).
2557 When KEYWORDS are given, only packages with those KEYWORDS are
2558 shown."
2559 (interactive)
2560 (require 'finder-inf nil t)
2561 (let* ((buf (get-buffer-create "*Packages*"))
2562 (win (get-buffer-window buf)))
2563 (with-current-buffer buf
2564 (package-menu-mode)
2565 (package-menu--generate nil packages keywords))
2566 (if win
2567 (select-window win)
2568 (switch-to-buffer buf))))
2570 ;; package-menu--generate rebinds "q" on the fly, so we have to
2571 ;; hard-code the binding in the doc-string here.
2572 (defun package-menu-filter (keyword)
2573 "Filter the *Packages* buffer.
2574 Show only those items that relate to the specified KEYWORD.
2575 To restore the full package list, type `q'."
2576 (interactive (list (completing-read "Keyword: " (package-all-keywords))))
2577 (package-show-package-list t (list keyword)))
2579 (defun package-list-packages-no-fetch ()
2580 "Display a list of packages.
2581 Does not fetch the updated list of packages before displaying.
2582 The list is displayed in a buffer named `*Packages*'."
2583 (interactive)
2584 (list-packages t))
2586 (provide 'package)
2588 ;;; package.el ends here