(vc-svn-registered): Catch all errors.
[bpt/emacs.git] / lisp / gnus / flow-fill.el
CommitLineData
f4dd4ae8 1;;; flow-fill.el --- interpret RFC2646 "flowed" text
c113de23 2
e84b4b86
TTN
3;; Copyright (C) 2000, 2001, 2002, 2003, 2004,
4;; 2005 Free Software Foundation, Inc.
c113de23
GM
5
6;; Author: Simon Josefsson <jas@pdc.kth.se>
7;; Keywords: mail
8
9;; This file is part of GNU Emacs.
10
11;; GNU Emacs is free software; you can redistribute it and/or modify
12;; it under the terms of the GNU General Public License as published by
13;; the Free Software Foundation; either version 2, or (at your option)
14;; any later version.
15
16;; GNU Emacs is distributed in the hope that it will be useful,
17;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19;; GNU General Public License for more details.
20
21;; You should have received a copy of the GNU General Public License
22;; along with GNU Emacs; see the file COPYING. If not, write to the
3a35cf56
LK
23;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24;; Boston, MA 02110-1301, USA.
c113de23
GM
25
26;;; Commentary:
27
28;; This implement decoding of RFC2646 formatted text, including the
29;; quoted-depth wins rules.
30
31;; Theory of operation: search for lines ending with SPC, save quote
32;; length of line, remove SPC and concatenate line with the following
33;; line if quote length of following line matches current line.
34
35;; When no further concatenations are possible, we've found a
36;; paragraph and we let `fill-region' fill the long line into several
37;; lines with the quote prefix as `fill-prefix'.
38
23f87bed 39;; Todo: implement basic `fill-region' (Emacs and XEmacs
01b5d73b 40;; implementations differ..)
c113de23 41
23f87bed 42;;; History:
c113de23
GM
43
44;; 2000-02-17 posted on ding mailing list
45;; 2000-02-19 use `point-at-{b,e}ol' in XEmacs
46;; 2000-03-11 no compile warnings for point-at-bol stuff
8f688cb0 47;; 2000-03-26 committed to gnus cvs
01b5d73b
DL
48;; 2000-10-23 don't flow "-- " lines, make "quote-depth wins" rule
49;; work when first line is at level 0.
23f87bed
MB
50;; 2002-01-12 probably incomplete encoding support
51;; 2003-12-08 started working on test harness.
c113de23
GM
52
53;;; Code:
54
01b5d73b
DL
55(eval-when-compile (require 'cl))
56
23f87bed
MB
57(defcustom fill-flowed-display-column 'fill-column
58 "Column beyond which format=flowed lines are wrapped, when displayed.
59This can be a Lisp expression or an integer."
bf247b6e 60 :version "22.1"
e79f14a4 61 :group 'mime-display
23f87bed
MB
62 :type '(choice (const :tag "Standard `fill-column'" fill-column)
63 (const :tag "Fit Window" (- (window-width) 5))
64 (sexp)
65 (integer)))
66
67(defcustom fill-flowed-encode-column 66
68 "Column beyond which format=flowed lines are wrapped, in outgoing messages.
69This can be a Lisp expression or an integer.
70RFC 2646 suggests 66 characters for readability."
bf247b6e 71 :version "22.1"
e79f14a4 72 :group 'mime-display
23f87bed
MB
73 :type '(choice (const :tag "Standard fill-column" fill-column)
74 (const :tag "RFC 2646 default (66)" 66)
75 (sexp)
76 (integer)))
77
c113de23
GM
78(eval-and-compile
79 (defalias 'fill-flowed-point-at-bol
80 (if (fboundp 'point-at-bol)
81 'point-at-bol
82 'line-beginning-position))
01b5d73b
DL
83
84 (defalias 'fill-flowed-point-at-eol
c113de23
GM
85 (if (fboundp 'point-at-eol)
86 'point-at-eol
87 'line-end-position)))
88
23f87bed
MB
89;;;###autoload
90(defun fill-flowed-encode (&optional buffer)
91 (with-current-buffer (or buffer (current-buffer))
92 ;; No point in doing this unless hard newlines is used.
93 (when use-hard-newlines
94 (let ((start (point-min)) end)
95 ;; Go through each paragraph, filling it and adding SPC
96 ;; as the last character on each line.
97 (while (setq end (text-property-any start (point-max) 'hard 't))
98 (let ((fill-column (eval fill-flowed-encode-column)))
99 (fill-region start end t 'nosqueeze 'to-eop))
100 (goto-char start)
101 ;; `fill-region' probably distorted end.
102 (setq end (text-property-any start (point-max) 'hard 't))
103 (while (and (< (point) end)
104 (re-search-forward "$" (1- end) t))
105 (insert " ")
106 (setq end (1+ end))
107 (forward-char))
108 (goto-char (setq start (1+ end)))))
109 t)))
110
111;;;###autoload
c113de23
GM
112(defun fill-flowed (&optional buffer)
113 (save-excursion
114 (set-buffer (or (current-buffer) buffer))
115 (goto-char (point-min))
116 (while (re-search-forward " $" nil t)
117 (when (save-excursion
118 (beginning-of-line)
119 (looking-at "^\\(>*\\)\\( ?\\)"))
23f87bed
MB
120 (let ((quote (match-string 1))
121 sig)
c113de23
GM
122 (if (string= quote "")
123 (setq quote nil))
124 (when (and quote (string= (match-string 2) ""))
125 (save-excursion
126 ;; insert SP after quote for pleasant reading of quoted lines
127 (beginning-of-line)
128 (when (> (skip-chars-forward ">") 0)
129 (insert " "))))
23f87bed 130 ;; XXX slightly buggy handling of "-- "
c113de23 131 (while (and (save-excursion
01b5d73b
DL
132 (ignore-errors (backward-char 3))
133 (setq sig (looking-at "-- "))
c113de23
GM
134 (looking-at "[^-][^-] "))
135 (save-excursion
136 (unless (eobp)
137 (forward-char 1)
23f87bed
MB
138 (looking-at (format "^\\(%s\\)\\([^>\n\r]\\)"
139 (or quote " ?"))))))
c113de23
GM
140 (save-excursion
141 (replace-match (if (string= (match-string 2) " ")
142 "" "\\2")))
143 (backward-delete-char -1)
144 (end-of-line))
01b5d73b 145 (unless sig
23f87bed
MB
146 (condition-case nil
147 (let ((fill-prefix (when quote (concat quote " ")))
148 (fill-column (eval fill-flowed-display-column))
149 filladapt-mode)
150 (fill-region (fill-flowed-point-at-bol)
151 (min (1+ (fill-flowed-point-at-eol))
152 (point-max))
153 'left 'nosqueeze))
154 (error
155 (forward-line 1)
156 nil))))))))
157
158;; Test vectors.
159
160(eval-when-compile
161 (defvar show-trailing-whitespace))
162
163(defvar fill-flowed-encode-tests
3b059693 164 `(
23f87bed
MB
165 ;; The syntax of each list element is:
166 ;; (INPUT . EXPECTED-OUTPUT)
3b059693
MB
167 (,(concat
168 "> Thou villainous ill-breeding spongy dizzy-eyed \n"
169 "> reeky elf-skinned pigeon-egg! \n"
170 ">> Thou artless swag-bellied milk-livered \n"
171 ">> dismal-dreaming idle-headed scut!\n"
172 ">>> Thou errant folly-fallen spleeny reeling-ripe \n"
173 ">>> unmuzzled ratsbane!\n"
174 ">>>> Henceforth, the coding style is to be strictly \n"
175 ">>>> enforced, including the use of only upper case.\n"
176 ">>>>> I've noticed a lack of adherence to the coding \n"
177 ">>>>> styles, of late.\n"
178 ">>>>>> Any complaints?")
179 .
180 ,(concat
181 "> Thou villainous ill-breeding spongy dizzy-eyed reeky elf-skinned\n"
182 "> pigeon-egg! \n"
183 ">> Thou artless swag-bellied milk-livered dismal-dreaming idle-headed\n"
184 ">> scut!\n"
185 ">>> Thou errant folly-fallen spleeny reeling-ripe unmuzzled ratsbane!\n"
186 ">>>> Henceforth, the coding style is to be strictly enforced,\n"
187 ">>>> including the use of only upper case.\n"
188 ">>>>> I've noticed a lack of adherence to the coding styles, of late.\n"
189 ">>>>>> Any complaints?\n"
190 ))
191 ;; (,(concat
192 ;; "\n"
193 ;; "> foo\n"
194 ;; "> \n"
195 ;; "> \n"
196 ;; "> bar\n")
197 ;; .
198 ;; ,(concat
199 ;; "\n"
200 ;; "> foo bar\n"))
23f87bed
MB
201 ))
202
203(defun fill-flowed-test ()
204 (interactive "")
205 (switch-to-buffer (get-buffer-create "*Format=Flowed test output*"))
206 (erase-buffer)
207 (setq show-trailing-whitespace t)
208 (dolist (test fill-flowed-encode-tests)
209 (let (start output)
210 (insert "***** BEGIN TEST INPUT *****\n")
211 (insert (car test))
212 (insert "***** END TEST INPUT *****\n\n")
213 (insert "***** BEGIN TEST OUTPUT *****\n")
214 (setq start (point))
215 (insert (car test))
216 (save-restriction
217 (narrow-to-region start (point))
218 (fill-flowed))
219 (setq output (buffer-substring start (point-max)))
220 (insert "***** END TEST OUTPUT *****\n")
221 (unless (string= output (cdr test))
222 (insert "\n***** BEGIN TEST EXPECTED OUTPUT *****\n")
223 (insert (cdr test))
224 (insert "***** END TEST EXPECTED OUTPUT *****\n"))
225 (insert "\n\n")))
226 (goto-char (point-max)))
c113de23
GM
227
228(provide 'flow-fill)
229
ab5796a9 230;;; arch-tag: addc0040-bc53-4f17-b4bc-1eb44eed6f0b
c113de23 231;;; flow-fill.el ends here