Rename `autoselect-window' to `mouse-autoselect-window'.
[emacs.git] / lisp / gnus / qp.el
blob8fd0053a812f68f000db991301c1491bb15d15f7
1 ;;; qp.el --- Quoted-Printable functions
3 ;; Copyright (C) 1998, 1999, 2000, 2001 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 2, or (at your option)
13 ;; 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; 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.
25 ;;; Commentary:
27 ;; Functions for encoding and decoding quoted-printable text as
28 ;; defined in RFC 2045.
30 ;;; Code:
32 (require 'mm-util)
33 (eval-when-compile (defvar mm-use-ultra-safe-encoding))
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.
40 Interactively, you can supply the CODING-SYSTEM argument
41 with \\[universal-coding-system-argument]."
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))
45 (unless (mm-coding-system-p coding-system) ; e.g. `ascii' from Gnus
46 (setq coding-system nil))
47 (save-excursion
48 (save-restriction
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.''
53 (let ((case-fold-search t))
54 (narrow-to-region from to)
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))
61 (while (and (skip-chars-forward "^=")
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)))
69 (insert byte)
70 (delete-char 3)
71 (unless (eq byte ?=)
72 (backward-char))))
74 (error "Malformed quoted-printable text")
75 (forward-char)))))
76 (if coding-system
77 (mm-decode-coding-region (point-min) (point-max) coding-system)))))
79 (defun quoted-printable-decode-string (string &optional coding-system)
80 "Decode the quoted-printable encoded STRING and return the result.
81 If CODING-SYSTEM is non-nil, decode the region with coding-system."
82 (with-temp-buffer
83 (insert string)
84 (quoted-printable-decode-region (point-min) (point-max) coding-system)
85 (buffer-string)))
87 (defun quoted-printable-encode-region (from to &optional fold class)
88 "Quoted-printable encode the region between FROM and TO per RFC 2045.
90 If FOLD, fold long lines at 76 characters (as required by the RFC).
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.
95 If `mm-use-ultra-safe-encoding' is set, fold lines unconditionally and
96 encode lines starting with \"From\"."
97 (interactive "r")
98 ;; Fixme: what should this do in XEmacs/Mule?
99 (if (fboundp 'find-charset-region) ; else XEmacs, non-Mule
100 (if (delq 'unknown ; Emacs 20 unibyte
101 (delq 'eight-bit-graphic ; Emacs 21
102 (delq 'eight-bit-control
103 (delq 'ascii (find-charset-region from to)))))
104 (error "Multibyte character in QP encoding region")))
105 (unless class
106 ;; Avoid using 8bit characters. = is \075.
107 ;; Equivalent to "^\000-\007\013\015-\037\200-\377="
108 (setq class "\010-\012\014\040-\074\076-\177"))
109 (save-excursion
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 (format "=%02X" (char-after))
119 (delete-char 1))))
120 ;; Encode white space at the end of lines.
121 (goto-char (point-min))
122 (while (re-search-forward "[ \t]+$" nil t)
123 (goto-char (match-beginning 0))
124 (while (not (eolp))
125 (insert
126 (prog1
127 (format "=%02X" (char-after))
128 (delete-char 1)))))
129 (let ((mm-use-ultra-safe-encoding
130 (and (boundp 'mm-use-ultra-safe-encoding)
131 mm-use-ultra-safe-encoding)))
132 (when (or fold mm-use-ultra-safe-encoding)
133 (let ((tab-width 1)) ; HTAB is one character.
134 (goto-char (point-min))
135 (while (not (eobp))
136 ;; In ultra-safe mode, encode "From " at the beginning
137 ;; of a line.
138 (when mm-use-ultra-safe-encoding
139 (if (looking-at "From ")
140 (replace-match "From=20" nil t)
141 (if (looking-at "-")
142 (replace-match "=2D" nil t))))
143 (end-of-line)
144 ;; Fold long lines.
145 (while (> (current-column) 76) ; tab-width must be 1.
146 (beginning-of-line)
147 (forward-char 75) ; 75 chars plus an "="
148 (search-backward "=" (- (point) 2) t)
149 (insert "=\n")
150 (end-of-line))
151 (forward-line))))))))
153 (defun quoted-printable-encode-string (string)
154 "Encode the STRING as quoted-printable and return the result."
155 (let ((default-enable-multibyte-characters (mm-multibyte-string-p string)))
156 (with-temp-buffer
157 (insert string)
158 (quoted-printable-encode-region (point-min) (point-max))
159 (buffer-string))))
161 (provide 'qp)
163 ;;; qp.el ends here