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