Import No Gnus v0.1.
[elisp/gnus.git-] / lisp / fill-flowed.el
1 ;;; fill-flowed.el --- interprete RFC2646 "flowed" text
2 ;; Copyright (C) 2000 Free Software Foundation, Inc.
3
4 ;; Author: Simon Josefsson <jas@pdc.kth.se>
5 ;; Keywords: mail
6
7 ;; This program is free software; you can redistribute it and/or modify
8 ;; it under the terms of the GNU General Public License as published by
9 ;; the Free Software Foundation; either version 2 of the License, or
10 ;; (at your option) any later version.
11 ;;
12 ;; This program is distributed in the hope that it will be useful,
13 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 ;; GNU General Public License for more details.
16 ;;
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with this program; if not, write to the Free Software
19 ;; Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20
21 ;;; Commentary:
22
23 ;; This implement decoding of RFC2646 formatted text, including the
24 ;; quoted-depth wins rules.
25
26 ;; Theory of operation: search for lines ending with SPC, save quote
27 ;; length of line, remove SPC and concatenate line with the following
28 ;; line if quote length of following line matches current line.
29
30 ;; When no further concatenations are possible, we've found a
31 ;; paragraph and we let `fill-region' fill the long line into several
32 ;; lines with the quote prefix as `fill-prefix'.
33
34 ;; Todo: encoding
35
36 ;; History:
37
38 ;; 2000-02-17  posted on ding mailing list
39 ;; 2000-02-19  use `point-at-{b,e}ol' in XEmacs
40 ;; 2000-03-11  no compile warnings for point-at-bol stuff
41 ;; 2000-03-26  commited to gnus cvs
42
43 ;;; Code:
44
45 (eval-and-compile
46   (fset 'fill-flowed-point-at-bol
47         (if (fboundp 'point-at-bol)
48             'point-at-bol
49           'line-beginning-position))
50   
51   (fset 'fill-flowed-point-at-eol
52         (if (fboundp 'point-at-eol)
53             'point-at-eol
54           'line-end-position)))
55
56 (defun fill-flowed (&optional buffer)
57   (save-excursion
58     (set-buffer (or (current-buffer) buffer))
59     (goto-char (point-min))
60     (while (re-search-forward " $" nil t)
61       (when (save-excursion
62               (beginning-of-line)
63               (looking-at "^\\(>*\\)\\( ?\\)"))
64         (let ((quote (match-string 1)))
65           (if (string= quote "")
66               (setq quote nil))
67           (when (and quote (string= (match-string 2) ""))
68             (save-excursion
69               ;; insert SP after quote for pleasant reading of quoted lines
70               (beginning-of-line)
71               (when (> (skip-chars-forward ">") 0)
72                 (insert " "))))
73           (while (and (save-excursion
74                         (backward-char 3)
75                         (looking-at "[^-][^-] "))
76                       (save-excursion
77                         (unless (eobp)
78                           (forward-char 1)
79                           (if quote
80                               (looking-at (format "^\\(%s\\)\\([^>]\\)" quote))
81                             (looking-at "^ ?")))))
82             (save-excursion
83               (replace-match (if (string= (match-string 2) " ")
84                                  "" "\\2")))
85             (backward-delete-char -1)
86             (end-of-line))
87           (let ((fill-prefix (when quote (concat quote " "))))
88             (fill-region (fill-flowed-point-at-bol)
89                          (fill-flowed-point-at-eol)
90                          'left 'nosqueeze)))))))
91
92 (provide 'fill-flowed)
93
94 ;;; fill-flowed.el ends here