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>
7 ;; Keywords: MIME, Base64, Quoted-Printable, uuencode, gzip64
9 ;; This file is part of FLIM (Faithful Library about Internet Message).
11 ;; This program is free software; you can redistribute it and/or
12 ;; modify it under the terms of the GNU General Public License as
13 ;; published by the Free Software Foundation; either version 2, or (at
14 ;; your option) any later version.
16 ;; This program is distributed in the hope that it will be useful, but
17 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 ;; General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
31 (defcustom mime-encoding-list
32 '("7bit" "8bit" "binary" "base64" "quoted-printable")
33 "List of Content-Transfer-Encoding. Each encoding must be string."
35 :type '(repeat string))
37 (defvar mel-encoding-module-alist nil)
39 (defun mime-encoding-list (&optional service)
40 "Return list of Content-Transfer-Encoding."
43 (mapatoms (lambda (sym)
45 (setq dest (cons (symbol-name sym) dest)))
47 (symbol-value (intern (format "%s-obarray" service))))
48 (let ((rest mel-encoding-module-alist)
50 (while (setq pair (car rest))
51 (let ((key (car pair)))
54 (setq dest (cons key dest))))
55 (setq rest (cdr rest)))
60 (defun mime-encoding-alist (&optional service)
61 "Return table of Content-Transfer-Encoding for completion."
62 (mapcar #'list (mime-encoding-list service))
65 (defsubst mel-use-module (name encodings)
67 (while (setq encoding (car encodings))
68 (set-alist 'mel-encoding-module-alist
70 (cons name (cdr (assoc encoding mel-encoding-module-alist))))
71 (setq encodings (cdr encodings))
74 (defsubst mel-find-function (service encoding)
75 (let* ((oba (symbol-value (intern (format "%s-obarray" service))))
76 (f (intern-soft encoding oba)))
78 (let ((rest (cdr (assoc encoding mel-encoding-module-alist))))
82 (null (setq f (intern-soft encoding oba)))
84 (setq rest (cdr rest))
89 ;;; @ setting for modules
92 (defvar base64-dl-module
93 (and (fboundp 'dynamic-link)
94 (let ((path (expand-file-name "base64.so" exec-directory)))
95 (and (file-exists-p path)
99 (mel-use-module 'mel-b '("base64" "B"))
100 (mel-use-module 'mel-q '("quoted-printable" "Q"))
101 (mel-use-module 'mel-g '("x-gzip64"))
102 (mel-use-module 'mel-u '("x-uue" "x-uuencode"))
105 (mel-use-module 'mel-ccl '("base64" "quoted-printable" "B" "Q"))
109 (mel-use-module 'mel-b-dl '("base64" "B"))
112 (mel-define-method-function (mime-encode-string string (nil "7bit"))
114 (mel-define-method-function (mime-decode-string string (nil "7bit"))
116 (mel-define-method mime-encode-region (start end (nil "7bit")))
117 (mel-define-method mime-decode-region (start end (nil "7bit")))
118 (mel-define-method-function (mime-insert-encoded-file filename (nil "7bit"))
119 'insert-file-contents-as-binary)
120 (mel-define-method-function (mime-write-decoded-region
121 start end filename (nil "7bit"))
122 'write-region-as-binary)
124 (mel-define-method-function (mime-encode-string string (nil "8bit"))
126 (mel-define-method-function (mime-decode-string string (nil "8bit"))
128 (mel-define-method mime-encode-region (start end (nil "8bit")))
129 (mel-define-method mime-decode-region (start end (nil "8bit")))
130 (mel-define-method-function (mime-insert-encoded-file filename (nil "8bit"))
131 'insert-file-contents-as-binary)
132 (mel-define-method-function (mime-write-decoded-region
133 start end filename (nil "8bit"))
134 'write-region-as-binary)
136 (mel-define-method-function (mime-encode-string string (nil "binary"))
138 (mel-define-method-function (mime-decode-string string (nil "binary"))
140 (mel-define-method mime-encode-region (start end (nil "binary")))
141 (mel-define-method mime-decode-region (start end (nil "binary")))
142 (mel-define-method-function (mime-insert-encoded-file filename (nil "binary"))
143 'insert-file-contents-as-binary)
144 (mel-define-method-function (mime-write-decoded-region
145 start end filename (nil "binary"))
146 'write-region-as-binary)
153 (defun mime-encode-region (start end encoding)
154 "Encode region START to END of current buffer using ENCODING.
155 ENCODING must be string."
157 (list (region-beginning) (region-end)
158 (completing-read "encoding: "
159 (mime-encoding-alist)
161 (funcall (mel-find-function 'mime-encode-region encoding) start end)
166 (defun mime-decode-region (start end encoding)
167 "Decode region START to END of current buffer using ENCODING.
168 ENCODING must be string."
170 (list (region-beginning) (region-end)
171 (completing-read "encoding: "
172 (mime-encoding-alist 'mime-decode-region)
174 (funcall (mel-find-function 'mime-decode-region encoding)
182 (defun mime-decode-string (string encoding)
183 "Decode STRING using ENCODING.
184 ENCODING must be string. If ENCODING is found in
185 `mime-string-decoding-method-alist' as its key, this function decodes
186 the STRING by its value."
187 (funcall (mel-find-function 'mime-decode-string encoding)
191 (mel-define-service encoded-text-encode-string (string encoding)
192 "Encode STRING as encoded-text using ENCODING.
193 ENCODING must be string.")
195 (mel-define-service encoded-text-decode-string (string encoding)
196 "Decode STRING as encoded-text using ENCODING.
197 ENCODING must be string.")
199 (defun base64-encoded-length (string)
200 (* (/ (+ (length string) 2) 3) 4))
202 (defsubst Q-encoding-printable-char-p (chr mode)
203 (and (not (memq chr '(?= ?? ?_)))
204 (<= ?\ chr)(<= chr ?~)
205 (cond ((eq mode 'text) t)
207 (not (memq chr '(?\( ?\) ?\\)))
210 (string-match "[A-Za-z0-9!*+/=_---]" (char-to-string chr))
213 (defun Q-encoded-text-length (string &optional mode)
214 (let ((l 0)(i 0)(len (length string)) chr)
216 (setq chr (elt string i))
217 (if (Q-encoding-printable-char-p chr mode)
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-alist)
236 (funcall (mel-find-function 'mime-insert-encoded-file encoding)
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-encoding-alist 'mime-write-decoded-region)
250 (funcall (mel-find-function 'mime-write-decoded-region encoding)
259 ;;; mel.el ends here.