*** empty log message ***
[gnus] / 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)
60   "QP-encode the region between FROM and TO.
61 If FOLD, fold long lines."
62   (interactive "r")
63   (save-excursion
64     (save-restriction
65       (narrow-to-region from to)
66       (goto-char (point-min))
67       (while (re-search-forward "[\000-\007\013\015-\037\200-\377_=]" nil t)
68         (insert
69          (prog1
70              (upcase (format "=%x" (char-after (1- (point)))))
71            (delete-char -1))))
72       (when fold
73         ;; Fold long lines.
74         (goto-char (point-min))
75         (end-of-line)
76         (while (> (current-column) 72)
77           (beginning-of-line)
78           (forward-char 72)
79           (search-backward "=" (- (point) 2) t)
80           (insert "=\n")
81           (end-of-line))))))
82
83 (defun quoted-printable-encode-string (string)
84  "QP-encode STRING and return the results."
85  (with-temp-buffer
86    (insert string)
87    (quoted-printable-encode-region (point-min) (point-max))
88    (buffer-string)))
89
90 (provide 'qp)
91
92 ;; qp.el ends here