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