Better explicit control over utf-8 input mode

This commit is contained in:
Tony Garnock-Jones 2014-12-27 15:23:46 -05:00
parent 45b89d9682
commit 46d97f0926
3 changed files with 20 additions and 6 deletions

View File

@ -145,7 +145,7 @@
[(<= #x20 b #x7e) (simple-key (integer->char b))]
[(= b #x7f) (simple-key 'backspace)]))
(define (lex-lcd-input port)
(define (lex-lcd-input port #:utf-8? [utf-8? (lcd-terminal-utf-8?)])
(cond
[(eof-object? (peek-byte port)) eof]
[(or (regexp-try-match #px"^\e\\[([0-9]+(;[0-9]+)*)?(.)" port)
@ -168,8 +168,8 @@
;; Characters between #\u80 and #\uff are ambiguous because in
;; some terminals, the high bit is set to indicate meta, and in
;; others, they are plain UTF-8 characters. We let the user
;; distinguish via the lcd-terminal-utf-8? parameter.
[(not (lcd-terminal-utf-8?))
;; distinguish via the #:utf-8? keyword argument.
[(not utf-8?)
(define b (read-byte port))
(if (< b 128)
(interpret-ascii-code b)

View File

@ -9,6 +9,12 @@
(define (main)
(tty-raw!)
(define utf-8?
(match (current-command-line-arguments)
[(or '#() '#("--utf-8")) #t]
['#("--no-utf-8") #f]
[_ (error 'main "Usage: test-raw [ --utf-8 / --no-utf-8 ]")]))
(plumber-add-flush! (current-plumber)
(lambda (handle)
(display (reset-mode x11-any-event-mouse-tracking-mode))))
@ -19,7 +25,7 @@
(display "Type keys. Press control-D to exit.\r\n")
(let loop ()
(flush-output)
(match (lex-lcd-input (current-input-port))
(match (lex-lcd-input (current-input-port) #:utf-8? utf-8?)
[(? eof-object?) (void)]
[(== (key #\D (set 'control))) (void)]
[key

View File

@ -58,6 +58,7 @@
key-reader ;; InputPort -> Key
[displayed-screen #:mutable] ;; Screen
[pending-screen #:mutable] ;; Screen
[utf-8-input? #:mutable] ;; Boolean
) #:prefab)
(define (make-screen rows columns pen)
@ -80,7 +81,14 @@
(current-output-port)
ansi:lex-lcd-input
(make-screen 24 80 tty-default-pen)
(make-screen 24 80 tty-default-pen)))
(make-screen 24 80 tty-default-pen)
(match (getenv "RMACS_UTF8_INPUT")
[(or #f "yes" "true" "1") #t]
[(or "no" "false" "0") #f]
[v (error 'RMACS_UTF8_INPUT
"Environment variable RMACS_UTF8_INPUT value ~v invalid: must be in ~v"
v
(list "yes" "true" "1" "no" "false" "0"))])))
(reset *stdin-tty*)
(plumber-add-flush! (current-plumber)
(lambda (h)
@ -360,7 +368,7 @@
;; Input
(define (tty-next-key tty)
(define k (ansi:lex-lcd-input (tty-input tty)))
(define k (ansi:lex-lcd-input (tty-input tty) #:utf-8? (tty-utf-8-input? tty)))
(if (equal? k (ansi:key #\[ (set 'control))) ;; ESC
(or (sync/timeout 0.5
(handle-evt (tty-next-key-evt tty)