1 ;;; flow-fill.el --- interpret RFC2646 "flowed" text
3 ;; Copyright (C) 2000-2017 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.
52 (eval-when-compile (require 'cl
))
54 (defcustom fill-flowed-display-column
'fill-column
55 "Column beyond which format=flowed lines are wrapped, when displayed.
56 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))
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."
70 :type
'(choice (const :tag
"Standard fill-column" fill-column
)
71 (const :tag
"RFC 2646 default (66)" 66)
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
))
85 (narrow-to-region start end
)
86 (let ((fill-column (eval fill-flowed-encode-column
)))
87 (fill-flowed-fill-buffer))
88 (goto-char (point-min))
89 (while (re-search-forward "\n" nil t
)
90 (replace-match " \n" t t
))
91 (goto-char (setq start
(1+ (point-max)))))))
94 (defun fill-flowed-fill-buffer ()
98 (goto-char (point-min))
100 (setq prefix
(and (looking-at "[> ]+")
102 (if (equal prefix prev-prefix
)
105 (narrow-to-region start
(point))
106 (let ((fill-prefix prev-prefix
))
107 (fill-region (point-min) (point-max) t
'nosqueeze
'to-eop
))
108 (goto-char (point-max)))
109 (setq prev-prefix prefix
112 (narrow-to-region start
(point))
113 (let ((fill-prefix prev-prefix
))
114 (fill-region (point-min) (point-max) t
'nosqueeze
'to-eop
)))))
117 (defun fill-flowed (&optional buffer delete-space
)
118 (with-current-buffer (or (current-buffer) buffer
)
119 (goto-char (point-min))
120 ;; Remove space stuffing.
121 (while (re-search-forward "^\\( \\|>+ $\\)" nil t
)
124 (goto-char (point-min))
125 (while (re-search-forward " $" nil t
)
126 (when (save-excursion
128 (looking-at "^\\(>*\\)\\( ?\\)"))
129 (let ((quote (match-string 1))
131 (if (string= quote
"")
133 (when (and quote
(string= (match-string 2) ""))
135 ;; insert SP after quote for pleasant reading of quoted lines
137 (when (> (skip-chars-forward ">") 0)
139 ;; XXX slightly buggy handling of "-- "
140 (while (and (save-excursion
141 (ignore-errors (backward-char 3))
142 (setq sig
(looking-at "-- "))
143 (looking-at "[^-][^-] "))
147 (looking-at (format "^\\(%s\\)\\([^>\n\r]\\)"
150 (replace-match (if (string= (match-string 2) " ")
152 (backward-delete-char -
1)
158 (let ((fill-prefix (when quote
(concat quote
" ")))
159 (fill-column (eval fill-flowed-display-column
))
161 (fill-region (point-at-bol)
162 (min (1+ (point-at-eol))
171 (defvar show-trailing-whitespace
)
173 (defvar fill-flowed-encode-tests
175 ;; The syntax of each list element is:
176 ;; (INPUT . EXPECTED-OUTPUT)
178 "> Thou villainous ill-breeding spongy dizzy-eyed \n"
179 "> reeky elf-skinned pigeon-egg! \n"
180 ">> Thou artless swag-bellied milk-livered \n"
181 ">> dismal-dreaming idle-headed scut!\n"
182 ">>> Thou errant folly-fallen spleeny reeling-ripe \n"
183 ">>> unmuzzled ratsbane!\n"
184 ">>>> Henceforth, the coding style is to be strictly \n"
185 ">>>> enforced, including the use of only upper case.\n"
186 ">>>>> I've noticed a lack of adherence to the coding \n"
187 ">>>>> styles, of late.\n"
188 ">>>>>> Any complaints?")
191 "> Thou villainous ill-breeding spongy dizzy-eyed reeky elf-skinned\n"
193 ">> Thou artless swag-bellied milk-livered dismal-dreaming idle-headed\n"
195 ">>> Thou errant folly-fallen spleeny reeling-ripe unmuzzled ratsbane!\n"
196 ">>>> Henceforth, the coding style is to be strictly enforced,\n"
197 ">>>> including the use of only upper case.\n"
198 ">>>>> I've noticed a lack of adherence to the coding styles, of late.\n"
199 ">>>>>> Any complaints?\n"
213 (defun fill-flowed-test ()
215 (switch-to-buffer (get-buffer-create "*Format=Flowed test output*"))
217 (setq show-trailing-whitespace t
)
218 (dolist (test fill-flowed-encode-tests
)
220 (insert "***** BEGIN TEST INPUT *****\n")
222 (insert "***** END TEST INPUT *****\n\n")
223 (insert "***** BEGIN TEST OUTPUT *****\n")
227 (narrow-to-region start
(point))
229 (setq output
(buffer-substring start
(point-max)))
230 (insert "***** END TEST OUTPUT *****\n")
231 (unless (string= output
(cdr test
))
232 (insert "\n***** BEGIN TEST EXPECTED OUTPUT *****\n")
234 (insert "***** END TEST EXPECTED OUTPUT *****\n"))
236 (goto-char (point-max)))
240 ;;; flow-fill.el ends here