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 'cl-lib
))
165 (eval-when-compile (require 'epg
)) ;For setf accessors.
167 (require 'tabulated-list
)
170 (defgroup package nil
171 "Manager for Emacs Lisp packages."
176 (defcustom package-enable-at-startup t
177 "Whether to activate installed packages when Emacs starts.
178 If non-nil, packages are activated after reading the init file
179 and before `after-init-hook'. Activation is not done if
180 `user-init-file' is nil (e.g. Emacs was started with \"-q\").
182 Even if the value is nil, you can type \\[package-initialize] to
183 activate the package system at any time."
188 (defcustom package-load-list
'(all)
189 "List of packages for `package-initialize' to load.
190 Each element in this list should be a list (NAME VERSION), or the
191 symbol `all'. The symbol `all' says to load the latest installed
192 versions of all packages not specified by other elements.
194 For an element (NAME VERSION), NAME is a package name (a symbol).
195 VERSION should be t, a string, or nil.
196 If VERSION is t, the most recent version is activated.
197 If VERSION is a string, only that version is ever loaded.
198 Any other version, even if newer, is silently ignored.
199 Hence, the package is \"held\" at that version.
200 If VERSION is nil, the package is not loaded (it is \"disabled\")."
201 :type
'(repeat symbol
)
206 (defvar Info-directory-list
)
207 (declare-function info-initialize
"info" ())
208 (declare-function url-http-file-exists-p
"url-http" (url))
209 (declare-function lm-header
"lisp-mnt" (header))
210 (declare-function lm-commentary
"lisp-mnt" (&optional file
))
212 (defcustom package-archives
'(("gnu" .
"http://elpa.gnu.org/packages/"))
213 "An alist of archives from which to fetch.
214 The default value points to the GNU Emacs package repository.
216 Each element has the form (ID . LOCATION).
217 ID is an archive name, as a string.
218 LOCATION specifies the base location for the archive.
219 If it starts with \"http:\", it is treated as a HTTP URL;
220 otherwise it should be an absolute directory name.
221 (Other types of URL are currently not supported.)
223 Only add locations that you trust, since fetching and installing
224 a package can run arbitrary code."
225 :type
'(alist :key-type
(string :tag
"Archive name")
226 :value-type
(string :tag
"URL or directory name"))
231 (defcustom package-archive-priorities nil
232 "An alist of priorities for packages.
234 Each element has the form (ARCHIVE-ID . PRIORITY).
236 When installing packages, the package with the highest version
237 number from the archive with the highest priority is
238 selected. When higher versions are available from archives with
239 lower priorities, the user has to select those manually.
241 Archives not in this list have the priority 0."
242 :type
'(alist :key-type
(string :tag
"Archive name")
243 :value-type
(integer :tag
"Priority (default is 0)"))
248 (defcustom package-pinned-packages nil
249 "An alist of packages that are pinned to specific archives.
250 This can be useful if you have multiple package archives enabled,
251 and want to control which archive a given package gets installed from.
253 Each element of the alist has the form (PACKAGE . ARCHIVE), where:
254 PACKAGE is a symbol representing a package
255 ARCHIVE is a string representing an archive (it should be the car of
256 an element in `package-archives', e.g. \"gnu\").
258 Adding an entry to this variable means that only ARCHIVE will be
259 considered as a source for PACKAGE. If other archives provide PACKAGE,
260 they are ignored (for this package). If ARCHIVE does not contain PACKAGE,
261 the package will be unavailable."
262 :type
'(alist :key-type
(symbol :tag
"Package")
263 :value-type
(string :tag
"Archive name"))
264 ;; I don't really see why this is risky...
265 ;; I suppose it could prevent you receiving updates for a package,
266 ;; via an entry (PACKAGE . NON-EXISTING). Which could be an issue
267 ;; if PACKAGE has a known vulnerability that is fixed in newer versions.
272 (defconst package-archive-version
1
273 "Version number of the package archive understood by this file.
274 Lower version numbers than this will probably be understood as well.")
276 ;; We don't prime the cache since it tends to get out of date.
277 (defvar package-archive-contents nil
278 "Cache of the contents of the Emacs Lisp Package Archive.
279 This is an alist mapping package names (symbols) to
280 non-empty lists of `package-desc' structures.")
281 (put 'package-archive-contents
'risky-local-variable t
)
283 (defcustom package-user-dir
(locate-user-emacs-file "elpa")
284 "Directory containing the user's Emacs Lisp packages.
285 The directory name should be absolute.
286 Apart from this directory, Emacs also looks for system-wide
287 packages in `package-directory-list'."
293 (defcustom package-directory-list
294 ;; Defaults are subdirs named "elpa" in the site-lisp dirs.
296 (dolist (f load-path
)
298 (equal (file-name-nondirectory f
) "site-lisp")
299 (push (expand-file-name "elpa" f
) result
)))
301 "List of additional directories containing Emacs Lisp packages.
302 Each directory name should be absolute.
304 These directories contain packages intended for system-wide; in
305 contrast, `package-user-dir' contains packages for personal use."
306 :type
'(repeat directory
)
311 (defvar epg-gpg-program
)
313 (defcustom package-check-signature
314 (if (progn (require 'epg-config
) (executable-find epg-gpg-program
))
316 "Non-nil means to check package signatures when installing.
317 The value `allow-unsigned' means to still install a package even if
320 This also applies to the \"archive-contents\" file that lists the
321 contents of the archive."
322 :type
'(choice (const nil
:tag
"Never")
323 (const allow-unsigned
:tag
"Allow unsigned")
324 (const t
:tag
"Check always"))
329 (defcustom package-unsigned-archives nil
330 "List of archives where we do not check for package signatures."
331 :type
'(repeat (string :tag
"Archive name"))
336 (defcustom package-selected-packages nil
337 "Store here packages installed explicitely by user.
338 This variable will be feeded automatically by emacs,
339 when installing a new package.
340 This variable will be used by `package-autoremove' to decide
341 which packages are no more needed.
342 You can use it to (re)install packages on other machines
343 by running `package-user-selected-packages-install'."
345 :type
'(repeat symbol
))
347 (defvar package--default-summary
"No description available.")
349 (cl-defstruct (package-desc
350 ;; Rename the default constructor from `make-package-desc'.
351 (:constructor package-desc-create
)
352 ;; Has the same interface as the old `define-package',
353 ;; which is still used in the "foo-pkg.el" files. Extra
354 ;; options can be supported by adding additional keys.
356 package-desc-from-define
357 (name-string version-string
&optional summary requirements
360 (name (intern name-string
))
361 (version (version-to-list version-string
))
362 (reqs (mapcar #'(lambda (elt)
364 (version-to-list (cadr elt
))))
365 (if (eq 'quote
(car requirements
))
368 (kind (plist-get rest-plist
:kind
))
369 (archive (plist-get rest-plist
:archive
))
372 (unless (memq (car rest-plist
) '(:kind
:archive
))
373 (let ((value (cadr rest-plist
)))
375 (push (cons (car rest-plist
)
376 (if (eq (car-safe value
) 'quote
)
380 (setq rest-plist
(cddr rest-plist
)))
382 "Structure containing information about an individual package.
385 `name' Name of the package, as a symbol.
387 `version' Version of the package, as a version list.
389 `summary' Short description of the package, typically taken from
390 the first line of the file.
392 `reqs' Requirements of the package. A list of (PACKAGE
393 VERSION-LIST) naming the dependent package and the minimum
396 `kind' The distribution format of the package. Currently, it is
397 either `single' or `tar'.
399 `archive' The name of the archive (as a string) whence this
402 `dir' The directory where the package is installed (if installed),
403 `builtin' if it is built-in, or nil otherwise.
405 `extras' Optional alist of additional keyword-value pairs.
407 `signed' Flag to indicate that the package is signed by provider."
410 (summary package--default-summary
)
419 (defun package-desc-full-name (pkg-desc)
421 (package-desc-name pkg-desc
)
422 (package-version-join (package-desc-version pkg-desc
))))
424 (defun package-desc-suffix (pkg-desc)
425 (pcase (package-desc-kind pkg-desc
)
429 (kind (error "Unknown package kind: %s" kind
))))
431 (defun package-desc--keywords (pkg-desc)
432 (let ((keywords (cdr (assoc :keywords
(package-desc-extras pkg-desc
)))))
433 (if (eq (car-safe keywords
) 'quote
)
437 ;; Package descriptor format used in finder-inf.el and package--builtins.
438 (cl-defstruct (package--bi-desc
439 (:constructor package-make-builtin
(version summary
))
445 (defvar package--builtins nil
446 "Alist of built-in packages.
447 The actual value is initialized by loading the library
448 `finder-inf'; this is not done until it is needed, e.g. by the
449 function `package-built-in-p'.
451 Each element has the form (PKG . PACKAGE-BI-DESC), where PKG is a package
452 name (a symbol) and DESC is a `package--bi-desc' structure.")
453 (put 'package--builtins
'risky-local-variable t
)
455 (defvar package-alist nil
456 "Alist of all packages available for activation.
457 Each element has the form (PKG . DESCS), where PKG is a package
458 name (a symbol) and DESCS is a non-empty list of `package-desc' structure,
459 sorted by decreasing versions.
461 This variable is set automatically by `package-load-descriptor',
462 called via `package-initialize'. To change which packages are
463 loaded and/or activated, customize `package-load-list'.")
464 (put 'package-alist
'risky-local-variable t
)
466 (defvar package-activated-list nil
467 ;; FIXME: This should implicitly include all builtin packages.
468 "List of the names of currently activated packages.")
469 (put 'package-activated-list
'risky-local-variable t
)
471 (defun package-version-join (vlist)
472 "Return the version string corresponding to the list VLIST.
473 This is, approximately, the inverse of `version-to-list'.
474 \(Actually, it returns only one of the possible inverses, since
475 `version-to-list' is a many-to-one operation.)"
478 (let ((str-list (list "." (int-to-string (car vlist
)))))
479 (dolist (num (cdr vlist
))
482 (push (int-to-string num
) str-list
)
485 (error "Invalid version list `%s'" vlist
))
487 ;; pre, or beta, or alpha
488 (cond ((equal "." (car str-list
))
490 ((not (string-match "[0-9]+" (car str-list
)))
491 (error "Invalid version list `%s'" vlist
)))
492 (push (cond ((= num -
1) "pre")
495 ((= num -
4) "snapshot"))
497 (if (equal "." (car str-list
))
499 (apply 'concat
(nreverse str-list
)))))
501 (defun package-load-descriptor (pkg-dir)
502 "Load the description file in directory PKG-DIR."
503 (let ((pkg-file (expand-file-name (package--description-file pkg-dir
)
505 (signed-file (concat pkg-dir
".signed")))
506 (when (file-exists-p pkg-file
)
508 (insert-file-contents pkg-file
)
509 (goto-char (point-min))
510 (let ((pkg-desc (package-process-define-package
511 (read (current-buffer)) pkg-file
)))
512 (setf (package-desc-dir pkg-desc
) pkg-dir
)
513 (if (file-exists-p signed-file
)
514 (setf (package-desc-signed pkg-desc
) t
))
517 (defun package-load-all-descriptors ()
518 "Load descriptors for installed Emacs Lisp packages.
519 This looks for package subdirectories in `package-user-dir' and
520 `package-directory-list'. The variable `package-load-list'
521 controls which package subdirectories may be loaded.
523 In each valid package subdirectory, this function loads the
524 description file containing a call to `define-package', which
525 updates `package-alist'."
526 (dolist (dir (cons package-user-dir package-directory-list
))
527 (when (file-directory-p dir
)
528 (dolist (subdir (directory-files dir
))
529 (let ((pkg-dir (expand-file-name subdir dir
)))
530 (when (file-directory-p pkg-dir
)
531 (package-load-descriptor pkg-dir
)))))))
533 (defun package-disabled-p (pkg-name version
)
534 "Return whether PKG-NAME at VERSION can be activated.
535 The decision is made according to `package-load-list'.
536 Return nil if the package can be activated.
537 Return t if the package is completely disabled.
538 Return the max version (as a string) if the package is held at a lower version."
539 (let ((force (assq pkg-name package-load-list
)))
540 (cond ((null force
) (not (memq 'all package-load-list
)))
541 ((null (setq force
(cadr force
))) t
) ; disabled
543 ((stringp force
) ; held
544 (unless (version-list-= version
(version-to-list force
))
546 (t (error "Invalid element in `package-load-list'")))))
548 (defun package-activate-1 (pkg-desc &optional reload
)
549 "Activate package given by PKG-DESC, even if it was already active.
550 If RELOAD is non-nil, also `load' any files inside the package which
551 correspond to previously loaded files (those returned by
552 `package--list-loaded-files')."
553 (let* ((name (package-desc-name pkg-desc
))
554 (pkg-dir (package-desc-dir pkg-desc
))
555 (pkg-dir-dir (file-name-as-directory pkg-dir
)))
557 (error "Internal error: unable to find directory for `%s'"
558 (package-desc-full-name pkg-desc
)))
559 ;; Add to load path, add autoloads, and activate the package.
560 (let* ((old-lp load-path
)
561 (autoloads-file (expand-file-name
562 (format "%s-autoloads" name
) pkg-dir
))
563 (loaded-files-list (and reload
(package--list-loaded-files pkg-dir
))))
564 (with-demoted-errors "Error in package-activate-1: %s"
565 (load autoloads-file nil t
))
566 (when (and (eq old-lp load-path
)
567 (not (or (member pkg-dir load-path
)
568 (member pkg-dir-dir load-path
))))
569 ;; Old packages don't add themselves to the `load-path', so we have to
571 (push pkg-dir load-path
))
572 ;; Call `load' on all files in `pkg-dir' already present in
573 ;; `load-history'. This is done so that macros in these files are updated
574 ;; to their new definitions. If another package is being installed which
575 ;; depends on this new definition, not doing this update would cause
576 ;; compilation errors and break the installation.
577 (with-demoted-errors "Error in package-activate-1: %s"
578 (mapc (lambda (feature) (load feature nil t
))
579 ;; Skip autoloads file since we already evaluated it above.
580 (remove (file-truename autoloads-file
) loaded-files-list
))))
582 (when (file-exists-p (expand-file-name "dir" pkg-dir
))
583 ;; FIXME: not the friendliest, but simple.
586 (push pkg-dir Info-directory-list
))
587 (push name package-activated-list
)
591 (declare-function find-library-name
"find-func" (library))
592 (defun package--list-loaded-files (dir)
593 "Recursively list all files in DIR which correspond to loaded features.
594 Returns the `file-name-sans-extension' of each file, relative to
595 DIR, sorted by most recently loaded last."
596 (let* ((history (delq nil
599 (and f
(file-name-sans-extension f
))))
601 (dir (file-truename dir
))
602 ;; List all files that have already been loaded.
607 (lambda (x) (let* ((file (file-relative-name x dir
))
608 ;; Previously loaded file, if any.
611 (file-name-sans-extension
612 (file-truename (find-library-name file
)))))
613 (pos (when previous
(member previous history
))))
614 ;; Return (RELATIVE-FILENAME . HISTORY-POSITION)
616 (cons (file-name-sans-extension file
) (length pos
)))))
617 (directory-files-recursively dir
"\\`[^\\.].*\\.el\\'")))))
618 ;; Turn the list of (FILENAME . POS) back into a list of features. Files in
619 ;; subdirectories are returned relative to DIR (so not actually features).
620 (let ((default-directory (file-name-as-directory dir
)))
621 (mapcar (lambda (x) (file-truename (car x
)))
622 (sort list-of-conflicts
623 ;; Sort the files by ascending HISTORY-POSITION.
624 (lambda (x y
) (< (cdr x
) (cdr y
))))))))
626 (defun package-built-in-p (package &optional min-version
)
627 "Return true if PACKAGE is built-in to Emacs.
628 Optional arg MIN-VERSION, if non-nil, should be a version list
629 specifying the minimum acceptable version."
630 (if (package-desc-p package
) ;; was built-in and then was converted
631 (eq 'builtin
(package-desc-dir package
))
632 (let ((bi (assq package package--builtin-versions
)))
634 (bi (version-list-<= min-version
(cdr bi
)))
635 ((remove 0 min-version
) nil
)
637 (require 'finder-inf nil t
) ; For `package--builtins'.
638 (assq package package--builtins
))))))
640 (defun package--from-builtin (bi-desc)
641 (package-desc-create :name
(pop bi-desc
)
642 :version
(package--bi-desc-version bi-desc
)
643 :summary
(package--bi-desc-summary bi-desc
)
646 ;; This function goes ahead and activates a newer version of a package
647 ;; if an older one was already activated. This is not ideal; we'd at
648 ;; least need to check to see if the package has actually been loaded,
649 ;; and not merely activated.
650 (defun package-activate (package &optional force
)
651 "Activate package PACKAGE.
652 If FORCE is true, (re-)activate it if it's already activated."
653 (let ((pkg-descs (cdr (assq package package-alist
))))
654 ;; Check if PACKAGE is available in `package-alist'.
657 (let ((available-version (package-desc-version (car pkg-descs
))))
658 (or (package-disabled-p package available-version
)
659 ;; Prefer a builtin package.
660 (package-built-in-p package available-version
))))
661 (setq pkg-descs
(cdr pkg-descs
)))
663 ;; If no such package is found, maybe it's built-in.
665 (package-built-in-p package
))
666 ;; If the package is already activated, just return t.
667 ((and (memq package package-activated-list
) (not force
))
669 ;; Otherwise, proceed with activation.
671 (let* ((pkg-vec (car pkg-descs
))
672 (fail (catch 'dep-failure
673 ;; Activate its dependencies recursively.
674 (dolist (req (package-desc-reqs pkg-vec
))
675 (unless (package-activate (car req
))
676 (throw 'dep-failure req
))))))
678 (warn "Unable to activate package `%s'.
679 Required package `%s-%s' is unavailable"
680 package
(car fail
) (package-version-join (cadr fail
)))
681 ;; If all goes well, activate the package itself.
682 (package-activate-1 pkg-vec force
)))))))
684 (defun define-package (_name-string _version-string
685 &optional _docstring _requirements
686 &rest _extra-properties
)
687 "Define a new package.
688 NAME-STRING is the name of the package, as a string.
689 VERSION-STRING is the version of the package, as a string.
690 DOCSTRING is a short description of the package, a string.
691 REQUIREMENTS is a list of dependencies on other packages.
692 Each requirement is of the form (OTHER-PACKAGE OTHER-VERSION),
693 where OTHER-VERSION is a string.
695 EXTRA-PROPERTIES is currently unused."
696 ;; FIXME: Placeholder! Should we keep it?
697 (error "Don't call me!"))
699 (defun package-process-define-package (exp origin
)
700 (unless (eq (car-safe exp
) 'define-package
)
701 (error "Can't find define-package in %s" origin
))
702 (let* ((new-pkg-desc (apply #'package-desc-from-define
(cdr exp
)))
703 (name (package-desc-name new-pkg-desc
))
704 (version (package-desc-version new-pkg-desc
))
705 (old-pkgs (assq name package-alist
)))
707 ;; If there's no old package, just add this to `package-alist'.
708 (push (list name new-pkg-desc
) package-alist
)
709 ;; If there is, insert the new package at the right place in the list.
711 (if (and (cdr old-pkgs
)
712 (version-list-< version
713 (package-desc-version (cadr old-pkgs
))))
714 (setq old-pkgs
(cdr old-pkgs
))
715 (push new-pkg-desc
(cdr old-pkgs
))
719 ;; From Emacs 22, but changed so it adds to load-path.
720 (defun package-autoload-ensure-default-file (file)
721 "Make sure that the autoload file FILE exists and if not create it."
722 (unless (file-exists-p file
)
724 (concat ";;; " (file-name-nondirectory file
)
725 " --- automatically extracted autoloads\n"
728 "(add-to-list 'load-path (or (file-name-directory #$) (car load-path)))\n"
729 "\f\n;; Local Variables:\n"
730 ";; version-control: never\n"
731 ";; no-byte-compile: t\n"
732 ";; no-update-autoloads: t\n"
734 ";;; " (file-name-nondirectory file
)
736 nil file nil
'silent
))
739 (defvar generated-autoload-file
)
740 (defvar version-control
)
742 (defun package-generate-autoloads (name pkg-dir
)
743 (let* ((auto-name (format "%s-autoloads.el" name
))
744 ;;(ignore-name (concat name "-pkg.el"))
745 (generated-autoload-file (expand-file-name auto-name pkg-dir
))
747 (version-control 'never
))
748 (package-autoload-ensure-default-file generated-autoload-file
)
749 (update-directory-autoloads pkg-dir
)
750 (let ((buf (find-buffer-visiting generated-autoload-file
)))
751 (when buf
(kill-buffer buf
)))
754 (defvar tar-parse-info
)
755 (declare-function tar-untar-buffer
"tar-mode" ())
756 (declare-function tar-header-name
"tar-mode" (tar-header) t
)
757 (declare-function tar-header-link-type
"tar-mode" (tar-header) t
)
759 (defun package-untar-buffer (dir)
760 "Untar the current buffer.
761 This uses `tar-untar-buffer' from Tar mode. All files should
762 untar into a directory named DIR; otherwise, signal an error."
765 ;; Make sure everything extracts into DIR.
766 (let ((regexp (concat "\\`" (regexp-quote (expand-file-name dir
)) "/"))
767 (case-fold-search (memq system-type
'(windows-nt ms-dos cygwin
))))
768 (dolist (tar-data tar-parse-info
)
769 (let ((name (expand-file-name (tar-header-name tar-data
))))
770 (or (string-match regexp name
)
771 ;; Tarballs created by some utilities don't list
772 ;; directories with a trailing slash (Bug#13136).
773 (and (string-equal dir name
)
774 (eq (tar-header-link-type tar-data
) 5))
775 (error "Package does not untar cleanly into directory %s/" dir
)))))
778 (defun package-generate-description-file (pkg-desc pkg-file
)
779 "Create the foo-pkg.el file for single-file packages."
780 (let* ((name (package-desc-name pkg-desc
)))
781 (let ((print-level nil
)
786 ";;; -*- no-byte-compile: t -*-\n"
789 (list 'define-package
791 (package-version-join (package-desc-version pkg-desc
))
792 (package-desc-summary pkg-desc
)
793 (let ((requires (package-desc-reqs pkg-desc
)))
795 ;; Turn version lists into string form.
799 (package-version-join (cadr elt
))))
801 (package--alist-to-plist-args
802 (package-desc-extras pkg-desc
))))
804 nil pkg-file nil
'silent
))))
806 (defun package--alist-to-plist-args (alist)
807 (mapcar 'macroexp-quote
809 (mapcar (lambda (pair) (list (car pair
) (cdr pair
))) alist
))))
810 (defun package-unpack (pkg-desc)
811 "Install the contents of the current buffer as a package."
812 (let* ((name (package-desc-name pkg-desc
))
813 (dirname (package-desc-full-name pkg-desc
))
814 (pkg-dir (expand-file-name dirname package-user-dir
)))
815 (pcase (package-desc-kind pkg-desc
)
817 (make-directory pkg-dir t
)
820 default-directory
'full
"\\`[^.].*\\.el\\'" 'nosort
)))
821 (dolist (source-file file-list
)
822 (let ((target-el-file
823 (expand-file-name (file-name-nondirectory source-file
) pkg-dir
)))
824 (copy-file source-file target-el-file t
)))
825 ;; Now that the files have been installed, this package is
826 ;; indistinguishable from a `tar' or a `single'. Let's make
827 ;; things simple by ensuring we're one of them.
828 (setf (package-desc-kind pkg-desc
)
829 (if (> (length file-list
) 1) 'tar
'single
))))
831 (make-directory package-user-dir t
)
832 ;; FIXME: should we delete PKG-DIR if it exists?
833 (let* ((default-directory (file-name-as-directory package-user-dir
)))
834 (package-untar-buffer dirname
)))
836 (let ((el-file (expand-file-name (format "%s.el" name
) pkg-dir
)))
837 (make-directory pkg-dir t
)
838 (package--write-file-no-coding el-file
)))
839 (kind (error "Unknown package kind: %S" kind
)))
840 (package--make-autoloads-and-stuff pkg-desc pkg-dir
)
841 ;; Update package-alist.
842 (let ((new-desc (package-load-descriptor pkg-dir
)))
843 ;; FIXME: Check that `new-desc' matches `desc'!
844 ;; FIXME: Compilation should be done as a separate, optional, step.
845 ;; E.g. for multi-package installs, we should first install all packages
846 ;; and then compile them.
847 (package--compile new-desc
))
848 ;; Try to activate it.
849 (package-activate name
'force
)
852 (defun package--make-autoloads-and-stuff (pkg-desc pkg-dir
)
853 "Generate autoloads, description file, etc.. for PKG-DESC installed at PKG-DIR."
854 (package-generate-autoloads (package-desc-name pkg-desc
) pkg-dir
)
855 (let ((desc-file (expand-file-name (package--description-file pkg-dir
)
857 (unless (file-exists-p desc-file
)
858 (package-generate-description-file pkg-desc desc-file
)))
859 ;; FIXME: Create foo.info and dir file from foo.texi?
862 (defun package--compile (pkg-desc)
863 "Byte-compile installed package PKG-DESC."
864 (package-activate-1 pkg-desc
)
865 (byte-recompile-directory (package-desc-dir pkg-desc
) 0 t
))
867 (defun package--write-file-no-coding (file-name)
868 (let ((buffer-file-coding-system 'no-conversion
))
869 (write-region (point-min) (point-max) file-name nil
'silent
)))
871 (defmacro package--with-work-buffer
(location file
&rest body
)
872 "Run BODY in a buffer containing the contents of FILE at LOCATION.
873 LOCATION is the base location of a package archive, and should be
874 one of the URLs (or file names) specified in `package-archives'.
875 FILE is the name of a file relative to that base location.
877 This macro retrieves FILE from LOCATION into a temporary buffer,
878 and evaluates BODY while that buffer is current. This work
879 buffer is killed afterwards. Return the last value in BODY."
880 (declare (indent 2) (debug t
))
882 (if (string-match-p "\\`https?:" ,location
)
883 (url-insert-file-contents (concat ,location
,file
))
884 (unless (file-name-absolute-p ,location
)
885 (error "Archive location %s is not an absolute file name"
887 (insert-file-contents (expand-file-name ,file
,location
)))
890 (defun package--archive-file-exists-p (location file
)
891 (let ((http (string-match "\\`https?:" location
)))
895 (url-http-file-exists-p (concat location file
)))
896 (file-exists-p (expand-file-name file location
)))))
898 (declare-function epg-make-context
"epg"
899 (&optional protocol armor textmode include-certs
903 (declare-function epg-verify-string
"epg" (context signature
904 &optional signed-text
))
905 (declare-function epg-context-result-for
"epg" (context name
))
906 (declare-function epg-signature-status
"epg" (signature))
907 (declare-function epg-signature-to-string
"epg" (signature))
909 (defun package--display-verify-error (context sig-file
)
910 (unless (equal (epg-context-error-output context
) "")
911 (with-output-to-temp-buffer "*Error*"
912 (with-current-buffer standard-output
913 (if (epg-context-result-for context
'verify
)
914 (insert (format "Failed to verify signature %s:\n" sig-file
)
915 (mapconcat #'epg-signature-to-string
916 (epg-context-result-for context
'verify
)
918 (insert (format "Error while verifying signature %s:\n" sig-file
)))
919 (insert "\nCommand output:\n" (epg-context-error-output context
))))))
921 (defun package--check-signature (location file
)
922 "Check signature of the current buffer.
923 GnuPG keyring is located under \"gnupg\" in `package-user-dir'."
924 (let* ((context (epg-make-context 'OpenPGP
))
925 (homedir (expand-file-name "gnupg" package-user-dir
))
926 (sig-file (concat file
".sig"))
927 (sig-content (package--with-work-buffer location sig-file
929 (setf (epg-context-home-directory context
) homedir
)
930 (condition-case error
931 (epg-verify-string context sig-content
(buffer-string))
933 (package--display-verify-error context sig-file
)
934 (signal (car error
) (cdr error
))))
935 (let (good-signatures had-fatal-error
)
936 ;; The .sig file may contain multiple signatures. Success if one
937 ;; of the signatures is good.
938 (dolist (sig (epg-context-result-for context
'verify
))
939 (if (eq (epg-signature-status sig
) 'good
)
940 (push sig good-signatures
)
941 ;; If package-check-signature is allow-unsigned, don't
942 ;; signal error when we can't verify signature because of
943 ;; missing public key. Other errors are still treated as
944 ;; fatal (bug#17625).
945 (unless (and (eq package-check-signature
'allow-unsigned
)
946 (eq (epg-signature-status sig
) 'no-pubkey
))
947 (setq had-fatal-error t
))))
948 (when (and (null good-signatures
) had-fatal-error
)
949 (package--display-verify-error context sig-file
)
950 (error "Failed to verify signature %s" sig-file
))
953 (defun package-install-from-archive (pkg-desc)
954 "Download and install a tar package."
955 ;; This won't happen, unless the archive is doing something wrong.
956 (when (eq (package-desc-kind pkg-desc
) 'dir
)
957 (error "Can't install directory package from archive"))
958 (let* ((location (package-archive-base pkg-desc
))
959 (file (concat (package-desc-full-name pkg-desc
)
960 (package-desc-suffix pkg-desc
)))
961 (sig-file (concat file
".sig"))
962 good-signatures pkg-descs
)
963 (package--with-work-buffer location file
964 (if (and package-check-signature
965 (not (member (package-desc-archive pkg-desc
)
966 package-unsigned-archives
)))
967 (if (package--archive-file-exists-p location sig-file
)
968 (setq good-signatures
(package--check-signature location file
))
969 (unless (eq package-check-signature
'allow-unsigned
)
970 (error "Unsigned package: `%s'"
971 (package-desc-name pkg-desc
)))))
972 (package-unpack pkg-desc
))
973 ;; Here the package has been installed successfully, mark it as
974 ;; signed if appropriate.
975 (when good-signatures
976 ;; Write out good signatures into NAME-VERSION.signed file.
977 (write-region (mapconcat #'epg-signature-to-string good-signatures
"\n")
980 (concat (package-desc-full-name pkg-desc
)
984 ;; Update the old pkg-desc which will be shown on the description buffer.
985 (setf (package-desc-signed pkg-desc
) t
)
986 ;; Update the new (activated) pkg-desc as well.
987 (setq pkg-descs
(cdr (assq (package-desc-name pkg-desc
) package-alist
)))
989 (setf (package-desc-signed (car pkg-descs
)) t
)))))
991 (defvar package--initialized nil
)
993 (defun package-installed-p (package &optional min-version
)
994 "Return true if PACKAGE, of MIN-VERSION or newer, is installed.
995 MIN-VERSION should be a version list."
996 (unless package--initialized
(error "package.el is not yet initialized!"))
998 (let ((pkg-descs (cdr (assq package package-alist
))))
1000 (version-list-<= min-version
1001 (package-desc-version (car pkg-descs
)))))
1002 ;; Also check built-in packages.
1003 (package-built-in-p package min-version
)))
1005 (defun package-compute-transaction (packages requirements
&optional seen
)
1006 "Return a list of packages to be installed, including PACKAGES.
1007 PACKAGES should be a list of `package-desc'.
1009 REQUIREMENTS should be a list of additional requirements; each
1010 element in this list should have the form (PACKAGE VERSION-LIST),
1011 where PACKAGE is a package name and VERSION-LIST is the required
1012 version of that package.
1014 This function recursively computes the requirements of the
1015 packages in REQUIREMENTS, and returns a list of all the packages
1016 that must be installed. Packages that are already installed are
1017 not included in this list.
1019 SEEN is used internally to detect infinite recursion."
1020 ;; FIXME: We really should use backtracking to explore the whole
1021 ;; search space (e.g. if foo require bar-1.3, and bar-1.4 requires toto-1.1
1022 ;; whereas bar-1.3 requires toto-1.0 and the user has put a hold on toto-1.0:
1023 ;; the current code might fail to see that it could install foo by using the
1025 (dolist (elt requirements
)
1026 (let* ((next-pkg (car elt
))
1027 (next-version (cadr elt
))
1029 (dolist (pkg packages
)
1030 (if (eq next-pkg
(package-desc-name pkg
))
1031 (setq already pkg
)))
1033 (if (version-list-<= next-version
(package-desc-version already
))
1034 ;; `next-pkg' is already in `packages', but its position there
1035 ;; means it might be installed too late: remove it from there, so
1036 ;; we re-add it (along with its dependencies) at an earlier place
1037 ;; below (bug#16994).
1038 (if (memq already seen
) ;Avoid inf-loop on dependency cycles.
1039 (message "Dependency cycle going through %S"
1040 (package-desc-full-name already
))
1041 (setq packages
(delq already packages
))
1043 (error "Need package `%s-%s', but only %s is being installed"
1044 next-pkg
(package-version-join next-version
)
1045 (package-version-join (package-desc-version already
)))))
1048 ((package-installed-p next-pkg next-version
) nil
)
1051 ;; A package is required, but not installed. It might also be
1052 ;; blocked via `package-load-list'.
1053 (let ((pkg-descs (cdr (assq next-pkg package-archive-contents
)))
1056 (while (and pkg-descs
(not found
))
1057 (let* ((pkg-desc (pop pkg-descs
))
1058 (version (package-desc-version pkg-desc
))
1059 (disabled (package-disabled-p next-pkg version
)))
1061 ((version-list-< version next-version
)
1063 "Need package `%s-%s', but only %s is available"
1064 next-pkg
(package-version-join next-version
)
1065 (package-version-join version
)))
1069 (if (stringp disabled
)
1070 (format "Package `%s' held at version %s, \
1071 but version %s required"
1073 (package-version-join next-version
))
1074 (format "Required package '%s' is disabled"
1076 (t (setq found pkg-desc
)))))
1079 (error "%s" problem
)
1080 (error "Package `%s-%s' is unavailable"
1081 next-pkg
(package-version-join next-version
))))
1083 (package-compute-transaction (cons found packages
)
1084 (package-desc-reqs found
)
1085 (cons found seen
))))))))
1088 (defun package-read-from-string (str)
1089 "Read a Lisp expression from STR.
1090 Signal an error if the entire string was not used."
1091 (let* ((read-data (read-from-string str
))
1094 ;; The call to `ignore' suppresses a compiler warning.
1095 (progn (ignore (read-from-string
1096 (substring str
(cdr read-data
))))
1098 (end-of-file nil
))))
1100 (error "Can't read whole string")
1103 (defun package--read-archive-file (file)
1104 "Re-read archive file FILE, if it exists.
1105 Will return the data from the file, or nil if the file does not exist.
1106 Will throw an error if the archive version is too new."
1107 (let ((filename (expand-file-name file package-user-dir
)))
1108 (when (file-exists-p filename
)
1110 (insert-file-contents-literally filename
)
1111 (let ((contents (read (current-buffer))))
1112 (if (> (car contents
) package-archive-version
)
1113 (error "Package archive version %d is higher than %d"
1114 (car contents
) package-archive-version
))
1117 (defun package-read-all-archive-contents ()
1118 "Re-read `archive-contents', if it exists.
1119 If successful, set `package-archive-contents'."
1120 (setq package-archive-contents nil
)
1121 (dolist (archive package-archives
)
1122 (package-read-archive-contents (car archive
))))
1124 (defun package-read-archive-contents (archive)
1125 "Re-read archive contents for ARCHIVE.
1126 If successful, set the variable `package-archive-contents'.
1127 If the archive version is too new, signal an error."
1128 ;; Version 1 of 'archive-contents' is identical to our internal
1130 (let* ((contents-file (format "archives/%s/archive-contents" archive
))
1131 (contents (package--read-archive-file contents-file
)))
1133 (dolist (package contents
)
1134 (package--add-to-archive-contents package archive
)))))
1136 ;; Package descriptor objects used inside the "archive-contents" file.
1137 ;; Changing this defstruct implies changing the format of the
1138 ;; "archive-contents" files.
1139 (cl-defstruct (package--ac-desc
1140 (:constructor package-make-ac-desc
(version reqs summary kind extras
))
1143 version reqs summary kind extras
)
1145 (defun package--add-to-archive-contents (package archive
)
1146 "Add the PACKAGE from the given ARCHIVE if necessary.
1147 PACKAGE should have the form (NAME . PACKAGE--AC-DESC).
1148 Also, add the originating archive to the `package-desc' structure."
1149 (let* ((name (car package
))
1150 (version (package--ac-desc-version (cdr package
)))
1152 (package-desc-create
1155 :reqs
(package--ac-desc-reqs (cdr package
))
1156 :summary
(package--ac-desc-summary (cdr package
))
1157 :kind
(package--ac-desc-kind (cdr package
))
1159 :extras
(and (> (length (cdr package
)) 4)
1160 ;; Older archive-contents files have only 4
1162 (package--ac-desc-extras (cdr package
)))))
1163 (pinned-to-archive (assoc name package-pinned-packages
)))
1164 ;; Skip entirely if pinned to another archive.
1165 (when (not (and pinned-to-archive
1166 (not (equal (cdr pinned-to-archive
) archive
))))
1167 (setq package-archive-contents
1168 (package--append-to-alist pkg-desc package-archive-contents
)))))
1170 (defun package--append-to-alist (pkg-desc alist
)
1171 "Append an entry for PKG-DESC to the start of ALIST and return it.
1172 This entry takes the form (`package-desc-name' PKG-DESC).
1174 If ALIST already has an entry with this name, destructively add
1175 PKG-DESC to the cdr of this entry instead, sorted by version
1177 (let* ((name (package-desc-name pkg-desc
))
1178 (priority-version (package-desc-priority-version pkg-desc
))
1179 (existing-packages (assq name alist
)))
1180 (if (not existing-packages
)
1181 (cons (list name pkg-desc
)
1183 (while (if (and (cdr existing-packages
)
1184 (version-list-< priority-version
1185 (package-desc-priority-version
1186 (cadr existing-packages
))))
1187 (setq existing-packages
(cdr existing-packages
))
1188 (push pkg-desc
(cdr existing-packages
))
1192 (defun package-download-transaction (packages)
1193 "Download and install all the packages in PACKAGES.
1194 PACKAGES should be a list of package-desc.
1195 This function assumes that all package requirements in
1196 PACKAGES are satisfied, i.e. that PACKAGES is computed
1197 using `package-compute-transaction'."
1198 (mapc #'package-install-from-archive packages
))
1201 (defun package-install (pkg &optional mark-selected
)
1202 "Install the package PKG.
1203 PKG can be a package-desc or the package name of one the available packages
1204 in an archive in `package-archives'. Interactively, prompt for its name.
1206 If called interactively or if MARK-SELECTED is non-nil, add PKG
1207 to `package-selected-packages'."
1210 ;; Initialize the package system to get the list of package
1211 ;; symbols for completion.
1212 (unless package--initialized
1213 (package-initialize t
))
1214 (unless package-archive-contents
1215 (package-refresh-contents))
1216 (list (intern (completing-read
1219 (mapcar (lambda (elt)
1220 (unless (package-installed-p (car elt
))
1221 (symbol-name (car elt
))))
1222 package-archive-contents
))
1225 (when (and mark-selected
(not (memq pkg package-selected-packages
)))
1226 (customize-save-variable 'package-selected-packages
1227 (cons pkg package-selected-packages
)))
1228 (package-download-transaction
1229 (if (package-desc-p pkg
)
1230 (package-compute-transaction (list pkg
)
1231 (package-desc-reqs pkg
))
1232 (package-compute-transaction ()
1233 (list (list pkg
))))))
1236 (defun package-reinstall (pkg)
1237 "Reinstall package PKG."
1238 (interactive (list (intern (completing-read
1239 "Reinstall package: "
1240 (mapcar #'symbol-name
1241 (mapcar #'car package-alist
))))))
1242 (package-delete (cadr (assq pkg package-alist
)) t
)
1243 (package-install pkg
))
1245 (defun package-strip-rcs-id (str)
1246 "Strip RCS version ID from the version string STR.
1247 If the result looks like a dotted numeric version, return it.
1248 Otherwise return nil."
1250 (when (string-match "\\`[ \t]*[$]Revision:[ \t]+" str
)
1251 (setq str
(substring str
(match-end 0))))
1253 (if (version-to-list str
)
1257 (declare-function lm-homepage
"lisp-mnt" (&optional file
))
1259 (defun package--prepare-dependencies (deps)
1260 "Turn DEPS into an acceptable list of dependencies.
1262 Any parts missing a version string get a default version string
1263 of \"0\" (meaning any version) and an appropriate level of lists
1264 is wrapped around any parts requiring it."
1267 (error "Invalid requirement specifier: %S" deps
))
1268 (t (mapcar (lambda (dep)
1270 ((symbolp dep
) `(,dep
"0"))
1272 (error "Invalid requirement specifier: %S" dep
))
1273 ((and (listp dep
) (null (cdr dep
)))
1274 (list (car dep
) "0"))
1278 (defun package-buffer-info ()
1279 "Return a `package-desc' describing the package in the current buffer.
1281 If the buffer does not contain a conforming package, signal an
1282 error. If there is a package, narrow the buffer to the file's
1284 (goto-char (point-min))
1285 (unless (re-search-forward "^;;; \\([^ ]*\\)\\.el ---[ \t]*\\(.*?\\)[ \t]*\\(-\\*-.*-\\*-[ \t]*\\)?$" nil t
)
1286 (error "Package lacks a file header"))
1287 (let ((file-name (match-string-no-properties 1))
1288 (desc (match-string-no-properties 2))
1289 (start (line-beginning-position)))
1290 (unless (search-forward (concat ";;; " file-name
".el ends here"))
1291 (error "Package lacks a terminating comment"))
1292 ;; Try to include a trailing newline.
1294 (narrow-to-region start
(point))
1296 ;; Use some headers we've invented to drive the process.
1297 (let* ((requires-str (lm-header "package-requires"))
1298 ;; Prefer Package-Version; if defined, the package author
1299 ;; probably wants us to use it. Otherwise try Version.
1301 (or (package-strip-rcs-id (lm-header "package-version"))
1302 (package-strip-rcs-id (lm-header "version"))))
1303 (homepage (lm-homepage)))
1306 "Package lacks a \"Version\" or \"Package-Version\" header"))
1307 (package-desc-from-define
1308 file-name pkg-version desc
1310 (package--prepare-dependencies
1311 (package-read-from-string requires-str
)))
1315 (declare-function tar-get-file-descriptor
"tar-mode" (file))
1316 (declare-function tar--extract
"tar-mode" (descriptor))
1318 (defun package-tar-file-info ()
1319 "Find package information for a tar file.
1320 The return result is a `package-desc'."
1321 (cl-assert (derived-mode-p 'tar-mode
))
1322 (let* ((dir-name (file-name-directory
1323 (tar-header-name (car tar-parse-info
))))
1324 (desc-file (package--description-file dir-name
))
1325 (tar-desc (tar-get-file-descriptor (concat dir-name desc-file
))))
1327 (error "No package descriptor file found"))
1328 (with-current-buffer (tar--extract tar-desc
)
1330 (package--read-pkg-desc 'tar
)
1331 (kill-buffer (current-buffer))))))
1333 (defun package-dir-info ()
1334 "Find package information for a directory.
1335 The return result is a `package-desc'."
1336 (cl-assert (derived-mode-p 'dired-mode
))
1337 (let* ((desc-file (package--description-file default-directory
)))
1338 (if (file-readable-p desc-file
)
1340 (insert-file-contents desc-file
)
1341 (package--read-pkg-desc 'dir
))
1342 (let ((files (directory-files default-directory t
"\\.el\\'" t
))
1346 (insert-file-contents (pop files
))
1347 ;; When we find the file with the data,
1348 (when (setq info
(ignore-errors (package-buffer-info)))
1351 ;; set the 'dir kind,
1352 (setf (package-desc-kind info
) 'dir
))))
1353 ;; and return the info.
1356 (defun package--read-pkg-desc (kind)
1357 "Read a `define-package' form in current buffer.
1358 Return the pkg-desc, with desc-kind set to KIND."
1359 (goto-char (point-min))
1361 (let* ((pkg-def-parsed (read (current-buffer)))
1363 (if (not (eq (car pkg-def-parsed
) 'define-package
))
1364 (error "Can't find define-package in %s"
1365 (tar-header-name tar-desc
))
1366 (apply #'package-desc-from-define
1367 (append (cdr pkg-def-parsed
))))))
1368 (setf (package-desc-kind pkg-desc
) kind
)
1373 (defun package-install-from-buffer ()
1374 "Install a package from the current buffer.
1375 The current buffer is assumed to be a single .el or .tar file or
1376 a directory. These must follow the packaging guidelines (see
1377 info node `(elisp)Packaging').
1379 Specially, if current buffer is a directory, the -pkg.el
1380 description file is not mandatory, in which case the information
1381 is derived from the main .el file in the directory.
1383 Downloads and installs required packages as needed."
1387 ((derived-mode-p 'dired-mode
)
1388 ;; This is the only way a package-desc object with a `dir'
1389 ;; desc-kind can be created. Such packages can't be
1390 ;; uploaded or installed from archives, they can only be
1391 ;; installed from local buffers or directories.
1393 ((derived-mode-p 'tar-mode
)
1394 (package-tar-file-info))
1396 (package-buffer-info))))
1397 (name (package-desc-name pkg-desc
)))
1398 ;; Download and install the dependencies.
1399 (let* ((requires (package-desc-reqs pkg-desc
))
1400 (transaction (package-compute-transaction nil requires
)))
1401 (package-download-transaction transaction
))
1402 ;; Install the package itself.
1403 (package-unpack pkg-desc
)
1404 (unless (memq name package-selected-packages
)
1405 (push name package-selected-packages
)
1406 (customize-save-variable 'package-selected-packages
1407 package-selected-packages
))
1411 (defun package-install-file (file)
1412 "Install a package from a file.
1413 The file can either be a tar file or an Emacs Lisp file."
1414 (interactive "fPackage file name: ")
1416 (if (file-directory-p file
)
1418 (setq default-directory file
)
1420 (insert-file-contents-literally file
)
1421 (when (string-match "\\.tar\\'" file
) (tar-mode)))
1422 (package-install-from-buffer)))
1424 (defun package--get-deps (pkg &optional only
)
1425 (let* ((pkg-desc (cadr (assq pkg package-alist
)))
1426 (direct-deps (cl-loop for p in
(package-desc-reqs pkg-desc
)
1428 when
(assq name package-alist
)
1430 (indirect-deps (unless (eq only
'direct
)
1433 (mapcar #'package--get-deps direct-deps
)))))
1435 (direct direct-deps
)
1436 (separate (list direct-deps indirect-deps
))
1437 (indirect indirect-deps
)
1438 (t (append direct-deps indirect-deps
)))))
1441 (defun package-install-user-selected-packages ()
1442 "Ensure packages in `package-selected-packages' are installed.
1443 If some packages are not installed propose to install them."
1445 (cl-loop for p in package-selected-packages
1446 unless
(package-installed-p p
)
1451 (format "%s packages will be installed:\n%s, proceed?"
1453 (mapconcat #'symbol-name lst
", ")))
1454 (mapc #'package-install lst
))
1455 (message "All your packages are already installed"))))
1457 (defun package--used-elsewhere-p (pkg-desc &optional pkg-list
)
1458 "Non-nil if PKG-DESC is a dependency of a package in PKG-LIST.
1459 Return the first package found in PKG-LIST of which PKG is a
1462 When not specified, PKG-LIST defaults to `package-alist'
1463 with PKG-DESC entry removed."
1464 (unless (string= (package-desc-status pkg-desc
) "obsolete")
1465 (let ((pkg (package-desc-name pkg-desc
)))
1466 (cl-loop with alist
= (or pkg-list
1467 (remove (assq pkg package-alist
)
1469 for p in alist thereis
1470 (and (memq pkg
(mapcar #'car
(package-desc-reqs (cadr p
))))
1473 (defun package-delete (pkg-desc &optional force
)
1474 "Delete package PKG-DESC.
1476 Argument PKG-DESC is a full description of package as vector.
1477 When package is used elsewhere as dependency of another package,
1478 refuse deleting it and return an error.
1479 If FORCE is non--nil package will be deleted even if it is used
1481 (let ((dir (package-desc-dir pkg-desc
))
1482 (name (package-desc-name pkg-desc
))
1483 pkg-used-elsewhere-by
)
1484 (cond ((not (string-prefix-p (file-name-as-directory
1485 (expand-file-name package-user-dir
))
1486 (expand-file-name dir
)))
1487 ;; Don't delete "system" packages.
1488 (error "Package `%s' is a system package, not deleting"
1489 (package-desc-full-name pkg-desc
)))
1491 (setq pkg-used-elsewhere-by
1492 (package--used-elsewhere-p pkg-desc
)))
1493 ;; Don't delete packages used as dependency elsewhere.
1494 (error "Package `%s' is used by `%s' as dependency, not deleting"
1495 (package-desc-full-name pkg-desc
)
1496 pkg-used-elsewhere-by
))
1498 (delete-directory dir t t
)
1499 ;; Remove NAME-VERSION.signed file.
1500 (let ((signed-file (concat dir
".signed")))
1501 (if (file-exists-p signed-file
)
1502 (delete-file signed-file
)))
1503 ;; Update package-alist.
1504 (let ((pkgs (assq name package-alist
)))
1505 (delete pkg-desc pkgs
)
1507 (setq package-alist
(delq pkgs package-alist
))))
1508 ;; Update package-selected-packages.
1509 (when (memq name package-selected-packages
)
1510 (customize-save-variable
1511 'package-selected-packages
(remove name package-selected-packages
)))
1512 (message "Package `%s' deleted." (package-desc-full-name pkg-desc
))))))
1515 (defun package-autoremove ()
1516 "Remove packages that are no more needed.
1518 Packages that are no more needed by other packages in
1519 `package-selected-packages' and their dependencies
1522 (let ((needed (cl-loop for p in package-selected-packages
1523 if
(assq p package-alist
)
1524 append
(package--get-deps p
))))
1525 (cl-loop for p in
(mapcar #'car package-alist
)
1526 unless
(or (memq p needed
)
1527 (memq p package-selected-packages
))
1530 (when (y-or-n-p (format "%s packages will be deleted:\n%s, proceed? "
1532 (mapconcat #'symbol-name lst
", ")))
1534 (package-delete (cadr (assq p package-alist
)) t
))
1536 (message "Nothing to autoremove")))))
1538 (defun package-archive-base (desc)
1539 "Return the archive containing the package NAME."
1540 (cdr (assoc (package-desc-archive desc
) package-archives
)))
1542 (defun package-archive-priority (archive)
1543 "Return the priority of ARCHIVE.
1545 The archive priorities are specified in
1546 `package-archive-priorities'. If not given there, the priority
1548 (or (cdr (assoc archive package-archive-priorities
))
1551 (defun package-desc-priority-version (pkg-desc)
1552 "Return the version PKG-DESC with the archive priority prepended.
1554 This allows for easy comparison of package versions from
1555 different archives if archive priorities are meant to be taken in
1557 (cons (package-archive-priority
1558 (package-desc-archive pkg-desc
))
1559 (package-desc-version pkg-desc
)))
1561 (defun package--download-one-archive (archive file
)
1562 "Retrieve an archive file FILE from ARCHIVE, and cache it.
1563 ARCHIVE should be a cons cell of the form (NAME . LOCATION),
1564 similar to an entry in `package-alist'. Save the cached copy to
1565 \"archives/NAME/archive-contents\" in `package-user-dir'."
1566 (let ((dir (expand-file-name (format "archives/%s" (car archive
))
1568 (sig-file (concat file
".sig"))
1570 (package--with-work-buffer (cdr archive
) file
1571 ;; Check signature of archive-contents, if desired.
1572 (if (and package-check-signature
1573 (not (member archive package-unsigned-archives
)))
1574 (if (package--archive-file-exists-p (cdr archive
) sig-file
)
1575 (setq good-signatures
(package--check-signature (cdr archive
)
1577 (unless (eq package-check-signature
'allow-unsigned
)
1578 (error "Unsigned archive `%s'"
1580 ;; Read the retrieved buffer to make sure it is valid (e.g. it
1581 ;; may fetch a URL redirect page).
1582 (when (listp (read (current-buffer)))
1583 (make-directory dir t
)
1584 (write-region nil nil
(expand-file-name file dir
) nil
'silent
)))
1585 (when good-signatures
1586 ;; Write out good signatures into archive-contents.signed file.
1587 (write-region (mapconcat #'epg-signature-to-string good-signatures
"\n")
1589 (expand-file-name (concat file
".signed") dir
)
1592 (declare-function epg-check-configuration
"epg-config"
1593 (config &optional minimum-version
))
1594 (declare-function epg-configuration
"epg-config" ())
1595 (declare-function epg-import-keys-from-file
"epg" (context keys
))
1598 (defun package-import-keyring (&optional file
)
1599 "Import keys from FILE."
1600 (interactive "fFile: ")
1601 (setq file
(expand-file-name file
))
1602 (let ((context (epg-make-context 'OpenPGP
))
1603 (homedir (expand-file-name "gnupg" package-user-dir
)))
1604 (with-file-modes 448
1605 (make-directory homedir t
))
1606 (setf (epg-context-home-directory context
) homedir
)
1607 (message "Importing %s..." (file-name-nondirectory file
))
1608 (epg-import-keys-from-file context file
)
1609 (message "Importing %s...done" (file-name-nondirectory file
))))
1612 (defun package-refresh-contents ()
1613 "Download the ELPA archive description if needed.
1614 This informs Emacs about the latest versions of all packages, and
1615 makes them available for download."
1617 ;; FIXME: Do it asynchronously.
1618 (unless (file-exists-p package-user-dir
)
1619 (make-directory package-user-dir t
))
1620 (let ((default-keyring (expand-file-name "package-keyring.gpg"
1622 (when (and package-check-signature
(file-exists-p default-keyring
))
1623 (condition-case-unless-debug error
1625 (epg-check-configuration (epg-configuration))
1626 (package-import-keyring default-keyring
))
1627 (error (message "Cannot import default keyring: %S" (cdr error
))))))
1628 (dolist (archive package-archives
)
1629 (condition-case-unless-debug nil
1630 (package--download-one-archive archive
"archive-contents")
1631 (error (message "Failed to download `%s' archive."
1633 (package-read-all-archive-contents))
1635 (defun package--find-non-dependencies ()
1636 "Return a list of installed packages which are not dependencies.
1637 Finds all packages in `package-alist' which are not dependencies
1638 of any other packages.
1639 Used to populate `package-selected-packages'."
1643 (mapcar (lambda (p) (mapcar #'car
(package-desc-reqs (cadr p
))))
1645 (cl-loop for p in package-alist
1647 unless
(memq name dep-list
)
1651 (defun package-initialize (&optional no-activate
)
1652 "Load Emacs Lisp packages, and activate them.
1653 The variable `package-load-list' controls which packages to load.
1654 If optional arg NO-ACTIVATE is non-nil, don't activate packages."
1656 (setq package-alist nil
)
1657 (package-load-all-descriptors)
1658 (package-read-all-archive-contents)
1660 (dolist (elt package-alist
)
1661 (package-activate (car elt
))))
1662 (when (and package-alist
(not package-selected-packages
))
1663 (customize-save-variable 'package-selected-packages
1664 (package--find-non-dependencies)))
1665 (setq package--initialized t
))
1668 ;;;; Package description buffer.
1671 (defun describe-package (package)
1672 "Display the full documentation of PACKAGE (a symbol)."
1674 (let* ((guess (function-called-at-point)))
1675 (require 'finder-inf nil t
)
1676 ;; Load the package list if necessary (but don't activate them).
1677 (unless package--initialized
1678 (package-initialize t
))
1679 (let ((packages (append (mapcar 'car package-alist
)
1680 (mapcar 'car package-archive-contents
)
1681 (mapcar 'car package--builtins
))))
1682 (unless (memq guess packages
)
1684 (setq packages
(mapcar 'symbol-name packages
))
1686 (completing-read (if guess
1687 (format "Describe package (default %s): "
1689 "Describe package: ")
1690 packages nil t nil nil guess
)))
1691 (list (intern val
))))))
1692 (if (not (or (package-desc-p package
) (and package
(symbolp package
))))
1693 (message "No package specified")
1694 (help-setup-xref (list #'describe-package package
)
1695 (called-interactively-p 'interactive
))
1696 (with-help-window (help-buffer)
1697 (with-current-buffer standard-output
1698 (describe-package-1 package
)))))
1700 (defun describe-package-1 (pkg)
1703 (if (package-desc-p pkg
) pkg
)
1704 (cadr (assq pkg package-alist
))
1705 (let ((built-in (assq pkg package--builtins
)))
1707 (package--from-builtin built-in
)
1708 (cadr (assq pkg package-archive-contents
))))))
1709 (name (if desc
(package-desc-name desc
) pkg
))
1710 (pkg-dir (if desc
(package-desc-dir desc
)))
1711 (reqs (if desc
(package-desc-reqs desc
)))
1712 (version (if desc
(package-desc-version desc
)))
1713 (archive (if desc
(package-desc-archive desc
)))
1714 (extras (and desc
(package-desc-extras desc
)))
1715 (homepage (cdr (assoc :url extras
)))
1716 (keywords (if desc
(package-desc--keywords desc
)))
1717 (built-in (eq pkg-dir
'builtin
))
1718 (installable (and archive
(not built-in
)))
1719 (status (if desc
(package-desc-status desc
) "orphan"))
1720 (signed (if desc
(package-desc-signed desc
))))
1723 (princ (if (memq (aref status
0) '(?a ?e ?i ?o ?u
)) "an " "a "))
1725 (princ " package.\n\n")
1727 (insert " " (propertize "Status" 'font-lock-face
'bold
) ": ")
1729 (insert (propertize (capitalize status
)
1730 'font-lock-face
'font-lock-builtin-face
)
1733 (insert (propertize (if (equal status
"unsigned")
1735 (capitalize status
)) ;FIXME: Why comment-face?
1736 'font-lock-face
'font-lock-comment-face
))
1738 ;; Todo: Add button for uninstalling.
1739 (help-insert-xref-button (abbreviate-file-name
1740 (file-name-as-directory pkg-dir
))
1741 'help-package-def pkg-dir
)
1742 (if (and (package-built-in-p name
)
1743 (not (package-built-in-p name version
)))
1744 (insert "',\n shadowing a "
1745 (propertize "built-in package"
1746 'font-lock-face
'font-lock-builtin-face
))
1750 (insert " (unsigned).")))
1752 (insert (capitalize status
))
1753 (insert " from " (format "%s" archive
))
1755 (package-make-button
1757 'action
'package-install-button-action
1758 'package-desc desc
))
1759 (t (insert (capitalize status
) ".")))
1761 (insert " " (propertize "Archive" 'font-lock-face
'bold
)
1762 ": " (or archive
"n/a") "\n")
1765 (propertize "Version" 'font-lock-face
'bold
) ": "
1766 (package-version-join version
) "\n"))
1768 (setq reqs
(if desc
(package-desc-reqs desc
)))
1770 (insert " " (propertize "Requires" 'font-lock-face
'bold
) ": ")
1774 (setq name
(car req
)
1776 text
(format "%s-%s" (symbol-name name
)
1777 (package-version-join vers
)))
1778 (cond (first (setq first nil
))
1779 ((>= (+ 2 (current-column) (length text
))
1783 (help-insert-xref-button text
'help-package name
))
1785 (insert " " (propertize "Summary" 'font-lock-face
'bold
)
1786 ": " (if desc
(package-desc-summary desc
)) "\n")
1788 (insert " " (propertize "Homepage" 'font-lock-face
'bold
) ": ")
1789 (help-insert-xref-button homepage
'help-url homepage
)
1792 (insert " " (propertize "Keywords" 'font-lock-face
'bold
) ": ")
1793 (dolist (k keywords
)
1794 (package-make-button
1797 'action
'package-keyword-button-action
)
1800 (let* ((all-pkgs (append (cdr (assq name package-alist
))
1801 (cdr (assq name package-archive-contents
))
1802 (let ((bi (assq name package--builtins
)))
1803 (if bi
(list (package--from-builtin bi
))))))
1804 (other-pkgs (delete desc all-pkgs
)))
1806 (insert " " (propertize "Other versions" 'font-lock-face
'bold
) ": "
1809 (let* ((ov (package-desc-version opkg
))
1810 (dir (package-desc-dir opkg
))
1811 (from (or (package-desc-archive opkg
)
1812 (if (stringp dir
) "installed" dir
))))
1813 (if (not ov
) (format "%s" from
)
1815 (make-text-button (package-version-join ov
) nil
1820 (describe-package opkg
)))
1828 ;; For built-in packages, insert the commentary.
1829 (let ((fn (locate-file (format "%s.el" name
) load-path
1830 load-file-rep-suffixes
))
1832 (insert (or (lm-commentary fn
) ""))
1835 (when (re-search-forward "^;;; Commentary:\n" nil t
)
1837 (while (re-search-forward "^\\(;+ ?\\)" nil t
)
1838 (replace-match ""))))
1839 (let ((readme (expand-file-name (format "%s-readme.txt" name
)
1842 ;; For elpa packages, try downloading the commentary. If that
1843 ;; fails, try an existing readme file in `package-user-dir'.
1844 (cond ((condition-case nil
1846 (package--with-work-buffer
1847 (package-archive-base desc
)
1848 (format "%s-readme.txt" name
)
1850 (goto-char (point-max))
1853 (write-region nil nil
1854 (expand-file-name readme package-user-dir
)
1856 (setq readme-string
(buffer-string))
1859 (insert readme-string
))
1860 ((file-readable-p readme
)
1861 (insert-file-contents readme
)
1862 (goto-char (point-max))))))))
1864 (defun package-install-button-action (button)
1865 (let ((pkg-desc (button-get button
'package-desc
)))
1866 (when (y-or-n-p (format "Install package `%s'? "
1867 (package-desc-full-name pkg-desc
)))
1868 (package-install pkg-desc
1)
1869 (revert-buffer nil t
)
1870 (goto-char (point-min)))))
1872 (defun package-keyword-button-action (button)
1873 (let ((pkg-keyword (button-get button
'package-keyword
)))
1874 (package-show-package-list t
(list pkg-keyword
))))
1876 (defun package-make-button (text &rest props
)
1877 (let ((button-text (if (display-graphic-p) text
(concat "[" text
"]")))
1878 (button-face (if (display-graphic-p)
1879 '(:box
(:line-width
2 :color
"dark grey")
1880 :background
"light grey"
1881 :foreground
"black")
1883 (apply 'insert-text-button button-text
'face button-face
'follow-link t
1887 ;;;; Package menu mode.
1889 (defvar package-menu-mode-map
1890 (let ((map (make-sparse-keymap))
1891 (menu-map (make-sparse-keymap "Package")))
1892 (set-keymap-parent map tabulated-list-mode-map
)
1893 (define-key map
"\C-m" 'package-menu-describe-package
)
1894 (define-key map
"u" 'package-menu-mark-unmark
)
1895 (define-key map
"\177" 'package-menu-backup-unmark
)
1896 (define-key map
"d" 'package-menu-mark-delete
)
1897 (define-key map
"i" 'package-menu-mark-install
)
1898 (define-key map
"U" 'package-menu-mark-upgrades
)
1899 (define-key map
"r" 'package-menu-refresh
)
1900 (define-key map
"f" 'package-menu-filter
)
1901 (define-key map
"~" 'package-menu-mark-obsolete-for-deletion
)
1902 (define-key map
"x" 'package-menu-execute
)
1903 (define-key map
"h" 'package-menu-quick-help
)
1904 (define-key map
"?" 'package-menu-describe-package
)
1905 (define-key map
[menu-bar package-menu
] (cons "Package" menu-map
))
1906 (define-key menu-map
[mq]
1907 '(menu-item "Quit" quit-window
1908 :help "Quit package selection"))
1909 (define-key menu-map [s1] '("--"))
1910 (define-key menu-map [mn]
1911 '(menu-item "Next" next-line
1913 (define-key menu-map [mp]
1914 '(menu-item "Previous" previous-line
1915 :help "Previous Line"))
1916 (define-key menu-map [s2] '("--"))
1917 (define-key menu-map [mu]
1918 '(menu-item "Unmark" package-menu-mark-unmark
1919 :help "Clear any marks on a package and move to the next line"))
1920 (define-key menu-map [munm]
1921 '(menu-item "Unmark Backwards" package-menu-backup-unmark
1922 :help "Back up one line and clear any marks on that package"))
1923 (define-key menu-map [md]
1924 '(menu-item "Mark for Deletion" package-menu-mark-delete
1925 :help "Mark a package for deletion and move to the next line"))
1926 (define-key menu-map [mi]
1927 '(menu-item "Mark for Install" package-menu-mark-install
1928 :help "Mark a package for installation and move to the next line"))
1929 (define-key menu-map [mupgrades]
1930 '(menu-item "Mark Upgradable Packages" package-menu-mark-upgrades
1931 :help "Mark packages that have a newer version for upgrading"))
1932 (define-key menu-map [s3] '("--"))
1933 (define-key menu-map [mf]
1934 '(menu-item "Filter Package List..." package-menu-filter
1935 :help "Filter package selection (q to go back)"))
1936 (define-key menu-map [mg]
1937 '(menu-item "Update Package List" revert-buffer
1938 :help "Update the list of packages"))
1939 (define-key menu-map [mr]
1940 '(menu-item "Refresh Package List" package-menu-refresh
1941 :help "Download the ELPA archive"))
1942 (define-key menu-map [s4] '("--"))
1943 (define-key menu-map [mt]
1944 '(menu-item "Mark Obsolete Packages" package-menu-mark-obsolete-for-deletion
1945 :help "Mark all obsolete packages for deletion"))
1946 (define-key menu-map [mx]
1947 '(menu-item "Execute Actions" package-menu-execute
1948 :help "Perform all the marked actions"))
1949 (define-key menu-map [s5] '("--"))
1950 (define-key menu-map [mh]
1951 '(menu-item "Help" package-menu-quick-help
1952 :help "Show short key binding help for package-menu-mode"))
1953 (define-key menu-map [mc]
1954 '(menu-item "Describe Package" package-menu-describe-package
1955 :help "Display information about this package"))
1957 "Local keymap for `package-menu-mode' buffers.")
1959 (defvar package-menu--new-package-list nil
1960 "List of newly-available packages since `list-packages' was last called.")
1962 (define-derived-mode package-menu-mode tabulated-list-mode "Package Menu"
1963 "Major mode for browsing a list of packages.
1964 Letters do not insert themselves; instead, they are commands.
1965 \\<package-menu-mode-map>
1966 \\{package-menu-mode-map}"
1967 (setq tabulated-list-format
1968 `[("Package" 18 package-menu--name-predicate)
1970 ("Status" 10 package-menu--status-predicate)
1971 ,@(if (cdr package-archives)
1972 '(("Archive" 10 package-menu--archive-predicate)))
1973 ("Description" 0 nil)])
1974 (setq tabulated-list-padding 2)
1975 (setq tabulated-list-sort-key (cons "Status" nil))
1976 (add-hook 'tabulated-list-revert-hook 'package-menu--refresh nil t)
1977 (tabulated-list-init-header))
1979 (defmacro package--push (pkg-desc status listname)
1980 "Convenience macro for `package-menu--generate'.
1981 If the alist stored in the symbol LISTNAME lacks an entry for a
1982 package PKG-DESC, add one. The alist is keyed with PKG-DESC."
1983 `(unless (assoc ,pkg-desc ,listname)
1984 ;; FIXME: Should we move status into pkg-desc?
1985 (push (cons ,pkg-desc ,status) ,listname)))
1987 (defvar package-list-unversioned nil
1988 "If non-nil include packages that don't have a version in `list-package'.")
1990 (defvar package-list-unsigned nil
1991 "If non-nil, mention in the list which packages were installed w/o signature.")
1993 (defun package-desc-status (pkg-desc)
1994 (let* ((name (package-desc-name pkg-desc))
1995 (dir (package-desc-dir pkg-desc))
1996 (lle (assq name package-load-list))
1998 (version (package-desc-version pkg-desc))
1999 (signed (package-desc-signed pkg-desc)))
2001 ((eq dir 'builtin) "built-in")
2002 ((and lle (null held)) "disabled")
2004 (let ((hv (if (stringp held) (version-to-list held))))
2006 ((version-list-= version hv) "held")
2007 ((version-list-< version hv) "obsolete")
2009 ((package-built-in-p name version) "obsolete")
2010 (dir ;One of the installed packages.
2012 ((not (file-exists-p (package-desc-dir pkg-desc))) "deleted")
2013 ((eq pkg-desc (cadr (assq name package-alist)))
2014 (if (or (not package-list-unsigned) signed) "installed" "unsigned"))
2017 (let* ((ins (cadr (assq name package-alist)))
2018 (ins-v (if ins (package-desc-version ins))))
2020 ((or (null ins) (version-list-< ins-v version))
2021 (if (memq name package-menu--new-package-list)
2023 ((version-list-< version ins-v) "obsolete")
2024 ((version-list-= version ins-v)
2025 (if (or (not package-list-unsigned) signed)
2026 "installed" "unsigned"))))))))
2028 (defun package-menu--refresh (&optional packages keywords)
2029 "Re-populate the `tabulated-list-entries'.
2030 PACKAGES should be nil or t, which means to display all known packages.
2031 KEYWORDS should be nil or a list of keywords."
2032 ;; Construct list of (PKG-DESC . STATUS).
2033 (unless packages (setq packages t))
2034 (let (info-list name)
2035 ;; Installed packages:
2036 (dolist (elt package-alist)
2037 (setq name (car elt))
2038 (when (or (eq packages t) (memq name packages))
2039 (dolist (pkg (cdr elt))
2040 (when (package--has-keyword-p pkg keywords)
2041 (package--push pkg (package-desc-status pkg) info-list)))))
2043 ;; Built-in packages:
2044 (dolist (elt package--builtins)
2045 (setq name (car elt))
2046 (when (and (not (eq name 'emacs)) ; Hide the `emacs' package.
2047 (package--has-keyword-p (package--from-builtin elt) keywords)
2048 (or package-list-unversioned
2049 (package--bi-desc-version (cdr elt)))
2050 (or (eq packages t) (memq name packages)))
2051 (package--push (package--from-builtin elt) "built-in" info-list)))
2053 ;; Available and disabled packages:
2054 (dolist (elt package-archive-contents)
2055 (setq name (car elt))
2056 (when (or (eq packages t) (memq name packages))
2057 (dolist (pkg (cdr elt))
2058 ;; Hide obsolete packages.
2059 (when (and (not (package-installed-p (package-desc-name pkg)
2060 (package-desc-version pkg)))
2061 (package--has-keyword-p pkg keywords))
2062 (package--push pkg (package-desc-status pkg) info-list)))))
2064 ;; Print the result.
2065 (setq tabulated-list-entries
2066 (mapcar #'package-menu--print-info info-list))))
2068 (defun package-all-keywords ()
2069 "Collect all package keywords"
2071 (package--mapc (lambda (desc)
2072 (let* ((desc-keywords (and desc (package-desc--keywords desc))))
2073 (setq keywords (append keywords desc-keywords)))))
2076 (defun package--mapc (function &optional packages)
2077 "Call FUNCTION for all known PACKAGES.
2078 PACKAGES can be nil or t, which means to display all known
2079 packages, or a list of packages.
2081 Built-in packages are converted with `package--from-builtin'."
2082 (unless packages (setq packages t))
2084 ;; Installed packages:
2085 (dolist (elt package-alist)
2086 (setq name (car elt))
2087 (when (or (eq packages t) (memq name packages))
2088 (mapc function (cdr elt))))
2090 ;; Built-in packages:
2091 (dolist (elt package--builtins)
2092 (setq name (car elt))
2093 (when (and (not (eq name 'emacs)) ; Hide the `emacs' package.
2094 (or package-list-unversioned
2095 (package--bi-desc-version (cdr elt)))
2096 (or (eq packages t) (memq name packages)))
2097 (funcall function (package--from-builtin elt))))
2099 ;; Available and disabled packages:
2100 (dolist (elt package-archive-contents)
2101 (setq name (car elt))
2102 (when (or (eq packages t) (memq name packages))
2103 (dolist (pkg (cdr elt))
2104 ;; Hide obsolete packages.
2105 (unless (package-installed-p (package-desc-name pkg)
2106 (package-desc-version pkg))
2107 (funcall function pkg)))))))
2109 (defun package--has-keyword-p (desc &optional keywords)
2110 "Test if package DESC has any of the given KEYWORDS.
2111 When none are given, the package matches."
2113 (let* ((desc-keywords (and desc (package-desc--keywords desc)))
2115 (dolist (k keywords)
2116 (when (and (not found)
2117 (member k desc-keywords))
2122 (defun package-menu--generate (remember-pos packages &optional keywords)
2123 "Populate the Package Menu.
2124 If REMEMBER-POS is non-nil, keep point on the same entry.
2125 PACKAGES should be t, which means to display all known packages,
2126 or a list of package names (symbols) to display.
2128 With KEYWORDS given, only packages with those keywords are
2130 (package-menu--refresh packages keywords)
2131 (setf (car (aref tabulated-list-format 0))
2133 (let ((filters (mapconcat 'identity keywords ",")))
2134 (concat "Package[" filters "]"))
2137 (define-key package-menu-mode-map "q" 'package-show-package-list)
2138 (define-key package-menu-mode-map "q" 'quit-window))
2139 (tabulated-list-init-header)
2140 (tabulated-list-print remember-pos))
2142 (defun package-menu--print-info (pkg)
2143 "Return a package entry suitable for `tabulated-list-entries'.
2144 PKG has the form (PKG-DESC . STATUS).
2145 Return (PKG-DESC [NAME VERSION STATUS DOC])."
2146 (let* ((pkg-desc (car pkg))
2149 (`"built-in" 'font-lock-builtin-face)
2150 (`"available" 'default)
2152 (`"held" 'font-lock-constant-face)
2153 (`"disabled" 'font-lock-warning-face)
2154 (`"installed" 'font-lock-comment-face)
2155 (`"unsigned" 'font-lock-warning-face)
2156 (_ 'font-lock-warning-face)))) ; obsolete.
2158 `[,(list (symbol-name (package-desc-name pkg-desc))
2161 'package-desc pkg-desc
2162 'action 'package-menu-describe-package)
2163 ,(propertize (package-version-join
2164 (package-desc-version pkg-desc))
2165 'font-lock-face face)
2166 ,(propertize status 'font-lock-face face)
2167 ,@(if (cdr package-archives)
2168 (list (propertize (or (package-desc-archive pkg-desc) "")
2169 'font-lock-face face)))
2170 ,(propertize (package-desc-summary pkg-desc)
2171 'font-lock-face face)])))
2173 (defun package-menu-refresh ()
2174 "Download the Emacs Lisp package archive.
2175 This fetches the contents of each archive specified in
2176 `package-archives', and then refreshes the package menu."
2178 (unless (derived-mode-p 'package-menu-mode)
2179 (user-error "The current buffer is not a Package Menu"))
2180 (package-refresh-contents)
2181 (package-menu--generate t t))
2183 (defun package-menu-describe-package (&optional button)
2184 "Describe the current package.
2185 If optional arg BUTTON is non-nil, describe its associated package."
2187 (let ((pkg-desc (if button (button-get button 'package-desc)
2188 (tabulated-list-get-id))))
2190 (describe-package pkg-desc)
2191 (user-error "No package here"))))
2193 ;; fixme numeric argument
2194 (defun package-menu-mark-delete (&optional _num)
2195 "Mark a package for deletion and move to the next line."
2197 (if (member (package-menu-get-status) '("installed" "obsolete" "unsigned"))
2198 (tabulated-list-put-tag "D" t)
2201 (defun package-menu-mark-install (&optional _num)
2202 "Mark a package for installation and move to the next line."
2204 (if (member (package-menu-get-status) '("available" "new"))
2205 (tabulated-list-put-tag "I" t)
2208 (defun package-menu-mark-unmark (&optional _num)
2209 "Clear any marks on a package and move to the next line."
2211 (tabulated-list-put-tag " " t))
2213 (defun package-menu-backup-unmark ()
2214 "Back up one line and clear any marks on that package."
2217 (tabulated-list-put-tag " "))
2219 (defun package-menu-mark-obsolete-for-deletion ()
2220 "Mark all obsolete packages for deletion."
2223 (goto-char (point-min))
2225 (if (equal (package-menu-get-status) "obsolete")
2226 (tabulated-list-put-tag "D" t)
2227 (forward-line 1)))))
2229 (defun package-menu-quick-help ()
2230 "Show short key binding help for package-menu-mode."
2232 (message "n-ext, i-nstall, d-elete, u-nmark, x-ecute, r-efresh, h-elp"))
2234 (define-obsolete-function-alias
2235 'package-menu-view-commentary 'package-menu-describe-package "24.1")
2237 (defun package-menu-get-status ()
2238 (let* ((id (tabulated-list-get-id))
2239 (entry (and id (assq id tabulated-list-entries))))
2241 (aref (cadr entry) 2)
2244 (defun package-menu--find-upgrades ()
2245 (let (installed available upgrades)
2246 ;; Build list of installed/available packages in this buffer.
2247 (dolist (entry tabulated-list-entries)
2248 ;; ENTRY is (PKG-DESC [NAME VERSION STATUS DOC])
2249 (let ((pkg-desc (car entry))
2250 (status (aref (cadr entry) 2)))
2251 (cond ((member status '("installed" "unsigned"))
2252 (push pkg-desc installed))
2253 ((member status '("available" "new"))
2254 (setq available (package--append-to-alist pkg-desc available))))))
2255 ;; Loop through list of installed packages, finding upgrades.
2256 (dolist (pkg-desc installed)
2257 (let* ((name (package-desc-name pkg-desc))
2258 (avail-pkg (cadr (assq name available))))
2260 (version-list-< (package-desc-priority-version pkg-desc)
2261 (package-desc-priority-version avail-pkg))
2262 (push (cons name avail-pkg) upgrades))))
2265 (defun package-menu-mark-upgrades ()
2266 "Mark all upgradable packages in the Package Menu.
2267 For each installed package with a newer version available, place
2268 an (I)nstall flag on the available version and a (D)elete flag on
2269 the installed version. A subsequent \\[package-menu-execute]
2270 call will upgrade the package."
2272 (unless (derived-mode-p 'package-menu-mode)
2273 (error "The current buffer is not a Package Menu"))
2274 (let ((upgrades (package-menu--find-upgrades)))
2276 (message "No packages to upgrade.")
2279 (goto-char (point-min))
2281 (let* ((pkg-desc (tabulated-list-get-id))
2282 (upgrade (cdr (assq (package-desc-name pkg-desc) upgrades))))
2283 (cond ((null upgrade)
2285 ((equal pkg-desc upgrade)
2286 (package-menu-mark-install))
2288 (package-menu-mark-delete))))))
2289 (message "%d package%s marked for upgrading."
2291 (if (= (length upgrades) 1) "" "s")))))
2293 (defun package-menu-execute (&optional noquery)
2294 "Perform marked Package Menu actions.
2295 Packages marked for installation are downloaded and installed;
2296 packages marked for deletion are removed.
2297 Optional argument NOQUERY non-nil means do not ask the user to confirm."
2299 (unless (derived-mode-p 'package-menu-mode)
2300 (error "The current buffer is not in Package Menu mode"))
2301 (let (install-list delete-list cmd pkg-desc)
2303 (goto-char (point-min))
2305 (setq cmd (char-after))
2306 (unless (eq cmd ?\s)
2307 ;; This is the key PKG-DESC.
2308 (setq pkg-desc (tabulated-list-get-id))
2310 (push pkg-desc delete-list))
2312 (push pkg-desc install-list))))
2318 (if (= (length install-list) 1)
2319 (format "Install package `%s'? "
2320 (package-desc-full-name (car install-list)))
2321 (format "Install these %d packages (%s)? "
2322 (length install-list)
2323 (mapconcat #'package-desc-full-name
2324 install-list ", ")))))
2326 (package-install p (and (null (package-installed-p p)) 1)))
2328 ;; Delete packages, prompting if necessary.
2333 (if (= (length delete-list) 1)
2334 (format "Delete package `%s'? "
2335 (package-desc-full-name (car delete-list)))
2336 (format "Delete these %d packages (%s)? "
2337 (length delete-list)
2338 (mapconcat #'package-desc-full-name
2339 delete-list ", ")))))
2340 (dolist (elt delete-list)
2341 (condition-case-unless-debug err
2342 (package-delete elt)
2343 (error (message (cadr err)))))
2345 (if (or delete-list install-list)
2346 (package-menu--generate t t)
2347 (message "No operations specified."))))
2349 (defun package-menu--version-predicate (A B)
2350 (let ((vA (or (aref (cadr A) 1) '(0)))
2351 (vB (or (aref (cadr B) 1) '(0))))
2352 (if (version-list-= vA vB)
2353 (package-menu--name-predicate A B)
2354 (version-list-< vA vB))))
2356 (defun package-menu--status-predicate (A B)
2357 (let ((sA (aref (cadr A) 2))
2358 (sB (aref (cadr B) 2)))
2359 (cond ((string= sA sB)
2360 (package-menu--name-predicate A B))
2361 ((string= sA "new") t)
2362 ((string= sB "new") nil)
2363 ((string= sA "available") t)
2364 ((string= sB "available") nil)
2365 ((string= sA "installed") t)
2366 ((string= sB "installed") nil)
2367 ((string= sA "unsigned") t)
2368 ((string= sB "unsigned") nil)
2369 ((string= sA "held") t)
2370 ((string= sB "held") nil)
2371 ((string= sA "built-in") t)
2372 ((string= sB "built-in") nil)
2373 ((string= sA "obsolete") t)
2374 ((string= sB "obsolete") nil)
2375 (t (string< sA sB)))))
2377 (defun package-menu--description-predicate (A B)
2378 (let ((dA (aref (cadr A) 3))
2379 (dB (aref (cadr B) 3)))
2381 (package-menu--name-predicate A B)
2384 (defun package-menu--name-predicate (A B)
2385 (string< (symbol-name (package-desc-name (car A)))
2386 (symbol-name (package-desc-name (car B)))))
2388 (defun package-menu--archive-predicate (A B)
2389 (string< (or (package-desc-archive (car A)) "")
2390 (or (package-desc-archive (car B)) "")))
2393 (defun list-packages (&optional no-fetch)
2394 "Display a list of packages.
2395 This first fetches the updated list of packages before
2396 displaying, unless a prefix argument NO-FETCH is specified.
2397 The list is displayed in a buffer named `*Packages*'."
2399 (require 'finder-inf nil t)
2400 ;; Initialize the package system if necessary.
2401 (unless package--initialized
2402 (package-initialize t))
2403 (let (old-archives new-packages)
2405 ;; Read the locally-cached archive-contents.
2406 (package-read-all-archive-contents)
2407 (setq old-archives package-archive-contents)
2408 ;; Fetch the remote list of packages.
2409 (package-refresh-contents)
2410 ;; Find which packages are new.
2411 (dolist (elt package-archive-contents)
2412 (unless (assq (car elt) old-archives)
2413 (push (car elt) new-packages))))
2415 ;; Generate the Package Menu.
2416 (let ((buf (get-buffer-create "*Packages*")))
2417 (with-current-buffer buf
2419 (set (make-local-variable 'package-menu--new-package-list)
2421 (package-menu--generate nil t))
2422 ;; The package menu buffer has keybindings. If the user types
2423 ;; `M-x list-packages', that suggests it should become current.
2424 (switch-to-buffer buf))
2426 (let ((upgrades (package-menu--find-upgrades)))
2428 (message "%d package%s can be upgraded; type `%s' to mark %s for upgrading."
2430 (if (= (length upgrades) 1) "" "s")
2431 (substitute-command-keys "\\[package-menu-mark-upgrades]")
2432 (if (= (length upgrades) 1) "it" "them"))))))
2435 (defalias 'package-list-packages 'list-packages)
2437 ;; Used in finder.el
2438 (defun package-show-package-list (&optional packages keywords)
2439 "Display PACKAGES in a *Packages* buffer.
2440 This is similar to `list-packages', but it does not fetch the
2441 updated list of packages, and it only displays packages with
2442 names in PACKAGES (which should be a list of symbols).
2444 When KEYWORDS are given, only packages with those KEYWORDS are
2447 (require 'finder-inf nil t)
2448 (let* ((buf (get-buffer-create "*Packages*"))
2449 (win (get-buffer-window buf)))
2450 (with-current-buffer buf
2452 (package-menu--generate nil packages keywords))
2455 (switch-to-buffer buf))))
2457 ;; package-menu--generate rebinds "q" on the fly, so we have to
2458 ;; hard-code the binding in the doc-string here.
2459 (defun package-menu-filter (keyword)
2460 "Filter the *Packages* buffer.
2461 Show only those items that relate to the specified KEYWORD.
2462 To restore the full package list, type `q'."
2463 (interactive (list (completing-read "Keyword: " (package-all-keywords))))
2464 (package-show-package-list t (list keyword)))
2466 (defun package-list-packages-no-fetch ()
2467 "Display a list of packages.
2468 Does not fetch the updated list of packages before displaying.
2469 The list is displayed in a buffer named `*Packages*'."
2475 ;;; package.el ends here