Commit | Line | Data |
---|---|---|
44198b6e CY |
1 | ;;; package.el --- Simple package system for Emacs |
2 | ||
3 | ;; Copyright (C) 2007, 2008, 2009, 2010 Free Software Foundation, Inc. | |
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 | ;; | |
80 | ;; M-x package-list-packages | |
81 | ;; Enters a mode similar to buffer-menu which lets you manage | |
82 | ;; packages. You can choose packages for install (mark with "i", | |
83 | ;; then "x" to execute) or deletion (not implemented yet), and you | |
84 | ;; can see what packages are available. This will automatically | |
85 | ;; fetch the latest list of packages from ELPA. | |
86 | ;; | |
87 | ;; M-x package-list-packages-no-fetch | |
88 | ;; Like package-list-packages, but does not automatically fetch the | |
89 | ;; new list of packages. | |
90 | ;; | |
91 | ;; M-x package-install-from-buffer | |
92 | ;; Install a package consisting of a single .el file that appears | |
93 | ;; in the current buffer. This only works for packages which | |
94 | ;; define a Version header properly; package.el also supports the | |
95 | ;; extension headers Package-Version (in case Version is an RCS id | |
96 | ;; or similar), and Package-Requires (if the package requires other | |
97 | ;; packages). | |
98 | ;; | |
99 | ;; M-x package-install-file | |
100 | ;; Install a package from the indicated file. The package can be | |
101 | ;; either a tar file or a .el file. A tar file must contain an | |
102 | ;; appropriately-named "-pkg.el" file; a .el file must be properly | |
103 | ;; formatted as with package-install-from-buffer. | |
104 | ||
105 | ;;; Thanks: | |
106 | ;;; (sorted by sort-lines): | |
107 | ||
108 | ;; Jim Blandy <jimb@red-bean.com> | |
109 | ;; Karl Fogel <kfogel@red-bean.com> | |
110 | ;; Kevin Ryde <user42@zip.com.au> | |
111 | ;; Lawrence Mitchell | |
112 | ;; Michael Olson <mwolson@member.fsf.org> | |
113 | ;; Sebastian Tennant <sebyte@smolny.plus.com> | |
114 | ;; Stefan Monnier <monnier@iro.umontreal.ca> | |
115 | ;; Vinicius Jose Latorre <viniciusjl@ig.com.br> | |
116 | ;; Phil Hagelberg <phil@hagelb.org> | |
117 | ||
118 | ;;; ToDo: | |
119 | ||
120 | ;; - putting info dirs at the start of the info path means | |
121 | ;; users see a weird ordering of categories. OTOH we want to | |
122 | ;; override later entries. maybe emacs needs to enforce | |
123 | ;; the standard layout? | |
124 | ;; - put bytecode in a separate directory tree | |
125 | ;; - perhaps give users a way to recompile their bytecode | |
126 | ;; or do it automatically when emacs changes | |
127 | ;; - give users a way to know whether a package is installed ok | |
128 | ;; - give users a way to view a package's documentation when it | |
129 | ;; only appears in the .el | |
130 | ;; - use/extend checkdoc so people can tell if their package will work | |
131 | ;; - "installed" instead of a blank in the status column | |
132 | ;; - tramp needs its files to be compiled in a certain order. | |
133 | ;; how to handle this? fix tramp? | |
134 | ;; - on emacs 21 we don't kill the -autoloads.el buffer. what about 22? | |
135 | ;; - maybe we need separate .elc directories for various emacs versions | |
136 | ;; and also emacs-vs-xemacs. That way conditional compilation can | |
137 | ;; work. But would this break anything? | |
138 | ;; - should store the package's keywords in archive-contents, then | |
139 | ;; let the users filter the package-menu by keyword. See | |
140 | ;; finder-by-keyword. (We could also let people view the | |
141 | ;; Commentary, but it isn't clear how useful this is.) | |
142 | ;; - William Xu suggests being able to open a package file without | |
143 | ;; installing it | |
144 | ;; - Interface with desktop.el so that restarting after an install | |
145 | ;; works properly | |
146 | ;; - Implement M-x package-upgrade, to upgrade any/all existing packages | |
147 | ;; - Use hierarchical layout. PKG/etc PKG/lisp PKG/info | |
148 | ;; ... except maybe lisp? | |
149 | ;; - It may be nice to have a macro that expands to the package's | |
150 | ;; private data dir, aka ".../etc". Or, maybe data-directory | |
151 | ;; needs to be a list (though this would be less nice) | |
152 | ;; a few packages want this, eg sokoban | |
153 | ;; - package menu needs: | |
154 | ;; ability to know which packages are built-in & thus not deletable | |
155 | ;; it can sometimes print odd results, like 0.3 available but 0.4 active | |
156 | ;; why is that? | |
157 | ;; - Allow multiple versions on the server...? | |
158 | ;; [ why bother? ] | |
159 | ;; - Don't install a package which will invalidate dependencies overall | |
160 | ;; - Allow something like (or (>= emacs 21.0) (>= xemacs 21.5)) | |
161 | ;; [ currently thinking, why bother.. KISS ] | |
162 | ;; - Allow optional package dependencies | |
163 | ;; then if we require 'bbdb', bbdb-specific lisp in lisp/bbdb | |
164 | ;; and just don't compile to add to load path ...? | |
165 | ;; - Have a list of archive URLs? [ maybe there's no point ] | |
166 | ;; - David Kastrup pointed out on the xemacs list that for GPL it | |
167 | ;; is friendlier to ship the source tree. We could "support" that | |
168 | ;; by just having a "src" subdir in the package. This isn't ideal | |
169 | ;; but it probably is not worth trying to support random source | |
170 | ;; tree layouts, build schemes, etc. | |
171 | ;; - Our treatment of the info path is somewhat bogus | |
172 | ;; - perhaps have an "unstable" tree in ELPA as well as a stable one | |
173 | ||
174 | ;;; Code: | |
175 | ||
176 | (defgroup package nil | |
177 | "Manager for Emacs Lisp packages." | |
178 | :group 'applications | |
179 | :version "24.1") | |
180 | ||
181 | ;;;###autoload | |
182 | (defcustom package-enable-at-startup t | |
183 | "Whether to activate installed packages when Emacs starts. | |
184 | If non-nil, packages are activated after reading the init file | |
185 | and before `after-init-hook'. Activation is not done if | |
186 | `user-init-file' is nil (e.g. Emacs was started with \"-q\"). | |
187 | ||
188 | Even if the value is nil, you can type \\[package-initialize] to | |
189 | activate the package system at any time." | |
190 | :type 'boolean | |
191 | :group 'package | |
192 | :version "24.1") | |
193 | ||
194 | (defcustom package-load-list '(all) | |
195 | "List of packages for `package-initialize' to load. | |
196 | Each element in this list should be a list (NAME VERSION), or the | |
197 | symbol `all'. The symbol `all' says to load the latest installed | |
198 | versions of all packages not specified by other elements. | |
199 | ||
200 | For an element (NAME VERSION), NAME is a package name (a symbol). | |
201 | VERSION should be t, a string, or nil. | |
202 | If VERSION is t, all versions are loaded, though obsolete ones | |
203 | will be put in `package-obsolete-alist' and not activated. | |
204 | If VERSION is a string, only that version is ever loaded. | |
205 | Any other version, even if newer, is silently ignored. | |
206 | Hence, the package is \"held\" at that version. | |
207 | If VERSION is nil, the package is not loaded (it is \"disabled\")." | |
208 | :type '(repeat symbol) | |
bc44bef7 | 209 | :risky t |
44198b6e CY |
210 | :group 'package |
211 | :version "24.1") | |
212 | ||
213 | (defvar Info-directory-list) | |
44198b6e CY |
214 | (declare-function info-initialize "info" ()) |
215 | (declare-function url-http-parse-response "url-http" ()) | |
216 | (declare-function lm-header "lisp-mnt" (header)) | |
217 | (declare-function lm-commentary "lisp-mnt" (&optional file)) | |
218 | (declare-function dired-delete-file "dired" (file &optional recursive trash)) | |
cb6c4991 | 219 | (defvar url-http-end-of-headers) |
44198b6e | 220 | |
bc44bef7 PH |
221 | (defcustom package-archives '(("gnu" . "http://elpa.gnu.org/packages/")) |
222 | "An alist of archives from which to fetch. | |
223 | The default value points to the GNU Emacs package repository. | |
224 | Each element has the form (ID . URL), where ID is an identifier | |
225 | string for an archive and URL is a http: URL (a string)." | |
226 | :type '(alist :key-type (string :tag "Archive name") | |
227 | :value-type (string :tag "Archive URL")) | |
228 | :risky t | |
229 | :group 'package | |
230 | :version "24.1") | |
44198b6e CY |
231 | |
232 | (defconst package-archive-version 1 | |
233 | "Version number of the package archive understood by this file. | |
234 | Lower version numbers than this will probably be understood as well.") | |
235 | ||
236 | (defconst package-el-version "1.0" | |
237 | "Version of package.el.") | |
238 | ||
239 | ;; We don't prime the cache since it tends to get out of date. | |
240 | (defvar package-archive-contents nil | |
241 | "Cache of the contents of the Emacs Lisp Package Archive. | |
242 | This is an alist mapping package names (symbols) to package | |
243 | descriptor vectors. These are like the vectors for `package-alist' | |
bc44bef7 PH |
244 | but have extra entries: one which is 'tar for tar packages and |
245 | 'single for single-file packages, and one which is the name of | |
246 | the archive from which it came.") | |
247 | (put 'package-archive-contents 'risky-local-variable t) | |
44198b6e CY |
248 | |
249 | (defcustom package-user-dir (locate-user-emacs-file "elpa") | |
250 | "Directory containing the user's Emacs Lisp packages. | |
251 | The directory name should be absolute. | |
252 | Apart from this directory, Emacs also looks for system-wide | |
253 | packages in `package-directory-list'." | |
254 | :type 'directory | |
bc44bef7 | 255 | :risky t |
44198b6e CY |
256 | :group 'package |
257 | :version "24.1") | |
258 | ||
259 | (defcustom package-directory-list | |
260 | ;; Defaults are subdirs named "elpa" in the site-lisp dirs. | |
261 | (let (result) | |
262 | (dolist (f load-path) | |
263 | (if (equal (file-name-nondirectory f) "site-lisp") | |
264 | (push (expand-file-name "elpa" f) result))) | |
265 | (nreverse result)) | |
266 | "List of additional directories containing Emacs Lisp packages. | |
267 | Each directory name should be absolute. | |
268 | ||
269 | These directories contain packages intended for system-wide; in | |
270 | contrast, `package-user-dir' contains packages for personal use." | |
271 | :type '(repeat directory) | |
bc44bef7 | 272 | :risky t |
44198b6e CY |
273 | :group 'package |
274 | :version "24.1") | |
275 | ||
44198b6e CY |
276 | (defconst package--builtins-base |
277 | ;; We use package-version split here to make sure to pick up the | |
278 | ;; minor version. | |
148cef8e | 279 | `((emacs . [,(version-to-list emacs-version) nil |
44198b6e | 280 | "GNU Emacs"]) |
148cef8e | 281 | (package . [,(version-to-list package-el-version) |
44198b6e CY |
282 | nil "Simple package system for GNU Emacs"])) |
283 | "Packages which are always built-in.") | |
284 | ||
285 | (defvar package--builtins | |
286 | (delq nil | |
287 | (append | |
288 | package--builtins-base | |
289 | (if (>= emacs-major-version 22) | |
290 | ;; FIXME: emacs 22 includes tramp, rcirc, maybe | |
291 | ;; other things... | |
376c2b6b | 292 | '((erc . [(5 2) nil "Internet Relay Chat client"]) |
44198b6e CY |
293 | ;; The external URL is version 1.15, so make sure the |
294 | ;; built-in one looks newer. | |
295 | (url . [(1 16) nil "URL handling libary"]))) | |
296 | (if (>= emacs-major-version 23) | |
297 | '(;; Strangely, nxml-version is missing in Emacs 23. | |
298 | ;; We pick the merge date as the version. | |
376c2b6b CY |
299 | (nxml . [(20071123) nil "Major mode for XML documents"]) |
300 | (bubbles . [(0 5) nil "A puzzle game"]))))) | |
44198b6e CY |
301 | "Alist of all built-in packages. |
302 | Maps the package name to a vector [VERSION REQS DOCSTRING].") | |
bc44bef7 | 303 | (put 'package--builtins 'risky-local-variable t) |
44198b6e CY |
304 | |
305 | (defvar package-alist package--builtins | |
306 | "Alist of all packages available for activation. | |
307 | This maps the package name to a vector [VERSION REQS DOCSTRING]. | |
308 | ||
309 | The value is generated by `package-load-descriptor', usually | |
310 | called via `package-initialize'. For user customizations of | |
311 | which packages to load/activate, see `package-load-list'.") | |
bc44bef7 | 312 | (put 'package-archive-contents 'risky-local-variable t) |
44198b6e CY |
313 | |
314 | (defvar package-activated-list | |
315 | (mapcar #'car package-alist) | |
316 | "List of the names of currently activated packages.") | |
bc44bef7 | 317 | (put 'package-activated-list 'risky-local-variable t) |
44198b6e CY |
318 | |
319 | (defvar package-obsolete-alist nil | |
320 | "Representation of obsolete packages. | |
321 | Like `package-alist', but maps package name to a second alist. | |
322 | The inner alist is keyed by version.") | |
bc44bef7 | 323 | (put 'package-obsolete-alist 'risky-local-variable t) |
44198b6e CY |
324 | |
325 | (defconst package-subdirectory-regexp | |
326 | "^\\([^.].*\\)-\\([0-9]+\\(?:[.][0-9]+\\)*\\)$" | |
327 | "Regular expression matching the name of a package subdirectory. | |
328 | The first subexpression is the package name. | |
329 | The second subexpression is the version string.") | |
330 | ||
331 | (defun package-version-join (l) | |
332 | "Turn a list of version numbers into a version string." | |
333 | (mapconcat 'int-to-string l ".")) | |
334 | ||
44198b6e CY |
335 | (defun package-strip-version (dirname) |
336 | "Strip the version from a combined package name and version. | |
337 | E.g., if given \"quux-23.0\", will return \"quux\"" | |
338 | (if (string-match package-subdirectory-regexp dirname) | |
339 | (match-string 1 dirname))) | |
340 | ||
341 | (defun package-load-descriptor (dir package) | |
bc44bef7 PH |
342 | "Load the description file in directory DIR for package PACKAGE." |
343 | (let* ((pkg-dir (expand-file-name package dir)) | |
344 | (pkg-file (expand-file-name | |
345 | (concat (package-strip-version package) "-pkg") | |
346 | pkg-dir))) | |
347 | (when (and (file-directory-p pkg-dir) | |
348 | (file-exists-p (concat pkg-file ".el"))) | |
349 | (load pkg-file nil t)))) | |
44198b6e CY |
350 | |
351 | (defun package-load-all-descriptors () | |
352 | "Load descriptors for installed Emacs Lisp packages. | |
353 | This looks for package subdirectories in `package-user-dir' and | |
354 | `package-directory-list'. The variable `package-load-list' | |
355 | controls which package subdirectories may be loaded. | |
356 | ||
357 | In each valid package subdirectory, this function loads the | |
358 | description file containing a call to `define-package', which | |
359 | updates `package-alist' and `package-obsolete-alist'." | |
360 | (let ((all (memq 'all package-load-list)) | |
361 | name version force) | |
362 | (dolist (dir (cons package-user-dir package-directory-list)) | |
363 | (when (file-directory-p dir) | |
364 | (dolist (subdir (directory-files dir)) | |
365 | (when (and (file-directory-p (expand-file-name subdir dir)) | |
366 | (string-match package-subdirectory-regexp subdir)) | |
367 | (setq name (intern (match-string 1 subdir)) | |
368 | version (match-string 2 subdir) | |
369 | force (assq name package-load-list)) | |
370 | (when (cond | |
371 | ((null force) | |
372 | all) ; not in package-load-list | |
373 | ((null (setq force (cadr force))) | |
374 | nil) ; disabled | |
375 | ((eq force t) | |
376 | t) | |
377 | ((stringp force) ; held | |
148cef8e CY |
378 | (version-list-= (version-to-list version) |
379 | (version-to-list force))) | |
44198b6e CY |
380 | (t |
381 | (error "Invalid element in `package-load-list'"))) | |
382 | (package-load-descriptor dir subdir)))))))) | |
383 | ||
384 | (defsubst package-desc-vers (desc) | |
385 | "Extract version from a package description vector." | |
386 | (aref desc 0)) | |
387 | ||
388 | (defsubst package-desc-reqs (desc) | |
389 | "Extract requirements from a package description vector." | |
390 | (aref desc 1)) | |
391 | ||
392 | (defsubst package-desc-doc (desc) | |
393 | "Extract doc string from a package description vector." | |
394 | (aref desc 2)) | |
395 | ||
396 | (defsubst package-desc-kind (desc) | |
397 | "Extract the kind of download from an archive package description vector." | |
398 | (aref desc 3)) | |
399 | ||
cced7584 CY |
400 | (defun package--dir (name version-string) |
401 | (let* ((subdir (concat name "-" version-string)) | |
44198b6e | 402 | (dir-list (cons package-user-dir package-directory-list)) |
cced7584 | 403 | pkg-dir) |
44198b6e | 404 | (while dir-list |
cced7584 CY |
405 | (let ((subdir-full (expand-file-name subdir (car dir-list)))) |
406 | (if (file-directory-p subdir-full) | |
407 | (setq pkg-dir subdir-full | |
408 | dir-list nil) | |
44198b6e | 409 | (setq dir-list (cdr dir-list))))) |
cced7584 CY |
410 | pkg-dir)) |
411 | ||
412 | (defun package-activate-1 (package pkg-vec) | |
413 | (let* ((name (symbol-name package)) | |
414 | (version-str (package-version-join (package-desc-vers pkg-vec))) | |
415 | (pkg-dir (package--dir name version-str))) | |
44198b6e CY |
416 | (unless pkg-dir |
417 | (error "Internal error: could not find directory for %s-%s" | |
cced7584 CY |
418 | name version-str)) |
419 | ;; Add info node. | |
44198b6e CY |
420 | (if (file-exists-p (expand-file-name "dir" pkg-dir)) |
421 | (progn | |
422 | ;; FIXME: not the friendliest, but simple. | |
423 | (require 'info) | |
424 | (info-initialize) | |
425 | (setq Info-directory-list (cons pkg-dir Info-directory-list)))) | |
cced7584 | 426 | ;; Add to load path, add autoloads, and activate the package. |
44198b6e | 427 | (setq load-path (cons pkg-dir load-path)) |
cced7584 | 428 | (load (expand-file-name (concat name "-autoloads") pkg-dir) nil t) |
44198b6e CY |
429 | (setq package-activated-list (cons package package-activated-list)) |
430 | ;; Don't return nil. | |
431 | t)) | |
432 | ||
433 | (defun package--built-in (package version) | |
434 | "Return true if the package is built-in to Emacs." | |
435 | (let ((elt (assq package package--builtins))) | |
148cef8e | 436 | (and elt (version-list-= (package-desc-vers (cdr elt)) version)))) |
44198b6e CY |
437 | |
438 | ;; FIXME: return a reason instead? | |
439 | (defun package-activate (package version) | |
440 | "Activate a package, and recursively activate its dependencies. | |
441 | Return nil if the package could not be activated." | |
442 | ;; Assume the user knows what he is doing -- go ahead and activate a | |
443 | ;; newer version of a package if an older one has already been | |
444 | ;; activated. This is not ideal; we'd at least need to check to see | |
445 | ;; if the package has actually been loaded, and not merely | |
446 | ;; activated. However, don't try to activate 'emacs', as that makes | |
447 | ;; no sense. | |
448 | (unless (eq package 'emacs) | |
449 | (let* ((pkg-desc (assq package package-alist)) | |
450 | (this-version (package-desc-vers (cdr pkg-desc))) | |
451 | (req-list (package-desc-reqs (cdr pkg-desc))) | |
cced7584 | 452 | ;; If the package was never activated, do it now. |
44198b6e | 453 | (keep-going (or (not (memq package package-activated-list)) |
148cef8e | 454 | (version-list-< version this-version)))) |
44198b6e CY |
455 | (while (and req-list keep-going) |
456 | (let* ((req (car req-list)) | |
457 | (req-name (car req)) | |
458 | (req-version (cadr req))) | |
459 | (or (package-activate req-name req-version) | |
460 | (setq keep-going nil))) | |
461 | (setq req-list (cdr req-list))) | |
462 | (if keep-going | |
463 | (package-activate-1 package (cdr pkg-desc)) | |
464 | ;; We get here if a dependency failed to activate -- but we | |
465 | ;; can also get here if the requested package was already | |
466 | ;; activated. Return non-nil in the latter case. | |
467 | (and (memq package package-activated-list) | |
148cef8e | 468 | (version-list-<= version this-version)))))) |
44198b6e CY |
469 | |
470 | (defun package-mark-obsolete (package pkg-vec) | |
471 | "Put package on the obsolete list, if not already there." | |
472 | (let ((elt (assq package package-obsolete-alist))) | |
473 | (if elt | |
474 | ;; If this obsolete version does not exist in the list, update | |
475 | ;; it the list. | |
476 | (unless (assoc (package-desc-vers pkg-vec) (cdr elt)) | |
477 | (setcdr elt (cons (cons (package-desc-vers pkg-vec) pkg-vec) | |
478 | (cdr elt)))) | |
479 | ;; Make a new association. | |
480 | (setq package-obsolete-alist | |
481 | (cons (cons package (list (cons (package-desc-vers pkg-vec) | |
482 | pkg-vec))) | |
483 | package-obsolete-alist))))) | |
484 | ||
485 | ;; (define-package "emacs" "21.4.1" "GNU Emacs core package.") | |
486 | ;; (define-package "erc" "5.1" "ERC - irc client" '((emacs "21.0"))) | |
487 | (defun define-package (name-str version-string | |
488 | &optional docstring requirements) | |
489 | "Define a new package. | |
490 | NAME is the name of the package, a string. | |
491 | VERSION-STRING is the version of the package, a dotted sequence | |
492 | of integers. | |
493 | DOCSTRING is the optional description. | |
494 | REQUIREMENTS is a list of requirements on other packages. | |
495 | Each requirement is of the form (OTHER-PACKAGE \"VERSION\")." | |
496 | (let* ((name (intern name-str)) | |
497 | (pkg-desc (assq name package-alist)) | |
148cef8e | 498 | (new-version (version-to-list version-string)) |
44198b6e CY |
499 | (new-pkg-desc |
500 | (cons name | |
501 | (vector new-version | |
502 | (mapcar | |
503 | (lambda (elt) | |
504 | (list (car elt) | |
148cef8e | 505 | (version-to-list (car (cdr elt))))) |
44198b6e CY |
506 | requirements) |
507 | docstring)))) | |
508 | ;; Only redefine a package if the redefinition is newer. | |
509 | (if (or (not pkg-desc) | |
148cef8e CY |
510 | (version-list-< (package-desc-vers (cdr pkg-desc)) |
511 | new-version)) | |
44198b6e CY |
512 | (progn |
513 | (when pkg-desc | |
514 | ;; Remove old package and declare it obsolete. | |
515 | (setq package-alist (delq pkg-desc package-alist)) | |
516 | (package-mark-obsolete (car pkg-desc) (cdr pkg-desc))) | |
517 | ;; Add package to the alist. | |
518 | (setq package-alist (cons new-pkg-desc package-alist))) | |
519 | ;; You can have two packages with the same version, for instance | |
520 | ;; one in the system package directory and one in your private | |
521 | ;; directory. We just let the first one win. | |
148cef8e CY |
522 | (unless (version-list-= new-version |
523 | (package-desc-vers (cdr pkg-desc))) | |
44198b6e CY |
524 | ;; The package is born obsolete. |
525 | (package-mark-obsolete (car new-pkg-desc) (cdr new-pkg-desc)))))) | |
526 | ||
527 | ;; From Emacs 22. | |
528 | (defun package-autoload-ensure-default-file (file) | |
529 | "Make sure that the autoload file FILE exists and if not create it." | |
530 | (unless (file-exists-p file) | |
531 | (write-region | |
532 | (concat ";;; " (file-name-nondirectory file) | |
533 | " --- automatically extracted autoloads\n" | |
534 | ";;\n" | |
535 | ";;; Code:\n\n" | |
536 | "\f\n;; Local Variables:\n" | |
537 | ";; version-control: never\n" | |
538 | ";; no-byte-compile: t\n" | |
539 | ";; no-update-autoloads: t\n" | |
540 | ";; End:\n" | |
541 | ";;; " (file-name-nondirectory file) | |
542 | " ends here\n") | |
543 | nil file)) | |
544 | file) | |
545 | ||
546 | (defun package-generate-autoloads (name pkg-dir) | |
547 | (let* ((auto-name (concat name "-autoloads.el")) | |
548 | (ignore-name (concat name "-pkg.el")) | |
549 | (generated-autoload-file (expand-file-name auto-name pkg-dir)) | |
550 | (version-control 'never)) | |
551 | (require 'autoload) | |
552 | (unless (fboundp 'autoload-ensure-default-file) | |
553 | (package-autoload-ensure-default-file generated-autoload-file)) | |
554 | (update-directory-autoloads pkg-dir))) | |
555 | ||
556 | (defun package-untar-buffer () | |
557 | "Untar the current buffer. | |
558 | This uses `tar-untar-buffer' if it is available. | |
559 | Otherwise it uses an external `tar' program. | |
560 | `default-directory' should be set by the caller." | |
561 | (require 'tar-mode) | |
562 | (if (fboundp 'tar-untar-buffer) | |
563 | (progn | |
564 | ;; tar-mode messes with narrowing, so we just let it have the | |
565 | ;; whole buffer to play with. | |
566 | (delete-region (point-min) (point)) | |
567 | (tar-mode) | |
568 | (tar-untar-buffer)) | |
569 | ;; FIXME: check the result. | |
570 | (call-process-region (point) (point-max) "tar" nil '(nil nil) nil | |
571 | "xf" "-"))) | |
572 | ||
573 | (defun package-unpack (name version) | |
574 | (let ((pkg-dir (expand-file-name (concat (symbol-name name) "-" version) | |
575 | package-user-dir))) | |
576 | ;; Be careful!! | |
577 | (make-directory package-user-dir t) | |
578 | (if (file-directory-p pkg-dir) | |
579 | (mapc (lambda (file) nil) ; 'delete-file -- FIXME: when we're | |
580 | ; more confident | |
581 | (directory-files pkg-dir t "^[^.]"))) | |
582 | (let* ((default-directory (file-name-as-directory package-user-dir))) | |
583 | (package-untar-buffer) | |
584 | (package-generate-autoloads (symbol-name name) pkg-dir) | |
585 | (let ((load-path (cons pkg-dir load-path))) | |
586 | (byte-recompile-directory pkg-dir 0 t))))) | |
587 | ||
bc44bef7 PH |
588 | (defun package--write-file-no-coding (file-name excl) |
589 | (let ((buffer-file-coding-system 'no-conversion)) | |
590 | (write-region (point-min) (point-max) file-name nil nil nil excl))) | |
591 | ||
44198b6e CY |
592 | (defun package-unpack-single (file-name version desc requires) |
593 | "Install the contents of the current buffer as a package." | |
594 | ;; Special case "package". | |
595 | (if (string= file-name "package") | |
bc44bef7 PH |
596 | (package--write-file-no-coding |
597 | (expand-file-name (concat file-name ".el") package-user-dir) | |
598 | nil) | |
44198b6e CY |
599 | (let* ((pkg-dir (expand-file-name (concat file-name "-" version) |
600 | package-user-dir)) | |
601 | (el-file (expand-file-name (concat file-name ".el") pkg-dir)) | |
602 | (pkg-file (expand-file-name (concat file-name "-pkg.el") pkg-dir))) | |
603 | (make-directory pkg-dir t) | |
bc44bef7 | 604 | (package--write-file-no-coding el-file 'excl) |
44198b6e CY |
605 | (let ((print-level nil) |
606 | (print-length nil)) | |
607 | (write-region | |
608 | (concat | |
609 | (prin1-to-string | |
610 | (list 'define-package | |
611 | file-name | |
612 | version | |
613 | desc | |
614 | (list 'quote | |
615 | ;; Turn version lists into string form. | |
616 | (mapcar | |
617 | (lambda (elt) | |
618 | (list (car elt) | |
619 | (package-version-join (car (cdr elt))))) | |
620 | requires)))) | |
621 | "\n") | |
622 | nil | |
623 | pkg-file | |
624 | nil nil nil 'excl)) | |
625 | (package-generate-autoloads file-name pkg-dir) | |
626 | (let ((load-path (cons pkg-dir load-path))) | |
627 | (byte-recompile-directory pkg-dir 0 t))))) | |
628 | ||
629 | (defun package-handle-response () | |
630 | "Handle the response from the server. | |
631 | Parse the HTTP response and throw if an error occurred. | |
632 | The url package seems to require extra processing for this. | |
633 | This should be called in a `save-excursion', in the download buffer. | |
634 | It will move point to somewhere in the headers." | |
635 | ;; We assume HTTP here. | |
636 | (require 'url-http) | |
637 | (let ((response (url-http-parse-response))) | |
638 | (when (or (< response 200) (>= response 300)) | |
639 | (display-buffer (current-buffer)) | |
640 | (error "Error during download request:%s" | |
641 | (buffer-substring-no-properties (point) (progn | |
642 | (end-of-line) | |
643 | (point))))))) | |
644 | ||
645 | (defun package-download-single (name version desc requires) | |
646 | "Download and install a single-file package." | |
647 | (let ((buffer (url-retrieve-synchronously | |
063e5294 | 648 | (concat (package-archive-url name) |
44198b6e CY |
649 | (symbol-name name) "-" version ".el")))) |
650 | (with-current-buffer buffer | |
651 | (package-handle-response) | |
652 | (re-search-forward "^$" nil 'move) | |
653 | (forward-char) | |
654 | (delete-region (point-min) (point)) | |
655 | (package-unpack-single (symbol-name name) version desc requires) | |
656 | (kill-buffer buffer)))) | |
657 | ||
658 | (defun package-download-tar (name version) | |
659 | "Download and install a tar package." | |
660 | (let ((tar-buffer (url-retrieve-synchronously | |
063e5294 | 661 | (concat (package-archive-url name) |
44198b6e CY |
662 | (symbol-name name) "-" version ".tar")))) |
663 | (with-current-buffer tar-buffer | |
664 | (package-handle-response) | |
665 | (re-search-forward "^$" nil 'move) | |
666 | (forward-char) | |
667 | (package-unpack name version) | |
668 | (kill-buffer tar-buffer)))) | |
669 | ||
bc44bef7 | 670 | (defun package-installed-p (package &optional min-version) |
44198b6e CY |
671 | (let ((pkg-desc (assq package package-alist))) |
672 | (and pkg-desc | |
148cef8e CY |
673 | (version-list-<= min-version |
674 | (package-desc-vers (cdr pkg-desc)))))) | |
44198b6e CY |
675 | |
676 | (defun package-compute-transaction (result requirements) | |
677 | (dolist (elt requirements) | |
678 | (let* ((next-pkg (car elt)) | |
679 | (next-version (cadr elt))) | |
680 | (unless (package-installed-p next-pkg next-version) | |
681 | ;; A package is required, but not installed. It might also be | |
682 | ;; blocked via `package-load-list'. | |
683 | (let ((pkg-desc (assq next-pkg package-archive-contents)) | |
684 | hold) | |
685 | (when (setq hold (assq next-pkg package-load-list)) | |
686 | (setq hold (cadr hold)) | |
687 | (cond ((eq hold nil) | |
688 | (error "Required package '%s' is disabled" | |
689 | (symbol-name next-pkg))) | |
690 | ((null (stringp hold)) | |
691 | (error "Invalid element in `package-load-list'")) | |
148cef8e | 692 | ((version-list-< (version-to-list hold) next-version) |
44198b6e CY |
693 | (error "Package '%s' held at version %s, \ |
694 | but version %s required" | |
695 | (symbol-name next-pkg) hold | |
696 | (package-version-join next-version))))) | |
697 | (unless pkg-desc | |
698 | (error "Package '%s' is not available for installation" | |
699 | (symbol-name next-pkg))) | |
148cef8e CY |
700 | (unless (version-list-<= next-version |
701 | (package-desc-vers (cdr pkg-desc))) | |
44198b6e CY |
702 | (error |
703 | "Need package '%s' with version %s, but only %s is available" | |
704 | (symbol-name next-pkg) (package-version-join next-version) | |
705 | (package-version-join (package-desc-vers (cdr pkg-desc))))) | |
706 | ;; Only add to the transaction if we don't already have it. | |
707 | (unless (memq next-pkg result) | |
708 | (setq result (cons next-pkg result))) | |
709 | (setq result | |
710 | (package-compute-transaction result | |
711 | (package-desc-reqs | |
712 | (cdr pkg-desc)))))))) | |
713 | result) | |
714 | ||
715 | (defun package-read-from-string (str) | |
716 | "Read a Lisp expression from STR. | |
717 | Signal an error if the entire string was not used." | |
718 | (let* ((read-data (read-from-string str)) | |
719 | (more-left | |
720 | (condition-case nil | |
721 | ;; The call to `ignore' suppresses a compiler warning. | |
722 | (progn (ignore (read-from-string | |
723 | (substring str (cdr read-data)))) | |
724 | t) | |
725 | (end-of-file nil)))) | |
726 | (if more-left | |
727 | (error "Can't read whole string") | |
728 | (car read-data)))) | |
729 | ||
730 | (defun package--read-archive-file (file) | |
731 | "Re-read archive file FILE, if it exists. | |
732 | Will return the data from the file, or nil if the file does not exist. | |
733 | Will throw an error if the archive version is too new." | |
734 | (let ((filename (expand-file-name file package-user-dir))) | |
735 | (if (file-exists-p filename) | |
736 | (with-temp-buffer | |
737 | (insert-file-contents-literally filename) | |
738 | (let ((contents (package-read-from-string | |
739 | (buffer-substring-no-properties (point-min) | |
740 | (point-max))))) | |
741 | (if (> (car contents) package-archive-version) | |
742 | (error "Package archive version %d is greater than %d - upgrade package.el" | |
743 | (car contents) package-archive-version)) | |
744 | (cdr contents)))))) | |
745 | ||
bc44bef7 | 746 | (defun package-read-all-archive-contents () |
44198b6e CY |
747 | "Re-read `archive-contents' and `builtin-packages', if they exist. |
748 | Set `package-archive-contents' and `package--builtins' if successful. | |
749 | Throw an error if the archive version is too new." | |
bc44bef7 PH |
750 | (dolist (archive package-archives) |
751 | (package-read-archive-contents (car archive))) | |
752 | (let ((builtins (package--read-archive-file "builtin-packages"))) | |
44198b6e CY |
753 | (if builtins |
754 | ;; Version 1 of 'builtin-packages' is a list where the car is | |
755 | ;; a split emacs version and the cdr is an alist suitable for | |
756 | ;; package--builtins. | |
148cef8e | 757 | (let ((our-version (version-to-list emacs-version)) |
44198b6e CY |
758 | (result package--builtins-base)) |
759 | (setq package--builtins | |
760 | (dolist (elt builtins result) | |
148cef8e | 761 | (if (version-list-<= (car elt) our-version) |
44198b6e CY |
762 | (setq result (append (cdr elt) result))))))))) |
763 | ||
bc44bef7 PH |
764 | (defun package-read-archive-contents (archive) |
765 | "Re-read `archive-contents' and `builtin-packages' for ARCHIVE. | |
766 | If successful, set `package-archive-contents' and `package--builtins'. | |
767 | If the archive version is too new, signal an error." | |
768 | (let ((archive-contents (package--read-archive-file | |
769 | (concat "archives/" archive | |
770 | "/archive-contents")))) | |
771 | (if archive-contents | |
772 | ;; Version 1 of 'archive-contents' is identical to our | |
773 | ;; internal representation. | |
774 | ;; TODO: merge archive lists | |
775 | (dolist (package archive-contents) | |
776 | (package--add-to-archive-contents package archive))))) | |
777 | ||
778 | (defun package--add-to-archive-contents (package archive) | |
779 | "Add the PACKAGE from the given ARCHIVE if necessary. | |
780 | Also, add the originating archive to the end of the package vector." | |
781 | (let* ((name (car package)) | |
782 | (version (aref (cdr package) 0)) | |
783 | (entry (cons (car package) | |
784 | (vconcat (cdr package) (vector archive)))) | |
785 | (existing-package (cdr (assq name package-archive-contents)))) | |
786 | (when (or (not existing-package) | |
148cef8e | 787 | (version-list-< (aref existing-package 0) version)) |
bc44bef7 PH |
788 | (add-to-list 'package-archive-contents entry)))) |
789 | ||
44198b6e CY |
790 | (defun package-download-transaction (transaction) |
791 | "Download and install all the packages in the given transaction." | |
792 | (dolist (elt transaction) | |
793 | (let* ((desc (cdr (assq elt package-archive-contents))) | |
794 | ;; As an exception, if package is "held" in | |
795 | ;; `package-load-list', download the held version. | |
796 | (hold (cadr (assq elt package-load-list))) | |
797 | (v-string (or (and (stringp hold) hold) | |
798 | (package-version-join (package-desc-vers desc)))) | |
799 | (kind (package-desc-kind desc))) | |
800 | (cond | |
801 | ((eq kind 'tar) | |
802 | (package-download-tar elt v-string)) | |
803 | ((eq kind 'single) | |
804 | (package-download-single elt v-string | |
805 | (package-desc-doc desc) | |
806 | (package-desc-reqs desc))) | |
807 | (t | |
808 | (error "Unknown package kind: %s" (symbol-name kind))))))) | |
809 | ||
810 | ;;;###autoload | |
811 | (defun package-install (name) | |
812 | "Install the package named NAME. | |
813 | Interactively, prompt for the package name. | |
063e5294 | 814 | The package is found on one of the archives in `package-archives'." |
44198b6e | 815 | (interactive |
bc44bef7 PH |
816 | (list (intern (completing-read "Install package: " |
817 | (mapcar (lambda (elt) | |
818 | (cons (symbol-name (car elt)) | |
819 | nil)) | |
820 | package-archive-contents) | |
821 | nil t)))) | |
44198b6e CY |
822 | (let ((pkg-desc (assq name package-archive-contents))) |
823 | (unless pkg-desc | |
bc44bef7 | 824 | (error "Package '%s' is not available for installation" |
44198b6e | 825 | (symbol-name name))) |
bc44bef7 PH |
826 | (package-download-transaction |
827 | (package-compute-transaction (list name) | |
828 | (package-desc-reqs (cdr pkg-desc))))) | |
44198b6e CY |
829 | ;; Try to activate it. |
830 | (package-initialize)) | |
831 | ||
832 | (defun package-strip-rcs-id (v-str) | |
833 | "Strip RCS version ID from the version string. | |
834 | If the result looks like a dotted numeric version, return it. | |
835 | Otherwise return nil." | |
836 | (if v-str | |
837 | (if (string-match "^[ \t]*[$]Revision:[ \t]\([0-9.]+\)[ \t]*[$]$" v-str) | |
838 | (match-string 1 v-str) | |
839 | (if (string-match "^[0-9.]*$" v-str) | |
840 | v-str)))) | |
841 | ||
842 | (defun package-buffer-info () | |
843 | "Return a vector of information about the package in the current buffer. | |
844 | The vector looks like [FILENAME REQUIRES DESCRIPTION VERSION COMMENTARY] | |
845 | FILENAME is the file name, a string. It does not have the \".el\" extension. | |
846 | REQUIRES is a requires list, or nil. | |
847 | DESCRIPTION is the package description (a string). | |
848 | VERSION is the version, a string. | |
849 | COMMENTARY is the commentary section, a string, or nil if none. | |
850 | Throws an exception if the buffer does not contain a conforming package. | |
851 | If there is a package, narrows the buffer to the file's boundaries. | |
852 | May narrow buffer or move point even on failure." | |
853 | (goto-char (point-min)) | |
854 | (if (re-search-forward "^;;; \\([^ ]*\\)\\.el --- \\(.*\\)$" nil t) | |
855 | (let ((file-name (match-string 1)) | |
856 | (desc (match-string 2)) | |
857 | (start (progn (beginning-of-line) (point)))) | |
858 | (if (search-forward (concat ";;; " file-name ".el ends here")) | |
859 | (progn | |
860 | ;; Try to include a trailing newline. | |
861 | (forward-line) | |
862 | (narrow-to-region start (point)) | |
863 | (require 'lisp-mnt) | |
864 | ;; Use some headers we've invented to drive the process. | |
865 | (let* ((requires-str (lm-header "package-requires")) | |
866 | (requires (if requires-str | |
867 | (package-read-from-string requires-str))) | |
868 | ;; Prefer Package-Version, because if it is | |
869 | ;; defined the package author probably wants us | |
870 | ;; to use it. Otherwise try Version. | |
871 | (pkg-version | |
872 | (or (package-strip-rcs-id (lm-header "package-version")) | |
873 | (package-strip-rcs-id (lm-header "version")))) | |
874 | (commentary (lm-commentary))) | |
875 | (unless pkg-version | |
876 | (error | |
877 | "Package does not define a usable \"Version\" or \"Package-Version\" header")) | |
878 | ;; Turn string version numbers into list form. | |
879 | (setq requires | |
880 | (mapcar | |
881 | (lambda (elt) | |
882 | (list (car elt) | |
148cef8e | 883 | (version-to-list (car (cdr elt))))) |
44198b6e CY |
884 | requires)) |
885 | (set-text-properties 0 (length file-name) nil file-name) | |
886 | (set-text-properties 0 (length pkg-version) nil pkg-version) | |
887 | (set-text-properties 0 (length desc) nil desc) | |
888 | (vector file-name requires desc pkg-version commentary))) | |
889 | (error "Package missing a terminating comment"))) | |
890 | (error "No starting comment for package"))) | |
891 | ||
892 | (defun package-tar-file-info (file) | |
893 | "Find package information for a tar file. | |
894 | FILE is the name of the tar file to examine. | |
895 | The return result is a vector like `package-buffer-info'." | |
896 | (unless (string-match "^\\(.+\\)-\\([0-9.]+\\)\\.tar$" file) | |
897 | (error "`%s' doesn't have a package-ish name" file)) | |
898 | (let* ((pkg-name (file-name-nondirectory (match-string-no-properties 1 file))) | |
899 | (pkg-version (match-string-no-properties 2 file)) | |
900 | ;; Extract the package descriptor. | |
901 | (pkg-def-contents (shell-command-to-string | |
902 | ;; Requires GNU tar. | |
903 | (concat "tar -xOf " file " " | |
904 | pkg-name "-" pkg-version "/" | |
905 | pkg-name "-pkg.el"))) | |
906 | (pkg-def-parsed (package-read-from-string pkg-def-contents))) | |
907 | (unless (eq (car pkg-def-parsed) 'define-package) | |
908 | (error "%s-pkg.el doesn't contain `define-package' sexp" pkg-name)) | |
909 | (let ((name-str (nth 1 pkg-def-parsed)) | |
910 | (version-string (nth 2 pkg-def-parsed)) | |
911 | (docstring (nth 3 pkg-def-parsed)) | |
912 | (requires (nth 4 pkg-def-parsed)) | |
913 | ||
914 | (readme (shell-command-to-string | |
915 | ;; Requires GNU tar. | |
916 | (concat "tar -xOf " file " " | |
917 | pkg-name "-" pkg-version "/README")))) | |
918 | (unless (equal pkg-version version-string) | |
919 | (error "Inconsistent versions!")) | |
920 | (unless (equal pkg-name name-str) | |
921 | (error "Inconsistent names!")) | |
922 | ;; Kind of a hack. | |
923 | (if (string-match ": Not found in archive" readme) | |
924 | (setq readme nil)) | |
925 | ;; Turn string version numbers into list form. | |
926 | (if (eq (car requires) 'quote) | |
927 | (setq requires (car (cdr requires)))) | |
928 | (setq requires | |
929 | (mapcar | |
930 | (lambda (elt) | |
931 | (list (car elt) | |
148cef8e | 932 | (version-to-list (car (cdr elt))))) |
44198b6e CY |
933 | requires)) |
934 | (vector pkg-name requires docstring version-string readme)))) | |
935 | ||
936 | (defun package-install-buffer-internal (pkg-info type) | |
937 | (save-excursion | |
938 | (save-restriction | |
939 | (let* ((file-name (aref pkg-info 0)) | |
940 | (requires (aref pkg-info 1)) | |
941 | (desc (if (string= (aref pkg-info 2) "") | |
942 | "No description available." | |
943 | (aref pkg-info 2))) | |
944 | (pkg-version (aref pkg-info 3))) | |
945 | ;; Download and install the dependencies. | |
946 | (let ((transaction (package-compute-transaction nil requires))) | |
947 | (package-download-transaction transaction)) | |
948 | ;; Install the package itself. | |
949 | (cond | |
950 | ((eq type 'single) | |
951 | (package-unpack-single file-name pkg-version desc requires)) | |
952 | ((eq type 'tar) | |
953 | (package-unpack (intern file-name) pkg-version)) | |
954 | (t | |
955 | (error "Unknown type: %s" (symbol-name type)))) | |
956 | ;; Try to activate it. | |
957 | (package-initialize))))) | |
958 | ||
959 | ;;;###autoload | |
960 | (defun package-install-from-buffer () | |
961 | "Install a package from the current buffer. | |
962 | The package is assumed to be a single .el file which | |
963 | follows the elisp comment guidelines; see | |
964 | info node `(elisp)Library Headers'." | |
965 | (interactive) | |
966 | (package-install-buffer-internal (package-buffer-info) 'single)) | |
967 | ||
968 | ;;;###autoload | |
969 | (defun package-install-file (file) | |
970 | "Install a package from a file. | |
971 | The file can either be a tar file or an Emacs Lisp file." | |
972 | (interactive "fPackage file name: ") | |
973 | (with-temp-buffer | |
974 | (insert-file-contents-literally file) | |
975 | (cond | |
976 | ((string-match "\\.el$" file) (package-install-from-buffer)) | |
977 | ((string-match "\\.tar$" file) | |
978 | (package-install-buffer-internal (package-tar-file-info file) 'tar)) | |
979 | (t (error "Unrecognized extension `%s'" (file-name-extension file)))))) | |
980 | ||
981 | (defun package-delete (name version) | |
982 | (require 'dired) ; for dired-delete-file | |
983 | (dired-delete-file (expand-file-name (concat name "-" version) | |
984 | package-user-dir) | |
985 | ;; FIXME: query user? | |
986 | 'always)) | |
987 | ||
063e5294 | 988 | (defun package-archive-url (name) |
bc44bef7 PH |
989 | "Return the archive containing the package NAME." |
990 | (let ((desc (cdr (assq (intern-soft name) package-archive-contents)))) | |
991 | (cdr (assoc (aref desc (- (length desc) 1)) package-archives)))) | |
992 | ||
993 | (defun package--download-one-archive (archive file) | |
994 | "Download an archive file FILE from ARCHIVE, and cache it locally." | |
995 | (let* ((archive-name (car archive)) | |
996 | (archive-url (cdr archive)) | |
997 | (dir (expand-file-name "archives" package-user-dir)) | |
998 | (dir (expand-file-name archive-name dir)) | |
999 | (buffer (url-retrieve-synchronously (concat archive-url file)))) | |
44198b6e CY |
1000 | (with-current-buffer buffer |
1001 | (package-handle-response) | |
1002 | (re-search-forward "^$" nil 'move) | |
1003 | (forward-char) | |
1004 | (delete-region (point-min) (point)) | |
bc44bef7 PH |
1005 | (make-directory dir t) |
1006 | (setq buffer-file-name (expand-file-name file dir)) | |
44198b6e | 1007 | (let ((version-control 'never)) |
bc44bef7 PH |
1008 | (save-buffer))) |
1009 | (kill-buffer buffer))) | |
44198b6e CY |
1010 | |
1011 | (defun package-refresh-contents () | |
1012 | "Download the ELPA archive description if needed. | |
1013 | Invoking this will ensure that Emacs knows about the latest versions | |
1014 | of all packages. This will let Emacs make them available for | |
1015 | download." | |
1016 | (interactive) | |
1017 | (unless (file-exists-p package-user-dir) | |
1018 | (make-directory package-user-dir t)) | |
bc44bef7 | 1019 | (dolist (archive package-archives) |
cb6c4991 CY |
1020 | (condition-case nil |
1021 | (package--download-one-archive archive "archive-contents") | |
1022 | (error (message "Failed to download archive `%s'." | |
1023 | (car archive))))) | |
bc44bef7 | 1024 | (package-read-all-archive-contents)) |
44198b6e CY |
1025 | |
1026 | ;;;###autoload | |
1027 | (defun package-initialize () | |
1028 | "Load Emacs Lisp packages, and activate them. | |
1029 | The variable `package-load-list' controls which packages to load." | |
1030 | (interactive) | |
1031 | (setq package-obsolete-alist nil) | |
1032 | (package-load-all-descriptors) | |
bc44bef7 | 1033 | (package-read-all-archive-contents) |
44198b6e CY |
1034 | ;; Try to activate all our packages. |
1035 | (mapc (lambda (elt) | |
1036 | (package-activate (car elt) (package-desc-vers (cdr elt)))) | |
1037 | package-alist)) | |
1038 | ||
1039 | \f | |
cced7584 | 1040 | ;;;; Package description buffer. |
44198b6e | 1041 | |
cced7584 CY |
1042 | ;;;###autoload |
1043 | (defun describe-package (package) | |
1044 | "Display the full documentation of PACKAGE (a symbol)." | |
1045 | (interactive | |
1046 | (let* ((packages (append (mapcar 'car package-alist) | |
1047 | (mapcar 'car package-archive-contents))) | |
1048 | (guess (function-called-at-point)) | |
1049 | val) | |
1050 | (unless (memq guess packages) | |
1051 | (setq guess nil)) | |
1052 | (setq packages (mapcar 'symbol-name packages)) | |
1053 | (setq val | |
1054 | (completing-read (if guess | |
1055 | (format "Describe package (default %s): " | |
1056 | guess) | |
1057 | "Describe package: ") | |
1058 | packages nil t nil nil guess)) | |
cb6c4991 | 1059 | (list (if (equal val "") guess (intern val))))) |
cced7584 CY |
1060 | (if (or (null package) (null (symbolp package))) |
1061 | (message "You did not specify a package") | |
1062 | (help-setup-xref (list #'describe-package package) | |
1063 | (called-interactively-p 'interactive)) | |
1064 | (with-help-window (help-buffer) | |
1065 | (with-current-buffer standard-output | |
1066 | (describe-package-1 package))))) | |
1067 | ||
1068 | (defun describe-package-1 (package) | |
cb6c4991 CY |
1069 | (let ((package-name (symbol-name package)) |
1070 | (built-in (assq package package--builtins)) | |
1071 | desc pkg-dir reqs version installable) | |
cced7584 CY |
1072 | (prin1 package) |
1073 | (princ " is ") | |
cb6c4991 CY |
1074 | (if (setq desc (cdr (assq package package-alist))) |
1075 | ;; This package is loaded (i.e. in `package-alist'). | |
1076 | (progn | |
1077 | (setq version (package-version-join (package-desc-vers desc))) | |
1078 | (cond (built-in | |
1079 | (princ "a built-in package.\n\n")) | |
1080 | ((setq pkg-dir (package--dir package-name version)) | |
1081 | (insert "an installed package.\n\n")) | |
1082 | (t ;; This normally does not happen. | |
1083 | (insert "a deleted package.\n\n") | |
1084 | (setq version nil)))) | |
1085 | ;; This package is not installed. | |
1086 | (setq desc (cdr (assq package package-archive-contents)) | |
8adb4c33 CY |
1087 | version (package-version-join (package-desc-vers desc)) |
1088 | installable t) | |
cb6c4991 CY |
1089 | (insert "an uninstalled package.\n\n")) |
1090 | ||
1091 | (insert " " (propertize "Status" 'face 'bold) ": ") | |
1092 | (cond (pkg-dir | |
1093 | (insert (propertize "Installed" 'face 'font-lock-comment-face)) | |
1094 | (insert " in `") | |
1095 | ;; Todo: Add button for uninstalling. | |
1096 | (help-insert-xref-button (file-name-as-directory pkg-dir) | |
1097 | 'help-package-def pkg-dir) | |
1098 | (insert "'.")) | |
1099 | (installable | |
1100 | (insert "Available -- ") | |
1101 | (let ((button-text (if (display-graphic-p) | |
1102 | "Install" | |
1103 | "[Install]")) | |
1104 | (button-face (if (display-graphic-p) | |
1105 | '(:box (:line-width 2 :color "dark grey") | |
1106 | :background "light grey" | |
1107 | :foreground "black") | |
1108 | 'link))) | |
1109 | (insert-text-button button-text | |
1110 | 'face button-face | |
1111 | 'follow-link t | |
1112 | 'package-symbol package | |
1113 | 'action 'package-install-button-action))) | |
1114 | (built-in | |
1115 | (insert (propertize "Built-in" 'face 'font-lock-builtin-face) ".")) | |
1116 | (t (insert "Deleted."))) | |
1117 | (insert "\n") | |
1118 | (when version | |
1119 | (insert " " (propertize "Version" 'face 'bold) ": " version "\n")) | |
8adb4c33 CY |
1120 | (setq reqs (package-desc-reqs desc)) |
1121 | (when reqs | |
cb6c4991 | 1122 | (insert " " (propertize "Requires" 'face 'bold) ": ") |
8adb4c33 CY |
1123 | (let ((first t) |
1124 | name vers text) | |
1125 | (dolist (req reqs) | |
1126 | (setq name (car req) | |
1127 | vers (cadr req) | |
1128 | text (format "%s-%s" (symbol-name name) | |
1129 | (package-version-join vers))) | |
1130 | (cond (first (setq first nil)) | |
1131 | ((>= (+ 2 (current-column) (length text)) | |
1132 | (window-width)) | |
1133 | (insert ",\n ")) | |
1134 | (t (insert ", "))) | |
1135 | (help-insert-xref-button text 'help-package name)) | |
1136 | (insert "\n"))) | |
cb6c4991 CY |
1137 | (insert " " (propertize "Summary" 'face 'bold) |
1138 | ": " (package-desc-doc desc) "\n\n") | |
1139 | ||
1140 | ;; Insert the package commentary. | |
1141 | ;; FIXME: We should try to be smarter about when to download. | |
1142 | (let ((readme (expand-file-name (concat package-name "-readme.txt") | |
1143 | package-user-dir))) | |
1144 | ;; Try downloading the commentary. If that fails, try an | |
1145 | ;; existing readme file in `package-user-dir'. | |
1146 | (cond ((let ((buffer | |
1147 | (condition-case nil | |
1148 | (url-retrieve-synchronously | |
1149 | (concat (package-archive-url package) | |
1150 | package-name "-readme.txt")) | |
1151 | (error nil))) | |
1152 | response) | |
1153 | (when buffer | |
1154 | (with-current-buffer buffer | |
1155 | (setq response (url-http-parse-response)) | |
1156 | (if (or (< response 200) (>= response 300)) | |
1157 | (setq response nil) | |
1158 | (setq buffer-file-name | |
1159 | (expand-file-name readme package-user-dir)) | |
1160 | (delete-region (point-min) (1+ url-http-end-of-headers)) | |
1161 | (save-buffer))) | |
1162 | (when response | |
1163 | (insert-buffer-substring buffer) | |
1164 | (kill-buffer buffer) | |
1165 | t)))) | |
1166 | ((file-readable-p readme) | |
1167 | (insert-file-contents readme) | |
1168 | (goto-char (point-max))))))) | |
1169 | ||
1170 | (defun package-install-button-action (button) | |
1171 | (let ((package (button-get button 'package-symbol))) | |
1172 | (when (y-or-n-p (format "Install package `%s'? " package)) | |
1173 | (package-install package) | |
1174 | (revert-buffer nil t) | |
1175 | (goto-char (point-min))))) | |
cced7584 CY |
1176 | |
1177 | \f | |
44198b6e CY |
1178 | ;;;; Package menu mode. |
1179 | ||
54ea2a0d | 1180 | (defvar package-menu-mode-map |
64eba874 DN |
1181 | (let ((map (make-keymap)) |
1182 | (menu-map (make-sparse-keymap "Package"))) | |
54ea2a0d | 1183 | (suppress-keymap map) |
8adb4c33 | 1184 | (define-key map "\C-m" 'package-menu-describe-package) |
54ea2a0d JB |
1185 | (define-key map "q" 'quit-window) |
1186 | (define-key map "n" 'next-line) | |
1187 | (define-key map "p" 'previous-line) | |
1188 | (define-key map "u" 'package-menu-mark-unmark) | |
1189 | (define-key map "\177" 'package-menu-backup-unmark) | |
1190 | (define-key map "d" 'package-menu-mark-delete) | |
1191 | (define-key map "i" 'package-menu-mark-install) | |
1192 | (define-key map "g" 'package-menu-revert) | |
1193 | (define-key map "r" 'package-menu-refresh) | |
1194 | (define-key map "~" 'package-menu-mark-obsolete-for-deletion) | |
1195 | (define-key map "x" 'package-menu-execute) | |
1196 | (define-key map "h" 'package-menu-quick-help) | |
cb6c4991 | 1197 | (define-key map "?" 'package-menu-describe-package) |
64eba874 DN |
1198 | (define-key map [menu-bar package-menu] (cons "Package" menu-map)) |
1199 | (define-key menu-map [mq] | |
1200 | '(menu-item "Quit" quit-window | |
1201 | :help "Quit package selection")) | |
1202 | (define-key menu-map [s1] '("--")) | |
1203 | (define-key menu-map [mn] | |
1204 | '(menu-item "Next" next-line | |
1205 | :help "Next Line")) | |
1206 | (define-key menu-map [mp] | |
1207 | '(menu-item "Previous" previous-line | |
1208 | :help "Previous Line")) | |
1209 | (define-key menu-map [s2] '("--")) | |
1210 | (define-key menu-map [mu] | |
1211 | '(menu-item "Unmark" package-menu-mark-unmark | |
1212 | :help "Clear any marks on a package and move to the next line")) | |
1213 | (define-key menu-map [munm] | |
1214 | '(menu-item "Unmark backwards" package-menu-backup-unmark | |
1215 | :help "Back up one line and clear any marks on that package")) | |
1216 | (define-key menu-map [md] | |
1217 | '(menu-item "Mark for deletion" package-menu-mark-delete | |
1218 | :help "Mark a package for deletion and move to the next line")) | |
1219 | (define-key menu-map [mi] | |
1220 | '(menu-item "Mark for install" package-menu-mark-install | |
1221 | :help "Mark a package for installation and move to the next line")) | |
1222 | (define-key menu-map [s3] '("--")) | |
1223 | (define-key menu-map [mg] | |
1224 | '(menu-item "Update package list" package-menu-revert | |
1225 | :help "Update the list of packages")) | |
1226 | (define-key menu-map [mr] | |
1227 | '(menu-item "Refresh package list" package-menu-refresh | |
1228 | :help "Download the ELPA archive")) | |
1229 | (define-key menu-map [s4] '("--")) | |
1230 | (define-key menu-map [mt] | |
1231 | '(menu-item "Mark obsolete packages" package-menu-mark-obsolete-for-deletion | |
1232 | :help "Mark all obsolete packages for deletion")) | |
1233 | (define-key menu-map [mx] | |
1234 | '(menu-item "Execute actions" package-menu-execute | |
1235 | :help "Perform all the marked actions")) | |
1236 | (define-key menu-map [s5] '("--")) | |
1237 | (define-key menu-map [mh] | |
1238 | '(menu-item "Help" package-menu-quick-help | |
1239 | :help "Show short key binding help for package-menu-mode")) | |
1240 | (define-key menu-map [mc] | |
1241 | '(menu-item "View Commentary" package-menu-view-commentary | |
1242 | :help "Display information about this package")) | |
54ea2a0d | 1243 | map) |
44198b6e CY |
1244 | "Local keymap for `package-menu-mode' buffers.") |
1245 | ||
44198b6e CY |
1246 | (defvar package-menu-sort-button-map |
1247 | (let ((map (make-sparse-keymap))) | |
1248 | (define-key map [header-line mouse-1] 'package-menu-sort-by-column) | |
1249 | (define-key map [follow-link] 'mouse-face) | |
1250 | map) | |
1251 | "Local keymap for package menu sort buttons.") | |
1252 | ||
1253 | (put 'package-menu-mode 'mode-class 'special) | |
1254 | ||
1255 | (defun package-menu-mode () | |
1256 | "Major mode for browsing a list of packages. | |
1257 | Letters do not insert themselves; instead, they are commands. | |
1258 | \\<package-menu-mode-map> | |
1259 | \\{package-menu-mode-map}" | |
1260 | (kill-all-local-variables) | |
1261 | (use-local-map package-menu-mode-map) | |
1262 | (setq major-mode 'package-menu-mode) | |
1263 | (setq mode-name "Package Menu") | |
1264 | (setq truncate-lines t) | |
1265 | (setq buffer-read-only t) | |
1266 | ;; Support Emacs 21. | |
1267 | (if (fboundp 'run-mode-hooks) | |
1268 | (run-mode-hooks 'package-menu-mode-hook) | |
1269 | (run-hooks 'package-menu-mode-hook))) | |
1270 | ||
1271 | (defun package-menu-refresh () | |
1272 | "Download the ELPA archive. | |
1273 | This fetches the file describing the current contents of | |
1274 | the Emacs Lisp Package Archive, and then refreshes the | |
1275 | package menu. This lets you see what new packages are | |
1276 | available for download." | |
1277 | (interactive) | |
1278 | (package-refresh-contents) | |
1279 | (package-list-packages-internal)) | |
1280 | ||
1281 | (defun package-menu-revert () | |
1282 | "Update the list of packages." | |
1283 | (interactive) | |
1284 | (package-list-packages-internal)) | |
1285 | ||
8adb4c33 CY |
1286 | (defun package-menu-describe-package () |
1287 | "Describe the package in the current line." | |
1288 | (interactive) | |
1289 | (let ((name (package-menu-get-package))) | |
1290 | (if name | |
1291 | (describe-package (intern name)) | |
1292 | (message "No package on this line")))) | |
1293 | ||
44198b6e CY |
1294 | (defun package-menu-mark-internal (what) |
1295 | (unless (eobp) | |
1296 | (let ((buffer-read-only nil)) | |
1297 | (beginning-of-line) | |
1298 | (delete-char 1) | |
1299 | (insert what) | |
1300 | (forward-line)))) | |
1301 | ||
1302 | ;; fixme numeric argument | |
1303 | (defun package-menu-mark-delete (num) | |
1304 | "Mark a package for deletion and move to the next line." | |
1305 | (interactive "p") | |
1306 | (package-menu-mark-internal "D")) | |
1307 | ||
1308 | (defun package-menu-mark-install (num) | |
1309 | "Mark a package for installation and move to the next line." | |
1310 | (interactive "p") | |
1311 | (package-menu-mark-internal "I")) | |
1312 | ||
1313 | (defun package-menu-mark-unmark (num) | |
1314 | "Clear any marks on a package and move to the next line." | |
1315 | (interactive "p") | |
1316 | (package-menu-mark-internal " ")) | |
1317 | ||
1318 | (defun package-menu-backup-unmark () | |
1319 | "Back up one line and clear any marks on that package." | |
1320 | (interactive) | |
1321 | (forward-line -1) | |
1322 | (package-menu-mark-internal " ") | |
1323 | (forward-line -1)) | |
1324 | ||
1325 | (defun package-menu-mark-obsolete-for-deletion () | |
1326 | "Mark all obsolete packages for deletion." | |
1327 | (interactive) | |
1328 | (save-excursion | |
1329 | (goto-char (point-min)) | |
1330 | (forward-line 2) | |
1331 | (while (not (eobp)) | |
1332 | (if (looking-at ".*\\s obsolete\\s ") | |
1333 | (package-menu-mark-internal "D") | |
1334 | (forward-line 1))))) | |
1335 | ||
1336 | (defun package-menu-quick-help () | |
1337 | "Show short key binding help for package-menu-mode." | |
1338 | (interactive) | |
1339 | (message "n-ext, i-nstall, d-elete, u-nmark, x-ecute, r-efresh, h-elp")) | |
1340 | ||
cb6c4991 CY |
1341 | (define-obsolete-function-alias |
1342 | 'package-menu-view-commentary 'package-menu-describe-package "24.1") | |
44198b6e CY |
1343 | |
1344 | ;; Return the name of the package on the current line. | |
1345 | (defun package-menu-get-package () | |
1346 | (save-excursion | |
1347 | (beginning-of-line) | |
1348 | (if (looking-at ". \\([^ \t]*\\)") | |
8adb4c33 | 1349 | (match-string-no-properties 1)))) |
44198b6e CY |
1350 | |
1351 | ;; Return the version of the package on the current line. | |
1352 | (defun package-menu-get-version () | |
1353 | (save-excursion | |
1354 | (beginning-of-line) | |
1355 | (if (looking-at ". [^ \t]*[ \t]*\\([0-9.]*\\)") | |
1356 | (match-string 1)))) | |
1357 | ||
1358 | (defun package-menu-get-status () | |
1359 | (save-excursion | |
1360 | (if (looking-at ". [^ \t]*[ \t]*[^ \t]*[ \t]*\\([^ \t]*\\)") | |
1361 | (match-string 1) | |
1362 | ""))) | |
1363 | ||
1364 | (defun package-menu-execute () | |
1365 | "Perform all the marked actions. | |
1366 | Packages marked for installation will be downloaded and | |
1367 | installed. Packages marked for deletion will be removed. | |
1368 | Note that after installing packages you will want to restart | |
1369 | Emacs." | |
1370 | (interactive) | |
1371 | (goto-char (point-min)) | |
44198b6e CY |
1372 | (while (not (eobp)) |
1373 | (let ((cmd (char-after)) | |
1374 | (pkg-name (package-menu-get-package)) | |
1375 | (pkg-vers (package-menu-get-version)) | |
1376 | (pkg-status (package-menu-get-status))) | |
1377 | (cond | |
1378 | ((eq cmd ?D) | |
1379 | (when (and (string= pkg-status "installed") | |
1380 | (string= pkg-name "package")) | |
1381 | ;; FIXME: actually, we could be tricky and remove all info. | |
1382 | ;; But that is drastic and the user can do that instead. | |
1383 | (error "Can't delete most recent version of `package'")) | |
1384 | ;; Ask for confirmation here? Maybe if package status is ""? | |
1385 | ;; Or if any lisp from package is actually loaded? | |
1386 | (message "Deleting %s-%s..." pkg-name pkg-vers) | |
1387 | (package-delete pkg-name pkg-vers) | |
1388 | (message "Deleting %s-%s... done" pkg-name pkg-vers)) | |
1389 | ((eq cmd ?I) | |
1390 | (package-install (intern pkg-name))))) | |
1391 | (forward-line)) | |
1392 | (package-menu-revert)) | |
1393 | ||
1394 | (defun package-print-package (package version key desc) | |
1395 | (let ((face | |
376c2b6b | 1396 | (cond ((string= key "built-in") 'font-lock-builtin-face) |
44198b6e CY |
1397 | ((string= key "available") 'default) |
1398 | ((string= key "held") 'font-lock-constant-face) | |
1399 | ((string= key "disabled") 'font-lock-warning-face) | |
1400 | ((string= key "installed") 'font-lock-comment-face) | |
1401 | (t ; obsolete, but also the default. | |
1402 | 'font-lock-warning-face)))) | |
1403 | (insert (propertize " " 'font-lock-face face)) | |
8adb4c33 CY |
1404 | (insert-text-button (symbol-name package) |
1405 | 'face 'link | |
1406 | 'follow-link t | |
1407 | 'package-symbol package | |
1408 | 'action (lambda (button) | |
1409 | (describe-package | |
1410 | (button-get button 'package-symbol)))) | |
44198b6e CY |
1411 | (indent-to 20 1) |
1412 | (insert (propertize (package-version-join version) 'font-lock-face face)) | |
8adb4c33 | 1413 | (indent-to 32 1) |
44198b6e CY |
1414 | (insert (propertize key 'font-lock-face face)) |
1415 | ;; FIXME: this 'when' is bogus... | |
1416 | (when desc | |
8adb4c33 | 1417 | (indent-to 43 1) |
376c2b6b CY |
1418 | (let ((opoint (point))) |
1419 | (insert (propertize desc 'font-lock-face face)) | |
1420 | (upcase-region opoint (min (point) (1+ opoint))))) | |
44198b6e CY |
1421 | (insert "\n"))) |
1422 | ||
1423 | (defun package-list-maybe-add (package version status description result) | |
1424 | (unless (assoc (cons package version) result) | |
1425 | (setq result (cons (list (cons package version) status description) | |
1426 | result))) | |
1427 | result) | |
1428 | ||
1429 | ;; This decides how we should sort; nil means by package name. | |
1430 | (defvar package-menu-sort-key nil) | |
1431 | ||
1432 | (defun package-list-packages-internal () | |
1433 | (package-initialize) ; FIXME: do this here? | |
1434 | (with-current-buffer (get-buffer-create "*Packages*") | |
1435 | (setq buffer-read-only nil) | |
1436 | (erase-buffer) | |
1437 | (let ((info-list) | |
376c2b6b CY |
1438 | name desc hold |
1439 | builtin) | |
44198b6e CY |
1440 | ;; List installed packages |
1441 | (dolist (elt package-alist) | |
376c2b6b | 1442 | ;; Ignore the Emacs package. |
44198b6e CY |
1443 | (setq name (car elt) |
1444 | desc (cdr elt) | |
1445 | hold (assq name package-load-list)) | |
cb6c4991 | 1446 | (unless (memq name '(emacs package)) |
376c2b6b CY |
1447 | (setq info-list |
1448 | (package-list-maybe-add | |
1449 | name (package-desc-vers desc) | |
1450 | ;; FIXME: it turns out to be tricky to see if this | |
1451 | ;; package is presently activated. | |
1452 | (cond ((stringp (cadr hold)) | |
1453 | "held") | |
1454 | ((and (setq builtin (assq name package--builtins)) | |
148cef8e | 1455 | (version-list-= |
376c2b6b | 1456 | (package-desc-vers (cdr builtin)) |
148cef8e | 1457 | (package-desc-vers desc))) |
376c2b6b CY |
1458 | "built-in") |
1459 | (t "installed")) | |
1460 | (package-desc-doc desc) | |
1461 | info-list)))) | |
44198b6e CY |
1462 | ;; List available packages |
1463 | (dolist (elt package-archive-contents) | |
1464 | (setq name (car elt) | |
1465 | desc (cdr elt) | |
1466 | hold (assq name package-load-list)) | |
1467 | (unless (and hold (stringp (cadr hold)) | |
1468 | (package-installed-p | |
148cef8e | 1469 | name (version-to-list (cadr hold)))) |
44198b6e CY |
1470 | (setq info-list |
1471 | (package-list-maybe-add name | |
1472 | (package-desc-vers desc) | |
1473 | (if (and hold (null (cadr hold))) | |
1474 | "disabled" | |
1475 | "available") | |
1476 | (package-desc-doc (cdr elt)) | |
1477 | info-list)))) | |
1478 | ;; List obsolete packages | |
1479 | (mapc (lambda (elt) | |
1480 | (mapc (lambda (inner-elt) | |
1481 | (setq info-list | |
1482 | (package-list-maybe-add (car elt) | |
1483 | (package-desc-vers | |
1484 | (cdr inner-elt)) | |
1485 | "obsolete" | |
1486 | (package-desc-doc | |
1487 | (cdr inner-elt)) | |
1488 | info-list))) | |
1489 | (cdr elt))) | |
1490 | package-obsolete-alist) | |
1491 | (let ((selector (cond | |
1492 | ((string= package-menu-sort-key "Version") | |
1493 | ;; FIXME this doesn't work. | |
1494 | #'(lambda (e) (cdr (car e)))) | |
1495 | ((string= package-menu-sort-key "Status") | |
1496 | #'(lambda (e) (car (cdr e)))) | |
1497 | ((string= package-menu-sort-key "Description") | |
1498 | #'(lambda (e) (car (cdr (cdr e))))) | |
1499 | (t ; "Package" is default. | |
1500 | #'(lambda (e) (symbol-name (car (car e)))))))) | |
1501 | (setq info-list | |
1502 | (sort info-list | |
1503 | (lambda (left right) | |
1504 | (let ((vleft (funcall selector left)) | |
1505 | (vright (funcall selector right))) | |
1506 | (string< vleft vright)))))) | |
1507 | (mapc (lambda (elt) | |
1508 | (package-print-package (car (car elt)) | |
1509 | (cdr (car elt)) | |
1510 | (car (cdr elt)) | |
1511 | (car (cdr (cdr elt))))) | |
1512 | info-list)) | |
1513 | (goto-char (point-min)) | |
1514 | (current-buffer))) | |
1515 | ||
1516 | (defun package-menu-sort-by-column (&optional e) | |
1517 | "Sort the package menu by the last column clicked on." | |
1518 | (interactive (list last-input-event)) | |
1519 | (if e (mouse-select-window e)) | |
1520 | (let* ((pos (event-start e)) | |
1521 | (obj (posn-object pos)) | |
1522 | (col (if obj | |
1523 | (get-text-property (cdr obj) 'column-name (car obj)) | |
1524 | (get-text-property (posn-point pos) 'column-name)))) | |
1525 | (setq package-menu-sort-key col)) | |
1526 | (package-list-packages-internal)) | |
1527 | ||
1528 | (defun package--list-packages () | |
1529 | "Display a list of packages. | |
1530 | Helper function that does all the work for the user-facing functions." | |
1531 | (with-current-buffer (package-list-packages-internal) | |
1532 | (package-menu-mode) | |
1533 | ;; Set up the header line. | |
1534 | (setq header-line-format | |
1535 | (mapconcat | |
1536 | (lambda (pair) | |
1537 | (let ((column (car pair)) | |
1538 | (name (cdr pair))) | |
1539 | (concat | |
1540 | ;; Insert a space that aligns the button properly. | |
1541 | (propertize " " 'display (list 'space :align-to column) | |
1542 | 'face 'fixed-pitch) | |
1543 | ;; Set up the column button. | |
1544 | (if (string= name "Version") | |
1545 | name | |
1546 | (propertize name | |
1547 | 'column-name name | |
1548 | 'help-echo "mouse-1: sort by column" | |
1549 | 'mouse-face 'highlight | |
1550 | 'keymap package-menu-sort-button-map))))) | |
1551 | ;; We take a trick from buff-menu and have a dummy leading | |
1552 | ;; space to align the header line with the beginning of the | |
1553 | ;; text. This doesn't really work properly on Emacs 21, | |
1554 | ;; but it is close enough. | |
1555 | '((0 . "") | |
1556 | (2 . "Package") | |
1557 | (20 . "Version") | |
376c2b6b CY |
1558 | (32 . "Status") |
1559 | (43 . "Description")) | |
44198b6e CY |
1560 | "")) |
1561 | ||
1562 | ;; It's okay to use pop-to-buffer here. The package menu buffer | |
1563 | ;; has keybindings, and the user just typed 'M-x | |
1564 | ;; package-list-packages', suggesting that they might want to use | |
1565 | ;; them. | |
1566 | (pop-to-buffer (current-buffer)))) | |
1567 | ||
1568 | ;;;###autoload | |
1569 | (defun package-list-packages () | |
1570 | "Display a list of packages. | |
1571 | Fetches the updated list of packages before displaying. | |
1572 | The list is displayed in a buffer named `*Packages*'." | |
1573 | (interactive) | |
1574 | (package-refresh-contents) | |
1575 | (package--list-packages)) | |
1576 | ||
cb8759ca CY |
1577 | ;;;###autoload |
1578 | (defalias 'list-packages 'package-list-packages) | |
1579 | ||
44198b6e CY |
1580 | (defun package-list-packages-no-fetch () |
1581 | "Display a list of packages. | |
1582 | Does not fetch the updated list of packages before displaying. | |
1583 | The list is displayed in a buffer named `*Packages*'." | |
1584 | (interactive) | |
1585 | (package--list-packages)) | |
1586 | ||
44198b6e CY |
1587 | (provide 'package) |
1588 | ||
1589 | ;;; package.el ends here |