1 ;; china-util.el -- utilities for Chinese
3 ;; Copyright (C) 1995 Free Software Foundation, Inc.
4 ;; Copyright (C) 1995 Electrotechnical Laboratory, JAPAN.
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
22 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
26 ;; Hz/ZW encoding stuffs
28 ;; HZ is an encoding method for Chinese character set GB2312 used
29 ;; widely in Internet. It is very similar to 7-bit environment of
30 ;; ISO-2022. The difference is that HZ uses the sequence "~{" and
31 ;; "~}" for designating GB2312 and ASCII respectively, hence, it
32 ;; doesn't uses ESC (0x1B) code.
34 ;; ZW is another encoding method for Chinese character set GB2312. It
35 ;; encodes Chinese characters line by line by starting each line with
36 ;; the sequence "zW". It also uses only 7-bit as HZ.
38 ;; ISO-2022 escape sequence to designate GB2312.
39 (defvar iso2022-gb-designation
"\e$A")
40 ;; HZ escape sequence to designate GB2312.
41 (defvar hz-gb-designnation
"~{")
42 ;; ISO-2022 escape sequence to designate ASCII.
43 (defvar iso2022-ascii-designation
"\e(B")
44 ;; HZ escape sequence to designate ASCII.
45 (defvar hz-ascii-designnation
"~}")
46 ;; Regexp of ZW sequence to start GB2312.
47 (defvar zw-start-gb
"^zW")
48 ;; Regexp for start of GB2312 in an encoding mixture of HZ and ZW.
49 (defvar hz
/zw-start-gb
(concat hz-gb-designnation
"\\|" zw-start-gb
))
51 (defvar decode-hz-line-continuation nil
52 "Flag to tell if we should care line continuation convention of Hz.")
55 (defun decode-hz-region (beg end
)
56 "Decode HZ/ZW encoded text in the current region.
57 Return the length of resulting text."
61 (narrow-to-region beg end
)
63 ;; We, at first, convert HZ/ZW to `coding-system-iso-2022-7',
67 (goto-char (point-min))
68 (while (search-forward "~" nil t
)
69 (if (= (following-char) ?
\n) (delete-char -
1))
70 (if (not (eobp)) (forward-char 1)))
72 ;; "^zW...\n" -> Chinese GB2312
73 ;; "~{...~}" -> Chinese GB2312
74 (goto-char (point-min))
75 (let ((chinese-found nil
))
76 (while (re-search-forward hz
/zw-start-gb nil t
)
77 (if (= (char-after (match-beginning 0)) ?z
)
78 ;; ZW -> coding-system-iso-20227-7
81 (insert iso2022-gb-designation
)
83 (insert iso2022-ascii-designation
))
84 ;; HZ -> coding-system-iso-20227-7
86 (insert iso2022-gb-designation
)
87 (let ((pos (save-excursion (end-of-line) (point))))
88 (if (search-forward hz-ascii-designnation pos t
)
89 (replace-match iso2022-ascii-designation
)
90 (if (not decode-hz-line-continuation
)
91 (insert iso2022-ascii-designation
)))))
92 (setq chinese-found t
))
94 (let ((enable-multibyte-characters nil
))
95 ;; Here we check if the text contains EUC (China) codes.
96 ;; If any, we had better decode them also.
97 (goto-char (point-min))
98 (re-search-forward "[\240-\377]" nil t
)))
99 (decode-coding-region (point-min) (point-max)
100 'coding-system-euc-china
)))
103 (goto-char (point-min))
104 (while (search-forward "~~" nil t
) (delete-char -
1))
105 (- (point-max) (point-min)))))
108 (defun decode-hz-buffer ()
109 "Decode HZ/ZW encoded text in the current buffer."
111 (decode-hz-region (point-min) (point-max)))
114 (defun encode-hz-region (beg end
)
115 "Encode the text in the current region to HZ.
116 Return the length of resulting text."
120 (narrow-to-region beg end
)
123 (goto-char (point-min))
124 (while (search-forward "~" nil t
) (insert ?~
))
126 ;; Chinese GB2312 -> "~{...~}"
127 (goto-char (point-min))
128 (if (re-search-forward "\\cc" nil t
)
129 (let ((enable-multibyte-characters nil
)
131 (goto-char (setq pos
(match-beginning 0)))
132 (encode-coding-region pos
(point-max) 'coding-system-iso-2022-7
)
134 (while (search-forward iso2022-gb-designation nil t
)
136 (insert hz-gb-designnation
))
138 (while (search-forward iso2022-ascii-designation nil t
)
140 (insert hz-ascii-designnation
))))
141 (- (point-max) (point-min)))))
144 (defun encode-hz-buffer ()
145 "Encode the text in the current buffer to HZ."
147 (encode-hz-region (point-min) (point-max)))
150 (provide 'language
/china-util
)
153 ;;; generated-autoload-file: "../loaddefs.el"
155 ;;; china-util.el ends here