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