;; vi-base.mim -- Provide bases for Vietnamese Input method
;; Copyright (C) 2008, 2009
;;   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 t nil vi-base)

(description "Provide bases for Vietnamese input methods.
This is acutually not a standalone input method, but is expected
to be included in the other Vietnamese input method (e.g. vi-telex, vi-vni).
")

;; Usage of variables:
;; M (mark): point after the last vowel
;; T (mark): point after the tone-mark-attached vowel
;; TPLACE: On which vowel a tone is attached (0:none, 1:1st, 2:2nd, or 3:3rd)
;; C: The initial consonant
;; V-1: The last vowel
;; V-2: The second from the last vowel (or 0 if there aren't two vowels)
;; V-3: The third from the last vowel (or 0 if there aren't three vowels)
;; C-AFTER-V: Number of the final consonants
;; SELECT: Which tone is selected

(macro
 (handle-mark
  ;; At first, cancel the tone mark if already put.
  (move T) (select 0)
  (move M)
  (cond (V-3
	 ;; Tripple-vowel
	 ;; Put a tone mark on the last vowel if it is ê, or ơ.
	 ;; Otherwise put a tone mark on the second vowel.
	 (cond ((| (= V-1 ?ê) (= V-1 ?ơ) (= V-1 ?Ê) (= V-1 ?Ơ))
		(set TPLACE 3))
	       (1 (move @-) (set TPLACE 2))))
	(V-2
	 ;; Double-vowel
	 ;; Put a tone mark on the last vowel in the following cases:
	 ;;  * The last vowel is  â, ă, ê, ô, ơ, ư, or ư (i.e. non-ASCII).
	 ;;  * The initial consonant and the first vowel is "gi" or "qu".
	 ;;  * tone-mark-on-last is customized to 1 in such equivocal
	 ;;    cases as "oa", "oe", "oo", and "uy".
	 ;; Otherwise put a tone mark on the first vowel.
	 (cond ((| (> V-1 127)
		   (& (| (= C ?g) (= C ?G)) (| (= V-2 ?i) (= V-2 ?I)))
		   (& (| (= C ?q) (= C ?Q)) (| (= V-2 ?u) (= V-2 ?U)))
		   (& (| tone-mark-on-last (> C-AFTER-V 0))
		      (| (& (| (= V-2 ?o) (= V-2 ?O))
			    (| (= V-1 ?a) (= V-1 ?A) (= V-1 ?e) (= V-1 ?E)
			       (= V-1 ?o) (= V-1 ?O)))
			 (& (| (= V-2 ?u) (= V-2 ?U))
			    (| (= V-1 ?y) (= V-1 ?Y))))))
		(set TPLACE 2))
	       (1 (move @-) (set TPLACE 1))))
	(1 (set TPLACE 1)))
  (select SELECT)
  (mark T)
  (move @>)))

(map
 ;; These must be defined in each input method.
 ;; (vowel-ext)
 ;; (consonant-ext)
 ;; (tone-mark)
 ;; (consonant-or-tone-mark)
 ;; (temporary-escape)

 (vowel
  ("a"  ("aáàảãạ")) ("A"  ("AÁÀẢÃẠ"))
  ("e"  ("eéèẻẽẹ")) ("E"  ("EÉÈẺẼẸ"))
  ("i"  ("iíìỉĩị")) ("I"  ("IÍÌỈĨỊ"))
  ("o"  ("oóòỏõọ")) ("O"  ("OÓÒỎÕỌ"))
  ("u"  ("uúùủũụ")) ("U"  ("UÚÙỦŨỤ"))
  ("y"  ("yýỳỷỹỵ")) ("Y"  ("YÝỲỶỸỴ")))

 (consonant
  ("b" ?b) ("c" ?c) ("d" ?d) ("g" ?g) ("h" ?h) ("k" ?k) ("l" ?l) ("m" ?m)
  ("n" ?n) ("p" ?p) ("q" ?q) ("t" ?t) ("v" ?v)
  ("B" ?B) ("C" ?C) ("D" ?D) ("G" ?G) ("H" ?H) ("K" ?K) ("L" ?L) ("M" ?M)
  ("N" ?N) ("P" ?P) ("Q" ?Q) ("T" ?T) ("V" ?V))
 ;;Khi cac phu am nay di sau nguyen am thi nen thoat ve tieng Anh
 ;;Should switch to English when these consonants are after any vowel (set temporary-escape)  
 (ending-1st-notviet-consonant ;
  ("q" ?q) ("Q" ?Q) ("d" ?d) ("D" ?D) ("g" ?g) ("G" ?G) ("h" ?h) ("H" ?H)
  ("k" ?k) ("K" ?K) ("l" ?l) ("L" ?L) ("v" ?v) ("V" ?V) ("b" ?b) ("B" ?B) ("z" ?z) ("Z" ?Z))
 ;; These are the consonants that can end a word, anything else is not VNese
 (ending-1st-consonant ; (e.g. tốt, đẹp, xấu)
  ("c" ?c) ("m" ?m) ("n" ?n) ("p" ?p) ("t" ?t)
  ("C" ?C) ("M" ?M) ("N" ?N) ("P" ?P) ("T" ?T))
 (ending-2nd-consonant ; Can only go with 'n' or 'c' (e.g. sóng, sang, ích)
  ("g" ?g) ("h" ?h)
  ("G" ?G) ("H" ?H))

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

 (smart-quotes
   ("'" ?\')
   ("''" "‘’")
   ("\"" ?\")
   ("\"\"" "“”"))

 (permanent-escape
  ("\\"))

 (backspace
  ((BackSpace) (cond (backspace-is-undo (undo))))))

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