Mercurial > emacs
annotate lisp/gnus/qp.el @ 32433:e68bc04229fe
(Fx_create_frame): Don't bother calling
face-set-after-frame-default since the caller does it for us anyway.
author | Stefan Monnier <monnier@iro.umontreal.ca> |
---|---|
date | Thu, 12 Oct 2000 20:31:31 +0000 |
parents | 5b42e5f7e809 |
children | 7a39fdec7aac |
rev | line source |
---|---|
31717 | 1 ;;; qp.el --- Quoted-Printable functions |
32103 | 2 |
31717 | 3 ;; Copyright (C) 1998, 1999, 2000 Free Software Foundation, Inc. |
4 | |
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org> | |
32103 | 6 ;; Keywords: mail, extensions |
7 | |
31717 | 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 | |
32103 | 27 ;; Functions for encoding and decoding quoted-printable text as |
28 ;; defined in RFC 2045. | |
29 | |
31717 | 30 ;;; Code: |
31 | |
32211
5b42e5f7e809
(mm-decode-coding-region, mm-encode-coding-region):
Dave Love <fx@gnu.org>
parents:
32107
diff
changeset
|
32 (autoload 'mm-decode-coding-region "mm-util") |
5b42e5f7e809
(mm-decode-coding-region, mm-encode-coding-region):
Dave Love <fx@gnu.org>
parents:
32107
diff
changeset
|
33 (autoload 'mm-encode-coding-region "mm-util") |
5b42e5f7e809
(mm-decode-coding-region, mm-encode-coding-region):
Dave Love <fx@gnu.org>
parents:
32107
diff
changeset
|
34 |
5b42e5f7e809
(mm-decode-coding-region, mm-encode-coding-region):
Dave Love <fx@gnu.org>
parents:
32107
diff
changeset
|
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
5b42e5f7e809
(mm-decode-coding-region, mm-encode-coding-region):
Dave Love <fx@gnu.org>
parents:
32107
diff
changeset
|
37 If CODING-SYSTEM is non-nil, decode bytes into characters with that |
5b42e5f7e809
(mm-decode-coding-region, mm-encode-coding-region):
Dave Love <fx@gnu.org>
parents:
32107
diff
changeset
|
38 coding-system." |
31717 | 39 (interactive "r") |
40 (save-excursion | |
41 (save-restriction | |
32211
5b42e5f7e809
(mm-decode-coding-region, mm-encode-coding-region):
Dave Love <fx@gnu.org>
parents:
32107
diff
changeset
|
42 ;; RFC 2045: An "=" followed by two hexadecimal digits, one or |
5b42e5f7e809
(mm-decode-coding-region, mm-encode-coding-region):
Dave Love <fx@gnu.org>
parents:
32107
diff
changeset
|
43 ;; both of which are lowercase letters in "abcdef", is formally |
5b42e5f7e809
(mm-decode-coding-region, mm-encode-coding-region):
Dave Love <fx@gnu.org>
parents:
32107
diff
changeset
|
44 ;; illegal. A robust implementation might choose to recognize |
5b42e5f7e809
(mm-decode-coding-region, mm-encode-coding-region):
Dave Love <fx@gnu.org>
parents:
32107
diff
changeset
|
45 ;; them as the corresponding uppercase letters. |
5b42e5f7e809
(mm-decode-coding-region, mm-encode-coding-region):
Dave Love <fx@gnu.org>
parents:
32107
diff
changeset
|
46 (let ((case-fold-search t)) |
31717 | 47 (narrow-to-region from to) |
32211
5b42e5f7e809
(mm-decode-coding-region, mm-encode-coding-region):
Dave Love <fx@gnu.org>
parents:
32107
diff
changeset
|
48 ;; Do this in case we're called from Gnus, say, in a buffer |
5b42e5f7e809
(mm-decode-coding-region, mm-encode-coding-region):
Dave Love <fx@gnu.org>
parents:
32107
diff
changeset
|
49 ;; which already contains non-ASCII characters which would |
5b42e5f7e809
(mm-decode-coding-region, mm-encode-coding-region):
Dave Love <fx@gnu.org>
parents:
32107
diff
changeset
|
50 ;; then get doubly-decoded below. |
5b42e5f7e809
(mm-decode-coding-region, mm-encode-coding-region):
Dave Love <fx@gnu.org>
parents:
32107
diff
changeset
|
51 (if coding-system |
5b42e5f7e809
(mm-decode-coding-region, mm-encode-coding-region):
Dave Love <fx@gnu.org>
parents:
32107
diff
changeset
|
52 (mm-encode-coding-region (point-min) (point-max) coding-system)) |
5b42e5f7e809
(mm-decode-coding-region, mm-encode-coding-region):
Dave Love <fx@gnu.org>
parents:
32107
diff
changeset
|
53 (goto-char (point-min)) |
32103 | 54 (while (and (skip-chars-forward "^=" to) |
55 (not (eobp))) | |
56 (cond ((eq (char-after (1+ (point))) ?\n) | |
57 (delete-char 2)) | |
58 ((looking-at "=[0-9A-F][0-9A-F]") | |
59 (let ((byte (string-to-int (buffer-substring (1+ (point)) | |
60 (+ 3 (point))) | |
61 16))) | |
32211
5b42e5f7e809
(mm-decode-coding-region, mm-encode-coding-region):
Dave Love <fx@gnu.org>
parents:
32107
diff
changeset
|
62 (insert byte) |
5b42e5f7e809
(mm-decode-coding-region, mm-encode-coding-region):
Dave Love <fx@gnu.org>
parents:
32107
diff
changeset
|
63 (delete-char 3) |
32103 | 64 (unless (eq byte ?=) |
65 (backward-char)))) | |
66 (t | |
67 (message "Malformed MIME quoted-printable message") | |
32211
5b42e5f7e809
(mm-decode-coding-region, mm-encode-coding-region):
Dave Love <fx@gnu.org>
parents:
32107
diff
changeset
|
68 (forward-char))))) |
5b42e5f7e809
(mm-decode-coding-region, mm-encode-coding-region):
Dave Love <fx@gnu.org>
parents:
32107
diff
changeset
|
69 (if coding-system |
5b42e5f7e809
(mm-decode-coding-region, mm-encode-coding-region):
Dave Love <fx@gnu.org>
parents:
32107
diff
changeset
|
70 (mm-decode-coding-region (point-min) (point-max) coding-system))))) |
31717 | 71 |
32211
5b42e5f7e809
(mm-decode-coding-region, mm-encode-coding-region):
Dave Love <fx@gnu.org>
parents:
32107
diff
changeset
|
72 (defun quoted-printable-decode-string (string &optional coding-system) |
32103 | 73 "Decode the quoted-printable encoded STRING and return the result. |
32211
5b42e5f7e809
(mm-decode-coding-region, mm-encode-coding-region):
Dave Love <fx@gnu.org>
parents:
32107
diff
changeset
|
74 If CODING-SYSTEM is non-nil, decode the region with coding-system." |
31717 | 75 (with-temp-buffer |
76 (insert string) | |
32211
5b42e5f7e809
(mm-decode-coding-region, mm-encode-coding-region):
Dave Love <fx@gnu.org>
parents:
32107
diff
changeset
|
77 (quoted-printable-decode-region (point-min) (point-max) coding-system) |
31717 | 78 (buffer-string))) |
79 | |
80 (defun quoted-printable-encode-region (from to &optional fold class) | |
32103 | 81 "Quoted-printable encode the region between FROM and TO per RFC 2045. |
31717 | 82 |
32103 | 83 If FOLD, fold long lines at 76 characters (as required by the RFC). |
84 If CLASS is non-nil, translate the characters matched by that class in | |
85 the form expected by `skip-chars-forward'. | |
31717 | 86 |
32103 | 87 If `mm-use-ultra-safe-encoding' is set, fold lines unconditionally and |
31717 | 88 encode lines starting with \"From\"." |
89 (interactive "r") | |
32107
31d231ad0b91
(quoted-printable-encode-region): Don't use mm-find-charset-region.
Dave Love <fx@gnu.org>
parents:
32103
diff
changeset
|
90 ;; Fixme: what should this do in XEmacs/Mule? |
31d231ad0b91
(quoted-printable-encode-region): Don't use mm-find-charset-region.
Dave Love <fx@gnu.org>
parents:
32103
diff
changeset
|
91 (if (fboundp 'find-charset-region) ; else XEmacs, non-Mule |
31d231ad0b91
(quoted-printable-encode-region): Don't use mm-find-charset-region.
Dave Love <fx@gnu.org>
parents:
32103
diff
changeset
|
92 (if (delq 'unknown ; Emacs 20 unibyte |
31d231ad0b91
(quoted-printable-encode-region): Don't use mm-find-charset-region.
Dave Love <fx@gnu.org>
parents:
32103
diff
changeset
|
93 (delq 'eight-bit-graphic ; Emacs 21 |
31d231ad0b91
(quoted-printable-encode-region): Don't use mm-find-charset-region.
Dave Love <fx@gnu.org>
parents:
32103
diff
changeset
|
94 (delq 'eight-bit-control |
31d231ad0b91
(quoted-printable-encode-region): Don't use mm-find-charset-region.
Dave Love <fx@gnu.org>
parents:
32103
diff
changeset
|
95 (delq 'ascii (find-charset-region from to))))) |
31d231ad0b91
(quoted-printable-encode-region): Don't use mm-find-charset-region.
Dave Love <fx@gnu.org>
parents:
32103
diff
changeset
|
96 (error "Multibyte character in QP encoding region"))) |
32103 | 97 (unless class |
98 (setq class "^\000-\007\013\015-\037\200-\377=")) | |
99 (if (fboundp 'string-as-multibyte) | |
100 (setq class (string-as-multibyte class))) | |
31717 | 101 (save-excursion |
102 (save-restriction | |
103 (narrow-to-region from to) | |
104 ;; Encode all the non-ascii and control characters. | |
105 (goto-char (point-min)) | |
32103 | 106 (while (and (skip-chars-forward class) |
31717 | 107 (not (eobp))) |
108 (insert | |
109 (prog1 | |
32103 | 110 (format "=%02x" (upcase (char-after))) |
31717 | 111 (delete-char 1)))) |
112 ;; Encode white space at the end of lines. | |
113 (goto-char (point-min)) | |
114 (while (re-search-forward "[ \t]+$" nil t) | |
115 (goto-char (match-beginning 0)) | |
116 (while (not (eolp)) | |
117 (insert | |
118 (prog1 | |
32103 | 119 (format "=%02x" (upcase (char-after))) |
31717 | 120 (delete-char 1))))) |
32103 | 121 (let ((mm-use-ultra-safe-encoding |
122 (and (boundp 'mm-use-ultra-safe-encoding) | |
123 mm-use-ultra-safe-encoding))) | |
124 (when (or fold mm-use-ultra-safe-encoding) | |
125 ;; Fold long lines. | |
126 (let ((tab-width 1)) ; HTAB is one character. | |
127 (goto-char (point-min)) | |
128 (while (not (eobp)) | |
129 ;; In ultra-safe mode, encode "From " at the beginning | |
130 ;; of a line. | |
131 (when mm-use-ultra-safe-encoding | |
132 (beginning-of-line) | |
133 (when (looking-at "From ") | |
134 (replace-match "From=20" nil t))) | |
135 (end-of-line) | |
136 (while (> (current-column) 76) ; tab-width must be 1. | |
137 (beginning-of-line) | |
138 (forward-char 75) ; 75 chars plus an "=" | |
139 (search-backward "=" (- (point) 2) t) | |
140 (insert "=\n") | |
141 (end-of-line)) | |
142 (unless (eobp) | |
143 (forward-line))))))))) | |
31717 | 144 |
145 (defun quoted-printable-encode-string (string) | |
32103 | 146 "Encode the STRING as quoted-printable and return the result." |
147 (with-temp-buffer | |
31717 | 148 (insert string) |
149 (quoted-printable-encode-region (point-min) (point-max)) | |
150 (buffer-string))) | |
151 | |
152 (provide 'qp) | |
153 | |
32103 | 154 ;;; qp.el ends here |