Replace xref-match-bounds with xref-match-length
[emacs.git] / lisp / language / viet-util.el
blobf8c11d478919df991c6eb60cf0565ead90dade2d
1 ;;; viet-util.el --- utilities for Vietnamese -*- coding: utf-8; -*-
3 ;; Copyright (C) 1998, 2001-2015 Free Software Foundation, Inc.
4 ;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
5 ;; 2005, 2006, 2007, 2008, 2009, 2010, 2011
6 ;; National Institute of Advanced Industrial Science and Technology (AIST)
7 ;; Registration Number H14PRO021
8 ;; Copyright (C) 2003
9 ;; National Institute of Advanced Industrial Science and Technology (AIST)
10 ;; Registration Number H13PRO009
12 ;; Keywords: mule, multilingual, Vietnamese
14 ;; This file is part of GNU Emacs.
16 ;; GNU Emacs is free software: you can redistribute it and/or modify
17 ;; it under the terms of the GNU General Public License as published by
18 ;; the Free Software Foundation, either version 3 of the License, or
19 ;; (at your option) any later version.
21 ;; GNU Emacs is distributed in the hope that it will be useful,
22 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
23 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 ;; GNU General Public License for more details.
26 ;; You should have received a copy of the GNU General Public License
27 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
29 ;;; Commentary:
31 ;; Vietnamese uses ASCII characters and additional 134 unique
32 ;; characters (these are Latin alphabets with various diacritical and
33 ;; tone marks). As far as I know, Vietnamese now has 5 different ways
34 ;; for representing these characters: VISCII, TCVN-5712, VPS, VIQR,
35 ;; and Unicode. VISCII, TCVN-5712 and VPS are simple 1-byte code
36 ;; which assigns 134 unique characters in control-code area
37 ;; (0x00..0x1F) and right half area (0x80..0xFF). VIQR is a mnemonic
38 ;; encoding specification representing diacritical marks by following
39 ;; ASCII characters.
41 ;;; Code:
43 (defvar viet-viscii-nonascii-translation-table)
45 ;;;###autoload
46 (defun viet-encode-viscii-char (char)
47 "Return VISCII character code of CHAR if appropriate."
48 (encode-char char 'viscii))
50 ;; VIQR is a mnemonic encoding specification for Vietnamese.
51 ;; It represents diacritical marks by ASCII characters as follows:
52 ;; ------------+----------+--------
53 ;; mark | mnemonic | example
54 ;; ------------+----------+---------
55 ;; breve | ( | a( -> ă
56 ;; circumflex | ^ | a^ -> â
57 ;; horn | + | o+ -> ơ
58 ;; ------------+----------+---------
59 ;; acute | ' | a' -> á
60 ;; grave | ` | a` -> à
61 ;; hook above | ? | a? -> ả
62 ;; tilde | ~ | a~ -> ã
63 ;; dot below | . | a. -> ạ
64 ;; ------------+----------+---------
65 ;; d bar | dd | dd -> đ
66 ;; ------------+----------+---------
68 (defvar viet-viqr-alist
69 '(;; lowercase
70 (?ắ . "a('") ; 161
71 (?ằ . "a(`") ; 162
72 (?ặ . "a(.") ; 163
73 (?ấ . "a^'") ; 164
74 (?ầ . "a^`") ; 165
75 (?ẩ . "a^?") ; 166
76 (?ậ . "a^.") ; 167
77 (?ẽ . "e~") ; 168
78 (?ẹ . "e.") ; 169
79 (?ế . "e^'") ; 170
80 (?ề . "e^`") ; 171
81 (?ể . "e^?") ; 172
82 (?ễ . "e^~") ; 173
83 (?ệ . "e^.") ; 174
84 (?ố . "o^'") ; 175
85 (?ồ . "o^`") ; 176
86 (?ổ . "o^?") ; 177
87 (?ỗ . "o^~") ; 178
88 (?ộ . "o^.") ; 181
89 (?ờ . "o+`") ; 182
90 (?ở . "o+?") ; 183
91 (?ị . "i.") ; 184
92 (?ơ . "o+") ; 189
93 (?ớ . "o+'") ; 190
94 (?ẳ . "a(?") ; 198
95 (?ẵ . "a(~") ; 199
96 (?ỳ . "y`") ; 207
97 (?ứ . "u+'") ; 209
98 (?ạ . "a.") ; 213
99 (?ỷ . "y?") ; 214
100 (?ừ . "u+`") ; 215
101 (?ử . "u+?") ; 216
102 (?ỹ . "y~") ; 219
103 (?ỵ . "y.") ; 220
104 (?ỡ . "o+~") ; 222
105 (?ư . "u+") ; 223
106 (?à . "a`") ; 224
107 (?á . "a'") ; 225
108 (?â . "a^") ; 226
109 (?ã . "a~") ; 227
110 (?ả . "a?") ; 228
111 (?ă . "a(") ; 229
112 (?ữ . "u+~") ; 230
113 (?ẫ . "a^~") ; 231
114 (?è . "e`") ; 232
115 (?é . "e'") ; 233
116 (?ê . "e^") ; 234
117 (?ẻ . "e?") ; 235
118 (?ì . "i`") ; 236
119 (?í . "i'") ; 237
120 (?ĩ . "i~") ; 238
121 (?ỉ . "i?") ; 239
122 (?đ . "dd") ; 240
123 (?ự . "u+.") ; 241
124 (?ò . "o`") ; 242
125 (?ó . "o'") ; 243
126 (?ô . "o^") ; 244
127 (?õ . "o~") ; 245
128 (?ỏ . "o?") ; 246
129 (?ọ . "o.") ; 247
130 (?ụ . "u.") ; 248
131 (?ù . "u`") ; 249
132 (?ú . "u'") ; 250
133 (?ũ . "u~") ; 251
134 (?ủ . "u?") ; 252
135 (?ý . "y'") ; 253
136 (?ợ . "o+.") ; 254
138 ;; upper case
139 (?Ắ . "A('") ; 161
140 (?Ằ . "A(`") ; 162
141 (?Ặ . "A(.") ; 163
142 (?Ấ . "A^'") ; 164
143 (?Ầ . "A^`") ; 165
144 (?Ẩ . "A^?") ; 166
145 (?Ậ . "A^.") ; 167
146 (?Ẽ . "E~") ; 168
147 (?Ẹ . "E.") ; 169
148 (?Ế . "E^'") ; 170
149 (?Ề . "E^`") ; 171
150 (?Ể . "E^?") ; 172
151 (?Ễ . "E^~") ; 173
152 (?Ệ . "E^.") ; 174
153 (?Ố . "O^'") ; 175
154 (?Ồ . "O^`") ; 176
155 (?Ổ . "O^?") ; 177
156 (?Ỗ . "O^~") ; 178
157 (?Ộ . "O^.") ; 181
158 (?Ờ . "O+`") ; 182
159 (?Ở . "O+?") ; 183
160 (?Ị . "I.") ; 184
161 (?Ơ . "O+") ; 189
162 (?Ớ . "O+'") ; 190
163 (?Ẳ . "A(?") ; 198
164 (?Ẵ . "A(~") ; 199
165 (?Ỳ . "Y`") ; 207
166 (?Ứ . "U+'") ; 209
167 (?Ạ . "A.") ; 213
168 (?Ỷ . "Y?") ; 214
169 (?Ừ . "U+`") ; 215
170 (?Ử . "U+?") ; 216
171 (?Ỹ . "Y~") ; 219
172 (?Ỵ . "Y.") ; 220
173 (?Ỡ . "O+~") ; 222
174 (?Ư . "U+") ; 223
175 (?À . "A`") ; 224
176 (?Á . "A'") ; 225
177 (? . "A^") ; 226
178 (?Ã . "A~") ; 227
179 (?Ả . "A?") ; 228
180 (?Ă . "A(") ; 229
181 (?Ữ . "U+~") ; 230
182 (?Ẫ . "A^~") ; 231
183 (?È . "E`") ; 232
184 (?É . "E'") ; 233
185 (?Ê . "E^") ; 234
186 (?Ẻ . "E?") ; 235
187 (?Ì . "I`") ; 236
188 (?Í . "I'") ; 237
189 (?Ĩ . "I~") ; 238
190 (?Ỉ . "I?") ; 239
191 (?Đ . "DD") ; 240
192 (?Đ . "dD") ; 240
193 (?Đ . "Dd") ; 240
194 (?Ự . "U+.") ; 241
195 (?Ò . "O`") ; 242
196 (?Ó . "O'") ; 243
197 (?Ô . "O^") ; 244
198 (?Õ . "O~") ; 245
199 (?Ỏ . "O?") ; 246
200 (?Ọ . "O.") ; 247
201 (?Ụ . "U.") ; 248
202 (?Ù . "U`") ; 249
203 (?Ú . "U'") ; 250
204 (?Ũ . "U~") ; 251
205 (?Ủ . "U?") ; 252
206 (?Ý . "Y'") ; 253
207 (?Ợ . "O+.") ; 254
209 ;; escape from composition
210 (?\( . "\\(") ; breve (left parenthesis)
211 (?^ . "\\^") ; circumflex (caret)
212 (?+ . "\\+") ; horn (plus sign)
213 (?' . "\\'") ; acute (apostrophe)
214 (?` . "\\`") ; grave (backquote)
215 (?? . "\\?") ; hook above (question mark)
216 (?~ . "\\~") ; tilde (tilde)
217 (?. . "\\.") ; dot below (period)
218 (?d . "\\d") ; d-bar (d)
219 (?\\ . "\\\\") ; literal backslash
221 "Alist of Vietnamese characters vs corresponding `VIQR' string.")
223 ;; Regular expression matching single Vietnamese character represented
224 ;; by VIQR.
225 (defconst viqr-regexp
226 "[aeiouyAEIOUY]\\([(^+]?['`?~.]\\|[(^+]\\)\\|[Dd][Dd]")
228 ;;;###autoload
229 (defun viet-decode-viqr-region (from to)
230 "Convert `VIQR' mnemonics of the current region to Vietnamese characters.
231 When called from a program, expects two arguments,
232 positions (integers or markers) specifying the stretch of the region."
233 (interactive "r")
234 (save-restriction
235 (narrow-to-region from to)
236 (goto-char (point-min))
237 (while (re-search-forward viqr-regexp nil t)
238 (let* ((viqr (buffer-substring (match-beginning 0) (match-end 0)))
239 (ch (car (rassoc viqr viet-viqr-alist))))
240 (if ch
241 (progn
242 (delete-region (match-beginning 0) (match-end 0))
243 (insert ch)))))))
245 ;;;###autoload
246 (defun viet-decode-viqr-buffer ()
247 "Convert `VIQR' mnemonics of the current buffer to Vietnamese characters."
248 (interactive)
249 (viet-decode-viqr-region (point-min) (point-max)))
251 ;;;###autoload
252 (defun viet-encode-viqr-region (from to)
253 "Convert Vietnamese characters of the current region to `VIQR' mnemonics.
254 When called from a program, expects two arguments,
255 positions (integers or markers) specifying the stretch of the region."
256 (interactive "r")
257 (save-restriction
258 (narrow-to-region from to)
259 (goto-char (point-min))
260 (while (re-search-forward "\\cv" nil t)
261 (let* ((ch (preceding-char))
262 (viqr (cdr (assq ch viet-viqr-alist))))
263 (if viqr
264 (progn
265 (delete-char -1)
266 (insert viqr)))))))
268 ;;;###autoload
269 (defun viet-encode-viqr-buffer ()
270 "Convert Vietnamese characters of the current buffer to `VIQR' mnemonics."
271 (interactive)
272 (viet-encode-viqr-region (point-min) (point-max)))
274 ;;;###autoload
275 (defun viqr-post-read-conversion (len)
276 (save-excursion
277 (save-restriction
278 (narrow-to-region (point) (+ (point) len))
279 (let ((buffer-modified-p (buffer-modified-p)))
280 (viet-decode-viqr-region (point-min) (point-max))
281 (set-buffer-modified-p buffer-modified-p)
282 (- (point-max) (point-min))))))
284 ;;;###autoload
285 (defun viqr-pre-write-conversion (from to)
286 (let ((old-buf (current-buffer)))
287 (set-buffer (generate-new-buffer " *temp*"))
288 (if (stringp from)
289 (insert from)
290 (insert-buffer-substring old-buf from to))
291 (viet-encode-viqr-region (point-min) (point-max))
292 ;; Should return nil as annotations.
293 nil))
296 (provide 'viet-util)
298 ;;; viet-util.el ends here