]> code.delx.au - gnu-emacs/blob - lisp/language/china-util.el
In setup-LANGUAGE-environment functions,
[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 (set-language-environment-coding-systems "Chinese-GB")
33 (setq default-input-method "chinese-py-punct"))
34
35 ;;;###autoload
36 (defun setup-chinese-big5-environment ()
37 "Setup multilingual environment (MULE) for Chinese Big5 users."
38 (interactive)
39 (setup-english-environment)
40 (set-language-environment-coding-systems "Chinese-BIG5")
41 (setq default-input-method "chinese-py-punct-b5"))
42
43 ;;;###autoload
44 (defun setup-chinese-cns-environment ()
45 "Setup multilingual environment (MULE) for Chinese CNS11643 family users."
46 (interactive)
47 (setup-english-environment)
48 (set-language-environment-coding-systems "Chinese-CNS")
49 (setq default-input-method "chinese-quick-cns"))
50
51 ;; Hz/ZW encoding stuffs
52
53 ;; HZ is an encoding method for Chinese character set GB2312 used
54 ;; widely in Internet. It is very similar to 7-bit environment of
55 ;; ISO-2022. The difference is that HZ uses the sequence "~{" and
56 ;; "~}" for designating GB2312 and ASCII respectively, hence, it
57 ;; doesn't uses ESC (0x1B) code.
58
59 ;; ZW is another encoding method for Chinese character set GB2312. It
60 ;; encodes Chinese characters line by line by starting each line with
61 ;; the sequence "zW". It also uses only 7-bit as HZ.
62
63 ;; ISO-2022 escape sequence to designate GB2312.
64 (defvar iso2022-gb-designation "\e$A")
65 ;; HZ escape sequence to designate GB2312.
66 (defvar hz-gb-designnation "~{")
67 ;; ISO-2022 escape sequence to designate ASCII.
68 (defvar iso2022-ascii-designation "\e(B")
69 ;; HZ escape sequence to designate ASCII.
70 (defvar hz-ascii-designnation "~}")
71 ;; Regexp of ZW sequence to start GB2312.
72 (defvar zw-start-gb "^zW")
73 ;; Regexp for start of GB2312 in an encoding mixture of HZ and ZW.
74 (defvar hz/zw-start-gb
75 (concat hz-gb-designnation "\\|" zw-start-gb "\\|[^\0-\177]"))
76
77 (defvar decode-hz-line-continuation nil
78 "Flag to tell if we should care line continuation convention of Hz.")
79
80 (defconst hz-set-msb-table
81 (let ((str (make-string 127 0))
82 (i 0))
83 (while (< i 33)
84 (aset str i i)
85 (setq i (1+ i)))
86 (while (< i 127)
87 (aset str i (+ i 128))
88 (setq i (1+ i)))
89 str))
90
91 ;;;###autoload
92 (defun decode-hz-region (beg end)
93 "Decode HZ/ZW encoded text in the current region.
94 Return the length of resulting text."
95 (interactive "r")
96 (save-excursion
97 (save-restriction
98 (let (pos ch)
99 (narrow-to-region beg end)
100
101 ;; We, at first, convert HZ/ZW to `euc-china',
102 ;; then decode it.
103
104 ;; "~\n" -> "\n", "~~" -> "~"
105 (goto-char (point-min))
106 (while (search-forward "~" nil t)
107 (setq ch (following-char))
108 (if (or (= ch ?\n) (= ch ?~)) (delete-char -1)))
109
110 ;; "^zW...\n" -> Chinese GB2312
111 ;; "~{...~}" -> Chinese GB2312
112 (goto-char (point-min))
113 (setq beg nil)
114 (while (re-search-forward hz/zw-start-gb nil t)
115 (setq pos (match-beginning 0)
116 ch (char-after pos))
117 ;; Record the first position to start conversion.
118 (or beg (setq beg pos))
119 (end-of-line)
120 (setq end (point))
121 (if (>= ch 128) ; 8bit GB2312
122 nil
123 (goto-char pos)
124 (delete-char 2)
125 (setq end (- end 2))
126 (if (= ch ?z) ; ZW -> euc-china
127 (progn
128 (translate-region (point) end hz-set-msb-table)
129 (goto-char end))
130 (if (search-forward hz-ascii-designnation
131 (if decode-hz-line-continuation nil end)
132 t)
133 (delete-char -2))
134 (setq end (point))
135 (translate-region pos (point) hz-set-msb-table))))
136 (if beg
137 (decode-coding-region beg end 'euc-china)))
138 (- (point-max) (point-min)))))
139
140 ;;;###autoload
141 (defun decode-hz-buffer ()
142 "Decode HZ/ZW encoded text in the current buffer."
143 (interactive)
144 (decode-hz-region (point-min) (point-max)))
145
146 ;;;###autoload
147 (defun encode-hz-region (beg end)
148 "Encode the text in the current region to HZ.
149 Return the length of resulting text."
150 (interactive "r")
151 (save-excursion
152 (save-restriction
153 (narrow-to-region beg end)
154
155 ;; "~" -> "~~"
156 (goto-char (point-min))
157 (while (search-forward "~" nil t) (insert ?~))
158
159 ;; Chinese GB2312 -> "~{...~}"
160 (goto-char (point-min))
161 (if (re-search-forward "\\cc" nil t)
162 (let ((enable-multibyte-characters nil)
163 pos)
164 (goto-char (setq pos (match-beginning 0)))
165 (encode-coding-region pos (point-max) 'iso-2022-7bit)
166 (goto-char pos)
167 (while (search-forward iso2022-gb-designation nil t)
168 (delete-char -3)
169 (insert hz-gb-designnation))
170 (goto-char pos)
171 (while (search-forward iso2022-ascii-designation nil t)
172 (delete-char -3)
173 (insert hz-ascii-designnation))))
174 (- (point-max) (point-min)))))
175
176 ;;;###autoload
177 (defun encode-hz-buffer ()
178 "Encode the text in the current buffer to HZ."
179 (interactive)
180 (encode-hz-region (point-min) (point-max)))
181
182 ;;
183 (provide 'china-util)
184
185 ;;; china-util.el ends here