2009-11-26 22:11:40 +01:00
|
|
|
|
;;;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2006, 2009 Free Software Foundation, Inc.
|
1997-03-01 02:04:00 +00:00
|
|
|
|
;;;;
|
2003-04-05 19:15:35 +00:00
|
|
|
|
;;;; This library is free software; you can redistribute it and/or
|
|
|
|
|
|
;;;; modify it under the terms of the GNU Lesser General Public
|
|
|
|
|
|
;;;; License as published by the Free Software Foundation; either
|
2009-06-17 00:22:09 +01:00
|
|
|
|
;;;; version 3 of the License, or (at your option) any later version.
|
1997-03-01 02:04:00 +00:00
|
|
|
|
;;;;
|
2003-04-05 19:15:35 +00:00
|
|
|
|
;;;; This library is distributed in the hope that it will be useful,
|
1997-03-01 02:04:00 +00:00
|
|
|
|
;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2003-04-05 19:15:35 +00:00
|
|
|
|
;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
|
|
;;;; Lesser General Public License for more details.
|
1997-03-01 02:04:00 +00:00
|
|
|
|
;;;;
|
2003-04-05 19:15:35 +00:00
|
|
|
|
;;;; You should have received a copy of the GNU Lesser General Public
|
|
|
|
|
|
;;;; License along with this library; if not, write to the Free Software
|
2005-05-23 19:57:22 +00:00
|
|
|
|
;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
2001-06-03 23:29:45 +00:00
|
|
|
|
;;;;
|
1997-03-01 02:04:00 +00:00
|
|
|
|
;;;; The author can be reached at djurfeldt@nada.kth.se
|
|
|
|
|
|
;;;; Mikael Djurfeldt, SANS/NADA KTH, 10044 STOCKHOLM, SWEDEN
|
1998-05-19 19:48:11 +00:00
|
|
|
|
;;;; (I didn't write this!)
|
1997-03-01 02:04:00 +00:00
|
|
|
|
;;;;
|
|
|
|
|
|
|
|
|
|
|
|
|
1998-05-19 19:48:11 +00:00
|
|
|
|
;;; *********************************************************************
|
|
|
|
|
|
;;; * This is the Guile side of the Emacs interface *
|
|
|
|
|
|
;;; * Experimental hACK---the real version will be coming soon (almost) *
|
|
|
|
|
|
;;; *********************************************************************
|
1997-03-01 02:04:00 +00:00
|
|
|
|
|
|
|
|
|
|
;;; {Session support for Emacs}
|
|
|
|
|
|
;;;
|
|
|
|
|
|
|
|
|
|
|
|
(define-module (ice-9 emacs)
|
1997-08-24 03:38:48 +00:00
|
|
|
|
:use-module (ice-9 debug)
|
1997-03-01 02:04:00 +00:00
|
|
|
|
:use-module (ice-9 threads)
|
1999-03-16 03:09:52 +00:00
|
|
|
|
:use-module (ice-9 session)
|
|
|
|
|
|
:no-backtrace)
|
1997-03-01 02:04:00 +00:00
|
|
|
|
|
|
|
|
|
|
(define emacs-escape-character #\sub)
|
|
|
|
|
|
|
|
|
|
|
|
(define emacs-output-port (current-output-port))
|
|
|
|
|
|
|
|
|
|
|
|
(define (make-emacs-command char)
|
|
|
|
|
|
(let ((cmd (list->string (list emacs-escape-character char))))
|
|
|
|
|
|
(lambda ()
|
|
|
|
|
|
(display cmd emacs-output-port))))
|
|
|
|
|
|
|
1997-08-24 03:38:48 +00:00
|
|
|
|
(define enter-input-wait (make-emacs-command #\s))
|
|
|
|
|
|
(define exit-input-wait (make-emacs-command #\f))
|
1997-03-01 02:04:00 +00:00
|
|
|
|
(define enter-read-character #\r)
|
1997-08-24 03:38:48 +00:00
|
|
|
|
(define sending-error (make-emacs-command #\F))
|
1997-03-01 02:04:00 +00:00
|
|
|
|
(define sending-backtrace (make-emacs-command #\B))
|
|
|
|
|
|
(define sending-result (make-emacs-command #\x))
|
1997-08-24 03:38:48 +00:00
|
|
|
|
(define end-of-text (make-emacs-command #\.))
|
|
|
|
|
|
(define no-stack (make-emacs-command #\S))
|
|
|
|
|
|
(define no-source (make-emacs-command #\R))
|
1997-03-01 02:04:00 +00:00
|
|
|
|
|
|
|
|
|
|
;; {Error handling}
|
|
|
|
|
|
;;
|
|
|
|
|
|
|
|
|
|
|
|
(add-hook! before-backtrace-hook sending-backtrace)
|
|
|
|
|
|
(add-hook! after-backtrace-hook end-of-text)
|
|
|
|
|
|
(add-hook! before-error-hook sending-error)
|
|
|
|
|
|
(add-hook! after-error-hook end-of-text)
|
|
|
|
|
|
|
|
|
|
|
|
;; {Repl}
|
|
|
|
|
|
;;
|
|
|
|
|
|
|
|
|
|
|
|
(set-current-error-port emacs-output-port)
|
|
|
|
|
|
|
|
|
|
|
|
(add-hook! before-read-hook
|
|
|
|
|
|
(lambda ()
|
|
|
|
|
|
(enter-input-wait)
|
|
|
|
|
|
(force-output emacs-output-port)))
|
|
|
|
|
|
|
|
|
|
|
|
(add-hook! after-read-hook
|
|
|
|
|
|
(lambda ()
|
|
|
|
|
|
(exit-input-wait)
|
|
|
|
|
|
(force-output emacs-output-port)))
|
|
|
|
|
|
|
|
|
|
|
|
;;; {Misc.}
|
|
|
|
|
|
|
|
|
|
|
|
(define (make-emacs-load-port orig-port)
|
|
|
|
|
|
(letrec ((read-char-fn (lambda args
|
|
|
|
|
|
(let ((c (read-char orig-port)))
|
|
|
|
|
|
(if (eq? c #\soh)
|
|
|
|
|
|
(throw 'end-of-chunk)
|
|
|
|
|
|
c)))))
|
|
|
|
|
|
|
|
|
|
|
|
(make-soft-port
|
|
|
|
|
|
(vector #f #f #f
|
|
|
|
|
|
read-char-fn
|
|
|
|
|
|
(lambda () (close-port orig-port)))
|
|
|
|
|
|
"r")))
|
|
|
|
|
|
|
1998-04-19 01:22:44 +00:00
|
|
|
|
(set-current-input-port (make-emacs-load-port (current-input-port)))
|
1997-03-01 02:04:00 +00:00
|
|
|
|
|
1997-08-21 09:32:37 +00:00
|
|
|
|
(define (result-to-emacs exp)
|
|
|
|
|
|
(sending-result)
|
|
|
|
|
|
(write exp emacs-output-port)
|
|
|
|
|
|
(end-of-text)
|
|
|
|
|
|
(force-output emacs-output-port))
|
|
|
|
|
|
|
1997-03-01 02:04:00 +00:00
|
|
|
|
(define load-acknowledge (make-emacs-command #\l))
|
|
|
|
|
|
|
|
|
|
|
|
(define load-port (current-input-port))
|
|
|
|
|
|
|
|
|
|
|
|
(define (flush-line port)
|
|
|
|
|
|
(let loop ((c (read-char port)))
|
|
|
|
|
|
(if (not (eq? c #\nl))
|
|
|
|
|
|
(loop (read-char port)))))
|
|
|
|
|
|
|
1997-08-20 19:52:03 +00:00
|
|
|
|
(define whitespace-chars (list #\space #\tab #\nl #\np))
|
1997-03-01 02:04:00 +00:00
|
|
|
|
|
|
|
|
|
|
(define (flush-whitespace port)
|
|
|
|
|
|
(catch 'end-of-chunk
|
|
|
|
|
|
(lambda ()
|
|
|
|
|
|
(let loop ((c (read-char port)))
|
|
|
|
|
|
(cond ((eq? c the-eof-object)
|
2000-12-08 15:39:10 +00:00
|
|
|
|
(error "End of file while receiving Emacs data"))
|
1997-03-01 02:04:00 +00:00
|
|
|
|
((memq c whitespace-chars) (loop (read-char port)))
|
|
|
|
|
|
((eq? c #\;) (flush-line port) (loop (read-char port)))
|
|
|
|
|
|
(else (unread-char c port))))
|
|
|
|
|
|
#f)
|
|
|
|
|
|
(lambda args
|
|
|
|
|
|
(read-char port) ; Read final newline
|
|
|
|
|
|
#t)))
|
|
|
|
|
|
|
1997-09-05 01:21:02 +00:00
|
|
|
|
(define (emacs-load filename linum colnum module interactivep)
|
2002-11-17 22:17:59 +00:00
|
|
|
|
(define (read-and-eval! port)
|
|
|
|
|
|
(let ((x (read port)))
|
|
|
|
|
|
(if (eof-object? x)
|
|
|
|
|
|
(throw 'end-of-file)
|
|
|
|
|
|
(primitive-eval x))))
|
1997-03-01 02:04:00 +00:00
|
|
|
|
(set-port-filename! %%load-port filename)
|
1997-08-14 19:23:13 +00:00
|
|
|
|
(set-port-line! %%load-port linum)
|
1997-08-24 03:38:48 +00:00
|
|
|
|
(set-port-column! %%load-port colnum)
|
1997-03-01 02:04:00 +00:00
|
|
|
|
(lazy-catch #t
|
|
|
|
|
|
(lambda ()
|
|
|
|
|
|
(let loop ((endp (flush-whitespace %%load-port)))
|
|
|
|
|
|
(if (not endp)
|
1998-06-15 13:00:35 +00:00
|
|
|
|
(begin
|
|
|
|
|
|
(save-module-excursion
|
|
|
|
|
|
(lambda ()
|
|
|
|
|
|
(if module
|
|
|
|
|
|
(set-current-module (resolve-module module #f)))
|
|
|
|
|
|
(let ((result
|
|
|
|
|
|
(start-stack read-and-eval!
|
|
|
|
|
|
(read-and-eval! %%load-port))))
|
|
|
|
|
|
(if interactivep
|
|
|
|
|
|
(result-to-emacs result)))))
|
1997-03-01 02:04:00 +00:00
|
|
|
|
(loop (flush-whitespace %%load-port)))
|
|
|
|
|
|
(begin
|
1999-03-12 09:49:20 +00:00
|
|
|
|
(load-acknowledge)))
|
|
|
|
|
|
(set-port-filename! %%load-port #f))) ;reset port filename
|
1997-03-01 02:04:00 +00:00
|
|
|
|
(lambda (key . args)
|
1999-03-12 09:49:20 +00:00
|
|
|
|
(set-port-filename! %%load-port #f)
|
1997-03-01 02:04:00 +00:00
|
|
|
|
(cond ((eq? key 'end-of-chunk)
|
1998-06-11 13:26:43 +00:00
|
|
|
|
(fluid-set! the-last-stack #f)
|
1997-03-01 02:04:00 +00:00
|
|
|
|
(set! stack-saved? #t)
|
|
|
|
|
|
(scm-error 'misc-error
|
1997-08-20 19:52:03 +00:00
|
|
|
|
#f
|
1997-03-01 02:04:00 +00:00
|
|
|
|
"Incomplete expression"
|
|
|
|
|
|
'()
|
|
|
|
|
|
'()))
|
|
|
|
|
|
((eq? key 'exit))
|
|
|
|
|
|
(else
|
1997-08-20 17:42:15 +00:00
|
|
|
|
(save-stack 2)
|
1997-03-01 02:04:00 +00:00
|
|
|
|
(catch 'end-of-chunk
|
|
|
|
|
|
(lambda ()
|
|
|
|
|
|
(let loop ()
|
|
|
|
|
|
(read-char %%load-port)
|
|
|
|
|
|
(loop)))
|
|
|
|
|
|
(lambda args
|
|
|
|
|
|
#f))
|
|
|
|
|
|
(apply throw key args))))))
|
|
|
|
|
|
|
1997-08-24 03:38:48 +00:00
|
|
|
|
(define (emacs-eval-request form)
|
2000-08-11 08:45:45 +00:00
|
|
|
|
(result-to-emacs (eval form (interaction-environment))))
|
1997-08-24 03:38:48 +00:00
|
|
|
|
|
1997-08-25 20:03:21 +00:00
|
|
|
|
;;*fixme* Not necessary to use flags no-stack and no-source
|
1997-08-24 03:38:48 +00:00
|
|
|
|
(define (get-frame-source frame)
|
1998-06-11 13:26:43 +00:00
|
|
|
|
(if (or (not (fluid-ref the-last-stack))
|
|
|
|
|
|
(>= frame (stack-length (fluid-ref the-last-stack))))
|
1997-08-24 03:38:48 +00:00
|
|
|
|
(begin
|
|
|
|
|
|
(no-stack)
|
|
|
|
|
|
#f)
|
1998-06-11 13:26:43 +00:00
|
|
|
|
(let* ((frame (stack-ref (fluid-ref the-last-stack)
|
|
|
|
|
|
(frame-number->index frame)))
|
1997-08-24 03:38:48 +00:00
|
|
|
|
(source (frame-source frame)))
|
|
|
|
|
|
(or source
|
|
|
|
|
|
(begin (no-source)
|
|
|
|
|
|
#f)))))
|
|
|
|
|
|
|
|
|
|
|
|
(define (emacs-select-frame frame)
|
|
|
|
|
|
(let ((source (get-frame-source frame)))
|
|
|
|
|
|
(if source
|
|
|
|
|
|
(let ((fname (source-property source 'filename))
|
|
|
|
|
|
(line (source-property source 'line))
|
|
|
|
|
|
(column (source-property source 'column)))
|
|
|
|
|
|
(if (and fname line column)
|
|
|
|
|
|
(list fname line column)
|
|
|
|
|
|
(begin (no-source)
|
|
|
|
|
|
'())))
|
|
|
|
|
|
'())))
|
|
|
|
|
|
|
1997-08-25 20:03:21 +00:00
|
|
|
|
(define (object->string x . method)
|
|
|
|
|
|
(with-output-to-string
|
|
|
|
|
|
(lambda ()
|
|
|
|
|
|
((if (null? method)
|
|
|
|
|
|
write
|
|
|
|
|
|
(car method))
|
|
|
|
|
|
x))))
|
|
|
|
|
|
|
|
|
|
|
|
(define (format template . rest)
|
|
|
|
|
|
(let loop ((chars (string->list template))
|
1998-11-01 04:52:58 +00:00
|
|
|
|
(result '())
|
|
|
|
|
|
(rest rest))
|
1997-08-25 20:03:21 +00:00
|
|
|
|
(cond ((null? chars) (list->string (reverse result)))
|
|
|
|
|
|
((char=? (car chars) #\%)
|
|
|
|
|
|
(loop (cddr chars)
|
|
|
|
|
|
(append (reverse
|
|
|
|
|
|
(string->list
|
|
|
|
|
|
(case (cadr chars)
|
|
|
|
|
|
((#\S) (object->string (car rest)))
|
|
|
|
|
|
((#\s) (object->string (car rest) display)))))
|
1998-11-01 04:52:58 +00:00
|
|
|
|
result)
|
|
|
|
|
|
(cdr rest)))
|
|
|
|
|
|
(else (loop (cdr chars) (cons (car chars) result) rest)))))
|
1997-08-25 20:03:21 +00:00
|
|
|
|
|
|
|
|
|
|
(define (error-args->string args)
|
|
|
|
|
|
(let ((msg (apply format (caddr args) (cadddr args))))
|
|
|
|
|
|
(if (symbol? (cadr args))
|
|
|
|
|
|
(string-append (symbol->string (cadr args))
|
|
|
|
|
|
": "
|
|
|
|
|
|
msg)
|
|
|
|
|
|
msg)))
|
|
|
|
|
|
|
2009-11-26 22:11:40 +01:00
|
|
|
|
;; FIXME: no longer working due to removal of local-eval
|
1997-08-24 03:38:48 +00:00
|
|
|
|
(define (emacs-frame-eval frame form)
|
|
|
|
|
|
(let ((source (get-frame-source frame)))
|
1997-08-25 20:03:21 +00:00
|
|
|
|
(if source
|
|
|
|
|
|
(catch #t
|
|
|
|
|
|
(lambda ()
|
|
|
|
|
|
(list 'result
|
|
|
|
|
|
(object->string
|
|
|
|
|
|
(local-eval (with-input-from-string form read)
|
|
|
|
|
|
(memoized-environment source)))))
|
|
|
|
|
|
(lambda args
|
|
|
|
|
|
(list (car args)
|
|
|
|
|
|
(error-args->string args))))
|
|
|
|
|
|
(begin
|
|
|
|
|
|
(no-source)
|
|
|
|
|
|
'()))))
|
1997-08-24 03:38:48 +00:00
|
|
|
|
|
|
|
|
|
|
(define (emacs-symdoc symbol)
|
|
|
|
|
|
(if (or (not (module-bound? (current-module) symbol))
|
2000-08-13 19:21:25 +00:00
|
|
|
|
(not (procedure? (eval symbol (interaction-environment)))))
|
1997-08-24 03:38:48 +00:00
|
|
|
|
'nil
|
2000-08-11 08:45:45 +00:00
|
|
|
|
(procedure-documentation (eval symbol (interaction-environment)))))
|
1997-08-24 03:38:48 +00:00
|
|
|
|
|
1997-03-01 02:04:00 +00:00
|
|
|
|
;;; A fix to get the emacs interface to work together with the module system.
|
|
|
|
|
|
;;;
|
2001-10-08 12:40:14 +00:00
|
|
|
|
(for-each (lambda (name value)
|
|
|
|
|
|
(module-define! the-root-module name value))
|
|
|
|
|
|
'(%%load-port
|
|
|
|
|
|
%%emacs-load
|
|
|
|
|
|
%%emacs-eval-request
|
|
|
|
|
|
%%emacs-select-frame
|
|
|
|
|
|
%%emacs-frame-eval
|
|
|
|
|
|
%%emacs-symdoc
|
|
|
|
|
|
%%apropos-internal)
|
|
|
|
|
|
(list load-port
|
|
|
|
|
|
emacs-load
|
|
|
|
|
|
emacs-eval-request
|
|
|
|
|
|
emacs-select-frame
|
|
|
|
|
|
emacs-frame-eval
|
|
|
|
|
|
emacs-symdoc
|
|
|
|
|
|
apropos-internal))
|