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 defun
(name args
&rest body
)
26 `(let ((proc (function (lambda ,args
,@body
))))
27 (%funcall
(@ (language elisp runtime
) set-symbol-function
!)
30 (%funcall
(@ (guile) set-procedure-property
!)
34 (defun omega () (omega))
36 (defmacro eval-and-compile
(&rest body
)
38 (eval-when-compile ,@body
)
43 (%funcall
(@ (language elisp runtime
) eval-elisp
) form
)))
49 (%funcall
(@ (guile) pair?
) object
))
51 (if object
(consp object
) t
))
53 (if list
(%funcall
(@ (guile) car
) list
) nil
))
55 (if list
(%funcall
(@ (guile) cdr
) list
) nil
))
56 (defun make-symbol (name)
57 (%funcall
(@ (guile) make-symbol
) name
))
59 (%funcall
(@ (guile) gensym
)))
60 (defun signal (error-symbol data
)
61 (%funcall
(@ (guile) throw
) 'elisp-condition error-symbol data
)))
63 (defmacro lambda
(&rest cdr
)
66 (defmacro prog1
(first &rest body
)
67 (let ((temp (gensym)))
68 `(let ((,temp
,first
))
69 (declare (lexical ,temp
))
73 (defun interactive (&optional arg
)
76 (defmacro prog2
(form1 form2
&rest body
)
77 `(progn ,form1
(prog1 ,form2
,@body
)))
79 (defmacro cond
(&rest clauses
)
82 (let ((first (car clauses
))
85 (let ((condition (car first
))
88 (let ((temp (gensym)))
89 `(let ((,temp
,condition
))
90 (declare (lexical ,temp
))
97 (signal 'wrong-type-argument
`(listp ,first
))))))
99 (defmacro and
(&rest conditions
)
100 (cond ((null conditions
) t
)
101 ((null (cdr conditions
)) (car conditions
))
102 (t `(if ,(car conditions
)
103 (and ,@(cdr conditions
))
106 (defmacro or
(&rest conditions
)
107 (cond ((null conditions
) nil
)
108 ((null (cdr conditions
)) (car conditions
))
109 (t (let ((temp (gensym)))
110 `(let ((,temp
,(car conditions
)))
111 (declare (lexical ,temp
))
114 (or ,@(cdr conditions
))))))))
116 (defmacro lexical-let
(bindings &rest body
)
117 (labels ((loop (list vars
)
120 (declare (lexical ,@vars
))
123 (if (consp (car list
))
124 `(,(car (car list
)) ,@vars
)
125 `(,(car list
) ,@vars
))))))
126 (loop bindings
'())))
128 (defmacro lexical-let
* (bindings &rest body
)
129 (labels ((loop (list vars
)
132 (declare (lexical ,@vars
))
135 (if (consp (car list
))
136 (cons (car (car list
)) vars
)
137 (cons (car list
) vars
))))))
138 (loop bindings
'())))
140 (defmacro while
(test &rest body
)
141 (let ((loop (gensym)))
144 (progn ,@body
(,loop
))
148 (defmacro unwind-protect
(bodyform &rest unwindforms
)
149 `(%funcall
(@ (guile) dynamic-wind
)
151 #'(lambda () ,bodyform
)
152 #'(lambda () ,@unwindforms
)))
154 (defmacro when
(cond &rest body
)
158 (defmacro unless
(cond &rest body
)
162 (defun symbolp (object)
163 (%funcall
(@ (guile) symbol?
) object
))
165 (defun %functionp
(object)
166 (%funcall
(@ (guile) procedure?
) object
))
168 (defun symbol-function (symbol)
169 (let ((f (%funcall
(@ (language elisp runtime
) symbol-function
)
171 (if (%funcall
(@ (language elisp falias
) falias?
) f
)
172 (%funcall
(@ (language elisp falias
) falias-object
) f
)
176 (%funcall
(@ (system base compile
) compile
)
178 (%funcall
(@ (guile) symbol-
>keyword
) 'from
)
180 (%funcall
(@ (guile) symbol-
>keyword
) 'to
)
183 (defun %indirect-function
(object)
188 (signal 'void-function nil
))
189 ((symbolp object
) ;++ cycle detection
191 (%funcall
(@ (language elisp runtime
) symbol-function
) object
)))
193 (eval `(function ,object
)))
195 (signal 'invalid-function
`(,object
)))))
197 (defun apply (function &rest arguments
)
198 (%funcall
(@ (guile) apply
)
200 (%indirect-function function
)
203 (defun funcall (function &rest arguments
)
204 (%funcall
(@ (guile) apply
)
205 (%indirect-function function
)
208 (defun autoload-do-load (fundef &optional funname macro-only
)
209 (and (load (cadr fundef
))
210 (%indirect-function funname
)))
212 (defun fset (symbol definition
)
213 (funcall (@ (language elisp runtime
) set-symbol-function
!)
217 (defun eq (obj1 obj2
)
219 (%funcall
(@ (guile) eq?
) obj1 obj2
)
222 (defun nthcdr (n list
)
225 (setq list
(cdr list
)
230 (car (nthcdr n list
)))
232 (defun fset (symbol definition
)
233 (funcall (@ (language elisp runtime
) set-symbol-function
!)
236 ((%funcall
(@ (guile) procedure?
) definition
)
238 ((and (consp definition
)
239 (eq (car definition
) 'macro
))
240 (if (%funcall
(@ (guile) procedure?
) (cdr definition
))
243 (funcall (@ (language elisp falias
) make-falias
)
245 (lambda (&rest args
) (apply (cdr definition
) args
)))
247 ((and (consp definition
)
248 (eq (car definition
) 'autoload
))
249 (if (or (eq (nth 4 definition
) 'macro
)
250 (eq (nth 4 definition
) t
))
253 (@ (language elisp falias
) make-falias
)
254 (function (lambda (&rest args
)
255 (apply (cdr (autoload-do-load definition symbol nil
)) args
)))
258 (@ (language elisp falias
) make-falias
)
259 (function (lambda (&rest args
)
260 (apply (autoload-do-load definition symbol nil
) args
)))
263 (funcall (@ (language elisp falias
) make-falias
)
264 (function (lambda (&rest args
) (apply definition args
)))
269 (funcall (@ (system base compile
) compile-file
)
271 (funcall (@ (guile) symbol-
>keyword
) 'from
)
273 (funcall (@ (guile) symbol-
>keyword
) 'to
)
277 ;;; Equality predicates
279 (defun eql (obj1 obj2
)
281 (funcall (@ (guile) eqv?
) obj1 obj2
)
284 (defun equal (obj1 obj2
)
286 (funcall (@ (guile) equal?
) obj1 obj2
)
291 ;;; `symbolp' and `symbol-function' are defined above.
293 (fset 'symbol-name
(@ (guile) symbol-
>string
))
294 (fset 'symbol-value
(@ (language elisp runtime
) symbol-value
))
295 (fset 'set
(@ (language elisp runtime
) set-symbol-value
!))
296 (fset 'makunbound
(@ (language elisp runtime
) makunbound
!))
297 (fset 'fmakunbound
(@ (language elisp runtime
) fmakunbound
!))
298 (fset 'boundp
(@ (language elisp runtime
) symbol-bound?
))
299 (fset 'fboundp
(@ (language elisp runtime
) symbol-fbound?
))
300 (fset 'intern
(@ (guile) string-
>symbol
))
302 ;(defun defvaralias (new-alias base-variable &optional docstring)
303 ; (let ((fluid (funcall (@ (language elisp runtime) symbol-fluid)
305 ; (funcall (@ (language elisp runtime) set-symbol-fluid!)
310 ;;; Numerical type predicates
312 (defun floatp (object)
313 (and (funcall (@ (guile) real?
) object
)
314 (or (funcall (@ (guile) inexact?
) object
)
315 (null (funcall (@ (guile) integer?
) object
)))))
317 (defun integerp (object)
318 (and (funcall (@ (guile) integer?
) object
)
319 (funcall (@ (guile) exact?
) object
)))
321 (defun numberp (object)
322 (funcall (@ (guile) real?
) object
))
324 (defun wholenump (object)
325 (and (integerp object
) (>= object
0)))
327 (defun zerop (object)
330 ;;; Numerical comparisons
332 (fset '= (@ (guile) =))
334 (defun /= (num1 num2
)
335 (null (= num1 num2
)))
337 (fset '< (@ (guile) <))
338 (fset '<= (@ (guile) <=))
339 (fset '> (@ (guile) >))
340 (fset '>= (@ (guile) >=))
342 (defun max (&rest numbers
)
343 (apply (@ (guile) max
) numbers
))
345 (defun min (&rest numbers
)
346 (apply (@ (guile) min
) numbers
))
348 ;;; Arithmetic functions
350 (fset '1+ (@ (guile) 1+))
351 (fset '1-
(@ (guile) 1-
))
352 (fset '+ (@ (guile) +))
353 (fset '-
(@ (guile) -
))
354 (fset '* (@ (guile) *))
355 (fset '%
(@ (guile) modulo
))
356 (fset 'abs
(@ (guile) abs
))
358 ;;; Floating-point rounding
360 (fset 'ffloor
(@ (guile) floor
))
361 (fset 'fceiling
(@ (guile) ceiling
))
362 (fset 'ftruncate
(@ (guile) truncate
))
363 (fset 'fround
(@ (guile) round
))
365 ;;; Numeric conversion
369 (funcall (@ (guile) exact-
>inexact
) arg
)
370 (signal 'wrong-type-argument
`(numberp ,arg
))))
377 (null (consp object
)))
379 (defun nlistp (object)
380 (null (listp object
)))
384 (fset 'cons
(@ (guile) cons
))
385 (fset 'list
(@ (guile) list
))
386 (fset 'make-list
(@ (guile) make-list
))
387 (fset 'append
(@ (guile) append
))
388 (fset 'reverse
(@ (guile) reverse
))
389 (fset 'nreverse
(@ (guile) reverse
!))
391 (defun car-safe (object)
396 (defun cdr-safe (object)
401 (defun setcar (cell newcar
)
404 (funcall (@ (guile) set-car
!) cell newcar
)
406 (signal 'wrong-type-argument
`(consp ,cell
))))
408 (defun setcdr (cell newcdr
)
411 (funcall (@ (guile) set-cdr
!) cell newcdr
)
413 (signal 'wrong-type-argument
`(consp ,cell
))))
415 (defun %member
(elt list test
)
419 (if (funcall test elt
(car list
))
421 (%member elt
(cdr list
) test
)))
422 (t (signal 'wrong-type-argument
`(listp ,list
)))))
424 (defun member (elt list
)
425 (%member elt list
#'equal
))
427 (defun memql (elt list
)
428 (%member elt list
#'eql
))
430 (defun memq (elt list
)
431 (%member elt list
#'eq
))
433 (defun assoc (key list
)
434 (funcall (@ (srfi srfi-1
) assoc
) key list
#'equal
))
436 (defun assq (key list
)
437 (funcall (@ (srfi srfi-1
) assoc
) key list
#'eq
))
439 (defun rplaca (cell newcar
)
440 (funcall (@ (guile) set-car
!) cell newcar
)
443 (defun rplacd (cell newcdr
)
444 (funcall (@ (guile) set-cdr
!) cell newcdr
)
459 (defmacro dolist
(spec &rest body
)
460 (apply #'(lambda (var list
&optional result
)
463 (cons 'lambda
(cons (list var
) body
))
470 (defun string (&rest characters
)
471 (funcall (@ (guile) list-
>string
)
472 (mapcar (@ (guile) integer-
>char
) characters
)))
474 (defun stringp (object)
475 (funcall (@ (guile) string?
) object
))
477 (defun string-equal (s1 s2
)
478 (let ((s1 (if (symbolp s1
) (symbol-name s1
) s1
))
479 (s2 (if (symbolp s2
) (symbol-name s2
) s2
)))
480 (funcall (@ (guile) string
=?
) s1 s2
)))
482 (fset 'string
= 'string-equal
)
484 (defun substring (string from
&optional to
)
485 (apply (@ (guile) substring
) string from
(if to
(list to
) nil
)))
488 (funcall (@ (guile) string-upcase
) obj
))
490 (defun downcase (obj)
491 (funcall (@ (guile) string-downcase
) obj
))
493 (defun string-match (regexp string
&optional start
)
494 (let ((m (funcall (@ (ice-9 regex
) string-match
)
499 (funcall (@ (ice-9 regex
) match
:start
) m
0)
504 (defun make-vector (length init
)
505 (funcall (@ (guile) make-vector
) length init
))
509 (defun length (sequence)
510 (funcall (if (listp sequence
)
512 (@ (guile) generalized-vector-length
))
515 (defun mapcar (function sequence
)
516 (funcall (@ (guile) map
) function sequence
))
518 (defun mapc (function sequence
)
519 (funcall (@ (guile) for-each
) function sequence
)
522 (defun aref (array idx
)
523 (funcall (@ (guile) generalized-vector-ref
) array idx
))
525 (defun aset (array idx newelt
)
526 (funcall (@ (guile) generalized-vector-set
!) array idx newelt
)
529 (defun concat (&rest sequences
)
530 (apply (@ (guile) string-append
) sequences
))
534 (defun %plist-member
(plist property test
)
538 (if (funcall test
(car plist
) property
)
540 (%plist-member
(cdr (cdr plist
)) property test
)))
541 (t (signal 'wrong-type-argument
`(listp ,plist
)))))
543 (defun %plist-get
(plist property test
)
544 (car (%plist-member plist property test
)))
546 (defun %plist-put
(plist property value test
)
547 (let ((x (%plist-member plist property test
)))
549 (progn (setcar x value
) plist
)
550 (cons property
(cons value plist
)))))
552 (defun plist-get (plist property
)
553 (%plist-get plist property
#'eq
))
555 (defun plist-put (plist property value
)
556 (%plist-put plist property value
#'eq
))
558 (defun plist-member (plist property
)
559 (%plist-member plist property
#'eq
))
561 (defun lax-plist-get (plist property
)
562 (%plist-get plist property
#'equal
))
564 (defun lax-plist-put (plist property value
)
565 (%plist-put plist property value
#'equal
))
567 (defvar plist-function
(funcall (@ (guile) make-object-property
)))
569 (defun symbol-plist (symbol)
570 (funcall plist-function symbol
))
572 (defun setplist (symbol plist
)
573 (funcall (funcall (@ (guile) setter
) plist-function
) symbol plist
))
575 (defun get (symbol propname
)
576 (plist-get (symbol-plist symbol
) propname
))
578 (defun put (symbol propname value
)
579 (setplist symbol
(plist-put (symbol-plist symbol
) propname value
)))
583 (defmacro condition-case
(var bodyform
&rest handlers
)
584 (let ((key (make-symbol "key"))
585 (error-symbol (make-symbol "error-symbol"))
586 (data (make-symbol "data"))
587 (conditions (make-symbol "conditions")))
588 (flet ((handler->cond-clause
(handler)
589 `((or ,@(mapcar #'(lambda (c) `(memq ',c
,conditions
))
590 (if (consp (car handler
))
592 (list (car handler
)))))
594 `(funcall (@ (guile) catch
)
596 #'(lambda () ,bodyform
)
597 #'(lambda (,key
,error-symbol
,data
)
598 (declare (lexical ,key
,error-symbol
,data
))
600 (get ,error-symbol
'error-conditions
))
602 `((,var
(cons ,error-symbol
,data
)))
604 (declare (lexical ,conditions
605 ,@(if var
`(,var
) '())))
606 (cond ,@(mapcar #'handler-
>cond-clause handlers
)
607 (t (signal ,error-symbol
,data
)))))))))
609 (put 'error
'error-conditions
'(error))
610 (put 'wrong-type-argument
'error-conditions
'(wrong-type-argument error
))
611 (put 'invalid-function
'error-conditions
'(invalid-function error
))
612 (put 'no-catch
'error-conditions
'(no-catch error
))
613 (put 'throw
'error-conditions
'(throw))
617 (defmacro catch
(tag &rest body
)
618 (let ((tag-value (make-symbol "tag-value"))
619 (c (make-symbol "c"))
620 (data (make-symbol "data")))
621 `(let ((,tag-value
,tag
))
622 (declare (lexical ,tag-value
))
627 (let ((,data
(cdr ,c
)))
628 (declare (lexical ,data
))
629 (if (eq (car ,data
) ,tag-value
)
631 (apply #'throw
,data
))))))))
633 (defun throw (tag value
)
634 (signal (if %catch
'throw
'no-catch
) (list tag value
)))
638 (defun princ (object)
639 (funcall (@ (guile) display
) object
))
641 (defun print (object)
642 (funcall (@ (guile) write
) object
))
644 (defun prin1 (object)
645 (funcall (@ (guile) write
) object
))
648 (funcall (@ (guile) newline
)))
650 (defun format* (stream string
&rest args
)
651 (apply (@ (guile) format
) stream string args
))
653 (defun send-string-to-terminal (string)
656 (defun read-from-minibuffer (prompt &rest ignore
)
658 (let ((value (funcall (@ (ice-9 rdelim
) read-line
))))
659 (if (funcall (@ (guile) eof-object?
) value
)
663 (defun prin1-to-string (object)
664 (format* nil
"~S" object
))
666 ;; Random number generation
668 (defvar %random-state
(funcall (@ (guile) copy-random-state
)
669 (@ (guile) *random-state
*)))
671 (defun random (&optional limit
)
674 (funcall (@ (guile) random-state-from-platform
))))
675 (funcall (@ (guile) random
)
676 (if (wholenump limit
)
678 (@ (guile) most-positive-fixnum
))
681 (defmacro save-excursion
(&rest body
)
682 `(call-with-save-excursion #'(lambda () ,@body
)))
684 (defmacro save-current-buffer
(&rest body
)
685 `(call-with-save-current-buffer #'(lambda () ,@body
)))
687 (defmacro save-restriction
(&rest body
)
688 `(call-with-save-restriction #'(lambda () ,@body
)))
690 (defmacro track-mouse
(&rest body
)
691 `(call-with-track-mouse #'(lambda () ,@body
)))
693 (defmacro setq-default
(var value
&rest args
)
694 `(progn (set-default ',var
,value
)
697 `(setq-default ,@args
))))
699 (defmacro catch
(tag &rest body
)
700 `(call-with-catch ,tag
#'(lambda () ,@body
)))
702 (defmacro condition-case
(var bodyform
&rest args
)
704 (let* ((handler (car args
))
705 (handlers (cdr args
))
706 (handler-conditions (car handler
))
707 (handler-body (cdr handler
)))
708 `(call-with-handler ',var
710 #'(lambda () ,@handler-body
)
717 (defun backtrace-frame (nframes)
718 (let* ((stack (funcall (@ (guile) make-stack
) t
))
719 (frame (stack-ref stack nframes
))
720 (proc (funcall (@ (guile) frame-procedure
) frame
))
721 (pname (or (and (%functionp proc
)
722 (funcall (@ (guile) procedure-name
) proc
))
724 (args (funcall (@ (guile) frame-arguments
) frame
)))
725 (cons t
(cons pname args
))))
729 (let* ((stack (funcall (@ (guile) make-stack
) t
))
730 (frame (funcall (@ (guile) stack-ref
) stack
1))
731 (space (funcall (@ (guile) integer-
>char
) 32)))
733 (princ (string 32 32))
734 (let ((proc (funcall (@ (guile) frame-procedure
) frame
)))
735 (prin1 (or (and (%functionp proc
)
736 (funcall (@ (guile) procedure-name
) proc
))
738 (prin1 (funcall (@ (guile) frame-arguments
) frame
))
740 (setq frame
(funcall (@ (guile) frame-previous
) frame
)))
743 (defun %set-eager-macroexpansion-mode
(ignore)
746 (defun progn (&rest args
) (error "Special operator"))
747 (defun eval-when-compile (&rest args
) (error "Special operator"))
748 (defun if (&rest args
) (error "Special operator"))
749 (defun defconst (&rest args
) (error "Special operator"))
750 (defun defvar (&rest args
) (error "Special operator"))
751 (defun setq (&rest args
) (error "Special operator"))
752 (defun let (&rest args
) (error "Special operator"))
753 (defun flet (&rest args
) (error "Special operator"))
754 (defun labels (&rest args
) (error "Special operator"))
755 (defun let* (&rest args
) (error "Special operator"))
756 (defun function (&rest args
) (error "Special operator"))
757 (defun defmacro (&rest args
) (error "Special operator"))
758 (defun quote (&rest args
) (error "Special operator"))