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