2
;;; Copyright (c) 2003-2008 uim Project http://code.google.com/p/uim/
4
;;; All rights reserved.
6
;;; Redistribution and use in source and binary forms, with or without
7
;;; modification, are permitted provided that the following conditions
9
;;; 1. Redistributions of source code must retain the above copyright
10
;;; notice, this list of conditions and the following disclaimer.
11
;;; 2. Redistributions in binary form must reproduce the above copyright
12
;;; notice, this list of conditions and the following disclaimer in the
13
;;; documentation and/or other materials provided with the distribution.
14
;;; 3. Neither the name of authors nor the names of its contributors
15
;;; may be used to endorse or promote products derived from this software
16
;;; without specific prior written permission.
18
;;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND
19
;;; ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20
;;; IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21
;;; ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE
22
;;; FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23
;;; DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24
;;; OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25
;;; HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26
;;; LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27
;;; OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31
(require-custom "generic-key-custom.scm")
32
(require-custom "look-custom.scm")
35
(define look-widgets '(widget_look_input_mode))
37
;; default activity for each widgets
38
(define default-widget_look_input_mode 'action_look_sleep)
40
;; actions of widget_look_input_mode
41
(define look-input-mode-actions
42
'(action_look_sleep action_look_direct action_look_look))
46
(register-action 'action_look_sleep
52
(N_ "Look Sleep Input Mode")))
54
(not (look-context-on? lc)))
56
(look-context-set-on! lc #f)))
58
(register-action 'action_look_direct
64
(N_ "Look Direct Input Mode")))
66
(and (look-context-on? lc)
67
(not (look-context-look? lc))))
69
(look-context-set-on! lc #t)
70
(look-context-set-look! lc #f)))
72
(register-action 'action_look_look
76
"e" ;; do you like nethack?
78
(N_ "Look Input Mode")))
80
(and (look-context-on? lc)
81
(look-context-look? lc)))
83
(look-context-set-on! lc #t)
84
(look-context-set-look! lc #t)))
86
;; Update widget definitions based on action configurations. The
87
;; procedure is needed for on-the-fly reconfiguration involving the
89
(define (look-configure-widgets)
90
(register-widget 'widget_look_input_mode
91
(activity-indicator-new look-input-mode-actions)
92
(actions-new look-input-mode-actions)))
94
(define look-context-rec-spec
101
(list 'candidates ())
103
(list 'prev ()) ; simple queue: ([string]prevword1 prevword2 ...)
104
(list 'dict #f) ; list ((([string]prevword1 prevword2 ...) . [alist]history) ...)
106
(define look-context-rec-spec look-context-rec-spec)
107
(define-record 'look-context look-context-rec-spec)
108
(define look-context-new-internal look-context-new)
110
;; XXX: fake R5RS functions
111
(define (look-internal:string->list s)
114
(reverse (string-to-list s))))
115
(define (look-internal:list->string l)
120
(define (look-internal:make-string n c)
121
(apply string-append (map (lambda (x) (symbol->string c)) (iota n))))
122
;; XXX: slow quick-sort
123
(define (look-internal:qsort! data proc)
127
(if (< (length data) 2)
130
(set! pivot (car data))
131
(for-each (lambda (x)
133
(set! left (cons x left ))
134
(set! right (cons x right))))
136
(append (look-internal:qsort! left proc) (cons pivot (look-internal:qsort! right proc)))))))
137
(define (look-to-lower-string str)
140
(if (ichar-upper-case? (string->charcode c))
141
(charcode->string (ichar-downcase (string->charcode c)))
143
(reverse (string-to-list str)))))
145
(define (look-history-sort li lessf)
147
(map car (look-internal:qsort!
149
(lambda (x y) (lessf (cdr x) (cdr y))))))
151
(define (look-history-eow? x)
153
(define (look-init-history seedf)
154
(list (cons #t (seedf))))
155
(define (look-make-eow stat)
157
(define (look-histroy-append str hist seedf eowf)
158
(let ((cs (look-internal:string->list str)))
160
(if (assq #t hist) ; eow?
162
(if (look-history-eow? x)
163
(look-make-eow (eowf (cdr x)))
166
(append (list (look-make-eow (seedf))) hist)))
167
((and (not (null? hist))
168
(assoc (car cs) hist))
170
(if (equal? (car cs) (car x))
173
(look-internal:list->string (cdr cs))
179
(append (list (cons (car cs)
181
(look-internal:list->string (cdr cs))
185
(define (look-history-search str hist)
186
(define (skip str hist)
187
(let ((cs (look-internal:string->list str)))
190
(let ((c (assoc (car cs) hist)))
192
(skip (look-internal:list->string (cdr cs)) (cdr c))
194
(define (connect-tree hist)
195
(let loop ((hist hist) (rest ""))
198
((find (lambda (x) (not (look-history-eow? x))) hist)
200
append (map (lambda (l)
201
(let ((li (loop (cdr l)
202
(string-append rest (look-internal:make-string 1 (car l))))))
206
(filter (lambda (x) (not (look-history-eow? x))) hist))))
208
(cons rest (cdar hist))))))
209
(connect-tree (filter (lambda (x) (not (look-history-eow? x)))
213
(define (look-history-stat-init)
215
(define (look-history-stat-inc x)
217
(define (look-history-stat-less x y)
220
;; XXX: non-atomic functions
221
(define (look-save-personal-dict lc)
222
(call-with-output-file look-personal-dict-filename
224
(im-clear-preedit lc)
228
(im-update-preedit lc)
229
(write (cons look-prepared-words
230
(look-context-dict lc))
232
(im-clear-preedit lc)
233
(im-update-preedit lc))))
235
(define (look-load-personal-dict lc)
236
(if (file-readable? look-personal-dict-filename)
237
(let ((dict (call-with-input-file look-personal-dict-filename
239
(im-clear-preedit lc)
243
(im-update-preedit lc)
249
(= (car dict) look-prepared-words))
250
(look-context-set-dict! lc (cdr dict)))))
251
(im-clear-preedit lc)
252
(im-update-preedit lc))
254
(define (look-learn lc)
255
(define (histroy-append hist)
256
(look-histroy-append (look-to-lower-string (look-context-left lc))
258
look-history-stat-init
259
look-history-stat-inc))
260
(cond ((= 0 look-prepared-words)
261
(let ((hist (if (not (look-context-dict lc))
262
(look-init-history look-history-stat-init)
263
(look-context-dict lc))))
264
(look-context-set-dict!
266
(histroy-append hist))))
267
((< (length (look-context-prev lc)) look-prepared-words)
270
(if (not (look-context-dict lc))
271
(look-context-set-dict!
273
(cons (look-context-prev lc)
274
(histroy-append (look-init-history look-history-stat-init))))
275
(if (assoc (look-context-prev lc)
276
(look-context-dict lc))
277
(look-context-set-dict!
280
(if (equal? (look-context-prev lc)
283
(histroy-append (cdr x)))
285
(look-context-dict lc)))
286
(look-context-set-dict!
288
(append (list (cons (look-context-prev lc)
289
(histroy-append (look-init-history look-history-stat-init))))
290
(look-context-dict lc)))))))
291
(if (< (length (look-context-prev lc)) look-prepared-words)
292
(look-context-set-prev! lc (append (look-context-prev lc)
293
(list (string->symbol (look-context-left lc)))))
294
(if (= 0 look-prepared-words)
296
(look-context-set-prev! lc (append (cdr (look-context-prev lc))
297
(list (string->symbol (look-context-left lc))))))))
299
(define (look-search-learned lc str)
300
(if (= 0 look-prepared-words)
301
(if (look-context-dict lc)
303
(look-history-search (look-to-lower-string str)
304
(look-context-dict lc))
305
look-history-stat-less)
307
(let ((res (if (look-context-dict lc)
308
(assoc (look-context-prev lc) (look-context-dict lc))
312
(look-history-search (look-to-lower-string (look-context-left lc))
314
look-history-stat-less)
317
(define look-context-on? look-context-on)
318
(define look-context-look? look-context-look)
320
(define (look-get-nth-candidate lc)
321
(if (< 0 (length (look-context-candidates lc)))
322
(nth (look-context-nth lc) (look-context-candidates lc))
325
(define (look-get-length-left lc)
326
(string-length (look-context-left lc)))
328
(define (look-append-left! lc str)
329
(look-context-set-left! lc (string-append (look-context-left lc) str)))
331
(define (look-remove-last-char-from-left! lc)
332
(let ((left (look-context-left lc)))
333
(if (< 0 (look-get-length-left lc))
334
(look-context-set-left! lc (apply string-append (reverse (cdr (string-to-list left)))))
335
(look-context-set-left! lc ""))))
337
(define (look-append-char-from-candidate-to-left! lc)
338
(let ((candidate (look-get-nth-candidate lc)))
339
(if (< 0 (string-length candidate))
340
(look-context-set-left! lc (string-append (look-context-left lc)
341
(car (reverse (string-to-list candidate))))))))
343
(define (look-append-from-candidate-to-left! lc)
344
(look-context-set-left! lc (string-append (look-context-left lc)
345
(look-get-nth-candidate lc)))
346
(look-context-set-candidates! lc '()))
348
(define (look-context-new . args)
349
(let ((lc (apply look-context-new-internal args)))
350
(look-context-set-widgets! lc look-widgets)
353
(define (look-context-clean lc)
354
(look-context-set-on! lc #f)
355
(look-context-set-look! lc #f)
356
(look-context-set-nth! lc 0)
357
(look-context-set-candidates! lc '())
358
(look-context-set-left! lc ""))
360
(define (look-context-flush lc)
362
(im-commit lc (look-context-left lc))
363
(look-context-set-look! lc #f)
364
(look-context-set-nth! lc 0)
365
(look-context-set-candidates! lc '())
366
(look-context-set-left! lc ""))
368
(define (look-push-back-mode lc lst)
371
(im-pushback-mode-list lc (caar lst))
372
(look-push-back-mode lc (cdr lst)))))
374
(define (look-init-handler id im arg)
375
(let ((lc (look-context-new id im)))
376
(look-load-personal-dict lc)
379
(define (look-release-handler lc)
382
(define (look-alphabetic-char? key state)
383
(and (or (not (modifier-key-mask state))
384
(shift-key-mask state))
385
(ichar-alphabetic? key)))
387
(define (look-next-candidate! lc)
388
(if (< (look-context-nth lc) (- (length (look-context-candidates lc)) 1))
389
(look-context-set-nth! lc (+ (look-context-nth lc) 1))))
391
(define (look-prev-candidate! lc)
392
(if (< 0 (look-context-nth lc))
393
(look-context-set-nth! lc (- (look-context-nth lc) 1))))
395
(define (look-look lc look-dict str)
396
(let* ((learned (look-search-learned lc str))
397
(looked (look-lib-look look-dict str)))
398
(look-context-set-dictlen! lc (length learned))
399
(append learned looked)))
401
(define (look-update lc)
402
(let ((str (look-context-left lc)))
403
(look-context-set-nth! lc 0)
404
(if (<= look-beginning-character-length (string-length str))
405
(look-context-set-candidates! lc (look-look lc look-dict str))
406
(look-context-set-candidates! lc '()))))
408
(define (look-format-candidates lc)
409
(let ((candidates (look-context-candidates lc)))
410
(if (or (= 0 (string-length (look-context-left lc)))
411
(<= (length candidates) (look-context-nth lc)))
413
(string-append look-fence-left
414
(nth (look-context-nth lc) candidates)
417
(define (look-format-candidates-nth lc)
418
(if (or (= 0 (string-length (look-context-left lc)))
419
(<= (length (look-context-candidates lc))
420
(look-context-nth lc)))
422
(let ((nth (if (< (look-context-nth lc)
423
(look-context-dictlen lc))
424
(+ 1 (look-context-nth lc))
426
(- (look-context-nth lc)
427
(look-context-dictlen lc)))))
428
(candidates (if (< (look-context-nth lc)
429
(look-context-dictlen lc))
430
(look-context-dictlen lc)
431
(- (length (look-context-candidates lc))
432
(look-context-dictlen lc)))))
436
(number->string candidates)
439
(define (look-update-preedit lc)
440
(im-clear-preedit lc)
443
(look-context-left lc))
446
(look-format-candidates lc))
447
(if (< (look-context-nth lc) (look-context-dictlen lc))
450
(look-format-candidates-nth lc))
453
(look-format-candidates-nth lc)))
454
(im-update-preedit lc))
456
(define (look-key-press-state-look lc key state)
457
(cond ((look-off-key? key state)
458
(look-context-clean lc)
459
(look-update-preedit lc))
460
((look-alphabetic-char? key state)
461
(look-append-left! lc (charcode->string key))
463
(look-update-preedit lc))
464
((look-completion-key? key state)
465
(look-append-from-candidate-to-left! lc)
466
(look-context-flush lc)
467
(look-update-preedit lc))
468
((and (look-next-char-key? key state)
469
(< 0 (look-get-length-left lc)))
470
(look-append-char-from-candidate-to-left! lc)
472
(look-update-preedit lc))
473
((look-prev-char-key? key state)
474
(cond ((<= (look-get-length-left lc) 0)
475
(look-context-flush lc)
476
;; or (look-context-clean lc)
479
(look-remove-last-char-from-left! lc)))
481
(look-update-preedit lc))
482
((look-next-candidate-key? key state)
483
(look-next-candidate! lc)
484
(look-update-preedit lc))
485
((look-prev-candidate-key? key state)
486
(look-prev-candidate! lc)
487
(look-update-preedit lc))
488
((look-save-dict-key? key state)
489
(look-save-personal-dict lc)
491
(look-context-flush lc)
492
(look-update-preedit lc))
493
((look-load-dict-key? key state)
494
(look-load-personal-dict lc)
496
(look-context-flush lc)
497
(look-update-preedit lc))
500
(look-context-flush lc)
501
(look-update-preedit lc))))
503
(define (look-key-press-state-direct lc key state)
504
(cond ((look-off-key? key state)
505
(look-context-clean lc)
506
(look-update-preedit lc))
507
((look-alphabetic-char? key state)
508
(look-context-set-left! lc (charcode->string key))
510
(look-update-preedit lc)
511
(look-context-set-look! lc #t))
512
((look-save-dict-key? key state)
513
(look-save-personal-dict lc)
515
((look-load-dict-key? key state)
516
(look-load-personal-dict lc)
519
(im-commit-raw lc))))
521
(define (look-key-press-state-sleep lc key state)
522
(cond ((look-on-key? key state)
523
(look-context-set-on! lc #t)
524
(look-context-set-look! lc #f))
526
(im-commit-raw lc))))
528
(define (look-key-press-handler lc key state)
529
(if (look-context-on? lc)
530
(if (look-context-look? lc)
531
(look-key-press-state-look lc key state)
532
(look-key-press-state-direct lc key state))
533
(look-key-press-state-sleep lc key state)))
535
(define (look-key-release-handler lc key state)
538
(define (look-reset-handler lc)
541
;;(define (look-mode-handler lc mode)
542
;; (create-context (look-context-id lc)
544
;; (car (nth mode im-list)))
547
(define (look-get-candidate-handler lc idx)
550
(define (look-set-candidate-index-handler lc idx)
553
(look-configure-widgets)
557
"*" ;; wildcard language. see i18n.scm
560
(N_ "Tiny predictive input method")
565
look-key-press-handler
566
look-key-release-handler
568
look-get-candidate-handler
569
look-set-candidate-index-handler
570
context-prop-activate-handler