;; zh-util.mim -- Provide utilities for Chinese input methods.
;; Copyright (C) 2005, 2008
;;   National Institute of Advanced Industrial Science and Technology (AIST)
;;   Registration Number H15PRO112

;; This file is part of the m17n library.

;; The m17n library is free software; you can redistribute it and/or
;; modify it under the terms of the GNU Lesser General Public License
;; as published by the Free Software Foundation; either version 2.1 of
;; the License, or (at your option) any later version.

;; The m17n library is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
;; Lesser General Public License for more details.

;; You should have received a copy of the GNU Lesser General Public
;; License along with the m17n library; if not, write to the Free
;; Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
;; Boston, MA 02110-1301, USA.

(input-method t nil zh-util)

(description "Provide utilities for Chinese input methods.
This is acutually not a standalone input method, but is expected
to be included in the other Chinese input method (e.g. zh-py).
")

(map
 ;; Typing 1, 2, ..., 0 selects the 0th, 1st, ..., 9th candidate.
 (choose
  ("1" (select 0))
  ("2" (select 1))
  ("3" (select 2))
  ("4" (select 3))
  ("5" (select 4))
  ("6" (select 5))
  ("7" (select 6))
  ("8" (select 7))
  ("9" (select 8))
  ("0" (select 9)))

 (change-candidate
  ((Left) (select @-))
  ((C-b) (select @-))
  ((Right) (select @+))
  ((C-f) (select @+))
  ((Up) (select @\[))
  ((C-p) (select @\[))
  ((Down) (select @\]))
  ((C-n) (select @\])))

 (focus-move
  ;; When an input spot is moved, commit the current predit by shifting
  ;; to init.
  ((input-focus-move)))

 (focus-change
  ;; When an input focus is out or in, keep the current preedit.
  ((input-focus-out) (set KK @@) (sub KK 1) (undo KK))
  ((input-focus-in) (set KK @@) (sub KK 1) (undo KK)))

 (backspace
  ((BackSpace)))

 (commit-preedit
  ((S-\ ))
  ((\ ))))

(state
 (check-undo
  ;; When Backspace is typed, cancel the last input.
  (backspace (undo))
  (focus-move (shift init))
  (focus-change)
  ;; When anything else is typed, produce the current candidate (if
  ;; any), and re-handle the last input in "init" state.
  (nil (hide) (shift init)))

 (select
  (t (set K @@) (sub K 1))
  ;; When a number is typed, select the corresponding canidate,
  ;; produce it, and shift to "init" state.
  (focus-move (shift init))
  (focus-change)
  (choose (hide) (shift init))
  (change-candidate)
  (backspace (undo K))
  ;; When key to force committing, commit the current preedit.
  (commit-preedit (shift init))
  ;; When anything else is typed, produce the current candidate,
  ;; and re-handle the last input in "init" state.
  (nil (hide) (shift init))))
