]> code.delx.au - gnu-emacs/blob - lisp/language/thai-util.el
Revision: miles@gnu.org--gnu-2005/emacs--unicode--0--patch-28
[gnu-emacs] / lisp / language / thai-util.el
1 ;;; thai-util.el --- utilities for Thai -*- coding: iso-2022-7bit; -*-
2
3 ;; Copyright (C) 1995 Electrotechnical Laboratory, JAPAN.
4 ;; Licensed to the Free Software Foundation.
5 ;; Copyright (C) 2003
6 ;; National Institute of Advanced Industrial Science and Technology (AIST)
7 ;; Registration Number H13PRO009
8
9 ;; Keywords: mule, multilingual, Thai, i18n
10
11 ;; This file is part of GNU Emacs.
12
13 ;; GNU Emacs is free software; you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; any later version.
17
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
22
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs; see the file COPYING. If not, write to the
25 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
26 ;; Boston, MA 02111-1307, USA.
27
28 ;;; Commentary:
29
30 ;;; Code:
31
32 ;; Setting information of Thai characters.
33
34 (defconst thai-category-table (make-category-table))
35 (define-category ?c "Thai consonant" thai-category-table)
36 (define-category ?v "Thai upper/lower vowel" thai-category-table)
37 (define-category ?t "Thai tone mark" thai-category-table)
38 (define-category ?u "Thai tone mark and upper sign" thai-category-table)
39 (define-category ?I "THAI CHARACTER SARA I" thai-category-table)
40 (define-category ?U "THAI CHARACTER THANTHAKHAT" thai-category-table)
41
42 ;; The general composing rules are as follows:
43 ;;
44 ;; T
45 ;; V U V U
46 ;; CV -> C, CU -> C, CVT -> C, Cv -> C, CvU -> C
47 ;; v v
48 ;;
49 ;; where C: consonant, V: vowel upper, v: vowel lower,
50 ;; T: tone mark, U: tone mark and upper sign.
51 ;; Special rule: The sign `\e,Tl\e(B' can be put on the vowel `\e,TT\e(B'.
52
53
54 (defvar thai-composition-pattern
55 "\\cc\\(\\cu\\|\\cI\\cU\\|\\cv\\ct?\\)\\|\\cv\\ct\\|\\cI\\cU"
56 "Regular expression matching a Thai composite sequence.")
57
58 (defun thai-self-insert-command (&optional n)
59 "Insert the Thai character you type.
60 The character will be composed with the surrounding Thai character
61 if necessary."
62 (interactive "*p")
63 (let ((pos (point))
64 category-set ch)
65 (self-insert-command n)
66 (or thai-auto-composition-mode
67 (thai-auto-composition (1- (point)) (point) 0))))
68
69 (let ((l '((?\e,T!\e(B consonant "LETTER KO KAI") ; 0xA1
70 (?\e,T"\e(B consonant "LETTER KHO KHAI") ; 0xA2
71 (?\e,T#\e(B consonant "LETTER KHO KHUAT") ; 0xA3
72 (?\e,T$\e(B consonant "LETTER KHO KHWAI") ; 0xA4
73 (?\e,T%\e(B consonant "LETTER KHO KHON") ; 0xA5
74 (?\e,T&\e(B consonant "LETTER KHO RAKHANG") ; 0xA6
75 (?\e,T'\e(B consonant "LETTER NGO NGU") ; 0xA7
76 (?\e,T(\e(B consonant "LETTER CHO CHAN") ; 0xA8
77 (?\e,T)\e(B consonant "LETTER CHO CHING") ; 0xA9
78 (?\e,T*\e(B consonant "LETTER CHO CHANG") ; 0xAA
79 (?\e,T+\e(B consonant "LETTER SO SO") ; 0xAB
80 (?\e,T,\e(B consonant "LETTER CHO CHOE") ; 0xAC
81 (?\e,T-\e(B consonant "LETTER YO YING") ; 0xAD
82 (?\e,T.\e(B consonant "LETTER DO CHADA") ; 0xAE
83 (?\e,T/\e(B consonant "LETTER TO PATAK") ; 0xAF
84 (?\e,T0\e(B consonant "LETTER THO THAN") ; 0xB0
85 (?\e,T1\e(B consonant "LETTER THO NANGMONTHO") ; 0xB1
86 (?\e,T2\e(B consonant "LETTER THO PHUTHAO") ; 0xB2
87 (?\e,T3\e(B consonant "LETTER NO NEN") ; 0xB3
88 (?\e,T4\e(B consonant "LETTER DO DEK") ; 0xB4
89 (?\e,T5\e(B consonant "LETTER TO TAO") ; 0xB5
90 (?\e,T6\e(B consonant "LETTER THO THUNG") ; 0xB6
91 (?\e,T7\e(B consonant "LETTER THO THAHAN") ; 0xB7
92 (?\e,T8\e(B consonant "LETTER THO THONG") ; 0xB8
93 (?\e,T9\e(B consonant "LETTER NO NU") ; 0xB9
94 (?\e,T:\e(B consonant "LETTER BO BAIMAI") ; 0xBA
95 (?\e,T;\e(B consonant "LETTER PO PLA") ; 0xBB
96 (?\e,T<\e(B consonant "LETTER PHO PHUNG") ; 0xBC
97 (?\e,T=\e(B consonant "LETTER FO FA") ; 0xBD
98 (?\e,T>\e(B consonant "LETTER PHO PHAN") ; 0xBE
99 (?\e,T?\e(B consonant "LETTER FO FAN") ; 0xBF
100 (?\e,T@\e(B consonant "LETTER PHO SAMPHAO") ; 0xC0
101 (?\e,TA\e(B consonant "LETTER MO MA") ; 0xC1
102 (?\e,TB\e(B consonant "LETTER YO YAK") ; 0xC2
103 (?\e,TC\e(B consonant "LETTER RO RUA") ; 0xC3
104 (?\e,TD\e(B vowel-base "LETTER RU (Pali vowel letter)") ; 0xC4
105 (?\e,TE\e(B consonant "LETTER LO LING") ; 0xC5
106 (?\e,TF\e(B vowel-base "LETTER LU (Pali vowel letter)") ; 0xC6
107 (?\e,TG\e(B consonant "LETTER WO WAEN") ; 0xC7
108 (?\e,TH\e(B consonant "LETTER SO SALA") ; 0xC8
109 (?\e,TI\e(B consonant "LETTER SO RUSI") ; 0xC9
110 (?\e,TJ\e(B consonant "LETTER SO SUA") ; 0xCA
111 (?\e,TK\e(B consonant "LETTER HO HIP") ; 0xCB
112 (?\e,TL\e(B consonant "LETTER LO CHULA") ; 0xCC
113 (?\e,TM\e(B consonant "LETTER O ANG") ; 0xCD
114 (?\e,TN\e(B consonant "LETTER HO NOK HUK") ; 0xCE
115 (?\e,TO\e(B special "PAI YAN NOI (abbreviation)") ; 0xCF
116 (?\e,TP\e(B vowel-base "VOWEL SIGN SARA A") ; 0xD0
117 (?\e,TQ\e(B vowel-upper "VOWEL SIGN MAI HAN-AKAT N/S-T") ; 0xD1
118 (?\e,TR\e(B vowel-base "VOWEL SIGN SARA AA") ; 0xD2
119 (?\e,TS\e(B vowel-base "VOWEL SIGN SARA AM") ; 0xD3
120 (?\e,TT\e(B vowel-upper "VOWEL SIGN SARA I N/S-T") ; 0xD4
121 (?\e,TU\e(B vowel-upper "VOWEL SIGN SARA II N/S-T") ; 0xD5
122 (?\e,TV\e(B vowel-upper "VOWEL SIGN SARA UE N/S-T") ; 0xD6
123 (?\e,TW\e(B vowel-upper "VOWEL SIGN SARA UEE N/S-T") ; 0xD7
124 (?\e,TX\e(B vowel-lower "VOWEL SIGN SARA U N/S-B") ; 0xD8
125 (?\e,TY\e(B vowel-lower "VOWEL SIGN SARA UU N/S-B") ; 0xD9
126 (?\e,TZ\e(B vowel-lower "VOWEL SIGN PHINTHU N/S-B (Pali virama)") ; 0xDA
127 (?\e,T[\e(B invalid nil) ; 0xDA
128 (?\e,T\\e(B invalid nil) ; 0xDC
129 (?\e,T]\e(B invalid nil) ; 0xDC
130 (?\e,T^\e(B invalid nil) ; 0xDC
131 (?\e,T_\e(B special "BAHT SIGN (currency symbol)") ; 0xDF
132 (?\e,T`\e(B vowel-base "VOWEL SIGN SARA E") ; 0xE0
133 (?\e,Ta\e(B vowel-base "VOWEL SIGN SARA AE") ; 0xE1
134 (?\e,Tb\e(B vowel-base "VOWEL SIGN SARA O") ; 0xE2
135 (?\e,Tc\e(B vowel-base "VOWEL SIGN SARA MAI MUAN") ; 0xE3
136 (?\e,Td\e(B vowel-base "VOWEL SIGN SARA MAI MALAI") ; 0xE4
137 (?\e,Te\e(B vowel-base "LAK KHANG YAO") ; 0xE5
138 (?\e,Tf\e(B special "MAI YAMOK (repetion)") ; 0xE6
139 (?\e,Tg\e(B sign-upper "VOWEL SIGN MAI TAI KHU N/S-T") ; 0xE7
140 (?\e,Th\e(B tone "TONE MAI EK N/S-T") ; 0xE8
141 (?\e,Ti\e(B tone "TONE MAI THO N/S-T") ; 0xE9
142 (?\e,Tj\e(B tone "TONE MAI TRI N/S-T") ; 0xEA
143 (?\e,Tk\e(B tone "TONE MAI CHATTAWA N/S-T") ; 0xEB
144 (?\e,Tl\e(B sign-upper "THANTHAKHAT N/S-T (cancellation mark)") ; 0xEC
145 (?\e,Tm\e(B sign-upper "NIKKHAHIT N/S-T (final nasal)") ; 0xED
146 (?\e,Tn\e(B sign-upper "YAMAKKAN N/S-T") ; 0xEE
147 (?\e,To\e(B special "FONRMAN") ; 0xEF
148 (?\e,Tp\e(B special "DIGIT ZERO") ; 0xF0
149 (?\e,Tq\e(B special "DIGIT ONE") ; 0xF1
150 (?\e,Tr\e(B special "DIGIT TWO") ; 0xF2
151 (?\e,Ts\e(B special "DIGIT THREE") ; 0xF3
152 (?\e,Tt\e(B special "DIGIT FOUR") ; 0xF4
153 (?\e,Tu\e(B special "DIGIT FIVE") ; 0xF5
154 (?\e,Tv\e(B special "DIGIT SIX") ; 0xF6
155 (?\e,Tw\e(B special "DIGIT SEVEN") ; 0xF7
156 (?\e,Tx\e(B special "DIGIT EIGHT") ; 0xF8
157 (?\e,Ty\e(B special "DIGIT NINE") ; 0xF9
158 (?\e,Tz\e(B special "ANGKHANKHU (ellipsis)") ; 0xFA
159 (?\e,T{\e(B special "KHOMUT (beginning of religious texts)") ; 0xFB
160 (?\e,T|\e(B invalid nil) ; 0xFC
161 (?\e,T}\e(B invalid nil) ; 0xFD
162 (?\e,T~\e(B invalid nil) ; 0xFE
163 ))
164 elm)
165 (while l
166 (setq elm (car l) l (cdr l))
167 (let ((char (car elm))
168 (ptype (nth 1 elm)))
169 (put-char-code-property char 'phonetic-type ptype)
170 (cond ((eq ptype 'consonant)
171 (modify-category-entry char ?c thai-category-table)
172 (global-set-key (vector char) 'thai-self-insert-command))
173 ((memq ptype '(vowel-upper vowel-lower))
174 (modify-category-entry char ?v thai-category-table)
175 (if (or (= char ?\e,TT\e(B) (= char ?\e$,1C4\e(B))
176 ;; Give category `I' to "SARA I".
177 (modify-category-entry char ?I thai-category-table))
178 (global-set-key (vector char) 'thai-self-insert-command))
179 ((eq ptype 'tone)
180 (modify-category-entry char ?t thai-category-table)
181 (modify-category-entry char ?u thai-category-table)
182 (global-set-key (vector char) 'thai-self-insert-command))
183 ((eq ptype 'sign-upper)
184 (modify-category-entry char ?u thai-category-table)
185 (if (or (= char ?\e,Tl\e(B) (= char ?\e$,1CL\e(B))
186 ;; Give category `U' to "THANTHAKHAT".
187 (modify-category-entry char ?U thai-category-table))
188 (global-set-key (vector char) 'thai-self-insert-command)))
189 (put-char-code-property char 'name (nth 2 elm)))))
190
191 (defun thai-compose-syllable (beg end &optional category-set string)
192 (or category-set
193 (setq category-set
194 (char-category-set (if string (aref string beg) (char-after beg)))))
195 (if (aref category-set ?c)
196 ;; Starting with a consonant. We do relative composition.
197 (if string
198 (compose-string string beg end)
199 (compose-region beg end))
200 ;; Vowel tone sequence.
201 (if string
202 (compose-string string beg end (list (aref string beg) '(Bc . Bc)
203 (aref string (1+ beg))))
204 (compose-region beg end (list (char-after beg) '(Bc . Bc)
205 (char-after (1+ beg))))))
206 (- end beg))
207
208 ;;;###autoload
209 (defun thai-compose-region (beg end)
210 "Compose Thai characters in the region.
211 When called from a program, expects two arguments,
212 positions (integers or markers) specifying the region."
213 (interactive "r")
214 (let ((pos (point)))
215 (save-restriction
216 (narrow-to-region beg end)
217 (goto-char (point-min))
218 (with-category-table thai-category-table
219 (while (re-search-forward thai-composition-pattern nil t)
220 (setq beg (match-beginning 0) end (match-end 0))
221 (if (and (> pos beg) (< pos end))
222 (setq pos end))
223 (thai-compose-syllable beg end
224 (char-category-set (char-after beg))))))
225 (goto-char pos)))
226
227 ;;;###autoload
228 (defun thai-compose-string (string)
229 "Compose Thai characters in STRING and return the resulting string."
230 (with-category-table thai-category-table
231 (let ((idx 0))
232 (while (setq idx (string-match thai-composition-pattern string idx))
233 (thai-compose-syllable idx (match-end 0) nil string)
234 (setq idx (match-end 0)))))
235 string)
236
237 ;;;###autoload
238 (defun thai-compose-buffer ()
239 "Compose Thai characters in the current buffer."
240 (interactive)
241 (thai-compose-region (point-min) (point-max)))
242
243 ;;;###autoload
244 (defun thai-composition-function (pos &optional string)
245 (setq pos (1- pos))
246 (let ((pattern "[\e,T!\e(B-\e,TCEG\e(B-\e,TN!\e(B-\e,TCEG\e(B-\e,TN\e(B][\e,TQT\e(B-\e,TWgnX\e(B-\e,TZQT\e(B-\e,TWgnX\e(B-\e,TZ\e(B]?[\e,Th\e(B-\e,Tmh\e(B-\e,Tm\e(B]?"))
247 (if string
248 (if (and (>= pos 0)
249 (eq (string-match pattern string pos) pos))
250 (prog1 (match-end 0)
251 (compose-string string pos (match-end 0))))
252 (if (>= pos (point-min))
253 (progn
254 (goto-char pos)
255 (if (looking-at pattern)
256 (prog1 (match-end 0)
257 (compose-region pos (match-end 0)))))))))
258
259 ;;
260 (provide 'thai-util)
261
262 ;;; arch-tag: 59425d6a-8cf9-4e06-a6ab-8ab7dc7a7a97
263 ;;; thai-util.el ends here