1
Fork 0
mirror of https://git.savannah.gnu.org/git/guile.git synced 2025-05-01 12:20:26 +02:00

repl read/write using current ports, not captured ports

Fixes bug in repl meta-commands after activating readline, which changes
the current input port.

* module/system/repl/common.scm (<repl>): Remove inport and outport
  fields.
  (make-repl): Adapt.
  (repl-read, repl-print): Just read and write to the current ports.

* module/system/repl/repl.scm (meta-reader): Meta-read from the current
  input port.

* module/system/repl/command.scm (read-command, define-meta-command):
  Read from the current input port.
This commit is contained in:
Andy Wingo 2010-11-18 14:32:53 +01:00
parent 9b5fcde6f9
commit c372cd74fd
3 changed files with 27 additions and 35 deletions

View file

@ -136,7 +136,7 @@
(define (read-command repl) (define (read-command repl)
(catch #t (catch #t
(lambda () (read (repl-inport repl))) (lambda () (read))
(lambda (key . args) (lambda (key . args)
(pmatch args (pmatch args
((,subr ,msg ,args . ,rest) ((,subr ,msg ,args . ,rest)
@ -148,11 +148,6 @@
(force-output) (force-output)
*unspecified*))) *unspecified*)))
(define read-line
(let ((orig-read-line read-line))
(lambda (repl)
(orig-read-line (repl-inport repl)))))
(define (meta-command repl) (define (meta-command repl)
(let ((command (read-command repl))) (let ((command (read-command repl)))
(cond (cond
@ -183,19 +178,19 @@
(% (let* ((expression0 (% (let* ((expression0
(catch #t (catch #t
(lambda () (lambda ()
(repl-reader "" (repl-reader
(lambda* (#:optional (port (repl-inport repl))) ""
((language-reader (repl-language repl)) (lambda* (#:optional (port (current-input-port)))
port (current-module))))) ((language-reader (repl-language repl))
port (current-module)))))
(lambda (k . args) (lambda (k . args)
(handle-read-error 'expression0 k args)))) (handle-read-error 'expression0 k args))))
...) ...)
(apply (lambda* datums (apply (lambda* datums
(with-output-to-port (repl-outport repl) b0 b1 ...)
(lambda () b0 b1 ...)))
(catch #t (catch #t
(lambda () (lambda ()
(let ((port (open-input-string (read-line repl)))) (let ((port (open-input-string (read-line))))
(let lp ((out '())) (let lp ((out '()))
(let ((x (read port))) (let ((x (read port)))
(if (eof-object? x) (if (eof-object? x)

View file

@ -26,7 +26,7 @@
#:use-module (ice-9 control) #:use-module (ice-9 control)
#:use-module (ice-9 history) #:use-module (ice-9 history)
#:export (<repl> make-repl repl-language repl-options #:export (<repl> make-repl repl-language repl-options
repl-tm-stats repl-gc-stats repl-inport repl-outport repl-debug repl-tm-stats repl-gc-stats repl-debug
repl-welcome repl-prompt repl-welcome repl-prompt
repl-read repl-compile repl-prepare-eval-thunk repl-eval repl-read repl-compile repl-prepare-eval-thunk repl-eval
repl-parse repl-print repl-option-ref repl-option-set! repl-parse repl-print repl-option-ref repl-option-set!
@ -102,7 +102,7 @@ See <http://www.gnu.org/licenses/lgpl.html>, for more details.")
;;; ;;;
(define-record/keywords <repl> (define-record/keywords <repl>
language options tm-stats gc-stats inport outport debug) language options tm-stats gc-stats debug)
(define repl-default-options (define repl-default-options
(copy-tree (copy-tree
@ -128,8 +128,6 @@ See <http://www.gnu.org/licenses/lgpl.html>, for more details.")
#:options (copy-tree repl-default-options) #:options (copy-tree repl-default-options)
#:tm-stats (times) #:tm-stats (times)
#:gc-stats (gc-stats) #:gc-stats (gc-stats)
#:inport (current-input-port)
#:outport (current-output-port)
#:debug debug)) #:debug debug))
(define (repl-welcome repl) (define (repl-welcome repl)
@ -151,8 +149,8 @@ See <http://www.gnu.org/licenses/lgpl.html>, for more details.")
(if (zero? level) "" (format #f " [~a]" level))))))) (if (zero? level) "" (format #f " [~a]" level)))))))
(define (repl-read repl) (define (repl-read repl)
((language-reader (repl-language repl)) (repl-inport repl) (let ((reader (language-reader (repl-language repl))))
(current-module))) (reader (current-input-port) (current-module))))
(define (repl-compile-options repl) (define (repl-compile-options repl)
(repl-option-ref repl 'compile-options)) (repl-option-ref repl 'compile-options))
@ -187,8 +185,8 @@ See <http://www.gnu.org/licenses/lgpl.html>, for more details.")
;; should be printed with the generic printer, `write'. The ;; should be printed with the generic printer, `write'. The
;; language-printer is something else: it prints expressions of ;; language-printer is something else: it prints expressions of
;; a given language, not the result of evaluation. ;; a given language, not the result of evaluation.
(write val (repl-outport repl)) (write val)
(newline (repl-outport repl))))) (newline))))
(define (repl-option-ref repl key) (define (repl-option-ref repl key)
(cadr (or (assq key (repl-options repl)) (cadr (or (assq key (repl-options repl))

View file

@ -63,20 +63,19 @@
(define meta-command-token (cons 'meta 'command)) (define meta-command-token (cons 'meta 'command))
(define (meta-reader read env) (define (meta-reader read env)
(lambda read-args (lambda* (#:optional (port (current-input-port)))
(let ((port (if (pair? read-args) (car read-args) (current-input-port)))) (with-input-from-port port
(with-input-from-port port (lambda ()
(lambda () (let ((ch (next-char #t)))
(let ((ch (next-char #t))) (cond ((eof-object? ch)
(cond ((eof-object? ch) ;; EOF objects are not buffered. It's quite possible
;; EOF objects are not buffered. It's quite possible ;; to peek an EOF then read something else. It's
;; to peek an EOF then read something else. It's ;; strange but it's how it works.
;; strange but it's how it works. ch)
ch) ((eqv? ch #\,)
((eqv? ch #\,) (read-char port)
(read-char port) meta-command-token)
meta-command-token) (else (read port env))))))))
(else (read port env)))))))))
;; repl-reader is a function defined in boot-9.scm, and is replaced by ;; repl-reader is a function defined in boot-9.scm, and is replaced by
;; something else if readline has been activated. much of this hoopla is ;; something else if readline has been activated. much of this hoopla is