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