]> code.delx.au - gnu-emacs/blob - lisp/language/thai-util.el
b15197be29ea7abaa34523a116193b52d1538807
[gnu-emacs] / lisp / language / thai-util.el
1 ;;; thai-util.el --- utilities for Thai -*- coding: utf-8; -*-
2
3 ;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008, 2009, 2010, 2011
5 ;; National Institute of Advanced Industrial Science and Technology (AIST)
6 ;; Registration Number H14PRO021
7 ;; Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
8 ;; Free Software Foundation, Inc.
9
10 ;; Keywords: mule, multilingual, Thai, i18n
11
12 ;; This file is part of GNU Emacs.
13
14 ;; GNU Emacs is free software: you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation, either version 3 of the License, or
17 ;; (at your option) any later version.
18
19 ;; GNU Emacs is distributed in the hope that it will be useful,
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 ;; GNU General Public License for more details.
23
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
26
27 ;;; Commentary:
28
29 ;;; Code:
30
31 (defvar thai-auto-composition-mode)
32
33 ;; Setting information of Thai characters.
34
35 (defconst thai-category-table (make-category-table))
36 (define-category ?c "Thai consonant" thai-category-table)
37 (define-category ?v "Thai upper/lower vowel" thai-category-table)
38 (define-category ?t "Thai tone mark" thai-category-table)
39 (define-category ?u "Thai tone mark and upper sign" thai-category-table)
40 (define-category ?I "THAI CHARACTER SARA I" thai-category-table)
41 (define-category ?U "THAI CHARACTER THANTHAKHAT" thai-category-table)
42
43 ;; The general composing rules are as follows:
44 ;;
45 ;; T
46 ;; V U V U
47 ;; CV -> C, CU -> C, CVT -> C, Cv -> C, CvU -> C
48 ;; v v
49 ;;
50 ;; where C: consonant, V: vowel upper, v: vowel lower,
51 ;; T: tone mark, U: tone mark and upper sign.
52 ;; Special rule: The sign `์' can be put on the vowel `ิ'.
53
54
55 (defvar thai-composition-pattern
56 "\\cc\\(\\cu\\|\\cI\\cU\\|\\cv\\ct?\\)\\|\\cv\\ct\\|\\cI\\cU"
57 "Regular expression matching a Thai composite sequence.")
58
59 (let ((l '((?ก consonant "LETTER KO KAI") ; 0xA1
60 (?ข consonant "LETTER KHO KHAI") ; 0xA2
61 (?ฃ consonant "LETTER KHO KHUAT") ; 0xA3
62 (?ค consonant "LETTER KHO KHWAI") ; 0xA4
63 (?ฅ consonant "LETTER KHO KHON") ; 0xA5
64 (?ฆ consonant "LETTER KHO RAKHANG") ; 0xA6
65 (?ง consonant "LETTER NGO NGU") ; 0xA7
66 (?จ consonant "LETTER CHO CHAN") ; 0xA8
67 (?ฉ consonant "LETTER CHO CHING") ; 0xA9
68 (?ช consonant "LETTER CHO CHANG") ; 0xAA
69 (?ซ consonant "LETTER SO SO") ; 0xAB
70 (?ฌ consonant "LETTER CHO CHOE") ; 0xAC
71 (?ญ consonant "LETTER YO YING") ; 0xAD
72 (?ฎ consonant "LETTER DO CHADA") ; 0xAE
73 (?ฏ consonant "LETTER TO PATAK") ; 0xAF
74 (?ฐ consonant "LETTER THO THAN") ; 0xB0
75 (?ฑ consonant "LETTER THO NANGMONTHO") ; 0xB1
76 (?ฒ consonant "LETTER THO PHUTHAO") ; 0xB2
77 (?ณ consonant "LETTER NO NEN") ; 0xB3
78 (?ด consonant "LETTER DO DEK") ; 0xB4
79 (?ต consonant "LETTER TO TAO") ; 0xB5
80 (?ถ consonant "LETTER THO THUNG") ; 0xB6
81 (?ท consonant "LETTER THO THAHAN") ; 0xB7
82 (?ธ consonant "LETTER THO THONG") ; 0xB8
83 (?น consonant "LETTER NO NU") ; 0xB9
84 (?บ consonant "LETTER BO BAIMAI") ; 0xBA
85 (?ป consonant "LETTER PO PLA") ; 0xBB
86 (?ผ consonant "LETTER PHO PHUNG") ; 0xBC
87 (?ฝ consonant "LETTER FO FA") ; 0xBD
88 (?พ consonant "LETTER PHO PHAN") ; 0xBE
89 (?ฟ consonant "LETTER FO FAN") ; 0xBF
90 (?ภ consonant "LETTER PHO SAMPHAO") ; 0xC0
91 (?ม consonant "LETTER MO MA") ; 0xC1
92 (?ย consonant "LETTER YO YAK") ; 0xC2
93 (?ร consonant "LETTER RO RUA") ; 0xC3
94 (?ฤ vowel-base "LETTER RU (Pali vowel letter)") ; 0xC4
95 (?ล consonant "LETTER LO LING") ; 0xC5
96 (?ฦ vowel-base "LETTER LU (Pali vowel letter)") ; 0xC6
97 (?ว consonant "LETTER WO WAEN") ; 0xC7
98 (?ศ consonant "LETTER SO SALA") ; 0xC8
99 (?ษ consonant "LETTER SO RUSI") ; 0xC9
100 (?ส consonant "LETTER SO SUA") ; 0xCA
101 (?ห consonant "LETTER HO HIP") ; 0xCB
102 (?ฬ consonant "LETTER LO CHULA") ; 0xCC
103 (?อ consonant "LETTER O ANG") ; 0xCD
104 (?ฮ consonant "LETTER HO NOK HUK") ; 0xCE
105 (?ฯ special "PAI YAN NOI (abbreviation)") ; 0xCF
106 (?ะ vowel-base "VOWEL SIGN SARA A") ; 0xD0
107 (?ั vowel-upper "VOWEL SIGN MAI HAN-AKAT N/S-T") ; 0xD1
108 (?า vowel-base "VOWEL SIGN SARA AA") ; 0xD2
109 (?ำ vowel-base "VOWEL SIGN SARA AM") ; 0xD3
110 (?ิ vowel-upper "VOWEL SIGN SARA I N/S-T") ; 0xD4
111 (?ี vowel-upper "VOWEL SIGN SARA II N/S-T") ; 0xD5
112 (?ึ vowel-upper "VOWEL SIGN SARA UE N/S-T") ; 0xD6
113 (?ื vowel-upper "VOWEL SIGN SARA UEE N/S-T") ; 0xD7
114 (?ุ vowel-lower "VOWEL SIGN SARA U N/S-B") ; 0xD8
115 (?ู vowel-lower "VOWEL SIGN SARA UU N/S-B") ; 0xD9
116 (?ฺ vowel-lower "VOWEL SIGN PHINTHU N/S-B (Pali virama)") ; 0xDA
117 (?฻ invalid nil) ; 0xDA
118 (?฼ invalid nil) ; 0xDC
119 (?฽ invalid nil) ; 0xDC
120 (?฾ invalid nil) ; 0xDC
121 (?฿ special "BAHT SIGN (currency symbol)") ; 0xDF
122 (?เ vowel-base "VOWEL SIGN SARA E") ; 0xE0
123 (?แ vowel-base "VOWEL SIGN SARA AE") ; 0xE1
124 (?โ vowel-base "VOWEL SIGN SARA O") ; 0xE2
125 (?ใ vowel-base "VOWEL SIGN SARA MAI MUAN") ; 0xE3
126 (?ไ vowel-base "VOWEL SIGN SARA MAI MALAI") ; 0xE4
127 (?ๅ vowel-base "LAK KHANG YAO") ; 0xE5
128 (?ๆ special "MAI YAMOK (repetion)") ; 0xE6
129 (?็ sign-upper "VOWEL SIGN MAI TAI KHU N/S-T") ; 0xE7
130 (?่ tone "TONE MAI EK N/S-T") ; 0xE8
131 (?้ tone "TONE MAI THO N/S-T") ; 0xE9
132 (?๊ tone "TONE MAI TRI N/S-T") ; 0xEA
133 (?๋ tone "TONE MAI CHATTAWA N/S-T") ; 0xEB
134 (?์ sign-upper "THANTHAKHAT N/S-T (cancellation mark)") ; 0xEC
135 (?ํ sign-upper "NIKKHAHIT N/S-T (final nasal)") ; 0xED
136 (?๎ sign-upper "YAMAKKAN N/S-T") ; 0xEE
137 (?๏ special "FONRMAN") ; 0xEF
138 (?๐ special "DIGIT ZERO") ; 0xF0
139 (?๑ special "DIGIT ONE") ; 0xF1
140 (?๒ special "DIGIT TWO") ; 0xF2
141 (?๓ special "DIGIT THREE") ; 0xF3
142 (?๔ special "DIGIT FOUR") ; 0xF4
143 (?๕ special "DIGIT FIVE") ; 0xF5
144 (?๖ special "DIGIT SIX") ; 0xF6
145 (?๗ special "DIGIT SEVEN") ; 0xF7
146 (?๘ special "DIGIT EIGHT") ; 0xF8
147 (?๙ special "DIGIT NINE") ; 0xF9
148 (?๚ special "ANGKHANKHU (ellipsis)") ; 0xFA
149 (?๛ special "KHOMUT (beginning of religious texts)") ; 0xFB
150 (?๜ invalid nil) ; 0xFC
151 (?๝ invalid nil) ; 0xFD
152 (?๞ invalid nil) ; 0xFE
153 ))
154 elm)
155 (while l
156 (setq elm (car l) l (cdr l))
157 (let ((char (car elm))
158 (ptype (nth 1 elm)))
159 (put-char-code-property char 'phonetic-type ptype)
160 (cond ((eq ptype 'consonant)
161 (modify-category-entry char ?c thai-category-table))
162 ((memq ptype '(vowel-upper vowel-lower))
163 (modify-category-entry char ?v thai-category-table)
164 (if (= char ?ิ)
165 ;; Give category `I' to "SARA I".
166 (modify-category-entry char ?I thai-category-table)))
167 ((eq ptype 'tone)
168 (modify-category-entry char ?t thai-category-table)
169 (modify-category-entry char ?u thai-category-table))
170 ((eq ptype 'sign-upper)
171 (modify-category-entry char ?u thai-category-table)
172 (if (= char ?์)
173 ;; Give category `U' to "THANTHAKHAT".
174 (modify-category-entry char ?U thai-category-table))))
175 (put-char-code-property char 'name (nth 2 elm)))))
176
177 (defun thai-compose-syllable (beg end &optional category-set string)
178 (or category-set
179 (setq category-set
180 (char-category-set (if string (aref string beg) (char-after beg)))))
181 (if (aref category-set ?c)
182 ;; Starting with a consonant. We do relative composition.
183 (if string
184 (compose-string string beg end)
185 (compose-region beg end))
186 ;; Vowel tone sequence.
187 (if string
188 (compose-string string beg end (list (aref string beg) '(Bc . Bc)
189 (aref string (1+ beg))))
190 (compose-region beg end (list (char-after beg) '(Bc . Bc)
191 (char-after (1+ beg))))))
192 (- end beg))
193
194 ;;;###autoload
195 (defun thai-compose-region (beg end)
196 "Compose Thai characters in the region.
197 When called from a program, expects two arguments,
198 positions (integers or markers) specifying the region."
199 (interactive "r")
200 (let ((pos (point)))
201 (save-restriction
202 (narrow-to-region beg end)
203 (goto-char (point-min))
204 (with-category-table thai-category-table
205 (while (re-search-forward thai-composition-pattern nil t)
206 (setq beg (match-beginning 0) end (match-end 0))
207 (if (and (> pos beg) (< pos end))
208 (setq pos end))
209 (thai-compose-syllable beg end
210 (char-category-set (char-after beg))))))
211 (goto-char pos)))
212
213 ;;;###autoload
214 (defun thai-compose-string (string)
215 "Compose Thai characters in STRING and return the resulting string."
216 (with-category-table thai-category-table
217 (let ((idx 0))
218 (while (setq idx (string-match thai-composition-pattern string idx))
219 (thai-compose-syllable idx (match-end 0) nil string)
220 (setq idx (match-end 0)))))
221 string)
222
223 ;;;###autoload
224 (defun thai-compose-buffer ()
225 "Compose Thai characters in the current buffer."
226 (interactive)
227 (thai-compose-region (point-min) (point-max)))
228
229 ;;;###autoload
230 (defun thai-composition-function (gstring)
231 (if (= (lgstring-char-len gstring) 1)
232 (compose-gstring-for-graphic gstring)
233 (or (font-shape-gstring gstring)
234 (let ((glyph-len (lgstring-glyph-len gstring))
235 (last-char (lgstring-char gstring
236 (1- (lgstring-char-len gstring))))
237 (i 0)
238 glyph)
239 (while (and (< i glyph-len)
240 (setq glyph (lgstring-glyph gstring i)))
241 (setq i (1+ i)))
242 (if (= last-char ?ำ)
243 (setq i (1- i)))
244 (compose-glyph-string-relative gstring 0 i 0.1)))))
245
246 ;; Thai-word-mode requires functions in the feature `thai-word'.
247 (require 'thai-word)
248
249 (defvar thai-word-mode-map
250 (let ((map (make-sparse-keymap)))
251 (define-key map [remap forward-word] 'thai-forward-word)
252 (define-key map [remap backward-word] 'thai-backward-word)
253 (define-key map [remap kill-word] 'thai-kill-word)
254 (define-key map [remap backward-kill-word] 'thai-backward-kill-word)
255 (define-key map [remap transpose-words] 'thai-transpose-words)
256 map)
257 "Keymap for `thai-word-mode'.")
258
259 (define-minor-mode thai-word-mode
260 "Minor mode to make word-oriented commands aware of Thai words.
261 The commands affected are \\[forward-word], \\[backward-word], \\[kill-word], \\[backward-kill-word], \\[transpose-words], and \\[fill-paragraph]."
262 :global t :group 'mule
263 (cond (thai-word-mode
264 ;; This enables linebreak between Thai characters.
265 (modify-category-entry (make-char 'thai-tis620) ?|)
266 ;; This enables linebreak at a Thai word boundary.
267 (put-charset-property 'thai-tis620 'fill-find-break-point-function
268 'thai-fill-find-break-point))
269 (t
270 (modify-category-entry (make-char 'thai-tis620) ?| nil t)
271 (put-charset-property 'thai-tis620 'fill-find-break-point-function
272 nil))))
273
274 ;; Function to call on entering the Thai language environment.
275 (defun setup-thai-language-environment-internal ()
276 (thai-word-mode 1))
277
278 ;; Function to call on exiting the Thai language environment.
279 (defun exit-thai-language-environment-internal ()
280 (thai-word-mode -1))
281
282 ;;
283 (provide 'thai-util)
284
285 ;; arch-tag: 59425d6a-8cf9-4e06-a6ab-8ab7dc7a7a97
286 ;;; thai-util.el ends here