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))
163 (let ((map (make-sparse-keymap))
165 (define-key map "\C-a" 'its-beginning-of-input-buffer)
166 (define-key map "\C-b" 'its-backward-SYL)
167 (define-key map "\C-d" 'its-delete-SYL)
168 (define-key map "\C-e" 'its-end-of-input-buffer)
169 (define-key map "\C-f" 'its-forward-SYL)
170 (define-key map "\C-]" 'its-cancel-input)
171 (define-key map "\C-h" 'its-mode-help-command)
172 (define-key map "\C-k" 'its-kill-line)
173 ;; (define-key map "\C-l" 'its-exit-mode)
174 (define-key map "\C-m" 'its-exit-mode) ; RET
175 (define-key map [return] 'its-exit-mode)
176 (define-key map "\C-t" 'its-transpose-chars)
177 (define-key map [delete] 'its-delete-backward-SYL)
178 (define-key map [right] 'its-forward-SYL)
179 (define-key map [left] 'its-backward-SYL)
180 (define-key map "\C-\\" 'its-exit-mode-off-input-method)
182 (define-key map (vector i) 'its-self-insert-char)
184 (define-key map " " 'its-kick-convert-region)
185 (define-key map "\177" 'its-delete-backward-SYL)
187 (define-key map "\C-p" 'its-previous-map)
188 (define-key map "\C-n" 'its-next-map)
189 ; (define-key map "\M-h" 'its-hiragana) ; hiragana-region for input-buffer
190 ; (define-key map "\M-k" 'its-katakana)
191 ; (define-key map "\M-<" 'its-hankaku)
192 ; (define-key map "\M->" 'its-zenkaku)
193 ; (define-key map "\M-\C-h" 'its-select-hiragana)
194 ; (define-key map "\M-\C-k" 'its-select-katakana)
195 ;;; (define-key map "\M-q" 'its-select-downcase) ;
196 ; (define-key map "\M-Q" 'its-select-upcase)
197 ; (define-key map "\M-z" 'its-select-zenkaku-downcase)
198 ; (define-key map "\M-Z" 'its-select-zenkaku-upcase)
200 "Keymap for ITS mode.")
202 (defvar its-fence-open "|" "*
\e$B%U%'%s%9$N;OE@$r<($9J8;zNs
\e(B (1
\e$BJ8;z
\e(B)")
203 (defvar its-fence-close "|" "*
\e$B%U%'%s%9$N=*E@$r<($9J8;zNs
\e(B (1
\e$BJ8;z
\e(B)")
204 (defvar its-fence-face nil "*
\e$B%U%'%s%9I=<($KMQ$$$k
\e(B face
\e$B$^$?$O
\e(B nil")
206 (defun its-put-cursor (cursor)
209 (add-text-properties p (point) (list 'local-map its-mode-map
211 'intangible 'its-part-2
215 ;; +-- START property
216 ;; | --- CURSOR Property
218 ;; v v v-- END Property
220 ;; ^^^ ^^^ ^^^------ SYL Property
222 ;; intangible intangible
225 (defun its-insert-fence-open ()
227 (insert its-fence-open)
228 (add-text-properties p (point)
230 '(invisible t its-start t intangible its-part-1)
231 '(its-start t intangible its-part-1)))))
233 (defun its-insert-fence-close ()
235 (insert its-fence-close)
236 (add-text-properties p (point)
238 '(invisible t its-end t intangible its-part-2)
239 '(its-end t intangible its-part-2)))
242 (defun its-start (key)
243 (its-insert-fence-open)
244 (its-insert-fence-close)
245 (its-put-cursor (its-input nil key))
246 (force-mode-line-update))
248 (defun its-restart (str)
250 (its-insert-fence-open)
251 (its-insert-fence-close)
254 (put-text-property p (point) 'intangible 'its-part-2)
258 (defun its-self-insert-char ()
260 (let ((key last-command-char)
261 (cursor (get-text-property (point) 'its-cursor))
264 (setq syl (get-text-property (1- (point)) 'its-syl)))
266 (delete-region (point) (1+ (point)))
267 (setq cursor (its-input syl key))
268 (its-put-cursor cursor)))
270 (defvar its-current-map nil)
271 (make-variable-buffer-local 'its-current-map)
272 (put 'its-current-map 'permanent-local t)
274 (defun its-initial-ISYL ()
275 (its-get-start-state its-current-map))
277 (defun its-make-VSYL (keyseq)
278 (cons keyseq (length keyseq)))
281 (defun its-input (syl key)
283 (setq syl (its-initial-ISYL)))
284 (let ((output (car syl))
287 ;; k/kk/s is "point in keyseq"
288 (its-input-to-vsyl syl key k/kk/s output)
290 (its-state-machine syl key 'its-buffer-ins/del-SYL))))
292 (defun its-input-to-vsyl (syl key point output)
295 (let ((len (length output)))
297 ;; point is at end of VSYL. Don't need to call state machine.
299 (its-buffer-ins/del-SYL
300 (its-make-VSYL (concat output (vector key))) syl)
302 ;; point is at middle of VSYL.
303 (let ((new-keyseq (concat (substring output 0 point)
305 (substring output point))))
306 (its-state-machine-keyseq new-keyseq 'its-buffer-ins/del-SYL))))))
308 (defvar its-barf-on-invalid-keyseq nil
309 "T means don't allow invalid key sequence in input buffer.")
312 ;;; ITS State Machine
316 (defun its-state-machine (state key emit)
317 (let ((next-state (its-get-next-state state key))
320 (let ((kst/t (its-get-kst/t next-state)))
321 (funcall emit next-state state)
322 (if (not (its-kst-p kst/t))
323 ;; Here we arrive to a terminal state.
324 ;; Emit a DSYL, and go ahead.
325 (let ((output (its-get-output next-state))
326 (keyseq (its-get-keyseq next-state))
329 ;; It's negative integer which specifies how many
330 ;; characters we go backwards
331 (its-state-machine-keyseq (substring keyseq back)
334 ;; Still, it's a intermediate state.
337 (setq expr-output-back (its-get-otherwise state key)))
338 (let ((keyseq (concat (its-get-keyseq state) (char-to-string key))))
339 (funcall emit expr-output-back state)
340 (its-state-machine-keyseq
341 (substring keyseq (its-eob-back expr-output-back)) emit))
342 ;; No next state for KEY. It's invalid sequence.
343 (if (< key 0) ; no next state for END of keystroke
345 (if its-barf-on-invalid-keyseq
346 (error its-barf-on-invalid-keyseq)
347 (funcall emit (cons (car state)
348 (list (its-get-keyseq state))) state)
350 (if its-barf-on-invalid-keyseq
351 (error its-barf-on-invalid-keyseq)
352 ;; XXX Should make DSYL (instead of VSYL)?
353 (let ((keyseq (concat (its-get-keyseq state) (vector key))))
354 (funcall emit (its-make-VSYL keyseq) state)
357 (defvar its-latest-SYL nil
358 "The latest SYL inserted.")
359 (defsubst its-update-latest-SYL (syl)
360 (setq its-latest-SYL syl))
363 (defun its-state-machine-keyseq (keyseq emit &optional eol)
365 (len (length keyseq))
366 (its-barf-on-invalid-keyseq nil) ; temporally disable DING
367 (syl (its-initial-ISYL))
370 (let ((key (aref keyseq i)))
372 (if (numberp (cdr syl)) ; VSYL
375 (its-make-VSYL (concat (car syl) (vector key)))
378 (its-state-machine syl key emit)))
381 (setq syl (its-initial-ISYL))
382 (setq syl its-latest-SYL))))
384 (its-state-machine syl -1 emit)
387 (defun its-buffer-ins/del-SYL (newsyl oldsyl)
388 (its-buffer-delete-SYL oldsyl)
389 (its-update-latest-SYL newsyl)
391 (insert (its-get-output newsyl))
392 (add-text-properties p (point)
393 (list 'its-syl newsyl
394 'its-map its-current-map
395 'its-lang its-current-language
396 'intangible 'its-part-1))
398 (put-text-property p (point) 'face its-fence-face))))
400 (defun its-buffer-delete-SYL (syl)
401 (let ((len (length (its-get-output syl))))
402 (delete-region (- (point) len) (point))))
404 (defun its-get-next-state (state key)
405 (let ((kst/t (its-get-kst/t state)))
406 (cdr (assq key (car kst/t)))))
409 (defun its-otherwise-match (expr key)
410 (or (null expr) ; <expr>::= NIL means "ANY"
411 (let ((case-fold-search nil))
412 (string-match expr (char-to-string key)))))
414 (defun its-get-otherwise (state key)
415 (let* ((kst/t (its-get-kst/t state))
419 (setq expr-output-back (car ebl))
420 (let ((expr (its-eob-keyexpr expr-output-back)))
421 (if (its-otherwise-match expr key)
423 (setq ebl (cdr ebl)))))
431 (defvar its-map-alist nil)
433 (defun its-get-map (name)
434 (assoc name its-map-alist))
436 (defun its-register-map (map)
437 (let* ((name (car map))
438 (place (assoc name its-map-alist)))
440 (setcdr place (cdr map))
441 (setq its-map-alist (cons map its-map-alist)))
444 (defmacro define-its-state-machine (map name indicator lang doc &rest exprs)
446 (eval-when (eval compile)
447 (let ((its-current-map (its-new-map ,name ,indicator ,lang)))
449 (setq ,map its-current-map)))
450 (define-its-compiled-map ,map ,doc)))
452 (defmacro define-its-compiled-map (map doc)
453 `(defconst ,map ',(symbol-value map) ,doc))
455 (defmacro define-its-state-machine-append (map &rest exprs)
457 `(let ((its-current-map ,map)))
459 (list `(setq ,map its-current-map))))
462 ;; Construct State Machine
464 (defun its-defrule (input output &optional back enable-overwrite)
465 "
\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
466 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
467 \e$BLa$C$FF0$/$b$N$H$9$k!#JQ495,B'$O$b$C$H$b:G6a$K
\e(B its-define-state-machine
468 \e$B$5$l$?JQ49I=$KEPO?$5$l$k!#
\e(B
470 (let ((state (its-goto-state (substring input 0 -1) nil t))
471 (key (aref input (1- (length input)))))
472 (if (and (its-get-next-state state key) (not enable-overwrite))
473 (error "Duplicated definition (%s)" input)
474 (its-make-next-state state key input output back))))
476 (defun its-goto-state (input &optional initial-state build-if-none)
477 (let ((len (length input))
479 (state (or initial-state (its-get-start-state its-current-map))))
482 (or (its-get-next-state state (aref input i))
484 (let ((keyseq (substring input 0 (1+ i))))
485 (its-make-next-state state (aref input i) keyseq keyseq))
486 (error "No such state (%s)" input)))
490 (defun its-defoutput (input display)
491 (let ((state (its-goto-state input)))
492 (its-set-output state display)))
494 (defun its-define-otherwise (state otherwise)
495 (let ((kst (its-get-kst/t state)))
497 (setcdr kst (cons otherwise (cdr kst)))
498 (its-set-kst state (cons nil (cons otherwise nil))))))
500 (defconst its-otherwise-back-one
501 (its-make-class+back nil -1))
503 (defun its-defrule-otherwise (state output &optional class back)
506 (setq class+back its-otherwise-back-one)
507 (setq class+back (its-make-class+back class back)))
508 (its-define-otherwise state
509 (its-make-otherwise output class+back))))
511 (defun its-defrule* (input output)
512 (let ((state (its-defrule input output)))
513 (its-defrule-otherwise state output)))
515 (defun its-make-next-state (state key keyseq output &optional back)
516 (let ((next-state (its-new-state output keyseq back))
517 (kst (its-get-kst/t state)))
519 (setcar kst (cons (cons key next-state) (car kst)))
520 (its-set-kst state (list (list (cons key next-state)))))
524 (defun its-beginning-of-input-buffer ()
527 (if (not (get-text-property (1- (point)) 'its-start))
528 (let ((begpos (previous-single-property-change (point) 'its-start)))
529 ;; Make SYLs have property of "part 2"
530 (put-text-property begpos (point) 'intangible 'its-part-2)
534 (defun its-end-of-input-buffer ()
537 (if (not (get-text-property (point) 'its-end))
538 (let ((endpos (next-single-property-change (point) 'its-end)))
539 ;; Make SYLs have property of "part 1"
540 (put-text-property (point) endpos 'intangible 'its-part-1)
544 ;; TODO: move in VSYL
545 (defun its-backward-SYL (n)
548 (let ((syl (get-text-property (1- (point)) 'its-syl))
551 (while (and syl (> n 0))
552 (setq p (- p (length (its-get-output syl))))
553 (setq syl (get-text-property (1- p) 'its-syl))
555 ;; Make SYLs have property of "part 2"
556 (put-text-property p old-point 'intangible 'its-part-2)
560 (signal 'beginning-of-buffer nil))))
562 ;; TODO: move in VSYL
563 (defun its-forward-SYL (n)
566 (let ((syl (get-text-property (point) 'its-syl))
569 (while (and syl (> n 0))
570 (setq p (+ p (length (its-get-output syl))))
571 (setq syl (get-text-property p 'its-syl))
573 ;; Make SYLs have property of "part 1"
574 (put-text-property p old-point 'intangible 'its-part-1)
578 (signal 'end-of-buffer nil))))
580 ;; TODO: handle VSYL. KILLFLAG
581 (defun its-delete-SYL (n killflag)
584 (let ((syl (get-text-property (point) 'its-syl))
586 (while (and syl (> n 0))
587 (setq p (+ p (length (its-get-output syl))))
588 (setq syl (get-text-property p 'its-syl))
593 (signal 'args-out-of-range (list p n)))
594 (delete-region (point) p)
596 (let ((s (get-text-property (1- (point)) 'its-start))
597 (e (get-text-property (point) 'its-end)))
599 (its-exit-mode-internal)
600 (its-put-cursor t))))))
603 (defun its-delete-backward-SYL (n killflag)
605 (let ((syl (get-text-property (1- (point)) 'its-syl))
606 (cursor (get-text-property (point) 'its-cursor)))
608 (signal 'beginning-of-buffer nil)
610 (its-delete-backward-SYL-internal n killflag)
611 (its-delete-backward-within-SYL syl n killflag)))))
614 (defun its-delete-backward-SYL-internal (n killflag)
615 (let ((syl (get-text-property (1- (point)) 'its-syl))
617 (while (and syl (> n 0))
618 (setq p (- p (length (its-get-output syl))))
619 (setq syl (get-text-property (1- p) 'its-syl))
622 (signal 'args-out-of-range (list p n))
623 (delete-region p (1+ (point))) ; also delete cursor
625 (let ((s (get-text-property (1- (point)) 'its-start))
626 (e (get-text-property (point) 'its-end)))
628 (its-exit-mode-internal)
629 (its-put-cursor t))))))
631 (defvar its-delete-by-keystroke nil)
634 (defun its-delete-backward-within-SYL (syl n killflag)
635 (let* ((keyseq (its-get-keyseq-syl syl))
636 (len (length keyseq))
638 (its-current-map (get-text-property (1- (point)) 'its-map)))
640 (signal 'args-out-of-range (list p n)))
642 (delete-region p (1+ p))
643 (its-buffer-delete-SYL syl)
646 (let ((s (get-text-property (1- (point)) 'its-start))
647 (e (get-text-property (point) 'its-end)))
649 (its-exit-mode-internal)
650 (its-put-cursor (not its-delete-by-keystroke))))
651 (setq keyseq (substring keyseq 0 (- len n)))
652 (let ((r (its-state-machine-keyseq keyseq 'its-buffer-ins/del-SYL)))
653 (its-put-cursor r)))))
656 (defun its-transpose-chars (n)
658 (let ((syl (get-text-property (1- (point)) 'its-syl))
659 (cursor (get-text-property (point) 'its-cursor)))
661 (signal 'beginning-of-buffer nil)
663 (its-delete-backward-SYL-internal n nil)
664 (its-delete-backward-within-SYL syl 2 nil)))))
667 (defun its-input-end ()
668 (let ((cursor (get-text-property (point) 'its-cursor)))
671 (its-input (get-text-property (1- (point)) 'its-syl) -1))
672 (delete-region (point) (1+ (point)))))
674 (defun its-exit-mode ()
678 (its-exit-mode-internal))
680 (defun its-exit-mode-off-input-method ()
684 (its-exit-mode-internal)
685 (inactivate-input-method))
687 ;; TODO: handle overwrite-mode, insertion-hook, fill...
688 (defun its-exit-mode-internal (&optional proceed-to-conversion)
691 (if (get-text-property (1- (point)) 'its-start)
692 (setq start (1- (point)))
693 (setq start (1- (previous-single-property-change (point) 'its-start))))
694 (delete-region start (1+ start))
695 ;; Delete close fence
696 (if (get-text-property (point) 'its-end)
698 (setq end (next-single-property-change (point) 'its-end)))
699 (delete-region end (1+ end))
700 ;; Remove all properties added by ITS
701 (remove-text-properties start end '(its-map nil
704 (if proceed-to-conversion
705 (egg-convert-region start end)
706 (remove-text-properties start end '(its-lang nil its-syl nil))
708 (run-hooks 'input-method-after-insert-chunk-hook))))
710 (defun its-kick-convert-region ()
713 (its-exit-mode-internal t))
715 (defun its-in-fence-p ()
716 (let ((prop (get-text-property (point) 'intangible)))
717 (or (eq prop 'its-part-1) (eq prop 'its-part-2))))
719 (defvar its-translation-result nil "")
721 (defun its-ins/del-SYL-batch (newsyl oldsyl)
722 (its-update-latest-SYL newsyl)
725 (not (its-kst-p (its-get-kst/t newsyl))))
727 (setq its-translation-result
728 (cons (its-get-output newsyl) its-translation-result))))
730 (defun its-translate-region (start end &optional map)
732 (setq its-translation-result nil)
735 (syl (its-initial-ISYL))
736 ;; temporally enable DING
737 (its-barf-on-invalid-keyseq "Invalid Romaji Sequence")
739 (while (< (point) end)
740 (let ((key (following-char)))
741 (setq cursor (its-state-machine syl key 'its-ins/del-SYL-batch))
744 (setq syl (its-initial-ISYL))
745 (setq syl its-latest-SYL))))
746 (if (eq syl its-latest-SYL)
747 (its-state-machine syl -1 'its-ins/del-SYL-batch))
748 (delete-region start end)
749 (apply 'insert (reverse its-translation-result))))
751 (require 'its-keydef)
754 ;;; its.el ends here.