(mime-message-structure): New variable.
[elisp/flim.git] / mel.el
1 ;;; mel.el : a MIME encoding/decoding library
2
3 ;; Copyright (C) 1995,1996,1997,1998 Free Software Foundation, Inc.
4
5 ;; Author: MORIOKA Tomohiko <morioka@jaist.ac.jp>
6 ;; modified by Shuhei KOBAYASHI <shuhei-k@jaist.ac.jp>
7 ;; Created: 1995/6/25
8 ;; Keywords: MIME, Base64, Quoted-Printable, uuencode, gzip64
9
10 ;; This file is part of MEL (MIME Encoding Library).
11
12 ;; This program is free software; you can redistribute it and/or
13 ;; modify it under the terms of the GNU General Public License as
14 ;; published by the Free Software Foundation; either version 2, or (at
15 ;; your option) any later version.
16
17 ;; This program is distributed in the hope that it will be useful, but
18 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20 ;; General Public License for more details.
21
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
26
27 ;;; Code:
28
29 (require 'emu)
30
31 (defconst mel-version "7.5")
32
33
34 ;;; @ variable
35 ;;;
36
37 (defvar base64-dl-module
38   (and (fboundp 'dynamic-link)
39        (let ((path (expand-file-name "base64.so" exec-directory)))
40          (and (file-exists-p path)
41               path))))
42
43
44 ;;; @ autoload
45 ;;;
46
47 (cond (base64-dl-module
48        (autoload 'base64-encode-string "mel-dl"
49          "Encode STRING to base64, and return the result.")
50        (autoload 'base64-decode-string "mel-dl"
51          "Decode STRING which is encoded in base64, and return the result.")
52        (autoload 'base64-encode-region "mel-dl"
53          "Encode current region by base64." t)
54        (autoload 'base64-decode-region "mel-dl"
55          "Decode current region by base64." t)
56        (autoload 'base64-insert-encoded-file "mel-dl"
57          "Encode contents of file to base64, and insert the result." t)
58        (autoload 'base64-write-decoded-region "mel-dl"
59          "Decode and write current region encoded by base64 into FILENAME." t)
60        ;; for encoded-word
61        (autoload 'base64-encoded-length "mel-dl")
62        )
63       (t
64        (autoload 'base64-encode-string "mel-b"
65          "Encode STRING to base64, and return the result.")
66        (autoload 'base64-decode-string "mel-b"
67          "Decode STRING which is encoded in base64, and return the result.")
68        (autoload 'base64-encode-region "mel-b"
69          "Encode current region by base64." t)
70        (autoload 'base64-decode-region "mel-b"
71          "Decode current region by base64." t)
72        (autoload 'base64-insert-encoded-file "mel-b"
73          "Encode contents of file to base64, and insert the result." t)
74        (autoload 'base64-write-decoded-region "mel-b"
75          "Decode and write current region encoded by base64 into FILENAME." t)
76        ;; for encoded-word
77        (autoload 'base64-encoded-length "mel-b")
78        ))
79
80 (autoload 'quoted-printable-encode-string "mel-q"
81   "Encode STRING to quoted-printable, and return the result.")
82 (autoload 'quoted-printable-decode-string "mel-q"
83   "Decode STRING which is encoded in quoted-printable, and return the result.")
84 (autoload 'quoted-printable-encode-region "mel-q"
85   "Encode current region by Quoted-Printable." t)
86 (autoload 'quoted-printable-decode-region "mel-q"
87   "Decode current region by Quoted-Printable." t)
88 (autoload 'quoted-printable-insert-encoded-file "mel-q"
89   "Encode contents of file to quoted-printable, and insert the result." t)
90 (autoload 'quoted-printable-write-decoded-region "mel-q"
91   "Decode and write current region encoded by quoted-printable into FILENAME."
92   t)
93 ;; for encoded-word
94 (autoload 'q-encoding-encode-string "mel-q"
95   "Encode STRING to Q-encoding of encoded-word, and return the result.")
96 (autoload 'q-encoding-decode-string "mel-q"
97   "Decode STRING which is encoded in Q-encoding and return the result.")
98 (autoload 'q-encoding-encoded-length "mel-q")
99
100 (autoload 'uuencode-encode-region "mel-u"
101   "Encode current region by unofficial uuencode format." t)
102 (autoload 'uuencode-decode-region "mel-u"
103   "Decode current region by unofficial uuencode format." t)
104 (autoload 'uuencode-insert-encoded-file "mel-u"
105   "Insert file encoded by unofficial uuencode format." t)
106 (autoload 'uuencode-write-decoded-region "mel-u"
107   "Decode and write current region encoded by uuencode into FILENAME." t)
108
109 (autoload 'gzip64-encode-region "mel-g"
110   "Encode current region by unofficial x-gzip64 format." t)
111 (autoload 'gzip64-decode-region "mel-g"
112   "Decode current region by unofficial x-gzip64 format." t)
113 (autoload 'gzip64-insert-encoded-file "mel-g"
114   "Insert file encoded by unofficial gzip64 format." t)
115 (autoload 'gzip64-write-decoded-region "mel-g"
116   "Decode and write current region encoded by gzip64 into FILENAME." t)
117
118
119 ;;; @ region
120 ;;;
121
122 ;;;###autoload
123 (defvar mime-encoding-method-alist
124   '(("base64"           . base64-encode-region)
125     ("quoted-printable" . quoted-printable-encode-region)
126     ;; Not standard, their use is DISCOURAGED.
127     ;; ("x-uue"            . uuencode-encode-region)
128     ;; ("x-gzip64"         . gzip64-encode-region)
129     ("7bit")
130     ("8bit")
131     ("binary")
132     )
133   "Alist of encoding vs. corresponding method to encode region.
134 Each element looks like (STRING . FUNCTION) or (STRING . nil).
135 STRING is content-transfer-encoding.
136 FUNCTION is region encoder and nil means not to encode.")
137
138 ;;;###autoload
139 (defvar mime-decoding-method-alist
140   '(("base64"           . base64-decode-region)
141     ("quoted-printable" . quoted-printable-decode-region)
142     ("x-uue"            . uuencode-decode-region)
143     ("x-uuencode"       . uuencode-decode-region)
144     ("x-gzip64"         . gzip64-decode-region)
145     )
146   "Alist of encoding vs. corresponding method to decode region.
147 Each element looks like (STRING . FUNCTION).
148 STRING is content-transfer-encoding.
149 FUNCTION is region decoder.")
150
151 ;;;###autoload
152 (defun mime-encode-region (start end encoding)
153   "Encode region START to END of current buffer using ENCODING.
154 ENCODING must be string.  If ENCODING is found in
155 `mime-encoding-method-alist' as its key, this function encodes the
156 region by its value."
157   (interactive
158    (list (region-beginning) (region-end)
159          (completing-read "encoding: "
160                           mime-encoding-method-alist
161                           nil t "base64"))
162    )
163   (let ((f (cdr (assoc encoding mime-encoding-method-alist))))
164     (if f
165         (funcall f start end)
166       )))
167
168 ;;;###autoload
169 (defun mime-decode-region (start end encoding)
170   "Decode region START to END of current buffer using ENCODING.
171 ENCODING must be string.  If ENCODING is found in
172 `mime-decoding-method-alist' as its key, this function decodes the
173 region by its value."
174   (interactive
175    (list (region-beginning) (region-end)
176          (completing-read "encoding: "
177                           mime-decoding-method-alist
178                           nil t "base64"))
179    )
180   (let ((f (cdr (assoc encoding mime-decoding-method-alist))))
181     (if f
182         (funcall f start end)
183       )))
184
185
186 ;;; @ file
187 ;;;
188
189 ;;;###autoload
190 (defvar mime-file-encoding-method-alist
191   '(("base64"           . base64-insert-encoded-file)
192     ("quoted-printable" . quoted-printable-insert-encoded-file)
193     ;; Not standard, their use is DISCOURAGED.
194     ;; ("x-uue"            . uuencode-insert-encoded-file)
195     ;; ("x-gzip64"         . gzip64-insert-encoded-file)
196     ("7bit"             . insert-file-contents-as-binary)
197     ("8bit"             . insert-file-contents-as-binary)
198     ("binary"           . insert-file-contents-as-binary)
199     )
200   "Alist of encoding vs. corresponding method to insert encoded file.
201 Each element looks like (STRING . FUNCTION).
202 STRING is content-transfer-encoding.
203 FUNCTION is function to insert encoded file.")
204
205 ;;;###autoload
206 (defvar mime-file-decoding-method-alist
207   '(("base64"           . base64-write-decoded-region)
208     ("quoted-printable" . quoted-printable-write-decoded-region)
209     ("x-uue"            . uuencode-write-decoded-region)
210     ("x-gzip64"         . gzip64-write-decoded-region)
211     ("7bit"             . write-region-as-binary)
212     ("8bit"             . write-region-as-binary)
213     ("binary"           . write-region-as-binary)
214     )
215   "Alist of encoding vs. corresponding method to write decoded region to file.
216 Each element looks like (STRING . FUNCTION).
217 STRING is content-transfer-encoding.
218 FUNCTION is function to write decoded region to file.")
219
220 ;;;###autoload
221 (defun mime-insert-encoded-file (filename encoding)
222   "Insert file FILENAME encoded by ENCODING format."
223   (interactive
224    (list (read-file-name "Insert encoded file: ")
225          (completing-read "encoding: "
226                           mime-encoding-method-alist
227                           nil t "base64"))
228    )
229   (let ((f (cdr (assoc encoding mime-file-encoding-method-alist))))
230     (if f
231         (funcall f filename)
232       )))
233
234 ;;;###autoload
235 (defun mime-write-decoded-region (start end filename encoding)
236   "Decode and write current region encoded by ENCODING into FILENAME.
237 START and END are buffer positions."
238   (interactive
239    (list (region-beginning) (region-end)
240          (read-file-name "Write decoded region to file: ")
241          (completing-read "encoding: "
242                           mime-file-decoding-method-alist
243                           nil t "base64")))
244   (let ((f (cdr (assoc encoding mime-file-decoding-method-alist))))
245     (if f
246         (funcall f start end filename)
247       )))
248
249
250 ;;; @ end
251 ;;;
252
253 (provide 'mel)
254
255 ;;; mel.el ends here.