diff options
author | jao <jao@gnu.org> | 2022-10-24 00:13:41 +0100 |
---|---|---|
committer | jao <jao@gnu.org> | 2022-10-24 00:13:41 +0100 |
commit | 68fb9f4724440ddb854421f51a37ee88e1044af2 (patch) | |
tree | 71c80b12af8c4b83dffe6b36442d2a9b258211ef /src/geiser/evaluation.scm | |
parent | ea8668dbe7ded2d2d7a31cdf5ee52c3088f0cfc1 (diff) | |
download | geiser-guile-68fb9f4724440ddb854421f51a37ee88e1044af2.tar.gz geiser-guile-68fb9f4724440ddb854421f51a37ee88e1044af2.tar.bz2 |
persistent port for asynchronous evaluation output
using ideas and code from Christine Lemmer-Webber, see #25 for discussion.
Diffstat (limited to 'src/geiser/evaluation.scm')
-rw-r--r-- | src/geiser/evaluation.scm | 28 |
1 files changed, 25 insertions, 3 deletions
diff --git a/src/geiser/evaluation.scm b/src/geiser/evaluation.scm index ed2fb39..7038d3a 100644 --- a/src/geiser/evaluation.scm +++ b/src/geiser/evaluation.scm @@ -27,6 +27,7 @@ #:use-module (system base pmatch) #:use-module (system vm program) #:use-module (ice-9 pretty-print) + #:use-module (ice-9 textual-ports) #:use-module (system vm loader))) (else (define-module (geiser evaluation) @@ -44,7 +45,8 @@ #:use-module (system base message) #:use-module (system base pmatch) #:use-module (system vm program) - #:use-module (ice-9 pretty-print)))) + #:use-module (ice-9 pretty-print) + #:use-module (ice-9 textual-ports)))) (define compile-opts '()) @@ -69,16 +71,36 @@ (ge:set-warnings 'none) +(define context-port #f) + +(define switcher-port + (make-soft-port (vector (lambda (c) (put-char c context-port)) + (lambda (s) (display s context-port)) + (lambda () (force-output context-port)) + (lambda () (close-port context-port)) + (lambda () 0)) + "w")) + +(define (call-with-switcher-output long-port thunk) + (let ((current (current-output-port))) + (parameterize ((current-output-port switcher-port)) + (dynamic-wind + (lambda () (set! context-port current)) + thunk + (lambda () (set! context-port long-port)))))) + (define (call-with-result thunk) (letrec* ((result #f) + (long-port (current-output-port)) + (run-thunk (lambda () (call-with-switcher-output long-port thunk))) (output (with-output-to-string (lambda () (with-fluids ((*current-warning-port* (current-output-port)) (*current-warning-prefix* "")) (with-error-to-port (current-output-port) - (lambda () (set! result - (map object->string (thunk)))))))))) + (lambda () + (set! result (map object->string (run-thunk)))))))))) (write `((result ,@result) (output . ,output))) (newline))) |