Commit | Line | Data |
---|---|---|
f4dd4ae8 | 1 | ;;; flow-fill.el --- interpret RFC2646 "flowed" text |
c113de23 | 2 | |
e84b4b86 | 3 | ;; Copyright (C) 2000, 2001, 2002, 2003, 2004, |
e3fe4da0 | 4 | ;; 2005, 2006, 2007, 2008 Free Software Foundation, Inc. |
c113de23 GM |
5 | |
6 | ;; Author: Simon Josefsson <jas@pdc.kth.se> | |
7 | ;; Keywords: mail | |
8 | ||
9 | ;; This file is part of GNU Emacs. | |
10 | ||
11 | ;; GNU Emacs is free software; you can redistribute it and/or modify | |
12 | ;; it under the terms of the GNU General Public License as published by | |
5a9dffec | 13 | ;; the Free Software Foundation; either version 3, or (at your option) |
c113de23 GM |
14 | ;; any later version. |
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 | |
18 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
19 | ;; GNU General Public License for more details. | |
20 | ||
21 | ;; You should have received a copy of the GNU General Public License | |
22 | ;; along with GNU Emacs; see the file COPYING. If not, write to the | |
3a35cf56 LK |
23 | ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
24 | ;; Boston, MA 02110-1301, USA. | |
c113de23 GM |
25 | |
26 | ;;; Commentary: | |
27 | ||
28 | ;; This implement decoding of RFC2646 formatted text, including the | |
29 | ;; quoted-depth wins rules. | |
30 | ||
31 | ;; Theory of operation: search for lines ending with SPC, save quote | |
32 | ;; length of line, remove SPC and concatenate line with the following | |
33 | ;; line if quote length of following line matches current line. | |
34 | ||
35 | ;; When no further concatenations are possible, we've found a | |
36 | ;; paragraph and we let `fill-region' fill the long line into several | |
37 | ;; lines with the quote prefix as `fill-prefix'. | |
38 | ||
23f87bed | 39 | ;; Todo: implement basic `fill-region' (Emacs and XEmacs |
01b5d73b | 40 | ;; implementations differ..) |
c113de23 | 41 | |
23f87bed | 42 | ;;; History: |
c113de23 GM |
43 | |
44 | ;; 2000-02-17 posted on ding mailing list | |
45 | ;; 2000-02-19 use `point-at-{b,e}ol' in XEmacs | |
46 | ;; 2000-03-11 no compile warnings for point-at-bol stuff | |
8f688cb0 | 47 | ;; 2000-03-26 committed to gnus cvs |
01b5d73b DL |
48 | ;; 2000-10-23 don't flow "-- " lines, make "quote-depth wins" rule |
49 | ;; work when first line is at level 0. | |
23f87bed MB |
50 | ;; 2002-01-12 probably incomplete encoding support |
51 | ;; 2003-12-08 started working on test harness. | |
c113de23 GM |
52 | |
53 | ;;; Code: | |
54 | ||
01b5d73b DL |
55 | (eval-when-compile (require 'cl)) |
56 | ||
23f87bed MB |
57 | (defcustom fill-flowed-display-column 'fill-column |
58 | "Column beyond which format=flowed lines are wrapped, when displayed. | |
59 | This can be a Lisp expression or an integer." | |
bf247b6e | 60 | :version "22.1" |
e79f14a4 | 61 | :group 'mime-display |
23f87bed MB |
62 | :type '(choice (const :tag "Standard `fill-column'" fill-column) |
63 | (const :tag "Fit Window" (- (window-width) 5)) | |
64 | (sexp) | |
65 | (integer))) | |
66 | ||
67 | (defcustom fill-flowed-encode-column 66 | |
68 | "Column beyond which format=flowed lines are wrapped, in outgoing messages. | |
69 | This can be a Lisp expression or an integer. | |
70 | RFC 2646 suggests 66 characters for readability." | |
bf247b6e | 71 | :version "22.1" |
e79f14a4 | 72 | :group 'mime-display |
23f87bed MB |
73 | :type '(choice (const :tag "Standard fill-column" fill-column) |
74 | (const :tag "RFC 2646 default (66)" 66) | |
75 | (sexp) | |
76 | (integer))) | |
77 | ||
23f87bed MB |
78 | ;;;###autoload |
79 | (defun fill-flowed-encode (&optional buffer) | |
80 | (with-current-buffer (or buffer (current-buffer)) | |
81 | ;; No point in doing this unless hard newlines is used. | |
82 | (when use-hard-newlines | |
83 | (let ((start (point-min)) end) | |
84 | ;; Go through each paragraph, filling it and adding SPC | |
85 | ;; as the last character on each line. | |
86 | (while (setq end (text-property-any start (point-max) 'hard 't)) | |
87 | (let ((fill-column (eval fill-flowed-encode-column))) | |
88 | (fill-region start end t 'nosqueeze 'to-eop)) | |
89 | (goto-char start) | |
90 | ;; `fill-region' probably distorted end. | |
91 | (setq end (text-property-any start (point-max) 'hard 't)) | |
92 | (while (and (< (point) end) | |
93 | (re-search-forward "$" (1- end) t)) | |
94 | (insert " ") | |
95 | (setq end (1+ end)) | |
96 | (forward-char)) | |
97 | (goto-char (setq start (1+ end))))) | |
98 | t))) | |
99 | ||
100 | ;;;###autoload | |
01c52d31 | 101 | (defun fill-flowed (&optional buffer delete-space) |
c113de23 GM |
102 | (save-excursion |
103 | (set-buffer (or (current-buffer) buffer)) | |
104 | (goto-char (point-min)) | |
5f4264e5 | 105 | ;; Remove space stuffing. |
e4a89ccf | 106 | (while (re-search-forward "^\\( \\|>+ $\\)" nil t) |
5f4264e5 MB |
107 | (delete-char -1) |
108 | (forward-line 1)) | |
109 | (goto-char (point-min)) | |
c113de23 | 110 | (while (re-search-forward " $" nil t) |
01c52d31 MB |
111 | (when delete-space |
112 | (delete-char -1)) | |
c113de23 GM |
113 | (when (save-excursion |
114 | (beginning-of-line) | |
ba0226dd | 115 | (looking-at "^\\(>*\\)\\( ?\\)")) |
23f87bed MB |
116 | (let ((quote (match-string 1)) |
117 | sig) | |
c113de23 GM |
118 | (if (string= quote "") |
119 | (setq quote nil)) | |
120 | (when (and quote (string= (match-string 2) "")) | |
121 | (save-excursion | |
122 | ;; insert SP after quote for pleasant reading of quoted lines | |
123 | (beginning-of-line) | |
124 | (when (> (skip-chars-forward ">") 0) | |
125 | (insert " ")))) | |
23f87bed | 126 | ;; XXX slightly buggy handling of "-- " |
c113de23 | 127 | (while (and (save-excursion |
01b5d73b DL |
128 | (ignore-errors (backward-char 3)) |
129 | (setq sig (looking-at "-- ")) | |
c113de23 GM |
130 | (looking-at "[^-][^-] ")) |
131 | (save-excursion | |
132 | (unless (eobp) | |
133 | (forward-char 1) | |
23f87bed MB |
134 | (looking-at (format "^\\(%s\\)\\([^>\n\r]\\)" |
135 | (or quote " ?")))))) | |
c113de23 GM |
136 | (save-excursion |
137 | (replace-match (if (string= (match-string 2) " ") | |
138 | "" "\\2"))) | |
139 | (backward-delete-char -1) | |
140 | (end-of-line)) | |
01b5d73b | 141 | (unless sig |
23f87bed MB |
142 | (condition-case nil |
143 | (let ((fill-prefix (when quote (concat quote " "))) | |
144 | (fill-column (eval fill-flowed-display-column)) | |
5f4264e5 MB |
145 | filladapt-mode |
146 | adaptive-fill-mode) | |
01c52d31 MB |
147 | (fill-region (point-at-bol) |
148 | (min (1+ (point-at-eol)) | |
23f87bed MB |
149 | (point-max)) |
150 | 'left 'nosqueeze)) | |
151 | (error | |
152 | (forward-line 1) | |
153 | nil)))))))) | |
154 | ||
155 | ;; Test vectors. | |
156 | ||
9efa445f | 157 | (defvar show-trailing-whitespace) |
23f87bed MB |
158 | |
159 | (defvar fill-flowed-encode-tests | |
3b059693 | 160 | `( |
23f87bed MB |
161 | ;; The syntax of each list element is: |
162 | ;; (INPUT . EXPECTED-OUTPUT) | |
3b059693 MB |
163 | (,(concat |
164 | "> Thou villainous ill-breeding spongy dizzy-eyed \n" | |
165 | "> reeky elf-skinned pigeon-egg! \n" | |
166 | ">> Thou artless swag-bellied milk-livered \n" | |
167 | ">> dismal-dreaming idle-headed scut!\n" | |
168 | ">>> Thou errant folly-fallen spleeny reeling-ripe \n" | |
169 | ">>> unmuzzled ratsbane!\n" | |
170 | ">>>> Henceforth, the coding style is to be strictly \n" | |
171 | ">>>> enforced, including the use of only upper case.\n" | |
172 | ">>>>> I've noticed a lack of adherence to the coding \n" | |
173 | ">>>>> styles, of late.\n" | |
174 | ">>>>>> Any complaints?") | |
175 | . | |
176 | ,(concat | |
177 | "> Thou villainous ill-breeding spongy dizzy-eyed reeky elf-skinned\n" | |
178 | "> pigeon-egg! \n" | |
179 | ">> Thou artless swag-bellied milk-livered dismal-dreaming idle-headed\n" | |
180 | ">> scut!\n" | |
181 | ">>> Thou errant folly-fallen spleeny reeling-ripe unmuzzled ratsbane!\n" | |
182 | ">>>> Henceforth, the coding style is to be strictly enforced,\n" | |
183 | ">>>> including the use of only upper case.\n" | |
184 | ">>>>> I've noticed a lack of adherence to the coding styles, of late.\n" | |
185 | ">>>>>> Any complaints?\n" | |
186 | )) | |
187 | ;; (,(concat | |
188 | ;; "\n" | |
189 | ;; "> foo\n" | |
190 | ;; "> \n" | |
191 | ;; "> \n" | |
192 | ;; "> bar\n") | |
193 | ;; . | |
194 | ;; ,(concat | |
195 | ;; "\n" | |
196 | ;; "> foo bar\n")) | |
23f87bed MB |
197 | )) |
198 | ||
199 | (defun fill-flowed-test () | |
200 | (interactive "") | |
201 | (switch-to-buffer (get-buffer-create "*Format=Flowed test output*")) | |
202 | (erase-buffer) | |
203 | (setq show-trailing-whitespace t) | |
204 | (dolist (test fill-flowed-encode-tests) | |
205 | (let (start output) | |
206 | (insert "***** BEGIN TEST INPUT *****\n") | |
207 | (insert (car test)) | |
208 | (insert "***** END TEST INPUT *****\n\n") | |
209 | (insert "***** BEGIN TEST OUTPUT *****\n") | |
210 | (setq start (point)) | |
211 | (insert (car test)) | |
212 | (save-restriction | |
213 | (narrow-to-region start (point)) | |
214 | (fill-flowed)) | |
215 | (setq output (buffer-substring start (point-max))) | |
216 | (insert "***** END TEST OUTPUT *****\n") | |
217 | (unless (string= output (cdr test)) | |
218 | (insert "\n***** BEGIN TEST EXPECTED OUTPUT *****\n") | |
219 | (insert (cdr test)) | |
220 | (insert "***** END TEST EXPECTED OUTPUT *****\n")) | |
221 | (insert "\n\n"))) | |
222 | (goto-char (point-max))) | |
c113de23 GM |
223 | |
224 | (provide 'flow-fill) | |
225 | ||
cbee283d | 226 | ;; arch-tag: addc0040-bc53-4f17-b4bc-1eb44eed6f0b |
c113de23 | 227 | ;;; flow-fill.el ends here |