(gnus-find-new-newsgroups): Use
[bpt/emacs.git] / lisp / gnus / qp.el
1 ;;; qp.el --- Quoted-Printable functions
2
3 ;; Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Keywords: mail, extensions
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Commentary:
26
27 ;; Functions for encoding and decoding quoted-printable text as
28 ;; defined in RFC 2045.
29
30 ;;; Code:
31
32 (require 'mm-util)
33 (eval-when-compile (defvar mm-use-ultra-safe-encoding))
34
35 (defun quoted-printable-decode-region (from to &optional coding-system)
36 "Decode quoted-printable in the region between FROM and TO, per RFC 2045.
37 If CODING-SYSTEM is non-nil, decode bytes into characters with that
38 coding-system."
39 (interactive
40 ;; Let the user determine the coding system with "C-x RET c".
41 (list (region-beginning) (region-end) coding-system-for-read))
42 (unless (mm-coding-system-p coding-system) ; e.g. `ascii' from Gnus
43 (setq coding-system nil))
44 (save-excursion
45 (save-restriction
46 ;; RFC 2045: ``An "=" followed by two hexadecimal digits, one
47 ;; or both of which are lowercase letters in "abcdef", is
48 ;; formally illegal. A robust implementation might choose to
49 ;; recognize them as the corresponding uppercase letters.''
50 (let ((case-fold-search t))
51 (narrow-to-region from to)
52 ;; Do this in case we're called from Gnus, say, in a buffer
53 ;; which already contains non-ASCII characters which would
54 ;; then get doubly-decoded below.
55 (if coding-system
56 (mm-encode-coding-region (point-min) (point-max) coding-system))
57 (goto-char (point-min))
58 (while (and (skip-chars-forward "^=")
59 (not (eobp)))
60 (cond ((eq (char-after (1+ (point))) ?\n)
61 (delete-char 2))
62 ((looking-at "=[0-9A-F][0-9A-F]")
63 (let ((byte (string-to-int (buffer-substring (1+ (point))
64 (+ 3 (point)))
65 16)))
66 (insert byte)
67 (delete-char 3)
68 (unless (eq byte ?=)
69 (backward-char))))
70 (t
71 (error "Malformed quoted-printable text")
72 (forward-char)))))
73 (if coding-system
74 (mm-decode-coding-region (point-min) (point-max) coding-system)))))
75
76 (defun quoted-printable-decode-string (string &optional coding-system)
77 "Decode the quoted-printable encoded STRING and return the result.
78 If CODING-SYSTEM is non-nil, decode the region with coding-system."
79 (with-temp-buffer
80 (insert string)
81 (quoted-printable-decode-region (point-min) (point-max) coding-system)
82 (buffer-string)))
83
84 (defun quoted-printable-encode-region (from to &optional fold class)
85 "Quoted-printable encode the region between FROM and TO per RFC 2045.
86
87 If FOLD, fold long lines at 76 characters (as required by the RFC).
88 If CLASS is non-nil, translate the characters not matched by that
89 regexp class, which is in the form expected by `skip-chars-forward'.
90 You should probably avoid non-ASCII characters in this arg.
91
92 If `mm-use-ultra-safe-encoding' is set, fold lines unconditionally and
93 encode lines starting with \"From\"."
94 (interactive "r")
95 ;; Fixme: what should this do in XEmacs/Mule?
96 (if (fboundp 'find-charset-region) ; else XEmacs, non-Mule
97 (if (delq 'unknown ; Emacs 20 unibyte
98 (delq 'eight-bit-graphic ; Emacs 21
99 (delq 'eight-bit-control
100 (delq 'ascii (find-charset-region from to)))))
101 (error "Multibyte character in QP encoding region")))
102 (unless class
103 ;; Avoid using 8bit characters. = is \075.
104 ;; Equivalent to "^\000-\007\013\015-\037\200-\377="
105 (setq class "\010-\012\014\040-\074\076-\177"))
106 (save-excursion
107 (save-restriction
108 (narrow-to-region from to)
109 ;; Encode all the non-ascii and control characters.
110 (goto-char (point-min))
111 (while (and (skip-chars-forward class)
112 (not (eobp)))
113 (insert
114 (prog1
115 (format "=%02X" (char-after))
116 (delete-char 1))))
117 ;; Encode white space at the end of lines.
118 (goto-char (point-min))
119 (while (re-search-forward "[ \t]+$" nil t)
120 (goto-char (match-beginning 0))
121 (while (not (eolp))
122 (insert
123 (prog1
124 (format "=%02X" (char-after))
125 (delete-char 1)))))
126 (let ((mm-use-ultra-safe-encoding
127 (and (boundp 'mm-use-ultra-safe-encoding)
128 mm-use-ultra-safe-encoding)))
129 (when (or fold mm-use-ultra-safe-encoding)
130 (let ((tab-width 1)) ; HTAB is one character.
131 (goto-char (point-min))
132 (while (not (eobp))
133 ;; In ultra-safe mode, encode "From " at the beginning
134 ;; of a line.
135 (when mm-use-ultra-safe-encoding
136 (if (looking-at "From ")
137 (replace-match "From=20" nil t)
138 (if (looking-at "-")
139 (replace-match "=2D" nil t))))
140 (end-of-line)
141 ;; Fold long lines.
142 (while (> (current-column) 76) ; tab-width must be 1.
143 (beginning-of-line)
144 (forward-char 75) ; 75 chars plus an "="
145 (search-backward "=" (- (point) 2) t)
146 (insert "=\n")
147 (end-of-line))
148 (forward-line))))))))
149
150 (defun quoted-printable-encode-string (string)
151 "Encode the STRING as quoted-printable and return the result."
152 (let ((default-enable-multibyte-characters (mm-multibyte-string-p string)))
153 (with-temp-buffer
154 (insert string)
155 (quoted-printable-encode-region (point-min) (point-max))
156 (buffer-string))))
157
158 (provide 'qp)
159
160 ;;; qp.el ends here