Last update forgot its-map change of fence mode.
[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 (require 'its-keydef)
163
164 (defvar its-mode-map
165   (let ((map (make-sparse-keymap))
166         (i 33))
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)
183     (while (< i 127)
184       (define-key map (vector i) 'its-self-insert-char)
185       (setq i (1+ i)))
186     (define-key map " "    'its-kick-convert-region)
187     (define-key map "\177" 'its-delete-backward-SYL)
188     ;;
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)
196     map)
197   "Keymap for ITS mode.")
198
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")
202
203 (defconst its-setup-fence-before-insert-SYL nil)
204
205 (defun its-put-cursor (cursor)
206   (let ((p (point))
207         (map (copy-keymap its-mode-map)))
208     (its-define-select-keys map)
209     (insert "!")
210     (add-text-properties p (point) (list 'local-map map
211                                          'invisible t
212                                          'intangible 'its-part-2
213                                          'its-cursor cursor))
214     (goto-char p)))
215
216 (defsubst its-set-cursor-status (cursor)
217   (put-text-property (point) (1+ (point)) 'its-cursor cursor)
218   cursor)
219
220 ;;
221 ;;  +-- START property
222 ;;  |          --- CURSOR Property
223 ;;  |         /
224 ;;  v        v    v-- END Property
225 ;;  |SYL SYL ^ SYL|
226 ;;   ^^^ ^^^   ^^^------ SYL Property
227 ;;  <-------><---->
228 ;; intangible intangible
229 ;;     1       2
230 ;;
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))
234         (p (point)) p1)
235     (insert its-fence-open)
236     (setq p1 (point))
237     (add-text-properties p p1 open-props)
238     (insert its-fence-close)
239     (add-text-properties p1 (point) close-props)
240     (if its-fence-face
241         (put-text-property 'invisible t p (point)))
242     (goto-char p1)
243     (its-put-cursor t)))
244
245 (defun its-start (key)
246   (let ((its-setup-fence-before-insert-SYL t))
247     (its-input nil key)
248     (force-mode-line-update)))
249
250 (defun its-restart (str)
251   (let (p)
252     (its-setup-fence-mode t)
253     (setq p (point))
254     (insert str)
255     (its-beginning-of-input-buffer)))
256
257 (defun its-self-insert-char ()
258   (interactive)
259   (let ((key last-command-char)
260         (syl nil))
261     (if (null (get-text-property (point) 'its-cursor))
262         (setq syl (get-text-property (1- (point)) 'its-syl)))
263     (its-input syl key)))
264
265 (defvar its-current-map nil)
266 (make-variable-buffer-local 'its-current-map)
267 (put 'its-current-map 'permanent-local t)
268
269 (defun its-initial-ISYL ()
270   (its-get-start-state its-current-map))
271
272 (defun its-make-VSYL (keyseq)
273   (cons keyseq (length keyseq)))
274
275 ;; Return CURSOR
276 (defun its-input (syl key)
277   (if (null syl)
278       (setq syl (its-initial-ISYL)))
279   (let ((output (car syl))
280         (k/kk/s (cdr syl)))
281     (if (numberp k/kk/s)
282         ;; k/kk/s is "point in keyseq"
283         (its-input-to-vsyl syl key k/kk/s output)
284       ;; It's ISYL
285       (its-state-machine syl key 'its-buffer-ins/del-SYL))))
286
287 (defun its-input-to-vsyl (syl key point output)
288   (if (< key 0)
289       t
290     (let ((len (length output)))
291       (if (= len point)
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)
297                                   (vector key)
298                                   (substring output point))))
299           (its-state-machine-keyseq new-keyseq 'its-buffer-ins/del-SYL))))))
300
301 (defvar its-barf-on-invalid-keyseq nil
302   "T means don't allow invalid key sequence in input buffer.")
303
304 (defun its-input-error ()
305   (error "Invalid Romaji Sequence"))
306
307 \f
308 ;;;
309 ;;; ITS State Machine
310 ;;;
311
312 ;; Return CURSOR
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)
316     (cond
317      ;; proceed to next status
318      (next-state
319       (setq kst/t (its-get-kst/t next-state)
320             output (its-get-output next-state)
321             keyseq (its-get-keyseq next-state))
322       (cond
323        ;; Still, it's a intermediate state.
324        ((its-kst-p kst/t)
325         (funcall emit next-state state nil))
326
327        ;; It's negative integer which specifies how many
328        ;; characters we go backwards
329        (kst/t
330         (funcall emit next-state state 'its-cursor)
331         (its-state-machine-keyseq (substring keyseq kst/t) emit (< key 0)))
332
333         ;; Here we arrive to a terminal state.
334         ;; Emit a DSYL, and go ahead.
335        (t
336         (funcall emit next-state state 'its-cursor))))
337
338      ;; push back by otherwise status
339      ((and (>= key 0)
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))
345
346      ;; No next state for KEY.  It's invalid sequence.
347      (its-barf-on-invalid-keyseq
348       (its-input-error))
349
350      ;; no next state for END of keystroke
351      ((< key 0)
352       ;; ISYL --> DSYL   XXX
353       (funcall emit (cons (car state)
354                           (list (its-get-keyseq state))) state t))
355      (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)))))
359
360 (defvar its-latest-SYL nil
361   "The latest SYL inserted.")
362 (defsubst its-update-latest-SYL (syl)
363   (setq its-latest-SYL syl))
364
365 ;; Return CURSOR
366 (defun its-state-machine-keyseq (keyseq emit &optional eol)
367   (let ((i 0)
368         (len (length keyseq))
369         (syl (its-initial-ISYL))
370         cursor)
371     (while (< i len)
372       (cond
373        ((numberp (cdr syl))
374         ;; VSYL - no need looping
375         (funcall emit (its-make-VSYL (concat (car syl) keyseq)) syl nil)
376         (setq cursor nil
377               i len))
378        (t
379         (setq cursor (its-state-machine syl (aref keyseq i) emit))))
380       (setq syl (if cursor (its-initial-ISYL) its-latest-SYL)
381             i (1+ i)))
382     (if eol
383         (its-state-machine syl -1 emit)
384       cursor)))
385
386 (defun its-buffer-ins/del-SYL (newsyl oldsyl cursor)
387   (if its-setup-fence-before-insert-SYL
388       (progn
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)
393   (let ((p (point)))
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))
400     (if its-fence-face
401         (put-text-property p (point) 'face its-fence-face))
402     (its-set-cursor-status cursor)))
403
404 (defun its-buffer-delete-SYL (syl)
405   (let ((len (length (its-get-output syl))))
406     (delete-region (- (point) len) (point))))
407
408 (defun its-get-next-state (state key)
409   (let ((kst/t (its-get-kst/t state)))
410     (cdr (assq key (car kst/t)))))
411
412 ;; XXX XXX XXX
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)))))
417
418 (defun its-get-otherwise (state key)
419   (let* ((kst/t (its-get-kst/t state))
420          (ebl (cdr kst/t))
421          expr-output-back)
422       (while ebl
423         (setq expr-output-back (car ebl))
424         (let ((expr (its-eob-keyexpr expr-output-back)))
425           (if (its-otherwise-match expr key)
426               (setq ebl nil)
427             (setq ebl (cdr ebl)))))
428       expr-output-back))
429 \f
430 ;;;
431 ;;; Name --> map
432 ;;;
433 ;;; ITS name: string
434
435 (defvar its-map-alist nil)
436
437 (defun its-get-map (name)
438   (assoc name its-map-alist))
439
440 (defun its-register-map (map)
441   (let* ((name (car map))
442          (place (assoc name its-map-alist)))
443     (if place
444         (setcdr place (cdr map))
445       (setq its-map-alist (cons map its-map-alist)))
446     map))
447
448 (defmacro define-its-state-machine (map name indicator lang doc &rest exprs)
449   `(progn
450      (eval-when (eval compile)
451        (let ((its-current-map (its-new-map ,name ,indicator ,lang)))
452          ,@exprs
453          (setq ,map its-current-map)))
454      (define-its-compiled-map ,map ,doc)))
455
456 (defmacro define-its-compiled-map (map doc)
457   `(defconst ,map ',(symbol-value map) ,doc))
458
459 (defmacro define-its-state-machine-append (map &rest exprs)
460   (append
461    `(let ((its-current-map ,map)))
462    exprs
463    (list `(setq ,map its-current-map))))
464
465 ;;
466 ;; Construct State Machine
467 ;;
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
473 Return last state."
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))))
479
480 (defun its-goto-state (input &optional initial-state build-if-none)
481   (let ((len (length input))
482         (i 0)
483         (state (or initial-state (its-get-start-state its-current-map))))
484     (while (< i len)
485       (setq state
486             (or (its-get-next-state state (aref input i))
487                 (if build-if-none
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)))
491             i (1+ i)))
492     state))
493
494 (defun its-defoutput (input display)
495   (let ((state (its-goto-state input)))
496     (its-set-output state display)))
497
498 (defun its-define-otherwise (state otherwise)
499   (let ((kst (its-get-kst/t state)))
500     (if kst
501         (setcdr kst (cons otherwise (cdr kst)))
502       (its-set-kst state (cons nil (cons otherwise nil))))))
503
504 (defconst its-otherwise-back-one
505   (its-make-class+back nil -1))
506
507 (defun its-defrule-otherwise (state output &optional class back)
508   (let (class+back)
509     (if (null 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))))
514
515 (defun its-defrule* (input output)
516   (let ((state (its-defrule input output)))
517     (its-defrule-otherwise state output)))
518
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)))
522     (if kst
523         (setcar kst (cons (cons key next-state) (car kst)))
524       (its-set-kst state (list (list (cons key next-state)))))
525     next-state))
526 \f
527 ;;;
528 (defun its-beginning-of-input-buffer ()
529   (interactive)
530   (its-input-end)
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)
535         (goto-char begpos)))
536   (its-put-cursor t))
537
538 (defun its-end-of-input-buffer ()
539   (interactive)
540   (its-input-end)
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)
545         (goto-char endpos)))
546   (its-put-cursor t))
547
548 ;; TODO: move in VSYL
549 (defun its-backward-SYL (n)
550   (interactive "p")
551   (its-input-end)
552   (let ((syl (get-text-property (1- (point)) 'its-syl))
553         (p (point))
554         (old-point (point)))
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))
558       (setq n (1- n)))
559     ;; Make SYLs have property of "part 2"
560     (put-text-property p old-point 'intangible 'its-part-2)
561     (goto-char p)
562     (its-put-cursor t)
563     (if (> n 0)
564         (signal 'beginning-of-buffer nil))))
565
566 ;; TODO: move in VSYL
567 (defun its-forward-SYL (n)
568   (interactive "p")
569   (its-input-end)
570   (let ((syl (get-text-property (point) 'its-syl))
571         (p (point))
572         (old-point (point)))
573     (while (and syl (> n 0))
574       (setq p (+ p (length (its-get-output syl))))
575       (setq syl (get-text-property p 'its-syl))
576       (setq n (1- n)))
577     ;; Make SYLs have property of "part 1"
578     (put-text-property p old-point 'intangible 'its-part-1)
579     (goto-char p)
580     (its-put-cursor t)
581     (if (> n 0)
582         (signal 'end-of-buffer nil))))
583
584 ;; TODO: handle VSYL.  KILLFLAG
585 (defun its-delete-SYL (n killflag)
586   (interactive "p\nP")
587   (its-input-end)
588   (let ((syl (get-text-property (point) 'its-syl))
589         (p (point)))
590     (while (and syl (> n 0))
591       (setq p (+ p (length (its-get-output syl))))
592       (setq syl (get-text-property p 'its-syl))
593       (setq n (1- n)))
594     (if (> n 0)
595         (progn
596           (its-put-cursor t)
597           (signal 'args-out-of-range (list p n)))
598       (delete-region (point) p)
599       ;; Check if empty
600       (let ((s (get-text-property (1- (point)) 'its-start))
601             (e (get-text-property (point) 'its-end)))
602         (if (and s e)
603             (its-exit-mode-internal)
604           (its-put-cursor t))))))
605
606 ;; TODO: killflag
607 (defun its-delete-backward-SYL (n killflag)
608   (interactive "p\nP")
609   (let ((syl (get-text-property (1- (point)) 'its-syl))
610         (cursor (get-text-property (point) 'its-cursor)))
611     (if (null syl)
612         (signal 'beginning-of-buffer nil)
613       (if (eq cursor t)
614           (its-delete-backward-SYL-internal n killflag)
615         (its-delete-backward-within-SYL syl n killflag)))))
616
617 ;; TODO: killflag
618 (defun its-delete-backward-SYL-internal (n killflag)
619   (let ((syl (get-text-property (1- (point)) 'its-syl))
620         (p (point)))
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))
624       (setq n (1- n)))
625     (if (> n 0)
626         (signal 'args-out-of-range (list p n))
627       (delete-region p (1+ (point)))    ; also delete cursor
628       ;; Check if empty
629       (let ((s (get-text-property (1- (point)) 'its-start))
630             (e (get-text-property (point) 'its-end)))
631         (if (and s e)
632             (its-exit-mode-internal)
633           (its-put-cursor t))))))
634
635 (defvar its-delete-by-keystroke nil)
636
637 ;; TODO: killflag
638 (defun its-delete-backward-within-SYL (syl n killflag)
639   (let* ((keyseq (its-get-keyseq-syl syl))
640          (len (length keyseq))
641          (p (point))
642          (its-current-map (get-text-property (1- (point)) 'its-map)))
643     (if (> n len)
644         (signal 'args-out-of-range (list p n)))
645     ;; Delete CURSOR
646     (delete-region p (1+ p))
647     (its-buffer-delete-SYL syl)
648     (if (= n len)
649         ;; Check if empty
650         (let ((s (get-text-property (1- (point)) 'its-start))
651               (e (get-text-property (point) 'its-end)))
652           (if (and s e)
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)))))
658
659 ;; XXX: NIY
660 (defun its-transpose-chars (n)
661   (interactive)
662   (let ((syl (get-text-property (1- (point)) 'its-syl))
663         (cursor (get-text-property (point) 'its-cursor)))
664     (if (null syl)
665         (signal 'beginning-of-buffer nil)
666       (if (eq cursor t)
667           (its-delete-backward-SYL-internal n nil)
668         (its-delete-backward-within-SYL syl 2 nil)))))
669
670 ;; Return VOID
671 (defun its-input-end ()
672   (let ((cursor (get-text-property (point) 'its-cursor)))
673     ;; key "END"
674     (if (null cursor)
675         (its-input (get-text-property (1- (point)) 'its-syl) -1))
676     (delete-region (point) (1+ (point)))))
677
678 (defun its-exit-mode ()
679   "Exit ITS mode."
680   (interactive)
681   (its-input-end)
682   (its-exit-mode-internal))
683
684 (defun its-exit-mode-off-input-method ()
685   "Exit ITS mode."
686   (interactive)
687   (its-input-end)
688   (its-exit-mode-internal)
689   (inactivate-input-method))
690
691 ;; TODO: handle overwrite-mode, insertion-hook, fill...
692 (defun its-exit-mode-internal (&optional proceed-to-conversion)
693   (let (start end)
694     ;; Delete open fence
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)
701         (setq end (point))
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
706                                         face nil
707                                         intangible nil))
708     (if proceed-to-conversion
709         (egg-convert-region start end)
710       (remove-text-properties start end '(its-lang nil its-syl nil))
711       (egg-do-auto-fill)
712       (run-hooks 'input-method-after-insert-chunk-hook))))
713
714 (defun its-kick-convert-region ()
715   (interactive)
716   (its-input-end)
717   (its-exit-mode-internal t))
718
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))))
722 \f
723 (defvar its-translation-result "" "")
724
725 (defun its-ins/del-SYL-batch (newsyl oldsyl cursor)
726   (its-update-latest-SYL newsyl)
727   (if (and newsyl
728            (consp (cdr newsyl))
729            (not (its-kst-p (its-get-kst/t newsyl))))
730       ;; DSYL
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)))
737   cursor)
738
739 (defun its-translate-region (start end)
740   (interactive "r")
741   (its-translate-region-internal start end)
742   (remove-text-properties start (point) '(its-lang nil)))
743
744 (defun its-translate-region-internal (start end)
745   (setq its-translation-result "")
746   (goto-char start)
747   (let ((i 0)
748         (syl (its-initial-ISYL))
749         ;; temporally enable DING
750         (its-barf-on-invalid-keyseq t)
751         cursor)
752     (while (< (point) end)
753       (let ((key (following-char)))
754         (setq cursor (its-state-machine syl key 'its-ins/del-SYL-batch))
755         (forward-char 1)
756         (if cursor
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)))
763 \f
764 (provide 'its)
765 ;;; its.el ends here.