summaryrefslogtreecommitdiff
path: root/catbird/line-editor.scm
blob: 74ced0b1fce776f2fd9815aa8e47fea5dfc111d2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
(define-module (catbird line-editor)
  #:use-module (catbird mode)
  #:use-module (catbird node)
  #:use-module (catbird node-2d)
  #:use-module (catbird observer)
  #:use-module (catbird region)
  #:use-module (catbird ring-buffer)
  #:use-module (catbird scene)
  #:use-module (chickadee)
  #:use-module (chickadee graphics color)
  #:use-module (chickadee graphics path)
  #:use-module (chickadee graphics text)
  #:use-module (chickadee math vector)
  #:use-module (chickadee scripting)
  #:use-module (ice-9 match)
  #:use-module (oop goops)
  #:use-module (srfi srfi-1)
  #:export (<line-editor>
            <line-edit-mode>
            backward-char
            backward-delete-char
            forward-delete-char
            backward-history
            beginning-of-line
            clear-line
            end-of-line
            forward-char
            forward-history
            get-line
            history-enabled?
            insert-char
            invert-color
            kill-line
            overwrite
            prompt
            save-to-history)
  #:re-export (color
               font))

;; TODO: Matching paren/quote highlighting.
(define-class <line-editor> (<node-2d>)
  (chars-before #:accessor chars-before #:init-value '())
  (chars-after #:accessor chars-after #:init-value '())
  (cached-line #:accessor cached-line #:init-value #f)
  (prompt #:accessor prompt #:init-keyword #:prompt #:init-value ""
          #:observe? #t)
  ;; TODO: Allow customizable history length.
  (history #:accessor history #:init-form (make-ring-buffer 128))
  (history-enabled? #:accessor history-enabled?
                    #:init-keyword #:history-enabled? #:init-value #t)
  (history-index #:accessor history-index #:init-value 0)
  (font #:accessor font #:init-keyword #:font #:init-thunk default-font
        #:asset? #t)
  (color #:accessor color #:init-keyword #:color #:init-value white)
  (invert-color #:accessor invert-color #:init-keyword #:invert-color
                #:init-value black)
  (accepting-input? #:accessor accepting-input? #:init-value #t))

(define-method (on-change (editor <line-editor>) slot old new)
  (update-visual editor))

(define-method (on-boot (editor <line-editor>))
  (attach-to editor
             (make <label>
               #:name 'prompt
               #:font (font editor)
               #:text (prompt editor)
               #:color (color editor))
             (make <label>
               #:name 'before-cursor
               #:rank 1
               #:font (font editor)
               #:color (color editor))
             (make <label>
               #:name 'on-cursor
               #:rank 1
               #:font (font editor))
             (make <label>
               #:name 'after-cursor
               #:rank 1
               #:font (font editor)
               #:color (color editor))
             (make <canvas>
               #:name 'cursor
               #:painter
               (with-style ((fill-color (color editor)))
                 (fill
                  (rectangle (vec2 0.0 0.0)
                             (font-line-width (font editor) "_")
                             (font-line-height (font editor)))))))
  (discard-next-char editor)
  (update-visual editor))

;; Whenever a command key sequence is pressed while the line editor is
;; active we have to stop accepting text input for one tick.
;; Otherwise, an errant character shows up.  For example, if the user
;; presses alt+p, and that is bound to (backward-history editor 1),
;; the 'p' character shows up at the end of the line.  This is due to
;; the fact that SDL generates a key event *and* an input event for
;; the same key press.
(define-method (discard-next-char (editor <line-editor>))
  (run-script editor
    (set! (accepting-input? editor) #f)
    (sleep (current-timestep))
    (set! (accepting-input? editor) #t)))

(define-method (update-visual (editor <line-editor>))
  (let* ((lprompt (& editor prompt))
         (cursor (& editor cursor))
         (before (& editor before-cursor))
         (on (& editor on-cursor))
         (after (& editor after-cursor)))
    (set! (cached-line editor) #f)
    ;; Stop cursor blink.  The cursor should only blink when the user
    ;; is idle.
    (stop-scripts cursor)
    ;; Ensure the cursor is visible in case we stopped the script
    ;; during a time when it was hidden.
    (show cursor)
    ;; Put the proper text in the proper labels
    (set! (text lprompt) (prompt editor))
    (set! (text before)
          (list->string (reverse (chars-before editor))))
    (set! (text on)
          (match (chars-after editor)
            (() "")
            ((c . _)
             (string c))))
    (set! (text after)
          (match (chars-after editor)
            (() "")
            ((_ . chars)
             (list->string chars))))
    ;; Line everything up.
    (place-right lprompt before)
    (place-right before on)
    (place-right on after)
    (align-left on cursor)
    ;; Adjust size
    (set! (width editor)
          (+ (width lprompt) (width before) (width on) (width after)))
    (set! (height editor) (height cursor))
    ;; Resume blinking cursor after a short idle timeout.
    (run-script cursor
      (forever
       (set! (color on) (invert-color editor))
       (sleep 0.5)
       (hide cursor)
       (set! (color on) (color editor))
       (sleep 0.5)
       (show cursor)))))

(define-method (get-line (editor <line-editor>))
  (or (cached-line editor)
      (let ((line (list->string
                   (append (reverse (chars-before editor))
                           (chars-after editor)))))
        (set! (cached-line editor) line)
        line)))

(define-method (overwrite (editor <line-editor>) str)
  (set! (chars-before editor) (reverse (string->list str)))
  (set! (chars-after editor) '())
  (update-visual editor))

(define-method (clear-line (editor <line-editor>))
  (discard-next-char editor)
  (overwrite editor "")
  (newest-history editor))

(define-method (insert-char (editor <line-editor>) char)
  (when (accepting-input? editor)
    (set! (chars-before editor) (cons char (chars-before editor)))
    (update-visual editor)))

(define-method (backward-delete-char (editor <line-editor>) n)
  (unless (<= n 0)
    (set! (chars-before editor)
          (drop (chars-before editor)
                (min n (length (chars-before editor))))))
  (discard-next-char editor)
  (update-visual editor))

(define-method (forward-delete-char (editor <line-editor>) n)
  (unless (<= n 0)
    (set! (chars-after editor)
          (drop (chars-after editor)
                (min n (length (chars-after editor))))))
  (discard-next-char editor)
  (update-visual editor))

(define-method (kill-line (editor <line-editor>))
  (forward-delete-char editor (length (chars-after editor))))

(define-method (backward-char (editor <line-editor>) n)
  (let loop ((n n)
             (before (chars-before editor))
             (after (chars-after editor)))
    (if (or (<= n 0) (null? before))
        (begin
          (set! (chars-before editor) before)
          (set! (chars-after editor) after))
        (loop (- n 1)
              (cdr before)
              (cons (car before) after))))
  (discard-next-char editor)
  (update-visual editor))

(define-method (forward-char (editor <line-editor>) n)
  (let loop ((n n)
             (before (chars-before editor))
             (after (chars-after editor)))
    (if (or (<= n 0) (null? after))
        (begin
          (set! (chars-before editor) before)
          (set! (chars-after editor) after))
        (loop (- n 1)
              (cons (car after) before)
              (cdr after))))
  (discard-next-char editor)
  (update-visual editor))

(define-method (beginning-of-line (editor <line-editor>))
  (backward-char editor (length (chars-before editor))))

(define-method (end-of-line (editor <line-editor>))
  (forward-char editor (length (chars-after editor))))

(define-method (save-to-history (editor <line-editor>))
  (ring-buffer-put! (history editor) (get-line editor)))

(define-method (history-ref (editor <line-editor>) i)
  (ring-buffer-ref (history editor) i))

(define-method (go-to-history (editor <line-editor>) i)
  (when (and (history-enabled? editor)
             (>= i 0)
             (< i (ring-buffer-length (history editor))))
    (set! (history-index editor) i)
    (overwrite editor (history-ref editor i))))

(define-method (backward-history (editor <line-editor>) n)
  (discard-next-char editor)
  (go-to-history editor (max (- (history-index editor) n) 0)))

(define-method (forward-history (editor <line-editor>) n)
  (discard-next-char editor)
  (go-to-history editor
                 (min (+ (history-index editor) n)
                      (- (ring-buffer-length (history editor)) 1))))

(define-method (newest-history (editor <line-editor>))
  (set! (history-index editor) (ring-buffer-length (history editor))))


;;;
;;; Line editing minor mode
;;;

(define-class <line-edit-mode> (<minor-mode>)
  (editor #:accessor editor #:init-keyword #:editor))

(define-method (insert-text (mode <line-edit-mode>) new-text)
  (let ((e (editor mode)))
    (string-for-each (lambda (char)
                       (insert-char e char))
                     new-text)))

(define-method (backward-delete-char (mode <line-edit-mode>))
  (backward-delete-char (editor mode) 1))

(define-method (forward-delete-char (mode <line-edit-mode>))
  (forward-delete-char (editor mode) 1))

(define-method (backward-char (mode <line-edit-mode>))
  (backward-char (editor mode) 1))

(define-method (forward-char (mode <line-edit-mode>))
  (forward-char (editor mode) 1))

(define-method (beginning-of-line (mode <line-edit-mode>))
  (beginning-of-line (editor mode)))

(define-method (end-of-line (mode <line-edit-mode>))
  (end-of-line (editor mode)))

(define-method (backward-history (mode <line-edit-mode>))
  (backward-history (editor mode) 1))

(define-method (forward-history (mode <line-edit-mode>))
  (forward-history (editor mode) 1))

(define-method (kill-line (mode <line-edit-mode>))
  (kill-line (editor mode)))

(bind-input <line-edit-mode> (key-press 'backspace) backward-delete-char)
(bind-input <line-edit-mode> (key-press 'delete) forward-delete-char)
(bind-input <line-edit-mode> (key-press 'd '(ctrl)) forward-delete-char)
(bind-input <line-edit-mode> (key-press 'left) backward-char)
(bind-input <line-edit-mode> (key-press 'b '(ctrl)) backward-char)
(bind-input <line-edit-mode> (key-press 'right) forward-char)
(bind-input <line-edit-mode> (key-press 'f '(ctrl)) forward-char)
(bind-input <line-edit-mode> (key-press 'home) beginning-of-line)
(bind-input <line-edit-mode> (key-press 'a '(ctrl)) beginning-of-line)
(bind-input <line-edit-mode> (key-press 'end) end-of-line)
(bind-input <line-edit-mode> (key-press 'e '(ctrl)) end-of-line)
(bind-input <line-edit-mode> (key-press 'up) backward-history)
(bind-input <line-edit-mode> (key-press 'p '(alt)) backward-history)
(bind-input <line-edit-mode> (key-press 'down) forward-history)
(bind-input <line-edit-mode> (key-press 'n '(alt)) forward-history)
(bind-input <line-edit-mode> (key-press 'k '(ctrl)) kill-line)
(bind-input <line-edit-mode> (text-input) insert-text)