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