31717
|
1 ;;; qp.el --- Quoted-Printable functions
|
32103
|
2
|
64754
|
3 ;; Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004,
|
79708
|
4 ;; 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
|
31717
|
5
|
|
6 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
|
32103
|
7 ;; Keywords: mail, extensions
|
|
8
|
31717
|
9 ;; This file is part of GNU Emacs.
|
|
10
|
94662
|
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
|
31717
|
12 ;; it under the terms of the GNU General Public License as published by
|
94662
|
13 ;; the Free Software Foundation, either version 3 of the License, or
|
|
14 ;; (at your option) any later version.
|
31717
|
15
|
|
16 ;; GNU Emacs is distributed in the hope that it will be useful,
|
|
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
94662
|
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
31717
|
19 ;; GNU General Public License for more details.
|
|
20
|
|
21 ;; You should have received a copy of the GNU General Public License
|
94662
|
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
|
31717
|
23
|
|
24 ;;; Commentary:
|
|
25
|
32103
|
26 ;; Functions for encoding and decoding quoted-printable text as
|
|
27 ;; defined in RFC 2045.
|
|
28
|
31717
|
29 ;;; Code:
|
|
30
|
32504
|
31 (require 'mm-util)
|
86154
|
32 (defvar mm-use-ultra-safe-encoding)
|
32211
|
33
|
56927
|
34 ;;;###autoload
|
32211
|
35 (defun quoted-printable-decode-region (from to &optional coding-system)
|
32103
|
36 "Decode quoted-printable in the region between FROM and TO, per RFC 2045.
|
32211
|
37 If CODING-SYSTEM is non-nil, decode bytes into characters with that
|
43737
|
38 coding-system.
|
|
39
|
|
40 Interactively, you can supply the CODING-SYSTEM argument
|
56927
|
41 with \\[universal-coding-system-argument].
|
|
42
|
|
43 The CODING-SYSTEM argument is a historical hangover and is deprecated.
|
|
44 QP encodes raw bytes and should be decoded into raw bytes. Decoding
|
|
45 them into characters should be done separately."
|
38103
|
46 (interactive
|
|
47 ;; Let the user determine the coding system with "C-x RET c".
|
|
48 (list (region-beginning) (region-end) coding-system-for-read))
|
32462
|
49 (unless (mm-coding-system-p coding-system) ; e.g. `ascii' from Gnus
|
|
50 (setq coding-system nil))
|
31717
|
51 (save-excursion
|
|
52 (save-restriction
|
32462
|
53 ;; RFC 2045: ``An "=" followed by two hexadecimal digits, one
|
|
54 ;; or both of which are lowercase letters in "abcdef", is
|
|
55 ;; formally illegal. A robust implementation might choose to
|
|
56 ;; recognize them as the corresponding uppercase letters.''
|
32211
|
57 (let ((case-fold-search t))
|
31717
|
58 (narrow-to-region from to)
|
32211
|
59 ;; Do this in case we're called from Gnus, say, in a buffer
|
|
60 ;; which already contains non-ASCII characters which would
|
|
61 ;; then get doubly-decoded below.
|
|
62 (if coding-system
|
|
63 (mm-encode-coding-region (point-min) (point-max) coding-system))
|
|
64 (goto-char (point-min))
|
35619
|
65 (while (and (skip-chars-forward "^=")
|
32103
|
66 (not (eobp)))
|
|
67 (cond ((eq (char-after (1+ (point))) ?\n)
|
|
68 (delete-char 2))
|
|
69 ((looking-at "=[0-9A-F][0-9A-F]")
|
62907
|
70 (let ((byte (string-to-number (buffer-substring (1+ (point))
|
85712
|
71 (+ 3 (point)))
|
|
72 16)))
|
47944
|
73 (mm-insert-byte byte 1)
|
56927
|
74 (delete-char 3)))
|
32103
|
75 (t
|
56927
|
76 (message "Malformed quoted-printable text")
|
32211
|
77 (forward-char)))))
|
|
78 (if coding-system
|
|
79 (mm-decode-coding-region (point-min) (point-max) coding-system)))))
|
31717
|
80
|
32211
|
81 (defun quoted-printable-decode-string (string &optional coding-system)
|
32103
|
82 "Decode the quoted-printable encoded STRING and return the result.
|
79313
|
83 If CODING-SYSTEM is non-nil, decode the string with coding-system.
|
56927
|
84 Use of CODING-SYSTEM is deprecated; this function should deal with
|
|
85 raw bytes, and coding conversion should be done separately."
|
|
86 (mm-with-unibyte-buffer
|
31717
|
87 (insert string)
|
32211
|
88 (quoted-printable-decode-region (point-min) (point-max) coding-system)
|
31717
|
89 (buffer-string)))
|
|
90
|
|
91 (defun quoted-printable-encode-region (from to &optional fold class)
|
32103
|
92 "Quoted-printable encode the region between FROM and TO per RFC 2045.
|
31717
|
93
|
32103
|
94 If FOLD, fold long lines at 76 characters (as required by the RFC).
|
35619
|
95 If CLASS is non-nil, translate the characters not matched by that
|
|
96 regexp class, which is in the form expected by `skip-chars-forward'.
|
|
97 You should probably avoid non-ASCII characters in this arg.
|
31717
|
98
|
32103
|
99 If `mm-use-ultra-safe-encoding' is set, fold lines unconditionally and
|
31717
|
100 encode lines starting with \"From\"."
|
|
101 (interactive "r")
|
32103
|
102 (unless class
|
34752
|
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"))
|
31717
|
106 (save-excursion
|
61644
|
107 (goto-char from)
|
|
108 (if (re-search-forward (mm-string-to-multibyte "[^\x0-\x7f\x80-\xff]")
|
|
109 to t)
|
|
110 (error "Multibyte character in QP encoding region"))
|
31717
|
111 (save-restriction
|
|
112 (narrow-to-region from to)
|
35619
|
113 ;; Encode all the non-ascii and control characters.
|
|
114 (goto-char (point-min))
|
|
115 (while (and (skip-chars-forward class)
|
|
116 (not (eobp)))
|
|
117 (insert
|
|
118 (prog1
|
69135
|
119 ;; To unibyte in case of Emacs 23 (unicode) eight-bit.
|
47944
|
120 (format "=%02X" (mm-multibyte-char-to-unibyte (char-after)))
|
35619
|
121 (delete-char 1))))
|
|
122 ;; Encode white space at the end of lines.
|
|
123 (goto-char (point-min))
|
|
124 (while (re-search-forward "[ \t]+$" nil t)
|
|
125 (goto-char (match-beginning 0))
|
|
126 (while (not (eolp))
|
31717
|
127 (insert
|
|
128 (prog1
|
34752
|
129 (format "=%02X" (char-after))
|
35931
|
130 (delete-char 1)))))
|
|
131 (let ((mm-use-ultra-safe-encoding
|
|
132 (and (boundp 'mm-use-ultra-safe-encoding)
|
|
133 mm-use-ultra-safe-encoding)))
|
|
134 (when (or fold mm-use-ultra-safe-encoding)
|
|
135 (let ((tab-width 1)) ; HTAB is one character.
|
|
136 (goto-char (point-min))
|
|
137 (while (not (eobp))
|
|
138 ;; In ultra-safe mode, encode "From " at the beginning
|
|
139 ;; of a line.
|
|
140 (when mm-use-ultra-safe-encoding
|
|
141 (if (looking-at "From ")
|
|
142 (replace-match "From=20" nil t)
|
|
143 (if (looking-at "-")
|
|
144 (replace-match "=2D" nil t))))
|
|
145 (end-of-line)
|
|
146 ;; Fold long lines.
|
|
147 (while (> (current-column) 76) ; tab-width must be 1.
|
|
148 (beginning-of-line)
|
|
149 (forward-char 75) ; 75 chars plus an "="
|
|
150 (search-backward "=" (- (point) 2) t)
|
|
151 (insert "=\n")
|
|
152 (end-of-line))
|
|
153 (forward-line))))))))
|
31717
|
154
|
|
155 (defun quoted-printable-encode-string (string)
|
32103
|
156 "Encode the STRING as quoted-printable and return the result."
|
35619
|
157 (let ((default-enable-multibyte-characters (mm-multibyte-string-p string)))
|
|
158 (with-temp-buffer
|
|
159 (insert string)
|
|
160 (quoted-printable-encode-region (point-min) (point-max))
|
|
161 (buffer-string))))
|
31717
|
162
|
|
163 (provide 'qp)
|
|
164
|
93975
|
165 ;; arch-tag: db89e52a-e4a1-4b69-926f-f434f04216ba
|
32103
|
166 ;;; qp.el ends here
|