Remove non-free old and crusty clearcase pkg
[packages] / mule-packages / mule-base / china-util.el
1 ;;; china-util.el --- utilities for Chinese
2
3 ;; Copyright (C) 1995,1999 Electrotechnical Laboratory, JAPAN.
4 ;; Licensed to the Free Software Foundation.
5 ;; Copyright (C) 1997 MORIOKA Tomohiko
6
7 ;; Keywords: mule, multilingual, Chinese
8
9 ;; This file is part of XEmacs.
10
11 ;; XEmacs is free software; you can redistribute it and/or modify it
12 ;; 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 ;; XEmacs is distributed in the hope that it will be useful, but
17 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19 ;; General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with XEmacs; see the file COPYING.  If not, write to the Free
23 ;; Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
24 ;; 02111-1307, USA.
25
26 ;;; Code:
27
28 ;;;###autoload
29 (defun setup-chinese-gb-environment-internal ()
30   (when (featurep 'egg)
31     (setq-default its:*current-map* (its:get-mode-map "PinYin")))
32   )
33
34 ;;;###autoload
35 (defun setup-chinese-gb-environment ()
36   "Setup multilingual environment (MULE) for Chinese GB2312 users."
37   (interactive)
38   (set-language-environment "Chinese-GB"))
39
40 ;;;###autoload
41 (defun setup-chinese-big5-environment ()
42   "Setup multilingual environment (MULE) for Chinese Big5 users."
43   (interactive)
44   (set-language-environment "Chinese-BIG5"))
45
46 ;; ;;;###autoload
47 ;; (defun setup-chinese-cns-environment ()
48 ;;   "Setup multilingual environment (MULE) for Chinese CNS11643 family users."
49 ;;   (interactive)
50 ;;   (set-language-environment "Chinese-CNS"))
51
52 ;; Hz/ZW encoding stuffs
53
54 ;; HZ is an encoding method for Chinese character set GB2312 used
55 ;; widely in Internet.  It is very similar to 7-bit environment of
56 ;; ISO-2022.  The difference is that HZ uses the sequence "~{" and
57 ;; "~}" for designating GB2312 and ASCII respectively, hence, it
58 ;; doesn't uses ESC (0x1B) code.
59
60 ;; ZW is another encoding method for Chinese character set GB2312.  It
61 ;; encodes Chinese characters line by line by starting each line with
62 ;; the sequence "zW".  It also uses only 7-bit as HZ.
63
64 ;; ISO-2022 escape sequence to designate GB2312.
65 (defvar iso2022-gb-designation "\e$A")
66 ;; HZ escape sequence to designate GB2312.
67 (defvar hz-gb-designnation "~{")
68 ;; ISO-2022 escape sequence to designate ASCII.
69 (defvar iso2022-ascii-designation "\e(B")
70 ;; HZ escape sequence to designate ASCII.
71 (defvar hz-ascii-designnation "~}")
72 ;; Regexp of ZW sequence to start GB2312.
73 (defvar zw-start-gb "^zW")
74 ;; Regexp for start of GB2312 in an encoding mixture of HZ and ZW.
75 (defvar hz/zw-start-gb
76   (concat hz-gb-designnation "\\|" zw-start-gb "\\|[^\0-\177]"))
77
78 (defvar decode-hz-line-continuation nil
79   "Flag to tell if we should care line continuation convention of Hz.")
80
81 (defconst hz-set-msb-table
82   (let ((str (make-string 127 0))
83         (i 0))
84     (while (< i 33)
85       (aset str i i)
86       (setq i (1+ i)))
87     (while (< i 127)
88       (aset str i (+ i 128))
89       (setq i (1+ i)))
90     str))
91
92 ;;;###autoload
93 (defun decode-hz-region (beg end)
94   "Decode HZ/ZW encoded text in the current region.
95 Return the length of resulting text."
96   (interactive "r")
97   (save-excursion
98     (save-restriction
99       (let (pos ch)
100         (narrow-to-region beg end)
101
102         ;; We, at first, convert HZ/ZW to `cn-gb-2312',
103         ;; then decode it.
104
105         ;; "~\n" -> "\n", "~~" -> "~"
106         (goto-char (point-min))
107         (while (search-forward "~" nil t)
108           (setq ch (following-char))
109           (if (or (= ch ?\n) (= ch ?~)) (delete-char -1)))
110
111         ;; "^zW...\n" -> Chinese GB2312
112         ;; "~{...~}"  -> Chinese GB2312
113         (goto-char (point-min))
114         (setq beg nil)
115         (while (re-search-forward hz/zw-start-gb nil t)
116           (setq pos (match-beginning 0)
117                 ch (char-after pos))
118           ;; Record the first position to start conversion.
119           (or beg (setq beg pos))
120           (end-of-line)
121           (setq end (point))
122           (if (>= ch 128)               ; 8bit GB2312
123               nil
124             (goto-char pos)
125             (delete-char 2)
126             (setq end (- end 2))
127             (if (= ch ?z)                       ; ZW -> cn-gb-2312
128                 (progn
129                   (translate-region (point) end hz-set-msb-table)
130                   (goto-char end))
131               (if (search-forward hz-ascii-designnation
132                                   (if decode-hz-line-continuation nil end)
133                                   t)
134                   (delete-char -2))
135               (setq end (point))
136               (translate-region pos (point) hz-set-msb-table))))
137         (if beg
138             (decode-coding-region beg end 'cn-gb-2312)))
139       (- (point-max) (point-min)))))
140
141 ;;;###autoload
142 (defun decode-hz-buffer ()
143   "Decode HZ/ZW encoded text in the current buffer."
144   (interactive)
145   (decode-hz-region (point-min) (point-max)))
146
147 ;;;###autoload
148 (defun encode-hz-region (beg end)
149   "Encode the text in the current region to HZ.
150 Return the length of resulting text."
151   (interactive "r")
152   (save-excursion
153     (save-restriction
154       (narrow-to-region beg end)
155
156       ;; "~" -> "~~"
157       (goto-char (point-min))
158       (while (search-forward "~" nil t) (insert ?~))
159
160       ;; Chinese GB2312 -> "~{...~}"
161       (goto-char (point-min))
162       (if (re-search-forward "\\cc" nil t)
163           (let (pos)
164             (goto-char (setq pos (match-beginning 0)))
165             (encode-coding-region pos (point-max) 'iso-2022-7bit)
166             (goto-char pos)
167             (while (search-forward iso2022-gb-designation nil t)
168               (delete-char -3)
169               (insert hz-gb-designnation))
170             (goto-char pos)
171             (while (search-forward iso2022-ascii-designation nil t)
172               (delete-char -3)
173               (insert hz-ascii-designnation))))
174       (- (point-max) (point-min)))))
175
176 ;;;###autoload
177 (defun encode-hz-buffer ()
178   "Encode the text in the current buffer to HZ."
179   (interactive)
180   (encode-hz-region (point-min) (point-max)))
181
182 ;;
183 (provide 'china-util)
184
185 ;;; china-util.el ends here