* lisp/emacs-lisp/package.el (package-desc): Simplify.
[bpt/emacs.git] / lisp / emacs-lisp / package.el
CommitLineData
12059709 1;;; package.el --- Simple package system for Emacs -*- lexical-binding:t -*-
44198b6e 2
ab422c4d 3;; Copyright (C) 2007-2013 Free Software Foundation, Inc.
44198b6e
CY
4
5;; Author: Tom Tromey <tromey@redhat.com>
a49ff80c 6;; Daniel Hackney <dan@haxney.org>
44198b6e 7;; Created: 10 Mar 2007
397703b4 8;; Version: 1.0.1
44198b6e 9;; Keywords: tools
469bfed9 10;; Package-Requires: ((tabulated-list "1.0"))
44198b6e
CY
11
12;; This file is part of GNU Emacs.
13
267b82ff 14;; GNU Emacs is free software: you can redistribute it and/or modify
44198b6e 15;; it under the terms of the GNU General Public License as published by
267b82ff
GM
16;; the Free Software Foundation, either version 3 of the License, or
17;; (at your option) any later version.
44198b6e
CY
18
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.
23
24;; You should have received a copy of the GNU General Public License
267b82ff 25;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
44198b6e
CY
26
27;;; Change Log:
28
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
34
35;;; Commentary:
36
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.)
45
44198b6e
CY
46;; A package is described by its name and version. The distribution
47;; format is either a tar file or a single .el file.
48
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.
54
bc44bef7 55;; A .el file is named "NAME-VERSION.el" in the remote archive, but is
44198b6e
CY
56;; installed as simply "NAME.el" in a directory named "NAME-VERSION".
57
bc44bef7
PH
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.
44198b6e
CY
62
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.
66
67;; Conceptually a package has multiple state transitions:
68;;
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.
77
78;; Other external functions you may want to use:
79;;
015eea59 80;; M-x list-packages
44198b6e
CY
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.
86;;
44198b6e
CY
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).
94;;
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.
100
101;;; Thanks:
102;;; (sorted by sort-lines):
103
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>
113
114;;; ToDo:
115
5580f89d
GM
116;; - a trust mechanism, since compiling a package can run arbitrary code.
117;; For example, download package signatures and check that they match.
44198b6e
CY
118;; - putting info dirs at the start of the info path means
119;; users see a weird ordering of categories. OTOH we want to
120;; override later entries. maybe emacs needs to enforce
121;; the standard layout?
122;; - put bytecode in a separate directory tree
123;; - perhaps give users a way to recompile their bytecode
124;; or do it automatically when emacs changes
125;; - give users a way to know whether a package is installed ok
126;; - give users a way to view a package's documentation when it
127;; only appears in the .el
128;; - use/extend checkdoc so people can tell if their package will work
129;; - "installed" instead of a blank in the status column
130;; - tramp needs its files to be compiled in a certain order.
131;; how to handle this? fix tramp?
132;; - on emacs 21 we don't kill the -autoloads.el buffer. what about 22?
133;; - maybe we need separate .elc directories for various emacs versions
134;; and also emacs-vs-xemacs. That way conditional compilation can
135;; work. But would this break anything?
136;; - should store the package's keywords in archive-contents, then
137;; let the users filter the package-menu by keyword. See
138;; finder-by-keyword. (We could also let people view the
139;; Commentary, but it isn't clear how useful this is.)
140;; - William Xu suggests being able to open a package file without
141;; installing it
142;; - Interface with desktop.el so that restarting after an install
143;; works properly
44198b6e
CY
144;; - Use hierarchical layout. PKG/etc PKG/lisp PKG/info
145;; ... except maybe lisp?
146;; - It may be nice to have a macro that expands to the package's
147;; private data dir, aka ".../etc". Or, maybe data-directory
148;; needs to be a list (though this would be less nice)
149;; a few packages want this, eg sokoban
150;; - package menu needs:
151;; ability to know which packages are built-in & thus not deletable
152;; it can sometimes print odd results, like 0.3 available but 0.4 active
153;; why is that?
154;; - Allow multiple versions on the server...?
155;; [ why bother? ]
156;; - Don't install a package which will invalidate dependencies overall
157;; - Allow something like (or (>= emacs 21.0) (>= xemacs 21.5))
158;; [ currently thinking, why bother.. KISS ]
159;; - Allow optional package dependencies
160;; then if we require 'bbdb', bbdb-specific lisp in lisp/bbdb
161;; and just don't compile to add to load path ...?
44198b6e 162;; - Our treatment of the info path is somewhat bogus
44198b6e
CY
163
164;;; Code:
165
f56be016
SM
166(eval-when-compile (require 'cl-lib))
167
e91a96fe
CY
168(require 'tabulated-list)
169
44198b6e
CY
170(defgroup package nil
171 "Manager for Emacs Lisp packages."
172 :group 'applications
173 :version "24.1")
174
175;;;###autoload
176(defcustom package-enable-at-startup t
177 "Whether to activate installed packages when Emacs starts.
178If non-nil, packages are activated after reading the init file
179and before `after-init-hook'. Activation is not done if
180`user-init-file' is nil (e.g. Emacs was started with \"-q\").
181
182Even if the value is nil, you can type \\[package-initialize] to
183activate the package system at any time."
184 :type 'boolean
185 :group 'package
186 :version "24.1")
187
188(defcustom package-load-list '(all)
189 "List of packages for `package-initialize' to load.
190Each element in this list should be a list (NAME VERSION), or the
191symbol `all'. The symbol `all' says to load the latest installed
192versions of all packages not specified by other elements.
193
194For an element (NAME VERSION), NAME is a package name (a symbol).
195VERSION should be t, a string, or nil.
a7d2d465 196If VERSION is t, the most recent version is activated.
44198b6e
CY
197If 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.
200If VERSION is nil, the package is not loaded (it is \"disabled\")."
201 :type '(repeat symbol)
bc44bef7 202 :risky t
44198b6e
CY
203 :group 'package
204 :version "24.1")
205
206(defvar Info-directory-list)
44198b6e
CY
207(declare-function info-initialize "info" ())
208(declare-function url-http-parse-response "url-http" ())
209(declare-function lm-header "lisp-mnt" (header))
210(declare-function lm-commentary "lisp-mnt" (&optional file))
cb6c4991 211(defvar url-http-end-of-headers)
44198b6e 212
bc44bef7
PH
213(defcustom package-archives '(("gnu" . "http://elpa.gnu.org/packages/"))
214 "An alist of archives from which to fetch.
215The default value points to the GNU Emacs package repository.
f561e49a
CY
216
217Each element has the form (ID . LOCATION).
218 ID is an archive name, as a string.
219 LOCATION specifies the base location for the archive.
220 If it starts with \"http:\", it is treated as a HTTP URL;
221 otherwise it should be an absolute directory name.
5580f89d
GM
222 (Other types of URL are currently not supported.)
223
224Only add locations that you trust, since fetching and installing
225a package can run arbitrary code."
bc44bef7 226 :type '(alist :key-type (string :tag "Archive name")
f561e49a 227 :value-type (string :tag "URL or directory name"))
bc44bef7
PH
228 :risky t
229 :group 'package
230 :version "24.1")
44198b6e 231
397703b4
YH
232(defcustom package-pinned-packages nil
233 "An alist of packages that are pinned to a specific archive
234
235Each element has the form (SYM . ID).
236 SYM is a package, as a symbol.
5b165ade 237 ID is an archive name. This should correspond to an
397703b4
YH
238 entry in `package-archives'.
239
240If the archive of name ID does not contain the package SYM, no
241other location will be considered, which will make the
242package unavailable."
243 :type '(alist :key-type (symbol :tag "Package")
244 :value-type (string :tag "Archive name"))
245 :risky t
246 :group 'package
247 :version "24.4")
248
44198b6e
CY
249(defconst package-archive-version 1
250 "Version number of the package archive understood by this file.
251Lower version numbers than this will probably be understood as well.")
252
44198b6e
CY
253;; We don't prime the cache since it tends to get out of date.
254(defvar package-archive-contents nil
255 "Cache of the contents of the Emacs Lisp Package Archive.
f56be016 256This is an alist mapping package names (symbols) to
12059709 257non-empty lists of `package-desc' structures.")
bc44bef7 258(put 'package-archive-contents 'risky-local-variable t)
44198b6e
CY
259
260(defcustom package-user-dir (locate-user-emacs-file "elpa")
261 "Directory containing the user's Emacs Lisp packages.
262The directory name should be absolute.
263Apart from this directory, Emacs also looks for system-wide
264packages in `package-directory-list'."
265 :type 'directory
bc44bef7 266 :risky t
44198b6e
CY
267 :group 'package
268 :version "24.1")
269
270(defcustom package-directory-list
271 ;; Defaults are subdirs named "elpa" in the site-lisp dirs.
272 (let (result)
273 (dolist (f load-path)
0be01d2c
CY
274 (and (stringp f)
275 (equal (file-name-nondirectory f) "site-lisp")
276 (push (expand-file-name "elpa" f) result)))
44198b6e
CY
277 (nreverse result))
278 "List of additional directories containing Emacs Lisp packages.
279Each directory name should be absolute.
280
281These directories contain packages intended for system-wide; in
282contrast, `package-user-dir' contains packages for personal use."
283 :type '(repeat directory)
bc44bef7 284 :risky t
44198b6e
CY
285 :group 'package
286 :version "24.1")
287
f56be016
SM
288(defvar package--default-summary "No description available.")
289
290(cl-defstruct (package-desc
291 ;; Rename the default constructor from `make-package-desc'.
292 (:constructor package-desc-create)
293 ;; Has the same interface as the old `define-package',
294 ;; which is still used in the "foo-pkg.el" files. Extra
295 ;; options can be supported by adding additional keys.
296 (:constructor
297 package-desc-from-define
298 (name-string version-string &optional summary requirements
056453c6 299 &rest rest-plist
f56be016
SM
300 &aux
301 (name (intern name-string))
302 (version (version-to-list version-string))
303 (reqs (mapcar #'(lambda (elt)
304 (list (car elt)
305 (version-to-list (cadr elt))))
306 (if (eq 'quote (car requirements))
307 (nth 1 requirements)
056453c6
DG
308 requirements)))
309 (kind (plist-get rest-plist :kind))
310 (archive (plist-get rest-plist :archive))
311 (extras (let (alist)
056453c6 312 (while rest-plist
abd1ae34
SM
313 (unless (memq (car rest-plist) '(:kind :archive))
314 (let ((value (cadr rest-plist)))
315 (when value
316 (push (cons (car rest-plist) value)
317 alist))))
056453c6
DG
318 (setq rest-plist (cddr rest-plist)))
319 alist)))))
f56be016 320 "Structure containing information about an individual package.
f56be016
SM
321Slots:
322
12059709 323`name' Name of the package, as a symbol.
f56be016
SM
324
325`version' Version of the package, as a version list.
326
327`summary' Short description of the package, typically taken from
12059709 328 the first line of the file.
f56be016 329
12059709
SM
330`reqs' Requirements of the package. A list of (PACKAGE
331 VERSION-LIST) naming the dependent package and the minimum
332 required version.
f56be016 333
12059709
SM
334`kind' The distribution format of the package. Currently, it is
335 either `single' or `tar'.
f56be016
SM
336
337`archive' The name of the archive (as a string) whence this
12059709 338 package came.
1b8dff23 339
12059709 340`dir' The directory where the package is installed (if installed),
056453c6
DG
341 `builtin' if it is built-in, or nil otherwise.
342
343`extras' Optional alist of additional keyword-value pairs."
f56be016
SM
344 name
345 version
346 (summary package--default-summary)
347 reqs
348 kind
1b8dff23 349 archive
056453c6
DG
350 dir
351 extras)
1b8dff23
SM
352
353;; Pseudo fields.
fd846ab4 354(defun package-desc-full-name (pkg-desc)
1b8dff23
SM
355 (format "%s-%s"
356 (package-desc-name pkg-desc)
357 (package-version-join (package-desc-version pkg-desc))))
f56be016 358
fd846ab4
SM
359(defun package-desc-suffix (pkg-desc)
360 (pcase (package-desc-kind pkg-desc)
361 (`single ".el")
362 (`tar ".tar")
363 (kind (error "Unknown package kind: %s" kind))))
364
f56be016
SM
365;; Package descriptor format used in finder-inf.el and package--builtins.
366(cl-defstruct (package--bi-desc
367 (:constructor package-make-builtin (version summary))
368 (:type vector))
369 version
370 reqs
371 summary)
372
96ae4c8f
CY
373(defvar package--builtins nil
374 "Alist of built-in packages.
015eea59
CY
375The actual value is initialized by loading the library
376`finder-inf'; this is not done until it is needed, e.g. by the
377function `package-built-in-p'.
378
f56be016
SM
379Each element has the form (PKG . PACKAGE-BI-DESC), where PKG is a package
380name (a symbol) and DESC is a `package--bi-desc' structure.")
bc44bef7 381(put 'package--builtins 'risky-local-variable t)
44198b6e 382
96ae4c8f 383(defvar package-alist nil
44198b6e 384 "Alist of all packages available for activation.
a7d2d465
SM
385Each element has the form (PKG . DESCS), where PKG is a package
386name (a symbol) and DESCS is a non-empty list of `package-desc' structure,
387sorted by decreasing versions.
96ae4c8f
CY
388
389This variable is set automatically by `package-load-descriptor',
390called via `package-initialize'. To change which packages are
391loaded and/or activated, customize `package-load-list'.")
25322144 392(put 'package-alist 'risky-local-variable t)
44198b6e 393
96ae4c8f 394(defvar package-activated-list nil
66bd25ab 395 ;; FIXME: This should implicitly include all builtin packages.
44198b6e 396 "List of the names of currently activated packages.")
bc44bef7 397(put 'package-activated-list 'risky-local-variable t)
44198b6e 398
ba08b241
CY
399(defun package-version-join (vlist)
400 "Return the version string corresponding to the list VLIST.
401This is, approximately, the inverse of `version-to-list'.
402\(Actually, it returns only one of the possible inverses, since
403`version-to-list' is a many-to-one operation.)"
404 (if (null vlist)
405 ""
406 (let ((str-list (list "." (int-to-string (car vlist)))))
407 (dolist (num (cdr vlist))
408 (cond
409 ((>= num 0)
410 (push (int-to-string num) str-list)
411 (push "." str-list))
412 ((< num -3)
413 (error "Invalid version list `%s'" vlist))
414 (t
415 ;; pre, or beta, or alpha
416 (cond ((equal "." (car str-list))
417 (pop str-list))
418 ((not (string-match "[0-9]+" (car str-list)))
419 (error "Invalid version list `%s'" vlist)))
420 (push (cond ((= num -1) "pre")
421 ((= num -2) "beta")
422 ((= num -3) "alpha"))
423 str-list))))
424 (if (equal "." (car str-list))
425 (pop str-list))
426 (apply 'concat (nreverse str-list)))))
44198b6e 427
1b8dff23
SM
428(defun package-load-descriptor (pkg-dir)
429 "Load the description file in directory PKG-DIR."
430 (let ((pkg-file (expand-file-name (package--description-file pkg-dir)
431 pkg-dir)))
432 (when (file-exists-p pkg-file)
433 (with-temp-buffer
434 (insert-file-contents pkg-file)
1b8dff23 435 (goto-char (point-min))
b5bcaee5
DG
436 (let ((pkg-desc (package-process-define-package
437 (read (current-buffer)) pkg-file)))
fd846ab4
SM
438 (setf (package-desc-dir pkg-desc) pkg-dir)
439 pkg-desc)))))
44198b6e
CY
440
441(defun package-load-all-descriptors ()
442 "Load descriptors for installed Emacs Lisp packages.
443This looks for package subdirectories in `package-user-dir' and
444`package-directory-list'. The variable `package-load-list'
445controls which package subdirectories may be loaded.
446
447In each valid package subdirectory, this function loads the
448description file containing a call to `define-package', which
a7d2d465 449updates `package-alist'."
1b8dff23
SM
450 (dolist (dir (cons package-user-dir package-directory-list))
451 (when (file-directory-p dir)
452 (dolist (subdir (directory-files dir))
453 (let ((pkg-dir (expand-file-name subdir dir)))
454 (when (file-directory-p pkg-dir)
455 (package-load-descriptor pkg-dir)))))))
456
457(defun package-disabled-p (pkg-name version)
458 "Return whether PKG-NAME at VERSION can be activated.
459The decision is made according to `package-load-list'.
460Return nil if the package can be activated.
461Return t if the package is completely disabled.
462Return the max version (as a string) if the package is held at a lower version."
463 (let ((force (assq pkg-name package-load-list)))
464 (cond ((null force) (not (memq 'all package-load-list)))
465 ((null (setq force (cadr force))) t) ; disabled
466 ((eq force t) nil)
467 ((stringp force) ; held
468 (unless (version-list-= version (version-to-list force))
469 force))
470 (t (error "Invalid element in `package-load-list'")))))
cced7584 471
f56be016
SM
472(defun package-activate-1 (pkg-desc)
473 (let* ((name (package-desc-name pkg-desc))
2d69b99e
SM
474 (pkg-dir (package-desc-dir pkg-desc))
475 (pkg-dir-dir (file-name-as-directory pkg-dir)))
44198b6e 476 (unless pkg-dir
1b8dff23
SM
477 (error "Internal error: unable to find directory for `%s'"
478 (package-desc-full-name pkg-desc)))
2d69b99e
SM
479 ;; Add to load path, add autoloads, and activate the package.
480 (let ((old-lp load-path))
14b511d4
SM
481 (with-demoted-errors
482 (load (expand-file-name (format "%s-autoloads" name) pkg-dir) nil t))
2d69b99e
SM
483 (when (and (eq old-lp load-path)
484 (not (or (member pkg-dir load-path)
485 (member pkg-dir-dir load-path))))
486 ;; Old packages don't add themselves to the `load-path', so we have to
487 ;; do it ourselves.
488 (push pkg-dir load-path)))
cced7584 489 ;; Add info node.
ebf662f4
CY
490 (when (file-exists-p (expand-file-name "dir" pkg-dir))
491 ;; FIXME: not the friendliest, but simple.
492 (require 'info)
493 (info-initialize)
494 (push pkg-dir Info-directory-list))
f56be016 495 (push name package-activated-list)
44198b6e
CY
496 ;; Don't return nil.
497 t))
498
7ede3b65
CY
499(defun package-built-in-p (package &optional min-version)
500 "Return true if PACKAGE is built-in to Emacs.
501Optional arg MIN-VERSION, if non-nil, should be a version list
502specifying the minimum acceptable version."
66bd25ab
SM
503 (let ((bi (assq package package--builtin-versions)))
504 (cond
505 (bi (version-list-<= min-version (cdr bi)))
506 (min-version nil)
507 (t
508 (require 'finder-inf nil t) ; For `package--builtins'.
509 (assq package package--builtins)))))
f56be016
SM
510
511(defun package--from-builtin (bi-desc)
512 (package-desc-create :name (pop bi-desc)
513 :version (package--bi-desc-version bi-desc)
12059709
SM
514 :summary (package--bi-desc-summary bi-desc)
515 :dir 'builtin))
44198b6e 516
4b99edf2
CY
517;; This function goes ahead and activates a newer version of a package
518;; if an older one was already activated. This is not ideal; we'd at
519;; least need to check to see if the package has actually been loaded,
520;; and not merely activated.
a7d2d465
SM
521(defun package-activate (package &optional force)
522 "Activate package PACKAGE.
523If FORCE is true, (re-)activate it if it's already activated."
524 (let ((pkg-descs (cdr (assq package package-alist))))
4b99edf2 525 ;; Check if PACKAGE is available in `package-alist'.
a7d2d465
SM
526 (while
527 (when pkg-descs
528 (let ((available-version (package-desc-version (car pkg-descs))))
529 (or (package-disabled-p package available-version)
530 ;; Prefer a builtin package.
531 (package-built-in-p package available-version))))
532 (setq pkg-descs (cdr pkg-descs)))
4b99edf2
CY
533 (cond
534 ;; If no such package is found, maybe it's built-in.
a7d2d465
SM
535 ((null pkg-descs)
536 (package-built-in-p package))
4b99edf2 537 ;; If the package is already activated, just return t.
a7d2d465 538 ((and (memq package package-activated-list) (not force))
4b99edf2
CY
539 t)
540 ;; Otherwise, proceed with activation.
541 (t
a7d2d465
SM
542 (let* ((pkg-vec (car pkg-descs))
543 (fail (catch 'dep-failure
544 ;; Activate its dependencies recursively.
545 (dolist (req (package-desc-reqs pkg-vec))
546 (unless (package-activate (car req) (cadr req))
547 (throw 'dep-failure req))))))
4b99edf2
CY
548 (if fail
549 (warn "Unable to activate package `%s'.
015eea59 550Required package `%s-%s' is unavailable"
4b99edf2
CY
551 package (car fail) (package-version-join (cadr fail)))
552 ;; If all goes well, activate the package itself.
f56be016 553 (package-activate-1 pkg-vec)))))))
44198b6e 554
fd846ab4
SM
555(defun define-package (_name-string _version-string
556 &optional _docstring _requirements
557 &rest _extra-properties)
44198b6e 558 "Define a new package.
4b99edf2 559NAME-STRING is the name of the package, as a string.
7ede3b65 560VERSION-STRING is the version of the package, as a string.
4b99edf2
CY
561DOCSTRING is a short description of the package, a string.
562REQUIREMENTS is a list of dependencies on other packages.
7ede3b65
CY
563 Each requirement is of the form (OTHER-PACKAGE OTHER-VERSION),
564 where OTHER-VERSION is a string.
187d3296
CY
565
566EXTRA-PROPERTIES is currently unused."
1b8dff23
SM
567 ;; FIXME: Placeholder! Should we keep it?
568 (error "Don't call me!"))
569
570(defun package-process-define-package (exp origin)
571 (unless (eq (car-safe exp) 'define-package)
572 (error "Can't find define-package in %s" origin))
573 (let* ((new-pkg-desc (apply #'package-desc-from-define (cdr exp)))
574 (name (package-desc-name new-pkg-desc))
575 (version (package-desc-version new-pkg-desc))
a7d2d465
SM
576 (old-pkgs (assq name package-alist)))
577 (if (null old-pkgs)
578 ;; If there's no old package, just add this to `package-alist'.
579 (push (list name new-pkg-desc) package-alist)
580 ;; If there is, insert the new package at the right place in the list.
5b165ade
SM
581 (while
582 (if (and (cdr old-pkgs)
583 (version-list-< version
584 (package-desc-version (cadr old-pkgs))))
585 (setq old-pkgs (cdr old-pkgs))
586 (push new-pkg-desc (cdr old-pkgs))
587 nil)))
1b8dff23 588 new-pkg-desc))
44198b6e 589
fd846ab4 590;; From Emacs 22, but changed so it adds to load-path.
44198b6e
CY
591(defun package-autoload-ensure-default-file (file)
592 "Make sure that the autoload file FILE exists and if not create it."
593 (unless (file-exists-p file)
594 (write-region
595 (concat ";;; " (file-name-nondirectory file)
596 " --- automatically extracted autoloads\n"
597 ";;\n"
4fac34ce
SM
598 ";;; Code:\n"
599 "(add-to-list 'load-path (or (file-name-directory #$) (car load-path)))\n"
44198b6e
CY
600 "\f\n;; Local Variables:\n"
601 ";; version-control: never\n"
602 ";; no-byte-compile: t\n"
603 ";; no-update-autoloads: t\n"
604 ";; End:\n"
605 ";;; " (file-name-nondirectory file)
606 " ends here\n")
607 nil file))
608 file)
609
12059709
SM
610(defvar generated-autoload-file)
611(defvar version-control)
612
44198b6e 613(defun package-generate-autoloads (name pkg-dir)
f56be016 614 (let* ((auto-name (format "%s-autoloads.el" name))
4d6769e1 615 ;;(ignore-name (concat name "-pkg.el"))
44198b6e
CY
616 (generated-autoload-file (expand-file-name auto-name pkg-dir))
617 (version-control 'never))
4fac34ce 618 (package-autoload-ensure-default-file generated-autoload-file)
cd44022c
DH
619 (update-directory-autoloads pkg-dir)
620 (let ((buf (find-buffer-visiting generated-autoload-file)))
f56be016
SM
621 (when buf (kill-buffer buf)))
622 auto-name))
44198b6e 623
4525ce3e
CY
624(defvar tar-parse-info)
625(declare-function tar-untar-buffer "tar-mode" ())
f20def1f
GM
626(declare-function tar-header-name "tar-mode" (tar-header) t)
627(declare-function tar-header-link-type "tar-mode" (tar-header) t)
4525ce3e
CY
628
629(defun package-untar-buffer (dir)
44198b6e 630 "Untar the current buffer.
4525ce3e
CY
631This uses `tar-untar-buffer' from Tar mode. All files should
632untar into a directory named DIR; otherwise, signal an error."
44198b6e 633 (require 'tar-mode)
4525ce3e
CY
634 (tar-mode)
635 ;; Make sure everything extracts into DIR.
3c94d7a6
CY
636 (let ((regexp (concat "\\`" (regexp-quote (expand-file-name dir)) "/"))
637 (case-fold-search (memq system-type '(windows-nt ms-dos cygwin))))
4525ce3e 638 (dolist (tar-data tar-parse-info)
3c94d7a6
CY
639 (let ((name (expand-file-name (tar-header-name tar-data))))
640 (or (string-match regexp name)
641 ;; Tarballs created by some utilities don't list
642 ;; directories with a trailing slash (Bug#13136).
643 (and (string-equal dir name)
644 (eq (tar-header-link-type tar-data) 5))
645 (error "Package does not untar cleanly into directory %s/" dir)))))
4525ce3e 646 (tar-untar-buffer))
44198b6e 647
fd846ab4
SM
648(defun package-generate-description-file (pkg-desc pkg-dir)
649 "Create the foo-pkg.el file for single-file packages."
650 (let* ((name (package-desc-name pkg-desc))
651 (pkg-file (expand-file-name (package--description-file pkg-dir)
652 pkg-dir)))
653 (let ((print-level nil)
654 (print-quoted t)
655 (print-length nil))
656 (write-region
657 (concat
658 (prin1-to-string
056453c6
DG
659 (nconc
660 (list 'define-package
661 (symbol-name name)
662 (package-version-join (package-desc-version pkg-desc))
663 (package-desc-summary pkg-desc)
664 (let ((requires (package-desc-reqs pkg-desc)))
665 (list 'quote
666 ;; Turn version lists into string form.
667 (mapcar
668 (lambda (elt)
669 (list (car elt)
670 (package-version-join (cadr elt))))
671 requires))))
672 (package--alist-to-plist
673 (package-desc-extras pkg-desc))))
fd846ab4
SM
674 "\n")
675 nil
676 pkg-file))))
677
056453c6
DG
678(defun package--alist-to-plist (alist)
679 (apply #'nconc (mapcar (lambda (pair) (list (car pair) (cdr pair))) alist)))
680
fd846ab4
SM
681(defun package-unpack (pkg-desc)
682 "Install the contents of the current buffer as a package."
683 (let* ((name (package-desc-name pkg-desc))
684 (dirname (package-desc-full-name pkg-desc))
4525ce3e 685 (pkg-dir (expand-file-name dirname package-user-dir)))
fd846ab4
SM
686 (pcase (package-desc-kind pkg-desc)
687 (`tar
688 (make-directory package-user-dir t)
689 ;; FIXME: should we delete PKG-DIR if it exists?
690 (let* ((default-directory (file-name-as-directory package-user-dir)))
691 (package-untar-buffer dirname)))
692 (`single
693 (let ((el-file (expand-file-name (format "%s.el" name) pkg-dir)))
694 (make-directory pkg-dir t)
695 (package--write-file-no-coding el-file)))
696 (kind (error "Unknown package kind: %S" kind)))
697 (package--make-autoloads-and-stuff pkg-desc pkg-dir)
698 ;; Update package-alist.
699 (let ((new-desc (package-load-descriptor pkg-dir)))
700 ;; FIXME: Check that `new-desc' matches `desc'!
701 ;; FIXME: Compilation should be done as a separate, optional, step.
702 ;; E.g. for multi-package installs, we should first install all packages
703 ;; and then compile them.
704 (package--compile new-desc))
705 ;; Try to activate it.
a7d2d465 706 (package-activate name 'force)
fd846ab4
SM
707 pkg-dir))
708
709(defun package--make-autoloads-and-stuff (pkg-desc pkg-dir)
710 "Generate autoloads, description file, etc.. for PKG-DESC installed at PKG-DIR."
711 (package-generate-autoloads (package-desc-name pkg-desc) pkg-dir)
712 (let ((desc-file (package--description-file pkg-dir)))
713 (unless (file-exists-p desc-file)
714 (package-generate-description-file pkg-desc pkg-dir)))
715 ;; FIXME: Create foo.info and dir file from foo.texi?
716 )
717
718(defun package--compile (pkg-desc)
719 "Byte-compile installed package PKG-DESC."
720 (package-activate-1 pkg-desc)
721 (byte-recompile-directory (package-desc-dir pkg-desc) 0 t))
44198b6e 722
c6affbde 723(defun package--write-file-no-coding (file-name)
bc44bef7 724 (let ((buffer-file-coding-system 'no-conversion))
c6affbde 725 (write-region (point-min) (point-max) file-name)))
bc44bef7 726
f561e49a
CY
727(defmacro package--with-work-buffer (location file &rest body)
728 "Run BODY in a buffer containing the contents of FILE at LOCATION.
729LOCATION is the base location of a package archive, and should be
730one of the URLs (or file names) specified in `package-archives'.
731FILE is the name of a file relative to that base location.
732
733This macro retrieves FILE from LOCATION into a temporary buffer,
734and evaluates BODY while that buffer is current. This work
735buffer is killed afterwards. Return the last value in BODY."
fd846ab4 736 (declare (indent 2) (debug t))
da91b5f2 737 `(let* ((http (string-match "\\`https?:" ,location))
f561e49a
CY
738 (buffer
739 (if http
740 (url-retrieve-synchronously (concat ,location ,file))
741 (generate-new-buffer "*package work buffer*"))))
742 (prog1
743 (with-current-buffer buffer
744 (if http
745 (progn (package-handle-response)
746 (re-search-forward "^$" nil 'move)
747 (forward-char)
748 (delete-region (point-min) (point)))
749 (unless (file-name-absolute-p ,location)
750 (error "Archive location %s is not an absolute file name"
751 ,location))
752 (insert-file-contents (expand-file-name ,file ,location)))
753 ,@body)
754 (kill-buffer buffer))))
755
44198b6e 756(defun package-handle-response ()
f561e49a 757 "Handle the response from a `url-retrieve-synchronously' call.
44198b6e
CY
758Parse the HTTP response and throw if an error occurred.
759The url package seems to require extra processing for this.
760This should be called in a `save-excursion', in the download buffer.
761It will move point to somewhere in the headers."
762 ;; We assume HTTP here.
763 (require 'url-http)
764 (let ((response (url-http-parse-response)))
765 (when (or (< response 200) (>= response 300))
44198b6e 766 (error "Error during download request:%s"
66bd25ab 767 (buffer-substring-no-properties (point) (line-end-position))))))
44198b6e 768
fd846ab4 769(defun package-install-from-archive (pkg-desc)
44198b6e 770 "Download and install a tar package."
fd846ab4
SM
771 (let ((location (package-archive-base pkg-desc))
772 (file (concat (package-desc-full-name pkg-desc)
773 (package-desc-suffix pkg-desc))))
f561e49a 774 (package--with-work-buffer location file
fd846ab4 775 (package-unpack pkg-desc))))
44198b6e 776
1ead7dbd
SM
777(defvar package--initialized nil)
778
bc44bef7 779(defun package-installed-p (package &optional min-version)
7ede3b65
CY
780 "Return true if PACKAGE, of MIN-VERSION or newer, is installed.
781MIN-VERSION should be a version list."
a7723be6 782 (unless package--initialized (error "package.el is not yet initialized!"))
a7d2d465
SM
783 (or
784 (let ((pkg-descs (cdr (assq package package-alist))))
785 (and pkg-descs
786 (version-list-<= min-version
787 (package-desc-version (car pkg-descs)))))
788 ;; Also check built-in packages.
789 (package-built-in-p package min-version)))
44198b6e 790
12059709
SM
791(defun package-compute-transaction (packages requirements)
792 "Return a list of packages to be installed, including PACKAGES.
793PACKAGES should be a list of `package-desc'.
96ae4c8f
CY
794
795REQUIREMENTS should be a list of additional requirements; each
7ede3b65
CY
796element in this list should have the form (PACKAGE VERSION-LIST),
797where PACKAGE is a package name and VERSION-LIST is the required
798version of that package.
96ae4c8f
CY
799
800This function recursively computes the requirements of the
801packages in REQUIREMENTS, and returns a list of all the packages
802that must be installed. Packages that are already installed are
803not included in this list."
12059709
SM
804 ;; FIXME: We really should use backtracking to explore the whole
805 ;; search space (e.g. if foo require bar-1.3, and bar-1.4 requires toto-1.1
806 ;; whereas bar-1.3 requires toto-1.0 and the user has put a hold on toto-1.0:
807 ;; the current code might fail to see that it could install foo by using the
808 ;; older bar-1.3).
44198b6e
CY
809 (dolist (elt requirements)
810 (let* ((next-pkg (car elt))
12059709
SM
811 (next-version (cadr elt))
812 (already ()))
813 (dolist (pkg packages)
814 (if (eq next-pkg (package-desc-name pkg))
815 (setq already pkg)))
816 (cond
817 (already
818 (if (version-list-< next-version (package-desc-version already))
819 ;; Move to front, so it gets installed early enough (bug#14082).
820 (setq packages (cons already (delq already packages)))
821 (error "Need package `%s-%s', but only %s is available"
822 next-pkg (package-version-join next-version)
823 (package-version-join (package-desc-version already)))))
824
825 ((package-installed-p next-pkg next-version) nil)
826
827 (t
44198b6e
CY
828 ;; A package is required, but not installed. It might also be
829 ;; blocked via `package-load-list'.
12059709
SM
830 (let ((pkg-descs (cdr (assq next-pkg package-archive-contents)))
831 (found nil)
832 (problem nil))
833 (while (and pkg-descs (not found))
834 (let* ((pkg-desc (pop pkg-descs))
835 (version (package-desc-version pkg-desc))
836 (disabled (package-disabled-p next-pkg version)))
837 (cond
838 ((version-list-< version next-version)
839 (error
840 "Need package `%s-%s', but only %s is available"
841 next-pkg (package-version-join next-version)
842 (package-version-join version)))
843 (disabled
844 (unless problem
845 (setq problem
846 (if (stringp disabled)
847 (format "Package `%s' held at version %s, \
44198b6e 848but version %s required"
12059709
SM
849 next-pkg disabled
850 (package-version-join next-version))
851 (format "Required package '%s' is disabled"
852 next-pkg)))))
853 (t (setq found pkg-desc)))))
854 (unless found
855 (if problem
856 (error problem)
857 (error "Package `%s-%s' is unavailable"
858 next-pkg (package-version-join next-version))))
859 (setq packages
860 (package-compute-transaction (cons found packages)
861 (package-desc-reqs found))))))))
862 packages)
44198b6e
CY
863
864(defun package-read-from-string (str)
865 "Read a Lisp expression from STR.
866Signal an error if the entire string was not used."
867 (let* ((read-data (read-from-string str))
187d3296
CY
868 (more-left
869 (condition-case nil
870 ;; The call to `ignore' suppresses a compiler warning.
871 (progn (ignore (read-from-string
872 (substring str (cdr read-data))))
873 t)
874 (end-of-file nil))))
44198b6e
CY
875 (if more-left
876 (error "Can't read whole string")
877 (car read-data))))
878
879(defun package--read-archive-file (file)
880 "Re-read archive file FILE, if it exists.
881Will return the data from the file, or nil if the file does not exist.
882Will throw an error if the archive version is too new."
883 (let ((filename (expand-file-name file package-user-dir)))
187d3296
CY
884 (when (file-exists-p filename)
885 (with-temp-buffer
886 (insert-file-contents-literally filename)
887 (let ((contents (read (current-buffer))))
888 (if (> (car contents) package-archive-version)
889 (error "Package archive version %d is higher than %d"
890 (car contents) package-archive-version))
891 (cdr contents))))))
44198b6e 892
bc44bef7 893(defun package-read-all-archive-contents ()
96ae4c8f
CY
894 "Re-read `archive-contents', if it exists.
895If successful, set `package-archive-contents'."
fbe3be3f 896 (setq package-archive-contents nil)
bc44bef7 897 (dolist (archive package-archives)
96ae4c8f 898 (package-read-archive-contents (car archive))))
44198b6e 899
bc44bef7 900(defun package-read-archive-contents (archive)
187d3296
CY
901 "Re-read archive contents for ARCHIVE.
902If successful, set the variable `package-archive-contents'.
bc44bef7 903If the archive version is too new, signal an error."
187d3296
CY
904 ;; Version 1 of 'archive-contents' is identical to our internal
905 ;; representation.
66bd25ab
SM
906 (let* ((contents-file (format "archives/%s/archive-contents" archive))
907 (contents (package--read-archive-file contents-file)))
908 (when contents
187d3296
CY
909 (dolist (package contents)
910 (package--add-to-archive-contents package archive)))))
bc44bef7 911
f56be016
SM
912;; Package descriptor objects used inside the "archive-contents" file.
913;; Changing this defstruct implies changing the format of the
914;; "archive-contents" files.
915(cl-defstruct (package--ac-desc
056453c6 916 (:constructor package-make-ac-desc (version reqs summary kind extras))
f56be016
SM
917 (:copier nil)
918 (:type vector))
056453c6 919 version reqs summary kind extras)
f56be016 920
bc44bef7
PH
921(defun package--add-to-archive-contents (package archive)
922 "Add the PACKAGE from the given ARCHIVE if necessary.
f56be016
SM
923PACKAGE should have the form (NAME . PACKAGE--AC-DESC).
924Also, add the originating archive to the `package-desc' structure."
925 (let* ((name (car package))
66bd25ab 926 (version (package--ac-desc-version (cdr package)))
f56be016
SM
927 (pkg-desc
928 (package-desc-create
929 :name name
66bd25ab 930 :version version
f56be016
SM
931 :reqs (package--ac-desc-reqs (cdr package))
932 :summary (package--ac-desc-summary (cdr package))
933 :kind (package--ac-desc-kind (cdr package))
056453c6
DG
934 :archive archive
935 :extras (and (> (length (cdr package)) 4)
936 ;; Older archive-contents files have only 4
937 ;; elements here.
938 (package--ac-desc-extras (cdr package)))))
12059709 939 (existing-packages (assq name package-archive-contents))
397703b4 940 (pinned-to-archive (assoc name package-pinned-packages)))
66bd25ab 941 (cond
12059709 942 ;; Skip entirely if pinned to another archive or already installed.
66bd25ab
SM
943 ((or (and pinned-to-archive
944 (not (equal (cdr pinned-to-archive) archive)))
945 (let ((bi (assq name package--builtin-versions)))
12059709 946 (and bi (version-list-= version (cdr bi))))
66bd25ab 947 (let ((ins (cdr (assq name package-alist))))
12059709
SM
948 (and ins (version-list-= version
949 (package-desc-version (car ins))))))
66bd25ab 950 nil)
12059709
SM
951 ((not existing-packages)
952 (push (list name pkg-desc) package-archive-contents))
953 (t
954 (while
955 (if (and (cdr existing-packages)
956 (version-list-<
957 version (package-desc-version (cadr existing-packages))))
958 (setq existing-packages (cdr existing-packages))
a49ff80c
SM
959 (push pkg-desc (cdr existing-packages))
960 nil))))))
12059709
SM
961
962(defun package-download-transaction (packages)
963 "Download and install all the packages in PACKAGES.
964PACKAGES should be a list of package-desc.
96ae4c8f 965This function assumes that all package requirements in
12059709 966PACKAGES are satisfied, i.e. that PACKAGES is computed
96ae4c8f 967using `package-compute-transaction'."
12059709 968 (mapc #'package-install-from-archive packages))
44198b6e
CY
969
970;;;###autoload
5b165ade
SM
971(defun package-install (pkg)
972 "Install the package PKG.
973PKG can be a package-desc or the package name of one the available packages
974in an archive in `package-archives'. Interactively, prompt for its name."
44198b6e 975 (interactive
7254299e
CY
976 (progn
977 ;; Initialize the package system to get the list of package
978 ;; symbols for completion.
979 (unless package--initialized
980 (package-initialize t))
70550acf
PH
981 (unless package-archive-contents
982 (package-refresh-contents))
5b165ade 983 (list (intern (completing-read
12059709
SM
984 "Install package: "
985 (mapcar (lambda (elt) (symbol-name (car elt)))
986 package-archive-contents)
5b165ade 987 nil t)))))
1b8dff23 988 (package-download-transaction
12059709
SM
989 (if (package-desc-p pkg)
990 (package-compute-transaction (list pkg)
991 (package-desc-reqs pkg))
992 (package-compute-transaction ()
993 (list (list pkg))))))
44198b6e 994
ffbf300e
CY
995(defun package-strip-rcs-id (str)
996 "Strip RCS version ID from the version string STR.
44198b6e
CY
997If the result looks like a dotted numeric version, return it.
998Otherwise return nil."
ffbf300e
CY
999 (when str
1000 (when (string-match "\\`[ \t]*[$]Revision:[ \t]+" str)
1001 (setq str (substring str (match-end 0))))
1002 (condition-case nil
1003 (if (version-to-list str)
1004 str)
1005 (error nil))))
44198b6e
CY
1006
1007(defun package-buffer-info ()
f56be016 1008 "Return a `package-desc' describing the package in the current buffer.
187d3296
CY
1009
1010If the buffer does not contain a conforming package, signal an
1011error. If there is a package, narrow the buffer to the file's
1012boundaries."
44198b6e 1013 (goto-char (point-min))
f677562b 1014 (unless (re-search-forward "^;;; \\([^ ]*\\)\\.el ---[ \t]*\\(.*?\\)[ \t]*\\(-\\*-.*-\\*-[ \t]*\\)?$" nil t)
d96ad422 1015 (error "Package lacks a file header"))
187d3296
CY
1016 (let ((file-name (match-string-no-properties 1))
1017 (desc (match-string-no-properties 2))
1018 (start (line-beginning-position)))
1019 (unless (search-forward (concat ";;; " file-name ".el ends here"))
1020 (error "Package lacks a terminating comment"))
1021 ;; Try to include a trailing newline.
1022 (forward-line)
1023 (narrow-to-region start (point))
1024 (require 'lisp-mnt)
1025 ;; Use some headers we've invented to drive the process.
1026 (let* ((requires-str (lm-header "package-requires"))
187d3296
CY
1027 ;; Prefer Package-Version; if defined, the package author
1028 ;; probably wants us to use it. Otherwise try Version.
1029 (pkg-version
1030 (or (package-strip-rcs-id (lm-header "package-version"))
056453c6
DG
1031 (package-strip-rcs-id (lm-header "version"))))
1032 (homepage (lm-homepage)))
187d3296
CY
1033 (unless pkg-version
1034 (error
1035 "Package lacks a \"Version\" or \"Package-Version\" header"))
f56be016
SM
1036 (package-desc-from-define
1037 file-name pkg-version desc
1038 (if requires-str (package-read-from-string requires-str))
056453c6 1039 :kind 'single
494e898b 1040 :url homepage))))
44198b6e 1041
9e277302
JB
1042(declare-function tar-get-file-descriptor "tar-mode" (file))
1043(declare-function tar--extract "tar-mode" (descriptor))
1044
fd846ab4 1045(defun package-tar-file-info ()
44198b6e 1046 "Find package information for a tar file.
fd846ab4
SM
1047The return result is a `package-desc'."
1048 (cl-assert (derived-mode-p 'tar-mode))
1049 (let* ((dir-name (file-name-directory
1050 (tar-header-name (car tar-parse-info))))
1b8dff23 1051 (desc-file (package--description-file dir-name))
fd846ab4
SM
1052 (tar-desc (tar-get-file-descriptor (concat dir-name desc-file))))
1053 (unless tar-desc
1054 (error "No package descriptor file found"))
1055 (with-current-buffer (tar--extract tar-desc)
1056 (goto-char (point-min))
1057 (unwind-protect
1058 (let* ((pkg-def-parsed (read (current-buffer)))
1059 (pkg-desc
1060 (if (not (eq (car pkg-def-parsed) 'define-package))
1061 (error "Can't find define-package in %s"
1062 (tar-header-name tar-desc))
1063 (apply #'package-desc-from-define
1064 (append (cdr pkg-def-parsed))))))
1065 (setf (package-desc-kind pkg-desc) 'tar)
1066 pkg-desc)
1067 (kill-buffer (current-buffer))))))
f56be016 1068
44198b6e 1069
187d3296 1070;;;###autoload
fd846ab4 1071(defun package-install-from-buffer ()
187d3296 1072 "Install a package from the current buffer.
fd846ab4
SM
1073The current buffer is assumed to be a single .el or .tar file that follows the
1074packaging guidelines; see info node `(elisp)Packaging'.
1075Downloads and installs required packages as needed."
1076 (interactive)
1077 (let ((pkg-desc (if (derived-mode-p 'tar-mode)
1078 (package-tar-file-info)
1079 (package-buffer-info))))
1080 ;; Download and install the dependencies.
1081 (let* ((requires (package-desc-reqs pkg-desc))
1082 (transaction (package-compute-transaction nil requires)))
1083 (package-download-transaction transaction))
1084 ;; Install the package itself.
1085 (package-unpack pkg-desc)
1086 pkg-desc))
44198b6e 1087
44198b6e
CY
1088;;;###autoload
1089(defun package-install-file (file)
1090 "Install a package from a file.
1091The file can either be a tar file or an Emacs Lisp file."
1092 (interactive "fPackage file name: ")
1093 (with-temp-buffer
1094 (insert-file-contents-literally file)
fd846ab4
SM
1095 (when (string-match "\\.tar\\'" file) (tar-mode))
1096 (package-install-from-buffer)))
44198b6e 1097
1b8dff23
SM
1098(defun package-delete (pkg-desc)
1099 (let ((dir (package-desc-dir pkg-desc)))
12059709
SM
1100 (if (not (string-prefix-p (file-name-as-directory
1101 (expand-file-name package-user-dir))
1102 (expand-file-name dir)))
1103 ;; Don't delete "system" packages.
1104 (error "Package `%s' is a system package, not deleting"
1105 (package-desc-full-name pkg-desc))
1106 (delete-directory dir t t)
1107 ;; Update package-alist.
1108 (let* ((name (package-desc-name pkg-desc)))
1109 (delete pkg-desc (assq name package-alist)))
1110 (message "Package `%s' deleted." (package-desc-full-name pkg-desc)))))
44198b6e 1111
fd846ab4 1112(defun package-archive-base (desc)
bc44bef7 1113 "Return the archive containing the package NAME."
fd846ab4 1114 (cdr (assoc (package-desc-archive desc) package-archives)))
bc44bef7
PH
1115
1116(defun package--download-one-archive (archive file)
f561e49a
CY
1117 "Retrieve an archive file FILE from ARCHIVE, and cache it.
1118ARCHIVE should be a cons cell of the form (NAME . LOCATION),
1119similar to an entry in `package-alist'. Save the cached copy to
1120\"archives/NAME/archive-contents\" in `package-user-dir'."
66bd25ab
SM
1121 (let* ((dir (expand-file-name (format "archives/%s" (car archive))
1122 package-user-dir)))
f561e49a 1123 (package--with-work-buffer (cdr archive) file
ebf662f4
CY
1124 ;; Read the retrieved buffer to make sure it is valid (e.g. it
1125 ;; may fetch a URL redirect page).
1126 (when (listp (read buffer))
1127 (make-directory dir t)
1128 (setq buffer-file-name (expand-file-name file dir))
1129 (let ((version-control 'never))
f561e49a 1130 (save-buffer))))))
44198b6e 1131
38bb2ca8 1132;;;###autoload
44198b6e
CY
1133(defun package-refresh-contents ()
1134 "Download the ELPA archive description if needed.
ebf662f4
CY
1135This informs Emacs about the latest versions of all packages, and
1136makes them available for download."
44198b6e 1137 (interactive)
5b165ade 1138 ;; FIXME: Do it asynchronously.
44198b6e
CY
1139 (unless (file-exists-p package-user-dir)
1140 (make-directory package-user-dir t))
bc44bef7 1141 (dolist (archive package-archives)
1be3ca5a 1142 (condition-case-unless-debug nil
cb6c4991 1143 (package--download-one-archive archive "archive-contents")
187d3296 1144 (error (message "Failed to download `%s' archive."
cb6c4991 1145 (car archive)))))
bc44bef7 1146 (package-read-all-archive-contents))
44198b6e
CY
1147
1148;;;###autoload
4b99edf2 1149(defun package-initialize (&optional no-activate)
44198b6e 1150 "Load Emacs Lisp packages, and activate them.
4b99edf2
CY
1151The variable `package-load-list' controls which packages to load.
1152If optional arg NO-ACTIVATE is non-nil, don't activate packages."
44198b6e 1153 (interactive)
a7d2d465 1154 (setq package-alist nil)
44198b6e 1155 (package-load-all-descriptors)
bc44bef7 1156 (package-read-all-archive-contents)
4b99edf2
CY
1157 (unless no-activate
1158 (dolist (elt package-alist)
a7d2d465 1159 (package-activate (car elt))))
4b99edf2 1160 (setq package--initialized t))
44198b6e
CY
1161
1162\f
cced7584 1163;;;; Package description buffer.
44198b6e 1164
cced7584
CY
1165;;;###autoload
1166(defun describe-package (package)
1167 "Display the full documentation of PACKAGE (a symbol)."
1168 (interactive
12059709 1169 (let* ((guess (function-called-at-point)))
4b99edf2
CY
1170 (require 'finder-inf nil t)
1171 ;; Load the package list if necessary (but don't activate them).
1172 (unless package--initialized
1173 (package-initialize t))
12059709
SM
1174 (let ((packages (append (mapcar 'car package-alist)
1175 (mapcar 'car package-archive-contents)
1176 (mapcar 'car package--builtins))))
1177 (unless (memq guess packages)
1178 (setq guess nil))
1179 (setq packages (mapcar 'symbol-name packages))
1180 (let ((val
1181 (completing-read (if guess
1182 (format "Describe package (default %s): "
1183 guess)
1184 "Describe package: ")
1185 packages nil t nil nil guess)))
1186 (list (intern val))))))
1187 (if (not (or (package-desc-p package) (and package (symbolp package))))
4b99edf2 1188 (message "No package specified")
cced7584
CY
1189 (help-setup-xref (list #'describe-package package)
1190 (called-interactively-p 'interactive))
1191 (with-help-window (help-buffer)
1192 (with-current-buffer standard-output
1193 (describe-package-1 package)))))
1194
12059709 1195(defun describe-package-1 (pkg)
96ae4c8f 1196 (require 'lisp-mnt)
12059709
SM
1197 (let* ((desc (or
1198 (if (package-desc-p pkg) pkg)
1199 (cadr (assq pkg package-alist))
1200 (let ((built-in (assq pkg package--builtins)))
1201 (if built-in
1202 (package--from-builtin built-in)
1203 (cadr (assq pkg package-archive-contents))))))
1204 (name (if desc (package-desc-name desc) pkg))
1205 (pkg-dir (if desc (package-desc-dir desc)))
1206 (reqs (if desc (package-desc-reqs desc)))
1207 (version (if desc (package-desc-version desc)))
1208 (archive (if desc (package-desc-archive desc)))
494e898b 1209 (homepage (if desc (cdr (assoc :url (package-desc-extras desc)))))
12059709
SM
1210 (built-in (eq pkg-dir 'builtin))
1211 (installable (and archive (not built-in)))
1212 (status (if desc (package-desc-status desc) "orphan")))
1213 (prin1 name)
cced7584 1214 (princ " is ")
12059709
SM
1215 (princ (if (memq (aref status 0) '(?a ?e ?i ?o ?u)) "an " "a "))
1216 (princ status)
1217 (princ " package.\n\n")
cb6c4991 1218
96ae4c8f 1219 (insert " " (propertize "Status" 'font-lock-face 'bold) ": ")
12059709
SM
1220 (cond (built-in
1221 (insert (propertize (capitalize status)
1222 'font-lock-face 'font-lock-builtin-face)
1223 "."))
1224 (pkg-dir
1225 (insert (propertize (capitalize status) ;FIXME: Why comment-face?
96ae4c8f 1226 'font-lock-face 'font-lock-comment-face))
cb6c4991
CY
1227 (insert " in `")
1228 ;; Todo: Add button for uninstalling.
12059709
SM
1229 (help-insert-xref-button (abbreviate-file-name
1230 (file-name-as-directory pkg-dir))
cb6c4991 1231 'help-package-def pkg-dir)
12059709
SM
1232 (if (and (package-built-in-p name)
1233 (not (package-built-in-p name version)))
4b99edf2
CY
1234 (insert "',\n shadowing a "
1235 (propertize "built-in package"
1236 'font-lock-face 'font-lock-builtin-face)
1237 ".")
1238 (insert "'.")))
cb6c4991 1239 (installable
12059709
SM
1240 (insert (capitalize status))
1241 (insert " from " (format "%s" archive))
55b056ba 1242 (insert " -- ")
4b99edf2 1243 (let ((button-text (if (display-graphic-p) "Install" "[Install]"))
cb6c4991
CY
1244 (button-face (if (display-graphic-p)
1245 '(:box (:line-width 2 :color "dark grey")
1246 :background "light grey"
1247 :foreground "black")
1248 'link)))
4b99edf2 1249 (insert-text-button button-text 'face button-face 'follow-link t
1b8dff23 1250 'package-desc desc
cb6c4991 1251 'action 'package-install-button-action)))
12059709 1252 (t (insert (capitalize status) ".")))
cb6c4991 1253 (insert "\n")
12059709 1254 (and version
96ae4c8f 1255 (insert " "
12059709
SM
1256 (propertize "Version" 'font-lock-face 'bold) ": "
1257 (package-version-join version) "\n"))
4b99edf2
CY
1258
1259 (setq reqs (if desc (package-desc-reqs desc)))
8adb4c33 1260 (when reqs
96ae4c8f 1261 (insert " " (propertize "Requires" 'font-lock-face 'bold) ": ")
8adb4c33
CY
1262 (let ((first t)
1263 name vers text)
1264 (dolist (req reqs)
1265 (setq name (car req)
1266 vers (cadr req)
1267 text (format "%s-%s" (symbol-name name)
1268 (package-version-join vers)))
1269 (cond (first (setq first nil))
1270 ((>= (+ 2 (current-column) (length text))
1271 (window-width))
1272 (insert ",\n "))
1273 (t (insert ", ")))
1274 (help-insert-xref-button text 'help-package name))
1275 (insert "\n")))
96ae4c8f 1276 (insert " " (propertize "Summary" 'font-lock-face 'bold)
12059709 1277 ": " (if desc (package-desc-summary desc)) "\n")
056453c6
DG
1278 (when homepage
1279 (insert " " (propertize "Homepage" 'font-lock-face 'bold) ": ")
1280 (help-insert-xref-button homepage 'help-url homepage)
1281 (insert "\n"))
12059709
SM
1282 (let* ((all-pkgs (append (cdr (assq name package-alist))
1283 (cdr (assq name package-archive-contents))
1284 (let ((bi (assq name package--builtins)))
1285 (if bi (list (package--from-builtin bi))))))
1286 (other-pkgs (delete desc all-pkgs)))
1287 (when other-pkgs
1288 (insert " " (propertize "Other versions" 'font-lock-face 'bold) ": "
1289 (mapconcat
1290 (lambda (opkg)
1291 (let* ((ov (package-desc-version opkg))
1292 (dir (package-desc-dir opkg))
1293 (from (or (package-desc-archive opkg)
1294 (if (stringp dir) "installed" dir))))
1295 (if (not ov) (format "%s" from)
1296 (format "%s (%s)"
1297 (make-text-button (package-version-join ov) nil
1298 'face 'link
1299 'follow-link t
1300 'action
1301 (lambda (_button)
1302 (describe-package opkg)))
1303 from))))
1304 other-pkgs ", ")
1305 ".\n")))
1306
1307 (insert "\n")
cb6c4991 1308
4b99edf2 1309 (if built-in
96ae4c8f 1310 ;; For built-in packages, insert the commentary.
12059709 1311 (let ((fn (locate-file (format "%s.el" name) load-path
96ae4c8f
CY
1312 load-file-rep-suffixes))
1313 (opoint (point)))
1314 (insert (or (lm-commentary fn) ""))
1315 (save-excursion
1316 (goto-char opoint)
1317 (when (re-search-forward "^;;; Commentary:\n" nil t)
1318 (replace-match ""))
1319 (while (re-search-forward "^\\(;+ ?\\)" nil t)
1320 (replace-match ""))))
12059709 1321 (let ((readme (expand-file-name (format "%s-readme.txt" name)
f561e49a
CY
1322 package-user-dir))
1323 readme-string)
96ae4c8f
CY
1324 ;; For elpa packages, try downloading the commentary. If that
1325 ;; fails, try an existing readme file in `package-user-dir'.
f561e49a 1326 (cond ((condition-case nil
12059709
SM
1327 (package--with-work-buffer
1328 (package-archive-base desc)
1329 (format "%s-readme.txt" name)
f561e49a
CY
1330 (setq buffer-file-name
1331 (expand-file-name readme package-user-dir))
1332 (let ((version-control 'never))
1333 (save-buffer))
1334 (setq readme-string (buffer-string))
1335 t)
1336 (error nil))
1337 (insert readme-string))
96ae4c8f
CY
1338 ((file-readable-p readme)
1339 (insert-file-contents readme)
1340 (goto-char (point-max))))))))
cb6c4991
CY
1341
1342(defun package-install-button-action (button)
1b8dff23
SM
1343 (let ((pkg-desc (button-get button 'package-desc)))
1344 (when (y-or-n-p (format "Install package `%s'? "
1345 (package-desc-full-name pkg-desc)))
1346 (package-install pkg-desc)
cb6c4991
CY
1347 (revert-buffer nil t)
1348 (goto-char (point-min)))))
cced7584
CY
1349
1350\f
44198b6e
CY
1351;;;; Package menu mode.
1352
54ea2a0d 1353(defvar package-menu-mode-map
e91a96fe 1354 (let ((map (make-sparse-keymap))
64eba874 1355 (menu-map (make-sparse-keymap "Package")))
e91a96fe 1356 (set-keymap-parent map tabulated-list-mode-map)
8adb4c33 1357 (define-key map "\C-m" 'package-menu-describe-package)
54ea2a0d
JB
1358 (define-key map "u" 'package-menu-mark-unmark)
1359 (define-key map "\177" 'package-menu-backup-unmark)
1360 (define-key map "d" 'package-menu-mark-delete)
1361 (define-key map "i" 'package-menu-mark-install)
25322144 1362 (define-key map "U" 'package-menu-mark-upgrades)
54ea2a0d
JB
1363 (define-key map "r" 'package-menu-refresh)
1364 (define-key map "~" 'package-menu-mark-obsolete-for-deletion)
1365 (define-key map "x" 'package-menu-execute)
1366 (define-key map "h" 'package-menu-quick-help)
cb6c4991 1367 (define-key map "?" 'package-menu-describe-package)
64eba874
DN
1368 (define-key map [menu-bar package-menu] (cons "Package" menu-map))
1369 (define-key menu-map [mq]
1370 '(menu-item "Quit" quit-window
1371 :help "Quit package selection"))
1372 (define-key menu-map [s1] '("--"))
1373 (define-key menu-map [mn]
1374 '(menu-item "Next" next-line
1375 :help "Next Line"))
1376 (define-key menu-map [mp]
1377 '(menu-item "Previous" previous-line
1378 :help "Previous Line"))
1379 (define-key menu-map [s2] '("--"))
1380 (define-key menu-map [mu]
1381 '(menu-item "Unmark" package-menu-mark-unmark
1382 :help "Clear any marks on a package and move to the next line"))
1383 (define-key menu-map [munm]
7cc6e154 1384 '(menu-item "Unmark Backwards" package-menu-backup-unmark
64eba874
DN
1385 :help "Back up one line and clear any marks on that package"))
1386 (define-key menu-map [md]
7cc6e154 1387 '(menu-item "Mark for Deletion" package-menu-mark-delete
64eba874
DN
1388 :help "Mark a package for deletion and move to the next line"))
1389 (define-key menu-map [mi]
7cc6e154 1390 '(menu-item "Mark for Install" package-menu-mark-install
64eba874 1391 :help "Mark a package for installation and move to the next line"))
d770725a 1392 (define-key menu-map [mupgrades]
7cc6e154 1393 '(menu-item "Mark Upgradable Packages" package-menu-mark-upgrades
d770725a 1394 :help "Mark packages that have a newer version for upgrading"))
64eba874
DN
1395 (define-key menu-map [s3] '("--"))
1396 (define-key menu-map [mg]
7cc6e154 1397 '(menu-item "Update Package List" revert-buffer
64eba874
DN
1398 :help "Update the list of packages"))
1399 (define-key menu-map [mr]
7cc6e154 1400 '(menu-item "Refresh Package List" package-menu-refresh
64eba874
DN
1401 :help "Download the ELPA archive"))
1402 (define-key menu-map [s4] '("--"))
1403 (define-key menu-map [mt]
7cc6e154 1404 '(menu-item "Mark Obsolete Packages" package-menu-mark-obsolete-for-deletion
64eba874
DN
1405 :help "Mark all obsolete packages for deletion"))
1406 (define-key menu-map [mx]
7cc6e154 1407 '(menu-item "Execute Actions" package-menu-execute
64eba874
DN
1408 :help "Perform all the marked actions"))
1409 (define-key menu-map [s5] '("--"))
1410 (define-key menu-map [mh]
1411 '(menu-item "Help" package-menu-quick-help
1412 :help "Show short key binding help for package-menu-mode"))
1413 (define-key menu-map [mc]
1414 '(menu-item "View Commentary" package-menu-view-commentary
1415 :help "Display information about this package"))
54ea2a0d 1416 map)
44198b6e
CY
1417 "Local keymap for `package-menu-mode' buffers.")
1418
60057926
CY
1419(defvar package-menu--new-package-list nil
1420 "List of newly-available packages since `list-packages' was last called.")
1421
e91a96fe 1422(define-derived-mode package-menu-mode tabulated-list-mode "Package Menu"
44198b6e
CY
1423 "Major mode for browsing a list of packages.
1424Letters do not insert themselves; instead, they are commands.
1425\\<package-menu-mode-map>
1426\\{package-menu-mode-map}"
e91a96fe
CY
1427 (setq tabulated-list-format [("Package" 18 package-menu--name-predicate)
1428 ("Version" 12 nil)
1429 ("Status" 10 package-menu--status-predicate)
1430 ("Description" 0 nil)])
1431 (setq tabulated-list-padding 2)
1432 (setq tabulated-list-sort-key (cons "Status" nil))
6874724a 1433 (add-hook 'tabulated-list-revert-hook 'package-menu--refresh nil t)
e91a96fe
CY
1434 (tabulated-list-init-header))
1435
1b8dff23 1436(defmacro package--push (pkg-desc status listname)
e91a96fe
CY
1437 "Convenience macro for `package-menu--generate'.
1438If the alist stored in the symbol LISTNAME lacks an entry for a
1b8dff23
SM
1439package PKG-DESC, add one. The alist is keyed with PKG-DESC."
1440 `(unless (assoc ,pkg-desc ,listname)
1441 ;; FIXME: Should we move status into pkg-desc?
1442 (push (cons ,pkg-desc ,status) ,listname)))
e91a96fe 1443
66bd25ab
SM
1444(defvar package-list-unversioned nil
1445 "If non-nil include packages that don't have a version in `list-package'.")
1446
12059709
SM
1447(defun package-desc-status (pkg-desc)
1448 (let* ((name (package-desc-name pkg-desc))
1449 (dir (package-desc-dir pkg-desc))
1450 (lle (assq name package-load-list))
1451 (held (cadr lle))
1452 (version (package-desc-version pkg-desc)))
1453 (cond
1454 ((eq dir 'builtin) "built-in")
1455 ((and lle (null held)) "disabled")
1456 ((stringp held)
1457 (let ((hv (if (stringp held) (version-to-list held))))
1458 (cond
1459 ((version-list-= version hv) "held")
1460 ((version-list-< version hv) "obsolete")
1461 (t "disabled"))))
1462 ((package-built-in-p name version) "obsolete")
1463 (dir ;One of the installed packages.
1464 (cond
1465 ((not (file-exists-p (package-desc-dir pkg-desc))) "deleted")
1466 ((eq pkg-desc (cadr (assq name package-alist))) "installed")
1467 (t "obsolete")))
1468 (t
1469 (let* ((ins (cadr (assq name package-alist)))
1470 (ins-v (if ins (package-desc-version ins))))
1471 (cond
1472 ((or (null ins) (version-list-< ins-v version))
1473 (if (memq name package-menu--new-package-list)
1474 "new" "available"))
1475 ((version-list-< version ins-v) "obsolete")
1476 ((version-list-= version ins-v) "installed")))))))
1477
1478(defun package-menu--refresh (&optional packages)
1479 "Re-populate the `tabulated-list-entries'.
1480PACKAGES should be nil or t, which means to display all known packages."
1b8dff23 1481 ;; Construct list of (PKG-DESC . STATUS).
12059709 1482 (unless packages (setq packages t))
4d6769e1 1483 (let (info-list name)
e91a96fe
CY
1484 ;; Installed packages:
1485 (dolist (elt package-alist)
1486 (setq name (car elt))
512e3ae1 1487 (when (or (eq packages t) (memq name packages))
12059709
SM
1488 (dolist (pkg (cdr elt))
1489 (package--push pkg (package-desc-status pkg) info-list))))
e91a96fe
CY
1490
1491 ;; Built-in packages:
1492 (dolist (elt package--builtins)
1493 (setq name (car elt))
512e3ae1 1494 (when (and (not (eq name 'emacs)) ; Hide the `emacs' package.
66bd25ab
SM
1495 (or package-list-unversioned
1496 (package--bi-desc-version (cdr elt)))
512e3ae1 1497 (or (eq packages t) (memq name packages)))
1b8dff23 1498 (package--push (package--from-builtin elt) "built-in" info-list)))
e91a96fe
CY
1499
1500 ;; Available and disabled packages:
1501 (dolist (elt package-archive-contents)
1502 (setq name (car elt))
512e3ae1 1503 (when (or (eq packages t) (memq name packages))
12059709
SM
1504 (dolist (pkg (cdr elt))
1505 ;; Hide obsolete packages.
1506 (unless (package-installed-p (package-desc-name pkg)
1507 (package-desc-version pkg))
1508 (package--push pkg (package-desc-status pkg) info-list)))))
e91a96fe 1509
e91a96fe 1510 ;; Print the result.
12059709
SM
1511 (setq tabulated-list-entries
1512 (mapcar #'package-menu--print-info info-list))))
1513
1514(defun package-menu--generate (remember-pos packages)
1515 "Populate the Package Menu.
1516 If REMEMBER-POS is non-nil, keep point on the same entry.
1517PACKAGES should be t, which means to display all known packages,
1518or a list of package names (symbols) to display."
1519 (package-menu--refresh packages)
1520 (tabulated-list-print remember-pos))
e91a96fe
CY
1521
1522(defun package-menu--print-info (pkg)
1523 "Return a package entry suitable for `tabulated-list-entries'.
1b8dff23
SM
1524PKG has the form (PKG-DESC . STATUS).
1525Return (PKG-DESC [NAME VERSION STATUS DOC])."
1526 (let* ((pkg-desc (car pkg))
1527 (status (cdr pkg))
1528 (face (pcase status
1529 (`"built-in" 'font-lock-builtin-face)
1530 (`"available" 'default)
1531 (`"new" 'bold)
1532 (`"held" 'font-lock-constant-face)
1533 (`"disabled" 'font-lock-warning-face)
1534 (`"installed" 'font-lock-comment-face)
1535 (_ 'font-lock-warning-face)))) ; obsolete.
1536 (list pkg-desc
1537 (vector (list (symbol-name (package-desc-name pkg-desc))
e91a96fe
CY
1538 'face 'link
1539 'follow-link t
1b8dff23 1540 'package-desc pkg-desc
e91a96fe 1541 'action 'package-menu-describe-package)
1b8dff23
SM
1542 (propertize (package-version-join
1543 (package-desc-version pkg-desc))
e91a96fe
CY
1544 'font-lock-face face)
1545 (propertize status 'font-lock-face face)
1b8dff23
SM
1546 (propertize (package-desc-summary pkg-desc)
1547 'font-lock-face face)))))
44198b6e
CY
1548
1549(defun package-menu-refresh ()
ebf662f4
CY
1550 "Download the Emacs Lisp package archive.
1551This fetches the contents of each archive specified in
1552`package-archives', and then refreshes the package menu."
44198b6e 1553 (interactive)
25322144 1554 (unless (derived-mode-p 'package-menu-mode)
96dbf5a8 1555 (user-error "The current buffer is not a Package Menu"))
44198b6e 1556 (package-refresh-contents)
512e3ae1 1557 (package-menu--generate t t))
44198b6e 1558
e91a96fe
CY
1559(defun package-menu-describe-package (&optional button)
1560 "Describe the current package.
1561If optional arg BUTTON is non-nil, describe its associated package."
44198b6e 1562 (interactive)
1b8dff23 1563 (let ((pkg-desc (if button (button-get button 'package-desc)
66bd25ab 1564 (tabulated-list-get-id))))
1b8dff23 1565 (if pkg-desc
12059709 1566 (describe-package pkg-desc)
96dbf5a8 1567 (user-error "No package here"))))
44198b6e
CY
1568
1569;; fixme numeric argument
4d6769e1 1570(defun package-menu-mark-delete (&optional _num)
44198b6e
CY
1571 "Mark a package for deletion and move to the next line."
1572 (interactive "p")
fb87e0fb 1573 (if (member (package-menu-get-status) '("installed" "obsolete"))
e91a96fe 1574 (tabulated-list-put-tag "D" t)
015eea59 1575 (forward-line)))
44198b6e 1576
4d6769e1 1577(defun package-menu-mark-install (&optional _num)
44198b6e
CY
1578 "Mark a package for installation and move to the next line."
1579 (interactive "p")
60057926 1580 (if (member (package-menu-get-status) '("available" "new"))
e91a96fe 1581 (tabulated-list-put-tag "I" t)
015eea59 1582 (forward-line)))
44198b6e 1583
4d6769e1 1584(defun package-menu-mark-unmark (&optional _num)
44198b6e
CY
1585 "Clear any marks on a package and move to the next line."
1586 (interactive "p")
e91a96fe 1587 (tabulated-list-put-tag " " t))
44198b6e
CY
1588
1589(defun package-menu-backup-unmark ()
1590 "Back up one line and clear any marks on that package."
1591 (interactive)
1592 (forward-line -1)
e91a96fe 1593 (tabulated-list-put-tag " "))
44198b6e
CY
1594
1595(defun package-menu-mark-obsolete-for-deletion ()
1596 "Mark all obsolete packages for deletion."
1597 (interactive)
1598 (save-excursion
1599 (goto-char (point-min))
44198b6e 1600 (while (not (eobp))
25322144 1601 (if (equal (package-menu-get-status) "obsolete")
e91a96fe 1602 (tabulated-list-put-tag "D" t)
44198b6e
CY
1603 (forward-line 1)))))
1604
1605(defun package-menu-quick-help ()
1606 "Show short key binding help for package-menu-mode."
1607 (interactive)
1608 (message "n-ext, i-nstall, d-elete, u-nmark, x-ecute, r-efresh, h-elp"))
1609
cb6c4991
CY
1610(define-obsolete-function-alias
1611 'package-menu-view-commentary 'package-menu-describe-package "24.1")
44198b6e 1612
44198b6e 1613(defun package-menu-get-status ()
1b8dff23
SM
1614 (let* ((id (tabulated-list-get-id))
1615 (entry (and id (assq id tabulated-list-entries))))
25322144
CY
1616 (if entry
1617 (aref (cadr entry) 2)
44198b6e
CY
1618 "")))
1619
25322144
CY
1620(defun package-menu--find-upgrades ()
1621 (let (installed available upgrades)
1622 ;; Build list of installed/available packages in this buffer.
1623 (dolist (entry tabulated-list-entries)
1b8dff23
SM
1624 ;; ENTRY is (PKG-DESC [NAME VERSION STATUS DOC])
1625 (let ((pkg-desc (car entry))
4d6769e1 1626 (status (aref (cadr entry) 2)))
25322144 1627 (cond ((equal status "installed")
1b8dff23 1628 (push pkg-desc installed))
60057926 1629 ((member status '("available" "new"))
1b8dff23
SM
1630 (push (cons (package-desc-name pkg-desc) pkg-desc)
1631 available)))))
1632 ;; Loop through list of installed packages, finding upgrades.
1633 (dolist (pkg-desc installed)
1634 (let ((avail-pkg (assq (package-desc-name pkg-desc) available)))
25322144 1635 (and avail-pkg
1b8dff23
SM
1636 (version-list-< (package-desc-version pkg-desc)
1637 (package-desc-version (cdr avail-pkg)))
25322144
CY
1638 (push avail-pkg upgrades))))
1639 upgrades))
1640
1641(defun package-menu-mark-upgrades ()
1642 "Mark all upgradable packages in the Package Menu.
1643For each installed package with a newer version available, place
1644an (I)nstall flag on the available version and a (D)elete flag on
1645the installed version. A subsequent \\[package-menu-execute]
1646call will upgrade the package."
1647 (interactive)
1648 (unless (derived-mode-p 'package-menu-mode)
1649 (error "The current buffer is not a Package Menu"))
1650 (let ((upgrades (package-menu--find-upgrades)))
1651 (if (null upgrades)
1652 (message "No packages to upgrade.")
1653 (widen)
1654 (save-excursion
1655 (goto-char (point-min))
1656 (while (not (eobp))
1b8dff23
SM
1657 (let* ((pkg-desc (tabulated-list-get-id))
1658 (upgrade (cdr (assq (package-desc-name pkg-desc) upgrades))))
25322144
CY
1659 (cond ((null upgrade)
1660 (forward-line 1))
1b8dff23 1661 ((equal pkg-desc upgrade)
25322144
CY
1662 (package-menu-mark-install))
1663 (t
1664 (package-menu-mark-delete))))))
1665 (message "%d package%s marked for upgrading."
1666 (length upgrades)
1667 (if (= (length upgrades) 1) "" "s")))))
1668
a7da0114 1669(defun package-menu-execute (&optional noquery)
015eea59
CY
1670 "Perform marked Package Menu actions.
1671Packages marked for installation are downloaded and installed;
eeb468da
GM
1672packages marked for deletion are removed.
1673Optional argument NOQUERY non-nil means do not ask the user to confirm."
44198b6e 1674 (interactive)
25322144 1675 (unless (derived-mode-p 'package-menu-mode)
e91a96fe 1676 (error "The current buffer is not in Package Menu mode"))
1b8dff23 1677 (let (install-list delete-list cmd pkg-desc)
015eea59
CY
1678 (save-excursion
1679 (goto-char (point-min))
1680 (while (not (eobp))
1681 (setq cmd (char-after))
e91a96fe 1682 (unless (eq cmd ?\s)
1b8dff23
SM
1683 ;; This is the key PKG-DESC.
1684 (setq pkg-desc (tabulated-list-get-id))
e91a96fe 1685 (cond ((eq cmd ?D)
1b8dff23 1686 (push pkg-desc delete-list))
e91a96fe 1687 ((eq cmd ?I)
1b8dff23 1688 (push pkg-desc install-list))))
015eea59 1689 (forward-line)))
25322144 1690 (when install-list
a7da0114
YB
1691 (if (or
1692 noquery
1693 (yes-or-no-p
1b8dff23
SM
1694 (if (= (length install-list) 1)
1695 (format "Install package `%s'? "
1696 (package-desc-full-name (car install-list)))
1697 (format "Install these %d packages (%s)? "
1698 (length install-list)
1699 (mapconcat #'package-desc-full-name
1700 install-list ", ")))))
25322144 1701 (mapc 'package-install install-list)))
015eea59
CY
1702 ;; Delete packages, prompting if necessary.
1703 (when delete-list
a7da0114
YB
1704 (if (or
1705 noquery
1706 (yes-or-no-p
015eea59 1707 (if (= (length delete-list) 1)
1b8dff23
SM
1708 (format "Delete package `%s'? "
1709 (package-desc-full-name (car delete-list)))
015eea59
CY
1710 (format "Delete these %d packages (%s)? "
1711 (length delete-list)
1b8dff23
SM
1712 (mapconcat #'package-desc-full-name
1713 delete-list ", ")))))
015eea59 1714 (dolist (elt delete-list)
1be3ca5a 1715 (condition-case-unless-debug err
1b8dff23 1716 (package-delete elt)
015eea59
CY
1717 (error (message (cadr err)))))
1718 (error "Aborted")))
015eea59 1719 (if (or delete-list install-list)
512e3ae1 1720 (package-menu--generate t t)
015eea59 1721 (message "No operations specified."))))
44198b6e 1722
e91a96fe
CY
1723(defun package-menu--version-predicate (A B)
1724 (let ((vA (or (aref (cadr A) 1) '(0)))
1725 (vB (or (aref (cadr B) 1) '(0))))
1726 (if (version-list-= vA vB)
1727 (package-menu--name-predicate A B)
1728 (version-list-< vA vB))))
1729
1730(defun package-menu--status-predicate (A B)
1731 (let ((sA (aref (cadr A) 2))
1732 (sB (aref (cadr B) 2)))
1733 (cond ((string= sA sB)
1734 (package-menu--name-predicate A B))
60057926
CY
1735 ((string= sA "new") t)
1736 ((string= sB "new") nil)
1737 ((string= sA "available") t)
e91a96fe 1738 ((string= sB "available") nil)
60057926 1739 ((string= sA "installed") t)
e91a96fe 1740 ((string= sB "installed") nil)
60057926 1741 ((string= sA "held") t)
e91a96fe 1742 ((string= sB "held") nil)
60057926 1743 ((string= sA "built-in") t)
e91a96fe 1744 ((string= sB "built-in") nil)
60057926
CY
1745 ((string= sA "obsolete") t)
1746 ((string= sB "obsolete") nil)
e91a96fe
CY
1747 (t (string< sA sB)))))
1748
1749(defun package-menu--description-predicate (A B)
1750 (let ((dA (aref (cadr A) 3))
1751 (dB (aref (cadr B) 3)))
1752 (if (string= dA dB)
1753 (package-menu--name-predicate A B)
1754 (string< dA dB))))
1755
1756(defun package-menu--name-predicate (A B)
1b8dff23
SM
1757 (string< (symbol-name (package-desc-name (car A)))
1758 (symbol-name (package-desc-name (car B)))))
44198b6e
CY
1759
1760;;;###autoload
e91a96fe 1761(defun list-packages (&optional no-fetch)
44198b6e 1762 "Display a list of packages.
e91a96fe
CY
1763This first fetches the updated list of packages before
1764displaying, unless a prefix argument NO-FETCH is specified.
44198b6e 1765The list is displayed in a buffer named `*Packages*'."
e91a96fe
CY
1766 (interactive "P")
1767 (require 'finder-inf nil t)
8a500a91 1768 ;; Initialize the package system if necessary.
4b99edf2
CY
1769 (unless package--initialized
1770 (package-initialize t))
60057926
CY
1771 (let (old-archives new-packages)
1772 (unless no-fetch
1773 ;; Read the locally-cached archive-contents.
1774 (package-read-all-archive-contents)
1775 (setq old-archives package-archive-contents)
1776 ;; Fetch the remote list of packages.
1777 (package-refresh-contents)
1778 ;; Find which packages are new.
1779 (dolist (elt package-archive-contents)
1780 (unless (assq (car elt) old-archives)
1781 (push (car elt) new-packages))))
1782
1783 ;; Generate the Package Menu.
1784 (let ((buf (get-buffer-create "*Packages*")))
1785 (with-current-buffer buf
1786 (package-menu-mode)
1787 (set (make-local-variable 'package-menu--new-package-list)
1788 new-packages)
1789 (package-menu--generate nil t))
1790 ;; The package menu buffer has keybindings. If the user types
1791 ;; `M-x list-packages', that suggests it should become current.
1792 (switch-to-buffer buf))
1793
1794 (let ((upgrades (package-menu--find-upgrades)))
1795 (if upgrades
1796 (message "%d package%s can be upgraded; type `%s' to mark %s for upgrading."
1797 (length upgrades)
1798 (if (= (length upgrades) 1) "" "s")
1799 (substitute-command-keys "\\[package-menu-mark-upgrades]")
1800 (if (= (length upgrades) 1) "it" "them"))))))
44198b6e 1801
cb8759ca 1802;;;###autoload
96ae4c8f 1803(defalias 'package-list-packages 'list-packages)
cb8759ca 1804
512e3ae1
CY
1805;; Used in finder.el
1806(defun package-show-package-list (packages)
1807 "Display PACKAGES in a *Packages* buffer.
1808This is similar to `list-packages', but it does not fetch the
1809updated list of packages, and it only displays packages with
1810names in PACKAGES (which should be a list of symbols)."
1811 (require 'finder-inf nil t)
1812 (let ((buf (get-buffer-create "*Packages*")))
1813 (with-current-buffer buf
1814 (package-menu-mode)
1815 (package-menu--generate nil packages))
1816 (switch-to-buffer buf)))
1817
44198b6e
CY
1818(defun package-list-packages-no-fetch ()
1819 "Display a list of packages.
1820Does not fetch the updated list of packages before displaying.
1821The list is displayed in a buffer named `*Packages*'."
1822 (interactive)
e91a96fe 1823 (list-packages t))
44198b6e 1824
44198b6e
CY
1825(provide 'package)
1826
1827;;; package.el ends here