1 ;;; thai-util.el --- utilities for Thai -*- coding: utf-8; -*-
3 ;; Copyright (C) 2000-2016 Free Software Foundation, Inc.
4 ;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
5 ;; 2005, 2006, 2007, 2008, 2009, 2010, 2011
6 ;; National Institute of Advanced Industrial Science and Technology (AIST)
7 ;; Registration Number H14PRO021
9 ;; Keywords: mule, multilingual, Thai, i18n
11 ;; This file is part of GNU Emacs.
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 3 of the License, or
16 ;; (at your option) any later version.
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.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
30 (defvar thai-auto-composition-mode)
32 ;; Setting information of Thai characters.
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)
42 ;; The general composing rules are as follows:
46 ;; CV -> C, CU -> C, CVT -> C, Cv -> C, CvU -> C
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 `์' can be put on the vowel `ิ'.
54 (defvar thai-composition-pattern
55 "\\cc\\(\\cu\\|\\cI\\cU\\|\\cv\\ct?\\)\\|\\cv\\ct\\|\\cI\\cU"
56 "Regular expression matching a Thai composite sequence.")
58 (let ((l '((?ก consonant) ; 0xA1
93 (?ฤ vowel-base) ; 0xC4
95 (?ฦ vowel-base) ; 0xC6
100 (?ห consonant) ; 0xCB
101 (?ฬ consonant) ; 0xCC
102 (?อ consonant) ; 0xCD
103 (?ฮ consonant) ; 0xCE
105 (?ะ vowel-base) ; 0xD0
106 (?ั vowel-upper) ; 0xD1
107 (?า vowel-base) ; 0xD2
108 (?ำ vowel-base) ; 0xD3
109 (?ิ vowel-upper) ; 0xD4
110 (?ี vowel-upper) ; 0xD5
111 (?ึ vowel-upper) ; 0xD6
112 (?ื vowel-upper) ; 0xD7
113 (?ุ vowel-lower) ; 0xD8
114 (?ู vowel-lower) ; 0xD9
115 (?ฺ vowel-lower) ; 0xDA
121 (?เ vowel-base) ; 0xE0
122 (?แ vowel-base) ; 0xE1
123 (?โ vowel-base) ; 0xE2
124 (?ใ vowel-base) ; 0xE3
125 (?ไ vowel-base) ; 0xE4
126 (?ๅ vowel-base) ; 0xE5
128 (?็ sign-upper) ; 0xE7
133 (?์ sign-upper) ; 0xEC
134 (?ํ sign-upper) ; 0xED
135 (?๎ sign-upper) ; 0xEE
155 (setq elm (car l) l (cdr l))
156 (let ((char (car elm))
158 (put-char-code-property char 'phonetic-type ptype)
159 (cond ((eq ptype 'consonant)
160 (modify-category-entry char ?c thai-category-table))
161 ((memq ptype '(vowel-upper vowel-lower))
162 (modify-category-entry char ?v thai-category-table)
164 ;; Give category `I' to "SARA I".
165 (modify-category-entry char ?I thai-category-table)))
167 (modify-category-entry char ?t thai-category-table)
168 (modify-category-entry char ?u thai-category-table))
169 ((eq ptype 'sign-upper)
170 (modify-category-entry char ?u thai-category-table)
172 ;; Give category `U' to "THANTHAKHAT".
173 (modify-category-entry char ?U thai-category-table)))))))
175 (defun thai-compose-syllable (beg end &optional category-set string)
178 (char-category-set (if string (aref string beg) (char-after beg)))))
179 (if (aref category-set ?c)
180 ;; Starting with a consonant. We do relative composition.
182 (compose-string string beg end)
183 (compose-region beg end))
184 ;; Vowel tone sequence.
186 (compose-string string beg end (list (aref string beg) '(Bc . Bc)
187 (aref string (1+ beg))))
188 (compose-region beg end (list (char-after beg) '(Bc . Bc)
189 (char-after (1+ beg))))))
193 (defun thai-compose-region (beg end)
194 "Compose Thai characters in the region.
195 When called from a program, expects two arguments,
196 positions (integers or markers) specifying the region."
200 (narrow-to-region beg end)
201 (goto-char (point-min))
202 (with-category-table thai-category-table
203 (while (re-search-forward thai-composition-pattern nil t)
204 (setq beg (match-beginning 0) end (match-end 0))
205 (if (and (> pos beg) (< pos end))
207 (thai-compose-syllable beg end
208 (char-category-set (char-after beg))))))
212 (defun thai-compose-string (string)
213 "Compose Thai characters in STRING and return the resulting string."
214 (with-category-table thai-category-table
216 (while (setq idx (string-match thai-composition-pattern string idx))
217 (thai-compose-syllable idx (match-end 0) nil string)
218 (setq idx (match-end 0)))))
222 (defun thai-compose-buffer ()
223 "Compose Thai characters in the current buffer."
225 (thai-compose-region (point-min) (point-max)))
228 (defun thai-composition-function (gstring)
229 (if (= (lgstring-char-len gstring) 1)
230 (compose-gstring-for-graphic gstring)
231 (or (font-shape-gstring gstring)
232 (let ((glyph-len (lgstring-glyph-len gstring))
233 (last-char (lgstring-char gstring
234 (1- (lgstring-char-len gstring))))
237 (while (and (< i glyph-len)
238 (setq glyph (lgstring-glyph gstring i)))
242 (compose-glyph-string-relative gstring 0 i 0.1)))))
244 ;; Thai-word-mode requires functions in the feature `thai-word'.
247 (defvar thai-word-mode-map
248 (let ((map (make-sparse-keymap)))
249 (define-key map [remap forward-word] 'thai-forward-word)
250 (define-key map [remap backward-word] 'thai-backward-word)
251 (define-key map [remap kill-word] 'thai-kill-word)
252 (define-key map [remap backward-kill-word] 'thai-backward-kill-word)
253 (define-key map [remap transpose-words] 'thai-transpose-words)
255 "Keymap for `thai-word-mode'.")
257 (define-minor-mode thai-word-mode
258 "Minor mode to make word-oriented commands aware of Thai words.
259 With a prefix argument ARG, enable the mode if ARG is positive,
260 and disable it otherwise. If called from Lisp, enable the mode
261 if ARG is omitted or nil. The commands affected are
262 \\[forward-word], \\[backward-word], \\[kill-word], \\[backward-kill-word],
263 \\[transpose-words], and \\[fill-paragraph]."
264 :global t :group 'mule
265 (cond (thai-word-mode
266 ;; This enables linebreak between Thai characters.
267 (modify-category-entry (make-char 'thai-tis620) ?|)
268 ;; This enables linebreak at a Thai word boundary.
269 (put-charset-property 'thai-tis620 'fill-find-break-point-function
270 'thai-fill-find-break-point))
272 (modify-category-entry (make-char 'thai-tis620) ?| nil t)
273 (put-charset-property 'thai-tis620 'fill-find-break-point-function
276 ;; Function to call on entering the Thai language environment.
277 (defun setup-thai-language-environment-internal ()
280 ;; Function to call on exiting the Thai language environment.
281 (defun exit-thai-language-environment-internal ()
287 ;;; thai-util.el ends here