2005-06-07 14:24:05 -04:00
|
|
|
(define-record-type terminal-buffer :terminal-buffer
|
|
|
|
(really-make-terminal-buffer width height view-index
|
2005-06-07 16:52:49 -04:00
|
|
|
x y buffer paint-fun
|
2005-06-07 14:24:05 -04:00
|
|
|
esc-code)
|
|
|
|
terminal-buffer?
|
|
|
|
(width terminal-buffer-width)
|
|
|
|
(height terminal-buffer-height)
|
|
|
|
(view-index terminal-buffer-view-index
|
|
|
|
set-terminal-buffer-view-index!)
|
|
|
|
(x terminal-buffer-x set-terminal-buffer-x!)
|
|
|
|
(y terminal-buffer-y set-terminal-buffer-y!)
|
|
|
|
(buffer terminal-buffer-buffer set-terminal-buffer-buffer!)
|
2005-06-07 16:52:49 -04:00
|
|
|
(paint-fun terminal-buffer-paint-fun set-terminal-buffer-paint-fun!)
|
2005-06-07 14:24:05 -04:00
|
|
|
(esc-code terminal-buffer-esc-code set-terminal-buffer-esc-code!))
|
|
|
|
|
|
|
|
(define-record-discloser :terminal-buffer
|
|
|
|
(lambda (tb)
|
|
|
|
`(terminal-buffer
|
|
|
|
(width ,(terminal-buffer-width tb))
|
|
|
|
(height ,(terminal-buffer-height tb))
|
|
|
|
(x ,(terminal-buffer-x tb))
|
|
|
|
(y ,(terminal-buffer-y tb))
|
|
|
|
(esc-code ,(map char->ascii (string->list (terminal-buffer-esc-code tb)))))))
|
|
|
|
|
|
|
|
(define (make-terminal-buffer width height)
|
|
|
|
(let ((buffer (map
|
|
|
|
(lambda (ignore)
|
|
|
|
(make-empty-line width))
|
|
|
|
(iota height))))
|
2005-06-07 16:52:49 -04:00
|
|
|
(really-make-terminal-buffer
|
|
|
|
width height buffer 0 0 buffer
|
|
|
|
curses-paint-terminal-buffer/complete
|
|
|
|
"")))
|
2005-06-07 14:24:05 -04:00
|
|
|
|
|
|
|
(define (line-at-cursor-position termbuf)
|
|
|
|
(list-ref (terminal-buffer-view-index termbuf)
|
|
|
|
(terminal-buffer-y termbuf)))
|
|
|
|
|
|
|
|
(define (make-empty-line width)
|
|
|
|
(make-string width #\space))
|
|
|
|
|
|
|
|
(define (cursor-at-end-of-line? termbuf)
|
|
|
|
(= (terminal-buffer-x termbuf)
|
|
|
|
(- (terminal-buffer-width termbuf) 1)))
|
|
|
|
|
|
|
|
(define (cursor-on-last-line? termbuf)
|
|
|
|
(= (terminal-buffer-y termbuf)
|
|
|
|
(- (terminal-buffer-height termbuf) 1)))
|
|
|
|
|
|
|
|
(define (append-empty-line termbuf)
|
|
|
|
(append! (terminal-buffer-buffer termbuf)
|
|
|
|
(list (make-empty-line (terminal-buffer-width termbuf)))))
|
|
|
|
|
|
|
|
(define (goto-next-line termbuf)
|
|
|
|
(set-terminal-buffer-y!
|
|
|
|
termbuf (+ (terminal-buffer-y termbuf) 1))
|
|
|
|
(maybe-scroll-terminal-buffer termbuf))
|
|
|
|
|
|
|
|
(define (move-cursor termbuf x-offset y-offset)
|
|
|
|
(let ((new-x (+ (terminal-buffer-x termbuf) x-offset))
|
|
|
|
(new-y (+ (terminal-buffer-y termbuf) y-offset)))
|
|
|
|
|
|
|
|
(cond
|
|
|
|
((< new-x 0)
|
|
|
|
(set-terminal-buffer-x! termbuf 0))
|
|
|
|
((>= new-x (terminal-buffer-width termbuf))
|
|
|
|
(set-terminal-buffer-x!
|
|
|
|
termbuf (- (terminal-buffer-width termbuf) 1)))
|
|
|
|
(else
|
|
|
|
(set-terminal-buffer-x! termbuf new-x)))
|
|
|
|
|
|
|
|
(cond
|
|
|
|
((< new-y 0)
|
|
|
|
(set-terminal-buffer-y! termbuf 0))
|
|
|
|
((>= new-y (terminal-buffer-width termbuf))
|
|
|
|
(set-terminal-buffer-y!
|
|
|
|
(termbuf (- (terminal-buffer-width termbuf) 1))))
|
|
|
|
(else
|
|
|
|
(set-terminal-buffer-y! termbuf new-y)))))
|
|
|
|
|
|
|
|
(define (scroll-view-index-down termbuf)
|
|
|
|
(set-terminal-buffer-view-index!
|
|
|
|
termbuf (cdr (terminal-buffer-view-index termbuf)))
|
2005-06-07 16:52:49 -04:00
|
|
|
(set-terminal-buffer-paint-fun!
|
|
|
|
termbuf paint-complete-buffer))
|
2005-06-07 14:24:05 -04:00
|
|
|
|
|
|
|
(define (maybe-scroll-terminal-buffer termbuf)
|
|
|
|
(and (>= (terminal-buffer-y termbuf)
|
|
|
|
(terminal-buffer-height termbuf))
|
|
|
|
(begin
|
|
|
|
(scroll-view-index-down termbuf)
|
|
|
|
(set-terminal-buffer-y!
|
|
|
|
termbuf
|
|
|
|
(- (terminal-buffer-y termbuf) 1)))))
|
|
|
|
|
|
|
|
(define (goto-beginning-of-line termbuf)
|
|
|
|
(set-terminal-buffer-x! termbuf 0))
|
|
|
|
|
|
|
|
(define (terminal-buffer-add-char termbuf char)
|
|
|
|
(cond
|
|
|
|
((not (string=? "" (terminal-buffer-esc-code termbuf)))
|
|
|
|
(read-escape-code termbuf char))
|
|
|
|
((char=? char (ascii->char 27))
|
|
|
|
(set-terminal-buffer-esc-code! termbuf (string char)))
|
2005-06-07 15:50:43 -04:00
|
|
|
((char=? char (ascii->char 13))
|
2005-06-07 16:52:49 -04:00
|
|
|
(goto-beginning-of-line termbuf)
|
|
|
|
(set-terminal-buffer-paint-fun! termbuf position-cursor))
|
2005-06-07 14:24:05 -04:00
|
|
|
((char=? char #\newline)
|
|
|
|
(if (cursor-on-last-line? termbuf)
|
|
|
|
(append-empty-line termbuf))
|
2005-06-07 16:52:49 -04:00
|
|
|
(goto-next-line termbuf)
|
|
|
|
(set-terminal-buffer-paint-fun! termbuf position-cursor))
|
2005-06-07 15:50:43 -04:00
|
|
|
((char-set-contains? char-set:printing char)
|
2005-06-07 14:24:05 -04:00
|
|
|
(add-normal-char termbuf char))))
|
|
|
|
|
|
|
|
(define (add-normal-char termbuf char)
|
|
|
|
(cond
|
|
|
|
((cursor-at-end-of-line? termbuf)
|
|
|
|
(append-empty-line termbuf)
|
|
|
|
(goto-next-line termbuf)
|
|
|
|
(goto-beginning-of-line termbuf)
|
2005-06-07 16:52:49 -04:00
|
|
|
(insert-char termbuf char)
|
|
|
|
(set-terminal-buffer-paint-fun!
|
|
|
|
termbuf paint-complete-buffer))
|
2005-06-07 14:24:05 -04:00
|
|
|
(else
|
|
|
|
(insert-char termbuf char)
|
2005-06-07 16:52:49 -04:00
|
|
|
(goto-next-char termbuf)
|
|
|
|
(set-terminal-buffer-paint-fun!
|
|
|
|
termbuf paint-single-char))))
|
2005-06-07 14:24:05 -04:00
|
|
|
|
2005-06-07 16:52:49 -04:00
|
|
|
(define (paint-complete-buffer termbuf win)
|
2005-06-07 14:24:05 -04:00
|
|
|
(wclear win)
|
|
|
|
(let lp ((i (terminal-buffer-height termbuf))
|
|
|
|
(lines (terminal-buffer-view-index termbuf))
|
|
|
|
(y 0))
|
|
|
|
(if (zero? i)
|
|
|
|
'blorf
|
|
|
|
(begin
|
|
|
|
(mvwaddstr win y 0 (car lines))
|
|
|
|
(lp (- i 1) (cdr lines) (+ y 1)))))
|
|
|
|
(position-cursor termbuf win))
|
|
|
|
|
|
|
|
(define (paint-single-line termbuf win)
|
2005-06-07 16:03:38 -04:00
|
|
|
(wmove win (terminal-buffer-y termbuf) 0)
|
2005-06-07 14:24:05 -04:00
|
|
|
(wclrtoeol win)
|
2005-06-07 16:03:38 -04:00
|
|
|
(waddstr win (line-at-cursor-position termbuf))
|
2005-06-07 14:24:05 -04:00
|
|
|
(position-cursor termbuf win))
|
|
|
|
|
2005-06-07 16:52:49 -04:00
|
|
|
(define (paint-single-char termbuf win)
|
|
|
|
(let ((x (if (zero? (terminal-buffer-x termbuf))
|
|
|
|
(terminal-buffer-x termbuf)
|
|
|
|
(- (terminal-buffer-x termbuf) 1))))
|
|
|
|
(waddch win
|
|
|
|
; (terminal-buffer-y termbuf)
|
|
|
|
; x
|
|
|
|
(string-ref (line-at-cursor-position termbuf) x))))
|
|
|
|
|
|
|
|
(define (curses-paint-terminal-buffer termbuf win)
|
|
|
|
((terminal-buffer-paint-fun termbuf) termbuf win))
|
|
|
|
|
|
|
|
(define curses-paint-terminal-buffer/complete
|
|
|
|
paint-complete-buffer)
|
|
|
|
|
2005-06-07 14:24:05 -04:00
|
|
|
(define (position-cursor termbuf win)
|
|
|
|
(wmove win
|
|
|
|
(terminal-buffer-y termbuf)
|
|
|
|
(terminal-buffer-x termbuf)))
|
|
|
|
|
|
|
|
(define (insert-char termbuf char)
|
|
|
|
(string-set! (line-at-cursor-position termbuf)
|
|
|
|
(terminal-buffer-x termbuf)
|
|
|
|
char))
|
|
|
|
|
|
|
|
(define (goto-next-char termbuf)
|
|
|
|
(set-terminal-buffer-x!
|
|
|
|
termbuf (+ 1 (terminal-buffer-x termbuf))))
|
|
|
|
|
|
|
|
(define (read-escape-code termbuf char)
|
|
|
|
(let ((code (string-append
|
|
|
|
(terminal-buffer-esc-code termbuf)
|
|
|
|
(string char))))
|
|
|
|
(cond
|
|
|
|
;; very ugly hack
|
|
|
|
((> (string-length code) 5)
|
|
|
|
(set-terminal-buffer-esc-code! termbuf ""))
|
|
|
|
((recognize-simple-cursor-movement code)
|
|
|
|
=> (lambda (lst)
|
|
|
|
(apply move-cursor (cons termbuf lst))
|
|
|
|
(set-terminal-buffer-esc-code! termbuf "")))
|
|
|
|
((recognize-cursor-movement code)
|
|
|
|
=> (lambda (lst)
|
|
|
|
(apply move-cursor (cons termbuf lst))
|
|
|
|
(set-terminal-buffer-esc-code! termbuf "")))
|
|
|
|
(else
|
|
|
|
(set-terminal-buffer-esc-code! termbuf code)))))
|
|
|
|
|
|
|
|
(define (recognize-cursor-movement partial-code)
|
|
|
|
(if-match
|
|
|
|
(regexp-search (rx (: ,(ascii->char 27)
|
|
|
|
#\[ (submatch digit) (submatch ("ABCD"))
|
|
|
|
eos))
|
|
|
|
partial-code)
|
|
|
|
(whole-code count direction)
|
|
|
|
(cond
|
|
|
|
((string=? direction "A")
|
|
|
|
(list 0 (- (string->number count))))
|
|
|
|
((string=? direction "B")
|
|
|
|
(list 0 (string->number count)))
|
|
|
|
((string=? direction "C")
|
|
|
|
(list (string->number count) 0))
|
|
|
|
((string=? direction "D")
|
|
|
|
(list (- (string->number count)) 0))
|
|
|
|
(else (error 'gnarf direction)))
|
|
|
|
#f))
|
|
|
|
|
|
|
|
(define (recognize-simple-cursor-movement partial-code)
|
|
|
|
(if-match
|
|
|
|
(regexp-search (rx (: ,(ascii->char 27) (? #\[) (? #\O) (submatch ("ABCD")) eos))
|
|
|
|
partial-code)
|
|
|
|
(whole-code direction)
|
|
|
|
(cond
|
|
|
|
((string=? direction "A") '( 0 -1))
|
|
|
|
((string=? direction "B") '( 0 1))
|
|
|
|
((string=? direction "C") '( 1 0))
|
|
|
|
((string=? direction "D") '(-1 0))
|
|
|
|
(else (error 'gnarf2 (string? direction) (char? direction) )))
|
|
|
|
(begin (debug-message "does not match ") #f)))
|