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 (mel-define-method-function
148 (mime-encode-string string (nil "quoted-printable"))
149 'quoted-printable-encode-string)
151 (mel-define-method-function
152 (mime-encode-region start end (nil "quoted-printable"))
153 'quoted-printable-encode-region)
155 (mel-define-method mime-insert-encoded-file (filename
156 (nil "quoted-printable"))
157 "Encode contents of file FILENAME to quoted-printable, and insert the result.
158 It calls external quoted-printable encoder specified by
159 `quoted-printable-external-encoder'. So you must install the program
160 \(maybe mmencode included in metamail or XEmacs package)."
161 (interactive (list (read-file-name "Insert encoded file: ")))
162 (apply (function call-process) (car quoted-printable-external-encoder)
163 filename t nil (cdr quoted-printable-external-encoder))
167 ;;; @ Quoted-Printable decoder
170 (defsubst quoted-printable-hex-char-to-num (chr)
171 (cond ((<= ?a chr) (+ (- chr ?a) 10))
172 ((<= ?A chr) (+ (- chr ?A) 10))
173 ((<= ?0 chr) (- chr ?0))
176 (defun quoted-printable-internal-decode-region (start end)
179 (narrow-to-region start end)
180 (goto-char (point-min))
181 (while (search-forward "=" nil t)
182 (let ((beg (match-beginning 0)))
183 (cond ((looking-at "\n")
184 (delete-region beg (match-end 0))
187 `,(concat "[" quoted-printable-hex-chars
188 "][" quoted-printable-hex-chars "]"))
189 (let* ((end (match-end 0))
190 (hex (buffer-substring (match-beginning 0) end)))
191 (delete-region beg end)
194 (ash (quoted-printable-hex-char-to-num (aref hex 0)) 4)
195 (quoted-printable-hex-char-to-num (aref hex 1))))
202 (defvar quoted-printable-external-decoder '("mmencode" "-q" "-u")
203 "*list of quoted-printable decoder program name and its arguments.")
205 (defun quoted-printable-external-decode-region (start end)
208 (apply (function call-process-region)
209 start end (car quoted-printable-external-decoder)
210 t t nil (cdr quoted-printable-external-decoder))
214 (defvar quoted-printable-internal-decoding-limit nil
215 "*limit size to use internal quoted-printable decoder.
216 If size of input to decode is larger than this limit,
217 external decoder is called.")
219 (defun quoted-printable-decode-region (start end)
220 "Decode current region by quoted-printable.
221 START and END are buffer positions.
222 This function calls internal quoted-printable decoder if size of
223 region is smaller than `quoted-printable-internal-decoding-limit',
224 otherwise it calls external quoted-printable decoder specified by
225 `quoted-printable-external-decoder'. In this case, you must install
226 the program (maybe mmencode included in metamail or XEmacs package)."
228 (if (and quoted-printable-internal-decoding-limit
229 (> (- end start) quoted-printable-internal-decoding-limit))
230 (quoted-printable-external-decode-region start end)
231 (quoted-printable-internal-decode-region start end)
234 (defun quoted-printable-decode-string (string)
235 "Decode STRING which is encoded in quoted-printable, and return the result."
238 (quoted-printable-decode-region (point-min)(point-max))
242 (mel-define-method-function
243 (mime-decode-string string (nil "quoted-printable"))
244 'quoted-printable-decode-string)
246 (mel-define-method-function
247 (mime-decode-region start end (nil "quoted-printable"))
248 'quoted-printable-decode-region)
251 (defvar quoted-printable-external-decoder-option-to-specify-file '("-o")
252 "*list of options of quoted-printable decoder program to specify file.")
254 (mel-define-method mime-write-decoded-region (start end filename
255 (nil "quoted-printable"))
256 "Decode and write current region encoded by quoted-printable into FILENAME.
257 START and END are buffer positions."
259 (list (region-beginning) (region-end)
260 (read-file-name "Write decoded region to file: ")))
262 (apply (function call-process-region)
263 start end (car quoted-printable-external-decoder)
265 (append (cdr quoted-printable-external-decoder)
266 quoted-printable-external-decoder-option-to-specify-file
271 ;;; @ Q-encoding encode/decode string
274 (defconst q-encoding-special-chars-alist
276 (comment ?= ?? ?_ ?\( ?\) ?\\)
277 (phrase ?= ?? ?_ ?\( ?\) ?\\ ?\" ?# ?$ ?% ?& ?' ?, ?. ?/
278 ?: ?\; ?< ?> ?@ ?\[ ?\] ?^ ?` ?{ ?| ?} ?~)
281 (defun q-encoding-encode-string (string &optional mode)
282 "Encode STRING to Q-encoding of encoded-word, and return the result.
283 MODE allows `text', `comment', `phrase' or nil. Default value is
285 (let ((specials (cdr (or (assq mode q-encoding-special-chars-alist)
286 (assq 'phrase q-encoding-special-chars-alist)
290 (cond ((eq chr ? ) "_")
291 ((or (< chr 32) (< 126 chr)
294 (quoted-printable-quote-char chr)
303 (defun q-encoding-decode-string (string)
304 "Decode STRING which is encoded in Q-encoding and return the result."
308 (cond ((eq chr ?_) " ")
312 (q (setq h (quoted-printable-hex-char-to-num chr))
315 (h (setq l (quoted-printable-hex-char-to-num chr))
317 (char-to-string (logior (ash h 4) l))
321 (t (char-to-string chr))
325 (mel-define-method-function (encoded-text-encode-string string (nil "Q"))
326 'q-encoding-encode-string)
328 (mel-define-method encoded-text-decode-string (string (nil "Q"))
329 (if (and (string-match Q-encoded-text-regexp string)
330 (string= string (match-string 0 string)))
331 (q-encoding-decode-string string)
332 (error "Invalid encoded-text %s" string)))
340 ;;; mel-q.el ends here