(apel-ver): Change APEL version to 10.8.
[elisp/apel.git] / mcs-xm.el
1 ;;; mcs-xm.el --- MIME charset implementation for XEmacs-mule
2
3 ;; Copyright (C) 1997,1998,1999,2000 Free Software Foundation, Inc.
4
5 ;; Author: MORIOKA Tomohiko <tomo@m17n.org>
6 ;; Keywords: MIME-charset, coding-system, 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., 51 Franklin Street, Fifth Floor,
23 ;; Boston, MA 02110-1301, 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 'poem)
33
34
35 (defun encode-mime-charset-region (start end charset &optional lbt)
36   "Encode the text between START and END as MIME CHARSET."
37   (let ((cs (mime-charset-to-coding-system charset lbt)))
38     (if cs
39         (encode-coding-region start end cs)
40       )))
41
42
43 (defcustom mime-charset-decoder-alist
44   (let ((alist
45          '((hz-gb-2312 . decode-mime-charset-region-for-hz)
46            (t . decode-mime-charset-region-default))))
47     (if (featurep 'utf-2000)
48         alist
49       (list*
50        '(iso-2022-jp . decode-mime-charset-region-with-iso646-unification)
51        '(iso-2022-jp-2 . decode-mime-charset-region-with-iso646-unification)
52        alist)))
53   "Alist MIME-charset vs. decoder function."
54   :group 'i18n
55   :type '(repeat (cons mime-charset function)))
56
57 (defsubst decode-mime-charset-region-default (start end charset lbt)
58   (let ((cs (mime-charset-to-coding-system charset lbt)))
59     (if cs
60         (decode-coding-region start end cs)
61       )))
62
63 (unless (featurep 'utf-2000)
64   (require 'mcs-xmu))
65
66 (defun decode-mime-charset-region-for-hz (start end charset lbt)
67   (if lbt
68       (save-restriction
69         (narrow-to-region start end)
70         (decode-coding-region (point-min)(point-max)
71                               (mime-charset-to-coding-system 'raw-text lbt))
72         (decode-hz-region (point-min)(point-max)))
73     (decode-hz-region start end)))
74
75 (defun decode-mime-charset-region (start end charset &optional lbt)
76   "Decode the text between START and END as MIME CHARSET."
77   (if (stringp charset)
78       (setq charset (intern (downcase charset)))
79     )
80   (let ((func (cdr (or (assq charset mime-charset-decoder-alist)
81                        (assq t mime-charset-decoder-alist)))))
82     (funcall func start end charset lbt)))
83
84 (defun encode-mime-charset-string (string charset &optional lbt)
85   "Encode the STRING as MIME CHARSET."
86   (let ((cs (mime-charset-to-coding-system charset lbt)))
87     (if cs
88         (encode-coding-string string cs)
89       string)))
90
91 ;; (defsubst decode-mime-charset-string (string charset)
92 ;;   "Decode the STRING as MIME CHARSET."
93 ;;   (let ((cs (mime-charset-to-coding-system charset)))
94 ;;     (if cs
95 ;;         (decode-coding-string string cs)
96 ;;       string)))
97 (defun decode-mime-charset-string (string charset &optional lbt)
98   "Decode the STRING as MIME CHARSET."
99   (with-temp-buffer
100     (insert string)
101     (decode-mime-charset-region (point-min)(point-max) charset lbt)
102     (buffer-string)))
103
104
105 (defvar charsets-mime-charset-alist
106   (delq
107    nil
108    `(((ascii)                                           . us-ascii)
109      ((ascii latin-iso8859-1)                           . iso-8859-1)
110      ((ascii latin-iso8859-2)                           . iso-8859-2)
111      ((ascii latin-iso8859-3)                           . iso-8859-3)
112      ((ascii latin-iso8859-4)                           . iso-8859-4)
113      ((ascii cyrillic-iso8859-5)                        . iso-8859-5)
114      ;;((ascii cyrillic-iso8859-5)                      . koi8-r)
115      ((ascii arabic-iso8859-6)                          . iso-8859-6)
116      ((ascii greek-iso8859-7)                           . iso-8859-7)
117      ((ascii hebrew-iso8859-8)                          . iso-8859-8)
118      ((ascii latin-iso8859-9)                           . iso-8859-9)
119      ,(if (find-coding-system 'iso-8859-14)
120           '((ascii latin-iso8859-14)                    . iso-8859-14))
121      ,(if (find-coding-system 'iso-8859-15)
122           '((ascii latin-iso8859-15)                    . iso-8859-15))
123      ,(if (featurep 'utf-2000)
124           '((ascii latin-jisx0201
125                    japanese-jisx0208-1978
126                    japanese-jisx0208
127                    japanese-jisx0208-1990)              . iso-2022-jp)
128         '((ascii latin-jisx0201
129                  japanese-jisx0208-1978 japanese-jisx0208)
130           . iso-2022-jp))
131      ,(if (featurep 'utf-2000)
132           '((ascii latin-jisx0201
133                    japanese-jisx0208-1978
134                    japanese-jisx0208
135                    japanese-jisx0208-1990
136                    japanese-jisx0213-1
137                    japanese-jisx0213-2)                 . iso-2022-jp-3)
138         '((ascii latin-jisx0201
139                  japanese-jisx0208-1978 japanese-jisx0208
140                  japanese-jisx0213-1
141                  japanese-jisx0213-2)                   . iso-2022-jp-3))
142      ,(if (featurep 'utf-2000)
143           '((ascii latin-jisx0201 katakana-jisx0201
144                    japanese-jisx0208-1990)              . shift_jis)
145         '((ascii latin-jisx0201
146                  katakana-jisx0201 japanese-jisx0208)   . shift_jis))
147      ((ascii korean-ksc5601)                            . euc-kr)
148      ((ascii chinese-gb2312)                            . gb2312)
149      ((ascii chinese-big5-1 chinese-big5-2)             . big5)
150      ((ascii thai-xtis)                                 . tis-620)
151      ,(if (featurep 'utf-2000)
152           '((ascii latin-jisx0201 latin-iso8859-1
153                    greek-iso8859-7
154                    japanese-jisx0208-1978 japanese-jisx0208
155                    japanese-jisx0208-1990
156                    japanese-jisx0212
157                    chinese-gb2312
158                    korean-ksc5601)              . iso-2022-jp-2)
159         '((ascii latin-jisx0201 latin-iso8859-1
160                  greek-iso8859-7
161                  japanese-jisx0208-1978 japanese-jisx0208
162                  japanese-jisx0212
163                  chinese-gb2312
164                  korean-ksc5601)                        . iso-2022-jp-2))
165      ;;((ascii latin-iso8859-1 greek-iso8859-7
166      ;;        latin-jisx0201 japanese-jisx0208-1978
167      ;;        chinese-gb2312 japanese-jisx0208
168      ;;        korean-ksc5601 japanese-jisx0212
169      ;;        chinese-cns11643-1 chinese-cns11643-2)      . iso-2022-int-1)
170      )))
171
172
173 (defun coding-system-to-mime-charset (coding-system)
174   "Convert CODING-SYSTEM to a MIME-charset.
175 Return nil if corresponding MIME-charset is not found."
176   (setq coding-system
177         (coding-system-name (coding-system-base coding-system)))
178   (or (car (rassq coding-system mime-charset-coding-system-alist))
179       coding-system))
180
181 (defun mime-charset-list ()
182   "Return a list of all existing MIME-charset."
183   (let ((dest (mapcar (function car) mime-charset-coding-system-alist))
184         (rest (coding-system-list))
185         cs)
186     (while rest
187       (setq cs (coding-system-name (coding-system-base (car rest))))
188       (or (rassq cs mime-charset-coding-system-alist)
189           (memq cs dest)
190           (setq dest (cons cs dest)))
191       (setq rest (cdr rest)))
192     dest))
193
194
195 ;;; @ end
196 ;;;
197
198 (require 'product)
199 (product-provide (provide 'mcs-xm) (require 'apel-ver))
200
201 ;;; mcs-xm.el ends here