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