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