1 ;;; china-util.el --- utilities for Chinese
3 ;; Copyright (C) 1995 Electrotechnical Laboratory, JAPAN.
4 ;; Licensed to the Free Software Foundation.
6 ;; Keywords: mule, multilingual, Chinese
8 ;; This file is part of GNU Emacs.
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)
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.
20 ;; You should have received a copy of the GNU General Public License
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.
28 (defun setup-chinese-gb-environment ()
29 "Setup multilingual environment (MULE) for Chinese GB2312 users."
31 (setup-english-environment)
33 (set-default-coding-systems 'chinese-iso-8bit
)
34 (setq coding-category-iso-8-2
'chinese-iso-8bit
)
35 (setq coding-category-iso-7-else
'chinese-iso-7bit
)
36 (setq coding-category-big5
'chinese-big5
)
39 '(coding-category-iso-7
40 coding-category-iso-7-else
41 coding-category-iso-8-2
43 coding-category-iso-8-1
44 coding-category-emacs-mule
45 coding-category-iso-8-else
))
47 (setq-default buffer-file-coding-system
'chinese-iso-8bit
)
48 (setq default-terminal-coding-system
'chinese-iso-8bit
)
49 (setq default-keyboard-coding-system
'chinese-iso-8bit
)
51 (setq default-input-method
"chinese-py-punct"))
54 (defun setup-chinese-big5-environment ()
55 "Setup multilingual environment (MULE) for Chinese Big5 users."
57 (setup-english-environment)
59 (set-default-coding-systems 'chinese-big5
)
60 (setq coding-category-iso-8-2
'chinese-big5
)
61 (setq coding-category-iso-7-else
'chinese-iso-7bit
)
62 (setq coding-category-big5
'chinese-big5
)
65 '(coding-category-iso-7
66 coding-category-iso-7-else
68 coding-category-iso-8-2
69 coding-category-emacs-mule
70 coding-category-iso-8-else
))
72 (setq-default buffer-file-coding-system
'chinese-big5
)
73 (setq default-terminal-coding-system
'chinese-big5
)
74 (setq default-keyboard-coding-system
'chinese-big5
)
76 (setq default-input-method
"chinese-py-punct-b5"))
79 (defun setup-chinese-cns-environment ()
80 "Setup multilingual environment (MULE) for Chinese CNS11643 family users."
82 (setup-english-environment)
84 (setq coding-category-iso-7-else
'chinese-iso-7bit
)
85 (setq coding-category-big5
'chinese-big5
)
86 (setq coding-category-iso-8-2
'chinese-big5
)
87 (set-default-coding-systems 'chinese-iso-7bit
)
90 '(coding-category-iso-7
91 coding-category-iso-7-else
92 coding-category-iso-8-2
94 coding-category-iso-7-else
))
96 (setq-default buffer-file-coding-system
'chinese-iso-7bit
)
97 (setq default-terminal-coding-system
'chinese-iso-7bit
)
98 (setq default-keyboard-coding-system
'chinese-iso-7bit
)
100 (setq default-input-method
"chinese-quick-cns"))
102 ;; Hz/ZW encoding stuffs
104 ;; HZ is an encoding method for Chinese character set GB2312 used
105 ;; widely in Internet. It is very similar to 7-bit environment of
106 ;; ISO-2022. The difference is that HZ uses the sequence "~{" and
107 ;; "~}" for designating GB2312 and ASCII respectively, hence, it
108 ;; doesn't uses ESC (0x1B) code.
110 ;; ZW is another encoding method for Chinese character set GB2312. It
111 ;; encodes Chinese characters line by line by starting each line with
112 ;; the sequence "zW". It also uses only 7-bit as HZ.
114 ;; ISO-2022 escape sequence to designate GB2312.
115 (defvar iso2022-gb-designation
"\e$A")
116 ;; HZ escape sequence to designate GB2312.
117 (defvar hz-gb-designnation
"~{")
118 ;; ISO-2022 escape sequence to designate ASCII.
119 (defvar iso2022-ascii-designation
"\e(B")
120 ;; HZ escape sequence to designate ASCII.
121 (defvar hz-ascii-designnation
"~}")
122 ;; Regexp of ZW sequence to start GB2312.
123 (defvar zw-start-gb
"^zW")
124 ;; Regexp for start of GB2312 in an encoding mixture of HZ and ZW.
125 (defvar hz
/zw-start-gb
(concat hz-gb-designnation
"\\|" zw-start-gb
))
127 (defvar decode-hz-line-continuation nil
128 "Flag to tell if we should care line continuation convention of Hz.")
131 (defun decode-hz-region (beg end
)
132 "Decode HZ/ZW encoded text in the current region.
133 Return the length of resulting text."
137 (narrow-to-region beg end
)
139 ;; We, at first, convert HZ/ZW to `iso-2022-7bit',
143 (goto-char (point-min))
144 (while (search-forward "~" nil t
)
145 (if (= (following-char) ?
\n) (delete-char -
1))
146 (if (not (eobp)) (forward-char 1)))
148 ;; "^zW...\n" -> Chinese GB2312
149 ;; "~{...~}" -> Chinese GB2312
150 (goto-char (point-min))
151 (let ((chinese-found nil
))
152 (while (re-search-forward hz
/zw-start-gb nil t
)
153 (if (= (char-after (match-beginning 0)) ?z
)
154 ;; ZW -> iso-2022-7bit
157 (insert iso2022-gb-designation
)
159 (insert iso2022-ascii-designation
))
160 ;; HZ -> iso-2022-7bit
162 (insert iso2022-gb-designation
)
163 (let ((pos (save-excursion (end-of-line) (point))))
164 (if (search-forward hz-ascii-designnation pos t
)
165 (replace-match iso2022-ascii-designation
)
166 (if (not decode-hz-line-continuation
)
167 (insert iso2022-ascii-designation
)))))
168 (setq chinese-found t
))
169 (if (or chinese-found
170 (let ((enable-multibyte-characters nil
))
171 ;; Here we check if the text contains EUC (China) codes.
172 ;; If any, we had better decode them also.
173 (goto-char (point-min))
174 (re-search-forward "[\240-\377]" nil t
)))
175 (decode-coding-region (point-min) (point-max) 'euc-china
)))
178 (goto-char (point-min))
179 (while (search-forward "~~" nil t
) (delete-char -
1))
180 (- (point-max) (point-min)))))
183 (defun decode-hz-buffer ()
184 "Decode HZ/ZW encoded text in the current buffer."
186 (decode-hz-region (point-min) (point-max)))
189 (defun encode-hz-region (beg end
)
190 "Encode the text in the current region to HZ.
191 Return the length of resulting text."
195 (narrow-to-region beg end
)
198 (goto-char (point-min))
199 (while (search-forward "~" nil t
) (insert ?~
))
201 ;; Chinese GB2312 -> "~{...~}"
202 (goto-char (point-min))
203 (if (re-search-forward "\\cc" nil t
)
204 (let ((enable-multibyte-characters nil
)
206 (goto-char (setq pos
(match-beginning 0)))
207 (encode-coding-region pos
(point-max) 'iso-2022-7bit
)
209 (while (search-forward iso2022-gb-designation nil t
)
211 (insert hz-gb-designnation
))
213 (while (search-forward iso2022-ascii-designation nil t
)
215 (insert hz-ascii-designnation
))))
216 (- (point-max) (point-min)))))
219 (defun encode-hz-buffer ()
220 "Encode the text in the current buffer to HZ."
222 (encode-hz-region (point-min) (point-max)))
225 (provide 'china-util
)
227 ;;; china-util.el ends here