]> code.delx.au - gnu-emacs/blob - lisp/language/china-util.el
(thai-pre-write-conversion): Use with-temp-buffer.
[gnu-emacs] / lisp / language / china-util.el
1 ;;; china-util.el --- utilities for Chinese
2
3 ;; Copyright (C) 1995 Electrotechnical Laboratory, JAPAN.
4 ;; Licensed to the Free Software Foundation.
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
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.
24
25 ;;; Code:
26
27 ;;;###autoload
28 (defun setup-chinese-gb-environment ()
29 "Setup multilingual environment (MULE) for Chinese GB2312 users."
30 (interactive)
31 (set-language-environment "Chinese-GB"))
32
33 ;;;###autoload
34 (defun setup-chinese-big5-environment ()
35 "Setup multilingual environment (MULE) for Chinese Big5 users."
36 (interactive)
37 (set-language-environment "Chinese-BIG5"))
38
39 ;;;###autoload
40 (defun setup-chinese-cns-environment ()
41 "Setup multilingual environment (MULE) for Chinese CNS11643 family users."
42 (interactive)
43 (set-language-environment "Chinese-CNS"))
44
45 ;; Hz/ZW encoding stuffs
46
47 ;; HZ is an encoding method for Chinese character set GB2312 used
48 ;; widely in Internet. It is very similar to 7-bit environment of
49 ;; ISO-2022. The difference is that HZ uses the sequence "~{" and
50 ;; "~}" for designating GB2312 and ASCII respectively, hence, it
51 ;; doesn't uses ESC (0x1B) code.
52
53 ;; ZW is another encoding method for Chinese character set GB2312. It
54 ;; encodes Chinese characters line by line by starting each line with
55 ;; the sequence "zW". It also uses only 7-bit as HZ.
56
57 ;; ISO-2022 escape sequence to designate GB2312.
58 (defvar iso2022-gb-designation "\e$A")
59 ;; HZ escape sequence to designate GB2312.
60 (defvar hz-gb-designnation "~{")
61 ;; ISO-2022 escape sequence to designate ASCII.
62 (defvar iso2022-ascii-designation "\e(B")
63 ;; HZ escape sequence to designate ASCII.
64 (defvar hz-ascii-designnation "~}")
65 ;; Regexp of ZW sequence to start GB2312.
66 (defvar zw-start-gb "^zW")
67 ;; Regexp for start of GB2312 in an encoding mixture of HZ and ZW.
68 (defvar hz/zw-start-gb
69 (concat hz-gb-designnation "\\|" zw-start-gb "\\|[^\0-\177]"))
70
71 (defvar decode-hz-line-continuation nil
72 "Flag to tell if we should care line continuation convention of Hz.")
73
74 (defconst hz-set-msb-table
75 (let ((str (make-string 127 0))
76 (i 0))
77 (while (< i 33)
78 (aset str i i)
79 (setq i (1+ i)))
80 (while (< i 127)
81 (aset str i (+ i 128))
82 (setq i (1+ i)))
83 str))
84
85 ;;;###autoload
86 (defun decode-hz-region (beg end)
87 "Decode HZ/ZW encoded text in the current region.
88 Return the length of resulting text."
89 (interactive "r")
90 (save-excursion
91 (save-restriction
92 (let (pos ch)
93 (narrow-to-region beg end)
94
95 ;; We, at first, convert HZ/ZW to `euc-china',
96 ;; then decode it.
97
98 ;; "~\n" -> "\n", "~~" -> "~"
99 (goto-char (point-min))
100 (while (search-forward "~" nil t)
101 (setq ch (following-char))
102 (if (or (= ch ?\n) (= ch ?~)) (delete-char -1)))
103
104 ;; "^zW...\n" -> Chinese GB2312
105 ;; "~{...~}" -> Chinese GB2312
106 (goto-char (point-min))
107 (setq beg nil)
108 (while (re-search-forward hz/zw-start-gb nil t)
109 (setq pos (match-beginning 0)
110 ch (char-after pos))
111 ;; Record the first position to start conversion.
112 (or beg (setq beg pos))
113 (end-of-line)
114 (setq end (point))
115 (if (>= ch 128) ; 8bit GB2312
116 nil
117 (goto-char pos)
118 (delete-char 2)
119 (setq end (- end 2))
120 (if (= ch ?z) ; ZW -> euc-china
121 (progn
122 (translate-region (point) end hz-set-msb-table)
123 (goto-char end))
124 (if (search-forward hz-ascii-designnation
125 (if decode-hz-line-continuation nil end)
126 t)
127 (delete-char -2))
128 (setq end (point))
129 (translate-region pos (point) hz-set-msb-table))))
130 (if beg
131 (decode-coding-region beg end 'euc-china)))
132 (- (point-max) (point-min)))))
133
134 ;;;###autoload
135 (defun decode-hz-buffer ()
136 "Decode HZ/ZW encoded text in the current buffer."
137 (interactive)
138 (decode-hz-region (point-min) (point-max)))
139
140 ;;;###autoload
141 (defun encode-hz-region (beg end)
142 "Encode the text in the current region to HZ.
143 Return the length of resulting text."
144 (interactive "r")
145 (save-excursion
146 (save-restriction
147 (narrow-to-region beg end)
148
149 ;; "~" -> "~~"
150 (goto-char (point-min))
151 (while (search-forward "~" nil t) (insert ?~))
152
153 ;; Chinese GB2312 -> "~{...~}"
154 (goto-char (point-min))
155 (if (re-search-forward "\\cc" nil t)
156 (let (pos)
157 (goto-char (setq pos (match-beginning 0)))
158 (encode-coding-region pos (point-max) 'iso-2022-7bit)
159 (goto-char pos)
160 (while (search-forward iso2022-gb-designation nil t)
161 (delete-char -3)
162 (insert hz-gb-designnation))
163 (goto-char pos)
164 (while (search-forward iso2022-ascii-designation nil t)
165 (delete-char -3)
166 (insert hz-ascii-designnation))))
167 (- (point-max) (point-min)))))
168
169 ;;;###autoload
170 (defun encode-hz-buffer ()
171 "Encode the text in the current buffer to HZ."
172 (interactive)
173 (encode-hz-region (point-min) (point-max)))
174
175 ;;
176 (provide 'china-util)
177
178 ;;; china-util.el ends here