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-start (key)
228 (insert its-fence-open)
229 (add-text-properties p (point)
230 (let ((props '(its-start t intangible its-part-1)))
232 (append '(invisible t) props)
235 (setq cursor (its-input nil key))
236 (its-put-cursor cursor)
239 (insert its-fence-close)
240 (add-text-properties p (point)
241 (let ((props '(its-end t intangible its-part-2)))
243 (append '(invisible t) props)
246 (force-mode-line-update)))
248 (defun its-self-insert-char ()
250 (let ((key last-command-char)
251 (cursor (get-text-property (point) 'its-cursor))
254 (setq syl (get-text-property (1- (point)) 'its-syl)))
256 (delete-region (point) (1+ (point)))
257 (setq cursor (its-input syl key))
258 (its-put-cursor cursor)))
260 (defvar its-current-map nil)
261 (make-variable-buffer-local 'its-current-map)
262 (put 'its-current-map 'permanent-local t)
264 (defun its-initial-ISYL ()
265 (its-get-start-state its-current-map))
267 (defun its-make-VSYL (keyseq)
268 (cons keyseq (length keyseq)))
271 (defun its-input (syl key)
273 (setq syl (its-initial-ISYL)))
274 (let ((output (car syl))
277 ;; k/kk/s is "point in keyseq"
278 (its-input-to-vsyl syl key k/kk/s output)
280 (its-state-machine syl key 'its-buffer-ins/del-SYL))))
282 (defun its-input-to-vsyl (syl key point output)
285 (let ((len (length output)))
287 ;; point is at end of VSYL. Don't need to call state machine.
289 (its-buffer-ins/del-SYL
290 (its-make-VSYL (concat output (vector key))) syl)
292 ;; point is at middle of VSYL.
293 (let ((new-keyseq (concat (substring output 0 point)
295 (substring output point))))
296 (its-state-machine-keyseq new-keyseq 'its-buffer-ins/del-SYL))))))
298 (defvar its-barf-on-invalid-keyseq nil
299 "T means don't allow invalid key sequence in input buffer.")
302 ;;; ITS State Machine
306 (defun its-state-machine (state key emit)
307 (let ((next-state (its-get-next-state state key))
310 (let ((kst/t (its-get-kst/t next-state)))
311 (funcall emit next-state state)
312 (if (not (its-kst-p kst/t))
313 ;; Here we arrive to a terminal state.
314 ;; Emit a DSYL, and go ahead.
315 (let ((output (its-get-output next-state))
316 (keyseq (its-get-keyseq next-state))
319 ;; It's negative integer which specifies how many
320 ;; characters we go backwards
321 (its-state-machine-keyseq (substring keyseq back)
324 ;; Still, it's a intermediate state.
327 (setq expr-output-back (its-get-otherwise state key)))
328 (let ((keyseq (concat (its-get-keyseq state) (char-to-string key))))
329 (funcall emit expr-output-back state)
330 (its-state-machine-keyseq
331 (substring keyseq (its-eob-back expr-output-back)) emit))
332 ;; No next state for KEY. It's invalid sequence.
333 (if (< key 0) ; no next state for END of keystroke
335 (if its-barf-on-invalid-keyseq
336 (error its-barf-on-invalid-keyseq)
337 (funcall emit (cons (car state)
338 (list (its-get-keyseq state))) state)
340 (if its-barf-on-invalid-keyseq
341 (error its-barf-on-invalid-keyseq)
342 ;; XXX Should make DSYL (instead of VSYL)?
343 (let ((keyseq (concat (its-get-keyseq state) (vector key))))
344 (funcall emit (its-make-VSYL keyseq) state)
347 (defvar its-latest-SYL nil
348 "The latest SYL inserted.")
349 (defsubst its-update-latest-SYL (syl)
350 (setq its-latest-SYL syl))
353 (defun its-state-machine-keyseq (keyseq emit &optional eol)
355 (len (length keyseq))
356 (its-barf-on-invalid-keyseq nil) ; temporally disable DING
357 (syl (its-initial-ISYL))
360 (let ((key (aref keyseq i)))
362 (if (numberp (cdr syl)) ; VSYL
365 (its-make-VSYL (concat (car syl) (vector key)))
368 (its-state-machine syl key emit)))
371 (setq syl (its-initial-ISYL))
372 (setq syl its-latest-SYL))))
374 (its-state-machine syl -1 emit)
377 (defun its-buffer-ins/del-SYL (newsyl oldsyl)
378 (its-buffer-delete-SYL oldsyl)
379 (its-update-latest-SYL newsyl)
381 (insert (its-get-output newsyl))
382 (add-text-properties p (point)
383 (list 'its-syl newsyl
384 'its-map its-current-map
385 'its-lang its-current-language
386 'intangible 'its-part-1))
388 (put-text-property p (point) 'face its-fence-face))))
390 (defun its-buffer-delete-SYL (syl)
391 (let ((len (length (its-get-output syl))))
392 (delete-region (- (point) len) (point))))
394 (defun its-get-next-state (state key)
395 (let ((kst/t (its-get-kst/t state)))
396 (cdr (assq key (car kst/t)))))
399 (defun its-otherwise-match (expr key)
400 (or (null expr) ; <expr>::= NIL means "ANY"
401 (let ((case-fold-search nil))
402 (string-match expr (char-to-string key)))))
404 (defun its-get-otherwise (state key)
405 (let* ((kst/t (its-get-kst/t state))
409 (setq expr-output-back (car ebl))
410 (let ((expr (its-eob-keyexpr expr-output-back)))
411 (if (its-otherwise-match expr key)
413 (setq ebl (cdr ebl)))))
421 (defvar its-map-alist nil)
423 (defun its-get-map (name)
424 (assoc name its-map-alist))
426 (defun its-register-map (map)
427 (let* ((name (car map))
428 (place (assoc name its-map-alist)))
430 (setcdr place (cdr map))
431 (setq its-map-alist (cons map its-map-alist)))
434 (defmacro define-its-state-machine (map name indicator lang doc &rest exprs)
436 (eval-when (eval compile)
437 (let ((its-current-map (its-new-map ,name ,indicator ,lang)))
439 (setq ,map its-current-map)))
440 (define-its-compiled-map ,map ,doc)))
442 (defmacro define-its-compiled-map (map doc)
443 `(defconst ,map ',(symbol-value map) ,doc))
445 (defmacro define-its-state-machine-append (map &rest exprs)
447 `(let ((its-current-map ,map)))
449 (list `(setq ,map its-current-map))))
452 ;; Construct State Machine
454 (defun its-defrule (input output &optional back enable-overwrite)
455 "
\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
456 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
457 \e$BLa$C$FF0$/$b$N$H$9$k!#JQ495,B'$O$b$C$H$b:G6a$K
\e(B its-define-state-machine
458 \e$B$5$l$?JQ49I=$KEPO?$5$l$k!#
\e(B
460 (let ((state (its-goto-state (substring input 0 -1) nil t))
461 (key (aref input (1- (length input)))))
462 (if (and (its-get-next-state state key) (not enable-overwrite))
463 (error "Duplicated definition (%s)" input)
464 (its-make-next-state state key input output back))))
466 (defun its-goto-state (input &optional initial-state build-if-none)
467 (let ((len (length input))
469 (state (or initial-state (its-get-start-state its-current-map))))
472 (or (its-get-next-state state (aref input i))
474 (let ((keyseq (substring input 0 (1+ i))))
475 (its-make-next-state state (aref input i) keyseq keyseq))
476 (error "No such state (%s)" input)))
480 (defun its-defoutput (input display)
481 (let ((state (its-goto-state input)))
482 (its-set-output state display)))
484 (defun its-define-otherwise (state otherwise)
485 (let ((kst (its-get-kst/t state)))
487 (setcdr kst (cons otherwise (cdr kst)))
488 (its-set-kst state (cons nil (cons otherwise nil))))))
490 (defconst its-otherwise-back-one
491 (its-make-class+back nil -1))
493 (defun its-defrule-otherwise (state output &optional class back)
496 (setq class+back its-otherwise-back-one)
497 (setq class+back (its-make-class+back class back)))
498 (its-define-otherwise state
499 (its-make-otherwise output class+back))))
501 (defun its-defrule* (input output)
502 (let ((state (its-defrule input output)))
503 (its-defrule-otherwise state output)))
505 (defun its-make-next-state (state key keyseq output &optional back)
506 (let ((next-state (its-new-state output keyseq back))
507 (kst (its-get-kst/t state)))
509 (setcar kst (cons (cons key next-state) (car kst)))
510 (its-set-kst state (list (list (cons key next-state)))))
514 (defun its-beginning-of-input-buffer ()
517 (if (not (get-text-property (1- (point)) 'its-start))
518 (let ((begpos (previous-single-property-change (point) 'its-start)))
519 ;; Make SYLs have property of "part 2"
520 (put-text-property begpos (point) 'intangible 'its-part-2)
524 (defun its-end-of-input-buffer ()
527 (if (not (get-text-property (point) 'its-end))
528 (let ((endpos (next-single-property-change (point) 'its-end)))
529 ;; Make SYLs have property of "part 1"
530 (put-text-property (point) endpos 'intangible 'its-part-1)
534 ;; TODO: move in VSYL
535 (defun its-backward-SYL (n)
538 (let ((syl (get-text-property (1- (point)) 'its-syl))
541 (while (and syl (> n 0))
542 (setq p (- p (length (its-get-output syl))))
543 (setq syl (get-text-property (1- p) 'its-syl))
545 ;; Make SYLs have property of "part 2"
546 (put-text-property p old-point 'intangible 'its-part-2)
550 (signal 'beginning-of-buffer nil))))
552 ;; TODO: move in VSYL
553 (defun its-forward-SYL (n)
556 (let ((syl (get-text-property (point) 'its-syl))
559 (while (and syl (> n 0))
560 (setq p (+ p (length (its-get-output syl))))
561 (setq syl (get-text-property p 'its-syl))
563 ;; Make SYLs have property of "part 1"
564 (put-text-property p old-point 'intangible 'its-part-1)
568 (signal 'end-of-buffer nil))))
570 ;; TODO: handle VSYL. KILLFLAG
571 (defun its-delete-SYL (n killflag)
574 (let ((syl (get-text-property (point) 'its-syl))
576 (while (and syl (> n 0))
577 (setq p (+ p (length (its-get-output syl))))
578 (setq syl (get-text-property p 'its-syl))
583 (signal 'args-out-of-range (list p n)))
584 (delete-region (point) p)
586 (let ((s (get-text-property (1- (point)) 'its-start))
587 (e (get-text-property (point) 'its-end)))
589 (its-exit-mode-internal)
590 (its-put-cursor t))))))
593 (defun its-delete-backward-SYL (n killflag)
595 (let ((syl (get-text-property (1- (point)) 'its-syl))
596 (cursor (get-text-property (point) 'its-cursor)))
598 (signal 'beginning-of-buffer nil)
600 (its-delete-backward-SYL-internal n killflag)
601 (its-delete-backward-within-SYL syl n killflag)))))
604 (defun its-delete-backward-SYL-internal (n killflag)
605 (let ((syl (get-text-property (1- (point)) 'its-syl))
607 (while (and syl (> n 0))
608 (setq p (- p (length (its-get-output syl))))
609 (setq syl (get-text-property (1- p) 'its-syl))
612 (signal 'args-out-of-range (list p n))
613 (delete-region p (1+ (point))) ; also delete cursor
615 (let ((s (get-text-property (1- (point)) 'its-start))
616 (e (get-text-property (point) 'its-end)))
618 (its-exit-mode-internal)
619 (its-put-cursor t))))))
621 (defvar its-delete-by-keystroke nil)
624 (defun its-delete-backward-within-SYL (syl n killflag)
625 (let* ((keyseq (its-get-keyseq-syl syl))
626 (len (length keyseq))
628 (its-current-map (get-text-property (1- (point)) 'its-map)))
630 (signal 'args-out-of-range (list p n)))
632 (delete-region p (1+ p))
633 (its-buffer-delete-SYL syl)
636 (let ((s (get-text-property (1- (point)) 'its-start))
637 (e (get-text-property (point) 'its-end)))
639 (its-exit-mode-internal)
640 (its-put-cursor (not its-delete-by-keystroke))))
641 (setq keyseq (substring keyseq 0 (- len n)))
642 (let ((r (its-state-machine-keyseq keyseq 'its-buffer-ins/del-SYL)))
643 (its-put-cursor r)))))
646 (defun its-transpose-chars (n)
648 (let ((syl (get-text-property (1- (point)) 'its-syl))
649 (cursor (get-text-property (point) 'its-cursor)))
651 (signal 'beginning-of-buffer nil)
653 (its-delete-backward-SYL-internal n nil)
654 (its-delete-backward-within-SYL syl 2 nil)))))
657 (defun its-input-end ()
658 (let ((cursor (get-text-property (point) 'its-cursor)))
661 (its-input (get-text-property (1- (point)) 'its-syl) -1))
662 (delete-region (point) (1+ (point)))))
664 (defun its-exit-mode ()
668 (its-exit-mode-internal))
670 (defun its-exit-mode-off-input-method ()
674 (its-exit-mode-internal)
675 (inactivate-input-method))
677 ;; TODO: handle overwrite-mode, insertion-hook, fill...
678 (defun its-exit-mode-internal (&optional proceed-to-conversion)
681 (if (get-text-property (1- (point)) 'its-start)
682 (setq start (1- (point)))
683 (setq start (1- (previous-single-property-change (point) 'its-start))))
684 (delete-region start (1+ start))
685 ;; Delete close fence
686 (if (get-text-property (point) 'its-end)
688 (setq end (next-single-property-change (point) 'its-end)))
689 (delete-region end (1+ end))
690 ;; Remove all properties added by ITS
691 (remove-text-properties start end '(its-syl nil
695 (if proceed-to-conversion
696 (egg-convert-region start end)
697 (remove-text-properties start end '(its-lang nil))
699 (run-hooks 'input-method-after-insert-chunk-hook))))
701 (defun its-kick-convert-region ()
704 (its-exit-mode-internal t))
706 (defun its-in-fence-p ()
707 (let ((prop (get-text-property (point) 'intangible)))
708 (or (eq prop 'its-part-1) (eq prop 'its-part-2))))
710 (defvar its-translation-result nil "")
712 (defun its-ins/del-SYL-batch (newsyl oldsyl)
713 (its-update-latest-SYL newsyl)
716 (not (its-kst-p (its-get-kst/t newsyl))))
718 (setq its-translation-result
719 (cons (its-get-output newsyl) its-translation-result))))
721 (defun its-translate-region (start end &optional map)
723 (setq its-translation-result nil)
726 (syl (its-initial-ISYL))
727 ;; temporally enable DING
728 (its-barf-on-invalid-keyseq "Invalid Romaji Sequence")
730 (while (< (point) end)
731 (let ((key (following-char)))
732 (setq cursor (its-state-machine syl key 'its-ins/del-SYL-batch))
735 (setq syl (its-initial-ISYL))
736 (setq syl its-latest-SYL))))
737 (if (eq syl its-latest-SYL)
738 (its-state-machine syl -1 'its-ins/del-SYL-batch))
739 (delete-region start end)
740 (apply 'insert (reverse its-translation-result))))
742 (load "its-keydef.el")
745 ;;; its.el ends here.