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