1 ;;; china-util.el --- utilities for Chinese -*- coding: iso-2022-7bit -*-
3 ;; Copyright (C) 1995 Electrotechnical Laboratory, JAPAN.
4 ;; Licensed to the Free Software Foundation.
5 ;; Copyright (C) 1995, 2001 Free Software Foundation, Inc.
7 ;; Keywords: mule, multilingual, Chinese
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
30 ;; Hz/ZW/EUC-TW encoding stuff
32 ;; HZ is an encoding method for Chinese character set GB2312 used
33 ;; widely in Internet. It is very similar to 7-bit environment of
34 ;; ISO-2022. The difference is that HZ uses the sequence "~{" and
35 ;; "~}" for designating GB2312 and ASCII respectively, hence, it
36 ;; doesn't uses ESC (0x1B) code.
38 ;; ZW is another encoding method for Chinese character set GB2312. It
39 ;; encodes Chinese characters line by line by starting each line with
40 ;; the sequence "zW". It also uses only 7-bit as HZ.
42 ;; EUC-TW is similar to EUC-KS or EUC-JP. Its main character set is
43 ;; plane 1 of CNS 11643; characters of planes 2 to 7 are accessed with
44 ;; a single shift escape followed by three bytes: the first gives the
45 ;; plane, the second and third the character code. Note that characters
46 ;; of plane 1 are (redundantly) accessible with a single shift escape
49 ;; ISO-2022 escape sequence to designate GB2312.
50 (defvar iso2022-gb-designation "\e$A")
51 ;; HZ escape sequence to designate GB2312.
52 (defvar hz-gb-designnation "~{")
53 ;; ISO-2022 escape sequence to designate ASCII.
54 (defvar iso2022-ascii-designation "\e(B")
55 ;; HZ escape sequence to designate ASCII.
56 (defvar hz-ascii-designnation "~}")
57 ;; Regexp of ZW sequence to start GB2312.
58 (defvar zw-start-gb "^zW")
59 ;; Regexp for start of GB2312 in an encoding mixture of HZ and ZW.
60 (defvar hz/zw-start-gb
61 (concat hz-gb-designnation "\\|" zw-start-gb "\\|[^\0-\177]"))
63 (defvar decode-hz-line-continuation nil
64 "Flag to tell if we should care line continuation convention of Hz.")
66 (defconst hz-set-msb-table
67 (let ((str (make-string 127 0))
73 (aset str i (+ i 128))
78 (defun decode-hz-region (beg end)
79 "Decode HZ/ZW encoded text in the current region.
80 Return the length of resulting text."
85 (narrow-to-region beg end)
87 ;; We, at first, convert HZ/ZW to `euc-china',
90 ;; "~\n" -> "\n", "~~" -> "~"
91 (goto-char (point-min))
92 (while (search-forward "~" nil t)
93 (setq ch (following-char))
94 (if (or (= ch ?\n) (= ch ?~)) (delete-char -1)))
96 ;; "^zW...\n" -> Chinese GB2312
97 ;; "~{...~}" -> Chinese GB2312
98 (goto-char (point-min))
100 (while (re-search-forward hz/zw-start-gb nil t)
101 (setq pos (match-beginning 0)
103 ;; Record the first position to start conversion.
104 (or beg (setq beg pos))
107 (if (>= ch 128) ; 8bit GB2312
112 (if (= ch ?z) ; ZW -> euc-china
114 (translate-region (point) end hz-set-msb-table)
116 (if (search-forward hz-ascii-designnation
117 (if decode-hz-line-continuation nil end)
121 (translate-region pos (point) hz-set-msb-table))))
123 (decode-coding-region beg end 'euc-china)))
124 (- (point-max) (point-min)))))
127 (defun decode-hz-buffer ()
128 "Decode HZ/ZW encoded text in the current buffer."
130 (decode-hz-region (point-min) (point-max)))
133 (defun encode-hz-region (beg end)
134 "Encode the text in the current region to HZ.
135 Return the length of resulting text."
139 (narrow-to-region beg end)
142 (goto-char (point-min))
143 (while (search-forward "~" nil t) (insert ?~))
145 ;; Chinese GB2312 -> "~{...~}"
146 (goto-char (point-min))
147 (if (re-search-forward "\\cc" nil t)
149 (goto-char (setq pos (match-beginning 0)))
150 (encode-coding-region pos (point-max) 'iso-2022-7bit)
152 (while (search-forward iso2022-gb-designation nil t)
154 (insert hz-gb-designnation))
156 (while (search-forward iso2022-ascii-designation nil t)
158 (insert hz-ascii-designnation))))
159 (- (point-max) (point-min)))))
162 (defun encode-hz-buffer ()
163 "Encode the text in the current buffer to HZ."
165 (encode-hz-region (point-min) (point-max)))
168 (defun post-read-decode-hz (len)
170 (buffer-modified-p (buffer-modified-p))
171 last-coding-system-used)
173 (decode-hz-region pos (+ pos len))
174 (set-buffer-modified-p buffer-modified-p))))
177 (defun pre-write-encode-hz (from to)
178 (let ((buf (current-buffer)))
179 (set-buffer (generate-new-buffer " *temp*"))
182 (insert-buffer-substring buf from to))
183 (let (last-coding-system-used)
184 (encode-hz-region 1 (point-max)))
187 (provide 'china-util)
189 ;;; china-util.el ends here