Mercurial > emacs
annotate lisp/language/china-util.el @ 89407:a9b9211d250a
*** empty log message ***
author | Kenichi Handa <handa@m17n.org> |
---|---|
date | Sat, 12 Apr 2003 02:37:18 +0000 |
parents | 0816a10eb8ac |
children | 2f877ed80fa6 |
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 |
3 ;; Copyright (C) 1995 Electrotechnical Laboratory, JAPAN. | |
18377
8b4a66c66dd6
Change copyright notice.
Richard M. Stallman <rms@gnu.org>
parents:
18309
diff
changeset
|
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. |
17052 | 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 | |
17071 | 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. | |
17052 | 25 |
38414
67b464da13ec
Some fixes to follow coding conventions.
Pavel Janík <Pavel@Janik.cz>
parents:
28906
diff
changeset
|
26 ;;; Commentary: |
67b464da13ec
Some fixes to follow coding conventions.
Pavel Janík <Pavel@Janik.cz>
parents:
28906
diff
changeset
|
27 |
17052 | 28 ;;; Code: |
29 | |
42153
ca6dbe4635da
Implementing euc-tw encoding.
Werner LEMBERG <wl@gnu.org>
parents:
38414
diff
changeset
|
30 ;; Hz/ZW/EUC-TW encoding stuff |
17052 | 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 | |
42153
ca6dbe4635da
Implementing euc-tw encoding.
Werner LEMBERG <wl@gnu.org>
parents:
38414
diff
changeset
|
42 ;; 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
|
43 ;; 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
|
44 ;; 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
|
45 ;; 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
|
46 ;; 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
|
47 ;; also. |
ca6dbe4635da
Implementing euc-tw encoding.
Werner LEMBERG <wl@gnu.org>
parents:
38414
diff
changeset
|
48 |
17052 | 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. | |
20738
f510736ce3d1
In setup-LANGUAGE-environment functions,
Kenichi Handa <handa@m17n.org>
parents:
19428
diff
changeset
|
60 (defvar hz/zw-start-gb |
f510736ce3d1
In setup-LANGUAGE-environment functions,
Kenichi Handa <handa@m17n.org>
parents:
19428
diff
changeset
|
61 (concat hz-gb-designnation "\\|" zw-start-gb "\\|[^\0-\177]")) |
17052 | 62 |
63 (defvar decode-hz-line-continuation nil | |
64 "Flag to tell if we should care line continuation convention of Hz.") | |
65 | |
20738
f510736ce3d1
In setup-LANGUAGE-environment functions,
Kenichi Handa <handa@m17n.org>
parents:
19428
diff
changeset
|
66 (defconst hz-set-msb-table |
f510736ce3d1
In setup-LANGUAGE-environment functions,
Kenichi Handa <handa@m17n.org>
parents:
19428
diff
changeset
|
67 (let ((str (make-string 127 0)) |
f510736ce3d1
In setup-LANGUAGE-environment functions,
Kenichi Handa <handa@m17n.org>
parents:
19428
diff
changeset
|
68 (i 0)) |
f510736ce3d1
In setup-LANGUAGE-environment functions,
Kenichi Handa <handa@m17n.org>
parents:
19428
diff
changeset
|
69 (while (< i 33) |
f510736ce3d1
In setup-LANGUAGE-environment functions,
Kenichi Handa <handa@m17n.org>
parents:
19428
diff
changeset
|
70 (aset str i i) |
f510736ce3d1
In setup-LANGUAGE-environment functions,
Kenichi Handa <handa@m17n.org>
parents:
19428
diff
changeset
|
71 (setq i (1+ i))) |
f510736ce3d1
In setup-LANGUAGE-environment functions,
Kenichi Handa <handa@m17n.org>
parents:
19428
diff
changeset
|
72 (while (< i 127) |
f510736ce3d1
In setup-LANGUAGE-environment functions,
Kenichi Handa <handa@m17n.org>
parents:
19428
diff
changeset
|
73 (aset str i (+ i 128)) |
f510736ce3d1
In setup-LANGUAGE-environment functions,
Kenichi Handa <handa@m17n.org>
parents:
19428
diff
changeset
|
74 (setq i (1+ i))) |
f510736ce3d1
In setup-LANGUAGE-environment functions,
Kenichi Handa <handa@m17n.org>
parents:
19428
diff
changeset
|
75 str)) |
f510736ce3d1
In setup-LANGUAGE-environment functions,
Kenichi Handa <handa@m17n.org>
parents:
19428
diff
changeset
|
76 |
17052 | 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 | |
20738
f510736ce3d1
In setup-LANGUAGE-environment functions,
Kenichi Handa <handa@m17n.org>
parents:
19428
diff
changeset
|
84 (let (pos ch) |
f510736ce3d1
In setup-LANGUAGE-environment functions,
Kenichi Handa <handa@m17n.org>
parents:
19428
diff
changeset
|
85 (narrow-to-region beg end) |
17052 | 86 |
20738
f510736ce3d1
In setup-LANGUAGE-environment functions,
Kenichi Handa <handa@m17n.org>
parents:
19428
diff
changeset
|
87 ;; We, at first, convert HZ/ZW to `euc-china', |
f510736ce3d1
In setup-LANGUAGE-environment functions,
Kenichi Handa <handa@m17n.org>
parents:
19428
diff
changeset
|
88 ;; then decode it. |
17052 | 89 |
20738
f510736ce3d1
In setup-LANGUAGE-environment functions,
Kenichi Handa <handa@m17n.org>
parents:
19428
diff
changeset
|
90 ;; "~\n" -> "\n", "~~" -> "~" |
f510736ce3d1
In setup-LANGUAGE-environment functions,
Kenichi Handa <handa@m17n.org>
parents:
19428
diff
changeset
|
91 (goto-char (point-min)) |
f510736ce3d1
In setup-LANGUAGE-environment functions,
Kenichi Handa <handa@m17n.org>
parents:
19428
diff
changeset
|
92 (while (search-forward "~" nil t) |
f510736ce3d1
In setup-LANGUAGE-environment functions,
Kenichi Handa <handa@m17n.org>
parents:
19428
diff
changeset
|
93 (setq ch (following-char)) |
f510736ce3d1
In setup-LANGUAGE-environment functions,
Kenichi Handa <handa@m17n.org>
parents:
19428
diff
changeset
|
94 (if (or (= ch ?\n) (= ch ?~)) (delete-char -1))) |
17052 | 95 |
20738
f510736ce3d1
In setup-LANGUAGE-environment functions,
Kenichi Handa <handa@m17n.org>
parents:
19428
diff
changeset
|
96 ;; "^zW...\n" -> Chinese GB2312 |
f510736ce3d1
In setup-LANGUAGE-environment functions,
Kenichi Handa <handa@m17n.org>
parents:
19428
diff
changeset
|
97 ;; "~{...~}" -> Chinese GB2312 |
f510736ce3d1
In setup-LANGUAGE-environment functions,
Kenichi Handa <handa@m17n.org>
parents:
19428
diff
changeset
|
98 (goto-char (point-min)) |
f510736ce3d1
In setup-LANGUAGE-environment functions,
Kenichi Handa <handa@m17n.org>
parents:
19428
diff
changeset
|
99 (setq beg nil) |
17052 | 100 (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
|
101 (setq pos (match-beginning 0) |
f510736ce3d1
In setup-LANGUAGE-environment functions,
Kenichi Handa <handa@m17n.org>
parents:
19428
diff
changeset
|
102 ch (char-after pos)) |
f510736ce3d1
In setup-LANGUAGE-environment functions,
Kenichi Handa <handa@m17n.org>
parents:
19428
diff
changeset
|
103 ;; Record the first position to start conversion. |
f510736ce3d1
In setup-LANGUAGE-environment functions,
Kenichi Handa <handa@m17n.org>
parents:
19428
diff
changeset
|
104 (or beg (setq beg pos)) |
f510736ce3d1
In setup-LANGUAGE-environment functions,
Kenichi Handa <handa@m17n.org>
parents:
19428
diff
changeset
|
105 (end-of-line) |
f510736ce3d1
In setup-LANGUAGE-environment functions,
Kenichi Handa <handa@m17n.org>
parents:
19428
diff
changeset
|
106 (setq end (point)) |
f510736ce3d1
In setup-LANGUAGE-environment functions,
Kenichi Handa <handa@m17n.org>
parents:
19428
diff
changeset
|
107 (if (>= ch 128) ; 8bit GB2312 |
f510736ce3d1
In setup-LANGUAGE-environment functions,
Kenichi Handa <handa@m17n.org>
parents:
19428
diff
changeset
|
108 nil |
f510736ce3d1
In setup-LANGUAGE-environment functions,
Kenichi Handa <handa@m17n.org>
parents:
19428
diff
changeset
|
109 (goto-char pos) |
f510736ce3d1
In setup-LANGUAGE-environment functions,
Kenichi Handa <handa@m17n.org>
parents:
19428
diff
changeset
|
110 (delete-char 2) |
f510736ce3d1
In setup-LANGUAGE-environment functions,
Kenichi Handa <handa@m17n.org>
parents:
19428
diff
changeset
|
111 (setq end (- end 2)) |
f510736ce3d1
In setup-LANGUAGE-environment functions,
Kenichi Handa <handa@m17n.org>
parents:
19428
diff
changeset
|
112 (if (= ch ?z) ; ZW -> euc-china |
f510736ce3d1
In setup-LANGUAGE-environment functions,
Kenichi Handa <handa@m17n.org>
parents:
19428
diff
changeset
|
113 (progn |
f510736ce3d1
In setup-LANGUAGE-environment functions,
Kenichi Handa <handa@m17n.org>
parents:
19428
diff
changeset
|
114 (translate-region (point) end hz-set-msb-table) |
f510736ce3d1
In setup-LANGUAGE-environment functions,
Kenichi Handa <handa@m17n.org>
parents:
19428
diff
changeset
|
115 (goto-char end)) |
f510736ce3d1
In setup-LANGUAGE-environment functions,
Kenichi Handa <handa@m17n.org>
parents:
19428
diff
changeset
|
116 (if (search-forward hz-ascii-designnation |
f510736ce3d1
In setup-LANGUAGE-environment functions,
Kenichi Handa <handa@m17n.org>
parents:
19428
diff
changeset
|
117 (if decode-hz-line-continuation nil end) |
f510736ce3d1
In setup-LANGUAGE-environment functions,
Kenichi Handa <handa@m17n.org>
parents:
19428
diff
changeset
|
118 t) |
f510736ce3d1
In setup-LANGUAGE-environment functions,
Kenichi Handa <handa@m17n.org>
parents:
19428
diff
changeset
|
119 (delete-char -2)) |
f510736ce3d1
In setup-LANGUAGE-environment functions,
Kenichi Handa <handa@m17n.org>
parents:
19428
diff
changeset
|
120 (setq end (point)) |
f510736ce3d1
In setup-LANGUAGE-environment functions,
Kenichi Handa <handa@m17n.org>
parents:
19428
diff
changeset
|
121 (translate-region pos (point) hz-set-msb-table)))) |
f510736ce3d1
In setup-LANGUAGE-environment functions,
Kenichi Handa <handa@m17n.org>
parents:
19428
diff
changeset
|
122 (if beg |
f510736ce3d1
In setup-LANGUAGE-environment functions,
Kenichi Handa <handa@m17n.org>
parents:
19428
diff
changeset
|
123 (decode-coding-region beg end 'euc-china))) |
17052 | 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) | |
20838
3d67d591c066
(encode-hz-region): Do not bind
Kenichi Handa <handa@m17n.org>
parents:
20738
diff
changeset
|
148 (let (pos) |
17052 | 149 (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
|
150 (encode-coding-region pos (point-max) 'iso-2022-7bit) |
17052 | 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 | |
88708 | 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)))) | |
42153
ca6dbe4635da
Implementing euc-tw encoding.
Werner LEMBERG <wl@gnu.org>
parents:
38414
diff
changeset
|
175 |
88708 | 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)) | |
17052 | 186 ;; |
18309
bd8b521f5218
Provide XXX-util instead of
Kenichi Handa <handa@m17n.org>
parents:
17993
diff
changeset
|
187 (provide 'china-util) |
17052 | 188 |
189 ;;; china-util.el ends here |