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