]> code.delx.au - gnu-emacs/blob - lisp/language/china-util.el
*** empty log message ***
[gnu-emacs] / lisp / language / china-util.el
1 ;;; china-util.el --- utilities for Chinese -*- coding: iso-2022-7bit -*-
2
3 ;; Copyright (C) 1995 Electrotechnical Laboratory, JAPAN.
4 ;; Licensed to the Free Software Foundation.
5 ;; Copyright (C) 1995, 2001 Free Software Foundation, Inc.
6
7 ;; Keywords: mule, multilingual, Chinese
8
9 ;; This file is part of GNU Emacs.
10
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)
14 ;; any later version.
15
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.
20
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.
25
26 ;;; Commentary:
27
28 ;;; Code:
29
30 ;; Hz/ZW/EUC-TW encoding stuff
31
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.
37
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.
41
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
47 ;; also.
48
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]"))
62
63 (defvar decode-hz-line-continuation nil
64 "Flag to tell if we should care line continuation convention of Hz.")
65
66 (defconst hz-set-msb-table
67 (let ((str (make-string 127 0))
68 (i 0))
69 (while (< i 33)
70 (aset str i i)
71 (setq i (1+ i)))
72 (while (< i 127)
73 (aset str i (+ i 128))
74 (setq i (1+ i)))
75 str))
76
77 ;;;###autoload
78 (defun decode-hz-region (beg end)
79 "Decode HZ/ZW encoded text in the current region.
80 Return the length of resulting text."
81 (interactive "r")
82 (save-excursion
83 (save-restriction
84 (let (pos ch)
85 (narrow-to-region beg end)
86
87 ;; We, at first, convert HZ/ZW to `euc-china',
88 ;; then decode it.
89
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)))
95
96 ;; "^zW...\n" -> Chinese GB2312
97 ;; "~{...~}" -> Chinese GB2312
98 (goto-char (point-min))
99 (setq beg nil)
100 (while (re-search-forward hz/zw-start-gb nil t)
101 (setq pos (match-beginning 0)
102 ch (char-after pos))
103 ;; Record the first position to start conversion.
104 (or beg (setq beg pos))
105 (end-of-line)
106 (setq end (point))
107 (if (>= ch 128) ; 8bit GB2312
108 nil
109 (goto-char pos)
110 (delete-char 2)
111 (setq end (- end 2))
112 (if (= ch ?z) ; ZW -> euc-china
113 (progn
114 (translate-region (point) end hz-set-msb-table)
115 (goto-char end))
116 (if (search-forward hz-ascii-designnation
117 (if decode-hz-line-continuation nil end)
118 t)
119 (delete-char -2))
120 (setq end (point))
121 (translate-region pos (point) hz-set-msb-table))))
122 (if beg
123 (decode-coding-region beg end 'euc-china)))
124 (- (point-max) (point-min)))))
125
126 ;;;###autoload
127 (defun decode-hz-buffer ()
128 "Decode HZ/ZW encoded text in the current buffer."
129 (interactive)
130 (decode-hz-region (point-min) (point-max)))
131
132 ;;;###autoload
133 (defun encode-hz-region (beg end)
134 "Encode the text in the current region to HZ.
135 Return the length of resulting text."
136 (interactive "r")
137 (save-excursion
138 (save-restriction
139 (narrow-to-region beg end)
140
141 ;; "~" -> "~~"
142 (goto-char (point-min))
143 (while (search-forward "~" nil t) (insert ?~))
144
145 ;; Chinese GB2312 -> "~{...~}"
146 (goto-char (point-min))
147 (if (re-search-forward "\\cc" nil t)
148 (let (pos)
149 (goto-char (setq pos (match-beginning 0)))
150 (encode-coding-region pos (point-max) 'iso-2022-7bit)
151 (goto-char pos)
152 (while (search-forward iso2022-gb-designation nil t)
153 (delete-char -3)
154 (insert hz-gb-designnation))
155 (goto-char pos)
156 (while (search-forward iso2022-ascii-designation nil t)
157 (delete-char -3)
158 (insert hz-ascii-designnation))))
159 (- (point-max) (point-min)))))
160
161 ;;;###autoload
162 (defun encode-hz-buffer ()
163 "Encode the text in the current buffer to HZ."
164 (interactive)
165 (encode-hz-region (point-min) (point-max)))
166
167 ;;;###autoload
168 (defun post-read-decode-hz (len)
169 (let ((pos (point))
170 (buffer-modified-p (buffer-modified-p))
171 last-coding-system-used)
172 (prog1
173 (decode-hz-region pos (+ pos len))
174 (set-buffer-modified-p buffer-modified-p))))
175
176 ;;;###autoload
177 (defun pre-write-encode-hz (from to)
178 (let ((buf (current-buffer)))
179 (set-buffer (generate-new-buffer " *temp*"))
180 (if (stringp from)
181 (insert from)
182 (insert-buffer-substring buf from to))
183 (let (last-coding-system-used)
184 (encode-hz-region 1 (point-max)))
185 nil))
186 ;;
187 (provide 'china-util)
188
189 ;;; china-util.el ends here