1 ;;; flow-fill.el --- interpret RFC2646 "flowed" text
3 ;; Copyright (C) 2000, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006 Free Software Foundation, Inc.
6 ;; Author: Simon Josefsson <jas@pdc.kth.se>
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
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.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
28 ;; This implement decoding of RFC2646 formatted text, including the
29 ;; quoted-depth wins rules.
31 ;; Theory of operation: search for lines ending with SPC, save quote
32 ;; length of line, remove SPC and concatenate line with the following
33 ;; line if quote length of following line matches current line.
35 ;; When no further concatenations are possible, we've found a
36 ;; paragraph and we let `fill-region' fill the long line into several
37 ;; lines with the quote prefix as `fill-prefix'.
39 ;; Todo: implement basic `fill-region' (Emacs and XEmacs
40 ;; implementations differ..)
44 ;; 2000-02-17 posted on ding mailing list
45 ;; 2000-02-19 use `point-at-{b,e}ol' in XEmacs
46 ;; 2000-03-11 no compile warnings for point-at-bol stuff
47 ;; 2000-03-26 committed to gnus cvs
48 ;; 2000-10-23 don't flow "-- " lines, make "quote-depth wins" rule
49 ;; work when first line is at level 0.
50 ;; 2002-01-12 probably incomplete encoding support
51 ;; 2003-12-08 started working on test harness.
55 (eval-when-compile (require 'cl
))
57 (defcustom fill-flowed-display-column
'fill-column
58 "Column beyond which format=flowed lines are wrapped, when displayed.
59 This can be a Lisp expression or an integer."
62 :type
'(choice (const :tag
"Standard `fill-column'" fill-column
)
63 (const :tag
"Fit Window" (- (window-width) 5))
67 (defcustom fill-flowed-encode-column
66
68 "Column beyond which format=flowed lines are wrapped, in outgoing messages.
69 This can be a Lisp expression or an integer.
70 RFC 2646 suggests 66 characters for readability."
73 :type
'(choice (const :tag
"Standard fill-column" fill-column
)
74 (const :tag
"RFC 2646 default (66)" 66)
79 (defalias 'fill-flowed-point-at-bol
80 (if (fboundp 'point-at-bol
)
82 'line-beginning-position
))
84 (defalias 'fill-flowed-point-at-eol
85 (if (fboundp 'point-at-eol
)
90 (defun fill-flowed-encode (&optional buffer
)
91 (with-current-buffer (or buffer
(current-buffer))
92 ;; No point in doing this unless hard newlines is used.
93 (when use-hard-newlines
94 (let ((start (point-min)) end
)
95 ;; Go through each paragraph, filling it and adding SPC
96 ;; as the last character on each line.
97 (while (setq end
(text-property-any start
(point-max) 'hard
't
))
98 (let ((fill-column (eval fill-flowed-encode-column
)))
99 (fill-region start end t
'nosqueeze
'to-eop
))
101 ;; `fill-region' probably distorted end.
102 (setq end
(text-property-any start
(point-max) 'hard
't
))
103 (while (and (< (point) end
)
104 (re-search-forward "$" (1- end
) t
))
108 (goto-char (setq start
(1+ end
)))))
112 (defun fill-flowed (&optional buffer
)
114 (set-buffer (or (current-buffer) buffer
))
115 (goto-char (point-min))
116 ;; Remove space stuffing.
117 (while (re-search-forward "^\\( \\|>+ $\\)" nil t
)
120 (goto-char (point-min))
121 (while (re-search-forward " $" nil t
)
122 (when (save-excursion
124 (looking-at "^\\(>*\\)\\( ?\\)"))
125 (let ((quote (match-string 1))
127 (if (string= quote
"")
129 (when (and quote
(string= (match-string 2) ""))
131 ;; insert SP after quote for pleasant reading of quoted lines
133 (when (> (skip-chars-forward ">") 0)
135 ;; XXX slightly buggy handling of "-- "
136 (while (and (save-excursion
137 (ignore-errors (backward-char 3))
138 (setq sig
(looking-at "-- "))
139 (looking-at "[^-][^-] "))
143 (looking-at (format "^\\(%s\\)\\([^>\n\r]\\)"
146 (replace-match (if (string= (match-string 2) " ")
148 (backward-delete-char -
1)
152 (let ((fill-prefix (when quote
(concat quote
" ")))
153 (fill-column (eval fill-flowed-display-column
))
156 (fill-region (fill-flowed-point-at-bol)
157 (min (1+ (fill-flowed-point-at-eol))
167 (defvar show-trailing-whitespace
))
169 (defvar fill-flowed-encode-tests
171 ;; The syntax of each list element is:
172 ;; (INPUT . EXPECTED-OUTPUT)
174 "> Thou villainous ill-breeding spongy dizzy-eyed \n"
175 "> reeky elf-skinned pigeon-egg! \n"
176 ">> Thou artless swag-bellied milk-livered \n"
177 ">> dismal-dreaming idle-headed scut!\n"
178 ">>> Thou errant folly-fallen spleeny reeling-ripe \n"
179 ">>> unmuzzled ratsbane!\n"
180 ">>>> Henceforth, the coding style is to be strictly \n"
181 ">>>> enforced, including the use of only upper case.\n"
182 ">>>>> I've noticed a lack of adherence to the coding \n"
183 ">>>>> styles, of late.\n"
184 ">>>>>> Any complaints?")
187 "> Thou villainous ill-breeding spongy dizzy-eyed reeky elf-skinned\n"
189 ">> Thou artless swag-bellied milk-livered dismal-dreaming idle-headed\n"
191 ">>> Thou errant folly-fallen spleeny reeling-ripe unmuzzled ratsbane!\n"
192 ">>>> Henceforth, the coding style is to be strictly enforced,\n"
193 ">>>> including the use of only upper case.\n"
194 ">>>>> I've noticed a lack of adherence to the coding styles, of late.\n"
195 ">>>>>> Any complaints?\n"
209 (defun fill-flowed-test ()
211 (switch-to-buffer (get-buffer-create "*Format=Flowed test output*"))
213 (setq show-trailing-whitespace t
)
214 (dolist (test fill-flowed-encode-tests
)
216 (insert "***** BEGIN TEST INPUT *****\n")
218 (insert "***** END TEST INPUT *****\n\n")
219 (insert "***** BEGIN TEST OUTPUT *****\n")
223 (narrow-to-region start
(point))
225 (setq output
(buffer-substring start
(point-max)))
226 (insert "***** END TEST OUTPUT *****\n")
227 (unless (string= output
(cdr test
))
228 (insert "\n***** BEGIN TEST EXPECTED OUTPUT *****\n")
230 (insert "***** END TEST EXPECTED OUTPUT *****\n"))
232 (goto-char (point-max)))
236 ;;; arch-tag: addc0040-bc53-4f17-b4bc-1eb44eed6f0b
237 ;;; flow-fill.el ends here