egg-980217.
[elisp/egg.git] / its.el
1 ;;; its.el --- Input Translation Systam AKA "ITS(uDekirunDa!)"
2
3 ;; Copyright (C) 1997, 1998 Mule Project, Powered by Electrotechnical
4 ;; Laboratory, JAPAN.
5 ;; Project Leader: Satoru Tomura <tomura@etl.go.jp>
6
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
11
12 ;; This file will be part of GNU Emacs (in future).
13
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)
17 ;; any later version.
18
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.
23
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.
28
29 ;;; Commentary:
30
31 ;;; Code:
32
33 (require 'cl)
34
35 (defvar its-current-language)
36 (make-variable-buffer-local 'its-current-language)
37 \f
38 ;; Data structure in ITS
39 ;; (1) SYL and CURSOR
40 ;;
41 ;; "SYL" stands for something like a syllable.
42 ;;
43 ;; <SYL> ::= ( <output> . ( <keyseq> . <terminal> ))   ; Determined:   DSYL
44 ;;        |  <state>                            ; Intermediate: ISYL
45 ;;        |  ( <output> . <point> )             ; Verbatim:     VSYL
46 ;;        |  nil                                ; None
47 ;;
48 ;; ;<state> ::=
49 ;; ;          ( <output> . ( <keyseq> . <key-state-table/terminal> ))
50 ;;
51 ;; <keyseq> ::= "string" of key sequence
52 ;; <output> ::= "string"
53 ;;
54 ;; <point> ::= integer which specifies point
55 ;;
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
59
60 ;; Data structures in ITS
61 ;; (2) State machine which recognizes SYL
62 ;;
63 ;; <state> ::= ( <output> <keyseq> . <key-state-table/terminal> )
64 ;;
65 ;; <key-state-table/terminal> ::= <key-state-table> ; intermediate state
66 ;;                             |  <terminal>        ; terminal state
67 ;;
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
73 ;;
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)
78 ;;
79 ;;
80 ;; <keyseq> ::= "string"
81 ;;
82 ;; <terminal> ::= nil
83 ;;             |  <howmanyback>
84 ;;
85 ;; <howmanyback> ::= integer which specifies how many key strokes we go back
86 ;;
87 ;; <output> ::= "string"
88
89 ;; Data structure in ITS (3) Map
90 ;;
91 ;; <map>         ::= ( <name> <indicator> <language> . <start-state> )
92 ;; <name>        ::= "string"
93 ;; <indicator>   ::= "string"
94 ;; <language>    ::= "string"
95 ;; <start-state> ::= <state>
96 ;;
97 \f
98 (defsubst its-new-state (output keyseq back)
99   (cons output (cons keyseq back)))
100
101 (defsubst its-new-map (name indicator language)
102   (cons name (cons indicator (cons language (its-new-state "" "" nil)))))
103
104 (defsubst its-get-indicator (map)
105   (nth 1 map))
106
107 (defsubst its-get-language (map)
108   (nth 2 map))
109
110 (defsubst its-get-start-state (map)
111   (nthcdr 3 map))
112
113 (defsubst its-get-kst/t (state)
114   (cdr (cdr state)))
115
116 (defsubst its-set-kst (state kst)
117   (setcdr (cdr state) kst))
118
119 (defsubst its-get-keyseq (state)
120   (car (cdr state)))
121
122 (defsubst its-set-keyseq (state keyseq)
123   (setcar (cdr state) keyseq))
124
125 (defun its-get-keyseq-cooked (state)
126   (let ((keyseq (its-get-keyseq state))
127         (back (its-get-kst/t state)))
128     (if back
129         (substring keyseq 0 back)
130       keyseq)))
131
132 (defsubst its-kst-p (kst/t)
133   (not (or (numberp kst/t) (null kst/t))))
134
135 (defsubst its-get-output (syl/state)
136   (car syl/state))
137
138 (defsubst its-set-output (state output)
139   (setcar state output))
140
141 (defsubst its-get-keyseq-syl (syl)
142   (let ((l (cdr syl)))
143     (cond ((stringp l)                  ; DSYL
144            l)
145           ((numberp l)                  ; VSYL
146            (car syl))
147           (t
148            (car (cdr syl))))))
149
150 (defsubst its-eob-keyexpr (eob)
151   (car (cdr eob)))
152 (defsubst its-eob-back (eob)
153   (cdr (cdr eob)))
154
155 (defsubst its-make-class+back (class back)
156   (cons class back))
157 (defsubst its-make-otherwise (output class+back)
158   (cons output class+back))
159 ;;
160 ;;
161
162 (defvar its-mode-map
163   (let ((map (make-sparse-keymap))
164         (i 33))
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)
181     (while (< i 127)
182       (define-key map (vector i) 'its-self-insert-char)
183       (setq i (1+ i)))
184     (define-key map " "    'its-kick-convert-region)
185     (define-key map "\177" 'its-delete-backward-SYL)
186     ;;
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)
199     map)
200   "Keymap for ITS mode.")
201
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")
205
206 (defun its-put-cursor (cursor)
207   (let ((p (point)))
208     (insert "!")
209     (add-text-properties p (point) (list 'local-map its-mode-map
210                                          'invisible t
211                                          'intangible 'its-part-2
212                                          'its-cursor cursor))
213     (goto-char p)))
214 ;;
215 ;;  +-- START property
216 ;;  |          --- CURSOR Property
217 ;;  |         /
218 ;;  v        v    v-- END Property
219 ;;  |SYL SYL ^ SYL|
220 ;;   ^^^ ^^^   ^^^------ SYL Property
221 ;;  <-------><---->
222 ;; intangible intangible
223 ;;     1       2
224 ;;
225 (defun its-start (key)
226   (let (p cursor)
227     (setq p (point))
228     (insert its-fence-open)
229     (add-text-properties p (point) 
230                          (let ((props '(its-start t intangible its-part-1)))
231                            (if its-fence-face
232                                (append '(invisible t) props)
233                              props)))
234     (setq p (point))
235     (setq cursor (its-input nil key))
236     (its-put-cursor cursor)
237     (forward-char 1)
238     (setq p (point))
239     (insert its-fence-close)
240     (add-text-properties p (point) 
241                          (let ((props '(its-end t intangible its-part-2)))
242                            (if its-fence-face
243                                (append '(invisible t) props)
244                              props)))
245     (forward-char -2)
246     (force-mode-line-update)))
247
248 (defun its-self-insert-char ()
249   (interactive)
250   (let ((key last-command-char)
251         (cursor (get-text-property (point) 'its-cursor))
252         (syl nil))
253     (if (null cursor)
254         (setq syl (get-text-property (1- (point)) 'its-syl)))
255     ;; delete cursor
256     (delete-region (point) (1+ (point)))
257     (setq cursor (its-input syl key))
258     (its-put-cursor cursor)))
259
260 (defvar its-current-map nil)
261 (make-variable-buffer-local 'its-current-map)
262 (put 'its-current-map 'permanent-local t)
263
264 (defun its-initial-ISYL ()
265   (its-get-start-state its-current-map))
266
267 (defun its-make-VSYL (keyseq)
268   (cons keyseq (length keyseq)))
269
270 ;; Return CURSOR
271 (defun its-input (syl key)
272   (if (null syl)
273       (setq syl (its-initial-ISYL)))
274   (let ((output (car syl))
275         (k/kk/s (cdr syl)))
276     (if (numberp k/kk/s)
277         ;; k/kk/s is "point in keyseq"
278         (its-input-to-vsyl syl key k/kk/s output)
279       ;; It's ISYL
280       (its-state-machine syl key 'its-buffer-ins/del-SYL))))
281
282 (defun its-input-to-vsyl (syl key point output)
283   (if (< key 0)
284       t
285     (let ((len (length output)))
286       (if (= len point)
287           ;; point is at end of VSYL.  Don't need to call state machine.
288           (progn
289             (its-buffer-ins/del-SYL
290              (its-make-VSYL (concat output (vector key))) syl)
291             nil)
292         ;; point is at middle of VSYL.
293         (let ((new-keyseq (concat (substring output 0 point)
294                                   (vector key)
295                                   (substring output point))))
296           (its-state-machine-keyseq new-keyseq 'its-buffer-ins/del-SYL))))))
297
298 (defvar its-barf-on-invalid-keyseq nil
299   "T means don't allow invalid key sequence in input buffer.")
300 \f
301 ;;;
302 ;;; ITS State Machine
303 ;;;
304
305 ;; Return CURSOR
306 (defun its-state-machine (state key emit)
307   (let ((next-state (its-get-next-state state key))
308         expr-output-back)
309     (if next-state
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))
317                     (back kst/t))
318                 (if back
319                     ;; It's negative integer which specifies how many
320                     ;; characters we go backwards
321                     (its-state-machine-keyseq (substring keyseq back)
322                                               emit (< key 0))
323                   'its-cursor))
324             ;; Still, it's a intermediate state.
325             nil))
326       (if (and (>= key 0)
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
334             ;; ISYL --> DSYL   XXX
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)
339               t)
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)
345               nil)))))))
346
347 (defvar its-latest-SYL nil
348   "The latest SYL inserted.")
349 (defsubst its-update-latest-SYL (syl)
350   (setq its-latest-SYL syl))
351
352 ;; Return CURSOR
353 (defun its-state-machine-keyseq (keyseq emit &optional eol)
354   (let ((i 0)
355         (len (length keyseq))
356         (its-barf-on-invalid-keyseq nil) ; temporally disable DING
357         (syl (its-initial-ISYL))
358         cursor)
359     (while (< i len)
360       (let ((key (aref keyseq i)))
361         (setq cursor 
362               (if (numberp (cdr syl))           ; VSYL
363                   (progn
364                     (funcall emit
365                              (its-make-VSYL (concat (car syl) (vector key)))
366                              syl)
367                     nil)
368                 (its-state-machine syl key emit)))
369         (setq i (1+ i))
370         (if cursor
371             (setq syl (its-initial-ISYL))
372           (setq syl its-latest-SYL))))
373     (if eol
374         (its-state-machine syl -1 emit)
375       cursor)))
376
377 (defun its-buffer-ins/del-SYL (newsyl oldsyl)
378   (its-buffer-delete-SYL oldsyl)
379   (its-update-latest-SYL newsyl)
380   (let ((p (point)))
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))
387     (if its-fence-face
388         (put-text-property p (point) 'face its-fence-face))))
389
390 (defun its-buffer-delete-SYL (syl)
391   (let ((len (length (its-get-output syl))))
392     (delete-region (- (point) len) (point))))
393
394 (defun its-get-next-state (state key)
395   (let ((kst/t (its-get-kst/t state)))
396     (cdr (assq key (car kst/t)))))
397
398 ;; XXX XXX XXX
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)))))
403
404 (defun its-get-otherwise (state key)
405   (let* ((kst/t (its-get-kst/t state))
406          (ebl (cdr kst/t))
407          expr-output-back)
408       (while ebl
409         (setq expr-output-back (car ebl))
410         (let ((expr (its-eob-keyexpr expr-output-back)))
411           (if (its-otherwise-match expr key)
412               (setq ebl nil)
413             (setq ebl (cdr ebl)))))
414       expr-output-back))
415 \f
416 ;;;
417 ;;; Name --> map
418 ;;;
419 ;;; ITS name: string
420
421 (defvar its-map-alist nil)
422
423 (defun its-get-map (name)
424   (assoc name its-map-alist))
425
426 (defun its-register-map (map)
427   (let* ((name (car map))
428          (place (assoc name its-map-alist)))
429     (if place
430         (setcdr place (cdr map))
431       (setq its-map-alist (cons map its-map-alist)))
432     map))
433
434 (defmacro define-its-state-machine (map name indicator lang doc &rest exprs)
435   `(progn
436      (eval-when (eval compile)
437        (let ((its-current-map (its-new-map ,name ,indicator ,lang)))
438          ,@exprs
439          (setq ,map its-current-map)))
440      (define-its-compiled-map ,map ,doc)))
441
442 (defmacro define-its-compiled-map (map doc)
443   `(defconst ,map ',(symbol-value map) ,doc))
444
445 (defmacro define-its-state-machine-append (map &rest exprs)
446   (append
447    `(let ((its-current-map ,map)))
448    exprs
449    (list `(setq ,map its-current-map))))
450
451 ;;
452 ;; Construct State Machine
453 ;;
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
459 Return last state."
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))))
465
466 (defun its-goto-state (input &optional initial-state build-if-none)
467   (let ((len (length input))
468         (i 0)
469         (state (or initial-state (its-get-start-state its-current-map))))
470     (while (< i len)
471       (setq state
472             (or (its-get-next-state state (aref input i))
473                 (if build-if-none
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)))
477             i (1+ i)))
478     state))
479
480 (defun its-defoutput (input display)
481   (let ((state (its-goto-state input)))
482     (its-set-output state display)))
483
484 (defun its-define-otherwise (state otherwise)
485   (let ((kst (its-get-kst/t state)))
486     (if kst
487         (setcdr kst (cons otherwise (cdr kst)))
488       (its-set-kst state (cons nil (cons otherwise nil))))))
489
490 (defconst its-otherwise-back-one
491   (its-make-class+back nil -1))
492
493 (defun its-defrule-otherwise (state output &optional class back)
494   (let (class+back)
495     (if (null 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))))
500
501 (defun its-defrule* (input output)
502   (let ((state (its-defrule input output)))
503     (its-defrule-otherwise state output)))
504
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)))
508     (if kst
509         (setcar kst (cons (cons key next-state) (car kst)))
510       (its-set-kst state (list (list (cons key next-state)))))
511     next-state))
512 \f
513 ;;;
514 (defun its-beginning-of-input-buffer ()
515   (interactive)
516   (its-input-end)
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)
521         (goto-char begpos)))
522   (its-put-cursor t))
523
524 (defun its-end-of-input-buffer ()
525   (interactive)
526   (its-input-end)
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)
531         (goto-char endpos)))
532   (its-put-cursor t))
533
534 ;; TODO: move in VSYL
535 (defun its-backward-SYL (n)
536   (interactive "p")
537   (its-input-end)
538   (let ((syl (get-text-property (1- (point)) 'its-syl))
539         (p (point))
540         (old-point (point)))
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))
544       (setq n (1- n)))
545     ;; Make SYLs have property of "part 2"
546     (put-text-property p old-point 'intangible 'its-part-2)
547     (goto-char p)
548     (its-put-cursor t)
549     (if (> n 0)
550         (signal 'beginning-of-buffer nil))))
551
552 ;; TODO: move in VSYL
553 (defun its-forward-SYL (n)
554   (interactive "p")
555   (its-input-end)
556   (let ((syl (get-text-property (point) 'its-syl))
557         (p (point))
558         (old-point (point)))
559     (while (and syl (> n 0))
560       (setq p (+ p (length (its-get-output syl))))
561       (setq syl (get-text-property p 'its-syl))
562       (setq n (1- n)))
563     ;; Make SYLs have property of "part 1"
564     (put-text-property p old-point 'intangible 'its-part-1)
565     (goto-char p)
566     (its-put-cursor t)
567     (if (> n 0)
568         (signal 'end-of-buffer nil))))
569
570 ;; TODO: handle VSYL.  KILLFLAG
571 (defun its-delete-SYL (n killflag)
572   (interactive "p\nP")
573   (its-input-end)
574   (let ((syl (get-text-property (point) 'its-syl))
575         (p (point)))
576     (while (and syl (> n 0))
577       (setq p (+ p (length (its-get-output syl))))
578       (setq syl (get-text-property p 'its-syl))
579       (setq n (1- n)))
580     (if (> n 0)
581         (progn
582           (its-put-cursor t)
583           (signal 'args-out-of-range (list p n)))
584       (delete-region (point) p)
585       ;; Check if empty
586       (let ((s (get-text-property (1- (point)) 'its-start))
587             (e (get-text-property (point) 'its-end)))
588         (if (and s e)
589             (its-exit-mode-internal)
590           (its-put-cursor t))))))
591
592 ;; TODO: killflag
593 (defun its-delete-backward-SYL (n killflag)
594   (interactive "p\nP")
595   (let ((syl (get-text-property (1- (point)) 'its-syl))
596         (cursor (get-text-property (point) 'its-cursor)))
597     (if (null syl)
598         (signal 'beginning-of-buffer nil)
599       (if (eq cursor t)
600           (its-delete-backward-SYL-internal n killflag)
601         (its-delete-backward-within-SYL syl n killflag)))))
602
603 ;; TODO: killflag
604 (defun its-delete-backward-SYL-internal (n killflag)
605   (let ((syl (get-text-property (1- (point)) 'its-syl))
606         (p (point)))
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))
610       (setq n (1- n)))
611     (if (> n 0)
612         (signal 'args-out-of-range (list p n))
613       (delete-region p (1+ (point)))    ; also delete cursor
614       ;; Check if empty
615       (let ((s (get-text-property (1- (point)) 'its-start))
616             (e (get-text-property (point) 'its-end)))
617         (if (and s e)
618             (its-exit-mode-internal)
619           (its-put-cursor t))))))
620
621 (defvar its-delete-by-keystroke nil)
622
623 ;; TODO: killflag
624 (defun its-delete-backward-within-SYL (syl n killflag)
625   (let* ((keyseq (its-get-keyseq-syl syl))
626          (len (length keyseq))
627          (p (point))
628          (its-current-map (get-text-property (1- (point)) 'its-map)))
629     (if (> n len)
630         (signal 'args-out-of-range (list p n)))
631     ;; Delete CURSOR
632     (delete-region p (1+ p))
633     (its-buffer-delete-SYL syl)
634     (if (= n len)
635         ;; Check if empty
636         (let ((s (get-text-property (1- (point)) 'its-start))
637               (e (get-text-property (point) 'its-end)))
638           (if (and s e)
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)))))
644
645 ;; XXX: NIY
646 (defun its-transpose-chars (n)
647   (interactive)
648   (let ((syl (get-text-property (1- (point)) 'its-syl))
649         (cursor (get-text-property (point) 'its-cursor)))
650     (if (null syl)
651         (signal 'beginning-of-buffer nil)
652       (if (eq cursor t)
653           (its-delete-backward-SYL-internal n nil)
654         (its-delete-backward-within-SYL syl 2 nil)))))
655
656 ;; Return VOID
657 (defun its-input-end ()
658   (let ((cursor (get-text-property (point) 'its-cursor)))
659     ;; key "END"
660     (if (null cursor)
661         (its-input (get-text-property (1- (point)) 'its-syl) -1))
662     (delete-region (point) (1+ (point)))))
663
664 (defun its-exit-mode ()
665   "Exit ITS mode."
666   (interactive)
667   (its-input-end)
668   (its-exit-mode-internal))
669
670 (defun its-exit-mode-off-input-method ()
671   "Exit ITS mode."
672   (interactive)
673   (its-input-end)
674   (its-exit-mode-internal)
675   (inactivate-input-method))
676
677 ;; TODO: handle overwrite-mode, insertion-hook, fill...
678 (defun its-exit-mode-internal (&optional proceed-to-conversion)
679   (let (start end)
680     ;; Delete open fence
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)
687         (setq end (point))
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
692                                         its-map nil
693                                         face nil
694                                         intangible nil))
695     (if proceed-to-conversion
696         (egg-convert-region start end)
697       (remove-text-properties start end '(its-lang nil))
698       (egg-do-auto-fill)
699       (run-hooks 'input-method-after-insert-chunk-hook))))
700
701 (defun its-kick-convert-region ()
702   (interactive)
703   (its-input-end)
704   (its-exit-mode-internal t))
705
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))))
709 \f
710 (defvar its-translation-result nil "")
711
712 (defun its-ins/del-SYL-batch (newsyl oldsyl)
713   (its-update-latest-SYL newsyl)
714   (if (and newsyl
715            (consp (cdr newsyl))
716            (not (its-kst-p (its-get-kst/t newsyl))))
717       ;; DSYL
718       (setq its-translation-result
719             (cons (its-get-output newsyl) its-translation-result))))
720
721 (defun its-translate-region (start end &optional map)
722   (interactive "r")
723   (setq its-translation-result nil)
724   (goto-char start)
725   (let ((i 0)
726         (syl (its-initial-ISYL))
727         ;; temporally enable DING
728         (its-barf-on-invalid-keyseq "Invalid Romaji Sequence")
729         cursor)
730     (while (< (point) end)
731       (let ((key (following-char)))
732         (setq cursor (its-state-machine syl key 'its-ins/del-SYL-batch))
733         (forward-char 1)
734         (if cursor
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))))
741 \f
742 (load "its-keydef.el")
743
744 (provide 'its)
745 ;;; its.el ends here.