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