New directory
[bpt/emacs.git] / lisp / textmodes / refill.el
CommitLineData
aaaf7be7
DL
1;;; refill.el --- `auto-fill' by refilling paragraphs on changes
2
ad2feb08 3;; Copyright (C) 2000, 2003 Free Software Foundation, Inc.
aaaf7be7
DL
4
5;; Author: Dave Love <fx@gnu.org>
6;; Keywords: wp
7
2be7dabc
GM
8;; This file is part of GNU Emacs.
9
10;; GNU Emacs is free software; you can redistribute it and/or modify
aaaf7be7 11;; it under the terms of the GNU General Public License as published by
2be7dabc
GM
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,
aaaf7be7
DL
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.
2be7dabc 19
aaaf7be7 20;; You should have received a copy of the GNU General Public License
2be7dabc
GM
21;; along with GNU Emacs; see the file COPYING. If not, write to the
22;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
aaaf7be7
DL
23;; Boston, MA 02111-1307, USA.
24
25;;; Commentary:
26
27;; Provides a mode where paragraphs are refilled after changes in them
7850e188 28;; (using `after-change-functions'). This gives something akin to typical
aaaf7be7
DL
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
7850e188 50;; `post-command-hook' ensures simply that refilling only happens
aaaf7be7
DL
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
7850e188 55;; after-change-functions don't work for this purpose; perhaps there was
aaaf7be7
DL
56;; some Emacs bug at the time. ISTR maniac has problems with
57;; whitespace at the end of paragraphs.]
58
d1a27f1d
SM
59;;; Todo/Bugs:
60
61;; - When deleting the first word on a line, the space after that word tends
62;; to become part of the fill-prefix, causing either wrong filling of the
63;; remaining text, or causing the cursor to move unexpectedly. Ex:
64;; Start with
65;; I>< blabla
66;;
67;; and hit backspace. We end up with
68;;
69;; ><blabla
70;; instead of
71;; >< blabla
72;;
73;; Other example. Start with
74;;
75;; Foo bar blablabla asdgf
76;; word>< asdfas dfasdfasd
77;; asd asdfa sdfasd sdf
78;;
79;; and hit M-backspace. We end up with
80;;
81;; Foo bar blablabla asdgf
82;; ><asdfas dfasdfasd asd
83;; asdfa sdfasd sdf
84
aaaf7be7
DL
85;;; Code:
86
e4caa91a
MR
87(defgroup refill nil
88 "Refilling paragraphs on changes."
89 :group 'fill)
90
867092e9
MB
91(defvar refill-ignorable-overlay nil
92 "Portion of the most recently filled paragraph not needing filling.
93This is used to optimize refilling.")
94(make-variable-buffer-local 'refill-ignorable-overlay)
95
96(defun refill-adjust-ignorable-overlay (overlay afterp beg end &optional len)
97 "Adjust OVERLAY to not include the about-to-be-modified region."
98 (when (not afterp)
867092e9
MB
99 (save-excursion
100 (goto-char beg)
101 (forward-line -1)
102 (if (<= (point) (overlay-start overlay))
103 ;; Just get OVERLAY out of the way
ad2feb08 104 (move-overlay overlay (point-min) (point-min))
db95369b 105 ;; Make overlay contain only the region
867092e9
MB
106 (move-overlay overlay (overlay-start overlay) (point))))))
107
108(defun refill-fill-paragraph-at (pos &optional arg)
109 "Like `fill-paragraph' at POS, but don't delete whitespace at paragraph end."
ad2feb08
SM
110 (save-excursion
111 (goto-char pos)
112 ;; FIXME: forward-paragraph seems to disregard `use-hard-newlines',
113 ;; leading to excessive refilling and wrong choice of fill-prefix.
114 ;; might be a bug in my paragraphs.el.
115 (forward-paragraph)
116 (skip-syntax-backward "-")
117 (let ((end (point))
118 (beg (progn (backward-paragraph) (point)))
119 (obeg (overlay-start refill-ignorable-overlay))
120 (oend (overlay-end refill-ignorable-overlay)))
121 (unless (> beg pos) ;Don't fill if point is outside the paragraph.
122 (goto-char pos)
123 (if (and (>= beg obeg) (< beg oend))
124 ;; Limit filling to the modified tail of the paragraph.
125 (let ( ;; When adaptive-fill-mode is enabled, the filling
126 ;; functions will attempt to set the fill prefix from
127 ;; the fake paragraph bounds we pass in, so set it
128 ;; ourselves first, using the real paragraph bounds.
129 (fill-prefix
130 (if (and adaptive-fill-mode
131 (or (null fill-prefix) (string= fill-prefix "")))
132 (fill-context-prefix beg end)
133 fill-prefix))
134 ;; Turn off adaptive-fill-mode temporarily
135 (adaptive-fill-mode nil))
136 (save-restriction
137 (if use-hard-newlines
138 (fill-region oend end arg)
139 (fill-region-as-paragraph oend end arg)))
140 (move-overlay refill-ignorable-overlay obeg (point)))
141 ;; Fill the whole paragraph
142 (save-restriction
143 (if use-hard-newlines
144 (fill-region beg end arg)
145 (fill-region-as-paragraph beg end arg)))
146 (move-overlay refill-ignorable-overlay beg (point)))))))
867092e9
MB
147
148(defun refill-fill-paragraph (arg)
149 "Like `fill-paragraph' but don't delete whitespace at paragraph end."
150 (refill-fill-paragraph-at (point) arg))
aaaf7be7
DL
151
152(defvar refill-doit nil
8b4703c4 153 "Non-nil tells `refill-post-command-function' to do its processing.
7850e188 154Set by `refill-after-change-function' in `after-change-functions' and
867092e9
MB
155unset by `refill-post-command-function' in `post-command-hook', and
156sometimes `refill-pre-command-function' in `pre-command-hook'. This
aaaf7be7
DL
157ensures refilling is only done once per command that causes a change,
158regardless of the number of after-change calls from commands doing
159complex processing.")
160(make-variable-buffer-local 'refill-doit)
161
162(defun refill-after-change-function (beg end len)
163 "Function for `after-change-functions' which just sets `refill-doit'."
164 (unless undo-in-progress
867092e9 165 (setq refill-doit end)))
aaaf7be7
DL
166
167(defun refill-post-command-function ()
168 "Post-command function to do refilling (conditionally)."
867092e9 169 (when refill-doit ; there was a change
aaaf7be7 170 ;; There's probably scope for more special cases here...
867092e9
MB
171 (if (eq this-command 'self-insert-command)
172 ;; Treat self-insertion commands specially, since they don't
173 ;; always reset `refill-doit' -- for self-insertion commands that
174 ;; *don't* cause a refill, we want to leave it turned on so that
175 ;; any subsequent non-modification command will cause a refill.
176 (when (aref auto-fill-chars (char-before))
177 ;; Respond to the same characters as auto-fill (other than
178 ;; newline, covered below).
179 (refill-fill-paragraph-at refill-doit)
180 (setq refill-doit nil))
181 (cond
182 ((or (eq this-command 'quoted-insert)
183 (eq this-command 'fill-paragraph)
184 (eq this-command 'fill-region))
185 nil)
186 ((or (eq this-command 'newline)
187 (eq this-command 'newline-and-indent)
188 (eq this-command 'open-line))
189 ;; Don't zap what was just inserted.
190 (save-excursion
191 (beginning-of-line) ; for newline-and-indent
192 (skip-chars-backward "\n")
193 (save-restriction
194 (narrow-to-region (point-min) (point))
195 (refill-fill-paragraph-at refill-doit)))
196 (widen)
197 (save-excursion
198 (skip-chars-forward "\n")
199 (save-restriction
200 (narrow-to-region (line-beginning-position) (point-max))
201 (refill-fill-paragraph-at refill-doit))))
202 (t
203 (refill-fill-paragraph-at refill-doit)))
204 (setq refill-doit nil))))
205
206(defun refill-pre-command-function ()
207 "Pre-command function to do refilling (conditionally)."
208 (when (and refill-doit (not (eq this-command 'self-insert-command)))
209 ;; A previous setting of `refill-doit' didn't result in a refill,
210 ;; because it was a self-insert-command. Since the next command is
211 ;; something else, do the refill now.
212 (refill-fill-paragraph-at refill-doit)
aaaf7be7
DL
213 (setq refill-doit nil)))
214
10ee3d79
DL
215(defvar refill-late-fill-paragraph-function nil)
216
aaaf7be7
DL
217;;;###autoload
218(define-minor-mode refill-mode
219 "Toggle Refill minor mode.
220With prefix arg, turn Refill mode on iff arg is positive.
221
222When Refill mode is on, the current paragraph will be formatted when
223changes are made within it. Self-inserting characters only cause
224refilling if they would cause auto-filling."
d1a27f1d 225 nil " Refill" '(("\177" . backward-delete-char-untabify))
f5bd4bf2
MB
226 ;; Remove old state if necessary
227 (when refill-ignorable-overlay
228 (delete-overlay refill-ignorable-overlay)
229 (kill-local-variable 'refill-ignorable-overlay))
657059d0 230 (when (local-variable-p 'refill-late-fill-paragraph-function)
f5bd4bf2
MB
231 (setq fill-paragraph-function refill-late-fill-paragraph-function)
232 (kill-local-variable 'refill-late-fill-paragraph-function))
aaaf7be7 233 (if refill-mode
7850e188
SM
234 (progn
235 (add-hook 'after-change-functions 'refill-after-change-function nil t)
236 (add-hook 'post-command-hook 'refill-post-command-function nil t)
867092e9 237 (add-hook 'pre-command-hook 'refill-pre-command-function nil t)
10ee3d79
DL
238 (set (make-local-variable 'refill-late-fill-paragraph-function)
239 fill-paragraph-function)
f5bd4bf2 240 ;; This provides the test for recursive paragraph filling.
7850e188
SM
241 (set (make-local-variable 'fill-paragraph-function)
242 'refill-fill-paragraph)
d1a27f1d
SM
243 ;; When using justification, doing DEL on 2 spaces should remove
244 ;; both, otherwise, the subsequent refill will undo the DEL.
245 (set (make-local-variable 'backward-delete-char-untabify-method)
246 'hungry)
867092e9
MB
247 (setq refill-ignorable-overlay (make-overlay 1 1 nil nil t))
248 (overlay-put refill-ignorable-overlay 'modification-hooks
249 '(refill-adjust-ignorable-overlay))
250 (overlay-put refill-ignorable-overlay 'insert-behind-hooks
251 '(refill-adjust-ignorable-overlay))
7850e188 252 (auto-fill-mode 0))
aaaf7be7
DL
253 (remove-hook 'after-change-functions 'refill-after-change-function t)
254 (remove-hook 'post-command-hook 'refill-post-command-function t)
d1a27f1d 255 (kill-local-variable 'backward-delete-char-untabify-method)))
aaaf7be7
DL
256
257(provide 'refill)
258
259;;; refill.el ends here