It's probably best to use dynamic-wind, turning the settings on in the before-thunk and off in the after-thunk, so you don't wind up with the terminal set wrongly when the process terminates, like this:

(dynamic-wind (lambda () (setup-port port)) (let loop ...) (lambda () (restore-port port))))


On Wed, Sep 4, 2019 at 12:05 PM Marc Feeley <feeley@iro.umontreal.ca> wrote:

Marc



> On Sep 4, 2019, at 11:34 AM, John Cowan <cowan@ccil.org> wrote:
>
> The buffering setting has to do with buffering within the Scheme process, whereas the tty setting has to do with buffering within the operating system.  In order to get single-character reads from the terminal, you have to turn off both.

Indeed.

The tty settings can be changed by calling tty-mode-set! as shown below.

Marc


;;; File: console-read-one-char-at-a-time.scm

;; run like this:
;;
;;   gsi console-read-one-char-at-a-time.scm

(define (setup port)
  (if (tty? port) ;; change the tty settings to read raw characters 1 at a time
      (tty-mode-set! port
                     #f ;; input-allow-special
                     #f ;; input-echo
                     #t ;; input-raw
                     #f ;; output-raw
                     0))) ;; speed

(define port (current-input-port))

(setup port)

(let loop ()
  (let ((c (read-char port)))
    (pp (list 'typed c))
    (loop)))