Commit | Line | Data |
---|---|---|
c113de23 | 1 | ;;; qp.el --- Quoted-Printable functions |
657b2c65 | 2 | |
5f8dd322 | 3 | ;; Copyright (C) 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc. |
c113de23 GM |
4 | |
5 | ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org> | |
657b2c65 DL |
6 | ;; Keywords: mail, extensions |
7 | ||
c113de23 GM |
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 | ||
657b2c65 DL |
27 | ;; Functions for encoding and decoding quoted-printable text as |
28 | ;; defined in RFC 2045. | |
c113de23 | 29 | |
657b2c65 | 30 | ;;; Code: |
c113de23 | 31 | |
b756f5b4 DL |
32 | (require 'mm-util) |
33 | (eval-when-compile (defvar mm-use-ultra-safe-encoding)) | |
d21a76da DL |
34 | |
35 | (defun quoted-printable-decode-region (from to &optional coding-system) | |
657b2c65 | 36 | "Decode quoted-printable in the region between FROM and TO, per RFC 2045. |
d21a76da | 37 | If CODING-SYSTEM is non-nil, decode bytes into characters with that |
5c543a33 SZ |
38 | coding-system. |
39 | ||
40 | Interactively, you can supply the CODING-SYSTEM argument | |
41 | with \\[universal-coding-system-argument]." | |
753b4645 EZ |
42 | (interactive |
43 | ;; Let the user determine the coding system with "C-x RET c". | |
44 | (list (region-beginning) (region-end) coding-system-for-read)) | |
b5287163 DL |
45 | (unless (mm-coding-system-p coding-system) ; e.g. `ascii' from Gnus |
46 | (setq coding-system nil)) | |
c113de23 GM |
47 | (save-excursion |
48 | (save-restriction | |
b5287163 DL |
49 | ;; RFC 2045: ``An "=" followed by two hexadecimal digits, one |
50 | ;; or both of which are lowercase letters in "abcdef", is | |
51 | ;; formally illegal. A robust implementation might choose to | |
52 | ;; recognize them as the corresponding uppercase letters.'' | |
d21a76da | 53 | (let ((case-fold-search t)) |
c113de23 | 54 | (narrow-to-region from to) |
d21a76da DL |
55 | ;; Do this in case we're called from Gnus, say, in a buffer |
56 | ;; which already contains non-ASCII characters which would | |
57 | ;; then get doubly-decoded below. | |
58 | (if coding-system | |
59 | (mm-encode-coding-region (point-min) (point-max) coding-system)) | |
60 | (goto-char (point-min)) | |
e4f99da7 | 61 | (while (and (skip-chars-forward "^=") |
657b2c65 DL |
62 | (not (eobp))) |
63 | (cond ((eq (char-after (1+ (point))) ?\n) | |
64 | (delete-char 2)) | |
65 | ((looking-at "=[0-9A-F][0-9A-F]") | |
66 | (let ((byte (string-to-int (buffer-substring (1+ (point)) | |
67 | (+ 3 (point))) | |
68 | 16))) | |
5f8dd322 | 69 | (mm-insert-byte byte 1) |
d21a76da | 70 | (delete-char 3) |
657b2c65 DL |
71 | (unless (eq byte ?=) |
72 | (backward-char)))) | |
657b2c65 | 73 | (t |
372edc63 | 74 | (error "Malformed quoted-printable text") |
d21a76da DL |
75 | (forward-char))))) |
76 | (if coding-system | |
77 | (mm-decode-coding-region (point-min) (point-max) coding-system))))) | |
c113de23 | 78 | |
d21a76da | 79 | (defun quoted-printable-decode-string (string &optional coding-system) |
657b2c65 | 80 | "Decode the quoted-printable encoded STRING and return the result. |
d21a76da | 81 | If CODING-SYSTEM is non-nil, decode the region with coding-system." |
c113de23 GM |
82 | (with-temp-buffer |
83 | (insert string) | |
d21a76da | 84 | (quoted-printable-decode-region (point-min) (point-max) coding-system) |
c113de23 GM |
85 | (buffer-string))) |
86 | ||
87 | (defun quoted-printable-encode-region (from to &optional fold class) | |
657b2c65 | 88 | "Quoted-printable encode the region between FROM and TO per RFC 2045. |
c113de23 | 89 | |
657b2c65 | 90 | If FOLD, fold long lines at 76 characters (as required by the RFC). |
e4f99da7 DL |
91 | If CLASS is non-nil, translate the characters not matched by that |
92 | regexp class, which is in the form expected by `skip-chars-forward'. | |
93 | You should probably avoid non-ASCII characters in this arg. | |
c113de23 | 94 | |
657b2c65 | 95 | If `mm-use-ultra-safe-encoding' is set, fold lines unconditionally and |
c113de23 GM |
96 | encode lines starting with \"From\"." |
97 | (interactive "r") | |
5f8dd322 DL |
98 | (save-excursion |
99 | (goto-char from) | |
100 | (if (fboundp 'string-to-multibyte) ; Emacs 22 | |
101 | (if (re-search-forward (string-to-multibyte "[^\x0-\x7f\x80-\xff]") | |
102 | to t) | |
103 | ;; Fixme: This is somewhat misleading. | |
104 | (error "Multibyte character in QP encoding region")) | |
105 | (if (re-search-forward (mm-string-as-multibyte "[^\0-\377]") to t) | |
106 | (error "Multibyte character in QP encoding region")))) | |
657b2c65 | 107 | (unless class |
158d6e07 SZ |
108 | ;; Avoid using 8bit characters. = is \075. |
109 | ;; Equivalent to "^\000-\007\013\015-\037\200-\377=" | |
110 | (setq class "\010-\012\014\040-\074\076-\177")) | |
c113de23 GM |
111 | (save-excursion |
112 | (save-restriction | |
113 | (narrow-to-region from to) | |
e4f99da7 DL |
114 | ;; Encode all the non-ascii and control characters. |
115 | (goto-char (point-min)) | |
116 | (while (and (skip-chars-forward class) | |
117 | (not (eobp))) | |
118 | (insert | |
119 | (prog1 | |
5f8dd322 DL |
120 | ;; To unibyte in case of Emacs 22 eight-bit. |
121 | (format "=%02X" (mm-multibyte-char-to-unibyte (char-after))) | |
e4f99da7 DL |
122 | (delete-char 1)))) |
123 | ;; Encode white space at the end of lines. | |
124 | (goto-char (point-min)) | |
125 | (while (re-search-forward "[ \t]+$" nil t) | |
126 | (goto-char (match-beginning 0)) | |
127 | (while (not (eolp)) | |
c113de23 GM |
128 | (insert |
129 | (prog1 | |
158d6e07 | 130 | (format "=%02X" (char-after)) |
b03b1ad2 DL |
131 | (delete-char 1))))) |
132 | (let ((mm-use-ultra-safe-encoding | |
133 | (and (boundp 'mm-use-ultra-safe-encoding) | |
134 | mm-use-ultra-safe-encoding))) | |
135 | (when (or fold mm-use-ultra-safe-encoding) | |
136 | (let ((tab-width 1)) ; HTAB is one character. | |
137 | (goto-char (point-min)) | |
138 | (while (not (eobp)) | |
139 | ;; In ultra-safe mode, encode "From " at the beginning | |
140 | ;; of a line. | |
141 | (when mm-use-ultra-safe-encoding | |
142 | (if (looking-at "From ") | |
143 | (replace-match "From=20" nil t) | |
144 | (if (looking-at "-") | |
145 | (replace-match "=2D" nil t)))) | |
146 | (end-of-line) | |
147 | ;; Fold long lines. | |
148 | (while (> (current-column) 76) ; tab-width must be 1. | |
149 | (beginning-of-line) | |
150 | (forward-char 75) ; 75 chars plus an "=" | |
151 | (search-backward "=" (- (point) 2) t) | |
152 | (insert "=\n") | |
153 | (end-of-line)) | |
154 | (forward-line)))))))) | |
c113de23 GM |
155 | |
156 | (defun quoted-printable-encode-string (string) | |
657b2c65 | 157 | "Encode the STRING as quoted-printable and return the result." |
e4f99da7 DL |
158 | (let ((default-enable-multibyte-characters (mm-multibyte-string-p string))) |
159 | (with-temp-buffer | |
160 | (insert string) | |
161 | (quoted-printable-encode-region (point-min) (point-max)) | |
162 | (buffer-string)))) | |
c113de23 GM |
163 | |
164 | (provide 'qp) | |
165 | ||
ab5796a9 | 166 | ;;; arch-tag: db89e52a-e4a1-4b69-926f-f434f04216ba |
657b2c65 | 167 | ;;; qp.el ends here |