Properly integrate modules into the loading process (Bug#30164).
[emacs.git] / lisp / cedet / inversion.el
blob48010f7315e68058a451720da772bcce9b4527dc
1 ;;; inversion.el --- When you need something in version XX.XX
3 ;;; Copyright (C) 2002-2003, 2005-2018 Free Software Foundation, Inc.
5 ;; Author: Eric M. Ludlam <zappo@gnu.org>
6 ;; Version: 1.3
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 <https://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]+\\)\\.?\\([0-9]*\\)?\\s-*\\.?alpha\\([0-9]+\\)?$" 4)
83 (beta "^\\([0-9]+\\)\\.\\([0-9]+\\)\\.?\\([0-9]*\\)?\\s-*\\.?beta\\([0-9]+\\)?$" 4)
84 (beta "^\\([0-9]+\\)\\.\\([0-9]+\\)\\.?\\([0-9]*\\)?\\s-*\\.?(beta\\([0-9]+\\)?)$" 4)
85 (beta "^[^/]+/\\w+--\\w+--\\([0-9]+\\)\\.\\([0-9]+\\)\\.\\([0-9]+\\)--patch-\\([0-9]+\\)" 4)
86 (beta "^\\w+: v\\([0-9]+\\)\\.\\([0-9]+\\)\\.\\([0-9]+\\)-\\([0-9]+\\)-\\(.*\\)" 5)
87 (prerelease "^\\([0-9]+\\)\\.\\([0-9]+\\)\\s-*\\.?pre\\([0-9]+\\)?$" 3)
88 (full "^\\([0-9]+\\)\\.\\([0-9]+\\)\\.?\\([0-9]+\\)?$" 3)
89 (fullsingle "^\\([0-9]+\\)$" 1)
90 (patch "^\\([0-9]+\\)\\.\\([0-9]+\\)\\.?\\([0-9]+\\)?\\s-*(patch \\([0-9]+\\))" 4)
91 (point "^\\([0-9]+\\)\\.\\([0-9]+\\)\\.\\([0-9]+\\)$" 3)
92 (point "^\\w+: v\\([0-9]+\\)\\.\\([0-9]+\\)\\.\\([0-9]+\\)-\\(0\\)-\\(.*\\)" 5)
93 (build "^\\([0-9]+\\)\\.\\([0-9]+\\)\\.\\([0-9]+\\).\\([0-9]+\\)$" 4)
94 (full "^[^/]+/\\w+--\\w+--\\([0-9]+\\)\\.\\([0-9]+\\)\\.\\([0-9]+\\)--version-\\([0-9]+\\)" 4)
95 (full "^\\w+: v\\([0-9]+\\)\\.\\([0-9]+\\)\\.\\([0-9]+\\)" 5)
97 "List of decoders for version strings.
98 Each decoder is of the form:
100 ( RELEASE-TYPE REGEXP MAX )
102 RELEASE-TYPE is a symbol specifying something like `beta' or `alpha'.
103 REGEXP is the regular expression to match a version string.
104 MAX is the maximum number of match-numbers in the release number.
105 Decoders must be ordered to decode least stable versions before the
106 more stable ones.")
108 ;;; Version Checking
110 (defun inversion-decode-version (version-string)
111 "Decode VERSION-STRING into an encoded list.
112 Return value is of the form:
113 (RELEASE MAJOR MINOR ...)
114 where RELEASE is a symbol such as `full', or `beta'."
115 (let ((decoders inversion-decoders)
116 (result nil))
117 (while (and decoders (not result))
118 (if (string-match (nth 1 (car decoders)) version-string)
119 (let ((ver nil)
120 (num-left (nth 2 (car decoders)))
121 (count 1))
122 (while (<= count num-left)
123 (setq ver (cons
124 (if (match-beginning count)
125 (string-to-number
126 (substring version-string
127 (match-beginning count)
128 (match-end count)))
130 ver)
131 count (1+ count)))
132 (setq result (cons (caar decoders) (nreverse ver))))
133 (setq decoders (cdr decoders))))
134 result))
136 (defun inversion-package-version (package)
137 "Return the decoded version for PACKAGE."
138 (let ((ver (symbol-value
139 (intern-soft
140 (concat (symbol-name package)
141 "-version"))))
142 (code nil))
143 (unless ver
144 (error "Package %S does not define %S-version" package package))
145 ;; Decode the code
146 (setq code (inversion-decode-version ver))
147 (unless code
148 (error "%S-version value (%s) cannot be decoded" package ver))
149 code))
151 (defun inversion-package-incompatibility-version (package)
152 "Return the decoded incompatibility version for PACKAGE.
153 The incompatibility version is specified by the programmer of
154 a package when a package is not backward compatible. It is
155 not an indication of new features or bug fixes."
156 (let ((ver (symbol-value
157 (intern-soft
158 (concat (symbol-name package)
159 "-incompatible-version")))))
160 (if (not ver)
162 ;; Decode the code
163 (inversion-decode-version ver))))
165 (defun inversion-recode (code)
166 "Convert CODE into a string."
167 (let ((r (nth 0 code)) ; release-type
168 (n (nth 1 code)) ; main number
169 (i (nth 2 code)) ; first increment
170 (p (nth 3 code))) ; second increment
171 (cond
172 ((eq r 'full)
173 (setq r "" p ""))
174 ((eq r 'point)
175 (setq r ".")))
176 (format "%s.%s%s%s" n i r p)))
178 (defun inversion-release-to-number (release-symbol)
179 "Convert RELEASE-SYMBOL into a number."
180 (let* ((ra (assoc release-symbol inversion-decoders))
181 (rn (- (length inversion-decoders)
182 (length (member ra inversion-decoders)))))
183 rn))
185 (defun inversion-= (ver1 ver2)
186 "Return non-nil if VER1 is equal to VER2."
187 (equal ver1 ver2))
189 (defun inversion-< (ver1 ver2)
190 "Return non-nil if VER1 is less than VER2."
191 (let ((v1-0 (inversion-release-to-number (nth 0 ver1)))
192 (v1-1 (nth 1 ver1))
193 (v1-2 (nth 2 ver1))
194 (v1-3 (nth 3 ver1))
195 (v1-4 (nth 4 ver1))
196 ;; v2
197 (v2-0 (inversion-release-to-number (nth 0 ver2)))
198 (v2-1 (nth 1 ver2))
199 (v2-2 (nth 2 ver2))
200 (v2-3 (nth 3 ver2))
201 (v2-4 (nth 4 ver2))
204 (cond ((and (equal (list v1-1 v1-2 v1-3 v1-4)
205 (list v2-1 v2-2 v2-3 v2-4))
206 v1-0 v2-0)
207 (< v1-0 v2-0))
208 ((and (equal v1-1 v2-1)
209 (equal v1-2 v2-2)
210 (equal v1-3 v2-3)
211 v1-4 v2-4) ; all or nothing if elt - is =
212 (< v1-4 v2-4))
213 ((and (equal v1-1 v2-1)
214 (equal v1-2 v2-2)
215 v1-3 v2-3) ; all or nothing if elt - is =
216 (< v1-3 v2-3))
217 ((and (equal v1-1 v2-1)
218 v1-2 v2-2)
219 (< v1-2 v2-2))
220 ((and v1-1 v2-1)
221 (< v1-1 v2-1))
224 (defun inversion-check-version (version incompatible-version
225 minimum &rest reserved)
226 "Check that a given version meets the minimum requirement.
227 VERSION, INCOMPATIBLE-VERSION and MINIMUM are of similar format to
228 return entries of `inversion-decode-version', or a classic version
229 string. INCOMPATIBLE-VERSION can be nil.
230 RESERVED arguments are kept for a later use.
231 Return:
232 - nil if everything is ok.
233 - `outdated' if VERSION is less than MINIMUM.
234 - `incompatible' if VERSION is not backward compatible with MINIMUM.
235 - t if the check failed."
236 (let ((code (if (stringp version)
237 (inversion-decode-version version)
238 version))
239 (req (if (stringp minimum)
240 (inversion-decode-version minimum)
241 minimum))
243 ;; Perform a test.
244 (cond
245 ((inversion-= code req)
246 ;; Same version.. Yay!
247 nil)
248 ((inversion-< code req)
249 ;; Version is too old!
250 'outdated)
251 ((inversion-< req code)
252 ;; Newer is installed. What to do?
253 (let ((incompatible
254 (if (stringp incompatible-version)
255 (inversion-decode-version incompatible-version)
256 incompatible-version)))
257 (cond
258 ((not incompatible) nil)
259 ((or (inversion-= req incompatible)
260 (inversion-< req incompatible))
261 ;; The requested version is = or < than what the package
262 ;; maintainer says is incompatible.
263 'incompatible)
264 ;; Things are ok.
265 (t nil))))
266 ;; Check failed
267 (t t))))
269 (defun inversion-test (package minimum &rest reserved)
270 "Test that PACKAGE meets the MINIMUM version requirement.
271 PACKAGE is a symbol, similar to what is passed to `require'.
272 MINIMUM is of similar format to return entries of
273 `inversion-decode-version', or a classic version string.
274 RESERVED arguments are kept for a later user.
275 This depends on the symbols `PACKAGE-version' and optionally
276 `PACKAGE-incompatible-version' being defined in PACKAGE.
277 Return nil if everything is ok. Return an error string otherwise."
278 (let ((check (inversion-check-version
279 (inversion-package-version package)
280 (inversion-package-incompatibility-version package)
281 minimum reserved)))
282 (cond
283 ((null check)
284 ;; Same version.. Yay!
285 nil)
286 ((eq check 'outdated)
287 ;; Version is too old!
288 (format "You need to upgrade package %s to %s" package minimum))
289 ((eq check 'incompatible)
290 ;; Newer is installed but the requested version is = or < than
291 ;; what the package maintainer says is incompatible, then throw
292 ;; that error.
293 (format "Package %s version is not backward compatible with %s"
294 package minimum))
295 ;; Check failed
296 (t "Inversion version check failed."))))
298 (defun inversion-reverse-test (package oldversion &rest reserved)
299 "Test that PACKAGE at OLDVERSION is still compatible.
300 If something like a save file is loaded at OLDVERSION, this
301 test will identify if OLDVERSION is compatible with the current version
302 of PACKAGE.
303 PACKAGE is a symbol, similar to what is passed to `require'.
304 OLDVERSION is of similar format to return entries of
305 `inversion-decode-version', or a classic version string.
306 RESERVED arguments are kept for a later user.
307 This depends on the symbols `PACKAGE-version' and optionally
308 `PACKAGE-incompatible-version' being defined in PACKAGE.
309 Return nil if everything is ok. Return an error string otherwise."
310 (let ((check (inversion-check-version
311 (inversion-package-version package)
312 (inversion-package-incompatibility-version package)
313 oldversion reserved)))
314 (cond
315 ((null check)
316 ;; Same version.. Yay!
317 nil)
318 ((eq check 'outdated)
319 ;; Version is too old!
320 (format "Package %s version %s is not compatible with current version"
321 package oldversion))
322 ((eq check 'incompatible)
323 ;; Newer is installed but the requested version is = or < than
324 ;; what the package maintainer says is incompatible, then throw
325 ;; that error.
326 (format "Package %s version is not backward compatible with %s"
327 package oldversion))
328 ;; Check failed
329 (t "Inversion version check failed."))))
331 (defun inversion-require (package version &optional file directory
332 &rest reserved)
333 "Declare that you need PACKAGE with at least VERSION.
334 PACKAGE might be found in FILE. (See `require'.)
335 Throws an error if VERSION is incompatible with what is installed.
336 Optional argument DIRECTORY is a location where new versions of
337 this tool can be located. If there is a versioning problem and
338 DIRECTORY is provided, inversion will offer to download the file.
339 Optional argument RESERVED is saved for later use."
340 (require package file)
341 (let ((err (inversion-test package version)))
342 (when err
343 (if directory
344 (inversion-download-package-ask err package directory version)
345 (error err)))
346 ;; Return the package symbol that was required.
347 package))
349 ;;;###autoload
350 (defun inversion-require-emacs (emacs-ver xemacs-ver sxemacs-ver)
351 "Declare that you need either EMACS-VER, XEMACS-VER or SXEMACS-ver.
352 Only checks one based on which kind of Emacs is being run."
353 (let ((err (inversion-test 'emacs
354 (cond ((featurep 'sxemacs)
355 sxemacs-ver)
356 ((featurep 'xemacs)
357 xemacs-ver)
359 emacs-ver)))))
360 (if err (error err)
361 ;; Something nice...
362 t)))
364 (defconst inversion-find-data
365 '("(def\\(var\\|const\\)\\s-+%s-%s\\s-+\"\\([^\"]+\\)" 2)
366 "Regexp template and match data index of a version string.")
368 (defun inversion-find-version (package)
369 "Search for the version and incompatible version of PACKAGE.
370 Does not load PACKAGE nor requires that it has been previously loaded.
371 Search in the directories in `load-path' for a PACKAGE.el library.
372 Visit the file found and search for the declarations of variables or
373 constants `PACKAGE-version' and `PACKAGE-incompatible-version'. The
374 value of these variables must be a version string.
376 Return a pair (VERSION-STRING . INCOMPATIBLE-VERSION-STRING) where
377 INCOMPATIBLE-VERSION-STRING can be nil.
378 Return nil when VERSION-STRING was not found."
379 (let* ((file (locate-library (format "%s.el" package) t))
380 (tag (car inversion-find-data))
381 (idx (nth 1 inversion-find-data))
382 version)
383 (when file
384 (with-temp-buffer
385 ;; The 3000 is a bit arbitrary, but should cut down on
386 ;; fileio as version info usually is at the very top
387 ;; of a file. After a long commentary could be bad.
388 (insert-file-contents-literally file nil 0 3000)
389 (goto-char (point-min))
390 (when (re-search-forward (format tag package 'version) nil t)
391 (setq version (list (match-string idx)))
392 (goto-char (point-min))
393 (when (re-search-forward
394 (format tag package 'incompatible-version) nil t)
395 (setcdr version (match-string idx))))))
396 version))
398 (defun inversion-add-to-load-path (package minimum
399 &optional installdir
400 &rest subdirs)
401 "Add the PACKAGE path to `load-path' if necessary.
402 MINIMUM is the minimum version requirement of PACKAGE.
403 Optional argument INSTALLDIR is the base directory where PACKAGE is
404 installed. It defaults to `default-directory'/PACKAGE.
405 SUBDIRS are sub-directories to add to `load-path', following the main
406 INSTALLDIR path."
407 (let ((ver (inversion-find-version package)))
408 ;; If PACKAGE not found or a bad version already in `load-path',
409 ;; prepend the new PACKAGE path, so it will be loaded first.
410 (when (or (not ver)
411 (and
412 (inversion-check-version (car ver) (cdr ver) minimum)
413 (message "Outdated %s %s shadowed to meet minimum version %s"
414 package (car ver) minimum)
416 (let* ((default-directory
417 (or installdir
418 (expand-file-name (format "./%s" package))))
419 subdir)
420 (when (file-directory-p default-directory)
421 ;; Add SUBDIRS
422 (while subdirs
423 (setq subdir (expand-file-name (car subdirs))
424 subdirs (cdr subdirs))
425 (when (file-directory-p subdir)
426 ;;(message "%S added to `load-path'" subdir)
427 (add-to-list 'load-path subdir)))
428 ;; Add the main path
429 ;;(message "%S added to `load-path'" default-directory)
430 (add-to-list 'load-path default-directory))
431 ;; We get to this point iff we do not accept or there is no
432 ;; system file. Let's check the version of what we just
433 ;; installed... just to be safe.
434 (let ((newver (inversion-find-version package)))
435 (if (not newver)
436 (error "Failed to find version for newly installed %s"
437 package))
438 (if (inversion-check-version (car newver) (cdr newver) minimum)
439 (error "Outdated %s %s just installed" package (car newver)))
440 )))))
442 ;;; URL and downloading code
444 (defun inversion-locate-package-files (package directory &optional version)
445 "Get a list of distributions of PACKAGE from DIRECTORY.
446 DIRECTORY can be an ange-ftp compatible filename, such as:
447 \"/ftp@ftp1.sourceforge.net/pub/sourceforge/PACKAGE\"
448 If it is a URL, wget will be used for download.
449 Optional argument VERSION will restrict the list of available versions
450 to the file matching VERSION exactly, or nil."
451 ;;DIRECTORY should also allow a URL:
452 ;; \"http://ftp1.sourceforge.net/PACKAGE\"
453 ;; but then I can get file listings easily.
454 (if (symbolp package) (setq package (symbol-name package)))
455 (directory-files directory t
456 (if version
457 (concat "^" package "-" version "\\>")
458 package)))
460 (defvar inversion-package-common-tails '( ".tar.gz"
461 ".tar"
462 ".zip"
463 ".gz"
465 "Common distribution mechanisms for Emacs Lisp packages.")
467 (defun inversion-locate-package-files-and-split (package directory &optional version)
468 "Use `inversion-locate-package-files' to get a list of PACKAGE files.
469 DIRECTORY is the location where distributions of PACKAGE are.
470 VERSION is an optional argument specifying a version to restrict to.
471 The return list is an alist with the version string in the CAR,
472 and the full path name in the CDR."
473 (if (symbolp package) (setq package (symbol-name package)))
474 (let ((f (inversion-locate-package-files package directory version))
475 (out nil))
476 (while f
477 (let* ((file (car f))
478 (dist (file-name-nondirectory file))
479 (tails inversion-package-common-tails)
480 (verstring nil))
481 (while (and tails (not verstring))
482 (when (string-match (concat (car tails) "$") dist)
483 (setq verstring
484 (substring dist (1+ (length package)) (match-beginning 0))))
485 (setq tails (cdr tails)))
486 (if (not verstring)
487 (error "Cannot decode version for %s" dist))
488 (setq out
489 (cons
490 (cons verstring file)
491 out))
492 (setq f (cdr f))))
493 out))
495 (defun inversion-download-package-ask (err package directory version)
496 "Due to ERR, offer to download PACKAGE from DIRECTORY.
497 The package should have VERSION available for download."
498 (if (symbolp package) (setq package (symbol-name package)))
499 (let ((files (inversion-locate-package-files-and-split
500 package directory version)))
501 (if (not files)
502 (error err)
503 (if (not (y-or-n-p (concat err ": Download update? ")))
504 (error err)
505 (let ((dest (read-directory-name (format "Download %s to: "
506 package)
507 t)))
508 (if (> (length files) 1)
509 (setq files
510 (list
511 "foo" ;; ignored
512 (read-file-name "Version to download: "
513 directory
514 files
516 (concat
517 (file-name-as-directory directory)
518 package)
519 nil))))
521 (copy-file (cdr (car files)) dest))))))
523 ;;; How we upgrade packages in Emacs has yet to be ironed out.
525 ;; (defun inversion-upgrade-package (package &optional directory)
526 ;; "Try to upgrade PACKAGE in DIRECTORY is available."
527 ;; (interactive "sPackage to upgrade: ")
528 ;; (if (stringp package) (setq package (intern package)))
529 ;; (if (not directory)
530 ;; ;; Hope that the package maintainer specified.
531 ;; (setq directory (symbol-value (or (intern-soft
532 ;; (concat (symbol-name package)
533 ;; "-url"))
534 ;; (intern-soft
535 ;; (concat (symbol-name package)
536 ;; "-directory"))))))
537 ;; (let ((files (inversion-locate-package-files-and-split
538 ;; package directory))
539 ;; (cver (inversion-package-version package))
540 ;; (newer nil))
541 ;; (mapc (lambda (f)
542 ;; (if (inversion-< cver (inversion-decode-version (car f)))
543 ;; (setq newer (cons f newer))))
544 ;; files)
545 ;; newer
546 ;; ))
548 (provide 'inversion)
550 ;;; inversion.el ends here