1 ;;; its.el --- Input Translation Systam AKA "ITS(uDekirunDa!)"
3 ;; Copyright (C) 1997, 1998 Mule Project, Powered by Electrotechnical
5 ;; Project Leader: Satoru Tomura <tomura@etl.go.jp>
7 ;; Author: NIIBE Yutaka <gniibe@mri.co.jp>
8 ;; KATAYAMA Yoshio <kate@pfu.co.jp>
9 ;; Maintainer: NIIBE Yutaka <gniibe@mri.co.jp>
10 ;; Keywords: mule, multilingual, input method
12 ;; This file will be part of GNU Emacs (in future).
14 ;; EGG is free software; you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation; either version 2, or (at your option)
19 ;; EGG is distributed in the hope that it will be useful,
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 ;; GNU General Public License for more details.
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with GNU Emacs; see the file COPYING. If not, write to the
26 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
27 ;; Boston, MA 02111-1307, USA.
35 (defvar its-current-language)
36 (make-variable-buffer-local 'its-current-language)
38 ;; Data structure in ITS
41 ;; "SYL" stands for something like a syllable.
43 ;; <SYL> ::= ( <output> . ( <keyseq> . <terminal> )) ; Determined: DSYL
44 ;; | <state> ; Intermediate: ISYL
45 ;; | ( <output> . <point> ) ; Verbatim: VSYL
49 ;; ; ( <output> . ( <keyseq> . <key-state-table/terminal> ))
51 ;; <keyseq> ::= "string" of key sequence
52 ;; <output> ::= "string"
54 ;; <point> ::= integer which specifies point
56 ;; <cursor> ::= nil ; Previous SYL is active (input will go that SYL)
57 ;; | t ; input makes new SYL. DEL deletes previous SYL
58 ;; | its-cursor ; DEL breaks previous SYL, input makes new SYL
60 ;; Data structures in ITS
61 ;; (2) State machine which recognizes SYL
63 ;; <state> ::= ( <output> <keyseq> . <key-state-table/terminal> )
65 ;; <key-state-table/terminal> ::= <key-state-table> ; intermediate state
66 ;; | <terminal> ; terminal state
68 ;; <key-state-table> ::= ( <key-state-alist> . <expr-output-back-list> )
69 ;; <key-state-alist> ::= ( <key-state> ... )
70 ;; <key-state> ::= ( <key> . <state> )
71 ;; <key> ::= Positive INTEGER which specifies KEY STROKE
72 ;; | -1 ; means END of key stroke
74 ;; Only applicable for last transition.
75 ;; <expr-output-back-list> ::= ( (<output> . (<keyexpr> . <howmanyback>))... )
76 ;; <keyexpr> ::= something like "[a-z]" which specifies class of key.
77 ;; | NIL; means ANY of key (except END of the key stroke)
80 ;; <keyseq> ::= "string"
85 ;; <howmanyback> ::= integer which specifies how many key strokes we go back
87 ;; <output> ::= "string"
89 ;; Data structure in ITS (3) Map
91 ;; <map> ::= ( <name> <indicator> <language> . <start-state> )
92 ;; <name> ::= "string"
93 ;; <indicator> ::= "string"
94 ;; <language> ::= "string"
95 ;; <start-state> ::= <state>
98 (defsubst its-new-state (output keyseq back)
99 (cons output (cons keyseq back)))
101 (defsubst its-new-map (name indicator language)
102 (cons name (cons indicator (cons language (its-new-state "" "" nil)))))
104 (defsubst its-get-indicator (map)
107 (defsubst its-get-language (map)
110 (defsubst its-get-start-state (map)
113 (defsubst its-get-kst/t (state)
116 (defsubst its-set-kst (state kst)
117 (setcdr (cdr state) kst))
119 (defsubst its-get-keyseq (state)
122 (defsubst its-set-keyseq (state keyseq)
123 (setcar (cdr state) keyseq))
125 (defun its-get-keyseq-cooked (state)
126 (let ((keyseq (its-get-keyseq state))
127 (back (its-get-kst/t state)))
129 (substring keyseq 0 back)
132 (defsubst its-kst-p (kst/t)
133 (not (or (numberp kst/t) (null kst/t))))
135 (defsubst its-get-output (syl/state)
138 (defsubst its-set-output (state output)
139 (setcar state output))
141 (defsubst its-get-keyseq-syl (syl)
143 (cond ((stringp l) ; DSYL
150 (defsubst its-eob-keyexpr (eob)
152 (defsubst its-eob-back (eob)
155 (defsubst its-make-class+back (class back)
157 (defsubst its-make-otherwise (output class+back)
158 (cons output class+back))
162 (require 'its-keydef)
165 (let ((map (make-sparse-keymap))
167 (define-key map "\C-a" 'its-beginning-of-input-buffer)
168 (define-key map "\C-b" 'its-backward-SYL)
169 (define-key map "\C-d" 'its-delete-SYL)
170 (define-key map "\C-e" 'its-end-of-input-buffer)
171 (define-key map "\C-f" 'its-forward-SYL)
172 (define-key map "\C-]" 'its-cancel-input)
173 (define-key map "\C-h" 'its-mode-help-command)
174 (define-key map "\C-k" 'its-kill-line)
175 ;; (define-key map "\C-l" 'its-exit-mode)
176 (define-key map "\C-m" 'its-exit-mode) ; RET
177 (define-key map [return] 'its-exit-mode)
178 (define-key map "\C-t" 'its-transpose-chars)
179 (define-key map [delete] 'its-delete-backward-SYL)
180 (define-key map [right] 'its-forward-SYL)
181 (define-key map [left] 'its-backward-SYL)
182 (define-key map "\C-\\" 'its-exit-mode-off-input-method)
184 (define-key map (vector i) 'its-self-insert-char)
186 (define-key map " " 'its-kick-convert-region)
187 (define-key map "\177" 'its-delete-backward-SYL)
189 (define-key map "\M-p" 'its-previous-map)
190 (define-key map "\M-n" 'its-next-map)
191 (define-key map "\M-h" 'its-hiragana) ; hiragana-region for input-buffer
192 (define-key map "\M-k" 'its-katakana)
193 (define-key map "\M-<" 'its-hankaku)
194 (define-key map "\M->" 'its-zenkaku)
195 (its-define-select-keys map t)
197 "Keymap for ITS mode.")
199 (defvar its-fence-open "|" "*
\e$B%U%'%s%9$N;OE@$r<($9J8;zNs
\e(B (1
\e$BJ8;z
\e(B)")
200 (defvar its-fence-close "|" "*
\e$B%U%'%s%9$N=*E@$r<($9J8;zNs
\e(B (1
\e$BJ8;z
\e(B)")
201 (defvar its-fence-face nil "*
\e$B%U%'%s%9I=<($KMQ$$$k
\e(B face
\e$B$^$?$O
\e(B nil")
203 (defconst its-setup-fence-before-insert-SYL nil)
205 (defun its-put-cursor (cursor)
207 (map (copy-keymap its-mode-map)))
208 (its-define-select-keys map)
210 (add-text-properties p (point) (list 'local-map map
212 'intangible 'its-part-2
216 (defsubst its-set-cursor-status (cursor)
217 (put-text-property (point) (1+ (point)) 'its-cursor cursor)
221 ;; +-- START property
222 ;; | --- CURSOR Property
224 ;; v v v-- END Property
226 ;; ^^^ ^^^ ^^^------ SYL Property
228 ;; intangible intangible
231 (defun its-setup-fence-mode ()
232 (let ((open-props '(its-start t intangible its-part-1))
233 (close-props '(its-end t intangible its-part-2))
235 (insert its-fence-open)
237 (add-text-properties p p1 open-props)
238 (insert its-fence-close)
239 (add-text-properties p1 (point) close-props)
241 (put-text-property 'invisible t p (point)))
245 (defun its-start (key)
246 (let ((its-setup-fence-before-insert-SYL t))
248 (force-mode-line-update)))
250 (defun its-restart (str)
252 (its-setup-fence-mode t)
255 (its-beginning-of-input-buffer)))
257 (defun its-self-insert-char ()
259 (let ((key last-command-char)
261 (if (null (get-text-property (point) 'its-cursor))
262 (setq syl (get-text-property (1- (point)) 'its-syl)))
263 (its-input syl key)))
265 (defvar its-current-map nil)
266 (make-variable-buffer-local 'its-current-map)
267 (put 'its-current-map 'permanent-local t)
269 (defun its-initial-ISYL ()
270 (its-get-start-state its-current-map))
272 (defun its-make-VSYL (keyseq)
273 (cons keyseq (length keyseq)))
276 (defun its-input (syl key)
278 (setq syl (its-initial-ISYL)))
279 (let ((output (car syl))
282 ;; k/kk/s is "point in keyseq"
283 (its-input-to-vsyl syl key k/kk/s output)
285 (its-state-machine syl key 'its-buffer-ins/del-SYL))))
287 (defun its-input-to-vsyl (syl key point output)
290 (let ((len (length output)))
292 ;; point is at end of VSYL. Don't need to call state machine.
293 (its-buffer-ins/del-SYL
294 (its-make-VSYL (concat output (vector key))) syl nil)
295 ;; point is at middle of VSYL.
296 (let ((new-keyseq (concat (substring output 0 point)
298 (substring output point))))
299 (its-state-machine-keyseq new-keyseq 'its-buffer-ins/del-SYL))))))
301 (defvar its-barf-on-invalid-keyseq nil
302 "T means don't allow invalid key sequence in input buffer.")
304 (defun its-input-error ()
305 (error "Invalid Romaji Sequence"))
309 ;;; ITS State Machine
313 (defun its-state-machine (state key emit)
314 (let ((next-state (its-get-next-state state key))
315 expr-output-back kst/t output keyseq back)
317 ;; proceed to next status
319 (setq kst/t (its-get-kst/t next-state)
320 output (its-get-output next-state)
321 keyseq (its-get-keyseq next-state))
323 ;; Still, it's a intermediate state.
325 (funcall emit next-state state nil))
327 ;; It's negative integer which specifies how many
328 ;; characters we go backwards
330 (funcall emit next-state state 'its-cursor)
331 (its-state-machine-keyseq (substring keyseq kst/t) emit (< key 0)))
333 ;; Here we arrive to a terminal state.
334 ;; Emit a DSYL, and go ahead.
336 (funcall emit next-state state 'its-cursor))))
338 ;; push back by otherwise status
340 (setq expr-output-back (its-get-otherwise state key)))
341 (setq keyseq (concat (its-get-keyseq state) (vector key)))
342 (funcall emit expr-output-back state t)
343 (its-state-machine-keyseq
344 (substring keyseq (its-eob-back expr-output-back)) emit))
346 ;; No next state for KEY. It's invalid sequence.
347 (its-barf-on-invalid-keyseq
350 ;; no next state for END of keystroke
353 (funcall emit (cons (car state)
354 (list (its-get-keyseq state))) state t))
356 ;; XXX Should make DSYL (instead of VSYL)?
357 (setq keyseq (concat (its-get-keyseq state) (vector key)))
358 (funcall emit (its-make-VSYL keyseq) state nil)))))
360 (defvar its-latest-SYL nil
361 "The latest SYL inserted.")
362 (defsubst its-update-latest-SYL (syl)
363 (setq its-latest-SYL syl))
366 (defun its-state-machine-keyseq (keyseq emit &optional eol)
368 (len (length keyseq))
369 (syl (its-initial-ISYL))
374 ;; VSYL - no need looping
375 (funcall emit (its-make-VSYL (concat (car syl) keyseq)) syl nil)
379 (setq cursor (its-state-machine syl (aref keyseq i) emit))))
380 (setq syl (if cursor (its-initial-ISYL) its-latest-SYL)
383 (its-state-machine syl -1 emit)
386 (defun its-buffer-ins/del-SYL (newsyl oldsyl cursor)
387 (if its-setup-fence-before-insert-SYL
389 (setq its-setup-fence-before-insert-SYL nil)
390 (its-setup-fence-mode)))
391 (its-buffer-delete-SYL oldsyl)
392 (its-update-latest-SYL newsyl)
394 (insert (its-get-output newsyl))
395 (add-text-properties p (point)
396 (list 'its-syl newsyl
397 'its-map its-current-map
398 'its-lang its-current-language
399 'intangible 'its-part-1))
401 (put-text-property p (point) 'face its-fence-face))
402 (its-set-cursor-status cursor)))
404 (defun its-buffer-delete-SYL (syl)
405 (let ((len (length (its-get-output syl))))
406 (delete-region (- (point) len) (point))))
408 (defun its-get-next-state (state key)
409 (let ((kst/t (its-get-kst/t state)))
410 (cdr (assq key (car kst/t)))))
413 (defun its-otherwise-match (expr key)
414 (or (null expr) ; <expr>::= NIL means "ANY"
415 (let ((case-fold-search nil))
416 (string-match expr (char-to-string key)))))
418 (defun its-get-otherwise (state key)
419 (let* ((kst/t (its-get-kst/t state))
423 (setq expr-output-back (car ebl))
424 (let ((expr (its-eob-keyexpr expr-output-back)))
425 (if (its-otherwise-match expr key)
427 (setq ebl (cdr ebl)))))
435 (defvar its-map-alist nil)
437 (defun its-get-map (name)
438 (assoc name its-map-alist))
440 (defun its-register-map (map)
441 (let* ((name (car map))
442 (place (assoc name its-map-alist)))
444 (setcdr place (cdr map))
445 (setq its-map-alist (cons map its-map-alist)))
448 (defmacro define-its-state-machine (map name indicator lang doc &rest exprs)
450 (eval-when (eval compile)
451 (let ((its-current-map (its-new-map ,name ,indicator ,lang)))
453 (setq ,map its-current-map)))
454 (define-its-compiled-map ,map ,doc)))
456 (defmacro define-its-compiled-map (map doc)
457 `(defconst ,map ',(symbol-value map) ,doc))
459 (defmacro define-its-state-machine-append (map &rest exprs)
461 `(let ((its-current-map ,map)))
463 (list `(setq ,map its-current-map))))
466 ;; Construct State Machine
468 (defun its-defrule (input output &optional back enable-overwrite)
469 "
\e$BF~NO
\e(B INPUT
\e$B$rG'<1$7
\e(B, OUTPUT
\e$B$r=PNO$9$k$h$&$K%9%F!<%H%^%7%s$r9=@.$9$k!#
\e(B
470 BACK
\e$B$,
\e(B(
\e$BIi$N
\e(B)
\e$B@0?t$N;~$O
\e(B, OUTPUT
\e$B$r=PNO$7$?8e
\e(B, BACK
\e$B$NJ,
\e(B key stroke
\e$B$r
\e(B
471 \e$BLa$C$FF0$/$b$N$H$9$k!#JQ495,B'$O$b$C$H$b:G6a$K
\e(B its-define-state-machine
472 \e$B$5$l$?JQ49I=$KEPO?$5$l$k!#
\e(B
474 (let ((state (its-goto-state (substring input 0 -1) nil t))
475 (key (aref input (1- (length input)))))
476 (if (and (its-get-next-state state key) (not enable-overwrite))
477 (error "Duplicated definition (%s)" input)
478 (its-make-next-state state key input output back))))
480 (defun its-goto-state (input &optional initial-state build-if-none)
481 (let ((len (length input))
483 (state (or initial-state (its-get-start-state its-current-map))))
486 (or (its-get-next-state state (aref input i))
488 (let ((keyseq (substring input 0 (1+ i))))
489 (its-make-next-state state (aref input i) keyseq keyseq))
490 (error "No such state (%s)" input)))
494 (defun its-defoutput (input display)
495 (let ((state (its-goto-state input)))
496 (its-set-output state display)))
498 (defun its-define-otherwise (state otherwise)
499 (let ((kst (its-get-kst/t state)))
501 (setcdr kst (cons otherwise (cdr kst)))
502 (its-set-kst state (cons nil (cons otherwise nil))))))
504 (defconst its-otherwise-back-one
505 (its-make-class+back nil -1))
507 (defun its-defrule-otherwise (state output &optional class back)
510 (setq class+back its-otherwise-back-one)
511 (setq class+back (its-make-class+back class back)))
512 (its-define-otherwise state
513 (its-make-otherwise output class+back))))
515 (defun its-defrule* (input output)
516 (let ((state (its-defrule input output)))
517 (its-defrule-otherwise state output)))
519 (defun its-make-next-state (state key keyseq output &optional back)
520 (let ((next-state (its-new-state output keyseq back))
521 (kst (its-get-kst/t state)))
523 (setcar kst (cons (cons key next-state) (car kst)))
524 (its-set-kst state (list (list (cons key next-state)))))
528 (defun its-beginning-of-input-buffer ()
531 (if (not (get-text-property (1- (point)) 'its-start))
532 (let ((begpos (previous-single-property-change (point) 'its-start)))
533 ;; Make SYLs have property of "part 2"
534 (put-text-property begpos (point) 'intangible 'its-part-2)
538 (defun its-end-of-input-buffer ()
541 (if (not (get-text-property (point) 'its-end))
542 (let ((endpos (next-single-property-change (point) 'its-end)))
543 ;; Make SYLs have property of "part 1"
544 (put-text-property (point) endpos 'intangible 'its-part-1)
548 ;; TODO: move in VSYL
549 (defun its-backward-SYL (n)
552 (let ((syl (get-text-property (1- (point)) 'its-syl))
555 (while (and syl (> n 0))
556 (setq p (- p (length (its-get-output syl))))
557 (setq syl (get-text-property (1- p) 'its-syl))
559 ;; Make SYLs have property of "part 2"
560 (put-text-property p old-point 'intangible 'its-part-2)
564 (signal 'beginning-of-buffer nil))))
566 ;; TODO: move in VSYL
567 (defun its-forward-SYL (n)
570 (let ((syl (get-text-property (point) 'its-syl))
573 (while (and syl (> n 0))
574 (setq p (+ p (length (its-get-output syl))))
575 (setq syl (get-text-property p 'its-syl))
577 ;; Make SYLs have property of "part 1"
578 (put-text-property p old-point 'intangible 'its-part-1)
582 (signal 'end-of-buffer nil))))
584 ;; TODO: handle VSYL. KILLFLAG
585 (defun its-delete-SYL (n killflag)
588 (let ((syl (get-text-property (point) 'its-syl))
590 (while (and syl (> n 0))
591 (setq p (+ p (length (its-get-output syl))))
592 (setq syl (get-text-property p 'its-syl))
597 (signal 'args-out-of-range (list p n)))
598 (delete-region (point) p)
600 (let ((s (get-text-property (1- (point)) 'its-start))
601 (e (get-text-property (point) 'its-end)))
603 (its-exit-mode-internal)
604 (its-put-cursor t))))))
607 (defun its-delete-backward-SYL (n killflag)
609 (let ((syl (get-text-property (1- (point)) 'its-syl))
610 (cursor (get-text-property (point) 'its-cursor)))
612 (signal 'beginning-of-buffer nil)
614 (its-delete-backward-SYL-internal n killflag)
615 (its-delete-backward-within-SYL syl n killflag)))))
618 (defun its-delete-backward-SYL-internal (n killflag)
619 (let ((syl (get-text-property (1- (point)) 'its-syl))
621 (while (and syl (> n 0))
622 (setq p (- p (length (its-get-output syl))))
623 (setq syl (get-text-property (1- p) 'its-syl))
626 (signal 'args-out-of-range (list p n))
627 (delete-region p (1+ (point))) ; also delete cursor
629 (let ((s (get-text-property (1- (point)) 'its-start))
630 (e (get-text-property (point) 'its-end)))
632 (its-exit-mode-internal)
633 (its-put-cursor t))))))
635 (defvar its-delete-by-keystroke nil)
638 (defun its-delete-backward-within-SYL (syl n killflag)
639 (let* ((keyseq (its-get-keyseq-syl syl))
640 (len (length keyseq))
642 (its-current-map (get-text-property (1- (point)) 'its-map)))
644 (signal 'args-out-of-range (list p n)))
646 (delete-region p (1+ p))
647 (its-buffer-delete-SYL syl)
650 (let ((s (get-text-property (1- (point)) 'its-start))
651 (e (get-text-property (point) 'its-end)))
653 (its-exit-mode-internal)
654 (its-put-cursor (not its-delete-by-keystroke))))
655 (setq keyseq (substring keyseq 0 (- len n)))
656 (let ((r (its-state-machine-keyseq keyseq 'its-buffer-ins/del-SYL)))
657 (its-put-cursor r)))))
660 (defun its-transpose-chars (n)
662 (let ((syl (get-text-property (1- (point)) 'its-syl))
663 (cursor (get-text-property (point) 'its-cursor)))
665 (signal 'beginning-of-buffer nil)
667 (its-delete-backward-SYL-internal n nil)
668 (its-delete-backward-within-SYL syl 2 nil)))))
671 (defun its-input-end ()
672 (let ((cursor (get-text-property (point) 'its-cursor)))
675 (its-input (get-text-property (1- (point)) 'its-syl) -1))
676 (delete-region (point) (1+ (point)))))
678 (defun its-exit-mode ()
682 (its-exit-mode-internal))
684 (defun its-exit-mode-off-input-method ()
688 (its-exit-mode-internal)
689 (inactivate-input-method))
691 ;; TODO: handle overwrite-mode, insertion-hook, fill...
692 (defun its-exit-mode-internal (&optional proceed-to-conversion)
695 (if (get-text-property (1- (point)) 'its-start)
696 (setq start (1- (point)))
697 (setq start (1- (previous-single-property-change (point) 'its-start))))
698 (delete-region start (1+ start))
699 ;; Delete close fence
700 (if (get-text-property (point) 'its-end)
702 (setq end (next-single-property-change (point) 'its-end)))
703 (delete-region end (1+ end))
704 ;; Remove all properties added by ITS
705 (remove-text-properties start end '(its-map nil
708 (if proceed-to-conversion
709 (egg-convert-region start end)
710 (remove-text-properties start end '(its-lang nil its-syl nil))
712 (run-hooks 'input-method-after-insert-chunk-hook))))
714 (defun its-kick-convert-region ()
717 (its-exit-mode-internal t))
719 (defun its-in-fence-p ()
720 (let ((prop (get-text-property (point) 'intangible)))
721 (or (eq prop 'its-part-1) (eq prop 'its-part-2))))
723 (defvar its-translation-result "" "")
725 (defun its-ins/del-SYL-batch (newsyl oldsyl cursor)
726 (its-update-latest-SYL newsyl)
729 (not (its-kst-p (its-get-kst/t newsyl))))
731 (let ((output (its-get-output newsyl))
732 (oldlen (length its-translation-result)))
733 (setq its-translation-result (concat its-translation-result output))
734 (put-text-property oldlen (length its-translation-result)
735 'its-lang its-current-language
736 its-translation-result)))
739 (defun its-translate-region (start end)
741 (its-translate-region-internal start end)
742 (remove-text-properties start (point) '(its-lang nil)))
744 (defun its-translate-region-internal (start end)
745 (setq its-translation-result "")
748 (syl (its-initial-ISYL))
749 ;; temporally enable DING
750 (its-barf-on-invalid-keyseq t)
752 (while (< (point) end)
753 (let ((key (following-char)))
754 (setq cursor (its-state-machine syl key 'its-ins/del-SYL-batch))
757 (setq syl (its-initial-ISYL))
758 (setq syl its-latest-SYL))))
759 (if (eq syl its-latest-SYL)
760 (its-state-machine syl -1 'its-ins/del-SYL-batch))
761 (delete-region start end)
762 (insert its-translation-result)))
765 ;;; its.el ends here.