comparison lisp/language/tibet-util.el @ 17301:0b94fe381355

Initial revision
author Kenichi Handa <handa@m17n.org>
date Sat, 05 Apr 1997 02:44:02 +0000
parents
children a3ca5e15c82a
comparison
equal deleted inserted replaced
17300:01d528c5dd18 17301:0b94fe381355
1 ;;; language/tibet-util.el -- Support for inputting Tibetan characters
2
3 ;; Copyright (C) 1995 Free Software Foundation, Inc.
4 ;; Copyright (C) 1995 Electrotechnical Laboratory, JAPAN.
5
6 ;; Keywords: multilingual, Tibetan
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;; Author: Toru TOMABECHI, <Toru.Tomabechi@orient.unil.ch>
26
27 ;; Created: Feb. 17. 1997
28
29 ;; History:
30 ;; 1997.03.13 Modification in treatment of text properties;
31 ;; Support for some special signs and punctuations.
32
33 ;;; Code:
34
35 ;;; This function makes a transcription string for
36 ;;; re-composing a character.
37
38 ;;;###autoload
39 (defun tibetan-tibetan-to-transcription (ch)
40 "Return a transcription string of Tibetan character CH"
41 (let ((char ch)
42 (l (append tibetan-consonant-transcription-alist
43 tibetan-vowel-transcription-alist
44 tibetan-precomposed-transcription-alist
45 tibetan-subjoined-transcription-alist))
46 decomp-l t-char trans str result)
47 (if (eq (char-charset char) 'composition)
48 (setq decomp-l (decompose-composite-char char 'list nil))
49 (setq decomp-l (cons char nil)))
50 (setq str "")
51 (while decomp-l
52 (setq t-char (char-to-string (car decomp-l)))
53 (setq trans (car (rassoc t-char l)))
54 (setq str (concat str trans))
55 (setq decomp-l (cdr decomp-l)))
56 (setq result str)))
57
58 ;;; This function translates transcription string into a string of
59 ;;; Tibetan characters.
60
61 ;;;###autoload
62 (defun tibetan-transcription-to-tibetan (transcription)
63 "Translate Roman transcription into a sequence of Tibetan components."
64 (let ((trans transcription)
65 (lp tibetan-precomposed-transcription-alist)
66 (l (append tibetan-consonant-transcription-alist
67 tibetan-vowel-transcription-alist
68 tibetan-subjoined-transcription-alist))
69 (case-fold-search nil)
70 substr t-char p-str t-str result)
71 (setq substr "")
72 (setq p-str "")
73 (setq t-str "")
74 (cond ((string-match tibetan-precomposed-regexp trans)
75 (setq substr (substring trans (match-beginning 0) (match-end 0)))
76 (setq trans (substring trans (match-end 0)))
77 (setq t-char (cdr (assoc substr lp)))
78 (setq p-str t-char)))
79 (while (string-match tibetan-regexp trans)
80 (setq substr (substring trans (match-beginning 0) (match-end 0)))
81 (setq trans (substring trans 0 (match-beginning 0)))
82 (setq t-char
83 (cdr (assoc substr l)))
84 (setq t-str (concat t-char t-str)))
85 (setq result (concat p-str t-str))))
86
87
88 ;;;
89 ;;; Functions for composing Tibetan character.
90 ;;;
91 ;;; A Tibetan syllable is typically structured as follows:
92 ;;;
93 ;;; [Prefix] C [C+] V [M] [Suffix [Post suffix]]
94 ;;;
95 ;;; where C's are all vertically stacked, V appears below or above
96 ;;; consonant cluster and M is always put above the C[C+]V combination.
97 ;;; (Sanskrit visarga, though it is a vowel modifier, is considered
98 ;;; to be a punctuation.)
99 ;;;
100 ;;; Here are examples of the words "bsgrubs" and "h'uM"
101 ;;;
102 ;;; $(7"72%q`"U1"7"G(B 2$(7"H`#A`"U0"_1(B
103 ;;;
104 ;;; M
105 ;;; b s b s h
106 ;;; g '
107 ;;; r u
108 ;;; u
109 ;;;
110 ;;; Consonants ''', 'w', 'y', 'r' take special forms when they are used
111 ;;; as subjoined consonant. Consonant 'r' takes another special form
112 ;;; when used as superjoined as in "rka", and so on, while it does not
113 ;;; change its form when conjoined with subjoined ''', 'w' or 'y'
114 ;;; as in "rwa", "rya".
115 ;;;
116 ;;;
117 ;;; As a Tibetan input method should avoid using conversion key,
118 ;;; we use a "Tibetan glyph -> transcription -> Tibetan glyph"
119 ;;; translation at each key input.
120 ;;;
121 ;;; 1st stage - Check the preceding char.
122 ;;; If the preceding char is Tibetan and composable, then
123 ;;;
124 ;;; 2nd stage - Translate the preceding char into transcription
125 ;;;
126 ;;; 3rd stage - Concatenate the transcription of preceding char
127 ;;; and the current input key.
128 ;;;
129 ;;; 4th stage - Re-translate the concatenated transcription into
130 ;;; a sequence of Tibetan letters.
131 ;;;
132 ;;; 5th stage - Convert leading consonants into one single precomposed char
133 ;;; if possible.
134 ;;;
135 ;;; 6th stage - Compose the consonants into one composite glyph.
136 ;;;
137 ;;; (If the current input is a vowel sign or a vowel modifier,
138 ;;; then it is composed with preceding char without checking
139 ;;; except when the preceding char is a punctuation or a digit.)
140 ;;;
141 ;;;
142
143 ;;; This function is used to avoid composition
144 ;;; between Tibetan and non-Tibetan chars.
145
146 ;;;###autoload
147 (defun tibetan-char-examin (ch)
148 "Check if char CH is Tibetan character.
149 Returns non-nil if CH is Tibetan. Otherwise, returns nil."
150 (let ((chr ch))
151 (if (eq (char-charset chr) 'composition)
152 (string-match "\\cq+" (decompose-composite-char chr))
153 (string-match "\\cq" (char-to-string chr)))))
154
155 ;;; This is used to avoid composition between digits, signs, punctuations
156 ;;; and word constituents.
157
158 ;;;###autoload
159 (defun tibetan-composable-examin (ch)
160 "Check if Tibetan char CH is composable.
161 Returns t if CH is a composable char \(i.e. neither punctuation nor digit)."
162 (let ((chr ch)
163 chstr)
164 (if (eq (char-charset chr) 'composition)
165 (setq chstr (decompose-composite-char chr))
166 (setq chstr (char-to-string chr)))
167 (not (string-match "[$(7!1(B-$(7!o"f$(8!;!=!?!@!A!D"`(B]" chstr))))
168
169
170 ;;; This checks if a character to be composed contains already
171 ;;; one or more vowels / vowel modifiers. If the character contains
172 ;;; them, then no more consonant should be added.
173
174 ;;;###autoload
175 (defun tibetan-complete-char-examin (ch)
176 "Check if composite char CH contains one or more vowel/vowel modifiers.
177 Returns non-nil, if CH contains vowel/vowel modifiers."
178 (let ((chr ch)
179 chstr)
180 (if (eq (char-charset chr) 'composition)
181 (setq chstr (decompose-composite-char chr))
182 (setq chstr (char-to-string chr)))
183 (string-match "[$(7!g!e"Q(B-$(7"^"_(B-$(7"l(B]" chstr)))
184
185 ;;; This function makes a composite character consisting of two characters
186 ;;; vertically stacked.
187
188 ;;;###autoload
189 (defun tibetan-vertical-stacking (first second upward)
190 "Return a vertically stacked composite char consisting of FIRST and SECOND.
191 If UPWARD is non-nil, then SECOND is put above FIRST."
192 (if upward
193 (compose-chars first '(tc . bc) second)
194 (compose-chars first '(bc . tc) second)))
195
196 ;;; This function makes a composite char from a string.
197 ;;; Note that this function returns a string, not a char.
198
199 ;;;###autoload
200 (defun tibetan-compose-string (str)
201 "Compose a sequence of Tibetan character components into a composite character.
202 Returns a string containing a composite character."
203 (let ((t-str str)
204 f-str s-str f-ch s-ch rest composed result)
205 ;;Make sure no redundant vowel sign is present.
206 (if (string-match
207 "^\\(.+\\)\\($(7"Q(B\\)\\([$(7!I!g!e"Q(B-$(7"^"_(B-$(7"l(B]\\)" t-str)
208 (setq t-str (concat
209 (match-string 1 t-str)
210 (match-string 3 t-str))))
211 (if (string-match
212 "^\\(.+\\)\\([$(7!I!g!e"Q(B-$(7"^"_(B-$(7"l(B]\\)\\($(7"Q(B\\)" t-str)
213 (setq t-str (concat
214 (match-string 1 t-str)
215 (match-string 2 t-str))))
216 ;;Start conversion.
217 (setq result "")
218 ;; Consecutive base/precomposed consonants are reduced to the last one.
219 (while (string-match "^\\([$(7"!(B-$(7"J$!(B-$(7%u(B]\\)\\([$(7"!(B-$(7"@"B(B-$(7"J$!(B-$(7%u(B].*\\)" t-str)
220 (setq result (concat result (match-string 1 t-str)))
221 (setq t-str (match-string 2 t-str)))
222 ;; Vowel/vowel modifier, subjoined consonants are added one by one
223 ;; to the preceding element.
224 (while
225 (string-match "^\\(.\\)\\([$(7"A#!(B-$(7#J!I!g!e"Q(B-$(7"^"_(B-$(7"l(B]\\)\\(.*\\)" t-str)
226 (setq f-str (match-string 1 t-str))
227 (setq f-ch (string-to-char f-str))
228 (setq s-str (match-string 2 t-str))
229 ;;Special treatment for 'a chung.
230 ;;If 'a follows a consonant, then turned into its subjoined form.
231 (if (and (string-match "$(7"A(B" s-str)
232 (not (tibetan-complete-char-examin f-ch)))
233 (setq s-str "$(7#A(B"))
234 (setq s-ch (string-to-char s-str))
235 (setq rest (match-string 3 t-str))
236 (cond ((string-match "\\c2" s-str);; upper vowel sign
237 (setq composed
238 (tibetan-vertical-stacking f-ch s-ch t)))
239 ((string-match "\\c3" s-str);; lower vowel sign
240 (setq composed
241 (tibetan-vertical-stacking f-ch s-ch nil)))
242 ;;Automatic conversion of ra-mgo (superscribed r).
243 ;;'r' is converted if followed by a subjoined consonant
244 ;;other than w, ', y, r.
245 ((and (string-match "$(7"C(B" f-str)
246 (not (string-match "[$(7#>#A#B#C(B]" s-str)))
247 (setq f-ch ?$(7#P(B)
248 (setq composed
249 (tibetan-vertical-stacking f-ch s-ch nil)))
250 ((not (tibetan-complete-char-examin f-ch))
251 ;;Initial base consonant is tranformed, if followed by
252 ;;a subjoined consonant, except when it is followed
253 ;;by a subscribed 'a.
254 (if (and (string-match "[$(7"!(B-$(7"="?"@"D(B-$(7"J(B]" f-str)
255 (not (string-match "$(7#A(B" s-str)))
256 (setq f-ch
257 (string-to-char
258 (cdr (assoc f-str tibetan-base-to-subjoined-alist)))))
259 (setq composed
260 (tibetan-vertical-stacking f-ch s-ch nil)))
261 (t
262 (setq composed s-str)
263 (setq result (concat result f-str))))
264 (setq t-str (concat composed rest)))
265 (setq result (concat result t-str))))
266
267 ;;; quail <-> conversion interface.
268
269 (defun tibetan-composition (pc key)
270 "Interface to quail input method.
271 Takes two arguments: char PC and string KEY, where PC is the preceding
272 character to be composed with current input KEY.
273 Returns a string which is the result of composition."
274 (let (trans cur-ch t-str result)
275 ;; Make a tibetan character corresponding to current input key.
276 (setq cur-ch (tibetan-transcription-to-tibetan key))
277 ;; Check if the preceding character is Tibetan and composable.
278 (cond ((and (tibetan-char-examin pc)
279 (tibetan-composable-examin pc))
280 ;;If Tibetan char corresponding to the current input key exists,
281 (cond (cur-ch
282 ;; Then,
283 ;; Convert the preceding character into transcription,
284 ;; and concatenate it with the current input key,
285 (setq trans (tibetan-tibetan-to-transcription pc))
286 (setq trans (concat trans key))
287 ;; Concatenated transcription is converted to
288 ;; a sequence of Tibetan characters,
289 (setq t-str (tibetan-transcription-to-tibetan trans))
290 ;; And it is composed into a composite character.
291 (setq result (tibetan-compose-string t-str)))
292 ;; Else,
293 (t
294 ;; Simply concatenate the preceding character and
295 ;; the current input key.
296 (setq result (char-to-string pc))
297 (setq result (concat result key)))))
298 ;; If the preceding char is not Tibetan or not composable,
299 (t
300 ;; pc = 0 means the point is at the beginning of buffer.
301 (if (not (eq pc 0))
302 (setq result (char-to-string pc)))
303 (if cur-ch
304 (setq result (concat result cur-ch))
305 (setq result (concat result key))))
306 )))
307
308
309 ;;;###autoload
310 (defun tibetan-decompose-region (beg end)
311 "Decompose Tibetan characters in the region BEG END into their components.
312 Components are: base and subjoined consonants, vowel signs, vowel modifiers.
313 One column punctuations are converted to their 2 column equivalents."
314 (interactive "r")
315 (let (ch-str ch-beg ch-end)
316 (save-excursion
317 (save-restriction
318 (narrow-to-region beg end)
319 (goto-char (point-min))
320 ;; \\cq = Tibetan character
321 (while (re-search-forward "\\cq" nil t)
322 (setq ch-str (buffer-substring-no-properties
323 (match-beginning 0) (match-end 0)))
324 ;; Save the points. Maybe, using save-match-data is preferable.
325 ;; But in order not to lose the trace(because the body is too long),
326 ;; we save the points in variables.
327 (setq ch-beg (match-beginning 0))
328 (setq ch-end (match-end 0))
329 ;; Here starts the decomposition.
330 (cond
331 ;; 1 column punctuations -> 2 column equivalent
332 ((string-match "[$(8!D!;!=!?!@!A"`(B]" ch-str)
333 (setq ch-str
334 (car (rassoc ch-str tibetan-precomposition-rule-alist))))
335 ;; Decomposition of composite character.
336 ((eq (char-charset (string-to-char ch-str)) 'composition)
337 ;; Make a string which consists of a sequence of
338 ;; components.
339 (setq ch-str (decompose-composite-char (string-to-char ch-str)))
340 ;; Converts nyi zla into base elements.
341 (cond ((string= ch-str "$(7#R#S#S#S(B")
342 (setq ch-str "$(7!4!5!5(B"))
343 ((string= ch-str "$(7#R#S#S(B")
344 (setq ch-str "$(7!4!5(B"))
345 ((string= ch-str "$(7#R#S!I(B")
346 (setq ch-str "$(7!6(B"))
347 ((string= ch-str "$(7#R#S(B")
348 (setq ch-str "$(7!4(B")))))
349 ;; If the sequence of components starts with a subjoined consonants,
350 (if (string-match "^\\([$(7#!(B-$(7#J(B]\\)\\(.*\\)$" ch-str)
351 ;; then the first components is converted to its base form.
352 (setq ch-str
353 (concat (car (rassoc (match-string 1 ch-str)
354 tibetan-base-to-subjoined-alist))
355 (match-string 2 ch-str))))
356 ;; If the sequence of components starts with a precomposed character,
357 (if (string-match "^\\([$(7$!(B-$(7%u(B]\\)\\(.*\\)$" ch-str)
358 ;; then it is converted into a sequence of components.
359 (setq ch-str
360 (concat (car (rassoc (match-string 1 ch-str)
361 tibetan-precomposition-rule-alist))
362 (match-string 2 ch-str))))
363 ;; Special treatment for superscribed r.
364 (if (string-match "^$(7#P(B\\(.*\\)$" ch-str)
365 (setq ch-str (concat "$(7"C(B" (match-string 1 ch-str))))
366 ;; Finally, the result of decomposition is inserted, and
367 ;; the composite character is deleted.
368 (insert-and-inherit ch-str)
369 (delete-region ch-beg ch-end))))))
370
371 ;;;###autoload
372 (defun tibetan-compose-region (beg end)
373 "Make composite chars from Tibetan character components in the region BEG END.
374 Two column punctuations are converted to their 1 column equivalents."
375 (interactive "r")
376 (let (str result)
377 (save-excursion
378 (save-restriction
379 (narrow-to-region beg end)
380 (goto-char (point-min))
381 ;; First, sequence of components which has a precomposed equivalent
382 ;; is converted.
383 (while (re-search-forward
384 tibetan-precomposition-rule-regexp nil t)
385 (setq str (buffer-substring-no-properties
386 (match-beginning 0) (match-end 0)))
387 (save-match-data
388 (insert-and-inherit
389 (cdr (assoc str tibetan-precomposition-rule-alist))))
390 (delete-region (match-beginning 0) (match-end 0)))
391 (goto-char (point-min))
392 ;; Then, composable elements are put into a composite character.
393 (while (re-search-forward
394 "[$(7"!(B-$(7"J$!(B-$(7%u(B]+[$(7#!(B-$(7#J!I!g!e"Q(B-$(7"^"_(B-$(7"l(B]+"
395 nil t)
396 (setq str (buffer-substring-no-properties
397 (match-beginning 0) (match-end 0)))
398 (save-match-data
399 (setq result (tibetan-compose-string str))
400 (insert-and-inherit result))
401 (delete-region (match-beginning 0) (match-end 0)))))))
402
403 ;;;
404 ;;; This variable is used to avoid repeated decomposition.
405 ;;;
406 (setq-default tibetan-decomposed nil)
407
408 ;;;###autoload
409 (defun tibetan-decompose-buffer ()
410 "Decomposes Tibetan characters in the buffer into their components.
411 See also docstring of the function tibetan-decompose-region."
412 (interactive)
413 (make-local-variable 'tibetan-decomposed)
414 (cond ((not tibetan-decomposed)
415 (tibetan-decompose-region (point-min) (point-max))
416 (setq tibetan-decomposed t))))
417
418 ;;;###autoload
419 (defun tibetan-compose-buffer ()
420 "Composes Tibetan character components in the buffer.
421 See also docstring of the function tibetan-compose-region."
422 (interactive)
423 (make-local-variable 'tibetan-decomposed)
424 (tibetan-compose-region (point-min) (point-max))
425 (setq tibetan-decomposed nil))
426
427 ;;;###autoload
428 (defun tibetan-post-read-conversion (len)
429 (save-excursion
430 (save-restriction
431 (let ((buffer-modified-p (buffer-modified-p)))
432 (narrow-to-region (point) (+ (point) len))
433 (tibetan-compose-region (point-min) (point-max))
434 (set-buffer-modified-p buffer-modified-p)
435 (point-max))))
436 (make-local-variable 'tibetan-decomposed)
437 (setq tibetan-decomposed nil))
438
439
440 ;;;###autoload
441 (defun tibetan-pre-write-conversion (from to)
442 (setq tibetan-decomposed-temp tibetan-decomposed)
443 (let ((old-buf (current-buffer))
444 (work-buf (get-buffer-create " *tibetan-work*")))
445 (set-buffer work-buf)
446 (erase-buffer)
447 (insert-buffer-substring old-buf from to)
448 (if (not tibetan-decomposed-temp)
449 (tibetan-decompose-region (point-min) (point-max)))))
450
451 (provide 'language/tibet-util)
452
453 ;;; language/tibet-util.el ends here.