Revision: miles@gnu.org--gnu-2004/gnus--devo--0--patch-99
[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   :group 'mime-display
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.
67 This can be a Lisp expression or an integer.
68 RFC 2646 suggests 66 characters for readability."
69   :group 'mime-display
70   :type '(choice (const :tag "Standard fill-column" fill-column)
71                  (const :tag "RFC 2646 default (66)" 66)
72                  (sexp)
73                  (integer)))
74
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))
84           (let ((fill-column (eval fill-flowed-encode-column)))
85             (fill-region start end t 'nosqueeze 'to-eop))
86           (goto-char start)
87           ;; `fill-region' probably distorted end.
88           (setq end (text-property-any start (point-max) 'hard 't))
89           (while (and (< (point) end)
90                       (re-search-forward "$" (1- end) t))
91             (insert " ")
92             (setq end (1+ end))
93             (forward-char))
94           (goto-char (setq start (1+ end)))))
95       t)))
96
97 ;;;###autoload
98 (defun fill-flowed (&optional buffer)
99   (save-excursion
100     (set-buffer (or (current-buffer) buffer))
101     (goto-char (point-min))
102     ;; Remove space stuffing.
103     (while (re-search-forward "^ " nil t)
104       (delete-char -1)
105       (forward-line 1))
106     (goto-char (point-min))
107     (while (re-search-forward " $" nil t)
108       (when (save-excursion
109               (beginning-of-line)
110               (looking-at "^\\(>+\\)\\( ?\\)"))
111         (let ((quote (match-string 1))
112               sig)
113           (if (string= quote "")
114               (setq quote nil))
115           (when (and quote (string= (match-string 2) ""))
116             (save-excursion
117               ;; insert SP after quote for pleasant reading of quoted lines
118               (beginning-of-line)
119               (when (> (skip-chars-forward ">") 0)
120                 (insert " "))))
121           ;; XXX slightly buggy handling of "-- "
122           (while (and (save-excursion
123                         (ignore-errors (backward-char 3))
124                         (setq sig (looking-at "-- "))
125                         (looking-at "[^-][^-] "))
126                       (save-excursion
127                         (unless (eobp)
128                           (forward-char 1)
129                           (looking-at (format "^\\(%s\\)\\([^>\n\r]\\)"
130                                               (or quote " ?"))))))
131             (save-excursion
132               (replace-match (if (string= (match-string 2) " ")
133                                  "" "\\2")))
134             (backward-delete-char -1)
135             (end-of-line))
136           (unless sig
137             (condition-case nil
138                 (let ((fill-prefix (when quote (concat quote " ")))
139                       (fill-column (eval fill-flowed-display-column))
140                       filladapt-mode)
141                   (fill-region (point-at-bol)
142                                (min (1+ (point-at-eol))
143                                     (point-max))
144                                'left 'nosqueeze))
145               (error
146                (forward-line 1)
147                nil))))))))
148
149 ;; Test vectors.
150
151 (eval-when-compile
152   (defvar show-trailing-whitespace))
153
154 (defvar fill-flowed-encode-tests
155   '(
156     ;; The syntax of each list element is:
157     ;; (INPUT . EXPECTED-OUTPUT)
158     ("> Thou villainous ill-breeding spongy dizzy-eyed 
159 > reeky elf-skinned pigeon-egg! 
160 >> Thou artless swag-bellied milk-livered 
161 >> dismal-dreaming idle-headed scut!
162 >>> Thou errant folly-fallen spleeny reeling-ripe 
163 >>> unmuzzled ratsbane!
164 >>>> Henceforth, the coding style is to be strictly 
165 >>>> enforced, including the use of only upper case.
166 >>>>> I've noticed a lack of adherence to the coding 
167 >>>>> styles, of late.
168 >>>>>> Any complaints?
169 " . "> Thou villainous ill-breeding spongy dizzy-eyed reeky elf-skinned
170 > pigeon-egg! 
171 >> Thou artless swag-bellied milk-livered dismal-dreaming idle-headed
172 >> scut!
173 >>> Thou errant folly-fallen spleeny reeling-ripe unmuzzled ratsbane!
174 >>>> Henceforth, the coding style is to be strictly enforced,
175 >>>> including the use of only upper case.
176 >>>>> I've noticed a lack of adherence to the coding styles, of late.
177 >>>>>> Any complaints?
178 ")
179 ;    ("
180 ;> foo
181 ;> 
182 ;> 
183 ;> bar
184 ;" . "
185 ;> foo bar
186 ;")
187     ))
188
189 (defun fill-flowed-test ()
190   (interactive "")
191   (switch-to-buffer (get-buffer-create "*Format=Flowed test output*"))
192   (erase-buffer)
193   (setq show-trailing-whitespace t)
194   (dolist (test fill-flowed-encode-tests)
195     (let (start output)
196       (insert "***** BEGIN TEST INPUT *****\n")
197       (insert (car test))
198       (insert "***** END TEST INPUT *****\n\n")
199       (insert "***** BEGIN TEST OUTPUT *****\n")
200       (setq start (point))
201       (insert (car test))
202       (save-restriction
203         (narrow-to-region start (point))
204         (fill-flowed))
205       (setq output (buffer-substring start (point-max)))
206       (insert "***** END TEST OUTPUT *****\n")
207       (unless (string= output (cdr test))
208         (insert "\n***** BEGIN TEST EXPECTED OUTPUT *****\n")
209         (insert (cdr test))
210         (insert "***** END TEST EXPECTED OUTPUT *****\n"))
211       (insert "\n\n")))
212   (goto-char (point-max)))
213
214 (provide 'flow-fill)
215
216 ;;; arch-tag: addc0040-bc53-4f17-b4bc-1eb44eed6f0b
217 ;;; flow-fill.el ends here