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.
27 ;; Hz/ZW encoding stuffs
29 ;; HZ is an encoding method for Chinese character set GB2312 used
30 ;; widely in Internet. It is very similar to 7-bit environment of
31 ;; ISO-2022. The difference is that HZ uses the sequence "~{" and
32 ;; "~}" for designating GB2312 and ASCII respectively, hence, it
33 ;; doesn't uses ESC (0x1B) code.
35 ;; ZW is another encoding method for Chinese character set GB2312. It
36 ;; encodes Chinese characters line by line by starting each line with
37 ;; the sequence "zW". It also uses only 7-bit as HZ.
39 ;; ISO-2022 escape sequence to designate GB2312.
40 (defvar iso2022-gb-designation
"\e$A")
41 ;; HZ escape sequence to designate GB2312.
42 (defvar hz-gb-designnation
"~{")
43 ;; ISO-2022 escape sequence to designate ASCII.
44 (defvar iso2022-ascii-designation
"\e(B")
45 ;; HZ escape sequence to designate ASCII.
46 (defvar hz-ascii-designnation
"~}")
47 ;; Regexp of ZW sequence to start GB2312.
48 (defvar zw-start-gb
"^zW")
49 ;; Regexp for start of GB2312 in an encoding mixture of HZ and ZW.
50 (defvar hz
/zw-start-gb
51 (concat hz-gb-designnation
"\\|" zw-start-gb
"\\|[^\0-\177]"))
53 (defvar decode-hz-line-continuation nil
54 "Flag to tell if we should care line continuation convention of Hz.")
56 (defconst hz-set-msb-table
57 (let ((str (make-string 127 0))
63 (aset str i
(+ i
128))
68 (defun decode-hz-region (beg end
)
69 "Decode HZ/ZW encoded text in the current region.
70 Return the length of resulting text."
75 (narrow-to-region beg end
)
77 ;; We, at first, convert HZ/ZW to `euc-china',
80 ;; "~\n" -> "\n", "~~" -> "~"
81 (goto-char (point-min))
82 (while (search-forward "~" nil t
)
83 (setq ch
(following-char))
84 (if (or (= ch ?
\n) (= ch ?~
)) (delete-char -
1)))
86 ;; "^zW...\n" -> Chinese GB2312
87 ;; "~{...~}" -> Chinese GB2312
88 (goto-char (point-min))
90 (while (re-search-forward hz
/zw-start-gb nil t
)
91 (setq pos
(match-beginning 0)
93 ;; Record the first position to start conversion.
94 (or beg
(setq beg pos
))
97 (if (>= ch
128) ; 8bit GB2312
102 (if (= ch ?z
) ; ZW -> euc-china
104 (translate-region (point) end hz-set-msb-table
)
106 (if (search-forward hz-ascii-designnation
107 (if decode-hz-line-continuation nil end
)
111 (translate-region pos
(point) hz-set-msb-table
))))
113 (decode-coding-region beg end
'euc-china
)))
114 (- (point-max) (point-min)))))
117 (defun decode-hz-buffer ()
118 "Decode HZ/ZW encoded text in the current buffer."
120 (decode-hz-region (point-min) (point-max)))
123 (defun encode-hz-region (beg end
)
124 "Encode the text in the current region to HZ.
125 Return the length of resulting text."
129 (narrow-to-region beg end
)
132 (goto-char (point-min))
133 (while (search-forward "~" nil t
) (insert ?~
))
135 ;; Chinese GB2312 -> "~{...~}"
136 (goto-char (point-min))
137 (if (re-search-forward "\\cc" nil t
)
139 (goto-char (setq pos
(match-beginning 0)))
140 (encode-coding-region pos
(point-max) 'iso-2022-7bit
)
142 (while (search-forward iso2022-gb-designation nil t
)
144 (insert hz-gb-designnation
))
146 (while (search-forward iso2022-ascii-designation nil t
)
148 (insert hz-ascii-designnation
))))
149 (- (point-max) (point-min)))))
152 (defun encode-hz-buffer ()
153 "Encode the text in the current buffer to HZ."
155 (encode-hz-region (point-min) (point-max)))
158 (provide 'china-util
)
160 ;;; china-util.el ends here