]> code.delx.au - gnu-emacs/blob - lisp/textmodes/refill.el
(refill-fill-paragraph-at): Don't preserve the trailing space since
[gnu-emacs] / 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 (defgroup refill nil
88 "Refilling paragraphs on changes."
89 :group 'fill)
90
91 (defvar refill-ignorable-overlay nil
92 "Portion of the most recently filled paragraph not needing filling.
93 This 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)
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
104 (move-overlay overlay 1 1)
105 ;; Make overlay contain only the region
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."
110 (let (fill-pfx)
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 (let ((end (point))
118 (beg (progn (backward-paragraph) (point)))
119 (obeg (overlay-start refill-ignorable-overlay))
120 (oend (overlay-end refill-ignorable-overlay)))
121 (goto-char pos)
122 (if (and (>= beg obeg) (< beg oend))
123 ;; Limit filling to the modified tail of the paragraph.
124 (let (;; When adaptive-fill-mode is enabled, the filling
125 ;; functions will attempt to set the fill prefix from
126 ;; the fake paragraph bounds we pass in, so set it
127 ;; ourselves first, using the real paragraph bounds.
128 (fill-prefix
129 (if (and adaptive-fill-mode
130 (or (null fill-prefix) (string= fill-prefix "")))
131 (fill-context-prefix beg end)
132 fill-prefix))
133 ;; Turn off adaptive-fill-mode temporarily
134 (adaptive-fill-mode nil))
135 (save-restriction
136 (if use-hard-newlines
137 (fill-region oend end arg)
138 (fill-region-as-paragraph oend end arg)))
139 (setq fill-pfx fill-prefix)
140 (move-overlay refill-ignorable-overlay obeg (point)))
141 ;; Fill the whole paragraph
142 (setq fill-pfx
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)))))
148 (skip-line-prefix fill-pfx)))
149
150 (defun refill-fill-paragraph (arg)
151 "Like `fill-paragraph' but don't delete whitespace at paragraph end."
152 (refill-fill-paragraph-at (point) arg))
153
154 (defvar refill-doit nil
155 "Non-nil means that `refill-post-command-function' does its processing.
156 Set by `refill-after-change-function' in `after-change-functions' and
157 unset by `refill-post-command-function' in `post-command-hook', and
158 sometimes `refill-pre-command-function' in `pre-command-hook'. This
159 ensures refilling is only done once per command that causes a change,
160 regardless of the number of after-change calls from commands doing
161 complex processing.")
162 (make-variable-buffer-local 'refill-doit)
163
164 (defun refill-after-change-function (beg end len)
165 "Function for `after-change-functions' which just sets `refill-doit'."
166 (unless undo-in-progress
167 (setq refill-doit end)))
168
169 (defun refill-post-command-function ()
170 "Post-command function to do refilling (conditionally)."
171 (when refill-doit ; there was a change
172 ;; There's probably scope for more special cases here...
173 (if (eq this-command 'self-insert-command)
174 ;; Treat self-insertion commands specially, since they don't
175 ;; always reset `refill-doit' -- for self-insertion commands that
176 ;; *don't* cause a refill, we want to leave it turned on so that
177 ;; any subsequent non-modification command will cause a refill.
178 (when (aref auto-fill-chars (char-before))
179 ;; Respond to the same characters as auto-fill (other than
180 ;; newline, covered below).
181 (refill-fill-paragraph-at refill-doit)
182 (setq refill-doit nil))
183 (cond
184 ((or (eq this-command 'quoted-insert)
185 (eq this-command 'fill-paragraph)
186 (eq this-command 'fill-region))
187 nil)
188 ((or (eq this-command 'newline)
189 (eq this-command 'newline-and-indent)
190 (eq this-command 'open-line))
191 ;; Don't zap what was just inserted.
192 (save-excursion
193 (beginning-of-line) ; for newline-and-indent
194 (skip-chars-backward "\n")
195 (save-restriction
196 (narrow-to-region (point-min) (point))
197 (refill-fill-paragraph-at refill-doit)))
198 (widen)
199 (save-excursion
200 (skip-chars-forward "\n")
201 (save-restriction
202 (narrow-to-region (line-beginning-position) (point-max))
203 (refill-fill-paragraph-at refill-doit))))
204 (t
205 (refill-fill-paragraph-at refill-doit)))
206 (setq refill-doit nil))))
207
208 (defun refill-pre-command-function ()
209 "Pre-command function to do refilling (conditionally)."
210 (when (and refill-doit (not (eq this-command 'self-insert-command)))
211 ;; A previous setting of `refill-doit' didn't result in a refill,
212 ;; because it was a self-insert-command. Since the next command is
213 ;; something else, do the refill now.
214 (refill-fill-paragraph-at refill-doit)
215 (setq refill-doit nil)))
216
217 (defvar refill-late-fill-paragraph-function nil)
218
219 ;;;###autoload
220 (define-minor-mode refill-mode
221 "Toggle Refill minor mode.
222 With prefix arg, turn Refill mode on iff arg is positive.
223
224 When Refill mode is on, the current paragraph will be formatted when
225 changes are made within it. Self-inserting characters only cause
226 refilling if they would cause auto-filling."
227 nil " Refill" '(("\177" . backward-delete-char-untabify))
228 ;; Remove old state if necessary
229 (when refill-ignorable-overlay
230 (delete-overlay refill-ignorable-overlay)
231 (kill-local-variable 'refill-ignorable-overlay))
232 (when (local-variable-p 'refill-late-fill-paragraph-function)
233 (setq fill-paragraph-function refill-late-fill-paragraph-function)
234 (kill-local-variable 'refill-late-fill-paragraph-function))
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-late-fill-paragraph-function)
241 fill-paragraph-function)
242 ;; This provides the test for recursive paragraph filling.
243 (set (make-local-variable 'fill-paragraph-function)
244 'refill-fill-paragraph)
245 ;; When using justification, doing DEL on 2 spaces should remove
246 ;; both, otherwise, the subsequent refill will undo the DEL.
247 (set (make-local-variable 'backward-delete-char-untabify-method)
248 'hungry)
249 (setq refill-ignorable-overlay (make-overlay 1 1 nil nil t))
250 (overlay-put refill-ignorable-overlay 'modification-hooks
251 '(refill-adjust-ignorable-overlay))
252 (overlay-put refill-ignorable-overlay 'insert-behind-hooks
253 '(refill-adjust-ignorable-overlay))
254 (auto-fill-mode 0))
255 (remove-hook 'after-change-functions 'refill-after-change-function t)
256 (remove-hook 'post-command-hook 'refill-post-command-function t)
257 (kill-local-variable 'backward-delete-char-untabify-method)))
258
259 (provide 'refill)
260
261 ;;; refill.el ends here