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