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.
29 ;; Hz/ZW encoding stuffs
31 ;; HZ is an encoding method for Chinese character set GB2312 used
32 ;; widely in Internet. It is very similar to 7-bit environment of
33 ;; ISO-2022. The difference is that HZ uses the sequence "~{" and
34 ;; "~}" for designating GB2312 and ASCII respectively, hence, it
35 ;; doesn't uses ESC (0x1B) code.
37 ;; ZW is another encoding method for Chinese character set GB2312. It
38 ;; encodes Chinese characters line by line by starting each line with
39 ;; the sequence "zW". It also uses only 7-bit as HZ.
41 ;; ISO-2022 escape sequence to designate GB2312.
42 (defvar iso2022-gb-designation
"\e$A")
43 ;; HZ escape sequence to designate GB2312.
44 (defvar hz-gb-designnation
"~{")
45 ;; ISO-2022 escape sequence to designate ASCII.
46 (defvar iso2022-ascii-designation
"\e(B")
47 ;; HZ escape sequence to designate ASCII.
48 (defvar hz-ascii-designnation
"~}")
49 ;; Regexp of ZW sequence to start GB2312.
50 (defvar zw-start-gb
"^zW")
51 ;; Regexp for start of GB2312 in an encoding mixture of HZ and ZW.
52 (defvar hz
/zw-start-gb
53 (concat hz-gb-designnation
"\\|" zw-start-gb
"\\|[^\0-\177]"))
55 (defvar decode-hz-line-continuation nil
56 "Flag to tell if we should care line continuation convention of Hz.")
58 (defconst hz-set-msb-table
59 (let ((str (make-string 127 0))
65 (aset str i
(+ i
128))
70 (defun decode-hz-region (beg end
)
71 "Decode HZ/ZW encoded text in the current region.
72 Return the length of resulting text."
77 (narrow-to-region beg end
)
79 ;; We, at first, convert HZ/ZW to `euc-china',
82 ;; "~\n" -> "\n", "~~" -> "~"
83 (goto-char (point-min))
84 (while (search-forward "~" nil t
)
85 (setq ch
(following-char))
86 (if (or (= ch ?
\n) (= ch ?~
)) (delete-char -
1)))
88 ;; "^zW...\n" -> Chinese GB2312
89 ;; "~{...~}" -> Chinese GB2312
90 (goto-char (point-min))
92 (while (re-search-forward hz
/zw-start-gb nil t
)
93 (setq pos
(match-beginning 0)
95 ;; Record the first position to start conversion.
96 (or beg
(setq beg pos
))
99 (if (>= ch
128) ; 8bit GB2312
104 (if (= ch ?z
) ; ZW -> euc-china
106 (translate-region (point) end hz-set-msb-table
)
108 (if (search-forward hz-ascii-designnation
109 (if decode-hz-line-continuation nil end
)
113 (translate-region pos
(point) hz-set-msb-table
))))
115 (decode-coding-region beg end
'euc-china
)))
116 (- (point-max) (point-min)))))
119 (defun decode-hz-buffer ()
120 "Decode HZ/ZW encoded text in the current buffer."
122 (decode-hz-region (point-min) (point-max)))
125 (defun encode-hz-region (beg end
)
126 "Encode the text in the current region to HZ.
127 Return the length of resulting text."
131 (narrow-to-region beg end
)
134 (goto-char (point-min))
135 (while (search-forward "~" nil t
) (insert ?~
))
137 ;; Chinese GB2312 -> "~{...~}"
138 (goto-char (point-min))
139 (if (re-search-forward "\\cc" nil t
)
141 (goto-char (setq pos
(match-beginning 0)))
142 (encode-coding-region pos
(point-max) 'iso-2022-7bit
)
144 (while (search-forward iso2022-gb-designation nil t
)
146 (insert hz-gb-designnation
))
148 (while (search-forward iso2022-ascii-designation nil t
)
150 (insert hz-ascii-designnation
))))
151 (- (point-max) (point-min)))))
154 (defun encode-hz-buffer ()
155 "Encode the text in the current buffer to HZ."
157 (encode-hz-region (point-min) (point-max)))
160 (provide 'china-util
)
162 ;;; china-util.el ends here