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