flow-fill.el (fill-flowed-encode, fill-flowed): Autoload.
[gnus] / lisp / flow-fill.el
1 ;;; flow-fill.el --- interprete RFC2646 "flowed" text
2
3 ;; Copyright (C) 2000, 2001, 2002, 2003 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: implement basic `fill-region' (Emacs and XEmacs
39 ;;       implementations differ..)
40
41 ;;; History:
42
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  committed 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
50
51 ;;; Code:
52
53 (eval-when-compile (require 'cl))
54
55 (defcustom fill-flowed-display-column 'fill-column
56   "Column beyond which format=flowed lines are wrapped, when displayed.
57 This can be a Lisp expression or an integer."
58   :type '(choice (const :tag "Standard `fill-column'" fill-column)
59                  (const :tag "Fit Window" (- (window-width) 5))
60                  (sexp)
61                  (integer)))
62
63 (defcustom fill-flowed-encode-column 66
64   "Column beyond which format=flowed lines are wrapped, in outgoing messages.
65 This can be a Lisp expression or an integer.
66 RFC 2646 suggests 66 characters for readability."
67   :type '(choice (const :tag "Standard fill-column" fill-column)
68                  (const :tag "RFC 2646 default (66)" 66)
69                  (sexp)
70                  (integer)))
71
72 (eval-and-compile
73   (defalias 'fill-flowed-point-at-bol
74         (if (fboundp 'point-at-bol)
75             'point-at-bol
76           'line-beginning-position))
77
78    (defalias 'fill-flowed-point-at-eol
79         (if (fboundp 'point-at-eol)
80             'point-at-eol
81           'line-end-position)))
82
83 ;;;###autoload
84 (defun fill-flowed-encode (&optional buffer)
85   (with-current-buffer (or buffer (current-buffer))
86     ;; No point in doing this unless hard newlines is used.
87     (when use-hard-newlines
88       (let ((start (point-min)) end)
89         ;; Go through each paragraph, filling it and adding SPC
90         ;; as the last character on each line.
91         (while (setq end (text-property-any start (point-max) 'hard 't))
92           (let ((fill-column (eval fill-flowed-encode-column)))
93             (fill-region start end t 'nosqueeze 'to-eop))
94           (goto-char start)
95           ;; `fill-region' probably distorted end.
96           (setq end (text-property-any start (point-max) 'hard 't))
97           (while (and (< (point) end)
98                       (re-search-forward "$" (1- end) t))
99             (insert " ")
100             (setq end (1+ end))
101             (forward-char))
102           (goto-char (setq start (1+ end)))))
103       t)))
104
105 ;;;###autoload
106 (defun fill-flowed (&optional buffer)
107   (save-excursion
108     (set-buffer (or (current-buffer) buffer))
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 (fill-flowed-point-at-bol)
145                                (min (1+ (fill-flowed-point-at-eol))
146                                     (point-max))
147                                'left 'nosqueeze))
148               (error
149                (forward-line 1)
150                nil))))))))
151
152 (provide 'flow-fill)
153
154 ;;; flow-fill.el ends here