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.
32 ;;; @ Quoted-Printable encoder
35 (defsubst quoted-printable-quote-char (character)
38 (char-to-string (aref quoted-printable-hex-chars (ash character -4)))
39 (char-to-string (aref quoted-printable-hex-chars (logand character 15)))
42 (defun quoted-printable-internal-encode-region (start end)
45 (narrow-to-region start end)
48 enable-multibyte-characters)
49 (while (< (point)(point-max))
54 ((looking-at "^From ")
55 (replace-match "=46rom ")
59 ((looking-at "[ \t]\n")
66 (let ((chr (char-after (point))))
72 (and (<= 32 chr)(/= chr ?=)(< chr 127))
83 (insert (quoted-printable-quote-char chr))
90 (defvar quoted-printable-external-encoder '("mmencode" "-q")
91 "*list of quoted-printable encoder program name and its arguments.")
93 (defun quoted-printable-external-encode-region (start end)
96 (narrow-to-region start end)
98 (apply (function call-process-region)
99 start end (car quoted-printable-external-encoder)
100 t t nil (cdr quoted-printable-external-encoder))
103 ;; regularize line break code
104 (goto-char (point-min))
105 (while (re-search-forward "\r$" nil t)
111 (defvar quoted-printable-internal-encoding-limit
112 (if (and (featurep 'xemacs)(featurep 'mule))
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)
139 (defun quoted-printable-encode-string (string)
140 "Encode STRING to quoted-printable, and return the result."
143 (quoted-printable-encode-region (point-min)(point-max))
148 (defun quoted-printable-insert-encoded-file (filename)
149 "Encode contents of file FILENAME to quoted-printable, and insert the result.
150 It calls external quoted-printable encoder specified by
151 `quoted-printable-external-encoder'. So you must install the program
152 \(maybe mmencode included in metamail or XEmacs package)."
153 (interactive (list (read-file-name "Insert encoded file: ")))
154 (apply (function call-process) (car quoted-printable-external-encoder)
155 filename t nil (cdr quoted-printable-external-encoder))
159 ;;; @ Quoted-Printable decoder
162 (defun quoted-printable-decode-string (string)
163 "Decode STRING which is encoded in quoted-printable, and return the result."
171 (cond ((<= ?a chr) (+ (- chr ?a) 10))
172 ((<= ?A chr) (+ (- chr ?A) 10))
173 ((<= ?0 chr) (- chr ?0))
177 (h (setq l (cond ((<= ?a chr) (+ (- chr ?a) 10))
178 ((<= ?A chr) (+ (- chr ?A) 10))
179 ((<= ?0 chr) (- chr ?0))
182 (char-to-string (logior (ash h 4) l))
186 (t (char-to-string chr))
190 (defun quoted-printable-internal-decode-region (start end)
193 (narrow-to-region start end)
194 (goto-char (point-min))
195 (while (re-search-forward "=\n" nil t)
198 (goto-char (point-min))
200 (while (re-search-forward quoted-printable-octet-regexp nil t)
201 (setq b (match-beginning 0))
202 (setq e (match-end 0))
203 (setq str (buffer-substring b e))
205 (insert (string-as-multibyte (quoted-printable-decode-string str)))
210 (defvar quoted-printable-external-decoder '("mmencode" "-q" "-u")
211 "*list of quoted-printable decoder program name and its arguments.")
213 (defun quoted-printable-external-decode-region (start end)
216 (apply (function call-process-region)
217 start end (car quoted-printable-external-decoder)
218 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 (defvar quoted-printable-external-decoder-option-to-specify-file '("-o")
244 "*list of options of quoted-printable decoder program to specify file.")
246 (defun quoted-printable-write-decoded-region (start end filename)
247 "Decode and write current region encoded by quoted-printable into FILENAME.
248 START and END are buffer positions."
250 (list (region-beginning) (region-end)
251 (read-file-name "Write decoded region to file: ")))
253 (apply (function call-process-region)
254 start end (car quoted-printable-external-decoder)
256 (append (cdr quoted-printable-external-decoder)
257 quoted-printable-external-decoder-option-to-specify-file
262 ;;; @ Q-encoding encode/decode string
265 (defconst q-encoding-special-chars-alist
267 (comment ?= ?? ?_ ?\( ?\) ?\\)
268 (phrase ?= ?? ?_ ?\( ?\) ?\\ ?\" ?# ?$ ?% ?& ?' ?, ?. ?/
269 ?: ?\; ?< ?> ?@ ?\[ ?\] ?^ ?` ?{ ?| ?} ?~)
272 (defun q-encoding-encode-string (string &optional mode)
273 "Encode STRING to Q-encoding of encoded-word, and return the result.
274 MODE allows `text', `comment', `phrase' or nil. Default value is
276 (let ((specials (cdr (or (assq mode q-encoding-special-chars-alist)
277 (assq 'phrase q-encoding-special-chars-alist)
281 (cond ((eq chr ? ) "_")
282 ((or (< chr 32) (< 126 chr)
285 (quoted-printable-quote-char chr)
294 (defun q-encoding-decode-string (string)
295 "Decode STRING which is encoded in Q-encoding and return the result."
299 (cond ((eq chr ?_) " ")
303 (q (setq h (cond ((<= ?a chr) (+ (- chr ?a) 10))
304 ((<= ?A chr) (+ (- chr ?A) 10))
305 ((<= ?0 chr) (- chr ?0))
309 (h (setq l (cond ((<= ?a chr) (+ (- chr ?a) 10))
310 ((<= ?A chr) (+ (- chr ?A) 10))
311 ((<= ?0 chr) (- chr ?0))
314 (char-to-string (logior (ash h 4) l))
318 (t (char-to-string chr))
326 (defun q-encoding-printable-char-p (chr mode)
327 (and (not (memq chr '(?= ?? ?_)))
328 (<= ?\ chr)(<= chr ?~)
329 (cond ((eq mode 'text) t)
331 (not (memq chr '(?\( ?\) ?\\)))
334 (string-match "[A-Za-z0-9!*+/=_---]" (char-to-string chr))
337 (defun q-encoding-encoded-length (string &optional mode)
338 (let ((l 0)(i 0)(len (length string)) chr)
340 (setq chr (elt string i))
341 (if (q-encoding-printable-char-p chr mode)
354 ;;; mel-q.el ends here