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