(ls-lisp-insert-directory): Make -B work
[emacs.git] / lisp / gnus / flow-fill.el
blobd28814224757bfb0236d5880ff7fdc332605771a
1 ;;; flow-fill.el --- interpret RFC2646 "flowed" text
3 ;; Copyright (C) 2000-2016 Free Software Foundation, Inc.
5 ;; Author: Simon Josefsson <jas@pdc.kth.se>
6 ;; Keywords: mail
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 <http://www.gnu.org/licenses/>.
23 ;;; Commentary:
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..)
39 ;;; History:
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.
50 ;;; Code:
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."
57 :version "22.1"
58 :group 'mime-display
59 :type '(choice (const :tag "Standard `fill-column'" fill-column)
60 (const :tag "Fit Window" (- (window-width) 5))
61 (sexp)
62 (integer)))
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."
68 :version "22.1"
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)))
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 (save-restriction
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)))))))
92 t)))
94 (defun fill-flowed-fill-buffer ()
95 (let ((prefix nil)
96 (prev-prefix nil)
97 (start (point-min)))
98 (goto-char (point-min))
99 (while (not (eobp))
100 (setq prefix (and (looking-at "[> ]+")
101 (match-string 0)))
102 (if (equal prefix prev-prefix)
103 (forward-line 1)
104 (save-restriction
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
110 start (point))))
111 (save-restriction
112 (narrow-to-region start (point))
113 (let ((fill-prefix prev-prefix))
114 (fill-region (point-min) (point-max) t 'nosqueeze 'to-eop)))))
116 ;;;###autoload
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)
122 (delete-char -1)
123 (forward-line 1))
124 (goto-char (point-min))
125 (while (re-search-forward " $" nil t)
126 (when (save-excursion
127 (beginning-of-line)
128 (looking-at "^\\(>*\\)\\( ?\\)"))
129 (let ((quote (match-string 1))
130 sig)
131 (if (string= quote "")
132 (setq quote nil))
133 (when (and quote (string= (match-string 2) ""))
134 (save-excursion
135 ;; insert SP after quote for pleasant reading of quoted lines
136 (beginning-of-line)
137 (when (> (skip-chars-forward ">") 0)
138 (insert " "))))
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 "[^-][^-] "))
144 (save-excursion
145 (unless (eobp)
146 (forward-char 1)
147 (looking-at (format "^\\(%s\\)\\([^>\n\r]\\)"
148 (or quote " ?"))))))
149 (save-excursion
150 (replace-match (if (string= (match-string 2) " ")
151 "" "\\2")))
152 (backward-delete-char -1)
153 (when delete-space
154 (delete-char -1))
155 (end-of-line))
156 (unless sig
157 (condition-case nil
158 (let ((fill-prefix (when quote (concat quote " ")))
159 (fill-column (eval fill-flowed-display-column))
160 adaptive-fill-mode)
161 (fill-region (point-at-bol)
162 (min (1+ (point-at-eol))
163 (point-max))
164 'left 'nosqueeze))
165 (error
166 (forward-line 1)
167 nil))))))))
169 ;; Test vectors.
171 (defvar show-trailing-whitespace)
173 (defvar fill-flowed-encode-tests
175 ;; The syntax of each list element is:
176 ;; (INPUT . EXPECTED-OUTPUT)
177 (,(concat
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?")
190 ,(concat
191 "> Thou villainous ill-breeding spongy dizzy-eyed reeky elf-skinned\n"
192 "> pigeon-egg! \n"
193 ">> Thou artless swag-bellied milk-livered dismal-dreaming idle-headed\n"
194 ">> scut!\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"
201 ;; (,(concat
202 ;; "\n"
203 ;; "> foo\n"
204 ;; "> \n"
205 ;; "> \n"
206 ;; "> bar\n")
207 ;; .
208 ;; ,(concat
209 ;; "\n"
210 ;; "> foo bar\n"))
213 (defun fill-flowed-test ()
214 (interactive "")
215 (switch-to-buffer (get-buffer-create "*Format=Flowed test output*"))
216 (erase-buffer)
217 (setq show-trailing-whitespace t)
218 (dolist (test fill-flowed-encode-tests)
219 (let (start output)
220 (insert "***** BEGIN TEST INPUT *****\n")
221 (insert (car test))
222 (insert "***** END TEST INPUT *****\n\n")
223 (insert "***** BEGIN TEST OUTPUT *****\n")
224 (setq start (point))
225 (insert (car test))
226 (save-restriction
227 (narrow-to-region start (point))
228 (fill-flowed))
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")
233 (insert (cdr test))
234 (insert "***** END TEST EXPECTED OUTPUT *****\n"))
235 (insert "\n\n")))
236 (goto-char (point-max)))
238 (provide 'flow-fill)
240 ;;; flow-fill.el ends here