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