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