(mime-string-decoding-method-alist): 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 (defvar mime-string-decoding-method-alist
153   '(("base64"           . base64-decode-string)
154     ("quoted-printable" . quoted-printable-decode-string)
155     ("7bit"             . identity)
156     ("8bit"             . identity)
157     ("binary"           . identity)
158     )
159   "Alist of encoding vs. corresponding method to decode string.
160 Each element looks like (STRING . FUNCTION).
161 STRING is content-transfer-encoding.
162 FUNCTION is string decoder.")
163
164 ;;;###autoload
165 (defun mime-encode-region (start end encoding)
166   "Encode region START to END of current buffer using ENCODING.
167 ENCODING must be string.  If ENCODING is found in
168 `mime-encoding-method-alist' as its key, this function encodes the
169 region by its value."
170   (interactive
171    (list (region-beginning) (region-end)
172          (completing-read "encoding: "
173                           mime-encoding-method-alist
174                           nil t "base64"))
175    )
176   (let ((f (cdr (assoc encoding mime-encoding-method-alist))))
177     (if f
178         (funcall f start end)
179       )))
180
181 ;;;###autoload
182 (defun mime-decode-region (start end encoding)
183   "Decode region START to END of current buffer using ENCODING.
184 ENCODING must be string.  If ENCODING is found in
185 `mime-decoding-method-alist' as its key, this function decodes the
186 region by its value."
187   (interactive
188    (list (region-beginning) (region-end)
189          (completing-read "encoding: "
190                           mime-decoding-method-alist
191                           nil t "base64"))
192    )
193   (let ((f (cdr (assoc encoding mime-decoding-method-alist))))
194     (if f
195         (funcall f start end)
196       )))
197
198 ;;;###autoload
199 (defun mime-decode-string (string encoding)
200   "Decode STRING using ENCODING.
201 ENCODING must be string.  If ENCODING is found in
202 `mime-string-decoding-method-alist' as its key, this function decodes
203 the STRING by its value."
204   (let ((f (cdr (assoc encoding mime-string-decoding-method-alist))))
205     (if f
206         (funcall f string)
207       (with-temp-buffer
208         (insert string)
209         (mime-decode-region (point-min)(point-max) encoding)
210         (buffer-string)
211         ))))
212
213
214 ;;; @ file
215 ;;;
216
217 ;;;###autoload
218 (defvar mime-file-encoding-method-alist
219   '(("base64"           . base64-insert-encoded-file)
220     ("quoted-printable" . quoted-printable-insert-encoded-file)
221     ;; Not standard, their use is DISCOURAGED.
222     ;; ("x-uue"            . uuencode-insert-encoded-file)
223     ;; ("x-gzip64"         . gzip64-insert-encoded-file)
224     ("7bit"             . insert-file-contents-as-binary)
225     ("8bit"             . insert-file-contents-as-binary)
226     ("binary"           . insert-file-contents-as-binary)
227     )
228   "Alist of encoding vs. corresponding method to insert encoded file.
229 Each element looks like (STRING . FUNCTION).
230 STRING is content-transfer-encoding.
231 FUNCTION is function to insert encoded file.")
232
233 ;;;###autoload
234 (defvar mime-file-decoding-method-alist
235   '(("base64"           . base64-write-decoded-region)
236     ("quoted-printable" . quoted-printable-write-decoded-region)
237     ("x-uue"            . uuencode-write-decoded-region)
238     ("x-gzip64"         . gzip64-write-decoded-region)
239     ("7bit"             . write-region-as-binary)
240     ("8bit"             . write-region-as-binary)
241     ("binary"           . write-region-as-binary)
242     )
243   "Alist of encoding vs. corresponding method to write decoded region to file.
244 Each element looks like (STRING . FUNCTION).
245 STRING is content-transfer-encoding.
246 FUNCTION is function to write decoded region to file.")
247
248 ;;;###autoload
249 (defun mime-insert-encoded-file (filename encoding)
250   "Insert file FILENAME encoded by ENCODING format."
251   (interactive
252    (list (read-file-name "Insert encoded file: ")
253          (completing-read "encoding: "
254                           mime-encoding-method-alist
255                           nil t "base64"))
256    )
257   (let ((f (cdr (assoc encoding mime-file-encoding-method-alist))))
258     (if f
259         (funcall f filename)
260       )))
261
262 ;;;###autoload
263 (defun mime-write-decoded-region (start end filename encoding)
264   "Decode and write current region encoded by ENCODING into FILENAME.
265 START and END are buffer positions."
266   (interactive
267    (list (region-beginning) (region-end)
268          (read-file-name "Write decoded region to file: ")
269          (completing-read "encoding: "
270                           mime-file-decoding-method-alist
271                           nil t "base64")))
272   (let ((f (cdr (assoc encoding mime-file-decoding-method-alist))))
273     (if f
274         (funcall f start end filename)
275       )))
276
277
278 ;;; @ end
279 ;;;
280
281 (provide 'mel)
282
283 ;;; mel.el ends here.