1 ;;; mel-q.el: Quoted-Printable and Q-encoding encoder/decoder for GNU Emacs
3 ;; Copyright (C) 1995,1996,1997,1998 Free Software Foundation, Inc.
5 ;; Author: MORIOKA Tomohiko <morioka@jaist.ac.jp>
7 ;; Keywords: MIME, Quoted-Printable, Q-encoding
9 ;; This file is part of MEL (MIME Encoding Library).
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 ;;; @ Quoted-Printable encoder
34 (defconst quoted-printable-hex-chars "0123456789ABCDEF")
36 (defsubst quoted-printable-quote-char (character)
39 (char-to-string (aref quoted-printable-hex-chars (ash character -4)))
40 (char-to-string (aref quoted-printable-hex-chars (logand character 15)))
43 (defun quoted-printable-internal-encode-region (start end)
46 (narrow-to-region start end)
49 enable-multibyte-characters)
50 (while (< (point)(point-max))
55 ((looking-at "^From ")
56 (replace-match "=46rom ")
60 ((looking-at "[ \t]\n")
67 (let ((chr (char-after (point))))
73 (and (<= 32 chr)(/= chr ?=)(< chr 127))
84 (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)
112 (defvar quoted-printable-internal-encoding-limit
113 (if (and (featurep 'xemacs)(featurep 'mule))
116 (if (exec-installed-p "mmencode")
118 (message "Don't found external encoder for Quoted-Printable!")
120 "*limit size to use internal quoted-printable encoder.
121 If size of input to encode is larger than this limit,
122 external encoder is called.")
124 (defun quoted-printable-encode-region (start end)
125 "Encode current region by quoted-printable.
126 START and END are buffer positions.
127 This function calls internal quoted-printable encoder if size of
128 region is smaller than `quoted-printable-internal-encoding-limit',
129 otherwise it calls external quoted-printable encoder specified by
130 `quoted-printable-external-encoder'. In this case, you must install
131 the program (maybe mmencode included in metamail or XEmacs package)."
133 (if (and quoted-printable-internal-encoding-limit
134 (> (- end start) quoted-printable-internal-encoding-limit))
135 (quoted-printable-external-encode-region start end)
136 (quoted-printable-internal-encode-region start end)
140 (defun quoted-printable-encode-string (string)
141 "Encode STRING to quoted-printable, and return the result."
144 (quoted-printable-encode-region (point-min)(point-max))
149 (defun quoted-printable-insert-encoded-file (filename)
150 "Encode contents of file FILENAME to quoted-printable, and insert the result.
151 It calls external quoted-printable encoder specified by
152 `quoted-printable-external-encoder'. So you must install the program
153 \(maybe mmencode included in metamail or XEmacs package)."
154 (interactive (list (read-file-name "Insert encoded file: ")))
155 (apply (function call-process) (car quoted-printable-external-encoder)
156 filename t nil (cdr quoted-printable-external-encoder))
160 ;;; @ Quoted-Printable decoder
163 (defun quoted-printable-decode-string (string)
164 "Decode STRING which is encoded in quoted-printable, and return the result."
172 (cond ((<= ?a chr) (+ (- chr ?a) 10))
173 ((<= ?A chr) (+ (- chr ?A) 10))
174 ((<= ?0 chr) (- chr ?0))
178 (h (setq l (cond ((<= ?a chr) (+ (- chr ?a) 10))
179 ((<= ?A chr) (+ (- chr ?A) 10))
180 ((<= ?0 chr) (- chr ?0))
183 (char-to-string (logior (ash h 4) l))
187 (t (char-to-string chr))
191 (defconst quoted-printable-octet-regexp
192 (concat "=[" quoted-printable-hex-chars
193 "][" quoted-printable-hex-chars "]"))
195 (defun quoted-printable-internal-decode-region (start end)
198 (narrow-to-region start end)
199 (goto-char (point-min))
200 (while (re-search-forward "=\n" nil t)
203 (goto-char (point-min))
205 (while (re-search-forward quoted-printable-octet-regexp nil t)
206 (setq b (match-beginning 0))
207 (setq e (match-end 0))
208 (setq str (buffer-substring b e))
210 (insert (quoted-printable-decode-string str))
215 (defvar quoted-printable-external-decoder '("mmencode" "-q" "-u")
216 "*list of quoted-printable decoder program name and its arguments.")
218 (defun quoted-printable-external-decode-region (start end)
221 (apply (function call-process-region)
222 start end (car quoted-printable-external-decoder)
223 t t nil (cdr quoted-printable-external-decoder))
227 (defvar quoted-printable-internal-decoding-limit nil
228 "*limit size to use internal quoted-printable decoder.
229 If size of input to decode is larger than this limit,
230 external decoder is called.")
232 (defun quoted-printable-decode-region (start end)
233 "Decode current region by quoted-printable.
234 START and END are buffer positions.
235 This function calls internal quoted-printable decoder if size of
236 region is smaller than `quoted-printable-internal-decoding-limit',
237 otherwise it calls external quoted-printable decoder specified by
238 `quoted-printable-external-decoder'. In this case, you must install
239 the program (maybe mmencode included in metamail or XEmacs package)."
241 (if (and quoted-printable-internal-decoding-limit
242 (> (- end start) quoted-printable-internal-decoding-limit))
243 (quoted-printable-external-decode-region start end)
244 (quoted-printable-internal-decode-region start end)
248 (defvar quoted-printable-external-decoder-option-to-specify-file '("-o")
249 "*list of options of quoted-printable decoder program to specify file.")
251 (defun quoted-printable-write-decoded-region (start end filename)
252 "Decode and write current region encoded by quoted-printable into FILENAME.
253 START and END are buffer positions."
255 (list (region-beginning) (region-end)
256 (read-file-name "Write decoded region to file: ")))
258 (apply (function call-process-region)
259 start end (car quoted-printable-external-decoder)
261 (append (cdr quoted-printable-external-decoder)
262 quoted-printable-external-decoder-option-to-specify-file
267 ;;; @ Q-encoding encode/decode string
270 (defconst q-encoding-special-chars-alist
272 (comment ?= ?? ?_ ?\( ?\) ?\\)
273 (phrase ?= ?? ?_ ?\( ?\) ?\\ ?\" ?# ?$ ?% ?& ?' ?, ?. ?/
274 ?: ?\; ?< ?> ?@ ?\[ ?\] ?^ ?` ?{ ?| ?} ?~)
277 (defun q-encoding-encode-string (string &optional mode)
278 "Encode STRING to Q-encoding of encoded-word, and return the result.
279 MODE allows `text', `comment', `phrase' or nil. Default value is
281 (let ((specials (cdr (or (assq mode q-encoding-special-chars-alist)
282 (assq 'phrase q-encoding-special-chars-alist)
286 (cond ((eq chr ? ) "_")
287 ((or (< chr 32) (< 126 chr)
290 (quoted-printable-quote-char chr)
299 (defun q-encoding-decode-string (string)
300 "Decode STRING which is encoded in Q-encoding and return the result."
304 (cond ((eq chr ?_) " ")
308 (q (setq h (cond ((<= ?a chr) (+ (- chr ?a) 10))
309 ((<= ?A chr) (+ (- chr ?A) 10))
310 ((<= ?0 chr) (- chr ?0))
314 (h (setq l (cond ((<= ?a chr) (+ (- chr ?a) 10))
315 ((<= ?A chr) (+ (- chr ?A) 10))
316 ((<= ?0 chr) (- chr ?0))
319 (char-to-string (logior (ash h 4) l))
323 (t (char-to-string chr))
331 (defun q-encoding-printable-char-p (chr mode)
332 (and (not (memq chr '(?= ?? ?_)))
333 (<= ?\ chr)(<= chr ?~)
334 (cond ((eq mode 'text) t)
336 (not (memq chr '(?\( ?\) ?\\)))
339 (string-match "[A-Za-z0-9!*+/=_---]" (char-to-string chr))
342 (defun q-encoding-encoded-length (string &optional mode)
343 (let ((l 0)(i 0)(len (length string)) chr)
345 (setq chr (elt string i))
346 (if (q-encoding-printable-char-p chr mode)
359 ;;; mel-q.el ends here