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)
109 (defun quoted-printable-internal-encode-string (string)
110 "Encode STRING to quoted-printable, and return the result."
113 (quoted-printable-internal-encode-region (point-min)(point-max))
117 (defun quoted-printable-external-encode-string (string)
118 "Encode STRING to quoted-printable, and return the result."
121 (quoted-printable-external-encode-region (point-min)(point-max))
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))
136 ;;; @ Quoted-Printable decoder
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))
145 (defun quoted-printable-internal-decode-region (start end)
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))
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)
163 (ash (quoted-printable-hex-char-to-num (aref hex 0)) 4)
164 (quoted-printable-hex-char-to-num (aref hex 1))))
171 (defvar quoted-printable-external-decoder '("mmencode" "-q" "-u")
172 "*list of quoted-printable decoder program name and its arguments.")
174 (defun quoted-printable-external-decode-region (start end)
177 (apply (function call-process-region)
178 start end (car quoted-printable-external-decoder)
179 t t nil (cdr quoted-printable-external-decoder))
182 (defun quoted-printable-internal-decode-string (string)
183 "Decode STRING which is encoded in quoted-printable, and return the result."
186 (quoted-printable-internal-decode-region (point-min)(point-max))
189 (defun quoted-printable-external-decode-string (string)
190 "Decode STRING which is encoded in quoted-printable, and return the result."
193 (quoted-printable-external-decode-region (point-min)(point-max))
196 (defvar quoted-printable-external-decoder-option-to-specify-file '("-o")
197 "*list of options of quoted-printable decoder program to specify file.")
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."
203 (list (region-beginning) (region-end)
204 (read-file-name "Write decoded region to file: ")))
206 (apply (function call-process-region)
207 start end (car quoted-printable-external-decoder)
209 (append (cdr quoted-printable-external-decoder)
210 quoted-printable-external-decoder-option-to-specify-file
215 ;;; @ Q-encoding encode/decode string
218 (defconst q-encoding-special-chars-alist
220 (comment ?= ?? ?_ ?\( ?\) ?\\)
221 (phrase ?= ?? ?_ ?\( ?\) ?\\ ?\" ?# ?$ ?% ?& ?' ?, ?. ?/
222 ?: ?\; ?< ?> ?@ ?\[ ?\] ?^ ?` ?{ ?| ?} ?~)
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
229 (let ((specials (cdr (or (assq mode q-encoding-special-chars-alist)
230 (assq 'phrase q-encoding-special-chars-alist)
234 (cond ((eq chr ? ) "_")
235 ((or (< chr 32) (< 126 chr)
238 (quoted-printable-quote-char chr)
247 (defun q-encoding-internal-decode-string (string)
248 "Decode STRING which is encoded in Q-encoding and return the result."
252 (cond ((eq chr ?_) " ")
256 (q (setq h (quoted-printable-hex-char-to-num chr))
259 (h (setq l (quoted-printable-hex-char-to-num chr))
261 (char-to-string (logior (ash h 4) l))
265 (t (char-to-string chr))
273 (defun q-encoding-printable-char-p (chr mode)
274 (and (not (memq chr '(?= ?? ?_)))
275 (<= ?\ chr)(<= chr ?~)
276 (cond ((eq mode 'text) t)
278 (not (memq chr '(?\( ?\) ?\\)))
281 (string-match "[A-Za-z0-9!*+/=_---]" (char-to-string chr))
284 (defun q-encoding-internal-encoded-length (string &optional mode)
285 (let ((l 0)(i 0)(len (length string)) chr)
287 (setq chr (elt string i))
288 (if (q-encoding-printable-char-p chr mode)
301 ;;; mel-q.el ends here