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