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