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