(gnus-dribble-read-file): Quote file-precious-flag.
[gnus] / lisp / flow-fill.el
1 ;;; flow-fill.el --- interpret RFC2646 "flowed" text
2
3 ;; Copyright (C) 2000, 2001, 2002, 2003, 2004,
4 ;;   2005 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 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
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
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
39 ;; Todo: implement basic `fill-region' (Emacs and XEmacs
40 ;;       implementations differ..)
41
42 ;;; History:
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
47 ;; 2000-03-26  committed to gnus cvs
48 ;; 2000-10-23  don't flow "-- " lines, make "quote-depth wins" rule
49 ;;             work when first line is at level 0.
50 ;; 2002-01-12  probably incomplete encoding support
51 ;; 2003-12-08  started working on test harness.
52
53 ;;; Code:
54
55 (eval-when-compile (require 'cl))
56
57 (defcustom fill-flowed-display-column 'fill-column
58   "Column beyond which format=flowed lines are wrapped, when displayed.
59 This can be a Lisp expression or an integer."
60   :version "22.1"
61   :group 'mime-display
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.
69 This can be a Lisp expression or an integer.
70 RFC 2646 suggests 66 characters for readability."
71   :version "22.1"
72   :group 'mime-display
73   :type '(choice (const :tag "Standard fill-column" fill-column)
74                  (const :tag "RFC 2646 default (66)" 66)
75                  (sexp)
76                  (integer)))
77
78 ;;;###autoload
79 (defun fill-flowed-encode (&optional buffer)
80   (with-current-buffer (or buffer (current-buffer))
81     ;; No point in doing this unless hard newlines is used.
82     (when use-hard-newlines
83       (let ((start (point-min)) end)
84         ;; Go through each paragraph, filling it and adding SPC
85         ;; as the last character on each line.
86         (while (setq end (text-property-any start (point-max) 'hard 't))
87           (let ((fill-column (eval fill-flowed-encode-column)))
88             (fill-region start end t 'nosqueeze 'to-eop))
89           (goto-char start)
90           ;; `fill-region' probably distorted end.
91           (setq end (text-property-any start (point-max) 'hard 't))
92           (while (and (< (point) end)
93                       (re-search-forward "$" (1- end) t))
94             (insert " ")
95             (setq end (1+ end))
96             (forward-char))
97           (goto-char (setq start (1+ end)))))
98       t)))
99
100 ;;;###autoload
101 (defun fill-flowed (&optional buffer)
102   (save-excursion
103     (set-buffer (or (current-buffer) buffer))
104     (goto-char (point-min))
105     ;; Remove space stuffing.
106     (while (re-search-forward "^ " nil t)
107       (delete-char -1)
108       (forward-line 1))
109     (goto-char (point-min))
110     (while (re-search-forward " $" nil t)
111       (when (save-excursion
112               (beginning-of-line)
113               (looking-at "^\\(>*\\)\\( ?\\)"))
114         (let ((quote (match-string 1))
115               sig)
116           (if (string= quote "")
117               (setq quote nil))
118           (when (and quote (string= (match-string 2) ""))
119             (save-excursion
120               ;; insert SP after quote for pleasant reading of quoted lines
121               (beginning-of-line)
122               (when (> (skip-chars-forward ">") 0)
123                 (insert " "))))
124           ;; XXX slightly buggy handling of "-- "
125           (while (and (save-excursion
126                         (ignore-errors (backward-char 3))
127                         (setq sig (looking-at "-- "))
128                         (looking-at "[^-][^-] "))
129                       (save-excursion
130                         (unless (eobp)
131                           (forward-char 1)
132                           (looking-at (format "^\\(%s\\)\\([^>\n\r]\\)"
133                                               (or quote " ?"))))))
134             (save-excursion
135               (replace-match (if (string= (match-string 2) " ")
136                                  "" "\\2")))
137             (backward-delete-char -1)
138             (end-of-line))
139           (unless sig
140             (condition-case nil
141                 (let ((fill-prefix (when quote (concat quote " ")))
142                       (fill-column (eval fill-flowed-display-column))
143                       filladapt-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 (eval-when-compile
155   (defvar show-trailing-whitespace))
156
157 (defvar fill-flowed-encode-tests
158   `(
159     ;; The syntax of each list element is:
160     ;; (INPUT . EXPECTED-OUTPUT)
161     (,(concat
162        "> Thou villainous ill-breeding spongy dizzy-eyed \n"
163        "> reeky elf-skinned pigeon-egg! \n"
164        ">> Thou artless swag-bellied milk-livered \n"
165        ">> dismal-dreaming idle-headed scut!\n"
166        ">>> Thou errant folly-fallen spleeny reeling-ripe \n"
167        ">>> unmuzzled ratsbane!\n"
168        ">>>> Henceforth, the coding style is to be strictly \n"
169        ">>>> enforced, including the use of only upper case.\n"
170        ">>>>> I've noticed a lack of adherence to the coding \n"
171        ">>>>> styles, of late.\n"
172        ">>>>>> Any complaints?")
173      .
174      ,(concat
175        "> Thou villainous ill-breeding spongy dizzy-eyed reeky elf-skinned\n"
176        "> pigeon-egg! \n"
177        ">> Thou artless swag-bellied milk-livered dismal-dreaming idle-headed\n"
178        ">> scut!\n"
179        ">>> Thou errant folly-fallen spleeny reeling-ripe unmuzzled ratsbane!\n"
180        ">>>> Henceforth, the coding style is to be strictly enforced,\n"
181        ">>>> including the use of only upper case.\n"
182        ">>>>> I've noticed a lack of adherence to the coding styles, of late.\n"
183        ">>>>>> Any complaints?\n"
184        ))
185     ;; (,(concat
186     ;;    "\n"
187     ;;    "> foo\n"
188     ;;    "> \n"
189     ;;    "> \n"
190     ;;    "> bar\n")
191     ;;  .
192     ;;  ,(concat
193     ;;    "\n"
194     ;;    "> foo bar\n"))
195     ))
196
197 (defun fill-flowed-test ()
198   (interactive "")
199   (switch-to-buffer (get-buffer-create "*Format=Flowed test output*"))
200   (erase-buffer)
201   (setq show-trailing-whitespace t)
202   (dolist (test fill-flowed-encode-tests)
203     (let (start output)
204       (insert "***** BEGIN TEST INPUT *****\n")
205       (insert (car test))
206       (insert "***** END TEST INPUT *****\n\n")
207       (insert "***** BEGIN TEST OUTPUT *****\n")
208       (setq start (point))
209       (insert (car test))
210       (save-restriction
211         (narrow-to-region start (point))
212         (fill-flowed))
213       (setq output (buffer-substring start (point-max)))
214       (insert "***** END TEST OUTPUT *****\n")
215       (unless (string= output (cdr test))
216         (insert "\n***** BEGIN TEST EXPECTED OUTPUT *****\n")
217         (insert (cdr test))
218         (insert "***** END TEST EXPECTED OUTPUT *****\n"))
219       (insert "\n\n")))
220   (goto-char (point-max)))
221
222 (provide 'flow-fill)
223
224 ;;; arch-tag: addc0040-bc53-4f17-b4bc-1eb44eed6f0b
225 ;;; flow-fill.el ends here