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