/
yahoo-jp.scm
1943 lines (1789 loc) · 68.8 KB
/
yahoo-jp.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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;; yahoo-jp.scm: yahoo-jp for uim.
;;;
;;; Copyright (c) 2008-2012 uim Project http://code.google.com/p/uim/
;;;
;;; All rights reserved.
;;;
;;; Redistribution and use in source and binary forms, with or without
;;; modification, are permitted provided that the following conditions
;;; are met:
;;; 1. Redistributions of source code must retain the above copyright
;;; notice, this list of conditions and the following disclaimer.
;;; 2. Redistributions in binary form must reproduce the above copyright
;;; notice, this list of conditions and the following disclaimer in the
;;; documentation and/or other materials provided with the distribution.
;;; 3. Neither the name of authors nor the names of its contributors
;;; may be used to endorse or promote products derived from this software
;;; without specific prior written permission.
;;;
;;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND
;;; ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
;;; IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
;;; ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE
;;; FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
;;; DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
;;; OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
;;; HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
;;; LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
;;; OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
;;; SUCH DAMAGE.
;;;;
(require-extension (srfi 1 2 6 23 34 48))
(require "ustr.scm")
(require "japanese.scm")
(require "http-client.scm")
(require "generic-predict.scm")
(require "util.scm")
(require-custom "generic-key-custom.scm")
(require-custom "yahoo-jp-custom.scm")
(require-custom "yahoo-jp-key-custom.scm")
(require-dynlib "expat")
;;; implementations
;;
;; canna emulating functions
;;
(define yahoo-jp-internal-context-rec-spec
(append
context-rec-spec
(list
(list 'yomi-seg '())
(list 'candidates '())
(list 'seg-cnts '())
(list 'prediction-word '())
(list 'prediction-candidates '())
(list 'prediction-appendix '())
(list 'prediction-nr '()))))
(define-record 'yahoo-jp-internal-context yahoo-jp-internal-context-rec-spec)
(define yahoo-jp-internal-context-new-internal yahoo-jp-internal-context-new)
(define (yahoo-jp-conversion str opts)
(define (fromconv str)
(iconv-convert "UTF-8" "EUC-JP" str))
(define (toconv str)
(iconv-convert "EUC-JP" "UTF-8" str))
(define (make-query appid)
(format "~aconversion?appid=~a&sentence=~a~a"
yahoo-jp-path
appid
(http:encode-uri-string (fromconv str))
opts))
(define (parse str)
(let ((parser (xml-parser-create "UTF-8"))
(path '())
(seg '())
(seg-txt "")
(candidate '())
(cand-queue '()))
(define (elem-start name atts)
(set! path (append path (list name))))
(define (elem-end name)
(cond ((equal? '("ResultSet" "Result" "SegmentList" "Segment" "CandidateList")
path)
(set! candidate (append candidate (list cand-queue)))
(set! cand-queue '()))
((equal? '("ResultSet" "Result" "SegmentList" "Segment")
path)
(set! seg (append seg (list seg-txt)))
(set! seg-txt "")))
(set! path (drop-right path 1)))
(define (chardata str)
(cond ((equal? '("ResultSet" "Result" "SegmentList" "Segment" "CandidateList" "Candidate")
path)
(set! cand-queue (append cand-queue (list (toconv str)))))
((equal? '("ResultSet" "Result" "SegmentList" "Segment" "SegmentText")
path)
(set! seg-txt (toconv str)))))
(xml-element-handler-set! parser elem-start elem-end)
(xml-characterdata-handler-set! parser chardata)
(xml-parse parser str 1)
(xml-parser-free parser)
(cons seg candidate)))
(let* ((appid (if (string=? yahoo-jp-appid "")
(begin (uim-notify-fatal (N_ "Please regist Api key from <a href='http://developer.yahoo.co.jp/'>developer network</a> and set value on advanced menu."))
#f)
yahoo-jp-appid))
(proxy (make-http-proxy-from-custom))
(ssl (and yahoo-jp-use-ssl?
(make-http-ssl (SSLv3-client-method) 443)))
(ret (and appid
(http:get yahoo-jp-server (make-query appid) 80 proxy ssl))))
(if (string? ret)
(parse ret)
(cons '() (list (list str))))))
(define (yahoo-jp-predict-memoize! yc str cand)
(let ((cache (yahoo-jp-context-prediction-cache yc)))
(yahoo-jp-context-set-prediction-cache!
yc
(append (if (<= yahoo-jp-prediction-cache-words
(length cache))
(cdr cache)
cache)
(list (cons str cand))))))
(define (yahoo-jp-predict yc str opts)
(let ((ret (assoc str (yahoo-jp-context-prediction-cache yc))))
(if ret
(cdr ret)
(let ((cand (yahoo-jp-predict-from-server str opts)))
(if (not (null? (car cand)))
(yahoo-jp-predict-memoize! yc str cand))
cand))))
(define (yahoo-jp-predict-from-server str opts)
(cadr (yahoo-jp-conversion str (string-append "&mode=predictive" opts))))
(define (yahoo-jp-conversion-make-resize-query yomi-seg)
(let ((len (length yomi-seg)))
(apply string-append (map (lambda (idx)
(if (= (+ idx 1) len)
(list-ref yomi-seg idx)
(string-append (list-ref yomi-seg idx) " ")))
(iota len)))))
(define (yahoo-jp-conversion-resize yomi-seg)
(yahoo-jp-conversion
(yahoo-jp-conversion-make-resize-query yomi-seg) ""))
(define (yahoo-jp-lib-init)
#t)
(define (yahoo-jp-lib-alloc-context)
(yahoo-jp-internal-context-new-internal))
(define (yahoo-jp-lib-get-nth-candidate yc seg nth)
(let* ((yx-ctx (yahoo-jp-context-yx-ctx yc))
(cand (yahoo-jp-internal-context-candidates yx-ctx)))
(list-ref (list-ref cand seg) nth)))
(define (yahoo-jp-lib-release-context yc)
#t)
(define (yahoo-jp-lib-get-unconv-candidate yc seg-idx)
(let* ((yx-ctx (yahoo-jp-context-yx-ctx yc))
(cand (yahoo-jp-internal-context-candidates yx-ctx)))
;; XXX
(car (take-right (list-ref cand seg-idx) 1))))
(define (yahoo-jp-lib-get-nr-segments yc)
(let* ((yx-ctx (yahoo-jp-context-yx-ctx yc))
(cand (yahoo-jp-internal-context-candidates yx-ctx)))
(length cand)))
(define (yahoo-jp-lib-get-nr-candidates yc seg)
(let* ((yx-ctx (yahoo-jp-context-yx-ctx yc))
(cand (yahoo-jp-internal-context-candidates yx-ctx)))
(length (list-ref cand seg))))
(define (yahoo-jp-next-yomi-seg yomi-seg seg cnt)
(let* ((kana-str (list-ref yomi-seg seg))
(kana-list (reverse (string-to-list kana-str))))
(cond ((and (< cnt 0) ;; shrink segment
(< 1 (length kana-list)))
(let* ((not-edited-head (if (< 0 seg)
(take yomi-seg seg)
'()))
(edited-head (list (apply string-append (drop-right kana-list (* -1 cnt)))))
(edited-tail (if (= (+ 1 seg) (length yomi-seg)) ;; end of segments
(take-right kana-list (* -1 cnt))
(let* ((next-char (car (take-right kana-list (* -1 cnt))))
(kana-next-str (list-ref yomi-seg (+ 1 seg))))
(list (string-append next-char kana-next-str)))))
(not-edited-tail (if (= (+ 1 seg) (length yomi-seg))
'()
(drop yomi-seg (+ seg 2)))))
(append not-edited-head edited-head edited-tail not-edited-tail)))
((and (< 0 cnt) ;; stretch segment
(< (+ seg 1) (length yomi-seg))
(< 0 (length (string-to-list (list-ref yomi-seg (+ seg 1))))))
(let* ((next-str (list-ref yomi-seg (+ seg 1)))
(next-kana-list (reverse (string-to-list next-str)))
(not-edited-head (if (< 0 seg)
(take yomi-seg seg)
'()))
(edited-head (list (apply string-append
(append kana-list
(take next-kana-list cnt)))))
(edited-tail (if (= 1 (length next-kana-list))
'()
(list (apply string-append (drop next-kana-list cnt)))))
(not-edited-tail (if (< (length yomi-seg) 2)
'()
(drop yomi-seg (+ 2 seg)))))
(append not-edited-head edited-head edited-tail not-edited-tail)))
(else
yomi-seg))))
(define (yahoo-jp-lib-resize-segment yc seg cnt)
(let* ((yx-ctx (yahoo-jp-context-yx-ctx yc))
(cand (yahoo-jp-internal-context-candidates yx-ctx))
(yomi-seg (yahoo-jp-internal-context-yomi-seg yx-ctx))
(next-yomi-seg (yahoo-jp-next-yomi-seg yomi-seg seg cnt))
(replace-yomi-seg-and-next-cand (yahoo-jp-conversion-resize next-yomi-seg))
(replace-yomi-seg (car replace-yomi-seg-and-next-cand))
(next-cand (cdr replace-yomi-seg-and-next-cand)))
(if (and next-cand
(not (equal? next-cand cand)))
(begin
(yahoo-jp-internal-context-set-candidates! yx-ctx next-cand)
(yahoo-jp-internal-context-set-yomi-seg! yx-ctx replace-yomi-seg)))
#t))
(define (yahoo-jp-lib-begin-conversion yc str)
(let* ((yomi-seg-and-cand (yahoo-jp-conversion str ""))
(yomi-seg (car yomi-seg-and-cand))
(cand (cdr yomi-seg-and-cand))
(yx-ctx (yahoo-jp-context-yx-ctx yc)))
(yahoo-jp-internal-context-set-yomi-seg! yx-ctx yomi-seg)
(yahoo-jp-internal-context-set-candidates! yx-ctx cand)
(length cand)))
(define (yahoo-jp-lib-commit-segments yc delta)
#t)
(define (yahoo-jp-lib-reset-conversion yc)
#f)
(define (yahoo-jp-lib-set-prediction-src-string yc str)
(cond ((eq? yahoo-jp-prediction-type 'www)
(let ((yx-ctx (yahoo-jp-context-yx-ctx yc))
(cands (yahoo-jp-predict yc str "")))
(yahoo-jp-internal-context-set-prediction-candidates! yx-ctx cands)
(yahoo-jp-internal-context-set-prediction-nr! yx-ctx (length cands))))
((eq? yahoo-jp-prediction-type 'uim)
(let* ((ret (predict-meta-search
(yahoo-jp-context-prediction-ctx yc)
str))
(yx-ctx (yahoo-jp-context-yx-ctx yc))
(word (predict-meta-word? ret))
(cands (predict-meta-candidates? ret))
(appendix (predict-meta-appendix? ret)))
(yahoo-jp-internal-context-set-prediction-word! yx-ctx word)
(yahoo-jp-internal-context-set-prediction-candidates! yx-ctx cands)
(yahoo-jp-internal-context-set-prediction-appendix! yx-ctx appendix)
(yahoo-jp-internal-context-set-prediction-nr! yx-ctx (length cands)))))
#f)
(define (yahoo-jp-lib-get-nr-predictions yc)
(let ((yx-ctx (yahoo-jp-context-yx-ctx yc)))
(yahoo-jp-internal-context-prediction-nr yx-ctx)))
(define (yahoo-jp-lib-get-nth-word yc nth)
(let* ((yx-ctx (yahoo-jp-context-yx-ctx yc))
(word (yahoo-jp-internal-context-prediction-word yx-ctx)))
(list-ref word nth)))
(define (yahoo-jp-lib-get-nth-prediction yc nth)
(let* ((yx-ctx (yahoo-jp-context-yx-ctx yc))
(cands (yahoo-jp-internal-context-prediction-candidates yx-ctx)))
(list-ref cands nth)))
(define (yahoo-jp-lib-get-nth-appendix yc nth)
(let* ((yx-ctx (yahoo-jp-context-yx-ctx yc))
(appendix (yahoo-jp-internal-context-prediction-appendix yx-ctx)))
(list-ref appendix nth)))
(define (yahoo-jp-lib-commit-nth-prediction yc nth)
(if (eq? yahoo-jp-prediction-type 'uim)
(let ((yx-ctx (yahoo-jp-context-yx-ctx yc)))
(predict-meta-commit
(yahoo-jp-context-prediction-ctx yc)
(yahoo-jp-lib-get-nth-word yc nth)
(yahoo-jp-lib-get-nth-prediction yc nth)
(yahoo-jp-lib-get-nth-appendix yc nth))))
#f)
(define yahoo-jp-init-lib-ok? #f)
(define yahoo-jp-type-direct ja-type-direct)
(define yahoo-jp-type-hiragana ja-type-hiragana)
(define yahoo-jp-type-katakana ja-type-katakana)
(define yahoo-jp-type-halfkana ja-type-halfkana)
(define yahoo-jp-type-halfwidth-alnum ja-type-halfwidth-alnum)
(define yahoo-jp-type-fullwidth-alnum ja-type-fullwidth-alnum)
(define yahoo-jp-input-rule-roma 0)
(define yahoo-jp-input-rule-kana 1)
(define yahoo-jp-input-rule-azik 2)
(define yahoo-jp-input-rule-act 3)
(define yahoo-jp-input-rule-kzik 4)
(define yahoo-jp-candidate-type-katakana -2)
(define yahoo-jp-candidate-type-hiragana -3)
(define yahoo-jp-candidate-type-halfkana -4)
(define yahoo-jp-candidate-type-halfwidth-alnum -5)
(define yahoo-jp-candidate-type-fullwidth-alnum -6)
(define yahoo-jp-candidate-type-upper-halfwidth-alnum -7)
(define yahoo-jp-candidate-type-upper-fullwidth-alnum -8)
;; I don't think the key needs to be customizable.
(define-key yahoo-jp-space-key? '(" "))
(define yahoo-jp-prepare-input-rule-activation
(lambda (yc)
(cond
((yahoo-jp-context-state yc)
(yahoo-jp-do-commit yc))
((yahoo-jp-context-transposing yc)
(im-commit yc (yahoo-jp-transposing-text yc)))
((and
(yahoo-jp-context-on yc)
(yahoo-jp-has-preedit? yc))
(im-commit
yc (yahoo-jp-make-whole-string yc #t (yahoo-jp-context-kana-mode yc)))))
(yahoo-jp-flush yc)
(yahoo-jp-update-preedit yc)))
(define yahoo-jp-prepare-input-mode-activation
(lambda (yc new-mode)
(let ((old-kana (yahoo-jp-context-kana-mode yc)))
(cond
((yahoo-jp-context-state yc)
(yahoo-jp-do-commit yc))
((yahoo-jp-context-transposing yc)
(im-commit yc (yahoo-jp-transposing-text yc))
(yahoo-jp-flush yc))
((and
(yahoo-jp-context-on yc)
(yahoo-jp-has-preedit? yc)
(not (= old-kana new-mode)))
(im-commit
yc (yahoo-jp-make-whole-string yc #t (yahoo-jp-context-kana-mode yc)))
(yahoo-jp-flush yc)))
(yahoo-jp-update-preedit yc))))
(register-action 'action_yahoo-jp_hiragana
(lambda (yc) ;; indication handler
'(ja_hiragana
"あ"
"ひらがな"
"ひらがな入力モード"))
(lambda (yc) ;; activity predicate
(and (yahoo-jp-context-on yc)
(not (yahoo-jp-context-alnum yc))
(= (yahoo-jp-context-kana-mode yc)
yahoo-jp-type-hiragana)))
(lambda (yc) ;; action handler
(yahoo-jp-prepare-input-mode-activation yc yahoo-jp-type-hiragana)
(yahoo-jp-context-set-on! yc #t)
(yahoo-jp-context-set-alnum! yc #f)
(yahoo-jp-context-change-kana-mode! yc yahoo-jp-type-hiragana)))
(register-action 'action_yahoo-jp_katakana
(lambda (yc)
'(ja_katakana
"ア"
"カタカナ"
"カタカナ入力モード"))
(lambda (yc)
(and (yahoo-jp-context-on yc)
(not (yahoo-jp-context-alnum yc))
(= (yahoo-jp-context-kana-mode yc)
yahoo-jp-type-katakana)))
(lambda (yc)
(yahoo-jp-prepare-input-mode-activation yc yahoo-jp-type-katakana)
(yahoo-jp-context-set-on! yc #t)
(yahoo-jp-context-set-alnum! yc #f)
(yahoo-jp-context-change-kana-mode! yc yahoo-jp-type-katakana)))
(register-action 'action_yahoo-jp_halfkana
(lambda (yc)
'(ja_halfkana
"ア"
"半角カタカナ"
"半角カタカナ入力モード"))
(lambda (yc)
(and (yahoo-jp-context-on yc)
(not (yahoo-jp-context-alnum yc))
(= (yahoo-jp-context-kana-mode yc) yahoo-jp-type-halfkana)))
(lambda (yc)
(yahoo-jp-prepare-input-mode-activation yc yahoo-jp-type-halfkana)
(yahoo-jp-context-set-on! yc #t)
(yahoo-jp-context-set-alnum! yc #f)
(yahoo-jp-context-change-kana-mode! yc yahoo-jp-type-halfkana)))
(register-action 'action_yahoo-jp_halfwidth_alnum
(lambda (yc) ;; indication handler
'(ja_halfwidth_alnum
"a"
"半角英数"
"半角英数入力モード"))
(lambda (yc) ;; activity predicate
(and (yahoo-jp-context-on yc)
(yahoo-jp-context-alnum yc)
(= (yahoo-jp-context-alnum-type yc)
yahoo-jp-type-halfwidth-alnum)))
(lambda (yc) ;; action handler
(yahoo-jp-prepare-input-mode-activation
yc (yahoo-jp-context-kana-mode yc))
(yahoo-jp-context-set-on! yc #t)
(yahoo-jp-context-set-alnum! yc #t)
(yahoo-jp-context-set-alnum-type!
yc yahoo-jp-type-halfwidth-alnum)))
(register-action 'action_yahoo-jp_direct
(lambda (yc)
'(ja_direct
"-"
"直接入力"
"直接(無変換)入力モード"))
(lambda (yc)
(not (yahoo-jp-context-on yc)))
(lambda (yc)
(yahoo-jp-prepare-input-mode-activation yc yahoo-jp-type-direct)
(yahoo-jp-context-set-on! yc #f)))
(register-action 'action_yahoo-jp_fullwidth_alnum
(lambda (yc)
'(ja_fullwidth_alnum
"A"
"全角英数"
"全角英数入力モード"))
(lambda (yc)
(and (yahoo-jp-context-on yc)
(yahoo-jp-context-alnum yc)
(= (yahoo-jp-context-alnum-type yc)
yahoo-jp-type-fullwidth-alnum)))
(lambda (yc)
(yahoo-jp-prepare-input-mode-activation
yc (yahoo-jp-context-kana-mode yc))
(yahoo-jp-context-set-on! yc #t)
(yahoo-jp-context-set-alnum! yc #t)
(yahoo-jp-context-set-alnum-type!
yc yahoo-jp-type-fullwidth-alnum)))
(register-action 'action_yahoo-jp_roma
(lambda (yc)
'(ja_romaji
"R"
"ローマ字"
"ローマ字入力モード"))
(lambda (yc)
(= (yahoo-jp-context-input-rule yc)
yahoo-jp-input-rule-roma))
(lambda (yc)
(yahoo-jp-prepare-input-rule-activation yc)
(rk-context-set-rule! (yahoo-jp-context-rkc yc)
ja-rk-rule)
(yahoo-jp-context-set-input-rule! yc yahoo-jp-input-rule-roma)))
(register-action 'action_yahoo-jp_kana
(lambda (yc)
'(ja_kana
"か"
"かな"
"かな入力モード"))
(lambda (yc)
(= (yahoo-jp-context-input-rule yc)
yahoo-jp-input-rule-kana))
(lambda (yc)
(yahoo-jp-prepare-input-rule-activation yc)
(require "japanese-kana.scm")
(yahoo-jp-context-set-input-rule! yc yahoo-jp-input-rule-kana)
(yahoo-jp-context-change-kana-mode!
yc (yahoo-jp-context-kana-mode yc))
(yahoo-jp-context-set-alnum! yc #f)))
(register-action 'action_yahoo-jp_azik
(lambda (yc)
'(ja_azik
"Z"
"AZIK"
"AZIK拡張ローマ字入力モード"))
(lambda (yc)
(= (yahoo-jp-context-input-rule yc)
yahoo-jp-input-rule-azik))
(lambda (yc)
(yahoo-jp-prepare-input-rule-activation yc)
(require "japanese-azik.scm")
(rk-context-set-rule! (yahoo-jp-context-rkc yc)
ja-azik-rule)
(yahoo-jp-context-set-input-rule! yc yahoo-jp-input-rule-azik)))
(register-action 'action_yahoo-jp_kzik
(lambda (yc)
'(ja_kzik
"K"
"KZIK"
"KZIK拡張ローマ字入力モード"))
(lambda (yc)
(= (yahoo-jp-context-input-rule yc)
yahoo-jp-input-rule-kzik))
(lambda (yc)
(yahoo-jp-prepare-input-rule-activation yc)
(require "japanese-kzik.scm")
(rk-context-set-rule! (yahoo-jp-context-rkc yc)
ja-kzik-rule)
(yahoo-jp-context-set-input-rule! yc yahoo-jp-input-rule-kzik)))
(register-action 'action_yahoo-jp_act
(lambda (yc)
'(ja_act
"C"
"ACT"
"ACT拡張ローマ字入力モード"))
(lambda (yc)
(= (yahoo-jp-context-input-rule yc)
yahoo-jp-input-rule-act))
(lambda (yc)
(yahoo-jp-prepare-input-rule-activation yc)
(require "japanese-act.scm")
(rk-context-set-rule! (yahoo-jp-context-rkc yc)
ja-act-rule)
(yahoo-jp-context-set-input-rule! yc yahoo-jp-input-rule-act)))
;; Update widget definitions based on action configurations. The
;; procedure is needed for on-the-fly reconfiguration involving the
;; custom API
(define yahoo-jp-configure-widgets
(lambda ()
(register-widget 'widget_yahoo-jp_input_mode
(activity-indicator-new yahoo-jp-input-mode-actions)
(actions-new yahoo-jp-input-mode-actions))
(register-widget 'widget_yahoo-jp_kana_input_method
(activity-indicator-new yahoo-jp-kana-input-method-actions)
(actions-new yahoo-jp-kana-input-method-actions))
(context-list-replace-widgets! 'yahoo-jp yahoo-jp-widgets)))
(define yahoo-jp-context-rec-spec
(append
context-rec-spec
(list
(list 'on #f)
(list 'state #f)
(list 'transposing #f)
(list 'transposing-type 0)
(list 'predicting #f)
(list 'yx-ctx ()) ;; yahoo-jp-internal-context
(list 'preconv-ustr #f) ;; preedit strings
(list 'rkc ())
(list 'segments #f) ;; ustr of candidate indices
(list 'candidate-window #f)
(list 'candidate-op-count 0)
(list 'prediction-ctx '())
(list 'prediction-window #f)
(list 'prediction-index #f)
(list 'prediction-cache '())
(list 'kana-mode yahoo-jp-type-hiragana)
(list 'alnum #f)
(list 'alnum-type yahoo-jp-type-halfwidth-alnum)
(list 'commit-raw #t)
(list 'input-rule yahoo-jp-input-rule-roma)
(list 'raw-ustr #f))))
(define-record 'yahoo-jp-context yahoo-jp-context-rec-spec)
(define yahoo-jp-context-new-internal yahoo-jp-context-new)
(define (yahoo-jp-context-new id im)
(let ((yc (yahoo-jp-context-new-internal id im))
(rkc (rk-context-new ja-rk-rule #t #f)))
; (yahoo-jp-context-set-yx-ctx! yc (if yahoo-jp-init-lib-ok?
; (yahoo-jp-lib-alloc-context) ()))
(yahoo-jp-context-set-yx-ctx! yc (yahoo-jp-lib-alloc-context))
(yahoo-jp-context-set-widgets! yc yahoo-jp-widgets)
(yahoo-jp-context-set-rkc! yc rkc)
(yahoo-jp-context-set-preconv-ustr! yc (ustr-new '()))
(yahoo-jp-context-set-raw-ustr! yc (ustr-new '()))
(yahoo-jp-context-set-segments! yc (ustr-new '()))
(if (and yahoo-jp-use-prediction?
(eq? yahoo-jp-prediction-type 'uim))
(begin
(yahoo-jp-context-set-prediction-ctx! yc (predict-make-meta-search))
(predict-meta-open (yahoo-jp-context-prediction-ctx yc) "yahoo-jp")
(predict-meta-set-external-charset! (yahoo-jp-context-prediction-ctx yc) "EUC-JP")))
yc))
(define (yahoo-jp-commit-raw yc)
(im-commit-raw yc)
(yahoo-jp-context-set-commit-raw! yc #t))
(define (yahoo-jp-context-kana-toggle yc)
(let* ((kana (yahoo-jp-context-kana-mode yc))
(opposite-kana (ja-opposite-kana kana)))
(yahoo-jp-context-change-kana-mode! yc opposite-kana)))
(define yahoo-jp-context-alkana-toggle
(lambda (yc)
(let ((alnum-state (yahoo-jp-context-alnum yc)))
(yahoo-jp-context-set-alnum! yc (not alnum-state)))))
(define yahoo-jp-context-change-kana-mode!
(lambda (yc kana-mode)
(if (= (yahoo-jp-context-input-rule yc)
yahoo-jp-input-rule-kana)
(rk-context-set-rule!
(yahoo-jp-context-rkc yc)
(cond
((= kana-mode yahoo-jp-type-hiragana) ja-kana-hiragana-rule)
((= kana-mode yahoo-jp-type-katakana) ja-kana-katakana-rule)
((= kana-mode yahoo-jp-type-halfkana) ja-kana-halfkana-rule))))
(yahoo-jp-context-set-kana-mode! yc kana-mode)))
(define yahoo-jp-make-whole-string
(lambda (yc convert-pending-into-kana? kana)
(let* ((rkc (yahoo-jp-context-rkc yc))
(pending (rk-pending rkc))
(residual-kana (rk-peek-terminal-match rkc))
(rule (yahoo-jp-context-input-rule yc))
(preconv-str (yahoo-jp-context-preconv-ustr yc))
(extract-kana
(if (= rule yahoo-jp-input-rule-kana)
(lambda (entry) (car entry))
(lambda (entry) (list-ref entry kana)))))
(if (= rule yahoo-jp-input-rule-kana)
(ja-make-kana-str
(ja-make-kana-str-list
(string-to-list
(string-append
(string-append-map-ustr-former extract-kana preconv-str)
(if convert-pending-into-kana?
(if residual-kana
(if (list? (car residual-kana))
(string-append-map extract-kana residual-kana)
(extract-kana residual-kana))
pending)
pending)
(string-append-map-ustr-latter extract-kana preconv-str))))
kana)
(string-append
(string-append-map-ustr-former extract-kana preconv-str)
(if convert-pending-into-kana?
(if residual-kana
(if (list? (car residual-kana))
(string-append-map extract-kana residual-kana)
(extract-kana residual-kana))
"")
pending)
(string-append-map-ustr-latter extract-kana preconv-str))))))
(define yahoo-jp-make-raw-string
(lambda (raw-str-list wide? upper?)
(if (not (null? raw-str-list))
(if wide?
(string-append
(ja-string-list-to-wide-alphabet
(if upper?
(map charcode->string
(map ichar-upcase
(map string->charcode
(string-to-list (car raw-str-list)))))
(string-to-list (car raw-str-list))))
(yahoo-jp-make-raw-string (cdr raw-str-list) wide? upper?))
(string-append
(if upper?
(string-list-concat
(map charcode->string
(map ichar-upcase
(map string->charcode
(string-to-list (car raw-str-list))))))
(car raw-str-list))
(yahoo-jp-make-raw-string (cdr raw-str-list) wide? upper?)))
"")))
(define yahoo-jp-make-whole-raw-string
(lambda (yc wide? upper?)
(yahoo-jp-make-raw-string (yahoo-jp-get-raw-str-seq yc) wide? upper?)))
(define (yahoo-jp-init-handler id im arg)
(if (not yahoo-jp-init-lib-ok?)
(begin
(yahoo-jp-lib-init)
(set! yahoo-jp-init-lib-ok? #t)))
(yahoo-jp-context-new id im))
(define (yahoo-jp-release-handler yc)
(if yc
(yahoo-jp-lib-release-context yc)))
(define (yahoo-jp-flush yc)
(rk-flush (yahoo-jp-context-rkc yc))
(ustr-clear! (yahoo-jp-context-preconv-ustr yc))
(ustr-clear! (yahoo-jp-context-raw-ustr yc))
(ustr-clear! (yahoo-jp-context-segments yc))
(yahoo-jp-context-set-transposing! yc #f)
(yahoo-jp-context-set-state! yc #f)
(if (or
(yahoo-jp-context-candidate-window yc)
(yahoo-jp-context-prediction-window yc))
(im-deactivate-candidate-selector yc))
(yahoo-jp-context-set-candidate-window! yc #f)
(yahoo-jp-context-set-prediction-window! yc #f)
(yahoo-jp-context-set-candidate-op-count! yc 0))
(define (yahoo-jp-begin-input yc key key-state)
(if (cond
((yahoo-jp-on-key? key key-state)
#t)
((and
yahoo-jp-use-mode-transition-keys-in-off-mode?
(cond
((yahoo-jp-hiragana-key? key key-state)
(yahoo-jp-context-set-kana-mode! yc yahoo-jp-type-hiragana)
(yahoo-jp-context-set-alnum! yc #f)
#t)
((yahoo-jp-katakana-key? key key-state)
(yahoo-jp-context-set-kana-mode! yc yahoo-jp-type-katakana)
(yahoo-jp-context-set-alnum! yc #f)
#t)
((yahoo-jp-halfkana-key? key key-state)
(yahoo-jp-context-set-kana-mode! yc yahoo-jp-type-halfkana)
(yahoo-jp-context-set-alnum! yc #f)
#t)
((yahoo-jp-halfwidth-alnum-key? key key-state)
(yahoo-jp-context-set-alnum-type! yc yahoo-jp-type-halfwidth-alnum)
(yahoo-jp-context-set-alnum! yc #t)
#t)
((yahoo-jp-halfwidth-alnum-key? key key-state)
(yahoo-jp-context-set-alnum-type! yc yahoo-jp-type-fullwidth-alnum)
(yahoo-jp-context-set-alnum! yc #t)
#t)
((yahoo-jp-kana-toggle-key? key key-state)
(yahoo-jp-context-kana-toggle yc)
(yahoo-jp-context-set-alnum! yc #f)
#t)
((yahoo-jp-alkana-toggle-key? key key-state)
(yahoo-jp-context-alkana-toggle yc)
#t)
(else
#f))))
(else
#f))
(begin
(yahoo-jp-context-set-on! yc #t)
(rk-flush (yahoo-jp-context-rkc yc))
(yahoo-jp-context-set-state! yc #f)
#t)
#f))
(define (yahoo-jp-update-preedit yc)
(if (not (yahoo-jp-context-commit-raw yc))
(let ((segments (if (yahoo-jp-context-on yc)
(if (yahoo-jp-context-transposing yc)
(yahoo-jp-context-transposing-state-preedit yc)
(if (yahoo-jp-context-state yc)
(yahoo-jp-compose-state-preedit yc)
(if (yahoo-jp-context-predicting yc)
(yahoo-jp-predicting-state-preedit yc)
(yahoo-jp-input-state-preedit yc))))
())))
(context-update-preedit yc segments))
(yahoo-jp-context-set-commit-raw! yc #f)))
(define (yahoo-jp-begin-conv yc)
(let ((yx-ctx (yahoo-jp-context-yx-ctx yc))
(preconv-str (yahoo-jp-make-whole-string yc #t yahoo-jp-type-hiragana)))
(if (and yx-ctx
(> (string-length preconv-str) 0))
(let ((num (yahoo-jp-lib-begin-conversion yc preconv-str)))
(if num
(begin
(ustr-set-latter-seq!
(yahoo-jp-context-segments yc)
(make-list num 0))
(yahoo-jp-context-set-state! yc #t)
;; Don't perform rk-flush here. The rkc must be restored when
;; yahoo-jp-cancel-conv invoked -- YamaKen 2004-10-25
))))))
(define yahoo-jp-cancel-conv
(lambda (yc)
(yahoo-jp-reset-candidate-window yc)
(yahoo-jp-context-set-state! yc #f)
(ustr-clear! (yahoo-jp-context-segments yc))
(yahoo-jp-lib-reset-conversion yc)))
(define (yahoo-jp-proc-input-state-no-preedit yc key key-state)
(let
((rkc (yahoo-jp-context-rkc yc))
(direct (ja-direct (charcode->string key)))
(rule (yahoo-jp-context-input-rule yc)))
(cond
((and yahoo-jp-use-with-vi?
(yahoo-jp-vi-escape-key? key key-state))
(yahoo-jp-flush yc)
(yahoo-jp-context-set-on! yc #f)
(yahoo-jp-commit-raw yc))
((yahoo-jp-off-key? key key-state)
(yahoo-jp-flush yc)
(yahoo-jp-context-set-on! yc #f))
((yahoo-jp-backspace-key? key key-state)
(yahoo-jp-commit-raw yc))
((yahoo-jp-delete-key? key key-state)
(yahoo-jp-commit-raw yc))
((and
(yahoo-jp-hiragana-key? key key-state)
(not
(and
(= (yahoo-jp-context-kana-mode yc) yahoo-jp-type-hiragana)
(not (yahoo-jp-context-alnum yc)))))
(yahoo-jp-context-change-kana-mode! yc yahoo-jp-type-hiragana)
(yahoo-jp-context-set-alnum! yc #f))
((and
(yahoo-jp-katakana-key? key key-state)
(not
(and
(= (yahoo-jp-context-kana-mode yc) yahoo-jp-type-katakana)
(not (yahoo-jp-context-alnum yc)))))
(yahoo-jp-context-change-kana-mode! yc yahoo-jp-type-katakana)
(yahoo-jp-context-set-alnum! yc #f))
((and
(yahoo-jp-halfkana-key? key key-state)
(not
(and
(= (yahoo-jp-context-kana-mode yc) yahoo-jp-type-halfkana)
(not (yahoo-jp-context-alnum yc)))))
(yahoo-jp-context-change-kana-mode! yc yahoo-jp-type-halfkana)
(yahoo-jp-context-set-alnum! yc #f))
((and
(yahoo-jp-halfwidth-alnum-key? key key-state)
(not
(and
(= (yahoo-jp-context-alnum-type yc) yahoo-jp-type-halfwidth-alnum)
(yahoo-jp-context-alnum yc))))
(yahoo-jp-context-set-alnum-type! yc yahoo-jp-type-halfwidth-alnum)
(yahoo-jp-context-set-alnum! yc #t))
((and
(yahoo-jp-fullwidth-alnum-key? key key-state)
(not
(and
(= (yahoo-jp-context-alnum-type yc) yahoo-jp-type-fullwidth-alnum)
(yahoo-jp-context-alnum yc))))
(yahoo-jp-context-set-alnum-type! yc yahoo-jp-type-fullwidth-alnum)
(yahoo-jp-context-set-alnum! yc #t))
((and
(not (yahoo-jp-context-alnum yc))
(yahoo-jp-kana-toggle-key? key key-state))
(yahoo-jp-context-kana-toggle yc))
((yahoo-jp-alkana-toggle-key? key key-state)
(yahoo-jp-context-alkana-toggle yc))
;; modifiers (except shift) => ignore
((and (modifier-key-mask key-state)
(not (shift-key-mask key-state)))
(yahoo-jp-commit-raw yc))
;; direct key => commit
(direct
(im-commit yc direct))
;; space key
((yahoo-jp-space-key? key key-state)
(if (yahoo-jp-context-alnum yc)
(im-commit yc (list-ref
ja-alnum-space
(- (yahoo-jp-context-alnum-type yc)
yahoo-jp-type-halfwidth-alnum)))
(im-commit yc (list-ref ja-space (yahoo-jp-context-kana-mode yc)))))
((symbol? key)
(yahoo-jp-commit-raw yc))
(else
(if (yahoo-jp-context-alnum yc)
(let ((key-str (charcode->string key)))
(ustr-insert-elem! (yahoo-jp-context-preconv-ustr yc)
(if (= (yahoo-jp-context-alnum-type yc)
yahoo-jp-type-halfwidth-alnum)
(list key-str key-str key-str)
(list (ja-wide key-str) (ja-wide key-str)
(ja-wide key-str))))
(ustr-insert-elem! (yahoo-jp-context-raw-ustr yc) key-str))
(let* ((key-str (charcode->string
(if (= rule yahoo-jp-input-rule-kana)
key
(ichar-downcase key))))
(res (rk-push-key! rkc key-str)))
(if res
(begin
(if (list? (car res))
(ustr-insert-seq! (yahoo-jp-context-preconv-ustr yc) res)
(ustr-insert-elem! (yahoo-jp-context-preconv-ustr yc) res))
(ustr-insert-elem! (yahoo-jp-context-raw-ustr yc) key-str))
(if (null? (rk-context-seq rkc))
(yahoo-jp-commit-raw yc)))))))))
(define (yahoo-jp-has-preedit? yc)
(or (not (ustr-empty? (yahoo-jp-context-preconv-ustr yc)))
(> (string-length (rk-pending (yahoo-jp-context-rkc yc))) 0)))
(define yahoo-jp-rotate-transposing-alnum-type
(lambda (cur-type state)
(cond
((and
(= cur-type yahoo-jp-type-halfwidth-alnum)
(= state yahoo-jp-type-halfwidth-alnum))
yahoo-jp-candidate-type-upper-halfwidth-alnum)
((and
(= cur-type yahoo-jp-type-fullwidth-alnum)
(= state yahoo-jp-type-fullwidth-alnum))
yahoo-jp-candidate-type-upper-fullwidth-alnum)
(else
state))))
(define yahoo-jp-proc-transposing-state
(lambda (yc key key-state)
(let ((rotate-list '())
(state #f))
(if (yahoo-jp-transpose-as-fullwidth-alnum-key? key key-state)
(set! rotate-list (cons yahoo-jp-type-fullwidth-alnum rotate-list)))
(if (yahoo-jp-transpose-as-halfwidth-alnum-key? key key-state)
(set! rotate-list (cons yahoo-jp-type-halfwidth-alnum rotate-list)))
(if (yahoo-jp-transpose-as-halfkana-key? key key-state)
(set! rotate-list (cons yahoo-jp-type-halfkana rotate-list)))
(if (yahoo-jp-transpose-as-katakana-key? key key-state)
(set! rotate-list (cons yahoo-jp-type-katakana rotate-list)))
(if (yahoo-jp-transpose-as-hiragana-key? key key-state)
(set! rotate-list (cons yahoo-jp-type-hiragana rotate-list)))
(if (yahoo-jp-context-transposing yc)
(let ((lst (member (yahoo-jp-context-transposing-type yc) rotate-list)))
(if (and lst
(not (null? (cdr lst))))
(set! state (car (cdr lst)))
(if (not (null? rotate-list))
(set! state (yahoo-jp-rotate-transposing-alnum-type
(yahoo-jp-context-transposing-type yc)
(car rotate-list))))))
(begin
(yahoo-jp-context-set-transposing! yc #t)
(set! state (car rotate-list))))
(cond
((and state
(or
(= state yahoo-jp-type-hiragana)
(= state yahoo-jp-type-katakana)
(= state yahoo-jp-type-halfkana)))
(yahoo-jp-context-set-transposing-type! yc state))
((and state
(or
(= state yahoo-jp-type-halfwidth-alnum)
(= state yahoo-jp-candidate-type-upper-halfwidth-alnum)
(= state yahoo-jp-type-fullwidth-alnum)
(= state yahoo-jp-candidate-type-upper-fullwidth-alnum)))
(if (not (= (yahoo-jp-context-input-rule yc) yahoo-jp-input-rule-kana))
(yahoo-jp-context-set-transposing-type! yc state)))
(else
(and
; commit
(if (yahoo-jp-commit-key? key key-state)
(begin
(im-commit yc (yahoo-jp-transposing-text yc))
(yahoo-jp-flush yc)
#f)
#t)
; begin-conv
(if (yahoo-jp-begin-conv-key? key key-state)
(begin
(yahoo-jp-context-set-transposing! yc #f)
(yahoo-jp-begin-conv yc)
#f)
#t)
; cancel
(if (or
(yahoo-jp-cancel-key? key key-state)
(yahoo-jp-backspace-key? key key-state))
(begin
(yahoo-jp-context-set-transposing! yc #f)
#f)
#t)
; ignore
(if (or
(yahoo-jp-prev-page-key? key key-state)
(yahoo-jp-next-page-key? key key-state)
(yahoo-jp-extend-segment-key? key key-state)
(yahoo-jp-shrink-segment-key? key key-state)
(yahoo-jp-next-segment-key? key key-state)
(yahoo-jp-beginning-of-preedit-key? key key-state)
(yahoo-jp-end-of-preedit-key? key key-state)
(yahoo-jp-next-candidate-key? key key-state)