* lisp/emacs-lisp/package.el (package-refresh-contents): Fix spelling error
[emacs.git] / lisp / emacs-lisp / package.el
blob0275da39229bc2dc5b9cc2fdaca9720e2d666b81
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 'subr-x))
165 (eval-when-compile (require 'cl-lib))
166 (eval-when-compile (require 'epg)) ;For setf accessors.
168 (require 'tabulated-list)
169 (require 'macroexp)
171 (defgroup package nil
172 "Manager for Emacs Lisp packages."
173 :group 'applications
174 :version "24.1")
176 ;;;###autoload
177 (defcustom package-enable-at-startup t
178 "Whether to activate installed packages when Emacs starts.
179 If non-nil, packages are activated after reading the init file
180 and before `after-init-hook'. Activation is not done if
181 `user-init-file' is nil (e.g. Emacs was started with \"-q\").
183 Even if the value is nil, you can type \\[package-initialize] to
184 activate the package system at any time."
185 :type 'boolean
186 :group 'package
187 :version "24.1")
189 (defcustom package-load-list '(all)
190 "List of packages for `package-initialize' to load.
191 Each element in this list should be a list (NAME VERSION), or the
192 symbol `all'. The symbol `all' says to load the latest installed
193 versions of all packages not specified by other elements.
195 For an element (NAME VERSION), NAME is a package name (a symbol).
196 VERSION should be t, a string, or nil.
197 If VERSION is t, the most recent version is activated.
198 If VERSION is a string, only that version is ever loaded.
199 Any other version, even if newer, is silently ignored.
200 Hence, the package is \"held\" at that version.
201 If VERSION is nil, the package is not loaded (it is \"disabled\")."
202 :type '(repeat symbol)
203 :risky t
204 :group 'package
205 :version "24.1")
207 (defvar Info-directory-list)
208 (declare-function info-initialize "info" ())
209 (declare-function url-http-file-exists-p "url-http" (url))
210 (declare-function lm-header "lisp-mnt" (header))
211 (declare-function lm-commentary "lisp-mnt" (&optional file))
213 (defcustom package-archives '(("gnu" . "http://elpa.gnu.org/packages/"))
214 "An alist of archives from which to fetch.
215 The default value points to the GNU Emacs package repository.
217 Each element has the form (ID . LOCATION).
218 ID is an archive name, as a string.
219 LOCATION specifies the base location for the archive.
220 If it starts with \"http:\", it is treated as a HTTP URL;
221 otherwise it should be an absolute directory name.
222 (Other types of URL are currently not supported.)
224 Only add locations that you trust, since fetching and installing
225 a package can run arbitrary code."
226 :type '(alist :key-type (string :tag "Archive name")
227 :value-type (string :tag "URL or directory name"))
228 :risky t
229 :group 'package
230 :version "24.1")
232 (defcustom package-archive-priorities nil
233 "An alist of priorities for packages.
235 Each element has the form (ARCHIVE-ID . PRIORITY).
237 When installing packages, the package with the highest version
238 number from the archive with the highest priority is
239 selected. When higher versions are available from archives with
240 lower priorities, the user has to select those manually.
242 Archives not in this list have the priority 0."
243 :type '(alist :key-type (string :tag "Archive name")
244 :value-type (integer :tag "Priority (default is 0)"))
245 :risky t
246 :group 'package
247 :version "25.1")
249 (defcustom package-pinned-packages nil
250 "An alist of packages that are pinned to specific archives.
251 This can be useful if you have multiple package archives enabled,
252 and want to control which archive a given package gets installed from.
254 Each element of the alist has the form (PACKAGE . ARCHIVE), where:
255 PACKAGE is a symbol representing a package
256 ARCHIVE is a string representing an archive (it should be the car of
257 an element in `package-archives', e.g. \"gnu\").
259 Adding an entry to this variable means that only ARCHIVE will be
260 considered as a source for PACKAGE. If other archives provide PACKAGE,
261 they are ignored (for this package). If ARCHIVE does not contain PACKAGE,
262 the package will be unavailable."
263 :type '(alist :key-type (symbol :tag "Package")
264 :value-type (string :tag "Archive name"))
265 ;; I don't really see why this is risky...
266 ;; I suppose it could prevent you receiving updates for a package,
267 ;; via an entry (PACKAGE . NON-EXISTING). Which could be an issue
268 ;; if PACKAGE has a known vulnerability that is fixed in newer versions.
269 :risky t
270 :group 'package
271 :version "24.4")
273 (defconst package-archive-version 1
274 "Version number of the package archive understood by this file.
275 Lower version numbers than this will probably be understood as well.")
277 ;; We don't prime the cache since it tends to get out of date.
278 (defvar package-archive-contents nil
279 "Cache of the contents of the Emacs Lisp Package Archive.
280 This is an alist mapping package names (symbols) to
281 non-empty lists of `package-desc' structures.")
282 (put 'package-archive-contents 'risky-local-variable t)
284 (defcustom package-user-dir (locate-user-emacs-file "elpa")
285 "Directory containing the user's Emacs Lisp packages.
286 The directory name should be absolute.
287 Apart from this directory, Emacs also looks for system-wide
288 packages in `package-directory-list'."
289 :type 'directory
290 :risky t
291 :group 'package
292 :version "24.1")
294 (defcustom package-directory-list
295 ;; Defaults are subdirs named "elpa" in the site-lisp dirs.
296 (let (result)
297 (dolist (f load-path)
298 (and (stringp f)
299 (equal (file-name-nondirectory f) "site-lisp")
300 (push (expand-file-name "elpa" f) result)))
301 (nreverse result))
302 "List of additional directories containing Emacs Lisp packages.
303 Each directory name should be absolute.
305 These directories contain packages intended for system-wide; in
306 contrast, `package-user-dir' contains packages for personal use."
307 :type '(repeat directory)
308 :risky t
309 :group 'package
310 :version "24.1")
312 (defvar epg-gpg-program)
314 (defcustom package-check-signature
315 (if (progn (require 'epg-config) (executable-find epg-gpg-program))
316 'allow-unsigned)
317 "Non-nil means to check package signatures when installing.
318 The value `allow-unsigned' means to still install a package even if
319 it is unsigned.
321 This also applies to the \"archive-contents\" file that lists the
322 contents of the archive."
323 :type '(choice (const nil :tag "Never")
324 (const allow-unsigned :tag "Allow unsigned")
325 (const t :tag "Check always"))
326 :risky t
327 :group 'package
328 :version "24.4")
330 (defcustom package-unsigned-archives nil
331 "List of archives where we do not check for package signatures."
332 :type '(repeat (string :tag "Archive name"))
333 :risky t
334 :group 'package
335 :version "24.4")
337 (defcustom package-selected-packages nil
338 "Store here packages installed explicitly by user.
339 This variable is fed automatically by Emacs when installing a new package.
340 This variable is used by `package-autoremove' to decide
341 which packages are no longer 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--compatibility-table nil
471 "Hash table connecting package names to their compatibility.
472 Each key is a symbol, the name of a package.
474 The value is either nil, representing an incompatible package, or
475 a version list, representing the highest compatible version of
476 that package which is available.
478 A package is considered incompatible if it requires an Emacs
479 version higher than the one being used. To check for package
480 \(in)compatibility, don't read this table directly, use
481 `package--incompatible-p' which also checks dependencies.")
483 (defvar package-activated-list nil
484 ;; FIXME: This should implicitly include all builtin packages.
485 "List of the names of currently activated packages.")
486 (put 'package-activated-list 'risky-local-variable t)
488 (defun package-version-join (vlist)
489 "Return the version string corresponding to the list VLIST.
490 This is, approximately, the inverse of `version-to-list'.
491 \(Actually, it returns only one of the possible inverses, since
492 `version-to-list' is a many-to-one operation.)"
493 (if (null vlist)
495 (let ((str-list (list "." (int-to-string (car vlist)))))
496 (dolist (num (cdr vlist))
497 (cond
498 ((>= num 0)
499 (push (int-to-string num) str-list)
500 (push "." str-list))
501 ((< num -4)
502 (error "Invalid version list `%s'" vlist))
504 ;; pre, or beta, or alpha
505 (cond ((equal "." (car str-list))
506 (pop str-list))
507 ((not (string-match "[0-9]+" (car str-list)))
508 (error "Invalid version list `%s'" vlist)))
509 (push (cond ((= num -1) "pre")
510 ((= num -2) "beta")
511 ((= num -3) "alpha")
512 ((= num -4) "snapshot"))
513 str-list))))
514 (if (equal "." (car str-list))
515 (pop str-list))
516 (apply 'concat (nreverse str-list)))))
518 (defun package-load-descriptor (pkg-dir)
519 "Load the description file in directory PKG-DIR."
520 (let ((pkg-file (expand-file-name (package--description-file pkg-dir)
521 pkg-dir))
522 (signed-file (concat pkg-dir ".signed")))
523 (when (file-exists-p pkg-file)
524 (with-temp-buffer
525 (insert-file-contents pkg-file)
526 (goto-char (point-min))
527 (let ((pkg-desc (package-process-define-package
528 (read (current-buffer)) pkg-file)))
529 (setf (package-desc-dir pkg-desc) pkg-dir)
530 (if (file-exists-p signed-file)
531 (setf (package-desc-signed pkg-desc) t))
532 pkg-desc)))))
534 (defun package-load-all-descriptors ()
535 "Load descriptors for installed Emacs Lisp packages.
536 This looks for package subdirectories in `package-user-dir' and
537 `package-directory-list'. The variable `package-load-list'
538 controls which package subdirectories may be loaded.
540 In each valid package subdirectory, this function loads the
541 description file containing a call to `define-package', which
542 updates `package-alist'."
543 (dolist (dir (cons package-user-dir package-directory-list))
544 (when (file-directory-p dir)
545 (dolist (subdir (directory-files dir))
546 (let ((pkg-dir (expand-file-name subdir dir)))
547 (when (file-directory-p pkg-dir)
548 (package-load-descriptor pkg-dir)))))))
550 (defun package-disabled-p (pkg-name version)
551 "Return whether PKG-NAME at VERSION can be activated.
552 The decision is made according to `package-load-list'.
553 Return nil if the package can be activated.
554 Return t if the package is completely disabled.
555 Return the max version (as a string) if the package is held at a lower version."
556 (let ((force (assq pkg-name package-load-list)))
557 (cond ((null force) (not (memq 'all package-load-list)))
558 ((null (setq force (cadr force))) t) ; disabled
559 ((eq force t) nil)
560 ((stringp force) ; held
561 (unless (version-list-= version (version-to-list force))
562 force))
563 (t (error "Invalid element in `package-load-list'")))))
565 (defun package-activate-1 (pkg-desc &optional reload)
566 "Activate package given by PKG-DESC, even if it was already active.
567 If RELOAD is non-nil, also `load' any files inside the package which
568 correspond to previously loaded files (those returned by
569 `package--list-loaded-files')."
570 (let* ((name (package-desc-name pkg-desc))
571 (pkg-dir (package-desc-dir pkg-desc))
572 (pkg-dir-dir (file-name-as-directory pkg-dir)))
573 (unless pkg-dir
574 (error "Internal error: unable to find directory for `%s'"
575 (package-desc-full-name pkg-desc)))
576 ;; Add to load path, add autoloads, and activate the package.
577 (let* ((old-lp load-path)
578 (autoloads-file (expand-file-name
579 (format "%s-autoloads" name) pkg-dir))
580 (loaded-files-list (and reload (package--list-loaded-files pkg-dir))))
581 (with-demoted-errors "Error in package-activate-1: %s"
582 (load autoloads-file nil t))
583 (when (and (eq old-lp load-path)
584 (not (or (member pkg-dir load-path)
585 (member pkg-dir-dir load-path))))
586 ;; Old packages don't add themselves to the `load-path', so we have to
587 ;; do it ourselves.
588 (push pkg-dir load-path))
589 ;; Call `load' on all files in `pkg-dir' already present in
590 ;; `load-history'. This is done so that macros in these files are updated
591 ;; to their new definitions. If another package is being installed which
592 ;; depends on this new definition, not doing this update would cause
593 ;; compilation errors and break the installation.
594 (with-demoted-errors "Error in package-activate-1: %s"
595 (mapc (lambda (feature) (load feature nil t))
596 ;; Skip autoloads file since we already evaluated it above.
597 (remove (file-truename autoloads-file) loaded-files-list))))
598 ;; Add info node.
599 (when (file-exists-p (expand-file-name "dir" pkg-dir))
600 ;; FIXME: not the friendliest, but simple.
601 (require 'info)
602 (info-initialize)
603 (push pkg-dir Info-directory-list))
604 (push name package-activated-list)
605 ;; Don't return nil.
608 (declare-function find-library-name "find-func" (library))
609 (defun package--list-loaded-files (dir)
610 "Recursively list all files in DIR which correspond to loaded features.
611 Returns the `file-name-sans-extension' of each file, relative to
612 DIR, sorted by most recently loaded last."
613 (let* ((history (delq nil
614 (mapcar (lambda (x)
615 (let ((f (car x)))
616 (and f (file-name-sans-extension f))))
617 load-history)))
618 (dir (file-truename dir))
619 ;; List all files that have already been loaded.
620 (list-of-conflicts
621 (delq
623 (mapcar
624 (lambda (x) (let* ((file (file-relative-name x dir))
625 ;; Previously loaded file, if any.
626 (previous
627 (ignore-errors
628 (file-name-sans-extension
629 (file-truename (find-library-name file)))))
630 (pos (when previous (member previous history))))
631 ;; Return (RELATIVE-FILENAME . HISTORY-POSITION)
632 (when pos
633 (cons (file-name-sans-extension file) (length pos)))))
634 (directory-files-recursively dir "\\`[^\\.].*\\.el\\'")))))
635 ;; Turn the list of (FILENAME . POS) back into a list of features. Files in
636 ;; subdirectories are returned relative to DIR (so not actually features).
637 (let ((default-directory (file-name-as-directory dir)))
638 (mapcar (lambda (x) (file-truename (car x)))
639 (sort list-of-conflicts
640 ;; Sort the files by ascending HISTORY-POSITION.
641 (lambda (x y) (< (cdr x) (cdr y))))))))
643 (defun package-built-in-p (package &optional min-version)
644 "Return true if PACKAGE is built-in to Emacs.
645 Optional arg MIN-VERSION, if non-nil, should be a version list
646 specifying the minimum acceptable version."
647 (if (package-desc-p package) ;; was built-in and then was converted
648 (eq 'builtin (package-desc-dir package))
649 (let ((bi (assq package package--builtin-versions)))
650 (cond
651 (bi (version-list-<= min-version (cdr bi)))
652 ((remove 0 min-version) nil)
654 (require 'finder-inf nil t) ; For `package--builtins'.
655 (assq package package--builtins))))))
657 (defun package--from-builtin (bi-desc)
658 (package-desc-create :name (pop bi-desc)
659 :version (package--bi-desc-version bi-desc)
660 :summary (package--bi-desc-summary bi-desc)
661 :dir 'builtin))
663 ;; This function goes ahead and activates a newer version of a package
664 ;; if an older one was already activated. This is not ideal; we'd at
665 ;; least need to check to see if the package has actually been loaded,
666 ;; and not merely activated.
667 (defun package-activate (package &optional force)
668 "Activate package PACKAGE.
669 If FORCE is true, (re-)activate it if it's already activated."
670 (let ((pkg-descs (cdr (assq package package-alist))))
671 ;; Check if PACKAGE is available in `package-alist'.
672 (while
673 (when pkg-descs
674 (let ((available-version (package-desc-version (car pkg-descs))))
675 (or (package-disabled-p package available-version)
676 ;; Prefer a builtin package.
677 (package-built-in-p package available-version))))
678 (setq pkg-descs (cdr pkg-descs)))
679 (cond
680 ;; If no such package is found, maybe it's built-in.
681 ((null pkg-descs)
682 (package-built-in-p package))
683 ;; If the package is already activated, just return t.
684 ((and (memq package package-activated-list) (not force))
686 ;; Otherwise, proceed with activation.
688 (let* ((pkg-vec (car pkg-descs))
689 (fail (catch 'dep-failure
690 ;; Activate its dependencies recursively.
691 (dolist (req (package-desc-reqs pkg-vec))
692 (unless (package-activate (car req))
693 (throw 'dep-failure req))))))
694 (if fail
695 (warn "Unable to activate package `%s'.
696 Required package `%s-%s' is unavailable"
697 package (car fail) (package-version-join (cadr fail)))
698 ;; If all goes well, activate the package itself.
699 (package-activate-1 pkg-vec force)))))))
701 (defun define-package (_name-string _version-string
702 &optional _docstring _requirements
703 &rest _extra-properties)
704 "Define a new package.
705 NAME-STRING is the name of the package, as a string.
706 VERSION-STRING is the version of the package, as a string.
707 DOCSTRING is a short description of the package, a string.
708 REQUIREMENTS is a list of dependencies on other packages.
709 Each requirement is of the form (OTHER-PACKAGE OTHER-VERSION),
710 where OTHER-VERSION is a string.
712 EXTRA-PROPERTIES is currently unused."
713 ;; FIXME: Placeholder! Should we keep it?
714 (error "Don't call me!"))
716 (defun package-process-define-package (exp origin)
717 (unless (eq (car-safe exp) 'define-package)
718 (error "Can't find define-package in %s" origin))
719 (let* ((new-pkg-desc (apply #'package-desc-from-define (cdr exp)))
720 (name (package-desc-name new-pkg-desc))
721 (version (package-desc-version new-pkg-desc))
722 (old-pkgs (assq name package-alist)))
723 (if (null old-pkgs)
724 ;; If there's no old package, just add this to `package-alist'.
725 (push (list name new-pkg-desc) package-alist)
726 ;; If there is, insert the new package at the right place in the list.
727 (while
728 (if (and (cdr old-pkgs)
729 (version-list-< version
730 (package-desc-version (cadr old-pkgs))))
731 (setq old-pkgs (cdr old-pkgs))
732 (push new-pkg-desc (cdr old-pkgs))
733 nil)))
734 new-pkg-desc))
736 ;; From Emacs 22, but changed so it adds to load-path.
737 (defun package-autoload-ensure-default-file (file)
738 "Make sure that the autoload file FILE exists and if not create it."
739 (unless (file-exists-p file)
740 (write-region
741 (concat ";;; " (file-name-nondirectory file)
742 " --- automatically extracted autoloads\n"
743 ";;\n"
744 ";;; Code:\n"
745 "(add-to-list 'load-path (or (file-name-directory #$) (car load-path)))\n"
746 "\f\n;; Local Variables:\n"
747 ";; version-control: never\n"
748 ";; no-byte-compile: t\n"
749 ";; no-update-autoloads: t\n"
750 ";; End:\n"
751 ";;; " (file-name-nondirectory file)
752 " ends here\n")
753 nil file nil 'silent))
754 file)
756 (defvar generated-autoload-file)
757 (defvar version-control)
759 (defun package-generate-autoloads (name pkg-dir)
760 (let* ((auto-name (format "%s-autoloads.el" name))
761 ;;(ignore-name (concat name "-pkg.el"))
762 (generated-autoload-file (expand-file-name auto-name pkg-dir))
763 (backup-inhibited t)
764 (version-control 'never))
765 (package-autoload-ensure-default-file generated-autoload-file)
766 (update-directory-autoloads pkg-dir)
767 (let ((buf (find-buffer-visiting generated-autoload-file)))
768 (when buf (kill-buffer buf)))
769 auto-name))
771 (defvar tar-parse-info)
772 (declare-function tar-untar-buffer "tar-mode" ())
773 (declare-function tar-header-name "tar-mode" (tar-header) t)
774 (declare-function tar-header-link-type "tar-mode" (tar-header) t)
776 (defun package-untar-buffer (dir)
777 "Untar the current buffer.
778 This uses `tar-untar-buffer' from Tar mode. All files should
779 untar into a directory named DIR; otherwise, signal an error."
780 (require 'tar-mode)
781 (tar-mode)
782 ;; Make sure everything extracts into DIR.
783 (let ((regexp (concat "\\`" (regexp-quote (expand-file-name dir)) "/"))
784 (case-fold-search (memq system-type '(windows-nt ms-dos cygwin))))
785 (dolist (tar-data tar-parse-info)
786 (let ((name (expand-file-name (tar-header-name tar-data))))
787 (or (string-match regexp name)
788 ;; Tarballs created by some utilities don't list
789 ;; directories with a trailing slash (Bug#13136).
790 (and (string-equal dir name)
791 (eq (tar-header-link-type tar-data) 5))
792 (error "Package does not untar cleanly into directory %s/" dir)))))
793 (tar-untar-buffer))
795 (defun package-generate-description-file (pkg-desc pkg-file)
796 "Create the foo-pkg.el file for single-file packages."
797 (let* ((name (package-desc-name pkg-desc)))
798 (let ((print-level nil)
799 (print-quoted t)
800 (print-length nil))
801 (write-region
802 (concat
803 ";;; -*- no-byte-compile: t -*-\n"
804 (prin1-to-string
805 (nconc
806 (list 'define-package
807 (symbol-name name)
808 (package-version-join (package-desc-version pkg-desc))
809 (package-desc-summary pkg-desc)
810 (let ((requires (package-desc-reqs pkg-desc)))
811 (list 'quote
812 ;; Turn version lists into string form.
813 (mapcar
814 (lambda (elt)
815 (list (car elt)
816 (package-version-join (cadr elt))))
817 requires))))
818 (package--alist-to-plist-args
819 (package-desc-extras pkg-desc))))
820 "\n")
821 nil pkg-file nil 'silent))))
823 (defun package--alist-to-plist-args (alist)
824 (mapcar 'macroexp-quote
825 (apply #'nconc
826 (mapcar (lambda (pair) (list (car pair) (cdr pair))) alist))))
827 (defun package-unpack (pkg-desc)
828 "Install the contents of the current buffer as a package."
829 (let* ((name (package-desc-name pkg-desc))
830 (dirname (package-desc-full-name pkg-desc))
831 (pkg-dir (expand-file-name dirname package-user-dir)))
832 (pcase (package-desc-kind pkg-desc)
833 (`dir
834 (make-directory pkg-dir t)
835 (let ((file-list
836 (directory-files
837 default-directory 'full "\\`[^.].*\\.el\\'" 'nosort)))
838 (dolist (source-file file-list)
839 (let ((target-el-file
840 (expand-file-name (file-name-nondirectory source-file) pkg-dir)))
841 (copy-file source-file target-el-file t)))
842 ;; Now that the files have been installed, this package is
843 ;; indistinguishable from a `tar' or a `single'. Let's make
844 ;; things simple by ensuring we're one of them.
845 (setf (package-desc-kind pkg-desc)
846 (if (> (length file-list) 1) 'tar 'single))))
847 (`tar
848 (make-directory package-user-dir t)
849 ;; FIXME: should we delete PKG-DIR if it exists?
850 (let* ((default-directory (file-name-as-directory package-user-dir)))
851 (package-untar-buffer dirname)))
852 (`single
853 (let ((el-file (expand-file-name (format "%s.el" name) pkg-dir)))
854 (make-directory pkg-dir t)
855 (package--write-file-no-coding el-file)))
856 (kind (error "Unknown package kind: %S" kind)))
857 (package--make-autoloads-and-stuff pkg-desc pkg-dir)
858 ;; Update package-alist.
859 (let ((new-desc (package-load-descriptor pkg-dir)))
860 ;; FIXME: Check that `new-desc' matches `desc'!
861 ;; FIXME: Compilation should be done as a separate, optional, step.
862 ;; E.g. for multi-package installs, we should first install all packages
863 ;; and then compile them.
864 (package--compile new-desc))
865 ;; Try to activate it.
866 (package-activate name 'force)
867 pkg-dir))
869 (defun package--make-autoloads-and-stuff (pkg-desc pkg-dir)
870 "Generate autoloads, description file, etc.. for PKG-DESC installed at PKG-DIR."
871 (package-generate-autoloads (package-desc-name pkg-desc) pkg-dir)
872 (let ((desc-file (expand-file-name (package--description-file pkg-dir)
873 pkg-dir)))
874 (unless (file-exists-p desc-file)
875 (package-generate-description-file pkg-desc desc-file)))
876 ;; FIXME: Create foo.info and dir file from foo.texi?
879 (defun package--compile (pkg-desc)
880 "Byte-compile installed package PKG-DESC."
881 (package-activate-1 pkg-desc)
882 (byte-recompile-directory (package-desc-dir pkg-desc) 0 t))
884 (defun package--write-file-no-coding (file-name)
885 (let ((buffer-file-coding-system 'no-conversion))
886 (write-region (point-min) (point-max) file-name nil 'silent)))
888 (defmacro package--with-work-buffer (location file &rest body)
889 "Run BODY in a buffer containing the contents of FILE at LOCATION.
890 LOCATION is the base location of a package archive, and should be
891 one of the URLs (or file names) specified in `package-archives'.
892 FILE is the name of a file relative to that base location.
894 This macro retrieves FILE from LOCATION into a temporary buffer,
895 and evaluates BODY while that buffer is current. This work
896 buffer is killed afterwards. Return the last value in BODY."
897 (declare (indent 2) (debug t))
898 `(with-temp-buffer
899 (if (string-match-p "\\`https?:" ,location)
900 (url-insert-file-contents (concat ,location ,file))
901 (unless (file-name-absolute-p ,location)
902 (error "Archive location %s is not an absolute file name"
903 ,location))
904 (insert-file-contents (expand-file-name ,file ,location)))
905 ,@body))
907 (defun package--archive-file-exists-p (location file)
908 (let ((http (string-match "\\`https?:" location)))
909 (if http
910 (progn
911 (require 'url-http)
912 (url-http-file-exists-p (concat location file)))
913 (file-exists-p (expand-file-name file location)))))
915 (declare-function epg-make-context "epg"
916 (&optional protocol armor textmode include-certs
917 cipher-algorithm
918 digest-algorithm
919 compress-algorithm))
920 (declare-function epg-verify-string "epg" (context signature
921 &optional signed-text))
922 (declare-function epg-context-result-for "epg" (context name))
923 (declare-function epg-signature-status "epg" (signature))
924 (declare-function epg-signature-to-string "epg" (signature))
926 (defun package--display-verify-error (context sig-file)
927 (unless (equal (epg-context-error-output context) "")
928 (with-output-to-temp-buffer "*Error*"
929 (with-current-buffer standard-output
930 (if (epg-context-result-for context 'verify)
931 (insert (format "Failed to verify signature %s:\n" sig-file)
932 (mapconcat #'epg-signature-to-string
933 (epg-context-result-for context 'verify)
934 "\n"))
935 (insert (format "Error while verifying signature %s:\n" sig-file)))
936 (insert "\nCommand output:\n" (epg-context-error-output context))))))
938 (defun package--check-signature (location file)
939 "Check signature of the current buffer.
940 GnuPG keyring is located under \"gnupg\" in `package-user-dir'."
941 (let* ((context (epg-make-context 'OpenPGP))
942 (homedir (expand-file-name "gnupg" package-user-dir))
943 (sig-file (concat file ".sig"))
944 (sig-content (package--with-work-buffer location sig-file
945 (buffer-string))))
946 (setf (epg-context-home-directory context) homedir)
947 (condition-case error
948 (epg-verify-string context sig-content (buffer-string))
949 (error
950 (package--display-verify-error context sig-file)
951 (signal (car error) (cdr error))))
952 (let (good-signatures had-fatal-error)
953 ;; The .sig file may contain multiple signatures. Success if one
954 ;; of the signatures is good.
955 (dolist (sig (epg-context-result-for context 'verify))
956 (if (eq (epg-signature-status sig) 'good)
957 (push sig good-signatures)
958 ;; If package-check-signature is allow-unsigned, don't
959 ;; signal error when we can't verify signature because of
960 ;; missing public key. Other errors are still treated as
961 ;; fatal (bug#17625).
962 (unless (and (eq package-check-signature 'allow-unsigned)
963 (eq (epg-signature-status sig) 'no-pubkey))
964 (setq had-fatal-error t))))
965 (when (and (null good-signatures) had-fatal-error)
966 (package--display-verify-error context sig-file)
967 (error "Failed to verify signature %s" sig-file))
968 good-signatures)))
970 (defun package-install-from-archive (pkg-desc)
971 "Download and install a tar package."
972 ;; This won't happen, unless the archive is doing something wrong.
973 (when (eq (package-desc-kind pkg-desc) 'dir)
974 (error "Can't install directory package from archive"))
975 (let* ((location (package-archive-base pkg-desc))
976 (file (concat (package-desc-full-name pkg-desc)
977 (package-desc-suffix pkg-desc)))
978 (sig-file (concat file ".sig"))
979 good-signatures pkg-descs)
980 (package--with-work-buffer location file
981 (if (and package-check-signature
982 (not (member (package-desc-archive pkg-desc)
983 package-unsigned-archives)))
984 (if (package--archive-file-exists-p location sig-file)
985 (setq good-signatures (package--check-signature location file))
986 (unless (eq package-check-signature 'allow-unsigned)
987 (error "Unsigned package: `%s'"
988 (package-desc-name pkg-desc)))))
989 (package-unpack pkg-desc))
990 ;; Here the package has been installed successfully, mark it as
991 ;; signed if appropriate.
992 (when good-signatures
993 ;; Write out good signatures into NAME-VERSION.signed file.
994 (write-region (mapconcat #'epg-signature-to-string good-signatures "\n")
996 (expand-file-name
997 (concat (package-desc-full-name pkg-desc)
998 ".signed")
999 package-user-dir)
1000 nil 'silent)
1001 ;; Update the old pkg-desc which will be shown on the description buffer.
1002 (setf (package-desc-signed pkg-desc) t)
1003 ;; Update the new (activated) pkg-desc as well.
1004 (setq pkg-descs (cdr (assq (package-desc-name pkg-desc) package-alist)))
1005 (if pkg-descs
1006 (setf (package-desc-signed (car pkg-descs)) t)))))
1008 (defvar package--initialized nil)
1010 (defun package-installed-p (package &optional min-version)
1011 "Return true if PACKAGE, of MIN-VERSION or newer, is installed.
1012 If PACKAGE is a symbol, it is the package name and MIN-VERSION
1013 should be a version list.
1015 If PACKAGE is a package-desc object, MIN-VERSION is ignored."
1016 (unless package--initialized (error "package.el is not yet initialized!"))
1017 (if (package-desc-p package)
1018 (let ((dir (package-desc-dir package)))
1019 (and (stringp dir)
1020 (file-exists-p dir)))
1022 (let ((pkg-descs (cdr (assq package package-alist))))
1023 (and pkg-descs
1024 (version-list-<= min-version
1025 (package-desc-version (car pkg-descs)))))
1026 ;; Also check built-in packages.
1027 (package-built-in-p package min-version))))
1029 (defun package-compute-transaction (packages requirements &optional seen)
1030 "Return a list of packages to be installed, including PACKAGES.
1031 PACKAGES should be a list of `package-desc'.
1033 REQUIREMENTS should be a list of additional requirements; each
1034 element in this list should have the form (PACKAGE VERSION-LIST),
1035 where PACKAGE is a package name and VERSION-LIST is the required
1036 version of that package.
1038 This function recursively computes the requirements of the
1039 packages in REQUIREMENTS, and returns a list of all the packages
1040 that must be installed. Packages that are already installed are
1041 not included in this list.
1043 SEEN is used internally to detect infinite recursion."
1044 ;; FIXME: We really should use backtracking to explore the whole
1045 ;; search space (e.g. if foo require bar-1.3, and bar-1.4 requires toto-1.1
1046 ;; whereas bar-1.3 requires toto-1.0 and the user has put a hold on toto-1.0:
1047 ;; the current code might fail to see that it could install foo by using the
1048 ;; older bar-1.3).
1049 (dolist (elt requirements)
1050 (let* ((next-pkg (car elt))
1051 (next-version (cadr elt))
1052 (already ()))
1053 (dolist (pkg packages)
1054 (if (eq next-pkg (package-desc-name pkg))
1055 (setq already pkg)))
1056 (when already
1057 (if (version-list-<= next-version (package-desc-version already))
1058 ;; `next-pkg' is already in `packages', but its position there
1059 ;; means it might be installed too late: remove it from there, so
1060 ;; we re-add it (along with its dependencies) at an earlier place
1061 ;; below (bug#16994).
1062 (if (memq already seen) ;Avoid inf-loop on dependency cycles.
1063 (message "Dependency cycle going through %S"
1064 (package-desc-full-name already))
1065 (setq packages (delq already packages))
1066 (setq already nil))
1067 (error "Need package `%s-%s', but only %s is being installed"
1068 next-pkg (package-version-join next-version)
1069 (package-version-join (package-desc-version already)))))
1070 (cond
1071 (already nil)
1072 ((package-installed-p next-pkg next-version) nil)
1075 ;; A package is required, but not installed. It might also be
1076 ;; blocked via `package-load-list'.
1077 (let ((pkg-descs (cdr (assq next-pkg package-archive-contents)))
1078 (found nil)
1079 (problem nil))
1080 (while (and pkg-descs (not found))
1081 (let* ((pkg-desc (pop pkg-descs))
1082 (version (package-desc-version pkg-desc))
1083 (disabled (package-disabled-p next-pkg version)))
1084 (cond
1085 ((version-list-< version next-version)
1086 (error
1087 "Need package `%s-%s', but only %s is available"
1088 next-pkg (package-version-join next-version)
1089 (package-version-join version)))
1090 (disabled
1091 (unless problem
1092 (setq problem
1093 (if (stringp disabled)
1094 (format "Package `%s' held at version %s, \
1095 but version %s required"
1096 next-pkg disabled
1097 (package-version-join next-version))
1098 (format "Required package '%s' is disabled"
1099 next-pkg)))))
1100 (t (setq found pkg-desc)))))
1101 (unless found
1102 (if problem
1103 (error "%s" problem)
1104 (error "Package `%s-%s' is unavailable"
1105 next-pkg (package-version-join next-version))))
1106 (setq packages
1107 (package-compute-transaction (cons found packages)
1108 (package-desc-reqs found)
1109 (cons found seen))))))))
1110 packages)
1112 (defun package-read-from-string (str)
1113 "Read a Lisp expression from STR.
1114 Signal an error if the entire string was not used."
1115 (let* ((read-data (read-from-string str))
1116 (more-left
1117 (condition-case nil
1118 ;; The call to `ignore' suppresses a compiler warning.
1119 (progn (ignore (read-from-string
1120 (substring str (cdr read-data))))
1122 (end-of-file nil))))
1123 (if more-left
1124 (error "Can't read whole string")
1125 (car read-data))))
1127 (defun package--read-archive-file (file)
1128 "Re-read archive file FILE, if it exists.
1129 Will return the data from the file, or nil if the file does not exist.
1130 Will throw an error if the archive version is too new."
1131 (let ((filename (expand-file-name file package-user-dir)))
1132 (when (file-exists-p filename)
1133 (with-temp-buffer
1134 (insert-file-contents-literally filename)
1135 (let ((contents (read (current-buffer))))
1136 (if (> (car contents) package-archive-version)
1137 (error "Package archive version %d is higher than %d"
1138 (car contents) package-archive-version))
1139 (cdr contents))))))
1141 (defun package-read-all-archive-contents ()
1142 "Re-read `archive-contents', if it exists.
1143 If successful, set `package-archive-contents'."
1144 (setq package-archive-contents nil)
1145 (dolist (archive package-archives)
1146 (package-read-archive-contents (car archive))))
1148 (defun package-read-archive-contents (archive)
1149 "Re-read archive contents for ARCHIVE.
1150 If successful, set the variable `package-archive-contents'.
1151 If the archive version is too new, signal an error."
1152 ;; Version 1 of 'archive-contents' is identical to our internal
1153 ;; representation.
1154 (let* ((contents-file (format "archives/%s/archive-contents" archive))
1155 (contents (package--read-archive-file contents-file)))
1156 (when contents
1157 (dolist (package contents)
1158 (package--add-to-archive-contents package archive)))))
1160 ;; Package descriptor objects used inside the "archive-contents" file.
1161 ;; Changing this defstruct implies changing the format of the
1162 ;; "archive-contents" files.
1163 (cl-defstruct (package--ac-desc
1164 (:constructor package-make-ac-desc (version reqs summary kind extras))
1165 (:copier nil)
1166 (:type vector))
1167 version reqs summary kind extras)
1169 (defun package--add-to-archive-contents (package archive)
1170 "Add the PACKAGE from the given ARCHIVE if necessary.
1171 PACKAGE should have the form (NAME . PACKAGE--AC-DESC).
1172 Also, add the originating archive to the `package-desc' structure."
1173 (let* ((name (car package))
1174 (version (package--ac-desc-version (cdr package)))
1175 (pkg-desc
1176 (package-desc-create
1177 :name name
1178 :version version
1179 :reqs (package--ac-desc-reqs (cdr package))
1180 :summary (package--ac-desc-summary (cdr package))
1181 :kind (package--ac-desc-kind (cdr package))
1182 :archive archive
1183 :extras (and (> (length (cdr package)) 4)
1184 ;; Older archive-contents files have only 4
1185 ;; elements here.
1186 (package--ac-desc-extras (cdr package)))))
1187 (pinned-to-archive (assoc name package-pinned-packages)))
1188 ;; Skip entirely if pinned to another archive.
1189 (when (not (and pinned-to-archive
1190 (not (equal (cdr pinned-to-archive) archive))))
1191 (setq package-archive-contents
1192 (package--append-to-alist pkg-desc package-archive-contents)))))
1194 (defun package--append-to-alist (pkg-desc alist)
1195 "Append an entry for PKG-DESC to the start of ALIST and return it.
1196 This entry takes the form (`package-desc-name' PKG-DESC).
1198 If ALIST already has an entry with this name, destructively add
1199 PKG-DESC to the cdr of this entry instead, sorted by version
1200 number."
1201 (let* ((name (package-desc-name pkg-desc))
1202 (priority-version (package-desc-priority-version pkg-desc))
1203 (existing-packages (assq name alist)))
1204 (if (not existing-packages)
1205 (cons (list name pkg-desc)
1206 alist)
1207 (while (if (and (cdr existing-packages)
1208 (version-list-< priority-version
1209 (package-desc-priority-version
1210 (cadr existing-packages))))
1211 (setq existing-packages (cdr existing-packages))
1212 (push pkg-desc (cdr existing-packages))
1213 nil))
1214 alist)))
1216 (defun package--user-selected-p (pkg)
1217 "Return non-nil if PKG is a package was installed by the user.
1218 PKG is a package name.
1219 This looks into `package-selected-packages', populating it first
1220 if it is still empty."
1221 (unless (consp package-selected-packages)
1222 (customize-save-variable
1223 'package-selected-packages
1224 (setq package-selected-packages (package--find-non-dependencies))))
1225 (memq pkg package-selected-packages))
1227 (defun package-download-transaction (packages)
1228 "Download and install all the packages in PACKAGES.
1229 PACKAGES should be a list of package-desc.
1230 This function assumes that all package requirements in
1231 PACKAGES are satisfied, i.e. that PACKAGES is computed
1232 using `package-compute-transaction'."
1233 (mapc #'package-install-from-archive packages))
1235 ;;;###autoload
1236 (defun package-install (pkg &optional dont-select)
1237 "Install the package PKG.
1238 PKG can be a package-desc or the package name of one the available packages
1239 in an archive in `package-archives'. Interactively, prompt for its name.
1241 If called interactively or if DONT-SELECT nil, add PKG to
1242 `package-selected-packages'.
1244 If PKG is a package-desc and it is already installed, don't try
1245 to install it but still mark it as selected."
1246 (interactive
1247 (progn
1248 ;; Initialize the package system to get the list of package
1249 ;; symbols for completion.
1250 (unless package--initialized
1251 (package-initialize t))
1252 (unless package-archive-contents
1253 (package-refresh-contents))
1254 (list (intern (completing-read
1255 "Install package: "
1256 (delq nil
1257 (mapcar (lambda (elt)
1258 (unless (package-installed-p (car elt))
1259 (symbol-name (car elt))))
1260 package-archive-contents))
1261 nil t))
1262 nil)))
1263 (let ((name (if (package-desc-p pkg)
1264 (package-desc-name pkg)
1265 pkg)))
1266 (unless (or dont-select (package--user-selected-p name))
1267 (customize-save-variable 'package-selected-packages
1268 (cons name package-selected-packages))))
1269 (if (package-desc-p pkg)
1270 (if (package-installed-p pkg)
1271 (message "`%s' is already installed" (package-desc-full-name pkg))
1272 (package-download-transaction
1273 (package-compute-transaction (list pkg)
1274 (package-desc-reqs pkg))))
1275 (package-download-transaction
1276 (package-compute-transaction ()
1277 (list (list pkg))))))
1279 ;;;###autoload
1280 (defun package-reinstall (pkg)
1281 "Reinstall package PKG.
1282 PKG should be either a symbol, the package name, or a package-desc
1283 object."
1284 (interactive (list (intern (completing-read
1285 "Reinstall package: "
1286 (mapcar #'symbol-name
1287 (mapcar #'car package-alist))))))
1288 (package-delete
1289 (if (package-desc-p pkg) pkg (cadr (assq pkg package-alist)))
1290 'force 'nosave)
1291 (package-install pkg 'dont-select))
1293 (defun package-strip-rcs-id (str)
1294 "Strip RCS version ID from the version string STR.
1295 If the result looks like a dotted numeric version, return it.
1296 Otherwise return nil."
1297 (when str
1298 (when (string-match "\\`[ \t]*[$]Revision:[ \t]+" str)
1299 (setq str (substring str (match-end 0))))
1300 (condition-case nil
1301 (if (version-to-list str)
1302 str)
1303 (error nil))))
1305 (declare-function lm-homepage "lisp-mnt" (&optional file))
1307 (defun package--prepare-dependencies (deps)
1308 "Turn DEPS into an acceptable list of dependencies.
1310 Any parts missing a version string get a default version string
1311 of \"0\" (meaning any version) and an appropriate level of lists
1312 is wrapped around any parts requiring it."
1313 (cond
1314 ((not (listp deps))
1315 (error "Invalid requirement specifier: %S" deps))
1316 (t (mapcar (lambda (dep)
1317 (cond
1318 ((symbolp dep) `(,dep "0"))
1319 ((stringp dep)
1320 (error "Invalid requirement specifier: %S" dep))
1321 ((and (listp dep) (null (cdr dep)))
1322 (list (car dep) "0"))
1323 (t dep)))
1324 deps))))
1326 (defun package-buffer-info ()
1327 "Return a `package-desc' describing the package in the current buffer.
1329 If the buffer does not contain a conforming package, signal an
1330 error. If there is a package, narrow the buffer to the file's
1331 boundaries."
1332 (goto-char (point-min))
1333 (unless (re-search-forward "^;;; \\([^ ]*\\)\\.el ---[ \t]*\\(.*?\\)[ \t]*\\(-\\*-.*-\\*-[ \t]*\\)?$" nil t)
1334 (error "Package lacks a file header"))
1335 (let ((file-name (match-string-no-properties 1))
1336 (desc (match-string-no-properties 2))
1337 (start (line-beginning-position)))
1338 (unless (search-forward (concat ";;; " file-name ".el ends here"))
1339 (error "Package lacks a terminating comment"))
1340 ;; Try to include a trailing newline.
1341 (forward-line)
1342 (narrow-to-region start (point))
1343 (require 'lisp-mnt)
1344 ;; Use some headers we've invented to drive the process.
1345 (let* ((requires-str (lm-header "package-requires"))
1346 ;; Prefer Package-Version; if defined, the package author
1347 ;; probably wants us to use it. Otherwise try Version.
1348 (pkg-version
1349 (or (package-strip-rcs-id (lm-header "package-version"))
1350 (package-strip-rcs-id (lm-header "version"))))
1351 (homepage (lm-homepage)))
1352 (unless pkg-version
1353 (error
1354 "Package lacks a \"Version\" or \"Package-Version\" header"))
1355 (package-desc-from-define
1356 file-name pkg-version desc
1357 (if requires-str
1358 (package--prepare-dependencies
1359 (package-read-from-string requires-str)))
1360 :kind 'single
1361 :url homepage))))
1363 (declare-function tar-get-file-descriptor "tar-mode" (file))
1364 (declare-function tar--extract "tar-mode" (descriptor))
1366 (defun package-tar-file-info ()
1367 "Find package information for a tar file.
1368 The return result is a `package-desc'."
1369 (cl-assert (derived-mode-p 'tar-mode))
1370 (let* ((dir-name (file-name-directory
1371 (tar-header-name (car tar-parse-info))))
1372 (desc-file (package--description-file dir-name))
1373 (tar-desc (tar-get-file-descriptor (concat dir-name desc-file))))
1374 (unless tar-desc
1375 (error "No package descriptor file found"))
1376 (with-current-buffer (tar--extract tar-desc)
1377 (unwind-protect
1378 (or (package--read-pkg-desc 'tar)
1379 (error "Can't find define-package in %s"
1380 (tar-header-name tar-desc)))
1381 (kill-buffer (current-buffer))))))
1383 (defun package-dir-info ()
1384 "Find package information for a directory.
1385 The return result is a `package-desc'."
1386 (cl-assert (derived-mode-p 'dired-mode))
1387 (let* ((desc-file (package--description-file default-directory)))
1388 (if (file-readable-p desc-file)
1389 (with-temp-buffer
1390 (insert-file-contents desc-file)
1391 (package--read-pkg-desc 'dir))
1392 (let ((files (directory-files default-directory t "\\.el\\'" t))
1393 info)
1394 (while files
1395 (with-temp-buffer
1396 (insert-file-contents (pop files))
1397 ;; When we find the file with the data,
1398 (when (setq info (ignore-errors (package-buffer-info)))
1399 ;; stop looping,
1400 (setq files nil)
1401 ;; set the 'dir kind,
1402 (setf (package-desc-kind info) 'dir))))
1403 ;; and return the info.
1404 info))))
1406 (defun package--read-pkg-desc (kind)
1407 "Read a `define-package' form in current buffer.
1408 Return the pkg-desc, with desc-kind set to KIND."
1409 (goto-char (point-min))
1410 (unwind-protect
1411 (let* ((pkg-def-parsed (read (current-buffer)))
1412 (pkg-desc
1413 (when (eq (car pkg-def-parsed) 'define-package)
1414 (apply #'package-desc-from-define
1415 (append (cdr pkg-def-parsed))))))
1416 (when pkg-desc
1417 (setf (package-desc-kind pkg-desc) kind)
1418 pkg-desc))))
1421 ;;;###autoload
1422 (defun package-install-from-buffer ()
1423 "Install a package from the current buffer.
1424 The current buffer is assumed to be a single .el or .tar file or
1425 a directory. These must follow the packaging guidelines (see
1426 info node `(elisp)Packaging').
1428 Specially, if current buffer is a directory, the -pkg.el
1429 description file is not mandatory, in which case the information
1430 is derived from the main .el file in the directory.
1432 Downloads and installs required packages as needed."
1433 (interactive)
1434 (let* ((pkg-desc
1435 (cond
1436 ((derived-mode-p 'dired-mode)
1437 ;; This is the only way a package-desc object with a `dir'
1438 ;; desc-kind can be created. Such packages can't be
1439 ;; uploaded or installed from archives, they can only be
1440 ;; installed from local buffers or directories.
1441 (package-dir-info))
1442 ((derived-mode-p 'tar-mode)
1443 (package-tar-file-info))
1445 (package-buffer-info))))
1446 (name (package-desc-name pkg-desc)))
1447 ;; Download and install the dependencies.
1448 (let* ((requires (package-desc-reqs pkg-desc))
1449 (transaction (package-compute-transaction nil requires)))
1450 (package-download-transaction transaction))
1451 ;; Install the package itself.
1452 (package-unpack pkg-desc)
1453 (unless (package--user-selected-p name)
1454 (customize-save-variable 'package-selected-packages
1455 (cons name package-selected-packages)))
1456 pkg-desc))
1458 ;;;###autoload
1459 (defun package-install-file (file)
1460 "Install a package from a file.
1461 The file can either be a tar file or an Emacs Lisp file."
1462 (interactive "fPackage file name: ")
1463 (with-temp-buffer
1464 (if (file-directory-p file)
1465 (progn
1466 (setq default-directory file)
1467 (dired-mode))
1468 (insert-file-contents-literally file)
1469 (when (string-match "\\.tar\\'" file) (tar-mode)))
1470 (package-install-from-buffer)))
1472 (defun package--get-deps (pkg &optional only)
1473 (let* ((pkg-desc (cadr (assq pkg package-alist)))
1474 (direct-deps (cl-loop for p in (package-desc-reqs pkg-desc)
1475 for name = (car p)
1476 when (assq name package-alist)
1477 collect name))
1478 (indirect-deps (unless (eq only 'direct)
1479 (delete-dups
1480 (cl-loop for p in direct-deps
1481 append (package--get-deps p))))))
1482 (cl-case only
1483 (direct direct-deps)
1484 (separate (list direct-deps indirect-deps))
1485 (indirect indirect-deps)
1486 (t (delete-dups (append direct-deps indirect-deps))))))
1488 ;;;###autoload
1489 (defun package-install-user-selected-packages ()
1490 "Ensure packages in `package-selected-packages' are installed.
1491 If some packages are not installed propose to install them."
1492 (interactive)
1493 ;; We don't need to populate `package-selected-packages' before
1494 ;; using here, because the outcome is the same either way (nothing
1495 ;; gets installed).
1496 (if (not package-selected-packages)
1497 (message "`package-selected-packages' is empty, nothing to install")
1498 (cl-loop for p in package-selected-packages
1499 unless (package-installed-p p)
1500 collect p into lst
1501 finally
1502 (if lst
1503 (when (y-or-n-p
1504 (format "%s packages will be installed:\n%s, proceed?"
1505 (length lst)
1506 (mapconcat #'symbol-name lst ", ")))
1507 (mapc #'package-install lst))
1508 (message "All your packages are already installed")))))
1510 (defun package--used-elsewhere-p (pkg-desc &optional pkg-list)
1511 "Non-nil if PKG-DESC is a dependency of a package in PKG-LIST.
1512 Return the first package found in PKG-LIST of which PKG is a
1513 dependency.
1515 When not specified, PKG-LIST defaults to `package-alist'
1516 with PKG-DESC entry removed."
1517 (unless (string= (package-desc-status pkg-desc) "obsolete")
1518 (let ((pkg (package-desc-name pkg-desc)))
1519 (cl-loop with alist = (or pkg-list
1520 (remove (assq pkg package-alist)
1521 package-alist))
1522 for p in alist thereis
1523 (and (memq pkg (mapcar #'car (package-desc-reqs (cadr p))))
1524 (car p))))))
1526 (defun package--newest-p (pkg)
1527 "Return t if PKG is the newest package with its name."
1528 (equal (cadr (assq (package-desc-name pkg) package-alist))
1529 pkg))
1531 (defun package-delete (pkg-desc &optional force nosave)
1532 "Delete package PKG-DESC.
1534 Argument PKG-DESC is a full description of package as vector.
1535 When package is used elsewhere as dependency of another package,
1536 refuse deleting it and return an error.
1537 If FORCE is non-nil package will be deleted even if it is used
1538 elsewhere.
1539 If NOSAVE is non-nil, the package is not removed from
1540 `package-selected-packages'."
1541 (let ((dir (package-desc-dir pkg-desc))
1542 (name (package-desc-name pkg-desc))
1543 pkg-used-elsewhere-by)
1544 ;; If the user is trying to delete this package, they definitely
1545 ;; don't want it marked as selected, so we remove it from
1546 ;; `package-selected-packages' even if it can't be deleted.
1547 (when (and (null nosave)
1548 (package--user-selected-p name)
1549 ;; Don't deselect if this is an older version of an
1550 ;; upgraded package.
1551 (package--newest-p pkg-desc))
1552 (customize-save-variable
1553 'package-selected-packages (remove name package-selected-packages)))
1554 (cond ((not (string-prefix-p (file-name-as-directory
1555 (expand-file-name package-user-dir))
1556 (expand-file-name dir)))
1557 ;; Don't delete "system" packages.
1558 (error "Package `%s' is a system package, not deleting"
1559 (package-desc-full-name pkg-desc)))
1560 ((and (null force)
1561 (setq pkg-used-elsewhere-by
1562 (package--used-elsewhere-p pkg-desc)))
1563 ;; Don't delete packages used as dependency elsewhere.
1564 (error "Package `%s' is used by `%s' as dependency, not deleting"
1565 (package-desc-full-name pkg-desc)
1566 pkg-used-elsewhere-by))
1568 (delete-directory dir t t)
1569 ;; Remove NAME-VERSION.signed file.
1570 (let ((signed-file (concat dir ".signed")))
1571 (if (file-exists-p signed-file)
1572 (delete-file signed-file)))
1573 ;; Update package-alist.
1574 (let ((pkgs (assq name package-alist)))
1575 (delete pkg-desc pkgs)
1576 (unless (cdr pkgs)
1577 (setq package-alist (delq pkgs package-alist))))
1578 (message "Package `%s' deleted." (package-desc-full-name pkg-desc))))))
1580 (defun package--removable-packages ()
1581 "Return a list of names of packages no longer needed.
1582 These are packages which are neither contained in
1583 `package-selected-packages' nor a dependency of one that is."
1584 (let ((needed (cl-loop for p in package-selected-packages
1585 if (assq p package-alist)
1586 ;; `p' and its dependencies are needed.
1587 append (cons p (package--get-deps p)))))
1588 (cl-loop for p in (mapcar #'car package-alist)
1589 unless (memq p needed)
1590 collect p)))
1592 ;;;###autoload
1593 (defun package-autoremove ()
1594 "Remove packages that are no more needed.
1596 Packages that are no more needed by other packages in
1597 `package-selected-packages' and their dependencies
1598 will be deleted."
1599 (interactive)
1600 ;; If `package-selected-packages' is nil, it would make no sense to
1601 ;; try to populate it here, because then `package-autoremove' will
1602 ;; do absolutely nothing.
1603 (when (or package-selected-packages
1604 (yes-or-no-p
1605 "`package-selected-packages' is empty! Really remove ALL packages? "))
1606 (let ((removable (package--removable-packages)))
1607 (if removable
1608 (when (y-or-n-p
1609 (format "%s packages will be deleted:\n%s, proceed? "
1610 (length removable)
1611 (mapconcat #'symbol-name removable ", ")))
1612 (mapc (lambda (p)
1613 (package-delete (cadr (assq p package-alist)) t))
1614 removable))
1615 (message "Nothing to autoremove")))))
1617 (defun package-archive-base (desc)
1618 "Return the archive containing the package NAME."
1619 (cdr (assoc (package-desc-archive desc) package-archives)))
1621 (defun package-archive-priority (archive)
1622 "Return the priority of ARCHIVE.
1624 The archive priorities are specified in
1625 `package-archive-priorities'. If not given there, the priority
1626 defaults to 0."
1627 (or (cdr (assoc archive package-archive-priorities))
1630 (defun package-desc-priority-version (pkg-desc)
1631 "Return the version PKG-DESC with the archive priority prepended.
1633 This allows for easy comparison of package versions from
1634 different archives if archive priorities are meant to be taken in
1635 consideration."
1636 (cons (package-archive-priority
1637 (package-desc-archive pkg-desc))
1638 (package-desc-version pkg-desc)))
1640 (defun package--download-one-archive (archive file)
1641 "Retrieve an archive file FILE from ARCHIVE, and cache it.
1642 ARCHIVE should be a cons cell of the form (NAME . LOCATION),
1643 similar to an entry in `package-alist'. Save the cached copy to
1644 \"archives/NAME/archive-contents\" in `package-user-dir'."
1645 (let ((dir (expand-file-name (format "archives/%s" (car archive))
1646 package-user-dir))
1647 (sig-file (concat file ".sig"))
1648 good-signatures)
1649 (package--with-work-buffer (cdr archive) file
1650 ;; Check signature of archive-contents, if desired.
1651 (if (and package-check-signature
1652 (not (member archive package-unsigned-archives)))
1653 (if (package--archive-file-exists-p (cdr archive) sig-file)
1654 (setq good-signatures (package--check-signature (cdr archive)
1655 file))
1656 (unless (eq package-check-signature 'allow-unsigned)
1657 (error "Unsigned archive `%s'"
1658 (car archive)))))
1659 ;; Read the retrieved buffer to make sure it is valid (e.g. it
1660 ;; may fetch a URL redirect page).
1661 (when (listp (read (current-buffer)))
1662 (make-directory dir t)
1663 (write-region nil nil (expand-file-name file dir) nil 'silent)))
1664 (when good-signatures
1665 ;; Write out good signatures into archive-contents.signed file.
1666 (write-region (mapconcat #'epg-signature-to-string good-signatures "\n")
1668 (expand-file-name (concat file ".signed") dir)
1669 nil 'silent))))
1671 (declare-function epg-check-configuration "epg-config"
1672 (config &optional minimum-version))
1673 (declare-function epg-configuration "epg-config" ())
1674 (declare-function epg-import-keys-from-file "epg" (context keys))
1676 ;;;###autoload
1677 (defun package-import-keyring (&optional file)
1678 "Import keys from FILE."
1679 (interactive "fFile: ")
1680 (setq file (expand-file-name file))
1681 (let ((context (epg-make-context 'OpenPGP))
1682 (homedir (expand-file-name "gnupg" package-user-dir)))
1683 (with-file-modes 448
1684 (make-directory homedir t))
1685 (setf (epg-context-home-directory context) homedir)
1686 (message "Importing %s..." (file-name-nondirectory file))
1687 (epg-import-keys-from-file context file)
1688 (message "Importing %s...done" (file-name-nondirectory file))))
1690 (defun package--build-compatibility-table ()
1691 "Build `package--compatibility-table' with `package--mapc'."
1692 ;; Build compat table.
1693 (setq package--compatibility-table (make-hash-table :test 'eq))
1694 (package--mapc #'package--add-to-compatibility-table))
1696 ;;;###autoload
1697 (defun package-refresh-contents ()
1698 "Download descriptions of all configured ELPA packages.
1699 For each archive configured in the variable `package-archives',
1700 inform Emacs about the latest versions of all packages it offers,
1701 and make them available for download."
1702 (interactive)
1703 ;; FIXME: Do it asynchronously.
1704 (unless (file-exists-p package-user-dir)
1705 (make-directory package-user-dir t))
1706 (let ((default-keyring (expand-file-name "package-keyring.gpg"
1707 data-directory)))
1708 (when (and package-check-signature (file-exists-p default-keyring))
1709 (condition-case-unless-debug error
1710 (progn
1711 (epg-check-configuration (epg-configuration))
1712 (package-import-keyring default-keyring))
1713 (error (message "Cannot import default keyring: %S" (cdr error))))))
1714 (dolist (archive package-archives)
1715 (condition-case-unless-debug nil
1716 (package--download-one-archive archive "archive-contents")
1717 (error (message "Failed to download `%s' archive."
1718 (car archive)))))
1719 (package-read-all-archive-contents)
1720 (package--build-compatibility-table)
1721 (message "Package refresh done"))
1723 (defun package--find-non-dependencies ()
1724 "Return a list of installed packages which are not dependencies.
1725 Finds all packages in `package-alist' which are not dependencies
1726 of any other packages.
1727 Used to populate `package-selected-packages'."
1728 (let ((dep-list
1729 (delete-dups
1730 (apply #'append
1731 (mapcar (lambda (p) (mapcar #'car (package-desc-reqs (cadr p))))
1732 package-alist)))))
1733 (cl-loop for p in package-alist
1734 for name = (car p)
1735 unless (memq name dep-list)
1736 collect name)))
1738 ;;;###autoload
1739 (defun package-initialize (&optional no-activate)
1740 "Load Emacs Lisp packages, and activate them.
1741 The variable `package-load-list' controls which packages to load.
1742 If optional arg NO-ACTIVATE is non-nil, don't activate packages."
1743 (interactive)
1744 (setq package-alist nil)
1745 (package-load-all-descriptors)
1746 (package-read-all-archive-contents)
1747 (unless no-activate
1748 (dolist (elt package-alist)
1749 (package-activate (car elt))))
1750 (setq package--initialized t)
1751 ;; This uses `package--mapc' so it must be called after
1752 ;; `package--initialized' is t.
1753 (package--build-compatibility-table))
1755 (defun package--add-to-compatibility-table (pkg)
1756 "If PKG is compatible (without dependencies), add to the compatibility table.
1757 PKG is a package-desc object.
1758 Only adds if its version is higher than what's already stored in
1759 the table."
1760 (unless (package--incompatible-p pkg 'shallow)
1761 (let* ((name (package-desc-name pkg))
1762 (version (or (package-desc-version pkg) '(0)))
1763 (table-version (gethash name package--compatibility-table)))
1764 (when (or (not table-version)
1765 (version-list-< table-version version))
1766 (puthash name version package--compatibility-table)))))
1769 ;;;; Package description buffer.
1771 ;;;###autoload
1772 (defun describe-package (package)
1773 "Display the full documentation of PACKAGE (a symbol)."
1774 (interactive
1775 (let* ((guess (function-called-at-point)))
1776 (require 'finder-inf nil t)
1777 ;; Load the package list if necessary (but don't activate them).
1778 (unless package--initialized
1779 (package-initialize t))
1780 (let ((packages (append (mapcar 'car package-alist)
1781 (mapcar 'car package-archive-contents)
1782 (mapcar 'car package--builtins))))
1783 (unless (memq guess packages)
1784 (setq guess nil))
1785 (setq packages (mapcar 'symbol-name packages))
1786 (let ((val
1787 (completing-read (if guess
1788 (format "Describe package (default %s): "
1789 guess)
1790 "Describe package: ")
1791 packages nil t nil nil guess)))
1792 (list (intern val))))))
1793 (if (not (or (package-desc-p package) (and package (symbolp package))))
1794 (message "No package specified")
1795 (help-setup-xref (list #'describe-package package)
1796 (called-interactively-p 'interactive))
1797 (with-help-window (help-buffer)
1798 (with-current-buffer standard-output
1799 (describe-package-1 package)))))
1801 (defun describe-package-1 (pkg)
1802 (require 'lisp-mnt)
1803 (let* ((desc (or
1804 (if (package-desc-p pkg) pkg)
1805 (cadr (assq pkg package-alist))
1806 (let ((built-in (assq pkg package--builtins)))
1807 (if built-in
1808 (package--from-builtin built-in)
1809 (cadr (assq pkg package-archive-contents))))))
1810 (name (if desc (package-desc-name desc) pkg))
1811 (pkg-dir (if desc (package-desc-dir desc)))
1812 (reqs (if desc (package-desc-reqs desc)))
1813 (version (if desc (package-desc-version desc)))
1814 (archive (if desc (package-desc-archive desc)))
1815 (extras (and desc (package-desc-extras desc)))
1816 (homepage (cdr (assoc :url extras)))
1817 (keywords (if desc (package-desc--keywords desc)))
1818 (built-in (eq pkg-dir 'builtin))
1819 (installable (and archive (not built-in)))
1820 (status (if desc (package-desc-status desc) "orphan"))
1821 (incompatible-reason (package--incompatible-p desc))
1822 (signed (if desc (package-desc-signed desc))))
1823 (when incompatible-reason
1824 (setq status "incompatible"))
1825 (prin1 name)
1826 (princ " is ")
1827 (princ (if (memq (aref status 0) '(?a ?e ?i ?o ?u)) "an " "a "))
1828 (princ status)
1829 (princ " package.\n\n")
1831 (insert " " (propertize "Status" 'font-lock-face 'bold) ": ")
1832 (cond (built-in
1833 (insert (propertize (capitalize status)
1834 'font-lock-face 'font-lock-builtin-face)
1835 "."))
1836 (pkg-dir
1837 (insert (propertize (if (member status '("unsigned" "dependency"))
1838 "Installed"
1839 (capitalize status)) ;FIXME: Why comment-face?
1840 'font-lock-face 'font-lock-comment-face))
1841 (insert " in `")
1842 ;; Todo: Add button for uninstalling.
1843 (help-insert-xref-button (abbreviate-file-name
1844 (file-name-as-directory pkg-dir))
1845 'help-package-def pkg-dir)
1846 (if (and (package-built-in-p name)
1847 (not (package-built-in-p name version)))
1848 (insert "',\n shadowing a "
1849 (propertize "built-in package"
1850 'font-lock-face 'font-lock-builtin-face))
1851 (insert "'"))
1852 (if signed
1853 (insert ".")
1854 (insert " (unsigned).")))
1855 (incompatible-reason
1856 (insert (propertize "Incompatible" 'face font-lock-warning-face)
1857 " because it depends on ")
1858 (if (stringp incompatible-reason)
1859 (insert "Emacs " incompatible-reason ".")
1860 (insert "uninstallable packages.")))
1861 (installable
1862 (insert (capitalize status))
1863 (insert " from " (format "%s" archive))
1864 (insert " -- ")
1865 (package-make-button
1866 "Install"
1867 'action 'package-install-button-action
1868 'package-desc desc))
1869 (t (insert (capitalize status) ".")))
1870 (insert "\n")
1871 (insert " " (propertize "Archive" 'font-lock-face 'bold)
1872 ": " (or archive "n/a") "\n")
1873 (and version
1874 (insert " "
1875 (propertize "Version" 'font-lock-face 'bold) ": "
1876 (package-version-join version) "\n"))
1878 (setq reqs (if desc (package-desc-reqs desc)))
1879 (when reqs
1880 (insert " " (propertize "Requires" 'font-lock-face 'bold) ": ")
1881 (let ((first t))
1882 (dolist (req reqs)
1883 (let* ((name (car req))
1884 (vers (cadr req))
1885 (text (format "%s-%s" (symbol-name name)
1886 (package-version-join vers)))
1887 (reason (if (and (listp incompatible-reason)
1888 (assq name incompatible-reason))
1889 " (not available)" "")))
1890 (cond (first (setq first nil))
1891 ((>= (+ 2 (current-column) (length text) (length reason))
1892 (window-width))
1893 (insert ",\n "))
1894 (t (insert ", ")))
1895 (help-insert-xref-button text 'help-package name)
1896 (insert reason)))
1897 (insert "\n")))
1898 (insert " " (propertize "Summary" 'font-lock-face 'bold)
1899 ": " (if desc (package-desc-summary desc)) "\n")
1900 (when homepage
1901 (insert " " (propertize "Homepage" 'font-lock-face 'bold) ": ")
1902 (help-insert-xref-button homepage 'help-url homepage)
1903 (insert "\n"))
1904 (when keywords
1905 (insert " " (propertize "Keywords" 'font-lock-face 'bold) ": ")
1906 (dolist (k keywords)
1907 (package-make-button
1909 'package-keyword k
1910 'action 'package-keyword-button-action)
1911 (insert " "))
1912 (insert "\n"))
1913 (let* ((all-pkgs (append (cdr (assq name package-alist))
1914 (cdr (assq name package-archive-contents))
1915 (let ((bi (assq name package--builtins)))
1916 (if bi (list (package--from-builtin bi))))))
1917 (other-pkgs (delete desc all-pkgs)))
1918 (when other-pkgs
1919 (insert " " (propertize "Other versions" 'font-lock-face 'bold) ": "
1920 (mapconcat
1921 (lambda (opkg)
1922 (let* ((ov (package-desc-version opkg))
1923 (dir (package-desc-dir opkg))
1924 (from (or (package-desc-archive opkg)
1925 (if (stringp dir) "installed" dir))))
1926 (if (not ov) (format "%s" from)
1927 (format "%s (%s)"
1928 (make-text-button (package-version-join ov) nil
1929 'face 'link
1930 'follow-link t
1931 'action
1932 (lambda (_button)
1933 (describe-package opkg)))
1934 from))))
1935 other-pkgs ", ")
1936 ".\n")))
1938 (insert "\n")
1940 (if built-in
1941 ;; For built-in packages, insert the commentary.
1942 (let ((fn (locate-file (format "%s.el" name) load-path
1943 load-file-rep-suffixes))
1944 (opoint (point)))
1945 (insert (or (lm-commentary fn) ""))
1946 (save-excursion
1947 (goto-char opoint)
1948 (when (re-search-forward "^;;; Commentary:\n" nil t)
1949 (replace-match ""))
1950 (while (re-search-forward "^\\(;+ ?\\)" nil t)
1951 (replace-match ""))))
1952 (let ((readme (expand-file-name (format "%s-readme.txt" name)
1953 package-user-dir))
1954 readme-string)
1955 ;; For elpa packages, try downloading the commentary. If that
1956 ;; fails, try an existing readme file in `package-user-dir'.
1957 (cond ((condition-case nil
1958 (save-excursion
1959 (package--with-work-buffer
1960 (package-archive-base desc)
1961 (format "%s-readme.txt" name)
1962 (save-excursion
1963 (goto-char (point-max))
1964 (unless (bolp)
1965 (insert ?\n)))
1966 (write-region nil nil
1967 (expand-file-name readme package-user-dir)
1968 nil 'silent)
1969 (setq readme-string (buffer-string))
1971 (error nil))
1972 (insert readme-string))
1973 ((file-readable-p readme)
1974 (insert-file-contents readme)
1975 (goto-char (point-max))))))))
1977 (defun package-install-button-action (button)
1978 (let ((pkg-desc (button-get button 'package-desc)))
1979 (when (y-or-n-p (format "Install package `%s'? "
1980 (package-desc-full-name pkg-desc)))
1981 (package-install pkg-desc nil)
1982 (revert-buffer nil t)
1983 (goto-char (point-min)))))
1985 (defun package-keyword-button-action (button)
1986 (let ((pkg-keyword (button-get button 'package-keyword)))
1987 (package-show-package-list t (list pkg-keyword))))
1989 (defun package-make-button (text &rest props)
1990 (let ((button-text (if (display-graphic-p) text (concat "[" text "]")))
1991 (button-face (if (display-graphic-p)
1992 '(:box (:line-width 2 :color "dark grey")
1993 :background "light grey"
1994 :foreground "black")
1995 'link)))
1996 (apply 'insert-text-button button-text 'face button-face 'follow-link t
1997 props)))
2000 ;;;; Package menu mode.
2002 (defvar package-menu-mode-map
2003 (let ((map (make-sparse-keymap))
2004 (menu-map (make-sparse-keymap "Package")))
2005 (set-keymap-parent map tabulated-list-mode-map)
2006 (define-key map "\C-m" 'package-menu-describe-package)
2007 (define-key map "u" 'package-menu-mark-unmark)
2008 (define-key map "\177" 'package-menu-backup-unmark)
2009 (define-key map "d" 'package-menu-mark-delete)
2010 (define-key map "i" 'package-menu-mark-install)
2011 (define-key map "U" 'package-menu-mark-upgrades)
2012 (define-key map "r" 'package-menu-refresh)
2013 (define-key map "f" 'package-menu-filter)
2014 (define-key map "~" 'package-menu-mark-obsolete-for-deletion)
2015 (define-key map "x" 'package-menu-execute)
2016 (define-key map "h" 'package-menu-quick-help)
2017 (define-key map "?" 'package-menu-describe-package)
2018 (define-key map [menu-bar package-menu] (cons "Package" menu-map))
2019 (define-key menu-map [mq]
2020 '(menu-item "Quit" quit-window
2021 :help "Quit package selection"))
2022 (define-key menu-map [s1] '("--"))
2023 (define-key menu-map [mn]
2024 '(menu-item "Next" next-line
2025 :help "Next Line"))
2026 (define-key menu-map [mp]
2027 '(menu-item "Previous" previous-line
2028 :help "Previous Line"))
2029 (define-key menu-map [s2] '("--"))
2030 (define-key menu-map [mu]
2031 '(menu-item "Unmark" package-menu-mark-unmark
2032 :help "Clear any marks on a package and move to the next line"))
2033 (define-key menu-map [munm]
2034 '(menu-item "Unmark Backwards" package-menu-backup-unmark
2035 :help "Back up one line and clear any marks on that package"))
2036 (define-key menu-map [md]
2037 '(menu-item "Mark for Deletion" package-menu-mark-delete
2038 :help "Mark a package for deletion and move to the next line"))
2039 (define-key menu-map [mi]
2040 '(menu-item "Mark for Install" package-menu-mark-install
2041 :help "Mark a package for installation and move to the next line"))
2042 (define-key menu-map [mupgrades]
2043 '(menu-item "Mark Upgradable Packages" package-menu-mark-upgrades
2044 :help "Mark packages that have a newer version for upgrading"))
2045 (define-key menu-map [s3] '("--"))
2046 (define-key menu-map [mf]
2047 '(menu-item "Filter Package List..." package-menu-filter
2048 :help "Filter package selection (q to go back)"))
2049 (define-key menu-map [mg]
2050 '(menu-item "Update Package List" revert-buffer
2051 :help "Update the list of packages"))
2052 (define-key menu-map [mr]
2053 '(menu-item "Refresh Package List" package-menu-refresh
2054 :help "Download the ELPA archive"))
2055 (define-key menu-map [s4] '("--"))
2056 (define-key menu-map [mt]
2057 '(menu-item "Mark Obsolete Packages" package-menu-mark-obsolete-for-deletion
2058 :help "Mark all obsolete packages for deletion"))
2059 (define-key menu-map [mx]
2060 '(menu-item "Execute Actions" package-menu-execute
2061 :help "Perform all the marked actions"))
2062 (define-key menu-map [s5] '("--"))
2063 (define-key menu-map [mh]
2064 '(menu-item "Help" package-menu-quick-help
2065 :help "Show short key binding help for package-menu-mode"))
2066 (define-key menu-map [mc]
2067 '(menu-item "Describe Package" package-menu-describe-package
2068 :help "Display information about this package"))
2069 map)
2070 "Local keymap for `package-menu-mode' buffers.")
2072 (defvar package-menu--new-package-list nil
2073 "List of newly-available packages since `list-packages' was last called.")
2075 (define-derived-mode package-menu-mode tabulated-list-mode "Package Menu"
2076 "Major mode for browsing a list of packages.
2077 Letters do not insert themselves; instead, they are commands.
2078 \\<package-menu-mode-map>
2079 \\{package-menu-mode-map}"
2080 (setq tabulated-list-format
2081 `[("Package" 18 package-menu--name-predicate)
2082 ("Version" 13 nil)
2083 ("Status" 10 package-menu--status-predicate)
2084 ,@(if (cdr package-archives)
2085 '(("Archive" 10 package-menu--archive-predicate)))
2086 ("Description" 0 nil)])
2087 (setq tabulated-list-padding 2)
2088 (setq tabulated-list-sort-key (cons "Status" nil))
2089 (add-hook 'tabulated-list-revert-hook 'package-menu--refresh nil t)
2090 (tabulated-list-init-header))
2092 (defmacro package--push (pkg-desc status listname)
2093 "Convenience macro for `package-menu--generate'.
2094 If the alist stored in the symbol LISTNAME lacks an entry for a
2095 package PKG-DESC, add one. The alist is keyed with PKG-DESC."
2096 `(unless (assoc ,pkg-desc ,listname)
2097 ;; FIXME: Should we move status into pkg-desc?
2098 (push (cons ,pkg-desc ,status) ,listname)))
2100 (defvar package-list-unversioned nil
2101 "If non-nil include packages that don't have a version in `list-package'.")
2103 (defvar package-list-unsigned nil
2104 "If non-nil, mention in the list which packages were installed w/o signature.")
2106 (defvar package--emacs-version-list (version-to-list emacs-version)
2107 "`emacs-version', as a list.")
2109 (defun package--incompatible-p (pkg &optional shallow)
2110 "Return non-nil if PKG has no chance of being installable.
2111 PKG is a package-desc object.
2113 If SHALLOW is non-nil, this only checks if PKG depends on a
2114 higher `emacs-version' than the one being used. Otherwise, also
2115 checks the viability of dependencies, according to
2116 `package--compatibility-table'.
2118 If PKG requires an incompatible Emacs version, the return value
2119 is this version (as a string).
2120 If PKG requires incompatible packages, the return value is a list
2121 of these dependencies, similar to the list returned by
2122 `package-desc-reqs'."
2123 (let* ((reqs (package-desc-reqs pkg))
2124 (version (cadr (assq 'emacs reqs))))
2125 (if (and version (version-list-< package--emacs-version-list version))
2126 (package-version-join version)
2127 (unless shallow
2128 (let (out)
2129 (dolist (dep (package-desc-reqs pkg) out)
2130 (let ((dep-name (car dep)))
2131 (unless (eq 'emacs dep-name)
2132 (let ((cv (gethash dep-name package--compatibility-table)))
2133 (when (version-list-< (or cv '(0)) (or (cadr dep) '(0)))
2134 (push dep out)))))))))))
2136 (defun package-desc-status (pkg-desc)
2137 (let* ((name (package-desc-name pkg-desc))
2138 (dir (package-desc-dir pkg-desc))
2139 (lle (assq name package-load-list))
2140 (held (cadr lle))
2141 (version (package-desc-version pkg-desc))
2142 (signed (or (not package-list-unsigned)
2143 (package-desc-signed pkg-desc))))
2144 (cond
2145 ((eq dir 'builtin) "built-in")
2146 ((and lle (null held)) "disabled")
2147 ((stringp held)
2148 (let ((hv (if (stringp held) (version-to-list held))))
2149 (cond
2150 ((version-list-= version hv) "held")
2151 ((version-list-< version hv) "obsolete")
2152 (t "disabled"))))
2153 ((package-built-in-p name version) "obsolete")
2154 ((package--incompatible-p pkg-desc) "incompat")
2155 (dir ;One of the installed packages.
2156 (cond
2157 ((not (file-exists-p (package-desc-dir pkg-desc))) "deleted")
2158 ((eq pkg-desc (cadr (assq name package-alist)))
2159 (if (not signed) "unsigned"
2160 (if (package--user-selected-p name)
2161 "installed" "dependency")))
2162 (t "obsolete")))
2164 (let* ((ins (cadr (assq name package-alist)))
2165 (ins-v (if ins (package-desc-version ins))))
2166 (cond
2167 ((or (null ins) (version-list-< ins-v version))
2168 (if (memq name package-menu--new-package-list)
2169 "new" "available"))
2170 ((version-list-< version ins-v) "obsolete")
2171 ((version-list-= version ins-v)
2172 (if (not signed) "unsigned"
2173 (if (package--user-selected-p name)
2174 "installed" "dependency")))))))))
2176 (defun package-menu--refresh (&optional packages keywords)
2177 "Re-populate the `tabulated-list-entries'.
2178 PACKAGES should be nil or t, which means to display all known packages.
2179 KEYWORDS should be nil or a list of keywords."
2180 ;; Construct list of (PKG-DESC . STATUS).
2181 (unless packages (setq packages t))
2182 (let (info-list name)
2183 ;; Installed packages:
2184 (dolist (elt package-alist)
2185 (setq name (car elt))
2186 (when (or (eq packages t) (memq name packages))
2187 (dolist (pkg (cdr elt))
2188 (when (package--has-keyword-p pkg keywords)
2189 (package--push pkg (package-desc-status pkg) info-list)))))
2191 ;; Built-in packages:
2192 (dolist (elt package--builtins)
2193 (setq name (car elt))
2194 (when (and (not (eq name 'emacs)) ; Hide the `emacs' package.
2195 (package--has-keyword-p (package--from-builtin elt) keywords)
2196 (or package-list-unversioned
2197 (package--bi-desc-version (cdr elt)))
2198 (or (eq packages t) (memq name packages)))
2199 (package--push (package--from-builtin elt) "built-in" info-list)))
2201 ;; Available and disabled packages:
2202 (dolist (elt package-archive-contents)
2203 (setq name (car elt))
2204 (when (or (eq packages t) (memq name packages))
2205 (dolist (pkg (cdr elt))
2206 ;; Hide obsolete packages.
2207 (when (and (not (package-installed-p (package-desc-name pkg)
2208 (package-desc-version pkg)))
2209 (package--has-keyword-p pkg keywords))
2210 (package--push pkg (package-desc-status pkg) info-list)))))
2212 ;; Print the result.
2213 (setq tabulated-list-entries
2214 (mapcar #'package-menu--print-info info-list))))
2216 (defun package-all-keywords ()
2217 "Collect all package keywords"
2218 (let (keywords)
2219 (package--mapc (lambda (desc)
2220 (let* ((desc-keywords (and desc (package-desc--keywords desc))))
2221 (setq keywords (append keywords desc-keywords)))))
2222 keywords))
2224 (defun package--mapc (function &optional packages)
2225 "Call FUNCTION for all known PACKAGES.
2226 PACKAGES can be nil or t, which means to display all known
2227 packages, or a list of packages.
2229 Built-in packages are converted with `package--from-builtin'."
2230 (unless packages (setq packages t))
2231 (let (name)
2232 ;; Installed packages:
2233 (dolist (elt package-alist)
2234 (setq name (car elt))
2235 (when (or (eq packages t) (memq name packages))
2236 (mapc function (cdr elt))))
2238 ;; Built-in packages:
2239 (dolist (elt package--builtins)
2240 (setq name (car elt))
2241 (when (and (not (eq name 'emacs)) ; Hide the `emacs' package.
2242 (or package-list-unversioned
2243 (package--bi-desc-version (cdr elt)))
2244 (or (eq packages t) (memq name packages)))
2245 (funcall function (package--from-builtin elt))))
2247 ;; Available and disabled packages:
2248 (dolist (elt package-archive-contents)
2249 (setq name (car elt))
2250 (when (or (eq packages t) (memq name packages))
2251 (dolist (pkg (cdr elt))
2252 ;; Hide obsolete packages.
2253 (unless (package-installed-p (package-desc-name pkg)
2254 (package-desc-version pkg))
2255 (funcall function pkg)))))))
2257 (defun package--has-keyword-p (desc &optional keywords)
2258 "Test if package DESC has any of the given KEYWORDS.
2259 When none are given, the package matches."
2260 (if keywords
2261 (let* ((desc-keywords (and desc (package-desc--keywords desc)))
2262 found)
2263 (dolist (k keywords)
2264 (when (and (not found)
2265 (member k desc-keywords))
2266 (setq found t)))
2267 found)
2270 (defun package-menu--generate (remember-pos packages &optional keywords)
2271 "Populate the Package Menu.
2272 If REMEMBER-POS is non-nil, keep point on the same entry.
2273 PACKAGES should be t, which means to display all known packages,
2274 or a list of package names (symbols) to display.
2276 With KEYWORDS given, only packages with those keywords are
2277 shown."
2278 (package-menu--refresh packages keywords)
2279 (setf (car (aref tabulated-list-format 0))
2280 (if keywords
2281 (let ((filters (mapconcat 'identity keywords ",")))
2282 (concat "Package[" filters "]"))
2283 "Package"))
2284 (if keywords
2285 (define-key package-menu-mode-map "q" 'package-show-package-list)
2286 (define-key package-menu-mode-map "q" 'quit-window))
2287 (tabulated-list-init-header)
2288 (tabulated-list-print remember-pos))
2290 (defun package-menu--print-info (pkg)
2291 "Return a package entry suitable for `tabulated-list-entries'.
2292 PKG has the form (PKG-DESC . STATUS).
2293 Return (PKG-DESC [NAME VERSION STATUS DOC])."
2294 (let* ((pkg-desc (car pkg))
2295 (status (cdr pkg))
2296 (face (pcase status
2297 (`"built-in" 'font-lock-builtin-face)
2298 (`"available" 'default)
2299 (`"new" 'bold)
2300 (`"held" 'font-lock-constant-face)
2301 (`"disabled" 'font-lock-warning-face)
2302 (`"installed" 'font-lock-comment-face)
2303 (`"dependency" 'font-lock-comment-face)
2304 (`"unsigned" 'font-lock-warning-face)
2305 (`"incompat" 'font-lock-comment-face)
2306 (_ 'font-lock-warning-face)))) ; obsolete.
2307 (list pkg-desc
2308 `[,(list (symbol-name (package-desc-name pkg-desc))
2309 'face 'link
2310 'follow-link t
2311 'package-desc pkg-desc
2312 'action 'package-menu-describe-package)
2313 ,(propertize (package-version-join
2314 (package-desc-version pkg-desc))
2315 'font-lock-face face)
2316 ,(propertize status 'font-lock-face face)
2317 ,@(if (cdr package-archives)
2318 (list (propertize (or (package-desc-archive pkg-desc) "")
2319 'font-lock-face face)))
2320 ,(propertize (package-desc-summary pkg-desc)
2321 'font-lock-face face)])))
2323 (defun package-menu-refresh ()
2324 "Download the Emacs Lisp package archive.
2325 This fetches the contents of each archive specified in
2326 `package-archives', and then refreshes the package menu."
2327 (interactive)
2328 (unless (derived-mode-p 'package-menu-mode)
2329 (user-error "The current buffer is not a Package Menu"))
2330 (package-refresh-contents)
2331 (package-menu--generate t t))
2333 (defun package-menu-describe-package (&optional button)
2334 "Describe the current package.
2335 If optional arg BUTTON is non-nil, describe its associated package."
2336 (interactive)
2337 (let ((pkg-desc (if button (button-get button 'package-desc)
2338 (tabulated-list-get-id))))
2339 (if pkg-desc
2340 (describe-package pkg-desc)
2341 (user-error "No package here"))))
2343 ;; fixme numeric argument
2344 (defun package-menu-mark-delete (&optional _num)
2345 "Mark a package for deletion and move to the next line."
2346 (interactive "p")
2347 (if (member (package-menu-get-status)
2348 '("installed" "dependency" "obsolete" "unsigned"))
2349 (tabulated-list-put-tag "D" t)
2350 (forward-line)))
2352 (defun package-menu-mark-install (&optional _num)
2353 "Mark a package for installation and move to the next line."
2354 (interactive "p")
2355 (if (member (package-menu-get-status) '("available" "new" "dependency"))
2356 (tabulated-list-put-tag "I" t)
2357 (forward-line)))
2359 (defun package-menu-mark-unmark (&optional _num)
2360 "Clear any marks on a package and move to the next line."
2361 (interactive "p")
2362 (tabulated-list-put-tag " " t))
2364 (defun package-menu-backup-unmark ()
2365 "Back up one line and clear any marks on that package."
2366 (interactive)
2367 (forward-line -1)
2368 (tabulated-list-put-tag " "))
2370 (defun package-menu-mark-obsolete-for-deletion ()
2371 "Mark all obsolete packages for deletion."
2372 (interactive)
2373 (save-excursion
2374 (goto-char (point-min))
2375 (while (not (eobp))
2376 (if (equal (package-menu-get-status) "obsolete")
2377 (tabulated-list-put-tag "D" t)
2378 (forward-line 1)))))
2380 (defun package-menu-quick-help ()
2381 "Show short key binding help for package-menu-mode."
2382 (interactive)
2383 (message "n-ext, i-nstall, d-elete, u-nmark, x-ecute, r-efresh, h-elp"))
2385 (define-obsolete-function-alias
2386 'package-menu-view-commentary 'package-menu-describe-package "24.1")
2388 (defun package-menu-get-status ()
2389 (let* ((id (tabulated-list-get-id))
2390 (entry (and id (assq id tabulated-list-entries))))
2391 (if entry
2392 (aref (cadr entry) 2)
2393 "")))
2395 (defun package-menu--find-upgrades ()
2396 (let (installed available upgrades)
2397 ;; Build list of installed/available packages in this buffer.
2398 (dolist (entry tabulated-list-entries)
2399 ;; ENTRY is (PKG-DESC [NAME VERSION STATUS DOC])
2400 (let ((pkg-desc (car entry))
2401 (status (aref (cadr entry) 2)))
2402 (cond ((member status '("installed" "dependency" "unsigned"))
2403 (push pkg-desc installed))
2404 ((member status '("available" "new"))
2405 (setq available (package--append-to-alist pkg-desc available))))))
2406 ;; Loop through list of installed packages, finding upgrades.
2407 (dolist (pkg-desc installed)
2408 (let* ((name (package-desc-name pkg-desc))
2409 (avail-pkg (cadr (assq name available))))
2410 (and avail-pkg
2411 (version-list-< (package-desc-priority-version pkg-desc)
2412 (package-desc-priority-version avail-pkg))
2413 (push (cons name avail-pkg) upgrades))))
2414 upgrades))
2416 (defun package-menu-mark-upgrades ()
2417 "Mark all upgradable packages in the Package Menu.
2418 For each installed package with a newer version available, place
2419 an (I)nstall flag on the available version and a (D)elete flag on
2420 the installed version. A subsequent \\[package-menu-execute]
2421 call will upgrade the package."
2422 (interactive)
2423 (unless (derived-mode-p 'package-menu-mode)
2424 (error "The current buffer is not a Package Menu"))
2425 (let ((upgrades (package-menu--find-upgrades)))
2426 (if (null upgrades)
2427 (message "No packages to upgrade.")
2428 (widen)
2429 (save-excursion
2430 (goto-char (point-min))
2431 (while (not (eobp))
2432 (let* ((pkg-desc (tabulated-list-get-id))
2433 (upgrade (cdr (assq (package-desc-name pkg-desc) upgrades))))
2434 (cond ((null upgrade)
2435 (forward-line 1))
2436 ((equal pkg-desc upgrade)
2437 (package-menu-mark-install))
2439 (package-menu-mark-delete))))))
2440 (message "%d package%s marked for upgrading."
2441 (length upgrades)
2442 (if (= (length upgrades) 1) "" "s")))))
2444 (defun package--sort-deps-in-alist (package only)
2445 "Return a list of dependencies for PACKAGE sorted by dependency.
2446 PACKAGE is included as the first element of the returned list.
2447 ONLY is an alist associating package names to package objects.
2448 Only these packages will be in the return value an their cdrs are
2449 destructively set to nil in ONLY."
2450 (let ((out))
2451 (dolist (dep (package-desc-reqs package))
2452 (when-let ((cell (assq (car dep) only))
2453 (dep-package (cdr-safe cell)))
2454 (setcdr cell nil)
2455 (setq out (append (package--sort-deps-in-alist dep-package only)
2456 out))))
2457 (cons package out)))
2459 (defun package--sort-by-dependence (package-list)
2460 "Return PACKAGE-LIST sorted by dependence.
2461 That is, any element of the returned list is guaranteed to not
2462 directly depend on any elements that come before it.
2464 PACKAGE-LIST is a list of package-desc objects.
2465 Indirect dependencies are guaranteed to be returned in order only
2466 if all the in-between dependencies are also in PACKAGE-LIST."
2467 (let ((alist (mapcar (lambda (p) (cons (package-desc-name p) p)) package-list))
2468 out-list)
2469 (dolist (cell alist out-list)
2470 ;; `package--sort-deps-in-alist' destructively changes alist, so
2471 ;; some cells might already be empty. We check this here.
2472 (when-let ((pkg-desc (cdr cell)))
2473 (setcdr cell nil)
2474 (setq out-list
2475 (append (package--sort-deps-in-alist pkg-desc alist)
2476 out-list))))))
2478 (defun package-menu-execute (&optional noquery)
2479 "Perform marked Package Menu actions.
2480 Packages marked for installation are downloaded and installed;
2481 packages marked for deletion are removed.
2482 Optional argument NOQUERY non-nil means do not ask the user to confirm."
2483 (interactive)
2484 (unless (derived-mode-p 'package-menu-mode)
2485 (error "The current buffer is not in Package Menu mode"))
2486 (let (install-list delete-list cmd pkg-desc)
2487 (save-excursion
2488 (goto-char (point-min))
2489 (while (not (eobp))
2490 (setq cmd (char-after))
2491 (unless (eq cmd ?\s)
2492 ;; This is the key PKG-DESC.
2493 (setq pkg-desc (tabulated-list-get-id))
2494 (cond ((eq cmd ?D)
2495 (push pkg-desc delete-list))
2496 ((eq cmd ?I)
2497 (push pkg-desc install-list))))
2498 (forward-line)))
2499 (when install-list
2500 (if (or
2501 noquery
2502 (yes-or-no-p
2503 (if (= (length install-list) 1)
2504 (format "Install package `%s'? "
2505 (package-desc-full-name (car install-list)))
2506 (format "Install these %d packages (%s)? "
2507 (length install-list)
2508 (mapconcat #'package-desc-full-name
2509 install-list ", ")))))
2510 (mapc (lambda (p)
2511 ;; Don't mark as selected if it's a new version of
2512 ;; an installed package.
2513 (package-install p (and (not (package-installed-p p))
2514 (package-installed-p
2515 (package-desc-name p)))))
2516 install-list)))
2517 ;; Delete packages, prompting if necessary.
2518 (when delete-list
2519 (if (or
2520 noquery
2521 (yes-or-no-p
2522 (if (= (length delete-list) 1)
2523 (format "Delete package `%s'? "
2524 (package-desc-full-name (car delete-list)))
2525 (format "Delete these %d packages (%s)? "
2526 (length delete-list)
2527 (mapconcat #'package-desc-full-name
2528 delete-list ", ")))))
2529 (dolist (elt (package--sort-by-dependence delete-list))
2530 (condition-case-unless-debug err
2531 (package-delete elt)
2532 (error (message (cadr err)))))
2533 (error "Aborted")))
2534 (if (not (or delete-list install-list))
2535 (message "No operations specified.")
2536 (when package-selected-packages
2537 (let ((removable (package--removable-packages)))
2538 (when (and removable
2539 (y-or-n-p
2540 (format "These %d packages are no longer needed, delete them (%s)? "
2541 (length removable)
2542 (mapconcat #'symbol-name removable ", "))))
2543 ;; We know these are removable, so we can use force instead of sorting them.
2544 (mapc (lambda (p) (package-delete (cadr (assq p package-alist)) 'force 'nosave))
2545 removable))))
2546 (package-menu--generate t t))))
2548 (defun package-menu--version-predicate (A B)
2549 (let ((vA (or (aref (cadr A) 1) '(0)))
2550 (vB (or (aref (cadr B) 1) '(0))))
2551 (if (version-list-= vA vB)
2552 (package-menu--name-predicate A B)
2553 (version-list-< vA vB))))
2555 (defun package-menu--status-predicate (A B)
2556 (let ((sA (aref (cadr A) 2))
2557 (sB (aref (cadr B) 2)))
2558 (cond ((string= sA sB)
2559 (package-menu--name-predicate A B))
2560 ((string= sA "new") t)
2561 ((string= sB "new") nil)
2562 ((string= sA "available") t)
2563 ((string= sB "available") nil)
2564 ((string= sA "installed") t)
2565 ((string= sB "installed") nil)
2566 ((string= sA "dependency") t)
2567 ((string= sB "dependency") nil)
2568 ((string= sA "unsigned") t)
2569 ((string= sB "unsigned") nil)
2570 ((string= sA "held") t)
2571 ((string= sB "held") nil)
2572 ((string= sA "built-in") t)
2573 ((string= sB "built-in") nil)
2574 ((string= sA "obsolete") t)
2575 ((string= sB "obsolete") nil)
2576 ((string= sA "incompat") t)
2577 ((string= sB "incompat") nil)
2578 (t (string< sA sB)))))
2580 (defun package-menu--description-predicate (A B)
2581 (let ((dA (aref (cadr A) 3))
2582 (dB (aref (cadr B) 3)))
2583 (if (string= dA dB)
2584 (package-menu--name-predicate A B)
2585 (string< dA dB))))
2587 (defun package-menu--name-predicate (A B)
2588 (string< (symbol-name (package-desc-name (car A)))
2589 (symbol-name (package-desc-name (car B)))))
2591 (defun package-menu--archive-predicate (A B)
2592 (string< (or (package-desc-archive (car A)) "")
2593 (or (package-desc-archive (car B)) "")))
2595 ;;;###autoload
2596 (defun list-packages (&optional no-fetch)
2597 "Display a list of packages.
2598 This first fetches the updated list of packages before
2599 displaying, unless a prefix argument NO-FETCH is specified.
2600 The list is displayed in a buffer named `*Packages*'."
2601 (interactive "P")
2602 (require 'finder-inf nil t)
2603 ;; Initialize the package system if necessary.
2604 (unless package--initialized
2605 (package-initialize t))
2606 (let (old-archives new-packages)
2607 (unless no-fetch
2608 ;; Read the locally-cached archive-contents.
2609 (package-read-all-archive-contents)
2610 (setq old-archives package-archive-contents)
2611 ;; Fetch the remote list of packages.
2612 (package-refresh-contents)
2613 ;; Find which packages are new.
2614 (dolist (elt package-archive-contents)
2615 (unless (assq (car elt) old-archives)
2616 (push (car elt) new-packages))))
2618 ;; Generate the Package Menu.
2619 (let ((buf (get-buffer-create "*Packages*")))
2620 (with-current-buffer buf
2621 (package-menu-mode)
2622 (set (make-local-variable 'package-menu--new-package-list)
2623 new-packages)
2624 (package-menu--generate nil t))
2625 ;; The package menu buffer has keybindings. If the user types
2626 ;; `M-x list-packages', that suggests it should become current.
2627 (switch-to-buffer buf))
2629 (let ((upgrades (package-menu--find-upgrades)))
2630 (if upgrades
2631 (message "%d package%s can be upgraded; type `%s' to mark %s for upgrading."
2632 (length upgrades)
2633 (if (= (length upgrades) 1) "" "s")
2634 (substitute-command-keys "\\[package-menu-mark-upgrades]")
2635 (if (= (length upgrades) 1) "it" "them"))))))
2637 ;;;###autoload
2638 (defalias 'package-list-packages 'list-packages)
2640 ;; Used in finder.el
2641 (defun package-show-package-list (&optional packages keywords)
2642 "Display PACKAGES in a *Packages* buffer.
2643 This is similar to `list-packages', but it does not fetch the
2644 updated list of packages, and it only displays packages with
2645 names in PACKAGES (which should be a list of symbols).
2647 When KEYWORDS are given, only packages with those KEYWORDS are
2648 shown."
2649 (interactive)
2650 (require 'finder-inf nil t)
2651 (let* ((buf (get-buffer-create "*Packages*"))
2652 (win (get-buffer-window buf)))
2653 (with-current-buffer buf
2654 (package-menu-mode)
2655 (package-menu--generate nil packages keywords))
2656 (if win
2657 (select-window win)
2658 (switch-to-buffer buf))))
2660 ;; package-menu--generate rebinds "q" on the fly, so we have to
2661 ;; hard-code the binding in the doc-string here.
2662 (defun package-menu-filter (keyword)
2663 "Filter the *Packages* buffer.
2664 Show only those items that relate to the specified KEYWORD.
2665 To restore the full package list, type `q'."
2666 (interactive (list (completing-read "Keyword: " (package-all-keywords))))
2667 (package-show-package-list t (list keyword)))
2669 (defun package-list-packages-no-fetch ()
2670 "Display a list of packages.
2671 Does not fetch the updated list of packages before displaying.
2672 The list is displayed in a buffer named `*Packages*'."
2673 (interactive)
2674 (list-packages t))
2676 (provide 'package)
2678 ;;; package.el ends here