Mercurial > hg > xemacs-beta
annotate lisp/cl-extra.el @ 5305:09fed7053634
Handle slightly more complex type specifications, #'coerce, #'typep.
2010-11-17 Aidan Kehoe <kehoea@parhasard.net>
* cl-extra.el (coerce):
In the argument list, name the first argument OBJECT, not X; the
former name was always used in the doc string and is clearer.
Handle vector type specifications which include the length of the
target sequence, error if there's a mismatch.
* cl-macs.el (cl-make-type-test): Handle type specifications
starting with the symbol 'eql.
author | Aidan Kehoe <kehoea@parhasard.net> |
---|---|
date | Wed, 17 Nov 2010 14:30:03 +0000 |
parents | 99de5fd48e87 |
children | f6471e4ae703 8d29f1c4bb98 |
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 ;;; Type coercion. | |
55 | |
5305
09fed7053634
Handle slightly more complex type specifications, #'coerce, #'typep.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5285
diff
changeset
|
56 (defun coerce (object type) |
428 | 57 "Coerce OBJECT to type TYPE. |
58 TYPE is a Common Lisp type specifier." | |
5305
09fed7053634
Handle slightly more complex type specifications, #'coerce, #'typep.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5285
diff
changeset
|
59 (cond ((eq type 'list) (if (listp object) object (append object nil))) |
09fed7053634
Handle slightly more complex type specifications, #'coerce, #'typep.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5285
diff
changeset
|
60 ((eq type 'vector) (if (vectorp object) object (vconcat object))) |
09fed7053634
Handle slightly more complex type specifications, #'coerce, #'typep.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5285
diff
changeset
|
61 ((eq type 'string) (if (stringp object) object (concat object))) |
09fed7053634
Handle slightly more complex type specifications, #'coerce, #'typep.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5285
diff
changeset
|
62 ((eq type 'array) (if (arrayp object) object (vconcat object))) |
09fed7053634
Handle slightly more complex type specifications, #'coerce, #'typep.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5285
diff
changeset
|
63 ((and (eq type 'character) (stringp object) |
09fed7053634
Handle slightly more complex type specifications, #'coerce, #'typep.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5285
diff
changeset
|
64 (eql (length object) 1)) (aref object 0)) |
09fed7053634
Handle slightly more complex type specifications, #'coerce, #'typep.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5285
diff
changeset
|
65 ((and (eq type 'character) (symbolp object)) |
09fed7053634
Handle slightly more complex type specifications, #'coerce, #'typep.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5285
diff
changeset
|
66 (coerce (symbol-name object) type)) |
2153 | 67 ;; XEmacs addition character <-> integer coercions |
5305
09fed7053634
Handle slightly more complex type specifications, #'coerce, #'typep.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5285
diff
changeset
|
68 ((and (eq type 'character) (char-int-p object)) (int-char object)) |
09fed7053634
Handle slightly more complex type specifications, #'coerce, #'typep.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5285
diff
changeset
|
69 ((and (memq type '(integer fixnum)) (characterp object)) |
09fed7053634
Handle slightly more complex type specifications, #'coerce, #'typep.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5285
diff
changeset
|
70 (char-int object)) |
09fed7053634
Handle slightly more complex type specifications, #'coerce, #'typep.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5285
diff
changeset
|
71 ((eq type 'float) (float object)) |
2153 | 72 ;; XEmacs addition: enhanced numeric type coercions |
2367 | 73 ((and-fboundp 'coerce-number |
5257
30bf66dd3ca0
Add fixnum as an accepted destination type, #'coerce
Aidan Kehoe <kehoea@parhasard.net>
parents:
5242
diff
changeset
|
74 (memq type '(integer ratio bigfloat fixnum)) |
5305
09fed7053634
Handle slightly more complex type specifications, #'coerce, #'typep.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5285
diff
changeset
|
75 (coerce-number object type))) |
2153 | 76 ;; XEmacs addition: bit-vector coercion |
4995
8431b52e43b1
Move the various map* functions to C; add #'map-into.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4800
diff
changeset
|
77 ((or (eq type 'bit-vector) |
8431b52e43b1
Move the various map* functions to C; add #'map-into.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4800
diff
changeset
|
78 (eq type 'simple-bit-vector)) |
5305
09fed7053634
Handle slightly more complex type specifications, #'coerce, #'typep.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5285
diff
changeset
|
79 (if (bit-vector-p object) |
09fed7053634
Handle slightly more complex type specifications, #'coerce, #'typep.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5285
diff
changeset
|
80 object |
09fed7053634
Handle slightly more complex type specifications, #'coerce, #'typep.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5285
diff
changeset
|
81 (apply 'bit-vector (append object nil)))) |
2153 | 82 ;; XEmacs addition: weak-list coercion |
428 | 83 ((eq type 'weak-list) |
5305
09fed7053634
Handle slightly more complex type specifications, #'coerce, #'typep.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5285
diff
changeset
|
84 (if (weak-list-p object) object |
428 | 85 (let ((wl (make-weak-list))) |
5305
09fed7053634
Handle slightly more complex type specifications, #'coerce, #'typep.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5285
diff
changeset
|
86 (set-weak-list-list wl (if (listp object) |
09fed7053634
Handle slightly more complex type specifications, #'coerce, #'typep.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5285
diff
changeset
|
87 object |
09fed7053634
Handle slightly more complex type specifications, #'coerce, #'typep.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5285
diff
changeset
|
88 (append object nil))) |
428 | 89 wl))) |
4995
8431b52e43b1
Move the various map* functions to C; add #'map-into.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4800
diff
changeset
|
90 ((and |
5305
09fed7053634
Handle slightly more complex type specifications, #'coerce, #'typep.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5285
diff
changeset
|
91 (memq (car-safe type) '(vector simple-array)) |
09fed7053634
Handle slightly more complex type specifications, #'coerce, #'typep.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5285
diff
changeset
|
92 (loop |
09fed7053634
Handle slightly more complex type specifications, #'coerce, #'typep.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5285
diff
changeset
|
93 for (ignore elements length) = type |
09fed7053634
Handle slightly more complex type specifications, #'coerce, #'typep.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5285
diff
changeset
|
94 initially (declare (special ignore)) |
09fed7053634
Handle slightly more complex type specifications, #'coerce, #'typep.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5285
diff
changeset
|
95 return (if (or (memq length '(* nil)) (eql length (length object))) |
09fed7053634
Handle slightly more complex type specifications, #'coerce, #'typep.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5285
diff
changeset
|
96 (cond |
09fed7053634
Handle slightly more complex type specifications, #'coerce, #'typep.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5285
diff
changeset
|
97 ((memq elements '(t * nil)) |
09fed7053634
Handle slightly more complex type specifications, #'coerce, #'typep.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5285
diff
changeset
|
98 (coerce object 'vector)) |
09fed7053634
Handle slightly more complex type specifications, #'coerce, #'typep.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5285
diff
changeset
|
99 ((memq elements '(string-char character)) |
09fed7053634
Handle slightly more complex type specifications, #'coerce, #'typep.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5285
diff
changeset
|
100 (coerce object 'string)) |
09fed7053634
Handle slightly more complex type specifications, #'coerce, #'typep.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5285
diff
changeset
|
101 ((eq elements 'bit) |
09fed7053634
Handle slightly more complex type specifications, #'coerce, #'typep.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5285
diff
changeset
|
102 (coerce object 'bit-vector))) |
09fed7053634
Handle slightly more complex type specifications, #'coerce, #'typep.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5285
diff
changeset
|
103 (error |
09fed7053634
Handle slightly more complex type specifications, #'coerce, #'typep.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5285
diff
changeset
|
104 'wrong-type-argument |
09fed7053634
Handle slightly more complex type specifications, #'coerce, #'typep.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5285
diff
changeset
|
105 "Type specifier length must equal sequence length" |
09fed7053634
Handle slightly more complex type specifications, #'coerce, #'typep.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5285
diff
changeset
|
106 type))))) |
09fed7053634
Handle slightly more complex type specifications, #'coerce, #'typep.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5285
diff
changeset
|
107 ((eq (car-safe type) 'simple-vector) |
09fed7053634
Handle slightly more complex type specifications, #'coerce, #'typep.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5285
diff
changeset
|
108 (coerce object (list* 'vector t (cdr type)))) |
09fed7053634
Handle slightly more complex type specifications, #'coerce, #'typep.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5285
diff
changeset
|
109 ((memq (car-safe type) |
09fed7053634
Handle slightly more complex type specifications, #'coerce, #'typep.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5285
diff
changeset
|
110 '(string simple-string base-string simple-base-string)) |
09fed7053634
Handle slightly more complex type specifications, #'coerce, #'typep.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5285
diff
changeset
|
111 (coerce object (list* 'vector 'character (cdr type)))) |
09fed7053634
Handle slightly more complex type specifications, #'coerce, #'typep.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5285
diff
changeset
|
112 ((eq (car-safe type) 'bit-vector) |
09fed7053634
Handle slightly more complex type specifications, #'coerce, #'typep.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5285
diff
changeset
|
113 (coerce object (list* 'vector 'bit (cdr type)))) |
09fed7053634
Handle slightly more complex type specifications, #'coerce, #'typep.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5285
diff
changeset
|
114 ((typep object type) object) |
09fed7053634
Handle slightly more complex type specifications, #'coerce, #'typep.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5285
diff
changeset
|
115 (t (error 'invalid-operation |
09fed7053634
Handle slightly more complex type specifications, #'coerce, #'typep.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5285
diff
changeset
|
116 "Can't coerce object to type" object type)))) |
428 | 117 |
5219
2d0937dc83cf
Tidying of CL files; make docstrings read better, remove commented-out code
Aidan Kehoe <kehoea@parhasard.net>
parents:
5162
diff
changeset
|
118 ;; XEmacs; #'equalp is in C. |
428 | 119 |
4997
8800b5350a13
Move #'some, #'every to C, implementing them with mapcarX.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4996
diff
changeset
|
120 ;; XEmacs; #'map, #'mapc, #'mapl, #'maplist, #'mapcon, #'some and #'every |
8800b5350a13
Move #'some, #'every to C, implementing them with mapcarX.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4996
diff
changeset
|
121 ;; are now in C, together with #'map-into, which was never in this file. |
428 | 122 |
5226
7789ae555c45
Add Common Lisp's #'complement to cl-extra.el.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5219
diff
changeset
|
123 ;; The compiler macro for this in cl-macs.el means if #'complement is handed |
7789ae555c45
Add Common Lisp's #'complement to cl-extra.el.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5219
diff
changeset
|
124 ;; a constant expression, byte-compiled code will see a byte-compiled |
7789ae555c45
Add Common Lisp's #'complement to cl-extra.el.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5219
diff
changeset
|
125 ;; function. |
7789ae555c45
Add Common Lisp's #'complement to cl-extra.el.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5219
diff
changeset
|
126 (defun complement (function &optional documentation) |
7789ae555c45
Add Common Lisp's #'complement to cl-extra.el.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5219
diff
changeset
|
127 "Return a function which gives the logical inverse of what FUNCTION would." |
7789ae555c45
Add Common Lisp's #'complement to cl-extra.el.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5219
diff
changeset
|
128 `(lambda (&rest arguments) ,@(if documentation (list documentation)) |
7789ae555c45
Add Common Lisp's #'complement to cl-extra.el.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5219
diff
changeset
|
129 (not (apply ',function arguments)))) |
7789ae555c45
Add Common Lisp's #'complement to cl-extra.el.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5219
diff
changeset
|
130 |
428 | 131 (defun notany (cl-pred cl-seq &rest cl-rest) |
132 "Return true if PREDICATE is false of every element of SEQ or SEQs." | |
133 (not (apply 'some cl-pred cl-seq cl-rest))) | |
134 | |
135 (defun notevery (cl-pred cl-seq &rest cl-rest) | |
136 "Return true if PREDICATE is false of some element of SEQ or SEQs." | |
137 (not (apply 'every cl-pred cl-seq cl-rest))) | |
138 | |
139 ;;; Support for `loop'. | |
2153 | 140 (defalias 'cl-map-keymap 'map-keymap) |
428 | 141 |
142 (defun cl-map-keymap-recursively (cl-func-rec cl-map &optional cl-base) | |
143 (or cl-base | |
2153 | 144 (setq cl-base (copy-sequence [0]))) |
145 (map-keymap | |
428 | 146 (function |
147 (lambda (cl-key cl-bind) | |
148 (aset cl-base (1- (length cl-base)) cl-key) | |
149 (if (keymapp cl-bind) | |
150 (cl-map-keymap-recursively | |
151 cl-func-rec cl-bind | |
2153 | 152 (vconcat cl-base (list 0))) |
428 | 153 (funcall cl-func-rec cl-base cl-bind)))) |
154 cl-map)) | |
155 | |
156 (defun cl-map-intervals (cl-func &optional cl-what cl-prop cl-start cl-end) | |
157 (or cl-what (setq cl-what (current-buffer))) | |
158 (if (bufferp cl-what) | |
159 (let (cl-mark cl-mark2 (cl-next t) cl-next2) | |
2153 | 160 (with-current-buffer cl-what |
428 | 161 (setq cl-mark (copy-marker (or cl-start (point-min)))) |
162 (setq cl-mark2 (and cl-end (copy-marker cl-end)))) | |
163 (while (and cl-next (or (not cl-mark2) (< cl-mark cl-mark2))) | |
2153 | 164 (setq cl-next (if cl-prop (next-single-property-change |
165 cl-mark cl-prop cl-what) | |
166 (next-property-change cl-mark cl-what)) | |
167 cl-next2 (or cl-next (with-current-buffer cl-what | |
168 (point-max)))) | |
428 | 169 (funcall cl-func (prog1 (marker-position cl-mark) |
170 (set-marker cl-mark cl-next2)) | |
171 (if cl-mark2 (min cl-next2 cl-mark2) cl-next2))) | |
172 (set-marker cl-mark nil) (if cl-mark2 (set-marker cl-mark2 nil))) | |
173 (or cl-start (setq cl-start 0)) | |
174 (or cl-end (setq cl-end (length cl-what))) | |
175 (while (< cl-start cl-end) | |
2153 | 176 (let ((cl-next (or (if cl-prop (next-single-property-change |
177 cl-start cl-prop cl-what) | |
178 (next-property-change cl-start cl-what)) | |
428 | 179 cl-end))) |
180 (funcall cl-func cl-start (min cl-next cl-end)) | |
181 (setq cl-start cl-next))))) | |
182 | |
183 (defun cl-map-overlays (cl-func &optional cl-buffer cl-start cl-end cl-arg) | |
184 (or cl-buffer (setq cl-buffer (current-buffer))) | |
502 | 185 (with-fboundp '(overlay-start overlay-end overlays-at next-overlay-change) |
186 (if-fboundp 'overlay-lists | |
428 | 187 |
502 | 188 ;; This is the preferred algorithm, though overlay-lists is |
189 ;; undocumented. | |
190 (let (cl-ovl) | |
2153 | 191 (with-current-buffer cl-buffer |
502 | 192 (setq cl-ovl (overlay-lists)) |
193 (if cl-start (setq cl-start (copy-marker cl-start))) | |
194 (if cl-end (setq cl-end (copy-marker cl-end)))) | |
195 (setq cl-ovl (nconc (car cl-ovl) (cdr cl-ovl))) | |
196 (while (and cl-ovl | |
197 (or (not (overlay-start (car cl-ovl))) | |
198 (and cl-end (>= (overlay-start (car cl-ovl)) cl-end)) | |
199 (and cl-start (<= (overlay-end (car cl-ovl)) | |
200 cl-start)) | |
201 (not (funcall cl-func (car cl-ovl) cl-arg)))) | |
202 (setq cl-ovl (cdr cl-ovl))) | |
203 (if cl-start (set-marker cl-start nil)) | |
204 (if cl-end (set-marker cl-end nil))) | |
205 | |
206 ;; This alternate algorithm fails to find zero-length overlays. | |
2153 | 207 (let ((cl-mark (with-current-buffer cl-buffer |
208 (copy-marker (or cl-start (point-min))))) | |
209 (cl-mark2 (and cl-end (with-current-buffer cl-buffer | |
210 (copy-marker cl-end)))) | |
502 | 211 cl-pos cl-ovl) |
212 (while (save-excursion | |
213 (and (setq cl-pos (marker-position cl-mark)) | |
214 (< cl-pos (or cl-mark2 (point-max))) | |
215 (progn | |
216 (set-buffer cl-buffer) | |
217 (setq cl-ovl (overlays-at cl-pos)) | |
218 (set-marker cl-mark (next-overlay-change cl-pos))))) | |
219 (while (and cl-ovl | |
220 (or (/= (overlay-start (car cl-ovl)) cl-pos) | |
221 (not (and (funcall cl-func (car cl-ovl) cl-arg) | |
222 (set-marker cl-mark nil))))) | |
223 (setq cl-ovl (cdr cl-ovl)))) | |
224 (set-marker cl-mark nil) (if cl-mark2 (set-marker cl-mark2 nil)))))) | |
428 | 225 |
226 ;;; Support for `setf'. | |
227 (defun cl-set-frame-visible-p (frame val) | |
228 (cond ((null val) (make-frame-invisible frame)) | |
229 ((eq val 'icon) (iconify-frame frame)) | |
230 (t (make-frame-visible frame))) | |
231 val) | |
232 | |
233 ;;; Support for `progv'. | |
234 (defvar cl-progv-save) | |
235 (defun cl-progv-before (syms values) | |
236 (while syms | |
2153 | 237 (push (if (boundp (car syms)) |
428 | 238 (cons (car syms) (symbol-value (car syms))) |
239 (car syms)) cl-progv-save) | |
240 (if values | |
2153 | 241 (set (pop syms) (pop values)) |
242 (makunbound (pop syms))))) | |
428 | 243 |
244 (defun cl-progv-after () | |
245 (while cl-progv-save | |
246 (if (consp (car cl-progv-save)) | |
247 (set (car (car cl-progv-save)) (cdr (car cl-progv-save))) | |
248 (makunbound (car cl-progv-save))) | |
2153 | 249 (pop cl-progv-save))) |
428 | 250 |
251 ;;; Numbers. | |
252 | |
253 (defun gcd (&rest args) | |
254 "Return the greatest common divisor of the arguments." | |
2153 | 255 (let ((a (abs (or (pop args) 0)))) |
428 | 256 (while args |
2153 | 257 (let ((b (abs (pop args)))) |
428 | 258 (while (> b 0) (setq b (% a (setq a b)))))) |
259 a)) | |
260 | |
261 (defun lcm (&rest args) | |
262 "Return the least common multiple of the arguments." | |
263 (if (memq 0 args) | |
264 0 | |
2153 | 265 (let ((a (abs (or (pop args) 1)))) |
428 | 266 (while args |
2153 | 267 (let ((b (abs (pop args)))) |
428 | 268 (setq a (* (/ a (gcd a b)) b)))) |
269 a))) | |
270 | |
271 (defun isqrt (a) | |
272 "Return the integer square root of the argument." | |
273 (if (and (integerp a) (> a 0)) | |
274 ;; XEmacs change | |
275 (let ((g (cond ((>= a 1000000) 10000) ((>= a 10000) 1000) | |
276 ((>= a 100) 100) (t 10))) | |
277 g2) | |
278 (while (< (setq g2 (/ (+ g (/ a g)) 2)) g) | |
279 (setq g g2)) | |
280 g) | |
281 (if (eq a 0) 0 (signal 'arith-error nil)))) | |
282 | |
4678
b5e1d4f6b66f
Make #'floor, #'ceiling, #'round, #'truncate conform to Common Lisp.
Aidan Kehoe <kehoea@parhasard.net>
parents:
2367
diff
changeset
|
283 ;; 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
|
284 ;; definition-and-call: |
b5e1d4f6b66f
Make #'floor, #'ceiling, #'round, #'truncate conform to Common Lisp.
Aidan Kehoe <kehoea@parhasard.net>
parents:
2367
diff
changeset
|
285 ((macro . (lambda (&rest symbols) |
b5e1d4f6b66f
Make #'floor, #'ceiling, #'round, #'truncate conform to Common Lisp.
Aidan Kehoe <kehoea@parhasard.net>
parents:
2367
diff
changeset
|
286 "Make some old CL package truncate and round functions available. |
428 | 287 |
4678
b5e1d4f6b66f
Make #'floor, #'ceiling, #'round, #'truncate conform to Common Lisp.
Aidan Kehoe <kehoea@parhasard.net>
parents:
2367
diff
changeset
|
288 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
|
289 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
|
290 (let (symbol result) |
b5e1d4f6b66f
Make #'floor, #'ceiling, #'round, #'truncate conform to Common Lisp.
Aidan Kehoe <kehoea@parhasard.net>
parents:
2367
diff
changeset
|
291 (while symbols |
b5e1d4f6b66f
Make #'floor, #'ceiling, #'round, #'truncate conform to Common Lisp.
Aidan Kehoe <kehoea@parhasard.net>
parents:
2367
diff
changeset
|
292 (setq symbol (car symbols) |
b5e1d4f6b66f
Make #'floor, #'ceiling, #'round, #'truncate conform to Common Lisp.
Aidan Kehoe <kehoea@parhasard.net>
parents:
2367
diff
changeset
|
293 symbols (cdr symbols)) |
b5e1d4f6b66f
Make #'floor, #'ceiling, #'round, #'truncate conform to Common Lisp.
Aidan Kehoe <kehoea@parhasard.net>
parents:
2367
diff
changeset
|
294 (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
|
295 ',symbol "21.5.29") |
4800
b828e06dbe38
New (Common Lisp) function get-propertie
Didier Verna <didier@xemacs.org>
parents:
4792
diff
changeset
|
296 result) |
4678
b5e1d4f6b66f
Make #'floor, #'ceiling, #'round, #'truncate conform to Common Lisp.
Aidan Kehoe <kehoea@parhasard.net>
parents:
2367
diff
changeset
|
297 (push |
b5e1d4f6b66f
Make #'floor, #'ceiling, #'round, #'truncate conform to Common Lisp.
Aidan Kehoe <kehoea@parhasard.net>
parents:
2367
diff
changeset
|
298 `(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
|
299 ,(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
|
300 symbol) |
b5e1d4f6b66f
Make #'floor, #'ceiling, #'round, #'truncate conform to Common Lisp.
Aidan Kehoe <kehoea@parhasard.net>
parents:
2367
diff
changeset
|
301 (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
|
302 result)) |
b5e1d4f6b66f
Make #'floor, #'ceiling, #'round, #'truncate conform to Common Lisp.
Aidan Kehoe <kehoea@parhasard.net>
parents:
2367
diff
changeset
|
303 (cons 'progn result)))) |
b5e1d4f6b66f
Make #'floor, #'ceiling, #'round, #'truncate conform to Common Lisp.
Aidan Kehoe <kehoea@parhasard.net>
parents:
2367
diff
changeset
|
304 ceiling floor round truncate) |
428 | 305 |
306 (defun mod* (x y) | |
307 "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
|
308 (nth-value 1 (floor x y))) |
428 | 309 |
310 (defun rem* (x y) | |
311 "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
|
312 (nth-value 1 (truncate x y))) |
428 | 313 |
314 (defun signum (a) | |
315 "Return 1 if A is positive, -1 if negative, 0 if zero." | |
316 (cond ((> a 0) 1) ((< a 0) -1) (t 0))) | |
317 | |
318 ;; Random numbers. | |
319 | |
320 (defvar *random-state*) | |
321 (defun random* (lim &optional state) | |
322 "Return a random nonnegative number less than LIM, an integer or float. | |
323 Optional second arg STATE is a random-state object." | |
324 (or state (setq state *random-state*)) | |
325 ;; Inspired by "ran3" from Numerical Recipes. Additive congruential method. | |
326 (let ((vec (aref state 3))) | |
327 (if (integerp vec) | |
328 (let ((i 0) (j (- 1357335 (% (abs vec) 1357333))) (k 1)) | |
329 (aset state 3 (setq vec (make-vector 55 nil))) | |
330 (aset vec 0 j) | |
331 (while (> (setq i (% (+ i 21) 55)) 0) | |
332 (aset vec i (setq j (prog1 k (setq k (- j k)))))) | |
333 (while (< (setq i (1+ i)) 200) (random* 2 state)))) | |
334 (let* ((i (aset state 1 (% (1+ (aref state 1)) 55))) | |
335 (j (aset state 2 (% (1+ (aref state 2)) 55))) | |
336 (n (logand 8388607 (aset vec i (- (aref vec i) (aref vec j)))))) | |
337 (if (integerp lim) | |
338 (if (<= lim 512) (% n lim) | |
339 (if (> lim 8388607) (setq n (+ (lsh n 9) (random* 512 state)))) | |
340 (let ((mask 1023)) | |
341 (while (< mask (1- lim)) (setq mask (1+ (+ mask mask)))) | |
342 (if (< (setq n (logand n mask)) lim) n (random* lim state)))) | |
343 (* (/ n '8388608e0) lim))))) | |
344 | |
345 (defun make-random-state (&optional state) | |
346 "Return a copy of random-state STATE, or of `*random-state*' if omitted. | |
347 If STATE is t, return a new state object seeded from the time of day." | |
348 (cond ((null state) (make-random-state *random-state*)) | |
349 ((vectorp state) (cl-copy-tree state t)) | |
350 ((integerp state) (vector 'cl-random-state-tag -1 30 state)) | |
351 (t (make-random-state (cl-random-time))))) | |
352 | |
353 (defun random-state-p (object) | |
354 "Return t if OBJECT is a random-state object." | |
355 (and (vectorp object) (= (length object) 4) | |
356 (eq (aref object 0) 'cl-random-state-tag))) | |
357 | |
358 | |
359 ;; Implementation limits. | |
360 | |
361 (defun cl-finite-do (func a b) | |
362 (condition-case nil | |
363 (let ((res (funcall func a b))) ; check for IEEE infinity | |
364 (and (numberp res) (/= res (/ res 2)) res)) | |
365 (arith-error nil))) | |
366 | |
367 (defun cl-float-limits () | |
368 (or most-positive-float (not (numberp '2e1)) | |
369 (let ((x '2e0) y z) | |
370 ;; Find maximum exponent (first two loops are optimizations) | |
371 (while (cl-finite-do '* x x) (setq x (* x x))) | |
372 (while (cl-finite-do '* x (/ x 2)) (setq x (* x (/ x 2)))) | |
373 (while (cl-finite-do '+ x x) (setq x (+ x x))) | |
374 (setq z x y (/ x 2)) | |
375 ;; Now fill in 1's in the mantissa. | |
376 (while (and (cl-finite-do '+ x y) (/= (+ x y) x)) | |
377 (setq x (+ x y) y (/ y 2))) | |
378 (setq most-positive-float x | |
379 most-negative-float (- x)) | |
380 ;; Divide down until mantissa starts rounding. | |
381 (setq x (/ x z) y (/ 16 z) x (* x y)) | |
382 (while (condition-case nil (and (= x (* (/ x 2) 2)) (> (/ y 2) 0)) | |
383 (arith-error nil)) | |
384 (setq x (/ x 2) y (/ y 2))) | |
385 (setq least-positive-normalized-float y | |
386 least-negative-normalized-float (- y)) | |
387 ;; Divide down until value underflows to zero. | |
388 (setq x (/ 1 z) y x) | |
389 (while (condition-case nil (> (/ x 2) 0) (arith-error nil)) | |
390 (setq x (/ x 2))) | |
391 (setq least-positive-float x | |
392 least-negative-float (- x)) | |
393 (setq x '1e0) | |
394 (while (/= (+ '1e0 x) '1e0) (setq x (/ x 2))) | |
395 (setq float-epsilon (* x 2)) | |
396 (setq x '1e0) | |
397 (while (/= (- '1e0 x) '1e0) (setq x (/ x 2))) | |
398 (setq float-negative-epsilon (* x 2)))) | |
399 nil) | |
400 | |
5219
2d0937dc83cf
Tidying of CL files; make docstrings read better, remove commented-out code
Aidan Kehoe <kehoea@parhasard.net>
parents:
5162
diff
changeset
|
401 ;; XEmacs; call cl-float-limits at dump time. |
2d0937dc83cf
Tidying of CL files; make docstrings read better, remove commented-out code
Aidan Kehoe <kehoea@parhasard.net>
parents:
5162
diff
changeset
|
402 (cl-float-limits) |
428 | 403 |
404 ;;; Sequence functions. | |
405 | |
5219
2d0937dc83cf
Tidying of CL files; make docstrings read better, remove commented-out code
Aidan Kehoe <kehoea@parhasard.net>
parents:
5162
diff
changeset
|
406 ;; XEmacs; #'subseq is in C. |
428 | 407 |
408 (defun concatenate (type &rest seqs) | |
409 "Concatenate, into a sequence of type TYPE, the argument SEQUENCES." | |
2153 | 410 ;; XEmacs change: use case instead of cond for clarity |
428 | 411 (case type |
412 (vector (apply 'vconcat seqs)) | |
413 (string (apply 'concat seqs)) | |
414 (list (apply 'append (append seqs '(nil)))) | |
5242
f3eca926258e
Bit vectors are also sequences; enforce this in some CL functions.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5226
diff
changeset
|
415 (bit-vector (apply 'bvconcat seqs)) |
5219
2d0937dc83cf
Tidying of CL files; make docstrings read better, remove commented-out code
Aidan Kehoe <kehoea@parhasard.net>
parents:
5162
diff
changeset
|
416 (t (error 'invalid-argument "Not a sequence type name" type)))) |
428 | 417 |
418 ;;; List functions. | |
419 | |
420 (defun revappend (x y) | |
421 "Equivalent to (append (reverse X) Y)." | |
422 (nconc (reverse x) y)) | |
423 | |
424 (defun nreconc (x y) | |
425 "Equivalent to (nconc (nreverse X) Y)." | |
426 (nconc (nreverse x) y)) | |
427 | |
5285
99de5fd48e87
Tighten up Common Lisp compatibility, #'butlast, #'nbutlast, #'tailp, #'ldiff
Aidan Kehoe <kehoea@parhasard.net>
parents:
5284
diff
changeset
|
428 ;; XEmacs; check LIST for type and circularity. |
428 | 429 (defun tailp (sublist list) |
430 "Return true if SUBLIST is a tail of LIST." | |
5285
99de5fd48e87
Tighten up Common Lisp compatibility, #'butlast, #'nbutlast, #'tailp, #'ldiff
Aidan Kehoe <kehoea@parhasard.net>
parents:
5284
diff
changeset
|
431 (check-argument-type #'listp list) |
99de5fd48e87
Tighten up Common Lisp compatibility, #'butlast, #'nbutlast, #'tailp, #'ldiff
Aidan Kehoe <kehoea@parhasard.net>
parents:
5284
diff
changeset
|
432 (let ((before list) (evenp t)) |
99de5fd48e87
Tighten up Common Lisp compatibility, #'butlast, #'nbutlast, #'tailp, #'ldiff
Aidan Kehoe <kehoea@parhasard.net>
parents:
5284
diff
changeset
|
433 (while (and (consp list) (not (eq sublist list))) |
99de5fd48e87
Tighten up Common Lisp compatibility, #'butlast, #'nbutlast, #'tailp, #'ldiff
Aidan Kehoe <kehoea@parhasard.net>
parents:
5284
diff
changeset
|
434 (setq list (cdr list) |
99de5fd48e87
Tighten up Common Lisp compatibility, #'butlast, #'nbutlast, #'tailp, #'ldiff
Aidan Kehoe <kehoea@parhasard.net>
parents:
5284
diff
changeset
|
435 evenp (not evenp)) |
99de5fd48e87
Tighten up Common Lisp compatibility, #'butlast, #'nbutlast, #'tailp, #'ldiff
Aidan Kehoe <kehoea@parhasard.net>
parents:
5284
diff
changeset
|
436 (if evenp (setq before (cdr before))) |
99de5fd48e87
Tighten up Common Lisp compatibility, #'butlast, #'nbutlast, #'tailp, #'ldiff
Aidan Kehoe <kehoea@parhasard.net>
parents:
5284
diff
changeset
|
437 (if (eq before list) (error 'circular-list list))) |
99de5fd48e87
Tighten up Common Lisp compatibility, #'butlast, #'nbutlast, #'tailp, #'ldiff
Aidan Kehoe <kehoea@parhasard.net>
parents:
5284
diff
changeset
|
438 (eql sublist list))) |
428 | 439 |
2153 | 440 (defalias 'cl-copy-tree 'copy-tree) |
428 | 441 |
442 ;;; Property lists. | |
443 | |
444 ;; XEmacs: our `get' groks DEFAULT. | |
445 (defalias 'get* 'get) | |
442 | 446 (defalias 'getf 'plist-get) |
428 | 447 |
5285
99de5fd48e87
Tighten up Common Lisp compatibility, #'butlast, #'nbutlast, #'tailp, #'ldiff
Aidan Kehoe <kehoea@parhasard.net>
parents:
5284
diff
changeset
|
448 ;; XEmacs; these are built-in. |
99de5fd48e87
Tighten up Common Lisp compatibility, #'butlast, #'nbutlast, #'tailp, #'ldiff
Aidan Kehoe <kehoea@parhasard.net>
parents:
5284
diff
changeset
|
449 (defalias 'cl-set-getf 'plist-put) |
99de5fd48e87
Tighten up Common Lisp compatibility, #'butlast, #'nbutlast, #'tailp, #'ldiff
Aidan Kehoe <kehoea@parhasard.net>
parents:
5284
diff
changeset
|
450 (defalias 'cl-do-remf 'plist-remprop) |
2153 | 451 (defalias 'cl-remprop 'remprop) |
452 | |
4800
b828e06dbe38
New (Common Lisp) function get-propertie
Didier Verna <didier@xemacs.org>
parents:
4792
diff
changeset
|
453 (defun get-properties (plist indicator-list) |
b828e06dbe38
New (Common Lisp) function get-propertie
Didier Verna <didier@xemacs.org>
parents:
4792
diff
changeset
|
454 "Find a property from INDICATOR-LIST in PLIST. |
b828e06dbe38
New (Common Lisp) function get-propertie
Didier Verna <didier@xemacs.org>
parents:
4792
diff
changeset
|
455 Return 3 values: |
b828e06dbe38
New (Common Lisp) function get-propertie
Didier Verna <didier@xemacs.org>
parents:
4792
diff
changeset
|
456 - the first property found, |
b828e06dbe38
New (Common Lisp) function get-propertie
Didier Verna <didier@xemacs.org>
parents:
4792
diff
changeset
|
457 - its value, |
b828e06dbe38
New (Common Lisp) function get-propertie
Didier Verna <didier@xemacs.org>
parents:
4792
diff
changeset
|
458 - the tail of PLIST beginning with the found entry." |
b828e06dbe38
New (Common Lisp) function get-propertie
Didier Verna <didier@xemacs.org>
parents:
4792
diff
changeset
|
459 (do ((plst plist (cddr plst))) |
b828e06dbe38
New (Common Lisp) function get-propertie
Didier Verna <didier@xemacs.org>
parents:
4792
diff
changeset
|
460 ((null plst) (values nil nil nil)) |
b828e06dbe38
New (Common Lisp) function get-propertie
Didier Verna <didier@xemacs.org>
parents:
4792
diff
changeset
|
461 (cond ((atom (cdr plst)) |
b828e06dbe38
New (Common Lisp) function get-propertie
Didier Verna <didier@xemacs.org>
parents:
4792
diff
changeset
|
462 (error "Malformed property list: %S." plist)) |
b828e06dbe38
New (Common Lisp) function get-propertie
Didier Verna <didier@xemacs.org>
parents:
4792
diff
changeset
|
463 ((memq (car plst) indicator-list) |
b828e06dbe38
New (Common Lisp) function get-propertie
Didier Verna <didier@xemacs.org>
parents:
4792
diff
changeset
|
464 (return (values (car plst) (cadr plst) plst)))))) |
2153 | 465 |
5075
868a9ffcc37b
Normally return a compiled function if one argument, #'constantly.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5056
diff
changeset
|
466 ;; See also the compiler macro in cl-macs.el. |
5056
6aba0daedb7c
Add #'constantly, as specified by ANSI Common Lisp.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4997
diff
changeset
|
467 (defun constantly (value &rest more-values) |
6aba0daedb7c
Add #'constantly, as specified by ANSI Common Lisp.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4997
diff
changeset
|
468 "Construct a function always returning VALUE, and possibly MORE-VALUES. |
6aba0daedb7c
Add #'constantly, as specified by ANSI Common Lisp.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4997
diff
changeset
|
469 |
6aba0daedb7c
Add #'constantly, as specified by ANSI Common Lisp.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4997
diff
changeset
|
470 The constructed function accepts any number of arguments, and ignores them. |
6aba0daedb7c
Add #'constantly, as specified by ANSI Common Lisp.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4997
diff
changeset
|
471 |
6aba0daedb7c
Add #'constantly, as specified by ANSI Common Lisp.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4997
diff
changeset
|
472 Members of MORE-VALUES, if provided, will be passed as multiple values; see |
6aba0daedb7c
Add #'constantly, as specified by ANSI Common Lisp.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4997
diff
changeset
|
473 `multiple-value-bind' and `multiple-value-setq'." |
5075
868a9ffcc37b
Normally return a compiled function if one argument, #'constantly.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5056
diff
changeset
|
474 (symbol-macrolet |
868a9ffcc37b
Normally return a compiled function if one argument, #'constantly.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5056
diff
changeset
|
475 ((arglist '(&rest ignore))) |
868a9ffcc37b
Normally return a compiled function if one argument, #'constantly.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5056
diff
changeset
|
476 (if (or more-values (eval-when-compile (not (cl-compiling-file)))) |
868a9ffcc37b
Normally return a compiled function if one argument, #'constantly.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5056
diff
changeset
|
477 `(lambda ,arglist (values-list ',(cons value more-values))) |
868a9ffcc37b
Normally return a compiled function if one argument, #'constantly.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5056
diff
changeset
|
478 (make-byte-code |
868a9ffcc37b
Normally return a compiled function if one argument, #'constantly.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5056
diff
changeset
|
479 arglist |
868a9ffcc37b
Normally return a compiled function if one argument, #'constantly.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5056
diff
changeset
|
480 (eval-when-compile |
868a9ffcc37b
Normally return a compiled function if one argument, #'constantly.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5056
diff
changeset
|
481 (let ((compiled (byte-compile-sexp #'(lambda (&rest ignore) |
868a9ffcc37b
Normally return a compiled function if one argument, #'constantly.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5056
diff
changeset
|
482 (declare (ignore ignore)) |
868a9ffcc37b
Normally return a compiled function if one argument, #'constantly.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5056
diff
changeset
|
483 'placeholder)))) |
868a9ffcc37b
Normally return a compiled function if one argument, #'constantly.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5056
diff
changeset
|
484 (assert (and |
868a9ffcc37b
Normally return a compiled function if one argument, #'constantly.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5056
diff
changeset
|
485 (equal [placeholder] |
868a9ffcc37b
Normally return a compiled function if one argument, #'constantly.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5056
diff
changeset
|
486 (compiled-function-constants compiled)) |
868a9ffcc37b
Normally return a compiled function if one argument, #'constantly.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5056
diff
changeset
|
487 (= 1 (compiled-function-stack-depth compiled))) |
868a9ffcc37b
Normally return a compiled function if one argument, #'constantly.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5056
diff
changeset
|
488 t |
868a9ffcc37b
Normally return a compiled function if one argument, #'constantly.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5056
diff
changeset
|
489 "Our assumptions about compiled code appear not to hold.") |
868a9ffcc37b
Normally return a compiled function if one argument, #'constantly.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5056
diff
changeset
|
490 (compiled-function-instructions compiled))) |
868a9ffcc37b
Normally return a compiled function if one argument, #'constantly.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5056
diff
changeset
|
491 (vector value) 1)))) |
2153 | 492 |
428 | 493 ;;; Hash tables. |
494 | |
495 ;; The `regular' Common Lisp hash-table stuff has been moved into C. | |
496 ;; Only backward compatibility stuff remains here. | |
497 (defun make-hashtable (size &optional test) | |
498 (make-hash-table :test test :size size)) | |
499 (defun make-weak-hashtable (size &optional test) | |
500 (make-hash-table :test test :size size :weakness t)) | |
501 (defun make-key-weak-hashtable (size &optional test) | |
502 (make-hash-table :test test :size size :weakness 'key)) | |
503 (defun make-value-weak-hashtable (size &optional test) | |
504 (make-hash-table :test test :size size :weakness 'value)) | |
505 | |
506 (define-obsolete-function-alias 'hashtablep 'hash-table-p) | |
507 (define-obsolete-function-alias 'hashtable-fullness 'hash-table-count) | |
508 (define-obsolete-function-alias 'hashtable-test-function 'hash-table-test) | |
509 (define-obsolete-function-alias 'hashtable-type 'hash-table-type) | |
510 (define-obsolete-function-alias 'hashtable-size 'hash-table-size) | |
511 (define-obsolete-function-alias 'copy-hashtable 'copy-hash-table) | |
512 | |
513 (make-obsolete 'make-hashtable 'make-hash-table) | |
514 (make-obsolete 'make-weak-hashtable 'make-hash-table) | |
515 (make-obsolete 'make-key-weak-hashtable 'make-hash-table) | |
516 (make-obsolete 'make-value-weak-hashtable 'make-hash-table) | |
517 (make-obsolete 'hash-table-type 'hash-table-weakness) | |
518 | |
519 (when (fboundp 'x-keysym-hash-table) | |
520 (make-obsolete 'x-keysym-hashtable 'x-keysym-hash-table)) | |
521 | |
522 ;; Compatibility stuff for old kludgy cl.el hash table implementation | |
523 (defvar cl-builtin-gethash (symbol-function 'gethash)) | |
524 (defvar cl-builtin-remhash (symbol-function 'remhash)) | |
525 (defvar cl-builtin-clrhash (symbol-function 'clrhash)) | |
526 (defvar cl-builtin-maphash (symbol-function 'maphash)) | |
527 | |
528 (defalias 'cl-gethash 'gethash) | |
529 (defalias 'cl-puthash 'puthash) | |
530 (defalias 'cl-remhash 'remhash) | |
531 (defalias 'cl-clrhash 'clrhash) | |
532 (defalias 'cl-maphash 'maphash) | |
2153 | 533 ;; These three actually didn't exist in Emacs-20. |
534 (defalias 'cl-make-hash-table 'make-hash-table) | |
535 (defalias 'cl-hash-table-p 'hash-table-p) | |
536 (defalias 'cl-hash-table-count 'hash-table-count) | |
428 | 537 |
538 ;;; Some debugging aids. | |
539 | |
540 (defun cl-prettyprint (form) | |
541 "Insert a pretty-printed rendition of a Lisp FORM in current buffer." | |
5162
41262f87eb39
Handle (function ...) specially, cl-prettyprint.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5075
diff
changeset
|
542 (let ((pt (point)) last just) |
428 | 543 (insert "\n" (prin1-to-string form) "\n") |
544 (setq last (point)) | |
545 (goto-char (1+ pt)) | |
5162
41262f87eb39
Handle (function ...) specially, cl-prettyprint.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5075
diff
changeset
|
546 (while (re-search-forward "(\\(?:\\(?:function\\|quote\\) \\)" last t) |
41262f87eb39
Handle (function ...) specially, cl-prettyprint.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5075
diff
changeset
|
547 (delete-region (match-beginning 0) (match-end 0)) |
41262f87eb39
Handle (function ...) specially, cl-prettyprint.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5075
diff
changeset
|
548 (if (= (length "(function ") (- (match-end 0) (match-beginning 0))) |
41262f87eb39
Handle (function ...) specially, cl-prettyprint.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5075
diff
changeset
|
549 (insert "#'") |
41262f87eb39
Handle (function ...) specially, cl-prettyprint.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5075
diff
changeset
|
550 (insert "'")) |
41262f87eb39
Handle (function ...) specially, cl-prettyprint.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5075
diff
changeset
|
551 (setq just (point)) |
428 | 552 (forward-sexp) |
5162
41262f87eb39
Handle (function ...) specially, cl-prettyprint.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5075
diff
changeset
|
553 (delete-char 1) |
41262f87eb39
Handle (function ...) specially, cl-prettyprint.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5075
diff
changeset
|
554 (goto-char just)) |
428 | 555 (goto-char (1+ pt)) |
556 (cl-do-prettyprint))) | |
557 | |
558 (defun cl-do-prettyprint () | |
559 (skip-chars-forward " ") | |
560 (if (looking-at "(") | |
4800
b828e06dbe38
New (Common Lisp) function get-propertie
Didier Verna <didier@xemacs.org>
parents:
4792
diff
changeset
|
561 (let ((skip (or (looking-at "((") |
2153 | 562 ;; XEmacs: be selective about trailing stuff after prog |
1729 | 563 (looking-at "(prog[nv12\\(ress-feedback\\|n-with-message\\)]") |
428 | 564 (looking-at "(unwind-protect ") |
565 (looking-at "(function (") | |
566 (looking-at "(cl-block-wrapper "))) | |
567 (two (or (looking-at "(defun ") (looking-at "(defmacro "))) | |
568 (let (or (looking-at "(let\\*? ") (looking-at "(while "))) | |
569 (set (looking-at "(p?set[qf] "))) | |
570 (if (or skip let | |
571 (progn | |
572 (forward-sexp) | |
573 (and (>= (current-column) 78) (progn (backward-sexp) t)))) | |
574 (let ((nl t)) | |
575 (forward-char 1) | |
576 (cl-do-prettyprint) | |
577 (or skip (looking-at ")") (cl-do-prettyprint)) | |
578 (or (not two) (looking-at ")") (cl-do-prettyprint)) | |
579 (while (not (looking-at ")")) | |
580 (if set (setq nl (not nl))) | |
581 (if nl (insert "\n")) | |
582 (lisp-indent-line) | |
583 (cl-do-prettyprint)) | |
584 (forward-char 1)))) | |
585 (forward-sexp))) | |
586 | |
587 (defvar cl-macroexpand-cmacs nil) | |
588 (defvar cl-closure-vars nil) | |
589 | |
590 (defun cl-macroexpand-all (form &optional env) | |
591 "Expand all macro calls through a Lisp FORM. | |
592 This also does some trivial optimizations to make the form prettier." | |
593 (while (or (not (eq form (setq form (macroexpand form env)))) | |
594 (and cl-macroexpand-cmacs | |
595 (not (eq form (setq form (compiler-macroexpand form))))))) | |
596 (cond ((not (consp form)) form) | |
597 ((memq (car form) '(let let*)) | |
598 (if (null (nth 1 form)) | |
599 (cl-macroexpand-all (cons 'progn (cddr form)) env) | |
600 (let ((letf nil) (res nil) (lets (cadr form))) | |
601 (while lets | |
2153 | 602 (push (if (consp (car lets)) |
428 | 603 (let ((exp (cl-macroexpand-all (caar lets) env))) |
604 (or (symbolp exp) (setq letf t)) | |
605 (cons exp (cl-macroexpand-body (cdar lets) env))) | |
606 (let ((exp (cl-macroexpand-all (car lets) env))) | |
607 (if (symbolp exp) exp | |
608 (setq letf t) (list exp nil)))) res) | |
609 (setq lets (cdr lets))) | |
610 (list* (if letf (if (eq (car form) 'let) 'letf 'letf*) (car form)) | |
611 (nreverse res) (cl-macroexpand-body (cddr form) env))))) | |
612 ((eq (car form) 'cond) | |
613 (cons (car form) | |
614 (mapcar (function (lambda (x) (cl-macroexpand-body x env))) | |
615 (cdr form)))) | |
616 ((eq (car form) 'condition-case) | |
617 (list* (car form) (nth 1 form) (cl-macroexpand-all (nth 2 form) env) | |
618 (mapcar (function | |
619 (lambda (x) | |
620 (cons (car x) (cl-macroexpand-body (cdr x) env)))) | |
621 (cdddr form)))) | |
622 ((memq (car form) '(quote function)) | |
623 (if (eq (car-safe (nth 1 form)) 'lambda) | |
624 (let ((body (cl-macroexpand-body (cddadr form) env))) | |
625 (if (and cl-closure-vars (eq (car form) 'function) | |
626 (cl-expr-contains-any body cl-closure-vars)) | |
627 (let* ((new (mapcar 'gensym cl-closure-vars)) | |
628 (sub (pairlis cl-closure-vars new)) (decls nil)) | |
629 (while (or (stringp (car body)) | |
630 (eq (car-safe (car body)) 'interactive)) | |
2153 | 631 (push (list 'quote (pop body)) decls)) |
428 | 632 (put (car (last cl-closure-vars)) 'used t) |
633 (append | |
634 (list 'list '(quote lambda) '(quote (&rest --cl-rest--))) | |
635 (sublis sub (nreverse decls)) | |
636 (list | |
637 (list* 'list '(quote apply) | |
2153 | 638 ;; XEmacs: put a quote before the function |
428 | 639 (list 'list '(quote quote) |
640 (list 'function | |
641 (list* 'lambda | |
642 (append new (cadadr form)) | |
643 (sublis sub body)))) | |
644 (nconc (mapcar (function | |
645 (lambda (x) | |
646 (list 'list '(quote quote) x))) | |
647 cl-closure-vars) | |
648 '((quote --cl-rest--))))))) | |
649 (list (car form) (list* 'lambda (cadadr form) body)))) | |
650 (let ((found (assq (cadr form) env))) | |
2153 | 651 ;; XEmacs: cadr/caddr operate on nil without errors |
428 | 652 (if (eq (cadr (caddr found)) 'cl-labels-args) |
653 (cl-macroexpand-all (cadr (caddr (cadddr found))) env) | |
654 form)))) | |
655 ((memq (car form) '(defun defmacro)) | |
656 (list* (car form) (nth 1 form) (cl-macroexpand-body (cddr form) env))) | |
657 ((and (eq (car form) 'progn) (not (cddr form))) | |
658 (cl-macroexpand-all (nth 1 form) env)) | |
659 ((eq (car form) 'setq) | |
660 (let* ((args (cl-macroexpand-body (cdr form) env)) (p args)) | |
661 (while (and p (symbolp (car p))) (setq p (cddr p))) | |
662 (if p (cl-macroexpand-all (cons 'setf args)) (cons 'setq args)))) | |
663 (t (cons (car form) (cl-macroexpand-body (cdr form) env))))) | |
664 | |
665 (defun cl-macroexpand-body (body &optional env) | |
666 (mapcar (function (lambda (x) (cl-macroexpand-all x env))) body)) | |
667 | |
668 (defun cl-prettyexpand (form &optional full) | |
669 (message "Expanding...") | |
670 (let ((cl-macroexpand-cmacs full) (cl-compiling-file full) | |
671 (byte-compile-macro-environment nil)) | |
672 (setq form (cl-macroexpand-all form | |
673 (and (not full) '((block) (eval-when))))) | |
674 (message "Formatting...") | |
675 (prog1 (cl-prettyprint form) | |
676 (message "")))) | |
677 | |
5284
d27c1ee1943b
Make the order of preloaded-file-list more sane.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5273
diff
changeset
|
678 ;; XEmacs addition; force cl-macs to be available from here on when |
d27c1ee1943b
Make the order of preloaded-file-list more sane.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5273
diff
changeset
|
679 ;; compiling files to be dumped. This is more reasonable than forcing other |
d27c1ee1943b
Make the order of preloaded-file-list more sane.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5273
diff
changeset
|
680 ;; files to do the same, multiple times. |
d27c1ee1943b
Make the order of preloaded-file-list more sane.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5273
diff
changeset
|
681 (eval-when-compile (or (cl-compiling-file) (load "cl-macs"))) |
d27c1ee1943b
Make the order of preloaded-file-list more sane.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5273
diff
changeset
|
682 |
428 | 683 (run-hooks 'cl-extra-load-hook) |
684 | |
2153 | 685 ;; XEmacs addition |
428 | 686 (provide 'cl-extra) |
687 | |
2153 | 688 ;;; arch-tag: bcd03437-0871-43fb-a8f1-ad0e0b5427ed |
428 | 689 ;;; cl-extra.el ends here |