1 ;;; flow-fill.el --- interpret RFC2646 "flowed" text -*- lexical-binding:t -*-
3 ;; Copyright (C) 2000-2018 Free Software Foundation, Inc.
5 ;; Author: Simon Josefsson <jas@pdc.kth.se>
8 ;; This file is part of GNU Emacs.
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 3 of the License, or
13 ;; (at your option) any later version.
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.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
25 ;; This implement decoding of RFC2646 formatted text, including the
26 ;; quoted-depth wins rules.
28 ;; Theory of operation: search for lines ending with SPC, save quote
29 ;; length of line, remove SPC and concatenate line with the following
30 ;; line if quote length of following line matches current line.
32 ;; When no further concatenations are possible, we've found a
33 ;; paragraph and we let `fill-region' fill the long line into several
34 ;; lines with the quote prefix as `fill-prefix'.
36 ;; Todo: implement basic `fill-region' (Emacs and XEmacs
37 ;; implementations differ..)
41 ;; 2000-02-17 posted on ding mailing list
42 ;; 2000-02-19 use `point-at-{b,e}ol' in XEmacs
43 ;; 2000-03-11 no compile warnings for point-at-bol stuff
44 ;; 2000-03-26 committed to gnus cvs
45 ;; 2000-10-23 don't flow "-- " lines, make "quote-depth wins" rule
46 ;; work when first line is at level 0.
47 ;; 2002-01-12 probably incomplete encoding support
48 ;; 2003-12-08 started working on test harness.
53 (defcustom fill-flowed-display-column
'fill-column
54 "Column beyond which format=flowed lines are wrapped, when displayed.
55 This can be a Lisp expression or an integer."
58 :type
'(choice (const :tag
"Standard `fill-column'" fill-column
)
59 (const :tag
"Fit Window" (- (window-width) 5))
63 (defcustom fill-flowed-encode-column
66
64 "Column beyond which format=flowed lines are wrapped, in outgoing messages.
65 This can be a Lisp expression or an integer.
66 RFC 2646 suggests 66 characters for readability."
69 :type
'(choice (const :tag
"Standard fill-column" fill-column
)
70 (const :tag
"RFC 2646 default (66)" 66)
75 (defun fill-flowed-encode (&optional buffer
)
76 (with-current-buffer (or buffer
(current-buffer))
77 ;; No point in doing this unless hard newlines is used.
78 (when use-hard-newlines
79 (let ((start (point-min)) end
)
80 ;; Go through each paragraph, filling it and adding SPC
81 ;; as the last character on each line.
82 (while (setq end
(text-property-any start
(point-max) 'hard
't
))
84 (narrow-to-region start end
)
85 (let ((fill-column (eval fill-flowed-encode-column
)))
86 (fill-flowed-fill-buffer))
87 (goto-char (point-min))
88 (while (re-search-forward "\n" nil t
)
89 (replace-match " \n" t t
))
90 (goto-char (setq start
(1+ (point-max)))))))
93 (defun fill-flowed-fill-buffer ()
97 (goto-char (point-min))
99 (setq prefix
(and (looking-at "[> ]+")
101 (if (equal prefix prev-prefix
)
104 (narrow-to-region start
(point))
105 (let ((fill-prefix prev-prefix
))
106 (fill-region (point-min) (point-max) t
'nosqueeze
'to-eop
))
107 (goto-char (point-max)))
108 (setq prev-prefix prefix
111 (narrow-to-region start
(point))
112 (let ((fill-prefix prev-prefix
))
113 (fill-region (point-min) (point-max) t
'nosqueeze
'to-eop
)))))
116 (defun fill-flowed (&optional buffer delete-space
)
117 (with-current-buffer (or (current-buffer) buffer
)
118 (goto-char (point-min))
119 ;; Remove space stuffing.
120 (while (re-search-forward "^\\( \\|>+ $\\)" nil t
)
123 (goto-char (point-min))
124 (while (re-search-forward " $" nil t
)
125 (when (save-excursion
127 (looking-at "^\\(>*\\)\\( ?\\)"))
128 (let ((quote (match-string 1))
130 (if (string= quote
"")
132 (when (and quote
(string= (match-string 2) ""))
134 ;; insert SP after quote for pleasant reading of quoted lines
136 (when (> (skip-chars-forward ">") 0)
138 ;; XXX slightly buggy handling of "-- "
139 (while (and (save-excursion
140 (ignore-errors (backward-char 3))
141 (setq sig
(looking-at "-- "))
142 (looking-at "[^-][^-] "))
146 (looking-at (format "^\\(%s\\)\\([^>\n\r]\\)"
149 (replace-match (if (string= (match-string 2) " ")
151 (backward-delete-char -
1)
157 (let ((fill-prefix (when quote
(concat quote
" ")))
158 (fill-column (eval fill-flowed-display-column
))
160 (fill-region (point-at-bol)
161 (min (1+ (point-at-eol))
170 (defvar show-trailing-whitespace
)
172 (defvar fill-flowed-encode-tests
174 ;; The syntax of each list element is:
175 ;; (INPUT . EXPECTED-OUTPUT)
177 "> Thou villainous ill-breeding spongy dizzy-eyed \n"
178 "> reeky elf-skinned pigeon-egg! \n"
179 ">> Thou artless swag-bellied milk-livered \n"
180 ">> dismal-dreaming idle-headed scut!\n"
181 ">>> Thou errant folly-fallen spleeny reeling-ripe \n"
182 ">>> unmuzzled ratsbane!\n"
183 ">>>> Henceforth, the coding style is to be strictly \n"
184 ">>>> enforced, including the use of only upper case.\n"
185 ">>>>> I've noticed a lack of adherence to the coding \n"
186 ">>>>> styles, of late.\n"
187 ">>>>>> Any complaints?")
190 "> Thou villainous ill-breeding spongy dizzy-eyed reeky elf-skinned\n"
192 ">> Thou artless swag-bellied milk-livered dismal-dreaming idle-headed\n"
194 ">>> Thou errant folly-fallen spleeny reeling-ripe unmuzzled ratsbane!\n"
195 ">>>> Henceforth, the coding style is to be strictly enforced,\n"
196 ">>>> including the use of only upper case.\n"
197 ">>>>> I've noticed a lack of adherence to the coding styles, of late.\n"
198 ">>>>>> Any complaints?\n"
212 (defun fill-flowed-test ()
214 (switch-to-buffer (get-buffer-create "*Format=Flowed test output*"))
216 (setq show-trailing-whitespace t
)
217 (dolist (test fill-flowed-encode-tests
)
219 (insert "***** BEGIN TEST INPUT *****\n")
221 (insert "***** END TEST INPUT *****\n\n")
222 (insert "***** BEGIN TEST OUTPUT *****\n")
226 (narrow-to-region start
(point))
228 (setq output
(buffer-substring start
(point-max)))
229 (insert "***** END TEST OUTPUT *****\n")
230 (unless (string= output
(cdr test
))
231 (insert "\n***** BEGIN TEST EXPECTED OUTPUT *****\n")
233 (insert "***** END TEST EXPECTED OUTPUT *****\n"))
235 (goto-char (point-max)))
239 ;;; flow-fill.el ends here