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