;; ko-romaja.mim -- Input method for Korean Hangul with Romaja keys.
;; Copyright (C) 2004, 2005
;;   National Institute of Advanced Industrial Science and Technology (AIST)
;;   Registration Number H15PRO112

;; This file is part of the m17n database; a sub-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 for Korean Hangul using Romaja keys.  The
;;; roman-transliteration rules follows that of IIIMF shown in this
;;; page: http://www3.sympatico.ca/d.moser/hangul.html

(input-method ko romaja)

(description
 "Hangul input method with romaja keys.
The roman-transliteration rules follows that of IIIMF shown in
the page <http://www3.sympatico.ca/d.moser/hangul.html>.

Common to CHOSEONG and JONGSEONG:
  ㄱ(g) ㄲ(gg,kk,qq,c) ㄴ(n) ㄷ(d) ㄹ(l) ㄹ(r) ㅁ(m) ㅂ(b,v) ㅅ(s)
  ㅆ(ss) ㅇ(ng) ㅇ(x) ㅈ(j) ㅊ(ch) ㅋ(k,q) ㅌ(t) ㅍ(p,f) ㅎ(h)

CHOSEONG:
  ㄸ(dd,tt) ㅃ(bb,vv) ㅉ(jj)

JONGSEONG:
  ㄳ(gs) ㄵ(nj) ㄶ(nh) ㄺ(lg) ㄻ(lm) ㄼ(lb) ㄽ(ls) ㄾ(lt) ㄿ(lp) ㅀ(lh) ㅄ(bs)

JUNGSEONG:
  ㅏ(a) ㅐ(ai,ae) ㅑ (ya,ia) ㅒ(yai,yae,iae) ㅓ(eo) ㅔ(e,eoi) ㅕ(yeo,ieo)
  ㅖ(ye,ie,yeoi) ㅗ(o) ㅘ(oa,wa,ua) ㅙ(oai,wae,uae,oae) ㅚ(oi,woe,uoe,oe)
  ㅛ(yo,io) ㅜ(u,w,oo) ㅝ(ueo,wo,uo) ㅞ(ue,we) ㅟ(wi) ㅠ(yu,iu) ㅡ(eu)
  ㅢ(eui,ui) ㅣ(i,y,ee)

Special:
  Type uppercase letter to specify CHOSEONG explicitly.
  Type \"I\" to toggle the composed-syllable mode and isolated-jamo mode.
  Type \">>\" to fullwidth ASCII letter mode, \"<<\" to shift out the mode.
  Type \"Z\" and a key to input fullwidth version of the key.
")

(title "로마자")

(macro
 (compose
  ;; Convert Jamo sequence LVT to a precomposed character.
  ;; See the Unicode Standard Version 3.0, Section 3.11.
  (> L 0
     ((set LIndex L) (sub LIndex 0x1100)
      (set VIndex V) (sub VIndex 0x1161)
      (set TIndex T) (sub TIndex 0x11A7)
      ;; S = (LIndex * VCount + Vindex) * TCount + TIndex + SBase
      (set S LIndex)
      (mul S 21) ;; VCount
      (add S VIndex)
      (mul S 28) ;; TCount
      (add S TIndex)
      (add S 0xAC00) ;; SBase
      (delete @<)
      (insert S))))

 ;; If L is not negative, convert Jame sequence LV to a precomposed
 ;; character while assuming T is 0x11A7.  If L is 0 (i.e. missing),
 ;; set L to 0x110B.
 (compose-vowel
  (< L 0
     ()
     ((= L 0 ((set L 0x110B)))
      (set T1 T)
      (set T 0x11A7)
      (compose)
      (set T T1)))))

(map
 ;; This map is used only in init state, which means typing these
 ;; uppercase letters in any other states causes shifting to init
 ;; state, and they are re-handled in init state.
 (uppercase
  ("B" (pushback "b")) ("C" (pushback "c")) ("D" (pushback "d"))
  ("F" (pushback "f")) ("G" (pushback "g")) ("H" (pushback "h"))
  ("J" (pushback "j")) ("K" (pushback "k")) ("L" (pushback "l"))
  ("M" (pushback "m")) ("N" (pushback "n")) ("P" (pushback "p"))
  ("Q" (pushback "q")) ("R" (pushback "r")) ("S" (pushback "s"))
  ("T" (pushback "t")) ("V" (pushback "v")) ("X" (pushback "x")))

 ;; Keys common to CHOSEONG and JONGSEONG.  Set L1 to CHOSEONG, T to
 ;; JONGSEONG, insert a Compatibility Jamo.  When this map is used in
 ;; init state, L is still 0, thus compose macro does nothing.
 ;; Otherwise, L is already set to a CHOSEONG, thus compose macro
 ;; composes LVT into a syllable.
 (X
  ("g"  (set L1 0x1100) (set T 0x11A8) ?ㄱ (compose))
  ("gg" (set L1 0x1101) (set T 0x11A9) ?ㄲ (compose))
  ("kk" (set L1 0x1101) (set T 0x11A9) ?ㄲ (compose))
  ("qq" (set L1 0x1101) (set T 0x11A9) ?ㄲ (compose))
  ("c"  (set L1 0x1101) (set T 0x11A9) ?ㄲ (compose))
  ("n"  (set L1 0x1102) (set T 0x11AB) ?ㄴ (compose))
  ("d"  (set L1 0x1103) (set T 0x11AE) ?ㄷ (compose))
  ("l"  (set L1 0x1105) (set T 0x11AF) ?ㄹ (compose))
  ("r"  (set L1 0x1105) (set T 0x11AF) ?ㄹ (compose))
  ("m"  (set L1 0x1106) (set T 0x11B7) ?ㅁ (compose))
  ("b"  (set L1 0x1107) (set T 0x11B8) ?ㅂ (compose))
  ("v"  (set L1 0x1107) (set T 0x11B8) ?ㅂ (compose))
  ("s"  (set L1 0x1109) (set T 0x11BA) ?ㅅ (compose))
  ("ss" (set L1 0x110A) (set T 0x11BB) ?ㅆ (compose))
  ("ng" (set L1 0x110B) (set T 0x11BC) ?ㅇ (compose))
  ("x"  (set L1 0x110B) (set T 0x11BC) ?ㅇ (compose))
  ("j"  (set L1 0x110C) (set T 0x11BD) ?ㅈ (compose))
  ("ch" (set L1 0x110E) (set T 0x11BE) ?ㅊ (compose))
  ("k"  (set L1 0x110F) (set T 0x11BF) ?ㅋ (compose))
  ("q"  (set L1 0x110F) (set T 0x11BF) ?ㅋ (compose))
  ("t"  (set L1 0x1110) (set T 0x11C0) ?ㅌ (compose))
  ("p"  (set L1 0x1111) (set T 0x11C1) ?ㅍ (compose))
  ("f"  (set L1 0x1111) (set T 0x11C1) ?ㅍ (compose))
  ("h"  (set L1 0x1112) (set T 0x11C2) ?ㅎ (compose)))

 ;; Keys only for CHOSEONG.
 ;; Set L to CHOSEONG, insert a Compatibility Jamo.
 (L
  ("dd" (set L 0x1104) ?ㄸ)
  ("tt" (set L 0x1104) ?ㄸ)
  ("bb" (set L 0x1108) ?ㅃ)
  ("vv" (set L 0x1108) ?ㅃ)
  ("jj" (set L 0x110D) ?ㅉ))

 ;; Keys only for JONGSEONG.
 ;; Set T to JONGSEONG, insert a Compatibility Jamo.
 (T
  ("gs" (set T 0x11AA) ?ㄳ (compose))
  ("nj" (set T 0x11AC) ?ㄵ (compose))
  ("nh" (set T 0x11AD) ?ㄶ (compose))
  ("lg" (set T 0x11B0) ?ㄺ (compose))
  ("lm" (set T 0x11B1) ?ㄻ (compose))
  ("lb" (set T 0x11B2) ?ㄼ (compose))
  ("ls" (set T 0x11B3) ?ㄽ (compose))
  ("lt" (set T 0x11B4) ?ㄾ (compose))
  ("lp" (set T 0x11B5) ?ㄿ (compose))
  ("lh" (set T 0x11B6) ?ㅀ (compose))
  ("bs" (set T 0x11B9) ?ㅄ (compose)))

 ;; Keys only for JUNGSEONG.
 ;; Set V to JUNGSEONG, insert a Compatibility Jamo.
 (V
  ("a"   (set V 0x1161) ?ㅏ (compose-vowel))
  ("ai"  (set V 0x1162) ?ㅐ (compose-vowel))
  ("ae"  (set V 0x1162) ?ㅐ (compose-vowel))
  ("ya"  (set V 0x1163) ?ㅑ (compose-vowel))
  ("ia"  (set V 0x1163) ?ㅑ (compose-vowel))
  ("yai" (set V 0x1164) ?ㅒ (compose-vowel))
  ("yae" (set V 0x1164) ?ㅒ (compose-vowel))
  ("iae" (set V 0x1164) ?ㅒ (compose-vowel))
  ("eo"  (set V 0x1165) ?ㅓ (compose-vowel))
  ("e"   (set V 0x1166) ?ㅔ (compose-vowel))
  ("eoi" (set V 0x1166) ?ㅔ (compose-vowel))
  ("yeo" (set V 0x1167) ?ㅕ (compose-vowel))
  ("ieo" (set V 0x1167) ?ㅕ (compose-vowel))
  ("ye"  (set V 0x1168) ?ㅖ (compose-vowel))
  ("ie"  (set V 0x1168) ?ㅖ (compose-vowel))
  ("yeoi"(set V 0x1168) ?ㅖ (compose-vowel))
  ("o"   (set V 0x1169) ?ㅗ (compose-vowel))
  ("oa"  (set V 0x116A) ?ㅘ (compose-vowel))
  ("wa"  (set V 0x116A) ?ㅘ (compose-vowel))
  ("ua"  (set V 0x116A) ?ㅘ (compose-vowel))
  ("oai" (set V 0x116B) ?ㅙ (compose-vowel))
  ("wae" (set V 0x116B) ?ㅙ (compose-vowel))
  ("uae" (set V 0x116B) ?ㅙ (compose-vowel))
  ("oae" (set V 0x116B) ?ㅙ (compose-vowel))
  ("oi"  (set V 0x116C) ?ㅚ (compose-vowel))
  ("woe" (set V 0x116C) ?ㅚ (compose-vowel))
  ("uoe" (set V 0x116C) ?ㅚ (compose-vowel))
  ("oe"  (set V 0x116C) ?ㅚ (compose-vowel))
  ("yo"  (set V 0x116D) ?ㅛ (compose-vowel))
  ("io"  (set V 0x116D) ?ㅛ (compose-vowel))
  ("u"   (set V 0x116E) ?ㅜ (compose-vowel))
  ("w"   (set V 0x116E) ?ㅜ (compose-vowel))
  ("oo"  (set V 0x116E) ?ㅜ (compose-vowel))
  ("ueo" (set V 0x116F) ?ㅝ (compose-vowel))
  ("wo"  (set V 0x116F) ?ㅝ (compose-vowel))
  ("uo"  (set V 0x116F) ?ㅝ (compose-vowel))
  ("ue"  (set V 0x1170) ?ㅞ (compose-vowel))
  ("we"  (set V 0x1170) ?ㅞ (compose-vowel))
  ("wi"  (set V 0x1171) ?ㅟ (compose-vowel))
  ("yu"  (set V 0x1172) ?ㅠ (compose-vowel))
  ("iu"  (set V 0x1172) ?ㅠ (compose-vowel))
  ("eu"  (set V 0x1173) ?ㅡ (compose-vowel))
  ("eui" (set V 0x1174) ?ㅢ (compose-vowel))
  ("ui"  (set V 0x1174) ?ㅢ (compose-vowel))
  ("i"   (set V 0x1175) ?ㅣ (compose-vowel))
  ("y"   (set V 0x1175) ?ㅣ (compose-vowel))
  ("ee"  (set V 0x1175) ?ㅣ (compose-vowel)))

 ;; Keys starting V.
 (V-head
  ("a") ("y") ("e") ("o") ("w") ("u") ("i"))

 ;; Input fullwidth punction characters by typing a punction twice.
 (punctuation
  ("," ?,)
  (",," ?、)
  ("." ?.)
  (".." ?。)
  ("?" ??)
  ("??" ?？)
  ("!" ?!)
  ("!!" ?！))

 (backspace
  ((BackSpace) (> @@ 1 ((undo)) ((unhandle)))))

 ;; Toggle normal syllable mode and isolated jamo mode.
 (toggle-mode
  ("I" (= ISOLATED 0 ((set ISOLATED 1)) ((set ISOLATED 0)))))

 (commit-preedit
  (";")
  ((S-\ ))))

(include (t nil cjk-util) map)

(state
 (init
  "로마자"
  ;; This is to suppress compose action.
  (t (set L 0))
  ;; Uppercase keys are translated to the corresponding lowercase keys.
  (uppercase)
  (X (set L L1) (shift after-L))
  (L (shift after-L))
  (V (shift after-LV))
  (T)
  (backspace)
  (punctuation)
  (enter-fullwidth-mode (shift fullwidth-mode))
  (enter-single-fullwidth-mode (shift single-fullwidth-mode))
  (toggle-mode (shift isolated-jamo-mode)))

 (after-L
  (V (shift after-LV))
  (commit-preedit (shift init))
  (backspace))
 (after-LV
  (X (shift after-LVX))
  (T (shift init))
  (commit-preedit (shift init))
  (backspace))
 (after-LVX
  (V-head (delete @<) (pushback 0) (shift fix-LV-redo-L))
  (commit-preedit (shift init))
  (backspace))

 (fix-LV-redo-L
  (t (set L 0))
  (X (set L L1) (shift fix-LV-redo-V))
  (L (shift fix-LV-redo-V))
  (V (set L 0x110B) (set T 0x11A7) (compose) (shift init)))
 (fix-LV-redo-V
  (V (set T 0x11A7) (compose) (shift init)))

 (isolated-jamo-mode
  "자모"
  ;; Suppress both compose and compose-vowel action.
  (t (set L -1))
  (X (commit))
  (L (set L -1) (commit))
  (T (commit))
  (V (commit))
  (commit-preedit (commit))
  (enter-fullwidth-mode (shift fullwidth-mode))
  (enter-single-fullwidth-mode (shift single-fullwidth-mode))
  (toggle-mode (shift init))
  (nil (unhandle))))

(include (t nil cjk-util) state)

;; Local Variables:
;; coding: utf-8
;; mode: lisp
;; End:
