-
Notifications
You must be signed in to change notification settings - Fork 0
/
egg-cnv.el
1275 lines (1138 loc) · 44.9 KB
/
egg-cnv.el
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
;;; egg-cnv.el --- Conversion Backend in Egg Input Method Architecture
;;;;;;;;;;;;;;;; About copyright notice ;;;;;;;;;;;;;;;;
;;;
;;; This code was originally written by NIIBE Yutaka in 1997 and 1998.
;;; It was plan to assign copyright to FSF and merged into GNU Emacs.
;;; Based on my own work, the feature of mixture of multiple languages
;;; support was added, the feature was written by an employee of PFU
;;; LIMITED. Because of that, it seems for me that a maintainer (at
;;; that time) added the copyright notice like:
;; Copyright (C) 1999,2000 PFU LIMITED
;;; But, I don't agree that this file is entirely copyrighted by PFU
;;; LIMITED. It's only for some parts, at maximum.
;;;
;;; I never assigned my code to PFU LIMITED.
;;;
;;; Although the copyright notice was wrong or not that accurate at
;;; least, everyone in the project(including the maintainer and PFU
;;; LIMITED) agreed to distribute the code under GPLv2+.
;;;
;;; Please don't do that again. Please agree and prepare your
;;; assignment to FSF when you develop something for Egg v4.
;;;
;;; -- gniibe 2014-10-31
;;;;;;;;;;;;;
;; Author: NIIBE Yutaka <gniibe@chroot.org>
;; KATAYAMA Yoshio <kate@pfu.co.jp>
;; Keywords: mule, multilingual, input method
;; This file is part of EGG.
;; EGG is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 2, or (at your option)
;; any later version.
;; EGG is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs; see the file COPYING. If not, write to the
;; Free Software Foundation, Inc.,
;; 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
;;; Commentary:
;;; Code:
(require 'egg-edep)
(defgroup egg-conv nil
"Conversion Backend Interface of Tamago-tsunagi."
:group 'egg)
(defcustom egg-conversion-wrap-select t
"*Candidate selection wraps around to first candidate, if non-NIL.
Otherwise stop at the last candidate."
:group 'egg-conv :type 'boolean)
(defcustom egg-conversion-auto-candidate-menu 0
"*Automatically enter the candidate selection mode at N times
next/previous-candidate, if positive number N."
:group 'egg-conv :type 'integer)
(defcustom egg-conversion-auto-candidate-menu-show-all nil
"*Enter show all candiate mode when automatic candidate selection
mode, if non-NIL."
:group 'egg-conv :type 'boolean)
(defcustom egg-conversion-sort-by-converted-string nil
"*Sort candidate list by converted string on candidate selection
mode, if non-NIL."
:group 'egg-conv :type 'boolean)
(defcustom egg-conversion-fence-invisible nil
"*Make fence marks invisible, if non-NIL."
:group 'egg-conv :type 'boolean)
(defcustom egg-conversion-fence-open "|"
"*String of conversion fence start mark. (should not be null string)"
:group 'egg-conv :type '(string :valid-regexp ".+"))
(defcustom egg-conversion-fence-close "|"
"*String of conversion fence end mark. (should not be null string)"
:group 'egg-conv :type '(string :valid-regexp ".+"))
(defcustom egg-conversion-face nil
"*Face (or alist of languages and faces) of text in conversion fences."
:group 'egg-conv
:type '(choice face
(repeat :tag "Language-Face alist"
(cons :tag "Language-Face"
(choice :tag "Language"
(const Japanese)
(const Chinese-GB)
(const Chinese-CNS)
(const Korean)
(const :tag "Default" t)
(symbol :tag "Other"))
face))))
(defcustom egg-conversion-major-separator " "
"*Major clause seperator"
:group 'egg-conv :type 'string)
(defcustom egg-conversion-minor-separator "-"
"*Minor clause seperator"
:group 'egg-conv :type 'string)
(defcustom egg-startup-file ".eggrc"
"*Egg startup file name."
:group 'egg-conv :type 'string)
(defcustom egg-startup-file-search-path '("~")
"*List of directories to search for egg-startup-file (default .eggrc)."
:group 'egg-conv :type '(repeat string))
(egg-add-message
'((nil
(no-rcfile "no egg-startup-file on %S")
(rcfile-error "error occured in egg-startup-file")
(candidate "candidates:")
(register-str "Chinese character:")
(register-yomi "word registration ``%s'' pronunciation:")
(registered "dictionary entry ``%s''(%s: %s) is registerd at %s"))
(Japanese
(no-rcfile "%S $B>e$K(B egg-startup-file $B$,$"$j$^$;$s(B")
(rcfile-error "egg-startup-file $B$G%(%i!<$,$"$j$^$7$?(B")
(candidate "$B8uJd(B:")
(register-str "$B4A;z(B:")
(register-yomi "$B<-=qEPO?!X(B%s$B!Y(B $BFI$_(B:")
(registered "$B<-=q9`L\!X(B%s$B!Y(B(%s: %s)$B$r(B %s $B$KEPO?$7$^$7$?(B"))
(Chinese-GB
(no-rcfile "$ATZ(B %S $AIOC;SP(B egg-startup-file")
(rcfile-error "$ATZ6AH!(B egg-startup-file $AJ1#,SP3v4m7"IzAK(B")
(candidate "$A:r29(B:")
(register-str "$A::WV(B:")
(register-yomi "$A4G5d5GB<!:(B%s$A!;(B $A6A7((B:")
(registered "$A4G5dOnD?!:(B%s$A!;(B(%s: %s)$ARQ1;5GB<5=(B %s $AVPAK(B"))
(Chinese-CNS
(no-rcfile "$(GGc(B %S $(GD8JtH4(B egg-startup-file")
(rcfile-error "$(GGc{tL=(B egg-startup-file $(GUk!"H4Exrc`uFmD'(B")
(register-str "$(GiGGs(B:")
(candidate "$(GT7fP(B:")
(register-yomi "$(Gy0L(`trg!Z(B%s$(G![(B $(G{tNN(B:")
(registered "$(Gy0L(bzFx!Z(B%s$(G![(B(%s: %s)$(GDX]7`trgL/(B %s $(GDcD'(B"))
(Korean
(no-rcfile "%S $(C?!(B egg-startup-file $(C@L(B $(C>x@>4O4Y(B")
(rcfile-error "egg-startup-file $(C?!(B $(C?!7/0!(B $(C9_;}G_@>4O4Y(B")
(candidate "$(CHD:8(B:")
(register-str "$(CGQ@Z(B:")
(register-yomi "$(C;g@|5n7O!:(B%s$(C!;(B $(C569}(B:")
(registered "$(C;g@|GW8q!:(B%s$(C!;(B(%s: %s)$(C@;(B %s$(C?!(B $(C5n7OG_@>4O4Y(B"))))
;;
;; <backend-alist> ::= ( ( <language> ( <stage>... )... )... )
;; <stage> ::= ( <backend> <backend-for-reconvert>... )
;; <backend-for-reconvert> ::= <backend>
;; <backend> ::= symbol
;;
(defvar egg-conversion-backend-alist nil)
(make-variable-buffer-local 'egg-conversion-backend-alist)
(put 'egg-conversion-backend-alist 'permanent-local t)
(defun egg-set-conversion-backend (backend-alist &optional force)
(let (pair lang backend-set)
(while backend-alist
(setq lang (caar backend-alist)
backend-set (cdar backend-alist)
backend-alist (cdr backend-alist)
pair (assq lang egg-conversion-backend-alist))
(cond
((null pair)
(setq egg-conversion-backend-alist
(cons (cons lang backend-set) egg-conversion-backend-alist)))
(force
(setcdr pair backend-set))))))
(defun egg-get-conversion-backend (language n use-default)
(let (backend)
(cond ((null n) (setq n 0))
((null (integerp n)) (setq n 1)))
(setq backend (nth (1+ n) (assq language egg-conversion-backend-alist)))
(if backend
(and backend (cons 0 (mapcar 'copy-sequence backend)))
(and use-default (cons 0 egg-default-conversion-backend)))))
(defsubst egg-default-conversion-backend-p (backend)
(eq (cdr backend) egg-default-conversion-backend))
(defsubst egg-get-current-backend (backend)
(car (nth (car backend) (cdr backend))))
(defsubst egg-get-reconvert-backend (backend &optional n)
(cond ((null n) (setq n 0))
((null (integerp n)) (setq n 1)))
(nth (1+ n) (nth (car backend) (cdr backend))))
(defmacro egg-bunsetsu-info () ''intangible)
(defsubst egg-get-bunsetsu-info (p &optional object)
(get-text-property p (egg-bunsetsu-info) object))
(defsubst egg-get-backend (p &optional object)
(get-text-property p 'egg-backend object))
(defsubst egg-get-language (p &optional object)
(get-text-property p 'egg-lang object))
(defsubst egg-get-bunsetsu-last (p &optional object)
(get-text-property p 'egg-bunsetsu-last object))
(defsubst egg-get-major-continue (p &optional object)
(get-text-property p 'egg-major-continue object))
(defsubst egg-get-char-size (p &optional object)
(get-text-property p 'egg-char-size object))
;; <bunsetsu-info> ::= ( <backend> . <backend-dependent-info> )
(defsubst egg-bunsetsu-create (backend info)
(cons backend info))
(defsubst egg-bunsetsu-get-backend (bunsetsu)
(car bunsetsu))
(defsubst egg-bunsetsu-set-backend (bunsetsu backend)
(setcar bunsetsu backend))
(defsubst egg-bunsetsu-get-info (bunsetsu)
(cdr bunsetsu))
(defsubst egg-bunsetsu-set-info (bunsetsu info)
(setcdr bunsetsu info))
(defun egg-conversion-fence-p ()
(and (egg-get-backend (point))
(get-text-property (point) 'read-only)))
(defvar egg-finalize-backend-list nil)
(defun egg-set-finalize-backend (func-list)
(mapcar (lambda (func)
(if (and func
(null (memq func egg-finalize-backend-list)))
(setq egg-finalize-backend-list
(cons func egg-finalize-backend-list))))
func-list))
(defmacro egg-define-backend-functions (list)
(cons 'progn
(mapcar
(lambda (def)
(let* ((func (car def))
(args (nth 1 def))
(backend (car args)))
(cond ((eq backend 'bunsetsu)
(setq backend `(egg-bunsetsu-get-backend ,backend)))
((eq backend 'bunsetsu-list)
(setq backend `(egg-bunsetsu-get-backend (car ,backend)))))
`(defun ,func ,args
(let ((func (get ,backend ',func)))
(and func
(funcall func ,@args))))))
list)))
(egg-define-backend-functions
((egg-start-conversion (backend source-string context))
(egg-get-bunsetsu-source (bunsetsu))
(egg-get-bunsetsu-converted (bunsetsu))
(egg-get-source-language (bunsetsu))
(egg-get-converted-language (bunsetsu))
(egg-major-bunsetsu-continue-p (bunsetsu))
(egg-list-candidates (bunsetsu-list prev-b next-b major))
(egg-decide-candidate (bunsetsu-list candidate-pos prev-b next-b))
(egg-special-candidate (bunsetsu-list prev-b next-b major type))
(egg-change-bunsetsu-length (bunsetsu-list prev-b next-b length major))
(egg-bunsetsu-combinable-p (bunsetsu next-b))
(egg-end-conversion (bunsetsu-list abort))
(egg-word-inspection (bunsetsu))
(egg-word-registration (backend source converted))))
(defun egg-finalize-backend ()
(run-hooks 'egg-finalize-backend-list))
(setplist 'egg-conversion-backend-noconv
'(egg-start-conversion egg-start-conversion-noconv
egg-get-bunsetsu-source egg-get-bunsetsu-source-noconv
egg-get-bunsetsu-converted egg-get-bunsetsu-converted-noconv
egg-get-source-language egg-get-source-language-noconv
egg-get-converted-language egg-get-converted-language-noconv
egg-end-conversion egg-end-conversion-noconv))
(defun egg-start-conversion-noconv (backend yomi-string context)
(let ((string (copy-sequence yomi-string))
(language (egg-get-language 0 yomi-string)))
(egg-remove-all-text-properties 0 (length string) string)
(list (egg-bunsetsu-create backend (vector string language)))))
(defun egg-get-bunsetsu-source-noconv (bunsetsu)
(aref (egg-bunsetsu-get-info bunsetsu) 0))
(defun egg-get-bunsetsu-converted-noconv (bunsetsu)
(aref (egg-bunsetsu-get-info bunsetsu) 0))
(defun egg-get-source-language-noconv (bunsetsu)
(aref (egg-bunsetsu-get-info bunsetsu) 1))
(defun egg-get-converted-language-noconv (bunsetsu)
(aref (egg-bunsetsu-get-info bunsetsu) 1))
(defun egg-end-conversion-noconv (bunsetsu-list abort)
nil)
(defconst egg-default-conversion-backend '((egg-conversion-backend-noconv)))
(defun egg-convert-region (start end &optional context nth-backend)
(interactive "r\ni\nP")
(let ((source (buffer-substring start end))
backend backend-source-list converted converted-list
lang len s success abort)
(if (>= start end)
;; nothing to do
nil
(delete-region start end)
(egg-setup-invisibility-spec)
(let ((inhibit-read-only t))
(its-define-select-keys egg-conversion-map)
(goto-char start)
(setq s (copy-sequence egg-conversion-fence-open)
len (length s)
start (+ start len)
end (+ end len))
(set-text-properties 0 len (list 'read-only t
'egg-start t
'egg-source source)
s)
(if context
(put-text-property 0 len 'egg-context context s))
(if egg-conversion-fence-invisible
(put-text-property 0 len 'invisible 'egg s))
(insert s)
(setq s (copy-sequence egg-conversion-fence-close)
len (length s))
(set-text-properties 0 len '(read-only t rear-nonsticky t egg-end t) s)
(if egg-conversion-fence-invisible
(put-text-property 0 len 'invisible 'egg s))
(insert s)
(goto-char start)
(insert source)
(goto-char start)
(setq source (copy-sequence source))
(egg-separate-languages source)
(setq backend-source-list (egg-assign-backend source nth-backend))
(while (and (null abort) backend-source-list)
(setq backend (car (car backend-source-list))
lang (nth 1 (car backend-source-list))
source (nth 2 (car backend-source-list))
backend-source-list (cdr backend-source-list))
(condition-case error
(progn
(setq converted (egg-start-conversion
(egg-get-current-backend backend)
source context))
(if (null converted)
(egg-error "no conversion result"))
(setq converted-list (nconc converted-list
(list (cons backend converted)))
context 'continued)
(or (egg-default-conversion-backend-p backend)
(setq success t)))
((egg-error quit)
(cond
((null (or success
(delq t (mapcar (lambda (s)
(egg-default-conversion-backend-p
(cdr (car s))))
backend-source-list))))
(message "egg %s backend: %s"
(if (cdr lang) lang (car lang))
(nth (if (eq (car error) 'quit) 0 1) error))
(ding)
(setq abort t))
((condition-case err
(y-or-n-p
(format "egg %s backend %s: continue? "
lang (nth (if (eq (car error) 'quit) 0 1) error)))
((error quit) nil))
(setq backend (egg-get-conversion-backend nil 0 t)
converted (egg-start-conversion
(egg-get-current-backend backend)
source context)
converted-list (nconc converted-list
(list (cons backend converted)))
context 'continued))
(t
(setq abort t))))))
(delete-region start end)
(while converted-list
(egg-insert-bunsetsu-list (caar converted-list) (cdar converted-list)
(or (null (cdr converted-list)) 'continue))
(setq converted-list (cdr converted-list)))
(goto-char start)
(cond (abort
(egg-abort-conversion))
((null success)
(egg-exit-conversion)))))))
(defun egg-separate-languages (str &optional last-lang)
(let (lang last-chinese
(len (length str)) i j l)
;; 1st pass -- mark undefined Chinese part
(if (or (eq last-lang 'Chinese-GB) (eq last-lang 'Chinese-CNS))
(setq last-chinese last-lang))
(setq i 0)
(while (< i len)
(setq j (egg-next-single-property-change i 'egg-lang str len))
(if (null (egg-get-language i str))
(progn
(setq c (egg-string-to-char-at str i)
cset (char-charset c))
(cond
((eq cset 'chinese-sisheng)
(egg-string-match-charset 'chinese-sisheng str i)
(setq l (match-end 0)
j (min j l)
lang 'Chinese))
((setq l (egg-chinese-syllable str i))
(setq j (+ i l)
lang 'Chinese))
((eq cset 'ascii)
(if (eq (string-match "[\0-\177\240-\377]+" str (1+ i)) (1+ i))
(setq j (match-end 0))
(setq j (1+ i)))
(if (and (< j len)
(eq (char-charset (egg-string-to-char-at str j))
'chinese-sisheng))
(setq j (max (1+ i) (- j 6))))
(setq lang nil))
((eq cset 'composition)
(setq j (+ i (egg-char-bytes c))
lang (egg-charset-to-language
(char-charset
(car (decompose-composite-char c 'list))))))
(t
(egg-string-match-charset cset str i)
(setq j (match-end 0)
lang (egg-charset-to-language cset))))
(if lang
(put-text-property i j 'egg-lang lang str))))
(setq i j))
;; 2nd pass -- set language property
(setq i 0)
(while (< i len)
(setq lang (egg-get-language i str))
(cond
((null lang)
(setq lang (or last-lang
(egg-next-part-lang str i))))
((equal lang 'Chinese)
(setq lang (or last-chinese
(egg-next-chinese-lang str i)))))
(setq last-lang lang)
(if (or (eq lang 'Chinese-GB) (eq lang 'Chinese-CNS))
(setq last-chinese lang))
(setq j i
i (egg-next-single-property-change i 'egg-lang str len))
(egg-remove-all-text-properties j i str)
(put-text-property j i 'egg-lang lang str))))
;;; Should think again the interface to language-info-alist
(defun egg-charset-to-language (charset)
(let ((list language-info-alist))
(while (and list
(null (memq charset (assq 'charset (car list)))))
(setq list (cdr list)))
(if list
(intern (car (car list))))))
(defun egg-next-part-lang (str pos)
(let ((lang (get-text-property
(egg-next-single-property-change pos 'egg-lang str (length str))
'egg-lang str)))
(if (eq lang 'Chinese)
(egg-next-chinese-lang str pos)
(or lang
its-current-language
egg-default-language))))
(defun egg-next-chinese-lang (str pos)
(let ((len (length str)) lang)
(while (and (< pos len) (null lang))
(setq pos (egg-next-single-property-change pos 'egg-lang str len)
lang (egg-get-language pos str))
(if (null (or (eq lang 'Chinese-GB)
(eq lang 'Chinese-CNS)))
(setq lang nil)))
(cond
(lang lang)
((eq its-current-language 'Chinese-GB) 'Chinese-GB)
((eq its-current-language 'Chinese-CNS) 'Chinese-CNS)
((eq egg-default-language 'Chinese-GB) 'Chinese-GB)
((eq egg-default-language 'Chinese-CNS) 'Chinese-CNS)
(t 'Chinese-GB))))
;;
;; return value ::= ( (<backend> ( <lang>... ) <source string> )... )
;;
(defun egg-assign-backend (source n)
(let ((len (length source))
(i 0)
j s lang backend retval)
(while (< i len)
(setq j (egg-next-single-property-change i 'egg-lang source len)
s (substring source i j)
lang (egg-get-language 0 s)
backend (egg-get-conversion-backend lang n t))
(egg-remove-all-text-properties 0 (- j i) s)
(put-text-property 0 (- j i) 'egg-lang lang s)
(setq retval (nconc retval (list (list backend (list lang) s)))
i j))
(prog1
retval
(while retval
(if (or (egg-default-conversion-backend-p (car (car retval)))
(null (equal (car (car retval)) (car (nth 1 retval)))))
(setq retval (cdr retval))
(nconc (nth 1 (car retval)) (nth 1 (nth 1 retval)))
(setcar (nthcdr 2 (car retval))
(concat (nth 2 (car retval)) (nth 2 (nth 1 retval))))
(setcdr retval (cddr retval)))))))
(defun egg-search-file (filename path)
(let (file)
(if (file-name-directory filename)
(setq file (substitute-in-file-name (expand-file-name filename))
file (and (file-readable-p file) file))
(while (and (null file) path)
(if (stringp (car path))
(setq file (substitute-in-file-name
(expand-file-name filename (car path)))
file (and (file-exists-p file) file)))
(setq path (cdr path)))
file)))
(defvar egg-default-startup-file "eggrc"
"Egg startup file name (system default)")
(defun egg-load-startup-file (backend lang)
(let ((eggrc (or (egg-search-file egg-startup-file
egg-startup-file-search-path)
(egg-search-file egg-default-startup-file load-path))))
(if eggrc
(condition-case error
(let ((egg-backend-type backend) (egg-language lang))
(load-file eggrc))
(error
(message "%s: %s" (car error)
(mapconcat (lambda (s) (format "%S" s)) (cdr error) ", "))
(egg-error 'rcfile-error))
(quit
(egg-error 'rcfile-error)))
(egg-error 'no-rcfile egg-startup-file-search-path))))
(defun egg-get-conversion-face (lang)
(if (null (consp egg-conversion-face))
egg-conversion-face
(cdr (or (assq lang egg-conversion-face)
(assq t egg-conversion-face)))))
(defvar egg-conversion-map
(let ((map (make-sparse-keymap))
(i 33))
(while (< i 127)
(define-key map (vector i) 'egg-exit-conversion-unread-char)
(setq i (1+ i)))
(define-key map "\C-@" 'egg-decide-first-char)
(define-key map [?\C-\ ] 'egg-decide-first-char)
(define-key map "\C-a" 'egg-beginning-of-conversion-buffer)
(define-key map "\C-b" 'egg-backward-bunsetsu)
(define-key map "\C-c" 'egg-abort-conversion)
(define-key map "\C-e" 'egg-end-of-conversion-buffer)
(define-key map "\C-f" 'egg-forward-bunsetsu)
(define-key map "\C-h" 'egg-help-command)
(define-key map "\C-i" 'egg-shrink-bunsetsu-major)
(define-key map "\C-k" 'egg-decide-before-point)
;; (define-key map "\C-l" 'egg-exit-conversion) ; Don't override C-L
(define-key map "\C-m" 'egg-exit-conversion)
(define-key map "\C-n" 'egg-next-candidate-major)
(define-key map "\C-o" 'egg-enlarge-bunsetsu-major)
(define-key map "\C-p" 'egg-previous-candidate-major)
(define-key map "\C-r" 'egg-reconvert-bunsetsu)
(define-key map "\C-t" 'egg-toroku-bunsetsu)
(define-key map "\C-v" 'egg-inspect-bunsetsu)
(define-key map "\M-i" 'egg-shrink-bunsetsu-minor)
(define-key map "\M-n" 'egg-next-candidate-minor)
(define-key map "\M-o" 'egg-enlarge-bunsetsu-minor)
(define-key map "\M-p" 'egg-previous-candidate-minor)
(define-key map "\M-r" 'egg-reconvert-bunsetsu-from-source)
(define-key map "\M-s" 'egg-select-candidate-major)
(define-key map "\M-v" 'egg-toggle-inspect-mode)
(define-key map "\M-z" 'egg-select-candidate-minor)
(define-key map "\e\C-s" 'egg-select-candidate-list-all-major)
(define-key map "\e\C-z" 'egg-select-candidate-list-all-minor)
(define-key map [return] 'egg-exit-conversion)
(define-key map [right] 'egg-forward-bunsetsu)
(define-key map [left] 'egg-backward-bunsetsu)
(define-key map [up] 'egg-previous-candidate)
(define-key map [down] 'egg-next-candidate)
(define-key map [backspace] 'egg-abort-conversion)
(define-key map [clear] 'egg-abort-conversion)
(define-key map [delete] 'egg-abort-conversion)
(define-key map " " 'egg-next-candidate)
(define-key map "/" 'egg-exit-conversion)
(define-key map "\M-h" 'egg-hiragana)
(define-key map "\M-k" 'egg-katakana)
(define-key map "\M-P" 'egg-pinyin)
(define-key map "\M-Z" 'egg-zhuyin)
(define-key map "\M-H" 'egg-hangul)
map)
"Keymap for EGG Conversion mode.")
(fset 'egg-conversion-map egg-conversion-map)
(defvar egg-conversion-mode nil)
(make-variable-buffer-local 'egg-conversion-mode)
(put 'egg-conversion-mode 'permanent-local t)
(or (assq 'egg-conversion-mode egg-sub-mode-map-alist)
(setq egg-sub-mode-map-alist (cons
'(egg-conversion-mode . egg-conversion-map)
egg-sub-mode-map-alist)))
(defun egg-conversion-enter/leave-fence (&optional old new)
(setq egg-conversion-mode (egg-conversion-fence-p)))
(add-hook 'egg-enter/leave-fence-hook 'egg-conversion-enter/leave-fence)
(defun egg-exit-conversion-unread-char ()
(interactive)
(setq egg-context (egg-exit-conversion)
unread-command-events (list last-command-event)
this-command 'egg-use-context))
(defun egg-make-bunsetsu (backend bunsetsu last)
(let* ((converted (copy-sequence (egg-get-bunsetsu-converted bunsetsu)))
(language (egg-get-converted-language bunsetsu))
(continue (and (null last) (egg-major-bunsetsu-continue-p bunsetsu)))
(face (egg-get-conversion-face language))
len len1)
(setq len1 (length converted))
(or (eq last t)
(setq converted (concat converted
(if continue
egg-conversion-minor-separator
egg-conversion-major-separator))))
(setq len (length converted))
(egg-remove-all-text-properties 0 len converted)
(add-text-properties 0 len
(list 'read-only t
(egg-bunsetsu-info) bunsetsu
'egg-backend backend
'egg-lang language
'egg-bunsetsu-last last
'egg-major-continue continue
'point-entered 'egg-enter/leave-fence
'point-left 'egg-enter/leave-fence
'modification-hooks '(egg-modify-fence))
converted)
(if face
(egg-set-face 0 len1 face converted))
converted))
(defun egg-insert-bunsetsu-list (backend bunsetsu-list &optional last before)
(let ((len (length bunsetsu-list)))
(funcall (if before 'insert-before-markers 'insert)
(mapconcat
(lambda (b)
(setq len (1- len))
(egg-make-bunsetsu backend b (and (= len 0) last)))
bunsetsu-list nil))))
(defun egg-beginning-of-conversion-buffer (n)
(interactive "p")
(cond
((<= n 0)
(egg-end-of-conversion-buffer 1))
((null (get-text-property (1- (point)) 'egg-start))
(goto-char (previous-single-property-change (point) 'egg-start)))))
(defun egg-end-of-conversion-buffer (n)
(interactive "p")
(cond
((<= n 0)
(egg-beginning-of-conversion-buffer 1))
(t
(goto-char (egg-next-single-property-change (point) 'egg-end))
(backward-char))))
(defun egg-backward-bunsetsu (n)
(interactive "p")
(while (and (> n 0)
(null (get-text-property (1- (point)) 'egg-start)))
(backward-char)
(setq n (1- n)))
(if (> n 0)
(signal 'beginning-of-buffer nil)))
(defun egg-forward-bunsetsu (n)
(interactive "p")
(while (and (>= n 0)
(null (get-text-property (point) 'egg-end)))
(forward-char)
(setq n (1- n)))
(backward-char)
(if (>= n 0)
(signal 'end-of-buffer nil)))
(defun egg-get-bunsetsu-tail (b)
(nth (1- (length b)) b))
(defun egg-previous-bunsetsu-point (p &optional n obj lim)
(or n (setq n 1))
(while (> n 0)
(setq p (previous-single-property-change p (egg-bunsetsu-info) obj lim)
n (1- n)))
p)
(defun egg-next-bunsetsu-point (p &optional n obj lim)
(or n (setq n 1))
(while (> n 0)
(setq p (egg-next-single-property-change p (egg-bunsetsu-info) obj lim)
n (1- n)))
p)
(defun egg-get-previous-bunsetsu (p)
(and (null (egg-get-bunsetsu-last (1- p)))
(egg-get-bunsetsu-info (1- p))))
(defun egg-get-previous-major-bunsetsu (p)
(let ((prev (egg-get-previous-bunsetsu p))
bunsetsu)
(while prev
(setq bunsetsu (cons prev bunsetsu)
p (egg-previous-bunsetsu-point p)
prev (and (egg-get-major-continue (1- p))
(egg-get-bunsetsu-info (1- p)))))
bunsetsu))
(defun egg-get-next-bunsetsu (p)
(and (null (egg-get-bunsetsu-last p))
(egg-get-bunsetsu-info (egg-next-bunsetsu-point p))))
(defun egg-get-major-bunsetsu (p)
(let ((next (egg-get-bunsetsu-info p))
bunsetsu)
(while next
(setq bunsetsu (cons next bunsetsu)
p (egg-next-bunsetsu-point p)
next (and (egg-get-major-continue (1- p))
(egg-get-bunsetsu-info p))))
(nreverse bunsetsu)))
(defsubst egg-get-major-bunsetsu-source (list)
(mapconcat 'egg-get-bunsetsu-source list nil))
(defsubst egg-get-major-bunsetsu-converted (list)
(mapconcat 'egg-get-bunsetsu-converted list nil))
(defvar egg-inspect-mode nil
"*Display clause information on candidate selection, if non-NIL.")
(defun egg-toggle-inspect-mode ()
(interactive)
(if (setq egg-inspect-mode (not egg-inspect-mode))
(egg-inspect-bunsetsu t)))
(defun egg-inspect-bunsetsu (&optional quiet)
(interactive)
(or (egg-word-inspection (egg-get-bunsetsu-info (point)))
quiet
(beep)))
(defvar egg-candidate-selection-info nil)
(make-variable-buffer-local 'egg-candidate-selection-info)
(defvar egg-candidate-selection-major t)
(make-variable-buffer-local 'egg-candidate-selection-major)
(defsubst egg-set-candsel-info (b major)
(setq egg-candidate-selection-info (list (car b) (cadr b) (caddr b) major)))
(defsubst egg-candsel-last-bunsetsu () (car egg-candidate-selection-info))
(defsubst egg-candsel-last-prev-b () (nth 1 egg-candidate-selection-info))
(defsubst egg-candsel-last-next-b () (nth 2 egg-candidate-selection-info))
(defsubst egg-candsel-last-major () (nth 3 egg-candidate-selection-info))
(defun egg-major-bunsetsu-head-p (head bunsetsu)
(while (and head (eq (car head) (car bunsetsu)))
(setq head (cdr head)
bunsetsu (cdr bunsetsu)))
(null head))
(defun egg-major-bunsetsu-tail-p (tail bunsetsu)
(egg-major-bunsetsu-head-p
tail (nthcdr (- (length bunsetsu) (length tail)) bunsetsu)))
(defun egg-get-candsel-target-major ()
(let ((bunsetsu (egg-get-major-bunsetsu (point)))
(prev-b (egg-get-previous-major-bunsetsu (point)))
next-b)
(cond
((and (egg-candsel-last-major)
(egg-major-bunsetsu-tail-p (egg-candsel-last-prev-b) prev-b)
(egg-major-bunsetsu-head-p (append (egg-candsel-last-bunsetsu)
(egg-candsel-last-next-b))
bunsetsu))
(setq bunsetsu (egg-candsel-last-bunsetsu)
prev-b (egg-candsel-last-prev-b)
next-b (egg-candsel-last-next-b)))
((null (egg-get-bunsetsu-last
(egg-next-bunsetsu-point (point) (1- (length bunsetsu)))))
(setq next-b (egg-get-major-bunsetsu
(egg-next-bunsetsu-point (point) (length bunsetsu))))))
(setq egg-candidate-selection-major t)
(list bunsetsu prev-b next-b t)))
(defun egg-get-candsel-target-minor ()
(let* ((bunsetsu (list (egg-get-bunsetsu-info (point))))
(prev-b (egg-get-previous-bunsetsu (point)))
(next-b (egg-get-next-bunsetsu (point))))
(setq egg-candidate-selection-major nil)
(list bunsetsu (and prev-b (list prev-b)) (and next-b (list next-b)) nil)))
(defun egg-check-candsel-target (b prev-b next-b major)
(if major
(and (egg-major-bunsetsu-tail-p
prev-b (egg-get-previous-major-bunsetsu (point)))
(let* ((cur-b (egg-get-major-bunsetsu (point)))
(next-p (egg-next-bunsetsu-point (point) (length cur-b))))
(egg-major-bunsetsu-head-p
(append b next-b)
(append cur-b (and (null (egg-get-bunsetsu-last (1- next-p)))
(egg-get-major-bunsetsu next-p))))))
(and (eq (egg-get-bunsetsu-info (point)) (car b))
(eq (egg-get-previous-bunsetsu (point)) (car prev-b))
(eq (egg-get-next-bunsetsu (point)) (car next-b)))))
(defun egg-insert-new-bunsetsu (b tail new-b)
(let* ((backend (egg-get-backend (point)))
(start (egg-previous-bunsetsu-point (point) (length (cadr new-b))))
(end (egg-next-bunsetsu-point (point) (+ (length b) (length tail))))
(last (egg-get-bunsetsu-last (1- end)))
(insert-before (buffer-has-markers-at end)))
(cond
((buffer-has-markers-at end)
(delete-region start end)
(egg-insert-bunsetsu-list backend
(append (cadr new-b) (car new-b) (caddr new-b))
last t))
((buffer-has-markers-at (egg-next-bunsetsu-point (point) (length b)))
(delete-region start end)
(egg-insert-bunsetsu-list backend (append (cadr new-b) (car new-b))
nil t)
(egg-insert-bunsetsu-list backend (caddr new-b) last))
((buffer-has-markers-at (point))
(delete-region start end)
(egg-insert-bunsetsu-list backend (cadr new-b) nil t)
(egg-insert-bunsetsu-list backend (append (car new-b) (caddr new-b))
last))
(t
(delete-region start end)
(egg-insert-bunsetsu-list backend
(append (cadr new-b) (car new-b) (caddr new-b))
last)))
(goto-char (egg-next-bunsetsu-point start (length (cadr new-b))))
(if egg-inspect-mode
(egg-inspect-bunsetsu t))))
(defun egg-next-candidate (n)
(interactive "p")
(if egg-candidate-selection-major
(egg-next-candidate-major n)
(egg-next-candidate-minor n)))
(defun egg-next-candidate-major (n)
(interactive "p")
(apply 'egg-next-candidate-internal n (egg-get-candsel-target-major)))
(defun egg-next-candidate-minor (n)
(interactive "p")
(apply 'egg-next-candidate-internal n (egg-get-candsel-target-minor)))
(defun egg-previous-candidate (n)
(interactive "p")
(if egg-candidate-selection-major
(egg-previous-candidate-major n)
(egg-previous-candidate-minor n)))
(defun egg-previous-candidate-major (n)
(interactive "p")
(apply 'egg-next-candidate-internal (- n) (egg-get-candsel-target-major)))
(defun egg-previous-candidate-minor (n)
(interactive "p")
(apply 'egg-next-candidate-internal (- n) (egg-get-candsel-target-minor)))
(defvar egg-candidate-select-counter 1)
(make-variable-buffer-local 'egg-candidate-select-counter)
(defun egg-next-candidate-internal (n b prev-b next-b major)
(if (eq last-command (if major 'egg-candidate-major 'egg-candidate-minor))
(setq egg-candidate-select-counter (1+ egg-candidate-select-counter))
(setq egg-candidate-select-counter 1))
(if (= egg-candidate-select-counter egg-conversion-auto-candidate-menu)
(egg-select-candidate-internal
nil egg-conversion-auto-candidate-menu-show-all
b prev-b next-b major)
(setq this-command (if major 'egg-candidate-major 'egg-candidate-minor))
(let ((inhibit-read-only t)
new-b candidates nitem i beep)
(setq candidates (egg-list-candidates b prev-b next-b major))
(if (null candidates)
(setq beep t)
(setq i (+ n (car candidates))
nitem (length (cdr candidates)))
(cond
((< i 0) ; go backward as if it is ring
(setq i (% i nitem))
(if (< i 0)
(setq i (+ i nitem))))
((< i nitem)) ; OK
(egg-conversion-wrap-select ; go backward as if it is ring
(setq i (% i nitem)))
(t ; don't go forward
(setq i (1- nitem)
beep t)))
(setq new-b (egg-decide-candidate b i prev-b next-b))
(egg-set-candsel-info new-b major)
(egg-insert-new-bunsetsu b (caddr new-b) new-b))
(if beep
(ding)))))
(defun egg-numbering-item (list)
(let ((n -1))
(mapcar (lambda (item) (cons item (setq n (1+ n)))) list)))
(defun egg-sort-item (list sort)
(if (eq (null sort) (null egg-conversion-sort-by-converted-string))
list
(sort list (lambda (a b) (string< (car a) (car b))))))
(defun egg-select-candidate-major (sort)
(interactive "P")
(apply 'egg-select-candidate-internal sort nil (egg-get-candsel-target-major)))
(defun egg-select-candidate-minor (sort)
(interactive "P")
(apply 'egg-select-candidate-internal sort nil (egg-get-candsel-target-minor)))
(defun egg-select-candidate-list-all-major (sort)
(interactive "P")
(apply 'egg-select-candidate-internal sort t (egg-get-candsel-target-major)))
(defun egg-select-candidate-list-all-minor (sort)
(interactive "P")
(apply 'egg-select-candidate-internal sort t (egg-get-candsel-target-minor)))
(defun egg-select-candidate-internal (sort all b prev-b next-b major)
(let ((prompt (egg-get-message 'candidate))
new-b candidates pos clist item-list i)
(setq candidates (egg-list-candidates b prev-b next-b major))
(if (null candidates)
(ding)
(setq pos (car candidates)
clist (cdr candidates)
item-list (egg-sort-item (egg-numbering-item clist) sort)
i (menudiag-select (list 'menu prompt item-list)
all
(list (assq (nth pos clist) item-list))))
(if (or (null (egg-conversion-fence-p))
(null (egg-check-candsel-target b prev-b next-b major)))
(error "Fence was already modified")
(let ((inhibit-read-only t))
(setq new-b (egg-decide-candidate b i prev-b next-b))
(egg-set-candsel-info new-b major)
(egg-insert-new-bunsetsu b (caddr new-b) new-b))))))
(defun egg-hiragana (&optional minor)
(interactive "P")
(if (null minor)
(apply 'egg-special-convert this-command (egg-get-candsel-target-major))
(apply 'egg-special-convert this-command (egg-get-candsel-target-minor))))
(defalias 'egg-katakana 'egg-hiragana)
(defalias 'egg-pinyin 'egg-hiragana)
(defalias 'egg-zhuyin 'egg-hiragana)
(defalias 'egg-hangul 'egg-hiragana)