*** empty log message ***
[bpt/guile.git] / emacs / guile.el
CommitLineData
2d857fb1
KN
1;;; guile.el --- Emacs Guile interface
2
3;; Copyright (C) 2001 Keisuke Nishida <kxn30@po.cwru.edu>
4
5;; GNU Emacs is free software; you can redistribute it and/or modify
6;; it under the terms of the GNU General Public License as published by
7;; the Free Software Foundation; either version 2, or (at your option)
8;; any later version.
9
10;; GNU Emacs is distributed in the hope that it will be useful,
11;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13;; GNU General Public License for more details.
14
15;; You should have received a copy of the GNU General Public License
16;; along with GNU Emacs; see the file COPYING. If not, write to the
17;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18;; Boston, MA 02111-1307, USA.
19
20;;; Code:
21
b77e2f28
KN
22(require 'cl)
23
2d857fb1
KN
24;;;
25;;; Low level interface
26;;;
27
2d857fb1
KN
28(defvar gulie-emacs-file
29 (catch 'return
30 (mapc (lambda (dir)
31 (let ((file (expand-file-name "guile-emacs.scm" dir)))
32 (if (file-exists-p file) (throw 'return file))))
33 load-path)
34 (error "Cannot find guile-emacs.scm")))
35
9ab0d788
KN
36(defvar gulie-channel-file
37 (catch 'return
38 (mapc (lambda (dir)
39 (let ((file (expand-file-name "channel.scm" dir)))
40 (if (file-exists-p file) (throw 'return file))))
41 load-path)))
42
43(defvar guile-libs
44 (nconc (if gulie-channel-file (list "-l" gulie-channel-file) '())
45 (list "-l" gulie-emacs-file)))
46
47;;;###autoload
2d857fb1
KN
48(defun guile:make-adapter (command channel)
49 (let* ((buff (generate-new-buffer " *guile object channel*"))
9ab0d788
KN
50 (libs (if gulie-channel-file (list "-l" gulie-channel-file) nil))
51 (proc (apply 'start-process "guile-oa" buff command "-q" guile-libs)))
2d857fb1
KN
52 (process-kill-without-query proc)
53 (accept-process-output proc)
54 (guile-process-require proc (format "(%s)\n" channel) "channel> ")
55 proc))
56
57(put 'guile-error 'error-conditions '(guile-error error))
58(put 'guile-error 'error-message "Guile error")
59
37052e60
KN
60(defvar guile-token-tag "<guile>")
61
62(defun guile-tokenp (x) (and (consp x) (eq (car x) guile-token-tag)))
63
9ab0d788 64;;;###autoload
2d857fb1
KN
65(defun guile:eval (string adapter)
66 (let ((output (guile-process-require adapter (concat "eval " string "\n")
67 "channel> ")))
68 (cond
69 ((string= output "") nil)
70 ((string-match "^\\(\\(value\\)\\|\\(token\\)\\|\\(exception\\)\\) = "
71 output)
72 (cond
73 ;; value
74 ((match-beginning 2)
75 (car (read-from-string (substring output (match-end 0)))))
76 ;; token
77 ((match-beginning 3)
37052e60 78 (cons guile-token-tag
2d857fb1
KN
79 (car (read-from-string (substring output (match-end 0))))))
80 ;; exception
81 ((match-beginning 4)
82 (signal 'guile-error
83 (car (read-from-string (substring output (match-end 0))))))))
84 (t
85 (error "Unsupported result" output)))))
86
87\f
88;;;
89;;; Guile Lisp adapter
90;;;
91
92(defvar guile-lisp-command "guile")
93(defvar guile-lisp-adapter nil)
94
95(defvar true "#t")
96(defvar false "#f")
97
98(defun guile-lisp-adapter ()
99 (if (and (processp guile-lisp-adapter)
100 (eq (process-status guile-lisp-adapter) 'run))
101 guile-lisp-adapter
102 (setq guile-lisp-adapter
103 (guile:make-adapter guile-lisp-command 'emacs-lisp-channel))))
104
105(defun guile-lisp-convert (x)
106 (cond
107 ((or (eq x true) (eq x false)) x)
37052e60 108 ((null x) "'()")
9ab0d788 109 ((keywordp x) (concat "#" (prin1-to-string x)))
2d857fb1 110 ((stringp x) (prin1-to-string x))
37052e60 111 ((guile-tokenp x) (cadr x))
2d857fb1 112 ((consp x)
37052e60
KN
113 (if (null (cdr x))
114 (list (guile-lisp-convert (car x)))
2d857fb1
KN
115 (cons (guile-lisp-convert (car x)) (guile-lisp-convert (cdr x)))))
116 (t x)))
117
9ab0d788
KN
118;;;###autoload
119(defun guile-lisp-eval (form)
120 (guile:eval (format "%s" (guile-lisp-convert form)) (guile-lisp-adapter)))
121
122(defun guile-lisp-flat-eval (&rest form)
123 (let ((args (mapcar (lambda (x)
124 (if (guile-tokenp x) (cadr x) (list 'quote x)))
125 (cdr form))))
126 (guile-lisp-eval (cons (car form) args))))
2d857fb1
KN
127
128;;;###autoload
9ab0d788
KN
129(defmacro guile-import (name &optional new-name &rest opts)
130 `(guile-process-import ',name ',new-name ',opts))
2d857fb1 131
9ab0d788
KN
132(defun guile-process-import (name new-name opts)
133 (let ((real (or new-name name))
134 (docs (if (memq :with-docs opts) true false)))
135 (eval (guile-lisp-eval `(guile-emacs-export ',name ',real ,docs)))))
2d857fb1
KN
136
137;;;###autoload
9ab0d788
KN
138(defmacro guile-import-module (name &rest opts)
139 `(guile-process-use-module ',name ',opts))
2d857fb1 140
9ab0d788 141(defun guile-process-use-module (name opts)
2d857fb1
KN
142 (unless (boundp 'guile-emacs-export-procedures)
143 (guile-import guile-emacs-export-procedures))
9ab0d788
KN
144 (let ((docs (if (memq :with-docs opts) true false)))
145 (guile-lisp-eval `(use-modules ,name))
146 (eval (guile-emacs-export-procedures name docs))
147 name))
2d857fb1
KN
148
149\f
150;;;
151;;; Process handling
152;;;
153
154(defvar guile-process-output-start nil)
155(defvar guile-process-output-value nil)
156(defvar guile-process-output-finished nil)
157(defvar guile-process-output-separator nil)
158
159(defun guile-process-require (process string separator)
160 (setq guile-process-output-value nil)
161 (setq guile-process-output-finished nil)
162 (setq guile-process-output-separator separator)
163 (let (temp-buffer)
164 (unless (process-buffer process)
165 (setq temp-buffer (guile-temp-buffer))
166 (set-process-buffer process temp-buffer))
167 (with-current-buffer (process-buffer process)
168 (goto-char (point-max))
169 (insert string)
170 (setq guile-process-output-start (point))
171 (set-process-filter process 'guile-process-filter)
172 (process-send-string process string)
173 (while (not guile-process-output-finished)
174 (unless (accept-process-output process 3)
175 (when (> (point) guile-process-output-start)
176 (display-buffer (current-buffer))
177 (error "BUG in Guile object channel!!")))))
178 (when temp-buffer
179 (set-process-buffer process nil)
180 (kill-buffer temp-buffer)))
181 guile-process-output-value)
182
183(defun guile-process-filter (process string)
184 (with-current-buffer (process-buffer process)
185 (insert string)
186 (forward-line -1)
187 (if (< (point) guile-process-output-start)
188 (goto-char guile-process-output-start))
189 (when (re-search-forward guile-process-output-separator nil 0)
190 (goto-char (match-beginning 0))
191 (setq guile-process-output-value
192 (buffer-substring guile-process-output-start (point)))
193 (setq guile-process-output-finished t))))
194
195(defun guile-process-kill (process)
196 (set-process-filter process nil)
197 (delete-process process)
198 (if (process-buffer process)
199 (kill-buffer (process-buffer process))))
200
201(provide 'guile)
202
203;;; guile.el ends here