Importing Pterodactyl Gnus v0.97.
[elisp/gnus.git-] / lisp / qp.el
1 ;;; qp.el --- Quoted-Printable functions
2 ;; Copyright (C) 1998,99 Free Software Foundation, Inc.
3
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
5 ;; This file is part of GNU Emacs.
6
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)
10 ;; any later version.
11
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.
16
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.
21
22 ;;; Commentary:
23
24 ;;; Code:
25
26 (defvar quoted-printable-encoding-characters
27   (mapcar 'identity "0123456789ABCDEFabcdef"))
28
29 (defun quoted-printable-decode-region (from to)
30   "Decode quoted-printable in the region between FROM and TO."
31   (interactive "r")
32   (save-excursion
33     (goto-char from)
34     (while (search-forward "=" to t)
35       (cond
36        ;; End of the line.
37        ((eq (char-after) ?\n)
38         (delete-char -1)
39         (delete-char 1))
40        ;; Encoded character.
41        ((and
42          (memq (char-after) quoted-printable-encoding-characters)
43          (memq (char-after (1+ (point)))
44                quoted-printable-encoding-characters))
45         (subst-char-in-region
46          (1- (point)) (point) ?=
47          (string-to-number
48           (buffer-substring (point) (+ 2 (point)))
49           16))
50         (delete-char 2))
51        ;; Quoted equal sign.
52        ((eq (char-after) ?=)
53         (delete-char 1))
54        ;; End of buffer.
55        ((eobp)
56         (delete-char -1))
57        ;; Invalid.
58        (t
59         (message "Malformed MIME quoted-printable message"))))))
60
61 (defun quoted-printable-decode-string (string)
62  "Decode the quoted-printable-encoded STRING and return the results."
63  (with-temp-buffer
64    (insert string)
65    (quoted-printable-decode-region (point-min) (point-max))
66    (buffer-string)))
67
68 (defun quoted-printable-encode-region (from to &optional fold class)
69   "QP-encode the region between FROM and TO.
70 If FOLD, fold long lines.  If CLASS, translate the characters
71 matched by that regexp."
72   (interactive "r")
73   (save-excursion
74     (save-restriction
75       (narrow-to-region from to)
76 ;;      (mm-encode-body)
77       ;; Encode all the non-ascii and control characters.
78       (goto-char (point-min))
79       (while (and (skip-chars-forward
80                    (or class "^\000-\007\013\015-\037\200-\377="))
81                   (not (eobp)))
82         (insert
83          (prog1
84              (upcase (format "=%02x" (char-after)))
85            (delete-char 1))))
86       ;; Encode white space at the end of lines.
87       (goto-char (point-min))
88       (while (re-search-forward "[ \t]+$" nil t)
89         (goto-char (match-beginning 0))
90         (while (not (eolp))
91           (insert
92            (prog1
93                (upcase (format "=%02x" (char-after)))
94              (delete-char 1)))))
95       (when fold
96         ;; Fold long lines.
97         (goto-char (point-min))
98         (while (not (eobp))
99           (end-of-line)
100           (while (> (current-column) 72)
101             (beginning-of-line)
102             (forward-char 72)
103             (search-backward "=" (- (point) 2) t)
104             (insert "=\n")
105             (end-of-line))
106           (forward-line))))))
107
108 (defun quoted-printable-encode-string (string)
109  "QP-encode STRING and return the results."
110  (mm-with-unibyte-buffer
111    (insert string)
112    (quoted-printable-encode-region (point-min) (point-max))
113    (buffer-string)))
114
115 (provide 'qp)
116
117 ;; qp.el ends here