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