]> code.delx.au - gnu-emacs/blobdiff - lisp/language/thai-util.el
Revision: miles@gnu.org--gnu-2005/emacs--unicode--0--patch-25
[gnu-emacs] / lisp / language / thai-util.el
index fd48ff4c022ea6a2518d67eb5959cfb8b653bce8..259a102c61ea0a012228789f85af77285cf040e0 100644 (file)
@@ -1,9 +1,12 @@
-;;; thai-util.el --- utilities for Thai
+;;; thai-util.el --- utilities for Thai -*- coding: iso-2022-7bit; -*-
 
 ;; Copyright (C) 1995 Electrotechnical Laboratory, JAPAN.
-;; Licensed to the Free Software Foundation.
+;;   Licensed to the Free Software Foundation.
+;; Copyright (C) 2003
+;;   National Institute of Advanced Industrial Science and Technology (AIST)
+;;   Registration Number H13PRO009
 
-;; Keywords: mule, multilingual, thai
+;; Keywords: mule, multilingual, Thai, i18n
 
 ;; This file is part of GNU Emacs.
 
 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 ;; Boston, MA 02111-1307, USA.
 
-;;; Code:
+;;; Commentary:
 
-;;;###autoload
-(defun setup-thai-environment ()
-  "Setup multilingual environment (MULE) for Thai."
-  (interactive)
-  (set-language-environment "Thai"))
+;;; Code:
 
 ;; Setting information of Thai characters.
 
-(defvar thai-category-table (copy-category-table))
-(or (category-docstring ?+ thai-category-table)
-    (define-category ?+ "Thai consonant" thai-category-table))
-(or (category-docstring ?- thai-category-table)
-    (define-category ?- "Thai diacritical mark" thai-category-table))
+(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
           ))
       elm)
   (while l
-    (setq elm (car l))
-    (let ((ptype (nth 1 elm)))
-      (put-char-code-property (car elm) 'phonetic-type ptype)
-      (if (eq ptype 'consonant)
-         (modify-category-entry (car elm) ?+ thai-category-table)
-       (if (memq ptype '(vowel-upper vowel-lower tone))
-         (modify-category-entry (car elm) ?- thai-category-table))))
-    (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)
@@ -154,14 +165,20 @@ positions (integers or markers) specifying the region."
   (interactive "r")
   (save-restriction
     (narrow-to-region beg end)
-    (decompose-region (point-min) (point-max))
     (goto-char (point-min))
-    (let ((current-ctbl (category-table)))
-      (set-category-table thai-category-table)
-      (unwind-protect
-         (while (re-search-forward "\\c+\\c-+" nil t)
-           (compose-region (match-beginning 0) (match-end 0)))
-       (set-category-table current-ctbl)))))
+    (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 ()
@@ -170,27 +187,23 @@ positions (integers or markers) specifying the region."
   (thai-compose-region (point-min) (point-max)))
 
 ;;;###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))))))
-
-;;;###autoload
-(defun thai-pre-write-conversion (from to)
-  (let ((old-buf (current-buffer)))
-    (set-buffer (generate-new-buffer " *temp*"))
-    (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 (pos &optional string)
+  (setq pos (1- pos))
+  (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]?"))
+    (if string
+       (if (and (>= pos 0)
+                (eq (string-match pattern string pos) pos))
+           (prog1 (match-end 0)
+             (compose-string string pos (match-end 0))))
+      (if (>= pos (point-min))
+         (progn
+           (goto-char pos)
+           (if (looking-at pattern)
+               (prog1 (match-end 0)
+                 (compose-region pos (match-end 0)))))))))
 
 ;;
 (provide 'thai-util)
 
+;;; arch-tag: 59425d6a-8cf9-4e06-a6ab-8ab7dc7a7a97
 ;;; thai-util.el ends here