1 ;;; mel.el : a MIME encoding/decoding library
3 ;; Copyright (C) 1995,1996,1997,1998 Free Software Foundation, Inc.
5 ;; Author: MORIOKA Tomohiko <morioka@jaist.ac.jp>
6 ;; modified by Shuhei KOBAYASHI <shuhei-k@jaist.ac.jp>
8 ;; Keywords: MIME, Base64, Quoted-Printable, uuencode, gzip64
10 ;; This file is part of MEL (MIME Encoding Library).
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.
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.
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.
30 (or (fboundp 'write-region-as-binary)
31 (error "Please install latest APEL."))
34 (defconst mel-version "7.2")
40 (defvar mime-temp-directory (or (getenv "MIME_TMP_DIR")
43 "*Directory for temporary files.")
45 (defvar base64-dl-module
46 (and (fboundp 'dynamic-link)
47 (expand-file-name "base64.so" exec-directory)))
53 (cond (base64-dl-module
54 (autoload 'base64-encode-string "mel-dl"
55 "Encode STRING to base64, and return the result.")
56 (autoload 'base64-decode-string "mel-dl"
57 "Decode STRING which is encoded in base64, and return the result.")
58 (autoload 'base64-encode-region "mel-dl"
59 "Encode current region by base64." t)
60 (autoload 'base64-decode-region "mel-dl"
61 "Decode current region by base64." t)
62 (autoload 'base64-insert-encoded-file "mel-dl"
63 "Encode contents of file to base64, and insert the result." t)
64 (autoload 'base64-write-decoded-region "mel-dl"
65 "Decode and write current region encoded by base64 into FILENAME." t)
67 (autoload 'base64-encoded-length "mel-dl")
70 (autoload 'base64-encode-string "mel-b"
71 "Encode STRING to base64, and return the result.")
72 (autoload 'base64-decode-string "mel-b"
73 "Decode STRING which is encoded in base64, and return the result.")
74 (autoload 'base64-encode-region "mel-b"
75 "Encode current region by base64." t)
76 (autoload 'base64-decode-region "mel-b"
77 "Decode current region by base64." t)
78 (autoload 'base64-insert-encoded-file "mel-b"
79 "Encode contents of file to base64, and insert the result." t)
80 (autoload 'base64-write-decoded-region "mel-b"
81 "Decode and write current region encoded by base64 into FILENAME." t)
83 (autoload 'base64-encoded-length "mel-b")
86 (autoload 'quoted-printable-encode-string "mel-q"
87 "Encode STRING to quoted-printable, and return the result.")
88 (autoload 'quoted-printable-decode-string "mel-q"
89 "Decode STRING which is encoded in quoted-printable, and return the result.")
90 (autoload 'quoted-printable-encode-region "mel-q"
91 "Encode current region by Quoted-Printable." t)
92 (autoload 'quoted-printable-decode-region "mel-q"
93 "Decode current region by Quoted-Printable." t)
94 (autoload 'quoted-printable-insert-encoded-file "mel-q"
95 "Encode contents of file to quoted-printable, and insert the result." t)
96 (autoload 'quoted-printable-write-decoded-region "mel-q"
97 "Decode and write current region encoded by quoted-printable into FILENAME."
100 (autoload 'q-encoding-encode-string "mel-q"
101 "Encode STRING to Q-encoding of encoded-word, and return the result.")
102 (autoload 'q-encoding-decode-string "mel-q"
103 "Decode STRING which is encoded in Q-encoding and return the result.")
104 (autoload 'q-encoding-encoded-length "mel-q")
106 (autoload 'uuencode-encode-region "mel-u"
107 "Encode current region by unofficial uuencode format." t)
108 (autoload 'uuencode-decode-region "mel-u"
109 "Decode current region by unofficial uuencode format." t)
110 (autoload 'uuencode-insert-encoded-file "mel-u"
111 "Insert file encoded by unofficial uuencode format." t)
112 (autoload 'uuencode-write-decoded-region "mel-u"
113 "Decode and write current region encoded by uuencode into FILENAME." t)
115 (autoload 'gzip64-encode-region "mel-g"
116 "Encode current region by unofficial x-gzip64 format." t)
117 (autoload 'gzip64-decode-region "mel-g"
118 "Decode current region by unofficial x-gzip64 format." t)
119 (autoload 'gzip64-insert-encoded-file "mel-g"
120 "Insert file encoded by unofficial gzip64 format." t)
121 (autoload 'gzip64-write-decoded-region "mel-g"
122 "Decode and write current region encoded by gzip64 into FILENAME." t)
129 (defvar mime-encoding-method-alist
130 '(("base64" . base64-encode-region)
131 ("quoted-printable" . quoted-printable-encode-region)
132 ;; Not standard, their use is DISCOURAGED.
133 ;; ("x-uue" . uuencode-encode-region)
134 ;; ("x-gzip64" . gzip64-encode-region)
139 "Alist of encoding vs. corresponding method to encode region.
140 Each element looks like (STRING . FUNCTION) or (STRING . nil).
141 STRING is content-transfer-encoding.
142 FUNCTION is region encoder and nil means not to encode.")
145 (defvar mime-decoding-method-alist
146 '(("base64" . base64-decode-region)
147 ("quoted-printable" . quoted-printable-decode-region)
148 ("x-uue" . uuencode-decode-region)
149 ("x-uuencode" . uuencode-decode-region)
150 ("x-gzip64" . gzip64-decode-region)
152 "Alist of encoding vs. corresponding method to decode region.
153 Each element looks like (STRING . FUNCTION).
154 STRING is content-transfer-encoding.
155 FUNCTION is region decoder.")
158 (defun mime-encode-region (start end encoding)
159 "Encode region START to END of current buffer using ENCODING.
160 ENCODING must be string. If ENCODING is found in
161 `mime-encoding-method-alist' as its key, this function encodes the
162 region by its value."
164 (list (region-beginning) (region-end)
165 (completing-read "encoding: "
166 mime-encoding-method-alist
169 (let ((f (cdr (assoc encoding mime-encoding-method-alist))))
171 (funcall f start end)
175 (defun mime-decode-region (start end encoding)
176 "Decode region START to END of current buffer using ENCODING.
177 ENCODING must be string. If ENCODING is found in
178 `mime-decoding-method-alist' as its key, this function decodes the
179 region by its value."
181 (list (region-beginning) (region-end)
182 (completing-read "encoding: "
183 mime-decoding-method-alist
186 (let ((f (cdr (assoc encoding mime-decoding-method-alist))))
188 (funcall f start end)
196 (defvar mime-file-encoding-method-alist
197 '(("base64" . base64-insert-encoded-file)
198 ("quoted-printable" . quoted-printable-insert-encoded-file)
199 ;; Not standard, their use is DISCOURAGED.
200 ;; ("x-uue" . uuencode-insert-encoded-file)
201 ;; ("x-gzip64" . gzip64-insert-encoded-file)
202 ("7bit" . insert-file-contents-as-binary)
203 ("8bit" . insert-file-contents-as-binary)
204 ("binary" . insert-file-contents-as-binary)
206 "Alist of encoding vs. corresponding method to insert encoded file.
207 Each element looks like (STRING . FUNCTION).
208 STRING is content-transfer-encoding.
209 FUNCTION is function to insert encoded file.")
212 (defvar mime-file-decoding-method-alist
213 '(("base64" . base64-write-decoded-region)
214 ("quoted-printable" . quoted-printable-write-decoded-region)
215 ("x-uue" . uuencode-write-decoded-region)
216 ("x-gzip64" . gzip64-write-decoded-region)
217 ("7bit" . write-region-as-binary)
218 ("8bit" . write-region-as-binary)
219 ("binary" . write-region-as-binary)
221 "Alist of encoding vs. corresponding method to write decoded region to file.
222 Each element looks like (STRING . FUNCTION).
223 STRING is content-transfer-encoding.
224 FUNCTION is function to write decoded region to file.")
227 (defun mime-insert-encoded-file (filename encoding)
228 "Insert file FILENAME encoded by ENCODING format."
230 (list (read-file-name "Insert encoded file: ")
231 (completing-read "encoding: "
232 mime-encoding-method-alist
235 (let ((f (cdr (assoc encoding mime-file-encoding-method-alist))))
241 (defun mime-write-decoded-region (start end filename encoding)
242 "Decode and write current region encoded by ENCODING into FILENAME.
243 START and END are buffer positions."
245 (list (region-beginning) (region-end)
246 (read-file-name "Write decoded region to file: ")
247 (completing-read "encoding: "
248 mime-file-decoding-method-alist
250 (let ((f (cdr (assoc encoding mime-file-decoding-method-alist))))
252 (funcall f start end filename)
261 ;;; mel.el ends here.