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