(charsets-mime-charset-alist): Use `cn-gb' instead of `cn-gb-2312'.
[elisp/apel.git] / mcs-xm.el
1 ;;; mcs-xm.el --- MIME charset implementation for XEmacs-mule
2
3 ;; Copyright (C) 1997,1998 Free Software Foundation, Inc.
4
5 ;; Author: MORIOKA Tomohiko <morioka@jaist.ac.jp>
6 ;; Keywords: emulation, compatibility, Mule
7
8 ;; This file is part of APEL (A Portable Emacs Library).
9
10 ;; This program is free software; you can redistribute it and/or
11 ;; modify it under the terms of the GNU General Public License as
12 ;; published by the Free Software Foundation; either version 2, or (at
13 ;; your option) any later version.
14
15 ;; This program is distributed in the hope that it will be useful, but
16 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18 ;; 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 ;;; Commentary:
26
27 ;;    This module requires Emacs 20.0.93, XEmacs 20.3-b5 (with mule)
28 ;;    or later.
29
30 ;;; Code:
31
32 (require 'mcs-20)
33
34
35 (defun encode-mime-charset-region (start end charset)
36   "Encode the text between START and END as MIME CHARSET."
37   (let ((cs (mime-charset-to-coding-system charset)))
38     (if cs
39         (encode-coding-region start end cs)
40       )))
41
42
43 (defcustom mime-charset-decoder-alist
44   '((iso-2022-jp . decode-mime-charset-region-with-iso646-unification)
45     (iso-2022-jp-2 . decode-mime-charset-region-with-iso646-unification)
46     (x-ctext . decode-mime-charset-region-with-iso646-unification)
47     (hz-gb-2312 . decode-mime-charset-region-for-hz)
48     (t . decode-mime-charset-region-default))
49   "Alist MIME-charset vs. decoder function."
50   :group 'i18n
51   :type '(repeat (cons mime-charset function)))
52
53 (defsubst decode-mime-charset-region-default (start end charset lbt)
54   (let ((cs (mime-charset-to-coding-system charset lbt)))
55     (if cs
56         (decode-coding-region start end cs)
57       )))
58
59 (defcustom mime-iso646-character-unification-alist
60   `,(let (dest
61           (i 33))
62       (while (< i 92)
63         (setq dest
64               (cons (cons (char-to-string (make-char 'latin-jisx0201 i))
65                           (format "%c" i))
66                     dest))
67         (setq i (1+ i)))
68       (setq i 93)
69       (while (< i 126)
70         (setq dest
71               (cons (cons (char-to-string (make-char 'latin-jisx0201 i))
72                           (format "%c" i))
73                     dest))
74         (setq i (1+ i)))
75       (nreverse dest))
76   "Alist unified string vs. canonical string."
77   :group 'i18n
78   :type '(repeat (cons string string)))
79
80 (defcustom mime-unified-character-face nil
81   "*Face of unified character."
82   :group 'i18n
83   :type 'face)
84
85 (defcustom mime-character-unification-limit-size 2048
86   "*Limit size to unify characters."
87   :group 'i18n
88   :type 'integer)
89
90 (defun decode-mime-charset-region-with-iso646-unification (start end charset
91                                                                  lbt)
92   (decode-mime-charset-region-default start end charset lbt)
93   (if (<= (- end start) mime-character-unification-limit-size)
94       (save-excursion
95         (let ((rest mime-iso646-character-unification-alist))
96           (while rest
97             (let ((pair (car rest)))
98               (goto-char (point-min))
99               (while (search-forward (car pair) nil t)
100                 (let ((str (cdr pair)))
101                   (put-text-property 0 (length str)
102                                      'face mime-unified-character-face str)
103                   (replace-match str 'fixed-case 'literal)
104                   )
105                 ))
106             (setq rest (cdr rest)))))
107     ))
108
109 (defun decode-mime-charset-region-for-hz (start end charset lbt)
110   (if lbt
111       (save-restriction
112         (narrow-to-region start end)
113         (decode-coding-region (point-min)(point-max)
114                               (mime-charset-to-coding-system 'raw-text lbt))
115         (decode-hz-region (point-min)(point-max)))
116     (decode-hz-region start end)))
117
118 (defun decode-mime-charset-region (start end charset &optional lbt)
119   "Decode the text between START and END as MIME CHARSET."
120   (if (stringp charset)
121       (setq charset (intern (downcase charset)))
122     )
123   (let ((func (cdr (or (assq charset mime-charset-decoder-alist)
124                        (assq t mime-charset-decoder-alist)))))
125     (funcall func start end charset lbt)))
126
127 (defsubst encode-mime-charset-string (string charset)
128   "Encode the STRING as MIME CHARSET."
129   (let ((cs (mime-charset-to-coding-system charset)))
130     (if cs
131         (encode-coding-string string cs)
132       string)))
133
134 ;; (defsubst decode-mime-charset-string (string charset)
135 ;;   "Decode the STRING as MIME CHARSET."
136 ;;   (let ((cs (mime-charset-to-coding-system charset)))
137 ;;     (if cs
138 ;;         (decode-coding-string string cs)
139 ;;       string)))
140 (defun decode-mime-charset-string (string charset &optional lbt)
141   "Decode the STRING as MIME CHARSET."
142   (with-temp-buffer
143     (insert string)
144     (decode-mime-charset-region (point-min)(point-max) charset lbt)
145     (buffer-string)))
146
147
148 (defvar charsets-mime-charset-alist
149   '(((ascii)                                            . us-ascii)
150     ((ascii latin-iso8859-1)                            . iso-8859-1)
151     ((ascii latin-iso8859-2)                            . iso-8859-2)
152     ((ascii latin-iso8859-3)                            . iso-8859-3)
153     ((ascii latin-iso8859-4)                            . iso-8859-4)
154     ((ascii cyrillic-iso8859-5)                         . iso-8859-5)
155 ;;; ((ascii cyrillic-iso8859-5)                         . koi8-r)
156     ((ascii arabic-iso8859-6)                           . iso-8859-6)
157     ((ascii greek-iso8859-7)                            . iso-8859-7)
158     ((ascii hebrew-iso8859-8)                           . iso-8859-8)
159     ((ascii latin-iso8859-9)                            . iso-8859-9)
160     ((ascii latin-jisx0201
161             japanese-jisx0208-1978 japanese-jisx0208)   . iso-2022-jp)
162     ((ascii latin-jisx0201
163             katakana-jisx0201 japanese-jisx0208)        . shift_jis)
164     ((ascii korean-ksc5601)                             . euc-kr)
165     ((ascii chinese-gb2312)                             . cn-gb)
166     ((ascii chinese-big5-1 chinese-big5-2)              . cn-big5)
167     ((ascii latin-iso8859-1 greek-iso8859-7
168             latin-jisx0201 japanese-jisx0208-1978
169             chinese-gb2312 japanese-jisx0208
170             korean-ksc5601 japanese-jisx0212)           . iso-2022-jp-2)
171     ((ascii latin-iso8859-1 greek-iso8859-7
172             latin-jisx0201 japanese-jisx0208-1978
173             chinese-gb2312 japanese-jisx0208
174             korean-ksc5601 japanese-jisx0212
175             chinese-cns11643-1 chinese-cns11643-2)      . iso-2022-int-1)
176     ))
177
178
179 ;;; @ end
180 ;;;
181
182 (provide 'mcs-xm)
183
184 ;;; mcs-xm.el ends here