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