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.
31 (defconst mel-version "7.4")
37 (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 (let ((path (expand-file-name "base64.so" exec-directory)))
48 (and (file-exists-p path)
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)
69 (autoload 'base64-encoded-length "mel-dl")
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)
85 (autoload 'base64-encoded-length "mel-b")
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."
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")
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)
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)
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)
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.")
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)
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.")
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."
166 (list (region-beginning) (region-end)
167 (completing-read "encoding: "
168 mime-encoding-method-alist
171 (let ((f (cdr (assoc encoding mime-encoding-method-alist))))
173 (funcall f start end)
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."
183 (list (region-beginning) (region-end)
184 (completing-read "encoding: "
185 mime-decoding-method-alist
188 (let ((f (cdr (assoc encoding mime-decoding-method-alist))))
190 (funcall f start end)
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)
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.")
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)
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.")
229 (defun mime-insert-encoded-file (filename encoding)
230 "Insert file FILENAME encoded by ENCODING format."
232 (list (read-file-name "Insert encoded file: ")
233 (completing-read "encoding: "
234 mime-encoding-method-alist
237 (let ((f (cdr (assoc encoding mime-file-encoding-method-alist))))
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."
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
252 (let ((f (cdr (assoc encoding mime-file-decoding-method-alist))))
254 (funcall f start end filename)
263 ;;; mel.el ends here.