* mel-ccl.el (mel-ccl-encode-q-generic): New compile-time
[elisp/flim.git] / mel-q.el
1 ;;; mel-q.el: Quoted-Printable and Q-encoding encoder/decoder for GNU Emacs
2
3 ;; Copyright (C) 1995,1996,1997,1998 Free Software Foundation, Inc.
4
5 ;; Author: MORIOKA Tomohiko <morioka@jaist.ac.jp>
6 ;; Created: 1995/6/25
7 ;; Keywords: MIME, Quoted-Printable, Q-encoding
8
9 ;; This file is part of MEL (MIME Encoding Library).
10
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.
15
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.
20
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.
25
26 ;;; Code:
27
28 (require 'emu)
29 (require 'mime-def)
30
31
32 ;;; @ Quoted-Printable encoder
33 ;;;
34
35 (defsubst quoted-printable-quote-char (character)
36   (concat
37    "="
38    (char-to-string (aref quoted-printable-hex-chars (ash character -4)))
39    (char-to-string (aref quoted-printable-hex-chars (logand character 15)))
40    ))
41
42 (defun quoted-printable-internal-encode-region (start end)
43   (save-excursion
44     (save-restriction
45       (narrow-to-region start end)
46       (goto-char start)
47       (let ((col 0))
48         (while (< (point)(point-max))
49           (cond ((>= col 75)
50                  (insert "=\n")
51                  (setq col 0)
52                  )
53                 ((looking-at-as-unibyte "^From ")
54                  (replace-match "=46rom ")
55                  (backward-char 1)
56                  (setq col (+ col 6))
57                  )
58                 ((looking-at-as-unibyte "[ \t]\n")
59                  (forward-char 1)
60                  (insert "=\n")
61                  (forward-char 1)
62                  (setq col 0)
63                  )
64                 (t
65                  (let ((chr (char-after (point))))
66                    (cond ((= chr ?\n)
67                           (forward-char 1)
68                           (setq col 0)
69                           )
70                          ((or (= chr ?\t)
71                               (and (<= 32 chr)(/= chr ?=)(< chr 127))
72                               )
73                           (forward-char 1)
74                           (setq col (1+ col))
75                           )
76                          ((>= col 73)
77                           (insert "=\n")
78                           (setq col 0)
79                           )
80                          (t
81                           (delete-char 1)
82                           (insert (quoted-printable-quote-char chr))
83                           (setq col (+ col 3))
84                           ))
85                    )))
86           )))))
87
88
89 (defvar quoted-printable-external-encoder '("mmencode" "-q")
90   "*list of quoted-printable encoder program name and its arguments.")
91
92 (defun quoted-printable-external-encode-region (start end)
93   (save-excursion
94     (save-restriction
95       (narrow-to-region start end)
96       (as-binary-process
97        (apply (function call-process-region)
98               start end (car quoted-printable-external-encoder)
99               t t nil (cdr quoted-printable-external-encoder))
100        )
101       ;; for OS/2
102       ;;   regularize line break code
103       (goto-char (point-min))
104       (while (re-search-forward "\r$" nil t)
105         (replace-match "")
106         )
107       )))
108
109 (defun quoted-printable-internal-encode-string (string)
110   "Encode STRING to quoted-printable, and return the result."
111   (with-temp-buffer
112     (insert string)
113     (quoted-printable-internal-encode-region (point-min)(point-max))
114     (buffer-string)
115     ))
116
117 (defun quoted-printable-external-encode-string (string)
118   "Encode STRING to quoted-printable, and return the result."
119   (with-temp-buffer
120     (insert string)
121     (quoted-printable-external-encode-region (point-min)(point-max))
122     (buffer-string)
123     ))
124
125 (defun quoted-printable-external-insert-encoded-file (filename)
126   "Encode contents of file FILENAME to quoted-printable, and insert the result.
127 It calls external quoted-printable encoder specified by
128 `quoted-printable-external-encoder'.  So you must install the program
129 \(maybe mmencode included in metamail or XEmacs package)."
130   (interactive (list (read-file-name "Insert encoded file: ")))
131   (apply (function call-process) (car quoted-printable-external-encoder)
132          filename t nil (cdr quoted-printable-external-encoder))
133   )
134
135
136 ;;; @ Quoted-Printable decoder
137 ;;;
138
139 (defsubst quoted-printable-hex-char-to-num (chr)
140   (cond ((<= ?a chr) (+ (- chr ?a) 10))
141         ((<= ?A chr) (+ (- chr ?A) 10))
142         ((<= ?0 chr) (- chr ?0))
143         ))
144
145 (defun quoted-printable-internal-decode-region (start end)
146   (save-excursion
147     (save-restriction
148       (narrow-to-region start end)
149       (goto-char (point-min))
150       (while (search-forward "=" nil t)
151         (let ((beg (match-beginning 0)))
152           (cond ((looking-at "\n")
153                  (delete-region beg (match-end 0))
154                  )
155                 ((looking-at
156                   `,(concat "[" quoted-printable-hex-chars
157                             "][" quoted-printable-hex-chars "]"))
158                  (let* ((end (match-end 0))
159                         (hex (buffer-substring (match-beginning 0) end)))
160                    (delete-region beg end)
161                    (insert
162                     (logior
163                      (ash (quoted-printable-hex-char-to-num (aref hex 0)) 4)
164                      (quoted-printable-hex-char-to-num (aref hex 1))))
165                    ))
166                 (t
167                  ;; invalid
168                  ))
169           )))))
170
171 (defvar quoted-printable-external-decoder '("mmencode" "-q" "-u")
172   "*list of quoted-printable decoder program name and its arguments.")
173
174 (defun quoted-printable-external-decode-region (start end)
175   (save-excursion
176     (as-binary-process
177      (apply (function call-process-region)
178             start end (car quoted-printable-external-decoder)
179             t t nil (cdr quoted-printable-external-decoder))
180      )))
181
182 (defun quoted-printable-internal-decode-string (string)
183   "Decode STRING which is encoded in quoted-printable, and return the result."
184   (with-temp-buffer
185     (insert string)
186     (quoted-printable-internal-decode-region (point-min)(point-max))
187     (buffer-string)))
188
189 (defun quoted-printable-external-decode-string (string)
190   "Decode STRING which is encoded in quoted-printable, and return the result."
191   (with-temp-buffer
192     (insert string)
193     (quoted-printable-external-decode-region (point-min)(point-max))
194     (buffer-string)))
195
196 (defvar quoted-printable-external-decoder-option-to-specify-file '("-o")
197   "*list of options of quoted-printable decoder program to specify file.")
198
199 (defun quoted-printable-external-write-decoded-region (start end filename)
200   "Decode and write current region encoded by quoted-printable into FILENAME.
201 START and END are buffer positions."
202   (interactive
203    (list (region-beginning) (region-end)
204          (read-file-name "Write decoded region to file: ")))
205   (as-binary-process
206    (apply (function call-process-region)
207           start end (car quoted-printable-external-decoder)
208           nil nil nil
209           (append (cdr quoted-printable-external-decoder)
210                   quoted-printable-external-decoder-option-to-specify-file
211                   (list filename))
212           )))
213
214 \f
215 ;;; @ Q-encoding encode/decode string
216 ;;;
217
218 (defconst q-encoding-special-chars-alist
219   '((text       ?= ?? ?_)
220     (comment    ?= ?? ?_ ?\( ?\) ?\\)
221     (phrase     ?= ?? ?_ ?\( ?\) ?\\ ?\" ?# ?$ ?% ?& ?' ?, ?. ?/
222                 ?: ?\; ?< ?> ?@ ?\[ ?\] ?^ ?` ?{ ?| ?} ?~)
223     ))
224
225 (defun q-encoding-internal-encode-string (string &optional mode)
226   "Encode STRING to Q-encoding of encoded-word, and return the result.
227 MODE allows `text', `comment', `phrase' or nil.  Default value is
228 `phrase'."
229   (let ((specials (cdr (or (assq mode q-encoding-special-chars-alist)
230                            (assq 'phrase q-encoding-special-chars-alist)
231                            ))))
232     (mapconcat (function
233                 (lambda (chr)
234                   (cond ((eq chr ? ) "_")
235                         ((or (< chr 32) (< 126 chr)
236                              (memq chr specials)
237                              )
238                          (quoted-printable-quote-char chr)
239                          )
240                         (t
241                          (char-to-string chr)
242                          ))
243                   ))
244                string "")
245     ))
246
247 (defun q-encoding-internal-decode-string (string)
248   "Decode STRING which is encoded in Q-encoding and return the result."
249   (let (q h l)
250     (mapconcat (function
251                 (lambda (chr)
252                   (cond ((eq chr ?_) " ")
253                         ((eq chr ?=)
254                          (setq q t)
255                          "")
256                         (q (setq h (quoted-printable-hex-char-to-num chr))
257                            (setq q nil)
258                            "")
259                         (h (setq l (quoted-printable-hex-char-to-num chr))
260                            (prog1
261                                (char-to-string (logior (ash h 4) l))
262                              (setq h nil)
263                              )
264                            )
265                         (t (char-to-string chr))
266                         )))
267                string "")))
268
269
270 ;;; @@ etc
271 ;;;
272
273 (defun q-encoding-printable-char-p (chr mode)
274   (and (not (memq chr '(?= ?? ?_)))
275        (<= ?\   chr)(<= chr ?~)
276        (cond ((eq mode 'text) t)
277              ((eq mode 'comment)
278               (not (memq chr '(?\( ?\) ?\\)))
279               )
280              (t
281               (string-match "[A-Za-z0-9!*+/=_---]" (char-to-string chr))
282               ))))
283
284 (defun q-encoding-internal-encoded-length (string &optional mode)
285   (let ((l 0)(i 0)(len (length string)) chr)
286     (while (< i len)
287       (setq chr (elt string i))
288       (if (q-encoding-printable-char-p chr mode)
289           (setq l (+ l 1))
290         (setq l (+ l 3))
291         )
292       (setq i (+ i 1)) )
293     l))
294
295
296 ;;; @ end
297 ;;;
298
299 (provide 'mel-q)
300
301 ;;; mel-q.el ends here