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