Remove unnecessary explicit subword-mode use from isearch
[emacs.git] / lisp / mail / flow-fill.el
blobdb2a30ad15eaaa3962d319931f934ca6f709b354
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>
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 <https://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:
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."
56 :version "22.1"
57 :group 'mime-display
58 :type '(choice (const :tag "Standard `fill-column'" fill-column)
59 (const :tag "Fit Window" (- (window-width) 5))
60 (sexp)
61 (integer)))
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."
67 :version "22.1"
68 :group 'mime-display
69 :type '(choice (const :tag "Standard fill-column" fill-column)
70 (const :tag "RFC 2646 default (66)" 66)
71 (sexp)
72 (integer)))
74 ;;;###autoload
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))
83 (save-restriction
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)))))))
91 t)))
93 (defun fill-flowed-fill-buffer ()
94 (let ((prefix nil)
95 (prev-prefix nil)
96 (start (point-min)))
97 (goto-char (point-min))
98 (while (not (eobp))
99 (setq prefix (and (looking-at "[> ]+")
100 (match-string 0)))
101 (if (equal prefix prev-prefix)
102 (forward-line 1)
103 (save-restriction
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
109 start (point))))
110 (save-restriction
111 (narrow-to-region start (point))
112 (let ((fill-prefix prev-prefix))
113 (fill-region (point-min) (point-max) t 'nosqueeze 'to-eop)))))
115 ;;;###autoload
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)
121 (delete-char -1)
122 (forward-line 1))
123 (goto-char (point-min))
124 (while (re-search-forward " $" nil t)
125 (when (save-excursion
126 (beginning-of-line)
127 (looking-at "^\\(>*\\)\\( ?\\)"))
128 (let ((quote (match-string 1))
129 sig)
130 (if (string= quote "")
131 (setq quote nil))
132 (when (and quote (string= (match-string 2) ""))
133 (save-excursion
134 ;; insert SP after quote for pleasant reading of quoted lines
135 (beginning-of-line)
136 (when (> (skip-chars-forward ">") 0)
137 (insert " "))))
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 "[^-][^-] "))
143 (save-excursion
144 (unless (eobp)
145 (forward-char 1)
146 (looking-at (format "^\\(%s\\)\\([^>\n\r]\\)"
147 (or quote " ?"))))))
148 (save-excursion
149 (replace-match (if (string= (match-string 2) " ")
150 "" "\\2")))
151 (backward-delete-char -1)
152 (when delete-space
153 (delete-char -1))
154 (end-of-line))
155 (unless sig
156 (condition-case nil
157 (let ((fill-prefix (when quote (concat quote " ")))
158 (fill-column (eval fill-flowed-display-column))
159 adaptive-fill-mode)
160 (fill-region (point-at-bol)
161 (min (1+ (point-at-eol))
162 (point-max))
163 'left 'nosqueeze))
164 (error
165 (forward-line 1)
166 nil))))))))
168 ;; Test vectors.
170 (defvar show-trailing-whitespace)
172 (defvar fill-flowed-encode-tests
174 ;; The syntax of each list element is:
175 ;; (INPUT . EXPECTED-OUTPUT)
176 (,(concat
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?")
189 ,(concat
190 "> Thou villainous ill-breeding spongy dizzy-eyed reeky elf-skinned\n"
191 "> pigeon-egg! \n"
192 ">> Thou artless swag-bellied milk-livered dismal-dreaming idle-headed\n"
193 ">> scut!\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"
200 ;; (,(concat
201 ;; "\n"
202 ;; "> foo\n"
203 ;; "> \n"
204 ;; "> \n"
205 ;; "> bar\n")
206 ;; .
207 ;; ,(concat
208 ;; "\n"
209 ;; "> foo bar\n"))
212 (defun fill-flowed-test ()
213 (interactive "")
214 (switch-to-buffer (get-buffer-create "*Format=Flowed test output*"))
215 (erase-buffer)
216 (setq show-trailing-whitespace t)
217 (dolist (test fill-flowed-encode-tests)
218 (let (start output)
219 (insert "***** BEGIN TEST INPUT *****\n")
220 (insert (car test))
221 (insert "***** END TEST INPUT *****\n\n")
222 (insert "***** BEGIN TEST OUTPUT *****\n")
223 (setq start (point))
224 (insert (car test))
225 (save-restriction
226 (narrow-to-region start (point))
227 (fill-flowed))
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")
232 (insert (cdr test))
233 (insert "***** END TEST EXPECTED OUTPUT *****\n"))
234 (insert "\n\n")))
235 (goto-char (point-max)))
237 (provide 'flow-fill)
239 ;;; flow-fill.el ends here