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 6.5 1997/04/30 17:23:04 morioka 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.
35 (defconst quoted-printable-hex-chars "0123456789ABCDEF")
36 (defconst quoted-printable-octet-regexp
37 (concat "=[" quoted-printable-hex-chars
38 "][" quoted-printable-hex-chars "]"))
44 (defvar quoted-printable-external-encoder '("mmencode" "-q")
45 "*list of quoted-printable encoder program name and its arguments.")
47 (defvar quoted-printable-external-decoder '("mmencode" "-q" "-u")
48 "*list of quoted-printable decoder program name and its arguments.")
50 (defvar quoted-printable-internal-encoding-limit 10000
51 "*limit size to use internal quoted-printable encoder.
52 If size of input to encode is larger than this limit,
53 external encoder is called.")
55 (defvar quoted-printable-internal-decoding-limit nil
56 "*limit size to use internal quoted-printable decoder.
57 If size of input to decode is larger than this limit,
58 external decoder is called.")
61 ;;; @ Quoted-Printable (Q-encode) encoder/decoder
64 (defun byte-to-hex-string (num)
65 (concat (char-to-string (elt quoted-printable-hex-chars (ash num -4)))
66 (char-to-string (elt quoted-printable-hex-chars (logand num 15)))
69 (defun quoted-printable-quote-char (chr)
71 (char-to-string (elt quoted-printable-hex-chars (ash chr -4)))
72 (char-to-string (elt quoted-printable-hex-chars (logand chr 15)))
76 ;;; @@ Quoted-Printable encoder/decoder for string
79 (defun quoted-printable-encode-string (string)
80 "Encode STRING to quoted-printable, and return the result."
87 ((or (< chr 32) (< 126 chr) (eq chr ?=))
91 (concat "=\n" (quoted-printable-quote-char chr))
95 (quoted-printable-quote-char chr)
100 (concat "=\n" (char-to-string chr))
109 (defun quoted-printable-decode-string (string)
110 "Decode STRING which is encoded in quoted-printable, and return the result."
118 (cond ((<= ?a chr) (+ (- chr ?a) 10))
119 ((<= ?A chr) (+ (- chr ?A) 10))
120 ((<= ?0 chr) (- chr ?0))
124 (h (setq l (cond ((<= ?a chr) (+ (- chr ?a) 10))
125 ((<= ?A chr) (+ (- chr ?A) 10))
126 ((<= ?0 chr) (- chr ?0))
129 (char-to-string (logior (ash h 4) l))
133 (t (char-to-string chr))
138 ;;; @@ Quoted-Printable encoder/decoder for region
141 (defun quoted-printable-internal-encode-region (beg end)
144 (narrow-to-region beg end)
145 (let ((str (buffer-substring beg end)))
146 (delete-region beg end)
147 (insert (quoted-printable-encode-string str))
154 (defun quoted-printable-internal-decode-region (beg end)
157 (narrow-to-region beg end)
158 (goto-char (point-min))
159 (while (re-search-forward "=\n" nil t)
162 (goto-char (point-min))
164 (while (re-search-forward quoted-printable-octet-regexp nil t)
165 (setq b (match-beginning 0))
166 (setq e (match-end 0))
167 (setq str (buffer-substring b e))
169 (insert (quoted-printable-decode-string str))
173 (defun quoted-printable-external-encode-region (beg end)
176 (narrow-to-region beg end)
178 (apply (function call-process-region)
179 beg end (car quoted-printable-external-encoder)
180 t t nil (cdr quoted-printable-external-encoder))
183 ;; regularize line break code
184 (goto-char (point-min))
185 (while (re-search-forward "\r$" nil t)
190 (defun quoted-printable-external-decode-region (beg end)
193 (apply (function call-process-region)
194 beg end (car quoted-printable-external-decoder)
195 t t nil (cdr quoted-printable-external-decoder))
198 (defun quoted-printable-encode-region (beg end)
199 "Encode current region by quoted-printable.
200 START and END are buffer positions.
201 This function calls internal quoted-printable encoder if size of
202 region is smaller than `quoted-printable-internal-encoding-limit',
203 otherwise it calls external quoted-printable encoder specified by
204 `quoted-printable-external-encoder'. In this case, you must install
205 the program (maybe mmencode included in metamail or XEmacs package)."
207 (if (and quoted-printable-internal-encoding-limit
208 (> (- end beg) quoted-printable-internal-encoding-limit))
209 (quoted-printable-external-encode-region beg end)
210 (quoted-printable-internal-encode-region beg end)
213 (defun quoted-printable-decode-region (beg end)
214 "Decode current region by quoted-printable.
215 START and END are buffer positions.
216 This function calls internal quoted-printable decoder if size of
217 region is smaller than `quoted-printable-internal-decoding-limit',
218 otherwise it calls external quoted-printable decoder specified by
219 `quoted-printable-external-decoder'. In this case, you must install
220 the program (maybe mmencode included in metamail or XEmacs package)."
222 (if (and quoted-printable-internal-decoding-limit
223 (> (- end beg) quoted-printable-internal-decoding-limit))
224 (quoted-printable-external-decode-region beg end)
225 (quoted-printable-internal-decode-region beg end)
229 ;;; @@ Quoted-Printable encoder/decoder for file
232 (defun quoted-printable-insert-encoded-file (filename)
233 "Encode contents of file FILENAME to quoted-printable, and insert the result.
234 It calls external quoted-printable encoder specified by
235 `quoted-printable-external-encoder'. So you must install the program
236 (maybe mmencode included in metamail or XEmacs package)."
237 (interactive (list (read-file-name "Insert encoded file: ")))
238 (apply (function call-process) (car quoted-printable-external-encoder)
239 filename t nil (cdr quoted-printable-external-encoder))
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 32) "_")
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