Commit | Line | Data |
---|---|---|
44198b6e CY |
1 | ;;; package.el --- Simple package system for Emacs |
2 | ||
73b0cd50 | 3 | ;; Copyright (C) 2007-2011 Free Software Foundation, Inc. |
44198b6e CY |
4 | |
5 | ;; Author: Tom Tromey <tromey@redhat.com> | |
6 | ;; Created: 10 Mar 2007 | |
7 | ;; Version: 0.9 | |
8 | ;; Keywords: tools | |
9 | ||
10 | ;; This file is part of GNU Emacs. | |
11 | ||
12 | ;; GNU Emacs is free software; you can redistribute it and/or modify | |
13 | ;; it under the terms of the GNU General Public License as published by | |
14 | ;; the Free Software Foundation; either version 3, or (at your option) | |
15 | ;; any later version. | |
16 | ||
17 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
18 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
19 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
20 | ;; GNU General Public License for more details. | |
21 | ||
22 | ;; You should have received a copy of the GNU General Public License | |
23 | ;; along with GNU Emacs; see the file COPYING. If not, write to the | |
24 | ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | |
25 | ;; Boston, MA 02110-1301, USA. | |
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 | ||
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 | ;; - Implement M-x package-upgrade, to upgrade any/all existing packages | |
143 | ;; - Use hierarchical layout. PKG/etc PKG/lisp PKG/info | |
144 | ;; ... except maybe lisp? | |
145 | ;; - It may be nice to have a macro that expands to the package's | |
146 | ;; private data dir, aka ".../etc". Or, maybe data-directory | |
147 | ;; needs to be a list (though this would be less nice) | |
148 | ;; a few packages want this, eg sokoban | |
149 | ;; - package menu needs: | |
150 | ;; ability to know which packages are built-in & thus not deletable | |
151 | ;; it can sometimes print odd results, like 0.3 available but 0.4 active | |
152 | ;; why is that? | |
153 | ;; - Allow multiple versions on the server...? | |
154 | ;; [ why bother? ] | |
155 | ;; - Don't install a package which will invalidate dependencies overall | |
156 | ;; - Allow something like (or (>= emacs 21.0) (>= xemacs 21.5)) | |
157 | ;; [ currently thinking, why bother.. KISS ] | |
158 | ;; - Allow optional package dependencies | |
159 | ;; then if we require 'bbdb', bbdb-specific lisp in lisp/bbdb | |
160 | ;; and just don't compile to add to load path ...? | |
161 | ;; - Have a list of archive URLs? [ maybe there's no point ] | |
162 | ;; - David Kastrup pointed out on the xemacs list that for GPL it | |
163 | ;; is friendlier to ship the source tree. We could "support" that | |
164 | ;; by just having a "src" subdir in the package. This isn't ideal | |
165 | ;; but it probably is not worth trying to support random source | |
166 | ;; tree layouts, build schemes, etc. | |
167 | ;; - Our treatment of the info path is somewhat bogus | |
168 | ;; - perhaps have an "unstable" tree in ELPA as well as a stable one | |
169 | ||
170 | ;;; Code: | |
171 | ||
e91a96fe CY |
172 | (require 'tabulated-list) |
173 | ||
44198b6e CY |
174 | (defgroup package nil |
175 | "Manager for Emacs Lisp packages." | |
176 | :group 'applications | |
177 | :version "24.1") | |
178 | ||
179 | ;;;###autoload | |
180 | (defcustom package-enable-at-startup t | |
181 | "Whether to activate installed packages when Emacs starts. | |
182 | If non-nil, packages are activated after reading the init file | |
183 | and before `after-init-hook'. Activation is not done if | |
184 | `user-init-file' is nil (e.g. Emacs was started with \"-q\"). | |
185 | ||
186 | Even if the value is nil, you can type \\[package-initialize] to | |
187 | activate the package system at any time." | |
188 | :type 'boolean | |
189 | :group 'package | |
190 | :version "24.1") | |
191 | ||
192 | (defcustom package-load-list '(all) | |
193 | "List of packages for `package-initialize' to load. | |
194 | Each element in this list should be a list (NAME VERSION), or the | |
195 | symbol `all'. The symbol `all' says to load the latest installed | |
196 | versions of all packages not specified by other elements. | |
197 | ||
198 | For an element (NAME VERSION), NAME is a package name (a symbol). | |
199 | VERSION should be t, a string, or nil. | |
200 | If VERSION is t, all versions are loaded, though obsolete ones | |
201 | will be put in `package-obsolete-alist' and not activated. | |
202 | If VERSION is a string, only that version is ever loaded. | |
203 | Any other version, even if newer, is silently ignored. | |
204 | Hence, the package is \"held\" at that version. | |
205 | If VERSION is nil, the package is not loaded (it is \"disabled\")." | |
206 | :type '(repeat symbol) | |
bc44bef7 | 207 | :risky t |
44198b6e CY |
208 | :group 'package |
209 | :version "24.1") | |
210 | ||
211 | (defvar Info-directory-list) | |
44198b6e CY |
212 | (declare-function info-initialize "info" ()) |
213 | (declare-function url-http-parse-response "url-http" ()) | |
214 | (declare-function lm-header "lisp-mnt" (header)) | |
215 | (declare-function lm-commentary "lisp-mnt" (&optional file)) | |
cb6c4991 | 216 | (defvar url-http-end-of-headers) |
44198b6e | 217 | |
bc44bef7 PH |
218 | (defcustom package-archives '(("gnu" . "http://elpa.gnu.org/packages/")) |
219 | "An alist of archives from which to fetch. | |
220 | The default value points to the GNU Emacs package repository. | |
f561e49a CY |
221 | |
222 | Each element has the form (ID . LOCATION). | |
223 | ID is an archive name, as a string. | |
224 | LOCATION specifies the base location for the archive. | |
225 | If it starts with \"http:\", it is treated as a HTTP URL; | |
226 | otherwise it should be an absolute directory name. | |
227 | (Other types of URL are currently not supported.)" | |
bc44bef7 | 228 | :type '(alist :key-type (string :tag "Archive name") |
f561e49a | 229 | :value-type (string :tag "URL or directory name")) |
bc44bef7 PH |
230 | :risky t |
231 | :group 'package | |
232 | :version "24.1") | |
44198b6e CY |
233 | |
234 | (defconst package-archive-version 1 | |
235 | "Version number of the package archive understood by this file. | |
236 | Lower version numbers than this will probably be understood as well.") | |
237 | ||
238 | (defconst package-el-version "1.0" | |
239 | "Version of package.el.") | |
240 | ||
241 | ;; We don't prime the cache since it tends to get out of date. | |
242 | (defvar package-archive-contents nil | |
243 | "Cache of the contents of the Emacs Lisp Package Archive. | |
244 | This is an alist mapping package names (symbols) to package | |
245 | descriptor vectors. These are like the vectors for `package-alist' | |
bc44bef7 PH |
246 | but have extra entries: one which is 'tar for tar packages and |
247 | 'single for single-file packages, and one which is the name of | |
248 | the archive from which it came.") | |
249 | (put 'package-archive-contents 'risky-local-variable t) | |
44198b6e CY |
250 | |
251 | (defcustom package-user-dir (locate-user-emacs-file "elpa") | |
252 | "Directory containing the user's Emacs Lisp packages. | |
253 | The directory name should be absolute. | |
254 | Apart from this directory, Emacs also looks for system-wide | |
255 | packages in `package-directory-list'." | |
256 | :type 'directory | |
bc44bef7 | 257 | :risky t |
44198b6e CY |
258 | :group 'package |
259 | :version "24.1") | |
260 | ||
261 | (defcustom package-directory-list | |
262 | ;; Defaults are subdirs named "elpa" in the site-lisp dirs. | |
263 | (let (result) | |
264 | (dolist (f load-path) | |
0be01d2c CY |
265 | (and (stringp f) |
266 | (equal (file-name-nondirectory f) "site-lisp") | |
267 | (push (expand-file-name "elpa" f) result))) | |
44198b6e CY |
268 | (nreverse result)) |
269 | "List of additional directories containing Emacs Lisp packages. | |
270 | Each directory name should be absolute. | |
271 | ||
272 | These directories contain packages intended for system-wide; in | |
273 | contrast, `package-user-dir' contains packages for personal use." | |
274 | :type '(repeat directory) | |
bc44bef7 | 275 | :risky t |
44198b6e CY |
276 | :group 'package |
277 | :version "24.1") | |
278 | ||
96ae4c8f CY |
279 | ;; The value is precomputed in finder-inf.el, but don't load that |
280 | ;; until it's needed (i.e. when `package-intialize' is called). | |
281 | (defvar package--builtins nil | |
282 | "Alist of built-in packages. | |
015eea59 CY |
283 | The actual value is initialized by loading the library |
284 | `finder-inf'; this is not done until it is needed, e.g. by the | |
285 | function `package-built-in-p'. | |
286 | ||
96ae4c8f CY |
287 | Each element has the form (PKG . DESC), where PKG is a package |
288 | name (a symbol) and DESC is a vector that describes the package. | |
7ede3b65 CY |
289 | The vector DESC has the form [VERSION-LIST REQS DOCSTRING]. |
290 | VERSION-LIST is a version list. | |
291 | REQS is a list of packages required by the package, each | |
292 | requirement having the form (NAME VL), where NAME is a string | |
293 | and VL is a version list. | |
96ae4c8f | 294 | DOCSTRING is a brief description of the package.") |
bc44bef7 | 295 | (put 'package--builtins 'risky-local-variable t) |
44198b6e | 296 | |
96ae4c8f | 297 | (defvar package-alist nil |
44198b6e | 298 | "Alist of all packages available for activation. |
96ae4c8f CY |
299 | Each element has the form (PKG . DESC), where PKG is a package |
300 | name (a symbol) and DESC is a vector that describes the package. | |
44198b6e | 301 | |
7ede3b65 CY |
302 | The vector DESC has the form [VERSION-LIST REQS DOCSTRING]. |
303 | VERSION-LIST is a version list. | |
304 | REQS is a list of packages required by the package, each | |
305 | requirement having the form (NAME VL) where NAME is a string | |
306 | and VL is a version list. | |
96ae4c8f CY |
307 | DOCSTRING is a brief description of the package. |
308 | ||
309 | This variable is set automatically by `package-load-descriptor', | |
310 | called via `package-initialize'. To change which packages are | |
311 | loaded and/or activated, customize `package-load-list'.") | |
bc44bef7 | 312 | (put 'package-archive-contents 'risky-local-variable t) |
44198b6e | 313 | |
96ae4c8f | 314 | (defvar package-activated-list nil |
44198b6e | 315 | "List of the names of currently activated packages.") |
bc44bef7 | 316 | (put 'package-activated-list 'risky-local-variable t) |
44198b6e CY |
317 | |
318 | (defvar package-obsolete-alist nil | |
319 | "Representation of obsolete packages. | |
320 | Like `package-alist', but maps package name to a second alist. | |
321 | The inner alist is keyed by version.") | |
bc44bef7 | 322 | (put 'package-obsolete-alist 'risky-local-variable t) |
44198b6e | 323 | |
ba08b241 CY |
324 | (defun package-version-join (vlist) |
325 | "Return the version string corresponding to the list VLIST. | |
326 | This is, approximately, the inverse of `version-to-list'. | |
327 | \(Actually, it returns only one of the possible inverses, since | |
328 | `version-to-list' is a many-to-one operation.)" | |
329 | (if (null vlist) | |
330 | "" | |
331 | (let ((str-list (list "." (int-to-string (car vlist))))) | |
332 | (dolist (num (cdr vlist)) | |
333 | (cond | |
334 | ((>= num 0) | |
335 | (push (int-to-string num) str-list) | |
336 | (push "." str-list)) | |
337 | ((< num -3) | |
338 | (error "Invalid version list `%s'" vlist)) | |
339 | (t | |
340 | ;; pre, or beta, or alpha | |
341 | (cond ((equal "." (car str-list)) | |
342 | (pop str-list)) | |
343 | ((not (string-match "[0-9]+" (car str-list))) | |
344 | (error "Invalid version list `%s'" vlist))) | |
345 | (push (cond ((= num -1) "pre") | |
346 | ((= num -2) "beta") | |
347 | ((= num -3) "alpha")) | |
348 | str-list)))) | |
349 | (if (equal "." (car str-list)) | |
350 | (pop str-list)) | |
351 | (apply 'concat (nreverse str-list))))) | |
44198b6e | 352 | |
44198b6e CY |
353 | (defun package-strip-version (dirname) |
354 | "Strip the version from a combined package name and version. | |
355 | E.g., if given \"quux-23.0\", will return \"quux\"" | |
4525ce3e | 356 | (if (string-match (concat "\\`" package-subdirectory-regexp "\\'") dirname) |
44198b6e CY |
357 | (match-string 1 dirname))) |
358 | ||
359 | (defun package-load-descriptor (dir package) | |
4b99edf2 | 360 | "Load the description file in directory DIR for package PACKAGE. |
7ede3b65 CY |
361 | Here, PACKAGE is a string of the form NAME-VERSION, where NAME is |
362 | the package name and VERSION is its version." | |
bc44bef7 PH |
363 | (let* ((pkg-dir (expand-file-name package dir)) |
364 | (pkg-file (expand-file-name | |
365 | (concat (package-strip-version package) "-pkg") | |
366 | pkg-dir))) | |
367 | (when (and (file-directory-p pkg-dir) | |
368 | (file-exists-p (concat pkg-file ".el"))) | |
369 | (load pkg-file nil t)))) | |
44198b6e CY |
370 | |
371 | (defun package-load-all-descriptors () | |
372 | "Load descriptors for installed Emacs Lisp packages. | |
373 | This looks for package subdirectories in `package-user-dir' and | |
374 | `package-directory-list'. The variable `package-load-list' | |
375 | controls which package subdirectories may be loaded. | |
376 | ||
377 | In each valid package subdirectory, this function loads the | |
378 | description file containing a call to `define-package', which | |
379 | updates `package-alist' and `package-obsolete-alist'." | |
380 | (let ((all (memq 'all package-load-list)) | |
4525ce3e | 381 | (regexp (concat "\\`" package-subdirectory-regexp "\\'")) |
44198b6e CY |
382 | name version force) |
383 | (dolist (dir (cons package-user-dir package-directory-list)) | |
384 | (when (file-directory-p dir) | |
385 | (dolist (subdir (directory-files dir)) | |
386 | (when (and (file-directory-p (expand-file-name subdir dir)) | |
4525ce3e | 387 | (string-match regexp subdir)) |
44198b6e CY |
388 | (setq name (intern (match-string 1 subdir)) |
389 | version (match-string 2 subdir) | |
390 | force (assq name package-load-list)) | |
391 | (when (cond | |
392 | ((null force) | |
393 | all) ; not in package-load-list | |
394 | ((null (setq force (cadr force))) | |
395 | nil) ; disabled | |
396 | ((eq force t) | |
397 | t) | |
398 | ((stringp force) ; held | |
148cef8e CY |
399 | (version-list-= (version-to-list version) |
400 | (version-to-list force))) | |
44198b6e CY |
401 | (t |
402 | (error "Invalid element in `package-load-list'"))) | |
403 | (package-load-descriptor dir subdir)))))))) | |
404 | ||
405 | (defsubst package-desc-vers (desc) | |
406 | "Extract version from a package description vector." | |
407 | (aref desc 0)) | |
408 | ||
409 | (defsubst package-desc-reqs (desc) | |
410 | "Extract requirements from a package description vector." | |
411 | (aref desc 1)) | |
412 | ||
413 | (defsubst package-desc-doc (desc) | |
414 | "Extract doc string from a package description vector." | |
415 | (aref desc 2)) | |
416 | ||
417 | (defsubst package-desc-kind (desc) | |
418 | "Extract the kind of download from an archive package description vector." | |
419 | (aref desc 3)) | |
420 | ||
015eea59 CY |
421 | (defun package--dir (name version) |
422 | "Return the directory where a package is installed, or nil if none. | |
423 | NAME and VERSION are both strings." | |
424 | (let* ((subdir (concat name "-" version)) | |
44198b6e | 425 | (dir-list (cons package-user-dir package-directory-list)) |
cced7584 | 426 | pkg-dir) |
44198b6e | 427 | (while dir-list |
cced7584 CY |
428 | (let ((subdir-full (expand-file-name subdir (car dir-list)))) |
429 | (if (file-directory-p subdir-full) | |
430 | (setq pkg-dir subdir-full | |
431 | dir-list nil) | |
44198b6e | 432 | (setq dir-list (cdr dir-list))))) |
cced7584 CY |
433 | pkg-dir)) |
434 | ||
435 | (defun package-activate-1 (package pkg-vec) | |
436 | (let* ((name (symbol-name package)) | |
437 | (version-str (package-version-join (package-desc-vers pkg-vec))) | |
438 | (pkg-dir (package--dir name version-str))) | |
44198b6e | 439 | (unless pkg-dir |
015eea59 | 440 | (error "Internal error: unable to find directory for `%s-%s'" |
cced7584 CY |
441 | name version-str)) |
442 | ;; Add info node. | |
ebf662f4 CY |
443 | (when (file-exists-p (expand-file-name "dir" pkg-dir)) |
444 | ;; FIXME: not the friendliest, but simple. | |
445 | (require 'info) | |
446 | (info-initialize) | |
447 | (push pkg-dir Info-directory-list)) | |
cced7584 | 448 | ;; Add to load path, add autoloads, and activate the package. |
ebf662f4 | 449 | (push pkg-dir load-path) |
cced7584 | 450 | (load (expand-file-name (concat name "-autoloads") pkg-dir) nil t) |
ebf662f4 | 451 | (push package package-activated-list) |
44198b6e CY |
452 | ;; Don't return nil. |
453 | t)) | |
454 | ||
7ede3b65 CY |
455 | (defun package-built-in-p (package &optional min-version) |
456 | "Return true if PACKAGE is built-in to Emacs. | |
457 | Optional arg MIN-VERSION, if non-nil, should be a version list | |
458 | specifying the minimum acceptable version." | |
4b99edf2 | 459 | (require 'finder-inf nil t) ; For `package--builtins'. |
44198b6e | 460 | (let ((elt (assq package package--builtins))) |
512e3ae1 | 461 | (and elt (version-list-<= min-version (package-desc-vers (cdr elt)))))) |
44198b6e | 462 | |
4b99edf2 CY |
463 | ;; This function goes ahead and activates a newer version of a package |
464 | ;; if an older one was already activated. This is not ideal; we'd at | |
465 | ;; least need to check to see if the package has actually been loaded, | |
466 | ;; and not merely activated. | |
7ede3b65 CY |
467 | (defun package-activate (package min-version) |
468 | "Activate package PACKAGE, of version MIN-VERSION or newer. | |
469 | MIN-VERSION should be a version list. | |
4b99edf2 | 470 | If PACKAGE has any dependencies, recursively activate them. |
44198b6e | 471 | Return nil if the package could not be activated." |
4b99edf2 CY |
472 | (let ((pkg-vec (cdr (assq package package-alist))) |
473 | available-version found) | |
474 | ;; Check if PACKAGE is available in `package-alist'. | |
475 | (when pkg-vec | |
476 | (setq available-version (package-desc-vers pkg-vec) | |
7ede3b65 | 477 | found (version-list-<= min-version available-version))) |
4b99edf2 CY |
478 | (cond |
479 | ;; If no such package is found, maybe it's built-in. | |
480 | ((null found) | |
7ede3b65 | 481 | (package-built-in-p package min-version)) |
4b99edf2 CY |
482 | ;; If the package is already activated, just return t. |
483 | ((memq package package-activated-list) | |
484 | t) | |
485 | ;; Otherwise, proceed with activation. | |
486 | (t | |
487 | (let ((fail (catch 'dep-failure | |
488 | ;; Activate its dependencies recursively. | |
489 | (dolist (req (package-desc-reqs pkg-vec)) | |
490 | (unless (package-activate (car req) (cadr req)) | |
491 | (throw 'dep-failure req)))))) | |
492 | (if fail | |
493 | (warn "Unable to activate package `%s'. | |
015eea59 | 494 | Required package `%s-%s' is unavailable" |
4b99edf2 CY |
495 | package (car fail) (package-version-join (cadr fail))) |
496 | ;; If all goes well, activate the package itself. | |
497 | (package-activate-1 package pkg-vec))))))) | |
44198b6e CY |
498 | |
499 | (defun package-mark-obsolete (package pkg-vec) | |
500 | "Put package on the obsolete list, if not already there." | |
501 | (let ((elt (assq package package-obsolete-alist))) | |
502 | (if elt | |
503 | ;; If this obsolete version does not exist in the list, update | |
504 | ;; it the list. | |
505 | (unless (assoc (package-desc-vers pkg-vec) (cdr elt)) | |
506 | (setcdr elt (cons (cons (package-desc-vers pkg-vec) pkg-vec) | |
507 | (cdr elt)))) | |
508 | ;; Make a new association. | |
ebf662f4 CY |
509 | (push (cons package (list (cons (package-desc-vers pkg-vec) |
510 | pkg-vec))) | |
511 | package-obsolete-alist)))) | |
44198b6e | 512 | |
4b99edf2 | 513 | (defun define-package (name-string version-string |
187d3296 CY |
514 | &optional docstring requirements |
515 | &rest extra-properties) | |
44198b6e | 516 | "Define a new package. |
4b99edf2 | 517 | NAME-STRING is the name of the package, as a string. |
7ede3b65 | 518 | VERSION-STRING is the version of the package, as a string. |
4b99edf2 CY |
519 | DOCSTRING is a short description of the package, a string. |
520 | REQUIREMENTS is a list of dependencies on other packages. | |
7ede3b65 CY |
521 | Each requirement is of the form (OTHER-PACKAGE OTHER-VERSION), |
522 | where OTHER-VERSION is a string. | |
187d3296 CY |
523 | |
524 | EXTRA-PROPERTIES is currently unused." | |
4b99edf2 CY |
525 | (let* ((name (intern name-string)) |
526 | (version (version-to-list version-string)) | |
44198b6e CY |
527 | (new-pkg-desc |
528 | (cons name | |
4b99edf2 | 529 | (vector version |
44198b6e CY |
530 | (mapcar |
531 | (lambda (elt) | |
532 | (list (car elt) | |
148cef8e | 533 | (version-to-list (car (cdr elt))))) |
44198b6e | 534 | requirements) |
4b99edf2 CY |
535 | docstring))) |
536 | (old-pkg (assq name package-alist))) | |
537 | (cond | |
538 | ;; If there's no old package, just add this to `package-alist'. | |
539 | ((null old-pkg) | |
540 | (push new-pkg-desc package-alist)) | |
541 | ((version-list-< (package-desc-vers (cdr old-pkg)) version) | |
542 | ;; Remove the old package and declare it obsolete. | |
543 | (package-mark-obsolete name (cdr old-pkg)) | |
544 | (setq package-alist (cons new-pkg-desc | |
545 | (delq old-pkg package-alist)))) | |
546 | ;; You can have two packages with the same version, e.g. one in | |
547 | ;; the system package directory and one in your private | |
548 | ;; directory. We just let the first one win. | |
549 | ((not (version-list-= (package-desc-vers (cdr old-pkg)) version)) | |
550 | ;; The package is born obsolete. | |
551 | (package-mark-obsolete name (cdr new-pkg-desc)))))) | |
44198b6e CY |
552 | |
553 | ;; From Emacs 22. | |
554 | (defun package-autoload-ensure-default-file (file) | |
555 | "Make sure that the autoload file FILE exists and if not create it." | |
556 | (unless (file-exists-p file) | |
557 | (write-region | |
558 | (concat ";;; " (file-name-nondirectory file) | |
559 | " --- automatically extracted autoloads\n" | |
560 | ";;\n" | |
561 | ";;; Code:\n\n" | |
562 | "\f\n;; Local Variables:\n" | |
563 | ";; version-control: never\n" | |
564 | ";; no-byte-compile: t\n" | |
565 | ";; no-update-autoloads: t\n" | |
566 | ";; End:\n" | |
567 | ";;; " (file-name-nondirectory file) | |
568 | " ends here\n") | |
569 | nil file)) | |
570 | file) | |
571 | ||
572 | (defun package-generate-autoloads (name pkg-dir) | |
9a70f03d | 573 | (require 'autoload) ;Load before we let-bind generated-autoload-file! |
44198b6e CY |
574 | (let* ((auto-name (concat name "-autoloads.el")) |
575 | (ignore-name (concat name "-pkg.el")) | |
576 | (generated-autoload-file (expand-file-name auto-name pkg-dir)) | |
577 | (version-control 'never)) | |
44198b6e CY |
578 | (unless (fboundp 'autoload-ensure-default-file) |
579 | (package-autoload-ensure-default-file generated-autoload-file)) | |
580 | (update-directory-autoloads pkg-dir))) | |
581 | ||
4525ce3e CY |
582 | (defvar tar-parse-info) |
583 | (declare-function tar-untar-buffer "tar-mode" ()) | |
584 | ||
585 | (defun package-untar-buffer (dir) | |
44198b6e | 586 | "Untar the current buffer. |
4525ce3e CY |
587 | This uses `tar-untar-buffer' from Tar mode. All files should |
588 | untar into a directory named DIR; otherwise, signal an error." | |
44198b6e | 589 | (require 'tar-mode) |
4525ce3e CY |
590 | (tar-mode) |
591 | ;; Make sure everything extracts into DIR. | |
592 | (let ((regexp (concat "\\`" (regexp-quote dir) "/"))) | |
593 | (dolist (tar-data tar-parse-info) | |
594 | (unless (string-match regexp (aref tar-data 2)) | |
595 | (error "Package does not untar cleanly into directory %s/" dir)))) | |
596 | (tar-untar-buffer)) | |
44198b6e CY |
597 | |
598 | (defun package-unpack (name version) | |
4525ce3e CY |
599 | (let* ((dirname (concat (symbol-name name) "-" version)) |
600 | (pkg-dir (expand-file-name dirname package-user-dir))) | |
44198b6e | 601 | (make-directory package-user-dir t) |
015eea59 | 602 | ;; FIXME: should we delete PKG-DIR if it exists? |
44198b6e | 603 | (let* ((default-directory (file-name-as-directory package-user-dir))) |
4525ce3e | 604 | (package-untar-buffer dirname) |
44198b6e CY |
605 | (package-generate-autoloads (symbol-name name) pkg-dir) |
606 | (let ((load-path (cons pkg-dir load-path))) | |
607 | (byte-recompile-directory pkg-dir 0 t))))) | |
608 | ||
c6affbde | 609 | (defun package--write-file-no-coding (file-name) |
bc44bef7 | 610 | (let ((buffer-file-coding-system 'no-conversion)) |
c6affbde | 611 | (write-region (point-min) (point-max) file-name))) |
bc44bef7 | 612 | |
44198b6e CY |
613 | (defun package-unpack-single (file-name version desc requires) |
614 | "Install the contents of the current buffer as a package." | |
615 | ;; Special case "package". | |
616 | (if (string= file-name "package") | |
bc44bef7 | 617 | (package--write-file-no-coding |
c6affbde | 618 | (expand-file-name (concat file-name ".el") package-user-dir)) |
ba08b241 CY |
619 | (let* ((pkg-dir (expand-file-name (concat file-name "-" |
620 | (package-version-join | |
621 | (version-to-list version))) | |
44198b6e CY |
622 | package-user-dir)) |
623 | (el-file (expand-file-name (concat file-name ".el") pkg-dir)) | |
624 | (pkg-file (expand-file-name (concat file-name "-pkg.el") pkg-dir))) | |
625 | (make-directory pkg-dir t) | |
c6affbde | 626 | (package--write-file-no-coding el-file) |
44198b6e CY |
627 | (let ((print-level nil) |
628 | (print-length nil)) | |
629 | (write-region | |
630 | (concat | |
631 | (prin1-to-string | |
632 | (list 'define-package | |
633 | file-name | |
634 | version | |
635 | desc | |
636 | (list 'quote | |
637 | ;; Turn version lists into string form. | |
638 | (mapcar | |
639 | (lambda (elt) | |
640 | (list (car elt) | |
015eea59 | 641 | (package-version-join (cadr elt)))) |
44198b6e CY |
642 | requires)))) |
643 | "\n") | |
644 | nil | |
645 | pkg-file | |
646 | nil nil nil 'excl)) | |
647 | (package-generate-autoloads file-name pkg-dir) | |
648 | (let ((load-path (cons pkg-dir load-path))) | |
649 | (byte-recompile-directory pkg-dir 0 t))))) | |
650 | ||
f561e49a CY |
651 | (defmacro package--with-work-buffer (location file &rest body) |
652 | "Run BODY in a buffer containing the contents of FILE at LOCATION. | |
653 | LOCATION is the base location of a package archive, and should be | |
654 | one of the URLs (or file names) specified in `package-archives'. | |
655 | FILE is the name of a file relative to that base location. | |
656 | ||
657 | This macro retrieves FILE from LOCATION into a temporary buffer, | |
658 | and evaluates BODY while that buffer is current. This work | |
659 | buffer is killed afterwards. Return the last value in BODY." | |
da91b5f2 | 660 | `(let* ((http (string-match "\\`https?:" ,location)) |
f561e49a CY |
661 | (buffer |
662 | (if http | |
663 | (url-retrieve-synchronously (concat ,location ,file)) | |
664 | (generate-new-buffer "*package work buffer*")))) | |
665 | (prog1 | |
666 | (with-current-buffer buffer | |
667 | (if http | |
668 | (progn (package-handle-response) | |
669 | (re-search-forward "^$" nil 'move) | |
670 | (forward-char) | |
671 | (delete-region (point-min) (point))) | |
672 | (unless (file-name-absolute-p ,location) | |
673 | (error "Archive location %s is not an absolute file name" | |
674 | ,location)) | |
675 | (insert-file-contents (expand-file-name ,file ,location))) | |
676 | ,@body) | |
677 | (kill-buffer buffer)))) | |
678 | ||
44198b6e | 679 | (defun package-handle-response () |
f561e49a | 680 | "Handle the response from a `url-retrieve-synchronously' call. |
44198b6e CY |
681 | Parse the HTTP response and throw if an error occurred. |
682 | The url package seems to require extra processing for this. | |
683 | This should be called in a `save-excursion', in the download buffer. | |
684 | It will move point to somewhere in the headers." | |
685 | ;; We assume HTTP here. | |
686 | (require 'url-http) | |
687 | (let ((response (url-http-parse-response))) | |
688 | (when (or (< response 200) (>= response 300)) | |
44198b6e CY |
689 | (error "Error during download request:%s" |
690 | (buffer-substring-no-properties (point) (progn | |
691 | (end-of-line) | |
692 | (point))))))) | |
693 | ||
694 | (defun package-download-single (name version desc requires) | |
695 | "Download and install a single-file package." | |
f561e49a CY |
696 | (let ((location (package-archive-base name)) |
697 | (file (concat (symbol-name name) "-" version ".el"))) | |
698 | (package--with-work-buffer location file | |
699 | (package-unpack-single (symbol-name name) version desc requires)))) | |
44198b6e CY |
700 | |
701 | (defun package-download-tar (name version) | |
702 | "Download and install a tar package." | |
f561e49a CY |
703 | (let ((location (package-archive-base name)) |
704 | (file (concat (symbol-name name) "-" version ".tar"))) | |
705 | (package--with-work-buffer location file | |
706 | (package-unpack name version)))) | |
44198b6e | 707 | |
bc44bef7 | 708 | (defun package-installed-p (package &optional min-version) |
7ede3b65 CY |
709 | "Return true if PACKAGE, of MIN-VERSION or newer, is installed. |
710 | MIN-VERSION should be a version list." | |
44198b6e | 711 | (let ((pkg-desc (assq package package-alist))) |
4b99edf2 CY |
712 | (if pkg-desc |
713 | (version-list-<= min-version | |
714 | (package-desc-vers (cdr pkg-desc))) | |
715 | ;; Also check built-in packages. | |
716 | (package-built-in-p package min-version)))) | |
44198b6e | 717 | |
96ae4c8f CY |
718 | (defun package-compute-transaction (package-list requirements) |
719 | "Return a list of packages to be installed, including PACKAGE-LIST. | |
720 | PACKAGE-LIST should be a list of package names (symbols). | |
721 | ||
722 | REQUIREMENTS should be a list of additional requirements; each | |
7ede3b65 CY |
723 | element in this list should have the form (PACKAGE VERSION-LIST), |
724 | where PACKAGE is a package name and VERSION-LIST is the required | |
725 | version of that package. | |
96ae4c8f CY |
726 | |
727 | This function recursively computes the requirements of the | |
728 | packages in REQUIREMENTS, and returns a list of all the packages | |
729 | that must be installed. Packages that are already installed are | |
730 | not included in this list." | |
44198b6e CY |
731 | (dolist (elt requirements) |
732 | (let* ((next-pkg (car elt)) | |
733 | (next-version (cadr elt))) | |
734 | (unless (package-installed-p next-pkg next-version) | |
735 | ;; A package is required, but not installed. It might also be | |
736 | ;; blocked via `package-load-list'. | |
737 | (let ((pkg-desc (assq next-pkg package-archive-contents)) | |
738 | hold) | |
739 | (when (setq hold (assq next-pkg package-load-list)) | |
740 | (setq hold (cadr hold)) | |
741 | (cond ((eq hold nil) | |
742 | (error "Required package '%s' is disabled" | |
743 | (symbol-name next-pkg))) | |
744 | ((null (stringp hold)) | |
745 | (error "Invalid element in `package-load-list'")) | |
148cef8e | 746 | ((version-list-< (version-to-list hold) next-version) |
015eea59 | 747 | (error "Package `%s' held at version %s, \ |
44198b6e CY |
748 | but version %s required" |
749 | (symbol-name next-pkg) hold | |
750 | (package-version-join next-version))))) | |
751 | (unless pkg-desc | |
015eea59 | 752 | (error "Package `%s-%s' is unavailable" |
4b99edf2 CY |
753 | (symbol-name next-pkg) |
754 | (package-version-join next-version))) | |
148cef8e CY |
755 | (unless (version-list-<= next-version |
756 | (package-desc-vers (cdr pkg-desc))) | |
44198b6e | 757 | (error |
015eea59 | 758 | "Need package `%s-%s', but only %s is available" |
44198b6e CY |
759 | (symbol-name next-pkg) (package-version-join next-version) |
760 | (package-version-join (package-desc-vers (cdr pkg-desc))))) | |
761 | ;; Only add to the transaction if we don't already have it. | |
96ae4c8f | 762 | (unless (memq next-pkg package-list) |
ebf662f4 | 763 | (push next-pkg package-list)) |
96ae4c8f CY |
764 | (setq package-list |
765 | (package-compute-transaction package-list | |
44198b6e CY |
766 | (package-desc-reqs |
767 | (cdr pkg-desc)))))))) | |
96ae4c8f | 768 | package-list) |
44198b6e CY |
769 | |
770 | (defun package-read-from-string (str) | |
771 | "Read a Lisp expression from STR. | |
772 | Signal an error if the entire string was not used." | |
773 | (let* ((read-data (read-from-string str)) | |
187d3296 CY |
774 | (more-left |
775 | (condition-case nil | |
776 | ;; The call to `ignore' suppresses a compiler warning. | |
777 | (progn (ignore (read-from-string | |
778 | (substring str (cdr read-data)))) | |
779 | t) | |
780 | (end-of-file nil)))) | |
44198b6e CY |
781 | (if more-left |
782 | (error "Can't read whole string") | |
783 | (car read-data)))) | |
784 | ||
785 | (defun package--read-archive-file (file) | |
786 | "Re-read archive file FILE, if it exists. | |
787 | Will return the data from the file, or nil if the file does not exist. | |
788 | Will throw an error if the archive version is too new." | |
789 | (let ((filename (expand-file-name file package-user-dir))) | |
187d3296 CY |
790 | (when (file-exists-p filename) |
791 | (with-temp-buffer | |
792 | (insert-file-contents-literally filename) | |
793 | (let ((contents (read (current-buffer)))) | |
794 | (if (> (car contents) package-archive-version) | |
795 | (error "Package archive version %d is higher than %d" | |
796 | (car contents) package-archive-version)) | |
797 | (cdr contents)))))) | |
44198b6e | 798 | |
bc44bef7 | 799 | (defun package-read-all-archive-contents () |
96ae4c8f CY |
800 | "Re-read `archive-contents', if it exists. |
801 | If successful, set `package-archive-contents'." | |
fbe3be3f | 802 | (setq package-archive-contents nil) |
bc44bef7 | 803 | (dolist (archive package-archives) |
96ae4c8f | 804 | (package-read-archive-contents (car archive)))) |
44198b6e | 805 | |
bc44bef7 | 806 | (defun package-read-archive-contents (archive) |
187d3296 CY |
807 | "Re-read archive contents for ARCHIVE. |
808 | If successful, set the variable `package-archive-contents'. | |
bc44bef7 | 809 | If the archive version is too new, signal an error." |
187d3296 CY |
810 | ;; Version 1 of 'archive-contents' is identical to our internal |
811 | ;; representation. | |
812 | (let* ((dir (concat "archives/" archive)) | |
813 | (contents-file (concat dir "/archive-contents")) | |
814 | contents) | |
815 | (when (setq contents (package--read-archive-file contents-file)) | |
816 | (dolist (package contents) | |
817 | (package--add-to-archive-contents package archive))))) | |
bc44bef7 PH |
818 | |
819 | (defun package--add-to-archive-contents (package archive) | |
820 | "Add the PACKAGE from the given ARCHIVE if necessary. | |
821 | Also, add the originating archive to the end of the package vector." | |
822 | (let* ((name (car package)) | |
823 | (version (aref (cdr package) 0)) | |
824 | (entry (cons (car package) | |
825 | (vconcat (cdr package) (vector archive)))) | |
826 | (existing-package (cdr (assq name package-archive-contents)))) | |
827 | (when (or (not existing-package) | |
148cef8e | 828 | (version-list-< (aref existing-package 0) version)) |
bc44bef7 PH |
829 | (add-to-list 'package-archive-contents entry)))) |
830 | ||
96ae4c8f CY |
831 | (defun package-download-transaction (package-list) |
832 | "Download and install all the packages in PACKAGE-LIST. | |
833 | PACKAGE-LIST should be a list of package names (symbols). | |
834 | This function assumes that all package requirements in | |
835 | PACKAGE-LIST are satisfied, i.e. that PACKAGE-LIST is computed | |
836 | using `package-compute-transaction'." | |
837 | (dolist (elt package-list) | |
44198b6e CY |
838 | (let* ((desc (cdr (assq elt package-archive-contents))) |
839 | ;; As an exception, if package is "held" in | |
840 | ;; `package-load-list', download the held version. | |
841 | (hold (cadr (assq elt package-load-list))) | |
842 | (v-string (or (and (stringp hold) hold) | |
843 | (package-version-join (package-desc-vers desc)))) | |
844 | (kind (package-desc-kind desc))) | |
845 | (cond | |
846 | ((eq kind 'tar) | |
847 | (package-download-tar elt v-string)) | |
848 | ((eq kind 'single) | |
849 | (package-download-single elt v-string | |
850 | (package-desc-doc desc) | |
851 | (package-desc-reqs desc))) | |
852 | (t | |
853 | (error "Unknown package kind: %s" (symbol-name kind))))))) | |
854 | ||
855 | ;;;###autoload | |
856 | (defun package-install (name) | |
857 | "Install the package named NAME. | |
858 | Interactively, prompt for the package name. | |
063e5294 | 859 | The package is found on one of the archives in `package-archives'." |
44198b6e | 860 | (interactive |
bc44bef7 PH |
861 | (list (intern (completing-read "Install package: " |
862 | (mapcar (lambda (elt) | |
863 | (cons (symbol-name (car elt)) | |
864 | nil)) | |
865 | package-archive-contents) | |
866 | nil t)))) | |
44198b6e CY |
867 | (let ((pkg-desc (assq name package-archive-contents))) |
868 | (unless pkg-desc | |
015eea59 | 869 | (error "Package `%s' is not available for installation" |
44198b6e | 870 | (symbol-name name))) |
bc44bef7 PH |
871 | (package-download-transaction |
872 | (package-compute-transaction (list name) | |
873 | (package-desc-reqs (cdr pkg-desc))))) | |
44198b6e CY |
874 | ;; Try to activate it. |
875 | (package-initialize)) | |
876 | ||
ffbf300e CY |
877 | (defun package-strip-rcs-id (str) |
878 | "Strip RCS version ID from the version string STR. | |
44198b6e CY |
879 | If the result looks like a dotted numeric version, return it. |
880 | Otherwise return nil." | |
ffbf300e CY |
881 | (when str |
882 | (when (string-match "\\`[ \t]*[$]Revision:[ \t]+" str) | |
883 | (setq str (substring str (match-end 0)))) | |
884 | (condition-case nil | |
885 | (if (version-to-list str) | |
886 | str) | |
887 | (error nil)))) | |
44198b6e CY |
888 | |
889 | (defun package-buffer-info () | |
187d3296 CY |
890 | "Return a vector describing the package in the current buffer. |
891 | The vector has the form | |
892 | ||
893 | [FILENAME REQUIRES DESCRIPTION VERSION COMMENTARY] | |
894 | ||
895 | FILENAME is the file name, a string, sans the \".el\" extension. | |
7ede3b65 CY |
896 | REQUIRES is a list of requirements, each requirement having the |
897 | form (NAME VER); NAME is a string and VER is a version list. | |
187d3296 | 898 | DESCRIPTION is the package description, a string. |
44198b6e CY |
899 | VERSION is the version, a string. |
900 | COMMENTARY is the commentary section, a string, or nil if none. | |
187d3296 CY |
901 | |
902 | If the buffer does not contain a conforming package, signal an | |
903 | error. If there is a package, narrow the buffer to the file's | |
904 | boundaries." | |
44198b6e | 905 | (goto-char (point-min)) |
187d3296 CY |
906 | (unless (re-search-forward "^;;; \\([^ ]*\\)\\.el --- \\(.*\\)$" nil t) |
907 | (error "Packages lacks a file header")) | |
908 | (let ((file-name (match-string-no-properties 1)) | |
909 | (desc (match-string-no-properties 2)) | |
910 | (start (line-beginning-position))) | |
911 | (unless (search-forward (concat ";;; " file-name ".el ends here")) | |
912 | (error "Package lacks a terminating comment")) | |
913 | ;; Try to include a trailing newline. | |
914 | (forward-line) | |
915 | (narrow-to-region start (point)) | |
916 | (require 'lisp-mnt) | |
917 | ;; Use some headers we've invented to drive the process. | |
918 | (let* ((requires-str (lm-header "package-requires")) | |
919 | (requires (if requires-str | |
920 | (package-read-from-string requires-str))) | |
921 | ;; Prefer Package-Version; if defined, the package author | |
922 | ;; probably wants us to use it. Otherwise try Version. | |
923 | (pkg-version | |
924 | (or (package-strip-rcs-id (lm-header "package-version")) | |
925 | (package-strip-rcs-id (lm-header "version")))) | |
926 | (commentary (lm-commentary))) | |
927 | (unless pkg-version | |
928 | (error | |
929 | "Package lacks a \"Version\" or \"Package-Version\" header")) | |
930 | ;; Turn string version numbers into list form. | |
931 | (setq requires | |
932 | (mapcar | |
933 | (lambda (elt) | |
934 | (list (car elt) | |
935 | (version-to-list (car (cdr elt))))) | |
936 | requires)) | |
937 | (vector file-name requires desc pkg-version commentary)))) | |
44198b6e CY |
938 | |
939 | (defun package-tar-file-info (file) | |
940 | "Find package information for a tar file. | |
941 | FILE is the name of the tar file to examine. | |
942 | The return result is a vector like `package-buffer-info'." | |
b511b994 MA |
943 | (let ((default-directory (file-name-directory file)) |
944 | (file (file-name-nondirectory file))) | |
4525ce3e CY |
945 | (unless (string-match (concat "\\`" package-subdirectory-regexp "\\.tar\\'") |
946 | file) | |
b511b994 MA |
947 | (error "Invalid package name `%s'" file)) |
948 | (let* ((pkg-name (match-string-no-properties 1 file)) | |
949 | (pkg-version (match-string-no-properties 2 file)) | |
950 | ;; Extract the package descriptor. | |
951 | (pkg-def-contents (shell-command-to-string | |
952 | ;; Requires GNU tar. | |
953 | (concat "tar -xOf " file " " | |
954 | ||
955 | pkg-name "-" pkg-version "/" | |
956 | pkg-name "-pkg.el"))) | |
957 | (pkg-def-parsed (package-read-from-string pkg-def-contents))) | |
958 | (unless (eq (car pkg-def-parsed) 'define-package) | |
959 | (error "No `define-package' sexp is present in `%s-pkg.el'" pkg-name)) | |
960 | (let ((name-str (nth 1 pkg-def-parsed)) | |
961 | (version-string (nth 2 pkg-def-parsed)) | |
962 | (docstring (nth 3 pkg-def-parsed)) | |
963 | (requires (nth 4 pkg-def-parsed)) | |
964 | (readme (shell-command-to-string | |
965 | ;; Requires GNU tar. | |
966 | (concat "tar -xOf " file " " | |
967 | pkg-name "-" pkg-version "/README")))) | |
968 | (unless (equal pkg-version version-string) | |
969 | (error "Package has inconsistent versions")) | |
970 | (unless (equal pkg-name name-str) | |
971 | (error "Package has inconsistent names")) | |
972 | ;; Kind of a hack. | |
973 | (if (string-match ": Not found in archive" readme) | |
974 | (setq readme nil)) | |
975 | ;; Turn string version numbers into list form. | |
976 | (if (eq (car requires) 'quote) | |
977 | (setq requires (car (cdr requires)))) | |
978 | (setq requires | |
979 | (mapcar (lambda (elt) | |
980 | (list (car elt) | |
981 | (version-to-list (cadr elt)))) | |
982 | requires)) | |
983 | (vector pkg-name requires docstring version-string readme))))) | |
44198b6e | 984 | |
187d3296 CY |
985 | ;;;###autoload |
986 | (defun package-install-from-buffer (pkg-info type) | |
987 | "Install a package from the current buffer. | |
988 | When called interactively, the current buffer is assumed to be a | |
989 | single .el file that follows the packaging guidelines; see info | |
990 | node `(elisp)Packaging'. | |
991 | ||
992 | When called from Lisp, PKG-INFO is a vector describing the | |
993 | information, of the type returned by `package-buffer-info'; and | |
994 | TYPE is the package type (either `single' or `tar')." | |
995 | (interactive (list (package-buffer-info) 'single)) | |
44198b6e CY |
996 | (save-excursion |
997 | (save-restriction | |
998 | (let* ((file-name (aref pkg-info 0)) | |
187d3296 | 999 | (requires (aref pkg-info 1)) |
44198b6e CY |
1000 | (desc (if (string= (aref pkg-info 2) "") |
1001 | "No description available." | |
1002 | (aref pkg-info 2))) | |
1003 | (pkg-version (aref pkg-info 3))) | |
1004 | ;; Download and install the dependencies. | |
1005 | (let ((transaction (package-compute-transaction nil requires))) | |
1006 | (package-download-transaction transaction)) | |
1007 | ;; Install the package itself. | |
1008 | (cond | |
1009 | ((eq type 'single) | |
1010 | (package-unpack-single file-name pkg-version desc requires)) | |
1011 | ((eq type 'tar) | |
1012 | (package-unpack (intern file-name) pkg-version)) | |
1013 | (t | |
1014 | (error "Unknown type: %s" (symbol-name type)))) | |
1015 | ;; Try to activate it. | |
1016 | (package-initialize))))) | |
1017 | ||
44198b6e CY |
1018 | ;;;###autoload |
1019 | (defun package-install-file (file) | |
1020 | "Install a package from a file. | |
1021 | The file can either be a tar file or an Emacs Lisp file." | |
1022 | (interactive "fPackage file name: ") | |
1023 | (with-temp-buffer | |
1024 | (insert-file-contents-literally file) | |
1025 | (cond | |
187d3296 CY |
1026 | ((string-match "\\.el$" file) |
1027 | (package-install-from-buffer (package-buffer-info) 'single)) | |
44198b6e | 1028 | ((string-match "\\.tar$" file) |
187d3296 | 1029 | (package-install-from-buffer (package-tar-file-info file) 'tar)) |
44198b6e CY |
1030 | (t (error "Unrecognized extension `%s'" (file-name-extension file)))))) |
1031 | ||
1032 | (defun package-delete (name version) | |
015eea59 CY |
1033 | (let ((dir (package--dir name version))) |
1034 | (if (string-equal (file-name-directory dir) | |
1035 | (file-name-as-directory | |
1036 | (expand-file-name package-user-dir))) | |
1037 | (progn | |
1038 | (delete-directory dir t t) | |
1039 | (message "Package `%s-%s' deleted." name version)) | |
1040 | ;; Don't delete "system" packages | |
1041 | (error "Package `%s-%s' is a system package, not deleting" | |
1042 | name version)))) | |
44198b6e | 1043 | |
f561e49a | 1044 | (defun package-archive-base (name) |
bc44bef7 PH |
1045 | "Return the archive containing the package NAME." |
1046 | (let ((desc (cdr (assq (intern-soft name) package-archive-contents)))) | |
1047 | (cdr (assoc (aref desc (- (length desc) 1)) package-archives)))) | |
1048 | ||
1049 | (defun package--download-one-archive (archive file) | |
f561e49a CY |
1050 | "Retrieve an archive file FILE from ARCHIVE, and cache it. |
1051 | ARCHIVE should be a cons cell of the form (NAME . LOCATION), | |
1052 | similar to an entry in `package-alist'. Save the cached copy to | |
1053 | \"archives/NAME/archive-contents\" in `package-user-dir'." | |
1054 | (let* ((dir (expand-file-name "archives" package-user-dir)) | |
1055 | (dir (expand-file-name (car archive) dir))) | |
1056 | (package--with-work-buffer (cdr archive) file | |
ebf662f4 CY |
1057 | ;; Read the retrieved buffer to make sure it is valid (e.g. it |
1058 | ;; may fetch a URL redirect page). | |
1059 | (when (listp (read buffer)) | |
1060 | (make-directory dir t) | |
1061 | (setq buffer-file-name (expand-file-name file dir)) | |
1062 | (let ((version-control 'never)) | |
f561e49a | 1063 | (save-buffer)))))) |
44198b6e CY |
1064 | |
1065 | (defun package-refresh-contents () | |
1066 | "Download the ELPA archive description if needed. | |
ebf662f4 CY |
1067 | This informs Emacs about the latest versions of all packages, and |
1068 | makes them available for download." | |
44198b6e CY |
1069 | (interactive) |
1070 | (unless (file-exists-p package-user-dir) | |
1071 | (make-directory package-user-dir t)) | |
bc44bef7 | 1072 | (dolist (archive package-archives) |
515de2e3 | 1073 | (condition-case-no-debug nil |
cb6c4991 | 1074 | (package--download-one-archive archive "archive-contents") |
187d3296 | 1075 | (error (message "Failed to download `%s' archive." |
cb6c4991 | 1076 | (car archive))))) |
bc44bef7 | 1077 | (package-read-all-archive-contents)) |
44198b6e | 1078 | |
4b99edf2 CY |
1079 | (defvar package--initialized nil) |
1080 | ||
44198b6e | 1081 | ;;;###autoload |
4b99edf2 | 1082 | (defun package-initialize (&optional no-activate) |
44198b6e | 1083 | "Load Emacs Lisp packages, and activate them. |
4b99edf2 CY |
1084 | The variable `package-load-list' controls which packages to load. |
1085 | If optional arg NO-ACTIVATE is non-nil, don't activate packages." | |
44198b6e | 1086 | (interactive) |
015eea59 CY |
1087 | (setq package-alist nil |
1088 | package-obsolete-alist nil) | |
44198b6e | 1089 | (package-load-all-descriptors) |
bc44bef7 | 1090 | (package-read-all-archive-contents) |
4b99edf2 CY |
1091 | (unless no-activate |
1092 | (dolist (elt package-alist) | |
1093 | (package-activate (car elt) (package-desc-vers (cdr elt))))) | |
1094 | (setq package--initialized t)) | |
44198b6e CY |
1095 | |
1096 | \f | |
cced7584 | 1097 | ;;;; Package description buffer. |
44198b6e | 1098 | |
cced7584 CY |
1099 | ;;;###autoload |
1100 | (defun describe-package (package) | |
1101 | "Display the full documentation of PACKAGE (a symbol)." | |
1102 | (interactive | |
8a500a91 CY |
1103 | (let* ((guess (function-called-at-point)) |
1104 | packages val) | |
4b99edf2 CY |
1105 | (require 'finder-inf nil t) |
1106 | ;; Load the package list if necessary (but don't activate them). | |
1107 | (unless package--initialized | |
1108 | (package-initialize t)) | |
8a500a91 | 1109 | (setq packages (append (mapcar 'car package-alist) |
4b99edf2 CY |
1110 | (mapcar 'car package-archive-contents) |
1111 | (mapcar 'car package--builtins))) | |
cced7584 CY |
1112 | (unless (memq guess packages) |
1113 | (setq guess nil)) | |
1114 | (setq packages (mapcar 'symbol-name packages)) | |
1115 | (setq val | |
1116 | (completing-read (if guess | |
1117 | (format "Describe package (default %s): " | |
1118 | guess) | |
1119 | "Describe package: ") | |
1120 | packages nil t nil nil guess)) | |
cb6c4991 | 1121 | (list (if (equal val "") guess (intern val))))) |
4b99edf2 CY |
1122 | (if (or (null package) (not (symbolp package))) |
1123 | (message "No package specified") | |
cced7584 CY |
1124 | (help-setup-xref (list #'describe-package package) |
1125 | (called-interactively-p 'interactive)) | |
1126 | (with-help-window (help-buffer) | |
1127 | (with-current-buffer standard-output | |
1128 | (describe-package-1 package))))) | |
1129 | ||
1130 | (defun describe-package-1 (package) | |
96ae4c8f | 1131 | (require 'lisp-mnt) |
cb6c4991 CY |
1132 | (let ((package-name (symbol-name package)) |
1133 | (built-in (assq package package--builtins)) | |
1134 | desc pkg-dir reqs version installable) | |
cced7584 CY |
1135 | (prin1 package) |
1136 | (princ " is ") | |
4b99edf2 CY |
1137 | (cond |
1138 | ;; Loaded packages are in `package-alist'. | |
1139 | ((setq desc (cdr (assq package package-alist))) | |
1140 | (setq version (package-version-join (package-desc-vers desc))) | |
1141 | (if (setq pkg-dir (package--dir package-name version)) | |
1142 | (insert "an installed package.\n\n") | |
1143 | ;; This normally does not happen. | |
1144 | (insert "a deleted package.\n\n"))) | |
1145 | ;; Available packages are in `package-archive-contents'. | |
1146 | ((setq desc (cdr (assq package package-archive-contents))) | |
1147 | (setq version (package-version-join (package-desc-vers desc)) | |
8adb4c33 | 1148 | installable t) |
4b99edf2 CY |
1149 | (if built-in |
1150 | (insert "a built-in package.\n\n") | |
1151 | (insert "an uninstalled package.\n\n"))) | |
1152 | (built-in | |
1153 | (setq desc (cdr built-in) | |
1154 | version (package-version-join (package-desc-vers desc))) | |
1155 | (insert "a built-in package.\n\n")) | |
1156 | (t | |
1157 | (insert "an orphan package.\n\n"))) | |
cb6c4991 | 1158 | |
96ae4c8f | 1159 | (insert " " (propertize "Status" 'font-lock-face 'bold) ": ") |
cb6c4991 | 1160 | (cond (pkg-dir |
96ae4c8f CY |
1161 | (insert (propertize "Installed" |
1162 | 'font-lock-face 'font-lock-comment-face)) | |
cb6c4991 CY |
1163 | (insert " in `") |
1164 | ;; Todo: Add button for uninstalling. | |
1165 | (help-insert-xref-button (file-name-as-directory pkg-dir) | |
1166 | 'help-package-def pkg-dir) | |
4b99edf2 CY |
1167 | (if built-in |
1168 | (insert "',\n shadowing a " | |
1169 | (propertize "built-in package" | |
1170 | 'font-lock-face 'font-lock-builtin-face) | |
1171 | ".") | |
1172 | (insert "'."))) | |
cb6c4991 | 1173 | (installable |
4b99edf2 CY |
1174 | (if built-in |
1175 | (insert (propertize "Built-in." 'font-lock-face 'font-lock-builtin-face) | |
1176 | " Alternate version available -- ") | |
1177 | (insert "Available -- ")) | |
1178 | (let ((button-text (if (display-graphic-p) "Install" "[Install]")) | |
cb6c4991 CY |
1179 | (button-face (if (display-graphic-p) |
1180 | '(:box (:line-width 2 :color "dark grey") | |
1181 | :background "light grey" | |
1182 | :foreground "black") | |
1183 | 'link))) | |
4b99edf2 | 1184 | (insert-text-button button-text 'face button-face 'follow-link t |
cb6c4991 CY |
1185 | 'package-symbol package |
1186 | 'action 'package-install-button-action))) | |
1187 | (built-in | |
4b99edf2 | 1188 | (insert (propertize "Built-in." 'font-lock-face 'font-lock-builtin-face))) |
cb6c4991 CY |
1189 | (t (insert "Deleted."))) |
1190 | (insert "\n") | |
4b99edf2 | 1191 | (and version (> (length version) 0) |
96ae4c8f CY |
1192 | (insert " " |
1193 | (propertize "Version" 'font-lock-face 'bold) ": " version "\n")) | |
4b99edf2 CY |
1194 | |
1195 | (setq reqs (if desc (package-desc-reqs desc))) | |
8adb4c33 | 1196 | (when reqs |
96ae4c8f | 1197 | (insert " " (propertize "Requires" 'font-lock-face 'bold) ": ") |
8adb4c33 CY |
1198 | (let ((first t) |
1199 | name vers text) | |
1200 | (dolist (req reqs) | |
1201 | (setq name (car req) | |
1202 | vers (cadr req) | |
1203 | text (format "%s-%s" (symbol-name name) | |
1204 | (package-version-join vers))) | |
1205 | (cond (first (setq first nil)) | |
1206 | ((>= (+ 2 (current-column) (length text)) | |
1207 | (window-width)) | |
1208 | (insert ",\n ")) | |
1209 | (t (insert ", "))) | |
1210 | (help-insert-xref-button text 'help-package name)) | |
1211 | (insert "\n"))) | |
96ae4c8f | 1212 | (insert " " (propertize "Summary" 'font-lock-face 'bold) |
4b99edf2 | 1213 | ": " (if desc (package-desc-doc desc)) "\n\n") |
cb6c4991 | 1214 | |
4b99edf2 | 1215 | (if built-in |
96ae4c8f CY |
1216 | ;; For built-in packages, insert the commentary. |
1217 | (let ((fn (locate-file (concat package-name ".el") load-path | |
1218 | load-file-rep-suffixes)) | |
1219 | (opoint (point))) | |
1220 | (insert (or (lm-commentary fn) "")) | |
1221 | (save-excursion | |
1222 | (goto-char opoint) | |
1223 | (when (re-search-forward "^;;; Commentary:\n" nil t) | |
1224 | (replace-match "")) | |
1225 | (while (re-search-forward "^\\(;+ ?\\)" nil t) | |
1226 | (replace-match "")))) | |
1227 | (let ((readme (expand-file-name (concat package-name "-readme.txt") | |
f561e49a CY |
1228 | package-user-dir)) |
1229 | readme-string) | |
96ae4c8f CY |
1230 | ;; For elpa packages, try downloading the commentary. If that |
1231 | ;; fails, try an existing readme file in `package-user-dir'. | |
f561e49a CY |
1232 | (cond ((condition-case nil |
1233 | (package--with-work-buffer (package-archive-base package) | |
1234 | (concat package-name "-readme.txt") | |
1235 | (setq buffer-file-name | |
1236 | (expand-file-name readme package-user-dir)) | |
1237 | (let ((version-control 'never)) | |
1238 | (save-buffer)) | |
1239 | (setq readme-string (buffer-string)) | |
1240 | t) | |
1241 | (error nil)) | |
1242 | (insert readme-string)) | |
96ae4c8f CY |
1243 | ((file-readable-p readme) |
1244 | (insert-file-contents readme) | |
1245 | (goto-char (point-max)))))))) | |
cb6c4991 CY |
1246 | |
1247 | (defun package-install-button-action (button) | |
1248 | (let ((package (button-get button 'package-symbol))) | |
1249 | (when (y-or-n-p (format "Install package `%s'? " package)) | |
1250 | (package-install package) | |
1251 | (revert-buffer nil t) | |
1252 | (goto-char (point-min))))) | |
cced7584 CY |
1253 | |
1254 | \f | |
44198b6e CY |
1255 | ;;;; Package menu mode. |
1256 | ||
54ea2a0d | 1257 | (defvar package-menu-mode-map |
e91a96fe | 1258 | (let ((map (make-sparse-keymap)) |
64eba874 | 1259 | (menu-map (make-sparse-keymap "Package"))) |
e91a96fe | 1260 | (set-keymap-parent map tabulated-list-mode-map) |
8adb4c33 | 1261 | (define-key map "\C-m" 'package-menu-describe-package) |
54ea2a0d JB |
1262 | (define-key map "u" 'package-menu-mark-unmark) |
1263 | (define-key map "\177" 'package-menu-backup-unmark) | |
1264 | (define-key map "d" 'package-menu-mark-delete) | |
1265 | (define-key map "i" 'package-menu-mark-install) | |
54ea2a0d JB |
1266 | (define-key map "r" 'package-menu-refresh) |
1267 | (define-key map "~" 'package-menu-mark-obsolete-for-deletion) | |
1268 | (define-key map "x" 'package-menu-execute) | |
1269 | (define-key map "h" 'package-menu-quick-help) | |
cb6c4991 | 1270 | (define-key map "?" 'package-menu-describe-package) |
64eba874 DN |
1271 | (define-key map [menu-bar package-menu] (cons "Package" menu-map)) |
1272 | (define-key menu-map [mq] | |
1273 | '(menu-item "Quit" quit-window | |
1274 | :help "Quit package selection")) | |
1275 | (define-key menu-map [s1] '("--")) | |
1276 | (define-key menu-map [mn] | |
1277 | '(menu-item "Next" next-line | |
1278 | :help "Next Line")) | |
1279 | (define-key menu-map [mp] | |
1280 | '(menu-item "Previous" previous-line | |
1281 | :help "Previous Line")) | |
1282 | (define-key menu-map [s2] '("--")) | |
1283 | (define-key menu-map [mu] | |
1284 | '(menu-item "Unmark" package-menu-mark-unmark | |
1285 | :help "Clear any marks on a package and move to the next line")) | |
1286 | (define-key menu-map [munm] | |
1287 | '(menu-item "Unmark backwards" package-menu-backup-unmark | |
1288 | :help "Back up one line and clear any marks on that package")) | |
1289 | (define-key menu-map [md] | |
1290 | '(menu-item "Mark for deletion" package-menu-mark-delete | |
1291 | :help "Mark a package for deletion and move to the next line")) | |
1292 | (define-key menu-map [mi] | |
1293 | '(menu-item "Mark for install" package-menu-mark-install | |
1294 | :help "Mark a package for installation and move to the next line")) | |
1295 | (define-key menu-map [s3] '("--")) | |
1296 | (define-key menu-map [mg] | |
e687c2cd | 1297 | '(menu-item "Update package list" revert-buffer |
64eba874 DN |
1298 | :help "Update the list of packages")) |
1299 | (define-key menu-map [mr] | |
1300 | '(menu-item "Refresh package list" package-menu-refresh | |
1301 | :help "Download the ELPA archive")) | |
1302 | (define-key menu-map [s4] '("--")) | |
1303 | (define-key menu-map [mt] | |
1304 | '(menu-item "Mark obsolete packages" package-menu-mark-obsolete-for-deletion | |
1305 | :help "Mark all obsolete packages for deletion")) | |
1306 | (define-key menu-map [mx] | |
1307 | '(menu-item "Execute actions" package-menu-execute | |
1308 | :help "Perform all the marked actions")) | |
1309 | (define-key menu-map [s5] '("--")) | |
1310 | (define-key menu-map [mh] | |
1311 | '(menu-item "Help" package-menu-quick-help | |
1312 | :help "Show short key binding help for package-menu-mode")) | |
1313 | (define-key menu-map [mc] | |
1314 | '(menu-item "View Commentary" package-menu-view-commentary | |
1315 | :help "Display information about this package")) | |
54ea2a0d | 1316 | map) |
44198b6e CY |
1317 | "Local keymap for `package-menu-mode' buffers.") |
1318 | ||
e91a96fe | 1319 | (define-derived-mode package-menu-mode tabulated-list-mode "Package Menu" |
44198b6e CY |
1320 | "Major mode for browsing a list of packages. |
1321 | Letters do not insert themselves; instead, they are commands. | |
1322 | \\<package-menu-mode-map> | |
1323 | \\{package-menu-mode-map}" | |
e91a96fe CY |
1324 | (setq tabulated-list-format [("Package" 18 package-menu--name-predicate) |
1325 | ("Version" 12 nil) | |
1326 | ("Status" 10 package-menu--status-predicate) | |
1327 | ("Description" 0 nil)]) | |
1328 | (setq tabulated-list-padding 2) | |
1329 | (setq tabulated-list-sort-key (cons "Status" nil)) | |
1330 | (tabulated-list-init-header)) | |
1331 | ||
1332 | (defmacro package--push (package desc status listname) | |
1333 | "Convenience macro for `package-menu--generate'. | |
1334 | If the alist stored in the symbol LISTNAME lacks an entry for a | |
1335 | package PACKAGE with descriptor DESC, add one. The alist is | |
7ede3b65 CY |
1336 | keyed with cons cells (PACKAGE . VERSION-LIST), where PACKAGE is |
1337 | a symbol and VERSION-LIST is a version list." | |
e91a96fe CY |
1338 | `(let* ((version (package-desc-vers ,desc)) |
1339 | (key (cons ,package version))) | |
1340 | (unless (assoc key ,listname) | |
1341 | (push (list key ,status (package-desc-doc ,desc)) ,listname)))) | |
1342 | ||
512e3ae1 | 1343 | (defun package-menu--generate (remember-pos packages) |
e91a96fe | 1344 | "Populate the Package Menu. |
512e3ae1 CY |
1345 | If REMEMBER-POS is non-nil, keep point on the same entry. |
1346 | PACKAGES should be t, which means to display all known packages, | |
1347 | or a list of package names (symbols) to display." | |
e91a96fe CY |
1348 | ;; Construct list of ((PACKAGE . VERSION) STATUS DESCRIPTION). |
1349 | (let (info-list name builtin) | |
1350 | ;; Installed packages: | |
1351 | (dolist (elt package-alist) | |
1352 | (setq name (car elt)) | |
512e3ae1 CY |
1353 | (when (or (eq packages t) (memq name packages)) |
1354 | (package--push name (cdr elt) | |
1355 | (if (stringp (cadr (assq name package-load-list))) | |
1356 | "held" "installed") | |
1357 | info-list))) | |
e91a96fe CY |
1358 | |
1359 | ;; Built-in packages: | |
1360 | (dolist (elt package--builtins) | |
1361 | (setq name (car elt)) | |
512e3ae1 CY |
1362 | (when (and (not (eq name 'emacs)) ; Hide the `emacs' package. |
1363 | (or (eq packages t) (memq name packages))) | |
e91a96fe CY |
1364 | (package--push name (cdr elt) "built-in" info-list))) |
1365 | ||
1366 | ;; Available and disabled packages: | |
1367 | (dolist (elt package-archive-contents) | |
1368 | (setq name (car elt)) | |
512e3ae1 CY |
1369 | (when (or (eq packages t) (memq name packages)) |
1370 | (let ((hold (assq name package-load-list))) | |
1371 | (package--push name (cdr elt) | |
1372 | (if (and hold (null (cadr hold))) | |
1373 | "disabled" | |
1374 | "available") | |
1375 | info-list)))) | |
e91a96fe CY |
1376 | |
1377 | ;; Obsolete packages: | |
1378 | (dolist (elt package-obsolete-alist) | |
1379 | (dolist (inner-elt (cdr elt)) | |
512e3ae1 CY |
1380 | (when (or (eq packages t) (memq (car elt) packages)) |
1381 | (package--push (car elt) (cdr inner-elt) "obsolete" info-list)))) | |
e91a96fe CY |
1382 | |
1383 | ;; Print the result. | |
1384 | (setq tabulated-list-entries (mapcar 'package-menu--print-info info-list)) | |
1385 | (tabulated-list-print remember-pos))) | |
1386 | ||
1387 | (defun package-menu--print-info (pkg) | |
1388 | "Return a package entry suitable for `tabulated-list-entries'. | |
1389 | PKG has the form ((PACKAGE . VERSION) STATUS DOC). | |
1390 | Return (KEY [NAME VERSION STATUS DOC]), where KEY is the | |
1391 | identifier (NAME . VERSION-LIST)." | |
1392 | (let* ((package (caar pkg)) | |
1393 | (version (cdr (car pkg))) | |
1394 | (status (nth 1 pkg)) | |
1395 | (doc (or (nth 2 pkg) "")) | |
1396 | (face (cond | |
1397 | ((string= status "built-in") 'font-lock-builtin-face) | |
1398 | ((string= status "available") 'default) | |
1399 | ((string= status "held") 'font-lock-constant-face) | |
1400 | ((string= status "disabled") 'font-lock-warning-face) | |
1401 | ((string= status "installed") 'font-lock-comment-face) | |
1402 | (t 'font-lock-warning-face)))) ; obsolete. | |
1403 | (list (cons package version) | |
1404 | (vector (list (symbol-name package) | |
1405 | 'face 'link | |
1406 | 'follow-link t | |
1407 | 'package-symbol package | |
1408 | 'action 'package-menu-describe-package) | |
1409 | (propertize (package-version-join version) | |
1410 | 'font-lock-face face) | |
1411 | (propertize status 'font-lock-face face) | |
1412 | (propertize doc 'font-lock-face face))))) | |
44198b6e CY |
1413 | |
1414 | (defun package-menu-refresh () | |
ebf662f4 CY |
1415 | "Download the Emacs Lisp package archive. |
1416 | This fetches the contents of each archive specified in | |
1417 | `package-archives', and then refreshes the package menu." | |
44198b6e | 1418 | (interactive) |
187d3296 CY |
1419 | (unless (eq major-mode 'package-menu-mode) |
1420 | (error "The current buffer is not a Package Menu")) | |
44198b6e | 1421 | (package-refresh-contents) |
512e3ae1 | 1422 | (package-menu--generate t t)) |
44198b6e | 1423 | |
e91a96fe CY |
1424 | (defun package-menu-describe-package (&optional button) |
1425 | "Describe the current package. | |
1426 | If optional arg BUTTON is non-nil, describe its associated package." | |
44198b6e | 1427 | (interactive) |
e91a96fe CY |
1428 | (let ((package (if button (button-get button 'package-symbol) |
1429 | (car (tabulated-list-get-id))))) | |
1430 | (if package | |
1431 | (describe-package package)))) | |
44198b6e CY |
1432 | |
1433 | ;; fixme numeric argument | |
1434 | (defun package-menu-mark-delete (num) | |
1435 | "Mark a package for deletion and move to the next line." | |
1436 | (interactive "p") | |
015eea59 | 1437 | (if (string-equal (package-menu-get-status) "installed") |
e91a96fe | 1438 | (tabulated-list-put-tag "D" t) |
015eea59 | 1439 | (forward-line))) |
44198b6e CY |
1440 | |
1441 | (defun package-menu-mark-install (num) | |
1442 | "Mark a package for installation and move to the next line." | |
1443 | (interactive "p") | |
015eea59 | 1444 | (if (string-equal (package-menu-get-status) "available") |
e91a96fe | 1445 | (tabulated-list-put-tag "I" t) |
015eea59 | 1446 | (forward-line))) |
44198b6e CY |
1447 | |
1448 | (defun package-menu-mark-unmark (num) | |
1449 | "Clear any marks on a package and move to the next line." | |
1450 | (interactive "p") | |
e91a96fe | 1451 | (tabulated-list-put-tag " " t)) |
44198b6e CY |
1452 | |
1453 | (defun package-menu-backup-unmark () | |
1454 | "Back up one line and clear any marks on that package." | |
1455 | (interactive) | |
1456 | (forward-line -1) | |
e91a96fe | 1457 | (tabulated-list-put-tag " ")) |
44198b6e CY |
1458 | |
1459 | (defun package-menu-mark-obsolete-for-deletion () | |
1460 | "Mark all obsolete packages for deletion." | |
1461 | (interactive) | |
1462 | (save-excursion | |
1463 | (goto-char (point-min)) | |
1464 | (forward-line 2) | |
1465 | (while (not (eobp)) | |
1466 | (if (looking-at ".*\\s obsolete\\s ") | |
e91a96fe | 1467 | (tabulated-list-put-tag "D" t) |
44198b6e CY |
1468 | (forward-line 1))))) |
1469 | ||
1470 | (defun package-menu-quick-help () | |
1471 | "Show short key binding help for package-menu-mode." | |
1472 | (interactive) | |
1473 | (message "n-ext, i-nstall, d-elete, u-nmark, x-ecute, r-efresh, h-elp")) | |
1474 | ||
cb6c4991 CY |
1475 | (define-obsolete-function-alias |
1476 | 'package-menu-view-commentary 'package-menu-describe-package "24.1") | |
44198b6e | 1477 | |
44198b6e CY |
1478 | (defun package-menu-get-status () |
1479 | (save-excursion | |
1480 | (if (looking-at ". [^ \t]*[ \t]*[^ \t]*[ \t]*\\([^ \t]*\\)") | |
1481 | (match-string 1) | |
1482 | ""))) | |
1483 | ||
1484 | (defun package-menu-execute () | |
015eea59 CY |
1485 | "Perform marked Package Menu actions. |
1486 | Packages marked for installation are downloaded and installed; | |
1487 | packages marked for deletion are removed." | |
44198b6e | 1488 | (interactive) |
e91a96fe CY |
1489 | (unless (eq major-mode 'package-menu-mode) |
1490 | (error "The current buffer is not in Package Menu mode")) | |
1491 | (let (install-list delete-list cmd id) | |
015eea59 CY |
1492 | (save-excursion |
1493 | (goto-char (point-min)) | |
1494 | (while (not (eobp)) | |
1495 | (setq cmd (char-after)) | |
e91a96fe CY |
1496 | (unless (eq cmd ?\s) |
1497 | ;; This is the key (PACKAGE . VERSION-LIST). | |
1498 | (setq id (tabulated-list-get-id)) | |
1499 | (cond ((eq cmd ?D) | |
1500 | (push (cons (symbol-name (car id)) | |
1501 | (package-version-join (cdr id))) | |
1502 | delete-list)) | |
1503 | ((eq cmd ?I) | |
1504 | (push (car id) install-list)))) | |
015eea59 CY |
1505 | (forward-line))) |
1506 | ;; Delete packages, prompting if necessary. | |
1507 | (when delete-list | |
1508 | (if (yes-or-no-p | |
1509 | (if (= (length delete-list) 1) | |
1510 | (format "Delete package `%s-%s'? " | |
1511 | (caar delete-list) | |
1512 | (cdr (car delete-list))) | |
1513 | (format "Delete these %d packages (%s)? " | |
1514 | (length delete-list) | |
1515 | (mapconcat (lambda (elt) | |
1516 | (concat (car elt) "-" (cdr elt))) | |
1517 | delete-list | |
1518 | ", ")))) | |
1519 | (dolist (elt delete-list) | |
515de2e3 | 1520 | (condition-case-no-debug err |
015eea59 CY |
1521 | (package-delete (car elt) (cdr elt)) |
1522 | (error (message (cadr err))))) | |
1523 | (error "Aborted"))) | |
1524 | (when install-list | |
1525 | (if (yes-or-no-p | |
1526 | (if (= (length install-list) 1) | |
1527 | (format "Install package `%s'? " (car install-list)) | |
1528 | (format "Install these %d packages (%s)? " | |
1529 | (length install-list) | |
e91a96fe CY |
1530 | (mapconcat 'symbol-name install-list ", ")))) |
1531 | (mapc 'package-install install-list))) | |
015eea59 CY |
1532 | ;; If we deleted anything, regenerate `package-alist'. This is done |
1533 | ;; automatically if we installed a package. | |
1534 | (and delete-list (null install-list) | |
1535 | (package-initialize)) | |
1536 | (if (or delete-list install-list) | |
512e3ae1 | 1537 | (package-menu--generate t t) |
015eea59 | 1538 | (message "No operations specified.")))) |
44198b6e | 1539 | |
e91a96fe CY |
1540 | (defun package-menu--version-predicate (A B) |
1541 | (let ((vA (or (aref (cadr A) 1) '(0))) | |
1542 | (vB (or (aref (cadr B) 1) '(0)))) | |
1543 | (if (version-list-= vA vB) | |
1544 | (package-menu--name-predicate A B) | |
1545 | (version-list-< vA vB)))) | |
1546 | ||
1547 | (defun package-menu--status-predicate (A B) | |
1548 | (let ((sA (aref (cadr A) 2)) | |
1549 | (sB (aref (cadr B) 2))) | |
1550 | (cond ((string= sA sB) | |
1551 | (package-menu--name-predicate A B)) | |
1552 | ((string= sA "available") t) | |
1553 | ((string= sB "available") nil) | |
1554 | ((string= sA "installed") t) | |
1555 | ((string= sB "installed") nil) | |
1556 | ((string= sA "held") t) | |
1557 | ((string= sB "held") nil) | |
1558 | ((string= sA "built-in") t) | |
1559 | ((string= sB "built-in") nil) | |
1560 | ((string= sA "obsolete") t) | |
1561 | ((string= sB "obsolete") nil) | |
1562 | (t (string< sA sB))))) | |
1563 | ||
1564 | (defun package-menu--description-predicate (A B) | |
1565 | (let ((dA (aref (cadr A) 3)) | |
1566 | (dB (aref (cadr B) 3))) | |
1567 | (if (string= dA dB) | |
1568 | (package-menu--name-predicate A B) | |
1569 | (string< dA dB)))) | |
1570 | ||
1571 | (defun package-menu--name-predicate (A B) | |
1572 | (string< (symbol-name (caar A)) | |
1573 | (symbol-name (caar B)))) | |
44198b6e CY |
1574 | |
1575 | ;;;###autoload | |
e91a96fe | 1576 | (defun list-packages (&optional no-fetch) |
44198b6e | 1577 | "Display a list of packages. |
e91a96fe CY |
1578 | This first fetches the updated list of packages before |
1579 | displaying, unless a prefix argument NO-FETCH is specified. | |
44198b6e | 1580 | The list is displayed in a buffer named `*Packages*'." |
e91a96fe CY |
1581 | (interactive "P") |
1582 | (require 'finder-inf nil t) | |
8a500a91 | 1583 | ;; Initialize the package system if necessary. |
4b99edf2 CY |
1584 | (unless package--initialized |
1585 | (package-initialize t)) | |
e91a96fe CY |
1586 | (unless no-fetch |
1587 | (package-refresh-contents)) | |
1588 | (let ((buf (get-buffer-create "*Packages*"))) | |
1589 | (with-current-buffer buf | |
1590 | (package-menu-mode) | |
512e3ae1 | 1591 | (package-menu--generate nil t)) |
e91a96fe CY |
1592 | ;; The package menu buffer has keybindings. If the user types |
1593 | ;; `M-x list-packages', that suggests it should become current. | |
1594 | (switch-to-buffer buf))) | |
44198b6e | 1595 | |
cb8759ca | 1596 | ;;;###autoload |
96ae4c8f | 1597 | (defalias 'package-list-packages 'list-packages) |
cb8759ca | 1598 | |
512e3ae1 CY |
1599 | ;; Used in finder.el |
1600 | (defun package-show-package-list (packages) | |
1601 | "Display PACKAGES in a *Packages* buffer. | |
1602 | This is similar to `list-packages', but it does not fetch the | |
1603 | updated list of packages, and it only displays packages with | |
1604 | names in PACKAGES (which should be a list of symbols)." | |
1605 | (require 'finder-inf nil t) | |
1606 | (let ((buf (get-buffer-create "*Packages*"))) | |
1607 | (with-current-buffer buf | |
1608 | (package-menu-mode) | |
1609 | (package-menu--generate nil packages)) | |
1610 | (switch-to-buffer buf))) | |
1611 | ||
44198b6e CY |
1612 | (defun package-list-packages-no-fetch () |
1613 | "Display a list of packages. | |
1614 | Does not fetch the updated list of packages before displaying. | |
1615 | The list is displayed in a buffer named `*Packages*'." | |
1616 | (interactive) | |
e91a96fe | 1617 | (list-packages t)) |
44198b6e | 1618 | |
44198b6e CY |
1619 | (provide 'package) |
1620 | ||
1621 | ;;; package.el ends here |