93ee0d67fb123d8b8598ac933bac490e8acabbd7
[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 ;; Version: $Id: mel.el,v 7.2 1998/01/11 16:16:44 morioka Exp $
9 ;; Keywords: MIME, Base64, Quoted-Printable, uuencode, gzip64
10
11 ;; This file is part of MEL (MIME Encoding Library).
12
13 ;; This program is free software; you can redistribute it and/or
14 ;; modify it under the terms of the GNU General Public License as
15 ;; published by the Free Software Foundation; either version 2, or (at
16 ;; your option) any later version.
17
18 ;; This program is distributed in the hope that it will be useful, but
19 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
21 ;; General Public License for more details.
22
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
25 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
26 ;; Boston, MA 02111-1307, USA.
27
28 ;;; Code:
29
30 ;;; @ variable
31 ;;;
32
33 (defvar mime-temp-directory (or (getenv "MIME_TMP_DIR")
34                                 (getenv "TM_TMP_DIR")
35                                 "/tmp/")
36   "*Directory for temporary files.")
37
38 (defvar base64-dl-module
39   (and (fboundp 'dynamic-link)
40        (expand-file-name "base64.so" exec-directory)))
41
42
43 ;;; @ autoload
44 ;;;
45
46 (cond (base64-dl-module
47        (autoload 'base64-encode-string "mel-dl"
48          "Encode STRING to base64, and return the result.")
49        (autoload 'base64-decode-string "mel-dl"
50          "Decode STRING which is encoded in base64, and return the result.")
51        (autoload 'base64-encode-region "mel-dl"
52          "Encode current region by base64." t)
53        (autoload 'base64-decode-region "mel-dl"
54          "Decode current region by base64." t)
55        (autoload 'base64-insert-encoded-file "mel-dl"
56          "Encode contents of file to base64, and insert the result." t)
57        ;; for encoded-word
58        (autoload 'base64-encoded-length "mel-dl")
59        )
60       (t
61        (autoload 'base64-encode-string "mel-b"
62          "Encode STRING to base64, and return the result.")
63        (autoload 'base64-decode-string "mel-b"
64          "Decode STRING which is encoded in base64, and return the result.")
65        (autoload 'base64-encode-region "mel-b"
66          "Encode current region by base64." t)
67        (autoload 'base64-decode-region "mel-b"
68          "Decode current region by base64." t)
69        (autoload 'base64-insert-encoded-file "mel-b"
70          "Encode contents of file to base64, and insert the result." t)
71        ;; for encoded-word
72        (autoload 'base64-encoded-length "mel-b")
73        ))
74
75 (autoload 'quoted-printable-encode-string "mel-q"
76   "Encode STRING to quoted-printable, and return the result.")
77 (autoload 'quoted-printable-decode-string "mel-q"
78   "Decode STRING which is encoded in quoted-printable, and return the result.")
79 (autoload 'quoted-printable-encode-region "mel-q"
80   "Encode current region by Quoted-Printable." t)
81 (autoload 'quoted-printable-decode-region "mel-q"
82   "Decode current region by Quoted-Printable." t)
83 (autoload 'quoted-printable-insert-encoded-file "mel-q"
84   "Encode contents of file to quoted-printable, and insert the result." t)
85 ;; for encoded-word
86 (autoload 'q-encoding-encode-string "mel-q"
87   "Encode STRING to Q-encoding of encoded-word, and return the result.")
88 (autoload 'q-encoding-decode-string "mel-q"
89   "Decode STRING which is encoded in Q-encoding and return the result.")
90 (autoload 'q-encoding-encoded-length "mel-q")
91
92 (autoload 'uuencode-encode-region "mel-u"
93   "Encode current region by unofficial uuencode format." t)
94 (autoload 'uuencode-decode-region "mel-u"
95   "Decode current region by unofficial uuencode format." t)
96 (autoload 'uuencode-insert-encoded-file "mel-u"
97   "Insert file encoded by unofficial uuencode format." t)
98
99 (autoload 'gzip64-encode-region "mel-g"
100   "Encode current region by unofficial x-gzip64 format." t)
101 (autoload 'gzip64-decode-region "mel-g"
102   "Decode current region by unofficial x-gzip64 format." t)
103 (autoload 'gzip64-insert-encoded-file "mel-g"
104   "Insert file encoded by unofficial gzip64 format." t)
105
106
107 ;;; @ region
108 ;;;
109
110 ;;;###autoload
111 (defvar mime-encoding-method-alist
112   '(("base64"           . base64-encode-region)
113     ("quoted-printable" . quoted-printable-encode-region)
114     ;; Not standard, their use is DISCOURAGED.
115     ;; ("x-uue"            . uuencode-encode-region)
116     ;; ("x-gzip64"         . gzip64-encode-region)
117     ("7bit")
118     ("8bit")
119     ("binary")
120     )
121   "Alist of encoding vs. corresponding method to encode region.
122 Each element looks like (STRING . FUNCTION) or (STRING . nil).
123 STRING is content-transfer-encoding.
124 FUNCTION is region encoder and nil means not to encode.")
125
126 ;;;###autoload
127 (defvar mime-decoding-method-alist
128   '(("base64"           . base64-decode-region)
129     ("quoted-printable" . quoted-printable-decode-region)
130     ("x-uue"            . uuencode-decode-region)
131     ("x-uuencode"       . uuencode-decode-region)
132     ("x-gzip64"         . gzip64-decode-region)
133     )
134   "Alist of encoding vs. corresponding method to decode region.
135 Each element looks like (STRING . FUNCTION).
136 STRING is content-transfer-encoding.
137 FUNCTION is region decoder.")
138
139 ;;;###autoload
140 (defun mime-encode-region (start end encoding)
141   "Encode region START to END of current buffer using ENCODING.
142 ENCODING must be string.  If ENCODING is found in
143 `mime-encoding-method-alist' as its key, this function encodes the
144 region by its value."
145   (interactive
146    (list (region-beginning) (region-end)
147          (completing-read "encoding: "
148                           mime-encoding-method-alist
149                           nil t "base64"))
150    )
151   (let ((f (cdr (assoc encoding mime-encoding-method-alist))))
152     (if f
153         (funcall f start end)
154       )))
155
156 ;;;###autoload
157 (defun mime-decode-region (start end encoding)
158   "Decode region START to END of current buffer using ENCODING.
159 ENCODING must be string.  If ENCODING is found in
160 `mime-decoding-method-alist' as its key, this function decodes the
161 region by its value."
162   (interactive
163    (list (region-beginning) (region-end)
164          (completing-read "encoding: "
165                           mime-decoding-method-alist
166                           nil t "base64"))
167    )
168   (let ((f (cdr (assoc encoding mime-decoding-method-alist))))
169     (if f
170         (funcall f start end)
171       )))
172
173
174 ;;; @ file
175 ;;;
176
177 ;;;###autoload
178 (defvar mime-file-encoding-method-alist
179   '(("base64"           . base64-insert-encoded-file)
180     ("quoted-printable" . quoted-printable-insert-encoded-file)
181     ;; Not standard, their use is DISCOURAGED.
182     ;; ("x-uue"            . uuencode-insert-encoded-file)
183     ;; ("x-gzip64"         . gzip64-insert-encoded-file)
184     ("7bit"             . insert-binary-file-contents-literally)
185     ("8bit"             . insert-binary-file-contents-literally)
186     ("binary"           . insert-binary-file-contents-literally)
187     )
188   "Alist of encoding vs. corresponding method to insert encoded file.
189 Each element looks like (STRING . FUNCTION).
190 STRING is content-transfer-encoding.
191 FUNCTION is function to insert encoded file.")
192
193 ;;;###autoload
194 (defun mime-insert-encoded-file (filename encoding)
195   "Insert file FILENAME encoded by ENCODING format."
196   (interactive
197    (list (read-file-name "Insert encoded file: ")
198          (completing-read "encoding: "
199                           mime-encoding-method-alist
200                           nil t "base64"))
201    )
202   (let ((f (cdr (assoc encoding mime-file-encoding-method-alist))))
203     (if f
204         (funcall f filename)
205       )))
206
207
208 ;;; @ end
209 ;;;
210
211 (provide 'mel)
212
213 ;;; mel.el ends here.