* nsgui.h: Fix compiler warning about gnulib redefining verify.
[emacs.git] / lisp / gnus / qp.el
blob584e24177af365b72d51fc4d4a26322db28f8c06
1 ;;; qp.el --- Quoted-Printable functions
3 ;; Copyright (C) 1998-2011 Free Software Foundation, Inc.
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Keywords: mail, extensions
8 ;; This file is part of GNU Emacs.
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 3 of the License, or
13 ;; (at your option) any later version.
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.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23 ;;; Commentary:
25 ;; Functions for encoding and decoding quoted-printable text as
26 ;; defined in RFC 2045.
28 ;;; Code:
30 (require 'mm-util)
31 (defvar mm-use-ultra-safe-encoding)
33 ;;;###autoload
34 (defun quoted-printable-decode-region (from to &optional coding-system)
35 "Decode quoted-printable in the region between FROM and TO, per RFC 2045.
36 If CODING-SYSTEM is non-nil, decode bytes into characters with that
37 coding-system.
39 Interactively, you can supply the CODING-SYSTEM argument
40 with \\[universal-coding-system-argument].
42 The CODING-SYSTEM argument is a historical hangover and is deprecated.
43 QP encodes raw bytes and should be decoded into raw bytes. Decoding
44 them into characters should be done separately."
45 (interactive
46 ;; Let the user determine the coding system with "C-x RET c".
47 (list (region-beginning) (region-end) coding-system-for-read))
48 (unless (mm-coding-system-p coding-system) ; e.g. `ascii' from Gnus
49 (setq coding-system nil))
50 (save-excursion
51 (save-restriction
52 ;; RFC 2045: ``An "=" followed by two hexadecimal digits, one
53 ;; or both of which are lowercase letters in "abcdef", is
54 ;; formally illegal. A robust implementation might choose to
55 ;; recognize them as the corresponding uppercase letters.''
56 (let ((case-fold-search t))
57 (narrow-to-region from to)
58 ;; Do this in case we're called from Gnus, say, in a buffer
59 ;; which already contains non-ASCII characters which would
60 ;; then get doubly-decoded below.
61 (if coding-system
62 (mm-encode-coding-region (point-min) (point-max) coding-system))
63 (goto-char (point-min))
64 (while (and (skip-chars-forward "^=")
65 (not (eobp)))
66 (cond ((eq (char-after (1+ (point))) ?\n)
67 (delete-char 2))
68 ((looking-at "=[0-9A-F][0-9A-F]")
69 (let ((byte (string-to-number (buffer-substring (1+ (point))
70 (+ 3 (point)))
71 16)))
72 (mm-insert-byte byte 1)
73 (delete-char 3)))
75 (message "Malformed quoted-printable text")
76 (forward-char)))))
77 (if coding-system
78 (mm-decode-coding-region (point-min) (point-max) coding-system)))))
80 (defun quoted-printable-decode-string (string &optional coding-system)
81 "Decode the quoted-printable encoded STRING and return the result.
82 If CODING-SYSTEM is non-nil, decode the string with coding-system.
83 Use of CODING-SYSTEM is deprecated; this function should deal with
84 raw bytes, and coding conversion should be done separately."
85 (mm-with-unibyte-buffer
86 (insert string)
87 (quoted-printable-decode-region (point-min) (point-max) coding-system)
88 (buffer-string)))
90 (defun quoted-printable-encode-region (from to &optional fold class)
91 "Quoted-printable encode the region between FROM and TO per RFC 2045.
93 If FOLD, fold long lines at 76 characters (as required by the RFC).
94 If CLASS is non-nil, translate the characters not matched by that
95 regexp class, which is in the form expected by `skip-chars-forward'.
96 You should probably avoid non-ASCII characters in this arg.
98 If `mm-use-ultra-safe-encoding' is set, fold lines unconditionally and
99 encode lines starting with \"From\"."
100 (interactive "r")
101 (unless class
102 ;; Avoid using 8bit characters. = is \075.
103 ;; Equivalent to "^\000-\007\013\015-\037\200-\377="
104 (setq class "\010-\012\014\040-\074\076-\177"))
105 (save-excursion
106 (goto-char from)
107 (if (re-search-forward (mm-string-to-multibyte "[^\x0-\x7f\x80-\xff]")
108 to t)
109 (error "Multibyte character in QP encoding region"))
110 (save-restriction
111 (narrow-to-region from to)
112 ;; Encode all the non-ascii and control characters.
113 (goto-char (point-min))
114 (while (and (skip-chars-forward class)
115 (not (eobp)))
116 (insert
117 (prog1
118 ;; To unibyte in case of Emacs 23 (unicode) eight-bit.
119 (format "=%02X" (mm-multibyte-char-to-unibyte (char-after)))
120 (delete-char 1))))
121 ;; Encode white space at the end of lines.
122 (goto-char (point-min))
123 (while (re-search-forward "[ \t]+$" nil t)
124 (goto-char (match-beginning 0))
125 (while (not (eolp))
126 (insert
127 (prog1
128 (format "=%02X" (char-after))
129 (delete-char 1)))))
130 (let ((mm-use-ultra-safe-encoding
131 (and (boundp 'mm-use-ultra-safe-encoding)
132 mm-use-ultra-safe-encoding)))
133 (when (or fold mm-use-ultra-safe-encoding)
134 (let ((tab-width 1)) ; HTAB is one character.
135 (goto-char (point-min))
136 (while (not (eobp))
137 ;; In ultra-safe mode, encode "From " at the beginning
138 ;; of a line.
139 (when mm-use-ultra-safe-encoding
140 (if (looking-at "From ")
141 (replace-match "From=20" nil t)
142 (if (looking-at "-")
143 (replace-match "=2D" nil t))))
144 (end-of-line)
145 ;; Fold long lines.
146 (while (> (current-column) 76) ; tab-width must be 1.
147 (beginning-of-line)
148 (forward-char 75) ; 75 chars plus an "="
149 (search-backward "=" (- (point) 2) t)
150 (insert "=\n")
151 (end-of-line))
152 (forward-line))))))))
154 (defun quoted-printable-encode-string (string)
155 "Encode the STRING as quoted-printable and return the result."
156 (with-temp-buffer
157 (if (mm-multibyte-string-p string)
158 (mm-enable-multibyte)
159 (mm-disable-multibyte))
160 (insert string)
161 (quoted-printable-encode-region (point-min) (point-max))
162 (buffer-string)))
164 (provide 'qp)
166 ;;; qp.el ends here