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