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