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