emacs-lisp/package.el (package-activate-1): Reload package after upgrade
[emacs.git] / lisp / emacs-lisp / package.el
blobe424e30de1f30affc96f83f1729a7354cfc06503
1 ;;; package.el --- Simple package system for Emacs -*- lexical-binding:t -*-
3 ;; Copyright (C) 2007-2014 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-pinned-packages nil
232 "An alist of packages that are pinned to specific archives.
233 This can be useful if you have multiple package archives enabled,
234 and want to control which archive a given package gets installed from.
236 Each element of the alist has the form (PACKAGE . ARCHIVE), where:
237 PACKAGE is a symbol representing a package
238 ARCHIVE is a string representing an archive (it should be the car of
239 an element in `package-archives', e.g. \"gnu\").
241 Adding an entry to this variable means that only ARCHIVE will be
242 considered as a source for PACKAGE. If other archives provide PACKAGE,
243 they are ignored (for this package). If ARCHIVE does not contain PACKAGE,
244 the package will be unavailable."
245 :type '(alist :key-type (symbol :tag "Package")
246 :value-type (string :tag "Archive name"))
247 ;; I don't really see why this is risky...
248 ;; I suppose it could prevent you receiving updates for a package,
249 ;; via an entry (PACKAGE . NON-EXISTING). Which could be an issue
250 ;; if PACKAGE has a known vulnerability that is fixed in newer versions.
251 :risky t
252 :group 'package
253 :version "24.4")
255 (defconst package-archive-version 1
256 "Version number of the package archive understood by this file.
257 Lower version numbers than this will probably be understood as well.")
259 ;; We don't prime the cache since it tends to get out of date.
260 (defvar package-archive-contents nil
261 "Cache of the contents of the Emacs Lisp Package Archive.
262 This is an alist mapping package names (symbols) to
263 non-empty lists of `package-desc' structures.")
264 (put 'package-archive-contents 'risky-local-variable t)
266 (defcustom package-user-dir (locate-user-emacs-file "elpa")
267 "Directory containing the user's Emacs Lisp packages.
268 The directory name should be absolute.
269 Apart from this directory, Emacs also looks for system-wide
270 packages in `package-directory-list'."
271 :type 'directory
272 :risky t
273 :group 'package
274 :version "24.1")
276 (defcustom package-directory-list
277 ;; Defaults are subdirs named "elpa" in the site-lisp dirs.
278 (let (result)
279 (dolist (f load-path)
280 (and (stringp f)
281 (equal (file-name-nondirectory f) "site-lisp")
282 (push (expand-file-name "elpa" f) result)))
283 (nreverse result))
284 "List of additional directories containing Emacs Lisp packages.
285 Each directory name should be absolute.
287 These directories contain packages intended for system-wide; in
288 contrast, `package-user-dir' contains packages for personal use."
289 :type '(repeat directory)
290 :risky t
291 :group 'package
292 :version "24.1")
294 (defvar epg-gpg-program)
296 (defcustom package-check-signature
297 (if (progn (require 'epg-config) (executable-find epg-gpg-program))
298 'allow-unsigned)
299 "Non-nil means to check package signatures when installing.
300 The value `allow-unsigned' means to still install a package even if
301 it is unsigned.
303 This also applies to the \"archive-contents\" file that lists the
304 contents of the archive."
305 :type '(choice (const nil :tag "Never")
306 (const allow-unsigned :tag "Allow unsigned")
307 (const t :tag "Check always"))
308 :risky t
309 :group 'package
310 :version "24.4")
312 (defcustom package-unsigned-archives nil
313 "List of archives where we do not check for package signatures."
314 :type '(repeat (string :tag "Archive name"))
315 :risky t
316 :group 'package
317 :version "24.4")
319 (defvar package--default-summary "No description available.")
321 (cl-defstruct (package-desc
322 ;; Rename the default constructor from `make-package-desc'.
323 (:constructor package-desc-create)
324 ;; Has the same interface as the old `define-package',
325 ;; which is still used in the "foo-pkg.el" files. Extra
326 ;; options can be supported by adding additional keys.
327 (:constructor
328 package-desc-from-define
329 (name-string version-string &optional summary requirements
330 &rest rest-plist
331 &aux
332 (name (intern name-string))
333 (version (version-to-list version-string))
334 (reqs (mapcar #'(lambda (elt)
335 (list (car elt)
336 (version-to-list (cadr elt))))
337 (if (eq 'quote (car requirements))
338 (nth 1 requirements)
339 requirements)))
340 (kind (plist-get rest-plist :kind))
341 (archive (plist-get rest-plist :archive))
342 (extras (let (alist)
343 (while rest-plist
344 (unless (memq (car rest-plist) '(:kind :archive))
345 (let ((value (cadr rest-plist)))
346 (when value
347 (push (cons (car rest-plist)
348 (if (eq (car-safe value) 'quote)
349 (cadr value)
350 value))
351 alist))))
352 (setq rest-plist (cddr rest-plist)))
353 alist)))))
354 "Structure containing information about an individual package.
355 Slots:
357 `name' Name of the package, as a symbol.
359 `version' Version of the package, as a version list.
361 `summary' Short description of the package, typically taken from
362 the first line of the file.
364 `reqs' Requirements of the package. A list of (PACKAGE
365 VERSION-LIST) naming the dependent package and the minimum
366 required version.
368 `kind' The distribution format of the package. Currently, it is
369 either `single' or `tar'.
371 `archive' The name of the archive (as a string) whence this
372 package came.
374 `dir' The directory where the package is installed (if installed),
375 `builtin' if it is built-in, or nil otherwise.
377 `extras' Optional alist of additional keyword-value pairs.
379 `signed' Flag to indicate that the package is signed by provider."
380 name
381 version
382 (summary package--default-summary)
383 reqs
384 kind
385 archive
387 extras
388 signed)
390 ;; Pseudo fields.
391 (defun package-desc-full-name (pkg-desc)
392 (format "%s-%s"
393 (package-desc-name pkg-desc)
394 (package-version-join (package-desc-version pkg-desc))))
396 (defun package-desc-suffix (pkg-desc)
397 (pcase (package-desc-kind pkg-desc)
398 (`single ".el")
399 (`tar ".tar")
400 (kind (error "Unknown package kind: %s" kind))))
402 (defun package-desc--keywords (pkg-desc)
403 (let ((keywords (cdr (assoc :keywords (package-desc-extras pkg-desc)))))
404 (if (eq (car-safe keywords) 'quote)
405 (nth 1 keywords)
406 keywords)))
408 ;; Package descriptor format used in finder-inf.el and package--builtins.
409 (cl-defstruct (package--bi-desc
410 (:constructor package-make-builtin (version summary))
411 (:type vector))
412 version
413 reqs
414 summary)
416 (defvar package--builtins nil
417 "Alist of built-in packages.
418 The actual value is initialized by loading the library
419 `finder-inf'; this is not done until it is needed, e.g. by the
420 function `package-built-in-p'.
422 Each element has the form (PKG . PACKAGE-BI-DESC), where PKG is a package
423 name (a symbol) and DESC is a `package--bi-desc' structure.")
424 (put 'package--builtins 'risky-local-variable t)
426 (defvar package-alist nil
427 "Alist of all packages available for activation.
428 Each element has the form (PKG . DESCS), where PKG is a package
429 name (a symbol) and DESCS is a non-empty list of `package-desc' structure,
430 sorted by decreasing versions.
432 This variable is set automatically by `package-load-descriptor',
433 called via `package-initialize'. To change which packages are
434 loaded and/or activated, customize `package-load-list'.")
435 (put 'package-alist 'risky-local-variable t)
437 (defvar package-activated-list nil
438 ;; FIXME: This should implicitly include all builtin packages.
439 "List of the names of currently activated packages.")
440 (put 'package-activated-list 'risky-local-variable t)
442 (defun package-version-join (vlist)
443 "Return the version string corresponding to the list VLIST.
444 This is, approximately, the inverse of `version-to-list'.
445 \(Actually, it returns only one of the possible inverses, since
446 `version-to-list' is a many-to-one operation.)"
447 (if (null vlist)
449 (let ((str-list (list "." (int-to-string (car vlist)))))
450 (dolist (num (cdr vlist))
451 (cond
452 ((>= num 0)
453 (push (int-to-string num) str-list)
454 (push "." str-list))
455 ((< num -4)
456 (error "Invalid version list `%s'" vlist))
458 ;; pre, or beta, or alpha
459 (cond ((equal "." (car str-list))
460 (pop str-list))
461 ((not (string-match "[0-9]+" (car str-list)))
462 (error "Invalid version list `%s'" vlist)))
463 (push (cond ((= num -1) "pre")
464 ((= num -2) "beta")
465 ((= num -3) "alpha")
466 ((= num -4) "snapshot"))
467 str-list))))
468 (if (equal "." (car str-list))
469 (pop str-list))
470 (apply 'concat (nreverse str-list)))))
472 (defun package-load-descriptor (pkg-dir)
473 "Load the description file in directory PKG-DIR."
474 (let ((pkg-file (expand-file-name (package--description-file pkg-dir)
475 pkg-dir))
476 (signed-file (concat pkg-dir ".signed")))
477 (when (file-exists-p pkg-file)
478 (with-temp-buffer
479 (insert-file-contents pkg-file)
480 (goto-char (point-min))
481 (let ((pkg-desc (package-process-define-package
482 (read (current-buffer)) pkg-file)))
483 (setf (package-desc-dir pkg-desc) pkg-dir)
484 (if (file-exists-p signed-file)
485 (setf (package-desc-signed pkg-desc) t))
486 pkg-desc)))))
488 (defun package-load-all-descriptors ()
489 "Load descriptors for installed Emacs Lisp packages.
490 This looks for package subdirectories in `package-user-dir' and
491 `package-directory-list'. The variable `package-load-list'
492 controls which package subdirectories may be loaded.
494 In each valid package subdirectory, this function loads the
495 description file containing a call to `define-package', which
496 updates `package-alist'."
497 (dolist (dir (cons package-user-dir package-directory-list))
498 (when (file-directory-p dir)
499 (dolist (subdir (directory-files dir))
500 (let ((pkg-dir (expand-file-name subdir dir)))
501 (when (file-directory-p pkg-dir)
502 (package-load-descriptor pkg-dir)))))))
504 (defun package-disabled-p (pkg-name version)
505 "Return whether PKG-NAME at VERSION can be activated.
506 The decision is made according to `package-load-list'.
507 Return nil if the package can be activated.
508 Return t if the package is completely disabled.
509 Return the max version (as a string) if the package is held at a lower version."
510 (let ((force (assq pkg-name package-load-list)))
511 (cond ((null force) (not (memq 'all package-load-list)))
512 ((null (setq force (cadr force))) t) ; disabled
513 ((eq force t) nil)
514 ((stringp force) ; held
515 (unless (version-list-= version (version-to-list force))
516 force))
517 (t (error "Invalid element in `package-load-list'")))))
519 (defun package-activate-1 (pkg-desc)
520 (let* ((name (package-desc-name pkg-desc))
521 (pkg-dir (package-desc-dir pkg-desc))
522 (pkg-dir-dir (file-name-as-directory pkg-dir)))
523 (unless pkg-dir
524 (error "Internal error: unable to find directory for `%s'"
525 (package-desc-full-name pkg-desc)))
526 ;; Add to load path, add autoloads, and activate the package.
527 (let* ((old-lp load-path)
528 (autoloads-file (expand-file-name
529 (format "%s-autoloads" name) pkg-dir))
530 (loaded-files-list (package--list-loaded-files pkg-dir)))
531 (with-demoted-errors (format "Error loading %s: %%s" name)
532 (load autoloads-file nil t))
533 (when (and (eq old-lp load-path)
534 (not (or (member pkg-dir load-path)
535 (member pkg-dir-dir load-path))))
536 ;; Old packages don't add themselves to the `load-path', so we have to
537 ;; do it ourselves.
538 (push pkg-dir load-path))
539 ;; Call `load' on all files in `pkg-dir' already present in
540 ;; `load-history'. This is done so that macros in these files are updated
541 ;; to their new definitions. If another package is being installed which
542 ;; depends on this new definition, not doing this update would cause
543 ;; compilation errors and break the installation.
544 (with-demoted-errors (format "Error loading %s: %%s" name)
545 (mapc (lambda (feature) (load feature nil t))
546 ;; Skip autoloads file since we already evaluated it above.
547 (remove (file-truename autoloads-file) loaded-files-list))))
548 ;; Add info node.
549 (when (file-exists-p (expand-file-name "dir" pkg-dir))
550 ;; FIXME: not the friendliest, but simple.
551 (require 'info)
552 (info-initialize)
553 (push pkg-dir Info-directory-list))
554 (push name package-activated-list)
555 ;; Don't return nil.
558 (defun package--list-loaded-files (dir)
559 "Recursively list all files in DIR which correspond to loaded features.
560 Returns the `file-name-sans-extension' of each file, relative to
561 DIR, sorted by most recently loaded last."
562 (let* ((history (mapcar (lambda (x) (file-name-sans-extension
563 (file-truename (car x))))
564 load-history))
565 (dir (file-truename dir))
566 ;; List all files that have already been loaded.
567 (list-of-conflicts
568 (remove
570 (mapcar
571 (lambda (x) (let* ((file (file-relative-name x dir))
572 ;; Previously loaded file, if any.
573 (previous
574 (ignore-errors
575 (file-name-sans-extension
576 (file-truename (find-library-name file)))))
577 (pos (when previous (member previous history))))
578 ;; Return (RELATIVE-FILENAME . HISTORY-POSITION)
579 (when pos
580 (cons (file-name-sans-extension file) (length pos)))))
581 (directory-files-recursively dir "\\`[^\\.].*\\.el\\'")))))
582 ;; Turn the list of (FILENAME . POS) back into a list of features. Files in
583 ;; subdirectories are returned relative to DIR (so not actually features).
584 (let ((default-directory (file-name-as-directory dir)))
585 (mapcar (lambda (x) (file-truename (car x)))
586 (sort list-of-conflicts
587 ;; Sort the files by ascending HISTORY-POSITION.
588 (lambda (x y) (< (cdr x) (cdr y))))))))
590 (defun package-built-in-p (package &optional min-version)
591 "Return true if PACKAGE is built-in to Emacs.
592 Optional arg MIN-VERSION, if non-nil, should be a version list
593 specifying the minimum acceptable version."
594 (if (package-desc-p package) ;; was built-in and then was converted
595 (eq 'builtin (package-desc-dir package))
596 (let ((bi (assq package package--builtin-versions)))
597 (cond
598 (bi (version-list-<= min-version (cdr bi)))
599 ((remove 0 min-version) nil)
601 (require 'finder-inf nil t) ; For `package--builtins'.
602 (assq package package--builtins))))))
604 (defun package--from-builtin (bi-desc)
605 (package-desc-create :name (pop bi-desc)
606 :version (package--bi-desc-version bi-desc)
607 :summary (package--bi-desc-summary bi-desc)
608 :dir 'builtin))
610 ;; This function goes ahead and activates a newer version of a package
611 ;; if an older one was already activated. This is not ideal; we'd at
612 ;; least need to check to see if the package has actually been loaded,
613 ;; and not merely activated.
614 (defun package-activate (package &optional force)
615 "Activate package PACKAGE.
616 If FORCE is true, (re-)activate it if it's already activated."
617 (let ((pkg-descs (cdr (assq package package-alist))))
618 ;; Check if PACKAGE is available in `package-alist'.
619 (while
620 (when pkg-descs
621 (let ((available-version (package-desc-version (car pkg-descs))))
622 (or (package-disabled-p package available-version)
623 ;; Prefer a builtin package.
624 (package-built-in-p package available-version))))
625 (setq pkg-descs (cdr pkg-descs)))
626 (cond
627 ;; If no such package is found, maybe it's built-in.
628 ((null pkg-descs)
629 (package-built-in-p package))
630 ;; If the package is already activated, just return t.
631 ((and (memq package package-activated-list) (not force))
633 ;; Otherwise, proceed with activation.
635 (let* ((pkg-vec (car pkg-descs))
636 (fail (catch 'dep-failure
637 ;; Activate its dependencies recursively.
638 (dolist (req (package-desc-reqs pkg-vec))
639 (unless (package-activate (car req) (cadr req))
640 (throw 'dep-failure req))))))
641 (if fail
642 (warn "Unable to activate package `%s'.
643 Required package `%s-%s' is unavailable"
644 package (car fail) (package-version-join (cadr fail)))
645 ;; If all goes well, activate the package itself.
646 (package-activate-1 pkg-vec)))))))
648 (defun define-package (_name-string _version-string
649 &optional _docstring _requirements
650 &rest _extra-properties)
651 "Define a new package.
652 NAME-STRING is the name of the package, as a string.
653 VERSION-STRING is the version of the package, as a string.
654 DOCSTRING is a short description of the package, a string.
655 REQUIREMENTS is a list of dependencies on other packages.
656 Each requirement is of the form (OTHER-PACKAGE OTHER-VERSION),
657 where OTHER-VERSION is a string.
659 EXTRA-PROPERTIES is currently unused."
660 ;; FIXME: Placeholder! Should we keep it?
661 (error "Don't call me!"))
663 (defun package-process-define-package (exp origin)
664 (unless (eq (car-safe exp) 'define-package)
665 (error "Can't find define-package in %s" origin))
666 (let* ((new-pkg-desc (apply #'package-desc-from-define (cdr exp)))
667 (name (package-desc-name new-pkg-desc))
668 (version (package-desc-version new-pkg-desc))
669 (old-pkgs (assq name package-alist)))
670 (if (null old-pkgs)
671 ;; If there's no old package, just add this to `package-alist'.
672 (push (list name new-pkg-desc) package-alist)
673 ;; If there is, insert the new package at the right place in the list.
674 (while
675 (if (and (cdr old-pkgs)
676 (version-list-< version
677 (package-desc-version (cadr old-pkgs))))
678 (setq old-pkgs (cdr old-pkgs))
679 (push new-pkg-desc (cdr old-pkgs))
680 nil)))
681 new-pkg-desc))
683 ;; From Emacs 22, but changed so it adds to load-path.
684 (defun package-autoload-ensure-default-file (file)
685 "Make sure that the autoload file FILE exists and if not create it."
686 (unless (file-exists-p file)
687 (write-region
688 (concat ";;; " (file-name-nondirectory file)
689 " --- automatically extracted autoloads\n"
690 ";;\n"
691 ";;; Code:\n"
692 "(add-to-list 'load-path (or (file-name-directory #$) (car load-path)))\n"
693 "\f\n;; Local Variables:\n"
694 ";; version-control: never\n"
695 ";; no-byte-compile: t\n"
696 ";; no-update-autoloads: t\n"
697 ";; End:\n"
698 ";;; " (file-name-nondirectory file)
699 " ends here\n")
700 nil file nil 'silent))
701 file)
703 (defvar generated-autoload-file)
704 (defvar version-control)
706 (defun package-generate-autoloads (name pkg-dir)
707 (let* ((auto-name (format "%s-autoloads.el" name))
708 ;;(ignore-name (concat name "-pkg.el"))
709 (generated-autoload-file (expand-file-name auto-name pkg-dir))
710 (backup-inhibited t)
711 (version-control 'never))
712 (package-autoload-ensure-default-file generated-autoload-file)
713 (update-directory-autoloads pkg-dir)
714 (let ((buf (find-buffer-visiting generated-autoload-file)))
715 (when buf (kill-buffer buf)))
716 auto-name))
718 (defvar tar-parse-info)
719 (declare-function tar-untar-buffer "tar-mode" ())
720 (declare-function tar-header-name "tar-mode" (tar-header) t)
721 (declare-function tar-header-link-type "tar-mode" (tar-header) t)
723 (defun package-untar-buffer (dir)
724 "Untar the current buffer.
725 This uses `tar-untar-buffer' from Tar mode. All files should
726 untar into a directory named DIR; otherwise, signal an error."
727 (require 'tar-mode)
728 (tar-mode)
729 ;; Make sure everything extracts into DIR.
730 (let ((regexp (concat "\\`" (regexp-quote (expand-file-name dir)) "/"))
731 (case-fold-search (memq system-type '(windows-nt ms-dos cygwin))))
732 (dolist (tar-data tar-parse-info)
733 (let ((name (expand-file-name (tar-header-name tar-data))))
734 (or (string-match regexp name)
735 ;; Tarballs created by some utilities don't list
736 ;; directories with a trailing slash (Bug#13136).
737 (and (string-equal dir name)
738 (eq (tar-header-link-type tar-data) 5))
739 (error "Package does not untar cleanly into directory %s/" dir)))))
740 (tar-untar-buffer))
742 (defun package-generate-description-file (pkg-desc pkg-file)
743 "Create the foo-pkg.el file for single-file packages."
744 (let* ((name (package-desc-name pkg-desc)))
745 (let ((print-level nil)
746 (print-quoted t)
747 (print-length nil))
748 (write-region
749 (concat
750 ";;; -*- no-byte-compile: t -*-\n"
751 (prin1-to-string
752 (nconc
753 (list 'define-package
754 (symbol-name name)
755 (package-version-join (package-desc-version pkg-desc))
756 (package-desc-summary pkg-desc)
757 (let ((requires (package-desc-reqs pkg-desc)))
758 (list 'quote
759 ;; Turn version lists into string form.
760 (mapcar
761 (lambda (elt)
762 (list (car elt)
763 (package-version-join (cadr elt))))
764 requires))))
765 (package--alist-to-plist-args
766 (package-desc-extras pkg-desc))))
767 "\n")
768 nil pkg-file nil 'silent))))
770 (defun package--alist-to-plist-args (alist)
771 (mapcar 'macroexp-quote
772 (apply #'nconc
773 (mapcar (lambda (pair) (list (car pair) (cdr pair))) alist))))
774 (defun package-unpack (pkg-desc)
775 "Install the contents of the current buffer as a package."
776 (let* ((name (package-desc-name pkg-desc))
777 (dirname (package-desc-full-name pkg-desc))
778 (pkg-dir (expand-file-name dirname package-user-dir)))
779 (pcase (package-desc-kind pkg-desc)
780 (`tar
781 (make-directory package-user-dir t)
782 ;; FIXME: should we delete PKG-DIR if it exists?
783 (let* ((default-directory (file-name-as-directory package-user-dir)))
784 (package-untar-buffer dirname)))
785 (`single
786 (let ((el-file (expand-file-name (format "%s.el" name) pkg-dir)))
787 (make-directory pkg-dir t)
788 (package--write-file-no-coding el-file)))
789 (kind (error "Unknown package kind: %S" kind)))
790 (package--make-autoloads-and-stuff pkg-desc pkg-dir)
791 ;; Update package-alist.
792 (let ((new-desc (package-load-descriptor pkg-dir)))
793 ;; FIXME: Check that `new-desc' matches `desc'!
794 ;; FIXME: Compilation should be done as a separate, optional, step.
795 ;; E.g. for multi-package installs, we should first install all packages
796 ;; and then compile them.
797 (package--compile new-desc))
798 ;; Try to activate it.
799 (package-activate name 'force)
800 pkg-dir))
802 (defun package--make-autoloads-and-stuff (pkg-desc pkg-dir)
803 "Generate autoloads, description file, etc.. for PKG-DESC installed at PKG-DIR."
804 (package-generate-autoloads (package-desc-name pkg-desc) pkg-dir)
805 (let ((desc-file (expand-file-name (package--description-file pkg-dir)
806 pkg-dir)))
807 (unless (file-exists-p desc-file)
808 (package-generate-description-file pkg-desc desc-file)))
809 ;; FIXME: Create foo.info and dir file from foo.texi?
812 (defun package--compile (pkg-desc)
813 "Byte-compile installed package PKG-DESC."
814 (package-activate-1 pkg-desc)
815 (byte-recompile-directory (package-desc-dir pkg-desc) 0 t))
817 (defun package--write-file-no-coding (file-name)
818 (let ((buffer-file-coding-system 'no-conversion))
819 (write-region (point-min) (point-max) file-name nil 'silent)))
821 (defmacro package--with-work-buffer (location file &rest body)
822 "Run BODY in a buffer containing the contents of FILE at LOCATION.
823 LOCATION is the base location of a package archive, and should be
824 one of the URLs (or file names) specified in `package-archives'.
825 FILE is the name of a file relative to that base location.
827 This macro retrieves FILE from LOCATION into a temporary buffer,
828 and evaluates BODY while that buffer is current. This work
829 buffer is killed afterwards. Return the last value in BODY."
830 (declare (indent 2) (debug t))
831 `(with-temp-buffer
832 (if (string-match-p "\\`https?:" ,location)
833 (url-insert-file-contents (concat ,location ,file))
834 (unless (file-name-absolute-p ,location)
835 (error "Archive location %s is not an absolute file name"
836 ,location))
837 (insert-file-contents (expand-file-name ,file ,location)))
838 ,@body))
840 (defun package--archive-file-exists-p (location file)
841 (let ((http (string-match "\\`https?:" location)))
842 (if http
843 (progn
844 (require 'url-http)
845 (url-http-file-exists-p (concat location file)))
846 (file-exists-p (expand-file-name file location)))))
848 (declare-function epg-make-context "epg"
849 (&optional protocol armor textmode include-certs
850 cipher-algorithm
851 digest-algorithm
852 compress-algorithm))
853 (declare-function epg-verify-string "epg" (context signature
854 &optional signed-text))
855 (declare-function epg-context-result-for "epg" (context name))
856 (declare-function epg-signature-status "epg" (signature))
857 (declare-function epg-signature-to-string "epg" (signature))
859 (defun package--display-verify-error (context sig-file)
860 (unless (equal (epg-context-error-output context) "")
861 (with-output-to-temp-buffer "*Error*"
862 (with-current-buffer standard-output
863 (if (epg-context-result-for context 'verify)
864 (insert (format "Failed to verify signature %s:\n" sig-file)
865 (mapconcat #'epg-signature-to-string
866 (epg-context-result-for context 'verify)
867 "\n"))
868 (insert (format "Error while verifying signature %s:\n" sig-file)))
869 (insert "\nCommand output:\n" (epg-context-error-output context))))))
871 (defun package--check-signature (location file)
872 "Check signature of the current buffer.
873 GnuPG keyring is located under \"gnupg\" in `package-user-dir'."
874 (let* ((context (epg-make-context 'OpenPGP))
875 (homedir (expand-file-name "gnupg" package-user-dir))
876 (sig-file (concat file ".sig"))
877 (sig-content (package--with-work-buffer location sig-file
878 (buffer-string))))
879 (setf (epg-context-home-directory context) homedir)
880 (condition-case error
881 (epg-verify-string context sig-content (buffer-string))
882 (error
883 (package--display-verify-error context sig-file)
884 (signal (car error) (cdr error))))
885 (let (good-signatures had-fatal-error)
886 ;; The .sig file may contain multiple signatures. Success if one
887 ;; of the signatures is good.
888 (dolist (sig (epg-context-result-for context 'verify))
889 (if (eq (epg-signature-status sig) 'good)
890 (push sig good-signatures)
891 ;; If package-check-signature is allow-unsigned, don't
892 ;; signal error when we can't verify signature because of
893 ;; missing public key. Other errors are still treated as
894 ;; fatal (bug#17625).
895 (unless (and (eq package-check-signature 'allow-unsigned)
896 (eq (epg-signature-status sig) 'no-pubkey))
897 (setq had-fatal-error t))))
898 (when (and (null good-signatures) had-fatal-error)
899 (package--display-verify-error context sig-file)
900 (error "Failed to verify signature %s" sig-file))
901 good-signatures)))
903 (defun package-install-from-archive (pkg-desc)
904 "Download and install a tar package."
905 (let* ((location (package-archive-base pkg-desc))
906 (file (concat (package-desc-full-name pkg-desc)
907 (package-desc-suffix pkg-desc)))
908 (sig-file (concat file ".sig"))
909 good-signatures pkg-descs)
910 (package--with-work-buffer location file
911 (if (and package-check-signature
912 (not (member (package-desc-archive pkg-desc)
913 package-unsigned-archives)))
914 (if (package--archive-file-exists-p location sig-file)
915 (setq good-signatures (package--check-signature location file))
916 (unless (eq package-check-signature 'allow-unsigned)
917 (error "Unsigned package: `%s'"
918 (package-desc-name pkg-desc)))))
919 (package-unpack pkg-desc))
920 ;; Here the package has been installed successfully, mark it as
921 ;; signed if appropriate.
922 (when good-signatures
923 ;; Write out good signatures into NAME-VERSION.signed file.
924 (write-region (mapconcat #'epg-signature-to-string good-signatures "\n")
926 (expand-file-name
927 (concat (package-desc-full-name pkg-desc)
928 ".signed")
929 package-user-dir)
930 nil 'silent)
931 ;; Update the old pkg-desc which will be shown on the description buffer.
932 (setf (package-desc-signed pkg-desc) t)
933 ;; Update the new (activated) pkg-desc as well.
934 (setq pkg-descs (cdr (assq (package-desc-name pkg-desc) package-alist)))
935 (if pkg-descs
936 (setf (package-desc-signed (car pkg-descs)) t)))))
938 (defvar package--initialized nil)
940 (defun package-installed-p (package &optional min-version)
941 "Return true if PACKAGE, of MIN-VERSION or newer, is installed.
942 MIN-VERSION should be a version list."
943 (unless package--initialized (error "package.el is not yet initialized!"))
945 (let ((pkg-descs (cdr (assq package package-alist))))
946 (and pkg-descs
947 (version-list-<= min-version
948 (package-desc-version (car pkg-descs)))))
949 ;; Also check built-in packages.
950 (package-built-in-p package min-version)))
952 (defun package-compute-transaction (packages requirements &optional seen)
953 "Return a list of packages to be installed, including PACKAGES.
954 PACKAGES should be a list of `package-desc'.
956 REQUIREMENTS should be a list of additional requirements; each
957 element in this list should have the form (PACKAGE VERSION-LIST),
958 where PACKAGE is a package name and VERSION-LIST is the required
959 version of that package.
961 This function recursively computes the requirements of the
962 packages in REQUIREMENTS, and returns a list of all the packages
963 that must be installed. Packages that are already installed are
964 not included in this list.
966 SEEN is used internally to detect infinite recursion."
967 ;; FIXME: We really should use backtracking to explore the whole
968 ;; search space (e.g. if foo require bar-1.3, and bar-1.4 requires toto-1.1
969 ;; whereas bar-1.3 requires toto-1.0 and the user has put a hold on toto-1.0:
970 ;; the current code might fail to see that it could install foo by using the
971 ;; older bar-1.3).
972 (dolist (elt requirements)
973 (let* ((next-pkg (car elt))
974 (next-version (cadr elt))
975 (already ()))
976 (dolist (pkg packages)
977 (if (eq next-pkg (package-desc-name pkg))
978 (setq already pkg)))
979 (when already
980 (if (version-list-<= next-version (package-desc-version already))
981 ;; `next-pkg' is already in `packages', but its position there
982 ;; means it might be installed too late: remove it from there, so
983 ;; we re-add it (along with its dependencies) at an earlier place
984 ;; below (bug#16994).
985 (if (memq already seen) ;Avoid inf-loop on dependency cycles.
986 (message "Dependency cycle going through %S"
987 (package-desc-full-name already))
988 (setq packages (delq already packages))
989 (setq already nil))
990 (error "Need package `%s-%s', but only %s is being installed"
991 next-pkg (package-version-join next-version)
992 (package-version-join (package-desc-version already)))))
993 (cond
994 (already nil)
995 ((package-installed-p next-pkg next-version) nil)
998 ;; A package is required, but not installed. It might also be
999 ;; blocked via `package-load-list'.
1000 (let ((pkg-descs (cdr (assq next-pkg package-archive-contents)))
1001 (found nil)
1002 (problem nil))
1003 (while (and pkg-descs (not found))
1004 (let* ((pkg-desc (pop pkg-descs))
1005 (version (package-desc-version pkg-desc))
1006 (disabled (package-disabled-p next-pkg version)))
1007 (cond
1008 ((version-list-< version next-version)
1009 (error
1010 "Need package `%s-%s', but only %s is available"
1011 next-pkg (package-version-join next-version)
1012 (package-version-join version)))
1013 (disabled
1014 (unless problem
1015 (setq problem
1016 (if (stringp disabled)
1017 (format "Package `%s' held at version %s, \
1018 but version %s required"
1019 next-pkg disabled
1020 (package-version-join next-version))
1021 (format "Required package '%s' is disabled"
1022 next-pkg)))))
1023 (t (setq found pkg-desc)))))
1024 (unless found
1025 (if problem
1026 (error "%s" problem)
1027 (error "Package `%s-%s' is unavailable"
1028 next-pkg (package-version-join next-version))))
1029 (setq packages
1030 (package-compute-transaction (cons found packages)
1031 (package-desc-reqs found)
1032 (cons found seen))))))))
1033 packages)
1035 (defun package-read-from-string (str)
1036 "Read a Lisp expression from STR.
1037 Signal an error if the entire string was not used."
1038 (let* ((read-data (read-from-string str))
1039 (more-left
1040 (condition-case nil
1041 ;; The call to `ignore' suppresses a compiler warning.
1042 (progn (ignore (read-from-string
1043 (substring str (cdr read-data))))
1045 (end-of-file nil))))
1046 (if more-left
1047 (error "Can't read whole string")
1048 (car read-data))))
1050 (defun package--read-archive-file (file)
1051 "Re-read archive file FILE, if it exists.
1052 Will return the data from the file, or nil if the file does not exist.
1053 Will throw an error if the archive version is too new."
1054 (let ((filename (expand-file-name file package-user-dir)))
1055 (when (file-exists-p filename)
1056 (with-temp-buffer
1057 (insert-file-contents-literally filename)
1058 (let ((contents (read (current-buffer))))
1059 (if (> (car contents) package-archive-version)
1060 (error "Package archive version %d is higher than %d"
1061 (car contents) package-archive-version))
1062 (cdr contents))))))
1064 (defun package-read-all-archive-contents ()
1065 "Re-read `archive-contents', if it exists.
1066 If successful, set `package-archive-contents'."
1067 (setq package-archive-contents nil)
1068 (dolist (archive package-archives)
1069 (package-read-archive-contents (car archive))))
1071 (defun package-read-archive-contents (archive)
1072 "Re-read archive contents for ARCHIVE.
1073 If successful, set the variable `package-archive-contents'.
1074 If the archive version is too new, signal an error."
1075 ;; Version 1 of 'archive-contents' is identical to our internal
1076 ;; representation.
1077 (let* ((contents-file (format "archives/%s/archive-contents" archive))
1078 (contents (package--read-archive-file contents-file)))
1079 (when contents
1080 (dolist (package contents)
1081 (package--add-to-archive-contents package archive)))))
1083 ;; Package descriptor objects used inside the "archive-contents" file.
1084 ;; Changing this defstruct implies changing the format of the
1085 ;; "archive-contents" files.
1086 (cl-defstruct (package--ac-desc
1087 (:constructor package-make-ac-desc (version reqs summary kind extras))
1088 (:copier nil)
1089 (:type vector))
1090 version reqs summary kind extras)
1092 (defun package--add-to-archive-contents (package archive)
1093 "Add the PACKAGE from the given ARCHIVE if necessary.
1094 PACKAGE should have the form (NAME . PACKAGE--AC-DESC).
1095 Also, add the originating archive to the `package-desc' structure."
1096 (let* ((name (car package))
1097 (version (package--ac-desc-version (cdr package)))
1098 (pkg-desc
1099 (package-desc-create
1100 :name name
1101 :version version
1102 :reqs (package--ac-desc-reqs (cdr package))
1103 :summary (package--ac-desc-summary (cdr package))
1104 :kind (package--ac-desc-kind (cdr package))
1105 :archive archive
1106 :extras (and (> (length (cdr package)) 4)
1107 ;; Older archive-contents files have only 4
1108 ;; elements here.
1109 (package--ac-desc-extras (cdr package)))))
1110 (existing-packages (assq name package-archive-contents))
1111 (pinned-to-archive (assoc name package-pinned-packages)))
1112 (cond
1113 ;; Skip entirely if pinned to another archive.
1114 ((and pinned-to-archive
1115 (not (equal (cdr pinned-to-archive) archive)))
1116 nil)
1117 ((not existing-packages)
1118 (push (list name pkg-desc) package-archive-contents))
1120 (while
1121 (if (and (cdr existing-packages)
1122 (version-list-<
1123 version (package-desc-version (cadr existing-packages))))
1124 (setq existing-packages (cdr existing-packages))
1125 (push pkg-desc (cdr existing-packages))
1126 nil))))))
1128 (defun package-download-transaction (packages)
1129 "Download and install all the packages in PACKAGES.
1130 PACKAGES should be a list of package-desc.
1131 This function assumes that all package requirements in
1132 PACKAGES are satisfied, i.e. that PACKAGES is computed
1133 using `package-compute-transaction'."
1134 (mapc #'package-install-from-archive packages))
1136 ;;;###autoload
1137 (defun package-install (pkg)
1138 "Install the package PKG.
1139 PKG can be a package-desc or the package name of one the available packages
1140 in an archive in `package-archives'. Interactively, prompt for its name."
1141 (interactive
1142 (progn
1143 ;; Initialize the package system to get the list of package
1144 ;; symbols for completion.
1145 (unless package--initialized
1146 (package-initialize t))
1147 (unless package-archive-contents
1148 (package-refresh-contents))
1149 (list (intern (completing-read
1150 "Install package: "
1151 (delq nil
1152 (mapcar (lambda (elt)
1153 (unless (package-installed-p (car elt))
1154 (symbol-name (car elt))))
1155 package-archive-contents))
1156 nil t)))))
1157 (package-download-transaction
1158 (if (package-desc-p pkg)
1159 (package-compute-transaction (list pkg)
1160 (package-desc-reqs pkg))
1161 (package-compute-transaction ()
1162 (list (list pkg))))))
1164 (defun package-strip-rcs-id (str)
1165 "Strip RCS version ID from the version string STR.
1166 If the result looks like a dotted numeric version, return it.
1167 Otherwise return nil."
1168 (when str
1169 (when (string-match "\\`[ \t]*[$]Revision:[ \t]+" str)
1170 (setq str (substring str (match-end 0))))
1171 (condition-case nil
1172 (if (version-to-list str)
1173 str)
1174 (error nil))))
1176 (declare-function lm-homepage "lisp-mnt" (&optional file))
1178 (defun package--prepare-dependencies (deps)
1179 "Turn DEPS into an acceptable list of dependencies.
1181 Any parts missing a version string get a default version string
1182 of \"0\" (meaning any version) and an appropriate level of lists
1183 is wrapped around any parts requiring it."
1184 (cond
1185 ((not (listp deps))
1186 (error "Invalid requirement specifier: %S" deps))
1187 (t (mapcar (lambda (dep)
1188 (cond
1189 ((symbolp dep) `(,dep "0"))
1190 ((stringp dep)
1191 (error "Invalid requirement specifier: %S" dep))
1192 ((and (listp dep) (null (cdr dep)))
1193 (list (car dep) "0"))
1194 (t dep)))
1195 deps))))
1197 (defun package-buffer-info ()
1198 "Return a `package-desc' describing the package in the current buffer.
1200 If the buffer does not contain a conforming package, signal an
1201 error. If there is a package, narrow the buffer to the file's
1202 boundaries."
1203 (goto-char (point-min))
1204 (unless (re-search-forward "^;;; \\([^ ]*\\)\\.el ---[ \t]*\\(.*?\\)[ \t]*\\(-\\*-.*-\\*-[ \t]*\\)?$" nil t)
1205 (error "Package lacks a file header"))
1206 (let ((file-name (match-string-no-properties 1))
1207 (desc (match-string-no-properties 2))
1208 (start (line-beginning-position)))
1209 (unless (search-forward (concat ";;; " file-name ".el ends here"))
1210 (error "Package lacks a terminating comment"))
1211 ;; Try to include a trailing newline.
1212 (forward-line)
1213 (narrow-to-region start (point))
1214 (require 'lisp-mnt)
1215 ;; Use some headers we've invented to drive the process.
1216 (let* ((requires-str (lm-header "package-requires"))
1217 ;; Prefer Package-Version; if defined, the package author
1218 ;; probably wants us to use it. Otherwise try Version.
1219 (pkg-version
1220 (or (package-strip-rcs-id (lm-header "package-version"))
1221 (package-strip-rcs-id (lm-header "version"))))
1222 (homepage (lm-homepage)))
1223 (unless pkg-version
1224 (error
1225 "Package lacks a \"Version\" or \"Package-Version\" header"))
1226 (package-desc-from-define
1227 file-name pkg-version desc
1228 (if requires-str
1229 (package--prepare-dependencies
1230 (package-read-from-string requires-str)))
1231 :kind 'single
1232 :url homepage))))
1234 (declare-function tar-get-file-descriptor "tar-mode" (file))
1235 (declare-function tar--extract "tar-mode" (descriptor))
1237 (defun package-tar-file-info ()
1238 "Find package information for a tar file.
1239 The return result is a `package-desc'."
1240 (cl-assert (derived-mode-p 'tar-mode))
1241 (let* ((dir-name (file-name-directory
1242 (tar-header-name (car tar-parse-info))))
1243 (desc-file (package--description-file dir-name))
1244 (tar-desc (tar-get-file-descriptor (concat dir-name desc-file))))
1245 (unless tar-desc
1246 (error "No package descriptor file found"))
1247 (with-current-buffer (tar--extract tar-desc)
1248 (goto-char (point-min))
1249 (unwind-protect
1250 (let* ((pkg-def-parsed (read (current-buffer)))
1251 (pkg-desc
1252 (if (not (eq (car pkg-def-parsed) 'define-package))
1253 (error "Can't find define-package in %s"
1254 (tar-header-name tar-desc))
1255 (apply #'package-desc-from-define
1256 (append (cdr pkg-def-parsed))))))
1257 (setf (package-desc-kind pkg-desc) 'tar)
1258 pkg-desc)
1259 (kill-buffer (current-buffer))))))
1262 ;;;###autoload
1263 (defun package-install-from-buffer ()
1264 "Install a package from the current buffer.
1265 The current buffer is assumed to be a single .el or .tar file that follows the
1266 packaging guidelines; see info node `(elisp)Packaging'.
1267 Downloads and installs required packages as needed."
1268 (interactive)
1269 (let ((pkg-desc (if (derived-mode-p 'tar-mode)
1270 (package-tar-file-info)
1271 (package-buffer-info))))
1272 ;; Download and install the dependencies.
1273 (let* ((requires (package-desc-reqs pkg-desc))
1274 (transaction (package-compute-transaction nil requires)))
1275 (package-download-transaction transaction))
1276 ;; Install the package itself.
1277 (package-unpack pkg-desc)
1278 pkg-desc))
1280 ;;;###autoload
1281 (defun package-install-file (file)
1282 "Install a package from a file.
1283 The file can either be a tar file or an Emacs Lisp file."
1284 (interactive "fPackage file name: ")
1285 (with-temp-buffer
1286 (insert-file-contents-literally file)
1287 (when (string-match "\\.tar\\'" file) (tar-mode))
1288 (package-install-from-buffer)))
1290 (defun package-delete (pkg-desc)
1291 (let ((dir (package-desc-dir pkg-desc)))
1292 (if (not (string-prefix-p (file-name-as-directory
1293 (expand-file-name package-user-dir))
1294 (expand-file-name dir)))
1295 ;; Don't delete "system" packages.
1296 (error "Package `%s' is a system package, not deleting"
1297 (package-desc-full-name pkg-desc))
1298 (delete-directory dir t t)
1299 ;; Remove NAME-VERSION.signed file.
1300 (let ((signed-file (concat dir ".signed")))
1301 (if (file-exists-p signed-file)
1302 (delete-file signed-file)))
1303 ;; Update package-alist.
1304 (let* ((name (package-desc-name pkg-desc))
1305 (pkgs (assq name package-alist)))
1306 (delete pkg-desc pkgs)
1307 (unless (cdr pkgs)
1308 (setq package-alist (delq pkgs package-alist))))
1309 (message "Package `%s' deleted." (package-desc-full-name pkg-desc)))))
1311 (defun package-archive-base (desc)
1312 "Return the archive containing the package NAME."
1313 (cdr (assoc (package-desc-archive desc) package-archives)))
1315 (defun package--download-one-archive (archive file)
1316 "Retrieve an archive file FILE from ARCHIVE, and cache it.
1317 ARCHIVE should be a cons cell of the form (NAME . LOCATION),
1318 similar to an entry in `package-alist'. Save the cached copy to
1319 \"archives/NAME/archive-contents\" in `package-user-dir'."
1320 (let ((dir (expand-file-name (format "archives/%s" (car archive))
1321 package-user-dir))
1322 (sig-file (concat file ".sig"))
1323 good-signatures)
1324 (package--with-work-buffer (cdr archive) file
1325 ;; Check signature of archive-contents, if desired.
1326 (if (and package-check-signature
1327 (not (member archive package-unsigned-archives)))
1328 (if (package--archive-file-exists-p (cdr archive) sig-file)
1329 (setq good-signatures (package--check-signature (cdr archive)
1330 file))
1331 (unless (eq package-check-signature 'allow-unsigned)
1332 (error "Unsigned archive `%s'"
1333 (car archive)))))
1334 ;; Read the retrieved buffer to make sure it is valid (e.g. it
1335 ;; may fetch a URL redirect page).
1336 (when (listp (read (current-buffer)))
1337 (make-directory dir t)
1338 (write-region nil nil (expand-file-name file dir) nil 'silent)))
1339 (when good-signatures
1340 ;; Write out good signatures into archive-contents.signed file.
1341 (write-region (mapconcat #'epg-signature-to-string good-signatures "\n")
1343 (expand-file-name (concat file ".signed") dir)
1344 nil 'silent))))
1346 (declare-function epg-check-configuration "epg-config"
1347 (config &optional minimum-version))
1348 (declare-function epg-configuration "epg-config" ())
1349 (declare-function epg-import-keys-from-file "epg" (context keys))
1351 ;;;###autoload
1352 (defun package-import-keyring (&optional file)
1353 "Import keys from FILE."
1354 (interactive "fFile: ")
1355 (setq file (expand-file-name file))
1356 (let ((context (epg-make-context 'OpenPGP))
1357 (homedir (expand-file-name "gnupg" package-user-dir)))
1358 (with-file-modes 448
1359 (make-directory homedir t))
1360 (setf (epg-context-home-directory context) homedir)
1361 (message "Importing %s..." (file-name-nondirectory file))
1362 (epg-import-keys-from-file context file)
1363 (message "Importing %s...done" (file-name-nondirectory file))))
1365 ;;;###autoload
1366 (defun package-refresh-contents ()
1367 "Download the ELPA archive description if needed.
1368 This informs Emacs about the latest versions of all packages, and
1369 makes them available for download."
1370 (interactive)
1371 ;; FIXME: Do it asynchronously.
1372 (unless (file-exists-p package-user-dir)
1373 (make-directory package-user-dir t))
1374 (let ((default-keyring (expand-file-name "package-keyring.gpg"
1375 data-directory)))
1376 (when (and package-check-signature (file-exists-p default-keyring))
1377 (condition-case-unless-debug error
1378 (progn
1379 (epg-check-configuration (epg-configuration))
1380 (package-import-keyring default-keyring))
1381 (error (message "Cannot import default keyring: %S" (cdr error))))))
1382 (dolist (archive package-archives)
1383 (condition-case-unless-debug nil
1384 (package--download-one-archive archive "archive-contents")
1385 (error (message "Failed to download `%s' archive."
1386 (car archive)))))
1387 (package-read-all-archive-contents))
1389 ;;;###autoload
1390 (defun package-initialize (&optional no-activate)
1391 "Load Emacs Lisp packages, and activate them.
1392 The variable `package-load-list' controls which packages to load.
1393 If optional arg NO-ACTIVATE is non-nil, don't activate packages."
1394 (interactive)
1395 (setq package-alist nil)
1396 (package-load-all-descriptors)
1397 (package-read-all-archive-contents)
1398 (unless no-activate
1399 (dolist (elt package-alist)
1400 (package-activate (car elt))))
1401 (setq package--initialized t))
1404 ;;;; Package description buffer.
1406 ;;;###autoload
1407 (defun describe-package (package)
1408 "Display the full documentation of PACKAGE (a symbol)."
1409 (interactive
1410 (let* ((guess (function-called-at-point)))
1411 (require 'finder-inf nil t)
1412 ;; Load the package list if necessary (but don't activate them).
1413 (unless package--initialized
1414 (package-initialize t))
1415 (let ((packages (append (mapcar 'car package-alist)
1416 (mapcar 'car package-archive-contents)
1417 (mapcar 'car package--builtins))))
1418 (unless (memq guess packages)
1419 (setq guess nil))
1420 (setq packages (mapcar 'symbol-name packages))
1421 (let ((val
1422 (completing-read (if guess
1423 (format "Describe package (default %s): "
1424 guess)
1425 "Describe package: ")
1426 packages nil t nil nil guess)))
1427 (list (intern val))))))
1428 (if (not (or (package-desc-p package) (and package (symbolp package))))
1429 (message "No package specified")
1430 (help-setup-xref (list #'describe-package package)
1431 (called-interactively-p 'interactive))
1432 (with-help-window (help-buffer)
1433 (with-current-buffer standard-output
1434 (describe-package-1 package)))))
1436 (defun describe-package-1 (pkg)
1437 (require 'lisp-mnt)
1438 (let* ((desc (or
1439 (if (package-desc-p pkg) pkg)
1440 (cadr (assq pkg package-alist))
1441 (let ((built-in (assq pkg package--builtins)))
1442 (if built-in
1443 (package--from-builtin built-in)
1444 (cadr (assq pkg package-archive-contents))))))
1445 (name (if desc (package-desc-name desc) pkg))
1446 (pkg-dir (if desc (package-desc-dir desc)))
1447 (reqs (if desc (package-desc-reqs desc)))
1448 (version (if desc (package-desc-version desc)))
1449 (archive (if desc (package-desc-archive desc)))
1450 (extras (and desc (package-desc-extras desc)))
1451 (homepage (cdr (assoc :url extras)))
1452 (keywords (if desc (package-desc--keywords desc)))
1453 (built-in (eq pkg-dir 'builtin))
1454 (installable (and archive (not built-in)))
1455 (status (if desc (package-desc-status desc) "orphan"))
1456 (signed (if desc (package-desc-signed desc))))
1457 (prin1 name)
1458 (princ " is ")
1459 (princ (if (memq (aref status 0) '(?a ?e ?i ?o ?u)) "an " "a "))
1460 (princ status)
1461 (princ " package.\n\n")
1463 (insert " " (propertize "Status" 'font-lock-face 'bold) ": ")
1464 (cond (built-in
1465 (insert (propertize (capitalize status)
1466 'font-lock-face 'font-lock-builtin-face)
1467 "."))
1468 (pkg-dir
1469 (insert (propertize (if (equal status "unsigned")
1470 "Installed"
1471 (capitalize status)) ;FIXME: Why comment-face?
1472 'font-lock-face 'font-lock-comment-face))
1473 (insert " in `")
1474 ;; Todo: Add button for uninstalling.
1475 (help-insert-xref-button (abbreviate-file-name
1476 (file-name-as-directory pkg-dir))
1477 'help-package-def pkg-dir)
1478 (if (and (package-built-in-p name)
1479 (not (package-built-in-p name version)))
1480 (insert "',\n shadowing a "
1481 (propertize "built-in package"
1482 'font-lock-face 'font-lock-builtin-face))
1483 (insert "'"))
1484 (if signed
1485 (insert ".")
1486 (insert " (unsigned).")))
1487 (installable
1488 (insert (capitalize status))
1489 (insert " from " (format "%s" archive))
1490 (insert " -- ")
1491 (package-make-button
1492 "Install"
1493 'action 'package-install-button-action
1494 'package-desc desc))
1495 (t (insert (capitalize status) ".")))
1496 (insert "\n")
1497 (insert " " (propertize "Archive" 'font-lock-face 'bold)
1498 ": " (or archive "n/a") "\n")
1499 (and version
1500 (insert " "
1501 (propertize "Version" 'font-lock-face 'bold) ": "
1502 (package-version-join version) "\n"))
1504 (setq reqs (if desc (package-desc-reqs desc)))
1505 (when reqs
1506 (insert " " (propertize "Requires" 'font-lock-face 'bold) ": ")
1507 (let ((first t)
1508 name vers text)
1509 (dolist (req reqs)
1510 (setq name (car req)
1511 vers (cadr req)
1512 text (format "%s-%s" (symbol-name name)
1513 (package-version-join vers)))
1514 (cond (first (setq first nil))
1515 ((>= (+ 2 (current-column) (length text))
1516 (window-width))
1517 (insert ",\n "))
1518 (t (insert ", ")))
1519 (help-insert-xref-button text 'help-package name))
1520 (insert "\n")))
1521 (insert " " (propertize "Summary" 'font-lock-face 'bold)
1522 ": " (if desc (package-desc-summary desc)) "\n")
1523 (when homepage
1524 (insert " " (propertize "Homepage" 'font-lock-face 'bold) ": ")
1525 (help-insert-xref-button homepage 'help-url homepage)
1526 (insert "\n"))
1527 (when keywords
1528 (insert " " (propertize "Keywords" 'font-lock-face 'bold) ": ")
1529 (dolist (k keywords)
1530 (package-make-button
1532 'package-keyword k
1533 'action 'package-keyword-button-action)
1534 (insert " "))
1535 (insert "\n"))
1536 (let* ((all-pkgs (append (cdr (assq name package-alist))
1537 (cdr (assq name package-archive-contents))
1538 (let ((bi (assq name package--builtins)))
1539 (if bi (list (package--from-builtin bi))))))
1540 (other-pkgs (delete desc all-pkgs)))
1541 (when other-pkgs
1542 (insert " " (propertize "Other versions" 'font-lock-face 'bold) ": "
1543 (mapconcat
1544 (lambda (opkg)
1545 (let* ((ov (package-desc-version opkg))
1546 (dir (package-desc-dir opkg))
1547 (from (or (package-desc-archive opkg)
1548 (if (stringp dir) "installed" dir))))
1549 (if (not ov) (format "%s" from)
1550 (format "%s (%s)"
1551 (make-text-button (package-version-join ov) nil
1552 'face 'link
1553 'follow-link t
1554 'action
1555 (lambda (_button)
1556 (describe-package opkg)))
1557 from))))
1558 other-pkgs ", ")
1559 ".\n")))
1561 (insert "\n")
1563 (if built-in
1564 ;; For built-in packages, insert the commentary.
1565 (let ((fn (locate-file (format "%s.el" name) load-path
1566 load-file-rep-suffixes))
1567 (opoint (point)))
1568 (insert (or (lm-commentary fn) ""))
1569 (save-excursion
1570 (goto-char opoint)
1571 (when (re-search-forward "^;;; Commentary:\n" nil t)
1572 (replace-match ""))
1573 (while (re-search-forward "^\\(;+ ?\\)" nil t)
1574 (replace-match ""))))
1575 (let ((readme (expand-file-name (format "%s-readme.txt" name)
1576 package-user-dir))
1577 readme-string)
1578 ;; For elpa packages, try downloading the commentary. If that
1579 ;; fails, try an existing readme file in `package-user-dir'.
1580 (cond ((condition-case nil
1581 (save-excursion
1582 (package--with-work-buffer
1583 (package-archive-base desc)
1584 (format "%s-readme.txt" name)
1585 (save-excursion
1586 (goto-char (point-max))
1587 (unless (bolp)
1588 (insert ?\n)))
1589 (write-region nil nil
1590 (expand-file-name readme package-user-dir)
1591 nil 'silent)
1592 (setq readme-string (buffer-string))
1594 (error nil))
1595 (insert readme-string))
1596 ((file-readable-p readme)
1597 (insert-file-contents readme)
1598 (goto-char (point-max))))))))
1600 (defun package-install-button-action (button)
1601 (let ((pkg-desc (button-get button 'package-desc)))
1602 (when (y-or-n-p (format "Install package `%s'? "
1603 (package-desc-full-name pkg-desc)))
1604 (package-install pkg-desc)
1605 (revert-buffer nil t)
1606 (goto-char (point-min)))))
1608 (defun package-keyword-button-action (button)
1609 (let ((pkg-keyword (button-get button 'package-keyword)))
1610 (package-show-package-list t (list pkg-keyword))))
1612 (defun package-make-button (text &rest props)
1613 (let ((button-text (if (display-graphic-p) text (concat "[" text "]")))
1614 (button-face (if (display-graphic-p)
1615 '(:box (:line-width 2 :color "dark grey")
1616 :background "light grey"
1617 :foreground "black")
1618 'link)))
1619 (apply 'insert-text-button button-text 'face button-face 'follow-link t
1620 props)))
1623 ;;;; Package menu mode.
1625 (defvar package-menu-mode-map
1626 (let ((map (make-sparse-keymap))
1627 (menu-map (make-sparse-keymap "Package")))
1628 (set-keymap-parent map tabulated-list-mode-map)
1629 (define-key map "\C-m" 'package-menu-describe-package)
1630 (define-key map "u" 'package-menu-mark-unmark)
1631 (define-key map "\177" 'package-menu-backup-unmark)
1632 (define-key map "d" 'package-menu-mark-delete)
1633 (define-key map "i" 'package-menu-mark-install)
1634 (define-key map "U" 'package-menu-mark-upgrades)
1635 (define-key map "r" 'package-menu-refresh)
1636 (define-key map "f" 'package-menu-filter)
1637 (define-key map "~" 'package-menu-mark-obsolete-for-deletion)
1638 (define-key map "x" 'package-menu-execute)
1639 (define-key map "h" 'package-menu-quick-help)
1640 (define-key map "?" 'package-menu-describe-package)
1641 (define-key map [menu-bar package-menu] (cons "Package" menu-map))
1642 (define-key menu-map [mq]
1643 '(menu-item "Quit" quit-window
1644 :help "Quit package selection"))
1645 (define-key menu-map [s1] '("--"))
1646 (define-key menu-map [mn]
1647 '(menu-item "Next" next-line
1648 :help "Next Line"))
1649 (define-key menu-map [mp]
1650 '(menu-item "Previous" previous-line
1651 :help "Previous Line"))
1652 (define-key menu-map [s2] '("--"))
1653 (define-key menu-map [mu]
1654 '(menu-item "Unmark" package-menu-mark-unmark
1655 :help "Clear any marks on a package and move to the next line"))
1656 (define-key menu-map [munm]
1657 '(menu-item "Unmark Backwards" package-menu-backup-unmark
1658 :help "Back up one line and clear any marks on that package"))
1659 (define-key menu-map [md]
1660 '(menu-item "Mark for Deletion" package-menu-mark-delete
1661 :help "Mark a package for deletion and move to the next line"))
1662 (define-key menu-map [mi]
1663 '(menu-item "Mark for Install" package-menu-mark-install
1664 :help "Mark a package for installation and move to the next line"))
1665 (define-key menu-map [mupgrades]
1666 '(menu-item "Mark Upgradable Packages" package-menu-mark-upgrades
1667 :help "Mark packages that have a newer version for upgrading"))
1668 (define-key menu-map [s3] '("--"))
1669 (define-key menu-map [mf]
1670 '(menu-item "Filter Package List..." package-menu-filter
1671 :help "Filter package selection (q to go back)"))
1672 (define-key menu-map [mg]
1673 '(menu-item "Update Package List" revert-buffer
1674 :help "Update the list of packages"))
1675 (define-key menu-map [mr]
1676 '(menu-item "Refresh Package List" package-menu-refresh
1677 :help "Download the ELPA archive"))
1678 (define-key menu-map [s4] '("--"))
1679 (define-key menu-map [mt]
1680 '(menu-item "Mark Obsolete Packages" package-menu-mark-obsolete-for-deletion
1681 :help "Mark all obsolete packages for deletion"))
1682 (define-key menu-map [mx]
1683 '(menu-item "Execute Actions" package-menu-execute
1684 :help "Perform all the marked actions"))
1685 (define-key menu-map [s5] '("--"))
1686 (define-key menu-map [mh]
1687 '(menu-item "Help" package-menu-quick-help
1688 :help "Show short key binding help for package-menu-mode"))
1689 (define-key menu-map [mc]
1690 '(menu-item "Describe Package" package-menu-describe-package
1691 :help "Display information about this package"))
1692 map)
1693 "Local keymap for `package-menu-mode' buffers.")
1695 (defvar package-menu--new-package-list nil
1696 "List of newly-available packages since `list-packages' was last called.")
1698 (define-derived-mode package-menu-mode tabulated-list-mode "Package Menu"
1699 "Major mode for browsing a list of packages.
1700 Letters do not insert themselves; instead, they are commands.
1701 \\<package-menu-mode-map>
1702 \\{package-menu-mode-map}"
1703 (setq tabulated-list-format
1704 `[("Package" 18 package-menu--name-predicate)
1705 ("Version" 12 nil)
1706 ("Status" 10 package-menu--status-predicate)
1707 ,@(if (cdr package-archives)
1708 '(("Archive" 10 package-menu--archive-predicate)))
1709 ("Description" 0 nil)])
1710 (setq tabulated-list-padding 2)
1711 (setq tabulated-list-sort-key (cons "Status" nil))
1712 (add-hook 'tabulated-list-revert-hook 'package-menu--refresh nil t)
1713 (tabulated-list-init-header))
1715 (defmacro package--push (pkg-desc status listname)
1716 "Convenience macro for `package-menu--generate'.
1717 If the alist stored in the symbol LISTNAME lacks an entry for a
1718 package PKG-DESC, add one. The alist is keyed with PKG-DESC."
1719 `(unless (assoc ,pkg-desc ,listname)
1720 ;; FIXME: Should we move status into pkg-desc?
1721 (push (cons ,pkg-desc ,status) ,listname)))
1723 (defvar package-list-unversioned nil
1724 "If non-nil include packages that don't have a version in `list-package'.")
1726 (defvar package-list-unsigned nil
1727 "If non-nil, mention in the list which packages were installed w/o signature.")
1729 (defun package-desc-status (pkg-desc)
1730 (let* ((name (package-desc-name pkg-desc))
1731 (dir (package-desc-dir pkg-desc))
1732 (lle (assq name package-load-list))
1733 (held (cadr lle))
1734 (version (package-desc-version pkg-desc))
1735 (signed (package-desc-signed pkg-desc)))
1736 (cond
1737 ((eq dir 'builtin) "built-in")
1738 ((and lle (null held)) "disabled")
1739 ((stringp held)
1740 (let ((hv (if (stringp held) (version-to-list held))))
1741 (cond
1742 ((version-list-= version hv) "held")
1743 ((version-list-< version hv) "obsolete")
1744 (t "disabled"))))
1745 ((package-built-in-p name version) "obsolete")
1746 (dir ;One of the installed packages.
1747 (cond
1748 ((not (file-exists-p (package-desc-dir pkg-desc))) "deleted")
1749 ((eq pkg-desc (cadr (assq name package-alist)))
1750 (if (or (not package-list-unsigned) signed) "installed" "unsigned"))
1751 (t "obsolete")))
1753 (let* ((ins (cadr (assq name package-alist)))
1754 (ins-v (if ins (package-desc-version ins))))
1755 (cond
1756 ((or (null ins) (version-list-< ins-v version))
1757 (if (memq name package-menu--new-package-list)
1758 "new" "available"))
1759 ((version-list-< version ins-v) "obsolete")
1760 ((version-list-= version ins-v)
1761 (if (or (not package-list-unsigned) signed)
1762 "installed" "unsigned"))))))))
1764 (defun package-menu--refresh (&optional packages keywords)
1765 "Re-populate the `tabulated-list-entries'.
1766 PACKAGES should be nil or t, which means to display all known packages.
1767 KEYWORDS should be nil or a list of keywords."
1768 ;; Construct list of (PKG-DESC . STATUS).
1769 (unless packages (setq packages t))
1770 (let (info-list name)
1771 ;; Installed packages:
1772 (dolist (elt package-alist)
1773 (setq name (car elt))
1774 (when (or (eq packages t) (memq name packages))
1775 (dolist (pkg (cdr elt))
1776 (when (package--has-keyword-p pkg keywords)
1777 (package--push pkg (package-desc-status pkg) info-list)))))
1779 ;; Built-in packages:
1780 (dolist (elt package--builtins)
1781 (setq name (car elt))
1782 (when (and (not (eq name 'emacs)) ; Hide the `emacs' package.
1783 (package--has-keyword-p (package--from-builtin elt) keywords)
1784 (or package-list-unversioned
1785 (package--bi-desc-version (cdr elt)))
1786 (or (eq packages t) (memq name packages)))
1787 (package--push (package--from-builtin elt) "built-in" info-list)))
1789 ;; Available and disabled packages:
1790 (dolist (elt package-archive-contents)
1791 (setq name (car elt))
1792 (when (or (eq packages t) (memq name packages))
1793 (dolist (pkg (cdr elt))
1794 ;; Hide obsolete packages.
1795 (when (and (not (package-installed-p (package-desc-name pkg)
1796 (package-desc-version pkg)))
1797 (package--has-keyword-p pkg keywords))
1798 (package--push pkg (package-desc-status pkg) info-list)))))
1800 ;; Print the result.
1801 (setq tabulated-list-entries
1802 (mapcar #'package-menu--print-info info-list))))
1804 (defun package-all-keywords ()
1805 "Collect all package keywords"
1806 (let (keywords)
1807 (package--mapc (lambda (desc)
1808 (let* ((desc-keywords (and desc (package-desc--keywords desc))))
1809 (setq keywords (append keywords desc-keywords)))))
1810 keywords))
1812 (defun package--mapc (function &optional packages)
1813 "Call FUNCTION for all known PACKAGES.
1814 PACKAGES can be nil or t, which means to display all known
1815 packages, or a list of packages.
1817 Built-in packages are converted with `package--from-builtin'."
1818 (unless packages (setq packages t))
1819 (let (name)
1820 ;; Installed packages:
1821 (dolist (elt package-alist)
1822 (setq name (car elt))
1823 (when (or (eq packages t) (memq name packages))
1824 (mapc function (cdr elt))))
1826 ;; Built-in packages:
1827 (dolist (elt package--builtins)
1828 (setq name (car elt))
1829 (when (and (not (eq name 'emacs)) ; Hide the `emacs' package.
1830 (or package-list-unversioned
1831 (package--bi-desc-version (cdr elt)))
1832 (or (eq packages t) (memq name packages)))
1833 (funcall function (package--from-builtin elt))))
1835 ;; Available and disabled packages:
1836 (dolist (elt package-archive-contents)
1837 (setq name (car elt))
1838 (when (or (eq packages t) (memq name packages))
1839 (dolist (pkg (cdr elt))
1840 ;; Hide obsolete packages.
1841 (unless (package-installed-p (package-desc-name pkg)
1842 (package-desc-version pkg))
1843 (funcall function pkg)))))))
1845 (defun package--has-keyword-p (desc &optional keywords)
1846 "Test if package DESC has any of the given KEYWORDS.
1847 When none are given, the package matches."
1848 (if keywords
1849 (let* ((desc-keywords (and desc (package-desc--keywords desc)))
1850 found)
1851 (dolist (k keywords)
1852 (when (and (not found)
1853 (member k desc-keywords))
1854 (setq found t)))
1855 found)
1858 (defun package-menu--generate (remember-pos packages &optional keywords)
1859 "Populate the Package Menu.
1860 If REMEMBER-POS is non-nil, keep point on the same entry.
1861 PACKAGES should be t, which means to display all known packages,
1862 or a list of package names (symbols) to display.
1864 With KEYWORDS given, only packages with those keywords are
1865 shown."
1866 (package-menu--refresh packages keywords)
1867 (setf (car (aref tabulated-list-format 0))
1868 (if keywords
1869 (let ((filters (mapconcat 'identity keywords ",")))
1870 (concat "Package[" filters "]"))
1871 "Package"))
1872 (if keywords
1873 (define-key package-menu-mode-map "q" 'package-show-package-list)
1874 (define-key package-menu-mode-map "q" 'quit-window))
1875 (tabulated-list-init-header)
1876 (tabulated-list-print remember-pos))
1878 (defun package-menu--print-info (pkg)
1879 "Return a package entry suitable for `tabulated-list-entries'.
1880 PKG has the form (PKG-DESC . STATUS).
1881 Return (PKG-DESC [NAME VERSION STATUS DOC])."
1882 (let* ((pkg-desc (car pkg))
1883 (status (cdr pkg))
1884 (face (pcase status
1885 (`"built-in" 'font-lock-builtin-face)
1886 (`"available" 'default)
1887 (`"new" 'bold)
1888 (`"held" 'font-lock-constant-face)
1889 (`"disabled" 'font-lock-warning-face)
1890 (`"installed" 'font-lock-comment-face)
1891 (`"unsigned" 'font-lock-warning-face)
1892 (_ 'font-lock-warning-face)))) ; obsolete.
1893 (list pkg-desc
1894 `[,(list (symbol-name (package-desc-name pkg-desc))
1895 'face 'link
1896 'follow-link t
1897 'package-desc pkg-desc
1898 'action 'package-menu-describe-package)
1899 ,(propertize (package-version-join
1900 (package-desc-version pkg-desc))
1901 'font-lock-face face)
1902 ,(propertize status 'font-lock-face face)
1903 ,@(if (cdr package-archives)
1904 (list (propertize (or (package-desc-archive pkg-desc) "")
1905 'font-lock-face face)))
1906 ,(propertize (package-desc-summary pkg-desc)
1907 'font-lock-face face)])))
1909 (defun package-menu-refresh ()
1910 "Download the Emacs Lisp package archive.
1911 This fetches the contents of each archive specified in
1912 `package-archives', and then refreshes the package menu."
1913 (interactive)
1914 (unless (derived-mode-p 'package-menu-mode)
1915 (user-error "The current buffer is not a Package Menu"))
1916 (package-refresh-contents)
1917 (package-menu--generate t t))
1919 (defun package-menu-describe-package (&optional button)
1920 "Describe the current package.
1921 If optional arg BUTTON is non-nil, describe its associated package."
1922 (interactive)
1923 (let ((pkg-desc (if button (button-get button 'package-desc)
1924 (tabulated-list-get-id))))
1925 (if pkg-desc
1926 (describe-package pkg-desc)
1927 (user-error "No package here"))))
1929 ;; fixme numeric argument
1930 (defun package-menu-mark-delete (&optional _num)
1931 "Mark a package for deletion and move to the next line."
1932 (interactive "p")
1933 (if (member (package-menu-get-status) '("installed" "obsolete" "unsigned"))
1934 (tabulated-list-put-tag "D" t)
1935 (forward-line)))
1937 (defun package-menu-mark-install (&optional _num)
1938 "Mark a package for installation and move to the next line."
1939 (interactive "p")
1940 (if (member (package-menu-get-status) '("available" "new"))
1941 (tabulated-list-put-tag "I" t)
1942 (forward-line)))
1944 (defun package-menu-mark-unmark (&optional _num)
1945 "Clear any marks on a package and move to the next line."
1946 (interactive "p")
1947 (tabulated-list-put-tag " " t))
1949 (defun package-menu-backup-unmark ()
1950 "Back up one line and clear any marks on that package."
1951 (interactive)
1952 (forward-line -1)
1953 (tabulated-list-put-tag " "))
1955 (defun package-menu-mark-obsolete-for-deletion ()
1956 "Mark all obsolete packages for deletion."
1957 (interactive)
1958 (save-excursion
1959 (goto-char (point-min))
1960 (while (not (eobp))
1961 (if (equal (package-menu-get-status) "obsolete")
1962 (tabulated-list-put-tag "D" t)
1963 (forward-line 1)))))
1965 (defun package-menu-quick-help ()
1966 "Show short key binding help for package-menu-mode."
1967 (interactive)
1968 (message "n-ext, i-nstall, d-elete, u-nmark, x-ecute, r-efresh, h-elp"))
1970 (define-obsolete-function-alias
1971 'package-menu-view-commentary 'package-menu-describe-package "24.1")
1973 (defun package-menu-get-status ()
1974 (let* ((id (tabulated-list-get-id))
1975 (entry (and id (assq id tabulated-list-entries))))
1976 (if entry
1977 (aref (cadr entry) 2)
1978 "")))
1980 (defun package-menu--find-upgrades ()
1981 (let (installed available upgrades)
1982 ;; Build list of installed/available packages in this buffer.
1983 (dolist (entry tabulated-list-entries)
1984 ;; ENTRY is (PKG-DESC [NAME VERSION STATUS DOC])
1985 (let ((pkg-desc (car entry))
1986 (status (aref (cadr entry) 2)))
1987 (cond ((member status '("installed" "unsigned"))
1988 (push pkg-desc installed))
1989 ((member status '("available" "new"))
1990 (push (cons (package-desc-name pkg-desc) pkg-desc)
1991 available)))))
1992 ;; Loop through list of installed packages, finding upgrades.
1993 (dolist (pkg-desc installed)
1994 (let ((avail-pkg (assq (package-desc-name pkg-desc) available)))
1995 (and avail-pkg
1996 (version-list-< (package-desc-version pkg-desc)
1997 (package-desc-version (cdr avail-pkg)))
1998 (push avail-pkg upgrades))))
1999 upgrades))
2001 (defun package-menu-mark-upgrades ()
2002 "Mark all upgradable packages in the Package Menu.
2003 For each installed package with a newer version available, place
2004 an (I)nstall flag on the available version and a (D)elete flag on
2005 the installed version. A subsequent \\[package-menu-execute]
2006 call will upgrade the package."
2007 (interactive)
2008 (unless (derived-mode-p 'package-menu-mode)
2009 (error "The current buffer is not a Package Menu"))
2010 (let ((upgrades (package-menu--find-upgrades)))
2011 (if (null upgrades)
2012 (message "No packages to upgrade.")
2013 (widen)
2014 (save-excursion
2015 (goto-char (point-min))
2016 (while (not (eobp))
2017 (let* ((pkg-desc (tabulated-list-get-id))
2018 (upgrade (cdr (assq (package-desc-name pkg-desc) upgrades))))
2019 (cond ((null upgrade)
2020 (forward-line 1))
2021 ((equal pkg-desc upgrade)
2022 (package-menu-mark-install))
2024 (package-menu-mark-delete))))))
2025 (message "%d package%s marked for upgrading."
2026 (length upgrades)
2027 (if (= (length upgrades) 1) "" "s")))))
2029 (defun package-menu-execute (&optional noquery)
2030 "Perform marked Package Menu actions.
2031 Packages marked for installation are downloaded and installed;
2032 packages marked for deletion are removed.
2033 Optional argument NOQUERY non-nil means do not ask the user to confirm."
2034 (interactive)
2035 (unless (derived-mode-p 'package-menu-mode)
2036 (error "The current buffer is not in Package Menu mode"))
2037 (let (install-list delete-list cmd pkg-desc)
2038 (save-excursion
2039 (goto-char (point-min))
2040 (while (not (eobp))
2041 (setq cmd (char-after))
2042 (unless (eq cmd ?\s)
2043 ;; This is the key PKG-DESC.
2044 (setq pkg-desc (tabulated-list-get-id))
2045 (cond ((eq cmd ?D)
2046 (push pkg-desc delete-list))
2047 ((eq cmd ?I)
2048 (push pkg-desc install-list))))
2049 (forward-line)))
2050 (when install-list
2051 (if (or
2052 noquery
2053 (yes-or-no-p
2054 (if (= (length install-list) 1)
2055 (format "Install package `%s'? "
2056 (package-desc-full-name (car install-list)))
2057 (format "Install these %d packages (%s)? "
2058 (length install-list)
2059 (mapconcat #'package-desc-full-name
2060 install-list ", ")))))
2061 (mapc 'package-install install-list)))
2062 ;; Delete packages, prompting if necessary.
2063 (when delete-list
2064 (if (or
2065 noquery
2066 (yes-or-no-p
2067 (if (= (length delete-list) 1)
2068 (format "Delete package `%s'? "
2069 (package-desc-full-name (car delete-list)))
2070 (format "Delete these %d packages (%s)? "
2071 (length delete-list)
2072 (mapconcat #'package-desc-full-name
2073 delete-list ", ")))))
2074 (dolist (elt delete-list)
2075 (condition-case-unless-debug err
2076 (package-delete elt)
2077 (error (message (cadr err)))))
2078 (error "Aborted")))
2079 (if (or delete-list install-list)
2080 (package-menu--generate t t)
2081 (message "No operations specified."))))
2083 (defun package-menu--version-predicate (A B)
2084 (let ((vA (or (aref (cadr A) 1) '(0)))
2085 (vB (or (aref (cadr B) 1) '(0))))
2086 (if (version-list-= vA vB)
2087 (package-menu--name-predicate A B)
2088 (version-list-< vA vB))))
2090 (defun package-menu--status-predicate (A B)
2091 (let ((sA (aref (cadr A) 2))
2092 (sB (aref (cadr B) 2)))
2093 (cond ((string= sA sB)
2094 (package-menu--name-predicate A B))
2095 ((string= sA "new") t)
2096 ((string= sB "new") nil)
2097 ((string= sA "available") t)
2098 ((string= sB "available") nil)
2099 ((string= sA "installed") t)
2100 ((string= sB "installed") nil)
2101 ((string= sA "unsigned") t)
2102 ((string= sB "unsigned") nil)
2103 ((string= sA "held") t)
2104 ((string= sB "held") nil)
2105 ((string= sA "built-in") t)
2106 ((string= sB "built-in") nil)
2107 ((string= sA "obsolete") t)
2108 ((string= sB "obsolete") nil)
2109 (t (string< sA sB)))))
2111 (defun package-menu--description-predicate (A B)
2112 (let ((dA (aref (cadr A) 3))
2113 (dB (aref (cadr B) 3)))
2114 (if (string= dA dB)
2115 (package-menu--name-predicate A B)
2116 (string< dA dB))))
2118 (defun package-menu--name-predicate (A B)
2119 (string< (symbol-name (package-desc-name (car A)))
2120 (symbol-name (package-desc-name (car B)))))
2122 (defun package-menu--archive-predicate (A B)
2123 (string< (or (package-desc-archive (car A)) "")
2124 (or (package-desc-archive (car B)) "")))
2126 ;;;###autoload
2127 (defun list-packages (&optional no-fetch)
2128 "Display a list of packages.
2129 This first fetches the updated list of packages before
2130 displaying, unless a prefix argument NO-FETCH is specified.
2131 The list is displayed in a buffer named `*Packages*'."
2132 (interactive "P")
2133 (require 'finder-inf nil t)
2134 ;; Initialize the package system if necessary.
2135 (unless package--initialized
2136 (package-initialize t))
2137 (let (old-archives new-packages)
2138 (unless no-fetch
2139 ;; Read the locally-cached archive-contents.
2140 (package-read-all-archive-contents)
2141 (setq old-archives package-archive-contents)
2142 ;; Fetch the remote list of packages.
2143 (package-refresh-contents)
2144 ;; Find which packages are new.
2145 (dolist (elt package-archive-contents)
2146 (unless (assq (car elt) old-archives)
2147 (push (car elt) new-packages))))
2149 ;; Generate the Package Menu.
2150 (let ((buf (get-buffer-create "*Packages*")))
2151 (with-current-buffer buf
2152 (package-menu-mode)
2153 (set (make-local-variable 'package-menu--new-package-list)
2154 new-packages)
2155 (package-menu--generate nil t))
2156 ;; The package menu buffer has keybindings. If the user types
2157 ;; `M-x list-packages', that suggests it should become current.
2158 (switch-to-buffer buf))
2160 (let ((upgrades (package-menu--find-upgrades)))
2161 (if upgrades
2162 (message "%d package%s can be upgraded; type `%s' to mark %s for upgrading."
2163 (length upgrades)
2164 (if (= (length upgrades) 1) "" "s")
2165 (substitute-command-keys "\\[package-menu-mark-upgrades]")
2166 (if (= (length upgrades) 1) "it" "them"))))))
2168 ;;;###autoload
2169 (defalias 'package-list-packages 'list-packages)
2171 ;; Used in finder.el
2172 (defun package-show-package-list (&optional packages keywords)
2173 "Display PACKAGES in a *Packages* buffer.
2174 This is similar to `list-packages', but it does not fetch the
2175 updated list of packages, and it only displays packages with
2176 names in PACKAGES (which should be a list of symbols).
2178 When KEYWORDS are given, only packages with those KEYWORDS are
2179 shown."
2180 (interactive)
2181 (require 'finder-inf nil t)
2182 (let* ((buf (get-buffer-create "*Packages*"))
2183 (win (get-buffer-window buf)))
2184 (with-current-buffer buf
2185 (package-menu-mode)
2186 (package-menu--generate nil packages keywords))
2187 (if win
2188 (select-window win)
2189 (switch-to-buffer buf))))
2191 ;; package-menu--generate rebinds "q" on the fly, so we have to
2192 ;; hard-code the binding in the doc-string here.
2193 (defun package-menu-filter (keyword)
2194 "Filter the *Packages* buffer.
2195 Show only those items that relate to the specified KEYWORD.
2196 To restore the full package list, type `q'."
2197 (interactive (list (completing-read "Keyword: " (package-all-keywords))))
2198 (package-show-package-list t (list keyword)))
2200 (defun package-list-packages-no-fetch ()
2201 "Display a list of packages.
2202 Does not fetch the updated list of packages before displaying.
2203 The list is displayed in a buffer named `*Packages*'."
2204 (interactive)
2205 (list-packages t))
2207 (provide 'package)
2209 ;;; package.el ends here