;; ko-han2.mim -- Input method for Korean Hangul
;; Copyright (C) 2003, 2004, 2005, 2006, 2008, 2009, 2010
;;   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.

;;; @if FOR_LATEX
;;; Hangul input method with 2-bul style.
;;; @endif
;;; @if FOR_HTML
;;; Hangul input method with 2벌식.
;;; @endif
;;; This input method uses this keyboard layout:
;;; @image html ko-han2.png "Keyboard Layout"
;;; @image latex ko-han2.eps "Keyboard Layout" width=\narrowwidth
;; ┌─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┐
;; │1!│2@│3#│4$│5%│6^│7&│8*│9(│0)│-_│=+│`~│
;; └┬┴┬┴┬┴┬┴┬┴┬┴┬┴┬┴┬┴┬┴┬┴┬┴┬┘
;;   │ㅃ│ㅉ│ㄸ│ㄲ│ㅆ│  │  │  │ㅒ│ㅖ│{ │} │
;;   │ㅂ│ㅈ│ㄷ│ㄱ│ㅅ│ㅛ│ㅕ│ㅑ│ㅐ│ㅔ│[ │] │
;;   └┬┴┬┴┬┴┬┴┬┴┬┴┬┴┬┴┬┴┬┴┬┴┬┴┐
;;     │ㅁ│ㄴ│ㅇ│ㄹ│ㅎ│ㅗ│ㅓ│ㅏ│ㅣ│;:│'"│\|│
;;     └┬┴┬┴┬┴┬┴┬┴┬┴┬┴┬┴┬┴┬┴┬┴─┘
;;       │ㅋ│ㅌ│ㅊ│ㅍ│ㅠ│ㅜ│ㅡ│,<│.>│/?│
;;       └─┴─┴─┴─┴─┴─┴─┴─┴─┴─┘

(input-method ko han2)

(description
 "Hangul input method with 2벌식.
This input method uses this keyboard layout:
  KEY:	r R s e E f a q Q t T d w W c z x v g k i j p P u h y n b m l o O
  한글:ㄱㄲㄴㄷㄸㄹㅁㅂㅃㅅㅆㅇㅈㅉㅊㅋㅌㅍㅎㅏㅑㅓㅔㅖㅕㅗㅛㅜㅠㅡㅣㅐㅒ
")

(title "한2")

(macro
 (compose
  ;; Convert Jamo sequence LV[T] into 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)))))

