added `html-xhtml' for XHTML input
[bpt/emacs.git] / lisp / textmodes / refill.el
1 ;;; refill.el --- `auto-fill' by refilling paragraphs on changes
2
3 ;; Copyright (C) 2000 Free Software Foundation, Inc.
4
5 ;; Author: Dave Love <fx@gnu.org>
6 ;; Keywords: wp
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 ;; Provides a mode where paragraphs are refilled after changes in them
28 ;; (using `after-change-functions'). This gives something akin to typical
29 ;; word processor-style filling. We restrict refilling due to
30 ;; self-insertion to the characters which trigger auto-fill.
31
32 ;; It partly satisfies a todo item in enriched.el for some value of
33 ;; `without slowing down editing too much'. It doesn't attempt to do
34 ;; anything (using `window-size-change-functions'?) about resizing
35 ;; windows -- who cares?
36
37 ;; This implementation is probably fragile and missing some special
38 ;; cases -- not extensively tested. Yanking paragraph breaks, for
39 ;; instance, won't DTRT by refilling all the relevant paragraphs.
40
41 ;; You could do it a bit more efficiently (and robustly?) with just an
42 ;; auto-fill function, but that doesn't cope with changes other than
43 ;; through self-insertion. (Using auto-fill and after-change
44 ;; functions together didn't seem winning.) This could probably
45 ;; benefit from a less-general and faster `fill-paragraph-function',
46 ;; ideally as a primitive.
47
48 ;; The work is done in a local post-command hook but only if
49 ;; `refill-doit' has been set by the after-change function. Using
50 ;; `post-command-hook' ensures simply that refilling only happens
51 ;; once per command.
52
53 ;; [Per Abrahamsen's maniac.el does a similar thing, but operates from
54 ;; post-command-hook. I don't understand the statement in it that
55 ;; after-change-functions don't work for this purpose; perhaps there was
56 ;; some Emacs bug at the time. ISTR maniac has problems with
57 ;; whitespace at the end of paragraphs.]
58
59 ;;; Code:
60
61 (defvar refill-ignorable-overlay nil
62 "Portion of the most recently filled paragraph not needing filling.
63 This is used to optimize refilling.")
64 (make-variable-buffer-local 'refill-ignorable-overlay)
65
66 (defun refill-adjust-ignorable-overlay (overlay afterp beg end &optional len)
67 "Adjust OVERLAY to not include the about-to-be-modified region."
68 (when (not afterp)
69 (save-excursion
70 (goto-char beg)
71 (forward-line -1)
72 (if (<= (point) (overlay-start overlay))
73 ;; Just get OVERLAY out of the way
74 (move-overlay overlay 1 1)
75 ;; Make overlay contain only the region
76 (move-overlay overlay (overlay-start overlay) (point))))))
77
78 (defun refill-fill-paragraph-at (pos &optional arg)
79 "Like `fill-paragraph' at POS, but don't delete whitespace at paragraph end."
80 (let (fill-pfx)
81 (save-excursion
82 (goto-char pos)
83 (forward-paragraph)
84 (skip-syntax-backward "-")
85 (let ((end (point))
86 (beg (progn (backward-paragraph) (point)))
87 (obeg (overlay-start refill-ignorable-overlay))
88 (oend (overlay-end refill-ignorable-overlay)))
89 (goto-char pos)
90 (if (and (>= beg obeg) (< beg oend))
91 ;; Limit filling to the modified tail of the paragraph.
92 (let (;; When adaptive-fill-mode is enabled, the filling
93 ;; functions will attempt to set the fill prefix from
94 ;; the fake paragraph bounds we pass in, so set it
95 ;; ourselves first, using the real paragraph bounds.
96 (fill-prefix
97 (if (and adaptive-fill-mode
98 (or (null fill-prefix) (string= fill-prefix "")))
99 (fill-context-prefix beg end)
100 fill-prefix))
101 ;; Turn off adaptive-fill-mode temporarily
102 (adaptive-fill-mode nil))
103 (save-restriction
104 (if use-hard-newlines
105 (fill-region oend end arg)
106 (fill-region-as-paragraph oend end arg)))
107 (setq fill-pfx fill-prefix)
108 (move-overlay refill-ignorable-overlay obeg (point)))
109 ;; Fill the whole paragraph
110 (setq fill-pfx
111 (save-restriction
112 (if use-hard-newlines
113 (fill-region beg end arg)
114 (fill-region-as-paragraph beg end arg))))
115 (move-overlay refill-ignorable-overlay beg (point)))))
116 (skip-line-prefix fill-pfx)))
117
118 (defun refill-fill-paragraph (arg)
119 "Like `fill-paragraph' but don't delete whitespace at paragraph end."
120 (refill-fill-paragraph-at (point) arg))
121
122 (defvar refill-doit nil
123 "Non-nil means that `refill-post-command-function' does its processing.
124 Set by `refill-after-change-function' in `after-change-functions' and
125 unset by `refill-post-command-function' in `post-command-hook', and
126 sometimes `refill-pre-command-function' in `pre-command-hook'. This
127 ensures refilling is only done once per command that causes a change,
128 regardless of the number of after-change calls from commands doing
129 complex processing.")
130 (make-variable-buffer-local 'refill-doit)
131
132 (defun refill-after-change-function (beg end len)
133 "Function for `after-change-functions' which just sets `refill-doit'."
134 (unless undo-in-progress
135 (setq refill-doit end)))
136
137 (defun refill-post-command-function ()
138 "Post-command function to do refilling (conditionally)."
139 (when refill-doit ; there was a change
140 ;; There's probably scope for more special cases here...
141 (if (eq this-command 'self-insert-command)
142 ;; Treat self-insertion commands specially, since they don't
143 ;; always reset `refill-doit' -- for self-insertion commands that
144 ;; *don't* cause a refill, we want to leave it turned on so that
145 ;; any subsequent non-modification command will cause a refill.
146 (when (aref auto-fill-chars (char-before))
147 ;; Respond to the same characters as auto-fill (other than
148 ;; newline, covered below).
149 (refill-fill-paragraph-at refill-doit)
150 (setq refill-doit nil))
151 (cond
152 ((or (eq this-command 'quoted-insert)
153 (eq this-command 'fill-paragraph)
154 (eq this-command 'fill-region))
155 nil)
156 ((or (eq this-command 'newline)
157 (eq this-command 'newline-and-indent)
158 (eq this-command 'open-line))
159 ;; Don't zap what was just inserted.
160 (save-excursion
161 (beginning-of-line) ; for newline-and-indent
162 (skip-chars-backward "\n")
163 (save-restriction
164 (narrow-to-region (point-min) (point))
165 (refill-fill-paragraph-at refill-doit)))
166 (widen)
167 (save-excursion
168 (skip-chars-forward "\n")
169 (save-restriction
170 (narrow-to-region (line-beginning-position) (point-max))
171 (refill-fill-paragraph-at refill-doit))))
172 (t
173 (refill-fill-paragraph-at refill-doit)))
174 (setq refill-doit nil))))
175
176 (defun refill-pre-command-function ()
177 "Pre-command function to do refilling (conditionally)."
178 (when (and refill-doit (not (eq this-command 'self-insert-command)))
179 ;; A previous setting of `refill-doit' didn't result in a refill,
180 ;; because it was a self-insert-command. Since the next command is
181 ;; something else, do the refill now.
182 (refill-fill-paragraph-at refill-doit)
183 (setq refill-doit nil)))
184
185 (defvar refill-late-fill-paragraph-function nil)
186
187 ;;;###autoload
188 (define-minor-mode refill-mode
189 "Toggle Refill minor mode.
190 With prefix arg, turn Refill mode on iff arg is positive.
191
192 When Refill mode is on, the current paragraph will be formatted when
193 changes are made within it. Self-inserting characters only cause
194 refilling if they would cause auto-filling."
195 nil " Refill" nil
196 ;; This provides the test for recursive paragraph filling.
197 (make-local-variable 'fill-paragraph-function)
198 (if refill-mode
199 (progn
200 (add-hook 'after-change-functions 'refill-after-change-function nil t)
201 (add-hook 'post-command-hook 'refill-post-command-function nil t)
202 (add-hook 'pre-command-hook 'refill-pre-command-function nil t)
203 (set (make-local-variable 'refill-late-fill-paragraph-function)
204 fill-paragraph-function)
205 (set (make-local-variable 'fill-paragraph-function)
206 'refill-fill-paragraph)
207 (setq refill-ignorable-overlay (make-overlay 1 1 nil nil t))
208 (overlay-put refill-ignorable-overlay 'modification-hooks
209 '(refill-adjust-ignorable-overlay))
210 (overlay-put refill-ignorable-overlay 'insert-behind-hooks
211 '(refill-adjust-ignorable-overlay))
212 (auto-fill-mode 0))
213 (remove-hook 'after-change-functions 'refill-after-change-function t)
214 (remove-hook 'post-command-hook 'refill-post-command-function t)
215 (delete-overlay refill-ignorable-overlay)
216 (setq fill-paragraph-function refill-late-fill-paragraph-function)))
217
218 (provide 'refill)
219
220 ;;; refill.el ends here