(Download): Renamed from "Anonymous FTP"; modify for
[elisp/apel.git] / mcs-xm.el
1 ;;; mcs-xm.el --- MIME charset implementation for XEmacs-mule
2
3 ;; Copyright (C) 1997,1998,1999,2000,2002,2010 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      ((ascii latin-jisx0201
132              japanese-jisx0208-1978 japanese-jisx0208)  . iso-2022-jp)
133      ;; ,(if (featurep 'utf-2000)
134      ;;      '((ascii latin-jisx0201
135      ;;               japanese-jisx0208-1978
136      ;;               japanese-jisx0208
137      ;;               japanese-jisx0208-1990
138      ;;               japanese-jisx0213-1
139      ;;               japanese-jisx0213-2)                 . iso-2022-jp-3)
140      ;;    '((ascii latin-jisx0201
141      ;;             japanese-jisx0208-1978 japanese-jisx0208
142      ;;             japanese-jisx0213-1
143      ;;             japanese-jisx0213-2)                   . iso-2022-jp-3))
144      ,(if (featurep 'utf-2000)
145           '((ascii latin-jisx0201 katakana-jisx0201
146                    japanese-jisx0208-1990)              . utf-8)
147         '((ascii latin-jisx0201
148                  katakana-jisx0201 japanese-jisx0208)   . shift_jis))
149      ((ascii korean-ksc5601)                            . euc-kr)
150      ((ascii chinese-gb2312)                            . gb2312)
151      ((ascii chinese-big5-1 chinese-big5-2)             . big5)
152      ((ascii thai-xtis)                                 . tis-620)
153      ;; ,(if (featurep 'utf-2000)
154      ;;      '((ascii latin-jisx0201 latin-iso8859-1
155      ;;               greek-iso8859-7
156      ;;               japanese-jisx0208-1978 japanese-jisx0208
157      ;;               japanese-jisx0208-1990
158      ;;               japanese-jisx0212
159      ;;               chinese-gb2312
160      ;;               korean-ksc5601)              . iso-2022-jp-2)
161      ;;    '((ascii latin-jisx0201 latin-iso8859-1
162      ;;             greek-iso8859-7
163      ;;             japanese-jisx0208-1978 japanese-jisx0208
164      ;;             japanese-jisx0212
165      ;;             chinese-gb2312
166      ;;             korean-ksc5601)                        . iso-2022-jp-2))
167      ;;((ascii latin-iso8859-1 greek-iso8859-7
168      ;;        latin-jisx0201 japanese-jisx0208-1978
169      ;;        chinese-gb2312 japanese-jisx0208
170      ;;        korean-ksc5601 japanese-jisx0212
171      ;;        chinese-cns11643-1 chinese-cns11643-2)      . iso-2022-int-1)
172      )))
173
174
175 (defun coding-system-to-mime-charset (coding-system)
176   "Convert CODING-SYSTEM to a MIME-charset.
177 Return nil if corresponding MIME-charset is not found."
178   (setq coding-system
179         (coding-system-name (coding-system-base coding-system)))
180   (or (car (rassq coding-system mime-charset-coding-system-alist))
181       coding-system))
182
183 (defun mime-charset-list ()
184   "Return a list of all existing MIME-charset."
185   (let ((dest (mapcar (function car) mime-charset-coding-system-alist))
186         (rest (coding-system-list))
187         cs)
188     (while rest
189       (setq cs (coding-system-name (coding-system-base (car rest))))
190       (or (rassq cs mime-charset-coding-system-alist)
191           (memq cs dest)
192           (setq dest (cons cs dest)))
193       (setq rest (cdr rest)))
194     dest))
195
196
197 ;;; @ end
198 ;;;
199
200 (require 'product)
201 (product-provide (provide 'mcs-xm) (require 'apel-ver))
202
203 ;;; mcs-xm.el ends here