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