Mercurial > emacs
annotate lisp/=cl.el @ 927:587a08c3a70b
*** empty log message ***
author | Jim Blandy <jimb@redhat.com> |
---|---|
date | Tue, 04 Aug 1992 07:27:12 +0000 |
parents | 52cd80cb5be1 |
children | 2619b7a9c11e |
rev | line source |
---|---|
662
8a533acedb77
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
584
diff
changeset
|
1 ;;; cl.el --- Common-Lisp extensions for GNU Emacs Lisp. |
8a533acedb77
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
584
diff
changeset
|
2 |
846
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
3 ;; Copyright (C) 1987, 1988, 1989 Free Software Foundation, Inc. |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
4 |
798
b7932f859d4e
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
662
diff
changeset
|
5 ;; Author: Cesar Quiroz <quiroz@cs.rochester.edu> |
b7932f859d4e
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
662
diff
changeset
|
6 ;; Keywords: extensions |
b7932f859d4e
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
662
diff
changeset
|
7 |
b7932f859d4e
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
662
diff
changeset
|
8 (defvar cl-version "2.0 beta 29 October 1989") |
b7932f859d4e
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
662
diff
changeset
|
9 |
188 | 10 ;; This file is part of GNU Emacs. |
11 | |
12 ;; GNU Emacs is distributed in the hope that it will be useful, | |
13 ;; but WITHOUT ANY WARRANTY. No author or distributor | |
14 ;; accepts responsibility to anyone for the consequences of using it | |
15 ;; or for whether it serves any particular purpose or works at all, | |
16 ;; unless he says so in writing. Refer to the GNU Emacs General Public | |
17 ;; License for full details. | |
18 | |
19 ;; Everyone is granted permission to copy, modify and redistribute | |
20 ;; GNU Emacs, but only under the conditions described in the | |
21 ;; GNU Emacs General Public License. A copy of this license is | |
22 ;; supposed to have been given to you along with GNU Emacs so you | |
23 ;; can know your rights and responsibilities. It should be in a | |
24 ;; file named COPYING. Among other things, the copyright notice | |
25 ;; and this notice must be preserved on all copies. | |
26 | |
798
b7932f859d4e
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
662
diff
changeset
|
27 ;;; Commentary: |
b7932f859d4e
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
662
diff
changeset
|
28 |
188 | 29 ;;;; These are extensions to Emacs Lisp that provide some form of |
30 ;;;; Common Lisp compatibility, beyond what is already built-in | |
31 ;;;; in Emacs Lisp. | |
32 ;;;; | |
33 ;;;; When developing them, I had the code spread among several files. | |
34 ;;;; This file 'cl.el' is a concatenation of those original files, | |
35 ;;;; minus some declarations that became redundant. The marks between | |
36 ;;;; the original files can be found easily, as they are lines that | |
37 ;;;; begin with four semicolons (as this does). The names of the | |
38 ;;;; original parts follow the four semicolons in uppercase, those | |
39 ;;;; names are GLOBAL, SYMBOLS, LISTS, SEQUENCES, CONDITIONALS, | |
40 ;;;; ITERATIONS, MULTIPLE VALUES, ARITH, SETF and DEFSTRUCT. If you | |
41 ;;;; add functions to this file, you might want to put them in a place | |
42 ;;;; that is compatible with the division above (or invent your own | |
43 ;;;; categories). | |
44 ;;;; | |
45 ;;;; To compile this file, make sure you load it first. This is | |
46 ;;;; because many things are implemented as macros and now that all | |
47 ;;;; the files are concatenated together one cannot ensure that | |
48 ;;;; declaration always precedes use. | |
49 ;;;; | |
50 | |
51 ;;;; GLOBAL | |
52 ;;;; This file provides utilities and declarations that are global | |
53 ;;;; to Common Lisp and so might be used by more than one of the | |
54 ;;;; other libraries. Especially, I intend to keep here some | |
55 ;;;; utilities that help parsing/destructuring some difficult calls. | |
56 ;;;; | |
57 ;;;; | |
58 ;;;; Cesar Quiroz @ UofR DofCSc - Dec. 1986 | |
59 ;;;; (quiroz@cs.rochester.edu) | |
60 | |
61 ;;; Too many pieces of the rest of this package use psetq. So it is unwise to | |
62 ;;; use here anything but plain Emacs Lisp! There is a neater recursive form | |
63 ;;; for the algorithm that deals with the bodies. | |
64 | |
798
b7932f859d4e
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
662
diff
changeset
|
65 ;;; Code: |
b7932f859d4e
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
662
diff
changeset
|
66 |
188 | 67 (defmacro psetq (&rest body) |
68 "(psetq {var value }...) => nil | |
69 Like setq, but all the values are computed before any assignment is made." | |
70 (let ((length (length body))) | |
71 (cond ((/= (% length 2) 0) | |
72 (error "psetq needs an even number of arguments, %d given" | |
73 length)) | |
74 ((null body) | |
75 '()) | |
76 (t | |
77 (list 'prog1 nil | |
78 (let ((setqs '()) | |
79 (bodyforms (reverse body))) | |
80 (while bodyforms | |
81 (let* ((value (car bodyforms)) | |
82 (place (cadr bodyforms))) | |
83 (setq bodyforms (cddr bodyforms)) | |
84 (if (null setqs) | |
85 (setq setqs (list 'setq place value)) | |
86 (setq setqs (list 'setq place | |
87 (list 'prog1 value | |
88 setqs)))))) | |
89 setqs)))))) | |
90 | |
91 ;;; utilities | |
92 ;;; | |
93 ;;; pair-with-newsyms takes a list and returns a list of lists of the | |
94 ;;; form (newsym form), such that a let* can then bind the evaluation | |
95 ;;; of the forms to the newsyms. The idea is to guarantee correct | |
96 ;;; order of evaluation of the subforms of a setf. It also returns a | |
97 ;;; list of the newsyms generated, in the corresponding order. | |
98 | |
99 (defun pair-with-newsyms (oldforms) | |
100 "PAIR-WITH-NEWSYMS OLDFORMS | |
101 The top-level components of the list oldforms are paired with fresh | |
102 symbols, the pairings list and the newsyms list are returned." | |
103 (do ((ptr oldforms (cdr ptr)) | |
104 (bindings '()) | |
105 (newsyms '())) | |
106 ((endp ptr) (values (nreverse bindings) (nreverse newsyms))) | |
107 (let ((newsym (gentemp))) | |
108 (setq bindings (cons (list newsym (car ptr)) bindings)) | |
109 (setq newsyms (cons newsym newsyms))))) | |
110 | |
111 (defun zip-lists (evens odds) | |
112 "Merge two lists EVENS and ODDS, taking elts from each list alternatingly. | |
113 EVENS and ODDS are two lists. ZIP-LISTS constructs a new list, whose | |
114 even numbered elements (0,2,...) come from EVENS and whose odd numbered | |
115 elements (1,3,...) come from ODDS. | |
116 The construction stops when the shorter list is exhausted." | |
117 (do* ((p0 evens (cdr p0)) | |
118 (p1 odds (cdr p1)) | |
119 (even (car p0) (car p0)) | |
120 (odd (car p1) (car p1)) | |
121 (result '())) | |
122 ((or (endp p0) (endp p1)) | |
123 (nreverse result)) | |
124 (setq result | |
125 (cons odd (cons even result))))) | |
126 | |
127 (defun unzip-list (list) | |
128 "Extract even and odd elements of LIST into two separate lists. | |
129 The argument LIST is separated in two strands, the even and the odd | |
130 numbered elements. Numbering starts with 0, so the first element | |
131 belongs in EVENS. No check is made that there is an even number of | |
132 elements to start with." | |
133 (do* ((ptr list (cddr ptr)) | |
134 (this (car ptr) (car ptr)) | |
135 (next (cadr ptr) (cadr ptr)) | |
136 (evens '()) | |
137 (odds '())) | |
138 ((endp ptr) | |
139 (values (nreverse evens) (nreverse odds))) | |
140 (setq evens (cons this evens)) | |
141 (setq odds (cons next odds)))) | |
142 | |
143 (defun reassemble-argslists (argslists) | |
144 "(reassemble-argslists ARGSLISTS) => a list of lists | |
145 ARGSLISTS is a list of sequences. Return a list of lists, the first | |
146 sublist being all the entries coming from ELT 0 of the original | |
147 sublists, the next those coming from ELT 1 and so on, until the | |
148 shortest list is exhausted." | |
149 (let* ((minlen (apply 'min (mapcar 'length argslists))) | |
150 (result '())) | |
151 (dotimes (i minlen (nreverse result)) | |
152 ;; capture all the elements at index i | |
153 (setq result | |
154 (cons (mapcar (function (lambda (sublist) (elt sublist i))) | |
155 argslists) | |
156 result))))) | |
157 | |
158 | |
159 ;;; Checking that a list of symbols contains no duplicates is a common | |
160 ;;; task when checking the legality of some macros. The check for 'eq | |
161 ;;; pairs can be too expensive, as it is quadratic on the length of | |
162 ;;; the list. I use a 4-pass, linear, counting approach. It surely | |
163 ;;; loses on small lists (less than 5 elements?), but should win for | |
164 ;;; larger lists. The fourth pass could be eliminated. | |
165 ;;; 10 dec 1986. Emacs Lisp has no REMPROP, so I just eliminated the | |
166 ;;; 4th pass. | |
167 (defun duplicate-symbols-p (list) | |
168 "Find all symbols appearing more than once in LIST. | |
169 Return a list of all such duplicates; nil if there are no duplicates." | |
170 (let ((duplicates '()) ;result built here | |
171 (propname (gensym)) ;we use a fresh property | |
172 ) | |
173 ;; check validity | |
174 (unless (and (listp list) | |
175 (every 'symbolp list)) | |
176 (error "a list of symbols is needed")) | |
177 ;; pass 1: mark | |
178 (dolist (x list) | |
179 (put x propname 0)) | |
180 ;; pass 2: count | |
181 (dolist (x list) | |
182 (put x propname (1+ (get x propname)))) | |
183 ;; pass 3: collect | |
184 (dolist (x list) | |
185 (if (> (get x propname) 1) | |
186 (setq duplicates (cons x duplicates)))) | |
187 ;; pass 4: unmark. eliminated. | |
188 ;; (dolist (x list) (remprop x propname)) | |
189 ;; return result | |
190 duplicates)) | |
191 | |
192 ;;;; end of cl-global.el | |
193 | |
194 ;;;; SYMBOLS | |
195 ;;;; This file provides the gentemp function, which generates fresh | |
196 ;;;; symbols, plus some other minor Common Lisp symbol tools. | |
197 ;;;; | |
198 ;;;; Cesar Quiroz @ UofR DofCSc - Dec. 1986 | |
199 ;;;; (quiroz@cs.rochester.edu) | |
200 | |
201 ;;; Keywords. There are no packages in Emacs Lisp, so this is only a | |
202 ;;; kludge around to let things be "as if" a keyword package was around. | |
203 | |
204 (defmacro defkeyword (x &optional docstring) | |
205 "Make symbol X a keyword (symbol whose value is itself). | |
206 Optional second arg DOCSTRING is a documentation string for it." | |
207 (cond ((symbolp x) | |
208 (list 'defconst x (list 'quote x) docstring)) | |
209 (t | |
210 (error "`%s' is not a symbol" (prin1-to-string x))))) | |
211 | |
212 (defun keywordp (sym) | |
213 "Return t if SYM is a keyword." | |
214 (if (and (symbolp sym) (char-equal (aref (symbol-name sym) 0) ?\:)) | |
215 ;; looks like one, make sure value is right | |
216 (set sym sym) | |
217 nil)) | |
218 | |
219 (defun keyword-of (sym) | |
220 "Return a keyword that is naturally associated with symbol SYM. | |
221 If SYM is keyword, the value is SYM. | |
222 Otherwise it is a keyword whose name is `:' followed by SYM's name." | |
223 (cond ((keywordp sym) | |
224 sym) | |
225 ((symbolp sym) | |
226 (let ((newsym (intern (concat ":" (symbol-name sym))))) | |
227 (set newsym newsym))) | |
228 (t | |
229 (error "expected a symbol, not `%s'" (prin1-to-string sym))))) | |
230 | |
231 ;;; Temporary symbols. | |
232 ;;; | |
233 | |
234 (defvar *gentemp-index* 0 | |
235 "Integer used by `gentemp' to produce new names.") | |
236 | |
237 (defvar *gentemp-prefix* "T$$_" | |
238 "Names generated by `gentemp begin' with this string by default.") | |
239 | |
240 (defun gentemp (&optional prefix oblist) | |
241 "Generate a fresh interned symbol. | |
242 There are two optional arguments, PREFIX and OBLIST. PREFIX is the string | |
243 that begins the new name, OBLIST is the obarray used to search for old | |
244 names. The defaults are just right, YOU SHOULD NEVER NEED THESE ARGUMENTS | |
245 IN YOUR OWN CODE." | |
246 (if (null prefix) | |
247 (setq prefix *gentemp-prefix*)) | |
248 (if (null oblist) | |
249 (setq oblist obarray)) ;default for the intern functions | |
250 (let ((newsymbol nil) | |
251 (newname)) | |
252 (while (not newsymbol) | |
253 (setq newname (concat prefix *gentemp-index*)) | |
254 (setq *gentemp-index* (+ *gentemp-index* 1)) | |
255 (if (not (intern-soft newname oblist)) | |
256 (setq newsymbol (intern newname oblist)))) | |
257 newsymbol)) | |
258 | |
259 (defvar *gensym-index* 0 | |
260 "Integer used by `gensym' to produce new names.") | |
261 | |
262 (defvar *gensym-prefix* "G$$_" | |
263 "Names generated by `gensym' begin with this string by default.") | |
264 | |
265 (defun gensym (&optional prefix) | |
266 "Generate a fresh uninterned symbol. | |
267 Optional arg PREFIX is the string that begins the new name. Most people | |
268 take just the default, except when debugging needs suggest otherwise." | |
269 (if (null prefix) | |
270 (setq prefix *gensym-prefix*)) | |
271 (let ((newsymbol nil) | |
272 (newname "")) | |
273 (while (not newsymbol) | |
274 (setq newname (concat prefix *gensym-index*)) | |
275 (setq *gensym-index* (+ *gensym-index* 1)) | |
276 (if (not (intern-soft newname)) | |
277 (setq newsymbol (make-symbol newname)))) | |
278 newsymbol)) | |
279 | |
280 ;;;; end of cl-symbols.el | |
281 | |
282 ;;;; CONDITIONALS | |
283 ;;;; This file provides some of the conditional constructs of | |
284 ;;;; Common Lisp. Total compatibility is again impossible, as the | |
285 ;;;; 'if' form is different in both languages, so only a good | |
286 ;;;; approximation is desired. | |
287 ;;;; | |
288 ;;;; Cesar Quiroz @ UofR DofCSc - Dec. 1986 | |
289 ;;;; (quiroz@cs.rochester.edu) | |
290 | |
291 ;;; indentation info | |
292 (put 'case 'lisp-indent-function 1) | |
293 (put 'ecase 'lisp-indent-function 1) | |
294 (put 'when 'lisp-indent-function 1) | |
295 (put 'unless 'lisp-indent-function 1) | |
296 | |
297 ;;; WHEN and UNLESS | |
298 ;;; These two forms are simplified ifs, with a single branch. | |
299 | |
300 (defmacro when (condition &rest body) | |
301 "(when CONDITION . BODY) => evaluate BODY if CONDITION is true." | |
302 (list* 'if (list 'not condition) '() body)) | |
303 | |
304 (defmacro unless (condition &rest body) | |
305 "(unless CONDITION . BODY) => evaluate BODY if CONDITION is false." | |
306 (list* 'if condition '() body)) | |
307 | |
308 ;;; CASE and ECASE | |
309 ;;; CASE selects among several clauses, based on the value (evaluated) | |
310 ;;; of a expression and a list of (unevaluated) key values. ECASE is | |
311 ;;; the same, but signals an error if no clause is activated. | |
312 | |
313 (defmacro case (expr &rest cases) | |
314 "(case EXPR . CASES) => evals EXPR, chooses from CASES on that value. | |
315 EXPR -> any form | |
316 CASES -> list of clauses, non empty | |
317 CLAUSE -> HEAD . BODY | |
318 HEAD -> t = catch all, must be last clause | |
319 -> otherwise = same as t | |
320 -> nil = illegal | |
321 -> atom = activated if (eql EXPR HEAD) | |
322 -> list of atoms = activated if (memq EXPR HEAD) | |
323 BODY -> list of forms, implicit PROGN is built around it. | |
324 EXPR is evaluated only once." | |
325 (let* ((newsym (gentemp)) | |
326 (clauses (case-clausify cases newsym))) | |
327 ;; convert case into a cond inside a let | |
328 (list 'let | |
329 (list (list newsym expr)) | |
330 (list* 'cond (nreverse clauses))))) | |
331 | |
332 (defmacro ecase (expr &rest cases) | |
333 "(ecase EXPR . CASES) => like `case', but error if no case fits. | |
334 `t'-clauses are not allowed." | |
335 (let* ((newsym (gentemp)) | |
336 (clauses (case-clausify cases newsym))) | |
337 ;; check that no 't clause is present. | |
338 ;; case-clausify would put one such at the beginning of clauses | |
339 (if (eq (caar clauses) t) | |
340 (error "no clause-head should be `t' or `otherwise' for `ecase'")) | |
341 ;; insert error-catching clause | |
342 (setq clauses | |
343 (cons | |
344 (list 't (list 'error | |
345 "ecase on %s = %s failed to take any branch" | |
346 (list 'quote expr) | |
347 (list 'prin1-to-string newsym))) | |
348 clauses)) | |
349 ;; generate code as usual | |
350 (list 'let | |
351 (list (list newsym expr)) | |
352 (list* 'cond (nreverse clauses))))) | |
353 | |
354 | |
355 (defun case-clausify (cases newsym) | |
356 "CASE-CLAUSIFY CASES NEWSYM => clauses for a 'cond' | |
357 Converts the CASES of a [e]case macro into cond clauses to be | |
358 evaluated inside a let that binds NEWSYM. Returns the clauses in | |
359 reverse order." | |
360 (do* ((currentpos cases (cdr currentpos)) | |
361 (nextpos (cdr cases) (cdr nextpos)) | |
362 (curclause (car cases) (car currentpos)) | |
363 (result '())) | |
364 ((endp currentpos) result) | |
365 (let ((head (car curclause)) | |
366 (body (cdr curclause))) | |
367 ;; construct a cond-clause according to the head | |
368 (cond ((null head) | |
369 (error "case clauses cannot have null heads: `%s'" | |
370 (prin1-to-string curclause))) | |
371 ((or (eq head 't) | |
372 (eq head 'otherwise)) | |
373 ;; check it is the last clause | |
374 (if (not (endp nextpos)) | |
375 (error "clause with `t' or `otherwise' head must be last")) | |
376 ;; accept this clause as a 't' for cond | |
377 (setq result (cons (cons 't body) result))) | |
378 ((atom head) | |
379 (setq result | |
380 (cons (cons (list 'eql newsym (list 'quote head)) body) | |
381 result))) | |
382 ((listp head) | |
383 (setq result | |
384 (cons (cons (list 'memq newsym (list 'quote head)) body) | |
385 result))) | |
386 (t | |
387 ;; catch-all for this parser | |
388 (error "don't know how to parse case clause `%s'" | |
389 (prin1-to-string head))))))) | |
390 | |
391 ;;;; end of cl-conditionals.el | |
392 | |
393 ;;;; ITERATIONS | |
394 ;;;; This file provides simple iterative macros (a la Common Lisp) | |
395 ;;;; constructed on the basis of let, let* and while, which are the | |
396 ;;;; primitive binding/iteration constructs of Emacs Lisp | |
397 ;;;; | |
398 ;;;; The Common Lisp iterations use to have a block named nil | |
399 ;;;; wrapped around them, and allow declarations at the beginning | |
400 ;;;; of their bodies and you can return a value using (return ...). | |
401 ;;;; Nothing of the sort exists in Emacs Lisp, so I haven't tried | |
402 ;;;; to imitate these behaviors. | |
403 ;;;; | |
404 ;;;; Other than the above, the semantics of Common Lisp are | |
405 ;;;; correctly reproduced to the extent this was reasonable. | |
406 ;;;; | |
407 ;;;; Cesar Quiroz @ UofR DofCSc - Dec. 1986 | |
408 ;;;; (quiroz@cs.rochester.edu) | |
409 | |
410 ;;; some lisp-indentation information | |
411 (put 'do 'lisp-indent-function 2) | |
412 (put 'do* 'lisp-indent-function 2) | |
413 (put 'dolist 'lisp-indent-function 1) | |
414 (put 'dotimes 'lisp-indent-function 1) | |
415 (put 'do-symbols 'lisp-indent-function 1) | |
416 (put 'do-all-symbols 'lisp-indent-function 1) | |
417 | |
418 | |
419 (defmacro do (stepforms endforms &rest body) | |
420 "(do STEPFORMS ENDFORMS . BODY): Iterate BODY, stepping some local | |
421 variables. STEPFORMS must be a list of symbols or lists. In the second | |
422 case, the lists must start with a symbol and contain up to two more forms. | |
423 In the STEPFORMS, a symbol is the same as a (symbol). The other two forms | |
424 are the initial value (def. NIL) and the form to step (def. itself). | |
425 | |
426 The values used by initialization and stepping are computed in parallel. | |
427 The ENDFORMS are a list (CONDITION . ENDBODY). If the CONDITION evaluates | |
428 to true in any iteration, ENDBODY is evaluated and the last form in it is | |
429 returned. | |
430 | |
431 The BODY (which may be empty) is evaluated at every iteration, with the | |
432 symbols of the STEPFORMS bound to the initial or stepped values." | |
433 | |
434 ;; check the syntax of the macro | |
435 (and (check-do-stepforms stepforms) | |
436 (check-do-endforms endforms)) | |
437 ;; construct emacs-lisp equivalent | |
438 (let ((initlist (extract-do-inits stepforms)) | |
439 (steplist (extract-do-steps stepforms)) | |
440 (endcond (car endforms)) | |
441 (endbody (cdr endforms))) | |
442 (cons 'let (cons initlist | |
443 (cons (cons 'while (cons (list 'not endcond) | |
444 (append body steplist))) | |
445 (append endbody)))))) | |
446 | |
447 | |
448 (defmacro do* (stepforms endforms &rest body) | |
449 "`do*' is to `do' as `let*' is to `let'. | |
450 STEPFORMS must be a list of symbols or lists. In the second case, the | |
451 lists must start with a symbol and contain up to two more forms. In the | |
452 STEPFORMS, a symbol is the same as a (symbol). The other two forms are | |
453 the initial value (def. NIL) and the form to step (def. itself). | |
454 | |
455 Initializations and steppings are done in the sequence they are written. | |
456 | |
457 The ENDFORMS are a list (CONDITION . ENDBODY). If the CONDITION evaluates | |
458 to true in any iteration, ENDBODY is evaluated and the last form in it is | |
459 returned. | |
460 | |
461 The BODY (which may be empty) is evaluated at every iteration, with | |
462 the symbols of the STEPFORMS bound to the initial or stepped values." | |
463 ;; check the syntax of the macro | |
464 (and (check-do-stepforms stepforms) | |
465 (check-do-endforms endforms)) | |
466 ;; construct emacs-lisp equivalent | |
467 (let ((initlist (extract-do-inits stepforms)) | |
468 (steplist (extract-do*-steps stepforms)) | |
469 (endcond (car endforms)) | |
470 (endbody (cdr endforms))) | |
471 (cons 'let* (cons initlist | |
472 (cons (cons 'while (cons (list 'not endcond) | |
473 (append body steplist))) | |
474 (append endbody)))))) | |
475 | |
476 | |
477 ;;; DO and DO* share the syntax checking functions that follow. | |
478 | |
479 (defun check-do-stepforms (forms) | |
480 "True if FORMS is a valid stepforms for the do[*] macro (q.v.)" | |
481 (if (nlistp forms) | |
482 (error "init/step form for do[*] should be a list, not `%s'" | |
483 (prin1-to-string forms)) | |
484 (mapcar | |
485 (function | |
486 (lambda (entry) | |
487 (if (not (or (symbolp entry) | |
488 (and (listp entry) | |
489 (symbolp (car entry)) | |
490 (< (length entry) 4)))) | |
491 (error "init/step must be %s, not `%s'" | |
492 "symbol or (symbol [init [step]])" | |
493 (prin1-to-string entry))))) | |
494 forms))) | |
495 | |
496 (defun check-do-endforms (forms) | |
497 "True if FORMS is a valid endforms for the do[*] macro (q.v.)" | |
498 (if (nlistp forms) | |
499 (error "termination form for do macro should be a list, not `%s'" | |
500 (prin1-to-string forms)))) | |
501 | |
502 (defun extract-do-inits (forms) | |
503 "Returns a list of the initializations (for do) in FORMS | |
504 (a stepforms, see the do macro). | |
505 FORMS is assumed syntactically valid." | |
506 (mapcar | |
507 (function | |
508 (lambda (entry) | |
509 (cond ((symbolp entry) | |
510 (list entry nil)) | |
511 ((listp entry) | |
512 (list (car entry) (cadr entry)))))) | |
513 forms)) | |
514 | |
515 ;;; There used to be a reason to deal with DO differently than with | |
516 ;;; DO*. The writing of PSETQ has made it largely unnecessary. | |
517 | |
518 (defun extract-do-steps (forms) | |
519 "EXTRACT-DO-STEPS FORMS => an s-expr. | |
520 FORMS is the stepforms part of a DO macro (q.v.). This function constructs | |
521 an s-expression that does the stepping at the end of an iteration." | |
522 (list (cons 'psetq (select-stepping-forms forms)))) | |
523 | |
524 (defun extract-do*-steps (forms) | |
525 "EXTRACT-DO*-STEPS FORMS => an s-expr. | |
526 FORMS is the stepforms part of a DO* macro (q.v.). This function constructs | |
527 an s-expression that does the stepping at the end of an iteration." | |
528 (list (cons 'setq (select-stepping-forms forms)))) | |
529 | |
530 (defun select-stepping-forms (forms) | |
531 "Separate only the forms that cause stepping." | |
532 (let ((result '()) ;ends up being (... var form ...) | |
533 (ptr forms) ;to traverse the forms | |
534 entry ;to explore each form in turn | |
535 ) | |
536 (while ptr ;(not (endp entry)) might be safer | |
537 (setq entry (car ptr)) | |
538 (cond ((and (listp entry) (= (length entry) 3)) | |
539 (setq result (append ;append in reverse order! | |
540 (list (caddr entry) (car entry)) | |
541 result)))) | |
542 (setq ptr (cdr ptr))) ;step in the list of forms | |
543 (nreverse result))) | |
544 | |
545 ;;; Other iterative constructs | |
546 | |
547 (defmacro dolist (stepform &rest body) | |
548 "(dolist (VAR LIST [RESULTFORM]) . BODY): do BODY for each elt of LIST. | |
549 The RESULTFORM defaults to nil. The VAR is bound to successive elements | |
550 of the value of LIST and remains bound (to the nil value) when the | |
551 RESULTFORM is evaluated." | |
552 ;; check sanity | |
553 (cond | |
554 ((nlistp stepform) | |
555 (error "stepform for `dolist' should be (VAR LIST [RESULT]), not `%s'" | |
556 (prin1-to-string stepform))) | |
557 ((not (symbolp (car stepform))) | |
558 (error "first component of stepform should be a symbol, not `%s'" | |
559 (prin1-to-string (car stepform)))) | |
560 ((> (length stepform) 3) | |
561 (error "too many components in stepform `%s'" | |
562 (prin1-to-string stepform)))) | |
563 ;; generate code | |
564 (let* ((var (car stepform)) | |
565 (listform (cadr stepform)) | |
566 (resultform (caddr stepform))) | |
567 (list 'progn | |
568 (list 'mapcar | |
569 (list 'function | |
570 (cons 'lambda (cons (list var) body))) | |
571 listform) | |
572 (list 'let | |
573 (list (list var nil)) | |
574 resultform)))) | |
575 | |
576 (defmacro dotimes (stepform &rest body) | |
577 "(dotimes (VAR COUNTFORM [RESULTFORM]) . BODY): Repeat BODY, counting in VAR. | |
578 The COUNTFORM should return a positive integer. The VAR is bound to | |
579 successive integers from 0 to COUNTFORM - 1 and the BODY is repeated for | |
580 each of them. At the end, the RESULTFORM is evaluated and its value | |
581 returned. During this last evaluation, the VAR is still bound, and its | |
582 value is the number of times the iteration occurred. An omitted RESULTFORM | |
583 defaults to nil." | |
584 ;; check sanity | |
585 (cond | |
586 ((nlistp stepform) | |
587 (error "stepform for `dotimes' should be (VAR COUNT [RESULT]), not `%s'" | |
588 (prin1-to-string stepform))) | |
589 ((not (symbolp (car stepform))) | |
590 (error "first component of stepform should be a symbol, not `%s'" | |
591 (prin1-to-string (car stepform)))) | |
592 ((> (length stepform) 3) | |
593 (error "too many components in stepform `%s'" | |
594 (prin1-to-string stepform)))) | |
595 ;; generate code | |
596 (let* ((var (car stepform)) | |
597 (countform (cadr stepform)) | |
598 (resultform (caddr stepform)) | |
599 (newsym (gentemp))) | |
600 (list | |
601 'let* (list (list newsym countform)) | |
602 (list* | |
603 'do* | |
604 (list (list var 0 (list '+ var 1))) | |
605 (list (list '>= var newsym) resultform) | |
606 body)))) | |
607 | |
608 (defmacro do-symbols (stepform &rest body) | |
609 "(do_symbols (VAR [OBARRAY [RESULTFORM]]) . BODY) | |
610 The VAR is bound to each of the symbols in OBARRAY (def. obarray) and | |
611 the BODY is repeatedly performed for each of those bindings. At the | |
612 end, RESULTFORM (def. nil) is evaluated and its value returned. | |
613 During this last evaluation, the VAR is still bound and its value is nil. | |
614 See also the function `mapatoms'." | |
615 ;; check sanity | |
616 (cond | |
617 ((nlistp stepform) | |
618 (error "stepform for `do-symbols' should be (VAR OBARRAY [RESULT]), not `%s'" | |
619 (prin1-to-string stepform))) | |
620 ((not (symbolp (car stepform))) | |
621 (error "first component of stepform should be a symbol, not `%s'" | |
622 (prin1-to-string (car stepform)))) | |
623 ((> (length stepform) 3) | |
624 (error "too many components in stepform `%s'" | |
625 (prin1-to-string stepform)))) | |
626 ;; generate code | |
627 (let* ((var (car stepform)) | |
628 (oblist (cadr stepform)) | |
629 (resultform (caddr stepform))) | |
630 (list 'progn | |
631 (list 'mapatoms | |
632 (list 'function | |
633 (cons 'lambda (cons (list var) body))) | |
634 oblist) | |
635 (list 'let | |
636 (list (list var nil)) | |
637 resultform)))) | |
638 | |
639 | |
640 (defmacro do-all-symbols (stepform &rest body) | |
641 "(do-all-symbols (VAR [RESULTFORM]) . BODY) | |
642 Is the same as (do-symbols (VAR obarray RESULTFORM) . BODY)." | |
643 (list* | |
644 'do-symbols | |
645 (list (car stepform) 'obarray (cadr stepform)) | |
646 body)) | |
647 | |
648 (defmacro loop (&rest body) | |
649 "(loop . BODY) repeats BODY indefinitely and does not return. | |
650 Normally BODY uses `throw' or `signal' to cause an exit. | |
651 The forms in BODY should be lists, as non-lists are reserved for new features." | |
652 ;; check that the body doesn't have atomic forms | |
653 (if (nlistp body) | |
654 (error "body of `loop' should be a list of lists or nil") | |
655 ;; ok, it is a list, check for atomic components | |
656 (mapcar | |
657 (function (lambda (component) | |
658 (if (nlistp component) | |
659 (error "components of `loop' should be lists")))) | |
660 body) | |
661 ;; build the infinite loop | |
662 (cons 'while (cons 't body)))) | |
663 | |
664 ;;;; end of cl-iterations.el | |
665 | |
666 ;;;; LISTS | |
667 ;;;; This file provides some of the lists machinery of Common-Lisp | |
668 ;;;; in a way compatible with Emacs Lisp. Especially, see the the | |
669 ;;;; typical c[ad]*r functions. | |
670 ;;;; | |
671 ;;;; Cesar Quiroz @ UofR DofCSc - Dec. 1986 | |
672 ;;;; (quiroz@cs.rochester.edu) | |
673 | |
674 (defvar *cl-valid-named-list-accessors* | |
675 '(first rest second third fourth fifth sixth seventh eighth ninth tenth)) | |
676 (defvar *cl-valid-nth-offsets* | |
677 '((second . 1) | |
678 (third . 2) | |
679 (fourth . 3) | |
680 (fifth . 4) | |
681 (sixth . 5) | |
682 (seventh . 6) | |
683 (eighth . 7) | |
684 (ninth . 8) | |
685 (tenth . 9))) | |
686 | |
687 (defun byte-compile-named-list-accessors (form) | |
688 "Generate code for (<accessor> FORM), where <accessor> is one of the named | |
689 list accessors: first, second, ..., tenth, rest." | |
690 (let* ((fun (car form)) | |
691 (arg (cadr form)) | |
692 (valid *cl-valid-named-list-accessors*) | |
693 (offsets *cl-valid-nth-offsets*)) | |
922 | 694 (cond |
695 | |
696 ;; Check that it's a form we're prepared to handle. | |
697 ((not (memq fun valid)) | |
698 (error | |
699 "cl.el internal bug: `%s' not in {first, ..., tenth, rest}" | |
700 fun)) | |
701 | |
702 ;; Check the number of arguments. | |
703 ((not (= (length form) 2)) | |
704 (byte-compile-subr-wrong-args form 1)) | |
705 | |
706 ;; If the result will simply be tossed, don't generate any code for | |
707 ;; it, and indicate that we have already discarded the value. | |
708 (for-effect | |
709 (setq for-effect nil)) | |
710 | |
711 ;; Generate code for the call. | |
712 ((eq fun 'first) | |
713 (byte-compile-form arg) | |
714 (byte-compile-out 'byte-car 0)) | |
715 ((eq fun 'rest) | |
716 (byte-compile-form arg) | |
717 (byte-compile-out 'byte-cdr 0)) | |
718 (t ;one of the others | |
719 (byte-compile-constant (cdr (assq fun offsets))) | |
720 (byte-compile-form arg) | |
721 (byte-compile-out 'byte-nth 0))))) | |
188 | 722 |
723 ;;; Synonyms for list functions | |
724 (defun first (x) | |
725 "Synonym for `car'" | |
726 (car x)) | |
727 (put 'first 'byte-compile 'byte-compile-named-list-accessors) | |
728 | |
729 (defun second (x) | |
730 "Return the second element of the list LIST." | |
731 (nth 1 x)) | |
732 (put 'second 'byte-compile 'byte-compile-named-list-accessors) | |
733 | |
734 (defun third (x) | |
735 "Return the third element of the list LIST." | |
736 (nth 2 x)) | |
737 (put 'third 'byte-compile 'byte-compile-named-list-accessors) | |
738 | |
739 (defun fourth (x) | |
740 "Return the fourth element of the list LIST." | |
741 (nth 3 x)) | |
742 (put 'fourth 'byte-compile 'byte-compile-named-list-accessors) | |
743 | |
744 (defun fifth (x) | |
745 "Return the fifth element of the list LIST." | |
746 (nth 4 x)) | |
747 (put 'fifth 'byte-compile 'byte-compile-named-list-accessors) | |
748 | |
749 (defun sixth (x) | |
750 "Return the sixth element of the list LIST." | |
751 (nth 5 x)) | |
752 (put 'sixth 'byte-compile 'byte-compile-named-list-accessors) | |
753 | |
754 (defun seventh (x) | |
755 "Return the seventh element of the list LIST." | |
756 (nth 6 x)) | |
757 (put 'seventh 'byte-compile 'byte-compile-named-list-accessors) | |
758 | |
759 (defun eighth (x) | |
760 "Return the eighth element of the list LIST." | |
761 (nth 7 x)) | |
762 (put 'eighth 'byte-compile 'byte-compile-named-list-accessors) | |
763 | |
764 (defun ninth (x) | |
765 "Return the ninth element of the list LIST." | |
766 (nth 8 x)) | |
767 (put 'ninth 'byte-compile 'byte-compile-named-list-accessors) | |
768 | |
769 (defun tenth (x) | |
770 "Return the tenth element of the list LIST." | |
771 (nth 9 x)) | |
772 (put 'tenth 'byte-compile 'byte-compile-named-list-accessors) | |
773 | |
774 (defun rest (x) | |
775 "Synonym for `cdr'" | |
776 (cdr x)) | |
777 (put 'rest 'byte-compile 'byte-compile-named-list-accessors) | |
778 | |
779 (defun endp (x) | |
780 "t if X is nil, nil if X is a cons; error otherwise." | |
781 (if (listp x) | |
782 (null x) | |
783 (error "endp received a non-cons, non-null argument `%s'" | |
784 (prin1-to-string x)))) | |
785 | |
786 (defun last (x) | |
787 "Returns the last link in the list LIST." | |
788 (if (nlistp x) | |
789 (error "arg to `last' must be a list")) | |
790 (do ((current-cons x (cdr current-cons)) | |
791 (next-cons (cdr x) (cdr next-cons))) | |
792 ((endp next-cons) current-cons))) | |
793 | |
794 (defun list-length (x) ;taken from CLtL sect. 15.2 | |
795 "Returns the length of a non-circular list, or `nil' for a circular one." | |
796 (do ((n 0) ;counter | |
797 (fast x (cddr fast)) ;fast pointer, leaps by 2 | |
798 (slow x (cdr slow)) ;slow pointer, leaps by 1 | |
799 (ready nil)) ;indicates termination | |
800 (ready n) | |
801 (cond ((endp fast) | |
802 (setq ready t)) ;return n | |
803 ((endp (cdr fast)) | |
804 (setq n (+ n 1)) | |
805 (setq ready t)) ;return n+1 | |
806 ((and (eq fast slow) (> n 0)) | |
807 (setq n nil) | |
808 (setq ready t)) ;return nil | |
809 (t | |
810 (setq n (+ n 2)))))) ;just advance counter | |
811 | |
812 (defun butlast (list &optional n) | |
813 "Return a new list like LIST but sans the last N elements. | |
814 N defaults to 1. If the list doesn't have N elements, nil is returned." | |
815 (if (null n) (setq n 1)) | |
816 (reverse (nthcdr n (reverse list)))) | |
817 | |
818 (defun list* (arg &rest others) | |
819 "Return a new list containing the first arguments consed onto the last arg. | |
820 Thus, (list* 1 2 3 '(a b)) returns (1 2 3 a b)." | |
821 (if (null others) | |
822 arg | |
823 (let* ((allargs (cons arg others)) | |
824 (front (butlast allargs)) | |
825 (back (last allargs))) | |
826 (rplacd (last front) (car back)) | |
827 front))) | |
828 | |
829 (defun adjoin (item list) | |
830 "Return a list which contains ITEM but is otherwise like LIST. | |
831 If ITEM occurs in LIST, the value is LIST. Otherwise it is (cons ITEM LIST). | |
832 When comparing ITEM against elements, `eql' is used." | |
833 (if (memq item list) | |
834 list | |
835 (cons item list))) | |
836 | |
837 (defun ldiff (list sublist) | |
838 "Return a new list like LIST but sans SUBLIST. | |
839 SUBLIST must be one of the links in LIST; otherwise the value is LIST itself." | |
840 (do ((result '()) | |
841 (curcons list (cdr curcons))) | |
842 ((or (endp curcons) (eq curcons sublist)) | |
843 (reverse result)) | |
844 (setq result (cons (car curcons) result)))) | |
845 | |
846 ;;; The popular c[ad]*r functions and other list accessors. | |
847 | |
848 ;;; To implement this efficiently, a new byte compile handler is used to | |
849 ;;; generate the minimal code, saving one function call. | |
850 | |
851 (defun byte-compile-ca*d*r (form) | |
852 "Generate code for a (c[ad]+r argument). This realizes the various | |
853 combinations of car and cdr whose names are supported in this implementation. | |
854 To use this functionality for a given function,just give its name a | |
855 'byte-compile property of 'byte-compile-ca*d*r" | |
856 (let* ((fun (car form)) | |
857 (arg (cadr form)) | |
858 (seq (mapcar (function (lambda (letter) | |
859 (if (= letter ?a) | |
860 'byte-car 'byte-cdr))) | |
861 (cdr (nreverse (cdr (append (symbol-name fun) nil))))))) | |
862 ;; SEQ is a list of byte-car and byte-cdr in the correct order. | |
922 | 863 (cond |
864 | |
865 ;; Is this a function we can handle? | |
866 ((null seq) | |
867 (error | |
868 "cl.el internal bug: `%s' cannot be compiled by byte-compile-ca*d*r" | |
869 (prin1-to-string form))) | |
870 | |
871 ;; Are we passing this function the correct number of arguments? | |
872 ((or (null (cdr form)) (cddr form)) | |
873 (byte-compile-subr-wrong-args form 1)) | |
874 | |
875 ;; Are we evaluating this expression for effect only? | |
876 (for-effect | |
877 | |
878 ;; We needn't generate any actual code, as long as we tell the rest | |
879 ;; of the compiler that we didn't push anything on the stack. | |
880 (setq for-effect nil)) | |
881 | |
882 ;; Generate code for the function. | |
883 (t | |
884 (byte-compile-form arg) | |
885 (while seq | |
886 (byte-compile-out (car seq) 0) | |
887 (setq seq (cdr seq))))))) | |
188 | 888 |
889 (defun caar (X) | |
890 "Return the car of the car of X." | |
891 (car (car X))) | |
892 (put 'caar 'byte-compile 'byte-compile-ca*d*r) | |
893 | |
894 (defun cadr (X) | |
895 "Return the car of the cdr of X." | |
896 (car (cdr X))) | |
897 (put 'cadr 'byte-compile 'byte-compile-ca*d*r) | |
898 | |
899 (defun cdar (X) | |
900 "Return the cdr of the car of X." | |
901 (cdr (car X))) | |
902 (put 'cdar 'byte-compile 'byte-compile-ca*d*r) | |
903 | |
904 (defun cddr (X) | |
905 "Return the cdr of the cdr of X." | |
906 (cdr (cdr X))) | |
907 (put 'cddr 'byte-compile 'byte-compile-ca*d*r) | |
908 | |
909 (defun caaar (X) | |
910 "Return the car of the car of the car of X." | |
911 (car (car (car X)))) | |
912 (put 'caaar 'byte-compile 'byte-compile-ca*d*r) | |
913 | |
914 (defun caadr (X) | |
915 "Return the car of the car of the cdr of X." | |
916 (car (car (cdr X)))) | |
917 (put 'caadr 'byte-compile 'byte-compile-ca*d*r) | |
918 | |
919 (defun cadar (X) | |
920 "Return the car of the cdr of the car of X." | |
921 (car (cdr (car X)))) | |
922 (put 'cadar 'byte-compile 'byte-compile-ca*d*r) | |
923 | |
924 (defun cdaar (X) | |
925 "Return the cdr of the car of the car of X." | |
926 (cdr (car (car X)))) | |
927 (put 'cdaar 'byte-compile 'byte-compile-ca*d*r) | |
928 | |
929 (defun caddr (X) | |
930 "Return the car of the cdr of the cdr of X." | |
931 (car (cdr (cdr X)))) | |
932 (put 'caddr 'byte-compile 'byte-compile-ca*d*r) | |
933 | |
934 (defun cdadr (X) | |
935 "Return the cdr of the car of the cdr of X." | |
936 (cdr (car (cdr X)))) | |
937 (put 'cdadr 'byte-compile 'byte-compile-ca*d*r) | |
938 | |
939 (defun cddar (X) | |
940 "Return the cdr of the cdr of the car of X." | |
941 (cdr (cdr (car X)))) | |
942 (put 'cddar 'byte-compile 'byte-compile-ca*d*r) | |
943 | |
944 (defun cdddr (X) | |
945 "Return the cdr of the cdr of the cdr of X." | |
946 (cdr (cdr (cdr X)))) | |
947 (put 'cdddr 'byte-compile 'byte-compile-ca*d*r) | |
948 | |
949 (defun caaaar (X) | |
950 "Return the car of the car of the car of the car of X." | |
951 (car (car (car (car X))))) | |
952 (put 'caaaar 'byte-compile 'byte-compile-ca*d*r) | |
953 | |
954 (defun caaadr (X) | |
955 "Return the car of the car of the car of the cdr of X." | |
956 (car (car (car (cdr X))))) | |
957 (put 'caaadr 'byte-compile 'byte-compile-ca*d*r) | |
958 | |
959 (defun caadar (X) | |
960 "Return the car of the car of the cdr of the car of X." | |
961 (car (car (cdr (car X))))) | |
962 (put 'caadar 'byte-compile 'byte-compile-ca*d*r) | |
963 | |
964 (defun cadaar (X) | |
965 "Return the car of the cdr of the car of the car of X." | |
966 (car (cdr (car (car X))))) | |
967 (put 'cadaar 'byte-compile 'byte-compile-ca*d*r) | |
968 | |
969 (defun cdaaar (X) | |
970 "Return the cdr of the car of the car of the car of X." | |
971 (cdr (car (car (car X))))) | |
972 (put 'cdaaar 'byte-compile 'byte-compile-ca*d*r) | |
973 | |
974 (defun caaddr (X) | |
975 "Return the car of the car of the cdr of the cdr of X." | |
976 (car (car (cdr (cdr X))))) | |
977 (put 'caaddr 'byte-compile 'byte-compile-ca*d*r) | |
978 | |
979 (defun cadadr (X) | |
980 "Return the car of the cdr of the car of the cdr of X." | |
981 (car (cdr (car (cdr X))))) | |
982 (put 'cadadr 'byte-compile 'byte-compile-ca*d*r) | |
983 | |
984 (defun cdaadr (X) | |
985 "Return the cdr of the car of the car of the cdr of X." | |
986 (cdr (car (car (cdr X))))) | |
987 (put 'cdaadr 'byte-compile 'byte-compile-ca*d*r) | |
988 | |
989 (defun caddar (X) | |
990 "Return the car of the cdr of the cdr of the car of X." | |
991 (car (cdr (cdr (car X))))) | |
992 (put 'caddar 'byte-compile 'byte-compile-ca*d*r) | |
993 | |
994 (defun cdadar (X) | |
995 "Return the cdr of the car of the cdr of the car of X." | |
996 (cdr (car (cdr (car X))))) | |
997 (put 'cdadar 'byte-compile 'byte-compile-ca*d*r) | |
998 | |
999 (defun cddaar (X) | |
1000 "Return the cdr of the cdr of the car of the car of X." | |
1001 (cdr (cdr (car (car X))))) | |
1002 (put 'cddaar 'byte-compile 'byte-compile-ca*d*r) | |
1003 | |
1004 (defun cadddr (X) | |
1005 "Return the car of the cdr of the cdr of the cdr of X." | |
1006 (car (cdr (cdr (cdr X))))) | |
1007 (put 'cadddr 'byte-compile 'byte-compile-ca*d*r) | |
1008 | |
1009 (defun cddadr (X) | |
1010 "Return the cdr of the cdr of the car of the cdr of X." | |
1011 (cdr (cdr (car (cdr X))))) | |
1012 (put 'cddadr 'byte-compile 'byte-compile-ca*d*r) | |
1013 | |
1014 (defun cdaddr (X) | |
1015 "Return the cdr of the car of the cdr of the cdr of X." | |
1016 (cdr (car (cdr (cdr X))))) | |
1017 (put 'cdaddr 'byte-compile 'byte-compile-ca*d*r) | |
1018 | |
1019 (defun cdddar (X) | |
1020 "Return the cdr of the cdr of the cdr of the car of X." | |
1021 (cdr (cdr (cdr (car X))))) | |
1022 (put 'cdddar 'byte-compile 'byte-compile-ca*d*r) | |
1023 | |
1024 (defun cddddr (X) | |
1025 "Return the cdr of the cdr of the cdr of the cdr of X." | |
1026 (cdr (cdr (cdr (cdr X))))) | |
1027 (put 'cddddr 'byte-compile 'byte-compile-ca*d*r) | |
1028 | |
1029 ;;; some inverses of the accessors are needed for setf purposes | |
1030 | |
1031 (defun setnth (n list newval) | |
1032 "Set (nth N LIST) to NEWVAL. Returns NEWVAL." | |
1033 (rplaca (nthcdr n list) newval)) | |
1034 | |
1035 (defun setnthcdr (n list newval) | |
1036 "(setnthcdr N LIST NEWVAL) => NEWVAL | |
1037 As a side effect, sets the Nth cdr of LIST to NEWVAL." | |
1038 (cond ((< n 0) | |
1039 (error "N must be 0 or greater, not %d" n)) | |
1040 ((= n 0) | |
1041 (rplaca list (car newval)) | |
1042 (rplacd list (cdr newval)) | |
1043 newval) | |
1044 (t | |
1045 (rplacd (nthcdr (- n 1) list) newval)))) | |
1046 | |
1047 ;;; A-lists machinery | |
1048 | |
1049 (defun acons (key item alist) | |
1050 "Return a new alist with KEY paired with ITEM; otherwise like ALIST. | |
1051 Does not copy ALIST." | |
1052 (cons (cons key item) alist)) | |
1053 | |
1054 (defun pairlis (keys data &optional alist) | |
1055 "Return a new alist with each elt of KEYS paired with an elt of DATA; | |
1056 optional 3rd arg ALIST is nconc'd at the end. KEYS and DATA must | |
1057 have the same length." | |
1058 (unless (= (length keys) (length data)) | |
1059 (error "keys and data should be the same length")) | |
1060 (do* ;;collect keys and data in front of alist | |
1061 ((kptr keys (cdr kptr)) ;traverses the keys | |
1062 (dptr data (cdr dptr)) ;traverses the data | |
1063 (key (car kptr) (car kptr)) ;current key | |
1064 (item (car dptr) (car dptr)) ;current data item | |
1065 (result alist)) | |
1066 ((endp kptr) result) | |
1067 (setq result (acons key item result)))) | |
1068 | |
1069 | |
1070 ;;;; SEQUENCES | |
1071 ;;;; Emacs Lisp provides many of the 'sequences' functionality of | |
1072 ;;;; Common Lisp. This file provides a few things that were left out. | |
1073 ;;;; | |
1074 | |
1075 | |
1076 (defkeyword :test "Used to designate positive (selection) tests.") | |
1077 (defkeyword :test-not "Used to designate negative (rejection) tests.") | |
1078 (defkeyword :key "Used to designate component extractions.") | |
1079 (defkeyword :predicate "Used to define matching of sequence components.") | |
1080 (defkeyword :start "Inclusive low index in sequence") | |
1081 (defkeyword :end "Exclusive high index in sequence") | |
1082 (defkeyword :start1 "Inclusive low index in first of two sequences.") | |
1083 (defkeyword :start2 "Inclusive low index in second of two sequences.") | |
1084 (defkeyword :end1 "Exclusive high index in first of two sequences.") | |
1085 (defkeyword :end2 "Exclusive high index in second of two sequences.") | |
1086 (defkeyword :count "Number of elements to affect.") | |
1087 (defkeyword :from-end "T when counting backwards.") | |
1088 | |
1089 (defun some (pred seq &rest moreseqs) | |
1090 "Test PREDICATE on each element of SEQUENCE; is it ever non-nil? | |
1091 Extra args are additional sequences; PREDICATE gets one arg from each | |
1092 sequence and we advance down all the sequences together in lock-step. | |
1093 A sequence means either a list or a vector." | |
1094 (let ((args (reassemble-argslists (list* seq moreseqs)))) | |
1095 (do* ((ready nil) ;flag: return when t | |
1096 (result nil) ;resulting value | |
1097 (applyval nil) ;result of applying pred once | |
1098 (remaining args | |
1099 (cdr remaining)) ;remaining argument sets | |
1100 (current (car remaining) ;current argument set | |
1101 (car remaining))) | |
1102 ((or ready (endp remaining)) result) | |
1103 (setq applyval (apply pred current)) | |
1104 (when applyval | |
1105 (setq ready t) | |
1106 (setq result applyval))))) | |
1107 | |
1108 (defun every (pred seq &rest moreseqs) | |
1109 "Test PREDICATE on each element of SEQUENCE; is it always non-nil? | |
1110 Extra args are additional sequences; PREDICATE gets one arg from each | |
1111 sequence and we advance down all the sequences together in lock-step. | |
1112 A sequence means either a list or a vector." | |
1113 (let ((args (reassemble-argslists (list* seq moreseqs)))) | |
1114 (do* ((ready nil) ;flag: return when t | |
1115 (result t) ;resulting value | |
1116 (applyval nil) ;result of applying pred once | |
1117 (remaining args | |
1118 (cdr remaining)) ;remaining argument sets | |
1119 (current (car remaining) ;current argument set | |
1120 (car remaining))) | |
1121 ((or ready (endp remaining)) result) | |
1122 (setq applyval (apply pred current)) | |
1123 (unless applyval | |
1124 (setq ready t) | |
1125 (setq result nil))))) | |
1126 | |
1127 (defun notany (pred seq &rest moreseqs) | |
1128 "Test PREDICATE on each element of SEQUENCE; is it always nil? | |
1129 Extra args are additional sequences; PREDICATE gets one arg from each | |
1130 sequence and we advance down all the sequences together in lock-step. | |
1131 A sequence means either a list or a vector." | |
1132 (let ((args (reassemble-argslists (list* seq moreseqs)))) | |
1133 (do* ((ready nil) ;flag: return when t | |
1134 (result t) ;resulting value | |
1135 (applyval nil) ;result of applying pred once | |
1136 (remaining args | |
1137 (cdr remaining)) ;remaining argument sets | |
1138 (current (car remaining) ;current argument set | |
1139 (car remaining))) | |
1140 ((or ready (endp remaining)) result) | |
1141 (setq applyval (apply pred current)) | |
1142 (when applyval | |
1143 (setq ready t) | |
1144 (setq result nil))))) | |
1145 | |
1146 (defun notevery (pred seq &rest moreseqs) | |
1147 "Test PREDICATE on each element of SEQUENCE; is it sometimes nil? | |
1148 Extra args are additional sequences; PREDICATE gets one arg from each | |
1149 sequence and we advance down all the sequences together in lock-step. | |
1150 A sequence means either a list or a vector." | |
1151 (let ((args (reassemble-argslists (list* seq moreseqs)))) | |
1152 (do* ((ready nil) ;flag: return when t | |
1153 (result nil) ;resulting value | |
1154 (applyval nil) ;result of applying pred once | |
1155 (remaining args | |
1156 (cdr remaining)) ;remaining argument sets | |
1157 (current (car remaining) ;current argument set | |
1158 (car remaining))) | |
1159 ((or ready (endp remaining)) result) | |
1160 (setq applyval (apply pred current)) | |
1161 (unless applyval | |
1162 (setq ready t) | |
1163 (setq result t))))) | |
1164 | |
1165 ;;; More sequence functions that don't need keyword arguments | |
1166 | |
1167 (defun concatenate (type &rest sequences) | |
1168 "(concatenate TYPE &rest SEQUENCES) => a sequence | |
1169 The sequence returned is of type TYPE (must be 'list, 'string, or 'vector) and | |
1170 contains the concatenation of the elements of all the arguments, in the order | |
1171 given." | |
1172 (let ((sequences (append sequences '(())))) | |
1173 (case type | |
1174 (list | |
1175 (apply (function append) sequences)) | |
1176 (string | |
1177 (apply (function concat) sequences)) | |
1178 (vector | |
1179 (apply (function vector) (apply (function append) sequences))) | |
1180 (t | |
1181 (error "type for concatenate `%s' not 'list, 'string or 'vector" | |
1182 (prin1-to-string type)))))) | |
1183 | |
1184 (defun map (type function &rest sequences) | |
1185 "(map TYPE FUNCTION &rest SEQUENCES) => a sequence | |
1186 The FUNCTION is called on each set of elements from the SEQUENCES \(stopping | |
1187 when the shortest sequence is terminated\) and the results are possibly | |
1188 returned in a sequence of type TYPE \(one of 'list, 'vector, 'string, or nil\) | |
1189 giving NIL for TYPE gets rid of the values." | |
1190 (if (not (memq type (list 'list 'string 'vector nil))) | |
1191 (error "type for map `%s' not 'list, 'string, 'vector or nil" | |
1192 (prin1-to-string type))) | |
1193 (let ((argslists (reassemble-argslists sequences)) | |
1194 results) | |
1195 (if (null type) | |
1196 (while argslists ;don't bother accumulating | |
1197 (apply function (car argslists)) | |
1198 (setq argslists (cdr argslists))) | |
1199 (setq results (mapcar (function (lambda (args) (apply function args))) | |
1200 argslists)) | |
1201 (case type | |
1202 (list | |
1203 results) | |
1204 (string | |
1205 (funcall (function concat) results)) | |
1206 (vector | |
1207 (apply (function vector) results)))))) | |
1208 | |
1209 ;;; an inverse of elt is needed for setf purposes | |
1210 | |
1211 (defun setelt (seq n newval) | |
1212 "In SEQUENCE, set the Nth element to NEWVAL. Returns NEWVAL. | |
1213 A sequence means either a list or a vector." | |
1214 (let ((l (length seq))) | |
1215 (if (or (< n 0) (>= n l)) | |
1216 (error "N(%d) should be between 0 and %d" n l) | |
1217 ;; only two cases need be considered valid, as strings are arrays | |
1218 (cond ((listp seq) | |
1219 (setnth n seq newval)) | |
1220 ((arrayp seq) | |
1221 (aset seq n newval)) | |
1222 (t | |
1223 (error "SEQ should be a sequence, not `%s'" | |
1224 (prin1-to-string seq))))))) | |
1225 | |
1226 ;;; Testing with keyword arguments. | |
1227 ;;; | |
1228 ;;; Many of the sequence functions use keywords to denote some stylized | |
1229 ;;; form of selecting entries in a sequence. The involved arguments | |
1230 ;;; are collected with a &rest marker (as Emacs Lisp doesn't have a &key | |
1231 ;;; marker), then they are passed to build-klist, who | |
1232 ;;; constructs an association list. That association list is used to | |
1233 ;;; test for satisfaction and matching. | |
1234 | |
1235 ;;; DON'T USE MEMBER, NOR ANY FUNCTION THAT COULD TAKE KEYWORDS HERE!!! | |
1236 | |
1237 (defun build-klist (argslist acceptable &optional allow-other-keys) | |
1238 "Decode a keyword argument list ARGSLIST for keywords in ACCEPTABLE. | |
1239 ARGSLIST is a list, presumably the &rest argument of a call, whose | |
1240 even numbered elements must be keywords. | |
1241 ACCEPTABLE is a list of keywords, the only ones that are truly acceptable. | |
1242 The result is an alist containing the arguments named by the keywords | |
1243 in ACCEPTABLE, or an error is signalled, if something failed. | |
1244 If the third argument (an optional) is non-nil, other keys are acceptable." | |
1245 ;; check legality of the arguments, then destructure them | |
1246 (unless (and (listp argslist) | |
1247 (evenp (length argslist))) | |
1248 (error "build-klist: odd number of keyword-args")) | |
1249 (unless (and (listp acceptable) | |
1250 (every 'keywordp acceptable)) | |
1251 (error "build-klist: second arg should be a list of keywords")) | |
1252 (multiple-value-bind | |
1253 (keywords forms) | |
1254 (unzip-list argslist) | |
1255 (unless (every 'keywordp keywords) | |
1256 (error "build-klist: expected keywords, found `%s'" | |
1257 (prin1-to-string keywords))) | |
1258 (unless (or allow-other-keys | |
1259 (every (function (lambda (keyword) | |
1260 (memq keyword acceptable))) | |
1261 keywords)) | |
1262 (error "bad keyword[s]: %s not in %s" | |
1263 (prin1-to-string (mapcan (function (lambda (keyword) | |
1264 (if (memq keyword acceptable) | |
1265 nil | |
1266 (list keyword)))) | |
1267 keywords)) | |
1268 (prin1-to-string acceptable))) | |
1269 (do* ;;pick up the pieces | |
1270 ((auxlist ;auxiliary a-list, may | |
1271 (pairlis keywords forms)) ;contain repetitions and junk | |
1272 (ptr acceptable (cdr ptr)) ;pointer in acceptable | |
1273 (this (car ptr) (car ptr)) ;current acceptable keyword | |
1274 (auxval nil) ;used to move values around | |
1275 (alist '())) ;used to build the result | |
1276 ((endp ptr) alist) | |
1277 ;; if THIS appears in auxlist, use its value | |
1278 (when (setq auxval (assq this auxlist)) | |
1279 (setq alist (cons auxval alist)))))) | |
1280 | |
1281 | |
1282 (defun extract-from-klist (klist key &optional default) | |
1283 "(extract-from-klist KLIST KEY [DEFAULT]) => value of KEY or DEFAULT | |
1284 Extract value associated with KEY in KLIST (return DEFAULT if nil)." | |
1285 (let ((retrieved (cdr (assq key klist)))) | |
1286 (or retrieved default))) | |
1287 | |
1288 (defun keyword-argument-supplied-p (klist key) | |
1289 "(keyword-argument-supplied-p KLIST KEY) => nil or something | |
1290 NIL if KEY (a keyword) does not appear in the KLIST." | |
1291 (assq key klist)) | |
1292 | |
1293 (defun add-to-klist (key item klist) | |
1294 "(ADD-TO-KLIST KEY ITEM KLIST) => new KLIST | |
1295 Add association (KEY . ITEM) to KLIST." | |
1296 (setq klist (acons key item klist))) | |
1297 | |
1298 (defun elt-satisfies-test-p (item elt klist) | |
1299 "(elt-satisfies-test-p ITEM ELT KLIST) => t or nil | |
1300 KLIST encodes a keyword-arguments test, as in CH. 14 of CLtL. | |
1301 True if the given ITEM and ELT satisfy the test." | |
1302 (let ((test (extract-from-klist klist :test)) | |
1303 (test-not (extract-from-klist klist :test-not)) | |
1304 (keyfn (extract-from-klist klist :key 'identity))) | |
1305 (cond (test | |
1306 (funcall test item (funcall keyfn elt))) | |
1307 (test-not | |
1308 (not (funcall test-not item (funcall keyfn elt)))) | |
1309 (t ;should never happen | |
1310 (error "neither :test nor :test-not in `%s'" | |
1311 (prin1-to-string klist)))))) | |
1312 | |
1313 (defun elt-satisfies-if-p (item klist) | |
1314 "(elt-satisfies-if-p ITEM KLIST) => t or nil | |
1315 True if an -if style function was called and ITEM satisfies the | |
1316 predicate under :predicate in KLIST." | |
1317 (let ((predicate (extract-from-klist klist :predicate)) | |
1318 (keyfn (extract-from-klist klist :key 'identity))) | |
1319 (funcall predicate item (funcall keyfn elt)))) | |
1320 | |
1321 (defun elt-satisfies-if-not-p (item klist) | |
1322 "(elt-satisfies-if-not-p ITEM KLIST) => t or nil | |
1323 KLIST encodes a keyword-arguments test, as in CH. 14 of CLtL. | |
1324 True if an -if-not style function was called and ITEM does not satisfy | |
1325 the predicate under :predicate in KLIST." | |
1326 (let ((predicate (extract-from-klist klist :predicate)) | |
1327 (keyfn (extract-from-klist klist :key 'identity))) | |
1328 (not (funcall predicate item (funcall keyfn elt))))) | |
1329 | |
1330 (defun elts-match-under-klist-p (e1 e2 klist) | |
1331 "(elts-match-under-klist-p E1 E2 KLIST) => t or nil | |
1332 KLIST encodes a keyword-arguments test, as in CH. 14 of CLtL. | |
1333 True if elements E1 and E2 match under the tests encoded in KLIST." | |
1334 (let ((test (extract-from-klist klist :test)) | |
1335 (test-not (extract-from-klist klist :test-not)) | |
1336 (keyfn (extract-from-klist klist :key 'identity))) | |
1337 (if (and test test-not) | |
1338 (error "both :test and :test-not in `%s'" | |
1339 (prin1-to-string klist))) | |
1340 (cond (test | |
1341 (funcall test (funcall keyfn e1) (funcall keyfn e2))) | |
1342 (test-not | |
1343 (not (funcall test-not (funcall keyfn e1) (funcall keyfn e2)))) | |
1344 (t ;should never happen | |
1345 (error "neither :test nor :test-not in `%s'" | |
1346 (prin1-to-string klist)))))) | |
1347 | |
1348 ;;; This macro simplifies using keyword args. It is less clumsy than using | |
1349 ;;; the primitives build-klist, etc... For instance, member could be written | |
1350 ;;; this way: | |
1351 | |
1352 ;;; (defun member (item list &rest kargs) | |
1353 ;;; (with-keyword-args kargs (test test-not (key 'identity)) | |
1354 ;;; ...)) | |
1355 | |
1356 ;;; Suggested by Robert Potter (potter@cs.rochester.edu, 15 Nov 1989) | |
1357 | |
1358 (defmacro with-keyword-args (keyargslist vardefs &rest body) | |
1359 "(WITH-KEYWORD-ARGS KEYARGSLIST VARDEFS . BODY) | |
1360 KEYARGSLIST can be either a symbol or a list of one or two symbols. | |
1361 In the second case, the second symbol is either T or NIL, indicating whether | |
1362 keywords other than the mentioned ones are tolerable. | |
1363 | |
1364 VARDEFS is a list. Each entry is either a VAR (symbol) or matches | |
1365 \(VAR [DEFAULT [KEYWORD]]). Just giving VAR is the same as giving | |
1366 \(VAR nil :VAR). | |
1367 | |
1368 The BODY is executed in an environment where each VAR (a symbol) is bound to | |
1369 the value present in the KEYARGSLIST provided, or to the DEFAULT. The value | |
1370 is searched by using the keyword form of VAR (i.e., :VAR) or the optional | |
1371 keyword if provided. | |
1372 | |
1373 Notice that this macro doesn't distinguish between a default value given | |
1374 explicitly by the user and one provided by default. See also the more | |
1375 primitive functions build-klist, add-to-klist, extract-from-klist, | |
1376 keyword-argument-supplied-p, elt-satisfies-test-p, elt-satisfies-if-p, | |
1377 elt-satisfies-if-not-p, elts-match-under-klist-p. They provide more complete, | |
1378 if clumsier, control over this feature." | |
1379 (let (allow-other-keys) | |
1380 (if (listp keyargslist) | |
1381 (if (> (length keyargslist) 2) | |
1382 (error | |
1383 "`%s' should be SYMBOL, (SYMBOL), or (SYMBOL t-OR-nil)" | |
1384 (prin1-to-string keyargslist)) | |
1385 (setq allow-other-keys (cadr keyargslist) | |
1386 keyargslist (car keyargslist)) | |
1387 (if (not (and | |
1388 (symbolp keyargslist) | |
1389 (memq allow-other-keys '(t nil)))) | |
1390 (error | |
1391 "first subform should be SYMBOL, (SYMBOL), or (SYMBOL t-OR-nil)" | |
1392 ))) | |
1393 (if (symbolp keyargslist) | |
1394 (setq allow-other-keys nil) | |
1395 (error | |
1396 "first subform should be SYMBOL, (SYMBOL), or (SYMBOL t-OR-nil)"))) | |
1397 (let (vars defaults keywords forms | |
1398 (klistname (gensym "KLIST_"))) | |
1399 (mapcar (function (lambda (entry) | |
1400 (if (symbolp entry) ;defaulty case | |
1401 (setq entry (list entry nil (keyword-of entry)))) | |
1402 (let* ((l (length entry)) | |
1403 (v (car entry)) | |
1404 (d (cadr entry)) | |
1405 (k (caddr entry))) | |
1406 (if (or (< l 1) (> l 3)) | |
1407 (error | |
1408 "`%s' must match (VAR [DEFAULT [KEYWORD]])" | |
1409 (prin1-to-string entry))) | |
1410 (if (or (null v) (not (symbolp v))) | |
1411 (error | |
1412 "bad variable `%s': must be non-null symbol" | |
1413 (prin1-to-string v))) | |
1414 (setq vars (cons v vars)) | |
1415 (setq defaults (cons d defaults)) | |
1416 (if (< l 3) | |
1417 (setq k (keyword-of v))) | |
1418 (if (and (= l 3) | |
1419 (or (null k) | |
1420 (not (keywordp k)))) | |
1421 (error | |
1422 "bad keyword `%s'" (prin1-to-string k))) | |
1423 (setq keywords (cons k keywords)) | |
1424 (setq forms (cons (list v (list 'extract-from-klist | |
1425 klistname | |
1426 k | |
1427 d)) | |
1428 forms))))) | |
1429 vardefs) | |
1430 (append | |
1431 (list 'let* (nconc (list (list klistname | |
1432 (list 'build-klist keyargslist | |
1433 (list 'quote keywords) | |
1434 allow-other-keys))) | |
1435 (nreverse forms))) | |
1436 body)))) | |
1437 (put 'with-keyword-args 'lisp-indent-function 1) | |
1438 | |
1439 | |
1440 ;;; REDUCE | |
1441 ;;; It is here mostly as an example of how to use KLISTs. | |
1442 ;;; | |
1443 ;;; First of all, you need to declare the keywords (done elsewhere in this | |
1444 ;;; file): | |
1445 ;;; (defkeyword :from-end "syntax of sequence functions") | |
1446 ;;; (defkeyword :start "syntax of sequence functions") | |
1447 ;;; etc... | |
1448 ;;; | |
1449 ;;; Then, you capture all the possible keyword arguments with a &rest | |
1450 ;;; argument. You can pass that list downward again, of course, but | |
1451 ;;; internally you need to parse it into a KLIST (an alist, really). One uses | |
1452 ;;; (build-klist REST-ARGS ACCEPTABLE-KEYWORDS [ALLOW-OTHER]). You can then | |
1453 ;;; test for presence by using (keyword-argument-supplied-p KLIST KEY) and | |
1454 ;;; extract a value with (extract-from-klist KLIST KEY [DEFAULT]). | |
1455 | |
1456 (defun reduce (function sequence &rest kargs) | |
1457 "Apply FUNCTION (a function of two arguments) to succesive pairs of elements | |
1458 from SEQUENCE. Some keyword arguments are valid after FUNCTION and SEQUENCE: | |
1459 :from-end If non-nil, process the values backwards | |
1460 :initial-value If given, prefix it to the SEQUENCE. Suffix, if :from-end | |
1461 :start Restrict reduction to the subsequence from this index | |
1462 :end Restrict reduction to the subsequence BEFORE this index. | |
1463 If the sequence is empty and no :initial-value is given, the FUNCTION is | |
1464 called on zero (not two) arguments. Otherwise, if there is exactly one | |
1465 element in the combination of SEQUENCE and the initial value, that element is | |
1466 returned." | |
1467 (let* ((klist (build-klist kargs '(:from-end :start :end :initial-value))) | |
1468 (length (length sequence)) | |
1469 (from-end (extract-from-klist klist :from-end)) | |
1470 (initial-value-given (keyword-argument-supplied-p | |
1471 klist :initial-value)) | |
1472 (start (extract-from-klist kargs :start 0)) | |
1473 (end (extract-from-klist kargs :end length))) | |
1474 (setq sequence (cl$subseq-as-list sequence start end)) | |
1475 (if from-end | |
1476 (setq sequence (reverse sequence))) | |
1477 (if initial-value-given | |
1478 (setq sequence (cons (extract-from-klist klist :initial-value) | |
1479 sequence))) | |
1480 (if (null sequence) | |
1481 (funcall function) ;only use of 0 arguments | |
1482 (let* ((result (car sequence)) | |
1483 (sequence (cdr sequence))) | |
1484 (while sequence | |
1485 (setq result (if from-end | |
1486 (funcall function (car sequence) result) | |
1487 (funcall function result (car sequence))) | |
1488 sequence (cdr sequence))) | |
1489 result)))) | |
1490 | |
1491 (defun cl$subseq-as-list (sequence start end) | |
1492 "(cl$subseq-as-list SEQUENCE START END) => a list" | |
1493 (let ((list (append sequence nil)) | |
1494 (length (length sequence)) | |
1495 result) | |
1496 (if (< start 0) | |
1497 (error "start should be >= 0, not %d" start)) | |
1498 (if (> end length) | |
1499 (error "end should be <= %d, not %d" length end)) | |
1500 (if (and (zerop start) (= end length)) | |
1501 list | |
1502 (let ((i start) | |
1503 (vector (apply 'vector list))) | |
1504 (while (/= i end) | |
1505 (setq result (cons (elt vector i) result)) | |
1506 (setq i (+ i 1))) | |
1507 (nreverse result))))) | |
1508 | |
1509 ;;;; end of cl-sequences.el | |
1510 | |
1511 ;;;; Some functions with keyword arguments | |
1512 ;;;; | |
1513 ;;;; Both list and sequence functions are considered here together. This | |
1514 ;;;; doesn't fit any more with the original split of functions in files. | |
1515 | |
1516 (defun member (item list &rest kargs) | |
1517 "Look for ITEM in LIST; return first tail of LIST the car of whose first | |
1518 cons cell tests the same as ITEM. Admits arguments :key, :test, and :test-not." | |
1519 (if (null kargs) ;treat this fast for efficiency | |
1520 (memq item list) | |
1521 (let* ((klist (build-klist kargs '(:test :test-not :key))) | |
1522 (test (extract-from-klist klist :test)) | |
1523 (testnot (extract-from-klist klist :test-not)) | |
1524 (key (extract-from-klist klist :key 'identity))) | |
1525 ;; another workaround allegledly for speed | |
1526 (if (and (or (eq test 'eq) (eq test 'eql) | |
1527 (eq test (symbol-function 'eq)) | |
1528 (eq test (symbol-function 'eql))) | |
1529 (null testnot) | |
1530 (or (eq key 'identity) ;either by default or so given | |
1531 (eq key (function identity)) ;could this happen? | |
1532 (eq key (symbol-function 'identity)) ;sheer paranoia | |
1533 )) | |
1534 (memq item list) | |
1535 (if (and test testnot) | |
1536 (error ":test and :test-not both specified for member")) | |
1537 (if (not (or test testnot)) | |
1538 (setq test 'eql)) | |
1539 ;; final hack: remove the indirection through the function names | |
1540 (if testnot | |
1541 (if (symbolp testnot) | |
1542 (setq testnot (symbol-function testnot))) | |
1543 (if (symbolp test) | |
1544 (setq test (symbol-function test)))) | |
1545 (if (symbolp key) | |
1546 (setq key (symbol-function key))) | |
1547 ;; ok, go for it | |
1548 (let ((ptr list) | |
1549 (done nil) | |
1550 (result '())) | |
1551 (if testnot | |
1552 (while (not (or done (endp ptr))) | |
1553 (cond ((not (funcall testnot item (funcall key (car ptr)))) | |
1554 (setq done t) | |
1555 (setq result ptr))) | |
1556 (setq ptr (cdr ptr))) | |
1557 (while (not (or done (endp ptr))) | |
1558 (cond ((funcall test item (funcall key (car ptr))) | |
1559 (setq done t) | |
1560 (setq result ptr))) | |
1561 (setq ptr (cdr ptr)))) | |
1562 result))))) | |
1563 | |
1564 ;;;; MULTIPLE VALUES | |
1565 ;;;; This package approximates the behavior of the multiple-values | |
1566 ;;;; forms of Common Lisp. | |
1567 ;;;; | |
1568 ;;;; Cesar Quiroz @ UofR DofCSc - Dec. 1986 | |
1569 ;;;; (quiroz@cs.rochester.edu) | |
1570 | |
1571 ;;; Lisp indentation information | |
1572 (put 'multiple-value-bind 'lisp-indent-function 2) | |
1573 (put 'multiple-value-setq 'lisp-indent-function 2) | |
1574 (put 'multiple-value-list 'lisp-indent-function nil) | |
1575 (put 'multiple-value-call 'lisp-indent-function 1) | |
1576 (put 'multiple-value-prog1 'lisp-indent-function 1) | |
1577 | |
1578 ;;; Global state of the package is kept here | |
1579 (defvar *mvalues-values* nil | |
1580 "Most recently returned multiple-values") | |
1581 (defvar *mvalues-count* nil | |
1582 "Count of multiple-values returned, or nil if the mechanism was not used") | |
1583 | |
1584 ;;; values is the standard multiple-value-return form. Must be the | |
1585 ;;; last thing evaluated inside a function. If the caller is not | |
1586 ;;; expecting multiple values, only the first one is passed. (values) | |
1587 ;;; is the same as no-values returned (unaware callers see nil). The | |
1588 ;;; alternative (values-list <list>) is just a convenient shorthand | |
1589 ;;; and complements multiple-value-list. | |
1590 | |
1591 (defun values (&rest val-forms) | |
1592 "Produce multiple values (zero or more). Each arg is one value. | |
1593 See also `multiple-value-bind', which is one way to examine the | |
1594 multiple values produced by a form. If the containing form or caller | |
1595 does not check specially to see multiple values, it will see only | |
1596 the first value." | |
1597 (setq *mvalues-values* val-forms) | |
1598 (setq *mvalues-count* (length *mvalues-values*)) | |
1599 (car *mvalues-values*)) | |
1600 | |
1601 (defun values-list (&optional val-forms) | |
1602 "Produce multiple values (zero or mode). Each element of LIST is one value. | |
1603 This is equivalent to (apply 'values LIST)." | |
1604 (cond ((nlistp val-forms) | |
1605 (error "Argument to values-list must be a list, not `%s'" | |
1606 (prin1-to-string val-forms)))) | |
1607 (setq *mvalues-values* val-forms) | |
1608 (setq *mvalues-count* (length *mvalues-values*)) | |
1609 (car *mvalues-values*)) | |
1610 | |
1611 ;;; Callers that want to see the multiple values use these macros. | |
1612 | |
1613 (defmacro multiple-value-list (form) | |
1614 "Execute FORM and return a list of all the (multiple) values FORM produces. | |
1615 See `values' and `multiple-value-bind'." | |
1616 (list 'progn | |
1617 (list 'setq '*mvalues-count* nil) | |
1618 (list 'let (list (list 'it '(gensym))) | |
1619 (list 'set 'it form) | |
1620 (list 'if '*mvalues-count* | |
1621 (list 'copy-sequence '*mvalues-values*) | |
1622 (list 'progn | |
1623 (list 'setq '*mvalues-count* 1) | |
1624 (list 'setq '*mvalues-values* | |
1625 (list 'list (list 'symbol-value 'it))) | |
1626 (list 'copy-sequence '*mvalues-values*)))))) | |
1627 | |
1628 (defmacro multiple-value-call (function &rest args) | |
1629 "Call FUNCTION on all the values produced by the remaining arguments. | |
1630 (multiple-value-call '+ (values 1 2) (values 3 4)) is 10." | |
1631 (let* ((result (gentemp)) | |
1632 (arg (gentemp))) | |
1633 (list 'apply (list 'function (eval function)) | |
1634 (list 'let* (list (list result '())) | |
1635 (list 'dolist (list arg (list 'quote args) result) | |
1636 (list 'setq result | |
1637 (list 'append | |
1638 result | |
1639 (list 'multiple-value-list | |
1640 (list 'eval arg))))))))) | |
1641 | |
1642 (defmacro multiple-value-bind (vars form &rest body) | |
1643 "Bind VARS to the (multiple) values produced by FORM, then do BODY. | |
1644 VARS is a list of variables; each is bound to one of FORM's values. | |
1645 If FORM doesn't make enough values, the extra variables are bound to nil. | |
1646 (Ordinary forms produce only one value; to produce more, use `values'.) | |
1647 Extra values are ignored. | |
1648 BODY (zero or more forms) is executed with the variables bound, | |
1649 then the bindings are unwound." | |
1650 (let* ((vals (gentemp)) ;name for intermediate values | |
1651 (clauses (mv-bind-clausify ;convert into clauses usable | |
1652 vars vals))) ; in a let form | |
1653 (list* 'let* | |
1654 (cons (list vals (list 'multiple-value-list form)) | |
1655 clauses) | |
1656 body))) | |
1657 | |
1658 (defmacro multiple-value-setq (vars form) | |
1659 "Set VARS to the (multiple) values produced by FORM. | |
1660 VARS is a list of variables; each is set to one of FORM's values. | |
1661 If FORM doesn't make enough values, the extra variables are set to nil. | |
1662 (Ordinary forms produce only one value; to produce more, use `values'.) | |
1663 Extra values are ignored." | |
1664 (let* ((vals (gentemp)) ;name for intermediate values | |
1665 (clauses (mv-bind-clausify ;convert into clauses usable | |
1666 vars vals))) ; in a setq (after append). | |
1667 (list 'let* | |
1668 (list (list vals (list 'multiple-value-list form))) | |
1669 (cons 'setq (apply (function append) clauses))))) | |
1670 | |
1671 (defmacro multiple-value-prog1 (form &rest body) | |
1672 "Evaluate FORM, then BODY, then produce the same values FORM produced. | |
1673 Thus, (multiple-value-prog1 (values 1 2) (foobar)) produces values 1 and 2. | |
1674 This is like `prog1' except that `prog1' would produce only one value, | |
1675 which would be the first of FORM's values." | |
1676 (let* ((heldvalues (gentemp))) | |
1677 (cons 'let* | |
1678 (cons (list (list heldvalues (list 'multiple-value-list form))) | |
1679 (append body (list (list 'values-list heldvalues))))))) | |
1680 | |
1681 ;;; utility functions | |
1682 ;;; | |
1683 ;;; mv-bind-clausify makes the pairs needed to have the variables in | |
1684 ;;; the variable list correspond with the values returned by the form. | |
1685 ;;; vals is a fresh symbol that intervenes in all the bindings. | |
1686 | |
1687 (defun mv-bind-clausify (vars vals) | |
1688 "MV-BIND-CLAUSIFY VARS VALS => Auxiliary list | |
1689 Forms a list of pairs `(,(nth i vars) (nth i vals)) for i from 0 to | |
1690 the length of VARS (a list of symbols). VALS is just a fresh symbol." | |
1691 (if (or (nlistp vars) | |
1692 (notevery 'symbolp vars)) | |
1693 (error "expected a list of symbols, not `%s'" | |
1694 (prin1-to-string vars))) | |
1695 (let* ((nvars (length vars)) | |
1696 (clauses '())) | |
1697 (dotimes (n nvars clauses) | |
1698 (setq clauses (cons (list (nth n vars) | |
1699 (list 'nth n vals)) clauses))))) | |
1700 | |
1701 ;;;; end of cl-multiple-values.el | |
1702 | |
1703 ;;;; ARITH | |
1704 ;;;; This file provides integer arithmetic extensions. Although | |
1705 ;;;; Emacs Lisp doesn't really support anything but integers, that | |
1706 ;;;; has still to be made to look more or less standard. | |
1707 ;;;; | |
1708 ;;;; | |
1709 ;;;; Cesar Quiroz @ UofR DofCSc - Dec. 1986 | |
1710 ;;;; (quiroz@cs.rochester.edu) | |
1711 | |
1712 | |
1713 (defun plusp (number) | |
1714 "True if NUMBER is strictly greater than zero." | |
1715 (> number 0)) | |
1716 | |
1717 (defun minusp (number) | |
1718 "True if NUMBER is strictly less than zero." | |
1719 (< number 0)) | |
1720 | |
1721 (defun oddp (number) | |
1722 "True if INTEGER is not divisible by 2." | |
1723 (/= (% number 2) 0)) | |
1724 | |
1725 (defun evenp (number) | |
1726 "True if INTEGER is divisible by 2." | |
1727 (= (% number 2) 0)) | |
1728 | |
1729 (defun abs (number) | |
1730 "Return the absolute value of NUMBER." | |
1731 (if (< number 0) | |
1732 (- number) | |
1733 number)) | |
1734 | |
1735 (defun signum (number) | |
1736 "Return -1, 0 or 1 according to the sign of NUMBER." | |
1737 (cond ((< number 0) | |
1738 -1) | |
1739 ((> number 0) | |
1740 1) | |
1741 (t ;exactly zero | |
1742 0))) | |
1743 | |
1744 (defun gcd (&rest integers) | |
1745 "Return the greatest common divisor of all the arguments. | |
1746 The arguments must be integers. With no arguments, value is zero." | |
1747 (let ((howmany (length integers))) | |
1748 (cond ((= howmany 0) | |
1749 0) | |
1750 ((= howmany 1) | |
1751 (abs (car integers))) | |
1752 ((> howmany 2) | |
1753 (apply (function gcd) | |
1754 (cons (gcd (nth 0 integers) (nth 1 integers)) | |
1755 (nthcdr 2 integers)))) | |
1756 (t ;howmany=2 | |
1757 ;; essentially the euclidean algorithm | |
1758 (when (zerop (* (nth 0 integers) (nth 1 integers))) | |
1759 (error "a zero argument is invalid for `gcd'")) | |
1760 (do* ((absa (abs (nth 0 integers))) ; better to operate only | |
1761 (absb (abs (nth 1 integers))) ;on positives. | |
1762 (dd (max absa absb)) ; setup correct order for the | |
1763 (ds (min absa absb)) ;succesive divisions. | |
1764 ;; intermediate results | |
1765 (q 0) | |
1766 (r 0) | |
1767 ;; final results | |
1768 (done nil) ; flag: end of iterations | |
1769 (result 0)) ; final value | |
1770 (done result) | |
1771 (setq q (/ dd ds)) | |
1772 (setq r (% dd ds)) | |
1773 (cond ((zerop r) (setq done t) (setq result ds)) | |
1774 (t (setq dd ds) (setq ds r)))))))) | |
1775 | |
1776 (defun lcm (integer &rest more) | |
1777 "Return the least common multiple of all the arguments. | |
1778 The arguments must be integers and there must be at least one of them." | |
1779 (let ((howmany (length more)) | |
1780 (a integer) | |
1781 (b (nth 0 more)) | |
1782 prod ; intermediate product | |
1783 (yetmore (nthcdr 1 more))) | |
1784 (cond ((zerop howmany) | |
1785 (abs a)) | |
1786 ((> howmany 1) ; recursive case | |
1787 (apply (function lcm) | |
1788 (cons (lcm a b) yetmore))) | |
1789 (t ; base case, just 2 args | |
1790 (setq prod (* a b)) | |
1791 (cond | |
1792 ((zerop prod) | |
1793 0) | |
1794 (t | |
1795 (/ (abs prod) (gcd a b)))))))) | |
1796 | |
1797 (defun isqrt (number) | |
1798 "Return the integer square root of NUMBER. | |
1799 NUMBER must not be negative. Result is largest integer less than or | |
1800 equal to the real square root of the argument." | |
1801 ;; The method used here is essentially the Newtonian iteration | |
1802 ;; x[n+1] <- (x[n] + Number/x[n]) / 2 | |
1803 ;; suitably adapted to integer arithmetic. | |
1804 ;; Thanks to Philippe Schnoebelen <phs@lifia.imag.fr> for suggesting the | |
1805 ;; termination condition. | |
1806 (cond ((minusp number) | |
1807 (error "argument to `isqrt' (%d) must not be negative" | |
1808 number)) | |
1809 ((zerop number) | |
1810 0) | |
1811 (t ;so (>= number 0) | |
1812 (do* ((approx 1) ;any positive integer will do | |
1813 (new 0) ;init value irrelevant | |
1814 (done nil)) | |
1815 (done (if (> (* approx approx) number) | |
1816 (- approx 1) | |
1817 approx)) | |
1818 (setq new (/ (+ approx (/ number approx)) 2) | |
1819 done (or (= new approx) (= new (+ approx 1))) | |
1820 approx new))))) | |
1821 | |
1822 (defun floor (number &optional divisor) | |
1823 "Divide DIVIDEND by DIVISOR, rounding toward minus infinity. | |
1824 DIVISOR defaults to 1. The remainder is produced as a second value." | |
1825 (cond | |
1826 ((and (null divisor) ; trivial case | |
1827 (numberp number)) | |
1828 (values number 0)) | |
1829 (t ; do the division | |
1830 (multiple-value-bind | |
1831 (q r s) | |
1832 (safe-idiv number divisor) | |
1833 (cond ((zerop s) | |
1834 (values 0 0)) | |
1835 ((plusp s) | |
1836 (values q r)) | |
1837 (t ;opposite-signs case | |
1838 (if (zerop r) | |
1839 (values (- q) 0) | |
1840 (let ((q (- (+ q 1)))) | |
1841 (values q (- number (* q divisor))))))))))) | |
1842 | |
1843 (defun ceiling (number &optional divisor) | |
1844 "Divide DIVIDEND by DIVISOR, rounding toward plus infinity. | |
1845 DIVISOR defaults to 1. The remainder is produced as a second value." | |
1846 (cond | |
1847 ((and (null divisor) ; trivial case | |
1848 (numberp number)) | |
1849 (values number 0)) | |
1850 (t ; do the division | |
1851 (multiple-value-bind | |
1852 (q r s) | |
1853 (safe-idiv number divisor) | |
1854 (cond ((zerop s) | |
1855 (values 0 0)) | |
1856 ((plusp s) | |
1857 (values (+ q 1) (- r divisor))) | |
1858 (t | |
1859 (values (- q) (+ number (* q divisor))))))))) | |
1860 | |
1861 (defun truncate (number &optional divisor) | |
1862 "Divide DIVIDEND by DIVISOR, rounding toward zero. | |
1863 DIVISOR defaults to 1. The remainder is produced as a second value." | |
1864 (cond | |
1865 ((and (null divisor) ; trivial case | |
1866 (numberp number)) | |
1867 (values number 0)) | |
1868 (t ; do the division | |
1869 (multiple-value-bind | |
1870 (q r s) | |
1871 (safe-idiv number divisor) | |
1872 (cond ((zerop s) | |
1873 (values 0 0)) | |
1874 ((plusp s) ;same as floor | |
1875 (values q r)) | |
1876 (t ;same as ceiling | |
1877 (values (- q) (+ number (* q divisor))))))))) | |
1878 | |
1879 (defun round (number &optional divisor) | |
1880 "Divide DIVIDEND by DIVISOR, rounding to nearest integer. | |
1881 DIVISOR defaults to 1. The remainder is produced as a second value." | |
1882 (cond ((and (null divisor) ; trivial case | |
1883 (numberp number)) | |
1884 (values number 0)) | |
1885 (t ; do the division | |
1886 (multiple-value-bind | |
1887 (q r s) | |
1888 (safe-idiv number divisor) | |
1889 (setq r (abs r)) | |
1890 ;; adjust magnitudes first, and then signs | |
1891 (let ((other-r (- (abs divisor) r))) | |
1892 (cond ((> r other-r) | |
1893 (setq q (+ q 1))) | |
1894 ((and (= r other-r) | |
1895 (oddp q)) | |
1896 ;; round to even is mandatory | |
1897 (setq q (+ q 1)))) | |
1898 (setq q (* s q)) | |
1899 (setq r (- number (* q divisor))) | |
1900 (values q r)))))) | |
1901 | |
1902 (defun mod (number divisor) | |
1903 "Return remainder of X by Y (rounding quotient toward minus infinity). | |
1904 That is, the remainder goes with the quotient produced by `floor'." | |
1905 (multiple-value-bind (q r) (floor number divisor) | |
1906 r)) | |
1907 | |
1908 (defun rem (number divisor) | |
1909 "Return remainder of X by Y (rounding quotient toward zero). | |
1910 That is, the remainder goes with the quotient produced by `truncate'." | |
1911 (multiple-value-bind (q r) (truncate number divisor) | |
1912 r)) | |
1913 | |
1914 ;;; internal utilities | |
1915 ;;; | |
1916 ;;; safe-idiv performs an integer division with positive numbers only. | |
1917 ;;; It is known that some machines/compilers implement weird remainder | |
1918 ;;; computations when working with negatives, so the idea here is to | |
1919 ;;; make sure we know what is coming back to the caller in all cases. | |
1920 | |
1921 ;;; Signum computation fixed by mad@math.keio.JUNET (MAEDA Atusi) | |
1922 | |
1923 (defun safe-idiv (a b) | |
1924 "SAFE-IDIV A B => Q R S | |
1925 Q=|A|/|B|, R is the rest, S is the sign of A/B." | |
1926 (unless (and (numberp a) (numberp b)) | |
1927 (error "arguments to `safe-idiv' must be numbers")) | |
1928 (when (zerop b) | |
1929 (error "cannot divide %d by zero" a)) | |
1930 (let* ((absa (abs a)) | |
1931 (absb (abs b)) | |
1932 (q (/ absa absb)) | |
1933 (s (* (signum a) (signum b))) | |
1934 (r (- a (* (* s q) b)))) | |
1935 (values q r s))) | |
1936 | |
1937 ;;;; end of cl-arith.el | |
1938 | |
1939 ;;;; SETF | |
1940 ;;;; This file provides the setf macro and friends. The purpose has | |
1941 ;;;; been modest, only the simplest defsetf forms are accepted. | |
1942 ;;;; Use it and enjoy. | |
1943 ;;;; | |
1944 ;;;; Cesar Quiroz @ UofR DofCSc - Dec. 1986 | |
1945 ;;;; (quiroz@cs.rochester.edu) | |
1946 | |
1947 | |
1948 (defkeyword :setf-update-fn | |
1949 "Property, its value is the function setf must invoke to update a | |
1950 generalized variable whose access form is a function call of the | |
1951 symbol that has this property.") | |
1952 | |
1953 (defkeyword :setf-update-doc | |
1954 "Property of symbols that have a `defsetf' update function on them, | |
1955 installed by the `defsetf' from its optional third argument.") | |
1956 | |
1957 (defmacro setf (&rest pairs) | |
1958 "Generalized `setq' that can set things other than variable values. | |
1959 A use of `setf' looks like (setf {PLACE VALUE}...). | |
1960 The behavior of (setf PLACE VALUE) is to access the generalized variable | |
1961 at PLACE and store VALUE there. It returns VALUE. If there is more | |
1962 than one PLACE and VALUE, each PLACE is set from its VALUE before | |
1963 the next PLACE is evaluated." | |
1964 (let ((nforms (length pairs))) | |
1965 ;; check the number of subforms | |
1966 (cond ((/= (% nforms 2) 0) | |
1967 (error "odd number of arguments to `setf'")) | |
1968 ((= nforms 0) | |
1969 nil) | |
1970 ((> nforms 2) | |
1971 ;; this is the recursive case | |
1972 (cons 'progn | |
1973 (do* ;collect the place-value pairs | |
1974 ((args pairs (cddr args)) | |
1975 (place (car args) (car args)) | |
1976 (value (cadr args) (cadr args)) | |
1977 (result '())) | |
1978 ((endp args) (nreverse result)) | |
1979 (setq result | |
1980 (cons (list 'setf place value) | |
1981 result))))) | |
1982 (t ;i.e., nforms=2 | |
1983 ;; this is the base case (SETF PLACE VALUE) | |
1984 (let* ((place (car pairs)) | |
1985 (value (cadr pairs)) | |
1986 (head nil) | |
1987 (updatefn nil)) | |
1988 ;; dispatch on the type of the PLACE | |
1989 (cond ((symbolp place) | |
1990 (list 'setq place value)) | |
1991 ((and (listp place) | |
1992 (setq head (car place)) | |
1993 (symbolp head) | |
1994 (setq updatefn (get head :setf-update-fn))) | |
1995 (if (or (and (consp updatefn) (eq (car updatefn) 'lambda)) | |
1996 (and (symbolp updatefn) | |
1997 (fboundp updatefn) | |
1998 (let ((defn (symbol-function updatefn))) | |
1999 (or (subrp defn) | |
2000 (and (consp defn) | |
2001 (eq (car defn) 'lambda)))))) | |
2002 (cons updatefn (append (cdr place) (list value))) | |
2003 (multiple-value-bind | |
2004 (bindings newsyms) | |
2005 (pair-with-newsyms (append (cdr place) (list value))) | |
2006 ;; this let gets new symbols to ensure adequate | |
2007 ;; order of evaluation of the subforms. | |
2008 (list 'let | |
2009 bindings | |
2010 (cons updatefn newsyms))))) | |
2011 (t | |
2012 (error "no `setf' update-function for `%s'" | |
2013 (prin1-to-string place))))))))) | |
2014 | |
2015 (defmacro defsetf (accessfn updatefn &optional docstring) | |
2016 "Define how `setf' works on a certain kind of generalized variable. | |
2017 A use of `defsetf' looks like (defsetf ACCESSFN UPDATEFN [DOCSTRING]). | |
2018 ACCESSFN is a symbol. UPDATEFN is a function or macro which takes | |
2019 one more argument than ACCESSFN does. DEFSETF defines the translation | |
2020 of (SETF (ACCESFN . ARGS) NEWVAL) to be a form like (UPDATEFN ARGS... NEWVAL). | |
2021 The function UPDATEFN must return its last arg, after performing the | |
2022 updating called for." | |
2023 ;; reject ill-formed requests. too bad one can't test for functionp | |
2024 ;; or macrop. | |
2025 (when (not (symbolp accessfn)) | |
2026 (error "first argument of `defsetf' must be a symbol, not `%s'" | |
2027 (prin1-to-string accessfn))) | |
2028 ;; update properties | |
2029 (list 'progn | |
2030 (list 'put (list 'quote accessfn) | |
2031 :setf-update-fn (list 'function updatefn)) | |
2032 (list 'put (list 'quote accessfn) :setf-update-doc docstring) | |
2033 ;; any better thing to return? | |
2034 (list 'quote accessfn))) | |
2035 | |
2036 ;;; This section provides the "default" setfs for Common-Emacs-Lisp | |
2037 ;;; The user will not normally add anything to this, although | |
2038 ;;; defstruct will introduce new ones as a matter of fact. | |
2039 ;;; | |
2040 ;;; Apply is a special case. The Common Lisp | |
2041 ;;; standard makes the case of apply be useful when the user writes | |
2042 ;;; something like (apply #'name ...), Emacs Lisp doesn't have the # | |
2043 ;;; stuff, but it has (function ...). Notice that V18 includes a new | |
2044 ;;; apply: this file is compatible with V18 and pre-V18 Emacses. | |
2045 | |
2046 ;;; INCOMPATIBILITY: the SETF macro evaluates its arguments in the | |
2047 ;;; (correct) left to right sequence *before* checking for apply | |
2048 ;;; methods (which should really be an special case inside setf). Due | |
2049 ;;; to this, the lambda expression defsetf'd to apply will succeed in | |
2050 ;;; applying the right function even if the name was not quoted, but | |
2051 ;;; computed! That extension is not Common Lisp (nor is particularly | |
2052 ;;; useful, I think). | |
2053 | |
2054 (defsetf apply | |
2055 (lambda (&rest args) | |
2056 ;; dissasemble the calling form | |
2057 ;; "(((quote fn) x1 x2 ... xn) val)" (function instead of quote, too) | |
2058 (let* ((fnform (car args)) ;functional form | |
2059 (applyargs (append ;arguments "to apply fnform" | |
2060 (apply 'list* (butlast (cdr args))) | |
2061 (last args))) | |
2062 (newupdater nil)) ; its update-fn, if any | |
2063 (if (and (symbolp fnform) | |
2064 (setq newupdater (get fnform :setf-update-fn))) | |
2065 (apply newupdater applyargs) | |
2066 (error "can't `setf' to `%s'" | |
2067 (prin1-to-string fnform))))) | |
2068 "`apply' is a special case for `setf'") | |
2069 | |
2070 | |
2071 (defsetf aref | |
2072 aset | |
2073 "`setf' inversion for `aref'") | |
2074 | |
2075 (defsetf nth | |
2076 setnth | |
2077 "`setf' inversion for `nth'") | |
2078 | |
2079 (defsetf nthcdr | |
2080 setnthcdr | |
2081 "`setf' inversion for `nthcdr'") | |
2082 | |
2083 (defsetf elt | |
2084 setelt | |
2085 "`setf' inversion for `elt'") | |
2086 | |
2087 (defsetf first | |
2088 (lambda (list val) (setnth 0 list val)) | |
2089 "`setf' inversion for `first'") | |
2090 | |
2091 (defsetf second | |
2092 (lambda (list val) (setnth 1 list val)) | |
2093 "`setf' inversion for `second'") | |
2094 | |
2095 (defsetf third | |
2096 (lambda (list val) (setnth 2 list val)) | |
2097 "`setf' inversion for `third'") | |
2098 | |
2099 (defsetf fourth | |
2100 (lambda (list val) (setnth 3 list val)) | |
2101 "`setf' inversion for `fourth'") | |
2102 | |
2103 (defsetf fifth | |
2104 (lambda (list val) (setnth 4 list val)) | |
2105 "`setf' inversion for `fifth'") | |
2106 | |
2107 (defsetf sixth | |
2108 (lambda (list val) (setnth 5 list val)) | |
2109 "`setf' inversion for `sixth'") | |
2110 | |
2111 (defsetf seventh | |
2112 (lambda (list val) (setnth 6 list val)) | |
2113 "`setf' inversion for `seventh'") | |
2114 | |
2115 (defsetf eighth | |
2116 (lambda (list val) (setnth 7 list val)) | |
2117 "`setf' inversion for `eighth'") | |
2118 | |
2119 (defsetf ninth | |
2120 (lambda (list val) (setnth 8 list val)) | |
2121 "`setf' inversion for `ninth'") | |
2122 | |
2123 (defsetf tenth | |
2124 (lambda (list val) (setnth 9 list val)) | |
2125 "`setf' inversion for `tenth'") | |
2126 | |
2127 (defsetf rest | |
2128 (lambda (list val) (setcdr list val)) | |
2129 "`setf' inversion for `rest'") | |
2130 | |
2131 (defsetf car setcar "Replace the car of a cons") | |
2132 | |
2133 (defsetf cdr setcdr "Replace the cdr of a cons") | |
2134 | |
2135 (defsetf caar | |
2136 (lambda (list val) (setcar (nth 0 list) val)) | |
2137 "`setf' inversion for `caar'") | |
2138 | |
2139 (defsetf cadr | |
2140 (lambda (list val) (setcar (cdr list) val)) | |
2141 "`setf' inversion for `cadr'") | |
2142 | |
2143 (defsetf cdar | |
2144 (lambda (list val) (setcdr (car list) val)) | |
2145 "`setf' inversion for `cdar'") | |
2146 | |
2147 (defsetf cddr | |
2148 (lambda (list val) (setcdr (cdr list) val)) | |
2149 "`setf' inversion for `cddr'") | |
2150 | |
2151 (defsetf caaar | |
2152 (lambda (list val) (setcar (caar list) val)) | |
2153 "`setf' inversion for `caaar'") | |
2154 | |
2155 (defsetf caadr | |
2156 (lambda (list val) (setcar (cadr list) val)) | |
2157 "`setf' inversion for `caadr'") | |
2158 | |
2159 (defsetf cadar | |
2160 (lambda (list val) (setcar (cdar list) val)) | |
2161 "`setf' inversion for `cadar'") | |
2162 | |
2163 (defsetf cdaar | |
2164 (lambda (list val) (setcdr (caar list) val)) | |
2165 "`setf' inversion for `cdaar'") | |
2166 | |
2167 (defsetf caddr | |
2168 (lambda (list val) (setcar (cddr list) val)) | |
2169 "`setf' inversion for `caddr'") | |
2170 | |
2171 (defsetf cdadr | |
2172 (lambda (list val) (setcdr (cadr list) val)) | |
2173 "`setf' inversion for `cdadr'") | |
2174 | |
2175 (defsetf cddar | |
2176 (lambda (list val) (setcdr (cdar list) val)) | |
2177 "`setf' inversion for `cddar'") | |
2178 | |
2179 (defsetf cdddr | |
2180 (lambda (list val) (setcdr (cddr list) val)) | |
2181 "`setf' inversion for `cdddr'") | |
2182 | |
2183 (defsetf caaaar | |
2184 (lambda (list val) (setcar (caaar list) val)) | |
2185 "`setf' inversion for `caaaar'") | |
2186 | |
2187 (defsetf caaadr | |
2188 (lambda (list val) (setcar (caadr list) val)) | |
2189 "`setf' inversion for `caaadr'") | |
2190 | |
2191 (defsetf caadar | |
2192 (lambda (list val) (setcar (cadar list) val)) | |
2193 "`setf' inversion for `caadar'") | |
2194 | |
2195 (defsetf cadaar | |
2196 (lambda (list val) (setcar (cdaar list) val)) | |
2197 "`setf' inversion for `cadaar'") | |
2198 | |
2199 (defsetf cdaaar | |
2200 (lambda (list val) (setcdr (caar list) val)) | |
2201 "`setf' inversion for `cdaaar'") | |
2202 | |
2203 (defsetf caaddr | |
2204 (lambda (list val) (setcar (caddr list) val)) | |
2205 "`setf' inversion for `caaddr'") | |
2206 | |
2207 (defsetf cadadr | |
2208 (lambda (list val) (setcar (cdadr list) val)) | |
2209 "`setf' inversion for `cadadr'") | |
2210 | |
2211 (defsetf cdaadr | |
2212 (lambda (list val) (setcdr (caadr list) val)) | |
2213 "`setf' inversion for `cdaadr'") | |
2214 | |
2215 (defsetf caddar | |
2216 (lambda (list val) (setcar (cddar list) val)) | |
2217 "`setf' inversion for `caddar'") | |
2218 | |
2219 (defsetf cdadar | |
2220 (lambda (list val) (setcdr (cadar list) val)) | |
2221 "`setf' inversion for `cdadar'") | |
2222 | |
2223 (defsetf cddaar | |
2224 (lambda (list val) (setcdr (cdaar list) val)) | |
2225 "`setf' inversion for `cddaar'") | |
2226 | |
2227 (defsetf cadddr | |
2228 (lambda (list val) (setcar (cdddr list) val)) | |
2229 "`setf' inversion for `cadddr'") | |
2230 | |
2231 (defsetf cddadr | |
2232 (lambda (list val) (setcdr (cdadr list) val)) | |
2233 "`setf' inversion for `cddadr'") | |
2234 | |
2235 (defsetf cdaddr | |
2236 (lambda (list val) (setcdr (caddr list) val)) | |
2237 "`setf' inversion for `cdaddr'") | |
2238 | |
2239 (defsetf cdddar | |
2240 (lambda (list val) (setcdr (cddar list) val)) | |
2241 "`setf' inversion for `cdddar'") | |
2242 | |
2243 (defsetf cddddr | |
2244 (lambda (list val) (setcdr (cddr list) val)) | |
2245 "`setf' inversion for `cddddr'") | |
2246 | |
2247 (defsetf get put "`setf' inversion for `get' is `put'") | |
2248 | |
2249 (defsetf symbol-function fset | |
2250 "`setf' inversion for `symbol-function' is `fset'") | |
2251 | |
2252 (defsetf symbol-plist setplist | |
2253 "`setf' inversion for `symbol-plist' is `setplist'") | |
2254 | |
2255 (defsetf symbol-value set | |
2256 "`setf' inversion for `symbol-value' is `set'") | |
2257 | |
2258 (defsetf point goto-char | |
2259 "To set (point) to N, use (goto-char N)") | |
2260 | |
2261 ;; how about defsetfing other Emacs forms? | |
2262 | |
2263 ;;; Modify macros | |
2264 ;;; | |
2265 ;;; It could be nice to implement define-modify-macro, but I don't | |
2266 ;;; think it really pays. | |
2267 | |
2268 (defmacro incf (ref &optional delta) | |
2269 "(incf REF [DELTA]) -> increment the g.v. REF by DELTA (default 1)" | |
2270 (if (null delta) | |
2271 (setq delta 1)) | |
2272 (list 'setf ref (list '+ ref delta))) | |
2273 | |
2274 (defmacro decf (ref &optional delta) | |
2275 "(decf REF [DELTA]) -> decrement the g.v. REF by DELTA (default 1)" | |
2276 (if (null delta) | |
2277 (setq delta 1)) | |
2278 (list 'setf ref (list '- ref delta))) | |
2279 | |
2280 (defmacro push (item ref) | |
2281 "(push ITEM REF) -> cons ITEM at the head of the g.v. REF (a list)" | |
2282 (list 'setf ref (list 'cons item ref))) | |
2283 | |
2284 (defmacro pushnew (item ref) | |
2285 "(pushnew ITEM REF): adjoin ITEM at the head of the g.v. REF (a list)" | |
2286 (list 'setf ref (list 'adjoin item ref))) | |
2287 | |
2288 (defmacro pop (ref) | |
2289 "(pop REF) -> (prog1 (car REF) (setf REF (cdr REF)))" | |
2290 (let ((listname (gensym))) | |
2291 (list 'let (list (list listname ref)) | |
2292 (list 'prog1 | |
2293 (list 'car listname) | |
2294 (list 'setf ref (list 'cdr listname)))))) | |
2295 | |
2296 ;;; PSETF | |
2297 ;;; | |
2298 ;;; Psetf is the generalized variable equivalent of psetq. The right | |
2299 ;;; hand sides are evaluated and assigned (via setf) to the left hand | |
2300 ;;; sides. The evaluations are done in an environment where they | |
2301 ;;; appear to occur in parallel. | |
2302 | |
2303 (defmacro psetf (&rest body) | |
2304 "(psetf {var value }...) => nil | |
2305 Like setf, but all the values are computed before any assignment is made." | |
2306 (let ((length (length body))) | |
2307 (cond ((/= (% length 2) 0) | |
2308 (error "psetf needs an even number of arguments, %d given" | |
2309 length)) | |
2310 ((null body) | |
2311 '()) | |
2312 (t | |
2313 (list 'prog1 nil | |
2314 (let ((setfs '()) | |
2315 (bodyforms (reverse body))) | |
2316 (while bodyforms | |
2317 (let* ((value (car bodyforms)) | |
2318 (place (cadr bodyforms))) | |
2319 (setq bodyforms (cddr bodyforms)) | |
2320 (if (null setfs) | |
2321 (setq setfs (list 'setf place value)) | |
2322 (setq setfs (list 'setf place | |
2323 (list 'prog1 value | |
2324 setfs)))))) | |
2325 setfs)))))) | |
2326 | |
2327 ;;; SHIFTF and ROTATEF | |
2328 ;;; | |
2329 | |
2330 (defmacro shiftf (&rest forms) | |
2331 "(shiftf PLACE1 PLACE2... NEWVALUE) | |
2332 Set PLACE1 to PLACE2, PLACE2 to PLACE3... | |
2333 Each PLACE is set to the old value of the following PLACE, | |
2334 and the last PLACE is set to the value NEWVALUE. | |
2335 Returns the old value of PLACE1." | |
2336 (unless (> (length forms) 1) | |
2337 (error "`shiftf' needs more than one argument")) | |
2338 (let ((places (butlast forms)) | |
2339 (newvalue (car (last forms)))) | |
2340 ;; the places are accessed to fresh symbols | |
2341 (multiple-value-bind | |
2342 (bindings newsyms) | |
2343 (pair-with-newsyms places) | |
2344 (list 'let bindings | |
2345 (cons 'setf | |
2346 (zip-lists places | |
2347 (append (cdr newsyms) (list newvalue)))) | |
2348 (car newsyms))))) | |
2349 | |
2350 (defmacro rotatef (&rest places) | |
2351 "(rotatef PLACE...) sets each PLACE to the old value of the following PLACE. | |
2352 The last PLACE is set to the old value of the first PLACE. | |
2353 Thus, the values rotate through the PLACEs. Returns nil." | |
2354 (if (null places) | |
2355 nil | |
2356 (multiple-value-bind | |
2357 (bindings newsyms) | |
2358 (pair-with-newsyms places) | |
2359 (list | |
2360 'let bindings | |
2361 (cons 'setf | |
2362 (zip-lists places | |
2363 (append (cdr newsyms) (list (car newsyms))))) | |
2364 nil)))) | |
2365 | |
2366 ;;;; STRUCTS | |
2367 ;;;; This file provides the structures mechanism. See the | |
2368 ;;;; documentation for Common-Lisp's defstruct. Mine doesn't | |
2369 ;;;; implement all the functionality of the standard, although some | |
2370 ;;;; more could be grafted if so desired. More details along with | |
2371 ;;;; the code. | |
2372 ;;;; | |
2373 ;;;; | |
2374 ;;;; Cesar Quiroz @ UofR DofCSc - Dec. 1986 | |
2375 ;;;; (quiroz@cs.rochester.edu) | |
2376 | |
2377 | |
2378 (defkeyword :include "Syntax of `defstruct'") | |
2379 (defkeyword :named "Syntax of `defstruct'") | |
2380 (defkeyword :conc-name "Syntax of `defstruct'") | |
2381 (defkeyword :copier "Syntax of `defstruct'") | |
2382 (defkeyword :predicate "Syntax of `defstruct'") | |
2383 (defkeyword :print-function "Syntax of `defstruct'") | |
2384 (defkeyword :type "Syntax of `defstruct'") | |
2385 (defkeyword :initial-offset "Syntax of `defstruct'") | |
2386 | |
2387 (defkeyword :structure-doc "Documentation string for a structure.") | |
2388 (defkeyword :structure-slotsn "Number of slots in structure") | |
2389 (defkeyword :structure-slots "List of the slot's names") | |
2390 (defkeyword :structure-indices "List of (KEYWORD-NAME . INDEX)") | |
2391 (defkeyword :structure-initforms "List of (KEYWORD-NAME . INITFORM)") | |
2392 (defkeyword :structure-includes | |
2393 "() or list of a symbol, that this struct includes") | |
2394 (defkeyword :structure-included-in | |
2395 "List of the structs that include this") | |
2396 | |
2397 | |
2398 (defmacro defstruct (&rest args) | |
2399 "(defstruct NAME [DOC-STRING] . SLOTS) define NAME as structure type. | |
2400 NAME must be a symbol, the name of the new structure. It could also | |
2401 be a list (NAME . OPTIONS). | |
2402 | |
2403 Each option is either a symbol, or a list of a keyword symbol taken from the | |
2404 list \{:conc-name, :copier, :constructor, :predicate, :include, | |
2405 :print-function, :type, :initial-offset\}. The meanings of these are as in | |
2406 CLtL, except that no BOA-constructors are provided, and the options | |
2407 \{:print-fuction, :type, :initial-offset\} are ignored quietly. All these | |
2408 structs are named, in the sense that their names can be used for type | |
2409 discrimination. | |
2410 | |
2411 The DOC-STRING is established as the `structure-doc' property of NAME. | |
2412 | |
2413 The SLOTS are one or more of the following: | |
2414 SYMBOL -- meaning the SYMBOL is the name of a SLOT of NAME | |
2415 list of SYMBOL and VALUE -- meaning that VALUE is the initial value of | |
2416 the slot. | |
2417 `defstruct' defines functions `make-NAME', `NAME-p', `copy-NAME' for the | |
2418 structure, and functions with the same name as the slots to access | |
2419 them. `setf' of the accessors sets their values." | |
2420 (multiple-value-bind | |
2421 (name options docstring slotsn slots initlist) | |
2422 (parse$defstruct$args args) | |
2423 ;; Names for the member functions come from the options. The | |
2424 ;; slots* stuff collects info about the slots declared explicitly. | |
2425 (multiple-value-bind | |
2426 (conc-name constructor copier predicate | |
2427 moreslotsn moreslots moreinits included) | |
2428 (parse$defstruct$options name options slots) | |
2429 ;; The moreslots* stuff refers to slots gained as a consequence | |
2430 ;; of (:include clauses). -- Oct 89: Only one :include tolerated | |
2431 (when (and (numberp moreslotsn) | |
2432 (> moreslotsn 0)) | |
2433 (setf slotsn (+ slotsn moreslotsn)) | |
2434 (setf slots (append moreslots slots)) | |
2435 (setf initlist (append moreinits initlist))) | |
2436 (unless (> slotsn 0) | |
2437 (error "%s needs at least one slot" | |
2438 (prin1-to-string name))) | |
2439 (let ((dups (duplicate-symbols-p slots))) | |
2440 (when dups | |
2441 (error "`%s' are duplicates" | |
2442 (prin1-to-string dups)))) | |
2443 (setq initlist (simplify$inits slots initlist)) | |
2444 (let (properties functions keywords accessors alterators returned) | |
2445 ;; compute properties of NAME | |
2446 (setq properties | |
2447 (append | |
2448 (list | |
2449 (list 'put (list 'quote name) :structure-doc | |
2450 docstring) | |
2451 (list 'put (list 'quote name) :structure-slotsn | |
2452 slotsn) | |
2453 (list 'put (list 'quote name) :structure-slots | |
2454 (list 'quote slots)) | |
2455 (list 'put (list 'quote name) :structure-initforms | |
2456 (list 'quote initlist)) | |
2457 (list 'put (list 'quote name) :structure-indices | |
2458 (list 'quote (extract$indices initlist)))) | |
2459 ;; If this definition :includes another defstruct, | |
2460 ;; modify both property lists. | |
2461 (cond (included | |
2462 (list | |
2463 (list 'put | |
2464 (list 'quote name) | |
2465 :structure-includes | |
2466 (list 'quote included)) | |
2467 (list 'pushnew | |
2468 (list 'quote name) | |
2469 (list 'get (list 'quote (car included)) | |
2470 :structure-included-in)))) | |
2471 (t | |
2472 (list | |
2473 (let ((old (gensym))) | |
2474 (list 'let | |
2475 (list (list old | |
2476 (list 'car | |
2477 (list 'get | |
2478 (list 'quote name) | |
2479 :structure-includes)))) | |
2480 (list 'when old | |
2481 (list 'put | |
2482 old | |
2483 :structure-included-in | |
2484 (list 'delq | |
2485 (list 'quote name) | |
2486 ;; careful with destructive | |
2487 ;;manipulation! | |
2488 (list | |
2489 'append | |
2490 (list | |
2491 'get | |
2492 old | |
2493 :structure-included-in) | |
2494 '()) | |
2495 ))))) | |
2496 (list 'put | |
2497 (list 'quote name) | |
2498 :structure-includes | |
2499 '())))) | |
2500 ;; If this definition used to be :included in another, warn | |
2501 ;; that things make break. On the other hand, the redefinition | |
2502 ;; may be trivial, so don't call it an error. | |
2503 (let ((old (gensym))) | |
2504 (list | |
2505 (list 'let | |
2506 (list (list old (list 'get | |
2507 (list 'quote name) | |
2508 :structure-included-in))) | |
2509 (list 'when old | |
2510 (list 'message | |
2511 "`%s' redefined. Should redefine `%s'?" | |
2512 (list 'quote name) | |
2513 (list 'prin1-to-string old)))))))) | |
2514 | |
2515 ;; Compute functions associated with NAME. This is not | |
2516 ;; handling BOA constructors yet, but here would be the place. | |
2517 (setq functions | |
2518 (list | |
2519 (list 'fset (list 'quote constructor) | |
2520 (list 'function | |
2521 (list 'lambda (list '&rest 'args) | |
2522 (list 'make$structure$instance | |
2523 (list 'quote name) | |
2524 'args)))) | |
2525 (list 'fset (list 'quote copier) | |
2526 (list 'function | |
2527 (list 'lambda (list 'struct) | |
2528 (list 'copy-sequence 'struct)))) | |
2529 (let ((typetag (gensym))) | |
2530 (list 'fset (list 'quote predicate) | |
2531 (list | |
2532 'function | |
2533 (list | |
2534 'lambda (list 'thing) | |
2535 (list 'and | |
2536 (list 'vectorp 'thing) | |
2537 (list 'let | |
2538 (list (list typetag | |
2539 (list 'elt 'thing 0))) | |
2540 (list 'or | |
2541 (list | |
2542 'and | |
2543 (list 'eq | |
2544 typetag | |
2545 (list 'quote name)) | |
2546 (list '= | |
2547 (list 'length 'thing) | |
2548 (1+ slotsn))) | |
2549 (list | |
2550 'memq | |
2551 typetag | |
2552 (list 'get | |
2553 (list 'quote name) | |
2554 :structure-included-in)))))) | |
2555 ))))) | |
2556 ;; compute accessors for NAME's slots | |
2557 (multiple-value-setq | |
2558 (accessors alterators keywords) | |
2559 (build$accessors$for name conc-name predicate slots slotsn)) | |
2560 ;; generate returned value -- not defined by the standard | |
2561 (setq returned | |
2562 (list | |
2563 (cons 'vector | |
2564 (mapcar | |
2565 '(lambda (x) (list 'quote x)) | |
2566 (cons name slots))))) | |
2567 ;; generate code | |
2568 (cons 'progn | |
2569 (nconc properties functions keywords | |
2570 accessors alterators returned)))))) | |
2571 | |
2572 (defun parse$defstruct$args (args) | |
2573 "(parse$defstruct$args ARGS) => NAME OPTIONS DOCSTRING SLOTSN SLOTS INITLIST | |
2574 NAME=symbol, OPTIONS=list of, DOCSTRING=string, SLOTSN=count of slots, | |
2575 SLOTS=list of their names, INITLIST=alist (keyword . initform)." | |
2576 (let (name ;args=(symbol...) or ((symbol...)...) | |
2577 options ;args=((symbol . options) ...) | |
2578 (docstring "") ;args=(head docstring . slotargs) | |
2579 slotargs ;second or third cdr of args | |
2580 (slotsn 0) ;number of slots | |
2581 (slots '()) ;list of slot names | |
2582 (initlist '())) ;list of (slot keyword . initform) | |
2583 ;; extract name and options | |
2584 (cond ((symbolp (car args)) ;simple name | |
2585 (setq name (car args) | |
2586 options '())) | |
2587 ((and (listp (car args)) ;(name . options) | |
2588 (symbolp (caar args))) | |
2589 (setq name (caar args) | |
2590 options (cdar args))) | |
2591 (t | |
2592 (error "first arg to `defstruct' must be symbol or (symbol ...)"))) | |
2593 (setq slotargs (cdr args)) | |
2594 ;; is there a docstring? | |
2595 (when (stringp (car slotargs)) | |
2596 (setq docstring (car slotargs) | |
2597 slotargs (cdr slotargs))) | |
2598 ;; now for the slots | |
2599 (multiple-value-bind | |
2600 (slotsn slots initlist) | |
2601 (process$slots slotargs) | |
2602 (values name options docstring slotsn slots initlist)))) | |
2603 | |
2604 (defun process$slots (slots) | |
2605 "(process$slots SLOTS) => SLOTSN SLOTSLIST INITLIST | |
2606 Converts a list of symbols or lists of symbol and form into the last 3 | |
2607 values returned by PARSE$DEFSTRUCT$ARGS." | |
2608 (let ((slotsn (length slots)) ;number of slots | |
2609 slotslist ;(slot1 slot2 ...) | |
2610 initlist) ;((:slot1 . init1) ...) | |
2611 (do* | |
2612 ((ptr slots (cdr ptr)) | |
2613 (this (car ptr) (car ptr))) | |
2614 ((endp ptr)) | |
2615 (cond ((symbolp this) | |
2616 (setq slotslist (cons this slotslist)) | |
2617 (setq initlist (acons (keyword-of this) nil initlist))) | |
2618 ((and (listp this) | |
2619 (symbolp (car this))) | |
2620 (let ((name (car this)) | |
2621 (form (cadr this))) | |
2622 ;; this silently ignores any slot options. bad... | |
2623 (setq slotslist (cons name slotslist)) | |
2624 (setq initlist (acons (keyword-of name) form initlist)))) | |
2625 (t | |
2626 (error "slot should be symbol or (symbol ...), not `%s'" | |
2627 (prin1-to-string this))))) | |
2628 (values slotsn (nreverse slotslist) (nreverse initlist)))) | |
2629 | |
2630 (defun parse$defstruct$options (name options slots) | |
2631 "(parse$defstruct$options name OPTIONS SLOTS) => many values | |
2632 A defstruct named NAME, with options list OPTIONS, has already slots SLOTS. | |
2633 Parse the OPTIONS and return the updated form of the struct's slots and other | |
2634 information. The values returned are: | |
2635 | |
2636 CONC-NAME is the string to use as prefix/suffix in the methods, | |
2637 CONST is the name of the official constructor, | |
2638 COPIER is the name of the structure copier, | |
2639 PRED is the name of the type predicate, | |
2640 MORESLOTSN is the number of slots added by :include, | |
2641 MORESLOTS is the list of slots added by :include, | |
2642 MOREINITS is the list of initialization forms added by :include, | |
2643 INCLUDED is nil, or the list of the symbol added by :include" | |
2644 (let* ((namestring (symbol-name name)) | |
2645 ;; to build the return values | |
2646 (conc-name (concat namestring "-")) | |
2647 (const (intern (concat "make-" namestring))) | |
2648 (copier (intern (concat "copy-" namestring))) | |
2649 (pred (intern (concat namestring "-p"))) | |
2650 (moreslotsn 0) | |
2651 (moreslots '()) | |
2652 (moreinits '()) | |
2653 ;; auxiliaries | |
2654 option-head ;When an option is not a plain | |
2655 option-second ; keyword, it must be a list of | |
2656 option-rest ; the form (head second . rest) | |
2657 these-slotsn ;When :include is found, the | |
2658 these-slots ; info about the included | |
2659 these-inits ; structure is added here. | |
2660 included ;NIL or (list INCLUDED) | |
2661 ) | |
2662 ;; Values above are the defaults. Now we read the options themselves | |
2663 (dolist (option options) | |
2664 ;; 2 cases arise, as options must be a keyword or a list | |
2665 (cond | |
2666 ((keywordp option) | |
2667 (case option | |
2668 (:named | |
2669 ) ;ignore silently | |
2670 (t | |
2671 (error "can't recognize option `%s'" | |
2672 (prin1-to-string option))))) | |
2673 ((and (listp option) | |
2674 (keywordp (setq option-head (car option)))) | |
2675 (setq option-second (second option)) | |
2676 (setq option-rest (nthcdr 2 option)) | |
2677 (case option-head | |
2678 (:conc-name | |
2679 (setq conc-name | |
2680 (cond | |
2681 ((stringp option-second) | |
2682 option-second) | |
2683 ((null option-second) | |
2684 "") | |
2685 (t | |
2686 (error "`%s' is invalid as `conc-name'" | |
2687 (prin1-to-string option-second)))))) | |
2688 (:copier | |
2689 (setq copier | |
2690 (cond | |
2691 ((and (symbolp option-second) | |
2692 (null option-rest)) | |
2693 option-second) | |
2694 (t | |
2695 (error "can't recognize option `%s'" | |
2696 (prin1-to-string option)))))) | |
2697 | |
2698 (:constructor ;no BOA-constructors allowed | |
2699 (setq const | |
2700 (cond | |
2701 ((and (symbolp option-second) | |
2702 (null option-rest)) | |
2703 option-second) | |
2704 (t | |
2705 (error "can't recognize option `%s'" | |
2706 (prin1-to-string option)))))) | |
2707 (:predicate | |
2708 (setq pred | |
2709 (cond | |
2710 ((and (symbolp option-second) | |
2711 (null option-rest)) | |
2712 option-second) | |
2713 (t | |
2714 (error "can't recognize option `%s'" | |
2715 (prin1-to-string option)))))) | |
2716 (:include | |
2717 (unless (symbolp option-second) | |
2718 (error "arg to `:include' should be a symbol, not `%s'" | |
2719 (prin1-to-string option-second))) | |
2720 (setq these-slotsn (get option-second :structure-slotsn) | |
2721 these-slots (get option-second :structure-slots) | |
2722 these-inits (get option-second :structure-initforms)) | |
2723 (unless (and (numberp these-slotsn) | |
2724 (> these-slotsn 0)) | |
2725 (error "`%s' is not a valid structure" | |
2726 (prin1-to-string option-second))) | |
2727 (if included | |
2728 (error "`%s' already includes `%s', can't include `%s' too" | |
2729 name (car included) option-second) | |
2730 (push option-second included)) | |
2731 (multiple-value-bind | |
2732 (xtra-slotsn xtra-slots xtra-inits) | |
2733 (process$slots option-rest) | |
2734 (when (> xtra-slotsn 0) | |
2735 (dolist (xslot xtra-slots) | |
2736 (unless (memq xslot these-slots) | |
2737 (error "`%s' is not a slot of `%s'" | |
2738 (prin1-to-string xslot) | |
2739 (prin1-to-string option-second)))) | |
2740 (setq these-inits (append xtra-inits these-inits))) | |
2741 (setq moreslotsn (+ moreslotsn these-slotsn)) | |
2742 (setq moreslots (append these-slots moreslots)) | |
2743 (setq moreinits (append these-inits moreinits)))) | |
2744 ((:print-function :type :initial-offset) | |
2745 ) ;ignore silently | |
2746 (t | |
2747 (error "can't recognize option `%s'" | |
2748 (prin1-to-string option))))) | |
2749 (t | |
2750 (error "can't recognize option `%s'" | |
2751 (prin1-to-string option))))) | |
2752 ;; Return values found | |
2753 (values conc-name const copier pred | |
2754 moreslotsn moreslots moreinits | |
2755 included))) | |
2756 | |
2757 (defun simplify$inits (slots initlist) | |
2758 "(simplify$inits SLOTS INITLIST) => new INITLIST | |
2759 Removes from INITLIST - an ALIST - any shadowed bindings." | |
2760 (let ((result '()) ;built here | |
2761 key ;from the slot | |
2762 ) | |
2763 (dolist (slot slots) | |
2764 (setq key (keyword-of slot)) | |
2765 (setq result (acons key (cdr (assoc key initlist)) result))) | |
2766 (nreverse result))) | |
2767 | |
2768 (defun extract$indices (initlist) | |
2769 "(extract$indices INITLIST) => indices list | |
2770 Kludge. From a list of pairs (keyword . form) build a list of pairs | |
2771 of the form (keyword . position in list from 0). Useful to precompute | |
2772 some of the work of MAKE$STRUCTURE$INSTANCE." | |
2773 (let ((result '()) | |
2774 (index 0)) | |
2775 (dolist (entry initlist (nreverse result)) | |
2776 (setq result (acons (car entry) index result) | |
2777 index (+ index 1))))) | |
2778 | |
2779 (defun build$accessors$for (name conc-name predicate slots slotsn) | |
2780 "(build$accessors$for NAME PREDICATE SLOTS SLOTSN) => FSETS DEFSETFS KWDS | |
2781 Generate the code for accesors and defsetfs of a structure called | |
2782 NAME, whose slots are SLOTS. Also, establishes the keywords for the | |
2783 slots names." | |
2784 (do ((i 0 (1+ i)) | |
2785 (accessors '()) | |
2786 (alterators '()) | |
2787 (keywords '()) | |
2788 (canonic "")) ;slot name with conc-name prepended | |
2789 ((>= i slotsn) | |
2790 (values | |
2791 (nreverse accessors) (nreverse alterators) (nreverse keywords))) | |
2792 (setq canonic (intern (concat conc-name (symbol-name (nth i slots))))) | |
2793 (setq accessors | |
2794 (cons | |
2795 (list 'fset (list 'quote canonic) | |
2796 (list 'function | |
2797 (list 'lambda (list 'object) | |
2798 (list 'cond | |
2799 (list (list predicate 'object) | |
2800 (list 'aref 'object (1+ i))) | |
2801 (list 't | |
2802 (list 'error | |
2803 "`%s' is not a struct %s" | |
2804 (list 'prin1-to-string | |
2805 'object) | |
2806 (list 'prin1-to-string | |
2807 (list 'quote | |
2808 name)))))))) | |
2809 accessors)) | |
2810 (setq alterators | |
2811 (cons | |
2812 (list 'defsetf canonic | |
2813 (list 'lambda (list 'object 'newval) | |
2814 (list 'cond | |
2815 (list (list predicate 'object) | |
2816 (list 'aset 'object (1+ i) 'newval)) | |
2817 (list 't | |
2818 (list 'error | |
2819 "`%s' not a `%s'" | |
2820 (list 'prin1-to-string | |
2821 'object) | |
2822 (list 'prin1-to-string | |
2823 (list 'quote | |
2824 name))))))) | |
2825 alterators)) | |
2826 (setq keywords | |
2827 (cons (list 'defkeyword (keyword-of (nth i slots))) | |
2828 keywords)))) | |
2829 | |
2830 (defun make$structure$instance (name args) | |
2831 "(make$structure$instance NAME ARGS) => new struct NAME | |
2832 A struct of type NAME is created, some slots might be initialized | |
2833 according to ARGS (the &rest argument of MAKE-name)." | |
2834 (unless (symbolp name) | |
2835 (error "`%s' is not a possible name for a structure" | |
2836 (prin1-to-string name))) | |
2837 (let ((initforms (get name :structure-initforms)) | |
2838 (slotsn (get name :structure-slotsn)) | |
2839 (indices (get name :structure-indices)) | |
2840 initalist ;pairlis'd on initforms | |
2841 initializers ;definitive initializers | |
2842 ) | |
2843 ;; check sanity of the request | |
2844 (unless (and (numberp slotsn) | |
2845 (> slotsn 0)) | |
2846 (error "`%s' is not a defined structure" | |
2847 (prin1-to-string name))) | |
2848 (unless (evenp (length args)) | |
2849 (error "slot initializers `%s' not of even length" | |
2850 (prin1-to-string args))) | |
2851 ;; analyze the initializers provided by the call | |
2852 (multiple-value-bind | |
2853 (speckwds specvals) ;keywords and values given | |
2854 (unzip-list args) ; by the user | |
2855 ;; check that all the arguments are introduced by keywords | |
2856 (unless (every (function keywordp) speckwds) | |
2857 (error "all of the names in `%s' should be keywords" | |
2858 (prin1-to-string speckwds))) | |
2859 ;; check that all the keywords are known | |
2860 (dolist (kwd speckwds) | |
2861 (unless (numberp (cdr (assoc kwd indices))) | |
2862 (error "`%s' is not a valid slot name for %s" | |
2863 (prin1-to-string kwd) (prin1-to-string name)))) | |
2864 ;; update initforms | |
2865 (setq initalist | |
2866 (pairlis speckwds | |
2867 (do* ;;protect values from further evaluation | |
2868 ((ptr specvals (cdr ptr)) | |
2869 (val (car ptr) (car ptr)) | |
2870 (result '())) | |
2871 ((endp ptr) (nreverse result)) | |
2872 (setq result | |
2873 (cons (list 'quote val) | |
2874 result))) | |
2875 (copy-sequence initforms))) | |
2876 ;; compute definitive initializers | |
2877 (setq initializers | |
2878 (do* ;;gather the values of the most definitive forms | |
2879 ((ptr indices (cdr ptr)) | |
2880 (key (caar ptr) (caar ptr)) | |
2881 (result '())) | |
2882 ((endp ptr) (nreverse result)) | |
2883 (setq result | |
2884 (cons (eval (cdr (assoc key initalist))) result)))) | |
2885 ;; do real initialization | |
2886 (apply (function vector) | |
2887 (cons name initializers))))) | |
2888 | |
2889 ;;;; end of cl-structs.el | |
2890 | |
2891 ;;; For lisp-interaction mode, so that multiple values can be seen when passed | |
2892 ;;; back. Lies every now and then... | |
2893 | |
2894 (defvar - nil "form currently under evaluation") | |
2895 (defvar + nil "previous -") | |
2896 (defvar ++ nil "previous +") | |
2897 (defvar +++ nil "previous ++") | |
2898 (defvar / nil "list of values returned by +") | |
2899 (defvar // nil "list of values returned by ++") | |
2900 (defvar /// nil "list of values returned by +++") | |
2901 (defvar * nil "(first) value of +") | |
2902 (defvar ** nil "(first) value of ++") | |
2903 (defvar *** nil "(first) value of +++") | |
2904 | |
2905 (defun cl-eval-print-last-sexp () | |
2906 "Evaluate sexp before point; print value\(s\) into current buffer. | |
2907 If the evaled form returns multiple values, they are shown one to a line. | |
2908 The variables -, +, ++, +++, *, **, ***, /, //, /// have their usual meaning. | |
2909 | |
2910 It clears the multiple-value passing mechanism, and does not pass back | |
2911 multiple values. Use this only if you are debugging cl.el and understand well | |
2912 how the multiple-value stuff works, because it can be fooled into believing | |
2913 that multiple values have been returned when they actually haven't, for | |
2914 instance | |
2915 \(identity \(values nil 1\)\) | |
2916 However, even when this fails, you can trust the first printed value to be | |
2917 \(one of\) the returned value\(s\)." | |
2918 (interactive) | |
2919 ;; top level call, can reset mvalues | |
2920 (setq *mvalues-count* nil | |
2921 *mvalues-values* nil) | |
2922 (setq - (car (read-from-string | |
2923 (buffer-substring | |
2924 (let ((stab (syntax-table))) | |
2925 (unwind-protect | |
2926 (save-excursion | |
2927 (set-syntax-table emacs-lisp-mode-syntax-table) | |
2928 (forward-sexp -1) | |
2929 (point)) | |
2930 (set-syntax-table stab))) | |
2931 (point))))) | |
2932 (setq *** ** | |
2933 ** * | |
2934 * (eval -)) | |
2935 (setq /// // | |
2936 // / | |
2937 / *mvalues-values*) | |
2938 (setq +++ ++ | |
2939 ++ + | |
2940 + -) | |
2941 (cond ((or (null *mvalues-count*) ;mvalues mechanism not used | |
2942 (not (eq * (car *mvalues-values*)))) | |
2943 (print * (current-buffer))) | |
2944 ((null /) ;no values returned | |
2945 (terpri (current-buffer))) | |
2946 (t ;more than zero mvalues | |
2947 (terpri (current-buffer)) | |
2948 (mapcar (function (lambda (value) | |
2949 (prin1 value (current-buffer)) | |
2950 (terpri (current-buffer)))) | |
2951 /))) | |
2952 (setq *mvalues-count* nil ;make sure | |
2953 *mvalues-values* nil)) | |
2954 | |
2955 ;;;; More LISTS functions | |
2956 ;;;; | |
2957 | |
2958 ;;; Some mapping functions on lists, commonly useful. | |
2959 ;;; They take no extra sequences, to go along with Emacs Lisp's MAPCAR. | |
2960 | |
2961 (defun mapc (function list) | |
2962 "(MAPC FUNCTION LIST) => LIST | |
2963 Apply FUNCTION to each element of LIST, return LIST. | |
2964 Like mapcar, but called only for effect." | |
2965 (let ((args list)) | |
2966 (while args | |
2967 (funcall function (car args)) | |
2968 (setq args (cdr args)))) | |
2969 list) | |
2970 | |
2971 (defun maplist (function list) | |
2972 "(MAPLIST FUNCTION LIST) => list'ed results of FUNCTION on cdrs of LIST | |
2973 Apply FUNCTION to successive sublists of LIST, return the list of the results" | |
2974 (let ((args list) | |
2975 results '()) | |
2976 (while args | |
2977 (setq results (cons (funcall function args) results) | |
2978 args (cdr args))) | |
2979 (nreverse results))) | |
2980 | |
2981 (defun mapl (function list) | |
2982 "(MAPL FUNCTION LIST) => LIST | |
2983 Apply FUNCTION to successive cdrs of LIST, return LIST. | |
2984 Like maplist, but called only for effect." | |
2985 (let ((args list)) | |
2986 (while args | |
2987 (funcall function args) | |
2988 (setq args (cdr args))) | |
2989 list)) | |
2990 | |
2991 (defun mapcan (function list) | |
2992 "(MAPCAN FUNCTION LIST) => nconc'd results of FUNCTION on LIST | |
2993 Apply FUNCTION to each element of LIST, nconc the results. | |
2994 Beware: nconc destroys its first argument! See copy-list." | |
2995 (let ((args list) | |
2996 (results '())) | |
2997 (while args | |
2998 (setq results (nconc (funcall function (car args)) results) | |
2999 args (cdr args))) | |
3000 (nreverse results))) | |
3001 | |
3002 (defun mapcon (function list) | |
3003 "(MAPCON FUNCTION LIST) => nconc'd results of FUNCTION on cdrs of LIST | |
3004 Apply FUNCTION to successive sublists of LIST, nconc the results. | |
3005 Beware: nconc destroys its first argument! See copy-list." | |
3006 (let ((args list) | |
3007 (results '())) | |
3008 (while args | |
3009 (setq results (nconc (funcall function args) results) | |
3010 args (cdr args))) | |
3011 (nreverse results))) | |
3012 | |
3013 ;;; Copiers | |
3014 | |
3015 (defun copy-list (list) | |
3016 "Build a copy of LIST" | |
3017 (append list '())) | |
3018 | |
3019 (defun copy-tree (tree) | |
3020 "Build a copy of the tree of conses TREE | |
3021 The argument is a tree of conses, it is recursively copied down to | |
3022 non conses. Circularity and sharing of substructure are not | |
3023 necessarily preserved." | |
3024 (if (consp tree) | |
3025 (cons (copy-tree (car tree)) | |
3026 (copy-tree (cdr tree))) | |
3027 tree)) | |
3028 | |
3029 ;;; reversals, and destructive manipulations of a list's spine | |
3030 | |
3031 (defun revappend (x y) | |
3032 "does what (append (reverse X) Y) would, only faster" | |
3033 (if (endp x) | |
3034 y | |
3035 (revappend (cdr x) (cons (car x) y)))) | |
3036 | |
3037 (defun nreconc (x y) | |
3038 "does (nconc (nreverse X) Y) would, only faster | |
3039 Destructive on X, be careful." | |
3040 (if (endp x) | |
3041 y | |
3042 ;; reuse the first cons of x, making it point to y | |
3043 (nreconc (cdr x) (prog1 x (rplacd x y))))) | |
3044 | |
3045 (defun nbutlast (list &optional n) | |
3046 "Side-effected LIST truncated N+1 conses from the end. | |
3047 This is the destructive version of BUTLAST. Returns () and does not | |
3048 modify the LIST argument if the length of the list is not at least N." | |
3049 (when (null n) (setf n 1)) | |
3050 (let ((length (list-length list))) | |
3051 (cond ((null length) | |
3052 list) | |
3053 ((< length n) | |
3054 '()) | |
3055 (t | |
3056 (setnthcdr (- length n) list nil) | |
3057 list)))) | |
3058 | |
3059 ;;; Substitutions | |
3060 | |
3061 (defun subst (new old tree) | |
3062 "NEW replaces OLD in a copy of TREE | |
3063 Uses eql for the test." | |
3064 (subst-if new (function (lambda (x) (eql x old))) tree)) | |
3065 | |
3066 (defun subst-if-not (new test tree) | |
3067 "NEW replaces any subtree or leaf that fails TEST in a copy of TREE" | |
3068 ;; (subst-if new (function (lambda (x) (not (funcall test x)))) tree) | |
3069 (cond ((not (funcall test tree)) | |
3070 new) | |
3071 ((atom tree) | |
3072 tree) | |
3073 (t ;no match so far | |
3074 (let ((head (subst-if-not new test (car tree))) | |
3075 (tail (subst-if-not new test (cdr tree)))) | |
3076 ;; If nothing changed, return originals. Else use the new | |
3077 ;; components to assemble a new tree. | |
3078 (if (and (eql head (car tree)) | |
3079 (eql tail (cdr tree))) | |
3080 tree | |
3081 (cons head tail)))))) | |
3082 | |
3083 (defun subst-if (new test tree) | |
3084 "NEW replaces any subtree or leaf that satisfies TEST in a copy of TREE" | |
3085 (cond ((funcall test tree) | |
3086 new) | |
3087 ((atom tree) | |
3088 tree) | |
3089 (t ;no match so far | |
3090 (let ((head (subst-if new test (car tree))) | |
3091 (tail (subst-if new test (cdr tree)))) | |
3092 ;; If nothing changed, return originals. Else use the new | |
3093 ;; components to assemble a new tree. | |
3094 (if (and (eql head (car tree)) | |
3095 (eql tail (cdr tree))) | |
3096 tree | |
3097 (cons head tail)))))) | |
3098 | |
3099 (defun sublis (alist tree) | |
3100 "Use association list ALIST to modify a copy of TREE | |
3101 If a subtree or leaf of TREE is a key in ALIST, it is replaced by the | |
3102 associated value. Not exactly Common Lisp, but close in spirit and | |
3103 compatible with the native Emacs Lisp ASSOC, which uses EQUAL." | |
3104 (let ((toplevel (assoc tree alist))) | |
3105 (cond (toplevel ;Bingo at top | |
3106 (cdr toplevel)) | |
3107 ((atom tree) ;Give up on this | |
3108 tree) | |
3109 (t | |
3110 (let ((head (sublis alist (car tree))) | |
3111 (tail (sublis alist (cdr tree)))) | |
3112 (if (and (eql head (car tree)) | |
3113 (eql tail (cdr tree))) | |
3114 tree | |
3115 (cons head tail))))))) | |
3116 | |
3117 (defun member-if (predicate list) | |
3118 "PREDICATE is applied to the members of LIST. As soon as one of them | |
3119 returns true, that tail of the list if returned. Else NIL." | |
3120 (catch 'found-member-if | |
3121 (while (not (endp list)) | |
3122 (if (funcall predicate (car list)) | |
3123 (throw 'found-member-if list) | |
3124 (setq list (cdr list)))) | |
3125 nil)) | |
3126 | |
3127 (defun member-if-not (predicate list) | |
3128 "PREDICATE is applied to the members of LIST. As soon as one of them | |
3129 returns false, that tail of the list if returned. Else NIL." | |
3130 (catch 'found-member-if-not | |
3131 (while (not (endp list)) | |
3132 (if (funcall predicate (car list)) | |
3133 (setq list (cdr list)) | |
3134 (throw 'found-member-if-not list))) | |
3135 nil)) | |
3136 | |
3137 (defun tailp (sublist list) | |
3138 "(tailp SUBLIST LIST) => True if SUBLIST is a sublist of LIST." | |
3139 (catch 'tailp-found | |
3140 (while (not (endp list)) | |
3141 (if (eq sublist list) | |
3142 (throw 'tailp-found t) | |
3143 (setq list (cdr list)))) | |
3144 nil)) | |
3145 | |
3146 ;;; Suggestion of phr%widow.Berkeley.EDU@lilac.berkeley.edu | |
3147 | |
3148 (defmacro declare (&rest decls) | |
3149 "Ignore a Common-Lisp declaration." | |
3150 "declarations are ignored in this implementation") | |
3151 | |
3152 (defun proclaim (&rest decls) | |
3153 "Ignore a Common-Lisp proclamation." | |
3154 "declarations are ignored in this implementation") | |
3155 | |
3156 (defmacro the (type form) | |
3157 "(the TYPE FORM) macroexpands to FORM | |
3158 No checking is even attempted. This is just for compatibility with | |
3159 Common-Lisp codes." | |
3160 form) | |
3161 | |
662
8a533acedb77
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
584
diff
changeset
|
3162 (provide 'cl) |
584 | 3163 |
662
8a533acedb77
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
584
diff
changeset
|
3164 ;;; cl.el ends here |