remove temporary its-barf-on-invalid-keyseq binding.
[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-insert-fence-open ()
226   (let ((p (point)))
227     (insert its-fence-open)
228     (add-text-properties p (point)
229                          (if its-fence-face
230                              '(invisible t its-start t intangible its-part-1)
231                            '(its-start t intangible its-part-1)))))
232
233 (defun its-insert-fence-close ()
234   (let ((p (point)))
235     (insert its-fence-close)
236     (add-text-properties p (point) 
237                          (if its-fence-face
238                              '(invisible t its-end t intangible its-part-2)
239                            '(its-end t intangible its-part-2)))
240     (goto-char p)))
241
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))
247
248 (defun its-restart (str)
249   (let (p)
250     (its-insert-fence-open)
251     (its-insert-fence-close)
252     (setq p (point))
253     (insert str)
254     (put-text-property p (point) 'intangible 'its-part-2)
255     (goto-char p)
256     (its-put-cursor t)))
257
258 (defun its-self-insert-char ()
259   (interactive)
260   (let ((key last-command-char)
261         (cursor (get-text-property (point) 'its-cursor))
262         (syl nil))
263     (if (null cursor)
264         (setq syl (get-text-property (1- (point)) 'its-syl)))
265     ;; delete cursor
266     (delete-region (point) (1+ (point)))
267     (setq cursor (its-input syl key))
268     (its-put-cursor cursor)))
269
270 (defvar its-current-map nil)
271 (make-variable-buffer-local 'its-current-map)
272 (put 'its-current-map 'permanent-local t)
273
274 (defun its-initial-ISYL ()
275   (its-get-start-state its-current-map))
276
277 (defun its-make-VSYL (keyseq)
278   (cons keyseq (length keyseq)))
279
280 ;; Return CURSOR
281 (defun its-input (syl key)
282   (if (null syl)
283       (setq syl (its-initial-ISYL)))
284   (let ((output (car syl))
285         (k/kk/s (cdr syl)))
286     (if (numberp k/kk/s)
287         ;; k/kk/s is "point in keyseq"
288         (its-input-to-vsyl syl key k/kk/s output)
289       ;; It's ISYL
290       (its-state-machine syl key 'its-buffer-ins/del-SYL))))
291
292 (defun its-input-to-vsyl (syl key point output)
293   (if (< key 0)
294       t
295     (let ((len (length output)))
296       (if (= len point)
297           ;; point is at end of VSYL.  Don't need to call state machine.
298           (progn
299             (its-buffer-ins/del-SYL
300              (its-make-VSYL (concat output (vector key))) syl)
301             nil)
302         ;; point is at middle of VSYL.
303         (let ((new-keyseq (concat (substring output 0 point)
304                                   (vector key)
305                                   (substring output point))))
306           (its-state-machine-keyseq new-keyseq 'its-buffer-ins/del-SYL))))))
307
308 (defvar its-barf-on-invalid-keyseq nil
309   "T means don't allow invalid key sequence in input buffer.")
310 \f
311 ;;;
312 ;;; ITS State Machine
313 ;;;
314
315 ;; Return CURSOR
316 (defun its-state-machine (state key emit)
317   (let ((next-state (its-get-next-state state key))
318         expr-output-back)
319     (if next-state
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))
327                     (back kst/t))
328                 (if back
329                     ;; It's negative integer which specifies how many
330                     ;; characters we go backwards
331                     (its-state-machine-keyseq (substring keyseq back)
332                                               emit (< key 0))
333                   'its-cursor))
334             ;; Still, it's a intermediate state.
335             nil))
336       (if (and (>= key 0)
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
344             ;; ISYL --> DSYL   XXX
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)
349               t)
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)
355               nil)))))))
356
357 (defvar its-latest-SYL nil
358   "The latest SYL inserted.")
359 (defsubst its-update-latest-SYL (syl)
360   (setq its-latest-SYL syl))
361
362 ;; Return CURSOR
363 (defun its-state-machine-keyseq (keyseq emit &optional eol)
364   (let ((i 0)
365         (len (length keyseq))
366         (syl (its-initial-ISYL))
367         cursor)
368     (while (< i len)
369       (let ((key (aref keyseq i)))
370         (setq cursor 
371               (if (numberp (cdr syl))           ; VSYL
372                   (progn
373                     (funcall emit
374                              (its-make-VSYL (concat (car syl) (vector key)))
375                              syl)
376                     nil)
377                 (its-state-machine syl key emit)))
378         (setq i (1+ i))
379         (if cursor
380             (setq syl (its-initial-ISYL))
381           (setq syl its-latest-SYL))))
382     (if eol
383         (its-state-machine syl -1 emit)
384       cursor)))
385
386 (defun its-buffer-ins/del-SYL (newsyl oldsyl)
387   (its-buffer-delete-SYL oldsyl)
388   (its-update-latest-SYL newsyl)
389   (let ((p (point)))
390     (insert (its-get-output newsyl))
391     (add-text-properties p (point)
392                          (list 'its-syl newsyl
393                                'its-map its-current-map
394                                'its-lang its-current-language
395                                'intangible 'its-part-1))
396     (if its-fence-face
397         (put-text-property p (point) 'face its-fence-face))))
398
399 (defun its-buffer-delete-SYL (syl)
400   (let ((len (length (its-get-output syl))))
401     (delete-region (- (point) len) (point))))
402
403 (defun its-get-next-state (state key)
404   (let ((kst/t (its-get-kst/t state)))
405     (cdr (assq key (car kst/t)))))
406
407 ;; XXX XXX XXX
408 (defun its-otherwise-match (expr key)
409   (or (null expr)                       ; <expr>::= NIL means "ANY"
410       (let ((case-fold-search nil))
411         (string-match expr (char-to-string key)))))
412
413 (defun its-get-otherwise (state key)
414   (let* ((kst/t (its-get-kst/t state))
415          (ebl (cdr kst/t))
416          expr-output-back)
417       (while ebl
418         (setq expr-output-back (car ebl))
419         (let ((expr (its-eob-keyexpr expr-output-back)))
420           (if (its-otherwise-match expr key)
421               (setq ebl nil)
422             (setq ebl (cdr ebl)))))
423       expr-output-back))
424 \f
425 ;;;
426 ;;; Name --> map
427 ;;;
428 ;;; ITS name: string
429
430 (defvar its-map-alist nil)
431
432 (defun its-get-map (name)
433   (assoc name its-map-alist))
434
435 (defun its-register-map (map)
436   (let* ((name (car map))
437          (place (assoc name its-map-alist)))
438     (if place
439         (setcdr place (cdr map))
440       (setq its-map-alist (cons map its-map-alist)))
441     map))
442
443 (defmacro define-its-state-machine (map name indicator lang doc &rest exprs)
444   `(progn
445      (eval-when (eval compile)
446        (let ((its-current-map (its-new-map ,name ,indicator ,lang)))
447          ,@exprs
448          (setq ,map its-current-map)))
449      (define-its-compiled-map ,map ,doc)))
450
451 (defmacro define-its-compiled-map (map doc)
452   `(defconst ,map ',(symbol-value map) ,doc))
453
454 (defmacro define-its-state-machine-append (map &rest exprs)
455   (append
456    `(let ((its-current-map ,map)))
457    exprs
458    (list `(setq ,map its-current-map))))
459
460 ;;
461 ;; Construct State Machine
462 ;;
463 (defun its-defrule (input output &optional back enable-overwrite)
464   "\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
465 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
466 \e$BLa$C$FF0$/$b$N$H$9$k!#JQ495,B'$O$b$C$H$b:G6a$K\e(B its-define-state-machine
467 \e$B$5$l$?JQ49I=$KEPO?$5$l$k!#\e(B
468 Return last state."
469   (let ((state (its-goto-state (substring input 0 -1) nil t))
470         (key (aref input (1- (length input)))))
471     (if (and (its-get-next-state state key) (not enable-overwrite))
472         (error "Duplicated definition (%s)" input)
473       (its-make-next-state state key input output back))))
474
475 (defun its-goto-state (input &optional initial-state build-if-none)
476   (let ((len (length input))
477         (i 0)
478         (state (or initial-state (its-get-start-state its-current-map))))
479     (while (< i len)
480       (setq state
481             (or (its-get-next-state state (aref input i))
482                 (if build-if-none
483                     (let ((keyseq (substring input 0 (1+ i))))
484                       (its-make-next-state state (aref input i) keyseq keyseq))
485                    (error "No such state (%s)" input)))
486             i (1+ i)))
487     state))
488
489 (defun its-defoutput (input display)
490   (let ((state (its-goto-state input)))
491     (its-set-output state display)))
492
493 (defun its-define-otherwise (state otherwise)
494   (let ((kst (its-get-kst/t state)))
495     (if kst
496         (setcdr kst (cons otherwise (cdr kst)))
497       (its-set-kst state (cons nil (cons otherwise nil))))))
498
499 (defconst its-otherwise-back-one
500   (its-make-class+back nil -1))
501
502 (defun its-defrule-otherwise (state output &optional class back)
503   (let (class+back)
504     (if (null back)
505         (setq class+back its-otherwise-back-one)
506       (setq class+back (its-make-class+back class back)))
507     (its-define-otherwise state
508                           (its-make-otherwise output class+back))))
509
510 (defun its-defrule* (input output)
511   (let ((state (its-defrule input output)))
512     (its-defrule-otherwise state output)))
513
514 (defun its-make-next-state (state key keyseq output &optional back)
515   (let ((next-state (its-new-state output keyseq back))
516         (kst (its-get-kst/t state)))
517     (if kst
518         (setcar kst (cons (cons key next-state) (car kst)))
519       (its-set-kst state (list (list (cons key next-state)))))
520     next-state))
521 \f
522 ;;;
523 (defun its-beginning-of-input-buffer ()
524   (interactive)
525   (its-input-end)
526   (if (not (get-text-property (1- (point)) 'its-start))
527       (let ((begpos (previous-single-property-change (point) 'its-start)))
528         ;; Make SYLs have property of "part 2"
529         (put-text-property begpos (point) 'intangible 'its-part-2)
530         (goto-char begpos)))
531   (its-put-cursor t))
532
533 (defun its-end-of-input-buffer ()
534   (interactive)
535   (its-input-end)
536   (if (not (get-text-property (point) 'its-end))
537       (let ((endpos (next-single-property-change (point) 'its-end)))
538         ;; Make SYLs have property of "part 1"
539         (put-text-property (point) endpos 'intangible 'its-part-1)
540         (goto-char endpos)))
541   (its-put-cursor t))
542
543 ;; TODO: move in VSYL
544 (defun its-backward-SYL (n)
545   (interactive "p")
546   (its-input-end)
547   (let ((syl (get-text-property (1- (point)) 'its-syl))
548         (p (point))
549         (old-point (point)))
550     (while (and syl (> n 0))
551       (setq p (- p (length (its-get-output syl))))
552       (setq syl (get-text-property (1- p) 'its-syl))
553       (setq n (1- n)))
554     ;; Make SYLs have property of "part 2"
555     (put-text-property p old-point 'intangible 'its-part-2)
556     (goto-char p)
557     (its-put-cursor t)
558     (if (> n 0)
559         (signal 'beginning-of-buffer nil))))
560
561 ;; TODO: move in VSYL
562 (defun its-forward-SYL (n)
563   (interactive "p")
564   (its-input-end)
565   (let ((syl (get-text-property (point) 'its-syl))
566         (p (point))
567         (old-point (point)))
568     (while (and syl (> n 0))
569       (setq p (+ p (length (its-get-output syl))))
570       (setq syl (get-text-property p 'its-syl))
571       (setq n (1- n)))
572     ;; Make SYLs have property of "part 1"
573     (put-text-property p old-point 'intangible 'its-part-1)
574     (goto-char p)
575     (its-put-cursor t)
576     (if (> n 0)
577         (signal 'end-of-buffer nil))))
578
579 ;; TODO: handle VSYL.  KILLFLAG
580 (defun its-delete-SYL (n killflag)
581   (interactive "p\nP")
582   (its-input-end)
583   (let ((syl (get-text-property (point) 'its-syl))
584         (p (point)))
585     (while (and syl (> n 0))
586       (setq p (+ p (length (its-get-output syl))))
587       (setq syl (get-text-property p 'its-syl))
588       (setq n (1- n)))
589     (if (> n 0)
590         (progn
591           (its-put-cursor t)
592           (signal 'args-out-of-range (list p n)))
593       (delete-region (point) p)
594       ;; Check if empty
595       (let ((s (get-text-property (1- (point)) 'its-start))
596             (e (get-text-property (point) 'its-end)))
597         (if (and s e)
598             (its-exit-mode-internal)
599           (its-put-cursor t))))))
600
601 ;; TODO: killflag
602 (defun its-delete-backward-SYL (n killflag)
603   (interactive "p\nP")
604   (let ((syl (get-text-property (1- (point)) 'its-syl))
605         (cursor (get-text-property (point) 'its-cursor)))
606     (if (null syl)
607         (signal 'beginning-of-buffer nil)
608       (if (eq cursor t)
609           (its-delete-backward-SYL-internal n killflag)
610         (its-delete-backward-within-SYL syl n killflag)))))
611
612 ;; TODO: killflag
613 (defun its-delete-backward-SYL-internal (n killflag)
614   (let ((syl (get-text-property (1- (point)) 'its-syl))
615         (p (point)))
616     (while (and syl (> n 0))
617       (setq p (- p (length (its-get-output syl))))
618       (setq syl (get-text-property (1- p) 'its-syl))
619       (setq n (1- n)))
620     (if (> n 0)
621         (signal 'args-out-of-range (list p n))
622       (delete-region p (1+ (point)))    ; also delete cursor
623       ;; Check if empty
624       (let ((s (get-text-property (1- (point)) 'its-start))
625             (e (get-text-property (point) 'its-end)))
626         (if (and s e)
627             (its-exit-mode-internal)
628           (its-put-cursor t))))))
629
630 (defvar its-delete-by-keystroke nil)
631
632 ;; TODO: killflag
633 (defun its-delete-backward-within-SYL (syl n killflag)
634   (let* ((keyseq (its-get-keyseq-syl syl))
635          (len (length keyseq))
636          (p (point))
637          (its-current-map (get-text-property (1- (point)) 'its-map)))
638     (if (> n len)
639         (signal 'args-out-of-range (list p n)))
640     ;; Delete CURSOR
641     (delete-region p (1+ p))
642     (its-buffer-delete-SYL syl)
643     (if (= n len)
644         ;; Check if empty
645         (let ((s (get-text-property (1- (point)) 'its-start))
646               (e (get-text-property (point) 'its-end)))
647           (if (and s e)
648               (its-exit-mode-internal)
649             (its-put-cursor (not its-delete-by-keystroke))))
650       (setq keyseq (substring keyseq 0 (- len n)))
651       (let ((r (its-state-machine-keyseq keyseq 'its-buffer-ins/del-SYL)))
652         (its-put-cursor r)))))
653
654 ;; XXX: NIY
655 (defun its-transpose-chars (n)
656   (interactive)
657   (let ((syl (get-text-property (1- (point)) 'its-syl))
658         (cursor (get-text-property (point) 'its-cursor)))
659     (if (null syl)
660         (signal 'beginning-of-buffer nil)
661       (if (eq cursor t)
662           (its-delete-backward-SYL-internal n nil)
663         (its-delete-backward-within-SYL syl 2 nil)))))
664
665 ;; Return VOID
666 (defun its-input-end ()
667   (let ((cursor (get-text-property (point) 'its-cursor)))
668     ;; key "END"
669     (if (null cursor)
670         (its-input (get-text-property (1- (point)) 'its-syl) -1))
671     (delete-region (point) (1+ (point)))))
672
673 (defun its-exit-mode ()
674   "Exit ITS mode."
675   (interactive)
676   (its-input-end)
677   (its-exit-mode-internal))
678
679 (defun its-exit-mode-off-input-method ()
680   "Exit ITS mode."
681   (interactive)
682   (its-input-end)
683   (its-exit-mode-internal)
684   (inactivate-input-method))
685
686 ;; TODO: handle overwrite-mode, insertion-hook, fill...
687 (defun its-exit-mode-internal (&optional proceed-to-conversion)
688   (let (start end)
689     ;; Delete open fence
690     (if (get-text-property (1- (point)) 'its-start)
691         (setq start (1- (point)))
692       (setq start (1- (previous-single-property-change (point) 'its-start))))
693     (delete-region start (1+ start))
694     ;; Delete close fence
695     (if (get-text-property (point) 'its-end)
696         (setq end (point))
697       (setq end (next-single-property-change (point) 'its-end)))
698     (delete-region end (1+ end))
699     ;; Remove all properties added by ITS
700     (remove-text-properties start end '(its-map nil
701                                         face nil
702                                         intangible nil))
703     (if proceed-to-conversion
704         (egg-convert-region start end)
705       (remove-text-properties start end '(its-lang nil its-syl nil))
706       (egg-do-auto-fill)
707       (run-hooks 'input-method-after-insert-chunk-hook))))
708
709 (defun its-kick-convert-region ()
710   (interactive)
711   (its-input-end)
712   (its-exit-mode-internal t))
713
714 (defun its-in-fence-p ()
715   (let ((prop (get-text-property (point) 'intangible)))
716     (or (eq prop 'its-part-1) (eq prop 'its-part-2))))
717 \f
718 (defvar its-translation-result "" "")
719
720 (defun its-ins/del-SYL-batch (newsyl oldsyl)
721   (its-update-latest-SYL newsyl)
722   (if (and newsyl
723            (consp (cdr newsyl))
724            (not (its-kst-p (its-get-kst/t newsyl))))
725       ;; DSYL
726       (let ((output (its-get-output newsyl))
727             (oldlen (length its-translation-result)))
728         (setq its-translation-result (concat its-translation-result output))
729         (put-text-property oldlen (length its-translation-result)
730                            'its-lang its-current-language
731                            its-translation-result))))
732
733 (defun its-translate-region (start end)
734   (interactive "r")
735   (its-translate-region-internal start end)
736   (remove-text-properties start (point) '(its-lang nil)))
737
738 (defun its-translate-region-internal (start end)
739   (setq its-translation-result "")
740   (goto-char start)
741   (let ((i 0)
742         (syl (its-initial-ISYL))
743         ;; temporally enable DING
744         (its-barf-on-invalid-keyseq "Invalid Romaji Sequence")
745         cursor)
746     (while (< (point) end)
747       (let ((key (following-char)))
748         (setq cursor (its-state-machine syl key 'its-ins/del-SYL-batch))
749         (forward-char 1)
750         (if cursor
751             (setq syl (its-initial-ISYL))
752           (setq syl its-latest-SYL))))
753     (if (eq syl its-latest-SYL)
754         (its-state-machine syl -1 'its-ins/del-SYL-batch))
755     (delete-region start end)
756     (insert its-translation-result)))
757 \f
758 (require 'its-keydef)
759
760 (provide 'its)
761 ;;; its.el ends here.