viet-util.el 9.7 KB

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