1 ;;; qp.el --- Quoted-Printable functions
2 ;; Copyright (C) 1998, 1999, 2000 Free Software Foundation, Inc.
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
5 ;; This file is part of GNU Emacs.
7 ;; GNU Emacs is free software; you can redistribute it and/or modify
8 ;; it under the terms of the GNU General Public License as published by
9 ;; the Free Software Foundation; either version 2, or (at your option)
12 ;; GNU Emacs is distributed in the hope that it will be useful,
13 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;; GNU General Public License for more details.
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with GNU Emacs; see the file COPYING. If not, write to the
19 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 ;; Boston, MA 02111-1307, USA.
28 (defvar quoted-printable-encoding-characters
29 (mapcar 'identity "0123456789ABCDEFabcdef"))
31 (defun quoted-printable-decode-region (from to &optional charset)
32 "Decode quoted-printable in the region between FROM and TO.
33 If CHARSET is non-nil, decode the region with charset."
38 (narrow-to-region from to)
48 ((eq (char-after) ?\n)
52 (memq (char-after) quoted-printable-encoding-characters)
53 (memq (char-after (1+ (point)))
54 quoted-printable-encoding-characters))
57 (buffer-substring (point) (+ 2 (point)))
67 (message "Malformed MIME quoted-printable message"))))
68 ((and charset start (not (eq (mm-charset-after) 'ascii)))
69 (mm-decode-coding-region start (point) charset)
74 (if (and charset start)
75 (mm-decode-coding-region start (point) charset))))))
77 (defun quoted-printable-decode-string (string &optional charset)
78 "Decode the quoted-printable-encoded STRING and return the results.
79 If CHARSET is non-nil, decode the region with charset."
82 (quoted-printable-decode-region (point-min) (point-max) charset)
85 (defun quoted-printable-encode-region (from to &optional fold class)
86 "QP-encode the region between FROM and TO.
88 If FOLD fold long lines. If CLASS, translate the characters
89 matched by that regexp.
91 If `mm-use-ultra-safe-encoding' is set, fold unconditionally and
92 encode lines starting with \"From\"."
96 (narrow-to-region from to)
98 ;; Encode all the non-ascii and control characters.
99 (goto-char (point-min))
100 (while (and (skip-chars-forward
101 (or class "^\000-\007\013\015-\037\200-\377="))
105 (upcase (format "=%02x" (char-after)))
107 ;; Encode white space at the end of lines.
108 (goto-char (point-min))
109 (while (re-search-forward "[ \t]+$" nil t)
110 (goto-char (match-beginning 0))
114 (upcase (format "=%02x" (char-after)))
116 (when (or fold mm-use-ultra-safe-encoding)
118 (goto-char (point-min))
120 ;; In ultra-safe mode, encode "From " at the beginning of a
122 (when mm-use-ultra-safe-encoding
124 (when (looking-at "From ")
125 (replace-match "From=20" nil t)))
127 (while (> (current-column) 72)
129 (forward-char 71);; 71 char plus an "="
130 (search-backward "=" (- (point) 2) t)
136 (defun quoted-printable-encode-string (string)
137 "QP-encode STRING and return the results."
138 (mm-with-unibyte-buffer
140 (quoted-printable-encode-region (point-min) (point-max))