4abbdc6c9fe64d76081aee09437657bfffec8d63
[elisp/gnus.git-] / lisp / flow-fill.el
1 ;;; flow-fill.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, implement basic `fill-region' (Emacs and XEmacs
35 ;;       implementations differ..)
36
37 ;; History:
38
39 ;; 2000-02-17  posted on ding mailing list
40 ;; 2000-02-19  use `point-at-{b,e}ol' in XEmacs
41 ;; 2000-03-11  no compile warnings for point-at-bol stuff
42 ;; 2000-03-26  commited to gnus cvs
43 ;; 2000-10-23  don't flow "-- " lines, make "quote-depth wins" rule
44 ;;             work when first line is at level 0.
45
46 ;;; Code:
47
48 (eval-and-compile
49   (defalias 'fill-flowed-point-at-bol
50         (if (fboundp 'point-at-bol)
51             'point-at-bol
52           'line-beginning-position))
53
54    (defalias 'fill-flowed-point-at-eol
55         (if (fboundp 'point-at-eol)
56             'point-at-eol
57           'line-end-position)))
58
59 (defun fill-flowed (&optional buffer)
60   (save-excursion
61     (set-buffer (or (current-buffer) buffer))
62     (goto-char (point-min))
63     (while (re-search-forward " $" nil t)
64       (when (save-excursion
65               (beginning-of-line)
66               (looking-at "^\\(>*\\)\\( ?\\)"))
67         (let ((quote (match-string 1)) sig)
68           (if (string= quote "")
69               (setq quote nil))
70           (when (and quote (string= (match-string 2) ""))
71             (save-excursion
72               ;; insert SP after quote for pleasant reading of quoted lines
73               (beginning-of-line)
74               (when (> (skip-chars-forward ">") 0)
75                 (insert " "))))
76           (while (and (save-excursion
77                         (ignore-errors (backward-char 3))
78                         (setq sig (looking-at "-- "))
79                         (looking-at "[^-][^-] "))
80                       (save-excursion
81                         (unless (eobp)
82                           (forward-char 1)
83                           (looking-at (format "^\\(%s\\)\\([^>]\\)" (or quote " ?"))))))
84             (save-excursion
85               (replace-match (if (string= (match-string 2) " ")
86                                  "" "\\2")))
87             (backward-delete-char -1)
88             (end-of-line))
89           (unless sig
90             (let ((fill-prefix (when quote (concat quote " "))))
91               (fill-region (fill-flowed-point-at-bol)
92                            (fill-flowed-point-at-eol)
93                            'left 'nosqueeze))))))))
94
95 (provide 'flow-fill)
96
97 ;;; flow-fill.el ends here