(tibetan-pre-write-conversion): Make it work
[bpt/emacs.git] / lisp / language / viet-util.el
CommitLineData
3fdc9c8f 1;;; viet-util.el --- utilities for Vietnamese
4ed46869
KH
2
3;; Copyright (C) 1995 Free Software Foundation, Inc.
4;; Copyright (C) 1995 Electrotechnical Laboratory, JAPAN.
5
6;; Keywords: mule, multilingual, Vietnamese
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
369314dc
KH
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.
4ed46869
KH
24
25;;; Commentary:
26
27;; Vietnamese uses ASCII characters and additional 134 unique
28;; characters (these are Latin alphabets with various diacritical and
29;; tone marks). As far as I know, Vietnamese now has 4 different ways
30;; for representing these characters: VISCII, VSCII, VIQR, and
31;; Unicode. VISCII and VSCII are simple 1-byte code which assigns 134
32;; unique characters in control-code area (0x00..0x1F) and right half
33;; area (0x80..0xFF). VIQR is a menmonic encoding specification
34;; representing diacritical marks by following ASCII characters.
35
36;;; Code:
37
38;; VIQR is a menmonic encoding specification for Vietnamese.
39;; It represents diacritical marks by ASCII characters as follows:
40;; ------------+----------+--------
41;; mark | mnemonic | example
42;; ------------+----------+---------
43;; breve | ( | a( -> \e,1e\e(B
44;; circumflex | ^ | a^ -> \e,1b\e(B
45;; horn | + | o+ -> \e,1=\e(B
46;; ------------+----------+---------
47;; acute | ' | a' -> \e,1a\e(B
48;; grave | ` | a` -> \e,1`\e(B
49;; hook above | ? | a? -> \e,1d\e(B
50;; tilde | ~ | a~ -> \e,1c\e(B
51;; dot below | . | a. -> \e,1U\e(B
52;; ------------+----------+---------
53;; d bar | dd | dd -> \e,1p\e(B
54;; ------------+----------+---------
55
56(defvar viet-viqr-alist
57 '(;; lowercase
58 (?\e,1!\e(B . "a('") ; 161
59 (?\e,1"\e(B . "a(`") ; 162
60 (?\e,1#\e(B . "a(.") ; 163
61 (?\e,1$\e(B . "a^'") ; 164
62 (?\e,1%\e(B . "a^`") ; 165
63 (?\e,1&\e(B . "a^?") ; 166
64 (?\e,1'\e(B . "a^.") ; 167
65 (?\e,1(\e(B . "e~") ; 168
66 (?\e,1)\e(B . "e.") ; 169
67 (?\e,1*\e(B . "e^'") ; 170
68 (?\e,1+\e(B . "e^`") ; 171
69 (?\e,1,\e(B . "e^?") ; 172
70 (?\e,1-\e(B . "e^~") ; 173
71 (?\e,1.\e(B . "e^.") ; 174
72 (?\e,1/\e(B . "o^'") ; 175
73 (?\e,10\e(B . "o^`") ; 176
74 (?\e,11\e(B . "o^?") ; 177
75 (?\e,12\e(B . "o^~") ; 178
76 (?\e,15\e(B . "o^.") ; 181
77 (?\e,16\e(B . "o+`") ; 182
78 (?\e,17\e(B . "o+?") ; 183
79 (?\e,18\e(B . "i.") ; 184
80 (?\e,1=\e(B . "o+") ; 189
81 (?\e,1>\e(B . "o+'") ; 190
82 (?\e,1F\e(B . "a(?") ; 198
83 (?\e,1G\e(B . "a(~") ; 199
84 (?\e,1O\e(B . "y`") ; 207
85 (?\e,1Q\e(B . "u+'") ; 209
86 (?\e,1U\e(B . "a.") ; 213
87 (?\e,1V\e(B . "y?") ; 214
88 (?\e,1W\e(B . "u+`") ; 215
89 (?\e,1X\e(B . "u+?") ; 216
90 (?\e,1[\e(B . "y~") ; 219
91 (?\e,1\\e(B . "y.") ; 220
92 (?\e,1^\e(B . "o+~") ; 222
93 (?\e,1_\e(B . "u+") ; 223
94 (?\e,1`\e(B . "a`") ; 224
95 (?\e,1a\e(B . "a'") ; 225
96 (?\e,1b\e(B . "a^") ; 226
97 (?\e,1c\e(B . "a~") ; 227
98 (?\e,1d\e(B . "a?") ; 228
99 (?\e,1e\e(B . "a(") ; 229
100 (?\e,1f\e(B . "u+~") ; 230
101 (?\e,1g\e(B . "a^~") ; 231
102 (?\e,1h\e(B . "e`") ; 232
103 (?\e,1i\e(B . "e'") ; 233
104 (?\e,1j\e(B . "e^") ; 234
105 (?\e,1k\e(B . "e?") ; 235
106 (?\e,1l\e(B . "i`") ; 236
107 (?\e,1m\e(B . "i'") ; 237
108 (?\e,1n\e(B . "i~") ; 238
109 (?\e,1o\e(B . "i?") ; 239
110 (?\e,1p\e(B . "dd") ; 240
111 (?\e,1q\e(B . "u+.") ; 241
112 (?\e,1r\e(B . "o`") ; 242
113 (?\e,1s\e(B . "o'") ; 243
114 (?\e,1t\e(B . "o^") ; 244
115 (?\e,1u\e(B . "o~") ; 245
116 (?\e,1v\e(B . "o?") ; 246
117 (?\e,1w\e(B . "o.") ; 247
118 (?\e,1x\e(B . "u.") ; 248
119 (?\e,1y\e(B . "u`") ; 249
120 (?\e,1z\e(B . "u'") ; 250
121 (?\e,1{\e(B . "u~") ; 251
122 (?\e,1|\e(B . "u?") ; 252
123 (?\e,1}\e(B . "y'") ; 253
124 (?\e,1~\e(B . "o+.") ; 254
125
126 ;; upper case
127 (?\e,2!\e(B . "A('") ; 161
128 (?\e,2"\e(B . "A(`") ; 162
129 (?\e,2#\e(B . "A(.") ; 163
130 (?\e,2$\e(B . "A^'") ; 164
131 (?\e,2%\e(B . "A^`") ; 165
132 (?\e,2&\e(B . "A^?") ; 166
133 (?\e,2'\e(B . "A^.") ; 167
134 (?\e,2(\e(B . "E~") ; 168
135 (?\e,2)\e(B . "E.") ; 169
136 (?\e,2*\e(B . "E^'") ; 170
137 (?\e,2+\e(B . "E^`") ; 171
138 (?\e,2,\e(B . "E^?") ; 172
139 (?\e,2-\e(B . "E^~") ; 173
140 (?\e,2.\e(B . "E^.") ; 174
141 (?\e,2/\e(B . "O^'") ; 175
142 (?\e,20\e(B . "O^`") ; 176
143 (?\e,21\e(B . "O^?") ; 177
144 (?\e,22\e(B . "O^~") ; 178
145 (?\e,25\e(B . "O^.") ; 181
146 (?\e,26\e(B . "O+`") ; 182
147 (?\e,27\e(B . "O+?") ; 183
148 (?\e,28\e(B . "I.") ; 184
149 (?\e,2=\e(B . "O+") ; 189
150 (?\e,2>\e(B . "O+'") ; 190
151 (?\e,2F\e(B . "A(?") ; 198
152 (?\e,2G\e(B . "A(~") ; 199
153 (?\e,2O\e(B . "Y`") ; 207
154 (?\e,2Q\e(B . "U+'") ; 209
155 (?\e,2U\e(B . "A.") ; 213
156 (?\e,2V\e(B . "Y?") ; 214
157 (?\e,2W\e(B . "U+`") ; 215
158 (?\e,2X\e(B . "U+?") ; 216
159 (?\e,2[\e(B . "Y~") ; 219
160 (?\e,2\\e(B . "Y.") ; 220
161 (?\e,2^\e(B . "O+~") ; 222
162 (?\e,2_\e(B . "U+") ; 223
163 (?\e,2`\e(B . "A`") ; 224
164 (?\e,2a\e(B . "A'") ; 225
165 (?\e,2b\e(B . "A^") ; 226
166 (?\e,2c\e(B . "A~") ; 227
167 (?\e,2d\e(B . "A?") ; 228
168 (?\e,2e\e(B . "A(") ; 229
169 (?\e,2f\e(B . "U+~") ; 230
170 (?\e,2g\e(B . "A^~") ; 231
171 (?\e,2h\e(B . "E`") ; 232
172 (?\e,2i\e(B . "E'") ; 233
173 (?\e,2j\e(B . "E^") ; 234
174 (?\e,2k\e(B . "E?") ; 235
175 (?\e,2l\e(B . "I`") ; 236
176 (?\e,2m\e(B . "I'") ; 237
177 (?\e,2n\e(B . "I~") ; 238
178 (?\e,2o\e(B . "I?") ; 239
179 (?\e,2p\e(B . "DD") ; 240
180 (?\e,2p\e(B . "dD") ; 240
181 (?\e,2p\e(B . "Dd") ; 240
182 (?\e,2q\e(B . "U+.") ; 241
183 (?\e,2r\e(B . "O`") ; 242
184 (?\e,2s\e(B . "O'") ; 243
185 (?\e,2t\e(B . "O^") ; 244
186 (?\e,2u\e(B . "O~") ; 245
187 (?\e,2v\e(B . "O?") ; 246
188 (?\e,2w\e(B . "O.") ; 247
189 (?\e,2x\e(B . "U.") ; 248
190 (?\e,2y\e(B . "U`") ; 249
191 (?\e,2z\e(B . "U'") ; 250
192 (?\e,2{\e(B . "U~") ; 251
193 (?\e,2|\e(B . "U?") ; 252
194 (?\e,2}\e(B . "Y'") ; 253
195 (?\e,2~\e(B . "O+.") ; 254
196
197 ;; escape from composition
198 (?\( . "\\(") ; breve (left parenthesis)
199 (?^ . "\\^") ; circumflex (caret)
200 (?+ . "\\+") ; horn (plus sign)
201 (?' . "\\'") ; acute (apostrophe)
202 (?` . "\\`") ; grave (backquote)
203 (?? . "\\?") ; hook above (question mark)
204 (?~ . "\\~") ; tilde (tilde)
205 (?. . "\\.") ; dot below (period)
206 (?d . "\\d") ; d-bar (d)
207 (?\\ . "\\\\") ; literal backslash
208 )
209 "Alist of Vietnamese characters vs corresponding `VIQR' string.")
210
211;; Regular expression matching single Vietnamese character represented
212;; by VIQR.
213(defconst viqr-regexp
214 "[aeiouyAEIOUY]\\([(^+]?['`?~.]\\|[(^+]\\)\\|[Dd][Dd]")
215
216;;;###autoload
217(defun viet-decode-viqr-region (from to)
218 "Convert `VIQR' mnemonics of the current region to Vietnamese characaters.
219When called from a program, expects two arguments,
220positions (integers or markers) specifying the stretch of the region."
221 (interactive "r")
222 (save-restriction
223 (narrow-to-region from to)
224 (goto-char (point-min))
225 (while (re-search-forward viqr-regexp nil t)
226 (let* ((viqr (buffer-substring (match-beginning 0) (match-end 0)))
06f84575 227 (ch (car (rassoc viqr viet-viqr-alist))))
4ed46869
KH
228 (if ch
229 (progn
230 (delete-region (match-beginning 0) (match-end 0))
231 (insert ch)))))))
232
233;;;###autoload
234(defun viet-decode-viqr-buffer ()
235 "Convert `VIQR' mnemonics of the current buffer to Vietnamese characaters."
236 (interactive)
237 (viet-decode-viqr-region (point-min) (point-max)))
238
239;;;###autoload
240(defun viet-encode-viqr-region (from to)
241 "Convert Vietnamese characaters of the current region to `VIQR' mnemonics.
242When called from a program, expects two arguments,
243positions (integers or markers) specifying the stretch of the region."
244 (interactive "r")
245 (save-restriction
246 (narrow-to-region from to)
247 (goto-char (point-min))
248 (while (re-search-forward "\\cv" nil t)
249 (let* ((ch (preceding-char))
06f84575 250 (viqr (cdr (assq ch viet-viqr-alist))))
4ed46869
KH
251 (if viqr
252 (progn
253 (delete-char -1)
254 (insert viqr)))))))
255
256;;;###autoload
257(defun viet-encode-viqr-buffer ()
258 "Convert Vietnamese characaters of the current buffer to `VIQR' mnemonics."
259 (interactive)
260 (viet-encode-viqr-region (point-min) (point-max)))
261
06f84575
KH
262;;;###autoload
263(defun viqr-post-read-conversion (len)
264 (save-excursion
265 (save-restriction
266 (narrow-to-region (point) (+ (point) len))
267 (let ((buffer-modified-p (buffer-modified-p)))
268 (viet-decode-viqr-region (point-min) (point-max))
269 (set-buffer-modified-p buffer-modified-p)
270 (- (point-max) (point-min))))))
271
272;;;###autoload
273(defun viqr-pre-write-conversion (from to)
274 (let ((old-buf (current-buffer))
275 (work-buf (get-buffer-create " *viet-work*")))
276 (set-buffer work-buf)
277 (erase-buffer)
278 (insert-buffer-substring old-buf from to)
279 (viet-encode-viqr-region (point-min) (point-max))))
280
4ed46869
KH
281;;;
282(provide 'language/viet-util)
283
284;;; Local Variables:
285;;; generated-autoload-file: "../loaddefs.el"
286;;; End:
287;;; viet-util.el ends here