Clean up syntax-table usage in xml.el
[emacs.git] / lisp / cedet / inversion.el
blob877ed54566c4b0fdc36bff8437e89268f4ef15df
1 ;;; inversion.el --- When you need something in version XX.XX
3 ;;; Copyright (C) 2002-2003, 2005-2012 Free Software Foundation, Inc.
5 ;; Author: Eric M. Ludlam <zappo@gnu.org>
6 ;; Version: 0.2
7 ;; Keywords: OO, lisp
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24 ;;; Commentary:
26 ;; Keeping track of rapidly developing software is a tough thing to
27 ;; do, especially if you want to have co-dependent packages which all
28 ;; move at different rates.
30 ;; This library provides a framework for specifying version numbers
31 ;; and (as side effect) have a flexible way of getting a desired feature set.
33 ;; If you would like to use this package to satisfy dependency replace this:
35 ;; (require 'spiffy)
37 ;; with this:
39 ;; (require 'inversion)
40 ;; (inversion-require 'spiffy "1.0")
42 ;; If you feel the need to not throw errors, you can do this instead:
44 ;; (let ((err (inversion-test 'spiffy "1.0")))
45 ;; (if err (your-stuff-here)))
47 ;; If you new package (2.0) needs to make sure a load file from your
48 ;; package is compatible, use this test:
50 ;; (if (not (inversion-reverse-test 'spiffy version-from-file))
51 ;; ;; Everything ok
52 ;; (do stuff)
53 ;; ;; Out of date
54 ;; (import-old-code))
56 ;; If you would like to make inversion optional, do this:
58 ;; (or (require 'inversion nil t)
59 ;; (defun inversion-test (p v)
60 ;; (string= v (symbol-value
61 ;; (intern-soft (concat (symbol-string p) "-version"))))))
63 ;; Or modify to specify `inversion-require' instead.
65 ;; TODO:
66 ;; Offer to download newer versions of a package.
68 ;;; History:
70 ;; Sept 3, 2002: First general publication.
72 ;;; Code:
74 (defvar inversion-version "1.3"
75 "Current version of InVersion.")
77 (defvar inversion-incompatible-version "0.1alpha1"
78 "An earlier release which is incompatible with this release.")
80 (defconst inversion-decoders
82 (alpha "^\\([0-9]+\\)\\.\\([0-9]+\\)\\s-*\\.?alpha\\([0-9]+\\)?$" 3)
83 (beta "^\\([0-9]+\\)\\.\\([0-9]+\\)\\s-*\\.?beta\\([0-9]+\\)?$" 3)
84 (beta "^\\([0-9]+\\)\\.\\([0-9]+\\)\\s-*(beta\\([0-9]+\\)?)" 3)
85 (prerelease "^\\([0-9]+\\)\\.\\([0-9]+\\)\\s-*\\.?pre\\([0-9]+\\)?$" 3)
86 (full "^\\([0-9]+\\)\\.\\([0-9]+\\)$" 2)
87 (fullsingle "^\\([0-9]+\\)$" 1)
88 (patch "^\\([0-9]+\\)\\.\\([0-9]+\\) (patch \\([0-9]+\\))" 3)
89 (point "^\\([0-9]+\\)\\.\\([0-9]+\\)\\.\\([0-9]+\\)$" 3)
90 (build "^\\([0-9]+\\)\\.\\([0-9]+\\)\\.\\([0-9]+\\).\\([0-9]+\\)$" 4)
92 "List of decoders for version strings.
93 Each decoder is of the form:
95 ( RELEASE-TYPE REGEXP MAX )
97 RELEASE-TYPE is a symbol specifying something like `beta' or `alpha'.
98 REGEXP is the regular expression to match a version string.
99 MAX is the maximum number of match-numbers in the release number.
100 Decoders must be ordered to decode least stable versions before the
101 more stable ones.")
103 ;;; Version Checking
105 (defun inversion-decode-version (version-string)
106 "Decode VERSION-STRING into an encoded list.
107 Return value is of the form:
108 (RELEASE MAJOR MINOR ...)
109 where RELEASE is a symbol such as `full', or `beta'."
110 (let ((decoders inversion-decoders)
111 (result nil))
112 (while (and decoders (not result))
113 (if (string-match (nth 1 (car decoders)) version-string)
114 (let ((ver nil)
115 (num-left (nth 2 (car decoders)))
116 (count 1))
117 (while (<= count num-left)
118 (setq ver (cons
119 (if (match-beginning count)
120 (string-to-number
121 (substring version-string
122 (match-beginning count)
123 (match-end count)))
125 ver)
126 count (1+ count)))
127 (setq result (cons (caar decoders) (nreverse ver))))
128 (setq decoders (cdr decoders))))
129 result))
131 (defun inversion-package-version (package)
132 "Return the decoded version for PACKAGE."
133 (let ((ver (symbol-value
134 (intern-soft
135 (concat (symbol-name package)
136 "-version"))))
137 (code nil))
138 (unless ver
139 (error "Package %S does not define %S-version" package package))
140 ;; Decode the code
141 (setq code (inversion-decode-version ver))
142 (unless code
143 (error "%S-version value cannot be decoded" package))
144 code))
146 (defun inversion-package-incompatibility-version (package)
147 "Return the decoded incompatibility version for PACKAGE.
148 The incompatibility version is specified by the programmer of
149 a package when a package is not backward compatible. It is
150 not an indication of new features or bug fixes."
151 (let ((ver (symbol-value
152 (intern-soft
153 (concat (symbol-name package)
154 "-incompatible-version")))))
155 (if (not ver)
157 ;; Decode the code
158 (inversion-decode-version ver))))
160 (defun inversion-recode (code)
161 "Convert CODE into a string."
162 (let ((r (nth 0 code)) ; release-type
163 (n (nth 1 code)) ; main number
164 (i (nth 2 code)) ; first increment
165 (p (nth 3 code))) ; second increment
166 (cond
167 ((eq r 'full)
168 (setq r "" p ""))
169 ((eq r 'point)
170 (setq r ".")))
171 (format "%s.%s%s%s" n i r p)))
173 (defun inversion-release-to-number (release-symbol)
174 "Convert RELEASE-SYMBOL into a number."
175 (let* ((ra (assoc release-symbol inversion-decoders))
176 (rn (- (length inversion-decoders)
177 (length (member ra inversion-decoders)))))
178 rn))
180 (defun inversion-= (ver1 ver2)
181 "Return non-nil if VER1 is equal to VER2."
182 (equal ver1 ver2))
184 (defun inversion-< (ver1 ver2)
185 "Return non-nil if VER1 is less than VER2."
186 (let ((v1-0 (inversion-release-to-number (nth 0 ver1)))
187 (v1-1 (nth 1 ver1))
188 (v1-2 (nth 2 ver1))
189 (v1-3 (nth 3 ver1))
190 (v1-4 (nth 4 ver1))
191 ;; v2
192 (v2-0 (inversion-release-to-number (nth 0 ver2)))
193 (v2-1 (nth 1 ver2))
194 (v2-2 (nth 2 ver2))
195 (v2-3 (nth 3 ver2))
196 (v2-4 (nth 4 ver2))
198 (or (and (= v1-0 v2-0)
199 (= v1-1 v2-1)
200 (= v1-2 v2-2)
201 (= v1-3 v2-3)
202 v1-4 v2-4 ; all or nothing if elt - is =
203 (< v1-4 v2-4))
204 (and (= v1-0 v2-0)
205 (= v1-1 v2-1)
206 (= v1-2 v2-2)
207 v1-3 v2-3 ; all or nothing if elt - is =
208 (< v1-3 v2-3))
209 (and (= v1-1 v2-1)
210 (< v1-2 v2-2))
211 (and (< v1-1 v2-1))
212 (and (< v1-0 v2-0)
213 (= v1-1 v2-1)
214 (= v1-2 v2-2)
218 (defun inversion-check-version (version incompatible-version
219 minimum &rest reserved)
220 "Check that a given version meets the minimum requirement.
221 VERSION, INCOMPATIBLE-VERSION and MINIMUM are of similar format to
222 return entries of `inversion-decode-version', or a classic version
223 string. INCOMPATIBLE-VERSION can be nil.
224 RESERVED arguments are kept for a later use.
225 Return:
226 - nil if everything is ok.
227 - 'outdated if VERSION is less than MINIMUM.
228 - 'incompatible if VERSION is not backward compatible with MINIMUM.
229 - t if the check failed."
230 (let ((code (if (stringp version)
231 (inversion-decode-version version)
232 version))
233 (req (if (stringp minimum)
234 (inversion-decode-version minimum)
235 minimum))
237 ;; Perform a test.
238 (cond
239 ((inversion-= code req)
240 ;; Same version.. Yay!
241 nil)
242 ((inversion-< code req)
243 ;; Version is too old!
244 'outdated)
245 ((inversion-< req code)
246 ;; Newer is installed. What to do?
247 (let ((incompatible
248 (if (stringp incompatible-version)
249 (inversion-decode-version incompatible-version)
250 incompatible-version)))
251 (cond
252 ((not incompatible) nil)
253 ((or (inversion-= req incompatible)
254 (inversion-< req incompatible))
255 ;; The requested version is = or < than what the package
256 ;; maintainer says is incompatible.
257 'incompatible)
258 ;; Things are ok.
259 (t nil))))
260 ;; Check failed
261 (t t))))
263 (defun inversion-test (package minimum &rest reserved)
264 "Test that PACKAGE meets the MINIMUM version requirement.
265 PACKAGE is a symbol, similar to what is passed to `require'.
266 MINIMUM is of similar format to return entries of
267 `inversion-decode-version', or a classic version string.
268 RESERVED arguments are kept for a later user.
269 This depends on the symbols `PACKAGE-version' and optionally
270 `PACKAGE-incompatible-version' being defined in PACKAGE.
271 Return nil if everything is ok. Return an error string otherwise."
272 (let ((check (inversion-check-version
273 (inversion-package-version package)
274 (inversion-package-incompatibility-version package)
275 minimum reserved)))
276 (cond
277 ((null check)
278 ;; Same version.. Yay!
279 nil)
280 ((eq check 'outdated)
281 ;; Version is too old!
282 (format "You need to upgrade package %s to %s" package minimum))
283 ((eq check 'incompatible)
284 ;; Newer is installed but the requested version is = or < than
285 ;; what the package maintainer says is incompatible, then throw
286 ;; that error.
287 (format "Package %s version is not backward compatible with %s"
288 package minimum))
289 ;; Check failed
290 (t "Inversion version check failed."))))
292 (defun inversion-reverse-test (package oldversion &rest reserved)
293 "Test that PACKAGE at OLDVERSION is still compatible.
294 If something like a save file is loaded at OLDVERSION, this
295 test will identify if OLDVERSION is compatible with the current version
296 of PACKAGE.
297 PACKAGE is a symbol, similar to what is passed to `require'.
298 OLDVERSION is of similar format to return entries of
299 `inversion-decode-version', or a classic version string.
300 RESERVED arguments are kept for a later user.
301 This depends on the symbols `PACKAGE-version' and optionally
302 `PACKAGE-incompatible-version' being defined in PACKAGE.
303 Return nil if everything is ok. Return an error string otherwise."
304 (let ((check (inversion-check-version
305 (inversion-package-version package)
306 (inversion-package-incompatibility-version package)
307 oldversion reserved)))
308 (cond
309 ((null check)
310 ;; Same version.. Yay!
311 nil)
312 ((eq check 'outdated)
313 ;; Version is too old!
314 (format "Package %s version %s is not compatible with current version"
315 package oldversion))
316 ((eq check 'incompatible)
317 ;; Newer is installed but the requested version is = or < than
318 ;; what the package maintainer says is incompatible, then throw
319 ;; that error.
320 (format "Package %s version is not backward compatible with %s"
321 package oldversion))
322 ;; Check failed
323 (t "Inversion version check failed."))))
325 (defun inversion-require (package version &optional file directory
326 &rest reserved)
327 "Declare that you need PACKAGE with at least VERSION.
328 PACKAGE might be found in FILE. (See `require'.)
329 Throws an error if VERSION is incompatible with what is installed.
330 Optional argument DIRECTORY is a location where new versions of
331 this tool can be located. If there is a versioning problem and
332 DIRECTORY is provided, inversion will offer to download the file.
333 Optional argument RESERVED is saved for later use."
334 (require package file)
335 (let ((err (inversion-test package version)))
336 (when err
337 (if directory
338 (inversion-download-package-ask err package directory version)
339 (error err)))
340 ;; Return the package symbol that was required.
341 package))
343 (defun inversion-require-emacs (emacs-ver xemacs-ver)
344 "Declare that you need either EMACS-VER, or XEMACS-VER.
345 Only checks one based on which kind of Emacs is being run."
346 (let ((err (inversion-test 'emacs
347 (if (featurep 'xemacs)
348 xemacs-ver
349 emacs-ver))))
350 (if err (error err)
351 ;; Something nice...
352 t)))
354 (defconst inversion-find-data
355 '("(def\\(var\\|const\\)\\s-+%s-%s\\s-+\"\\([^\"]+\\)" 2)
356 "Regexp template and match data index of a version string.")
358 (defun inversion-find-version (package)
359 "Search for the version and incompatible version of PACKAGE.
360 Does not load PACKAGE nor requires that it has been previously loaded.
361 Search in the directories in `load-path' for a PACKAGE.el library.
362 Visit the file found and search for the declarations of variables or
363 constants `PACKAGE-version' and `PACKAGE-incompatible-version'. The
364 value of these variables must be a version string.
366 Return a pair (VERSION-STRING . INCOMPATIBLE-VERSION-STRING) where
367 INCOMPATIBLE-VERSION-STRING can be nil.
368 Return nil when VERSION-STRING was not found."
369 (let* ((file (locate-library (format "%s.el" package) t))
370 (tag (car inversion-find-data))
371 (idx (nth 1 inversion-find-data))
372 version)
373 (when file
374 (with-temp-buffer
375 ;; The 3000 is a bit arbitrary, but should cut down on
376 ;; fileio as version info usually is at the very top
377 ;; of a file. After a long commentary could be bad.
378 (insert-file-contents-literally file nil 0 3000)
379 (goto-char (point-min))
380 (when (re-search-forward (format tag package 'version) nil t)
381 (setq version (list (match-string idx)))
382 (goto-char (point-min))
383 (when (re-search-forward
384 (format tag package 'incompatible-version) nil t)
385 (setcdr version (match-string idx))))))
386 version))
388 (defun inversion-add-to-load-path (package minimum
389 &optional installdir
390 &rest subdirs)
391 "Add the PACKAGE path to `load-path' if necessary.
392 MINIMUM is the minimum version requirement of PACKAGE.
393 Optional argument INSTALLDIR is the base directory where PACKAGE is
394 installed. It defaults to `default-directory'/PACKAGE.
395 SUBDIRS are sub-directories to add to `load-path', following the main
396 INSTALLDIR path."
397 (let ((ver (inversion-find-version package)))
398 ;; If PACKAGE not found or a bad version already in `load-path',
399 ;; prepend the new PACKAGE path, so it will be loaded first.
400 (when (or (not ver)
401 (and
402 (inversion-check-version (car ver) (cdr ver) minimum)
403 (message "Outdated %s %s shadowed to meet minimum version %s"
404 package (car ver) minimum)
406 (let* ((default-directory
407 (or installdir
408 (expand-file-name (format "./%s" package))))
409 subdir)
410 (when (file-directory-p default-directory)
411 ;; Add SUBDIRS
412 (while subdirs
413 (setq subdir (expand-file-name (car subdirs))
414 subdirs (cdr subdirs))
415 (when (file-directory-p subdir)
416 ;;(message "%S added to `load-path'" subdir)
417 (add-to-list 'load-path subdir)))
418 ;; Add the main path
419 ;;(message "%S added to `load-path'" default-directory)
420 (add-to-list 'load-path default-directory))
421 ;; We get to this point iff we do not accept or there is no
422 ;; system file. Let's check the version of what we just
423 ;; installed... just to be safe.
424 (let ((newver (inversion-find-version package)))
425 (if (not newver)
426 (error "Failed to find version for newly installed %s"
427 package))
428 (if (inversion-check-version (car newver) (cdr newver) minimum)
429 (error "Outdated %s %s just installed" package (car newver)))
430 )))))
432 ;;; URL and downloading code
434 (defun inversion-locate-package-files (package directory &optional version)
435 "Get a list of distributions of PACKAGE from DIRECTORY.
436 DIRECTORY can be an ange-ftp compatible filename, such as:
437 \"/ftp@ftp1.sourceforge.net/pub/sourceforge/PACKAGE\"
438 If it is a URL, wget will be used for download.
439 Optional argument VERSION will restrict the list of available versions
440 to the file matching VERSION exactly, or nil."
441 ;;DIRECTORY should also allow a URL:
442 ;; \"http://ftp1.sourceforge.net/PACKAGE\"
443 ;; but then I can get file listings easily.
444 (if (symbolp package) (setq package (symbol-name package)))
445 (directory-files directory t
446 (if version
447 (concat "^" package "-" version "\\>")
448 package)))
450 (defvar inversion-package-common-tails '( ".tar.gz"
451 ".tar"
452 ".zip"
453 ".gz"
455 "Common distribution mechanisms for Emacs Lisp packages.")
457 (defun inversion-locate-package-files-and-split (package directory &optional version)
458 "Use `inversion-locate-package-files' to get a list of PACKAGE files.
459 DIRECTORY is the location where distributions of PACKAGE are.
460 VERSION is an optional argument specifying a version to restrict to.
461 The return list is an alist with the version string in the CAR,
462 and the full path name in the CDR."
463 (if (symbolp package) (setq package (symbol-name package)))
464 (let ((f (inversion-locate-package-files package directory version))
465 (out nil))
466 (while f
467 (let* ((file (car f))
468 (dist (file-name-nondirectory file))
469 (tails inversion-package-common-tails)
470 (verstring nil))
471 (while (and tails (not verstring))
472 (when (string-match (concat (car tails) "$") dist)
473 (setq verstring
474 (substring dist (1+ (length package)) (match-beginning 0))))
475 (setq tails (cdr tails)))
476 (if (not verstring)
477 (error "Cannot decode version for %s" dist))
478 (setq out
479 (cons
480 (cons verstring file)
481 out))
482 (setq f (cdr f))))
483 out))
485 (defun inversion-download-package-ask (err package directory version)
486 "Due to ERR, offer to download PACKAGE from DIRECTORY.
487 The package should have VERSION available for download."
488 (if (symbolp package) (setq package (symbol-name package)))
489 (let ((files (inversion-locate-package-files-and-split
490 package directory version)))
491 (if (not files)
492 (error err)
493 (if (not (y-or-n-p (concat err ": Download update? ")))
494 (error err)
495 (let ((dest (read-directory-name (format "Download %s to: "
496 package)
497 t)))
498 (if (> (length files) 1)
499 (setq files
500 (list
501 "foo" ;; ignored
502 (read-file-name "Version to download: "
503 directory
504 files
506 (concat
507 (file-name-as-directory directory)
508 package)
509 nil))))
511 (copy-file (cdr (car files)) dest))))))
513 ;;; How we upgrade packages in Emacs has yet to be ironed out.
515 ;; (defun inversion-upgrade-package (package &optional directory)
516 ;; "Try to upgrade PACKAGE in DIRECTORY is available."
517 ;; (interactive "sPackage to upgrade: ")
518 ;; (if (stringp package) (setq package (intern package)))
519 ;; (if (not directory)
520 ;; ;; Hope that the package maintainer specified.
521 ;; (setq directory (symbol-value (or (intern-soft
522 ;; (concat (symbol-name package)
523 ;; "-url"))
524 ;; (intern-soft
525 ;; (concat (symbol-name package)
526 ;; "-directory"))))))
527 ;; (let ((files (inversion-locate-package-files-and-split
528 ;; package directory))
529 ;; (cver (inversion-package-version package))
530 ;; (newer nil))
531 ;; (mapc (lambda (f)
532 ;; (if (inversion-< cver (inversion-decode-version (car f)))
533 ;; (setq newer (cons f newer))))
534 ;; files)
535 ;; newer
536 ;; ))
538 (provide 'inversion)
540 ;;; inversion.el ends here