1 ;;; mel-q.el: Quoted-Printable and Q-encoding encoder/decoder for GNU Emacs
3 ;; Copyright (C) 1995,1996,1997 Free Software Foundation, Inc.
5 ;; Author: MORIOKA Tomohiko <morioka@jaist.ac.jp>
7 ;; Version: $Id: mel-q.el,v 1.2 1998-06-15 05:54:28 steveb Exp $
8 ;; Keywords: MIME, Quoted-Printable, Q-encoding
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.
32 ;;; @ Quoted-Printable encoder
35 (defconst quoted-printable-hex-chars "0123456789ABCDEF")
37 (defsubst quoted-printable-quote-char (character)
40 (char-to-string (aref quoted-printable-hex-chars (ash character -4)))
41 (char-to-string (aref quoted-printable-hex-chars (logand character 15)))
44 (defun quoted-printable-internal-encode-region (start end)
47 (narrow-to-region start end)
50 enable-multibyte-characters)
51 (while (< (point)(point-max))
56 ((looking-at "^From ")
57 (replace-match "=46rom ")
61 ((looking-at "[ \t]\n")
68 (let ((chr (char-after (point))))
74 (and (<= 32 chr)(/= chr ?=)(< chr 127))
85 (insert (quoted-printable-quote-char chr))
91 (defvar quoted-printable-external-encoder '("mmencode" "-q")
92 "*list of quoted-printable encoder program name and its arguments.")
94 (defun quoted-printable-external-encode-region (start end)
97 (narrow-to-region start end)
99 (apply (function call-process-region)
100 start end (car quoted-printable-external-encoder)
101 t t nil (cdr quoted-printable-external-encoder))
104 ;; regularize line break code
105 (goto-char (point-min))
106 (while (re-search-forward "\r$" nil t)
111 (defvar quoted-printable-internal-encoding-limit
112 (if (and (featurep 'xemacs)(featurep 'mule))
114 (require 'file-detect)
115 (if (exec-installed-p "mmencode")
117 (message "Don't found external encoder for Quoted-Printable!")
119 "*limit size to use internal quoted-printable encoder.
120 If size of input to encode is larger than this limit,
121 external encoder is called.")
123 (defun quoted-printable-encode-region (start end)
124 "Encode current region by quoted-printable.
125 START and END are buffer positions.
126 This function calls internal quoted-printable encoder if size of
127 region is smaller than `quoted-printable-internal-encoding-limit',
128 otherwise it calls external quoted-printable encoder specified by
129 `quoted-printable-external-encoder'. In this case, you must install
130 the program (maybe mmencode included in metamail or XEmacs package)."
132 (if (and quoted-printable-internal-encoding-limit
133 (> (- end start) quoted-printable-internal-encoding-limit))
134 (quoted-printable-external-encode-region start end)
135 (quoted-printable-internal-encode-region start end)
138 (defun quoted-printable-encode-string (string)
139 "Encode STRING to quoted-printable, and return the result."
142 (quoted-printable-encode-region (point-min)(point-max))
146 (defun quoted-printable-insert-encoded-file (filename)
147 "Encode contents of file FILENAME to quoted-printable, and insert the result.
148 It calls external quoted-printable encoder specified by
149 `quoted-printable-external-encoder'. So you must install the program
150 \(maybe mmencode included in metamail or XEmacs package)."
151 (interactive (list (read-file-name "Insert encoded file: ")))
152 (apply (function call-process) (car quoted-printable-external-encoder)
153 filename t nil (cdr quoted-printable-external-encoder))
157 ;;; @ Quoted-Printable decoder
160 (defun quoted-printable-decode-string (string)
161 "Decode STRING which is encoded in quoted-printable, and return the result."
169 (cond ((<= ?a chr) (+ (- chr ?a) 10))
170 ((<= ?A chr) (+ (- chr ?A) 10))
171 ((<= ?0 chr) (- chr ?0))
175 (h (setq l (cond ((<= ?a chr) (+ (- chr ?a) 10))
176 ((<= ?A chr) (+ (- chr ?A) 10))
177 ((<= ?0 chr) (- chr ?0))
180 (char-to-string (logior (ash h 4) l))
184 (t (char-to-string chr))
188 (defconst quoted-printable-octet-regexp
189 (concat "=[" quoted-printable-hex-chars
190 "][" quoted-printable-hex-chars "]"))
192 (defun quoted-printable-internal-decode-region (start end)
195 (narrow-to-region start end)
196 (goto-char (point-min))
197 (while (re-search-forward "=\n" nil t)
200 (goto-char (point-min))
202 (while (re-search-forward quoted-printable-octet-regexp nil t)
203 (setq b (match-beginning 0))
204 (setq e (match-end 0))
205 (setq str (buffer-substring b e))
207 (insert (quoted-printable-decode-string str))
211 (defvar quoted-printable-external-decoder '("mmencode" "-q" "-u")
212 "*list of quoted-printable decoder program name and its arguments.")
214 (defun quoted-printable-external-decode-region (start end)
217 (apply (function call-process-region)
218 start end (car quoted-printable-external-decoder)
219 t t nil (cdr quoted-printable-external-decoder))
222 (defvar quoted-printable-internal-decoding-limit nil
223 "*limit size to use internal quoted-printable decoder.
224 If size of input to decode is larger than this limit,
225 external decoder is called.")
227 (defun quoted-printable-decode-region (start end)
228 "Decode current region by quoted-printable.
229 START and END are buffer positions.
230 This function calls internal quoted-printable decoder if size of
231 region is smaller than `quoted-printable-internal-decoding-limit',
232 otherwise it calls external quoted-printable decoder specified by
233 `quoted-printable-external-decoder'. In this case, you must install
234 the program (maybe mmencode included in metamail or XEmacs package)."
236 (if (and quoted-printable-internal-decoding-limit
237 (> (- end start) quoted-printable-internal-decoding-limit))
238 (quoted-printable-external-decode-region start end)
239 (quoted-printable-internal-decode-region start end)
243 ;;; @ Q-encoding encode/decode string
246 (defconst q-encoding-special-chars-alist
248 (comment ?= ?? ?_ ?\( ?\) ?\\)
249 (phrase ?= ?? ?_ ?\( ?\) ?\\ ?\" ?\# ?$ ?% ?& ?' ?, ?. ?/
250 ?: ?\; ?< ?> ?@ ?\[ ?\] ?^ ?` ?{ ?| ?} ?~)
253 (defun q-encoding-encode-string (string &optional mode)
254 "Encode STRING to Q-encoding of encoded-word, and return the result.
255 MODE allows `text', `comment', `phrase' or nil. Default value is
257 (let ((specials (cdr (or (assq mode q-encoding-special-chars-alist)
258 (assq 'phrase q-encoding-special-chars-alist)
262 (cond ((eq chr ? ) "_")
263 ((or (< chr 32) (< 126 chr)
266 (quoted-printable-quote-char chr)
275 (defun q-encoding-decode-string (string)
276 "Decode STRING which is encoded in Q-encoding and return the result."
280 (cond ((eq chr ?_) " ")
284 (q (setq h (cond ((<= ?a chr) (+ (- chr ?a) 10))
285 ((<= ?A chr) (+ (- chr ?A) 10))
286 ((<= ?0 chr) (- chr ?0))
290 (h (setq l (cond ((<= ?a chr) (+ (- chr ?a) 10))
291 ((<= ?A chr) (+ (- chr ?A) 10))
292 ((<= ?0 chr) (- chr ?0))
295 (char-to-string (logior (ash h 4) l))
299 (t (char-to-string chr))
307 (defun q-encoding-printable-char-p (chr mode)
308 (and (not (memq chr '(?= ?? ?_)))
309 (<= ?\ chr)(<= chr ?~)
310 (cond ((eq mode 'text) t)
312 (not (memq chr '(?\( ?\) ?\\)))
315 (string-match "[A-Za-z0-9!*+/=_---]" (char-to-string chr))
318 (defun q-encoding-encoded-length (string &optional mode)
319 (let ((l 0)(i 0)(len (length string)) chr)
321 (setq chr (elt string i))
322 (if (q-encoding-printable-char-p chr mode)
335 ;;; mel-q.el ends here