(mime-file-encoding-method-alist): Use `insert-binary-file-contents'
[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        ;; for encoded-word
60        (autoload 'base64-encoded-length "mel-dl")
61        )
62       (t
63        (autoload 'base64-encode-string "mel-b"
64          "Encode STRING to base64, and return the result.")
65        (autoload 'base64-decode-string "mel-b"
66          "Decode STRING which is encoded in base64, and return the result.")
67        (autoload 'base64-encode-region "mel-b"
68          "Encode current region by base64." t)
69        (autoload 'base64-decode-region "mel-b"
70          "Decode current region by base64." t)
71        (autoload 'base64-insert-encoded-file "mel-b"
72          "Encode contents of file to base64, and insert the result." t)
73        ;; for encoded-word
74        (autoload 'base64-encoded-length "mel-b")
75        ))
76
77 (autoload 'quoted-printable-encode-string "mel-q"
78   "Encode STRING to quoted-printable, and return the result.")
79 (autoload 'quoted-printable-decode-string "mel-q"
80   "Decode STRING which is encoded in quoted-printable, and return the result.")
81 (autoload 'quoted-printable-encode-region "mel-q"
82   "Encode current region by Quoted-Printable." t)
83 (autoload 'quoted-printable-decode-region "mel-q"
84   "Decode current region by Quoted-Printable." t)
85 (autoload 'quoted-printable-insert-encoded-file "mel-q"
86   "Encode contents of file to quoted-printable, and insert the result." t)
87 ;; for encoded-word
88 (autoload 'q-encoding-encode-string "mel-q"
89   "Encode STRING to Q-encoding of encoded-word, and return the result.")
90 (autoload 'q-encoding-decode-string "mel-q"
91   "Decode STRING which is encoded in Q-encoding and return the result.")
92 (autoload 'q-encoding-encoded-length "mel-q")
93
94 (autoload 'uuencode-encode-region "mel-u"
95   "Encode current region by unofficial uuencode format." t)
96 (autoload 'uuencode-decode-region "mel-u"
97   "Decode current region by unofficial uuencode format." t)
98 (autoload 'uuencode-insert-encoded-file "mel-u"
99   "Insert file encoded by unofficial uuencode format." t)
100
101 (autoload 'gzip64-encode-region "mel-g"
102   "Encode current region by unofficial x-gzip64 format." t)
103 (autoload 'gzip64-decode-region "mel-g"
104   "Decode current region by unofficial x-gzip64 format." t)
105 (autoload 'gzip64-insert-encoded-file "mel-g"
106   "Insert file encoded by unofficial gzip64 format." t)
107
108
109 ;;; @ region
110 ;;;
111
112 ;;;###autoload
113 (defvar mime-encoding-method-alist
114   '(("base64"           . base64-encode-region)
115     ("quoted-printable" . quoted-printable-encode-region)
116     ;; Not standard, their use is DISCOURAGED.
117     ;; ("x-uue"            . uuencode-encode-region)
118     ;; ("x-gzip64"         . gzip64-encode-region)
119     ("7bit")
120     ("8bit")
121     ("binary")
122     )
123   "Alist of encoding vs. corresponding method to encode region.
124 Each element looks like (STRING . FUNCTION) or (STRING . nil).
125 STRING is content-transfer-encoding.
126 FUNCTION is region encoder and nil means not to encode.")
127
128 ;;;###autoload
129 (defvar mime-decoding-method-alist
130   '(("base64"           . base64-decode-region)
131     ("quoted-printable" . quoted-printable-decode-region)
132     ("x-uue"            . uuencode-decode-region)
133     ("x-uuencode"       . uuencode-decode-region)
134     ("x-gzip64"         . gzip64-decode-region)
135     )
136   "Alist of encoding vs. corresponding method to decode region.
137 Each element looks like (STRING . FUNCTION).
138 STRING is content-transfer-encoding.
139 FUNCTION is region decoder.")
140
141 ;;;###autoload
142 (defun mime-encode-region (start end encoding)
143   "Encode region START to END of current buffer using ENCODING.
144 ENCODING must be string.  If ENCODING is found in
145 `mime-encoding-method-alist' as its key, this function encodes the
146 region by its value."
147   (interactive
148    (list (region-beginning) (region-end)
149          (completing-read "encoding: "
150                           mime-encoding-method-alist
151                           nil t "base64"))
152    )
153   (let ((f (cdr (assoc encoding mime-encoding-method-alist))))
154     (if f
155         (funcall f start end)
156       )))
157
158 ;;;###autoload
159 (defun mime-decode-region (start end encoding)
160   "Decode region START to END of current buffer using ENCODING.
161 ENCODING must be string.  If ENCODING is found in
162 `mime-decoding-method-alist' as its key, this function decodes the
163 region by its value."
164   (interactive
165    (list (region-beginning) (region-end)
166          (completing-read "encoding: "
167                           mime-decoding-method-alist
168                           nil t "base64"))
169    )
170   (let ((f (cdr (assoc encoding mime-decoding-method-alist))))
171     (if f
172         (funcall f start end)
173       )))
174
175
176 ;;; @ file
177 ;;;
178
179 ;;;###autoload
180 (defvar mime-file-encoding-method-alist
181   '(("base64"           . base64-insert-encoded-file)
182     ("quoted-printable" . quoted-printable-insert-encoded-file)
183     ;; Not standard, their use is DISCOURAGED.
184     ;; ("x-uue"            . uuencode-insert-encoded-file)
185     ;; ("x-gzip64"         . gzip64-insert-encoded-file)
186     ("7bit"             . insert-binary-file-contents)
187     ("8bit"             . insert-binary-file-contents)
188     ("binary"           . insert-binary-file-contents)
189     )
190   "Alist of encoding vs. corresponding method to insert encoded file.
191 Each element looks like (STRING . FUNCTION).
192 STRING is content-transfer-encoding.
193 FUNCTION is function to insert encoded file.")
194
195 ;;;###autoload
196 (defun mime-insert-encoded-file (filename encoding)
197   "Insert file FILENAME encoded by ENCODING format."
198   (interactive
199    (list (read-file-name "Insert encoded file: ")
200          (completing-read "encoding: "
201                           mime-encoding-method-alist
202                           nil t "base64"))
203    )
204   (let ((f (cdr (assoc encoding mime-file-encoding-method-alist))))
205     (if f
206         (funcall f filename)
207       )))
208
209
210 ;;; @ end
211 ;;;
212
213 (provide 'mel)
214
215 ;;; mel.el ends here.