From 966ece57c21dca49e65f66d5882995e5f077f262 Mon Sep 17 00:00:00 2001 From: Alex Shinn Date: Sat, 8 Dec 2012 15:23:19 +0900 Subject: [PATCH] Allowing multi-line input to (chibi repl) from Emacs and raw terminals. --- lib/chibi/repl.scm | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/lib/chibi/repl.scm b/lib/chibi/repl.scm index 1437e62d..6fbc7b27 100644 --- a/lib/chibi/repl.scm +++ b/lib/chibi/repl.scm @@ -28,11 +28,26 @@ (define (write-to-string x) (call-with-output-string (lambda (out) (write x out)))) -(define (buffer-complete-sexp? buf) - (call-with-input-string (buffer->string buf) +(define (complete-sexp? str) + (call-with-input-string str (lambda (in) (let lp () (if (not (eof-object? (read/ss in))) (lp)))))) +(define (read-line/complete-sexp in) + (let lp ((res "")) + (let ((line (read-line in))) + (cond + ((eof-object? line) + (if (equal? res "") line res)) + (else + (let ((res (string-append res line "\n"))) + (if (guard (exn (else #f)) (complete-sexp? res)) + res + (lp res)))))))) + +(define (buffer-complete-sexp? buf) + (complete-sexp? (buffer->string buf))) + (define module? vector?) (define (module-env mod) (vector-ref mod 1)) @@ -149,7 +164,7 @@ (raw? (display prompt out) (flush-output out) - (read-line in)) + (read-line/complete-sexp in)) (else (edit-line in out 'prompt: prompt