(map
 (L-or-T-head
  ("q") ("Q") ("w") ("W") ("e") ("E") ("r") ("R") ("t") ("T")
  ("a") ("s") ("S") ("d") ("D") ("f") ("F") ("g") ("G")
  ("z") ("x") ("c") ("C") ("v"))

 (L-or-T
  ;; Set L1 to CHOSEONG, T to JONGSEONG.
  ("r" (set L1 ?ᄀ) (set T ?ᆨ) ?ㄱ (compose))
  ("R" (set L1 ?ᄁ) (set T ?ᆩ) ?ㄲ (compose))
  ("s" (set L1 ?ᄂ) (set T ?ᆫ) ?ㄴ (compose))
  ("e" (set L1 ?ᄃ) (set T ?ᆮ) ?ㄷ (compose))
  ("f" (set L1 ?ᄅ) (set T ?ᆯ) ?ㄹ (compose))
  ("a" (set L1 ?ᄆ) (set T ?ᆷ) ?ㅁ (compose))
  ("q" (set L1 ?ᄇ) (set T ?ᆸ) ?ㅂ (compose))
  ("t" (set L1 ?ᄉ) (set T ?ᆺ) ?ㅅ (compose))
  ("T" (set L1 ?ᄊ) (set T ?ᆻ) ?ㅆ (compose))
  ("d" (set L1 ?ᄋ) (set T ?ᆼ) ?ㅇ (compose))
  ("w" (set L1 ?ᄌ) (set T ?ᆽ) ?ㅈ (compose))
  ("c" (set L1 ?ᄎ) (set T ?ᆾ) ?ㅊ (compose))
  ("z" (set L1 ?ᄏ) (set T ?ᆿ) ?ㅋ (compose))
  ("x" (set L1 ?ᄐ) (set T ?ᇀ) ?ㅌ (compose))
  ("v" (set L1 ?ᄑ) (set T ?ᇁ) ?ㅍ (compose))
  ("g" (set L1 ?ᄒ) (set T ?ᇂ) ?ㅎ (compose))
  ;;("sr" (set L1 ?ᄓ) (set T ?ᇅ) ?ᄓ (compose))
  ;;("se" (set L1 ?ᄕ) (set T ?ᇆ) ?ㅦ (compose))
  ;;("er" (set L1 ?ᄗ) (set T ?ᇊ) ?ᄗ (compose))
  ;;("fs" (set L1 ?ᄘ) (set T ?ᇍ) ?ᄘ (compose))
  ;;("F" (set L1 ?ᄙ) (set T ?ᇐ) ?ᄙ (compose))
  ;;("fg" (set L1 ?ᄚ) (set T ?ᆶ) ?ㅀ (compose))
  ;;("aq" (set L1 ?ᄜ) (set T ?ᇜ) ?ㅮ (compose))
  ;;("ad" (set L1 ?ᄝ) (set T ?ᇢ) ?ㅱ (compose))
  ;;("qt" (set L1 ?ᄡ) (set T ?ᆹ) ?ㅄ (compose))
  ;;("qv" (set L1 ?ᄪ) (set T ?ᇤ) ?ᄪ (compose))
  ;;("qd" (set L1 ?ᄫ) (set T ?ᇦ) ?ㅸ (compose))
  ;;("tr" (set L1 ?ᄭ) (set T ?ᇧ) ?ㅺ (compose))
  ;;("te" (set L1 ?ᄯ) (set T ?ᇨ) ?ㅼ (compose))
  ;;("tf" (set L1 ?ᄰ) (set T ?ᇩ) ?ᄰ (compose))
  ;;("tq" (set L1 ?ᄲ) (set T ?ᇪ) ?ㅽ (compose))
  ;;("t/" (set L1 ?ᅀ) (set T ?ᇫ) ?ㅿ (compose))
  ;;("dr" (set L1 ?ᅁ) (set T ?ᇬ) ?ᅁ (compose))
  ;;("dt" (set L1 ?ᅅ) (set T ?ᇱ) ?ㆂ (compose))
  ;;("dt/" (set L1 ?ᅆ) (set T ?ᇲ) ?ㆃ (compose))
  ;;("d/" (set L1 ?ᅌ) (set T ?ᇰ) ?ㆁ (compose))
  ;;("vq" (set L1 ?ᅖ) (set T ?ᇳ) ?ᅖ (compose))
  ;;("vd" (set L1 ?ᅗ) (set T ?ᇴ) ?ㆄ (compose))
  ;;("g/" (set L1 ?ᅙ) (set T ?ᇹ) ?ㆆ (compose))
  )

 (L
  ;; Set L to CHOSEONG.
  ("rr" (set L ?ᄁ) ?ㄲ)
  ("E" (set L ?ᄄ) ?ㄸ)
  ("ee" (set L ?ᄄ) ?ㄸ)
  ("Q" (set L ?ᄈ) ?ㅃ)
  ("qq" (set L ?ᄈ) ?ㅃ)
  ("tt" (set L ?ᄊ) ?ㅆ)
  ("W" (set L ?ᄍ) ?ㅉ)
  ("ww" (set L ?ᄍ) ?ㅉ)
  ;;("S" (set L ?ᄔ) ?ㅥ)
  ;;("sq" (set L ?ᄖ) ?ᄖ)
  ;;("fd" (set L ?ᄛ) ?ᄛ)
  ;;("qr" (set L ?ᄞ) ?ㅲ)
  ;;("qs" (set L ?ᄟ) ?ᄟ)
  ;;("qe" (set L ?ᄠ) ?ㅳ)
  ;;("qtr" (set L ?ᄢ) ?ㅴ)
  ;;("qte" (set L ?ᄣ) ?ㅵ)
  ;;("qtq" (set L ?ᄤ) ?ᄤ)
  ;;("qtt" (set L ?ᄥ) ?ᄥ)
  ;;("qtw" (set L ?ᄦ) ?ᄦ)
  ;;("qw" (set L ?ᄧ) ?ㅶ)
  ;;("qc" (set L ?ᄨ) ?ᄨ)
  ;;("qx" (set L ?ᄩ) ?ㅷ)
  ;;("Qd" (set L ?ᄬ) ?ㅹ)
  ;;("ts" (set L ?ᄮ) ?ㅻ)
  ;;("ta" (set L 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) ?ㅏ (compose))
  ("o" (set V ?ᅢ) ?ㅐ (compose))
  ("i" (set V ?ᅣ) ?ㅑ (compose))
  ("O" (set V ?ᅤ) ?ㅒ (compose))
  ("j" (set V ?ᅥ) ?ㅓ (compose))
  ("p" (set V ?ᅦ) ?ㅔ (compose))
  ("u" (set V ?ᅧ) ?ㅕ (compose))
  ("P" (set V ?ᅨ) ?ㅖ (compose))
  ("h" (set V ?ᅩ) ?ㅗ (compose))
  ("hk" (set V ?ᅪ) ?ㅘ (compose))
  ("ho" (set V ?ᅫ) ?ㅙ (compose))
  ("hl" (set V ?ᅬ) ?ㅚ (compose))
  ("y" (set V ?ᅭ) ?ㅛ (compose))
  ("n" (set V ?ᅮ) ?ㅜ (compose))
  ("nj" (set V ?ᅯ) ?ㅝ (compose))
  ("np" (set V ?ᅰ) ?ㅞ (compose))
  ("nl" (set V ?ᅱ) ?ㅟ (compose))
  ("b" (set V ?ᅲ) ?ㅠ (compose))
  ("m" (set V ?ᅳ) ?ㅡ (compose))
  ("ml" (set V ?ᅴ) ?ㅢ (compose))
  ("l" (set V ?ᅵ) ?ㅣ (compose))
  ;;("kh" (set V ?ᅶ) ?ᅶ (compose))
  ;;("kn" (set V ?ᅷ) ?ᅷ (compose))
  ;;("ih" (set V ?ᅸ) ?ᅸ (compose))
  ;;("iy" (set V ?ᅹ) ?ᅹ (compose))
  ;;("jh" (set V ?ᅺ) ?ᅺ (compose))
  ;;("jn" (set V ?ᅻ) ?ᅻ (compose))
  ;;("jm" (set V ?ᅼ) ?ᅼ (compose))
  ;;("uh" (set V ?ᅽ) ?ᅽ (compose))
  ;;("un" (set V ?ᅾ) ?ᅾ (compose))
  ;;("hj" (set V ?ᅿ) ?ᅿ (compose))
  ;;("hp" (set V ?ᆀ) ?ᆀ (compose))
  ;;("hP" (set V ?ᆁ) ?ᆁ (compose))
  ;;("hh" (set V ?ᆂ) ?ᆂ (compose))
  ;;("hn" (set V ?ᆃ) ?ᆃ (compose))
  ;;("yi" (set V ?ᆄ) ?ㆇ (compose))
  ;;("yO" (set V ?ᆅ) ?ㆈ (compose))
  ;;("yu" (set V ?ᆆ) ?ᆆ (compose))
  ;;("yh" (set V ?ᆇ) ?ᆇ (compose))
  ;;("yl" (set V ?ᆈ) ?ㆉ (compose))
  ;;("nk" (set V ?ᆉ) ?ᆉ (compose))
  ;;("no" (set V ?ᆊ) ?ᆊ (compose))
  ;;("njm" (set V ?ᆋ) ?ᆋ (compose))
  ;;("nu" (set V ?ᆌ) ?ᆌ (compose))
  ;;("nn" (set V ?ᆍ) ?ᆍ (compose))
  ;;("bk" (set V ?ᆎ) ?ᆎ (compose))
  ;;("bj" (set V ?ᆏ) ?ᆏ (compose))
  ;;("bp" (set V ?ᆐ) ?ᆐ (compose))
  ;;("bu" (set V ?ᆑ) ?ㆊ (compose))
  ;;("bP" (set V ?ᆒ) ?ㆋ (compose))
  ;;("bn" (set V ?ᆓ) ?ᆓ (compose))
  ;;("bl" (set V ?ᆔ) ?ㆌ (compose))
  ;;("mn" (set V ?ᆕ) ?ᆕ (compose))
  ;;("mm" (set V ?ᆖ) ?ᆖ (compose))
  ;;("mln" (set V ?ᆗ) ?ᆗ (compose))
  ;;("lk" (set V ?ᆘ) ?ᆘ (compose))
  ;;("li" (set V ?ᆙ) ?ᆙ (compose))
  ;;("lh" (set V ?ᆚ) ?ᆚ (compose))
  ;;("ln" (set V ?ᆛ) ?ᆛ (compose))
  ;;("lm" (set V ?ᆜ) ?ᆜ (compose))
  ;;("lK" (set V ?ᆝ) ?ᆝ (compose))
  ;;("K" (set V ?ᆞ) ?ᆞ (compose))
  ;;("Kj" (set V ?ᆟ) ?ᆟ (compose))
  ;;("Kn" (set V ?ᆠ) ?ᆠ (compose))
  ;;("Kl" (set V ?ᆡ) ?ᆡ (compose))
  ;;("KK" (set V ?ᆢ) ?ᆢ (compose))
  )

 (T-or-TL
  ;; Set T to JONGSEONG, T1 to the alternate JONGSEONG.
  ("rr" (set T ?ᆩ) (set T1 ?ᆨ) ?ㄲ (compose))
  ("rt" (set T ?ᆪ) (set T1 ?ᆨ) ?ㄳ (compose))
  ("sw" (set T ?ᆬ) (set T1 ?ᆫ) ?ㄵ (compose))
  ("sg" (set T ?ᆭ) (set T1 ?ᆫ) ?ㄶ (compose))
  ("fr" (set T ?ᆰ) (set T1 ?ᆯ) ?ㄺ (compose))
  ("fa" (set T ?ᆱ) (set T1 ?ᆯ) ?ㄻ (compose))
  ("fq" (set T ?ᆲ) (set T1 ?ᆯ) ?ㄼ (compose))
  ("ft" (set T ?ᆳ) (set T1 ?ᆯ) ?ㄽ (compose))
  ("fx" (set T ?ᆴ) (set T1 ?ᆯ) ?ㄾ (compose))
  ("fv" (set T ?ᆵ) (set T1 ?ᆯ) ?ㄿ (compose))
  ("fg" (set T ?ᆶ) (set T1 ?ᆯ) ?ㅀ (compose))
  ("qt" (set T ?ᆹ) (set T1 ?ᆸ) ?ㅄ (compose))
  ("tt" (set T ?ᆻ) (set T1 ?ᆺ) ?ㅄ (compose))
  ;;("rf" (set T ?ᇃ) ?ᇃ (compose))
  ;;("rtr" (set T ?ᇄ) ?ᇄ (compose))
  ;;("st" (set T ?ᇇ) ?ㅧ (compose))
  ;;("st/" (set T ?ᇈ) ?ㅨ (compose))
  ;;("sx" (set T ?ᇉ) ?ᇉ (compose))
  ;;("ef" (set T ?ᇋ) ?ᇋ (compose))
  ;;("frt" (set T ?ᇌ) ?ᇌ (compose))
  ;;("fe" (set T ?ᇎ) ?ㅪ (compose))
  ;;("feg" (set T ?ᇏ) ?ᇏ (compose))
  ;;("far" (set T ?ᇑ) ?ᇑ (compose))
  ;;("fat" (set T ?ᇒ) ?ᇒ (compose))
  ;;("fqt" (set T ?ᇓ) ?ㅫ (compose))
  ;;("fqg" (set T ?ᇔ) ?ᇔ (compose))
  ;;("fqd" (set T ?ᇕ) ?ᇕ (compose))
  ;;("ftt" (set T ?ᇖ) ?ᇖ (compose))
  ;;("ft/" (set T ?ᇗ) ?ㅬ (compose))
  ;;("fz" (set T ?ᇘ) ?ᇘ (compose))
  ;;("fMd" (set T ?ᇙ) ?ㅭ (compose))
  ;;("ar" (set T ?ᇚ) ?ᇚ (compose))
  ;;("af" (set T ?ᇛ) ?ᇛ (compose))
  ;;("at" (set T ?ᇝ) ?ㅯ (compose))
  ;;("aT" (set T ?ᇞ) ?ᇞ (compose))
  ;;("at/" (set T ?ᇟ) ?ㅰ (compose))
  ;;("ac" (set T ?ᇠ) ?ᇠ (compose))
  ;;("ag" (set T ?ᇡ) ?ᇡ (compose))
  ;;("qf" (set T ?ᇣ) ?ᇣ (compose))
  ;;("qg" (set T ?ᇥ) ?ᇥ (compose))
  ;;("dR" (set T ?ᇭ) ?ᇭ (compose))
  ;;("dt/" (set T ?ᇮ) ?ㆃ (compose))
  ;;("dz" (set T ?ᇯ) ?ᇯ (compose))
  ;;("gs" (set T ?ᇵ) ?ᇵ (compose))
  ;;("gf" (set T ?ᇶ) ?ᇶ (compose))
  ;;("ga" (set T ?ᇷ) ?ᇷ (compose))
  ;;("gq" (set T ?ᇸ) ?ᇸ (compose))
  )

 (special
  ("J" (set V ?ᅠ) ?ᅠ (compose)))

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

(state
 (init
  ;; This is to suppress compose action.
  (t (set L 0))
  (L-or-T (set L L1) (set T 0x11A7) (shift state-L))
  (L (set T 0x11A7) (shift state-L))
  (V)
  (special)
  (backspace))
 (state-L
  (V (shift state-LV))
  (backspace))
 (state-LV
  (L-or-T (shift state-LVL-or-LVT))
  (T-or-TL (shift state-LVT-or-LVTL))
  (backspace))

 (state-LVL-or-LVT
  (L-or-T-head (delete @<) (pushback 0) (set L 0) (shift state-fix-LVT))
  (V-head (delete @<) (pushback 0) (set L 0) (shift state-fix-LV))
  (backspace))
 (state-fix-LVT
  (L-or-T (set L L1) (set T 0x11A7) (shift state-fix-LVT-2))
  (L (set T 0x11A7) (shift state-fix-LVT-2)))
 (state-fix-LVT-2
  (V (shift state-fix-LVT-3)))
 (state-fix-LVT-3
  (L-or-T (shift init)))
 (state-fix-LV
  (L-or-T (set L L1) (set T 0x11A7) (shift state-fix-LV-2))
  (L (set T 0x11A7) (shift state-fix-LV-2)))
 (state-fix-LV-2
  (V (shift init)))

 ;; e.g. "rkrt"
 (state-LVT-or-LVTL
  (L-or-T-head ;; e.g. "rkrtr"; make the last "r" start a new composing.
   (pushback 1) (shift init))
  (V-head ;; e.g. "rkrth"; make the last "th" start a new composing.
   (delete @<) (set T T1) (compose) (pushback 2) (shift init))
  (backspace)))

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