613
|
1 ;;; cl-extra.el --- Common Lisp extensions for XEmacs Lisp (part two)
|
428
|
2
|
2153
|
3 ;; Copyright (C) 1993,2000,2003 Free Software Foundation, Inc.
|
801
|
4 ;; Copyright (C) 2002 Ben Wing.
|
428
|
5
|
|
6 ;; Author: Dave Gillespie <daveg@synaptics.com>
|
|
7 ;; Maintainer: XEmacs Development Team
|
|
8 ;; Version: 2.02
|
|
9 ;; Keywords: extensions, dumped
|
|
10
|
|
11 ;; This file is part of XEmacs.
|
|
12
|
|
13 ;; XEmacs is free software; you can redistribute it and/or modify it
|
|
14 ;; under the terms of the GNU General Public License as published by
|
|
15 ;; the Free Software Foundation; either version 2, or (at your option)
|
|
16 ;; any later version.
|
|
17
|
|
18 ;; XEmacs is distributed in the hope that it will be useful, but
|
|
19 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
21 ;; General Public License for more details.
|
|
22
|
|
23 ;; You should have received a copy of the GNU General Public License
|
|
24 ;; along with XEmacs; see the file COPYING. If not, write to the Free
|
|
25 ;; Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
|
|
26 ;; 02111-1307, USA.
|
|
27
|
2153
|
28 ;;; Synched up with: FSF 21.3.
|
428
|
29
|
|
30 ;;; Commentary:
|
|
31
|
|
32 ;; This file is dumped with XEmacs.
|
|
33
|
|
34 ;; These are extensions to Emacs Lisp that provide a degree of
|
|
35 ;; Common Lisp compatibility, beyond what is already built-in
|
|
36 ;; in Emacs Lisp.
|
|
37 ;;
|
|
38 ;; This package was written by Dave Gillespie; it is a complete
|
|
39 ;; rewrite of Cesar Quiroz's original cl.el package of December 1986.
|
|
40 ;;
|
|
41 ;; Bug reports, comments, and suggestions are welcome!
|
|
42
|
|
43 ;; This file contains portions of the Common Lisp extensions
|
|
44 ;; package which are autoloaded since they are relatively obscure.
|
|
45
|
|
46 ;; See cl.el for Change Log.
|
|
47
|
|
48
|
|
49 ;;; Code:
|
2153
|
50 ;; XEmacs addition
|
428
|
51 (eval-when-compile
|
|
52 (require 'obsolete))
|
|
53
|
|
54 (or (memq 'cl-19 features)
|
|
55 (error "Tried to load `cl-extra' before `cl'!"))
|
|
56
|
|
57
|
|
58 ;;; Type coercion.
|
|
59
|
|
60 (defun coerce (x type)
|
|
61 "Coerce OBJECT to type TYPE.
|
|
62 TYPE is a Common Lisp type specifier."
|
|
63 (cond ((eq type 'list) (if (listp x) x (append x nil)))
|
|
64 ((eq type 'vector) (if (vectorp x) x (vconcat x)))
|
|
65 ((eq type 'string) (if (stringp x) x (concat x)))
|
|
66 ((eq type 'array) (if (arrayp x) x (vconcat x)))
|
|
67 ((and (eq type 'character) (stringp x) (= (length x) 1)) (aref x 0))
|
|
68 ((and (eq type 'character) (symbolp x)) (coerce (symbol-name x) type))
|
2153
|
69 ;; XEmacs addition character <-> integer coercions
|
446
|
70 ((and (eq type 'character) (char-int-p x)) (int-char x))
|
|
71 ((and (eq type 'integer) (characterp x)) (char-int x))
|
428
|
72 ((eq type 'float) (float x))
|
2153
|
73 ;; XEmacs addition: enhanced numeric type coercions
|
2367
|
74 ((and-fboundp 'coerce-number
|
|
75 (memq type '(integer ratio bigfloat))
|
|
76 (coerce-number x type)))
|
2153
|
77 ;; XEmacs addition: bit-vector coercion
|
428
|
78 ((eq type 'bit-vector) (if (bit-vector-p x) x
|
|
79 (apply 'bit-vector (append x nil))))
|
2153
|
80 ;; XEmacs addition: weak-list coercion
|
428
|
81 ((eq type 'weak-list)
|
|
82 (if (weak-list-p x) x
|
|
83 (let ((wl (make-weak-list)))
|
|
84 (set-weak-list-list wl (if (listp x) x (append x nil)))
|
|
85 wl)))
|
|
86 ((typep x type) x)
|
|
87 (t (error "Can't coerce %s to type %s" x type))))
|
|
88
|
|
89
|
|
90 ;;; Predicates.
|
|
91
|
|
92 (defun equalp (x y)
|
|
93 "Return t if two Lisp objects have similar structures and contents.
|
|
94 This is like `equal', except that it accepts numerically equal
|
|
95 numbers of different types (float vs. integer), and also compares
|
|
96 strings case-insensitively."
|
|
97 (cond ((eq x y) t)
|
|
98 ((stringp x)
|
2153
|
99 ;; XEmacs change: avoid downcase
|
801
|
100 (eq t (compare-strings x nil nil y nil nil t)))
|
2153
|
101 ;; XEmacs addition: compare characters
|
428
|
102 ((characterp x)
|
|
103 (and (characterp y)
|
|
104 (or (char-equal x y)
|
|
105 (char-equal (downcase x) (downcase y)))))
|
|
106 ((numberp x)
|
|
107 (and (numberp y) (= x y)))
|
|
108 ((consp x)
|
|
109 (while (and (consp x) (consp y) (equalp (car x) (car y)))
|
2153
|
110 (setq x (cdr x) y (cdr y)))
|
428
|
111 (and (not (consp x)) (equalp x y)))
|
|
112 ((vectorp x)
|
|
113 (and (vectorp y) (= (length x) (length y))
|
|
114 (let ((i (length x)))
|
|
115 (while (and (>= (setq i (1- i)) 0)
|
|
116 (equalp (aref x i) (aref y i))))
|
|
117 (< i 0))))
|
|
118 (t (equal x y))))
|
|
119
|
|
120
|
|
121 ;;; Control structures.
|
|
122
|
|
123 (defun cl-mapcar-many (cl-func cl-seqs)
|
|
124 (if (cdr (cdr cl-seqs))
|
|
125 (let* ((cl-res nil)
|
|
126 (cl-n (apply 'min (mapcar 'length cl-seqs)))
|
|
127 (cl-i 0)
|
|
128 (cl-args (copy-sequence cl-seqs))
|
|
129 cl-p1 cl-p2)
|
|
130 (setq cl-seqs (copy-sequence cl-seqs))
|
|
131 (while (< cl-i cl-n)
|
|
132 (setq cl-p1 cl-seqs cl-p2 cl-args)
|
|
133 (while cl-p1
|
|
134 (setcar cl-p2
|
|
135 (if (consp (car cl-p1))
|
|
136 (prog1 (car (car cl-p1))
|
|
137 (setcar cl-p1 (cdr (car cl-p1))))
|
|
138 (aref (car cl-p1) cl-i)))
|
|
139 (setq cl-p1 (cdr cl-p1) cl-p2 (cdr cl-p2)))
|
2153
|
140 (push (apply cl-func cl-args) cl-res)
|
428
|
141 (setq cl-i (1+ cl-i)))
|
|
142 (nreverse cl-res))
|
|
143 (let ((cl-res nil)
|
|
144 (cl-x (car cl-seqs))
|
|
145 (cl-y (nth 1 cl-seqs)))
|
|
146 (let ((cl-n (min (length cl-x) (length cl-y)))
|
|
147 (cl-i -1))
|
|
148 (while (< (setq cl-i (1+ cl-i)) cl-n)
|
2153
|
149 (push (funcall cl-func
|
|
150 (if (consp cl-x) (pop cl-x) (aref cl-x cl-i))
|
|
151 (if (consp cl-y) (pop cl-y) (aref cl-y cl-i)))
|
428
|
152 cl-res)))
|
|
153 (nreverse cl-res))))
|
|
154
|
|
155 (defun map (cl-type cl-func cl-seq &rest cl-rest)
|
|
156 "Map a function across one or more sequences, returning a sequence.
|
|
157 TYPE is the sequence type to return, FUNC is the function, and SEQS
|
|
158 are the argument sequences."
|
|
159 (let ((cl-res (apply 'mapcar* cl-func cl-seq cl-rest)))
|
|
160 (and cl-type (coerce cl-res cl-type))))
|
|
161
|
|
162 (defun maplist (cl-func cl-list &rest cl-rest)
|
|
163 "Map FUNC to each sublist of LIST or LISTS.
|
|
164 Like `mapcar', except applies to lists and their cdr's rather than to
|
|
165 the elements themselves."
|
|
166 (if cl-rest
|
|
167 (let ((cl-res nil)
|
|
168 (cl-args (cons cl-list (copy-sequence cl-rest)))
|
|
169 cl-p)
|
|
170 (while (not (memq nil cl-args))
|
2153
|
171 (push (apply cl-func cl-args) cl-res)
|
428
|
172 (setq cl-p cl-args)
|
2153
|
173 (while cl-p (setcar cl-p (cdr (pop cl-p)) )))
|
428
|
174 (nreverse cl-res))
|
|
175 (let ((cl-res nil))
|
|
176 (while cl-list
|
2153
|
177 (push (funcall cl-func cl-list) cl-res)
|
428
|
178 (setq cl-list (cdr cl-list)))
|
|
179 (nreverse cl-res))))
|
|
180
|
2153
|
181 ;; XEmacs change: in Emacs, this function is named cl-mapc.
|
428
|
182 (defun mapc (cl-func cl-seq &rest cl-rest)
|
|
183 "Like `mapcar', but does not accumulate values returned by the function."
|
|
184 (if cl-rest
|
|
185 (apply 'map nil cl-func cl-seq cl-rest)
|
|
186 ;; XEmacs change: in the simplest case we call mapc-internal,
|
|
187 ;; which really doesn't accumulate any results.
|
|
188 (mapc-internal cl-func cl-seq))
|
|
189 cl-seq)
|
|
190
|
2153
|
191 ;; XEmacs addition: FSF compatibility
|
|
192 (defalias 'cl-mapc 'mapc)
|
|
193
|
428
|
194 (defun mapl (cl-func cl-list &rest cl-rest)
|
|
195 "Like `maplist', but does not accumulate values returned by the function."
|
|
196 (if cl-rest
|
|
197 (apply 'maplist cl-func cl-list cl-rest)
|
|
198 (let ((cl-p cl-list))
|
|
199 (while cl-p (funcall cl-func cl-p) (setq cl-p (cdr cl-p)))))
|
|
200 cl-list)
|
|
201
|
|
202 (defun mapcan (cl-func cl-seq &rest cl-rest)
|
|
203 "Like `mapcar', but nconc's together the values returned by the function."
|
|
204 (apply 'nconc (apply 'mapcar* cl-func cl-seq cl-rest)))
|
|
205
|
|
206 (defun mapcon (cl-func cl-list &rest cl-rest)
|
|
207 "Like `maplist', but nconc's together the values returned by the function."
|
|
208 (apply 'nconc (apply 'maplist cl-func cl-list cl-rest)))
|
|
209
|
|
210 (defun some (cl-pred cl-seq &rest cl-rest)
|
|
211 "Return true if PREDICATE is true of any element of SEQ or SEQs.
|
|
212 If so, return the true (non-nil) value returned by PREDICATE."
|
|
213 (if (or cl-rest (nlistp cl-seq))
|
|
214 (catch 'cl-some
|
|
215 (apply 'map nil
|
|
216 (function (lambda (&rest cl-x)
|
|
217 (let ((cl-res (apply cl-pred cl-x)))
|
|
218 (if cl-res (throw 'cl-some cl-res)))))
|
|
219 cl-seq cl-rest) nil)
|
|
220 (let ((cl-x nil))
|
2153
|
221 (while (and cl-seq (not (setq cl-x (funcall cl-pred (pop cl-seq))))))
|
428
|
222 cl-x)))
|
|
223
|
|
224 (defun every (cl-pred cl-seq &rest cl-rest)
|
|
225 "Return true if PREDICATE is true of every element of SEQ or SEQs."
|
|
226 (if (or cl-rest (nlistp cl-seq))
|
|
227 (catch 'cl-every
|
|
228 (apply 'map nil
|
|
229 (function (lambda (&rest cl-x)
|
|
230 (or (apply cl-pred cl-x) (throw 'cl-every nil))))
|
|
231 cl-seq cl-rest) t)
|
|
232 (while (and cl-seq (funcall cl-pred (car cl-seq)))
|
|
233 (setq cl-seq (cdr cl-seq)))
|
|
234 (null cl-seq)))
|
|
235
|
|
236 (defun notany (cl-pred cl-seq &rest cl-rest)
|
|
237 "Return true if PREDICATE is false of every element of SEQ or SEQs."
|
|
238 (not (apply 'some cl-pred cl-seq cl-rest)))
|
|
239
|
|
240 (defun notevery (cl-pred cl-seq &rest cl-rest)
|
|
241 "Return true if PREDICATE is false of some element of SEQ or SEQs."
|
|
242 (not (apply 'every cl-pred cl-seq cl-rest)))
|
|
243
|
|
244 ;;; Support for `loop'.
|
2153
|
245 (defalias 'cl-map-keymap 'map-keymap)
|
428
|
246
|
|
247 (defun cl-map-keymap-recursively (cl-func-rec cl-map &optional cl-base)
|
|
248 (or cl-base
|
2153
|
249 (setq cl-base (copy-sequence [0])))
|
|
250 (map-keymap
|
428
|
251 (function
|
|
252 (lambda (cl-key cl-bind)
|
|
253 (aset cl-base (1- (length cl-base)) cl-key)
|
|
254 (if (keymapp cl-bind)
|
|
255 (cl-map-keymap-recursively
|
|
256 cl-func-rec cl-bind
|
2153
|
257 (vconcat cl-base (list 0)))
|
428
|
258 (funcall cl-func-rec cl-base cl-bind))))
|
|
259 cl-map))
|
|
260
|
|
261 (defun cl-map-intervals (cl-func &optional cl-what cl-prop cl-start cl-end)
|
|
262 (or cl-what (setq cl-what (current-buffer)))
|
|
263 (if (bufferp cl-what)
|
|
264 (let (cl-mark cl-mark2 (cl-next t) cl-next2)
|
2153
|
265 (with-current-buffer cl-what
|
428
|
266 (setq cl-mark (copy-marker (or cl-start (point-min))))
|
|
267 (setq cl-mark2 (and cl-end (copy-marker cl-end))))
|
|
268 (while (and cl-next (or (not cl-mark2) (< cl-mark cl-mark2)))
|
2153
|
269 (setq cl-next (if cl-prop (next-single-property-change
|
|
270 cl-mark cl-prop cl-what)
|
|
271 (next-property-change cl-mark cl-what))
|
|
272 cl-next2 (or cl-next (with-current-buffer cl-what
|
|
273 (point-max))))
|
428
|
274 (funcall cl-func (prog1 (marker-position cl-mark)
|
|
275 (set-marker cl-mark cl-next2))
|
|
276 (if cl-mark2 (min cl-next2 cl-mark2) cl-next2)))
|
|
277 (set-marker cl-mark nil) (if cl-mark2 (set-marker cl-mark2 nil)))
|
|
278 (or cl-start (setq cl-start 0))
|
|
279 (or cl-end (setq cl-end (length cl-what)))
|
|
280 (while (< cl-start cl-end)
|
2153
|
281 (let ((cl-next (or (if cl-prop (next-single-property-change
|
|
282 cl-start cl-prop cl-what)
|
|
283 (next-property-change cl-start cl-what))
|
428
|
284 cl-end)))
|
|
285 (funcall cl-func cl-start (min cl-next cl-end))
|
|
286 (setq cl-start cl-next)))))
|
|
287
|
|
288 (defun cl-map-overlays (cl-func &optional cl-buffer cl-start cl-end cl-arg)
|
|
289 (or cl-buffer (setq cl-buffer (current-buffer)))
|
502
|
290 (with-fboundp '(overlay-start overlay-end overlays-at next-overlay-change)
|
|
291 (if-fboundp 'overlay-lists
|
428
|
292
|
502
|
293 ;; This is the preferred algorithm, though overlay-lists is
|
|
294 ;; undocumented.
|
|
295 (let (cl-ovl)
|
2153
|
296 (with-current-buffer cl-buffer
|
502
|
297 (setq cl-ovl (overlay-lists))
|
|
298 (if cl-start (setq cl-start (copy-marker cl-start)))
|
|
299 (if cl-end (setq cl-end (copy-marker cl-end))))
|
|
300 (setq cl-ovl (nconc (car cl-ovl) (cdr cl-ovl)))
|
|
301 (while (and cl-ovl
|
|
302 (or (not (overlay-start (car cl-ovl)))
|
|
303 (and cl-end (>= (overlay-start (car cl-ovl)) cl-end))
|
|
304 (and cl-start (<= (overlay-end (car cl-ovl))
|
|
305 cl-start))
|
|
306 (not (funcall cl-func (car cl-ovl) cl-arg))))
|
|
307 (setq cl-ovl (cdr cl-ovl)))
|
|
308 (if cl-start (set-marker cl-start nil))
|
|
309 (if cl-end (set-marker cl-end nil)))
|
|
310
|
|
311 ;; This alternate algorithm fails to find zero-length overlays.
|
2153
|
312 (let ((cl-mark (with-current-buffer cl-buffer
|
|
313 (copy-marker (or cl-start (point-min)))))
|
|
314 (cl-mark2 (and cl-end (with-current-buffer cl-buffer
|
|
315 (copy-marker cl-end))))
|
502
|
316 cl-pos cl-ovl)
|
|
317 (while (save-excursion
|
|
318 (and (setq cl-pos (marker-position cl-mark))
|
|
319 (< cl-pos (or cl-mark2 (point-max)))
|
|
320 (progn
|
|
321 (set-buffer cl-buffer)
|
|
322 (setq cl-ovl (overlays-at cl-pos))
|
|
323 (set-marker cl-mark (next-overlay-change cl-pos)))))
|
|
324 (while (and cl-ovl
|
|
325 (or (/= (overlay-start (car cl-ovl)) cl-pos)
|
|
326 (not (and (funcall cl-func (car cl-ovl) cl-arg)
|
|
327 (set-marker cl-mark nil)))))
|
|
328 (setq cl-ovl (cdr cl-ovl))))
|
|
329 (set-marker cl-mark nil) (if cl-mark2 (set-marker cl-mark2 nil))))))
|
428
|
330
|
|
331 ;;; Support for `setf'.
|
|
332 (defun cl-set-frame-visible-p (frame val)
|
|
333 (cond ((null val) (make-frame-invisible frame))
|
|
334 ((eq val 'icon) (iconify-frame frame))
|
|
335 (t (make-frame-visible frame)))
|
|
336 val)
|
|
337
|
|
338 ;;; Support for `progv'.
|
|
339 (defvar cl-progv-save)
|
|
340 (defun cl-progv-before (syms values)
|
|
341 (while syms
|
2153
|
342 (push (if (boundp (car syms))
|
428
|
343 (cons (car syms) (symbol-value (car syms)))
|
|
344 (car syms)) cl-progv-save)
|
|
345 (if values
|
2153
|
346 (set (pop syms) (pop values))
|
|
347 (makunbound (pop syms)))))
|
428
|
348
|
|
349 (defun cl-progv-after ()
|
|
350 (while cl-progv-save
|
|
351 (if (consp (car cl-progv-save))
|
|
352 (set (car (car cl-progv-save)) (cdr (car cl-progv-save)))
|
|
353 (makunbound (car cl-progv-save)))
|
2153
|
354 (pop cl-progv-save)))
|
428
|
355
|
|
356
|
|
357 ;;; Numbers.
|
|
358
|
|
359 (defun gcd (&rest args)
|
|
360 "Return the greatest common divisor of the arguments."
|
2153
|
361 (let ((a (abs (or (pop args) 0))))
|
428
|
362 (while args
|
2153
|
363 (let ((b (abs (pop args))))
|
428
|
364 (while (> b 0) (setq b (% a (setq a b))))))
|
|
365 a))
|
|
366
|
|
367 (defun lcm (&rest args)
|
|
368 "Return the least common multiple of the arguments."
|
|
369 (if (memq 0 args)
|
|
370 0
|
2153
|
371 (let ((a (abs (or (pop args) 1))))
|
428
|
372 (while args
|
2153
|
373 (let ((b (abs (pop args))))
|
428
|
374 (setq a (* (/ a (gcd a b)) b))))
|
|
375 a)))
|
|
376
|
|
377 (defun isqrt (a)
|
|
378 "Return the integer square root of the argument."
|
|
379 (if (and (integerp a) (> a 0))
|
|
380 ;; XEmacs change
|
|
381 (let ((g (cond ((>= a 1000000) 10000) ((>= a 10000) 1000)
|
|
382 ((>= a 100) 100) (t 10)))
|
|
383 g2)
|
|
384 (while (< (setq g2 (/ (+ g (/ a g)) 2)) g)
|
|
385 (setq g g2))
|
|
386 g)
|
|
387 (if (eq a 0) 0 (signal 'arith-error nil))))
|
|
388
|
2153
|
389 ;; XEmacs addition
|
428
|
390 (defun cl-expt (x y)
|
|
391 "Return X raised to the power of Y. Works only for integer arguments."
|
|
392 (if (<= y 0) (if (= y 0) 1 (if (memq x '(-1 1)) (cl-expt x (- y)) 0))
|
|
393 (* (if (= (% y 2) 0) 1 x) (cl-expt (* x x) (/ y 2)))))
|
|
394 (or (and (fboundp 'expt) (subrp (symbol-function 'expt)))
|
|
395 (defalias 'expt 'cl-expt))
|
|
396
|
|
397 (defun floor* (x &optional y)
|
|
398 "Return a list of the floor of X and the fractional part of X.
|
|
399 With two arguments, return floor and remainder of their quotient."
|
|
400 (let ((q (floor x y)))
|
|
401 (list q (- x (if y (* y q) q)))))
|
|
402
|
|
403 (defun ceiling* (x &optional y)
|
|
404 "Return a list of the ceiling of X and the fractional part of X.
|
|
405 With two arguments, return ceiling and remainder of their quotient."
|
|
406 (let ((res (floor* x y)))
|
|
407 (if (= (car (cdr res)) 0) res
|
|
408 (list (1+ (car res)) (- (car (cdr res)) (or y 1))))))
|
|
409
|
|
410 (defun truncate* (x &optional y)
|
|
411 "Return a list of the integer part of X and the fractional part of X.
|
|
412 With two arguments, return truncation and remainder of their quotient."
|
|
413 (if (eq (>= x 0) (or (null y) (>= y 0)))
|
|
414 (floor* x y) (ceiling* x y)))
|
|
415
|
|
416 (defun round* (x &optional y)
|
|
417 "Return a list of X rounded to the nearest integer and the remainder.
|
|
418 With two arguments, return rounding and remainder of their quotient."
|
|
419 (if y
|
|
420 (if (and (integerp x) (integerp y))
|
|
421 (let* ((hy (/ y 2))
|
|
422 (res (floor* (+ x hy) y)))
|
|
423 (if (and (= (car (cdr res)) 0)
|
|
424 (= (+ hy hy) y)
|
|
425 (/= (% (car res) 2) 0))
|
|
426 (list (1- (car res)) hy)
|
|
427 (list (car res) (- (car (cdr res)) hy))))
|
|
428 (let ((q (round (/ x y))))
|
|
429 (list q (- x (* q y)))))
|
|
430 (if (integerp x) (list x 0)
|
|
431 (let ((q (round x)))
|
|
432 (list q (- x q))))))
|
|
433
|
|
434 (defun mod* (x y)
|
|
435 "The remainder of X divided by Y, with the same sign as Y."
|
|
436 (nth 1 (floor* x y)))
|
|
437
|
|
438 (defun rem* (x y)
|
|
439 "The remainder of X divided by Y, with the same sign as X."
|
|
440 (nth 1 (truncate* x y)))
|
|
441
|
|
442 (defun signum (a)
|
|
443 "Return 1 if A is positive, -1 if negative, 0 if zero."
|
|
444 (cond ((> a 0) 1) ((< a 0) -1) (t 0)))
|
|
445
|
|
446
|
|
447 ;; Random numbers.
|
|
448
|
|
449 (defvar *random-state*)
|
|
450 (defun random* (lim &optional state)
|
|
451 "Return a random nonnegative number less than LIM, an integer or float.
|
|
452 Optional second arg STATE is a random-state object."
|
|
453 (or state (setq state *random-state*))
|
|
454 ;; Inspired by "ran3" from Numerical Recipes. Additive congruential method.
|
|
455 (let ((vec (aref state 3)))
|
|
456 (if (integerp vec)
|
|
457 (let ((i 0) (j (- 1357335 (% (abs vec) 1357333))) (k 1))
|
|
458 (aset state 3 (setq vec (make-vector 55 nil)))
|
|
459 (aset vec 0 j)
|
|
460 (while (> (setq i (% (+ i 21) 55)) 0)
|
|
461 (aset vec i (setq j (prog1 k (setq k (- j k))))))
|
|
462 (while (< (setq i (1+ i)) 200) (random* 2 state))))
|
|
463 (let* ((i (aset state 1 (% (1+ (aref state 1)) 55)))
|
|
464 (j (aset state 2 (% (1+ (aref state 2)) 55)))
|
|
465 (n (logand 8388607 (aset vec i (- (aref vec i) (aref vec j))))))
|
|
466 (if (integerp lim)
|
|
467 (if (<= lim 512) (% n lim)
|
|
468 (if (> lim 8388607) (setq n (+ (lsh n 9) (random* 512 state))))
|
|
469 (let ((mask 1023))
|
|
470 (while (< mask (1- lim)) (setq mask (1+ (+ mask mask))))
|
|
471 (if (< (setq n (logand n mask)) lim) n (random* lim state))))
|
|
472 (* (/ n '8388608e0) lim)))))
|
|
473
|
|
474 (defun make-random-state (&optional state)
|
|
475 "Return a copy of random-state STATE, or of `*random-state*' if omitted.
|
|
476 If STATE is t, return a new state object seeded from the time of day."
|
|
477 (cond ((null state) (make-random-state *random-state*))
|
|
478 ((vectorp state) (cl-copy-tree state t))
|
|
479 ((integerp state) (vector 'cl-random-state-tag -1 30 state))
|
|
480 (t (make-random-state (cl-random-time)))))
|
|
481
|
|
482 (defun random-state-p (object)
|
|
483 "Return t if OBJECT is a random-state object."
|
|
484 (and (vectorp object) (= (length object) 4)
|
|
485 (eq (aref object 0) 'cl-random-state-tag)))
|
|
486
|
|
487
|
|
488 ;; Implementation limits.
|
|
489
|
|
490 (defun cl-finite-do (func a b)
|
|
491 (condition-case nil
|
|
492 (let ((res (funcall func a b))) ; check for IEEE infinity
|
|
493 (and (numberp res) (/= res (/ res 2)) res))
|
|
494 (arith-error nil)))
|
|
495
|
|
496 (defvar most-positive-float)
|
|
497 (defvar most-negative-float)
|
|
498 (defvar least-positive-float)
|
|
499 (defvar least-negative-float)
|
|
500 (defvar least-positive-normalized-float)
|
|
501 (defvar least-negative-normalized-float)
|
|
502 (defvar float-epsilon)
|
|
503 (defvar float-negative-epsilon)
|
|
504
|
|
505 (defun cl-float-limits ()
|
|
506 (or most-positive-float (not (numberp '2e1))
|
|
507 (let ((x '2e0) y z)
|
|
508 ;; Find maximum exponent (first two loops are optimizations)
|
|
509 (while (cl-finite-do '* x x) (setq x (* x x)))
|
|
510 (while (cl-finite-do '* x (/ x 2)) (setq x (* x (/ x 2))))
|
|
511 (while (cl-finite-do '+ x x) (setq x (+ x x)))
|
|
512 (setq z x y (/ x 2))
|
|
513 ;; Now fill in 1's in the mantissa.
|
|
514 (while (and (cl-finite-do '+ x y) (/= (+ x y) x))
|
|
515 (setq x (+ x y) y (/ y 2)))
|
|
516 (setq most-positive-float x
|
|
517 most-negative-float (- x))
|
|
518 ;; Divide down until mantissa starts rounding.
|
|
519 (setq x (/ x z) y (/ 16 z) x (* x y))
|
|
520 (while (condition-case nil (and (= x (* (/ x 2) 2)) (> (/ y 2) 0))
|
|
521 (arith-error nil))
|
|
522 (setq x (/ x 2) y (/ y 2)))
|
|
523 (setq least-positive-normalized-float y
|
|
524 least-negative-normalized-float (- y))
|
|
525 ;; Divide down until value underflows to zero.
|
|
526 (setq x (/ 1 z) y x)
|
|
527 (while (condition-case nil (> (/ x 2) 0) (arith-error nil))
|
|
528 (setq x (/ x 2)))
|
|
529 (setq least-positive-float x
|
|
530 least-negative-float (- x))
|
|
531 (setq x '1e0)
|
|
532 (while (/= (+ '1e0 x) '1e0) (setq x (/ x 2)))
|
|
533 (setq float-epsilon (* x 2))
|
|
534 (setq x '1e0)
|
|
535 (while (/= (- '1e0 x) '1e0) (setq x (/ x 2)))
|
|
536 (setq float-negative-epsilon (* x 2))))
|
|
537 nil)
|
|
538
|
|
539
|
|
540 ;;; Sequence functions.
|
|
541
|
|
542 ;XEmacs -- our built-in is more powerful.
|
|
543 ;(defun subseq (seq start &optional end)
|
|
544 ; "Return the subsequence of SEQ from START to END.
|
|
545 ;If END is omitted, it defaults to the length of the sequence.
|
|
546 ;If START or END is negative, it counts from the end."
|
|
547 ; (if (stringp seq) (substring seq start end)
|
|
548 ; (let (len)
|
|
549 ; (and end (< end 0) (setq end (+ end (setq len (length seq)))))
|
|
550 ; (if (< start 0) (setq start (+ start (or len (setq len (length seq))))))
|
|
551 ; (cond ((listp seq)
|
|
552 ; (if (> start 0) (setq seq (nthcdr start seq)))
|
|
553 ; (if end
|
|
554 ; (let ((res nil))
|
|
555 ; (while (>= (setq end (1- end)) start)
|
2153
|
556 ; (push (pop seq) res))
|
428
|
557 ; (nreverse res))
|
|
558 ; (copy-sequence seq)))
|
|
559 ; (t
|
|
560 ; (or end (setq end (or len (length seq))))
|
|
561 ; (let ((res (make-vector (max (- end start) 0) nil))
|
|
562 ; (i 0))
|
|
563 ; (while (< start end)
|
|
564 ; (aset res i (aref seq start))
|
|
565 ; (setq i (1+ i) start (1+ start)))
|
|
566 ; res))))))
|
|
567
|
|
568 (defun concatenate (type &rest seqs)
|
|
569 "Concatenate, into a sequence of type TYPE, the argument SEQUENCES."
|
2153
|
570 ;; XEmacs change: use case instead of cond for clarity
|
428
|
571 (case type
|
|
572 (vector (apply 'vconcat seqs))
|
|
573 (string (apply 'concat seqs))
|
|
574 (list (apply 'append (append seqs '(nil))))
|
|
575 (t (error "Not a sequence type name: %s" type))))
|
|
576
|
|
577 ;;; List functions.
|
|
578
|
|
579 (defun revappend (x y)
|
|
580 "Equivalent to (append (reverse X) Y)."
|
|
581 (nconc (reverse x) y))
|
|
582
|
|
583 (defun nreconc (x y)
|
|
584 "Equivalent to (nconc (nreverse X) Y)."
|
|
585 (nconc (nreverse x) y))
|
|
586
|
|
587 (defun list-length (x)
|
|
588 "Return the length of a list. Return nil if list is circular."
|
|
589 (let ((n 0) (fast x) (slow x))
|
|
590 (while (and (cdr fast) (not (and (eq fast slow) (> n 0))))
|
|
591 (setq n (+ n 2) fast (cdr (cdr fast)) slow (cdr slow)))
|
|
592 (if fast (if (cdr fast) nil (1+ n)) n)))
|
|
593
|
|
594 (defun tailp (sublist list)
|
|
595 "Return true if SUBLIST is a tail of LIST."
|
|
596 (while (and (consp list) (not (eq sublist list)))
|
|
597 (setq list (cdr list)))
|
|
598 (if (numberp sublist) (equal sublist list) (eq sublist list)))
|
|
599
|
2153
|
600 (defalias 'cl-copy-tree 'copy-tree)
|
428
|
601
|
|
602
|
|
603 ;;; Property lists.
|
|
604
|
|
605 ;; XEmacs: our `get' groks DEFAULT.
|
|
606 (defalias 'get* 'get)
|
442
|
607 (defalias 'getf 'plist-get)
|
428
|
608
|
|
609 (defun cl-set-getf (plist tag val)
|
|
610 (let ((p plist))
|
|
611 (while (and p (not (eq (car p) tag))) (setq p (cdr (cdr p))))
|
|
612 (if p (progn (setcar (cdr p) val) plist) (list* tag val plist))))
|
|
613
|
|
614 (defun cl-do-remf (plist tag)
|
|
615 (let ((p (cdr plist)))
|
|
616 (while (and (cdr p) (not (eq (car (cdr p)) tag))) (setq p (cdr (cdr p))))
|
|
617 (and (cdr p) (progn (setcdr p (cdr (cdr (cdr p)))) t))))
|
|
618
|
2153
|
619 ;; XEmacs change: we have a builtin remprop
|
|
620 (defalias 'cl-remprop 'remprop)
|
|
621
|
|
622
|
|
623
|
428
|
624 ;;; Hash tables.
|
|
625
|
|
626 ;; The `regular' Common Lisp hash-table stuff has been moved into C.
|
|
627 ;; Only backward compatibility stuff remains here.
|
|
628 (defun make-hashtable (size &optional test)
|
|
629 (make-hash-table :test test :size size))
|
|
630 (defun make-weak-hashtable (size &optional test)
|
|
631 (make-hash-table :test test :size size :weakness t))
|
|
632 (defun make-key-weak-hashtable (size &optional test)
|
|
633 (make-hash-table :test test :size size :weakness 'key))
|
|
634 (defun make-value-weak-hashtable (size &optional test)
|
|
635 (make-hash-table :test test :size size :weakness 'value))
|
|
636
|
|
637 (define-obsolete-function-alias 'hashtablep 'hash-table-p)
|
|
638 (define-obsolete-function-alias 'hashtable-fullness 'hash-table-count)
|
|
639 (define-obsolete-function-alias 'hashtable-test-function 'hash-table-test)
|
|
640 (define-obsolete-function-alias 'hashtable-type 'hash-table-type)
|
|
641 (define-obsolete-function-alias 'hashtable-size 'hash-table-size)
|
|
642 (define-obsolete-function-alias 'copy-hashtable 'copy-hash-table)
|
|
643
|
|
644 (make-obsolete 'make-hashtable 'make-hash-table)
|
|
645 (make-obsolete 'make-weak-hashtable 'make-hash-table)
|
|
646 (make-obsolete 'make-key-weak-hashtable 'make-hash-table)
|
|
647 (make-obsolete 'make-value-weak-hashtable 'make-hash-table)
|
|
648 (make-obsolete 'hash-table-type 'hash-table-weakness)
|
|
649
|
|
650 (when (fboundp 'x-keysym-hash-table)
|
|
651 (make-obsolete 'x-keysym-hashtable 'x-keysym-hash-table))
|
|
652
|
|
653 ;; Compatibility stuff for old kludgy cl.el hash table implementation
|
|
654 (defvar cl-builtin-gethash (symbol-function 'gethash))
|
|
655 (defvar cl-builtin-remhash (symbol-function 'remhash))
|
|
656 (defvar cl-builtin-clrhash (symbol-function 'clrhash))
|
|
657 (defvar cl-builtin-maphash (symbol-function 'maphash))
|
|
658
|
|
659 (defalias 'cl-gethash 'gethash)
|
|
660 (defalias 'cl-puthash 'puthash)
|
|
661 (defalias 'cl-remhash 'remhash)
|
|
662 (defalias 'cl-clrhash 'clrhash)
|
|
663 (defalias 'cl-maphash 'maphash)
|
2153
|
664 ;; These three actually didn't exist in Emacs-20.
|
|
665 (defalias 'cl-make-hash-table 'make-hash-table)
|
|
666 (defalias 'cl-hash-table-p 'hash-table-p)
|
|
667 (defalias 'cl-hash-table-count 'hash-table-count)
|
428
|
668
|
|
669 ;;; Some debugging aids.
|
|
670
|
|
671 (defun cl-prettyprint (form)
|
|
672 "Insert a pretty-printed rendition of a Lisp FORM in current buffer."
|
|
673 (let ((pt (point)) last)
|
|
674 (insert "\n" (prin1-to-string form) "\n")
|
|
675 (setq last (point))
|
|
676 (goto-char (1+ pt))
|
|
677 (while (search-forward "(quote " last t)
|
|
678 (delete-backward-char 7)
|
|
679 (insert "'")
|
|
680 (forward-sexp)
|
|
681 (delete-char 1))
|
|
682 (goto-char (1+ pt))
|
|
683 (cl-do-prettyprint)))
|
|
684
|
|
685 (defun cl-do-prettyprint ()
|
|
686 (skip-chars-forward " ")
|
|
687 (if (looking-at "(")
|
1729
|
688 (let ((skip (or (looking-at "((")
|
2153
|
689 ;; XEmacs: be selective about trailing stuff after prog
|
1729
|
690 (looking-at "(prog[nv12\\(ress-feedback\\|n-with-message\\)]")
|
428
|
691 (looking-at "(unwind-protect ")
|
|
692 (looking-at "(function (")
|
|
693 (looking-at "(cl-block-wrapper ")))
|
|
694 (two (or (looking-at "(defun ") (looking-at "(defmacro ")))
|
|
695 (let (or (looking-at "(let\\*? ") (looking-at "(while ")))
|
|
696 (set (looking-at "(p?set[qf] ")))
|
|
697 (if (or skip let
|
|
698 (progn
|
|
699 (forward-sexp)
|
|
700 (and (>= (current-column) 78) (progn (backward-sexp) t))))
|
|
701 (let ((nl t))
|
|
702 (forward-char 1)
|
|
703 (cl-do-prettyprint)
|
|
704 (or skip (looking-at ")") (cl-do-prettyprint))
|
|
705 (or (not two) (looking-at ")") (cl-do-prettyprint))
|
|
706 (while (not (looking-at ")"))
|
|
707 (if set (setq nl (not nl)))
|
|
708 (if nl (insert "\n"))
|
|
709 (lisp-indent-line)
|
|
710 (cl-do-prettyprint))
|
|
711 (forward-char 1))))
|
|
712 (forward-sexp)))
|
|
713
|
|
714 (defvar cl-macroexpand-cmacs nil)
|
|
715 (defvar cl-closure-vars nil)
|
|
716
|
|
717 (defun cl-macroexpand-all (form &optional env)
|
|
718 "Expand all macro calls through a Lisp FORM.
|
|
719 This also does some trivial optimizations to make the form prettier."
|
|
720 (while (or (not (eq form (setq form (macroexpand form env))))
|
|
721 (and cl-macroexpand-cmacs
|
|
722 (not (eq form (setq form (compiler-macroexpand form)))))))
|
|
723 (cond ((not (consp form)) form)
|
|
724 ((memq (car form) '(let let*))
|
|
725 (if (null (nth 1 form))
|
|
726 (cl-macroexpand-all (cons 'progn (cddr form)) env)
|
|
727 (let ((letf nil) (res nil) (lets (cadr form)))
|
|
728 (while lets
|
2153
|
729 (push (if (consp (car lets))
|
428
|
730 (let ((exp (cl-macroexpand-all (caar lets) env)))
|
|
731 (or (symbolp exp) (setq letf t))
|
|
732 (cons exp (cl-macroexpand-body (cdar lets) env)))
|
|
733 (let ((exp (cl-macroexpand-all (car lets) env)))
|
|
734 (if (symbolp exp) exp
|
|
735 (setq letf t) (list exp nil)))) res)
|
|
736 (setq lets (cdr lets)))
|
|
737 (list* (if letf (if (eq (car form) 'let) 'letf 'letf*) (car form))
|
|
738 (nreverse res) (cl-macroexpand-body (cddr form) env)))))
|
|
739 ((eq (car form) 'cond)
|
|
740 (cons (car form)
|
|
741 (mapcar (function (lambda (x) (cl-macroexpand-body x env)))
|
|
742 (cdr form))))
|
|
743 ((eq (car form) 'condition-case)
|
|
744 (list* (car form) (nth 1 form) (cl-macroexpand-all (nth 2 form) env)
|
|
745 (mapcar (function
|
|
746 (lambda (x)
|
|
747 (cons (car x) (cl-macroexpand-body (cdr x) env))))
|
|
748 (cdddr form))))
|
|
749 ((memq (car form) '(quote function))
|
|
750 (if (eq (car-safe (nth 1 form)) 'lambda)
|
|
751 (let ((body (cl-macroexpand-body (cddadr form) env)))
|
|
752 (if (and cl-closure-vars (eq (car form) 'function)
|
|
753 (cl-expr-contains-any body cl-closure-vars))
|
|
754 (let* ((new (mapcar 'gensym cl-closure-vars))
|
|
755 (sub (pairlis cl-closure-vars new)) (decls nil))
|
|
756 (while (or (stringp (car body))
|
|
757 (eq (car-safe (car body)) 'interactive))
|
2153
|
758 (push (list 'quote (pop body)) decls))
|
428
|
759 (put (car (last cl-closure-vars)) 'used t)
|
|
760 (append
|
|
761 (list 'list '(quote lambda) '(quote (&rest --cl-rest--)))
|
|
762 (sublis sub (nreverse decls))
|
|
763 (list
|
|
764 (list* 'list '(quote apply)
|
2153
|
765 ;; XEmacs: put a quote before the function
|
428
|
766 (list 'list '(quote quote)
|
|
767 (list 'function
|
|
768 (list* 'lambda
|
|
769 (append new (cadadr form))
|
|
770 (sublis sub body))))
|
|
771 (nconc (mapcar (function
|
|
772 (lambda (x)
|
|
773 (list 'list '(quote quote) x)))
|
|
774 cl-closure-vars)
|
|
775 '((quote --cl-rest--)))))))
|
|
776 (list (car form) (list* 'lambda (cadadr form) body))))
|
|
777 (let ((found (assq (cadr form) env)))
|
2153
|
778 ;; XEmacs: cadr/caddr operate on nil without errors
|
428
|
779 (if (eq (cadr (caddr found)) 'cl-labels-args)
|
|
780 (cl-macroexpand-all (cadr (caddr (cadddr found))) env)
|
|
781 form))))
|
|
782 ((memq (car form) '(defun defmacro))
|
|
783 (list* (car form) (nth 1 form) (cl-macroexpand-body (cddr form) env)))
|
|
784 ((and (eq (car form) 'progn) (not (cddr form)))
|
|
785 (cl-macroexpand-all (nth 1 form) env))
|
|
786 ((eq (car form) 'setq)
|
|
787 (let* ((args (cl-macroexpand-body (cdr form) env)) (p args))
|
|
788 (while (and p (symbolp (car p))) (setq p (cddr p)))
|
|
789 (if p (cl-macroexpand-all (cons 'setf args)) (cons 'setq args))))
|
|
790 (t (cons (car form) (cl-macroexpand-body (cdr form) env)))))
|
|
791
|
|
792 (defun cl-macroexpand-body (body &optional env)
|
|
793 (mapcar (function (lambda (x) (cl-macroexpand-all x env))) body))
|
|
794
|
|
795 (defun cl-prettyexpand (form &optional full)
|
|
796 (message "Expanding...")
|
|
797 (let ((cl-macroexpand-cmacs full) (cl-compiling-file full)
|
|
798 (byte-compile-macro-environment nil))
|
|
799 (setq form (cl-macroexpand-all form
|
|
800 (and (not full) '((block) (eval-when)))))
|
|
801 (message "Formatting...")
|
|
802 (prog1 (cl-prettyprint form)
|
|
803 (message ""))))
|
|
804
|
|
805
|
|
806
|
|
807 (run-hooks 'cl-extra-load-hook)
|
|
808
|
2153
|
809 ;; XEmacs addition
|
428
|
810 (provide 'cl-extra)
|
|
811
|
2153
|
812 ;;; arch-tag: bcd03437-0871-43fb-a8f1-ad0e0b5427ed
|
428
|
813 ;;; cl-extra.el ends here
|