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 (while (< (point)(point-max))
53 ((looking-at-as-unibyte "^From ")
54 (replace-match "=46rom ")
58 ((looking-at-as-unibyte "[ \t]\n")
65 (let ((chr (char-after (point))))
71 (and (<= 32 chr)(/= chr ?=)(< chr 127))
82 (insert (quoted-printable-quote-char chr))
89 (defvar quoted-printable-external-encoder '("mmencode" "-q")
90 "*list of quoted-printable encoder program name and its arguments.")
92 (defun quoted-printable-external-encode-region (start end)
95 (narrow-to-region start end)
97 (apply (function call-process-region)
98 start end (car quoted-printable-external-encoder)
99 t t nil (cdr quoted-printable-external-encoder))
102 ;; regularize line break code
103 (goto-char (point-min))
104 (while (re-search-forward "\r$" nil t)
110 (defvar quoted-printable-internal-encoding-limit
111 (if (and (featurep 'xemacs)(featurep 'mule))
114 (if (exec-installed-p "mmencode")
116 (message "Don't found external encoder for Quoted-Printable!")
118 "*limit size to use internal quoted-printable encoder.
119 If size of input to encode is larger than this limit,
120 external encoder is called.")
122 (defun quoted-printable-encode-region (start end)
123 "Encode current region by quoted-printable.
124 START and END are buffer positions.
125 This function calls internal quoted-printable encoder if size of
126 region is smaller than `quoted-printable-internal-encoding-limit',
127 otherwise it calls external quoted-printable encoder specified by
128 `quoted-printable-external-encoder'. In this case, you must install
129 the program (maybe mmencode included in metamail or XEmacs package)."
131 (if (and quoted-printable-internal-encoding-limit
132 (> (- end start) quoted-printable-internal-encoding-limit))
133 (quoted-printable-external-encode-region start end)
134 (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))
147 (defun quoted-printable-insert-encoded-file (filename)
148 "Encode contents of file FILENAME to quoted-printable, and insert the result.
149 It calls external quoted-printable encoder specified by
150 `quoted-printable-external-encoder'. So you must install the program
151 \(maybe mmencode included in metamail or XEmacs package)."
152 (interactive (list (read-file-name "Insert encoded file: ")))
153 (apply (function call-process) (car quoted-printable-external-encoder)
154 filename t nil (cdr quoted-printable-external-encoder))
158 ;;; @ Quoted-Printable decoder
161 (defun quoted-printable-decode-string (string)
162 "Decode STRING which is encoded in quoted-printable, and return the result."
170 (cond ((<= ?a chr) (+ (- chr ?a) 10))
171 ((<= ?A chr) (+ (- chr ?A) 10))
172 ((<= ?0 chr) (- chr ?0))
176 (h (setq l (cond ((<= ?a chr) (+ (- chr ?a) 10))
177 ((<= ?A chr) (+ (- chr ?A) 10))
178 ((<= ?0 chr) (- chr ?0))
181 (char-to-string (logior (ash h 4) l))
185 (t (char-to-string chr))
189 (defun quoted-printable-internal-decode-region (start end)
192 (narrow-to-region start end)
193 (goto-char (point-min))
194 (while (re-search-forward "=\n" nil t)
197 (goto-char (point-min))
199 (while (re-search-forward quoted-printable-octet-regexp nil t)
200 (setq b (match-beginning 0))
201 (setq e (match-end 0))
202 (setq str (buffer-substring b e))
204 (insert (string-as-multibyte (quoted-printable-decode-string str)))
209 (defvar quoted-printable-external-decoder '("mmencode" "-q" "-u")
210 "*list of quoted-printable decoder program name and its arguments.")
212 (defun quoted-printable-external-decode-region (start end)
215 (apply (function call-process-region)
216 start end (car quoted-printable-external-decoder)
217 t t nil (cdr quoted-printable-external-decoder))
221 (defvar quoted-printable-internal-decoding-limit nil
222 "*limit size to use internal quoted-printable decoder.
223 If size of input to decode is larger than this limit,
224 external decoder is called.")
226 (defun quoted-printable-decode-region (start end)
227 "Decode current region by quoted-printable.
228 START and END are buffer positions.
229 This function calls internal quoted-printable decoder if size of
230 region is smaller than `quoted-printable-internal-decoding-limit',
231 otherwise it calls external quoted-printable decoder specified by
232 `quoted-printable-external-decoder'. In this case, you must install
233 the program (maybe mmencode included in metamail or XEmacs package)."
235 (if (and quoted-printable-internal-decoding-limit
236 (> (- end start) quoted-printable-internal-decoding-limit))
237 (quoted-printable-external-decode-region start end)
238 (quoted-printable-internal-decode-region start end)
242 (defvar quoted-printable-external-decoder-option-to-specify-file '("-o")
243 "*list of options of quoted-printable decoder program to specify file.")
245 (defun quoted-printable-write-decoded-region (start end filename)
246 "Decode and write current region encoded by quoted-printable into FILENAME.
247 START and END are buffer positions."
249 (list (region-beginning) (region-end)
250 (read-file-name "Write decoded region to file: ")))
252 (apply (function call-process-region)
253 start end (car quoted-printable-external-decoder)
255 (append (cdr quoted-printable-external-decoder)
256 quoted-printable-external-decoder-option-to-specify-file
261 ;;; @ Q-encoding encode/decode string
264 (defconst q-encoding-special-chars-alist
266 (comment ?= ?? ?_ ?\( ?\) ?\\)
267 (phrase ?= ?? ?_ ?\( ?\) ?\\ ?\" ?# ?$ ?% ?& ?' ?, ?. ?/
268 ?: ?\; ?< ?> ?@ ?\[ ?\] ?^ ?` ?{ ?| ?} ?~)
271 (defun q-encoding-encode-string (string &optional mode)
272 "Encode STRING to Q-encoding of encoded-word, and return the result.
273 MODE allows `text', `comment', `phrase' or nil. Default value is
275 (let ((specials (cdr (or (assq mode q-encoding-special-chars-alist)
276 (assq 'phrase q-encoding-special-chars-alist)
280 (cond ((eq chr ? ) "_")
281 ((or (< chr 32) (< 126 chr)
284 (quoted-printable-quote-char chr)
293 (defun q-encoding-decode-string (string)
294 "Decode STRING which is encoded in Q-encoding and return the result."
298 (cond ((eq chr ?_) " ")
302 (q (setq h (cond ((<= ?a chr) (+ (- chr ?a) 10))
303 ((<= ?A chr) (+ (- chr ?A) 10))
304 ((<= ?0 chr) (- chr ?0))
308 (h (setq l (cond ((<= ?a chr) (+ (- chr ?a) 10))
309 ((<= ?A chr) (+ (- chr ?A) 10))
310 ((<= ?0 chr) (- chr ?0))
313 (char-to-string (logior (ash h 4) l))
317 (t (char-to-string chr))
325 (defun q-encoding-printable-char-p (chr mode)
326 (and (not (memq chr '(?= ?? ?_)))
327 (<= ?\ chr)(<= chr ?~)
328 (cond ((eq mode 'text) t)
330 (not (memq chr '(?\( ?\) ?\\)))
333 (string-match "[A-Za-z0-9!*+/=_---]" (char-to-string chr))
336 (defun q-encoding-encoded-length (string &optional mode)
337 (let ((l 0)(i 0)(len (length string)) chr)
339 (setq chr (elt string i))
340 (if (q-encoding-printable-char-p chr mode)
353 ;;; mel-q.el ends here