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