;; ta-lk-renganathan.mim -- Tamil input method with Renganathan layout
;; Copyright (C) 2008
;;   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 ta lk-renganathan (version "1.6.0"))

(description "Tamil input method with Renganathan layout.
For the detail, see the page: <http://www.locallanguages.lk/>
")

(title "க")

(variable
 (use-surrounding-text (_"Surrounding text vs. preedit
If 1, try to use surrounding text.  Otherwise, use preedit.")
		       0 1 0))

(macro
 (pre-proc
  (cond
   ((= @-1 0x200C)
    (delete @-1)))
  (cond
   ((= @+0 0x200C)
    (delete @+1))))

 (post-proc
  (cond
   ((& (>= @-1 0x0B95) (<= @-1 0x0BB9)	; a consonant
       (>= @+0 0x0BC6) (<= @+0 0x0BCC))	; a left/two-part vowel sign
    ;; prevent syllable formation
    0x200C)

   ((& (| (< @-2 0x0B95) (> @-2 0x0BB9))  ; not a consonant
       (>= @-1 0x0BC6) (<= @-1 0x0BCC)	  ; a left/two-part vowel sign
       (>= @+0 0x0B95) (<= @+0 0x0BB9)	  ; a consonant
       (| (< @+1 0x0BBE) (> @+1 0x0BD7))) ; not a vowel sign
    ;; exchange @-1 and @+0
    (set vs @-1)
    (set c @+0)
    (delete @-1)
    (delete @+1)
    (insert c)
    (insert vs))

   ((& (= @-1 0x200C)
       (| (< @-2 0x0B95) (> @-2 0x0BB9)	; not a constant
	  (< @+0 0x0BC6) (> @+0 0x0BCC))) ; not a left/two-part vowel sign
    ;; delete unnecessary ZWNJ
    (delete @-1))

   ((& (= @+0 0x200C)
       (| (< @-1 0x0B95) (> @-1 0x0BB9)	; not a constant
	  (< @+1 0x0BC6) (> @+1 0x0BCC))) ; not a left/two-part vowel sign
    ;; delete unnecessary ZWNJ
    (delete @+1))))

 (consonant-proc
  (move @<)
  (cond
   ((= @-1 0x200C)
    (delete @-1)))
  (set vs 0)
  (cond
   ((& (>= @-1 0x0BC6) (<= @-1 0x0BC8))	; a left vowel sign
    (cond
     ((= @-2 0x200C)
      (set vs @-1)
      (delete @-2))
     ((| (< @-2 0x0B95) (> @-2 0x0BB9))	; not a consonant
      (set vs @-1)
      (delete @-1)))))

  (move @>)
  (cond
   ((& (= @+0 0x200C)
       (| (< @+1 0x0BC6) (> @+1 0x0BCC))) ; not a left vowel sign
    (delete @+1)))

  (cond
   ((= vs 0x0BC6)
    (cond
     ((= @+0 0x0BBE)
      (delete @+1)
      0x0BCA)
     ((| (= @+0 0x0BB3) (= @+0 0x0BD7))
      (delete @+1)
      0x0BCC)
     (1
      0x0BC6)))
   ((= vs 0x0BC7)
    (cond
     ((= @+0 0x0BBE)
      (delete @+1)
      0x0BCB)
     (1
      0x0BC7)))
   ((= vs 0x0BC8)
    0x0BC8))

  (cond
   ((& (>= @-1 0x0B95) (<= @-1 0x0BB9)	; a consonant
       (>= @+0 0x0BC6) (<= @+0 0x0BCC))	; a left vowel sign
    0x200C)))

 (check-surrounding-text
  (cond
   ((& (= @+0 0x0B95) (= @+1 0x0BCD) (= @+2 0x0BB7)) ; k.ssa follows
    0x0B95 0x0BCD 0x0BB7
    (delete @+3))
   ((& (>= @+0 0x0B95) (<= @+0 0x0BB9))	; a consonant follows
    (set c @+0)
    (insert c)
    (delete @+1))
   ((& (>= @-1 0x0B95) (<= @-1 0x0BB9))	; a consonant precedes
    0x200C)))
 )

(map
 (starter
  ("q") ("w") ("e") ("r") ("t") ("y") ("u") ("i") ("o") ("p") ("[") ("]")
  ("a") ("s") ("d") ("f") ("g") ("h") ("j") ("k") ("l") (";")
  ("z") ("x") ("c") ("v") ("b") ("n") ("m") ("`")
  ("Q") ("W") ("E") ("R") ("T") ("Y") ("U") ("I") ("O") ("P") ("{") ("}")
  ("A") ("S") ("D") ("F") ("G") ("H") ("J") ("K") ("L")
  ("Z") ("X") ("C") ("V") ("B") ("N") ("M")
  ((A-q)) ((A-w)) ((A-e)) ((A-r)) ((A-t)) ((A-y)) ((A-u)) ((A-i))
  ((A-1)) ((A-2)) ((A-3)) ((A-4)) ((A-5)) ((A-6)) ((A-7)) ((A-8)) ((A-9))
  ((A-0)) ((A-`)) ((A-=)) ((A--))
  ((BackSpace)) ((Delete)))

 (composite
  ("f;" "க்")				; 0B95 0BCD
  ("f;W" "க்‌ஷ")			; 0B95 0BCD 200C 0BB7
  ("Zh" "ஒளா")				; 0B92 0BB3 0BBE
  ("Zp" "ஒளி")				; 0B92 0BB3 0BBF
  ("ZP" "ஒளீ")				; 0B92 0BB3 0BC0
  ("Z[" "ஒளு")				; 0B92 0BB3 0BC1
  ("Z{" "ஒளூ")				; 0B92 0BB3 0BC2
  ("Z;" "ஒள்")				; 0B92 0BB3 0BCD
  ("xs" "ஔ")				; 0B94
  ("xsh" "ஒளா")				; 0B92 0BB3 0BBE
  ("xsp" "ஒளி")				; 0B92 0BB3 0BBF
  ("xsP" "ஒளீ")				; 0B92 0BB3 0BC0
  ("xs[" "ஒளு")				; 0B92 0BB3 0BC1
  ("xs{" "ஒளூ")				; 0B92 0BB3 0BC2
  ("xs;" "ஒள்")				; 0B92 0BB3 0BCD
  )

 (consonant
  ("f" "க")				; 0B95
  ("q" "ங")				; 0B99
  ("r" "ச")				; 0B9A
  ("E" "ஜ")				; 0B9C
  ("]" "ஞ")				; 0B9E
  ("l" "ட")				; 0B9F
  ("z" "ண")				; 0BA3
  ("j" "த")				; 0BA4
  ("e" "ந")				; 0BA8
  ("d" "ன")				; 0BA9
  ("g" "ப")				; 0BAA
  ("k" "ம")				; 0BAE
  ("a" "ய")				; 0BAF
  ("u" "ர")				; 0BB0
  ("w" "ற")				; 0BB1
  ("y" "ல")				; 0BB2
  ("o" "ழ")				; 0BB4
  ("t" "வ")				; 0BB5
  ("U" "ஶ")				; 0BB6
  ("Q" "ஸ")				; 0BB8
  ("R" "ஹ")				; 0BB9

  ("T" "க்ஷ")				; 0B95 0BCD 0BB7
  )

 (sign
  ("p" "ி")				; 0BBF
  ("P" "ீ")				; 0BC0
  ("[" "ு")				; 0BC1
  ("{" "ூ")				; 0BC2
  (";" "்")				; 0BCD
  )

 (lla
  ("s"))

 (ssa
  ("W"))

 (aa
  ("h"))

 (e
  ("n"))

 (ee
  ("N"))

 (ai
  ("i"))

 (backspace
  ((BackSpace)))

 (delete
  ((Delete)))

 (misc
  ("`" "ஃ")				; 0B83
  ("m" "அ")				; 0B85
  ("M" "ஆ")				; 0B86
  ("b" "இ")				; 0B87
  ("B" "ஈ")				; 0B88
  ("c" "உ")				; 0B89
  ("C" "ஊ")				; 0B8A
  ("v" "எ")				; 0B8E
  ("V" "ஏ")				; 0B8F
  ("I" "ஐ")				; 0B90
  ("x" "ஒ")				; 0B92
  ("X" "ஓ")				; 0B93
  ("Z" "ஔ")				; 0B94

  ("O" "ௐ")				; 0BD0

  ((A-0) "௦")				; 0BE6
  ((A-1) "௧")				; 0BE7
  ((A-2) "௨")				; 0BE8
  ((A-3) "௩")				; 0BE9
  ((A-4) "௪")				; 0BEA
  ((A-5) "௫")				; 0BEB
  ((A-6) "௬")				; 0BEC
  ((A-7) "௭")				; 0BED
  ((A-8) "௮")				; 0BEE
  ((A-9) "௯")				; 0BEF
  ((A-q) "௰")				; 0BF0
  ((A-w) "௱")				; 0BF1
  ((A-e) "௲")				; 0BF2
  ((A-y) "௳")				; 0BF3
  ((A-u) "௴")				; 0BF4
  ((A-i) "௵")				; 0BF5
  ((A-r) "௶")				; 0BF6
  ((A-t) "௷")				; 0BF7
  ((A-`) "௸")				; 0BF8
  ((A-=) "௹")				; 0BF9
  ((A--) "௺")				; 0BFA

  ("Y" "ஶ்ரீ")				; 0BB6 0BCD 0BB0 0BC0

  ("}" ";")				; 003B

  ("A" "")
  ("S" "")
  ("D" "")
  ("F" "")
  ("G" "")
  ("H" "")
  ("J" "")
  ("K" "")
  ("L" "")
  ))

(state
 (init
  (starter
   (pushback 1)
   (cond
    ((& (= use-surrounding-text 1) (= @-0 -1))
     (shift surrounding-text))
    (1
     (shift preedit)))))

 (surrounding-text
  (consonant
   (consonant-proc)
   (commit))

  (lla
   (cond
    ((= @-1 0x200C)
     (delete @-1)))

   (cond
    ((& (>= @-2 0x0B95) (<= @-2 0x0BB9)	; a consonant
	(= @-1 0x0BC6))
     (delete @-1)
     0x0BCC
     (cond
      ((= @+0 0x200C)
       (delete @+1))))
    ((= @-1 0x0B92)
     (delete @-1)
     0x0B94
     (cond
      ((= @+0 0x200C)
       (delete @+1))))
    (1
     0x0BB3
     (consonant-proc)))

   (commit))

  (ssa
   (cond
    ((= @-1 0x200C)
     (delete @-1)))

   (cond
    ((& (= @-2 0x0B95) (= @-1 0x0BCD))
     0x200C))
   0x0BB7
   (consonant-proc)

   (commit))

  (sign
   (cond
    ((= @+0 0x200C)
     (delete @+1)))
   (move @<)
   (cond
    ((= @-1 0x200C)
     (delete @-1)))

   (cond
    ((= @-1 0x0B94)
     (delete @-1)
     0x0B92 0x0BB3)
    ((= @-1 0x0BCC)
     (delete @-1)
     0x0BC6 0x0BB3))

   (commit))

  (aa
   (pre-proc)

   (cond
    ((= @-1 0x0BC6)
     (cond
      ((& (| (< @-3 0x0B95) (> @-3 0x0BB9)) ; not a consonant
	  (= @-2 0x200C))
       (delete @-2))
      (1
       (delete @-1)))
     0x0BCA)

    ((= @-1 0x0BC7)
     (cond
      ((& (| (< @-3 0x0B95) (> @-3 0x0BB9)) ; not a consonant
	  (= @-2 0x200C))
       (delete @-2))
      (1
       (delete @-1)))
     0x0BCB)

    ((= @-1 0x0B94)
     (delete @-1)
     0x0B92 0x0BB3 0x0BBE)

    ((= @-1 0x0BCC)
     (delete @-1)
     0x0BC6 0x0BB3 0x0BBE)

    (1
     0x0BBE))

   (commit))

  (e
   (pre-proc)

   (cond
    ;; additional check-surrounding-text for this vowel sign
    ((& (>= @+0 0x0B95) (<= @+0 0x0BB9)	; a consonant
	(= @+1 0x0BB3))
     (set c @+0)
     (delete @+2)
     (insert c)
     0x0BCC)

    ;; ordinary check-surrounding-text
    (1
     (check-surrounding-text)
     (cond
      ((= @+0 0x0BBE)
       (delete @+1)
       0x0BCA)
      ((= @+0 0x0BD7)
       (delete @+1)
       0x0BCC)
      (1
       0x0BC6))))

   (commit))

  (ee
   (pre-proc)

   (check-surrounding-text)
   (cond
    ((= @+0 0x0BBE)
     (delete @+1)
     0x0BCB)
    (1
     0x0BC7))

   (commit))

  (ai
   (pre-proc)

   (check-surrounding-text)
   0x0BC8

   (commit))

  (backspace
   (pre-proc)

   (cond
    ((& (>= @-4 0x0B95) (<= @-4 0x0BB9)	; a consonant
	(= @-3 0x0BC6)
	(= @-2 0x0BB3)
	(| (& (>= @-1 0x0BBE) (<= @-1 0x0BC2)) ; a right/above/below vowel sign
	   (= @-1 0x0BCD)))
     (delete @-3)
     0x0BCC)

    ((& (= @-4 0x0BB6) (= @-3 0x0BCD) (= @-2 0x0BB0) (= @-1 0x0BC0)) ; shrii
     (delete @-4))

    ((& (= @-3 0x0B92)
	(= @-2 0x0BB3)
	(| (& (>= @-1 0x0BBE) (<= @-1 0x0BC2)) ; a right/above/below vowel sign
	   (= @-1 0x0BCD)))
     (delete @-3)
     0x0B94)

    ((& (= @-3 0x0B95) (= @-2 0x0BCD) (= @-1 0x0BB7)) ; k.ssa
     (delete @-3))

    ((| (= @-1 0x0BCA) (= @-1 0x0BCC))	; vowel sign e/ai
     (delete @-1)
     0x0BC6)

    ((= @-1 0x0BCB)			; vowel sign ee
     (delete @-1)
     0x0BC7)

    ((& (>= @-1 0x0BC6) (<= @-1 0x0BC8)) ; a left vowel sign
     (set vs @-1)
     (delete @-1)
     (cond
      ((& (>= @-1 0x0B95) (<= @-1 0x0BB9)) ; a consonant
       (cond
	((& (= @-3 0x0B95) (= @-2 0x0BCD) (= @-1 0x0BB7)) ; k.ssa
	 (delete @-3))
	(1
	 (delete @-1)))

       (cond
	((& (>= @+0 0x0B95) (<= @+0 0x0BB9)    ; a consonant follows
	    (| (< @+1 0x0BBE) (> @+1 0x0BD7))) ; not a vowel sign
	 ;; visual  : E C1 | C2  ->  E C2 |
	 ;; logical : C1 E | C2  ->  C2 E |
	 (cond
	  ((& (= @+0 0x0B95) (= @+1 0x0BCD) (= @+2 0x0BB7)) ; k.ssa
	   (delete @+3)
	   0x0B95 0x0BCD 0x0BB7)
	  (1
	   (set c @+0)
	   (delete @+1)
	   (insert c)))
	 (insert vs))

	((& (>= @-1 0x0B95) (<= @-1 0x0BB9)) ; a consonant precedes
	 ;; visual  : C1 E C2 | X  ->  C1 E | X
	 ;; logical : C1 C2 E | X  ->  C1 N E | X
	 0x200C
	 (insert vs))

	(1
	 ;; visual  : X1 E C | X2  ->  X1 E | X2
	 ;; logical : X1 C E | X2  ->  X1 E | X2
	 (insert vs))))

      ((= @-1 0x200C)
       (delete @-1))))

    (1
     (delete @-1)))

   (post-proc)
   (commit))

  (delete
   (pre-proc)

   (cond
    ((& (= @+0 0x0BB6) (= @+1 0x0BCD) (= @+2 0x0BB0) (= @+3 0x0C0)) ; shrii
     (delete @+4))

    ((& (= @+0 0x0B95) (= @+1 0x0BCD) (= @+2 0x0BB7)) ; k.ssa
     (cond
      ((& (>= @+3 0x0BC6) (<= @+3 0x0BC8)) ; a left vowel sign
       (delete @+4)
       0x0B95 0x0BCD 0x0BB7)
      ((| (= @+3 0x0BCA) (= @+3 0x0BCB)) ; vowel sign e/ee
       (delete @+4)
       0x0B95 0x0BCD 0x0BB7 0x0BBE)
      ((| (& (>= @+3 0x0BBE) (<= @+3 0x0BC2)) ; vowel sign aa .. uu
	  (= @+3 0x0BCC)		      ; vowel sign ai
	  (= @+3 0x0BCD))		      ; virama
       (delete @+4))
      (1
       (delete @+3))))

    ((& (>= @+0 0x0B95) (<= @+0 0x0BB9)) ; a consonant
     (set c @+0)
     (cond
      ((& (>= @+1 0x0BC6) (<= @+1 0x0BC8)) ; a left vowel sign
       (delete @+2)
       (insert c))
      ((| (= @+1 0x0BCA) (= @+1 0x0BCB)) ; vowel sign e/ee
       (delete @+2)
       (insert c)
       0x0BBE)
      ((| (& (>= @+1 0x0BBE) (<= @+1 0x0BC2)) ; vowel sign aa .. uu
	  (= @+1 0x0BCC)		      ; vowel sign ai
	  (= @+1 0x0BCD))		      ; virama
       (delete @+2))
      (1
       (delete @+1))))

    ((| (= @+0 0x0BCA) (= @+0 0x0BCB))	; vowel sign e/ee
     (delete @+1)
     0x0BBE)

    ((= @+0 0x0BCC)			; vowel sign ai
     (delete @+1)
     0x0BB3)

    (1
     (delete @+1)))

   (post-proc)
   (commit))

  (misc
   (move @<)
   (cond
    ((= @-1 0x200C)
     (delete @-1)))
   (move @>)
   (cond
    ((= @+0 0x200C)
     (delete @+1)))
   (commit)))

 (preedit
  (consonant (shift c-state))
  (composite (commit))
  (sign (commit))
  (misc (commit))
  (lla "ள" (shift c-state))		; 0BB3
  (ssa "ஷ" (shift c-state))		; 0BB7
  (aa "ா" (commit))			; 0BBE
  (e  "ெ" (shift e-state))		; 0BC6
  (ee "ே" (shift ee-state))		; 0BC7
  (ai "ை" (shift ai-state))		; 0BC8
  (nil
   (unhandle)))

 (c-state
  (sign (shift c-s-state))
  (aa "ா" (shift c-s-state))		; 0BBE
  (backspace (undo)))

 (c-s-state
  (sign
   (cond
    ((= @-2 0x0BCC)
     (move @-)
     (delete @-)
     0x0BC6 0x0BB3
     (move @+))
    (1
     (commit)
     (shift preedit))))
  (aa
   "ா"
   (cond
    ((= @-2 0x0BCC)
     (move @-)
     (delete @-)
     0x0BC6 0x0BB3
     (move @+))
    (1
     (commit)
     (shift preedit))))
  (backspace (undo)))

 (e-state
  (consonant (move @<) (delete @+) (move @>) "ெ" (shift e-c-state))
  (lla (delete @-) "ளெ" (shift e-c-state))
  (ssa (delete @-) "ஷெ" (shift e-c-state))
  (backspace (delete @-) (shift preedit))
  (nil (undo -1)))

 (ee-state
  (consonant (move @<) (delete @+) (move @>) "ே" (shift ee-c-state))
  (lla (delete @-) "ளே" (shift ee-c-state))
  (ssa (delete @-) "ஷே" (shift ee-c-state))
  (backspace (delete @-) (shift preedit))
  (nil (undo -1)))

 (ai-state
  (consonant (move @<) (delete @+) (move @>) "ை" (shift c-s-state))
  (lla (delete @-) "ளை" (shift c-s-state))
  (ssa (delete @-) "ஷை" (shift c-s-state))
  (backspace (delete @-) (shift preedit))
  (nil (undo -1)))

 (e-c-state
  (aa (delete @-) "ொ" (shift c-s-state))
  (lla (delete @-) "ௌ" (shift c-s-state))
  (backspace (delete @<) "ெ" (shift e-state)))

 (ee-c-state
  (aa (delete @-) "ோ" (shift c-s-state))
  (backspace (delete @<) "ே" (shift ee-state)))
 )

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