japan-util.el 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. ;;; japan-util.el --- utilities for Japanese -*- coding: iso-2022-7bit; -*-
  2. ;; Copyright (C) 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. ;; Keywords: mule, multilingual, Japanese
  8. ;; This file is part of GNU Emacs.
  9. ;; GNU Emacs is free software: you can redistribute it and/or modify
  10. ;; it under the terms of the GNU General Public License as published by
  11. ;; the Free Software Foundation, either version 3 of the License, or
  12. ;; (at your option) any later version.
  13. ;; GNU Emacs is distributed in the hope that it will be useful,
  14. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. ;; GNU General Public License for more details.
  17. ;; You should have received a copy of the GNU General Public License
  18. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  19. ;;; Commentary:
  20. ;;; Code:
  21. ;;;###autoload
  22. (defun setup-japanese-environment-internal ()
  23. ;; By default, we use 'japanese-iso-8bit for file names. But, the
  24. ;; following prefer-coding-system will override it.
  25. (if (memq system-type '(windows-nt ms-dos cygwin))
  26. (prefer-coding-system 'japanese-shift-jis)
  27. (prefer-coding-system 'japanese-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j(B ?$B%j(B ?(IX(B) (?$B$k(B ?$B%k(B ?(IY(B) (?$B$l(B ?$B%l(B ?(IZ(B) (?$B$m(B ?$B%m(B ?(I[(B)
  39. (?$B$o(B ?$B%o(B ?(I\(B) (?$B$p(B ?$B%p(B "(I2(B") (?$B$q(B ?$B%q(B "(I4(B") (?$B$r(B ?$B%r(B ?(I&(B)
  40. (?$B$s(B ?$B%s(B ?(I](B)
  41. (?$B$,(B ?$B%,(B "(I6^(B") (?$B$.(B ?$B%.(B "(I7^(B") (?$B$0(B ?$B%0(B "(I8^(B") (?$B$2(B ?$B%2(B "(I9^(B") (?$B$4(B ?$B%4(B "(I:^(B")
  42. (?$B$6(B ?$B%6(B "(I;^(B") (?$B$8(B ?$B%8(B "(I<^(B") (?$B$:(B ?$B%:(B "(I=^(B") (?$B$<(B ?$B%<(B "(I>^(B") (?$B$>(B ?$B%>(B "(I?^(B")
  43. (?$B$@(B ?$B%@(B "(I@^(B") (?$B$B(B ?$B%B(B "(IA^(B") (?$B$E(B ?$B%E(B "(IB^(B") (?$B$G(B ?$B%G(B "(IC^(B") (?$B$I(B ?$B%I(B "(ID^(B")
  44. (?$B$P(B ?$B%P(B "(IJ^(B") (?$B$S(B ?$B%S(B "(IK^(B") (?$B$V(B ?$B%V(B "(IL^(B") (?$B$Y(B ?$B%Y(B "(IM^(B") (?$B$\(B ?$B%\(B "(IN^(B")
  45. (?$B$Q(B ?$B%Q(B "(IJ_(B") (?$B$T(B ?$B%T(B "(IK_(B") (?$B$W(B ?$B%W(B "(IL_(B") (?$B$Z(B ?$B%Z(B "(IM_(B") (?$B$](B ?$B%](B "(IN_(B")
  46. (?$B$!(B ?$B%!(B ?(I'(B) (?$B$#(B ?$B%#(B ?(I((B) (?$B$%(B ?$B%%(B ?(I)(B) (?$B$'(B ?$B%'(B ?(I*(B) (?$B$)(B ?$B%)(B ?(I+(B)
  47. (?$B$C(B ?$B%C(B ?(I/(B)
  48. (?$B$c(B ?$B%c(B ?(I,(B) (?$B$e(B ?$B%e(B ?(I-(B) (?$B$g(B ?$B%g(B ?(I.(B)
  49. (?$B$n(B ?$B%n(B "(I\(B")
  50. ("$B$&!+(B" ?$B%t(B "(I3^(B") (nil ?$B%u(B "(I6(B") (nil ?$B%v(B "(I9(B"))
  51. "Japanese JISX0208 Kana character table.
  52. Each element is of the form (HIRAGANA KATAKANA HANKAKU-KATAKANA), where
  53. HIRAGANA and KATAKANA belong to `japanese-jisx0208',
  54. HANKAKU-KATAKANA belongs to `japanese-jisx0201-kana'.")
  55. ;; Put properties 'katakana, 'hiragana, and 'jix0201 to each Japanese
  56. ;; kana characters for conversion among them.
  57. (let ((l japanese-kana-table)
  58. slot hiragana katakana jisx0201)
  59. (while l
  60. (setq slot (car l)
  61. hiragana (car slot) katakana (nth 1 slot) jisx0201 (nth 2 slot)
  62. l (cdr l))
  63. (if hiragana
  64. (if (stringp hiragana)
  65. (if (> (length hiragana) 1)
  66. (let ((hira (aref hiragana 0)))
  67. (put-char-code-property
  68. hira 'kana-composition
  69. (cons (cons (aref hiragana 1) katakana)
  70. (get-char-code-property hira 'kana-composition)))))
  71. (put-char-code-property hiragana 'katakana katakana)
  72. (put-char-code-property hiragana 'jisx0201 jisx0201)))
  73. (when (integerp katakana)
  74. (put-char-code-property katakana 'hiragana hiragana)
  75. (put-char-code-property katakana 'jisx0201 jisx0201))
  76. (if jisx0201
  77. (if (stringp jisx0201)
  78. (if (> (length jisx0201) 1)
  79. (let ((kana (aref jisx0201 0)))
  80. (put-char-code-property
  81. kana 'kana-composition
  82. (cons (cons (aref jisx0201 1) katakana)
  83. (get-char-code-property kana 'kana-composition)))))
  84. (put-char-code-property jisx0201 'hiragana hiragana)
  85. (put-char-code-property jisx0201 'katakana katakana)
  86. (put-char-code-property jisx0201 'jisx0208 katakana)))))
  87. (defconst japanese-symbol-table
  88. '((?\$B!!(B ?\ ) (?$B!$(B ?, ?(I$(B) (?$B!%(B ?. ?(I!(B) (?$B!"(B ?, ?(I$(B) (?$B!#(B ?. ?(I!(B) (?$B!&(B nil ?(I%(B)
  89. (?$B!'(B ?:) (?$B!((B ?\;) (?$B!)(B ??) (?$B!*(B ?!) (?$B!+(B nil ?(I^(B) (?$B!,(B nil ?(I_(B)
  90. (?$B!-(B ?') (?$B!.(B ?`) (?$B!0(B ?^) (?$B!2(B ?_) (?$B!<(B ?- ?(I0(B) (?$B!=(B ?-) (?$B!>(B ?-)
  91. (?$B!?(B ?/) (?$B!@(B ?\\) (?$B!A(B ?~) (?$B!C(B ?|) (?$B!F(B ?`) (?$B!G(B ?') (?$B!H(B ?\") (?$B!I(B ?\")
  92. (?\$B!J(B ?\() (?\$B!K(B ?\)) (?\$B!N(B ?[) (?\$B!O(B ?]) (?\$B!P(B ?{) (?\$B!Q(B ?})
  93. (?$B!R(B ?<) (?$B!S(B ?>) (?\$B!V(B nil ?\(I"(B) (?\$B!W(B nil ?\(I#(B)
  94. (?$B!\(B ?+) (?$B!](B ?-) (?$B!a(B ?=) (?$B!c(B ?<) (?$B!d(B ?>)
  95. (?$B!l(B ?') (?$B!m(B ?\") (?$B!o(B ?\\) (?$B!p(B ?$) (?$B!s(B ?%) (?$B!t(B ?#) (?$B!u(B ?&) (?$B!v(B ?*)
  96. (?$B!w(B ?@)
  97. ;; cp932-2-byte
  98. (#x2015 ?-) (#xFF5E ?~) (#xFF0D ?-))
  99. "Japanese JISX0208 and CP932 symbol character table.
  100. Each element is of the form (SYMBOL ASCII HANKAKU), where SYMBOL
  101. belongs to `japanese-jisx0208' or `cp932', ASCII belongs to `ascii',
  102. and HANKAKU belongs to `japanese-jisx0201-kana'.")
  103. ;; Put properties 'jisx0208, 'jisx0201, and 'ascii to each Japanese
  104. ;; symbol and ASCII characters for conversion among them.
  105. (let ((l japanese-symbol-table)
  106. slot jisx0208 ascii jisx0201)
  107. (while l
  108. (setq slot (car l)
  109. jisx0208 (car slot) ascii (nth 1 slot) jisx0201 (nth 2 slot)
  110. l (cdr l))
  111. (if ascii
  112. (progn
  113. (put-char-code-property jisx0208 'ascii ascii)
  114. (if (encode-char jisx0208 'japanese-jisx0208)
  115. (put-char-code-property ascii 'jisx0208 jisx0208))))
  116. (if jisx0201
  117. (progn
  118. (put-char-code-property jisx0208 'jisx0201 jisx0201)
  119. (if (encode-char jisx0208 'japanese-jisx0208)
  120. (put-char-code-property jisx0201 'jisx0208 jisx0208))))))
  121. (defconst japanese-alpha-numeric-table
  122. '((?$B#0(B . ?0) (?$B#1(B . ?1) (?$B#2(B . ?2) (?$B#3(B . ?3) (?$B#4(B . ?4)
  123. (?$B#5(B . ?5) (?$B#6(B . ?6) (?$B#7(B . ?7) (?$B#8(B . ?8) (?$B#9(B . ?9)
  124. (?$B#A(B . ?A) (?$B#B(B . ?B) (?$B#C(B . ?C) (?$B#D(B . ?D) (?$B#E(B . ?E)
  125. (?$B#F(B . ?F) (?$B#G(B . ?G) (?$B#H(B . ?H) (?$B#I(B . ?I) (?$B#J(B . ?J)
  126. (?$B#K(B . ?K) (?$B#L(B . ?L) (?$B#M(B . ?M) (?$B#N(B . ?N) (?$B#O(B . ?O)
  127. (?$B#P(B . ?P) (?$B#Q(B . ?Q) (?$B#R(B . ?R) (?$B#S(B . ?S) (?$B#T(B . ?T)
  128. (?$B#U(B . ?U) (?$B#V(B . ?V) (?$B#W(B . ?W) (?$B#X(B . ?X) (?$B#Y(B . ?Y) (?$B#Z(B . ?Z)
  129. (?$B#a(B . ?a) (?$B#b(B . ?b) (?$B#c(B . ?c) (?$B#d(B . ?d) (?$B#e(B . ?e)
  130. (?$B#f(B . ?f) (?$B#g(B . ?g) (?$B#h(B . ?h) (?$B#i(B . ?i) (?$B#j(B . ?j)
  131. (?$B#k(B . ?k) (?$B#l(B . ?l) (?$B#m(B . ?m) (?$B#n(B . ?n) (?$B#o(B . ?o)
  132. (?$B#p(B . ?p) (?$B#q(B . ?q) (?$B#r(B . ?r) (?$B#s(B . ?s) (?$B#t(B . ?t)
  133. (?$B#u(B . ?u) (?$B#v(B . ?v) (?$B#w(B . ?w) (?$B#x(B . ?x) (?$B#y(B . ?y) (?$B#z(B . ?z))
  134. "Japanese JISX0208 alpha numeric character table.
  135. Each element is of the form (ALPHA-NUMERIC ASCII), where ALPHA-NUMERIC
  136. belongs to `japanese-jisx0208', ASCII belongs to `ascii'.")
  137. ;; Put properties 'jisx0208 and 'ascii to each Japanese alpha numeric
  138. ;; and ASCII characters for conversion between them.
  139. (let ((l japanese-alpha-numeric-table)
  140. slot jisx0208 ascii)
  141. (while l
  142. (setq slot (car l)
  143. jisx0208 (car slot) ascii (cdr slot)
  144. l (cdr l))
  145. (put-char-code-property jisx0208 'ascii ascii)
  146. (put-char-code-property ascii 'jisx0208 jisx0208)))
  147. ;; Convert string STR by FUNC and return a resulting string.
  148. (defun japanese-string-conversion (str func &rest args)
  149. (let ((buf (get-buffer-create " *Japanese work*")))
  150. (with-current-buffer buf
  151. (erase-buffer)
  152. (insert str)
  153. (apply func 1 (point) args)
  154. (buffer-string))))
  155. ;;;###autoload
  156. (defun japanese-katakana (obj &optional hankaku)
  157. "Convert argument to Katakana and return that.
  158. The argument may be a character or string. The result has the same type.
  159. The argument object is not altered--the value is a copy.
  160. Optional argument HANKAKU t means to convert to `hankaku' Katakana
  161. \(`japanese-jisx0201-kana'), in which case return value
  162. may be a string even if OBJ is a character if two Katakanas are
  163. necessary to represent OBJ."
  164. (if (stringp obj)
  165. (japanese-string-conversion obj 'japanese-katakana-region hankaku)
  166. (or (get-char-code-property obj (if hankaku 'jisx0201 'katakana))
  167. obj)))
  168. ;;;###autoload
  169. (defun japanese-hiragana (obj)
  170. "Convert argument to Hiragana and return that.
  171. The argument may be a character or string. The result has the same type.
  172. The argument object is not altered--the value is a copy."
  173. (if (stringp obj)
  174. (japanese-string-conversion obj 'japanese-hiragana-region)
  175. (or (get-char-code-property obj 'hiragana)
  176. obj)))
  177. ;;;###autoload
  178. (defun japanese-hankaku (obj &optional ascii-only)
  179. "Convert argument to `hankaku' and return that.
  180. The argument may be a character or string. The result has the same type.
  181. The argument object is not altered--the value is a copy.
  182. Optional argument ASCII-ONLY non-nil means to return only ASCII character."
  183. (if (stringp obj)
  184. (japanese-string-conversion obj 'japanese-hankaku-region ascii-only)
  185. (or (and (not ascii-only)
  186. (get-char-code-property obj 'jisx0201))
  187. (get-char-code-property obj 'ascii)
  188. obj)))
  189. ;;;###autoload
  190. (defun japanese-zenkaku (obj)
  191. "Convert argument to `zenkaku' and return that.
  192. The argument may be a character or string. The result has the same type.
  193. The argument object is not altered--the value is a copy."
  194. (if (stringp obj)
  195. (japanese-string-conversion obj 'japanese-zenkaku-region)
  196. (or (get-char-code-property obj 'jisx0208)
  197. obj)))
  198. (defun japanese-replace-region (from to string)
  199. "Replace the region specified by FROM and TO to STRING."
  200. (goto-char from)
  201. (insert string)
  202. (delete-char (- to from)))
  203. ;;;###autoload
  204. (defun japanese-katakana-region (from to &optional hankaku)
  205. "Convert Japanese `hiragana' chars in the region to `katakana' chars.
  206. Optional argument HANKAKU t means to convert to `hankaku katakana' character
  207. of which charset is `japanese-jisx0201-kana'."
  208. (interactive "r\nP")
  209. (save-restriction
  210. (narrow-to-region from to)
  211. (save-excursion
  212. (goto-char (point-min))
  213. (while (re-search-forward "\\cH\\|\\cK" nil t)
  214. (let* ((kana (preceding-char))
  215. (composition
  216. (and (not hankaku)
  217. (get-char-code-property kana 'kana-composition)))
  218. next slot)
  219. (if (and composition (setq slot (assq (following-char) composition)))
  220. (japanese-replace-region (match-beginning 0) (1+ (point))
  221. (cdr slot))
  222. (let ((kata (get-char-code-property
  223. kana (if hankaku 'jisx0201 'katakana))))
  224. (if kata
  225. (japanese-replace-region (match-beginning 0) (point)
  226. kata)))))))))
  227. ;;;###autoload
  228. (defun japanese-hiragana-region (from to)
  229. "Convert Japanese `katakana' chars in the region to `hiragana' chars."
  230. (interactive "r")
  231. (save-restriction
  232. (narrow-to-region from to)
  233. (save-excursion
  234. (goto-char (point-min))
  235. (while (re-search-forward "\\cK\\|\\ck" nil t)
  236. (let* ((kata (preceding-char))
  237. (composition (get-char-code-property kata 'kana-composition))
  238. next slot)
  239. (if (and composition (setq slot (assq (following-char) composition)))
  240. (japanese-replace-region (match-beginning 0) (1+ (point))
  241. (get-char-code-property
  242. (cdr slot) 'hiragana))
  243. (let ((hira (get-char-code-property kata 'hiragana)))
  244. (if hira
  245. (japanese-replace-region (match-beginning 0) (point)
  246. hira)))))))))
  247. ;;;###autoload
  248. (defun japanese-hankaku-region (from to &optional ascii-only)
  249. "Convert Japanese `zenkaku' chars in the region to `hankaku' chars.
  250. `Zenkaku' chars belong to `japanese-jisx0208'
  251. `Hankaku' chars belong to `ascii' or `japanese-jisx0201-kana'.
  252. Optional argument ASCII-ONLY non-nil means to convert only to ASCII char."
  253. (interactive "r\nP")
  254. (save-restriction
  255. (narrow-to-region from to)
  256. (save-excursion
  257. (goto-char (point-min))
  258. (while (re-search-forward "\\cj" nil t)
  259. (let* ((zenkaku (preceding-char))
  260. (hankaku (or (and (not ascii-only)
  261. (get-char-code-property zenkaku 'jisx0201))
  262. (get-char-code-property zenkaku 'ascii))))
  263. (if hankaku
  264. (japanese-replace-region (match-beginning 0) (match-end 0)
  265. hankaku)))))))
  266. ;;;###autoload
  267. (defun japanese-zenkaku-region (from to &optional katakana-only)
  268. "Convert hankaku' chars in the region to Japanese `zenkaku' chars.
  269. `Zenkaku' chars belong to `japanese-jisx0208'
  270. `Hankaku' chars belong to `ascii' or `japanese-jisx0201-kana'.
  271. Optional argument KATAKANA-ONLY non-nil means to convert only KATAKANA char."
  272. (interactive "r\nP")
  273. (save-restriction
  274. (narrow-to-region from to)
  275. (save-excursion
  276. (goto-char (point-min))
  277. (while (or (and katakana-only
  278. (re-search-forward "\\ck" nil t))
  279. (and (not katakana-only)
  280. (re-search-forward "\\ca\\|\\ck" nil t)))
  281. (let* ((hankaku (preceding-char))
  282. (composition (get-char-code-property hankaku 'kana-composition))
  283. next slot)
  284. (if (and composition (setq slot (assq (following-char) composition)))
  285. (japanese-replace-region (match-beginning 0) (1+ (point))
  286. (cdr slot))
  287. (let ((zenkaku (japanese-zenkaku hankaku)))
  288. (if zenkaku
  289. (japanese-replace-region (match-beginning 0) (match-end 0)
  290. zenkaku)))))))))
  291. ;;;###autoload
  292. (defun read-hiragana-string (prompt &optional initial-input)
  293. "Read a Hiragana string from the minibuffer, prompting with string PROMPT.
  294. If non-nil, second arg INITIAL-INPUT is a string to insert before reading."
  295. (read-multilingual-string prompt initial-input "japanese-hiragana"))
  296. ;;
  297. (provide 'japan-util)
  298. ;;; japan-util.el ends here