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