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