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
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/>.
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
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
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:
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
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.
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>
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>
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
140 ;; - Interface with desktop.el so that restarting after an install
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
152 ;; - Allow multiple versions on the server...?
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
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)
171 (defgroup package nil
172 "Manager for Emacs Lisp packages."
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."
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)
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"))
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)"))
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.
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'."
294 (defcustom package-directory-list
295 ;; Defaults are subdirs named "elpa" in the site-lisp dirs.
297 (dolist (f load-path)
299 (equal (file-name-nondirectory f) "site-lisp")
300 (push (expand-file-name "elpa" f) 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)
312 (defvar epg-gpg-program)
314 (defcustom package-check-signature
315 (if (progn (require 'epg-config) (executable-find epg-gpg-program))
317 "Non-nil means to check package signatures when installing.
318 The value `allow-unsigned' means to still install a package even if
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"))
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"))
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."
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.
361 package-desc-from-define
362 (name-string version-string &optional summary requirements
365 (name (intern name-string))
366 (version (version-to-list version-string))
367 (reqs (mapcar #'(lambda (elt)
369 (version-to-list (cadr elt))))
370 (if (eq 'quote (car requirements))
373 (kind (plist-get rest-plist :kind))
374 (archive (plist-get rest-plist :archive))
377 (unless (memq (car rest-plist) '(:kind :archive))
378 (let ((value (cadr rest-plist)))
380 (push (cons (car rest-plist)
381 (if (eq (car-safe value) 'quote)
385 (setq rest-plist (cddr rest-plist)))
387 "Structure containing information about an individual package.
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
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
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."
415 (summary package--default-summary)
424 (defun package-desc-full-name (pkg-desc)
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)
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)
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))
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.)"
483 (let ((str-list (list "." (int-to-string (car vlist)))))
484 (dolist (num (cdr vlist))
487 (push (int-to-string num) str-list)
490 (error "Invalid version list `%s'" vlist))
492 ;; pre, or beta, or alpha
493 (cond ((equal "." (car str-list))
495 ((not (string-match "[0-9]+" (car str-list)))
496 (error "Invalid version list `%s'" vlist)))
497 (push (cond ((= num -1) "pre")
500 ((= num -4) "snapshot"))
502 (if (equal "." (car 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)
510 (signed-file (concat pkg-dir ".signed")))
511 (when (file-exists-p pkg-file)
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))
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
548 ((stringp force) ; held
549 (unless (version-list-= version (version-to-list 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)))
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
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))))
587 (when (file-exists-p (expand-file-name "dir" pkg-dir))
588 ;; FIXME: not the friendliest, but simple.
591 (push pkg-dir Info-directory-list))
592 (push name package-activated-list)
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
604 (and f (file-name-sans-extension f))))
606 (dir (file-truename dir))
607 ;; List all files that have already been loaded.
612 (lambda (x) (let* ((file (file-relative-name x dir))
613 ;; Previously loaded file, if any.
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)
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)))
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)
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'.
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)))
668 ;; If no such package is found, maybe it's built-in.
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))))))
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)))
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.
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))
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)
729 (concat ";;; " (file-name-nondirectory file)
730 " --- automatically extracted autoloads\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"
739 ";;; " (file-name-nondirectory file)
741 nil file nil 'silent))
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))
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)))
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."
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)))))
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)
791 ";;; -*- no-byte-compile: t -*-\n"
794 (list 'define-package
796 (package-version-join (package-desc-version pkg-desc))
797 (package-desc-summary pkg-desc)
798 (let ((requires (package-desc-reqs pkg-desc)))
800 ;; Turn version lists into string form.
804 (package-version-join (cadr elt))))
806 (package--alist-to-plist-args
807 (package-desc-extras pkg-desc))))
809 nil pkg-file nil 'silent))))
811 (defun package--alist-to-plist-args (alist)
812 (mapcar 'macroexp-quote
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)
822 (make-directory pkg-dir t)
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))))
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)))
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)
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)
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))
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"
892 (insert-file-contents (expand-file-name ,file ,location)))
895 (defun package--archive-file-exists-p (location file)
896 (let ((http (string-match "\\`https?:" location)))
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
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)
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
934 (setf (epg-context-home-directory context) homedir)
935 (condition-case error
936 (epg-verify-string context sig-content (buffer-string))
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))
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")
985 (concat (package-desc-full-name pkg-desc)
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)))
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)))
1008 (file-exists-p dir)))
1010 (let ((pkg-descs (cdr (assq package package-alist))))
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
1037 (dolist (elt requirements)
1038 (let* ((next-pkg (car elt))
1039 (next-version (cadr elt))
1041 (dolist (pkg packages)
1042 (if (eq next-pkg (package-desc-name pkg))
1043 (setq already pkg)))
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))
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)))))
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)))
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)))
1073 ((version-list-< version next-version)
1075 "Need package `%s-%s', but only %s is available"
1076 next-pkg (package-version-join next-version)
1077 (package-version-join version)))
1081 (if (stringp disabled)
1082 (format "Package `%s' held at version %s, \
1083 but version %s required"
1085 (package-version-join next-version))
1086 (format "Required package '%s' is disabled"
1088 (t (setq found pkg-desc)))))
1091 (error "%s" problem)
1092 (error "Package `%s-%s' is unavailable"
1093 next-pkg (package-version-join next-version))))
1095 (package-compute-transaction (cons found packages)
1096 (package-desc-reqs found)
1097 (cons found seen))))))))
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))
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))))
1112 (error "Can't read whole string")
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)
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))
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
1142 (let* ((contents-file (format "archives/%s/archive-contents" archive))
1143 (contents (package--read-archive-file contents-file)))
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))
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)))
1164 (package-desc-create
1167 :reqs (package--ac-desc-reqs (cdr package))
1168 :summary (package--ac-desc-summary (cdr package))
1169 :kind (package--ac-desc-kind (cdr package))
1171 :extras (and (> (length (cdr package)) 4)
1172 ;; Older archive-contents files have only 4
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
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)
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))
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))
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."
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
1245 (mapcar (lambda (elt)
1246 (unless (package-installed-p (car elt))
1247 (symbol-name (car elt))))
1248 package-archive-contents))
1251 (let ((name (if (package-desc-p pkg)
1252 (package-desc-name 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))))))
1268 (defun package-reinstall (pkg)
1269 "Reinstall package PKG.
1270 PKG shoul be either a symbol, the package name, or a package-desc
1272 (interactive (list (intern (completing-read
1273 "Reinstall package: "
1274 (mapcar #'symbol-name
1275 (mapcar #'car package-alist))))))
1277 (if (package-desc-p pkg) pkg (cadr (assq pkg package-alist)))
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."
1286 (when (string-match "\\`[ \t]*[$]Revision:[ \t]+" str)
1287 (setq str (substring str (match-end 0))))
1289 (if (version-to-list str)
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."
1303 (error "Invalid requirement specifier: %S" deps))
1304 (t (mapcar (lambda (dep)
1306 ((symbolp dep) `(,dep "0"))
1308 (error "Invalid requirement specifier: %S" dep))
1309 ((and (listp dep) (null (cdr dep)))
1310 (list (car dep) "0"))
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
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.
1330 (narrow-to-region start (point))
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.
1337 (or (package-strip-rcs-id (lm-header "package-version"))
1338 (package-strip-rcs-id (lm-header "version"))))
1339 (homepage (lm-homepage)))
1342 "Package lacks a \"Version\" or \"Package-Version\" header"))
1343 (package-desc-from-define
1344 file-name pkg-version desc
1346 (package--prepare-dependencies
1347 (package-read-from-string requires-str)))
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))))
1363 (error "No package descriptor file found"))
1364 (with-current-buffer (tar--extract tar-desc)
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)
1378 (insert-file-contents desc-file)
1379 (package--read-pkg-desc 'dir))
1380 (let ((files (directory-files default-directory t "\\.el\\'" t))
1384 (insert-file-contents (pop files))
1385 ;; When we find the file with the data,
1386 (when (setq info (ignore-errors (package-buffer-info)))
1389 ;; set the 'dir kind,
1390 (setf (package-desc-kind info) 'dir))))
1391 ;; and return the 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))
1399 (let* ((pkg-def-parsed (read (current-buffer)))
1401 (when (eq (car pkg-def-parsed) 'define-package)
1402 (apply #'package-desc-from-define
1403 (append (cdr pkg-def-parsed))))))
1405 (setf (package-desc-kind pkg-desc) kind)
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."
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.
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)))
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: ")
1452 (if (file-directory-p file)
1454 (setq default-directory file)
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)
1464 when (assq name package-alist)
1466 (indirect-deps (unless (eq only 'direct)
1468 (cl-loop for p in direct-deps
1469 append (package--get-deps p))))))
1471 (direct direct-deps)
1472 (separate (list direct-deps indirect-deps))
1473 (indirect indirect-deps)
1474 (t (delete-dups (append direct-deps indirect-deps))))))
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."
1481 ;; We don't need to populate `package-selected-packages' before
1482 ;; using here, because the outcome is the same either way (nothing
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)
1492 (format "%s packages will be installed:\n%s, proceed?"
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
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)
1510 for p in alist thereis
1511 (and (memq pkg (mapcar #'car (package-desc-reqs (cadr 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))
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
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)))
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)
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)
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
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
1593 "`package-selected-packages' is empty! Really remove ALL packages? "))
1594 (let ((removable (package--removable-packages)))
1597 (format "%s packages will be deleted:\n%s, proceed? "
1599 (mapconcat #'symbol-name removable ", ")))
1601 (package-delete (cadr (assq p package-alist)) t))
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
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
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))
1635 (sig-file (concat file ".sig"))
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)
1644 (unless (eq package-check-signature 'allow-unsigned)
1645 (error "Unsigned archive `%s'"
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)
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))
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))))
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."
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"
1689 (when (and package-check-signature (file-exists-p default-keyring))
1690 (condition-case-unless-debug error
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."
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'."
1710 (mapcar (lambda (p) (mapcar #'car (package-desc-reqs (cadr p))))
1712 (cl-loop for p in package-alist
1714 unless (memq name dep-list)
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."
1723 (setq package-alist nil)
1724 (package-load-all-descriptors)
1725 (package-read-all-archive-contents)
1727 (dolist (elt package-alist)
1728 (package-activate (car elt))))
1729 (setq package--initialized t))
1732 ;;;; Package description buffer.
1735 (defun describe-package (package)
1736 "Display the full documentation of PACKAGE (a symbol)."
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)
1748 (setq packages (mapcar 'symbol-name packages))
1750 (completing-read (if guess
1751 (format "Describe package (default %s): "
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)
1767 (if (package-desc-p pkg) pkg)
1768 (cadr (assq pkg package-alist))
1769 (let ((built-in (assq pkg package--builtins)))
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))))
1787 (princ (if (memq (aref status 0) '(?a ?e ?i ?o ?u)) "an " "a "))
1789 (princ " package.\n\n")
1791 (insert " " (propertize "Status" 'font-lock-face 'bold) ": ")
1793 (insert (propertize (capitalize status)
1794 'font-lock-face 'font-lock-builtin-face)
1797 (insert (propertize (if (member status '("unsigned" "dependency"))
1799 (if (equal status "incompat")
1801 (capitalize status))) ;FIXME: Why comment-face?
1802 'font-lock-face 'font-lock-comment-face))
1804 ;; Todo: Add button for uninstalling.
1805 (help-insert-xref-button (abbreviate-file-name
1806 (file-name-as-directory pkg-dir))
1807 'help-package-def pkg-dir)
1808 (if (and (package-built-in-p name)
1809 (not (package-built-in-p name version)))
1810 (insert "',\n shadowing a "
1811 (propertize "built-in package"
1812 'font-lock-face 'font-lock-builtin-face))
1816 (insert " (unsigned).")))
1818 (insert (capitalize status))
1819 (insert " from " (format "%s" archive))
1821 (package-make-button
1823 'action 'package-install-button-action
1824 'package-desc desc))
1825 (t (insert (capitalize status) ".")))
1827 (insert " " (propertize "Archive" 'font-lock-face 'bold)
1828 ": " (or archive "n/a") "\n")
1831 (propertize "Version" 'font-lock-face 'bold) ": "
1832 (package-version-join version) "\n"))
1834 (setq reqs (if desc (package-desc-reqs desc)))
1836 (insert " " (propertize "Requires" 'font-lock-face 'bold) ": ")
1840 (setq name (car req)
1842 text (format "%s-%s" (symbol-name name)
1843 (package-version-join vers)))
1844 (cond (first (setq first nil))
1845 ((>= (+ 2 (current-column) (length text))
1849 (help-insert-xref-button text 'help-package name))
1851 (insert " " (propertize "Summary" 'font-lock-face 'bold)
1852 ": " (if desc (package-desc-summary desc)) "\n")
1854 (insert " " (propertize "Homepage" 'font-lock-face 'bold) ": ")
1855 (help-insert-xref-button homepage 'help-url homepage)
1858 (insert " " (propertize "Keywords" 'font-lock-face 'bold) ": ")
1859 (dolist (k keywords)
1860 (package-make-button
1863 'action 'package-keyword-button-action)
1866 (let* ((all-pkgs (append (cdr (assq name package-alist))
1867 (cdr (assq name package-archive-contents))
1868 (let ((bi (assq name package--builtins)))
1869 (if bi (list (package--from-builtin bi))))))
1870 (other-pkgs (delete desc all-pkgs)))
1872 (insert " " (propertize "Other versions" 'font-lock-face 'bold) ": "
1875 (let* ((ov (package-desc-version opkg))
1876 (dir (package-desc-dir opkg))
1877 (from (or (package-desc-archive opkg)
1878 (if (stringp dir) "installed" dir))))
1879 (if (not ov) (format "%s" from)
1881 (make-text-button (package-version-join ov) nil
1886 (describe-package opkg)))
1894 ;; For built-in packages, insert the commentary.
1895 (let ((fn (locate-file (format "%s.el" name) load-path
1896 load-file-rep-suffixes))
1898 (insert (or (lm-commentary fn) ""))
1901 (when (re-search-forward "^;;; Commentary:\n" nil t)
1903 (while (re-search-forward "^\\(;+ ?\\)" nil t)
1904 (replace-match ""))))
1905 (let ((readme (expand-file-name (format "%s-readme.txt" name)
1908 ;; For elpa packages, try downloading the commentary. If that
1909 ;; fails, try an existing readme file in `package-user-dir'.
1910 (cond ((condition-case nil
1912 (package--with-work-buffer
1913 (package-archive-base desc)
1914 (format "%s-readme.txt" name)
1916 (goto-char (point-max))
1919 (write-region nil nil
1920 (expand-file-name readme package-user-dir)
1922 (setq readme-string (buffer-string))
1925 (insert readme-string))
1926 ((file-readable-p readme)
1927 (insert-file-contents readme)
1928 (goto-char (point-max))))))))
1930 (defun package-install-button-action (button)
1931 (let ((pkg-desc (button-get button 'package-desc)))
1932 (when (y-or-n-p (format "Install package `%s'? "
1933 (package-desc-full-name pkg-desc)))
1934 (package-install pkg-desc nil)
1935 (revert-buffer nil t)
1936 (goto-char (point-min)))))
1938 (defun package-keyword-button-action (button)
1939 (let ((pkg-keyword (button-get button 'package-keyword)))
1940 (package-show-package-list t (list pkg-keyword))))
1942 (defun package-make-button (text &rest props)
1943 (let ((button-text (if (display-graphic-p) text (concat "[" text "]")))
1944 (button-face (if (display-graphic-p)
1945 '(:box (:line-width 2 :color "dark grey")
1946 :background "light grey"
1947 :foreground "black")
1949 (apply 'insert-text-button button-text 'face button-face 'follow-link t
1953 ;;;; Package menu mode.
1955 (defvar package-menu-mode-map
1956 (let ((map (make-sparse-keymap))
1957 (menu-map (make-sparse-keymap "Package")))
1958 (set-keymap-parent map tabulated-list-mode-map)
1959 (define-key map "\C-m" 'package-menu-describe-package)
1960 (define-key map "u" 'package-menu-mark-unmark)
1961 (define-key map "\177" 'package-menu-backup-unmark)
1962 (define-key map "d" 'package-menu-mark-delete)
1963 (define-key map "i" 'package-menu-mark-install)
1964 (define-key map "U" 'package-menu-mark-upgrades)
1965 (define-key map "r" 'package-menu-refresh)
1966 (define-key map "f" 'package-menu-filter)
1967 (define-key map "~" 'package-menu-mark-obsolete-for-deletion)
1968 (define-key map "x" 'package-menu-execute)
1969 (define-key map "h" 'package-menu-quick-help)
1970 (define-key map "?" 'package-menu-describe-package)
1971 (define-key map [menu-bar package-menu] (cons "Package" menu-map))
1972 (define-key menu-map [mq]
1973 '(menu-item "Quit" quit-window
1974 :help "Quit package selection"))
1975 (define-key menu-map [s1] '("--"))
1976 (define-key menu-map [mn]
1977 '(menu-item "Next" next-line
1979 (define-key menu-map [mp]
1980 '(menu-item "Previous" previous-line
1981 :help "Previous Line"))
1982 (define-key menu-map [s2] '("--"))
1983 (define-key menu-map [mu]
1984 '(menu-item "Unmark" package-menu-mark-unmark
1985 :help "Clear any marks on a package and move to the next line"))
1986 (define-key menu-map [munm]
1987 '(menu-item "Unmark Backwards" package-menu-backup-unmark
1988 :help "Back up one line and clear any marks on that package"))
1989 (define-key menu-map [md]
1990 '(menu-item "Mark for Deletion" package-menu-mark-delete
1991 :help "Mark a package for deletion and move to the next line"))
1992 (define-key menu-map [mi]
1993 '(menu-item "Mark for Install" package-menu-mark-install
1994 :help "Mark a package for installation and move to the next line"))
1995 (define-key menu-map [mupgrades]
1996 '(menu-item "Mark Upgradable Packages" package-menu-mark-upgrades
1997 :help "Mark packages that have a newer version for upgrading"))
1998 (define-key menu-map [s3] '("--"))
1999 (define-key menu-map [mf]
2000 '(menu-item "Filter Package List..." package-menu-filter
2001 :help "Filter package selection (q to go back)"))
2002 (define-key menu-map [mg]
2003 '(menu-item "Update Package List" revert-buffer
2004 :help "Update the list of packages"))
2005 (define-key menu-map [mr]
2006 '(menu-item "Refresh Package List" package-menu-refresh
2007 :help "Download the ELPA archive"))
2008 (define-key menu-map [s4] '("--"))
2009 (define-key menu-map [mt]
2010 '(menu-item "Mark Obsolete Packages" package-menu-mark-obsolete-for-deletion
2011 :help "Mark all obsolete packages for deletion"))
2012 (define-key menu-map [mx]
2013 '(menu-item "Execute Actions" package-menu-execute
2014 :help "Perform all the marked actions"))
2015 (define-key menu-map [s5] '("--"))
2016 (define-key menu-map [mh]
2017 '(menu-item "Help" package-menu-quick-help
2018 :help "Show short key binding help for package-menu-mode"))
2019 (define-key menu-map [mc]
2020 '(menu-item "Describe Package" package-menu-describe-package
2021 :help "Display information about this package"))
2023 "Local keymap for `package-menu-mode' buffers.")
2025 (defvar package-menu--new-package-list nil
2026 "List of newly-available packages since `list-packages' was last called.")
2028 (define-derived-mode package-menu-mode tabulated-list-mode "Package Menu"
2029 "Major mode for browsing a list of packages.
2030 Letters do not insert themselves; instead, they are commands.
2031 \\<package-menu-mode-map>
2032 \\{package-menu-mode-map}"
2033 (setq tabulated-list-format
2034 `[("Package" 18 package-menu--name-predicate)
2036 ("Status" 10 package-menu--status-predicate)
2037 ,@(if (cdr package-archives)
2038 '(("Archive" 10 package-menu--archive-predicate)))
2039 ("Description" 0 nil)])
2040 (setq tabulated-list-padding 2)
2041 (setq tabulated-list-sort-key (cons "Status" nil))
2042 (add-hook 'tabulated-list-revert-hook 'package-menu--refresh nil t)
2043 (tabulated-list-init-header))
2045 (defmacro package--push (pkg-desc status listname)
2046 "Convenience macro for `package-menu--generate'.
2047 If the alist stored in the symbol LISTNAME lacks an entry for a
2048 package PKG-DESC, add one. The alist is keyed with PKG-DESC."
2049 `(unless (assoc ,pkg-desc ,listname)
2050 ;; FIXME: Should we move status into pkg-desc?
2051 (push (cons ,pkg-desc ,status) ,listname)))
2053 (defvar package-list-unversioned nil
2054 "If non-nil include packages that don't have a version in `list-package'.")
2056 (defvar package-list-unsigned nil
2057 "If non-nil, mention in the list which packages were installed w/o signature.")
2059 (defvar package--emacs-version-list (version-to-list emacs-version)
2060 "`emacs-version', as a list.")
2062 (defun package--incompatible-p (pkg)
2063 "Return non-nil if PKG has no chance of being installable.
2064 PKG is a package-desc object.
2065 Return value is a string describing the reason why the package is
2068 Currently, this only checks if PKG depends on a higher
2069 `emacs-version' than the one being used."
2070 (let* ((reqs (package-desc-reqs pkg))
2071 (version (cadr (assq 'emacs reqs))))
2072 (if (and version (version-list-< package--emacs-version-list version))
2073 (format "`%s' requires Emacs %s, but current version is %s"
2074 (package-desc-full-name pkg)
2075 (package-version-join version)
2078 (defun package-desc-status (pkg-desc)
2079 (let* ((name (package-desc-name pkg-desc))
2080 (dir (package-desc-dir pkg-desc))
2081 (lle (assq name package-load-list))
2083 (version (package-desc-version pkg-desc))
2084 (signed (or (not package-list-unsigned)
2085 (package-desc-signed pkg-desc))))
2087 ((eq dir 'builtin) "built-in")
2088 ((and lle (null held)) "disabled")
2090 (let ((hv (if (stringp held) (version-to-list held))))
2092 ((version-list-= version hv) "held")
2093 ((version-list-< version hv) "obsolete")
2095 ((package-built-in-p name version) "obsolete")
2096 ((package--incompatible-p pkg-desc) "incompat")
2097 (dir ;One of the installed packages.
2099 ((not (file-exists-p (package-desc-dir pkg-desc))) "deleted")
2100 ((eq pkg-desc (cadr (assq name package-alist)))
2101 (if (not signed) "unsigned"
2102 (if (package--user-selected-p name)
2103 "installed" "dependency")))
2106 (let* ((ins (cadr (assq name package-alist)))
2107 (ins-v (if ins (package-desc-version ins))))
2109 ((or (null ins) (version-list-< ins-v version))
2110 (if (memq name package-menu--new-package-list)
2112 ((version-list-< version ins-v) "obsolete")
2113 ((version-list-= version ins-v)
2114 (if (not signed) "unsigned"
2115 (if (package--user-selected-p name)
2116 "installed" "dependency")))))))))
2118 (defun package-menu--refresh (&optional packages keywords)
2119 "Re-populate the `tabulated-list-entries'.
2120 PACKAGES should be nil or t, which means to display all known packages.
2121 KEYWORDS should be nil or a list of keywords."
2122 ;; Construct list of (PKG-DESC . STATUS).
2123 (unless packages (setq packages t))
2124 (let (info-list name)
2125 ;; Installed packages:
2126 (dolist (elt package-alist)
2127 (setq name (car elt))
2128 (when (or (eq packages t) (memq name packages))
2129 (dolist (pkg (cdr elt))
2130 (when (package--has-keyword-p pkg keywords)
2131 (package--push pkg (package-desc-status pkg) info-list)))))
2133 ;; Built-in packages:
2134 (dolist (elt package--builtins)
2135 (setq name (car elt))
2136 (when (and (not (eq name 'emacs)) ; Hide the `emacs' package.
2137 (package--has-keyword-p (package--from-builtin elt) keywords)
2138 (or package-list-unversioned
2139 (package--bi-desc-version (cdr elt)))
2140 (or (eq packages t) (memq name packages)))
2141 (package--push (package--from-builtin elt) "built-in" info-list)))
2143 ;; Available and disabled packages:
2144 (dolist (elt package-archive-contents)
2145 (setq name (car elt))
2146 (when (or (eq packages t) (memq name packages))
2147 (dolist (pkg (cdr elt))
2148 ;; Hide obsolete packages.
2149 (when (and (not (package-installed-p (package-desc-name pkg)
2150 (package-desc-version pkg)))
2151 (package--has-keyword-p pkg keywords))
2152 (package--push pkg (package-desc-status pkg) info-list)))))
2154 ;; Print the result.
2155 (setq tabulated-list-entries
2156 (mapcar #'package-menu--print-info info-list))))
2158 (defun package-all-keywords ()
2159 "Collect all package keywords"
2161 (package--mapc (lambda (desc)
2162 (let* ((desc-keywords (and desc (package-desc--keywords desc))))
2163 (setq keywords (append keywords desc-keywords)))))
2166 (defun package--mapc (function &optional packages)
2167 "Call FUNCTION for all known PACKAGES.
2168 PACKAGES can be nil or t, which means to display all known
2169 packages, or a list of packages.
2171 Built-in packages are converted with `package--from-builtin'."
2172 (unless packages (setq packages t))
2174 ;; Installed packages:
2175 (dolist (elt package-alist)
2176 (setq name (car elt))
2177 (when (or (eq packages t) (memq name packages))
2178 (mapc function (cdr elt))))
2180 ;; Built-in packages:
2181 (dolist (elt package--builtins)
2182 (setq name (car elt))
2183 (when (and (not (eq name 'emacs)) ; Hide the `emacs' package.
2184 (or package-list-unversioned
2185 (package--bi-desc-version (cdr elt)))
2186 (or (eq packages t) (memq name packages)))
2187 (funcall function (package--from-builtin elt))))
2189 ;; Available and disabled packages:
2190 (dolist (elt package-archive-contents)
2191 (setq name (car elt))
2192 (when (or (eq packages t) (memq name packages))
2193 (dolist (pkg (cdr elt))
2194 ;; Hide obsolete packages.
2195 (unless (package-installed-p (package-desc-name pkg)
2196 (package-desc-version pkg))
2197 (funcall function pkg)))))))
2199 (defun package--has-keyword-p (desc &optional keywords)
2200 "Test if package DESC has any of the given KEYWORDS.
2201 When none are given, the package matches."
2203 (let* ((desc-keywords (and desc (package-desc--keywords desc)))
2205 (dolist (k keywords)
2206 (when (and (not found)
2207 (member k desc-keywords))
2212 (defun package-menu--generate (remember-pos packages &optional keywords)
2213 "Populate the Package Menu.
2214 If REMEMBER-POS is non-nil, keep point on the same entry.
2215 PACKAGES should be t, which means to display all known packages,
2216 or a list of package names (symbols) to display.
2218 With KEYWORDS given, only packages with those keywords are
2220 (package-menu--refresh packages keywords)
2221 (setf (car (aref tabulated-list-format 0))
2223 (let ((filters (mapconcat 'identity keywords ",")))
2224 (concat "Package[" filters "]"))
2227 (define-key package-menu-mode-map "q" 'package-show-package-list)
2228 (define-key package-menu-mode-map "q" 'quit-window))
2229 (tabulated-list-init-header)
2230 (tabulated-list-print remember-pos))
2232 (defun package-menu--print-info (pkg)
2233 "Return a package entry suitable for `tabulated-list-entries'.
2234 PKG has the form (PKG-DESC . STATUS).
2235 Return (PKG-DESC [NAME VERSION STATUS DOC])."
2236 (let* ((pkg-desc (car pkg))
2239 (`"built-in" 'font-lock-builtin-face)
2240 (`"available" 'default)
2242 (`"held" 'font-lock-constant-face)
2243 (`"disabled" 'font-lock-warning-face)
2244 (`"installed" 'font-lock-comment-face)
2245 (`"dependency" 'font-lock-comment-face)
2246 (`"unsigned" 'font-lock-warning-face)
2247 (`"incompat" 'font-lock-comment-face)
2248 (_ 'font-lock-warning-face)))) ; obsolete.
2250 `[,(list (symbol-name (package-desc-name pkg-desc))
2253 'package-desc pkg-desc
2254 'action 'package-menu-describe-package)
2255 ,(propertize (package-version-join
2256 (package-desc-version pkg-desc))
2257 'font-lock-face face)
2258 ,(propertize status 'font-lock-face face)
2259 ,@(if (cdr package-archives)
2260 (list (propertize (or (package-desc-archive pkg-desc) "")
2261 'font-lock-face face)))
2262 ,(propertize (package-desc-summary pkg-desc)
2263 'font-lock-face face)])))
2265 (defun package-menu-refresh ()
2266 "Download the Emacs Lisp package archive.
2267 This fetches the contents of each archive specified in
2268 `package-archives', and then refreshes the package menu."
2270 (unless (derived-mode-p 'package-menu-mode)
2271 (user-error "The current buffer is not a Package Menu"))
2272 (package-refresh-contents)
2273 (package-menu--generate t t))
2275 (defun package-menu-describe-package (&optional button)
2276 "Describe the current package.
2277 If optional arg BUTTON is non-nil, describe its associated package."
2279 (let ((pkg-desc (if button (button-get button 'package-desc)
2280 (tabulated-list-get-id))))
2282 (describe-package pkg-desc)
2283 (user-error "No package here"))))
2285 ;; fixme numeric argument
2286 (defun package-menu-mark-delete (&optional _num)
2287 "Mark a package for deletion and move to the next line."
2289 (if (member (package-menu-get-status)
2290 '("installed" "dependency" "obsolete" "unsigned"))
2291 (tabulated-list-put-tag "D" t)
2294 (defun package-menu-mark-install (&optional _num)
2295 "Mark a package for installation and move to the next line."
2297 (if (member (package-menu-get-status) '("available" "new" "dependency"))
2298 (tabulated-list-put-tag "I" t)
2301 (defun package-menu-mark-unmark (&optional _num)
2302 "Clear any marks on a package and move to the next line."
2304 (tabulated-list-put-tag " " t))
2306 (defun package-menu-backup-unmark ()
2307 "Back up one line and clear any marks on that package."
2310 (tabulated-list-put-tag " "))
2312 (defun package-menu-mark-obsolete-for-deletion ()
2313 "Mark all obsolete packages for deletion."
2316 (goto-char (point-min))
2318 (if (equal (package-menu-get-status) "obsolete")
2319 (tabulated-list-put-tag "D" t)
2320 (forward-line 1)))))
2322 (defun package-menu-quick-help ()
2323 "Show short key binding help for package-menu-mode."
2325 (message "n-ext, i-nstall, d-elete, u-nmark, x-ecute, r-efresh, h-elp"))
2327 (define-obsolete-function-alias
2328 'package-menu-view-commentary 'package-menu-describe-package "24.1")
2330 (defun package-menu-get-status ()
2331 (let* ((id (tabulated-list-get-id))
2332 (entry (and id (assq id tabulated-list-entries))))
2334 (aref (cadr entry) 2)
2337 (defun package-menu--find-upgrades ()
2338 (let (installed available upgrades)
2339 ;; Build list of installed/available packages in this buffer.
2340 (dolist (entry tabulated-list-entries)
2341 ;; ENTRY is (PKG-DESC [NAME VERSION STATUS DOC])
2342 (let ((pkg-desc (car entry))
2343 (status (aref (cadr entry) 2)))
2344 (cond ((member status '("installed" "dependency" "unsigned"))
2345 (push pkg-desc installed))
2346 ((member status '("available" "new"))
2347 (setq available (package--append-to-alist pkg-desc available))))))
2348 ;; Loop through list of installed packages, finding upgrades.
2349 (dolist (pkg-desc installed)
2350 (let* ((name (package-desc-name pkg-desc))
2351 (avail-pkg (cadr (assq name available))))
2353 (version-list-< (package-desc-priority-version pkg-desc)
2354 (package-desc-priority-version avail-pkg))
2355 (push (cons name avail-pkg) upgrades))))
2358 (defun package-menu-mark-upgrades ()
2359 "Mark all upgradable packages in the Package Menu.
2360 For each installed package with a newer version available, place
2361 an (I)nstall flag on the available version and a (D)elete flag on
2362 the installed version. A subsequent \\[package-menu-execute]
2363 call will upgrade the package."
2365 (unless (derived-mode-p 'package-menu-mode)
2366 (error "The current buffer is not a Package Menu"))
2367 (let ((upgrades (package-menu--find-upgrades)))
2369 (message "No packages to upgrade.")
2372 (goto-char (point-min))
2374 (let* ((pkg-desc (tabulated-list-get-id))
2375 (upgrade (cdr (assq (package-desc-name pkg-desc) upgrades))))
2376 (cond ((null upgrade)
2378 ((equal pkg-desc upgrade)
2379 (package-menu-mark-install))
2381 (package-menu-mark-delete))))))
2382 (message "%d package%s marked for upgrading."
2384 (if (= (length upgrades) 1) "" "s")))))
2386 (defun package--sort-deps-in-alist (package only)
2387 "Return a list of dependencies for PACKAGE sorted by dependency.
2388 PACKAGE is included as the first element of the returned list.
2389 ONLY is an alist associating package names to package objects.
2390 Only these packages will be in the return value an their cdrs are
2391 destructively set to nil in ONLY."
2393 (dolist (dep (package-desc-reqs package))
2394 (when-let ((cell (assq (car dep) only))
2395 (dep-package (cdr-safe cell)))
2397 (setq out (append (package--sort-deps-in-alist dep-package only)
2399 (cons package out)))
2401 (defun package--sort-by-dependence (package-list)
2402 "Return PACKAGE-LIST sorted by dependence.
2403 That is, any element of the returned list is guaranteed to not
2404 directly depend on any elements that come before it.
2406 PACKAGE-LIST is a list of package-desc objects.
2407 Indirect dependencies are guaranteed to be returned in order only
2408 if all the in-between dependencies are also in PACKAGE-LIST."
2409 (let ((alist (mapcar (lambda (p) (cons (package-desc-name p) p)) package-list))
2411 (dolist (cell alist out-list)
2412 ;; `package--sort-deps-in-alist' destructively changes alist, so
2413 ;; some cells might already be empty. We check this here.
2414 (when-let ((pkg-desc (cdr cell)))
2417 (append (package--sort-deps-in-alist pkg-desc alist)
2420 (defun package-menu-execute (&optional noquery)
2421 "Perform marked Package Menu actions.
2422 Packages marked for installation are downloaded and installed;
2423 packages marked for deletion are removed.
2424 Optional argument NOQUERY non-nil means do not ask the user to confirm."
2426 (unless (derived-mode-p 'package-menu-mode)
2427 (error "The current buffer is not in Package Menu mode"))
2428 (let (install-list delete-list cmd pkg-desc)
2430 (goto-char (point-min))
2432 (setq cmd (char-after))
2433 (unless (eq cmd ?\s)
2434 ;; This is the key PKG-DESC.
2435 (setq pkg-desc (tabulated-list-get-id))
2437 (push pkg-desc delete-list))
2439 (push pkg-desc install-list))))
2445 (if (= (length install-list) 1)
2446 (format "Install package `%s'? "
2447 (package-desc-full-name (car install-list)))
2448 (format "Install these %d packages (%s)? "
2449 (length install-list)
2450 (mapconcat #'package-desc-full-name
2451 install-list ", ")))))
2453 ;; Don't mark as selected if it's a new version of
2454 ;; an installed package.
2455 (package-install p (and (not (package-installed-p p))
2456 (package-installed-p
2457 (package-desc-name p)))))
2459 ;; Delete packages, prompting if necessary.
2464 (if (= (length delete-list) 1)
2465 (format "Delete package `%s'? "
2466 (package-desc-full-name (car delete-list)))
2467 (format "Delete these %d packages (%s)? "
2468 (length delete-list)
2469 (mapconcat #'package-desc-full-name
2470 delete-list ", ")))))
2471 (dolist (elt (package--sort-by-dependence delete-list))
2472 (condition-case-unless-debug err
2473 (package-delete elt)
2474 (error (message (cadr err)))))
2476 (if (not (or delete-list install-list))
2477 (message "No operations specified.")
2478 (when package-selected-packages
2479 (let ((removable (package--removable-packages)))
2480 (when (and removable
2482 (format "These %d packages are no longer needed, delete them (%s)? "
2484 (mapconcat #'symbol-name removable ", "))))
2485 ;; We know these are removable, so we can use force instead of sorting them.
2486 (mapc (lambda (p) (package-delete (cadr (assq p package-alist)) 'force 'nosave))
2488 (package-menu--generate t t))))
2490 (defun package-menu--version-predicate (A B)
2491 (let ((vA (or (aref (cadr A) 1) '(0)))
2492 (vB (or (aref (cadr B) 1) '(0))))
2493 (if (version-list-= vA vB)
2494 (package-menu--name-predicate A B)
2495 (version-list-< vA vB))))
2497 (defun package-menu--status-predicate (A B)
2498 (let ((sA (aref (cadr A) 2))
2499 (sB (aref (cadr B) 2)))
2500 (cond ((string= sA sB)
2501 (package-menu--name-predicate A B))
2502 ((string= sA "new") t)
2503 ((string= sB "new") nil)
2504 ((string= sA "available") t)
2505 ((string= sB "available") nil)
2506 ((string= sA "installed") t)
2507 ((string= sB "installed") nil)
2508 ((string= sA "dependency") t)
2509 ((string= sB "dependency") nil)
2510 ((string= sA "unsigned") t)
2511 ((string= sB "unsigned") nil)
2512 ((string= sA "held") t)
2513 ((string= sB "held") nil)
2514 ((string= sA "built-in") t)
2515 ((string= sB "built-in") nil)
2516 ((string= sA "obsolete") t)
2517 ((string= sB "obsolete") nil)
2518 ((string= sA "incompat") t)
2519 ((string= sB "incompat") nil)
2520 (t (string< sA sB)))))
2522 (defun package-menu--description-predicate (A B)
2523 (let ((dA (aref (cadr A) 3))
2524 (dB (aref (cadr B) 3)))
2526 (package-menu--name-predicate A B)
2529 (defun package-menu--name-predicate (A B)
2530 (string< (symbol-name (package-desc-name (car A)))
2531 (symbol-name (package-desc-name (car B)))))
2533 (defun package-menu--archive-predicate (A B)
2534 (string< (or (package-desc-archive (car A)) "")
2535 (or (package-desc-archive (car B)) "")))
2538 (defun list-packages (&optional no-fetch)
2539 "Display a list of packages.
2540 This first fetches the updated list of packages before
2541 displaying, unless a prefix argument NO-FETCH is specified.
2542 The list is displayed in a buffer named `*Packages*'."
2544 (require 'finder-inf nil t)
2545 ;; Initialize the package system if necessary.
2546 (unless package--initialized
2547 (package-initialize t))
2548 (let (old-archives new-packages)
2550 ;; Read the locally-cached archive-contents.
2551 (package-read-all-archive-contents)
2552 (setq old-archives package-archive-contents)
2553 ;; Fetch the remote list of packages.
2554 (package-refresh-contents)
2555 ;; Find which packages are new.
2556 (dolist (elt package-archive-contents)
2557 (unless (assq (car elt) old-archives)
2558 (push (car elt) new-packages))))
2560 ;; Generate the Package Menu.
2561 (let ((buf (get-buffer-create "*Packages*")))
2562 (with-current-buffer buf
2564 (set (make-local-variable 'package-menu--new-package-list)
2566 (package-menu--generate nil t))
2567 ;; The package menu buffer has keybindings. If the user types
2568 ;; `M-x list-packages', that suggests it should become current.
2569 (switch-to-buffer buf))
2571 (let ((upgrades (package-menu--find-upgrades)))
2573 (message "%d package%s can be upgraded; type `%s' to mark %s for upgrading."
2575 (if (= (length upgrades) 1) "" "s")
2576 (substitute-command-keys "\\[package-menu-mark-upgrades]")
2577 (if (= (length upgrades) 1) "it" "them"))))))
2580 (defalias 'package-list-packages 'list-packages)
2582 ;; Used in finder.el
2583 (defun package-show-package-list (&optional packages keywords)
2584 "Display PACKAGES in a *Packages* buffer.
2585 This is similar to `list-packages', but it does not fetch the
2586 updated list of packages, and it only displays packages with
2587 names in PACKAGES (which should be a list of symbols).
2589 When KEYWORDS are given, only packages with those KEYWORDS are
2592 (require 'finder-inf nil t)
2593 (let* ((buf (get-buffer-create "*Packages*"))
2594 (win (get-buffer-window buf)))
2595 (with-current-buffer buf
2597 (package-menu--generate nil packages keywords))
2600 (switch-to-buffer buf))))
2602 ;; package-menu--generate rebinds "q" on the fly, so we have to
2603 ;; hard-code the binding in the doc-string here.
2604 (defun package-menu-filter (keyword)
2605 "Filter the *Packages* buffer.
2606 Show only those items that relate to the specified KEYWORD.
2607 To restore the full package list, type `q'."
2608 (interactive (list (completing-read "Keyword: " (package-all-keywords))))
2609 (package-show-package-list t (list keyword)))
2611 (defun package-list-packages-no-fetch ()
2612 "Display a list of packages.
2613 Does not fetch the updated list of packages before displaying.
2614 The list is displayed in a buffer named `*Packages*'."
2620 ;;; package.el ends here