d0e93bc5355e6fd552d53951d79248c57665d380
[elisp/gnus.git-] / lisp / qp.el
1 ;;; qp.el --- Quoted-printable functions
2 ;; Copyright (C) 1998 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 "0123456789ABCDEF"))
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 ((eq (following-char) ?\n)
36              (delete-char -1)
37              (delete-char 1))
38             ((and
39               (memq (following-char) quoted-printable-encoding-characters)
40               (memq (char-after (1+ (point)))
41                     quoted-printable-encoding-characters))
42              (subst-char-in-region
43               (1- (point)) (point) ?=
44               (string-to-number
45                (buffer-substring (point) (+ 2 (point)))
46                16))
47              (delete-char 2))
48             ((looking-at "=")
49              (delete-char 1))
50             ((message "Malformed MIME quoted-printable message"))))))
51
52 (defun quoted-printable-decode-string (string)
53  "Decode the quoted-printable-encoded STRING and return the results."
54  (with-temp-buffer
55    (insert string)
56    (quoted-printable-decode-region (point-min) (point-max))
57    (buffer-string)))
58
59 (defun quoted-printable-encode-region (from to &optional fold class)
60   "QP-encode the region between FROM and TO.
61 If FOLD, fold long lines.  If CLASS, translate the characters
62 matched by that regexp."
63   (interactive "r")
64   (save-excursion
65     (save-restriction
66       (narrow-to-region from to)
67       (goto-char (point-min))
68       (while (re-search-forward
69               (or class "[\000-\007\013\015-\037\200-\377=]") nil t)
70         (insert
71          (prog1
72              (upcase (format "=%x" (char-after (1- (point)))))
73            (delete-char -1))))
74       (when fold
75         ;; Fold long lines.
76         (goto-char (point-min))
77         (end-of-line)
78         (while (> (current-column) 72)
79           (beginning-of-line)
80           (forward-char 72)
81           (search-backward "=" (- (point) 2) t)
82           (insert "=\n")
83           (end-of-line))))))
84
85 (defun quoted-printable-encode-string (string)
86  "QP-encode STRING and return the results."
87  (with-temp-buffer
88    (insert string)
89    (quoted-printable-encode-region (point-min) (point-max))
90    (buffer-string)))
91
92 (provide 'qp)
93
94 ;; qp.el ends here