32750
|
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
|
38401
|
8 ;; This file is part of GNU Emacs.
|
|
9
|
|
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
|
32750
|
11 ;; it under the terms of the GNU General Public License as published by
|
38401
|
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,
|
32750
|
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.
|
38401
|
19
|
32750
|
20 ;; You should have received a copy of the GNU General Public License
|
38401
|
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
|
|
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
32750
|
23 ;; Boston, MA 02111-1307, USA.
|
|
24
|
|
25 ;;; Commentary:
|
|
26
|
|
27 ;; Provides a mode where paragraphs are refilled after changes in them
|
32761
|
28 ;; (using `after-change-functions'). This gives something akin to typical
|
32750
|
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
|
32761
|
50 ;; `post-command-hook' ensures simply that refilling only happens
|
32750
|
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
|
32761
|
55 ;; after-change-functions don't work for this purpose; perhaps there was
|
32750
|
56 ;; some Emacs bug at the time. ISTR maniac has problems with
|
|
57 ;; whitespace at the end of paragraphs.]
|
|
58
|
39894
b27f7112ce6f
(refill-mode): Bind DEL to backward-delete-char-untabify.
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
59 ;;; Todo/Bugs:
|
b27f7112ce6f
(refill-mode): Bind DEL to backward-delete-char-untabify.
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
60
|
b27f7112ce6f
(refill-mode): Bind DEL to backward-delete-char-untabify.
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
61 ;; - When deleting the first word on a line, the space after that word tends
|
b27f7112ce6f
(refill-mode): Bind DEL to backward-delete-char-untabify.
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
62 ;; to become part of the fill-prefix, causing either wrong filling of the
|
b27f7112ce6f
(refill-mode): Bind DEL to backward-delete-char-untabify.
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
63 ;; remaining text, or causing the cursor to move unexpectedly. Ex:
|
b27f7112ce6f
(refill-mode): Bind DEL to backward-delete-char-untabify.
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
64 ;; Start with
|
b27f7112ce6f
(refill-mode): Bind DEL to backward-delete-char-untabify.
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
65 ;; I>< blabla
|
b27f7112ce6f
(refill-mode): Bind DEL to backward-delete-char-untabify.
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
66 ;;
|
b27f7112ce6f
(refill-mode): Bind DEL to backward-delete-char-untabify.
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
67 ;; and hit backspace. We end up with
|
b27f7112ce6f
(refill-mode): Bind DEL to backward-delete-char-untabify.
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
68 ;;
|
b27f7112ce6f
(refill-mode): Bind DEL to backward-delete-char-untabify.
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
69 ;; ><blabla
|
b27f7112ce6f
(refill-mode): Bind DEL to backward-delete-char-untabify.
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
70 ;; instead of
|
b27f7112ce6f
(refill-mode): Bind DEL to backward-delete-char-untabify.
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
71 ;; >< blabla
|
b27f7112ce6f
(refill-mode): Bind DEL to backward-delete-char-untabify.
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
72 ;;
|
b27f7112ce6f
(refill-mode): Bind DEL to backward-delete-char-untabify.
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
73 ;; Other example. Start with
|
b27f7112ce6f
(refill-mode): Bind DEL to backward-delete-char-untabify.
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
74 ;;
|
b27f7112ce6f
(refill-mode): Bind DEL to backward-delete-char-untabify.
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
75 ;; Foo bar blablabla asdgf
|
b27f7112ce6f
(refill-mode): Bind DEL to backward-delete-char-untabify.
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
76 ;; word>< asdfas dfasdfasd
|
b27f7112ce6f
(refill-mode): Bind DEL to backward-delete-char-untabify.
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
77 ;; asd asdfa sdfasd sdf
|
b27f7112ce6f
(refill-mode): Bind DEL to backward-delete-char-untabify.
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
78 ;;
|
b27f7112ce6f
(refill-mode): Bind DEL to backward-delete-char-untabify.
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
79 ;; and hit M-backspace. We end up with
|
b27f7112ce6f
(refill-mode): Bind DEL to backward-delete-char-untabify.
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
80 ;;
|
b27f7112ce6f
(refill-mode): Bind DEL to backward-delete-char-untabify.
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
81 ;; Foo bar blablabla asdgf
|
b27f7112ce6f
(refill-mode): Bind DEL to backward-delete-char-untabify.
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
82 ;; ><asdfas dfasdfasd asd
|
b27f7112ce6f
(refill-mode): Bind DEL to backward-delete-char-untabify.
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
83 ;; asdfa sdfasd sdf
|
b27f7112ce6f
(refill-mode): Bind DEL to backward-delete-char-untabify.
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
84
|
32750
|
85 ;;; Code:
|
|
86
|
48415
|
87 (defgroup refill nil
|
|
88 "Refilling paragraphs on changes."
|
|
89 :group 'fill)
|
|
90
|
33660
|
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)
|
32750
|
99 (save-excursion
|
33660
|
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)
|
39894
b27f7112ce6f
(refill-mode): Bind DEL to backward-delete-char-untabify.
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
113 ;; FIXME: forward-paragraph seems to disregard `use-hard-newlines',
|
b27f7112ce6f
(refill-mode): Bind DEL to backward-delete-char-untabify.
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
114 ;; leading to excessive refilling and wrong choice of fill-prefix.
|
b27f7112ce6f
(refill-mode): Bind DEL to backward-delete-char-untabify.
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
115 ;; might be a bug in my paragraphs.el.
|
32750
|
116 (forward-paragraph)
|
|
117 (skip-syntax-backward "-")
|
|
118 (let ((end (point))
|
33660
|
119 (beg (progn (backward-paragraph) (point)))
|
|
120 (obeg (overlay-start refill-ignorable-overlay))
|
|
121 (oend (overlay-end refill-ignorable-overlay)))
|
|
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 (setq fill-pfx fill-prefix)
|
|
141 (move-overlay refill-ignorable-overlay obeg (point)))
|
|
142 ;; Fill the whole paragraph
|
|
143 (setq fill-pfx
|
|
144 (save-restriction
|
|
145 (if use-hard-newlines
|
|
146 (fill-region beg end arg)
|
|
147 (fill-region-as-paragraph beg end arg))))
|
|
148 (move-overlay refill-ignorable-overlay beg (point)))))
|
|
149 (skip-line-prefix fill-pfx)))
|
|
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))
|
32750
|
154
|
|
155 (defvar refill-doit nil
|
|
156 "Non-nil means that `refill-post-command-function' does its processing.
|
32761
|
157 Set by `refill-after-change-function' in `after-change-functions' and
|
33660
|
158 unset by `refill-post-command-function' in `post-command-hook', and
|
|
159 sometimes `refill-pre-command-function' in `pre-command-hook'. This
|
32750
|
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
|
33660
|
168 (setq refill-doit end)))
|
32750
|
169
|
|
170 (defun refill-post-command-function ()
|
|
171 "Post-command function to do refilling (conditionally)."
|
33660
|
172 (when refill-doit ; there was a change
|
32750
|
173 ;; There's probably scope for more special cases here...
|
33660
|
174 (if (eq this-command 'self-insert-command)
|
|
175 ;; Treat self-insertion commands specially, since they don't
|
|
176 ;; always reset `refill-doit' -- for self-insertion commands that
|
|
177 ;; *don't* cause a refill, we want to leave it turned on so that
|
|
178 ;; any subsequent non-modification command will cause a refill.
|
|
179 (when (aref auto-fill-chars (char-before))
|
|
180 ;; Respond to the same characters as auto-fill (other than
|
|
181 ;; newline, covered below).
|
|
182 (refill-fill-paragraph-at refill-doit)
|
|
183 (setq refill-doit nil))
|
|
184 (cond
|
|
185 ((or (eq this-command 'quoted-insert)
|
|
186 (eq this-command 'fill-paragraph)
|
|
187 (eq this-command 'fill-region))
|
|
188 nil)
|
|
189 ((or (eq this-command 'newline)
|
|
190 (eq this-command 'newline-and-indent)
|
|
191 (eq this-command 'open-line))
|
|
192 ;; Don't zap what was just inserted.
|
|
193 (save-excursion
|
|
194 (beginning-of-line) ; for newline-and-indent
|
|
195 (skip-chars-backward "\n")
|
|
196 (save-restriction
|
|
197 (narrow-to-region (point-min) (point))
|
|
198 (refill-fill-paragraph-at refill-doit)))
|
|
199 (widen)
|
|
200 (save-excursion
|
|
201 (skip-chars-forward "\n")
|
|
202 (save-restriction
|
|
203 (narrow-to-region (line-beginning-position) (point-max))
|
|
204 (refill-fill-paragraph-at refill-doit))))
|
|
205 (t
|
|
206 (refill-fill-paragraph-at refill-doit)))
|
|
207 (setq refill-doit nil))))
|
|
208
|
|
209 (defun refill-pre-command-function ()
|
|
210 "Pre-command function to do refilling (conditionally)."
|
|
211 (when (and refill-doit (not (eq this-command 'self-insert-command)))
|
|
212 ;; A previous setting of `refill-doit' didn't result in a refill,
|
|
213 ;; because it was a self-insert-command. Since the next command is
|
|
214 ;; something else, do the refill now.
|
|
215 (refill-fill-paragraph-at refill-doit)
|
32750
|
216 (setq refill-doit nil)))
|
|
217
|
32960
|
218 (defvar refill-late-fill-paragraph-function nil)
|
|
219
|
32750
|
220 ;;;###autoload
|
|
221 (define-minor-mode refill-mode
|
|
222 "Toggle Refill minor mode.
|
|
223 With prefix arg, turn Refill mode on iff arg is positive.
|
|
224
|
|
225 When Refill mode is on, the current paragraph will be formatted when
|
|
226 changes are made within it. Self-inserting characters only cause
|
|
227 refilling if they would cause auto-filling."
|
39894
b27f7112ce6f
(refill-mode): Bind DEL to backward-delete-char-untabify.
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
228 nil " Refill" '(("\177" . backward-delete-char-untabify))
|
42263
|
229 ;; Remove old state if necessary
|
|
230 (when refill-ignorable-overlay
|
|
231 (delete-overlay refill-ignorable-overlay)
|
|
232 (kill-local-variable 'refill-ignorable-overlay))
|
|
233 (when refill-late-fill-paragraph-function
|
|
234 (setq fill-paragraph-function refill-late-fill-paragraph-function)
|
|
235 (kill-local-variable 'refill-late-fill-paragraph-function))
|
32750
|
236 (if refill-mode
|
32761
|
237 (progn
|
|
238 (add-hook 'after-change-functions 'refill-after-change-function nil t)
|
|
239 (add-hook 'post-command-hook 'refill-post-command-function nil t)
|
33660
|
240 (add-hook 'pre-command-hook 'refill-pre-command-function nil t)
|
32960
|
241 (set (make-local-variable 'refill-late-fill-paragraph-function)
|
|
242 fill-paragraph-function)
|
42263
|
243 ;; This provides the test for recursive paragraph filling.
|
32761
|
244 (set (make-local-variable 'fill-paragraph-function)
|
|
245 'refill-fill-paragraph)
|
39894
b27f7112ce6f
(refill-mode): Bind DEL to backward-delete-char-untabify.
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
246 ;; When using justification, doing DEL on 2 spaces should remove
|
b27f7112ce6f
(refill-mode): Bind DEL to backward-delete-char-untabify.
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
247 ;; both, otherwise, the subsequent refill will undo the DEL.
|
b27f7112ce6f
(refill-mode): Bind DEL to backward-delete-char-untabify.
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
248 (set (make-local-variable 'backward-delete-char-untabify-method)
|
b27f7112ce6f
(refill-mode): Bind DEL to backward-delete-char-untabify.
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
249 'hungry)
|
33660
|
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))
|
32761
|
255 (auto-fill-mode 0))
|
32750
|
256 (remove-hook 'after-change-functions 'refill-after-change-function t)
|
|
257 (remove-hook 'post-command-hook 'refill-post-command-function t)
|
39894
b27f7112ce6f
(refill-mode): Bind DEL to backward-delete-char-untabify.
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
258 (kill-local-variable 'backward-delete-char-untabify-method)))
|
32750
|
259
|
|
260 (provide 'refill)
|
|
261
|
|
262 ;;; refill.el ends here
|