(defun setup-thai-environment ()
"Setup multilingual environment (MULE) for Thai."
(interactive)
- (setup-8-bit-environment "Thai" 'thai-tis620 'thai-tis620
- "thai-kesmanee"))
+ (set-language-environment "Thai"))
;; Setting information of Thai characters.
+(defconst thai-category-table (make-category-table))
+(define-category ?c "Thai consonant" thai-category-table)
+(define-category ?v "Thai upper/lower vowel" thai-category-table)
+(define-category ?t "Thai tone" thai-category-table)
+
+;; The general composing rules are as follows:
+;;
+;; T
+;; V T V T
+;; CV -> C, CT -> C, CVT -> C, Cv -> C, CvT -> C
+;; v v
+;;
+;; where C: consonant, V: vowel upper, v: vowel lower, T: tone mark.
+
+(defvar thai-composition-pattern "\\cc\\(\\ct\\|\\cv\\ct?\\)"
+ "Regular expression matching a Thai composite sequence.")
+
(let ((l '((?\e,T!\e(B consonant "LETTER KO KAI") ; 0xA1
(?\e,T"\e(B consonant "LETTER KHO KHAI") ; 0xA2
(?\e,T#\e(B consonant "LETTER KHO KHUAT") ; 0xA3
))
elm)
(while l
- (setq elm (car l))
- (put-char-code-property (car elm) 'phonetic-type (car (cdr elm)))
- (put-char-code-property (car elm) 'name (nth 2 elm))
- (setq l (cdr l))))
+ (setq elm (car l) l (cdr l))
+ (let ((char (car elm))
+ (ptype (nth 1 elm)))
+ (put-char-code-property char 'phonetic-type ptype)
+ (cond ((eq ptype 'consonant)
+ (modify-category-entry char ?c thai-category-table))
+ ((memq ptype '(vowel-upper vowel-lower))
+ (modify-category-entry char ?v thai-category-table))
+ ((eq ptype 'tone)
+ (modify-category-entry char ?t thai-category-table)))
+ (put-char-code-property char 'name (nth 2 elm)))))
;;;###autoload
(defun thai-compose-region (beg end)
(interactive "r")
(save-restriction
(narrow-to-region beg end)
- (decompose-region (point-min) (point-max))
(goto-char (point-min))
- (while (re-search-forward "\\c0\\(\\c2\\|\\c3\\|\\c4\\)+" nil t)
- (if (aref (char-category-set (char-after (match-beginning 0))) ?t)
- (compose-region (match-beginning 0) (match-end 0))))))
+ (with-category-table thai-category-table
+ (while (re-search-forward thai-composition-pattern nil t)
+ (compose-region (match-beginning 0) (match-end 0))))))
+;;;###autoload
+(defun thai-compose-string (string)
+ "Compose Thai characters in STRING and return the resulting string."
+ (with-category-table thai-category-table
+ (let ((idx 0))
+ (while (setq idx (string-match thai-composition-pattern string idx))
+ (compose-string string idx (match-end 0))
+ (setq idx (match-end 0)))))
+ string)
+
;;;###autoload
(defun thai-compose-buffer ()
"Compose Thai characters in the current buffer."
;;;###autoload
(defun thai-post-read-conversion (len)
- (save-excursion
- (save-restriction
- (let ((buffer-modified-p (buffer-modified-p)))
- (narrow-to-region (point) (+ (point) len))
- (thai-compose-region (point-min) (point-max))
- (set-buffer-modified-p buffer-modified-p)
- (- (point-max) (point-min))))))
+ (thai-compose-region (point) (+ (point) len))
+ len)
;;;###autoload
-(defun thai-pre-write-conversion (from to)
- (let ((old-buf (current-buffer))
- (work-buf (get-buffer-create " *thai-work*")))
- (set-buffer work-buf)
- (erase-buffer)
- (if (stringp from)
- (insert from)
- (insert-buffer-substring old-buf from to))
- (decompose-region (point-min) (point-max))
- ;; Should return nil as annotations.
- nil))
+(defun thai-composition-function (from to pattern &optional string)
+ "Compose Thai text in the region FROM and TO.
+The text matches the regular expression PATTERN.
+Optional 4th argument STRING, if non-nil, is a string containing text
+to compose.
+
+The return value is number of composed characters."a
+ (if (< (1+ from) to)
+ (prog1 (- to from)
+ (if string
+ (compose-string from to)
+ (compose-region from to))
+ (- to from))))
;;
(provide 'thai-util)