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) | |
0be01d2c CY |
263 | (and (stringp f) |
264 | (equal (file-name-nondirectory f) "site-lisp") | |
265 | (push (expand-file-name "elpa" f) result))) | |
44198b6e CY |
266 | (nreverse result)) |
267 | "List of additional directories containing Emacs Lisp packages. | |
268 | Each directory name should be absolute. | |
269 | ||
270 | These directories contain packages intended for system-wide; in | |
271 | contrast, `package-user-dir' contains packages for personal use." | |
272 | :type '(repeat directory) | |
bc44bef7 | 273 | :risky t |
44198b6e CY |
274 | :group 'package |
275 | :version "24.1") | |
276 | ||
96ae4c8f CY |
277 | ;; The value is precomputed in finder-inf.el, but don't load that |
278 | ;; until it's needed (i.e. when `package-intialize' is called). | |
279 | (defvar package--builtins nil | |
280 | "Alist of built-in packages. | |
281 | Each element has the form (PKG . DESC), where PKG is a package | |
282 | name (a symbol) and DESC is a vector that describes the package. | |
283 | ||
284 | The vector DESC has the form [VERSION REQS DOCSTRING]. | |
285 | VERSION is a version list. | |
286 | REQS is a list of packages (symbols) required by the package. | |
287 | DOCSTRING is a brief description of the package.") | |
bc44bef7 | 288 | (put 'package--builtins 'risky-local-variable t) |
44198b6e | 289 | |
96ae4c8f | 290 | (defvar package-alist nil |
44198b6e | 291 | "Alist of all packages available for activation. |
96ae4c8f CY |
292 | Each element has the form (PKG . DESC), where PKG is a package |
293 | name (a symbol) and DESC is a vector that describes the package. | |
44198b6e | 294 | |
96ae4c8f CY |
295 | The vector DESC has the form [VERSION REQS DOCSTRING]. |
296 | VERSION is a version list. | |
297 | REQS is a list of packages (symbols) required by the package. | |
298 | DOCSTRING is a brief description of the package. | |
299 | ||
300 | This variable is set automatically by `package-load-descriptor', | |
301 | called via `package-initialize'. To change which packages are | |
302 | loaded and/or activated, customize `package-load-list'.") | |
bc44bef7 | 303 | (put 'package-archive-contents 'risky-local-variable t) |
44198b6e | 304 | |
96ae4c8f | 305 | (defvar package-activated-list nil |
44198b6e | 306 | "List of the names of currently activated packages.") |
bc44bef7 | 307 | (put 'package-activated-list 'risky-local-variable t) |
44198b6e CY |
308 | |
309 | (defvar package-obsolete-alist nil | |
310 | "Representation of obsolete packages. | |
311 | Like `package-alist', but maps package name to a second alist. | |
312 | The inner alist is keyed by version.") | |
bc44bef7 | 313 | (put 'package-obsolete-alist 'risky-local-variable t) |
44198b6e CY |
314 | |
315 | (defconst package-subdirectory-regexp | |
316 | "^\\([^.].*\\)-\\([0-9]+\\(?:[.][0-9]+\\)*\\)$" | |
317 | "Regular expression matching the name of a package subdirectory. | |
318 | The first subexpression is the package name. | |
319 | The second subexpression is the version string.") | |
320 | ||
321 | (defun package-version-join (l) | |
322 | "Turn a list of version numbers into a version string." | |
323 | (mapconcat 'int-to-string l ".")) | |
324 | ||
44198b6e CY |
325 | (defun package-strip-version (dirname) |
326 | "Strip the version from a combined package name and version. | |
327 | E.g., if given \"quux-23.0\", will return \"quux\"" | |
328 | (if (string-match package-subdirectory-regexp dirname) | |
329 | (match-string 1 dirname))) | |
330 | ||
331 | (defun package-load-descriptor (dir package) | |
4b99edf2 CY |
332 | "Load the description file in directory DIR for package PACKAGE. |
333 | Here, PACKAGE is a string of the form NAME-VER, where NAME is the | |
334 | package name and VER is its version." | |
bc44bef7 PH |
335 | (let* ((pkg-dir (expand-file-name package dir)) |
336 | (pkg-file (expand-file-name | |
337 | (concat (package-strip-version package) "-pkg") | |
338 | pkg-dir))) | |
339 | (when (and (file-directory-p pkg-dir) | |
340 | (file-exists-p (concat pkg-file ".el"))) | |
341 | (load pkg-file nil t)))) | |
44198b6e CY |
342 | |
343 | (defun package-load-all-descriptors () | |
344 | "Load descriptors for installed Emacs Lisp packages. | |
345 | This looks for package subdirectories in `package-user-dir' and | |
346 | `package-directory-list'. The variable `package-load-list' | |
347 | controls which package subdirectories may be loaded. | |
348 | ||
349 | In each valid package subdirectory, this function loads the | |
350 | description file containing a call to `define-package', which | |
351 | updates `package-alist' and `package-obsolete-alist'." | |
352 | (let ((all (memq 'all package-load-list)) | |
353 | name version force) | |
354 | (dolist (dir (cons package-user-dir package-directory-list)) | |
355 | (when (file-directory-p dir) | |
356 | (dolist (subdir (directory-files dir)) | |
357 | (when (and (file-directory-p (expand-file-name subdir dir)) | |
358 | (string-match package-subdirectory-regexp subdir)) | |
359 | (setq name (intern (match-string 1 subdir)) | |
360 | version (match-string 2 subdir) | |
361 | force (assq name package-load-list)) | |
362 | (when (cond | |
363 | ((null force) | |
364 | all) ; not in package-load-list | |
365 | ((null (setq force (cadr force))) | |
366 | nil) ; disabled | |
367 | ((eq force t) | |
368 | t) | |
369 | ((stringp force) ; held | |
148cef8e CY |
370 | (version-list-= (version-to-list version) |
371 | (version-to-list force))) | |
44198b6e CY |
372 | (t |
373 | (error "Invalid element in `package-load-list'"))) | |
374 | (package-load-descriptor dir subdir)))))))) | |
375 | ||
376 | (defsubst package-desc-vers (desc) | |
377 | "Extract version from a package description vector." | |
378 | (aref desc 0)) | |
379 | ||
380 | (defsubst package-desc-reqs (desc) | |
381 | "Extract requirements from a package description vector." | |
382 | (aref desc 1)) | |
383 | ||
384 | (defsubst package-desc-doc (desc) | |
385 | "Extract doc string from a package description vector." | |
386 | (aref desc 2)) | |
387 | ||
388 | (defsubst package-desc-kind (desc) | |
389 | "Extract the kind of download from an archive package description vector." | |
390 | (aref desc 3)) | |
391 | ||
cced7584 CY |
392 | (defun package--dir (name version-string) |
393 | (let* ((subdir (concat name "-" version-string)) | |
44198b6e | 394 | (dir-list (cons package-user-dir package-directory-list)) |
cced7584 | 395 | pkg-dir) |
44198b6e | 396 | (while dir-list |
cced7584 CY |
397 | (let ((subdir-full (expand-file-name subdir (car dir-list)))) |
398 | (if (file-directory-p subdir-full) | |
399 | (setq pkg-dir subdir-full | |
400 | dir-list nil) | |
44198b6e | 401 | (setq dir-list (cdr dir-list))))) |
cced7584 CY |
402 | pkg-dir)) |
403 | ||
404 | (defun package-activate-1 (package pkg-vec) | |
405 | (let* ((name (symbol-name package)) | |
406 | (version-str (package-version-join (package-desc-vers pkg-vec))) | |
407 | (pkg-dir (package--dir name version-str))) | |
44198b6e CY |
408 | (unless pkg-dir |
409 | (error "Internal error: could not find directory for %s-%s" | |
cced7584 CY |
410 | name version-str)) |
411 | ;; Add info node. | |
ebf662f4 CY |
412 | (when (file-exists-p (expand-file-name "dir" pkg-dir)) |
413 | ;; FIXME: not the friendliest, but simple. | |
414 | (require 'info) | |
415 | (info-initialize) | |
416 | (push pkg-dir Info-directory-list)) | |
cced7584 | 417 | ;; Add to load path, add autoloads, and activate the package. |
ebf662f4 | 418 | (push pkg-dir load-path) |
cced7584 | 419 | (load (expand-file-name (concat name "-autoloads") pkg-dir) nil t) |
ebf662f4 | 420 | (push package package-activated-list) |
44198b6e CY |
421 | ;; Don't return nil. |
422 | t)) | |
423 | ||
4b99edf2 CY |
424 | (defun package-built-in-p (package &optional version) |
425 | "Return true if PACKAGE, of VERSION or newer, is built-in to Emacs." | |
426 | (require 'finder-inf nil t) ; For `package--builtins'. | |
44198b6e | 427 | (let ((elt (assq package package--builtins))) |
4b99edf2 | 428 | (and elt (version-list-<= version (package-desc-vers (cdr elt)))))) |
44198b6e | 429 | |
4b99edf2 CY |
430 | ;; This function goes ahead and activates a newer version of a package |
431 | ;; if an older one was already activated. This is not ideal; we'd at | |
432 | ;; least need to check to see if the package has actually been loaded, | |
433 | ;; and not merely activated. | |
44198b6e | 434 | (defun package-activate (package version) |
4b99edf2 CY |
435 | "Activate package PACKAGE, of version VERSION or newer. |
436 | If PACKAGE has any dependencies, recursively activate them. | |
44198b6e | 437 | Return nil if the package could not be activated." |
4b99edf2 CY |
438 | (let ((pkg-vec (cdr (assq package package-alist))) |
439 | available-version found) | |
440 | ;; Check if PACKAGE is available in `package-alist'. | |
441 | (when pkg-vec | |
442 | (setq available-version (package-desc-vers pkg-vec) | |
443 | found (version-list-<= version available-version))) | |
444 | (cond | |
445 | ;; If no such package is found, maybe it's built-in. | |
446 | ((null found) | |
447 | (package-built-in-p package version)) | |
448 | ;; If the package is already activated, just return t. | |
449 | ((memq package package-activated-list) | |
450 | t) | |
451 | ;; Otherwise, proceed with activation. | |
452 | (t | |
453 | (let ((fail (catch 'dep-failure | |
454 | ;; Activate its dependencies recursively. | |
455 | (dolist (req (package-desc-reqs pkg-vec)) | |
456 | (unless (package-activate (car req) (cadr req)) | |
457 | (throw 'dep-failure req)))))) | |
458 | (if fail | |
459 | (warn "Unable to activate package `%s'. | |
460 | Required package `%s', version %s, is unavailable" | |
461 | package (car fail) (package-version-join (cadr fail))) | |
462 | ;; If all goes well, activate the package itself. | |
463 | (package-activate-1 package pkg-vec))))))) | |
44198b6e CY |
464 | |
465 | (defun package-mark-obsolete (package pkg-vec) | |
466 | "Put package on the obsolete list, if not already there." | |
467 | (let ((elt (assq package package-obsolete-alist))) | |
468 | (if elt | |
469 | ;; If this obsolete version does not exist in the list, update | |
470 | ;; it the list. | |
471 | (unless (assoc (package-desc-vers pkg-vec) (cdr elt)) | |
472 | (setcdr elt (cons (cons (package-desc-vers pkg-vec) pkg-vec) | |
473 | (cdr elt)))) | |
474 | ;; Make a new association. | |
ebf662f4 CY |
475 | (push (cons package (list (cons (package-desc-vers pkg-vec) |
476 | pkg-vec))) | |
477 | package-obsolete-alist)))) | |
44198b6e | 478 | |
4b99edf2 | 479 | (defun define-package (name-string version-string |
187d3296 CY |
480 | &optional docstring requirements |
481 | &rest extra-properties) | |
44198b6e | 482 | "Define a new package. |
4b99edf2 CY |
483 | NAME-STRING is the name of the package, as a string. |
484 | VERSION-STRING is the version of the package, as a list of | |
485 | integers of the form produced by `version-to-list'. | |
486 | DOCSTRING is a short description of the package, a string. | |
487 | REQUIREMENTS is a list of dependencies on other packages. | |
187d3296 CY |
488 | Each requirement is of the form (OTHER-PACKAGE \"VERSION\"). |
489 | ||
490 | EXTRA-PROPERTIES is currently unused." | |
4b99edf2 CY |
491 | (let* ((name (intern name-string)) |
492 | (version (version-to-list version-string)) | |
44198b6e CY |
493 | (new-pkg-desc |
494 | (cons name | |
4b99edf2 | 495 | (vector version |
44198b6e CY |
496 | (mapcar |
497 | (lambda (elt) | |
498 | (list (car elt) | |
148cef8e | 499 | (version-to-list (car (cdr elt))))) |
44198b6e | 500 | requirements) |
4b99edf2 CY |
501 | docstring))) |
502 | (old-pkg (assq name package-alist))) | |
503 | (cond | |
504 | ;; If there's no old package, just add this to `package-alist'. | |
505 | ((null old-pkg) | |
506 | (push new-pkg-desc package-alist)) | |
507 | ((version-list-< (package-desc-vers (cdr old-pkg)) version) | |
508 | ;; Remove the old package and declare it obsolete. | |
509 | (package-mark-obsolete name (cdr old-pkg)) | |
510 | (setq package-alist (cons new-pkg-desc | |
511 | (delq old-pkg package-alist)))) | |
512 | ;; You can have two packages with the same version, e.g. one in | |
513 | ;; the system package directory and one in your private | |
514 | ;; directory. We just let the first one win. | |
515 | ((not (version-list-= (package-desc-vers (cdr old-pkg)) version)) | |
516 | ;; The package is born obsolete. | |
517 | (package-mark-obsolete name (cdr new-pkg-desc)))))) | |
44198b6e CY |
518 | |
519 | ;; From Emacs 22. | |
520 | (defun package-autoload-ensure-default-file (file) | |
521 | "Make sure that the autoload file FILE exists and if not create it." | |
522 | (unless (file-exists-p file) | |
523 | (write-region | |
524 | (concat ";;; " (file-name-nondirectory file) | |
525 | " --- automatically extracted autoloads\n" | |
526 | ";;\n" | |
527 | ";;; Code:\n\n" | |
528 | "\f\n;; Local Variables:\n" | |
529 | ";; version-control: never\n" | |
530 | ";; no-byte-compile: t\n" | |
531 | ";; no-update-autoloads: t\n" | |
532 | ";; End:\n" | |
533 | ";;; " (file-name-nondirectory file) | |
534 | " ends here\n") | |
535 | nil file)) | |
536 | file) | |
537 | ||
538 | (defun package-generate-autoloads (name pkg-dir) | |
539 | (let* ((auto-name (concat name "-autoloads.el")) | |
540 | (ignore-name (concat name "-pkg.el")) | |
541 | (generated-autoload-file (expand-file-name auto-name pkg-dir)) | |
542 | (version-control 'never)) | |
543 | (require 'autoload) | |
544 | (unless (fboundp 'autoload-ensure-default-file) | |
545 | (package-autoload-ensure-default-file generated-autoload-file)) | |
546 | (update-directory-autoloads pkg-dir))) | |
547 | ||
548 | (defun package-untar-buffer () | |
549 | "Untar the current buffer. | |
550 | This uses `tar-untar-buffer' if it is available. | |
551 | Otherwise it uses an external `tar' program. | |
552 | `default-directory' should be set by the caller." | |
553 | (require 'tar-mode) | |
554 | (if (fboundp 'tar-untar-buffer) | |
555 | (progn | |
556 | ;; tar-mode messes with narrowing, so we just let it have the | |
557 | ;; whole buffer to play with. | |
558 | (delete-region (point-min) (point)) | |
559 | (tar-mode) | |
560 | (tar-untar-buffer)) | |
561 | ;; FIXME: check the result. | |
562 | (call-process-region (point) (point-max) "tar" nil '(nil nil) nil | |
563 | "xf" "-"))) | |
564 | ||
565 | (defun package-unpack (name version) | |
566 | (let ((pkg-dir (expand-file-name (concat (symbol-name name) "-" version) | |
567 | package-user-dir))) | |
568 | ;; Be careful!! | |
569 | (make-directory package-user-dir t) | |
570 | (if (file-directory-p pkg-dir) | |
571 | (mapc (lambda (file) nil) ; 'delete-file -- FIXME: when we're | |
572 | ; more confident | |
573 | (directory-files pkg-dir t "^[^.]"))) | |
574 | (let* ((default-directory (file-name-as-directory package-user-dir))) | |
575 | (package-untar-buffer) | |
576 | (package-generate-autoloads (symbol-name name) pkg-dir) | |
577 | (let ((load-path (cons pkg-dir load-path))) | |
578 | (byte-recompile-directory pkg-dir 0 t))))) | |
579 | ||
bc44bef7 PH |
580 | (defun package--write-file-no-coding (file-name excl) |
581 | (let ((buffer-file-coding-system 'no-conversion)) | |
582 | (write-region (point-min) (point-max) file-name nil nil nil excl))) | |
583 | ||
44198b6e CY |
584 | (defun package-unpack-single (file-name version desc requires) |
585 | "Install the contents of the current buffer as a package." | |
586 | ;; Special case "package". | |
587 | (if (string= file-name "package") | |
bc44bef7 PH |
588 | (package--write-file-no-coding |
589 | (expand-file-name (concat file-name ".el") package-user-dir) | |
590 | nil) | |
44198b6e CY |
591 | (let* ((pkg-dir (expand-file-name (concat file-name "-" version) |
592 | package-user-dir)) | |
593 | (el-file (expand-file-name (concat file-name ".el") pkg-dir)) | |
594 | (pkg-file (expand-file-name (concat file-name "-pkg.el") pkg-dir))) | |
595 | (make-directory pkg-dir t) | |
bc44bef7 | 596 | (package--write-file-no-coding el-file 'excl) |
44198b6e CY |
597 | (let ((print-level nil) |
598 | (print-length nil)) | |
599 | (write-region | |
600 | (concat | |
601 | (prin1-to-string | |
602 | (list 'define-package | |
603 | file-name | |
604 | version | |
605 | desc | |
606 | (list 'quote | |
607 | ;; Turn version lists into string form. | |
608 | (mapcar | |
609 | (lambda (elt) | |
610 | (list (car elt) | |
611 | (package-version-join (car (cdr elt))))) | |
612 | requires)))) | |
613 | "\n") | |
614 | nil | |
615 | pkg-file | |
616 | nil nil nil 'excl)) | |
617 | (package-generate-autoloads file-name pkg-dir) | |
618 | (let ((load-path (cons pkg-dir load-path))) | |
619 | (byte-recompile-directory pkg-dir 0 t))))) | |
620 | ||
621 | (defun package-handle-response () | |
622 | "Handle the response from the server. | |
623 | Parse the HTTP response and throw if an error occurred. | |
624 | The url package seems to require extra processing for this. | |
625 | This should be called in a `save-excursion', in the download buffer. | |
626 | It will move point to somewhere in the headers." | |
627 | ;; We assume HTTP here. | |
628 | (require 'url-http) | |
629 | (let ((response (url-http-parse-response))) | |
630 | (when (or (< response 200) (>= response 300)) | |
631 | (display-buffer (current-buffer)) | |
632 | (error "Error during download request:%s" | |
633 | (buffer-substring-no-properties (point) (progn | |
634 | (end-of-line) | |
635 | (point))))))) | |
636 | ||
637 | (defun package-download-single (name version desc requires) | |
638 | "Download and install a single-file package." | |
639 | (let ((buffer (url-retrieve-synchronously | |
063e5294 | 640 | (concat (package-archive-url name) |
44198b6e CY |
641 | (symbol-name name) "-" version ".el")))) |
642 | (with-current-buffer buffer | |
643 | (package-handle-response) | |
644 | (re-search-forward "^$" nil 'move) | |
645 | (forward-char) | |
646 | (delete-region (point-min) (point)) | |
647 | (package-unpack-single (symbol-name name) version desc requires) | |
648 | (kill-buffer buffer)))) | |
649 | ||
650 | (defun package-download-tar (name version) | |
651 | "Download and install a tar package." | |
652 | (let ((tar-buffer (url-retrieve-synchronously | |
063e5294 | 653 | (concat (package-archive-url name) |
44198b6e CY |
654 | (symbol-name name) "-" version ".tar")))) |
655 | (with-current-buffer tar-buffer | |
656 | (package-handle-response) | |
657 | (re-search-forward "^$" nil 'move) | |
658 | (forward-char) | |
659 | (package-unpack name version) | |
660 | (kill-buffer tar-buffer)))) | |
661 | ||
bc44bef7 | 662 | (defun package-installed-p (package &optional min-version) |
4b99edf2 CY |
663 | "Return true if PACKAGE, of VERSION or newer, is installed. |
664 | Built-in packages also qualify." | |
44198b6e | 665 | (let ((pkg-desc (assq package package-alist))) |
4b99edf2 CY |
666 | (if pkg-desc |
667 | (version-list-<= min-version | |
668 | (package-desc-vers (cdr pkg-desc))) | |
669 | ;; Also check built-in packages. | |
670 | (package-built-in-p package min-version)))) | |
44198b6e | 671 | |
96ae4c8f CY |
672 | (defun package-compute-transaction (package-list requirements) |
673 | "Return a list of packages to be installed, including PACKAGE-LIST. | |
674 | PACKAGE-LIST should be a list of package names (symbols). | |
675 | ||
676 | REQUIREMENTS should be a list of additional requirements; each | |
677 | element in this list should have the form (PACKAGE VERSION), | |
678 | where PACKAGE is a package name and VERSION is the required | |
679 | version of that package (as a list). | |
680 | ||
681 | This function recursively computes the requirements of the | |
682 | packages in REQUIREMENTS, and returns a list of all the packages | |
683 | that must be installed. Packages that are already installed are | |
684 | not included in this list." | |
44198b6e CY |
685 | (dolist (elt requirements) |
686 | (let* ((next-pkg (car elt)) | |
687 | (next-version (cadr elt))) | |
688 | (unless (package-installed-p next-pkg next-version) | |
689 | ;; A package is required, but not installed. It might also be | |
690 | ;; blocked via `package-load-list'. | |
691 | (let ((pkg-desc (assq next-pkg package-archive-contents)) | |
692 | hold) | |
693 | (when (setq hold (assq next-pkg package-load-list)) | |
694 | (setq hold (cadr hold)) | |
695 | (cond ((eq hold nil) | |
696 | (error "Required package '%s' is disabled" | |
697 | (symbol-name next-pkg))) | |
698 | ((null (stringp hold)) | |
699 | (error "Invalid element in `package-load-list'")) | |
148cef8e | 700 | ((version-list-< (version-to-list hold) next-version) |
44198b6e CY |
701 | (error "Package '%s' held at version %s, \ |
702 | but version %s required" | |
703 | (symbol-name next-pkg) hold | |
704 | (package-version-join next-version))))) | |
705 | (unless pkg-desc | |
4b99edf2 CY |
706 | (error "Package '%s', version %s, unavailable for installation" |
707 | (symbol-name next-pkg) | |
708 | (package-version-join next-version))) | |
148cef8e CY |
709 | (unless (version-list-<= next-version |
710 | (package-desc-vers (cdr pkg-desc))) | |
44198b6e CY |
711 | (error |
712 | "Need package '%s' with version %s, but only %s is available" | |
713 | (symbol-name next-pkg) (package-version-join next-version) | |
714 | (package-version-join (package-desc-vers (cdr pkg-desc))))) | |
715 | ;; Only add to the transaction if we don't already have it. | |
96ae4c8f | 716 | (unless (memq next-pkg package-list) |
ebf662f4 | 717 | (push next-pkg package-list)) |
96ae4c8f CY |
718 | (setq package-list |
719 | (package-compute-transaction package-list | |
44198b6e CY |
720 | (package-desc-reqs |
721 | (cdr pkg-desc)))))))) | |
96ae4c8f | 722 | package-list) |
44198b6e CY |
723 | |
724 | (defun package-read-from-string (str) | |
725 | "Read a Lisp expression from STR. | |
726 | Signal an error if the entire string was not used." | |
727 | (let* ((read-data (read-from-string str)) | |
187d3296 CY |
728 | (more-left |
729 | (condition-case nil | |
730 | ;; The call to `ignore' suppresses a compiler warning. | |
731 | (progn (ignore (read-from-string | |
732 | (substring str (cdr read-data)))) | |
733 | t) | |
734 | (end-of-file nil)))) | |
44198b6e CY |
735 | (if more-left |
736 | (error "Can't read whole string") | |
737 | (car read-data)))) | |
738 | ||
739 | (defun package--read-archive-file (file) | |
740 | "Re-read archive file FILE, if it exists. | |
741 | Will return the data from the file, or nil if the file does not exist. | |
742 | Will throw an error if the archive version is too new." | |
743 | (let ((filename (expand-file-name file package-user-dir))) | |
187d3296 CY |
744 | (when (file-exists-p filename) |
745 | (with-temp-buffer | |
746 | (insert-file-contents-literally filename) | |
747 | (let ((contents (read (current-buffer)))) | |
748 | (if (> (car contents) package-archive-version) | |
749 | (error "Package archive version %d is higher than %d" | |
750 | (car contents) package-archive-version)) | |
751 | (cdr contents)))))) | |
44198b6e | 752 | |
bc44bef7 | 753 | (defun package-read-all-archive-contents () |
96ae4c8f CY |
754 | "Re-read `archive-contents', if it exists. |
755 | If successful, set `package-archive-contents'." | |
bc44bef7 | 756 | (dolist (archive package-archives) |
96ae4c8f | 757 | (package-read-archive-contents (car archive)))) |
44198b6e | 758 | |
bc44bef7 | 759 | (defun package-read-archive-contents (archive) |
187d3296 CY |
760 | "Re-read archive contents for ARCHIVE. |
761 | If successful, set the variable `package-archive-contents'. | |
bc44bef7 | 762 | If the archive version is too new, signal an error." |
187d3296 CY |
763 | ;; Version 1 of 'archive-contents' is identical to our internal |
764 | ;; representation. | |
765 | (let* ((dir (concat "archives/" archive)) | |
766 | (contents-file (concat dir "/archive-contents")) | |
767 | contents) | |
768 | (when (setq contents (package--read-archive-file contents-file)) | |
769 | (dolist (package contents) | |
770 | (package--add-to-archive-contents package archive))))) | |
bc44bef7 PH |
771 | |
772 | (defun package--add-to-archive-contents (package archive) | |
773 | "Add the PACKAGE from the given ARCHIVE if necessary. | |
774 | Also, add the originating archive to the end of the package vector." | |
775 | (let* ((name (car package)) | |
776 | (version (aref (cdr package) 0)) | |
777 | (entry (cons (car package) | |
778 | (vconcat (cdr package) (vector archive)))) | |
779 | (existing-package (cdr (assq name package-archive-contents)))) | |
780 | (when (or (not existing-package) | |
148cef8e | 781 | (version-list-< (aref existing-package 0) version)) |
bc44bef7 PH |
782 | (add-to-list 'package-archive-contents entry)))) |
783 | ||
96ae4c8f CY |
784 | (defun package-download-transaction (package-list) |
785 | "Download and install all the packages in PACKAGE-LIST. | |
786 | PACKAGE-LIST should be a list of package names (symbols). | |
787 | This function assumes that all package requirements in | |
788 | PACKAGE-LIST are satisfied, i.e. that PACKAGE-LIST is computed | |
789 | using `package-compute-transaction'." | |
790 | (dolist (elt package-list) | |
44198b6e CY |
791 | (let* ((desc (cdr (assq elt package-archive-contents))) |
792 | ;; As an exception, if package is "held" in | |
793 | ;; `package-load-list', download the held version. | |
794 | (hold (cadr (assq elt package-load-list))) | |
795 | (v-string (or (and (stringp hold) hold) | |
796 | (package-version-join (package-desc-vers desc)))) | |
797 | (kind (package-desc-kind desc))) | |
798 | (cond | |
799 | ((eq kind 'tar) | |
800 | (package-download-tar elt v-string)) | |
801 | ((eq kind 'single) | |
802 | (package-download-single elt v-string | |
803 | (package-desc-doc desc) | |
804 | (package-desc-reqs desc))) | |
805 | (t | |
806 | (error "Unknown package kind: %s" (symbol-name kind))))))) | |
807 | ||
808 | ;;;###autoload | |
809 | (defun package-install (name) | |
810 | "Install the package named NAME. | |
811 | Interactively, prompt for the package name. | |
063e5294 | 812 | The package is found on one of the archives in `package-archives'." |
44198b6e | 813 | (interactive |
bc44bef7 PH |
814 | (list (intern (completing-read "Install package: " |
815 | (mapcar (lambda (elt) | |
816 | (cons (symbol-name (car elt)) | |
817 | nil)) | |
818 | package-archive-contents) | |
819 | nil t)))) | |
44198b6e CY |
820 | (let ((pkg-desc (assq name package-archive-contents))) |
821 | (unless pkg-desc | |
bc44bef7 | 822 | (error "Package '%s' is not available for installation" |
44198b6e | 823 | (symbol-name name))) |
bc44bef7 PH |
824 | (package-download-transaction |
825 | (package-compute-transaction (list name) | |
826 | (package-desc-reqs (cdr pkg-desc))))) | |
44198b6e CY |
827 | ;; Try to activate it. |
828 | (package-initialize)) | |
829 | ||
830 | (defun package-strip-rcs-id (v-str) | |
831 | "Strip RCS version ID from the version string. | |
832 | If the result looks like a dotted numeric version, return it. | |
833 | Otherwise return nil." | |
834 | (if v-str | |
835 | (if (string-match "^[ \t]*[$]Revision:[ \t]\([0-9.]+\)[ \t]*[$]$" v-str) | |
836 | (match-string 1 v-str) | |
837 | (if (string-match "^[0-9.]*$" v-str) | |
838 | v-str)))) | |
839 | ||
840 | (defun package-buffer-info () | |
187d3296 CY |
841 | "Return a vector describing the package in the current buffer. |
842 | The vector has the form | |
843 | ||
844 | [FILENAME REQUIRES DESCRIPTION VERSION COMMENTARY] | |
845 | ||
846 | FILENAME is the file name, a string, sans the \".el\" extension. | |
44198b6e | 847 | REQUIRES is a requires list, or nil. |
187d3296 | 848 | DESCRIPTION is the package description, a string. |
44198b6e CY |
849 | VERSION is the version, a string. |
850 | COMMENTARY is the commentary section, a string, or nil if none. | |
187d3296 CY |
851 | |
852 | If the buffer does not contain a conforming package, signal an | |
853 | error. If there is a package, narrow the buffer to the file's | |
854 | boundaries." | |
44198b6e | 855 | (goto-char (point-min)) |
187d3296 CY |
856 | (unless (re-search-forward "^;;; \\([^ ]*\\)\\.el --- \\(.*\\)$" nil t) |
857 | (error "Packages lacks a file header")) | |
858 | (let ((file-name (match-string-no-properties 1)) | |
859 | (desc (match-string-no-properties 2)) | |
860 | (start (line-beginning-position))) | |
861 | (unless (search-forward (concat ";;; " file-name ".el ends here")) | |
862 | (error "Package lacks a terminating comment")) | |
863 | ;; Try to include a trailing newline. | |
864 | (forward-line) | |
865 | (narrow-to-region start (point)) | |
866 | (require 'lisp-mnt) | |
867 | ;; Use some headers we've invented to drive the process. | |
868 | (let* ((requires-str (lm-header "package-requires")) | |
869 | (requires (if requires-str | |
870 | (package-read-from-string requires-str))) | |
871 | ;; Prefer Package-Version; if defined, the package author | |
872 | ;; probably wants us to use it. Otherwise try Version. | |
873 | (pkg-version | |
874 | (or (package-strip-rcs-id (lm-header "package-version")) | |
875 | (package-strip-rcs-id (lm-header "version")))) | |
876 | (commentary (lm-commentary))) | |
877 | (unless pkg-version | |
878 | (error | |
879 | "Package lacks a \"Version\" or \"Package-Version\" header")) | |
880 | ;; Turn string version numbers into list form. | |
881 | (setq requires | |
882 | (mapcar | |
883 | (lambda (elt) | |
884 | (list (car elt) | |
885 | (version-to-list (car (cdr elt))))) | |
886 | requires)) | |
887 | (vector file-name requires desc pkg-version commentary)))) | |
44198b6e CY |
888 | |
889 | (defun package-tar-file-info (file) | |
890 | "Find package information for a tar file. | |
891 | FILE is the name of the tar file to examine. | |
892 | The return result is a vector like `package-buffer-info'." | |
893 | (unless (string-match "^\\(.+\\)-\\([0-9.]+\\)\\.tar$" file) | |
187d3296 | 894 | (error "Invalid package name `%s'" file)) |
44198b6e CY |
895 | (let* ((pkg-name (file-name-nondirectory (match-string-no-properties 1 file))) |
896 | (pkg-version (match-string-no-properties 2 file)) | |
897 | ;; Extract the package descriptor. | |
898 | (pkg-def-contents (shell-command-to-string | |
899 | ;; Requires GNU tar. | |
900 | (concat "tar -xOf " file " " | |
901 | pkg-name "-" pkg-version "/" | |
902 | pkg-name "-pkg.el"))) | |
903 | (pkg-def-parsed (package-read-from-string pkg-def-contents))) | |
904 | (unless (eq (car pkg-def-parsed) 'define-package) | |
187d3296 CY |
905 | (error "No `define-package' sexp is present in `%s-pkg.el'" pkg-name)) |
906 | (let ((name-str (nth 1 pkg-def-parsed)) | |
44198b6e | 907 | (version-string (nth 2 pkg-def-parsed)) |
187d3296 CY |
908 | (docstring (nth 3 pkg-def-parsed)) |
909 | (requires (nth 4 pkg-def-parsed)) | |
44198b6e CY |
910 | (readme (shell-command-to-string |
911 | ;; Requires GNU tar. | |
912 | (concat "tar -xOf " file " " | |
913 | pkg-name "-" pkg-version "/README")))) | |
914 | (unless (equal pkg-version version-string) | |
187d3296 | 915 | (error "Package has inconsistent versions")) |
44198b6e | 916 | (unless (equal pkg-name name-str) |
187d3296 | 917 | (error "Package has inconsistent names")) |
44198b6e CY |
918 | ;; Kind of a hack. |
919 | (if (string-match ": Not found in archive" readme) | |
920 | (setq readme nil)) | |
921 | ;; Turn string version numbers into list form. | |
922 | (if (eq (car requires) 'quote) | |
923 | (setq requires (car (cdr requires)))) | |
924 | (setq requires | |
187d3296 CY |
925 | (mapcar (lambda (elt) |
926 | (list (car elt) | |
927 | (version-to-list (cadr elt)))) | |
928 | requires)) | |
44198b6e CY |
929 | (vector pkg-name requires docstring version-string readme)))) |
930 | ||
187d3296 CY |
931 | ;;;###autoload |
932 | (defun package-install-from-buffer (pkg-info type) | |
933 | "Install a package from the current buffer. | |
934 | When called interactively, the current buffer is assumed to be a | |
935 | single .el file that follows the packaging guidelines; see info | |
936 | node `(elisp)Packaging'. | |
937 | ||
938 | When called from Lisp, PKG-INFO is a vector describing the | |
939 | information, of the type returned by `package-buffer-info'; and | |
940 | TYPE is the package type (either `single' or `tar')." | |
941 | (interactive (list (package-buffer-info) 'single)) | |
44198b6e CY |
942 | (save-excursion |
943 | (save-restriction | |
944 | (let* ((file-name (aref pkg-info 0)) | |
187d3296 | 945 | (requires (aref pkg-info 1)) |
44198b6e CY |
946 | (desc (if (string= (aref pkg-info 2) "") |
947 | "No description available." | |
948 | (aref pkg-info 2))) | |
949 | (pkg-version (aref pkg-info 3))) | |
950 | ;; Download and install the dependencies. | |
951 | (let ((transaction (package-compute-transaction nil requires))) | |
952 | (package-download-transaction transaction)) | |
953 | ;; Install the package itself. | |
954 | (cond | |
955 | ((eq type 'single) | |
956 | (package-unpack-single file-name pkg-version desc requires)) | |
957 | ((eq type 'tar) | |
958 | (package-unpack (intern file-name) pkg-version)) | |
959 | (t | |
960 | (error "Unknown type: %s" (symbol-name type)))) | |
961 | ;; Try to activate it. | |
962 | (package-initialize))))) | |
963 | ||
44198b6e CY |
964 | ;;;###autoload |
965 | (defun package-install-file (file) | |
966 | "Install a package from a file. | |
967 | The file can either be a tar file or an Emacs Lisp file." | |
968 | (interactive "fPackage file name: ") | |
969 | (with-temp-buffer | |
970 | (insert-file-contents-literally file) | |
971 | (cond | |
187d3296 CY |
972 | ((string-match "\\.el$" file) |
973 | (package-install-from-buffer (package-buffer-info) 'single)) | |
44198b6e | 974 | ((string-match "\\.tar$" file) |
187d3296 | 975 | (package-install-from-buffer (package-tar-file-info file) 'tar)) |
44198b6e CY |
976 | (t (error "Unrecognized extension `%s'" (file-name-extension file)))))) |
977 | ||
978 | (defun package-delete (name version) | |
979 | (require 'dired) ; for dired-delete-file | |
980 | (dired-delete-file (expand-file-name (concat name "-" version) | |
981 | package-user-dir) | |
982 | ;; FIXME: query user? | |
983 | 'always)) | |
984 | ||
063e5294 | 985 | (defun package-archive-url (name) |
bc44bef7 PH |
986 | "Return the archive containing the package NAME." |
987 | (let ((desc (cdr (assq (intern-soft name) package-archive-contents)))) | |
988 | (cdr (assoc (aref desc (- (length desc) 1)) package-archives)))) | |
989 | ||
990 | (defun package--download-one-archive (archive file) | |
991 | "Download an archive file FILE from ARCHIVE, and cache it locally." | |
992 | (let* ((archive-name (car archive)) | |
993 | (archive-url (cdr archive)) | |
994 | (dir (expand-file-name "archives" package-user-dir)) | |
995 | (dir (expand-file-name archive-name dir)) | |
996 | (buffer (url-retrieve-synchronously (concat archive-url file)))) | |
44198b6e CY |
997 | (with-current-buffer buffer |
998 | (package-handle-response) | |
999 | (re-search-forward "^$" nil 'move) | |
1000 | (forward-char) | |
1001 | (delete-region (point-min) (point)) | |
ebf662f4 CY |
1002 | ;; Read the retrieved buffer to make sure it is valid (e.g. it |
1003 | ;; may fetch a URL redirect page). | |
1004 | (when (listp (read buffer)) | |
1005 | (make-directory dir t) | |
1006 | (setq buffer-file-name (expand-file-name file dir)) | |
1007 | (let ((version-control 'never)) | |
1008 | (save-buffer)))) | |
bc44bef7 | 1009 | (kill-buffer buffer))) |
44198b6e CY |
1010 | |
1011 | (defun package-refresh-contents () | |
1012 | "Download the ELPA archive description if needed. | |
ebf662f4 CY |
1013 | This informs Emacs about the latest versions of all packages, and |
1014 | makes them available for download." | |
44198b6e CY |
1015 | (interactive) |
1016 | (unless (file-exists-p package-user-dir) | |
1017 | (make-directory package-user-dir t)) | |
bc44bef7 | 1018 | (dolist (archive package-archives) |
cb6c4991 CY |
1019 | (condition-case nil |
1020 | (package--download-one-archive archive "archive-contents") | |
187d3296 | 1021 | (error (message "Failed to download `%s' archive." |
cb6c4991 | 1022 | (car archive))))) |
bc44bef7 | 1023 | (package-read-all-archive-contents)) |
44198b6e | 1024 | |
4b99edf2 CY |
1025 | (defvar package--initialized nil) |
1026 | ||
44198b6e | 1027 | ;;;###autoload |
4b99edf2 | 1028 | (defun package-initialize (&optional no-activate) |
44198b6e | 1029 | "Load Emacs Lisp packages, and activate them. |
4b99edf2 CY |
1030 | The variable `package-load-list' controls which packages to load. |
1031 | If optional arg NO-ACTIVATE is non-nil, don't activate packages." | |
44198b6e | 1032 | (interactive) |
4b99edf2 | 1033 | (setq package-obsolete-alist nil) |
44198b6e | 1034 | (package-load-all-descriptors) |
bc44bef7 | 1035 | (package-read-all-archive-contents) |
4b99edf2 CY |
1036 | (unless no-activate |
1037 | (dolist (elt package-alist) | |
1038 | (package-activate (car elt) (package-desc-vers (cdr elt))))) | |
1039 | (setq package--initialized t)) | |
44198b6e CY |
1040 | |
1041 | \f | |
cced7584 | 1042 | ;;;; Package description buffer. |
44198b6e | 1043 | |
cced7584 CY |
1044 | ;;;###autoload |
1045 | (defun describe-package (package) | |
1046 | "Display the full documentation of PACKAGE (a symbol)." | |
1047 | (interactive | |
8a500a91 CY |
1048 | (let* ((guess (function-called-at-point)) |
1049 | packages val) | |
4b99edf2 CY |
1050 | (require 'finder-inf nil t) |
1051 | ;; Load the package list if necessary (but don't activate them). | |
1052 | (unless package--initialized | |
1053 | (package-initialize t)) | |
8a500a91 | 1054 | (setq packages (append (mapcar 'car package-alist) |
4b99edf2 CY |
1055 | (mapcar 'car package-archive-contents) |
1056 | (mapcar 'car package--builtins))) | |
cced7584 CY |
1057 | (unless (memq guess packages) |
1058 | (setq guess nil)) | |
1059 | (setq packages (mapcar 'symbol-name packages)) | |
1060 | (setq val | |
1061 | (completing-read (if guess | |
1062 | (format "Describe package (default %s): " | |
1063 | guess) | |
1064 | "Describe package: ") | |
1065 | packages nil t nil nil guess)) | |
cb6c4991 | 1066 | (list (if (equal val "") guess (intern val))))) |
4b99edf2 CY |
1067 | (if (or (null package) (not (symbolp package))) |
1068 | (message "No package specified") | |
cced7584 CY |
1069 | (help-setup-xref (list #'describe-package package) |
1070 | (called-interactively-p 'interactive)) | |
1071 | (with-help-window (help-buffer) | |
1072 | (with-current-buffer standard-output | |
1073 | (describe-package-1 package))))) | |
1074 | ||
1075 | (defun describe-package-1 (package) | |
96ae4c8f | 1076 | (require 'lisp-mnt) |
cb6c4991 CY |
1077 | (let ((package-name (symbol-name package)) |
1078 | (built-in (assq package package--builtins)) | |
1079 | desc pkg-dir reqs version installable) | |
cced7584 CY |
1080 | (prin1 package) |
1081 | (princ " is ") | |
4b99edf2 CY |
1082 | (cond |
1083 | ;; Loaded packages are in `package-alist'. | |
1084 | ((setq desc (cdr (assq package package-alist))) | |
1085 | (setq version (package-version-join (package-desc-vers desc))) | |
1086 | (if (setq pkg-dir (package--dir package-name version)) | |
1087 | (insert "an installed package.\n\n") | |
1088 | ;; This normally does not happen. | |
1089 | (insert "a deleted package.\n\n"))) | |
1090 | ;; Available packages are in `package-archive-contents'. | |
1091 | ((setq desc (cdr (assq package package-archive-contents))) | |
1092 | (setq version (package-version-join (package-desc-vers desc)) | |
8adb4c33 | 1093 | installable t) |
4b99edf2 CY |
1094 | (if built-in |
1095 | (insert "a built-in package.\n\n") | |
1096 | (insert "an uninstalled package.\n\n"))) | |
1097 | (built-in | |
1098 | (setq desc (cdr built-in) | |
1099 | version (package-version-join (package-desc-vers desc))) | |
1100 | (insert "a built-in package.\n\n")) | |
1101 | (t | |
1102 | (insert "an orphan package.\n\n"))) | |
cb6c4991 | 1103 | |
96ae4c8f | 1104 | (insert " " (propertize "Status" 'font-lock-face 'bold) ": ") |
cb6c4991 | 1105 | (cond (pkg-dir |
96ae4c8f CY |
1106 | (insert (propertize "Installed" |
1107 | 'font-lock-face 'font-lock-comment-face)) | |
cb6c4991 CY |
1108 | (insert " in `") |
1109 | ;; Todo: Add button for uninstalling. | |
1110 | (help-insert-xref-button (file-name-as-directory pkg-dir) | |
1111 | 'help-package-def pkg-dir) | |
4b99edf2 CY |
1112 | (if built-in |
1113 | (insert "',\n shadowing a " | |
1114 | (propertize "built-in package" | |
1115 | 'font-lock-face 'font-lock-builtin-face) | |
1116 | ".") | |
1117 | (insert "'."))) | |
cb6c4991 | 1118 | (installable |
4b99edf2 CY |
1119 | (if built-in |
1120 | (insert (propertize "Built-in." 'font-lock-face 'font-lock-builtin-face) | |
1121 | " Alternate version available -- ") | |
1122 | (insert "Available -- ")) | |
1123 | (let ((button-text (if (display-graphic-p) "Install" "[Install]")) | |
cb6c4991 CY |
1124 | (button-face (if (display-graphic-p) |
1125 | '(:box (:line-width 2 :color "dark grey") | |
1126 | :background "light grey" | |
1127 | :foreground "black") | |
1128 | 'link))) | |
4b99edf2 | 1129 | (insert-text-button button-text 'face button-face 'follow-link t |
cb6c4991 CY |
1130 | 'package-symbol package |
1131 | 'action 'package-install-button-action))) | |
1132 | (built-in | |
4b99edf2 | 1133 | (insert (propertize "Built-in." 'font-lock-face 'font-lock-builtin-face))) |
cb6c4991 CY |
1134 | (t (insert "Deleted."))) |
1135 | (insert "\n") | |
4b99edf2 | 1136 | (and version (> (length version) 0) |
96ae4c8f CY |
1137 | (insert " " |
1138 | (propertize "Version" 'font-lock-face 'bold) ": " version "\n")) | |
4b99edf2 CY |
1139 | |
1140 | (setq reqs (if desc (package-desc-reqs desc))) | |
8adb4c33 | 1141 | (when reqs |
96ae4c8f | 1142 | (insert " " (propertize "Requires" 'font-lock-face 'bold) ": ") |
8adb4c33 CY |
1143 | (let ((first t) |
1144 | name vers text) | |
1145 | (dolist (req reqs) | |
1146 | (setq name (car req) | |
1147 | vers (cadr req) | |
1148 | text (format "%s-%s" (symbol-name name) | |
1149 | (package-version-join vers))) | |
1150 | (cond (first (setq first nil)) | |
1151 | ((>= (+ 2 (current-column) (length text)) | |
1152 | (window-width)) | |
1153 | (insert ",\n ")) | |
1154 | (t (insert ", "))) | |
1155 | (help-insert-xref-button text 'help-package name)) | |
1156 | (insert "\n"))) | |
96ae4c8f | 1157 | (insert " " (propertize "Summary" 'font-lock-face 'bold) |
4b99edf2 | 1158 | ": " (if desc (package-desc-doc desc)) "\n\n") |
cb6c4991 | 1159 | |
4b99edf2 | 1160 | (if built-in |
96ae4c8f CY |
1161 | ;; For built-in packages, insert the commentary. |
1162 | (let ((fn (locate-file (concat package-name ".el") load-path | |
1163 | load-file-rep-suffixes)) | |
1164 | (opoint (point))) | |
1165 | (insert (or (lm-commentary fn) "")) | |
1166 | (save-excursion | |
1167 | (goto-char opoint) | |
1168 | (when (re-search-forward "^;;; Commentary:\n" nil t) | |
1169 | (replace-match "")) | |
1170 | (while (re-search-forward "^\\(;+ ?\\)" nil t) | |
1171 | (replace-match "")))) | |
1172 | (let ((readme (expand-file-name (concat package-name "-readme.txt") | |
1173 | package-user-dir))) | |
1174 | ;; For elpa packages, try downloading the commentary. If that | |
1175 | ;; fails, try an existing readme file in `package-user-dir'. | |
1176 | (cond ((let ((buffer (ignore-errors | |
1177 | (url-retrieve-synchronously | |
1178 | (concat (package-archive-url package) | |
1179 | package-name "-readme.txt")))) | |
1180 | response) | |
1181 | (when buffer | |
1182 | (with-current-buffer buffer | |
1183 | (setq response (url-http-parse-response)) | |
1184 | (if (or (< response 200) (>= response 300)) | |
1185 | (setq response nil) | |
1186 | (setq buffer-file-name | |
1187 | (expand-file-name readme package-user-dir)) | |
1188 | (delete-region (point-min) (1+ url-http-end-of-headers)) | |
1189 | (save-buffer))) | |
1190 | (when response | |
1191 | (insert-buffer-substring buffer) | |
1192 | (kill-buffer buffer) | |
1193 | t)))) | |
1194 | ((file-readable-p readme) | |
1195 | (insert-file-contents readme) | |
1196 | (goto-char (point-max)))))))) | |
cb6c4991 CY |
1197 | |
1198 | (defun package-install-button-action (button) | |
1199 | (let ((package (button-get button 'package-symbol))) | |
1200 | (when (y-or-n-p (format "Install package `%s'? " package)) | |
1201 | (package-install package) | |
1202 | (revert-buffer nil t) | |
1203 | (goto-char (point-min))))) | |
cced7584 CY |
1204 | |
1205 | \f | |
44198b6e CY |
1206 | ;;;; Package menu mode. |
1207 | ||
54ea2a0d | 1208 | (defvar package-menu-mode-map |
64eba874 DN |
1209 | (let ((map (make-keymap)) |
1210 | (menu-map (make-sparse-keymap "Package"))) | |
43207249 | 1211 | (set-keymap-parent map button-buffer-map) |
8adb4c33 | 1212 | (define-key map "\C-m" 'package-menu-describe-package) |
54ea2a0d JB |
1213 | (define-key map "q" 'quit-window) |
1214 | (define-key map "n" 'next-line) | |
1215 | (define-key map "p" 'previous-line) | |
1216 | (define-key map "u" 'package-menu-mark-unmark) | |
1217 | (define-key map "\177" 'package-menu-backup-unmark) | |
1218 | (define-key map "d" 'package-menu-mark-delete) | |
1219 | (define-key map "i" 'package-menu-mark-install) | |
e687c2cd | 1220 | (define-key map "g" 'revert-buffer) |
54ea2a0d JB |
1221 | (define-key map "r" 'package-menu-refresh) |
1222 | (define-key map "~" 'package-menu-mark-obsolete-for-deletion) | |
1223 | (define-key map "x" 'package-menu-execute) | |
1224 | (define-key map "h" 'package-menu-quick-help) | |
cb6c4991 | 1225 | (define-key map "?" 'package-menu-describe-package) |
96ae4c8f CY |
1226 | (define-key map [follow-link] 'mouse-face) |
1227 | (define-key map [mouse-2] 'mouse-select-window) | |
64eba874 DN |
1228 | (define-key map [menu-bar package-menu] (cons "Package" menu-map)) |
1229 | (define-key menu-map [mq] | |
1230 | '(menu-item "Quit" quit-window | |
1231 | :help "Quit package selection")) | |
1232 | (define-key menu-map [s1] '("--")) | |
1233 | (define-key menu-map [mn] | |
1234 | '(menu-item "Next" next-line | |
1235 | :help "Next Line")) | |
1236 | (define-key menu-map [mp] | |
1237 | '(menu-item "Previous" previous-line | |
1238 | :help "Previous Line")) | |
1239 | (define-key menu-map [s2] '("--")) | |
1240 | (define-key menu-map [mu] | |
1241 | '(menu-item "Unmark" package-menu-mark-unmark | |
1242 | :help "Clear any marks on a package and move to the next line")) | |
1243 | (define-key menu-map [munm] | |
1244 | '(menu-item "Unmark backwards" package-menu-backup-unmark | |
1245 | :help "Back up one line and clear any marks on that package")) | |
1246 | (define-key menu-map [md] | |
1247 | '(menu-item "Mark for deletion" package-menu-mark-delete | |
1248 | :help "Mark a package for deletion and move to the next line")) | |
1249 | (define-key menu-map [mi] | |
1250 | '(menu-item "Mark for install" package-menu-mark-install | |
1251 | :help "Mark a package for installation and move to the next line")) | |
1252 | (define-key menu-map [s3] '("--")) | |
1253 | (define-key menu-map [mg] | |
e687c2cd | 1254 | '(menu-item "Update package list" revert-buffer |
64eba874 DN |
1255 | :help "Update the list of packages")) |
1256 | (define-key menu-map [mr] | |
1257 | '(menu-item "Refresh package list" package-menu-refresh | |
1258 | :help "Download the ELPA archive")) | |
1259 | (define-key menu-map [s4] '("--")) | |
1260 | (define-key menu-map [mt] | |
1261 | '(menu-item "Mark obsolete packages" package-menu-mark-obsolete-for-deletion | |
1262 | :help "Mark all obsolete packages for deletion")) | |
1263 | (define-key menu-map [mx] | |
1264 | '(menu-item "Execute actions" package-menu-execute | |
1265 | :help "Perform all the marked actions")) | |
1266 | (define-key menu-map [s5] '("--")) | |
1267 | (define-key menu-map [mh] | |
1268 | '(menu-item "Help" package-menu-quick-help | |
1269 | :help "Show short key binding help for package-menu-mode")) | |
1270 | (define-key menu-map [mc] | |
1271 | '(menu-item "View Commentary" package-menu-view-commentary | |
1272 | :help "Display information about this package")) | |
54ea2a0d | 1273 | map) |
44198b6e CY |
1274 | "Local keymap for `package-menu-mode' buffers.") |
1275 | ||
44198b6e CY |
1276 | (defvar package-menu-sort-button-map |
1277 | (let ((map (make-sparse-keymap))) | |
1278 | (define-key map [header-line mouse-1] 'package-menu-sort-by-column) | |
96ae4c8f | 1279 | (define-key map [header-line mouse-2] 'package-menu-sort-by-column) |
44198b6e CY |
1280 | (define-key map [follow-link] 'mouse-face) |
1281 | map) | |
1282 | "Local keymap for package menu sort buttons.") | |
1283 | ||
1284 | (put 'package-menu-mode 'mode-class 'special) | |
1285 | ||
1286 | (defun package-menu-mode () | |
1287 | "Major mode for browsing a list of packages. | |
1288 | Letters do not insert themselves; instead, they are commands. | |
1289 | \\<package-menu-mode-map> | |
1290 | \\{package-menu-mode-map}" | |
1291 | (kill-all-local-variables) | |
1292 | (use-local-map package-menu-mode-map) | |
1293 | (setq major-mode 'package-menu-mode) | |
1294 | (setq mode-name "Package Menu") | |
1295 | (setq truncate-lines t) | |
1296 | (setq buffer-read-only t) | |
cd205c76 | 1297 | (set (make-local-variable 'revert-buffer-function) 'package-menu-revert) |
187d3296 CY |
1298 | (setq header-line-format |
1299 | (mapconcat | |
1300 | (lambda (pair) | |
1301 | (let ((column (car pair)) | |
1302 | (name (cdr pair))) | |
1303 | (concat | |
1304 | ;; Insert a space that aligns the button properly. | |
1305 | (propertize " " 'display (list 'space :align-to column) | |
1306 | 'face 'fixed-pitch) | |
1307 | ;; Set up the column button. | |
1308 | (propertize name | |
1309 | 'column-name name | |
1310 | 'help-echo "mouse-1: sort by column" | |
1311 | 'mouse-face 'highlight | |
1312 | 'keymap package-menu-sort-button-map)))) | |
1313 | ;; We take a trick from buff-menu and have a dummy leading | |
1314 | ;; space to align the header line with the beginning of the | |
1315 | ;; text. This doesn't really work properly on Emacs 21, but | |
1316 | ;; it is close enough. | |
1317 | '((0 . "") | |
1318 | (2 . "Package") | |
1319 | (20 . "Version") | |
1320 | (32 . "Status") | |
1321 | (43 . "Description")) | |
1322 | "")) | |
1323 | (run-mode-hooks 'package-menu-mode-hook)) | |
44198b6e CY |
1324 | |
1325 | (defun package-menu-refresh () | |
ebf662f4 CY |
1326 | "Download the Emacs Lisp package archive. |
1327 | This fetches the contents of each archive specified in | |
1328 | `package-archives', and then refreshes the package menu." | |
44198b6e | 1329 | (interactive) |
187d3296 CY |
1330 | (unless (eq major-mode 'package-menu-mode) |
1331 | (error "The current buffer is not a Package Menu")) | |
44198b6e | 1332 | (package-refresh-contents) |
96ae4c8f | 1333 | (package--generate-package-list)) |
44198b6e | 1334 | |
e687c2cd CY |
1335 | (defun package-menu-revert (&optional arg noconfirm) |
1336 | "Update the list of packages. | |
1337 | This function is the `revert-buffer-function' for Package Menu | |
1338 | buffers. The arguments are ignored." | |
44198b6e | 1339 | (interactive) |
187d3296 CY |
1340 | (unless (eq major-mode 'package-menu-mode) |
1341 | (error "The current buffer is not a Package Menu")) | |
96ae4c8f | 1342 | (package--generate-package-list)) |
44198b6e | 1343 | |
8adb4c33 CY |
1344 | (defun package-menu-describe-package () |
1345 | "Describe the package in the current line." | |
1346 | (interactive) | |
1347 | (let ((name (package-menu-get-package))) | |
1348 | (if name | |
1349 | (describe-package (intern name)) | |
1350 | (message "No package on this line")))) | |
1351 | ||
44198b6e CY |
1352 | (defun package-menu-mark-internal (what) |
1353 | (unless (eobp) | |
1354 | (let ((buffer-read-only nil)) | |
1355 | (beginning-of-line) | |
1356 | (delete-char 1) | |
1357 | (insert what) | |
1358 | (forward-line)))) | |
1359 | ||
1360 | ;; fixme numeric argument | |
1361 | (defun package-menu-mark-delete (num) | |
1362 | "Mark a package for deletion and move to the next line." | |
1363 | (interactive "p") | |
1364 | (package-menu-mark-internal "D")) | |
1365 | ||
1366 | (defun package-menu-mark-install (num) | |
1367 | "Mark a package for installation and move to the next line." | |
1368 | (interactive "p") | |
1369 | (package-menu-mark-internal "I")) | |
1370 | ||
1371 | (defun package-menu-mark-unmark (num) | |
1372 | "Clear any marks on a package and move to the next line." | |
1373 | (interactive "p") | |
1374 | (package-menu-mark-internal " ")) | |
1375 | ||
1376 | (defun package-menu-backup-unmark () | |
1377 | "Back up one line and clear any marks on that package." | |
1378 | (interactive) | |
1379 | (forward-line -1) | |
1380 | (package-menu-mark-internal " ") | |
1381 | (forward-line -1)) | |
1382 | ||
1383 | (defun package-menu-mark-obsolete-for-deletion () | |
1384 | "Mark all obsolete packages for deletion." | |
1385 | (interactive) | |
1386 | (save-excursion | |
1387 | (goto-char (point-min)) | |
1388 | (forward-line 2) | |
1389 | (while (not (eobp)) | |
1390 | (if (looking-at ".*\\s obsolete\\s ") | |
1391 | (package-menu-mark-internal "D") | |
1392 | (forward-line 1))))) | |
1393 | ||
1394 | (defun package-menu-quick-help () | |
1395 | "Show short key binding help for package-menu-mode." | |
1396 | (interactive) | |
1397 | (message "n-ext, i-nstall, d-elete, u-nmark, x-ecute, r-efresh, h-elp")) | |
1398 | ||
cb6c4991 CY |
1399 | (define-obsolete-function-alias |
1400 | 'package-menu-view-commentary 'package-menu-describe-package "24.1") | |
44198b6e CY |
1401 | |
1402 | ;; Return the name of the package on the current line. | |
1403 | (defun package-menu-get-package () | |
1404 | (save-excursion | |
1405 | (beginning-of-line) | |
1406 | (if (looking-at ". \\([^ \t]*\\)") | |
8adb4c33 | 1407 | (match-string-no-properties 1)))) |
44198b6e CY |
1408 | |
1409 | ;; Return the version of the package on the current line. | |
1410 | (defun package-menu-get-version () | |
1411 | (save-excursion | |
1412 | (beginning-of-line) | |
1413 | (if (looking-at ". [^ \t]*[ \t]*\\([0-9.]*\\)") | |
1414 | (match-string 1)))) | |
1415 | ||
1416 | (defun package-menu-get-status () | |
1417 | (save-excursion | |
1418 | (if (looking-at ". [^ \t]*[ \t]*[^ \t]*[ \t]*\\([^ \t]*\\)") | |
1419 | (match-string 1) | |
1420 | ""))) | |
1421 | ||
1422 | (defun package-menu-execute () | |
1423 | "Perform all the marked actions. | |
1424 | Packages marked for installation will be downloaded and | |
1425 | installed. Packages marked for deletion will be removed. | |
1426 | Note that after installing packages you will want to restart | |
1427 | Emacs." | |
1428 | (interactive) | |
1429 | (goto-char (point-min)) | |
44198b6e CY |
1430 | (while (not (eobp)) |
1431 | (let ((cmd (char-after)) | |
1432 | (pkg-name (package-menu-get-package)) | |
1433 | (pkg-vers (package-menu-get-version)) | |
1434 | (pkg-status (package-menu-get-status))) | |
1435 | (cond | |
1436 | ((eq cmd ?D) | |
1437 | (when (and (string= pkg-status "installed") | |
1438 | (string= pkg-name "package")) | |
1439 | ;; FIXME: actually, we could be tricky and remove all info. | |
1440 | ;; But that is drastic and the user can do that instead. | |
1441 | (error "Can't delete most recent version of `package'")) | |
1442 | ;; Ask for confirmation here? Maybe if package status is ""? | |
1443 | ;; Or if any lisp from package is actually loaded? | |
1444 | (message "Deleting %s-%s..." pkg-name pkg-vers) | |
1445 | (package-delete pkg-name pkg-vers) | |
1446 | (message "Deleting %s-%s... done" pkg-name pkg-vers)) | |
1447 | ((eq cmd ?I) | |
1448 | (package-install (intern pkg-name))))) | |
1449 | (forward-line)) | |
1450 | (package-menu-revert)) | |
1451 | ||
1452 | (defun package-print-package (package version key desc) | |
1453 | (let ((face | |
376c2b6b | 1454 | (cond ((string= key "built-in") 'font-lock-builtin-face) |
44198b6e CY |
1455 | ((string= key "available") 'default) |
1456 | ((string= key "held") 'font-lock-constant-face) | |
1457 | ((string= key "disabled") 'font-lock-warning-face) | |
1458 | ((string= key "installed") 'font-lock-comment-face) | |
1459 | (t ; obsolete, but also the default. | |
1460 | 'font-lock-warning-face)))) | |
1461 | (insert (propertize " " 'font-lock-face face)) | |
8adb4c33 CY |
1462 | (insert-text-button (symbol-name package) |
1463 | 'face 'link | |
1464 | 'follow-link t | |
1465 | 'package-symbol package | |
1466 | 'action (lambda (button) | |
1467 | (describe-package | |
1468 | (button-get button 'package-symbol)))) | |
44198b6e CY |
1469 | (indent-to 20 1) |
1470 | (insert (propertize (package-version-join version) 'font-lock-face face)) | |
8adb4c33 | 1471 | (indent-to 32 1) |
44198b6e CY |
1472 | (insert (propertize key 'font-lock-face face)) |
1473 | ;; FIXME: this 'when' is bogus... | |
1474 | (when desc | |
8adb4c33 | 1475 | (indent-to 43 1) |
376c2b6b CY |
1476 | (let ((opoint (point))) |
1477 | (insert (propertize desc 'font-lock-face face)) | |
1478 | (upcase-region opoint (min (point) (1+ opoint))))) | |
44198b6e CY |
1479 | (insert "\n"))) |
1480 | ||
1481 | (defun package-list-maybe-add (package version status description result) | |
1482 | (unless (assoc (cons package version) result) | |
ebf662f4 | 1483 | (push (list (cons package version) status description) result)) |
44198b6e CY |
1484 | result) |
1485 | ||
187d3296 CY |
1486 | (defvar package-menu-package-list nil |
1487 | "List of packages to display in the Package Menu buffer. | |
1488 | A value of nil means to display all packages.") | |
44198b6e | 1489 | |
187d3296 CY |
1490 | (defvar package-menu-sort-key nil |
1491 | "Sort key for the current Package Menu buffer.") | |
1492 | ||
1493 | (defun package--generate-package-list () | |
1494 | "Populate the current Package Menu buffer." | |
187d3296 CY |
1495 | (let ((inhibit-read-only t) |
1496 | info-list name desc hold builtin) | |
44198b6e | 1497 | (erase-buffer) |
187d3296 CY |
1498 | ;; List installed packages |
1499 | (dolist (elt package-alist) | |
1500 | (setq name (car elt)) | |
4b99edf2 CY |
1501 | (when (or (null package-menu-package-list) |
1502 | (memq name package-menu-package-list)) | |
187d3296 | 1503 | (setq desc (cdr elt) |
4b99edf2 | 1504 | hold (cadr (assq name package-load-list))) |
187d3296 CY |
1505 | (setq info-list |
1506 | (package-list-maybe-add | |
1507 | name (package-desc-vers desc) | |
1508 | ;; FIXME: it turns out to be tricky to see if this | |
1509 | ;; package is presently activated. | |
4b99edf2 CY |
1510 | (if (stringp hold) "held" "installed") |
1511 | (package-desc-doc desc) | |
1512 | info-list)))) | |
1513 | ||
1514 | ;; List built-in packages | |
1515 | (dolist (elt package--builtins) | |
1516 | (setq name (car elt)) | |
1517 | (when (and (not (eq name 'emacs)) ; Hide the `emacs' package. | |
1518 | (or (null package-menu-package-list) | |
1519 | (memq name package-menu-package-list))) | |
1520 | (setq desc (cdr elt)) | |
1521 | (setq info-list | |
1522 | (package-list-maybe-add | |
1523 | name (package-desc-vers desc) | |
1524 | "built-in" | |
187d3296 CY |
1525 | (package-desc-doc desc) |
1526 | info-list)))) | |
1527 | ||
1528 | ;; List available and disabled packages | |
1529 | (dolist (elt package-archive-contents) | |
1530 | (setq name (car elt) | |
1531 | desc (cdr elt) | |
1532 | hold (assq name package-load-list)) | |
1533 | (when (or (null package-menu-package-list) | |
1534 | (memq name package-menu-package-list)) | |
1535 | (setq info-list | |
1536 | (package-list-maybe-add name | |
1537 | (package-desc-vers desc) | |
1538 | (if (and hold (null (cadr hold))) | |
1539 | "disabled" | |
1540 | "available") | |
1541 | (package-desc-doc (cdr elt)) | |
1542 | info-list)))) | |
1543 | ;; List obsolete packages | |
1544 | (mapc (lambda (elt) | |
1545 | (mapc (lambda (inner-elt) | |
1546 | (setq info-list | |
1547 | (package-list-maybe-add (car elt) | |
1548 | (package-desc-vers | |
1549 | (cdr inner-elt)) | |
1550 | "obsolete" | |
1551 | (package-desc-doc | |
1552 | (cdr inner-elt)) | |
1553 | info-list))) | |
1554 | (cdr elt))) | |
1555 | package-obsolete-alist) | |
1556 | ||
1557 | (setq info-list | |
1558 | (sort info-list | |
1559 | (cond ((string= package-menu-sort-key "Package") | |
1560 | 'package-menu--name-predicate) | |
1561 | ((string= package-menu-sort-key "Version") | |
1562 | 'package-menu--version-predicate) | |
1563 | ((string= package-menu-sort-key "Description") | |
1564 | 'package-menu--description-predicate) | |
1565 | (t ; By default, sort by package status | |
1566 | 'package-menu--status-predicate)))) | |
1567 | ||
1568 | (dolist (elt info-list) | |
1569 | (package-print-package (car (car elt)) | |
1570 | (cdr (car elt)) | |
1571 | (car (cdr elt)) | |
1572 | (car (cdr (cdr elt))))) | |
44198b6e | 1573 | (goto-char (point-min)) |
96ae4c8f | 1574 | (set-buffer-modified-p nil) |
44198b6e CY |
1575 | (current-buffer))) |
1576 | ||
96ae4c8f | 1577 | (defun package-menu--version-predicate (left right) |
187d3296 CY |
1578 | (let ((vleft (or (cdr (car left)) '(0))) |
1579 | (vright (or (cdr (car right)) '(0)))) | |
1580 | (if (version-list-= vleft vright) | |
96ae4c8f | 1581 | (package-menu--name-predicate left right) |
187d3296 | 1582 | (version-list-< vleft vright)))) |
96ae4c8f CY |
1583 | |
1584 | (defun package-menu--status-predicate (left right) | |
1585 | (let ((sleft (cadr left)) | |
1586 | (sright (cadr right))) | |
1587 | (cond ((string= sleft sright) | |
1588 | (package-menu--name-predicate left right)) | |
1589 | ((string= sleft "available") t) | |
1590 | ((string= sright "available") nil) | |
1591 | ((string= sleft "installed") t) | |
1592 | ((string= sright "installed") nil) | |
1593 | ((string= sleft "held") t) | |
1594 | ((string= sright "held") nil) | |
1595 | ((string= sleft "built-in") t) | |
1596 | ((string= sright "built-in") nil) | |
1597 | ((string= sleft "obsolete") t) | |
1598 | ((string= sright "obsolete") nil) | |
1599 | (t (string< sleft sright))))) | |
1600 | ||
1601 | (defun package-menu--description-predicate (left right) | |
1602 | (let ((sleft (car (cddr left))) | |
1603 | (sright (car (cddr right)))) | |
1604 | (if (string= sleft sright) | |
1605 | (package-menu--name-predicate left right) | |
1606 | (string< sleft sright)))) | |
1607 | ||
1608 | (defun package-menu--name-predicate (left right) | |
1609 | (string< (symbol-name (caar left)) | |
1610 | (symbol-name (caar right)))) | |
1611 | ||
44198b6e | 1612 | (defun package-menu-sort-by-column (&optional e) |
187d3296 | 1613 | "Sort the package menu by the column of the mouse click E." |
96ae4c8f | 1614 | (interactive "e") |
44198b6e | 1615 | (let* ((pos (event-start e)) |
187d3296 CY |
1616 | (obj (posn-object pos)) |
1617 | (col (if obj | |
1618 | (get-text-property (cdr obj) 'column-name (car obj)) | |
1619 | (get-text-property (posn-point pos) 'column-name))) | |
1620 | (buf (window-buffer (posn-window (event-start e))))) | |
1621 | (with-current-buffer buf | |
1622 | (when (eq major-mode 'package-menu-mode) | |
1623 | (setq package-menu-sort-key col) | |
1624 | (package--generate-package-list))))) | |
96ae4c8f CY |
1625 | |
1626 | (defun package--list-packages (&optional packages) | |
187d3296 CY |
1627 | "Generate and pop to the *Packages* buffer. |
1628 | Optional PACKAGES is a list of names of packages (symbols) to | |
1629 | list; the default is to display everything in `package-alist'." | |
4b99edf2 | 1630 | (require 'finder-inf nil t) |
187d3296 | 1631 | (with-current-buffer (get-buffer-create "*Packages*") |
44198b6e | 1632 | (package-menu-mode) |
187d3296 CY |
1633 | (set (make-local-variable 'package-menu-package-list) packages) |
1634 | (set (make-local-variable 'package-menu-sort-key) nil) | |
1635 | (package--generate-package-list) | |
44198b6e | 1636 | ;; It's okay to use pop-to-buffer here. The package menu buffer |
96ae4c8f CY |
1637 | ;; has keybindings, and the user just typed `M-x list-packages', |
1638 | ;; suggesting that they might want to use them. | |
44198b6e CY |
1639 | (pop-to-buffer (current-buffer)))) |
1640 | ||
1641 | ;;;###autoload | |
96ae4c8f | 1642 | (defun list-packages () |
44198b6e CY |
1643 | "Display a list of packages. |
1644 | Fetches the updated list of packages before displaying. | |
1645 | The list is displayed in a buffer named `*Packages*'." | |
1646 | (interactive) | |
8a500a91 | 1647 | ;; Initialize the package system if necessary. |
4b99edf2 CY |
1648 | (unless package--initialized |
1649 | (package-initialize t)) | |
44198b6e CY |
1650 | (package-refresh-contents) |
1651 | (package--list-packages)) | |
1652 | ||
cb8759ca | 1653 | ;;;###autoload |
96ae4c8f | 1654 | (defalias 'package-list-packages 'list-packages) |
cb8759ca | 1655 | |
44198b6e CY |
1656 | (defun package-list-packages-no-fetch () |
1657 | "Display a list of packages. | |
1658 | Does not fetch the updated list of packages before displaying. | |
1659 | The list is displayed in a buffer named `*Packages*'." | |
1660 | (interactive) | |
1661 | (package--list-packages)) | |
1662 | ||
44198b6e CY |
1663 | (provide 'package) |
1664 | ||
1665 | ;;; package.el ends here |