824582ffb0a07c605ff1566c00a676678388a2df
[elisp/apel.git] / mcs-e20.el
1 ;;; mcs-e20.el --- MIME charset implementation for Emacs 20.1 and 20.2
2
3 ;; Copyright (C) 1996,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.1 and 20.2.
28
29 ;;; Code:
30
31 (defsubst encode-mime-charset-region (start end charset &optional lbt)
32   "Encode the text between START and END as MIME CHARSET."
33   (let (cs)
34     (if (and enable-multibyte-characters
35              (setq cs (mime-charset-to-coding-system charset lbt)))
36         (encode-coding-region start end cs)
37       )))
38
39 (defsubst decode-mime-charset-region (start end charset &optional lbt)
40   "Decode the text between START and END as MIME CHARSET."
41   (let (cs)
42     (if (and enable-multibyte-characters
43              (setq cs (mime-charset-to-coding-system charset lbt)))
44         (decode-coding-region start end cs)
45       )))
46
47
48 (defsubst encode-mime-charset-string (string charset &optional lbt)
49   "Encode the STRING as MIME CHARSET."
50   (let (cs)
51     (if (and enable-multibyte-characters
52              (setq cs (mime-charset-to-coding-system charset lbt)))
53         (encode-coding-string string cs)
54       string)))
55
56 (defsubst decode-mime-charset-string (string charset &optional lbt)
57   "Decode the STRING as MIME CHARSET."
58   (let (cs)
59     (if (and enable-multibyte-characters
60              (setq cs (mime-charset-to-coding-system charset lbt)))
61         (decode-coding-string string cs)
62       string)))
63
64
65 (defvar charsets-mime-charset-alist
66   '(((ascii)                                            . us-ascii)
67     ((ascii latin-iso8859-1)                            . iso-8859-1)
68     ((ascii latin-iso8859-2)                            . iso-8859-2)
69     ((ascii latin-iso8859-3)                            . iso-8859-3)
70     ((ascii latin-iso8859-4)                            . iso-8859-4)
71 ;;; ((ascii cyrillic-iso8859-5)                         . iso-8859-5)
72     ((ascii cyrillic-iso8859-5)                         . koi8-r)
73     ((ascii arabic-iso8859-6)                           . iso-8859-6)
74     ((ascii greek-iso8859-7)                            . iso-8859-7)
75     ((ascii hebrew-iso8859-8)                           . iso-8859-8)
76     ((ascii latin-iso8859-9)                            . iso-8859-9)
77     ((ascii latin-jisx0201
78             japanese-jisx0208-1978 japanese-jisx0208)   . iso-2022-jp)
79     ((ascii latin-jisx0201
80             katakana-jisx0201 japanese-jisx0208)        . shift_jis)
81     ((ascii korean-ksc5601)                             . euc-kr)
82     ((ascii chinese-gb2312)                             . gb2312)
83     ((ascii chinese-big5-1 chinese-big5-2)              . big5)
84     ((ascii latin-iso8859-1 greek-iso8859-7
85             latin-jisx0201 japanese-jisx0208-1978
86             chinese-gb2312 japanese-jisx0208
87             korean-ksc5601 japanese-jisx0212)           . iso-2022-jp-2)
88 ;     ((ascii latin-iso8859-1 greek-iso8859-7
89 ;           latin-jisx0201 japanese-jisx0208-1978
90 ;           chinese-gb2312 japanese-jisx0208
91 ;           korean-ksc5601 japanese-jisx0212
92 ;           chinese-cns11643-1 chinese-cns11643-2)      . iso-2022-int-1)
93 ;     ((ascii latin-iso8859-1 latin-iso8859-2
94 ;           cyrillic-iso8859-5 greek-iso8859-7
95 ;           latin-jisx0201 japanese-jisx0208-1978
96 ;           chinese-gb2312 japanese-jisx0208
97 ;           korean-ksc5601 japanese-jisx0212
98 ;           chinese-cns11643-1 chinese-cns11643-2
99 ;           chinese-cns11643-3 chinese-cns11643-4
100 ;           chinese-cns11643-5 chinese-cns11643-6
101 ;           chinese-cns11643-7)                         . iso-2022-int-1)
102     ))
103
104 (defun-maybe coding-system-get (coding-system prop)
105   "Extract a value from CODING-SYSTEM's property list for property PROP."
106   (plist-get (coding-system-plist coding-system) prop)
107   )
108
109 (defun coding-system-to-mime-charset (coding-system)
110   "Convert CODING-SYSTEM to a MIME-charset.
111 Return nil if corresponding MIME-charset is not found."
112   (or (car (rassq coding-system mime-charset-coding-system-alist))
113       (coding-system-get coding-system 'mime-charset)
114       ))
115
116 (defun-maybe-cond mime-charset-list ()
117   "Return a list of all existing MIME-charset."
118   ((boundp 'coding-system-list)
119    (let ((dest (mapcar (function car) mime-charset-coding-system-alist))
120          (rest coding-system-list)
121          cs)
122      (while rest
123        (setq cs (car rest))
124        (unless (rassq cs mime-charset-coding-system-alist)
125          (if (setq cs (coding-system-get cs 'mime-charset))
126              (or (rassq cs mime-charset-coding-system-alist)
127                  (memq cs dest)  
128                  (setq dest (cons cs dest))
129                  )))
130        (setq rest (cdr rest)))
131      dest))
132    (t
133     (let ((dest (mapcar (function car) mime-charset-coding-system-alist))
134           (rest (coding-system-list))
135           cs)
136       (while rest
137         (setq cs (car rest))
138         (unless (rassq cs mime-charset-coding-system-alist)
139           (when (setq cs (or (coding-system-get cs 'mime-charset)
140                              (and
141                               (setq cs (aref
142                                         (coding-system-get cs 'coding-spec)
143                                         2))
144                               (string-match "(MIME:[ \t]*\\([^,)]+\\)" cs)
145                               (match-string 1 cs))))
146             (setq cs (intern (downcase cs)))
147             (or (rassq cs mime-charset-coding-system-alist)
148                 (memq cs dest)
149                 (setq dest (cons cs dest))
150                 )))
151         (setq rest (cdr rest)))
152       dest)
153     ))
154
155 ;;; @ end
156 ;;;
157
158 (require 'mcs-20)
159
160 (provide 'mcs-e20)
161
162 ;;; mcs-e20.el ends here