1 ;;;; Copyright (C) 1996, 1997, 1998 Free Software Foundation, Inc.
3 ;;;; This program is free software; you can redistribute it and/or modify
4 ;;;; it under the terms of the GNU General Public License as published by
5 ;;;; the Free Software Foundation; either version 2, or (at your option)
6 ;;;; any later version.
8 ;;;; This program is distributed in the hope that it will be useful,
9 ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
10 ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 ;;;; GNU General Public License for more details.
13 ;;;; You should have received a copy of the GNU General Public License
14 ;;;; along with this software; see the file COPYING. If not, write to
15 ;;;; the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
16 ;;;; Boston, MA 02111-1307 USA
18 ;;;; The author can be reached at djurfeldt@nada.kth.se
19 ;;;; Mikael Djurfeldt, SANS/NADA KTH, 10044 STOCKHOLM, SWEDEN
20 ;;;; (I didn't write this!)
24 ;;; *********************************************************************
25 ;;; * This is the Guile side of the Emacs interface *
26 ;;; * Experimental hACK---the real version will be coming soon (almost) *
27 ;;; *********************************************************************
29 ;;; {Session support for Emacs}
32 (define-module (ice-9 emacs)
33 :use-module (ice-9 debug)
34 :use-module (ice-9 threads)
35 :use-module (ice-9 session))
37 (define emacs-escape-character #\sub)
39 (define emacs-output-port (current-output-port))
41 (define (make-emacs-command char)
42 (let ((cmd (list->string (list emacs-escape-character char))))
44 (display cmd emacs-output-port))))
46 (define enter-input-wait (make-emacs-command #\s))
47 (define exit-input-wait (make-emacs-command #\f))
48 (define enter-read-character #\r)
49 (define sending-error (make-emacs-command #\F))
50 (define sending-backtrace (make-emacs-command #\B))
51 (define sending-result (make-emacs-command #\x))
52 (define end-of-text (make-emacs-command #\.))
53 (define no-stack (make-emacs-command #\S))
54 (define no-source (make-emacs-command #\R))
59 (add-hook! before-backtrace-hook sending-backtrace)
60 (add-hook! after-backtrace-hook end-of-text)
61 (add-hook! before-error-hook sending-error)
62 (add-hook! after-error-hook end-of-text)
67 (set-current-error-port emacs-output-port)
69 (add-hook! before-read-hook
72 (force-output emacs-output-port)))
74 (add-hook! after-read-hook
77 (force-output emacs-output-port)))
81 (define (make-emacs-load-port orig-port)
82 (letrec ((read-char-fn (lambda args
83 (let ((c (read-char orig-port)))
91 (lambda () (close-port orig-port)))
94 (set-current-input-port (make-emacs-load-port (current-input-port)))
96 (define (result-to-emacs exp)
98 (write exp emacs-output-port)
100 (force-output emacs-output-port))
102 (define load-acknowledge (make-emacs-command #\l))
104 (define load-port (current-input-port))
106 (define (flush-line port)
107 (let loop ((c (read-char port)))
108 (if (not (eq? c #\nl))
109 (loop (read-char port)))))
111 (define whitespace-chars (list #\space #\tab #\nl #\np))
113 (define (flush-whitespace port)
116 (let loop ((c (read-char port)))
117 (cond ((eq? c the-eof-object)
118 (error "End of file while recieving Emacs data"))
119 ((memq c whitespace-chars) (loop (read-char port)))
120 ((eq? c #\;) (flush-line port) (loop (read-char port)))
121 (else (unread-char c port))))
124 (read-char port) ; Read final newline
127 (define (emacs-load filename linum colnum module interactivep)
128 (set-port-filename! %%load-port filename)
129 (set-port-line! %%load-port linum)
130 (set-port-column! %%load-port colnum)
133 (let loop ((endp (flush-whitespace %%load-port)))
136 (save-module-excursion
139 (set-current-module (resolve-module module #f)))
141 (start-stack read-and-eval!
142 (read-and-eval! %%load-port))))
144 (result-to-emacs result)))))
145 (loop (flush-whitespace %%load-port)))
147 (load-acknowledge))))
150 (cond ((eq? key 'end-of-chunk)
151 (fluid-set! the-last-stack #f)
152 (set! stack-saved? #t)
153 (scm-error 'misc-error
155 "Incomplete expression"
164 (read-char %%load-port)
168 (apply throw key args))))))
170 (define (emacs-eval-request form)
171 (result-to-emacs (eval form)))
173 ;;*fixme* Not necessary to use flags no-stack and no-source
174 (define (get-frame-source frame)
175 (if (or (not (fluid-ref the-last-stack))
176 (>= frame (stack-length (fluid-ref the-last-stack))))
180 (let* ((frame (stack-ref (fluid-ref the-last-stack)
181 (frame-number->index frame)))
182 (source (frame-source frame)))
187 (define (emacs-select-frame frame)
188 (let ((source (get-frame-source frame)))
190 (let ((fname (source-property source 'filename))
191 (line (source-property source 'line))
192 (column (source-property source 'column)))
193 (if (and fname line column)
194 (list fname line column)
199 (define (object->string x . method)
200 (with-output-to-string
207 (define (format template . rest)
208 (let loop ((chars (string->list template))
210 (cond ((null? chars) (list->string (reverse result)))
211 ((char=? (car chars) #\%)
216 ((#\S) (object->string (car rest)))
217 ((#\s) (object->string (car rest) display)))))
219 (else (loop (cdr chars) (cons (car chars) result))))))
221 (define (error-args->string args)
222 (let ((msg (apply format (caddr args) (cadddr args))))
223 (if (symbol? (cadr args))
224 (string-append (symbol->string (cadr args))
229 (define (emacs-frame-eval frame form)
230 (let ((source (get-frame-source frame)))
236 (local-eval (with-input-from-string form read)
237 (memoized-environment source)))))
240 (error-args->string args))))
245 (define (emacs-symdoc symbol)
246 (if (or (not (module-bound? (current-module) symbol))
247 (not (procedure? (eval symbol))))
249 (procedure-documentation (eval symbol))))
251 ;;; A fix to get the emacs interface to work together with the module system.
253 (variable-set! (builtin-variable '%%load-port) load-port)
254 (variable-set! (builtin-variable '%%emacs-load) emacs-load)
255 (variable-set! (builtin-variable '%%emacs-eval-request) emacs-eval-request)
256 (variable-set! (builtin-variable '%%emacs-select-frame) emacs-select-frame)
257 (variable-set! (builtin-variable '%%emacs-frame-eval) emacs-frame-eval)
258 (variable-set! (builtin-variable '%%emacs-symdoc) emacs-symdoc)
259 (variable-set! (builtin-variable '%%apropos-internal) apropos-internal)