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