1 ;;; china-util.el --- utilities for Chinese -*- coding: utf-8 -*-
3 ;; Copyright (C) 1995, 2001-2013 Free Software Foundation, Inc.
4 ;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
5 ;; 2005, 2006, 2007, 2008, 2009, 2010, 2011
6 ;; National Institute of Advanced Industrial Science and Technology (AIST)
7 ;; Registration Number H14PRO021
9 ;; National Institute of Advanced Industrial Science and Technology (AIST)
10 ;; Registration Number H13PRO009
12 ;; Keywords: mule, multilingual, Chinese
14 ;; This file is part of GNU Emacs.
16 ;; GNU Emacs is free software: you can redistribute it and/or modify
17 ;; it under the terms of the GNU General Public License as published by
18 ;; the Free Software Foundation, either version 3 of the License, or
19 ;; (at your option) any later version.
21 ;; GNU Emacs is distributed in the hope that it will be useful,
22 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
23 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 ;; GNU General Public License for more details.
26 ;; You should have received a copy of the GNU General Public License
27 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
33 ;; Hz/ZW/EUC-TW encoding stuff
35 ;; HZ is an encoding method for Chinese character set GB2312 used
36 ;; widely in Internet. It is very similar to 7-bit environment of
37 ;; ISO-2022. The difference is that HZ uses the sequence "~{" and
38 ;; "~}" for designating GB2312 and ASCII respectively, hence, it
39 ;; doesn't uses ESC (0x1B) code.
41 ;; ZW is another encoding method for Chinese character set GB2312. It
42 ;; encodes Chinese characters line by line by starting each line with
43 ;; the sequence "zW". It also uses only 7-bit as HZ.
45 ;; EUC-TW is similar to EUC-KS or EUC-JP. Its main character set is
46 ;; plane 1 of CNS 11643; characters of planes 2 to 7 are accessed with
47 ;; a single shift escape followed by three bytes: the first gives the
48 ;; plane, the second and third the character code. Note that characters
49 ;; of plane 1 are (redundantly) accessible with a single shift escape
52 ;; ISO-2022 escape sequence to designate GB2312.
53 (defvar iso2022-gb-designation
"\e$A")
54 ;; HZ escape sequence to designate GB2312.
55 (defvar hz-gb-designnation
"~{")
56 ;; ISO-2022 escape sequence to designate ASCII.
57 (defvar iso2022-ascii-designation
"\e(B")
58 ;; HZ escape sequence to designate ASCII.
59 (defvar hz-ascii-designnation
"~}")
60 ;; Regexp of ZW sequence to start GB2312.
61 (defvar zw-start-gb
"^zW")
62 ;; Regexp for start of GB2312 in an encoding mixture of HZ and ZW.
63 (defvar hz
/zw-start-gb
64 (concat hz-gb-designnation
"\\|" zw-start-gb
"\\|[^\0-\177]"))
66 (defvar decode-hz-line-continuation nil
67 "Flag to tell if we should care line continuation convention of Hz.")
69 (defconst hz-set-msb-table
77 (push (decode-char 'eight-bit
(+ i
128)) chars
)
79 (apply 'string
(nreverse chars
)))))
82 (defun decode-hz-region (beg end
)
83 "Decode HZ/ZW encoded text in the current region.
84 Return the length of resulting text."
89 (narrow-to-region beg end
)
91 ;; We, at first, convert HZ/ZW to `euc-china',
94 ;; "~\n" -> "\n", "~~" -> "~"
95 (goto-char (point-min))
96 (while (search-forward "~" nil t
)
97 (setq ch
(following-char))
98 (if (or (= ch ?
\n) (= ch ?~
)) (delete-char -
1)))
100 ;; "^zW...\n" -> Chinese GB2312
101 ;; "~{...~}" -> Chinese GB2312
102 (goto-char (point-min))
104 (while (re-search-forward hz
/zw-start-gb nil t
)
105 (setq pos
(match-beginning 0)
107 ;; Record the first position to start conversion.
108 (or beg
(setq beg pos
))
111 (if (>= ch
128) ; 8bit GB2312
116 (if (= ch ?z
) ; ZW -> euc-china
118 (translate-region (point) end hz-set-msb-table
)
120 (if (search-forward hz-ascii-designnation
121 (if decode-hz-line-continuation nil end
)
125 (translate-region pos
(point) hz-set-msb-table
))))
127 (decode-coding-region beg end
'euc-china
)))
128 (- (point-max) (point-min)))))
131 (defun decode-hz-buffer ()
132 "Decode HZ/ZW encoded text in the current buffer."
134 (decode-hz-region (point-min) (point-max)))
137 (defun encode-hz-region (beg end
)
138 "Encode the text in the current region to HZ.
139 Return the length of resulting text."
143 (narrow-to-region beg end
)
146 (goto-char (point-min))
147 (while (search-forward "~" nil t
) (insert ?~
))
149 ;; Chinese GB2312 -> "~{...~}"
150 (goto-char (point-min))
151 (if (re-search-forward "\\cc" nil t
)
153 (goto-char (setq pos
(match-beginning 0)))
154 (encode-coding-region pos
(point-max) 'iso-2022-7bit
)
156 (while (search-forward iso2022-gb-designation nil t
)
158 (insert hz-gb-designnation
))
160 (while (search-forward iso2022-ascii-designation nil t
)
162 (insert hz-ascii-designnation
))))
163 (- (point-max) (point-min)))))
166 (defun encode-hz-buffer ()
167 "Encode the text in the current buffer to HZ."
169 (encode-hz-region (point-min) (point-max)))
172 (defun post-read-decode-hz (len)
174 (buffer-modified-p (buffer-modified-p))
175 last-coding-system-used
)
177 (decode-hz-region pos
(+ pos len
))
178 (set-buffer-modified-p buffer-modified-p
))))
181 (defun pre-write-encode-hz (from to
)
182 (let ((buf (current-buffer)))
183 (set-buffer (generate-new-buffer " *temp*"))
186 (insert-buffer-substring buf from to
))
187 (let (last-coding-system-used)
188 (encode-hz-region 1 (point-max)))
191 (provide 'china-util
)
193 ;;; china-util.el ends here