Mercurial > emacs
annotate lisp/gnus/qp.el @ 50298:9c1195ddde1a
(easy-menu-add-item): Align the docstring with the code.
author | Stefan Monnier <monnier@iro.umontreal.ca> |
---|---|
date | Mon, 24 Mar 2003 17:28:10 +0000 |
parents | 03cfc305a0fa |
children | 695cf19ef79e d7ddb3e565de |
rev | line source |
---|---|
31717 | 1 ;;; qp.el --- Quoted-Printable functions |
32103 | 2 |
47944
03cfc305a0fa
(quoted-printable-encode-region): Use mm-insert-byte.
Dave Love <fx@gnu.org>
parents:
43737
diff
changeset
|
3 ;; Copyright (C) 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc. |
31717 | 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 | |
32504 | 32 (require 'mm-util) |
33 (eval-when-compile (defvar mm-use-ultra-safe-encoding)) | |
32211
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 |
43737
4f7c660e6029
* qp.el (quoted-printable-decode-region): Doc addition.
ShengHuo ZHU <zsh@cs.rochester.edu>
parents:
38103
diff
changeset
|
38 coding-system. |
4f7c660e6029
* qp.el (quoted-printable-decode-region): Doc addition.
ShengHuo ZHU <zsh@cs.rochester.edu>
parents:
38103
diff
changeset
|
39 |
4f7c660e6029
* qp.el (quoted-printable-decode-region): Doc addition.
ShengHuo ZHU <zsh@cs.rochester.edu>
parents:
38103
diff
changeset
|
40 Interactively, you can supply the CODING-SYSTEM argument |
4f7c660e6029
* qp.el (quoted-printable-decode-region): Doc addition.
ShengHuo ZHU <zsh@cs.rochester.edu>
parents:
38103
diff
changeset
|
41 with \\[universal-coding-system-argument]." |
38103
282970d20daa
(quoted-printable-decode-region): If called interactively,
Eli Zaretskii <eliz@gnu.org>
parents:
35931
diff
changeset
|
42 (interactive |
282970d20daa
(quoted-printable-decode-region): If called interactively,
Eli Zaretskii <eliz@gnu.org>
parents:
35931
diff
changeset
|
43 ;; Let the user determine the coding system with "C-x RET c". |
282970d20daa
(quoted-printable-decode-region): If called interactively,
Eli Zaretskii <eliz@gnu.org>
parents:
35931
diff
changeset
|
44 (list (region-beginning) (region-end) coding-system-for-read)) |
32462
7a39fdec7aac
(quoted-printable-decode-region): Avoid invalid
Dave Love <fx@gnu.org>
parents:
32211
diff
changeset
|
45 (unless (mm-coding-system-p coding-system) ; e.g. `ascii' from Gnus |
7a39fdec7aac
(quoted-printable-decode-region): Avoid invalid
Dave Love <fx@gnu.org>
parents:
32211
diff
changeset
|
46 (setq coding-system nil)) |
31717 | 47 (save-excursion |
48 (save-restriction | |
32462
7a39fdec7aac
(quoted-printable-decode-region): Avoid invalid
Dave Love <fx@gnu.org>
parents:
32211
diff
changeset
|
49 ;; RFC 2045: ``An "=" followed by two hexadecimal digits, one |
7a39fdec7aac
(quoted-printable-decode-region): Avoid invalid
Dave Love <fx@gnu.org>
parents:
32211
diff
changeset
|
50 ;; or both of which are lowercase letters in "abcdef", is |
7a39fdec7aac
(quoted-printable-decode-region): Avoid invalid
Dave Love <fx@gnu.org>
parents:
32211
diff
changeset
|
51 ;; formally illegal. A robust implementation might choose to |
7a39fdec7aac
(quoted-printable-decode-region): Avoid invalid
Dave Love <fx@gnu.org>
parents:
32211
diff
changeset
|
52 ;; recognize them as the corresponding uppercase letters.'' |
32211
5b42e5f7e809
(mm-decode-coding-region, mm-encode-coding-region):
Dave Love <fx@gnu.org>
parents:
32107
diff
changeset
|
53 (let ((case-fold-search t)) |
31717 | 54 (narrow-to-region from to) |
32211
5b42e5f7e809
(mm-decode-coding-region, mm-encode-coding-region):
Dave Love <fx@gnu.org>
parents:
32107
diff
changeset
|
55 ;; 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
|
56 ;; 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
|
57 ;; then get doubly-decoded below. |
5b42e5f7e809
(mm-decode-coding-region, mm-encode-coding-region):
Dave Love <fx@gnu.org>
parents:
32107
diff
changeset
|
58 (if coding-system |
5b42e5f7e809
(mm-decode-coding-region, mm-encode-coding-region):
Dave Love <fx@gnu.org>
parents:
32107
diff
changeset
|
59 (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
|
60 (goto-char (point-min)) |
35619
a670d2680870
Remove un-logged bogus changes from 2000-12-20.
Dave Love <fx@gnu.org>
parents:
34752
diff
changeset
|
61 (while (and (skip-chars-forward "^=") |
32103 | 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))) | |
47944
03cfc305a0fa
(quoted-printable-encode-region): Use mm-insert-byte.
Dave Love <fx@gnu.org>
parents:
43737
diff
changeset
|
69 (mm-insert-byte byte 1) |
32211
5b42e5f7e809
(mm-decode-coding-region, mm-encode-coding-region):
Dave Love <fx@gnu.org>
parents:
32107
diff
changeset
|
70 (delete-char 3) |
32103 | 71 (unless (eq byte ?=) |
72 (backward-char)))) | |
73 (t | |
34025
07dcbc7e702f
(quoted-printable-decode-region): Use error, not message
Dave Love <fx@gnu.org>
parents:
32504
diff
changeset
|
74 (error "Malformed quoted-printable text") |
32211
5b42e5f7e809
(mm-decode-coding-region, mm-encode-coding-region):
Dave Love <fx@gnu.org>
parents:
32107
diff
changeset
|
75 (forward-char))))) |
5b42e5f7e809
(mm-decode-coding-region, mm-encode-coding-region):
Dave Love <fx@gnu.org>
parents:
32107
diff
changeset
|
76 (if coding-system |
5b42e5f7e809
(mm-decode-coding-region, mm-encode-coding-region):
Dave Love <fx@gnu.org>
parents:
32107
diff
changeset
|
77 (mm-decode-coding-region (point-min) (point-max) coding-system))))) |
31717 | 78 |
32211
5b42e5f7e809
(mm-decode-coding-region, mm-encode-coding-region):
Dave Love <fx@gnu.org>
parents:
32107
diff
changeset
|
79 (defun quoted-printable-decode-string (string &optional coding-system) |
32103 | 80 "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
|
81 If CODING-SYSTEM is non-nil, decode the region with coding-system." |
31717 | 82 (with-temp-buffer |
83 (insert string) | |
32211
5b42e5f7e809
(mm-decode-coding-region, mm-encode-coding-region):
Dave Love <fx@gnu.org>
parents:
32107
diff
changeset
|
84 (quoted-printable-decode-region (point-min) (point-max) coding-system) |
31717 | 85 (buffer-string))) |
86 | |
87 (defun quoted-printable-encode-region (from to &optional fold class) | |
32103 | 88 "Quoted-printable encode the region between FROM and TO per RFC 2045. |
31717 | 89 |
32103 | 90 If FOLD, fold long lines at 76 characters (as required by the RFC). |
35619
a670d2680870
Remove un-logged bogus changes from 2000-12-20.
Dave Love <fx@gnu.org>
parents:
34752
diff
changeset
|
91 If CLASS is non-nil, translate the characters not matched by that |
a670d2680870
Remove un-logged bogus changes from 2000-12-20.
Dave Love <fx@gnu.org>
parents:
34752
diff
changeset
|
92 regexp class, which is in the form expected by `skip-chars-forward'. |
a670d2680870
Remove un-logged bogus changes from 2000-12-20.
Dave Love <fx@gnu.org>
parents:
34752
diff
changeset
|
93 You should probably avoid non-ASCII characters in this arg. |
31717 | 94 |
32103 | 95 If `mm-use-ultra-safe-encoding' is set, fold lines unconditionally and |
31717 | 96 encode lines starting with \"From\"." |
97 (interactive "r") | |
47944
03cfc305a0fa
(quoted-printable-encode-region): Use mm-insert-byte.
Dave Love <fx@gnu.org>
parents:
43737
diff
changeset
|
98 (save-excursion |
03cfc305a0fa
(quoted-printable-encode-region): Use mm-insert-byte.
Dave Love <fx@gnu.org>
parents:
43737
diff
changeset
|
99 (goto-char from) |
03cfc305a0fa
(quoted-printable-encode-region): Use mm-insert-byte.
Dave Love <fx@gnu.org>
parents:
43737
diff
changeset
|
100 (if (fboundp 'string-to-multibyte) ; Emacs 22 |
03cfc305a0fa
(quoted-printable-encode-region): Use mm-insert-byte.
Dave Love <fx@gnu.org>
parents:
43737
diff
changeset
|
101 (if (re-search-forward (string-to-multibyte "[^\x0-\x7f\x80-\xff]") |
03cfc305a0fa
(quoted-printable-encode-region): Use mm-insert-byte.
Dave Love <fx@gnu.org>
parents:
43737
diff
changeset
|
102 to t) |
03cfc305a0fa
(quoted-printable-encode-region): Use mm-insert-byte.
Dave Love <fx@gnu.org>
parents:
43737
diff
changeset
|
103 ;; Fixme: This is somewhat misleading. |
03cfc305a0fa
(quoted-printable-encode-region): Use mm-insert-byte.
Dave Love <fx@gnu.org>
parents:
43737
diff
changeset
|
104 (error "Multibyte character in QP encoding region")) |
03cfc305a0fa
(quoted-printable-encode-region): Use mm-insert-byte.
Dave Love <fx@gnu.org>
parents:
43737
diff
changeset
|
105 (if (re-search-forward (mm-string-as-multibyte "[^\0-\377]") to t) |
03cfc305a0fa
(quoted-printable-encode-region): Use mm-insert-byte.
Dave Love <fx@gnu.org>
parents:
43737
diff
changeset
|
106 (error "Multibyte character in QP encoding region")))) |
32103 | 107 (unless class |
34752
f04f551e94ce
* message.el (message-narrow-to-head-1): New function.
ShengHuo ZHU <zsh@cs.rochester.edu>
parents:
34025
diff
changeset
|
108 ;; Avoid using 8bit characters. = is \075. |
f04f551e94ce
* message.el (message-narrow-to-head-1): New function.
ShengHuo ZHU <zsh@cs.rochester.edu>
parents:
34025
diff
changeset
|
109 ;; Equivalent to "^\000-\007\013\015-\037\200-\377=" |
f04f551e94ce
* message.el (message-narrow-to-head-1): New function.
ShengHuo ZHU <zsh@cs.rochester.edu>
parents:
34025
diff
changeset
|
110 (setq class "\010-\012\014\040-\074\076-\177")) |
31717 | 111 (save-excursion |
112 (save-restriction | |
113 (narrow-to-region from to) | |
35619
a670d2680870
Remove un-logged bogus changes from 2000-12-20.
Dave Love <fx@gnu.org>
parents:
34752
diff
changeset
|
114 ;; Encode all the non-ascii and control characters. |
a670d2680870
Remove un-logged bogus changes from 2000-12-20.
Dave Love <fx@gnu.org>
parents:
34752
diff
changeset
|
115 (goto-char (point-min)) |
a670d2680870
Remove un-logged bogus changes from 2000-12-20.
Dave Love <fx@gnu.org>
parents:
34752
diff
changeset
|
116 (while (and (skip-chars-forward class) |
a670d2680870
Remove un-logged bogus changes from 2000-12-20.
Dave Love <fx@gnu.org>
parents:
34752
diff
changeset
|
117 (not (eobp))) |
a670d2680870
Remove un-logged bogus changes from 2000-12-20.
Dave Love <fx@gnu.org>
parents:
34752
diff
changeset
|
118 (insert |
a670d2680870
Remove un-logged bogus changes from 2000-12-20.
Dave Love <fx@gnu.org>
parents:
34752
diff
changeset
|
119 (prog1 |
47944
03cfc305a0fa
(quoted-printable-encode-region): Use mm-insert-byte.
Dave Love <fx@gnu.org>
parents:
43737
diff
changeset
|
120 ;; To unibyte in case of Emacs 22 eight-bit. |
03cfc305a0fa
(quoted-printable-encode-region): Use mm-insert-byte.
Dave Love <fx@gnu.org>
parents:
43737
diff
changeset
|
121 (format "=%02X" (mm-multibyte-char-to-unibyte (char-after))) |
35619
a670d2680870
Remove un-logged bogus changes from 2000-12-20.
Dave Love <fx@gnu.org>
parents:
34752
diff
changeset
|
122 (delete-char 1)))) |
a670d2680870
Remove un-logged bogus changes from 2000-12-20.
Dave Love <fx@gnu.org>
parents:
34752
diff
changeset
|
123 ;; Encode white space at the end of lines. |
a670d2680870
Remove un-logged bogus changes from 2000-12-20.
Dave Love <fx@gnu.org>
parents:
34752
diff
changeset
|
124 (goto-char (point-min)) |
a670d2680870
Remove un-logged bogus changes from 2000-12-20.
Dave Love <fx@gnu.org>
parents:
34752
diff
changeset
|
125 (while (re-search-forward "[ \t]+$" nil t) |
a670d2680870
Remove un-logged bogus changes from 2000-12-20.
Dave Love <fx@gnu.org>
parents:
34752
diff
changeset
|
126 (goto-char (match-beginning 0)) |
a670d2680870
Remove un-logged bogus changes from 2000-12-20.
Dave Love <fx@gnu.org>
parents:
34752
diff
changeset
|
127 (while (not (eolp)) |
31717 | 128 (insert |
129 (prog1 | |
34752
f04f551e94ce
* message.el (message-narrow-to-head-1): New function.
ShengHuo ZHU <zsh@cs.rochester.edu>
parents:
34025
diff
changeset
|
130 (format "=%02X" (char-after)) |
35931
ccc41398da84
(quoted-printable-encode-region): Remove redundant code
Dave Love <fx@gnu.org>
parents:
35619
diff
changeset
|
131 (delete-char 1))))) |
ccc41398da84
(quoted-printable-encode-region): Remove redundant code
Dave Love <fx@gnu.org>
parents:
35619
diff
changeset
|
132 (let ((mm-use-ultra-safe-encoding |
ccc41398da84
(quoted-printable-encode-region): Remove redundant code
Dave Love <fx@gnu.org>
parents:
35619
diff
changeset
|
133 (and (boundp 'mm-use-ultra-safe-encoding) |
ccc41398da84
(quoted-printable-encode-region): Remove redundant code
Dave Love <fx@gnu.org>
parents:
35619
diff
changeset
|
134 mm-use-ultra-safe-encoding))) |
ccc41398da84
(quoted-printable-encode-region): Remove redundant code
Dave Love <fx@gnu.org>
parents:
35619
diff
changeset
|
135 (when (or fold mm-use-ultra-safe-encoding) |
ccc41398da84
(quoted-printable-encode-region): Remove redundant code
Dave Love <fx@gnu.org>
parents:
35619
diff
changeset
|
136 (let ((tab-width 1)) ; HTAB is one character. |
ccc41398da84
(quoted-printable-encode-region): Remove redundant code
Dave Love <fx@gnu.org>
parents:
35619
diff
changeset
|
137 (goto-char (point-min)) |
ccc41398da84
(quoted-printable-encode-region): Remove redundant code
Dave Love <fx@gnu.org>
parents:
35619
diff
changeset
|
138 (while (not (eobp)) |
ccc41398da84
(quoted-printable-encode-region): Remove redundant code
Dave Love <fx@gnu.org>
parents:
35619
diff
changeset
|
139 ;; In ultra-safe mode, encode "From " at the beginning |
ccc41398da84
(quoted-printable-encode-region): Remove redundant code
Dave Love <fx@gnu.org>
parents:
35619
diff
changeset
|
140 ;; of a line. |
ccc41398da84
(quoted-printable-encode-region): Remove redundant code
Dave Love <fx@gnu.org>
parents:
35619
diff
changeset
|
141 (when mm-use-ultra-safe-encoding |
ccc41398da84
(quoted-printable-encode-region): Remove redundant code
Dave Love <fx@gnu.org>
parents:
35619
diff
changeset
|
142 (if (looking-at "From ") |
ccc41398da84
(quoted-printable-encode-region): Remove redundant code
Dave Love <fx@gnu.org>
parents:
35619
diff
changeset
|
143 (replace-match "From=20" nil t) |
ccc41398da84
(quoted-printable-encode-region): Remove redundant code
Dave Love <fx@gnu.org>
parents:
35619
diff
changeset
|
144 (if (looking-at "-") |
ccc41398da84
(quoted-printable-encode-region): Remove redundant code
Dave Love <fx@gnu.org>
parents:
35619
diff
changeset
|
145 (replace-match "=2D" nil t)))) |
ccc41398da84
(quoted-printable-encode-region): Remove redundant code
Dave Love <fx@gnu.org>
parents:
35619
diff
changeset
|
146 (end-of-line) |
ccc41398da84
(quoted-printable-encode-region): Remove redundant code
Dave Love <fx@gnu.org>
parents:
35619
diff
changeset
|
147 ;; Fold long lines. |
ccc41398da84
(quoted-printable-encode-region): Remove redundant code
Dave Love <fx@gnu.org>
parents:
35619
diff
changeset
|
148 (while (> (current-column) 76) ; tab-width must be 1. |
ccc41398da84
(quoted-printable-encode-region): Remove redundant code
Dave Love <fx@gnu.org>
parents:
35619
diff
changeset
|
149 (beginning-of-line) |
ccc41398da84
(quoted-printable-encode-region): Remove redundant code
Dave Love <fx@gnu.org>
parents:
35619
diff
changeset
|
150 (forward-char 75) ; 75 chars plus an "=" |
ccc41398da84
(quoted-printable-encode-region): Remove redundant code
Dave Love <fx@gnu.org>
parents:
35619
diff
changeset
|
151 (search-backward "=" (- (point) 2) t) |
ccc41398da84
(quoted-printable-encode-region): Remove redundant code
Dave Love <fx@gnu.org>
parents:
35619
diff
changeset
|
152 (insert "=\n") |
ccc41398da84
(quoted-printable-encode-region): Remove redundant code
Dave Love <fx@gnu.org>
parents:
35619
diff
changeset
|
153 (end-of-line)) |
ccc41398da84
(quoted-printable-encode-region): Remove redundant code
Dave Love <fx@gnu.org>
parents:
35619
diff
changeset
|
154 (forward-line)))))))) |
31717 | 155 |
156 (defun quoted-printable-encode-string (string) | |
32103 | 157 "Encode the STRING as quoted-printable and return the result." |
35619
a670d2680870
Remove un-logged bogus changes from 2000-12-20.
Dave Love <fx@gnu.org>
parents:
34752
diff
changeset
|
158 (let ((default-enable-multibyte-characters (mm-multibyte-string-p string))) |
a670d2680870
Remove un-logged bogus changes from 2000-12-20.
Dave Love <fx@gnu.org>
parents:
34752
diff
changeset
|
159 (with-temp-buffer |
a670d2680870
Remove un-logged bogus changes from 2000-12-20.
Dave Love <fx@gnu.org>
parents:
34752
diff
changeset
|
160 (insert string) |
a670d2680870
Remove un-logged bogus changes from 2000-12-20.
Dave Love <fx@gnu.org>
parents:
34752
diff
changeset
|
161 (quoted-printable-encode-region (point-min) (point-max)) |
a670d2680870
Remove un-logged bogus changes from 2000-12-20.
Dave Love <fx@gnu.org>
parents:
34752
diff
changeset
|
162 (buffer-string)))) |
31717 | 163 |
164 (provide 'qp) | |
165 | |
32103 | 166 ;;; qp.el ends here |