* mime-def.el (mime-library-version-string): bump up to 1.9.1.
[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
32 ;;; @ variable
33 ;;;
34
35 (defvar base64-dl-module
36   (and (fboundp 'dynamic-link)
37        (let ((path (expand-file-name "base64.so" exec-directory)))
38          (and (file-exists-p path)
39               path))))
40
41
42 ;;; @ autoload
43 ;;;
44
45 (cond (base64-dl-module
46        (autoload 'base64-encode-string "mel-dl"
47          "Encode STRING to base64, and return the result.")
48        (autoload 'base64-decode-string "mel-dl"
49          "Decode STRING which is encoded in base64, and return the result.")
50        (autoload 'base64-encode-region "mel-dl"
51          "Encode current region by base64." t)
52        (autoload 'base64-decode-region "mel-dl"
53          "Decode current region by base64." t)
54        (autoload 'base64-insert-encoded-file "mel-dl"
55          "Encode contents of file to base64, and insert the result." t)
56        (autoload 'base64-write-decoded-region "mel-dl"
57          "Decode and write current region encoded by base64 into FILENAME." t)
58        ;; for encoded-word
59        (autoload 'base64-encoded-length "mel-dl")
60        )
61       (t
62        (autoload 'base64-encode-string "mel-b"
63          "Encode STRING to base64, and return the result.")
64        (autoload 'base64-decode-string "mel-b"
65          "Decode STRING which is encoded in base64, and return the result.")
66        (autoload 'base64-encode-region "mel-b"
67          "Encode current region by base64." t)
68        (autoload 'base64-decode-region "mel-b"
69          "Decode current region by base64." t)
70        (autoload 'base64-insert-encoded-file "mel-b"
71          "Encode contents of file to base64, and insert the result." t)
72        (autoload 'base64-write-decoded-region "mel-b"
73          "Decode and write current region encoded by base64 into FILENAME." t)
74        ;; for encoded-word
75        (autoload 'base64-encoded-length "mel-b")
76        ))
77
78 (autoload 'quoted-printable-encode-string "mel-q"
79   "Encode STRING to quoted-printable, and return the result.")
80 (autoload 'quoted-printable-decode-string "mel-q"
81   "Decode STRING which is encoded in quoted-printable, and return the result.")
82 (autoload 'quoted-printable-encode-region "mel-q"
83   "Encode current region by Quoted-Printable." t)
84 (autoload 'quoted-printable-decode-region "mel-q"
85   "Decode current region by Quoted-Printable." t)
86 (autoload 'quoted-printable-insert-encoded-file "mel-q"
87   "Encode contents of file to quoted-printable, and insert the result." t)
88 (autoload 'quoted-printable-write-decoded-region "mel-q"
89   "Decode and write current region encoded by quoted-printable into FILENAME."
90   t)
91 ;; for encoded-word
92 (autoload 'q-encoding-encode-string "mel-q"
93   "Encode STRING to Q-encoding of encoded-word, and return the result.")
94 (autoload 'q-encoding-decode-string "mel-q"
95   "Decode STRING which is encoded in Q-encoding and return the result.")
96 (autoload 'q-encoding-encoded-length "mel-q")
97
98 (autoload 'uuencode-encode-region "mel-u"
99   "Encode current region by unofficial uuencode format." t)
100 (autoload 'uuencode-decode-region "mel-u"
101   "Decode current region by unofficial uuencode format." t)
102 (autoload 'uuencode-insert-encoded-file "mel-u"
103   "Insert file encoded by unofficial uuencode format." t)
104 (autoload 'uuencode-write-decoded-region "mel-u"
105   "Decode and write current region encoded by uuencode into FILENAME." t)
106
107 (autoload 'gzip64-encode-region "mel-g"
108   "Encode current region by unofficial x-gzip64 format." t)
109 (autoload 'gzip64-decode-region "mel-g"
110   "Decode current region by unofficial x-gzip64 format." t)
111 (autoload 'gzip64-insert-encoded-file "mel-g"
112   "Insert file encoded by unofficial gzip64 format." t)
113 (autoload 'gzip64-write-decoded-region "mel-g"
114   "Decode and write current region encoded by gzip64 into FILENAME." t)
115
116
117 ;;; @ region
118 ;;;
119
120 ;;;###autoload
121 (defvar mime-encoding-method-alist
122   '(("base64"           . base64-encode-region)
123     ("quoted-printable" . quoted-printable-encode-region)
124     ;; Not standard, their use is DISCOURAGED.
125     ;; ("x-uue"            . uuencode-encode-region)
126     ;; ("x-gzip64"         . gzip64-encode-region)
127     ("7bit")
128     ("8bit")
129     ("binary")
130     )
131   "Alist of encoding vs. corresponding method to encode region.
132 Each element looks like (STRING . FUNCTION) or (STRING . nil).
133 STRING is content-transfer-encoding.
134 FUNCTION is region encoder and nil means not to encode.")
135
136 ;;;###autoload
137 (defvar mime-decoding-method-alist
138   '(("base64"           . base64-decode-region)
139     ("quoted-printable" . quoted-printable-decode-region)
140     ("x-uue"            . uuencode-decode-region)
141     ("x-uuencode"       . uuencode-decode-region)
142     ("x-gzip64"         . gzip64-decode-region)
143     )
144   "Alist of encoding vs. corresponding method to decode region.
145 Each element looks like (STRING . FUNCTION).
146 STRING is content-transfer-encoding.
147 FUNCTION is region decoder.")
148
149 ;;;###autoload
150 (defun mime-encode-region (start end encoding)
151   "Encode region START to END of current buffer using ENCODING.
152 ENCODING must be string.  If ENCODING is found in
153 `mime-encoding-method-alist' as its key, this function encodes the
154 region by its value."
155   (interactive
156    (list (region-beginning) (region-end)
157          (completing-read "encoding: "
158                           mime-encoding-method-alist
159                           nil t "base64"))
160    )
161   (let ((f (cdr (assoc encoding mime-encoding-method-alist))))
162     (if f
163         (funcall f start end)
164       )))
165
166 ;;;###autoload
167 (defun mime-decode-region (start end encoding)
168   "Decode region START to END of current buffer using ENCODING.
169 ENCODING must be string.  If ENCODING is found in
170 `mime-decoding-method-alist' as its key, this function decodes the
171 region by its value."
172   (interactive
173    (list (region-beginning) (region-end)
174          (completing-read "encoding: "
175                           mime-decoding-method-alist
176                           nil t "base64"))
177    )
178   (let ((f (cdr (assoc encoding mime-decoding-method-alist))))
179     (if f
180         (funcall f start end)
181       )))
182
183
184 ;;; @ string
185 ;;;
186
187 ;;;###autoload
188 (defvar mime-string-decoding-method-alist
189   '(("base64"           . base64-decode-string)
190     ("quoted-printable" . quoted-printable-decode-string)
191     ("7bit"             . identity)
192     ("8bit"             . identity)
193     ("binary"           . identity)
194     )
195   "Alist of encoding vs. corresponding method to decode string.
196 Each element looks like (STRING . FUNCTION).
197 STRING is content-transfer-encoding.
198 FUNCTION is string decoder.")
199
200 ;;;###autoload
201 (defun mime-decode-string (string encoding)
202   "Decode STRING using ENCODING.
203 ENCODING must be string.  If ENCODING is found in
204 `mime-string-decoding-method-alist' as its key, this function decodes
205 the STRING by its value."
206   (let ((f (cdr (assoc encoding mime-string-decoding-method-alist))))
207     (if f
208         (funcall f string)
209       (with-temp-buffer
210         (insert string)
211         (mime-decode-region (point-min)(point-max) encoding)
212         (buffer-string)
213         ))))
214
215
216 ;;; @ file
217 ;;;
218
219 ;;;###autoload
220 (defvar mime-file-encoding-method-alist
221   '(("base64"           . base64-insert-encoded-file)
222     ("quoted-printable" . quoted-printable-insert-encoded-file)
223     ;; Not standard, their use is DISCOURAGED.
224     ;; ("x-uue"            . uuencode-insert-encoded-file)
225     ;; ("x-gzip64"         . gzip64-insert-encoded-file)
226     ("7bit"             . insert-file-contents-as-binary)
227     ("8bit"             . insert-file-contents-as-binary)
228     ("binary"           . insert-file-contents-as-binary)
229     )
230   "Alist of encoding vs. corresponding method to insert encoded file.
231 Each element looks like (STRING . FUNCTION).
232 STRING is content-transfer-encoding.
233 FUNCTION is function to insert encoded file.")
234
235 ;;;###autoload
236 (defvar mime-file-decoding-method-alist
237   '(("base64"           . base64-write-decoded-region)
238     ("quoted-printable" . quoted-printable-write-decoded-region)
239     ("x-uue"            . uuencode-write-decoded-region)
240     ("x-gzip64"         . gzip64-write-decoded-region)
241     ("7bit"             . write-region-as-binary)
242     ("8bit"             . write-region-as-binary)
243     ("binary"           . write-region-as-binary)
244     )
245   "Alist of encoding vs. corresponding method to write decoded region to file.
246 Each element looks like (STRING . FUNCTION).
247 STRING is content-transfer-encoding.
248 FUNCTION is function to write decoded region to file.")
249
250 ;;;###autoload
251 (defun mime-insert-encoded-file (filename encoding)
252   "Insert file FILENAME encoded by ENCODING format."
253   (interactive
254    (list (read-file-name "Insert encoded file: ")
255          (completing-read "encoding: "
256                           mime-encoding-method-alist
257                           nil t "base64"))
258    )
259   (let ((f (cdr (assoc encoding mime-file-encoding-method-alist))))
260     (if f
261         (funcall f filename)
262       )))
263
264 ;;;###autoload
265 (defun mime-write-decoded-region (start end filename encoding)
266   "Decode and write current region encoded by ENCODING into FILENAME.
267 START and END are buffer positions."
268   (interactive
269    (list (region-beginning) (region-end)
270          (read-file-name "Write decoded region to file: ")
271          (completing-read "encoding: "
272                           mime-file-decoding-method-alist
273                           nil t "base64")))
274   (let ((f (cdr (assoc encoding mime-file-decoding-method-alist))))
275     (if f
276         (funcall f start end filename)
277       )))
278
279
280 ;;; @ end
281 ;;;
282
283 (provide 'mel)
284
285 ;;; mel.el ends here.