Merge branch 'emacs-24' of git.sv.gnu.org:/srv/git/emacs into emacs-24
[emacs.git] / lisp / gnus / flow-fill.el
blobe3a26e7de5273658377c8ab74cf41b8d8319a8e1
1 ;;; flow-fill.el --- interpret RFC2646 "flowed" text
3 ;; Copyright (C) 2000-2014 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 filladapt-mode
161 adaptive-fill-mode)
162 (fill-region (point-at-bol)
163 (min (1+ (point-at-eol))
164 (point-max))
165 'left 'nosqueeze))
166 (error
167 (forward-line 1)
168 nil))))))))
170 ;; Test vectors.
172 (defvar show-trailing-whitespace)
174 (defvar fill-flowed-encode-tests
176 ;; The syntax of each list element is:
177 ;; (INPUT . EXPECTED-OUTPUT)
178 (,(concat
179 "> Thou villainous ill-breeding spongy dizzy-eyed \n"
180 "> reeky elf-skinned pigeon-egg! \n"
181 ">> Thou artless swag-bellied milk-livered \n"
182 ">> dismal-dreaming idle-headed scut!\n"
183 ">>> Thou errant folly-fallen spleeny reeling-ripe \n"
184 ">>> unmuzzled ratsbane!\n"
185 ">>>> Henceforth, the coding style is to be strictly \n"
186 ">>>> enforced, including the use of only upper case.\n"
187 ">>>>> I've noticed a lack of adherence to the coding \n"
188 ">>>>> styles, of late.\n"
189 ">>>>>> Any complaints?")
191 ,(concat
192 "> Thou villainous ill-breeding spongy dizzy-eyed reeky elf-skinned\n"
193 "> pigeon-egg! \n"
194 ">> Thou artless swag-bellied milk-livered dismal-dreaming idle-headed\n"
195 ">> scut!\n"
196 ">>> Thou errant folly-fallen spleeny reeling-ripe unmuzzled ratsbane!\n"
197 ">>>> Henceforth, the coding style is to be strictly enforced,\n"
198 ">>>> including the use of only upper case.\n"
199 ">>>>> I've noticed a lack of adherence to the coding styles, of late.\n"
200 ">>>>>> Any complaints?\n"
202 ;; (,(concat
203 ;; "\n"
204 ;; "> foo\n"
205 ;; "> \n"
206 ;; "> \n"
207 ;; "> bar\n")
208 ;; .
209 ;; ,(concat
210 ;; "\n"
211 ;; "> foo bar\n"))
214 (defun fill-flowed-test ()
215 (interactive "")
216 (switch-to-buffer (get-buffer-create "*Format=Flowed test output*"))
217 (erase-buffer)
218 (setq show-trailing-whitespace t)
219 (dolist (test fill-flowed-encode-tests)
220 (let (start output)
221 (insert "***** BEGIN TEST INPUT *****\n")
222 (insert (car test))
223 (insert "***** END TEST INPUT *****\n\n")
224 (insert "***** BEGIN TEST OUTPUT *****\n")
225 (setq start (point))
226 (insert (car test))
227 (save-restriction
228 (narrow-to-region start (point))
229 (fill-flowed))
230 (setq output (buffer-substring start (point-max)))
231 (insert "***** END TEST OUTPUT *****\n")
232 (unless (string= output (cdr test))
233 (insert "\n***** BEGIN TEST EXPECTED OUTPUT *****\n")
234 (insert (cdr test))
235 (insert "***** END TEST EXPECTED OUTPUT *****\n"))
236 (insert "\n\n")))
237 (goto-char (point-max)))
239 (provide 'flow-fill)
241 ;;; flow-fill.el ends here