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