27d045cfa21dfb3ce76504be95f2327229d13138
[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 (or (fboundp 'write-region-as-binary)
31     (error "Please install latest APEL."))
32
33
34 (defconst mel-version "7.2")
35
36
37 ;;; @ variable
38 ;;;
39
40 (defvar mime-temp-directory (or (getenv "MIME_TMP_DIR")
41                                 (getenv "TM_TMP_DIR")
42                                 "/tmp/")
43   "*Directory for temporary files.")
44
45 (defvar base64-dl-module
46   (and (fboundp 'dynamic-link)
47        (expand-file-name "base64.so" exec-directory)))
48
49
50 ;;; @ autoload
51 ;;;
52
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)
66        ;; for encoded-word
67        (autoload 'base64-encoded-length "mel-dl")
68        )
69       (t
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)
82        ;; for encoded-word
83        (autoload 'base64-encoded-length "mel-b")
84        ))
85
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."
98   t)
99 ;; for encoded-word
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")
105
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)
114
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)
123
124
125 ;;; @ region
126 ;;;
127
128 ;;;###autoload
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)
135     ("7bit")
136     ("8bit")
137     ("binary")
138     )
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.")
143
144 ;;;###autoload
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)
151     )
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.")
156
157 ;;;###autoload
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."
163   (interactive
164    (list (region-beginning) (region-end)
165          (completing-read "encoding: "
166                           mime-encoding-method-alist
167                           nil t "base64"))
168    )
169   (let ((f (cdr (assoc encoding mime-encoding-method-alist))))
170     (if f
171         (funcall f start end)
172       )))
173
174 ;;;###autoload
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."
180   (interactive
181    (list (region-beginning) (region-end)
182          (completing-read "encoding: "
183                           mime-decoding-method-alist
184                           nil t "base64"))
185    )
186   (let ((f (cdr (assoc encoding mime-decoding-method-alist))))
187     (if f
188         (funcall f start end)
189       )))
190
191
192 ;;; @ file
193 ;;;
194
195 ;;;###autoload
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)
205     )
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.")
210
211 ;;;###autoload
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)
220     )
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.")
225
226 ;;;###autoload
227 (defun mime-insert-encoded-file (filename encoding)
228   "Insert file FILENAME encoded by ENCODING format."
229   (interactive
230    (list (read-file-name "Insert encoded file: ")
231          (completing-read "encoding: "
232                           mime-encoding-method-alist
233                           nil t "base64"))
234    )
235   (let ((f (cdr (assoc encoding mime-file-encoding-method-alist))))
236     (if f
237         (funcall f filename)
238       )))
239
240 ;;;###autoload
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."
244   (interactive
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
249                           nil t "base64")))
250   (let ((f (cdr (assoc encoding mime-file-decoding-method-alist))))
251     (if f
252         (funcall f start end filename)
253       )))
254
255
256 ;;; @ end
257 ;;;
258
259 (provide 'mel)
260
261 ;;; mel.el ends here.