Commit | Line | Data |
---|---|---|
ddc9006b | 1 | ;;; Guile Emacs Lisp -*- lexical-binding: t -*- |
6937c7aa BT |
2 | |
3 | ;;; Copyright (C) 2011 Free Software Foundation, Inc. | |
4 | ||
5 | ;;; This library is free software; you can redistribute it and/or modify | |
6 | ;;; it under the terms of the GNU Lesser General Public License as | |
7 | ;;; published by the Free Software Foundation; either version 3 of the | |
8 | ;;; License, or (at your option) any later version. | |
9 | ;;; | |
10 | ;;; This library is distributed in the hope that it will be useful, but | |
11 | ;;; WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 | ;;; Lesser General Public License for more details. | |
14 | ;;; | |
15 | ;;; You should have received a copy of the GNU Lesser General Public | |
16 | ;;; License along with this library; if not, write to the Free Software | |
17 | ;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA | |
18 | ;;; 02110-1301 USA | |
19 | ||
20 | ;;; Code: | |
9b15703d BT |
21 | |
22 | (defmacro @ (module symbol) | |
23 | `(guile-ref ,module ,symbol)) | |
24 | ||
fb50a753 RT |
25 | (defmacro @@ (module symbol) |
26 | `(guile-private-ref ,module ,symbol)) | |
27 | ||
1a58ce20 RT |
28 | (defmacro defun (name args &rest body) |
29 | `(let ((proc (function (lambda ,args ,@body)))) | |
30 | (%funcall (@ (language elisp runtime) set-symbol-function!) | |
31 | ',name | |
32 | proc) | |
33 | (%funcall (@ (guile) set-procedure-property!) | |
34 | proc 'name ',name) | |
35 | ',name)) | |
36 | ||
37 | (defun omega () (omega)) | |
38 | ||
b652e2b9 BT |
39 | (defmacro eval-and-compile (&rest body) |
40 | `(progn | |
41 | (eval-when-compile ,@body) | |
42 | (progn ,@body))) | |
43 | ||
a92f076c RT |
44 | (defmacro %define-compiler-macro (name args &rest body) |
45 | `(eval-and-compile | |
46 | (%funcall | |
47 | (@ (language elisp runtime) set-symbol-plist!) | |
48 | ',name | |
49 | (%funcall | |
50 | (@ (guile) cons*) | |
51 | '%compiler-macro | |
52 | #'(lambda ,args ,@body) | |
53 | (%funcall (@ (language elisp runtime) symbol-plist) ',name))) | |
54 | ',name)) | |
55 | ||
d88a217d RT |
56 | (defmacro defsubst (name args &rest body) |
57 | `(progn | |
58 | (defun ,name ,args ,@body) | |
59 | (eval-and-compile | |
60 | (%define-compiler-macro ,name (form) | |
61 | (%funcall (@ (guile) cons*) | |
62 | '%funcall | |
63 | (%funcall | |
64 | (@ (guile) list) | |
65 | 'function | |
66 | (%funcall (@ (guile) cons*) 'lambda ',args ',body)) | |
67 | (%funcall (@ (guile) cdr) form)))))) | |
68 | ||
1a58ce20 RT |
69 | (eval-and-compile |
70 | (defun eval (form) | |
71 | (%funcall (@ (language elisp runtime) eval-elisp) form))) | |
72 | ||
b652e2b9 | 73 | (eval-and-compile |
3c452993 RT |
74 | (defsubst null (object) |
75 | (declare (lexical object)) | |
b652e2b9 | 76 | (if object nil t)) |
3c452993 RT |
77 | (defsubst consp (x) |
78 | (declare (lexical x)) | |
79 | (%funcall (@ (guile) pair?) x)) | |
80 | (defsubst atom (x) | |
81 | (declare (lexical x)) | |
82 | (null (consp x))) | |
b652e2b9 | 83 | (defun listp (object) |
3c452993 | 84 | (declare (lexical object)) |
b652e2b9 | 85 | (if object (consp object) t)) |
3c452993 RT |
86 | (defsubst car (list) |
87 | (declare (lexical list)) | |
b05ca4ab | 88 | (if list (%funcall (@ (guile) car) list) nil)) |
3c452993 RT |
89 | (defsubst cdr (list) |
90 | (declare (lexical list)) | |
b05ca4ab BT |
91 | (if list (%funcall (@ (guile) cdr) list) nil)) |
92 | (defun make-symbol (name) | |
93 | (%funcall (@ (guile) make-symbol) name)) | |
1a58ce20 RT |
94 | (defun gensym () |
95 | (%funcall (@ (guile) gensym))) | |
5950f674 BT |
96 | (defun signal (error-symbol data) |
97 | (%funcall (@ (guile) throw) 'elisp-condition error-symbol data))) | |
b652e2b9 BT |
98 | |
99 | (defmacro lambda (&rest cdr) | |
100 | `#'(lambda ,@cdr)) | |
101 | ||
102 | (defmacro prog1 (first &rest body) | |
1a58ce20 | 103 | (let ((temp (gensym))) |
13f022c9 BT |
104 | `(let ((,temp ,first)) |
105 | (declare (lexical ,temp)) | |
b652e2b9 BT |
106 | ,@body |
107 | ,temp))) | |
108 | ||
1a58ce20 RT |
109 | (defun interactive (&optional arg) |
110 | nil) | |
111 | ||
b652e2b9 BT |
112 | (defmacro prog2 (form1 form2 &rest body) |
113 | `(progn ,form1 (prog1 ,form2 ,@body))) | |
114 | ||
115 | (defmacro cond (&rest clauses) | |
116 | (if (null clauses) | |
117 | nil | |
118 | (let ((first (car clauses)) | |
119 | (rest (cdr clauses))) | |
120 | (if (listp first) | |
121 | (let ((condition (car first)) | |
122 | (body (cdr first))) | |
123 | (if (null body) | |
1a58ce20 | 124 | (let ((temp (gensym))) |
13f022c9 BT |
125 | `(let ((,temp ,condition)) |
126 | (declare (lexical ,temp)) | |
b652e2b9 BT |
127 | (if ,temp |
128 | ,temp | |
129 | (cond ,@rest)))) | |
130 | `(if ,condition | |
131 | (progn ,@body) | |
132 | (cond ,@rest)))) | |
133 | (signal 'wrong-type-argument `(listp ,first)))))) | |
134 | ||
135 | (defmacro and (&rest conditions) | |
136 | (cond ((null conditions) t) | |
137 | ((null (cdr conditions)) (car conditions)) | |
138 | (t `(if ,(car conditions) | |
139 | (and ,@(cdr conditions)) | |
140 | nil)))) | |
141 | ||
142 | (defmacro or (&rest conditions) | |
143 | (cond ((null conditions) nil) | |
144 | ((null (cdr conditions)) (car conditions)) | |
1a58ce20 | 145 | (t (let ((temp (gensym))) |
13f022c9 BT |
146 | `(let ((,temp ,(car conditions))) |
147 | (declare (lexical ,temp)) | |
b652e2b9 BT |
148 | (if ,temp |
149 | ,temp | |
150 | (or ,@(cdr conditions)))))))) | |
151 | ||
9083c48d BT |
152 | (defmacro lexical-let (bindings &rest body) |
153 | (labels ((loop (list vars) | |
154 | (if (null list) | |
155 | `(let ,bindings | |
156 | (declare (lexical ,@vars)) | |
157 | ,@body) | |
158 | (loop (cdr list) | |
159 | (if (consp (car list)) | |
160 | `(,(car (car list)) ,@vars) | |
161 | `(,(car list) ,@vars)))))) | |
162 | (loop bindings '()))) | |
163 | ||
164 | (defmacro lexical-let* (bindings &rest body) | |
165 | (labels ((loop (list vars) | |
166 | (if (null list) | |
167 | `(let* ,bindings | |
168 | (declare (lexical ,@vars)) | |
169 | ,@body) | |
170 | (loop (cdr list) | |
171 | (if (consp (car list)) | |
172 | (cons (car (car list)) vars) | |
173 | (cons (car list) vars)))))) | |
174 | (loop bindings '()))) | |
175 | ||
9b90b453 | 176 | (defmacro while (test &rest body) |
1a58ce20 | 177 | (let ((loop (gensym))) |
9b90b453 BT |
178 | `(labels ((,loop () |
179 | (if ,test | |
180 | (progn ,@body (,loop)) | |
181 | nil))) | |
182 | (,loop)))) | |
183 | ||
b652e2b9 | 184 | (defmacro unwind-protect (bodyform &rest unwindforms) |
1a58ce20 RT |
185 | `(%funcall (@ (guile) dynamic-wind) |
186 | #'(lambda () nil) | |
187 | #'(lambda () ,bodyform) | |
188 | #'(lambda () ,@unwindforms))) | |
9b15703d | 189 | |
c0652730 BT |
190 | (defmacro when (cond &rest body) |
191 | `(if ,cond | |
192 | (progn ,@body))) | |
193 | ||
194 | (defmacro unless (cond &rest body) | |
195 | `(when (not ,cond) | |
196 | ,@body)) | |
197 | ||
b05ca4ab BT |
198 | (defun symbolp (object) |
199 | (%funcall (@ (guile) symbol?) object)) | |
200 | ||
1a58ce20 | 201 | (defun %functionp (object) |
b05ca4ab BT |
202 | (%funcall (@ (guile) procedure?) object)) |
203 | ||
204 | (defun symbol-function (symbol) | |
205 | (let ((f (%funcall (@ (language elisp runtime) symbol-function) | |
206 | symbol))) | |
207 | (if (%funcall (@ (language elisp falias) falias?) f) | |
208 | (%funcall (@ (language elisp falias) falias-object) f) | |
209 | f))) | |
97d9da9a | 210 | |
5bcc6d9e | 211 | (defun eval (form) |
b05ca4ab BT |
212 | (%funcall (@ (system base compile) compile) |
213 | form | |
214 | (%funcall (@ (guile) symbol->keyword) 'from) | |
215 | 'elisp | |
216 | (%funcall (@ (guile) symbol->keyword) 'to) | |
217 | 'value)) | |
218 | ||
219 | (defun %indirect-function (object) | |
220 | (cond | |
1a58ce20 | 221 | ((%functionp object) |
b05ca4ab | 222 | object) |
1a58ce20 RT |
223 | ((null object) |
224 | (signal 'void-function nil)) | |
b05ca4ab | 225 | ((symbolp object) ;++ cycle detection |
1a58ce20 RT |
226 | (%indirect-function |
227 | (%funcall (@ (language elisp runtime) symbol-function) object))) | |
b05ca4ab BT |
228 | ((listp object) |
229 | (eval `(function ,object))) | |
230 | (t | |
231 | (signal 'invalid-function `(,object))))) | |
232 | ||
233 | (defun apply (function &rest arguments) | |
234 | (%funcall (@ (guile) apply) | |
235 | (@ (guile) apply) | |
236 | (%indirect-function function) | |
237 | arguments)) | |
238 | ||
239 | (defun funcall (function &rest arguments) | |
240 | (%funcall (@ (guile) apply) | |
241 | (%indirect-function function) | |
242 | arguments)) | |
243 | ||
1a58ce20 RT |
244 | (defun autoload-do-load (fundef &optional funname macro-only) |
245 | (and (load (cadr fundef)) | |
246 | (%indirect-function funname))) | |
247 | ||
248 | (defun fset (symbol definition) | |
249 | (funcall (@ (language elisp runtime) set-symbol-function!) | |
250 | symbol | |
251 | definition)) | |
252 | ||
253 | (defun eq (obj1 obj2) | |
254 | (if obj1 | |
255 | (%funcall (@ (guile) eq?) obj1 obj2) | |
256 | (if obj2 nil t))) | |
257 | ||
258 | (defun nthcdr (n list) | |
259 | (let ((i 0)) | |
260 | (while (< i n) | |
261 | (setq list (cdr list) | |
262 | i (+ i 1))) | |
263 | list)) | |
264 | ||
265 | (defun nth (n list) | |
266 | (car (nthcdr n list))) | |
267 | ||
b05ca4ab BT |
268 | (defun fset (symbol definition) |
269 | (funcall (@ (language elisp runtime) set-symbol-function!) | |
270 | symbol | |
1a58ce20 RT |
271 | (cond |
272 | ((%funcall (@ (guile) procedure?) definition) | |
273 | definition) | |
274 | ((and (consp definition) | |
275 | (eq (car definition) 'macro)) | |
276 | (if (%funcall (@ (guile) procedure?) (cdr definition)) | |
277 | definition | |
278 | (cons 'macro | |
279 | (funcall (@ (language elisp falias) make-falias) | |
280 | (function | |
281 | (lambda (&rest args) (apply (cdr definition) args))) | |
282 | (cdr definition))))) | |
283 | ((and (consp definition) | |
284 | (eq (car definition) 'autoload)) | |
285 | (if (or (eq (nth 4 definition) 'macro) | |
286 | (eq (nth 4 definition) t)) | |
287 | (cons 'macro | |
288 | (funcall | |
289 | (@ (language elisp falias) make-falias) | |
290 | (function (lambda (&rest args) | |
291 | (apply (cdr (autoload-do-load definition symbol nil)) args))) | |
292 | definition)) | |
293 | (funcall | |
294 | (@ (language elisp falias) make-falias) | |
295 | (function (lambda (&rest args) | |
296 | (apply (autoload-do-load definition symbol nil) args))) | |
297 | definition))) | |
79403d94 RT |
298 | ((and (symbolp definition) |
299 | (let ((fn (symbol-function definition))) | |
300 | (and (consp fn) | |
301 | (or (eq (car fn) 'macro) | |
302 | (and (eq (car fn) 'autoload) | |
303 | (or (eq (nth 4 fn) 'macro) | |
304 | (eq (nth 4 fn) t))))))) | |
305 | (cons 'macro | |
306 | (funcall | |
307 | (@ (language elisp falias) make-falias) | |
308 | (function (lambda (&rest args) `(,definition ,@args))) | |
309 | definition))) | |
1a58ce20 | 310 | (t |
b05ca4ab | 311 | (funcall (@ (language elisp falias) make-falias) |
1a58ce20 RT |
312 | (function (lambda (&rest args) (apply definition args))) |
313 | definition)))) | |
b05ca4ab BT |
314 | definition) |
315 | ||
1a58ce20 | 316 | (defun gload (file) |
5bcc6d9e BT |
317 | (funcall (@ (system base compile) compile-file) |
318 | file | |
319 | (funcall (@ (guile) symbol->keyword) 'from) | |
320 | 'elisp | |
321 | (funcall (@ (guile) symbol->keyword) 'to) | |
322 | 'value) | |
323 | t) | |
324 | ||
9b15703d BT |
325 | ;;; Equality predicates |
326 | ||
0ab2a63a BT |
327 | (defun eql (obj1 obj2) |
328 | (if obj1 | |
329 | (funcall (@ (guile) eqv?) obj1 obj2) | |
330 | (null obj2))) | |
331 | ||
332 | (defun equal (obj1 obj2) | |
333 | (if obj1 | |
334 | (funcall (@ (guile) equal?) obj1 obj2) | |
335 | (null obj2))) | |
9b15703d | 336 | |
85bc6238 BT |
337 | ;;; Symbols |
338 | ||
b05ca4ab BT |
339 | ;;; `symbolp' and `symbol-function' are defined above. |
340 | ||
5c65ee51 | 341 | (fset 'symbol-name (@ (guile) symbol->string)) |
85bc6238 | 342 | (fset 'symbol-value (@ (language elisp runtime) symbol-value)) |
85bc6238 BT |
343 | (fset 'set (@ (language elisp runtime) set-symbol-value!)) |
344 | (fset 'makunbound (@ (language elisp runtime) makunbound!)) | |
345 | (fset 'fmakunbound (@ (language elisp runtime) fmakunbound!)) | |
346 | (fset 'boundp (@ (language elisp runtime) symbol-bound?)) | |
347 | (fset 'fboundp (@ (language elisp runtime) symbol-fbound?)) | |
5c65ee51 | 348 | (fset 'intern (@ (guile) string->symbol)) |
85bc6238 | 349 | |
1a58ce20 RT |
350 | ;(defun defvaralias (new-alias base-variable &optional docstring) |
351 | ; (let ((fluid (funcall (@ (language elisp runtime) symbol-fluid) | |
352 | ; base-variable))) | |
353 | ; (funcall (@ (language elisp runtime) set-symbol-fluid!) | |
354 | ; new-alias | |
355 | ; fluid) | |
356 | ; base-variable)) | |
14b288ce | 357 | |
9b15703d BT |
358 | ;;; Numerical type predicates |
359 | ||
360 | (defun floatp (object) | |
361 | (and (funcall (@ (guile) real?) object) | |
362 | (or (funcall (@ (guile) inexact?) object) | |
363 | (null (funcall (@ (guile) integer?) object))))) | |
364 | ||
365 | (defun integerp (object) | |
fc45b7e8 BT |
366 | (and (funcall (@ (guile) integer?) object) |
367 | (funcall (@ (guile) exact?) object))) | |
9b15703d BT |
368 | |
369 | (defun numberp (object) | |
370 | (funcall (@ (guile) real?) object)) | |
371 | ||
372 | (defun wholenump (object) | |
fc45b7e8 | 373 | (and (integerp object) (>= object 0))) |
9b15703d BT |
374 | |
375 | (defun zerop (object) | |
376 | (= object 0)) | |
377 | ||
378 | ;;; Numerical comparisons | |
379 | ||
380 | (fset '= (@ (guile) =)) | |
381 | ||
382 | (defun /= (num1 num2) | |
383 | (null (= num1 num2))) | |
384 | ||
385 | (fset '< (@ (guile) <)) | |
386 | (fset '<= (@ (guile) <=)) | |
387 | (fset '> (@ (guile) >)) | |
388 | (fset '>= (@ (guile) >=)) | |
389 | ||
390 | (defun max (&rest numbers) | |
391 | (apply (@ (guile) max) numbers)) | |
392 | ||
393 | (defun min (&rest numbers) | |
394 | (apply (@ (guile) min) numbers)) | |
395 | ||
396 | ;;; Arithmetic functions | |
397 | ||
398 | (fset '1+ (@ (guile) 1+)) | |
399 | (fset '1- (@ (guile) 1-)) | |
400 | (fset '+ (@ (guile) +)) | |
401 | (fset '- (@ (guile) -)) | |
402 | (fset '* (@ (guile) *)) | |
403 | (fset '% (@ (guile) modulo)) | |
404 | (fset 'abs (@ (guile) abs)) | |
405 | ||
406 | ;;; Floating-point rounding | |
407 | ||
408 | (fset 'ffloor (@ (guile) floor)) | |
409 | (fset 'fceiling (@ (guile) ceiling)) | |
410 | (fset 'ftruncate (@ (guile) truncate)) | |
411 | (fset 'fround (@ (guile) round)) | |
412 | ||
413 | ;;; Numeric conversion | |
414 | ||
415 | (defun float (arg) | |
416 | (if (numberp arg) | |
417 | (funcall (@ (guile) exact->inexact) arg) | |
418 | (signal 'wrong-type-argument `(numberp ,arg)))) | |
419 | ||
420 | ;;; List predicates | |
421 | ||
9b15703d BT |
422 | (fset 'not #'null) |
423 | ||
424 | (defun atom (object) | |
425 | (null (consp object))) | |
426 | ||
9b15703d BT |
427 | (defun nlistp (object) |
428 | (null (listp object))) | |
429 | ||
430 | ;;; Lists | |
431 | ||
432 | (fset 'cons (@ (guile) cons)) | |
433 | (fset 'list (@ (guile) list)) | |
434 | (fset 'make-list (@ (guile) make-list)) | |
435 | (fset 'append (@ (guile) append)) | |
436 | (fset 'reverse (@ (guile) reverse)) | |
12c00a04 | 437 | (fset 'nreverse (@ (guile) reverse!)) |
9b15703d | 438 | |
9b15703d BT |
439 | (defun car-safe (object) |
440 | (if (consp object) | |
441 | (car object) | |
442 | nil)) | |
443 | ||
444 | (defun cdr-safe (object) | |
445 | (if (consp object) | |
446 | (cdr object) | |
447 | nil)) | |
448 | ||
449 | (defun setcar (cell newcar) | |
450 | (if (consp cell) | |
451 | (progn | |
452 | (funcall (@ (guile) set-car!) cell newcar) | |
453 | newcar) | |
454 | (signal 'wrong-type-argument `(consp ,cell)))) | |
455 | ||
456 | (defun setcdr (cell newcdr) | |
457 | (if (consp cell) | |
458 | (progn | |
459 | (funcall (@ (guile) set-cdr!) cell newcdr) | |
460 | newcdr) | |
461 | (signal 'wrong-type-argument `(consp ,cell)))) | |
462 | ||
df9cd3b4 BT |
463 | (defun %member (elt list test) |
464 | (cond | |
465 | ((null list) nil) | |
466 | ((consp list) | |
467 | (if (funcall test elt (car list)) | |
468 | list | |
469 | (%member elt (cdr list) test))) | |
470 | (t (signal 'wrong-type-argument `(listp ,list))))) | |
471 | ||
472 | (defun member (elt list) | |
473 | (%member elt list #'equal)) | |
474 | ||
475 | (defun memql (elt list) | |
476 | (%member elt list #'eql)) | |
477 | ||
478 | (defun memq (elt list) | |
479 | (%member elt list #'eq)) | |
480 | ||
12c00a04 BT |
481 | (defun assoc (key list) |
482 | (funcall (@ (srfi srfi-1) assoc) key list #'equal)) | |
483 | ||
484 | (defun assq (key list) | |
485 | (funcall (@ (srfi srfi-1) assoc) key list #'eq)) | |
486 | ||
487 | (defun rplaca (cell newcar) | |
488 | (funcall (@ (guile) set-car!) cell newcar) | |
489 | newcar) | |
490 | ||
491 | (defun rplacd (cell newcdr) | |
492 | (funcall (@ (guile) set-cdr!) cell newcdr) | |
493 | newcdr) | |
494 | ||
495 | (defun caar (x) | |
496 | (car (car x))) | |
497 | ||
498 | (defun cadr (x) | |
499 | (car (cdr x))) | |
500 | ||
501 | (defun cdar (x) | |
502 | (cdr (car x))) | |
503 | ||
504 | (defun cddr (x) | |
505 | (cdr (cdr x))) | |
506 | ||
507 | (defmacro dolist (spec &rest body) | |
508 | (apply #'(lambda (var list &optional result) | |
1a58ce20 RT |
509 | (list 'progn |
510 | (list 'mapc | |
511 | (cons 'lambda (cons (list var) body)) | |
512 | list) | |
513 | result)) | |
12c00a04 BT |
514 | spec)) |
515 | ||
8f2f6566 BT |
516 | ;;; Strings |
517 | ||
518 | (defun string (&rest characters) | |
519 | (funcall (@ (guile) list->string) | |
520 | (mapcar (@ (guile) integer->char) characters))) | |
521 | ||
12c00a04 BT |
522 | (defun stringp (object) |
523 | (funcall (@ (guile) string?) object)) | |
524 | ||
525 | (defun string-equal (s1 s2) | |
526 | (let ((s1 (if (symbolp s1) (symbol-name s1) s1)) | |
527 | (s2 (if (symbolp s2) (symbol-name s2) s2))) | |
528 | (funcall (@ (guile) string=?) s1 s2))) | |
529 | ||
530 | (fset 'string= 'string-equal) | |
531 | ||
532 | (defun substring (string from &optional to) | |
533 | (apply (@ (guile) substring) string from (if to (list to) nil))) | |
534 | ||
535 | (defun upcase (obj) | |
536 | (funcall (@ (guile) string-upcase) obj)) | |
537 | ||
538 | (defun downcase (obj) | |
539 | (funcall (@ (guile) string-downcase) obj)) | |
540 | ||
541 | (defun string-match (regexp string &optional start) | |
542 | (let ((m (funcall (@ (ice-9 regex) string-match) | |
543 | regexp | |
544 | string | |
545 | (or start 0)))) | |
546 | (if m | |
547 | (funcall (@ (ice-9 regex) match:start) m 0) | |
548 | nil))) | |
549 | ||
550 | ;; Vectors | |
551 | ||
552 | (defun make-vector (length init) | |
553 | (funcall (@ (guile) make-vector) length init)) | |
554 | ||
9b15703d BT |
555 | ;;; Sequences |
556 | ||
ebc30e3f BT |
557 | (defun length (sequence) |
558 | (funcall (if (listp sequence) | |
559 | (@ (guile) length) | |
560 | (@ (guile) generalized-vector-length)) | |
561 | sequence)) | |
8f2f6566 BT |
562 | |
563 | (defun mapcar (function sequence) | |
564 | (funcall (@ (guile) map) function sequence)) | |
12ca82ca | 565 | |
12c00a04 BT |
566 | (defun mapc (function sequence) |
567 | (funcall (@ (guile) for-each) function sequence) | |
568 | sequence) | |
569 | ||
570 | (defun aref (array idx) | |
571 | (funcall (@ (guile) generalized-vector-ref) array idx)) | |
572 | ||
573 | (defun aset (array idx newelt) | |
574 | (funcall (@ (guile) generalized-vector-set!) array idx newelt) | |
575 | newelt) | |
576 | ||
577 | (defun concat (&rest sequences) | |
578 | (apply (@ (guile) string-append) sequences)) | |
579 | ||
12ca82ca BT |
580 | ;;; Property lists |
581 | ||
582 | (defun %plist-member (plist property test) | |
76c50ec5 BT |
583 | (cond |
584 | ((null plist) nil) | |
585 | ((consp plist) | |
586 | (if (funcall test (car plist) property) | |
587 | (cdr plist) | |
588 | (%plist-member (cdr (cdr plist)) property test))) | |
589 | (t (signal 'wrong-type-argument `(listp ,plist))))) | |
12ca82ca BT |
590 | |
591 | (defun %plist-get (plist property test) | |
592 | (car (%plist-member plist property test))) | |
593 | ||
594 | (defun %plist-put (plist property value test) | |
13f022c9 | 595 | (let ((x (%plist-member plist property test))) |
12ca82ca BT |
596 | (if x |
597 | (progn (setcar x value) plist) | |
598 | (cons property (cons value plist))))) | |
599 | ||
600 | (defun plist-get (plist property) | |
601 | (%plist-get plist property #'eq)) | |
602 | ||
603 | (defun plist-put (plist property value) | |
604 | (%plist-put plist property value #'eq)) | |
605 | ||
606 | (defun plist-member (plist property) | |
607 | (%plist-member plist property #'eq)) | |
608 | ||
609 | (defun lax-plist-get (plist property) | |
610 | (%plist-get plist property #'equal)) | |
611 | ||
612 | (defun lax-plist-put (plist property value) | |
613 | (%plist-put plist property value #'equal)) | |
614 | ||
615 | (defvar plist-function (funcall (@ (guile) make-object-property))) | |
616 | ||
617 | (defun symbol-plist (symbol) | |
618 | (funcall plist-function symbol)) | |
619 | ||
620 | (defun setplist (symbol plist) | |
621 | (funcall (funcall (@ (guile) setter) plist-function) symbol plist)) | |
622 | ||
623 | (defun get (symbol propname) | |
624 | (plist-get (symbol-plist symbol) propname)) | |
625 | ||
626 | (defun put (symbol propname value) | |
627 | (setplist symbol (plist-put (symbol-plist symbol) propname value))) | |
5950f674 BT |
628 | |
629 | ;;; Nonlocal exits | |
630 | ||
631 | (defmacro condition-case (var bodyform &rest handlers) | |
632 | (let ((key (make-symbol "key")) | |
633 | (error-symbol (make-symbol "error-symbol")) | |
634 | (data (make-symbol "data")) | |
635 | (conditions (make-symbol "conditions"))) | |
636 | (flet ((handler->cond-clause (handler) | |
637 | `((or ,@(mapcar #'(lambda (c) `(memq ',c ,conditions)) | |
638 | (if (consp (car handler)) | |
639 | (car handler) | |
640 | (list (car handler))))) | |
641 | ,@(cdr handler)))) | |
642 | `(funcall (@ (guile) catch) | |
643 | 'elisp-condition | |
644 | #'(lambda () ,bodyform) | |
645 | #'(lambda (,key ,error-symbol ,data) | |
b07a7449 | 646 | (declare (lexical ,key ,error-symbol ,data)) |
5950f674 BT |
647 | (let ((,conditions |
648 | (get ,error-symbol 'error-conditions)) | |
649 | ,@(if var | |
650 | `((,var (cons ,error-symbol ,data))) | |
651 | '())) | |
b07a7449 BT |
652 | (declare (lexical ,conditions |
653 | ,@(if var `(,var) '()))) | |
5950f674 BT |
654 | (cond ,@(mapcar #'handler->cond-clause handlers) |
655 | (t (signal ,error-symbol ,data))))))))) | |
ce9b7cc2 BT |
656 | |
657 | (put 'error 'error-conditions '(error)) | |
658 | (put 'wrong-type-argument 'error-conditions '(wrong-type-argument error)) | |
659 | (put 'invalid-function 'error-conditions '(invalid-function error)) | |
660 | (put 'no-catch 'error-conditions '(no-catch error)) | |
8fb67871 BT |
661 | (put 'throw 'error-conditions '(throw)) |
662 | ||
85b3dd6c BT |
663 | (defvar %catch nil) |
664 | ||
8fb67871 BT |
665 | (defmacro catch (tag &rest body) |
666 | (let ((tag-value (make-symbol "tag-value")) | |
667 | (c (make-symbol "c")) | |
668 | (data (make-symbol "data"))) | |
669 | `(let ((,tag-value ,tag)) | |
b07a7449 | 670 | (declare (lexical ,tag-value)) |
8fb67871 | 671 | (condition-case ,c |
85b3dd6c BT |
672 | (let ((%catch t)) |
673 | ,@body) | |
8fb67871 BT |
674 | (throw |
675 | (let ((,data (cdr ,c))) | |
b07a7449 | 676 | (declare (lexical ,data)) |
8fb67871 BT |
677 | (if (eq (car ,data) ,tag-value) |
678 | (car (cdr ,data)) | |
85b3dd6c | 679 | (apply #'throw ,data)))))))) |
ce9b7cc2 | 680 | |
8fb67871 | 681 | (defun throw (tag value) |
85b3dd6c | 682 | (signal (if %catch 'throw 'no-catch) (list tag value))) |
39864d20 BT |
683 | |
684 | ;;; I/O | |
685 | ||
686 | (defun princ (object) | |
687 | (funcall (@ (guile) display) object)) | |
688 | ||
689 | (defun print (object) | |
690 | (funcall (@ (guile) write) object)) | |
691 | ||
1a58ce20 RT |
692 | (defun prin1 (object) |
693 | (funcall (@ (guile) write) object)) | |
694 | ||
39864d20 BT |
695 | (defun terpri () |
696 | (funcall (@ (guile) newline))) | |
697 | ||
698 | (defun format* (stream string &rest args) | |
699 | (apply (@ (guile) format) stream string args)) | |
43ff6804 BT |
700 | |
701 | (defun send-string-to-terminal (string) | |
702 | (princ string)) | |
703 | ||
704 | (defun read-from-minibuffer (prompt &rest ignore) | |
705 | (princ prompt) | |
706 | (let ((value (funcall (@ (ice-9 rdelim) read-line)))) | |
707 | (if (funcall (@ (guile) eof-object?) value) | |
708 | "" | |
709 | value))) | |
710 | ||
711 | (defun prin1-to-string (object) | |
712 | (format* nil "~S" object)) | |
52d24724 BT |
713 | |
714 | ;; Random number generation | |
715 | ||
716 | (defvar %random-state (funcall (@ (guile) copy-random-state) | |
717 | (@ (guile) *random-state*))) | |
718 | ||
719 | (defun random (&optional limit) | |
1a58ce20 RT |
720 | (if (eq limit t) |
721 | (setq %random-state | |
722 | (funcall (@ (guile) random-state-from-platform)))) | |
723 | (funcall (@ (guile) random) | |
724 | (if (wholenump limit) | |
725 | limit | |
726 | (@ (guile) most-positive-fixnum)) | |
727 | %random-state)) | |
728 | ||
729 | (defmacro save-excursion (&rest body) | |
730 | `(call-with-save-excursion #'(lambda () ,@body))) | |
731 | ||
732 | (defmacro save-current-buffer (&rest body) | |
733 | `(call-with-save-current-buffer #'(lambda () ,@body))) | |
734 | ||
735 | (defmacro save-restriction (&rest body) | |
736 | `(call-with-save-restriction #'(lambda () ,@body))) | |
737 | ||
738 | (defmacro track-mouse (&rest body) | |
739 | `(call-with-track-mouse #'(lambda () ,@body))) | |
740 | ||
741 | (defmacro setq-default (var value &rest args) | |
742 | `(progn (set-default ',var ,value) | |
743 | ,(if (null args) | |
744 | var | |
745 | `(setq-default ,@args)))) | |
746 | ||
747 | (defmacro catch (tag &rest body) | |
748 | `(call-with-catch ,tag #'(lambda () ,@body))) | |
749 | ||
750 | (defmacro condition-case (var bodyform &rest args) | |
751 | (if (consp args) | |
752 | (let* ((handler (car args)) | |
753 | (handlers (cdr args)) | |
754 | (handler-conditions (car handler)) | |
755 | (handler-body (cdr handler))) | |
756 | `(call-with-handler ',var | |
757 | ',handler-conditions | |
758 | #'(lambda () ,@handler-body) | |
759 | #'(lambda () | |
760 | (condition-case ,var | |
761 | ,bodyform | |
762 | ,@handlers)))) | |
763 | bodyform)) | |
764 | ||
765 | (defun backtrace-frame (nframes) | |
766 | (let* ((stack (funcall (@ (guile) make-stack) t)) | |
767 | (frame (stack-ref stack nframes)) | |
768 | (proc (funcall (@ (guile) frame-procedure) frame)) | |
769 | (pname (or (and (%functionp proc) | |
770 | (funcall (@ (guile) procedure-name) proc)) | |
771 | proc)) | |
772 | (args (funcall (@ (guile) frame-arguments) frame))) | |
773 | (cons t (cons pname args)))) | |
774 | ||
775 | (defun backtrace () | |
776 | (interactive) | |
777 | (let* ((stack (funcall (@ (guile) make-stack) t)) | |
778 | (frame (funcall (@ (guile) stack-ref) stack 1)) | |
779 | (space (funcall (@ (guile) integer->char) 32))) | |
780 | (while frame | |
781 | (princ (string 32 32)) | |
782 | (let ((proc (funcall (@ (guile) frame-procedure) frame))) | |
783 | (prin1 (or (and (%functionp proc) | |
784 | (funcall (@ (guile) procedure-name) proc)) | |
785 | proc))) | |
786 | (prin1 (funcall (@ (guile) frame-arguments) frame)) | |
787 | (terpri) | |
788 | (setq frame (funcall (@ (guile) frame-previous) frame))) | |
789 | nil)) | |
790 | ||
791 | (defun %set-eager-macroexpansion-mode (ignore) | |
792 | nil) |