Update FSF's address.
[bpt/emacs.git] / lisp / textmodes / refill.el
1 ;;; refill.el --- `auto-fill' by refilling paragraphs on changes
2
3 ;; Copyright (C) 2000, 2003, 2005 Free Software Foundation, Inc.
4
5 ;; Author: Dave Love <fx@gnu.org>
6 ;; Maintainer: Miles Bader <miles@gnu.org>
7 ;; Keywords: wp
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
25
26 ;;; Commentary:
27
28 ;; Provides a mode where paragraphs are refilled after changes in them
29 ;; (using `after-change-functions'). This gives something akin to typical
30 ;; word processor-style filling. We restrict refilling due to
31 ;; self-insertion to the characters which trigger auto-fill.
32
33 ;; It partly satisfies a todo item in enriched.el for some value of
34 ;; `without slowing down editing too much'. It doesn't attempt to do
35 ;; anything (using `window-size-change-functions'?) about resizing
36 ;; windows -- who cares?
37
38 ;; This implementation is probably fragile and missing some special
39 ;; cases -- not extensively tested. Yanking paragraph breaks, for
40 ;; instance, won't DTRT by refilling all the relevant paragraphs.
41
42 ;; You could do it a bit more efficiently (and robustly?) with just an
43 ;; auto-fill function, but that doesn't cope with changes other than
44 ;; through self-insertion. (Using auto-fill and after-change
45 ;; functions together didn't seem winning.) This could probably
46 ;; benefit from a less-general and faster `fill-paragraph-function',
47 ;; ideally as a primitive.
48
49 ;; The work is done in a local post-command hook but only if
50 ;; `refill-doit' has been set by the after-change function. Using
51 ;; `post-command-hook' ensures simply that refilling only happens
52 ;; once per command.
53
54 ;; [Per Abrahamsen's maniac.el does a similar thing, but operates from
55 ;; post-command-hook. I don't understand the statement in it that
56 ;; after-change-functions don't work for this purpose; perhaps there was
57 ;; some Emacs bug at the time. ISTR maniac has problems with
58 ;; whitespace at the end of paragraphs.]
59
60 ;;; Todo/Bugs:
61
62 ;; - When deleting the first word on a line, the space after that word tends
63 ;; to become part of the fill-prefix, causing either wrong filling of the
64 ;; remaining text, or causing the cursor to move unexpectedly. Ex:
65 ;; Start with
66 ;; I>< blabla
67 ;;
68 ;; and hit backspace. We end up with
69 ;;
70 ;; ><blabla
71 ;; instead of
72 ;; >< blabla
73 ;;
74 ;; Other example. Start with
75 ;;
76 ;; Foo bar blablabla asdgf
77 ;; word>< asdfas dfasdfasd
78 ;; asd asdfa sdfasd sdf
79 ;;
80 ;; and hit M-backspace. We end up with
81 ;;
82 ;; Foo bar blablabla asdgf
83 ;; ><asdfas dfasdfasd asd
84 ;; asdfa sdfasd sdf
85
86 ;;; Code:
87
88 (eval-when-compile (require 'cl))
89
90 (defgroup refill nil
91 "Refilling paragraphs on changes."
92 :group 'fill)
93
94 (defvar refill-ignorable-overlay nil
95 "Portion of the most recently filled paragraph not needing filling.
96 This is used to optimize refilling.")
97 (make-variable-buffer-local 'refill-ignorable-overlay)
98
99 (defun refill-adjust-ignorable-overlay (overlay afterp beg end &optional len)
100 "Adjust OVERLAY to not include the about-to-be-modified region."
101 (when (not afterp)
102 (save-excursion
103 (goto-char beg)
104 (forward-line -1)
105 (if (<= (point) (overlay-start overlay))
106 ;; Just get OVERLAY out of the way
107 (move-overlay overlay (point-min) (point-min))
108 ;; Make overlay contain only the region
109 (move-overlay overlay (overlay-start overlay) (point))))))
110
111 (defun refill-fill-paragraph-at (pos &optional arg)
112 "Like `fill-paragraph' at POS, but don't delete whitespace at paragraph end."
113 (save-excursion
114 (goto-char pos)
115 ;; FIXME: forward-paragraph seems to disregard `use-hard-newlines',
116 ;; leading to excessive refilling and wrong choice of fill-prefix.
117 ;; might be a bug in my paragraphs.el.
118 (forward-paragraph)
119 (skip-syntax-backward "-")
120 (let ((end (point))
121 (beg (progn (backward-paragraph) (point)))
122 (obeg (overlay-start refill-ignorable-overlay))
123 (oend (overlay-end refill-ignorable-overlay)))
124 (unless (> beg pos) ;Don't fill if point is outside the paragraph.
125 (goto-char pos)
126 (if (and (>= beg obeg) (< beg oend))
127 ;; Limit filling to the modified tail of the paragraph.
128 (let ( ;; When adaptive-fill-mode is enabled, the filling
129 ;; functions will attempt to set the fill prefix from
130 ;; the fake paragraph bounds we pass in, so set it
131 ;; ourselves first, using the real paragraph bounds.
132 (fill-prefix
133 (if (and adaptive-fill-mode
134 (or (null fill-prefix) (string= fill-prefix "")))
135 (fill-context-prefix beg end)
136 fill-prefix))
137 ;; Turn off adaptive-fill-mode temporarily
138 (adaptive-fill-mode nil))
139 (save-restriction
140 (if use-hard-newlines
141 (fill-region oend end arg)
142 (fill-region-as-paragraph oend end arg)))
143 (move-overlay refill-ignorable-overlay obeg (point)))
144 ;; Fill the whole paragraph
145 (save-restriction
146 (if use-hard-newlines
147 (fill-region beg end arg)
148 (fill-region-as-paragraph beg end arg)))
149 (move-overlay refill-ignorable-overlay beg (point)))))))
150
151 (defun refill-fill-paragraph (arg)
152 "Like `fill-paragraph' but don't delete whitespace at paragraph end."
153 (refill-fill-paragraph-at (point) arg))
154
155 (defvar refill-doit nil
156 "Non-nil tells `refill-post-command-function' to do its processing.
157 Set by `refill-after-change-function' in `after-change-functions' and
158 unset by `refill-post-command-function' in `post-command-hook', and
159 sometimes `refill-pre-command-function' in `pre-command-hook'. This
160 ensures refilling is only done once per command that causes a change,
161 regardless of the number of after-change calls from commands doing
162 complex processing.")
163 (make-variable-buffer-local 'refill-doit)
164
165 (defun refill-after-change-function (beg end len)
166 "Function for `after-change-functions' which just sets `refill-doit'."
167 (unless undo-in-progress
168 (setq refill-doit end)))
169
170 (defun refill-post-command-function ()
171 "Post-command function to do refilling (conditionally)."
172 (when refill-doit ; there was a change
173 ;; There's probably scope for more special cases here...
174 (case this-command
175 (self-insert-command
176 ;; Treat self-insertion commands specially, since they don't
177 ;; always reset `refill-doit' -- for self-insertion commands that
178 ;; *don't* cause a refill, we want to leave it turned on so that
179 ;; any subsequent non-modification command will cause a refill.
180 (when (aref auto-fill-chars (char-before))
181 ;; Respond to the same characters as auto-fill (other than
182 ;; newline, covered below).
183 (refill-fill-paragraph-at refill-doit)
184 (setq refill-doit nil)))
185 ((quoted-insert fill-paragraph fill-region) nil)
186 ((newline newline-and-indent open-line indent-new-comment-line
187 reindent-then-newline-and-indent)
188 ;; Don't zap what was just inserted.
189 (save-excursion
190 (beginning-of-line) ; for newline-and-indent
191 (skip-chars-backward "\n")
192 (save-restriction
193 (narrow-to-region (point-min) (point))
194 (refill-fill-paragraph-at refill-doit)))
195 (widen)
196 (save-excursion
197 (skip-chars-forward "\n")
198 (save-restriction
199 (narrow-to-region (line-beginning-position) (point-max))
200 (refill-fill-paragraph-at refill-doit))))
201 (t
202 (refill-fill-paragraph-at refill-doit)))
203 (setq refill-doit nil)))
204
205 (defun refill-pre-command-function ()
206 "Pre-command function to do refilling (conditionally)."
207 (when (and refill-doit (not (eq this-command 'self-insert-command)))
208 ;; A previous setting of `refill-doit' didn't result in a refill,
209 ;; because it was a self-insert-command. Since the next command is
210 ;; something else, do the refill now.
211 (refill-fill-paragraph-at refill-doit)
212 (setq refill-doit nil)))
213
214 (defvar refill-saved-state nil)
215
216 ;;;###autoload
217 (define-minor-mode refill-mode
218 "Toggle Refill minor mode.
219 With prefix arg, turn Refill mode on iff arg is positive.
220
221 When Refill mode is on, the current paragraph will be formatted when
222 changes are made within it. Self-inserting characters only cause
223 refilling if they would cause auto-filling."
224 :group 'refill
225 :lighter " Refill"
226 :keymap '(("\177" . backward-delete-char-untabify))
227 ;; Remove old state if necessary
228 (when refill-ignorable-overlay
229 (delete-overlay refill-ignorable-overlay)
230 (kill-local-variable 'refill-ignorable-overlay))
231 (when (local-variable-p 'refill-saved-state)
232 (dolist (x refill-saved-state)
233 (set (make-local-variable (car x)) (cdr x)))
234 (kill-local-variable 'refill-saved-state))
235 (if refill-mode
236 (progn
237 (add-hook 'after-change-functions 'refill-after-change-function nil t)
238 (add-hook 'post-command-hook 'refill-post-command-function nil t)
239 (add-hook 'pre-command-hook 'refill-pre-command-function nil t)
240 (set (make-local-variable 'refill-saved-state)
241 (mapcar (lambda (s) (cons s (symbol-value s)))
242 '(fill-paragraph-function auto-fill-function)))
243 ;; This provides the test for recursive paragraph filling.
244 (set (make-local-variable 'fill-paragraph-function)
245 'refill-fill-paragraph)
246 ;; When using justification, doing DEL on 2 spaces should remove
247 ;; both, otherwise, the subsequent refill will undo the DEL.
248 (set (make-local-variable 'backward-delete-char-untabify-method)
249 'hungry)
250 (setq refill-ignorable-overlay (make-overlay 1 1 nil nil t))
251 (overlay-put refill-ignorable-overlay 'modification-hooks
252 '(refill-adjust-ignorable-overlay))
253 (overlay-put refill-ignorable-overlay 'insert-behind-hooks
254 '(refill-adjust-ignorable-overlay))
255 (auto-fill-mode 0))
256 (remove-hook 'after-change-functions 'refill-after-change-function t)
257 (remove-hook 'post-command-hook 'refill-post-command-function t)
258 (kill-local-variable 'backward-delete-char-untabify-method)))
259
260 (provide 'refill)
261
262 ;; arch-tag: 2c4ce9e8-1daa-4a3b-b6f8-fd6ac5bf6138
263 ;;; refill.el ends here