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