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 (set-language-environment "Chinese-GB"))
34 (defun setup-chinese-big5-environment ()
35 "Setup multilingual environment (MULE) for Chinese Big5 users."
37 (set-language-environment "Chinese-BIG5"))
40 (defun setup-chinese-cns-environment ()
41 "Setup multilingual environment (MULE) for Chinese CNS11643 family users."
43 (set-language-environment "Chinese-CNS"))
45 ;; Hz/ZW encoding stuffs
47 ;; HZ is an encoding method for Chinese character set GB2312 used
48 ;; widely in Internet. It is very similar to 7-bit environment of
49 ;; ISO-2022. The difference is that HZ uses the sequence "~{" and
50 ;; "~}" for designating GB2312 and ASCII respectively, hence, it
51 ;; doesn't uses ESC (0x1B) code.
53 ;; ZW is another encoding method for Chinese character set GB2312. It
54 ;; encodes Chinese characters line by line by starting each line with
55 ;; the sequence "zW". It also uses only 7-bit as HZ.
57 ;; ISO-2022 escape sequence to designate GB2312.
58 (defvar iso2022-gb-designation
"\e$A")
59 ;; HZ escape sequence to designate GB2312.
60 (defvar hz-gb-designnation
"~{")
61 ;; ISO-2022 escape sequence to designate ASCII.
62 (defvar iso2022-ascii-designation
"\e(B")
63 ;; HZ escape sequence to designate ASCII.
64 (defvar hz-ascii-designnation
"~}")
65 ;; Regexp of ZW sequence to start GB2312.
66 (defvar zw-start-gb
"^zW")
67 ;; Regexp for start of GB2312 in an encoding mixture of HZ and ZW.
68 (defvar hz
/zw-start-gb
69 (concat hz-gb-designnation
"\\|" zw-start-gb
"\\|[^\0-\177]"))
71 (defvar decode-hz-line-continuation nil
72 "Flag to tell if we should care line continuation convention of Hz.")
74 (defconst hz-set-msb-table
75 (let ((str (make-string 127 0))
81 (aset str i
(+ i
128))
86 (defun decode-hz-region (beg end
)
87 "Decode HZ/ZW encoded text in the current region.
88 Return the length of resulting text."
93 (narrow-to-region beg end
)
95 ;; We, at first, convert HZ/ZW to `euc-china',
98 ;; "~\n" -> "\n", "~~" -> "~"
99 (goto-char (point-min))
100 (while (search-forward "~" nil t
)
101 (setq ch
(following-char))
102 (if (or (= ch ?
\n) (= ch ?~
)) (delete-char -
1)))
104 ;; "^zW...\n" -> Chinese GB2312
105 ;; "~{...~}" -> Chinese GB2312
106 (goto-char (point-min))
108 (while (re-search-forward hz
/zw-start-gb nil t
)
109 (setq pos
(match-beginning 0)
111 ;; Record the first position to start conversion.
112 (or beg
(setq beg pos
))
115 (if (>= ch
128) ; 8bit GB2312
120 (if (= ch ?z
) ; ZW -> euc-china
122 (translate-region (point) end hz-set-msb-table
)
124 (if (search-forward hz-ascii-designnation
125 (if decode-hz-line-continuation nil end
)
129 (translate-region pos
(point) hz-set-msb-table
))))
131 (decode-coding-region beg end
'euc-china
)))
132 (- (point-max) (point-min)))))
135 (defun decode-hz-buffer ()
136 "Decode HZ/ZW encoded text in the current buffer."
138 (decode-hz-region (point-min) (point-max)))
141 (defun encode-hz-region (beg end
)
142 "Encode the text in the current region to HZ.
143 Return the length of resulting text."
147 (narrow-to-region beg end
)
150 (goto-char (point-min))
151 (while (search-forward "~" nil t
) (insert ?~
))
153 ;; Chinese GB2312 -> "~{...~}"
154 (goto-char (point-min))
155 (if (re-search-forward "\\cc" nil t
)
157 (goto-char (setq pos
(match-beginning 0)))
158 (encode-coding-region pos
(point-max) 'iso-2022-7bit
)
160 (while (search-forward iso2022-gb-designation nil t
)
162 (insert hz-gb-designnation
))
164 (while (search-forward iso2022-ascii-designation nil t
)
166 (insert hz-ascii-designnation
))))
167 (- (point-max) (point-min)))))
170 (defun encode-hz-buffer ()
171 "Encode the text in the current buffer to HZ."
173 (encode-hz-region (point-min) (point-max)))
176 (provide 'china-util
)
178 ;;; china-util.el ends here