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