Mercurial > emacs
annotate lisp/language/china-util.el @ 89571:242f2cc0134b
(Fdefine_coding_system_alias): Update Vcoding_system_list.
author | Kenichi Handa <handa@m17n.org> |
---|---|
date | Mon, 06 Oct 2003 11:21:31 +0000 |
parents | 2f877ed80fa6 |
children | 68c22ea6027c |
rev | line source |
---|---|
42153
ca6dbe4635da
Implementing euc-tw encoding.
Werner LEMBERG <wl@gnu.org>
parents:
38414
diff
changeset
|
1 ;;; china-util.el --- utilities for Chinese -*- coding: iso-2022-7bit -*- |
17052 | 2 |
51079
bd6dedbdc53f
(hz-set-msb-table): Build when compiling.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
42153
diff
changeset
|
3 ;; Copyright (C) 1995, 2003 Electrotechnical Laboratory, JAPAN. |
89483 | 4 ;; Licensed to the Free Software Foundation. |
42153
ca6dbe4635da
Implementing euc-tw encoding.
Werner LEMBERG <wl@gnu.org>
parents:
38414
diff
changeset
|
5 ;; Copyright (C) 1995, 2001 Free Software Foundation, Inc. |
89483 | 6 ;; Copyright (C) 2003 |
7 ;; National Institute of Advanced Industrial Science and Technology (AIST) | |
8 ;; Registration Number H13PRO009 | |
17052 | 9 |
10 ;; Keywords: mule, multilingual, Chinese | |
11 | |
12 ;; This file is part of GNU Emacs. | |
13 | |
14 ;; GNU Emacs is free software; you can redistribute it and/or modify | |
15 ;; it under the terms of the GNU General Public License as published by | |
16 ;; the Free Software Foundation; either version 2, or (at your option) | |
17 ;; any later version. | |
18 | |
19 ;; GNU Emacs is distributed in the hope that it will be useful, | |
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
22 ;; GNU General Public License for more details. | |
23 | |
24 ;; You should have received a copy of the GNU General Public License | |
17071 | 25 ;; along with GNU Emacs; see the file COPYING. If not, write to the |
26 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
27 ;; Boston, MA 02111-1307, USA. | |
17052 | 28 |
38414
67b464da13ec
Some fixes to follow coding conventions.
Pavel Janík <Pavel@Janik.cz>
parents:
28906
diff
changeset
|
29 ;;; Commentary: |
67b464da13ec
Some fixes to follow coding conventions.
Pavel Janík <Pavel@Janik.cz>
parents:
28906
diff
changeset
|
30 |
17052 | 31 ;;; Code: |
32 | |
42153
ca6dbe4635da
Implementing euc-tw encoding.
Werner LEMBERG <wl@gnu.org>
parents:
38414
diff
changeset
|
33 ;; Hz/ZW/EUC-TW encoding stuff |
17052 | 34 |
35 ;; HZ is an encoding method for Chinese character set GB2312 used | |
36 ;; widely in Internet. It is very similar to 7-bit environment of | |
37 ;; ISO-2022. The difference is that HZ uses the sequence "~{" and | |
38 ;; "~}" for designating GB2312 and ASCII respectively, hence, it | |
39 ;; doesn't uses ESC (0x1B) code. | |
40 | |
41 ;; ZW is another encoding method for Chinese character set GB2312. It | |
42 ;; encodes Chinese characters line by line by starting each line with | |
43 ;; the sequence "zW". It also uses only 7-bit as HZ. | |
44 | |
42153
ca6dbe4635da
Implementing euc-tw encoding.
Werner LEMBERG <wl@gnu.org>
parents:
38414
diff
changeset
|
45 ;; EUC-TW is similar to EUC-KS or EUC-JP. Its main character set is |
ca6dbe4635da
Implementing euc-tw encoding.
Werner LEMBERG <wl@gnu.org>
parents:
38414
diff
changeset
|
46 ;; plane 1 of CNS 11643; characters of planes 2 to 7 are accessed with |
ca6dbe4635da
Implementing euc-tw encoding.
Werner LEMBERG <wl@gnu.org>
parents:
38414
diff
changeset
|
47 ;; a single shift escape followed by three bytes: the first gives the |
ca6dbe4635da
Implementing euc-tw encoding.
Werner LEMBERG <wl@gnu.org>
parents:
38414
diff
changeset
|
48 ;; plane, the second and third the character code. Note that characters |
ca6dbe4635da
Implementing euc-tw encoding.
Werner LEMBERG <wl@gnu.org>
parents:
38414
diff
changeset
|
49 ;; of plane 1 are (redundantly) accessible with a single shift escape |
ca6dbe4635da
Implementing euc-tw encoding.
Werner LEMBERG <wl@gnu.org>
parents:
38414
diff
changeset
|
50 ;; also. |
ca6dbe4635da
Implementing euc-tw encoding.
Werner LEMBERG <wl@gnu.org>
parents:
38414
diff
changeset
|
51 |
17052 | 52 ;; ISO-2022 escape sequence to designate GB2312. |
53 (defvar iso2022-gb-designation "\e$A") | |
54 ;; HZ escape sequence to designate GB2312. | |
55 (defvar hz-gb-designnation "~{") | |
56 ;; ISO-2022 escape sequence to designate ASCII. | |
57 (defvar iso2022-ascii-designation "\e(B") | |
58 ;; HZ escape sequence to designate ASCII. | |
59 (defvar hz-ascii-designnation "~}") | |
60 ;; Regexp of ZW sequence to start GB2312. | |
61 (defvar zw-start-gb "^zW") | |
62 ;; Regexp for start of GB2312 in an encoding mixture of HZ and ZW. | |
20738
f510736ce3d1
In setup-LANGUAGE-environment functions,
Kenichi Handa <handa@m17n.org>
parents:
19428
diff
changeset
|
63 (defvar hz/zw-start-gb |
f510736ce3d1
In setup-LANGUAGE-environment functions,
Kenichi Handa <handa@m17n.org>
parents:
19428
diff
changeset
|
64 (concat hz-gb-designnation "\\|" zw-start-gb "\\|[^\0-\177]")) |
17052 | 65 |
66 (defvar decode-hz-line-continuation nil | |
67 "Flag to tell if we should care line continuation convention of Hz.") | |
68 | |
20738
f510736ce3d1
In setup-LANGUAGE-environment functions,
Kenichi Handa <handa@m17n.org>
parents:
19428
diff
changeset
|
69 (defconst hz-set-msb-table |
51079
bd6dedbdc53f
(hz-set-msb-table): Build when compiling.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
42153
diff
changeset
|
70 (eval-when-compile |
bd6dedbdc53f
(hz-set-msb-table): Build when compiling.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
42153
diff
changeset
|
71 (let ((chars nil) |
bd6dedbdc53f
(hz-set-msb-table): Build when compiling.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
42153
diff
changeset
|
72 (i 0)) |
bd6dedbdc53f
(hz-set-msb-table): Build when compiling.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
42153
diff
changeset
|
73 (while (< i 33) |
bd6dedbdc53f
(hz-set-msb-table): Build when compiling.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
42153
diff
changeset
|
74 (push i chars) |
bd6dedbdc53f
(hz-set-msb-table): Build when compiling.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
42153
diff
changeset
|
75 (setq i (1+ i))) |
bd6dedbdc53f
(hz-set-msb-table): Build when compiling.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
42153
diff
changeset
|
76 (while (< i 127) |
bd6dedbdc53f
(hz-set-msb-table): Build when compiling.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
42153
diff
changeset
|
77 (push (+ i 128) chars) |
bd6dedbdc53f
(hz-set-msb-table): Build when compiling.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
42153
diff
changeset
|
78 (setq i (1+ i))) |
bd6dedbdc53f
(hz-set-msb-table): Build when compiling.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
42153
diff
changeset
|
79 (apply 'string (nreverse chars))))) |
20738
f510736ce3d1
In setup-LANGUAGE-environment functions,
Kenichi Handa <handa@m17n.org>
parents:
19428
diff
changeset
|
80 |
17052 | 81 ;;;###autoload |
82 (defun decode-hz-region (beg end) | |
83 "Decode HZ/ZW encoded text in the current region. | |
84 Return the length of resulting text." | |
85 (interactive "r") | |
86 (save-excursion | |
87 (save-restriction | |
20738
f510736ce3d1
In setup-LANGUAGE-environment functions,
Kenichi Handa <handa@m17n.org>
parents:
19428
diff
changeset
|
88 (let (pos ch) |
f510736ce3d1
In setup-LANGUAGE-environment functions,
Kenichi Handa <handa@m17n.org>
parents:
19428
diff
changeset
|
89 (narrow-to-region beg end) |
17052 | 90 |
20738
f510736ce3d1
In setup-LANGUAGE-environment functions,
Kenichi Handa <handa@m17n.org>
parents:
19428
diff
changeset
|
91 ;; We, at first, convert HZ/ZW to `euc-china', |
f510736ce3d1
In setup-LANGUAGE-environment functions,
Kenichi Handa <handa@m17n.org>
parents:
19428
diff
changeset
|
92 ;; then decode it. |
17052 | 93 |
20738
f510736ce3d1
In setup-LANGUAGE-environment functions,
Kenichi Handa <handa@m17n.org>
parents:
19428
diff
changeset
|
94 ;; "~\n" -> "\n", "~~" -> "~" |
f510736ce3d1
In setup-LANGUAGE-environment functions,
Kenichi Handa <handa@m17n.org>
parents:
19428
diff
changeset
|
95 (goto-char (point-min)) |
f510736ce3d1
In setup-LANGUAGE-environment functions,
Kenichi Handa <handa@m17n.org>
parents:
19428
diff
changeset
|
96 (while (search-forward "~" nil t) |
f510736ce3d1
In setup-LANGUAGE-environment functions,
Kenichi Handa <handa@m17n.org>
parents:
19428
diff
changeset
|
97 (setq ch (following-char)) |
f510736ce3d1
In setup-LANGUAGE-environment functions,
Kenichi Handa <handa@m17n.org>
parents:
19428
diff
changeset
|
98 (if (or (= ch ?\n) (= ch ?~)) (delete-char -1))) |
17052 | 99 |
20738
f510736ce3d1
In setup-LANGUAGE-environment functions,
Kenichi Handa <handa@m17n.org>
parents:
19428
diff
changeset
|
100 ;; "^zW...\n" -> Chinese GB2312 |
f510736ce3d1
In setup-LANGUAGE-environment functions,
Kenichi Handa <handa@m17n.org>
parents:
19428
diff
changeset
|
101 ;; "~{...~}" -> Chinese GB2312 |
f510736ce3d1
In setup-LANGUAGE-environment functions,
Kenichi Handa <handa@m17n.org>
parents:
19428
diff
changeset
|
102 (goto-char (point-min)) |
f510736ce3d1
In setup-LANGUAGE-environment functions,
Kenichi Handa <handa@m17n.org>
parents:
19428
diff
changeset
|
103 (setq beg nil) |
17052 | 104 (while (re-search-forward hz/zw-start-gb nil t) |
20738
f510736ce3d1
In setup-LANGUAGE-environment functions,
Kenichi Handa <handa@m17n.org>
parents:
19428
diff
changeset
|
105 (setq pos (match-beginning 0) |
f510736ce3d1
In setup-LANGUAGE-environment functions,
Kenichi Handa <handa@m17n.org>
parents:
19428
diff
changeset
|
106 ch (char-after pos)) |
f510736ce3d1
In setup-LANGUAGE-environment functions,
Kenichi Handa <handa@m17n.org>
parents:
19428
diff
changeset
|
107 ;; Record the first position to start conversion. |
f510736ce3d1
In setup-LANGUAGE-environment functions,
Kenichi Handa <handa@m17n.org>
parents:
19428
diff
changeset
|
108 (or beg (setq beg pos)) |
f510736ce3d1
In setup-LANGUAGE-environment functions,
Kenichi Handa <handa@m17n.org>
parents:
19428
diff
changeset
|
109 (end-of-line) |
f510736ce3d1
In setup-LANGUAGE-environment functions,
Kenichi Handa <handa@m17n.org>
parents:
19428
diff
changeset
|
110 (setq end (point)) |
f510736ce3d1
In setup-LANGUAGE-environment functions,
Kenichi Handa <handa@m17n.org>
parents:
19428
diff
changeset
|
111 (if (>= ch 128) ; 8bit GB2312 |
f510736ce3d1
In setup-LANGUAGE-environment functions,
Kenichi Handa <handa@m17n.org>
parents:
19428
diff
changeset
|
112 nil |
f510736ce3d1
In setup-LANGUAGE-environment functions,
Kenichi Handa <handa@m17n.org>
parents:
19428
diff
changeset
|
113 (goto-char pos) |
f510736ce3d1
In setup-LANGUAGE-environment functions,
Kenichi Handa <handa@m17n.org>
parents:
19428
diff
changeset
|
114 (delete-char 2) |
f510736ce3d1
In setup-LANGUAGE-environment functions,
Kenichi Handa <handa@m17n.org>
parents:
19428
diff
changeset
|
115 (setq end (- end 2)) |
f510736ce3d1
In setup-LANGUAGE-environment functions,
Kenichi Handa <handa@m17n.org>
parents:
19428
diff
changeset
|
116 (if (= ch ?z) ; ZW -> euc-china |
f510736ce3d1
In setup-LANGUAGE-environment functions,
Kenichi Handa <handa@m17n.org>
parents:
19428
diff
changeset
|
117 (progn |
f510736ce3d1
In setup-LANGUAGE-environment functions,
Kenichi Handa <handa@m17n.org>
parents:
19428
diff
changeset
|
118 (translate-region (point) end hz-set-msb-table) |
f510736ce3d1
In setup-LANGUAGE-environment functions,
Kenichi Handa <handa@m17n.org>
parents:
19428
diff
changeset
|
119 (goto-char end)) |
f510736ce3d1
In setup-LANGUAGE-environment functions,
Kenichi Handa <handa@m17n.org>
parents:
19428
diff
changeset
|
120 (if (search-forward hz-ascii-designnation |
f510736ce3d1
In setup-LANGUAGE-environment functions,
Kenichi Handa <handa@m17n.org>
parents:
19428
diff
changeset
|
121 (if decode-hz-line-continuation nil end) |
f510736ce3d1
In setup-LANGUAGE-environment functions,
Kenichi Handa <handa@m17n.org>
parents:
19428
diff
changeset
|
122 t) |
f510736ce3d1
In setup-LANGUAGE-environment functions,
Kenichi Handa <handa@m17n.org>
parents:
19428
diff
changeset
|
123 (delete-char -2)) |
f510736ce3d1
In setup-LANGUAGE-environment functions,
Kenichi Handa <handa@m17n.org>
parents:
19428
diff
changeset
|
124 (setq end (point)) |
f510736ce3d1
In setup-LANGUAGE-environment functions,
Kenichi Handa <handa@m17n.org>
parents:
19428
diff
changeset
|
125 (translate-region pos (point) hz-set-msb-table)))) |
f510736ce3d1
In setup-LANGUAGE-environment functions,
Kenichi Handa <handa@m17n.org>
parents:
19428
diff
changeset
|
126 (if beg |
f510736ce3d1
In setup-LANGUAGE-environment functions,
Kenichi Handa <handa@m17n.org>
parents:
19428
diff
changeset
|
127 (decode-coding-region beg end 'euc-china))) |
17052 | 128 (- (point-max) (point-min))))) |
129 | |
130 ;;;###autoload | |
131 (defun decode-hz-buffer () | |
132 "Decode HZ/ZW encoded text in the current buffer." | |
133 (interactive) | |
134 (decode-hz-region (point-min) (point-max))) | |
135 | |
136 ;;;###autoload | |
137 (defun encode-hz-region (beg end) | |
138 "Encode the text in the current region to HZ. | |
139 Return the length of resulting text." | |
140 (interactive "r") | |
141 (save-excursion | |
142 (save-restriction | |
143 (narrow-to-region beg end) | |
144 | |
145 ;; "~" -> "~~" | |
146 (goto-char (point-min)) | |
147 (while (search-forward "~" nil t) (insert ?~)) | |
148 | |
149 ;; Chinese GB2312 -> "~{...~}" | |
150 (goto-char (point-min)) | |
151 (if (re-search-forward "\\cc" nil t) | |
20838
3d67d591c066
(encode-hz-region): Do not bind
Kenichi Handa <handa@m17n.org>
parents:
20738
diff
changeset
|
152 (let (pos) |
17052 | 153 (goto-char (setq pos (match-beginning 0))) |
18553
62e17ab4e33e
Use true coding system names instead of
Kenichi Handa <handa@m17n.org>
parents:
18377
diff
changeset
|
154 (encode-coding-region pos (point-max) 'iso-2022-7bit) |
17052 | 155 (goto-char pos) |
156 (while (search-forward iso2022-gb-designation nil t) | |
157 (delete-char -3) | |
158 (insert hz-gb-designnation)) | |
159 (goto-char pos) | |
160 (while (search-forward iso2022-ascii-designation nil t) | |
161 (delete-char -3) | |
162 (insert hz-ascii-designnation)))) | |
163 (- (point-max) (point-min))))) | |
164 | |
165 ;;;###autoload | |
166 (defun encode-hz-buffer () | |
167 "Encode the text in the current buffer to HZ." | |
168 (interactive) | |
169 (encode-hz-region (point-min) (point-max))) | |
170 | |
88708 | 171 ;;;###autoload |
172 (defun post-read-decode-hz (len) | |
173 (let ((pos (point)) | |
174 (buffer-modified-p (buffer-modified-p)) | |
175 last-coding-system-used) | |
176 (prog1 | |
177 (decode-hz-region pos (+ pos len)) | |
178 (set-buffer-modified-p buffer-modified-p)))) | |
42153
ca6dbe4635da
Implementing euc-tw encoding.
Werner LEMBERG <wl@gnu.org>
parents:
38414
diff
changeset
|
179 |
88708 | 180 ;;;###autoload |
181 (defun pre-write-encode-hz (from to) | |
182 (let ((buf (current-buffer))) | |
183 (set-buffer (generate-new-buffer " *temp*")) | |
184 (if (stringp from) | |
185 (insert from) | |
186 (insert-buffer-substring buf from to)) | |
187 (let (last-coding-system-used) | |
188 (encode-hz-region 1 (point-max))) | |
189 nil)) | |
17052 | 190 ;; |
18309
bd8b521f5218
Provide XXX-util instead of
Kenichi Handa <handa@m17n.org>
parents:
17993
diff
changeset
|
191 (provide 'china-util) |
17052 | 192 |
193 ;;; china-util.el ends here |