(viper-ex-work-buf, viper-ex-print-buf): Use defvar, not defconst.
[bpt/emacs.git] / lisp / language / viet-util.el
CommitLineData
60370d40 1;;; viet-util.el --- utilities for Vietnamese -*- coding: iso-2022-7bit; -*-
4ed46869 2
eaa61218
KH
3;; Copyright (C) 1998 Free Software Foundation, Inc.
4;; Copyright (C) 1995, 1997
5;; National Institute of Advanced Industrial Science and Technology (AIST)
6;; Registration Number H14PRO021
4ed46869
KH
7
8;; Keywords: mule, multilingual, Vietnamese
9
10;; This file is part of GNU Emacs.
11
12;; GNU Emacs is free software; you can redistribute it and/or modify
13;; it under the terms of the GNU General Public License as published by
14;; the Free Software Foundation; either version 2, or (at your option)
15;; any later version.
16
17;; GNU Emacs is distributed in the hope that it will be useful,
18;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20;; GNU General Public License for more details.
21
22;; You should have received a copy of the GNU General Public License
369314dc
KH
23;; along with GNU Emacs; see the file COPYING. If not, write to the
24;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25;; Boston, MA 02111-1307, USA.
4ed46869
KH
26
27;;; Commentary:
28
29;; Vietnamese uses ASCII characters and additional 134 unique
30;; characters (these are Latin alphabets with various diacritical and
d2247e75
PJ
31;; tone marks). As far as I know, Vietnamese now has 5 different ways
32;; for representing these characters: VISCII, TCVN-5712, VPS, VIQR,
33;; and Unicode. VISCII, TCVN-5712 and VPS are simple 1-byte code
34;; which assigns 134 unique characters in control-code area
35;; (0x00..0x1F) and right half area (0x80..0xFF). VIQR is a menmonic
36;; encoding specification representing diacritical marks by following
37;; ASCII characters.
4ed46869
KH
38
39;;; Code:
40
0c9470b3
KH
41;;;###autoload
42(defun viet-encode-viscii-char (char)
43 "Return VISCII character code of CHAR if appropriate."
44 (aref (char-table-extra-slot viet-viscii-nonascii-translation-table 0)
45 char))
46
4ed46869
KH
47;; VIQR is a menmonic encoding specification for Vietnamese.
48;; It represents diacritical marks by ASCII characters as follows:
49;; ------------+----------+--------
50;; mark | mnemonic | example
51;; ------------+----------+---------
52;; breve | ( | a( -> \e,1e\e(B
53;; circumflex | ^ | a^ -> \e,1b\e(B
54;; horn | + | o+ -> \e,1=\e(B
55;; ------------+----------+---------
56;; acute | ' | a' -> \e,1a\e(B
57;; grave | ` | a` -> \e,1`\e(B
58;; hook above | ? | a? -> \e,1d\e(B
59;; tilde | ~ | a~ -> \e,1c\e(B
60;; dot below | . | a. -> \e,1U\e(B
61;; ------------+----------+---------
62;; d bar | dd | dd -> \e,1p\e(B
63;; ------------+----------+---------
64
65(defvar viet-viqr-alist
66 '(;; lowercase
67 (?\e,1!\e(B . "a('") ; 161
68 (?\e,1"\e(B . "a(`") ; 162
69 (?\e,1#\e(B . "a(.") ; 163
70 (?\e,1$\e(B . "a^'") ; 164
71 (?\e,1%\e(B . "a^`") ; 165
72 (?\e,1&\e(B . "a^?") ; 166
73 (?\e,1'\e(B . "a^.") ; 167
74 (?\e,1(\e(B . "e~") ; 168
75 (?\e,1)\e(B . "e.") ; 169
76 (?\e,1*\e(B . "e^'") ; 170
77 (?\e,1+\e(B . "e^`") ; 171
78 (?\e,1,\e(B . "e^?") ; 172
79 (?\e,1-\e(B . "e^~") ; 173
80 (?\e,1.\e(B . "e^.") ; 174
81 (?\e,1/\e(B . "o^'") ; 175
82 (?\e,10\e(B . "o^`") ; 176
83 (?\e,11\e(B . "o^?") ; 177
84 (?\e,12\e(B . "o^~") ; 178
85 (?\e,15\e(B . "o^.") ; 181
86 (?\e,16\e(B . "o+`") ; 182
87 (?\e,17\e(B . "o+?") ; 183
88 (?\e,18\e(B . "i.") ; 184
89 (?\e,1=\e(B . "o+") ; 189
90 (?\e,1>\e(B . "o+'") ; 190
91 (?\e,1F\e(B . "a(?") ; 198
92 (?\e,1G\e(B . "a(~") ; 199
93 (?\e,1O\e(B . "y`") ; 207
94 (?\e,1Q\e(B . "u+'") ; 209
95 (?\e,1U\e(B . "a.") ; 213
96 (?\e,1V\e(B . "y?") ; 214
97 (?\e,1W\e(B . "u+`") ; 215
98 (?\e,1X\e(B . "u+?") ; 216
99 (?\e,1[\e(B . "y~") ; 219
100 (?\e,1\\e(B . "y.") ; 220
101 (?\e,1^\e(B . "o+~") ; 222
102 (?\e,1_\e(B . "u+") ; 223
103 (?\e,1`\e(B . "a`") ; 224
104 (?\e,1a\e(B . "a'") ; 225
105 (?\e,1b\e(B . "a^") ; 226
106 (?\e,1c\e(B . "a~") ; 227
107 (?\e,1d\e(B . "a?") ; 228
108 (?\e,1e\e(B . "a(") ; 229
109 (?\e,1f\e(B . "u+~") ; 230
110 (?\e,1g\e(B . "a^~") ; 231
111 (?\e,1h\e(B . "e`") ; 232
112 (?\e,1i\e(B . "e'") ; 233
113 (?\e,1j\e(B . "e^") ; 234
114 (?\e,1k\e(B . "e?") ; 235
115 (?\e,1l\e(B . "i`") ; 236
116 (?\e,1m\e(B . "i'") ; 237
117 (?\e,1n\e(B . "i~") ; 238
118 (?\e,1o\e(B . "i?") ; 239
119 (?\e,1p\e(B . "dd") ; 240
120 (?\e,1q\e(B . "u+.") ; 241
121 (?\e,1r\e(B . "o`") ; 242
122 (?\e,1s\e(B . "o'") ; 243
123 (?\e,1t\e(B . "o^") ; 244
124 (?\e,1u\e(B . "o~") ; 245
125 (?\e,1v\e(B . "o?") ; 246
126 (?\e,1w\e(B . "o.") ; 247
127 (?\e,1x\e(B . "u.") ; 248
128 (?\e,1y\e(B . "u`") ; 249
129 (?\e,1z\e(B . "u'") ; 250
130 (?\e,1{\e(B . "u~") ; 251
131 (?\e,1|\e(B . "u?") ; 252
132 (?\e,1}\e(B . "y'") ; 253
133 (?\e,1~\e(B . "o+.") ; 254
134
135 ;; upper case
136 (?\e,2!\e(B . "A('") ; 161
137 (?\e,2"\e(B . "A(`") ; 162
138 (?\e,2#\e(B . "A(.") ; 163
139 (?\e,2$\e(B . "A^'") ; 164
140 (?\e,2%\e(B . "A^`") ; 165
141 (?\e,2&\e(B . "A^?") ; 166
142 (?\e,2'\e(B . "A^.") ; 167
143 (?\e,2(\e(B . "E~") ; 168
144 (?\e,2)\e(B . "E.") ; 169
145 (?\e,2*\e(B . "E^'") ; 170
146 (?\e,2+\e(B . "E^`") ; 171
147 (?\e,2,\e(B . "E^?") ; 172
148 (?\e,2-\e(B . "E^~") ; 173
149 (?\e,2.\e(B . "E^.") ; 174
150 (?\e,2/\e(B . "O^'") ; 175
151 (?\e,20\e(B . "O^`") ; 176
152 (?\e,21\e(B . "O^?") ; 177
153 (?\e,22\e(B . "O^~") ; 178
154 (?\e,25\e(B . "O^.") ; 181
155 (?\e,26\e(B . "O+`") ; 182
156 (?\e,27\e(B . "O+?") ; 183
157 (?\e,28\e(B . "I.") ; 184
158 (?\e,2=\e(B . "O+") ; 189
159 (?\e,2>\e(B . "O+'") ; 190
160 (?\e,2F\e(B . "A(?") ; 198
161 (?\e,2G\e(B . "A(~") ; 199
162 (?\e,2O\e(B . "Y`") ; 207
163 (?\e,2Q\e(B . "U+'") ; 209
164 (?\e,2U\e(B . "A.") ; 213
165 (?\e,2V\e(B . "Y?") ; 214
166 (?\e,2W\e(B . "U+`") ; 215
167 (?\e,2X\e(B . "U+?") ; 216
168 (?\e,2[\e(B . "Y~") ; 219
169 (?\e,2\\e(B . "Y.") ; 220
170 (?\e,2^\e(B . "O+~") ; 222
171 (?\e,2_\e(B . "U+") ; 223
172 (?\e,2`\e(B . "A`") ; 224
173 (?\e,2a\e(B . "A'") ; 225
174 (?\e,2b\e(B . "A^") ; 226
175 (?\e,2c\e(B . "A~") ; 227
176 (?\e,2d\e(B . "A?") ; 228
177 (?\e,2e\e(B . "A(") ; 229
178 (?\e,2f\e(B . "U+~") ; 230
179 (?\e,2g\e(B . "A^~") ; 231
180 (?\e,2h\e(B . "E`") ; 232
181 (?\e,2i\e(B . "E'") ; 233
182 (?\e,2j\e(B . "E^") ; 234
183 (?\e,2k\e(B . "E?") ; 235
184 (?\e,2l\e(B . "I`") ; 236
185 (?\e,2m\e(B . "I'") ; 237
186 (?\e,2n\e(B . "I~") ; 238
187 (?\e,2o\e(B . "I?") ; 239
188 (?\e,2p\e(B . "DD") ; 240
189 (?\e,2p\e(B . "dD") ; 240
190 (?\e,2p\e(B . "Dd") ; 240
191 (?\e,2q\e(B . "U+.") ; 241
192 (?\e,2r\e(B . "O`") ; 242
193 (?\e,2s\e(B . "O'") ; 243
194 (?\e,2t\e(B . "O^") ; 244
195 (?\e,2u\e(B . "O~") ; 245
196 (?\e,2v\e(B . "O?") ; 246
197 (?\e,2w\e(B . "O.") ; 247
198 (?\e,2x\e(B . "U.") ; 248
199 (?\e,2y\e(B . "U`") ; 249
200 (?\e,2z\e(B . "U'") ; 250
201 (?\e,2{\e(B . "U~") ; 251
202 (?\e,2|\e(B . "U?") ; 252
203 (?\e,2}\e(B . "Y'") ; 253
204 (?\e,2~\e(B . "O+.") ; 254
205
206 ;; escape from composition
207 (?\( . "\\(") ; breve (left parenthesis)
208 (?^ . "\\^") ; circumflex (caret)
209 (?+ . "\\+") ; horn (plus sign)
210 (?' . "\\'") ; acute (apostrophe)
211 (?` . "\\`") ; grave (backquote)
212 (?? . "\\?") ; hook above (question mark)
213 (?~ . "\\~") ; tilde (tilde)
214 (?. . "\\.") ; dot below (period)
215 (?d . "\\d") ; d-bar (d)
216 (?\\ . "\\\\") ; literal backslash
217 )
218 "Alist of Vietnamese characters vs corresponding `VIQR' string.")
219
220;; Regular expression matching single Vietnamese character represented
221;; by VIQR.
222(defconst viqr-regexp
223 "[aeiouyAEIOUY]\\([(^+]?['`?~.]\\|[(^+]\\)\\|[Dd][Dd]")
224
225;;;###autoload
226(defun viet-decode-viqr-region (from to)
c256b4ab 227 "Convert `VIQR' mnemonics of the current region to Vietnamese characters.
4ed46869
KH
228When called from a program, expects two arguments,
229positions (integers or markers) specifying the stretch of the region."
230 (interactive "r")
231 (save-restriction
232 (narrow-to-region from to)
233 (goto-char (point-min))
234 (while (re-search-forward viqr-regexp nil t)
235 (let* ((viqr (buffer-substring (match-beginning 0) (match-end 0)))
06f84575 236 (ch (car (rassoc viqr viet-viqr-alist))))
4ed46869
KH
237 (if ch
238 (progn
239 (delete-region (match-beginning 0) (match-end 0))
240 (insert ch)))))))
241
242;;;###autoload
243(defun viet-decode-viqr-buffer ()
c256b4ab 244 "Convert `VIQR' mnemonics of the current buffer to Vietnamese characters."
4ed46869
KH
245 (interactive)
246 (viet-decode-viqr-region (point-min) (point-max)))
247
248;;;###autoload
249(defun viet-encode-viqr-region (from to)
c256b4ab 250 "Convert Vietnamese characters of the current region to `VIQR' mnemonics.
4ed46869
KH
251When called from a program, expects two arguments,
252positions (integers or markers) specifying the stretch of the region."
253 (interactive "r")
254 (save-restriction
255 (narrow-to-region from to)
256 (goto-char (point-min))
257 (while (re-search-forward "\\cv" nil t)
258 (let* ((ch (preceding-char))
06f84575 259 (viqr (cdr (assq ch viet-viqr-alist))))
4ed46869
KH
260 (if viqr
261 (progn
262 (delete-char -1)
263 (insert viqr)))))))
264
265;;;###autoload
266(defun viet-encode-viqr-buffer ()
c256b4ab 267 "Convert Vietnamese characters of the current buffer to `VIQR' mnemonics."
4ed46869
KH
268 (interactive)
269 (viet-encode-viqr-region (point-min) (point-max)))
270
06f84575
KH
271;;;###autoload
272(defun viqr-post-read-conversion (len)
273 (save-excursion
274 (save-restriction
275 (narrow-to-region (point) (+ (point) len))
276 (let ((buffer-modified-p (buffer-modified-p)))
277 (viet-decode-viqr-region (point-min) (point-max))
278 (set-buffer-modified-p buffer-modified-p)
279 (- (point-max) (point-min))))))
280
281;;;###autoload
282(defun viqr-pre-write-conversion (from to)
ada51178 283 (let ((old-buf (current-buffer)))
319fb5a9
KH
284 (set-buffer (generate-new-buffer " *temp*"))
285 (if (stringp from)
286 (insert from)
287 (insert-buffer-substring old-buf from to))
288 (viet-encode-viqr-region (point-min) (point-max))
567b103c
KH
289 ;; Should return nil as annotations.
290 nil))
06f84575 291
4ed46869 292;;;
650e8505 293(provide 'viet-util)
4ed46869 294
ab5796a9 295;;; arch-tag: 082a4d3b-168f-45b4-b3e1-82bfa1b5a194
4ed46869 296;;; viet-util.el ends here