2 ;;; $Id: mel-q.el,v 1.5 1995/06/26 05:56:39 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))
109 (while (re-search-forward "^.*$" nil t)
111 (quoted-printable-encode-string
112 (buffer-substring (match-beginning 0)(match-end 0))
116 (defun quoted-printable-internal-decode-region (beg end)
119 (narrow-to-region beg end)
120 (goto-char (point-min))
121 (while (re-search-forward "=\n" nil t)
124 (goto-char (point-min))
126 (while (re-search-forward quoted-printable-octet-regexp nil t)
127 (setq b (match-beginning 0))
128 (setq e (match-end 0))
129 (setq str (buffer-substring b e))
130 (replace-match (quoted-printable-decode-string str))
134 (cond ((boundp 'MULE)
135 (define-program-coding-system
136 nil (car quoted-printable-external-encoder) *noconv*)
137 (define-program-coding-system
138 nil (car quoted-printable-external-decoder) *noconv*)
141 (define-program-kanji-code
142 nil (car quoted-printable-external-encoder) 0)
143 (define-program-kanji-code
144 nil (car quoted-printable-external-decoder) 0)
147 (defun quoted-printable-external-encode-region (beg end)
149 (apply (function call-process-region)
150 beg end (car quoted-printable-external-encoder)
151 t t nil (cdr quoted-printable-external-encoder))
154 (defun quoted-printable-external-decode-region (beg end)
156 (apply (function call-process-region)
157 beg end (car quoted-printable-external-decoder)
158 t t nil (cdr quoted-printable-external-decoder))
161 (defun quoted-printable-encode-region (beg end)
163 (if (and quoted-printable-internal-encoding-limit
164 (> (- end beg) quoted-printable-internal-encoding-limit))
165 (quoted-printable-external-encode-region beg end)
166 (quoted-printable-internal-encode-region beg end)
169 (defun quoted-printable-decode-region (beg end)
171 (if (and quoted-printable-internal-decoding-limit
172 (> (- end beg) quoted-printable-internal-decoding-limit))
173 (quoted-printable-external-decode-region beg end)
174 (quoted-printable-internal-decode-region beg end)
178 ;;; @ Q-encoding encode/decode string
181 (defun q-encoding-encode-string-for-text (str)
184 (cond ((eq chr 32) "_")
185 ((or (< chr 32) (< 126 chr) (eq chr ?=))
186 (quoted-printable-quote-char chr)
188 (t (char-to-string chr))
192 (defun q-encoding-encode-string-for-comment (str)
195 (cond ((eq chr 32) "_")
196 ((or (< chr 32) (< 126 chr)
197 (memq chr '(?= ?\( ?\) ?\\))
199 (quoted-printable-quote-char chr)
201 (t (char-to-string chr))
205 (defun q-encoding-encode-string-for-phrase (str)
208 (cond ((or (and (<= ?A chr)(<= chr ?Z))
209 (and (<= ?a chr)(<= chr ?z))
210 (and (<= ?0 chr)(<= chr ?9))
211 (memq chr '(?! ?* ?+ ?- ?/))
215 (t (quoted-printable-quote-char chr))
219 (defun q-encoding-encode-string (str &optional mode)
220 (cond ((eq mode 'text)
221 (q-encoding-encode-string-for-text str)
224 (q-encoding-encode-string-for-comment str)
227 (q-encoding-encode-string-for-phrase str)
229 (t (quoted-printable-encode-string str))
232 (defun q-encoding-decode-string (str)
236 (cond ((eq chr ?_) " ")
240 (q (setq h (cond ((<= ?a chr) (+ (- chr ?a) 10))
241 ((<= ?A chr) (+ (- chr ?A) 10))
242 ((<= ?0 chr) (- chr ?0))
246 (h (setq l (cond ((<= ?a chr) (+ (- chr ?a) 10))
247 ((<= ?A chr) (+ (- chr ?A) 10))
248 ((<= ?0 chr) (- chr ?0))
251 (char-to-string (logior (ash h 4) l))
255 (t (char-to-string chr))
263 (defun q-encoding-encoded-length (string &optional mode)
264 (let ((l 0)(i 0)(len (length string)) chr)
266 (setq chr (elt string i))
267 (if (string-match "[A-Za-z0-9!*+/=_---]" (char-to-string chr))