Remove text on directory-abbrev-alist omitted in 2011-01-08 commit.
[emacs.git] / lisp / language / tibet-util.el
blob2f446a181411f17833387fd5aa7bb11ef70db947
1 ;;; tibet-util.el --- utilities for Tibetan -*- coding: iso-2022-7bit; -*-
3 ;; Copyright (C) 1997, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
4 ;; Free Software Foundation, Inc.
5 ;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
6 ;; 2005, 2006, 2007, 2008, 2009, 2010, 2011
7 ;; National Institute of Advanced Industrial Science and Technology (AIST)
8 ;; Registration Number H14PRO021
10 ;; Author: Toru TOMABECHI <Toru.Tomabechi@orient.unil.ch>
11 ;; Keywords: multilingual, Tibetan
12 ;; Created: Feb. 17. 1997
14 ;; This file is part of GNU Emacs.
16 ;; GNU Emacs is free software: you can redistribute it and/or modify
17 ;; it under the terms of the GNU General Public License as published by
18 ;; the Free Software Foundation, either version 3 of the License, or
19 ;; (at your option) any later version.
21 ;; GNU Emacs is distributed in the hope that it will be useful,
22 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
23 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 ;; GNU General Public License for more details.
26 ;; You should have received a copy of the GNU General Public License
27 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
29 ;;; History:
30 ;; 1997.03.13 Modification in treatment of text properties;
31 ;; Support for some special signs and punctuations.
32 ;; 1999.10.25 Modification for a new composition way by K.Handa.
34 ;;; Commentary:
36 ;;; Code:
38 (defconst tibetan-obsolete-glyphs
39 `(("\e$(7!=\e(B" . "\e$(7!=\e(B") ; 2 col <-> 1 col
40 ("\e$(7!?\e(B" . "\e$(7!?\e(B")
41 ("\e$(7!@\e(B" . "\e$(7!@\e(B")
42 ("\e$(7!A\e(B" . "\e$(7!A\e(B")
43 ("\e$(7"`\e(B" . "\e$(7"`\e(B")
44 ("\e$(7!;\e(B" . "\e$(7!;\e(B")
45 ("\e$(7!D\e(B" . "\e$(7!D\e(B")
46 ;; Yes these are dirty. But ...
47 ("\e$(7!>\e(B \e$(7!>\e(B" . ,(compose-string "\e$(7!>\e(B \e$(7!>\e(B" 0 3 [?\e$(7!>\e(B (Br . Bl) ? (Br . Bl) ?\e$(7!>\e(B]))
48 ("\e$(7!4!5!5\e(B" . ,(compose-string
49 "\e$(7#R#S#S#S\e(B" 0 4
50 [?\e$(7#R\e(B (Br . Bl) ?\e$(7#S\e(B (Br . Bl) ?\e$(7#S\e(B (Br . Bl) ?\e$(7#S\e(B]))
51 ("\e$(7!4!5\e(B" . ,(compose-string "\e$(7#R#S#S\e(B" 0 3 [?\e$(7#R\e(B (Br . Bl) ?\e$(7#S\e(B (Br . Bl) ?\e$(7#S\e(B]))
52 ("\e$(7!6\e(B" . ,(compose-string "\e$(7#R#S!I\e(B" 0 3 [?\e$(7#R\e(B (Br . Bl) ?\e$(7#S\e(B (br . tr) ?\e$(7!I\e(B]))
53 ("\e$(7!4\e(B" . ,(compose-string "\e$(7#R#S\e(B" 0 2 [?\e$(7#R\e(B (Br . Bl) ?\e$(7#S\e(B]))))
55 ;;;###autoload
56 (defun tibetan-char-p (ch)
57 "Check if char CH is Tibetan character.
58 Returns non-nil if CH is Tibetan. Otherwise, returns nil."
59 (memq (char-charset ch) '(tibetan tibetan-1-column)))
61 ;;; Functions for Tibetan <-> Tibetan-transcription.
63 ;;;###autoload
64 (defun tibetan-tibetan-to-transcription (str)
65 "Transcribe Tibetan string STR and return the corresponding Roman string."
66 (let (;; Accumulate transcriptions here in reverse order.
67 (trans nil)
68 (len (length str))
69 (i 0)
70 ch this-trans)
71 (while (< i len)
72 (let ((idx (string-match tibetan-precomposition-rule-regexp str i)))
73 (if (eq idx i)
74 ;; Ith character and the followings matches precomposable
75 ;; Tibetan sequence.
76 (setq i (match-end 0)
77 this-trans
78 (car (rassoc
79 (cdr (assoc (match-string 0 str)
80 tibetan-precomposition-rule-alist))
81 tibetan-precomposed-transcription-alist)))
82 (setq ch (substring str i (1+ i))
83 i (1+ i)
84 this-trans
85 (car (or (rassoc ch tibetan-consonant-transcription-alist)
86 (rassoc ch tibetan-vowel-transcription-alist)
87 (rassoc ch tibetan-subjoined-transcription-alist)))))
88 (setq trans (cons this-trans trans))))
89 (apply 'concat (nreverse trans))))
91 ;;;###autoload
92 (defun tibetan-transcription-to-tibetan (str)
93 "Convert Tibetan Roman string STR to Tibetan character string.
94 The returned string has no composition information."
95 (let (;; Case is significant.
96 (case-fold-search nil)
97 (idx 0)
98 ;; Accumulate Tibetan strings here in reverse order.
99 (t-str-list nil)
100 i subtrans)
101 (while (setq i (string-match tibetan-regexp str idx))
102 (if (< idx i)
103 ;; STR contains a pattern that doesn't match Tibetan
104 ;; transcription. Include the pattern as is.
105 (setq t-str-list (cons (substring str idx i) t-str-list)))
106 (setq subtrans (match-string 0 str)
107 idx (match-end 0))
108 (let ((t-char (cdr (assoc subtrans
109 tibetan-precomposed-transcription-alist))))
110 (if t-char
111 ;; SUBTRANS corresponds to a transcription for
112 ;; precomposable Tibetan sequence.
113 (setq t-char (car (rassoc t-char
114 tibetan-precomposition-rule-alist)))
115 (setq t-char
116 (cdr
117 (or (assoc subtrans tibetan-consonant-transcription-alist)
118 (assoc subtrans tibetan-vowel-transcription-alist)
119 (assoc subtrans tibetan-modifier-transcription-alist)
120 (assoc subtrans tibetan-subjoined-transcription-alist)))))
121 (setq t-str-list (cons t-char t-str-list))))
122 (if (< idx (length str))
123 (setq t-str-list (cons (substring str idx) t-str-list)))
124 (apply 'concat (nreverse t-str-list))))
127 ;;; Functions for composing/decomposing Tibetan sequence.
129 ;;; A Tibetan syllable is typically structured as follows:
131 ;;; [Prefix] C [C+] V [M] [Suffix [Post suffix]]
133 ;;; where C's are all vertically stacked, V appears below or above
134 ;;; consonant cluster and M is always put above the C[C+]V combination.
135 ;;; (Sanskrit visarga, though it is a vowel modifier, is considered
136 ;;; to be a punctuation.)
138 ;;; Here are examples of the words "bsgrubs" and "hfauM"
140 ;;; \e$(7"7"G###C"U"7"G\e(B \e$(7"H"R"U"_\e(B
142 ;;; M
143 ;;; b s b s h
144 ;;; g fa
145 ;;; r u
146 ;;; u
148 ;;; Consonants `'' (\e$(7"A\e(B), `w' (\e$(7">\e(B), `y' (\e$(7"B\e(B), `r' (\e$(7"C\e(B) take special
149 ;;; forms when they are used as subjoined consonant. Consonant `r'
150 ;;; takes another special form when used as superjoined in such a case
151 ;;; as "rka", while it does not change its form when conjoined with
152 ;;; subjoined `'', `w' or `y' as in "rwa", "rya".
154 ;; Append a proper composition rule and glyph to COMPONENTS to compose
155 ;; CHAR with a composition that has COMPONENTS.
157 (defun tibetan-add-components (components char)
158 (let ((last (last components))
159 (stack-upper '(tc . bc))
160 (stack-under '(bc . tc))
161 rule comp-vowel tmp)
162 ;; Special treatment for 'a chung.
163 ;; If 'a follows a consonant, turn it into the subjoined form.
164 ;; * Disabled by Tomabechi 2000/06/09 *
165 ;; Because in Unicode, \e$(7"A\e(B may follow directly a consonant without
166 ;; any intervening vowel, as in \e$(7"9"""Q"A!;\e(B=\e$(7"9\e(B \e$(7""\e(B \e$(7"A\e(B not \e$(7"9\e(B \e$(7""\e(B \e$(7"Q\e(B \e$(7"A\e(B
167 ;;(if (and (= char ?\e$(7"A\e(B)
168 ;; (aref (char-category-set (car last)) ?0))
169 ;; (setq char ?\e$(7"R\e(B)) ;; modified for new font by Tomabechi 1999/12/10
171 ;; Composite vowel signs are decomposed before being added
172 ;; Added by Tomabechi 2000/06/08
173 (if (memq char '(?\e$(7"T\e(B ?\e$(7"V\e(B ?\e$(7"W\e(B ?\e$(7"X\e(B ?\e$(7"Y\e(B ?\e$(7"Z\e(B ?\e$(7"b\e(B))
174 (setq comp-vowel
175 (copy-sequence
176 (cddr (assoc (char-to-string char)
177 tibetan-composite-vowel-alist)))
178 char
179 (cadr (assoc (char-to-string char)
180 tibetan-composite-vowel-alist))))
181 (cond
182 ;; Compose upper vowel sign vertically over.
183 ((aref (char-category-set char) ?2)
184 (setq rule stack-upper))
186 ;; Compose lower vowel sign vertically under.
187 ((aref (char-category-set char) ?3)
188 (if (or (eq char ?\e$(7"Q\e(B) ;; `\e$(7"Q\e(B' and `\e$,1FP\e(B' should not visible when composed.
189 (eq char #xF70))
190 (setq rule nil)
191 (setq rule stack-under)))
192 ;; Transform ra-mgo (superscribed r) if followed by a subjoined
193 ;; consonant other than w, ', y, r.
194 ((and (= (car last) ?\e$(7"C\e(B)
195 (not (memq char '(?\e$(7#>\e(B ?\e$(7"R\e(B ?\e$(7#B\e(B ?\e$(7#C\e(B))))
196 (setcar last ?\e$(7!"\e(B) ;; modified for newfont by Tomabechi 1999/12/10
197 (setq rule stack-under))
198 ;; Transform initial base consonant if followed by a subjoined
199 ;; consonant but 'a.
201 (let ((laststr (char-to-string (car last))))
202 (if (and (/= char ?\e$(7"R\e(B) ;; modified for new font by Tomabechi
203 (string-match "[\e$(7"!\e(B-\e$(7"="?"@"D\e(B-\e$(7"J"K\e(B]" laststr))
204 (setcar last (string-to-char
205 (cdr (assoc (char-to-string (car last))
206 tibetan-base-to-subjoined-alist)))))
207 (setq rule stack-under))))
209 (if rule
210 (setcdr last (list rule char)))
211 ;; Added by Tomabechi 2000/06/08
212 (if comp-vowel
213 (nconc last comp-vowel))
216 ;;;###autoload
217 (defun tibetan-compose-string (str)
218 "Compose Tibetan string STR."
219 (let ((idx 0))
220 ;; `\e$(7"A\e(B' is included in the pattern for subjoined consonants
221 ;; because we treat it specially in tibetan-add-components.
222 ;; (This feature is removed by Tomabechi 2000/06/08)
223 (while (setq idx (string-match tibetan-composable-pattern str idx))
224 (let ((from idx)
225 (to (match-end 0))
226 components)
227 (if (eq (string-match tibetan-precomposition-rule-regexp str idx) idx)
228 (setq idx (match-end 0)
229 components
230 (list (string-to-char
231 (cdr
232 (assoc (match-string 0 str)
233 tibetan-precomposition-rule-alist)))))
234 (setq components (list (aref str idx))
235 idx (1+ idx)))
236 (while (< idx to)
237 (tibetan-add-components components (aref str idx))
238 (setq idx (1+ idx)))
239 (compose-string str from to components))))
240 str)
242 ;;;###autoload
243 (defun tibetan-compose-region (beg end)
244 "Compose Tibetan text the region BEG and END."
245 (interactive "r")
246 (let (str result chars)
247 (save-excursion
248 (save-restriction
249 (narrow-to-region beg end)
250 (goto-char (point-min))
251 ;; `\e$(7"A\e(B' is included in the pattern for subjoined consonants
252 ;; because we treat it specially in tibetan-add-components.
253 ;; (This feature is removed by Tomabechi 2000/06/08)
254 (while (re-search-forward tibetan-composable-pattern nil t)
255 (let ((from (match-beginning 0))
256 (to (match-end 0))
257 components)
258 (goto-char from)
259 (if (looking-at tibetan-precomposition-rule-regexp)
260 (progn
261 (setq components
262 (list (string-to-char
263 (cdr
264 (assoc (match-string 0)
265 tibetan-precomposition-rule-alist)))))
266 (goto-char (match-end 0)))
267 (setq components (list (char-after from)))
268 (forward-char 1))
269 (while (< (point) to)
270 (tibetan-add-components components (following-char))
271 (forward-char 1))
272 (compose-region from to components)))))))
274 (defvar tibetan-decompose-precomposition-alist
275 (mapcar (function (lambda (x) (cons (string-to-char (cdr x)) (car x))))
276 tibetan-precomposition-rule-alist))
278 ;;;###autoload
279 (defun tibetan-decompose-region (from to)
280 "Decompose Tibetan text in the region FROM and TO.
281 This is different from decompose-region because precomposed Tibetan characters
282 are decomposed into normal Tibetan character sequences."
283 (interactive "r")
284 (save-restriction
285 (narrow-to-region from to)
286 (decompose-region from to)
287 (goto-char from)
288 (while (not (eobp))
289 (let* ((char (following-char))
290 (slot (assq char tibetan-decompose-precomposition-alist)))
291 (if slot
292 (progn
293 (delete-char 1)
294 (insert (cdr slot)))
295 (forward-char 1))))))
298 ;;;###autoload
299 (defun tibetan-decompose-string (str)
300 "Decompose Tibetan string STR.
301 This is different from decompose-string because precomposed Tibetan characters
302 are decomposed into normal Tibetan character sequences."
303 (let ((new "")
304 (len (length str))
305 (idx 0)
306 char slot)
307 (while (< idx len)
308 (setq char (aref str idx)
309 slot (assq (aref str idx) tibetan-decompose-precomposition-alist)
310 new (concat new (if slot (cdr slot) (char-to-string char)))
311 idx (1+ idx)))
312 new))
315 ;;; This variable is used to avoid repeated decomposition.
317 (setq-default tibetan-decomposed nil)
319 ;;;###autoload
320 (defun tibetan-decompose-buffer ()
321 "Decomposes Tibetan characters in the buffer into their components.
322 See also the documentation of the function `tibetan-decompose-region'."
323 (interactive)
324 (make-local-variable 'tibetan-decomposed)
325 (cond ((not tibetan-decomposed)
326 (tibetan-decompose-region (point-min) (point-max))
327 (setq tibetan-decomposed t))))
329 ;;;###autoload
330 (defun tibetan-compose-buffer ()
331 "Composes Tibetan character components in the buffer.
332 See also docstring of the function tibetan-compose-region."
333 (interactive)
334 (make-local-variable 'tibetan-decomposed)
335 (tibetan-compose-region (point-min) (point-max))
336 (setq tibetan-decomposed nil))
338 ;;;###autoload
339 (defun tibetan-post-read-conversion (len)
340 (save-excursion
341 (save-restriction
342 (let ((buffer-modified-p (buffer-modified-p)))
343 (narrow-to-region (point) (+ (point) len))
344 (tibetan-compose-region (point-min) (point-max))
345 (set-buffer-modified-p buffer-modified-p)
346 (make-local-variable 'tibetan-decomposed)
347 (setq tibetan-decomposed nil)
348 (- (point-max) (point-min))))))
351 ;;;###autoload
352 (defun tibetan-pre-write-conversion (from to)
353 (setq tibetan-decomposed-temp tibetan-decomposed)
354 (let ((old-buf (current-buffer)))
355 (set-buffer (generate-new-buffer " *temp*"))
356 (if (stringp from)
357 (insert from)
358 (insert-buffer-substring old-buf from to))
359 (if (not tibetan-decomposed-temp)
360 (tibetan-decompose-region (point-min) (point-max)))
361 ;; Should return nil as annotations.
362 nil))
366 ;;; Unicode-related definitions.
369 (defvar tibetan-canonicalize-for-unicode-alist
370 '(("\e$(7"Q\e(B" . "") ;; remove vowel a
371 ("\e$(7"T\e(B" . "\e$(7"R"S\e(B") ;; decompose vowels whose use is ``discouraged'' in Unicode 3.0
372 ("\e$(7"V\e(B" . "\e$(7"R"U\e(B")
373 ("\e$(7"W\e(B" . "\e$(7#C"a\e(B")
374 ("\e$(7"X\e(B" . "\e$(7#C"R"a\e(B")
375 ("\e$(7"Y\e(B" . "\e$(7#D"a\e(B")
376 ("\e$(7"Z\e(B" . "\e$(7#D"R"a\e(B")
377 ("\e$(7"b\e(B" . "\e$(7"R"a\e(B"))
378 "Rules for canonicalizing Tibetan vowels for Unicode.")
380 (defvar tibetan-canonicalize-for-unicode-regexp
381 "[\e$(7"Q"T"V"W"X"Y"Z"b\e(B]"
382 "Regexp for Tibetan vowels to be canonicalized in Unicode.")
384 (defun tibetan-canonicalize-for-unicode-region (from to)
385 (save-restriction
386 (narrow-to-region from to)
387 (goto-char from)
388 (while (re-search-forward tibetan-canonicalize-for-unicode-regexp nil t)
389 (let (
390 ;;(from (match-beginning 0))
391 ;;(to (match-end 0))
392 (canonical-form
393 (cdr (assoc (match-string 0)
394 tibetan-canonicalize-for-unicode-alist))))
395 ;;(goto-char from)
396 ;;(delete-region from to)
397 ;;(insert canonical-form)
398 (replace-match canonical-form)
399 ))))
401 (defvar tibetan-strict-unicode t
402 "*Flag to control Tibetan canonicalizing for Unicode.
404 If non-nil, the vowel a is removed and composite vowels are decomposed
405 before writing buffer in Unicode. See also
406 `tibetan-canonicalize-for-unicode-regexp' and
407 `tibetan-canonicalize-for-unicode-alist'.")
409 ;;;###autoload
410 (defun tibetan-pre-write-canonicalize-for-unicode (from to)
411 (let ((old-buf (current-buffer))
412 (strict-unicode tibetan-strict-unicode))
413 (set-buffer (generate-new-buffer " *temp*"))
414 (if (stringp from)
415 (insert from)
416 (insert-buffer-substring old-buf from to))
417 (if strict-unicode
418 (tibetan-canonicalize-for-unicode-region (point-min) (point-max)))
419 ;; Should return nil as annotations.
420 nil))
422 (provide 'tibet-util)
424 ;; arch-tag: 7a7333e8-1584-446c-b39c-a02b9def265d
425 ;;; tibet-util.el ends here