]> code.delx.au - gnu-emacs/blob - lisp/language/china-util.el
(standard-display-cyrillic-translit):
[gnu-emacs] / lisp / language / china-util.el
1 ;;; china-util.el --- utilities for Chinese
2
3 ;; Copyright (C) 1995 Electrotechnical Laboratory, JAPAN.
4 ;; Licensed to the Free Software Foundation.
5
6 ;; Keywords: mule, multilingual, Chinese
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
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.
24
25 ;;; Code:
26
27 ;;;###autoload
28 (defun setup-chinese-gb-environment ()
29 "Setup multilingual environment (MULE) for Chinese GB2312 users."
30 (interactive)
31 (setup-english-environment)
32
33 (setq primary-language "Chinese-GB")
34
35 (set-default-coding-systems 'chinese-iso-8bit)
36 (setq coding-category-iso-8-2 'chinese-iso-8bit)
37 (setq coding-category-iso-7-else 'chinese-iso-7bit)
38 (setq coding-category-big5 'chinese-big5)
39
40 (set-coding-priority
41 '(coding-category-iso-7
42 coding-category-iso-7-else
43 coding-category-iso-8-2
44 coding-category-big5
45 coding-category-iso-8-1
46 coding-category-emacs-mule
47 coding-category-iso-8-else))
48
49 (setq-default buffer-file-coding-system 'chinese-iso-8bit)
50 (setq default-terminal-coding-system 'chinese-iso-8bit)
51 (setq default-keyboard-coding-system 'chinese-iso-8bit)
52 (setq sendmail-coding-system nil
53 rmail-file-coding-system 'chinese-iso-7bit)
54
55 (setq default-input-method "chinese-py-punct")
56 (setq-default default-input-method "chinese-py-punct"))
57
58 ;;;###autoload
59 (defun setup-chinese-big5-environment ()
60 "Setup multilingual environment (MULE) for Chinese Big5 users."
61 (interactive)
62 (setup-english-environment)
63
64 (setq primary-language "Chinese-BIG5")
65
66 (set-default-coding-systems 'chinese-big5)
67 (setq coding-category-iso-8-2 'chinese-big5)
68 (setq coding-category-iso-7-else 'chinese-iso-7bit)
69 (setq coding-category-big5 'chinese-big5)
70
71 (set-coding-priority
72 '(coding-category-iso-7
73 coding-category-iso-7-else
74 coding-category-big5
75 coding-category-iso-8-2
76 coding-category-emacs-mule
77 coding-category-iso-8-else))
78
79 (setq-default buffer-file-coding-system 'chinese-big5)
80 (setq default-terminal-coding-system 'chinese-big5)
81 (setq default-keyboard-coding-system 'chinese-big5)
82 (setq sendmail-coding-system nil
83 rmail-file-coding-system 'chienese-iso-7bit)
84
85 (setq default-input-method "chinese-py-punct-b5")
86 (setq-default default-input-method "chinese-py-punct-b5"))
87
88 ;;;###autoload
89 (defun setup-chinese-cns-environment ()
90 "Setup multilingual environment (MULE) for Chinese CNS11643 family users."
91 (interactive)
92 (setup-english-environment)
93
94 (setq primary-language "Chinese-CNS")
95
96 (setq coding-category-iso-7-else 'chinese-iso-7bit)
97 (setq coding-category-big5 'chinese-big5)
98 (setq coding-category-iso-8-2 'chinese-big5)
99 (set-default-coding-systems 'chinese-iso-7bit)
100
101 (set-coding-priority
102 '(coding-category-iso-7
103 coding-category-iso-7-else
104 coding-category-iso-8-2
105 coding-category-big5
106 coding-category-iso-7-else))
107
108 (setq-default buffer-file-coding-system 'chinese-iso-7bit)
109 (setq default-terminal-coding-system 'chinese-iso-7bit)
110 (setq default-keyboard-coding-system 'chinese-iso-7bit)
111 (setq sendmail-coding-system nil
112 rmail-file-coding-system 'chinese-iso-7bit)
113
114 (setq default-input-method "chinese-quick-cns")
115 (setq-default default-input-method "chinese-quick-cns"))
116
117 ;; Hz/ZW encoding stuffs
118
119 ;; HZ is an encoding method for Chinese character set GB2312 used
120 ;; widely in Internet. It is very similar to 7-bit environment of
121 ;; ISO-2022. The difference is that HZ uses the sequence "~{" and
122 ;; "~}" for designating GB2312 and ASCII respectively, hence, it
123 ;; doesn't uses ESC (0x1B) code.
124
125 ;; ZW is another encoding method for Chinese character set GB2312. It
126 ;; encodes Chinese characters line by line by starting each line with
127 ;; the sequence "zW". It also uses only 7-bit as HZ.
128
129 ;; ISO-2022 escape sequence to designate GB2312.
130 (defvar iso2022-gb-designation "\e$A")
131 ;; HZ escape sequence to designate GB2312.
132 (defvar hz-gb-designnation "~{")
133 ;; ISO-2022 escape sequence to designate ASCII.
134 (defvar iso2022-ascii-designation "\e(B")
135 ;; HZ escape sequence to designate ASCII.
136 (defvar hz-ascii-designnation "~}")
137 ;; Regexp of ZW sequence to start GB2312.
138 (defvar zw-start-gb "^zW")
139 ;; Regexp for start of GB2312 in an encoding mixture of HZ and ZW.
140 (defvar hz/zw-start-gb (concat hz-gb-designnation "\\|" zw-start-gb))
141
142 (defvar decode-hz-line-continuation nil
143 "Flag to tell if we should care line continuation convention of Hz.")
144
145 ;;;###autoload
146 (defun decode-hz-region (beg end)
147 "Decode HZ/ZW encoded text in the current region.
148 Return the length of resulting text."
149 (interactive "r")
150 (save-excursion
151 (save-restriction
152 (narrow-to-region beg end)
153
154 ;; We, at first, convert HZ/ZW to `iso-2022-7bit',
155 ;; then decode it.
156
157 ;; "~\n" -> "\n"
158 (goto-char (point-min))
159 (while (search-forward "~" nil t)
160 (if (= (following-char) ?\n) (delete-char -1))
161 (if (not (eobp)) (forward-char 1)))
162
163 ;; "^zW...\n" -> Chinese GB2312
164 ;; "~{...~}" -> Chinese GB2312
165 (goto-char (point-min))
166 (let ((chinese-found nil))
167 (while (re-search-forward hz/zw-start-gb nil t)
168 (if (= (char-after (match-beginning 0)) ?z)
169 ;; ZW -> iso-2022-7bit
170 (progn
171 (delete-char -2)
172 (insert iso2022-gb-designation)
173 (end-of-line)
174 (insert iso2022-ascii-designation))
175 ;; HZ -> iso-2022-7bit
176 (delete-char -2)
177 (insert iso2022-gb-designation)
178 (let ((pos (save-excursion (end-of-line) (point))))
179 (if (search-forward hz-ascii-designnation pos t)
180 (replace-match iso2022-ascii-designation)
181 (if (not decode-hz-line-continuation)
182 (insert iso2022-ascii-designation)))))
183 (setq chinese-found t))
184 (if (or chinese-found
185 (let ((enable-multibyte-characters nil))
186 ;; Here we check if the text contains EUC (China) codes.
187 ;; If any, we had better decode them also.
188 (goto-char (point-min))
189 (re-search-forward "[\240-\377]" nil t)))
190 (decode-coding-region (point-min) (point-max) 'euc-china)))
191
192 ;; "~~" -> "~"
193 (goto-char (point-min))
194 (while (search-forward "~~" nil t) (delete-char -1))
195 (- (point-max) (point-min)))))
196
197 ;;;###autoload
198 (defun decode-hz-buffer ()
199 "Decode HZ/ZW encoded text in the current buffer."
200 (interactive)
201 (decode-hz-region (point-min) (point-max)))
202
203 ;;;###autoload
204 (defun encode-hz-region (beg end)
205 "Encode the text in the current region to HZ.
206 Return the length of resulting text."
207 (interactive "r")
208 (save-excursion
209 (save-restriction
210 (narrow-to-region beg end)
211
212 ;; "~" -> "~~"
213 (goto-char (point-min))
214 (while (search-forward "~" nil t) (insert ?~))
215
216 ;; Chinese GB2312 -> "~{...~}"
217 (goto-char (point-min))
218 (if (re-search-forward "\\cc" nil t)
219 (let ((enable-multibyte-characters nil)
220 pos)
221 (goto-char (setq pos (match-beginning 0)))
222 (encode-coding-region pos (point-max) 'iso-2022-7bit)
223 (goto-char pos)
224 (while (search-forward iso2022-gb-designation nil t)
225 (delete-char -3)
226 (insert hz-gb-designnation))
227 (goto-char pos)
228 (while (search-forward iso2022-ascii-designation nil t)
229 (delete-char -3)
230 (insert hz-ascii-designnation))))
231 (- (point-max) (point-min)))))
232
233 ;;;###autoload
234 (defun encode-hz-buffer ()
235 "Encode the text in the current buffer to HZ."
236 (interactive)
237 (encode-hz-region (point-min) (point-max)))
238
239 ;;
240 (provide 'china-util)
241
242 ;;; china-util.el ends here