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-map nil)
36 (make-variable-buffer-local 'its-current-map)
37 (put 'its-current-map 'permanent-local t)
39 (defvar its-current-select-func nil)
40 (make-variable-buffer-local 'its-current-select-func)
41 (put 'its-current-select-func 'permanent-local t)
43 (defvar its-previous-select-func nil)
44 (make-variable-buffer-local 'its-previous-select-func)
45 (put 'its-previous-select-func 'permanent-local t)
47 (defvar its-current-language)
48 (make-variable-buffer-local 'its-current-language)
49 (put 'its-current-language 'permanent-local t)
51 ;; Data structure in ITS
54 ;; "SYL" stands for something like a syllable.
56 ;; <SYL> ::= ( <output> . ( <keyseq> . <terminal> )) ; Determined: DSYL
57 ;; | <state> ; Intermediate: ISYL
58 ;; | ( <output> . <point> ) ; Verbatim: VSYL
62 ;; ; ( <output> . ( <keyseq> . <key-state-table/terminal> ))
64 ;; <keyseq> ::= "string" of key sequence
65 ;; <output> ::= "string"
67 ;; <point> ::= integer which specifies point
69 ;; <cursor> ::= nil ; Previous SYL is active (input will go that SYL)
70 ;; | t ; input makes new SYL. DEL deletes previous SYL
71 ;; | its-cursor ; DEL breaks previous SYL, input makes new SYL
73 ;; Data structures in ITS
74 ;; (2) State machine which recognizes SYL
76 ;; <state> ::= ( <output> <keyseq> . <key-state-table/terminal> )
78 ;; <key-state-table/terminal> ::= <key-state-table> ; intermediate state
79 ;; | <terminal> ; terminal state
81 ;; <key-state-table> ::= ( <key-state-alist> . <expr-output-back-list> )
82 ;; <key-state-alist> ::= ( <key-state> ... )
83 ;; <key-state> ::= ( <key> . <state> )
84 ;; <key> ::= Positive INTEGER which specifies KEY STROKE
85 ;; | -1 ; means END of key stroke
87 ;; Only applicable for last transition.
88 ;; <expr-output-back-list> ::= ( (<output> . (<keyexpr> . <howmanyback>))... )
89 ;; <keyexpr> ::= something like "[a-z]" which specifies class of key.
90 ;; | NIL; means ANY of key (except END of the key stroke)
93 ;; <keyseq> ::= "string"
98 ;; <howmanyback> ::= integer which specifies how many key strokes we go back
100 ;; <output> ::= "string"
102 ;; Data structure in ITS (3) Map
104 ;; <map> ::= ( <name> <indicator> <language> . <start-state> )
105 ;; <name> ::= "string"
106 ;; <indicator> ::= "string"
107 ;; <language> ::= "string"
108 ;; <start-state> ::= <state>
111 (defsubst its-new-state (output keyseq back)
112 (cons output (cons keyseq back)))
114 (defsubst its-new-map (name indicator language)
115 (cons name (cons indicator (cons language (its-new-state "" "" nil)))))
117 (defsubst its-get-indicator (map)
120 (defsubst its-get-language (map)
123 (defsubst its-get-start-state (map)
126 (defsubst its-get-kst/t (state)
129 (defsubst its-set-kst (state kst)
130 (setcdr (cdr state) kst))
132 (defsubst its-get-keyseq (state)
135 (defsubst its-set-keyseq (state keyseq)
136 (setcar (cdr state) keyseq))
138 (defun its-get-keyseq-cooked (state)
139 (let ((keyseq (its-get-keyseq state))
140 (back (its-get-kst/t state)))
142 (substring keyseq 0 back)
145 (defsubst its-kst-p (kst/t)
146 (not (or (numberp kst/t) (null kst/t))))
148 (defsubst its-get-output (syl/state)
151 (defsubst its-set-output (state output)
152 (setcar state output))
154 (defsubst its-get-keyseq-syl (syl)
156 (cond ((stringp l) ; DSYL
161 (substring (car l) 0 (cdr l)))
165 (defsubst its-eob-keyexpr (eob)
167 (defsubst its-eob-back (eob)
170 (defsubst its-make-class+back (class back)
172 (defsubst its-make-otherwise (output class+back)
173 (cons output class+back))
175 (defsubst its-DSYL-with-back-p (syl)
176 (and (consp (cdr syl))
177 (numberp (its-get-kst/t syl))))
179 (defsubst its-concrete-DSYL-p (syl)
182 (defsubst its-make-concrete-DSYL (syl)
183 (if (consp (cdr syl))
184 (cons (its-get-output syl) (its-get-keyseq-syl syl))
190 (eval-when (eval load compile)
191 (require 'its-keydef))
194 (let ((map (make-sparse-keymap))
196 (define-key map "\C-a" 'its-beginning-of-input-buffer)
197 (define-key map "\C-b" 'its-backward-SYL)
198 (define-key map "\C-c" 'its-cancel-input)
199 (define-key map "\C-d" 'its-delete-SYL)
200 (define-key map "\C-e" 'its-end-of-input-buffer)
201 (define-key map "\C-f" 'its-forward-SYL)
202 (define-key map "\C-g" 'its-select-previous-mode)
203 (define-key map "\C-]" 'its-cancel-input)
204 (define-key map "\C-h" 'its-mode-help-command)
205 (define-key map "\C-k" 'its-kill-line)
206 ;; (define-key map "\C-l" 'its-exit-mode)
207 (define-key map "\C-m" 'its-exit-mode) ; RET
208 (define-key map [return] 'its-exit-mode)
209 (define-key map "\C-t" 'its-transpose-chars)
210 (define-key map "\C-w" 'its-kick-convert-region)
211 (define-key map "\C-y" 'its-yank)
212 (define-key map "\M-y" 'its-yank-pop)
213 (define-key map [backspace] 'its-delete-backward-SYL)
214 (define-key map [delete] 'its-delete-backward-SYL)
215 (define-key map [M-backspace] 'its-delete-backward-SYL-by-keystroke)
216 (define-key map [M-delete] 'its-delete-backward-SYL-by-keystroke)
217 (define-key map [right] 'its-forward-SYL)
218 (define-key map [left] 'its-backward-SYL)
220 (define-key map (vector i) 'its-self-insert-char)
222 (define-key map " " 'its-kick-convert-region-or-self-insert)
223 (define-key map "\177" 'its-delete-backward-SYL)
225 (define-key map "\M-p" 'its-previous-map)
226 (define-key map "\M-n" 'its-next-map)
227 (define-key map "\M-h" 'its-hiragana) ; hiragana-region for input-buffer
228 (define-key map "\M-k" 'its-katakana)
229 (define-key map "\M-<" 'its-hankaku)
230 (define-key map "\M->" 'its-zenkaku)
231 (its-define-select-keys map t)
233 "Keymap for ITS mode.")
235 (defvar its-fence-open "|" "*
\e$B%U%'%s%9$N;OE@$r<($9J8;zNs
\e(B (1
\e$BJ8;z0J>e
\e(B)")
236 (defvar its-fence-close "|" "*
\e$B%U%'%s%9$N=*E@$r<($9J8;zNs
\e(B (1
\e$BJ8;z0J>e
\e(B)")
237 (defvar its-fence-face nil "*
\e$B%U%'%s%9I=<($KMQ$$$k
\e(B face
\e$B$^$?$O
\e(B nil")
239 (defconst its-setup-fence-before-insert-SYL nil)
241 (defun its-get-fence-face ()
242 (let ((face (and (consp its-fence-face)
243 (or (assoc its-current-language its-fence-face)
244 (assoc t its-fence-face)))))
245 (if face (cdr face) its-fence-face)))
247 (defun its-put-cursor (cursor)
249 (map (copy-keymap its-mode-map)))
250 (its-define-select-keys map)
252 (add-text-properties p (point) (list 'local-map map
255 'intangible 'its-part-2
259 (defsubst its-set-cursor-status (cursor)
260 (put-text-property (point) (1+ (point)) 'its-cursor cursor)
264 ;; +-- START property
265 ;; | --- CURSOR Property
267 ;; v v v-- END Property
269 ;; ^^^ ^^^ ^^^------ SYL Property
271 ;; intangible intangible
274 (defun its-setup-fence-mode ()
275 (let ((open-props '(its-start t intangible its-part-1))
276 (close-props '(rear-nonsticky t its-end t intangible its-part-2))
278 ;; Put open-fence before inhibit-read-only to detect read-nly
279 (insert its-fence-open)
280 (let ((inhibit-read-only t))
282 (add-text-properties p p1 open-props)
283 (insert its-fence-close)
284 (add-text-properties p1 (point) close-props)
286 (put-text-property p (point) 'invisible t))
287 (put-text-property p (point) 'read-only t)
289 (its-put-cursor t))))
291 (defun its-start (key)
292 (let ((its-setup-fence-before-insert-SYL t))
293 (its-input nil key)))
295 (defun its-restart (str &optional set-prop)
297 (its-setup-fence-mode)
301 (its-setup-yanked-portion p (point)))
302 (its-beginning-of-input-buffer)))
304 (defun its-self-insert-char ()
306 (let ((inhibit-read-only t)
307 (key last-command-char)
308 (cursor (get-text-property (point) 'its-cursor))
309 (syl (get-text-property (1- (point)) 'its-syl)))
312 (not (eq (get-text-property (1- (point)) 'its-map) its-current-map)))
313 (put-text-property (- (point) (length (its-get-output syl))) (point)
314 'its-syl (its-make-concrete-DSYL syl))
318 (its-input syl key)))
320 (defun its-initial-ISYL ()
321 (its-get-start-state its-current-map))
323 (defun its-make-VSYL (keyseq)
324 (cons keyseq (length keyseq)))
326 (defvar its-barf-on-invalid-keyseq nil
327 "T means don't allow invalid key sequence in input buffer.")
329 (defun its-input-error ()
330 (error "Invalid Romaji Sequence"))
333 (defun its-input (syl key)
335 (setq syl (its-initial-ISYL)))
336 (let ((output (car syl))
340 ;; k/kk/s is "point in keyseq"
341 (its-input-to-vsyl syl key k/kk/s output))
342 ((and its-barf-on-invalid-keyseq
343 (null (its-keyseq-acceptable-p (vector key) syl)))
344 ;; signal before altering
348 (its-state-machine syl key 'its-buffer-ins/del-SYL)))))
350 (defun its-input-to-vsyl (syl key point output)
352 (its-set-cursor-status t)
353 (let ((len (length output)))
355 ;; point is at end of VSYL. Don't need to call state machine.
356 (its-buffer-ins/del-SYL
357 (its-make-VSYL (concat output (vector key))) syl nil)
358 ;; point is at middle of VSYL.
359 (let ((new-keyseq (concat (substring output 0 point)
361 (substring output point))))
362 (its-state-machine-keyseq new-keyseq 'its-buffer-ins/del-SYL))))))
365 ;;; ITS State Machine
368 (defvar its-disable-special-action nil)
371 (defun its-state-machine (state key emit)
372 (let ((next-state (its-get-next-state state key))
373 expr-output-back kst/t output keyseq back)
375 ;; proceed to next status
377 (not (and its-disable-special-action
378 (eq (its-get-kst/t next-state) t))))
379 (setq kst/t (its-get-kst/t next-state)
380 output (its-get-output next-state)
381 keyseq (its-get-keyseq next-state))
385 (funcall emit (cons "" keyseq) state 'its-cursor)
386 (apply (car output) (cdr output)))
388 ;; Still, it's a intermediate state.
390 (funcall emit next-state state nil))
392 ;; It's negative integer which specifies how many
393 ;; characters we go backwards
395 (funcall emit next-state state 'its-cursor)
396 (its-state-machine-keyseq (substring keyseq kst/t) emit (< key 0)))
398 ;; Here we arrive to a terminal state.
399 ;; Emit a DSYL, and go ahead.
401 (funcall emit next-state state 'its-cursor))))
403 ;; push back by otherwise status
405 (setq expr-output-back (its-get-otherwise state key)))
406 (setq keyseq (concat (its-get-keyseq state) (vector key)))
408 (cons (its-get-output expr-output-back)
409 (cons keyseq (its-eob-back expr-output-back)))
411 (its-state-machine-keyseq
412 (substring keyseq (its-eob-back expr-output-back)) emit))
414 ((eq its-barf-on-invalid-keyseq 'its-keyseq-test)
415 'its-keyseq-test-failed)
417 ;; No next state for KEY. It's invalid sequence.
418 (its-barf-on-invalid-keyseq
422 ;; XXX Should make DSYL (instead of VSYL)?
423 (setq keyseq (concat (its-get-keyseq state) (if (> key 0) (vector key))))
424 (funcall emit (its-make-VSYL keyseq) state nil)))))
426 (defvar its-latest-SYL nil
427 "The latest SYL inserted.")
428 (defsubst its-update-latest-SYL (syl)
429 (setq its-latest-SYL syl))
432 (defun its-state-machine-keyseq (keyseq emit &optional eol)
434 (len (length keyseq))
435 (syl (its-initial-ISYL))
440 ;; VSYL - no need looping
442 (its-make-VSYL (concat (car syl) (substring keyseq i)))
447 (setq cursor (its-state-machine syl (aref keyseq i) emit))))
448 (if (eq cursor 'its-keyseq-test-failed)
450 (setq syl (if cursor (its-initial-ISYL) its-latest-SYL)
452 (if (and eol (not (eq cursor 'its-keyseq-test-failed)))
453 (its-state-machine syl -1 emit)
456 (defun its-buffer-ins/del-SYL (newsyl oldsyl cursor)
457 (if its-setup-fence-before-insert-SYL
459 (setq its-setup-fence-before-insert-SYL nil)
460 (its-setup-fence-mode)))
461 (let ((inhibit-read-only t))
462 (its-buffer-delete-SYL oldsyl)
463 (its-update-latest-SYL newsyl)
465 (insert (its-get-output newsyl))
466 (add-text-properties p (point)
467 (list 'its-map its-current-map
469 'egg-lang its-current-language
471 'intangible 'its-part-1))
473 (put-text-property p (point) 'face (its-get-fence-face)))
474 (its-set-cursor-status cursor))))
476 (defun its-buffer-delete-SYL (syl)
477 (let ((len (length (its-get-output syl))))
478 (delete-region (- (point) len) (point))))
480 (defun its-get-next-state (state key)
481 (let ((kst/t (its-get-kst/t state)))
483 (cdr (assq key (car kst/t))))))
486 (defun its-otherwise-match (expr key)
487 (or (null expr) ; <expr>::= NIL means "ANY"
488 (let ((case-fold-search nil))
489 (string-match expr (char-to-string key)))))
491 (defun its-get-otherwise (state key)
492 (let* ((kst/t (its-get-kst/t state))
496 (setq expr-output-back (car ebl))
497 (let ((expr (its-eob-keyexpr expr-output-back)))
498 (if (its-otherwise-match expr key)
500 (setq ebl (cdr ebl)))))
503 (defun its-keyseq-acceptable-p (keyseq &optional syl eol)
505 (len (length keyseq))
506 (its-barf-on-invalid-keyseq 'its-keyseq-test)
508 (emit (lambda (nsyl osyl cursor)
509 (its-update-latest-SYL nsyl)
511 (its-current-map its-current-map)
512 (its-current-select-func its-current-select-func)
513 (its-current-language its-current-language)
514 (its-zhuyin its-zhuyin)
515 (its-previous-select-func its-previous-select-func)
518 (setq syl (its-initial-ISYL)))
519 (while (and syl (< i len))
520 (setq cursor (its-state-machine syl (aref keyseq i) emit))
522 ((eq cursor 'its-keyseq-test-failed)
525 (setq syl (its-initial-ISYL)))
530 (setq cursor (its-state-machine syl -1 emit)))
531 (not (eq cursor 'its-keyseq-test-failed))))
538 (defvar its-map-alist nil)
540 (defun its-get-map (name)
541 (assoc name its-map-alist))
543 (defun its-register-map (map)
544 (let* ((name (car map))
545 (place (assoc name its-map-alist)))
547 (setcdr place (cdr map))
548 (setq its-map-alist (cons map its-map-alist)))
551 (defmacro define-its-state-machine (map name indicator lang doc &rest exprs)
553 (eval-when (eval compile)
554 (let ((its-current-map (its-new-map ,name ,indicator ,lang)))
556 (setq ,map its-current-map)))
557 (define-its-compiled-map ,map ,doc)))
559 (defmacro define-its-compiled-map (map doc)
560 `(defconst ,map ',(symbol-value map) ,doc))
562 (defmacro define-its-state-machine-append (map &rest exprs)
564 `(let ((its-current-map ,map)))
566 (list `(setq ,map its-current-map))))
569 ;; Construct State Machine
571 (defun its-defrule (input output &optional back enable-overwrite)
572 "
\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
573 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
574 \e$BLa$C$FF0$/$b$N$H$9$k!#JQ495,B'$O$b$C$H$b:G6a$K
\e(B its-define-state-machine
575 \e$B$5$l$?JQ49I=$KEPO?$5$l$k!#
\e(B
577 (let ((state (its-goto-state (substring input 0 -1) nil t))
578 (key (aref input (1- (length input)))))
579 (if (and (its-get-next-state state key) (not enable-overwrite))
580 (error "Duplicated definition (%s)" input)
581 (its-make-next-state state key input output back))))
583 (defun its-goto-state (input &optional initial-state build-if-none)
584 (let ((len (length input))
586 (state (or initial-state (its-get-start-state its-current-map))))
589 (or (its-get-next-state state (aref input i))
591 (let ((keyseq (substring input 0 (1+ i))))
592 (its-make-next-state state (aref input i) keyseq keyseq))
593 (error "No such state (%s)" input)))
597 (defun its-defoutput (input display)
598 (let ((state (its-goto-state input)))
599 (its-set-output state display)))
601 (defun its-define-otherwise (state otherwise)
602 (let ((kst (its-get-kst/t state)))
604 (setcdr kst (cons otherwise (cdr kst)))
605 (its-set-kst state (cons nil (cons otherwise nil))))))
607 (defconst its-otherwise-back-one
608 (its-make-class+back nil -1))
610 (defun its-defrule-otherwise (state output &optional class back)
613 (setq class+back its-otherwise-back-one)
614 (setq class+back (its-make-class+back class back)))
615 (its-define-otherwise state
616 (its-make-otherwise output class+back))))
618 (defun its-defrule* (input output)
619 (let ((state (its-defrule input output)))
620 (its-defrule-otherwise state output)))
622 (defun its-make-next-state (state key keyseq output &optional back)
623 (let ((next-state (its-new-state output keyseq back))
624 (kst (its-get-kst/t state)))
627 (its-set-kst state (list (list (cons key next-state)))))
629 (setcar kst (cons (cons key next-state) (car kst))))
631 (error "Can't make new state after %S" (its-get-keyseq state))))
634 (defmacro its-defrule-select-mode-temporally (input select-func)
635 `(its-defrule ,input '(its-select-mode-temporally
636 ,(intern (concat "its-select-"
637 (symbol-name select-func))))
641 (defun its-beginning-of-input-buffer ()
643 (let ((inhibit-read-only t))
645 (if (not (get-text-property (1- (point)) 'its-start))
646 (let ((begpos (previous-single-property-change (point) 'its-start)))
647 ;; Make SYLs have property of "part 2"
648 (put-text-property begpos (point) 'intangible 'its-part-2)
652 (defun its-end-of-input-buffer ()
654 (let ((inhibit-read-only t))
656 (if (not (get-text-property (point) 'its-end))
657 (let ((endpos (next-single-property-change (point) 'its-end)))
658 ;; Make SYLs have property of "part 1"
659 (put-text-property (point) endpos 'intangible 'its-part-1)
663 (defun its-kill-line (n)
665 (let ((inhibit-read-only t)
670 ((get-text-property (1- (point)) 'its-start)
672 ((get-text-property (point) 'its-end)
675 (delete-region (next-single-property-change (point) 'its-end)
679 ((get-text-property (point) 'its-end)
681 ((get-text-property (1- (point)) 'its-start)
684 (delete-region (point)
685 (previous-single-property-change (point) 'its-start))
686 (its-put-cursor t))))))
688 (defun its-cancel-input ()
690 (let ((inhibit-read-only t))
691 (delete-region (if (get-text-property (1- (point)) 'its-start)
693 (previous-single-property-change (1- (point)) 'its-start))
694 (if (get-text-property (point) 'its-end)
696 (next-single-property-change (point) 'its-end)))
698 (its-exit-mode-internal)))
700 ;; TODO: move in VSYL
701 (defun its-backward-SYL (n)
703 (let ((inhibit-read-only t)
706 (setq syl (get-text-property (1- (point)) 'its-syl)
709 (while (and syl (> n 0))
710 (setq p (- p (length (its-get-output syl))))
711 (setq syl (get-text-property (1- p) 'its-syl))
713 ;; Make SYLs have property of "part 2"
714 (put-text-property p old-point 'intangible 'its-part-2)
718 (signal 'beginning-of-buffer nil))))
720 ;; TODO: move in VSYL
721 (defun its-forward-SYL (n)
723 (let ((inhibit-read-only t)
726 (setq syl (get-text-property (point) 'its-syl)
729 (while (and syl (> n 0))
730 (setq p (+ p (length (its-get-output syl))))
731 (setq syl (get-text-property p 'its-syl))
733 ;; Make SYLs have property of "part 1"
734 (put-text-property old-point p 'intangible 'its-part-1)
738 (signal 'end-of-buffer nil))))
740 ;; TODO: handle VSYL. KILLFLAG
741 (defun its-delete-SYL (n killflag)
743 (let ((inhibit-read-only t)
746 (setq syl (get-text-property (point) 'its-syl)
748 (while (and syl (> n 0))
749 (setq p (+ p (length (its-get-output syl))))
750 (setq syl (get-text-property p 'its-syl))
755 (signal 'end-of-buffer nil))
756 (delete-region (point) p)
759 (if (and (get-text-property (1- (point)) 'its-start)
760 (get-text-property (1+ (point)) 'its-end))
761 (its-exit-mode-internal)))))
764 (defun its-delete-backward-SYL (n killflag)
766 (let ((inhibit-read-only t)
767 (syl (get-text-property (1- (point)) 'its-syl))
768 (cursor (get-text-property (point) 'its-cursor)))
770 (signal 'beginning-of-buffer nil)
772 (its-delete-backward-SYL-internal n killflag)
773 (its-delete-backward-within-SYL syl n killflag)))))
776 (defun its-delete-backward-SYL-internal (n killflag)
777 (let ((syl (get-text-property (1- (point)) 'its-syl))
779 (while (and syl (> n 0))
780 (setq p (- p (length (its-get-output syl))))
781 (setq syl (get-text-property (1- p) 'its-syl))
784 (signal 'beginning-of-buffer nil)
785 (delete-region p (1+ (point))) ; also delete cursor
788 (if (and (get-text-property (1- (point)) 'its-start)
789 (get-text-property (1+ (point)) 'its-end))
790 (its-exit-mode-internal)))))
792 (defvar its-delete-by-keystroke nil)
794 (defun its-delete-backward-SYL-by-keystroke (n killflag)
796 (let ((inhibit-read-only t)
797 (its-delete-by-keystroke t))
798 (its-delete-backward-SYL n killflag)))
801 (defun its-delete-backward-within-SYL (syl n killflag)
802 (let* ((keyseq (its-get-keyseq-syl syl))
803 (len (length keyseq))
804 (p (- (point) (length (its-get-output syl))))
805 (its-current-map (get-text-property (1- (point)) 'its-map))
806 (its-current-language (get-text-property (1- (point)) 'egg-lang))
809 (signal 'args-out-of-range (list (- (point) n) (point))))
810 (if its-delete-by-keystroke
811 (while (null (or (eq p pp) (its-concrete-DSYL-p syl)))
813 (while (and (setq syl (get-text-property (1- p) 'its-syl))
814 (its-DSYL-with-back-p syl)
815 (<= (setq back (- (its-get-kst/t syl))) len)
817 (equal (substring (its-get-keyseq syl) (- back))
818 (substring keyseq 0 back)))
819 (setq keyseq (concat (its-get-keyseq-syl syl) keyseq)
821 p (- p (length (its-get-output syl)))))
822 (if (and (eq p pp) syl (> n len))
824 keyseq (its-get-keyseq-syl syl)
826 p (- p (length (its-get-output syl))))))
827 (if (and (> n len) (its-concrete-DSYL-p syl))
832 (while (and (> n len) (setq syl (get-text-property (1- p) 'its-syl)))
834 p (- p (length (its-get-output syl)))))
836 (signal 'beginning-of-buffer nil))
837 (delete-region p (point))
840 (its-state-machine-keyseq (substring keyseq 0 (- len n))
841 'its-buffer-ins/del-SYL))
843 ((and (get-text-property (1- (point)) 'its-start)
844 (get-text-property (1+ (point)) 'its-end))
845 (its-exit-mode-internal))
846 ((and its-delete-by-keystroke
847 (null (its-concrete-DSYL-p (get-text-property (1- p) 'its-syl))))
848 (its-set-cursor-status 'its-cursor))
850 (its-set-cursor-status t)))))
852 (defun its-transpose-chars (n)
854 (let ((inhibit-read-only t)
855 (syl (get-text-property (1- (point)) 'its-syl))
856 (cursor (get-text-property (point) 'its-cursor))
860 (signal 'beginning-of-buffer nil))
862 (if (and (= n 1) (get-text-property (1+ (point)) 'its-end))
865 (setq syl (get-text-property (1- (point)) 'its-syl))
867 (signal 'beginning-of-buffer nil))))
868 (its-buffer-delete-SYL syl)
870 (if (get-text-property (1+ (point)) 'its-end)
872 (its-buffer-ins/del-SYL syl nil t)
873 (signal 'end-of-buffer nil)))
877 (if (get-text-property (1- (point)) 'its-start)
879 (its-buffer-ins/del-SYL syl nil t)
880 (signal 'beginning-of-buffer nil)))
883 (its-buffer-ins/del-SYL syl nil t))
885 (setq keyseq (its-get-keyseq-syl syl)
888 ((or (> n 1) (<= len 1))
889 (signal 'end-of-buffer nil))
891 (signal 'beginning-of-buffer nil))
893 (setq n (if (> n 0) (- -1 n) (1- n)))
894 (setq keyseq (concat (substring keyseq 0 n)
895 (substring keyseq -1)
896 (substring keyseq n -1)))
897 (if (and its-barf-on-invalid-keyseq
898 (null (its-keyseq-acceptable-p keyseq)))
900 (delete-region (- (point) (length (its-get-output syl))) (point))
901 (its-state-machine-keyseq keyseq 'its-buffer-ins/del-SYL)))))))
903 (defun its-yank (&optional arg)
905 (let ((inhibit-read-only t))
909 (its-setup-yanked-portion (region-beginning) (region-end))))
911 (defun its-yank-pop (arg)
913 (let ((inhibit-read-only t))
917 (its-setup-yanked-portion (region-beginning) (region-end))))
919 (defun its-setup-yanked-portion (start end)
920 (let ((yank-before (eq (point) end))
921 (max-sisheng (make-char 'chinese-sisheng 127))
923 (remove-text-properties start end '(intangible nil))
924 (egg-separate-languages start end t)
926 (while (< (point) end)
928 lang (get-text-property p 'egg-lang))
930 (or (equal lang "Chinese-GB") (equal lang "Chinese-CNS"))
931 (<= (following-char) max-sisheng)
932 (setq len (egg-chinese-syllable (buffer-substring (point) end))))
933 (goto-char (+ (point) len))
935 (setq syl (buffer-substring-no-properties p (point)))
936 (put-text-property p (point) 'its-syl (cons syl syl))
938 (let ((its-current-language (get-text-property p 'egg-lang)))
939 (put-text-property p (point) 'face (its-get-fence-face)))))
941 (add-text-properties start end '(read-only t intangible its-part-1))
942 (add-text-properties start end '(read-only t intangible its-part-2))
943 (delete-region (point) (1+ (point)))
945 (its-put-cursor t))))
948 (defun its-input-end ()
949 (let ((cursor (get-text-property (point) 'its-cursor)))
952 (its-input (get-text-property (1- (point)) 'its-syl) -1))
953 (delete-region (point) (1+ (point)))))
955 (defun its-exit-mode ()
958 (let ((inhibit-read-only t))
959 (its-exit-mode-internal)))
961 (defun its-exit-mode-off-input-method ()
964 (let ((inhibit-read-only t))
965 (its-exit-mode-internal)
966 (inactivate-input-method)))
968 ;; TODO: handle overwrite-mode, insertion-hook, fill...
969 (defun its-exit-mode-internal (&optional proceed-to-conversion)
971 (its-select-previous-mode t)
973 (delete-region (point) (1+ (point)))
975 (setq s (if (get-text-property (1- (point)) 'its-start)
977 (previous-single-property-change (point) 'its-start))
978 start (- s (length its-fence-open)))
979 (delete-region start s)
980 ;; Delete close fence
981 (setq end (if (get-text-property (point) 'its-end)
983 (next-single-property-change (point) 'its-end))
984 e (+ end (length its-fence-close)))
985 (delete-region end e)
986 (if proceed-to-conversion
987 (egg-convert-region start end)
988 ;; Remove all properties
989 (set-text-properties start end nil)
991 (run-hooks 'input-method-after-insert-chunk-hook))))
993 (defun its-kick-convert-region ()
995 (let ((inhibit-read-only t))
996 (its-exit-mode-internal t)))
998 (defun its-kick-convert-region-or-self-insert ()
1000 (let ((syl (and (null (get-text-property (point) 'its-cursor))
1001 (get-text-property (1- (point)) 'its-syl))))
1002 (if (its-keyseq-acceptable-p (vector last-command-char) syl)
1003 (its-self-insert-char)
1004 (its-kick-convert-region))))
1006 (defun its-in-fence-p ()
1007 (eq (get-text-property (point) 'intangible) 'its-part-2))
1009 (defvar its-translation-result "" "")
1011 (defun its-ins/del-SYL-batch (newsyl oldsyl cursor)
1012 (its-update-latest-SYL newsyl)
1014 (consp (cdr newsyl))
1015 (not (its-kst-p (its-get-kst/t newsyl))))
1017 (let ((output (its-get-output newsyl))
1018 (oldlen (length its-translation-result)))
1019 (setq its-translation-result (concat its-translation-result output))
1020 (put-text-property oldlen (length its-translation-result)
1021 'egg-lang its-current-language
1022 its-translation-result)))
1025 (defun its-translate-region (start end)
1027 (its-translate-region-internal start end)
1028 (set-text-properties start (point) nil))
1030 (defun its-translate-region-internal (start end)
1031 (setq its-translation-result "")
1034 (syl (its-initial-ISYL))
1035 ;; temporally enable DING
1036 (its-barf-on-invalid-keyseq t)
1038 (while (< (point) end)
1039 (let ((key (following-char)))
1040 (setq cursor (its-state-machine syl key 'its-ins/del-SYL-batch))
1043 (setq syl (its-initial-ISYL))
1044 (setq syl its-latest-SYL))))
1045 (if (eq syl its-latest-SYL)
1046 (its-state-machine syl -1 'its-ins/del-SYL-batch))
1047 (delete-region start end)
1048 (insert its-translation-result)))
1050 (defun its-set-mode-line-title ()
1051 (let ((title (its-get-indicator its-current-map)))
1052 (setq current-input-method-title (if its-previous-select-func
1053 (concat "<" title ">")
1055 (force-mode-line-update)))
1057 (defun its-select-mode-temporally (func)
1058 (let ((select-func its-current-select-func))
1060 (if (null its-previous-select-func)
1061 (setq its-previous-select-func select-func))
1062 (its-set-mode-line-title)))
1064 (defun its-select-previous-mode (&optional quiet)
1066 (if (null its-previous-select-func)
1069 (funcall its-previous-select-func)
1070 (setq its-previous-select-func nil)
1071 (its-set-mode-line-title)))
1074 ;;; its.el ends here.