2 ;;; $Id: mel-q.el,v 2.1 1995/10/25 05:00:54 morioka Exp $
8 (defconst quoted-printable-hex-chars "0123456789ABCDEF")
9 (defconst quoted-printable-octet-regexp
10 (concat "=[" quoted-printable-hex-chars
11 "][" quoted-printable-hex-chars "]"))
17 (defvar quoted-printable-external-encoder '("mmencode" "-q")
18 "*list of quoted-printable encoder program name and its arguments.")
20 (defvar quoted-printable-external-decoder '("mmencode" "-q" "-u")
21 "*list of quoted-printable decoder program name and its arguments.")
23 (defvar quoted-printable-internal-encoding-limit 10000
24 "*limit size to use internal quoted-printable encoder.
25 If size of input to encode is larger than this limit,
26 external encoder is called.")
28 (defvar quoted-printable-internal-decoding-limit nil
29 "*limit size to use internal quoted-printable decoder.
30 If size of input to decode is larger than this limit,
31 external decoder is called.")
34 ;;; @ Quoted-Printable (Q-encode) encoder/decoder
37 (defun quoted-printable-quote-char (chr)
39 (char-to-string (elt quoted-printable-hex-chars (ash chr -4)))
40 (char-to-string (elt quoted-printable-hex-chars (logand chr 15)))
44 ;;; @@ Quoted-Printable encode/decode string
47 (defun quoted-printable-encode-string (str)
51 (cond ((or (< chr 32) (< 126 chr) (eq chr ?=))
55 (concat "=\n" (quoted-printable-quote-char chr))
59 (quoted-printable-quote-char chr)
64 (concat "=\n" (char-to-string chr))
73 (defun quoted-printable-decode-string (str)
81 (cond ((<= ?a chr) (+ (- chr ?a) 10))
82 ((<= ?A chr) (+ (- chr ?A) 10))
83 ((<= ?0 chr) (- chr ?0))
87 (h (setq l (cond ((<= ?a chr) (+ (- chr ?a) 10))
88 ((<= ?A chr) (+ (- chr ?A) 10))
89 ((<= ?0 chr) (- chr ?0))
92 (char-to-string (logior (ash h 4) l))
96 (t (char-to-string chr))
101 ;;; @@ Quoted-Printable encode/decode region
104 (defun quoted-printable-internal-encode-region (beg end)
107 (narrow-to-region beg end)
108 (goto-char (point-min))
112 (beginning-of-line) (setq b (point))
113 (end-of-line) (setq e (point))
116 (setq str (buffer-substring b e))
118 (insert (quoted-printable-encode-string str))
127 (defun quoted-printable-internal-decode-region (beg end)
130 (narrow-to-region beg end)
131 (goto-char (point-min))
132 (while (re-search-forward "=\n" nil t)
135 (goto-char (point-min))
137 (while (re-search-forward quoted-printable-octet-regexp nil t)
138 (setq b (match-beginning 0))
139 (setq e (match-end 0))
140 (setq str (buffer-substring b e))
142 (insert (quoted-printable-decode-string str))
146 (cond ((boundp 'MULE)
147 (define-program-coding-system
148 nil (car quoted-printable-external-encoder) *noconv*)
149 (define-program-coding-system
150 nil (car quoted-printable-external-decoder) *noconv*)
153 (define-program-kanji-code
154 nil (car quoted-printable-external-encoder) 0)
155 (define-program-kanji-code
156 nil (car quoted-printable-external-decoder) 0)
159 (defun quoted-printable-external-encode-region (beg end)
161 (let ((selective-display nil) ;Disable ^M to nl translation.
163 (kanji-flag nil)) ;NEmacs
164 (apply (function call-process-region)
165 beg end (car quoted-printable-external-encoder)
166 t t nil (cdr quoted-printable-external-encoder))
169 (defun quoted-printable-external-decode-region (beg end)
171 (let ((selective-display nil) ;Disable ^M to nl translation.
173 (kanji-flag nil)) ;NEmacs
174 (apply (function call-process-region)
175 beg end (car quoted-printable-external-decoder)
176 t t nil (cdr quoted-printable-external-decoder))
179 (defun quoted-printable-encode-region (beg end)
181 (if (and quoted-printable-internal-encoding-limit
182 (> (- end beg) quoted-printable-internal-encoding-limit))
183 (quoted-printable-external-encode-region beg end)
184 (quoted-printable-internal-encode-region beg end)
187 (defun quoted-printable-decode-region (beg end)
189 (if (and quoted-printable-internal-decoding-limit
190 (> (- end beg) quoted-printable-internal-decoding-limit))
191 (quoted-printable-external-decode-region beg end)
192 (quoted-printable-internal-decode-region beg end)
196 ;;; @ Q-encoding encode/decode string
199 (defun q-encoding-encode-string-for-text (str)
202 (cond ((eq chr 32) "_")
203 ((or (< chr 32) (< 126 chr) (eq chr ?=))
204 (quoted-printable-quote-char chr)
206 (t (char-to-string chr))
210 (defun q-encoding-encode-string-for-comment (str)
213 (cond ((eq chr 32) "_")
214 ((or (< chr 32) (< 126 chr)
215 (memq chr '(?= ?\( ?\) ?\\))
217 (quoted-printable-quote-char chr)
219 (t (char-to-string chr))
223 (defun q-encoding-encode-string-for-phrase (str)
226 (cond ((eq chr 32) "_")
227 ((or (and (<= ?A chr)(<= chr ?Z))
228 (and (<= ?a chr)(<= chr ?z))
229 (and (<= ?0 chr)(<= chr ?9))
230 (memq chr '(?! ?* ?+ ?- ?/))
234 (t (quoted-printable-quote-char chr))
238 (defun q-encoding-encode-string (str &optional mode)
239 (cond ((eq mode 'text)
240 (q-encoding-encode-string-for-text str)
243 (q-encoding-encode-string-for-comment str)
246 (q-encoding-encode-string-for-phrase str)
249 (defun q-encoding-decode-string (str)
253 (cond ((eq chr ?_) " ")
257 (q (setq h (cond ((<= ?a chr) (+ (- chr ?a) 10))
258 ((<= ?A chr) (+ (- chr ?A) 10))
259 ((<= ?0 chr) (- chr ?0))
263 (h (setq l (cond ((<= ?a chr) (+ (- chr ?a) 10))
264 ((<= ?A chr) (+ (- chr ?A) 10))
265 ((<= ?0 chr) (- chr ?0))
268 (char-to-string (logior (ash h 4) l))
272 (t (char-to-string chr))
280 (defun q-encoding-encoded-length (string &optional mode)
281 (let ((l 0)(i 0)(len (length string)) chr)
283 (setq chr (elt string i))
284 (if (string-match "[A-Za-z0-9!*+/=_---]" (char-to-string chr))