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