1 ;;; qp.el --- Quoted-Printable functions
2 ;; Copyright (C) 1998,99 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.
26 (defvar quoted-printable-encoding-characters
27 (mapcar 'identity "0123456789ABCDEFabcdef"))
29 (defun quoted-printable-decode-region (from to)
30 "Decode quoted-printable in the region between FROM and TO."
34 (while (search-forward "=" to t)
37 ((eq (char-after) ?\n)
42 (memq (char-after) quoted-printable-encoding-characters)
43 (memq (char-after (1+ (point)))
44 quoted-printable-encoding-characters))
46 (1- (point)) (point) ?=
48 (buffer-substring (point) (+ 2 (point)))
59 (message "Malformed MIME quoted-printable message"))))))
61 (defun quoted-printable-decode-string (string)
62 "Decode the quoted-printable-encoded STRING and return the results."
65 (quoted-printable-decode-region (point-min) (point-max))
68 (defun quoted-printable-encode-region (from to &optional fold class)
69 "QP-encode the region between FROM and TO.
71 If FOLD fold long lines. If CLASS, translate the characters
72 matched by that regexp.
74 If `mm-use-ultra-safe-encoding' is set, fold unconditionally and
75 encode lines starting with \"From\"."
79 (narrow-to-region from to)
81 ;; Encode all the non-ascii and control characters.
82 (goto-char (point-min))
83 (while (and (skip-chars-forward
84 (or class "^\000-\007\013\015-\037\200-\377="))
88 (upcase (format "=%02x" (char-after)))
90 ;; Encode white space at the end of lines.
91 (goto-char (point-min))
92 (while (re-search-forward "[ \t]+$" nil t)
93 (goto-char (match-beginning 0))
97 (upcase (format "=%02x" (char-after)))
99 (when (or fold mm-use-ultra-safe-encoding)
101 (goto-char (point-min))
103 ;; In ultra-safe mode, encode "From " at the beginning of a
105 (when mm-use-ultra-safe-encoding
107 (when (looking-at "From ")
108 (replace-match "From=20" nil t)))
110 (while (> (current-column) 72)
112 (forward-char 71);; 71 char plus an "="
113 (search-backward "=" (- (point) 2) t)
119 (defun quoted-printable-encode-string (string)
120 "QP-encode STRING and return the results."
121 (mm-with-unibyte-buffer
123 (quoted-printable-encode-region (point-min) (point-max))