I started reading through the article "Build Your Own Text Editor" about how to build a simple terminal-based text editor in C. I thought it would be fun to see if something similar could be done in Scheme (Chez, Racket, or Chicken).
I am having problems with capturing terminal input in raw mode. I can't figure out how to obtain a character immediately on a keypress. I put together the following monstrosity (which does not work anyway) in Racket based on the answer for Common Lisp at Rosetta Code.
;; echo and work
(define (eaw)
(let ((acc "")
(ip (current-input-port))
(done #f))
(let loop ()
(if (char-ready? ip)
(let ((c (read-char ip)))
(begin
(if (char=? c #\q)
(begin
(set! done #t)
acc)
(begin
(when (not done)
(set! acc (string-append acc (string c))))
(loop)))))
(if done
(begin
(display (string? acc))
acc)
(begin
;; do some work
(display ".")
(sleep 0.15)
(loop)))))))
The Racket example on the same page doesn't work for me -- problems with trying to open the terminal.
It looks like Chez's expediter, used in the REPL, is written in C. In the Chicken ncurses library, it calls out to the C runtime to implement the getch function. Not sure where Racket implements it.
Is there no way I can implement raw keyboard input in Scheme (any one of them) so the program can respond immediately to shortcut keys and so on?