Initial revision
[bpt/emacs.git] / lisp / language / china-util.el
1 ;; china-util.el -- utilities for Chinese
2
3 ;; Copyright (C) 1995 Free Software Foundation, Inc.
4 ;; Copyright (C) 1995 Electrotechnical Laboratory, JAPAN.
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
22 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
23
24 ;;; Code:
25
26 ;; Hz/ZW encoding stuffs
27
28 ;; HZ is an encoding method for Chinese character set GB2312 used
29 ;; widely in Internet. It is very similar to 7-bit environment of
30 ;; ISO-2022. The difference is that HZ uses the sequence "~{" and
31 ;; "~}" for designating GB2312 and ASCII respectively, hence, it
32 ;; doesn't uses ESC (0x1B) code.
33
34 ;; ZW is another encoding method for Chinese character set GB2312. It
35 ;; encodes Chinese characters line by line by starting each line with
36 ;; the sequence "zW". It also uses only 7-bit as HZ.
37
38 ;; ISO-2022 escape sequence to designate GB2312.
39 (defvar iso2022-gb-designation "\e$A")
40 ;; HZ escape sequence to designate GB2312.
41 (defvar hz-gb-designnation "~{")
42 ;; ISO-2022 escape sequence to designate ASCII.
43 (defvar iso2022-ascii-designation "\e(B")
44 ;; HZ escape sequence to designate ASCII.
45 (defvar hz-ascii-designnation "~}")
46 ;; Regexp of ZW sequence to start GB2312.
47 (defvar zw-start-gb "^zW")
48 ;; Regexp for start of GB2312 in an encoding mixture of HZ and ZW.
49 (defvar hz/zw-start-gb (concat hz-gb-designnation "\\|" zw-start-gb))
50
51 (defvar decode-hz-line-continuation nil
52 "Flag to tell if we should care line continuation convention of Hz.")
53
54 ;;;###autoload
55 (defun decode-hz-region (beg end)
56 "Decode HZ/ZW encoded text in the current region.
57 Return the length of resulting text."
58 (interactive "r")
59 (save-excursion
60 (save-restriction
61 (narrow-to-region beg end)
62
63 ;; We, at first, convert HZ/ZW to `coding-system-iso-2022-7',
64 ;; then decode it.
65
66 ;; "~\n" -> "\n"
67 (goto-char (point-min))
68 (while (search-forward "~" nil t)
69 (if (= (following-char) ?\n) (delete-char -1))
70 (if (not (eobp)) (forward-char 1)))
71
72 ;; "^zW...\n" -> Chinese GB2312
73 ;; "~{...~}" -> Chinese GB2312
74 (goto-char (point-min))
75 (let ((chinese-found nil))
76 (while (re-search-forward hz/zw-start-gb nil t)
77 (if (= (char-after (match-beginning 0)) ?z)
78 ;; ZW -> coding-system-iso-20227-7
79 (progn
80 (delete-char -2)
81 (insert iso2022-gb-designation)
82 (end-of-line)
83 (insert iso2022-ascii-designation))
84 ;; HZ -> coding-system-iso-20227-7
85 (delete-char -2)
86 (insert iso2022-gb-designation)
87 (let ((pos (save-excursion (end-of-line) (point))))
88 (if (search-forward hz-ascii-designnation pos t)
89 (replace-match iso2022-ascii-designation)
90 (if (not decode-hz-line-continuation)
91 (insert iso2022-ascii-designation)))))
92 (setq chinese-found t))
93 (if (or chinese-found
94 (let ((enable-multibyte-characters nil))
95 ;; Here we check if the text contains EUC (China) codes.
96 ;; If any, we had better decode them also.
97 (goto-char (point-min))
98 (re-search-forward "[\240-\377]" nil t)))
99 (decode-coding-region (point-min) (point-max)
100 'coding-system-euc-china)))
101
102 ;; "~~" -> "~"
103 (goto-char (point-min))
104 (while (search-forward "~~" nil t) (delete-char -1))
105 (- (point-max) (point-min)))))
106
107 ;;;###autoload
108 (defun decode-hz-buffer ()
109 "Decode HZ/ZW encoded text in the current buffer."
110 (interactive)
111 (decode-hz-region (point-min) (point-max)))
112
113 ;;;###autoload
114 (defun encode-hz-region (beg end)
115 "Encode the text in the current region to HZ.
116 Return the length of resulting text."
117 (interactive "r")
118 (save-excursion
119 (save-restriction
120 (narrow-to-region beg end)
121
122 ;; "~" -> "~~"
123 (goto-char (point-min))
124 (while (search-forward "~" nil t) (insert ?~))
125
126 ;; Chinese GB2312 -> "~{...~}"
127 (goto-char (point-min))
128 (if (re-search-forward "\\cc" nil t)
129 (let ((enable-multibyte-characters nil)
130 pos)
131 (goto-char (setq pos (match-beginning 0)))
132 (encode-coding-region pos (point-max) 'coding-system-iso-2022-7)
133 (goto-char pos)
134 (while (search-forward iso2022-gb-designation nil t)
135 (delete-char -3)
136 (insert hz-gb-designnation))
137 (goto-char pos)
138 (while (search-forward iso2022-ascii-designation nil t)
139 (delete-char -3)
140 (insert hz-ascii-designnation))))
141 (- (point-max) (point-min)))))
142
143 ;;;###autoload
144 (defun encode-hz-buffer ()
145 "Encode the text in the current buffer to HZ."
146 (interactive)
147 (encode-hz-region (point-min) (point-max)))
148
149 ;;
150 (provide 'language/china-util)
151
152 ;;; Local Variables:
153 ;;; generated-autoload-file: "../loaddefs.el"
154 ;;; End:
155 ;;; china-util.el ends here