(texinfo-section-types-regexp)
[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 ;;; 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
85 ;;; Code:
86
87 (defvar refill-ignorable-overlay nil
88 "Portion of the most recently filled paragraph not needing filling.
89 This is used to optimize refilling.")
90 (make-variable-buffer-local 'refill-ignorable-overlay)
91
92 (defun refill-adjust-ignorable-overlay (overlay afterp beg end &optional len)
93 "Adjust OVERLAY to not include the about-to-be-modified region."
94 (when (not afterp)
95 (save-excursion
96 (goto-char beg)
97 (forward-line -1)
98 (if (<= (point) (overlay-start overlay))
99 ;; Just get OVERLAY out of the way
100 (move-overlay overlay 1 1)
101 ;; Make overlay contain only the region
102 (move-overlay overlay (overlay-start overlay) (point))))))
103
104 (defun refill-fill-paragraph-at (pos &optional arg)
105 "Like `fill-paragraph' at POS, but don't delete whitespace at paragraph end."
106 (let (fill-pfx)
107 (save-excursion
108 (goto-char pos)
109 ;; FIXME: forward-paragraph seems to disregard `use-hard-newlines',
110 ;; leading to excessive refilling and wrong choice of fill-prefix.
111 ;; might be a bug in my paragraphs.el.
112 (forward-paragraph)
113 (skip-syntax-backward "-")
114 (let ((end (point))
115 (beg (progn (backward-paragraph) (point)))
116 (obeg (overlay-start refill-ignorable-overlay))
117 (oend (overlay-end refill-ignorable-overlay)))
118 (goto-char pos)
119 (if (and (>= beg obeg) (< beg oend))
120 ;; Limit filling to the modified tail of the paragraph.
121 (let (;; When adaptive-fill-mode is enabled, the filling
122 ;; functions will attempt to set the fill prefix from
123 ;; the fake paragraph bounds we pass in, so set it
124 ;; ourselves first, using the real paragraph bounds.
125 (fill-prefix
126 (if (and adaptive-fill-mode
127 (or (null fill-prefix) (string= fill-prefix "")))
128 (fill-context-prefix beg end)
129 fill-prefix))
130 ;; Turn off adaptive-fill-mode temporarily
131 (adaptive-fill-mode nil))
132 (save-restriction
133 (if use-hard-newlines
134 (fill-region oend end arg)
135 (fill-region-as-paragraph oend end arg)))
136 (setq fill-pfx fill-prefix)
137 (move-overlay refill-ignorable-overlay obeg (point)))
138 ;; Fill the whole paragraph
139 (setq fill-pfx
140 (save-restriction
141 (if use-hard-newlines
142 (fill-region beg end arg)
143 (fill-region-as-paragraph beg end arg))))
144 (move-overlay refill-ignorable-overlay beg (point)))))
145 (skip-line-prefix fill-pfx)))
146
147 (defun refill-fill-paragraph (arg)
148 "Like `fill-paragraph' but don't delete whitespace at paragraph end."
149 (refill-fill-paragraph-at (point) arg))
150
151 (defvar refill-doit nil
152 "Non-nil means that `refill-post-command-function' does its processing.
153 Set by `refill-after-change-function' in `after-change-functions' and
154 unset by `refill-post-command-function' in `post-command-hook', and
155 sometimes `refill-pre-command-function' in `pre-command-hook'. This
156 ensures refilling is only done once per command that causes a change,
157 regardless of the number of after-change calls from commands doing
158 complex processing.")
159 (make-variable-buffer-local 'refill-doit)
160
161 (defun refill-after-change-function (beg end len)
162 "Function for `after-change-functions' which just sets `refill-doit'."
163 (unless undo-in-progress
164 (setq refill-doit end)))
165
166 (defun refill-post-command-function ()
167 "Post-command function to do refilling (conditionally)."
168 (when refill-doit ; there was a change
169 ;; There's probably scope for more special cases here...
170 (if (eq this-command 'self-insert-command)
171 ;; Treat self-insertion commands specially, since they don't
172 ;; always reset `refill-doit' -- for self-insertion commands that
173 ;; *don't* cause a refill, we want to leave it turned on so that
174 ;; any subsequent non-modification command will cause a refill.
175 (when (aref auto-fill-chars (char-before))
176 ;; Respond to the same characters as auto-fill (other than
177 ;; newline, covered below).
178 (refill-fill-paragraph-at refill-doit)
179 (setq refill-doit nil))
180 (cond
181 ((or (eq this-command 'quoted-insert)
182 (eq this-command 'fill-paragraph)
183 (eq this-command 'fill-region))
184 nil)
185 ((or (eq this-command 'newline)
186 (eq this-command 'newline-and-indent)
187 (eq this-command 'open-line))
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-late-fill-paragraph-function 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 nil " Refill" '(("\177" . backward-delete-char-untabify))
225 ;; This provides the test for recursive paragraph filling.
226 (make-local-variable 'fill-paragraph-function)
227 (if refill-mode
228 (progn
229 (add-hook 'after-change-functions 'refill-after-change-function nil t)
230 (add-hook 'post-command-hook 'refill-post-command-function nil t)
231 (add-hook 'pre-command-hook 'refill-pre-command-function nil t)
232 (set (make-local-variable 'refill-late-fill-paragraph-function)
233 fill-paragraph-function)
234 (set (make-local-variable 'fill-paragraph-function)
235 'refill-fill-paragraph)
236 ;; When using justification, doing DEL on 2 spaces should remove
237 ;; both, otherwise, the subsequent refill will undo the DEL.
238 (set (make-local-variable 'backward-delete-char-untabify-method)
239 'hungry)
240 (setq refill-ignorable-overlay (make-overlay 1 1 nil nil t))
241 (overlay-put refill-ignorable-overlay 'modification-hooks
242 '(refill-adjust-ignorable-overlay))
243 (overlay-put refill-ignorable-overlay 'insert-behind-hooks
244 '(refill-adjust-ignorable-overlay))
245 (auto-fill-mode 0))
246 (remove-hook 'after-change-functions 'refill-after-change-function t)
247 (remove-hook 'post-command-hook 'refill-post-command-function t)
248 (delete-overlay refill-ignorable-overlay)
249 (setq fill-paragraph-function refill-late-fill-paragraph-function)
250 (kill-local-variable 'backward-delete-char-untabify-method)))
251
252 (provide 'refill)
253
254 ;;; refill.el ends here