1 ;;; Guile Emacs Lisp -*- lexical-binding: t -*-
3 ;;; Copyright (C) 2011 Free Software Foundation, Inc.
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.
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.
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
22 (defmacro @ (module symbol
)
23 `(guile-ref ,module
,symbol
))
25 (defmacro @@ (module symbol
)
26 `(guile-private-ref ,module
,symbol
))
28 (defmacro defun
(name args
&rest body
)
29 `(let ((proc (function (lambda ,args
,@body
))))
30 (%funcall
(@ (language elisp runtime
) set-symbol-function
!)
33 (%funcall
(@ (guile) set-procedure-property
!)
37 (defun omega () (omega))
39 (defmacro eval-and-compile
(&rest body
)
41 (eval-when-compile ,@body
)
46 (%funcall
(@ (language elisp runtime
) eval-elisp
) form
)))
52 (%funcall
(@ (guile) pair?
) object
))
54 (if object
(consp object
) t
))
56 (if list
(%funcall
(@ (guile) car
) list
) nil
))
58 (if list
(%funcall
(@ (guile) cdr
) list
) nil
))
59 (defun make-symbol (name)
60 (%funcall
(@ (guile) make-symbol
) name
))
62 (%funcall
(@ (guile) gensym
)))
63 (defun signal (error-symbol data
)
64 (%funcall
(@ (guile) throw
) 'elisp-condition error-symbol data
)))
66 (defmacro lambda
(&rest cdr
)
69 (defmacro prog1
(first &rest body
)
70 (let ((temp (gensym)))
71 `(let ((,temp
,first
))
72 (declare (lexical ,temp
))
76 (defun interactive (&optional arg
)
79 (defmacro prog2
(form1 form2
&rest body
)
80 `(progn ,form1
(prog1 ,form2
,@body
)))
82 (defmacro cond
(&rest clauses
)
85 (let ((first (car clauses
))
88 (let ((condition (car first
))
91 (let ((temp (gensym)))
92 `(let ((,temp
,condition
))
93 (declare (lexical ,temp
))
100 (signal 'wrong-type-argument
`(listp ,first
))))))
102 (defmacro and
(&rest conditions
)
103 (cond ((null conditions
) t
)
104 ((null (cdr conditions
)) (car conditions
))
105 (t `(if ,(car conditions
)
106 (and ,@(cdr conditions
))
109 (defmacro or
(&rest conditions
)
110 (cond ((null conditions
) nil
)
111 ((null (cdr conditions
)) (car conditions
))
112 (t (let ((temp (gensym)))
113 `(let ((,temp
,(car conditions
)))
114 (declare (lexical ,temp
))
117 (or ,@(cdr conditions
))))))))
119 (defmacro lexical-let
(bindings &rest body
)
120 (labels ((loop (list vars
)
123 (declare (lexical ,@vars
))
126 (if (consp (car list
))
127 `(,(car (car list
)) ,@vars
)
128 `(,(car list
) ,@vars
))))))
129 (loop bindings
'())))
131 (defmacro lexical-let
* (bindings &rest body
)
132 (labels ((loop (list vars
)
135 (declare (lexical ,@vars
))
138 (if (consp (car list
))
139 (cons (car (car list
)) vars
)
140 (cons (car list
) vars
))))))
141 (loop bindings
'())))
143 (defmacro while
(test &rest body
)
144 (let ((loop (gensym)))
147 (progn ,@body
(,loop
))
151 (defmacro unwind-protect
(bodyform &rest unwindforms
)
152 `(%funcall
(@ (guile) dynamic-wind
)
154 #'(lambda () ,bodyform
)
155 #'(lambda () ,@unwindforms
)))
157 (defmacro when
(cond &rest body
)
161 (defmacro unless
(cond &rest body
)
165 (defun symbolp (object)
166 (%funcall
(@ (guile) symbol?
) object
))
168 (defun %functionp
(object)
169 (%funcall
(@ (guile) procedure?
) object
))
171 (defun symbol-function (symbol)
172 (let ((f (%funcall
(@ (language elisp runtime
) symbol-function
)
174 (if (%funcall
(@ (language elisp falias
) falias?
) f
)
175 (%funcall
(@ (language elisp falias
) falias-object
) f
)
179 (%funcall
(@ (system base compile
) compile
)
181 (%funcall
(@ (guile) symbol-
>keyword
) 'from
)
183 (%funcall
(@ (guile) symbol-
>keyword
) 'to
)
186 (defun %indirect-function
(object)
191 (signal 'void-function nil
))
192 ((symbolp object
) ;++ cycle detection
194 (%funcall
(@ (language elisp runtime
) symbol-function
) object
)))
196 (eval `(function ,object
)))
198 (signal 'invalid-function
`(,object
)))))
200 (defun apply (function &rest arguments
)
201 (%funcall
(@ (guile) apply
)
203 (%indirect-function function
)
206 (defun funcall (function &rest arguments
)
207 (%funcall
(@ (guile) apply
)
208 (%indirect-function function
)
211 (defun autoload-do-load (fundef &optional funname macro-only
)
212 (and (load (cadr fundef
))
213 (%indirect-function funname
)))
215 (defun fset (symbol definition
)
216 (funcall (@ (language elisp runtime
) set-symbol-function
!)
220 (defun eq (obj1 obj2
)
222 (%funcall
(@ (guile) eq?
) obj1 obj2
)
225 (defun nthcdr (n list
)
228 (setq list
(cdr list
)
233 (car (nthcdr n list
)))
235 (defun fset (symbol definition
)
236 (funcall (@ (language elisp runtime
) set-symbol-function
!)
239 ((%funcall
(@ (guile) procedure?
) definition
)
241 ((and (consp definition
)
242 (eq (car definition
) 'macro
))
243 (if (%funcall
(@ (guile) procedure?
) (cdr definition
))
246 (funcall (@ (language elisp falias
) make-falias
)
248 (lambda (&rest args
) (apply (cdr definition
) args
)))
250 ((and (consp definition
)
251 (eq (car definition
) 'autoload
))
252 (if (or (eq (nth 4 definition
) 'macro
)
253 (eq (nth 4 definition
) t
))
256 (@ (language elisp falias
) make-falias
)
257 (function (lambda (&rest args
)
258 (apply (cdr (autoload-do-load definition symbol nil
)) args
)))
261 (@ (language elisp falias
) make-falias
)
262 (function (lambda (&rest args
)
263 (apply (autoload-do-load definition symbol nil
) args
)))
266 (funcall (@ (language elisp falias
) make-falias
)
267 (function (lambda (&rest args
) (apply definition args
)))
272 (funcall (@ (system base compile
) compile-file
)
274 (funcall (@ (guile) symbol-
>keyword
) 'from
)
276 (funcall (@ (guile) symbol-
>keyword
) 'to
)
280 ;;; Equality predicates
282 (defun eql (obj1 obj2
)
284 (funcall (@ (guile) eqv?
) obj1 obj2
)
287 (defun equal (obj1 obj2
)
289 (funcall (@ (guile) equal?
) obj1 obj2
)
294 ;;; `symbolp' and `symbol-function' are defined above.
296 (fset 'symbol-name
(@ (guile) symbol-
>string
))
297 (fset 'symbol-value
(@ (language elisp runtime
) symbol-value
))
298 (fset 'set
(@ (language elisp runtime
) set-symbol-value
!))
299 (fset 'makunbound
(@ (language elisp runtime
) makunbound
!))
300 (fset 'fmakunbound
(@ (language elisp runtime
) fmakunbound
!))
301 (fset 'boundp
(@ (language elisp runtime
) symbol-bound?
))
302 (fset 'fboundp
(@ (language elisp runtime
) symbol-fbound?
))
303 (fset 'intern
(@ (guile) string-
>symbol
))
305 ;(defun defvaralias (new-alias base-variable &optional docstring)
306 ; (let ((fluid (funcall (@ (language elisp runtime) symbol-fluid)
308 ; (funcall (@ (language elisp runtime) set-symbol-fluid!)
313 ;;; Numerical type predicates
315 (defun floatp (object)
316 (and (funcall (@ (guile) real?
) object
)
317 (or (funcall (@ (guile) inexact?
) object
)
318 (null (funcall (@ (guile) integer?
) object
)))))
320 (defun integerp (object)
321 (and (funcall (@ (guile) integer?
) object
)
322 (funcall (@ (guile) exact?
) object
)))
324 (defun numberp (object)
325 (funcall (@ (guile) real?
) object
))
327 (defun wholenump (object)
328 (and (integerp object
) (>= object
0)))
330 (defun zerop (object)
333 ;;; Numerical comparisons
335 (fset '= (@ (guile) =))
337 (defun /= (num1 num2
)
338 (null (= num1 num2
)))
340 (fset '< (@ (guile) <))
341 (fset '<= (@ (guile) <=))
342 (fset '> (@ (guile) >))
343 (fset '>= (@ (guile) >=))
345 (defun max (&rest numbers
)
346 (apply (@ (guile) max
) numbers
))
348 (defun min (&rest numbers
)
349 (apply (@ (guile) min
) numbers
))
351 ;;; Arithmetic functions
353 (fset '1+ (@ (guile) 1+))
354 (fset '1-
(@ (guile) 1-
))
355 (fset '+ (@ (guile) +))
356 (fset '-
(@ (guile) -
))
357 (fset '* (@ (guile) *))
358 (fset '%
(@ (guile) modulo
))
359 (fset 'abs
(@ (guile) abs
))
361 ;;; Floating-point rounding
363 (fset 'ffloor
(@ (guile) floor
))
364 (fset 'fceiling
(@ (guile) ceiling
))
365 (fset 'ftruncate
(@ (guile) truncate
))
366 (fset 'fround
(@ (guile) round
))
368 ;;; Numeric conversion
372 (funcall (@ (guile) exact-
>inexact
) arg
)
373 (signal 'wrong-type-argument
`(numberp ,arg
))))
380 (null (consp object
)))
382 (defun nlistp (object)
383 (null (listp object
)))
387 (fset 'cons
(@ (guile) cons
))
388 (fset 'list
(@ (guile) list
))
389 (fset 'make-list
(@ (guile) make-list
))
390 (fset 'append
(@ (guile) append
))
391 (fset 'reverse
(@ (guile) reverse
))
392 (fset 'nreverse
(@ (guile) reverse
!))
394 (defun car-safe (object)
399 (defun cdr-safe (object)
404 (defun setcar (cell newcar
)
407 (funcall (@ (guile) set-car
!) cell newcar
)
409 (signal 'wrong-type-argument
`(consp ,cell
))))
411 (defun setcdr (cell newcdr
)
414 (funcall (@ (guile) set-cdr
!) cell newcdr
)
416 (signal 'wrong-type-argument
`(consp ,cell
))))
418 (defun %member
(elt list test
)
422 (if (funcall test elt
(car list
))
424 (%member elt
(cdr list
) test
)))
425 (t (signal 'wrong-type-argument
`(listp ,list
)))))
427 (defun member (elt list
)
428 (%member elt list
#'equal
))
430 (defun memql (elt list
)
431 (%member elt list
#'eql
))
433 (defun memq (elt list
)
434 (%member elt list
#'eq
))
436 (defun assoc (key list
)
437 (funcall (@ (srfi srfi-1
) assoc
) key list
#'equal
))
439 (defun assq (key list
)
440 (funcall (@ (srfi srfi-1
) assoc
) key list
#'eq
))
442 (defun rplaca (cell newcar
)
443 (funcall (@ (guile) set-car
!) cell newcar
)
446 (defun rplacd (cell newcdr
)
447 (funcall (@ (guile) set-cdr
!) cell newcdr
)
462 (defmacro dolist
(spec &rest body
)
463 (apply #'(lambda (var list
&optional result
)
466 (cons 'lambda
(cons (list var
) body
))
473 (defun string (&rest characters
)
474 (funcall (@ (guile) list-
>string
)
475 (mapcar (@ (guile) integer-
>char
) characters
)))
477 (defun stringp (object)
478 (funcall (@ (guile) string?
) object
))
480 (defun string-equal (s1 s2
)
481 (let ((s1 (if (symbolp s1
) (symbol-name s1
) s1
))
482 (s2 (if (symbolp s2
) (symbol-name s2
) s2
)))
483 (funcall (@ (guile) string
=?
) s1 s2
)))
485 (fset 'string
= 'string-equal
)
487 (defun substring (string from
&optional to
)
488 (apply (@ (guile) substring
) string from
(if to
(list to
) nil
)))
491 (funcall (@ (guile) string-upcase
) obj
))
493 (defun downcase (obj)
494 (funcall (@ (guile) string-downcase
) obj
))
496 (defun string-match (regexp string
&optional start
)
497 (let ((m (funcall (@ (ice-9 regex
) string-match
)
502 (funcall (@ (ice-9 regex
) match
:start
) m
0)
507 (defun make-vector (length init
)
508 (funcall (@ (guile) make-vector
) length init
))
512 (defun length (sequence)
513 (funcall (if (listp sequence
)
515 (@ (guile) generalized-vector-length
))
518 (defun mapcar (function sequence
)
519 (funcall (@ (guile) map
) function sequence
))
521 (defun mapc (function sequence
)
522 (funcall (@ (guile) for-each
) function sequence
)
525 (defun aref (array idx
)
526 (funcall (@ (guile) generalized-vector-ref
) array idx
))
528 (defun aset (array idx newelt
)
529 (funcall (@ (guile) generalized-vector-set
!) array idx newelt
)
532 (defun concat (&rest sequences
)
533 (apply (@ (guile) string-append
) sequences
))
537 (defun %plist-member
(plist property test
)
541 (if (funcall test
(car plist
) property
)
543 (%plist-member
(cdr (cdr plist
)) property test
)))
544 (t (signal 'wrong-type-argument
`(listp ,plist
)))))
546 (defun %plist-get
(plist property test
)
547 (car (%plist-member plist property test
)))
549 (defun %plist-put
(plist property value test
)
550 (let ((x (%plist-member plist property test
)))
552 (progn (setcar x value
) plist
)
553 (cons property
(cons value plist
)))))
555 (defun plist-get (plist property
)
556 (%plist-get plist property
#'eq
))
558 (defun plist-put (plist property value
)
559 (%plist-put plist property value
#'eq
))
561 (defun plist-member (plist property
)
562 (%plist-member plist property
#'eq
))
564 (defun lax-plist-get (plist property
)
565 (%plist-get plist property
#'equal
))
567 (defun lax-plist-put (plist property value
)
568 (%plist-put plist property value
#'equal
))
570 (defvar plist-function
(funcall (@ (guile) make-object-property
)))
572 (defun symbol-plist (symbol)
573 (funcall plist-function symbol
))
575 (defun setplist (symbol plist
)
576 (funcall (funcall (@ (guile) setter
) plist-function
) symbol plist
))
578 (defun get (symbol propname
)
579 (plist-get (symbol-plist symbol
) propname
))
581 (defun put (symbol propname value
)
582 (setplist symbol
(plist-put (symbol-plist symbol
) propname value
)))
586 (defmacro condition-case
(var bodyform
&rest handlers
)
587 (let ((key (make-symbol "key"))
588 (error-symbol (make-symbol "error-symbol"))
589 (data (make-symbol "data"))
590 (conditions (make-symbol "conditions")))
591 (flet ((handler->cond-clause
(handler)
592 `((or ,@(mapcar #'(lambda (c) `(memq ',c
,conditions
))
593 (if (consp (car handler
))
595 (list (car handler
)))))
597 `(funcall (@ (guile) catch
)
599 #'(lambda () ,bodyform
)
600 #'(lambda (,key
,error-symbol
,data
)
601 (declare (lexical ,key
,error-symbol
,data
))
603 (get ,error-symbol
'error-conditions
))
605 `((,var
(cons ,error-symbol
,data
)))
607 (declare (lexical ,conditions
608 ,@(if var
`(,var
) '())))
609 (cond ,@(mapcar #'handler-
>cond-clause handlers
)
610 (t (signal ,error-symbol
,data
)))))))))
612 (put 'error
'error-conditions
'(error))
613 (put 'wrong-type-argument
'error-conditions
'(wrong-type-argument error
))
614 (put 'invalid-function
'error-conditions
'(invalid-function error
))
615 (put 'no-catch
'error-conditions
'(no-catch error
))
616 (put 'throw
'error-conditions
'(throw))
620 (defmacro catch
(tag &rest body
)
621 (let ((tag-value (make-symbol "tag-value"))
622 (c (make-symbol "c"))
623 (data (make-symbol "data")))
624 `(let ((,tag-value
,tag
))
625 (declare (lexical ,tag-value
))
630 (let ((,data
(cdr ,c
)))
631 (declare (lexical ,data
))
632 (if (eq (car ,data
) ,tag-value
)
634 (apply #'throw
,data
))))))))
636 (defun throw (tag value
)
637 (signal (if %catch
'throw
'no-catch
) (list tag value
)))
641 (defun princ (object)
642 (funcall (@ (guile) display
) object
))
644 (defun print (object)
645 (funcall (@ (guile) write
) object
))
647 (defun prin1 (object)
648 (funcall (@ (guile) write
) object
))
651 (funcall (@ (guile) newline
)))
653 (defun format* (stream string
&rest args
)
654 (apply (@ (guile) format
) stream string args
))
656 (defun send-string-to-terminal (string)
659 (defun read-from-minibuffer (prompt &rest ignore
)
661 (let ((value (funcall (@ (ice-9 rdelim
) read-line
))))
662 (if (funcall (@ (guile) eof-object?
) value
)
666 (defun prin1-to-string (object)
667 (format* nil
"~S" object
))
669 ;; Random number generation
671 (defvar %random-state
(funcall (@ (guile) copy-random-state
)
672 (@ (guile) *random-state
*)))
674 (defun random (&optional limit
)
677 (funcall (@ (guile) random-state-from-platform
))))
678 (funcall (@ (guile) random
)
679 (if (wholenump limit
)
681 (@ (guile) most-positive-fixnum
))
684 (defmacro save-excursion
(&rest body
)
685 `(call-with-save-excursion #'(lambda () ,@body
)))
687 (defmacro save-current-buffer
(&rest body
)
688 `(call-with-save-current-buffer #'(lambda () ,@body
)))
690 (defmacro save-restriction
(&rest body
)
691 `(call-with-save-restriction #'(lambda () ,@body
)))
693 (defmacro track-mouse
(&rest body
)
694 `(call-with-track-mouse #'(lambda () ,@body
)))
696 (defmacro setq-default
(var value
&rest args
)
697 `(progn (set-default ',var
,value
)
700 `(setq-default ,@args
))))
702 (defmacro catch
(tag &rest body
)
703 `(call-with-catch ,tag
#'(lambda () ,@body
)))
705 (defmacro condition-case
(var bodyform
&rest args
)
707 (let* ((handler (car args
))
708 (handlers (cdr args
))
709 (handler-conditions (car handler
))
710 (handler-body (cdr handler
)))
711 `(call-with-handler ',var
713 #'(lambda () ,@handler-body
)
720 (defun backtrace-frame (nframes)
721 (let* ((stack (funcall (@ (guile) make-stack
) t
))
722 (frame (stack-ref stack nframes
))
723 (proc (funcall (@ (guile) frame-procedure
) frame
))
724 (pname (or (and (%functionp proc
)
725 (funcall (@ (guile) procedure-name
) proc
))
727 (args (funcall (@ (guile) frame-arguments
) frame
)))
728 (cons t
(cons pname args
))))
732 (let* ((stack (funcall (@ (guile) make-stack
) t
))
733 (frame (funcall (@ (guile) stack-ref
) stack
1))
734 (space (funcall (@ (guile) integer-
>char
) 32)))
736 (princ (string 32 32))
737 (let ((proc (funcall (@ (guile) frame-procedure
) frame
)))
738 (prin1 (or (and (%functionp proc
)
739 (funcall (@ (guile) procedure-name
) proc
))
741 (prin1 (funcall (@ (guile) frame-arguments
) frame
))
743 (setq frame
(funcall (@ (guile) frame-previous
) frame
)))
746 (defun %set-eager-macroexpansion-mode
(ignore)