1 ;;; flow-fill.el --- interprete RFC2646 "flowed" text
3 ;; Copyright (C) 2000, 2001 Free Software Foundation, Inc.
5 ;; Author: Simon Josefsson <jas@pdc.kth.se>
8 ;; This file is part of GNU Emacs.
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)
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.
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.
27 ;; This implement decoding of RFC2646 formatted text, including the
28 ;; quoted-depth wins rules.
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.
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'.
38 ;; Todo: implement basic `fill-region' (Emacs and XEmacs
39 ;; implementations differ..)
43 ;; 2000-02-17 posted on ding mailing list
44 ;; 2000-02-19 use `point-at-{b,e}ol' in XEmacs
45 ;; 2000-03-11 no compile warnings for point-at-bol stuff
46 ;; 2000-03-26 commited to gnus cvs
47 ;; 2000-10-23 don't flow "-- " lines, make "quote-depth wins" rule
48 ;; work when first line is at level 0.
49 ;; 2002-01-12 probably incomplete encoding support
53 (eval-when-compile (require 'cl))
56 (defalias 'fill-flowed-point-at-bol
57 (if (fboundp 'point-at-bol)
59 'line-beginning-position))
61 (defalias 'fill-flowed-point-at-eol
62 (if (fboundp 'point-at-eol)
66 (defun fill-flowed-encode (&optional buffer)
67 (with-current-buffer (or buffer (current-buffer))
68 ;; No point in doing this unless hard newlines is used.
69 (when use-hard-newlines
70 (let ((start (point-min)) end)
71 ;; Go through each paragraph, filling it and adding SPC
72 ;; as the last character on each line.
73 (while (setq end (text-property-any start (point-max) 'hard 't))
74 (let ((fill-column 66))
75 (fill-region start end t 'nosqueeze 'to-eop))
77 ;; `fill-region' probably distorted end.
78 (setq end (text-property-any start (point-max) 'hard 't))
79 (while (and (< (point) end)
80 (re-search-forward "$" (1- end) t))
84 (goto-char (setq start (1+ end)))))
87 (defun fill-flowed (&optional buffer)
89 (set-buffer (or (current-buffer) buffer))
90 (goto-char (point-min))
91 (while (re-search-forward " $" nil t)
94 (looking-at "^\\(>*\\)\\( ?\\)"))
95 (let ((quote (match-string 1)) sig)
96 (if (string= quote "")
98 (when (and quote (string= (match-string 2) ""))
100 ;; insert SP after quote for pleasant reading of quoted lines
102 (when (> (skip-chars-forward ">") 0)
104 ;; XXX slightly buggy handling of "-- "
105 (while (and (save-excursion
106 (ignore-errors (backward-char 3))
107 (setq sig (looking-at "-- "))
108 (looking-at "[^-][^-] "))
112 (looking-at (format "^\\(%s\\)\\([^>]\\)" (or quote " ?"))))))
114 (replace-match (if (string= (match-string 2) " ")
116 (backward-delete-char -1)
119 (let ((fill-prefix (when quote (concat quote " ")))
120 (fill-column (1- (window-width))))
121 (fill-region (fill-flowed-point-at-bol)
122 (min (1+ (fill-flowed-point-at-eol)) (point-max))
123 'left 'nosqueeze))))))))
127 ;;; flow-fill.el ends here