Mercurial > emacs
annotate lisp/language/china-util.el @ 37223:6ae575a7a695
(defface menu): Doc fix.
author | Gerd Moellmann <gerd@gnu.org> |
---|---|
date | Thu, 05 Apr 2001 16:15:50 +0000 |
parents | 60eb71a9f901 |
children | 67b464da13ec |
rev | line source |
---|---|
17315
a3ca5e15c82a
Fix the format of the first line.
Kenichi Handa <handa@m17n.org>
parents:
17098
diff
changeset
|
1 ;;; china-util.el --- utilities for Chinese |
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. |
17052 | 5 |
6 ;; Keywords: mule, multilingual, Chinese | |
7 | |
8 ;; This file is part of GNU Emacs. | |
9 | |
10 ;; GNU Emacs is free software; you can redistribute it and/or modify | |
11 ;; it under the terms of the GNU General Public License as published by | |
12 ;; the Free Software Foundation; either version 2, or (at your option) | |
13 ;; any later version. | |
14 | |
15 ;; GNU Emacs is distributed in the hope that it will be useful, | |
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
18 ;; GNU General Public License for more details. | |
19 | |
20 ;; You should have received a copy of the GNU General Public License | |
17071 | 21 ;; along with GNU Emacs; see the file COPYING. If not, write to the |
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
23 ;; Boston, MA 02111-1307, USA. | |
17052 | 24 |
25 ;;; Code: | |
26 | |
27 ;; Hz/ZW encoding stuffs | |
28 | |
29 ;; HZ is an encoding method for Chinese character set GB2312 used | |
30 ;; widely in Internet. It is very similar to 7-bit environment of | |
31 ;; ISO-2022. The difference is that HZ uses the sequence "~{" and | |
32 ;; "~}" for designating GB2312 and ASCII respectively, hence, it | |
33 ;; doesn't uses ESC (0x1B) code. | |
34 | |
35 ;; ZW is another encoding method for Chinese character set GB2312. It | |
36 ;; encodes Chinese characters line by line by starting each line with | |
37 ;; the sequence "zW". It also uses only 7-bit as HZ. | |
38 | |
39 ;; ISO-2022 escape sequence to designate GB2312. | |
40 (defvar iso2022-gb-designation "\e$A") | |
41 ;; HZ escape sequence to designate GB2312. | |
42 (defvar hz-gb-designnation "~{") | |
43 ;; ISO-2022 escape sequence to designate ASCII. | |
44 (defvar iso2022-ascii-designation "\e(B") | |
45 ;; HZ escape sequence to designate ASCII. | |
46 (defvar hz-ascii-designnation "~}") | |
47 ;; Regexp of ZW sequence to start GB2312. | |
48 (defvar zw-start-gb "^zW") | |
49 ;; 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
|
50 (defvar hz/zw-start-gb |
f510736ce3d1
In setup-LANGUAGE-environment functions,
Kenichi Handa <handa@m17n.org>
parents:
19428
diff
changeset
|
51 (concat hz-gb-designnation "\\|" zw-start-gb "\\|[^\0-\177]")) |
17052 | 52 |
53 (defvar decode-hz-line-continuation nil | |
54 "Flag to tell if we should care line continuation convention of Hz.") | |
55 | |
20738
f510736ce3d1
In setup-LANGUAGE-environment functions,
Kenichi Handa <handa@m17n.org>
parents:
19428
diff
changeset
|
56 (defconst hz-set-msb-table |
f510736ce3d1
In setup-LANGUAGE-environment functions,
Kenichi Handa <handa@m17n.org>
parents:
19428
diff
changeset
|
57 (let ((str (make-string 127 0)) |
f510736ce3d1
In setup-LANGUAGE-environment functions,
Kenichi Handa <handa@m17n.org>
parents:
19428
diff
changeset
|
58 (i 0)) |
f510736ce3d1
In setup-LANGUAGE-environment functions,
Kenichi Handa <handa@m17n.org>
parents:
19428
diff
changeset
|
59 (while (< i 33) |
f510736ce3d1
In setup-LANGUAGE-environment functions,
Kenichi Handa <handa@m17n.org>
parents:
19428
diff
changeset
|
60 (aset str i i) |
f510736ce3d1
In setup-LANGUAGE-environment functions,
Kenichi Handa <handa@m17n.org>
parents:
19428
diff
changeset
|
61 (setq i (1+ i))) |
f510736ce3d1
In setup-LANGUAGE-environment functions,
Kenichi Handa <handa@m17n.org>
parents:
19428
diff
changeset
|
62 (while (< i 127) |
f510736ce3d1
In setup-LANGUAGE-environment functions,
Kenichi Handa <handa@m17n.org>
parents:
19428
diff
changeset
|
63 (aset str i (+ i 128)) |
f510736ce3d1
In setup-LANGUAGE-environment functions,
Kenichi Handa <handa@m17n.org>
parents:
19428
diff
changeset
|
64 (setq i (1+ i))) |
f510736ce3d1
In setup-LANGUAGE-environment functions,
Kenichi Handa <handa@m17n.org>
parents:
19428
diff
changeset
|
65 str)) |
f510736ce3d1
In setup-LANGUAGE-environment functions,
Kenichi Handa <handa@m17n.org>
parents:
19428
diff
changeset
|
66 |
17052 | 67 ;;;###autoload |
68 (defun decode-hz-region (beg end) | |
69 "Decode HZ/ZW encoded text in the current region. | |
70 Return the length of resulting text." | |
71 (interactive "r") | |
72 (save-excursion | |
73 (save-restriction | |
20738
f510736ce3d1
In setup-LANGUAGE-environment functions,
Kenichi Handa <handa@m17n.org>
parents:
19428
diff
changeset
|
74 (let (pos ch) |
f510736ce3d1
In setup-LANGUAGE-environment functions,
Kenichi Handa <handa@m17n.org>
parents:
19428
diff
changeset
|
75 (narrow-to-region beg end) |
17052 | 76 |
20738
f510736ce3d1
In setup-LANGUAGE-environment functions,
Kenichi Handa <handa@m17n.org>
parents:
19428
diff
changeset
|
77 ;; We, at first, convert HZ/ZW to `euc-china', |
f510736ce3d1
In setup-LANGUAGE-environment functions,
Kenichi Handa <handa@m17n.org>
parents:
19428
diff
changeset
|
78 ;; then decode it. |
17052 | 79 |
20738
f510736ce3d1
In setup-LANGUAGE-environment functions,
Kenichi Handa <handa@m17n.org>
parents:
19428
diff
changeset
|
80 ;; "~\n" -> "\n", "~~" -> "~" |
f510736ce3d1
In setup-LANGUAGE-environment functions,
Kenichi Handa <handa@m17n.org>
parents:
19428
diff
changeset
|
81 (goto-char (point-min)) |
f510736ce3d1
In setup-LANGUAGE-environment functions,
Kenichi Handa <handa@m17n.org>
parents:
19428
diff
changeset
|
82 (while (search-forward "~" nil t) |
f510736ce3d1
In setup-LANGUAGE-environment functions,
Kenichi Handa <handa@m17n.org>
parents:
19428
diff
changeset
|
83 (setq ch (following-char)) |
f510736ce3d1
In setup-LANGUAGE-environment functions,
Kenichi Handa <handa@m17n.org>
parents:
19428
diff
changeset
|
84 (if (or (= ch ?\n) (= ch ?~)) (delete-char -1))) |
17052 | 85 |
20738
f510736ce3d1
In setup-LANGUAGE-environment functions,
Kenichi Handa <handa@m17n.org>
parents:
19428
diff
changeset
|
86 ;; "^zW...\n" -> Chinese GB2312 |
f510736ce3d1
In setup-LANGUAGE-environment functions,
Kenichi Handa <handa@m17n.org>
parents:
19428
diff
changeset
|
87 ;; "~{...~}" -> Chinese GB2312 |
f510736ce3d1
In setup-LANGUAGE-environment functions,
Kenichi Handa <handa@m17n.org>
parents:
19428
diff
changeset
|
88 (goto-char (point-min)) |
f510736ce3d1
In setup-LANGUAGE-environment functions,
Kenichi Handa <handa@m17n.org>
parents:
19428
diff
changeset
|
89 (setq beg nil) |
17052 | 90 (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
|
91 (setq pos (match-beginning 0) |
f510736ce3d1
In setup-LANGUAGE-environment functions,
Kenichi Handa <handa@m17n.org>
parents:
19428
diff
changeset
|
92 ch (char-after pos)) |
f510736ce3d1
In setup-LANGUAGE-environment functions,
Kenichi Handa <handa@m17n.org>
parents:
19428
diff
changeset
|
93 ;; Record the first position to start conversion. |
f510736ce3d1
In setup-LANGUAGE-environment functions,
Kenichi Handa <handa@m17n.org>
parents:
19428
diff
changeset
|
94 (or beg (setq beg pos)) |
f510736ce3d1
In setup-LANGUAGE-environment functions,
Kenichi Handa <handa@m17n.org>
parents:
19428
diff
changeset
|
95 (end-of-line) |
f510736ce3d1
In setup-LANGUAGE-environment functions,
Kenichi Handa <handa@m17n.org>
parents:
19428
diff
changeset
|
96 (setq end (point)) |
f510736ce3d1
In setup-LANGUAGE-environment functions,
Kenichi Handa <handa@m17n.org>
parents:
19428
diff
changeset
|
97 (if (>= ch 128) ; 8bit GB2312 |
f510736ce3d1
In setup-LANGUAGE-environment functions,
Kenichi Handa <handa@m17n.org>
parents:
19428
diff
changeset
|
98 nil |
f510736ce3d1
In setup-LANGUAGE-environment functions,
Kenichi Handa <handa@m17n.org>
parents:
19428
diff
changeset
|
99 (goto-char pos) |
f510736ce3d1
In setup-LANGUAGE-environment functions,
Kenichi Handa <handa@m17n.org>
parents:
19428
diff
changeset
|
100 (delete-char 2) |
f510736ce3d1
In setup-LANGUAGE-environment functions,
Kenichi Handa <handa@m17n.org>
parents:
19428
diff
changeset
|
101 (setq end (- end 2)) |
f510736ce3d1
In setup-LANGUAGE-environment functions,
Kenichi Handa <handa@m17n.org>
parents:
19428
diff
changeset
|
102 (if (= ch ?z) ; ZW -> euc-china |
f510736ce3d1
In setup-LANGUAGE-environment functions,
Kenichi Handa <handa@m17n.org>
parents:
19428
diff
changeset
|
103 (progn |
f510736ce3d1
In setup-LANGUAGE-environment functions,
Kenichi Handa <handa@m17n.org>
parents:
19428
diff
changeset
|
104 (translate-region (point) end hz-set-msb-table) |
f510736ce3d1
In setup-LANGUAGE-environment functions,
Kenichi Handa <handa@m17n.org>
parents:
19428
diff
changeset
|
105 (goto-char end)) |
f510736ce3d1
In setup-LANGUAGE-environment functions,
Kenichi Handa <handa@m17n.org>
parents:
19428
diff
changeset
|
106 (if (search-forward hz-ascii-designnation |
f510736ce3d1
In setup-LANGUAGE-environment functions,
Kenichi Handa <handa@m17n.org>
parents:
19428
diff
changeset
|
107 (if decode-hz-line-continuation nil end) |
f510736ce3d1
In setup-LANGUAGE-environment functions,
Kenichi Handa <handa@m17n.org>
parents:
19428
diff
changeset
|
108 t) |
f510736ce3d1
In setup-LANGUAGE-environment functions,
Kenichi Handa <handa@m17n.org>
parents:
19428
diff
changeset
|
109 (delete-char -2)) |
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 (translate-region pos (point) hz-set-msb-table)))) |
f510736ce3d1
In setup-LANGUAGE-environment functions,
Kenichi Handa <handa@m17n.org>
parents:
19428
diff
changeset
|
112 (if beg |
f510736ce3d1
In setup-LANGUAGE-environment functions,
Kenichi Handa <handa@m17n.org>
parents:
19428
diff
changeset
|
113 (decode-coding-region beg end 'euc-china))) |
17052 | 114 (- (point-max) (point-min))))) |
115 | |
116 ;;;###autoload | |
117 (defun decode-hz-buffer () | |
118 "Decode HZ/ZW encoded text in the current buffer." | |
119 (interactive) | |
120 (decode-hz-region (point-min) (point-max))) | |
121 | |
122 ;;;###autoload | |
123 (defun encode-hz-region (beg end) | |
124 "Encode the text in the current region to HZ. | |
125 Return the length of resulting text." | |
126 (interactive "r") | |
127 (save-excursion | |
128 (save-restriction | |
129 (narrow-to-region beg end) | |
130 | |
131 ;; "~" -> "~~" | |
132 (goto-char (point-min)) | |
133 (while (search-forward "~" nil t) (insert ?~)) | |
134 | |
135 ;; Chinese GB2312 -> "~{...~}" | |
136 (goto-char (point-min)) | |
137 (if (re-search-forward "\\cc" nil t) | |
20838
3d67d591c066
(encode-hz-region): Do not bind
Kenichi Handa <handa@m17n.org>
parents:
20738
diff
changeset
|
138 (let (pos) |
17052 | 139 (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
|
140 (encode-coding-region pos (point-max) 'iso-2022-7bit) |
17052 | 141 (goto-char pos) |
142 (while (search-forward iso2022-gb-designation nil t) | |
143 (delete-char -3) | |
144 (insert hz-gb-designnation)) | |
145 (goto-char pos) | |
146 (while (search-forward iso2022-ascii-designation nil t) | |
147 (delete-char -3) | |
148 (insert hz-ascii-designnation)))) | |
149 (- (point-max) (point-min))))) | |
150 | |
151 ;;;###autoload | |
152 (defun encode-hz-buffer () | |
153 "Encode the text in the current buffer to HZ." | |
154 (interactive) | |
155 (encode-hz-region (point-min) (point-max))) | |
156 | |
157 ;; | |
18309
bd8b521f5218
Provide XXX-util instead of
Kenichi Handa <handa@m17n.org>
parents:
17993
diff
changeset
|
158 (provide 'china-util) |
17052 | 159 |
160 ;;; china-util.el ends here |