-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.scm
executable file
·391 lines (350 loc) · 13.8 KB
/
main.scm
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
#!/usr/bin/env -S guix shell -D --file=guix.scm -- guile --no-auto-compile
!#
;; (add-to-load-path (dirname (current-filename)))
(use-modules ;; (rime main)
(ncurses curses)
(ncurses menu)
(rime)
(rime utils)
(ice-9 match)
(srfi srfi-1)
(srfi srfi-26)
(srfi srfi-34))
(define ~ (cut format #f <...>))
(define-syntax-rule (push! elt v) (set! v (cons elt v)))
(define get-preedit
(compose composition-preedit
context-composition
get-context))
(define (move-end session-id)
(set-caret-pos
session-id
(string-length
(get-preedit session-id))))
(define (move-beginning session-id)
(set-caret-pos session-id 0))
(define (move-right session-id)
(set-caret-pos session-id
(min (1+ (get-caret-pot session-id))
(string-length
(get-preedit session-id)))))
(define (move-left session-id)
(set-caret-pos session-id
(max 0 (- (get-caret-pot session-id) 1))))
(define (build-composition-predit-with-cursor composition)
(let* ((preedit (composition-preedit composition))
(leng (composition-length composition))
(cursor-post (composition-cursor-post composition))
(cursor (max (- (string-length preedit)
(- leng cursor-post))
0)))
(string-append
(string-take
preedit
cursor)
"|"
(string-drop
preedit
cursor))))
;; (define ncurses-to-x11-alist
;; `(;; (265 . #xffbe)
;; ;; #xffc9
;; ,@(map (lambda (a b)))))
(define* (r-process-key #:optional (session-id (create-session)) keycode (mask 0))
(push! keycode notifications)
(match keycode
(#\dc1
(clear stdscr)
(refresh stdscr)
(endwin)
(finalize)
(exit 0))
(#\esc
(process-key session-id 27;; #xff1b
mask))
((and (? char?) (? (cut char-set-contains? char-set:iso-control <>) ))
(let ((i (char->integer keycode)))
(process-key session-id (if (<= i 26)
(+ 96 i)
i)
(if (<= i 31)
(+ 4 mask)
mask))))
((? (cut eqv? <> (key-f 4)))
(process-key session-id #xffc1 mask))
((? (cut member <> (list KEY_ENTER #\cr #\nl)))
(process-key session-id #xff0d mask))
((? (cut eqv? KEY_LEFT <>))
(process-key session-id #xff51 mask))
((? (cut eqv? KEY_NPAGE <>))
(process-key session-id #xff9b mask))
((? (cut eqv? KEY_RIGHT <>))
(process-key session-id #xff53 mask))
((? (cut eqv? KEY_UP <>))
(process-key session-id #xff52 mask))
((? (cut eqv? KEY_BACKSPACE <>))
(process-key session-id #xff08 mask))
((and (? number?) (? (lambda (a) (and (<= 200 a) (>= 237)))))
(process-key session-id keycode mask))
(_ (process-key session-id (if (number? keycode)
keycode
(char->integer keycode))
mask))))
(define (draw-candidates win candidates highlighted-index)
(let ((v 0))
(map (lambda (a)
(when (= v highlighted-index)
(attr-on! win (color-pair 1)))
(set! v (1+ v))
(addstr win (string-append (number->string v) ". "
(candidate-text a)
(let ((comment (candidate-comment a)))
(if (string-null? comment)
""
(string-append "(" comment ")")))
" "))
(when (= v (1+ highlighted-index))
(attr-off! win (color-pair 1))))
candidates)))
(define* (draw-info session-id #:optional win)
(let* ((s (get-status session-id))
(commit (get-commit session-id))
(context (get-context session-id))
(comp (context-composition context))
(menu (context-menu context)))
(clear win)
(box win (acs-vline) (acs-hline))
(move win 1 1)
(addstr win (~ " is-composing: ~S" (status-is-composing s)))
(addstr win (~ " is-ascii-mode: ~S" (status-is-ascii-mode s)))
(addstr win (~ " is-ascii-punct: ~S" (status-is-ascii-punct s)))
(addstr win (~ " is-full-shape: ~S" (status-is-full-shape s)))
(addstr win (~ " is-simplified: ~S" (status-is-simplified s)))
(addstr win (~ " is-disabled: ~S" (status-is-disabled s)))
(addstr win (~ " is-traditional: ~S" (status-is-traditional s)))
(move win 2 1)
(addstr win (~ " commit: ~S" (commit-text commit)))
(addstr win " candidates: ")
(draw-candidates win (menu-candidates menu)(menu-highlighted-candidate-index menu))
(move win 3 1)
(addstr win (~ " get-current-schema: ~S" (get-current-schema session-id)))
(addstr win (~ " schema-name: ~S" (status-schema-name s)))
(addstr win (~ " page-size: ~S" (menu-page-size menu)))
(addstr win (~ " page-no: ~S" (menu-page-no menu)))
(addstr win (~ " is-last-page: ~S" (menu-is-last-page menu)))
(addstr win (~ " menu-highlighted-candidate-index: ~S" (menu-highlighted-candidate-index menu)))
(addstr win (~ " num-candidates: ~S" (menu-num-candidates menu)))
(addstr win (~ " select-keys: ~S" (menu-select-keys menu)))
(move win 4 1)
(addstr win (~ " text-preview: ~S" (context-commit-text-preview context)))
(addstr win (~ " context-select-labels: ~S" (context-select-labels context)))
(move win 5 1)
(addstr win (~ " preedit: \"~a\"" (composition-preedit comp)))
(addstr win (~ " length: ~S" (composition-length comp)))
(addstr win (~ " raw-input: ~a" (get-input session-id)))
(addstr win (~ " raw-input-length: ~S" (string-length (get-input session-id))))
(move win 6 1)
(addstr win (~ " cursor-pos: ~S" (composition-cursor-post comp)))
(addstr win (~ " sel-start: ~S" (composition-sel-start comp)) )
(addstr win (~ " sel-end: ~S" (composition-sel-end comp)))
(free-context context)
(free-status s)
(free-commit commit)
(noutrefresh win)))
(define (draw-util session-id win)
(clear win)
(box win (acs-vline) (acs-hline))
(move win 1 1)
(addstr win (~ " shared-data-dir: ~S" (get-shared-data-dir)))
(addstr win (~ " user-data-dir: ~S\n" (get-user-data-dir)))
(addstr win (~ " sync-dir: ~S" (get-sync-dir)))
(addstr win (~ " staging-dir: ~S" (get-staging-dir)))
(addstr win (~ " user-id: ~S" (get-user-id)))
(addstr win (~ " user-data-sync-dir: ~S" (get-user-data-sync-dir)))
(addstr win (~ " prebuilt-data-dir: ~S\n" (get-prebuilt-data-dir)))
(noutrefresh win))
(define (draw-notifications win)
(clear win)
(move win 1 1)
(for-each (lambda (v)
(addstr win (~ " ~a " (getcury win) ))
(addch win (acs-vline))
(addstr win (~ "~S" v))
(move win (1+ (getcury win)) 1))
notifications)
(box win (acs-vline) (acs-hline))
(noutrefresh win))
(define (draw-main session-id win)
(let* ((context (get-context session-id))
(comp (context-composition context))
(menu (context-menu context)))
(clear win)
(move win 1 1)
(box win (acs-vline) (acs-hline))
(addch win (acs-vline))
(draw-candidates
win
(menu-candidates menu)
(menu-highlighted-candidate-index menu))
(unless (null? (menu-candidates menu))
(addstr win (string-append
" "
(if (= 0 (menu-page-no menu)) " " "<")
(number->string (1+ (menu-page-no menu)))
(if (menu-is-last-page menu) " " ">"))))
(move win 2 2)
(addstr win (build-composition-predit-with-cursor comp))
;; (addstr win (composition-preedit comp))
(move win 3 1)
(addstr win (string-append " " (context-commit-text-preview context)))
(free-context context)
(noutrefresh win)))
(define notifications '())
(define (start-rime)
(let ((traits (make-traits
#:user-data-dir
(or (and=>
(getenv "XDG_CONFIG_HOME")
(cut string-append <> "/grime/"))
(string-append (getenv "HOME") "/.config/grime/"))
#:min-log-level 0)))
(display "set-notification-handler..." )
(when #f
(set-notification-handler (lambda v
(push! v notifications))))
(display " done\n")
(display "setup..." )(setup traits) (display " done\n")
(display "initialize...")(initialize traits) (display " done\n")
(display "deployer-initialize...") (deployer-initialize traits) (display " done\n"))
(when (start-maintenance #f)
;;(deploy-config-file "guile_rime.yaml" "config_version")
(display "join-maintenance-thread...") (join-maintenance-thread) (display " done\n")))
(start-rime)
(define stdscr (initscr))
(raw!)
(start-color!)
(init-pair! 1 COLOR_BLACK COLOR_WHITE)
(nonl!)
(intrflush! #f)
(curs-set 0)
(keypad! stdscr #t)
(noecho!)
(define ww (newwin 5 (- (cols) 2) 1 1))
(define winn (newwin 10 (- (cols) 2) 6 1))
(define utils-win (newwin 9 (- (cols) 4) (- (lines) 9) 1))
(define log-win (newwin (- (lines) 29) 120 20 1))
(define session-id (create-session))
(define (choose-schema item)
(let ((id (schema-list-item-id item))
(conf (user-config-open "user")))
(select-schema session-id id)
(config-set-string conf "var/previously_selected_schema" id)
(set! (option session-id "simplification") #t)
(config-close conf)))
(let* ((l (schema-list-list (get-schema-list)))
(my-menu (new-menu (let ((b 0))
(map (lambda (a)
(set! b (1+ b))
(new-item (string-append
(number->string b) ". "
(schema-list-item-name a))
(schema-list-item-id a))) l))))
(my-menu-win (newwin (round (/ (lines) 2)) (round (/ (cols) 2))
(round (/ (lines) 4)) (round (/ (cols) 4))))
(my-menu-subwin (derwin my-menu-win (length l)
50
(round (/ (- (getmaxy my-menu-win) (length l)) 2))
(round (/ (- (getmaxx my-menu-win) 50) 2))
;; (getmaxx my-menu-win)
;; (map (lambda (i) (round (/ i 2)))
;; )
;; (round (/ (lines) 8)) (list (round (/ (cols) 8)))
)))
(keypad! my-menu-win #t)
(addstr my-menu-win (~ "~S" (get-current-schema session-id)))
(set-menu-mark! my-menu "=> ")
(set-menu-win! my-menu my-menu-win)
(set-menu-sub! my-menu my-menu-subwin)
(box my-menu-win (acs-vline) (acs-hline))
(box my-menu-subwin (acs-vline) (acs-hline))
(post-menu my-menu)
(refresh my-menu-win)
(let loop ((c (getch my-menu-win)))
(cond
;; Move down the menu when down arrow is pressed and then loop.
((member c (list KEY_DOWN #\so))
(begin
(menu-driver my-menu REQ_DOWN_ITEM)
(loop (getch my-menu-win))))
;; Move up the menu when the up arrow is pressed and then loop.
((member c (list KEY_UP #\dle))
(begin
(menu-driver my-menu REQ_UP_ITEM)
(loop (getch my-menu-win))))
;; When enter is pressed, return the selection and quit.
((member c (list KEY_ENTER #\cr #\nl))
(begin
(unpost-menu my-menu)
(refresh my-menu-win)
(choose-schema (list-ref l (item-index (current-item my-menu))))))
((false-if-exception (char-numeric? c))
(choose-schema (list-ref l (- (string->number (~ "~a" c)) 1)))
(unpost-menu my-menu)
(refresh my-menu-win))
;; If 'Q' , 'q' or 'C-q' is pressed, quit. Otherwise, loop.
((member c (list #\Q #\q #\dc1))
(begin (clear stdscr)
(refresh stdscr)
(endwin)
(finalize)
(exit 0)))
(else
(loop (getch my-menu-win))))))
;; (box winn (acs-vline) (acs-hline))
;; (box ww (acs-vline) (acs-hline))
;; (box utils-win (acs-vline) (acs-hline))
;; (box log-win (acs-vline) (acs-hline))
;; (noutrefresh winn)
;; (noutrefresh ww)
;; (noutrefresh utils-win)
;; (noutrefresh log-win)
;; (doupdate)
;; (scrollok! ww #t)
(clearok! ww #t)
(clearok! winn #f)
(clearok! log-win #t)
(clearok! utils-win #t)
;(sleep 2)
(dynamic-wind
(const #t)
(lambda _ (while #t
(let* ((ch (getch stdscr))
(is-esc? (eqv? ch #\esc)))
;; (when (eqv? ch #\dc1)
;; (clear stdscr)
;; (refresh stdscr)
;; (endwin)
;; (finalize)
;; (exit 0))
(guard (c ((check-error? c)
(push! (format #f "~a:~a:~a: ~a: source is `~S'. Value is `~S'. ~a"
(check-error-file-name c)
(check-error-line c)
(check-error-column c)
(check-error-checker c)
(check-error-source c)
(check-error-value c)
(check-error-message c))
notifications)
;; (raise c)
))
(draw-notifications log-win)
(r-process-key session-id
(if is-esc? (getch stdscr) ch)
(if is-esc? 8 0))
(draw-main session-id ww)
(draw-info session-id winn)
(draw-util session-id utils-win))
(doupdate))))
(lambda _
(endwin)))