1 ;;; refill.el --- `auto-fill' by refilling paragraphs on changes
3 ;; Copyright (C) 2000-2013 Free Software Foundation, Inc.
5 ;; Author: Dave Love <fx@gnu.org>
6 ;; Maintainer: Miles Bader <miles@gnu.org>
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
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.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
26 ;; Provides a mode where paragraphs are refilled after changes in them
27 ;; (using `after-change-functions'). This gives something akin to typical
28 ;; word processor-style filling. We restrict refilling due to
29 ;; self-insertion to the characters which trigger auto-fill.
31 ;; It partly satisfies a todo item in enriched.el for some value of
32 ;; `without slowing down editing too much'. It doesn't attempt to do
33 ;; anything (using `window-size-change-functions'?) about resizing
34 ;; windows -- who cares?
36 ;; This implementation is probably fragile and missing some special
37 ;; cases -- not extensively tested. Yanking paragraph breaks, for
38 ;; instance, won't DTRT by refilling all the relevant paragraphs.
40 ;; You could do it a bit more efficiently (and robustly?) with just an
41 ;; auto-fill function, but that doesn't cope with changes other than
42 ;; through self-insertion. (Using auto-fill and after-change
43 ;; functions together didn't seem winning.) This could probably
44 ;; benefit from a less-general and faster `fill-paragraph-function',
45 ;; ideally as a primitive.
47 ;; The work is done in a local post-command hook but only if
48 ;; `refill-doit' has been set by the after-change function. Using
49 ;; `post-command-hook' ensures simply that refilling only happens
52 ;; [Per Abrahamsen's maniac.el does a similar thing, but operates from
53 ;; post-command-hook. I don't understand the statement in it that
54 ;; after-change-functions don't work for this purpose; perhaps there was
55 ;; some Emacs bug at the time. ISTR maniac has problems with
56 ;; whitespace at the end of paragraphs.]
60 ;; - When deleting the first word on a line, the space after that word tends
61 ;; to become part of the fill-prefix, causing either wrong filling of the
62 ;; remaining text, or causing the cursor to move unexpectedly. Ex:
66 ;; and hit backspace. We end up with
72 ;; Other example. Start with
74 ;; Foo bar blablabla asdgf
75 ;; word>< asdfas dfasdfasd
76 ;; asd asdfa sdfasd sdf
78 ;; and hit M-backspace. We end up with
80 ;; Foo bar blablabla asdgf
81 ;; ><asdfas dfasdfasd asd
87 ;;; (defgroup refill nil
88 ;;; "Refilling paragraphs on changes."
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
)
96 (defun refill-adjust-ignorable-overlay (overlay afterp beg end
&optional len
)
97 "Adjust OVERLAY to not include the about-to-be-modified region."
102 (if (<= (point) (overlay-start overlay
))
103 ;; Just get OVERLAY out of the way
104 (move-overlay overlay
(point-min) (point-min))
105 ;; Make overlay contain only the region
106 (move-overlay overlay
(overlay-start overlay
) (point))))))
108 (defun refill-fill-paragraph-at (pos &optional arg
)
109 "Like `fill-paragraph' at POS, but don't delete whitespace at paragraph end."
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.
116 (skip-syntax-backward "-")
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.
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.
130 (if (and adaptive-fill-mode
131 (or (null fill-prefix
) (string= fill-prefix
"")))
132 (fill-context-prefix beg end
)
134 ;; Turn off adaptive-fill-mode temporarily
135 (adaptive-fill-mode nil
))
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
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)))))))
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
))
152 (defvar refill-doit nil
153 "Non-nil tells `refill-post-command-function' to do its processing.
154 Set by `refill-after-change-function' in `after-change-functions' and
155 unset by `refill-post-command-function' in `post-command-hook', and
156 sometimes `refill-pre-command-function' in `pre-command-hook'. This
157 ensures refilling is only done once per command that causes a change,
158 regardless of the number of after-change calls from commands doing
159 complex processing.")
160 (make-variable-buffer-local 'refill-doit
)
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
165 (setq refill-doit end
)))
167 (defun refill-post-command-function ()
168 "Post-command function to do refilling (conditionally)."
169 (when refill-doit
; there was a change
170 ;; There's probably scope for more special cases here...
172 (`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 ((or `quoted-insert
`fill-paragraph
`fill-region
) nil
)
183 ((or `newline
`newline-and-indent
`open-line
`indent-new-comment-line
184 `reindent-then-newline-and-indent
)
185 ;; Don't zap what was just inserted.
187 (beginning-of-line) ; for newline-and-indent
188 (skip-chars-backward "\n")
190 (narrow-to-region (point-min) (point))
191 (refill-fill-paragraph-at refill-doit
)))
194 (skip-chars-forward "\n")
196 (narrow-to-region (line-beginning-position) (point-max))
197 (refill-fill-paragraph-at refill-doit
))))
199 (refill-fill-paragraph-at refill-doit
)))
200 (setq refill-doit nil
)))
202 (defun refill-pre-command-function ()
203 "Pre-command function to do refilling (conditionally)."
204 (when (and refill-doit
(not (eq this-command
'self-insert-command
)))
205 ;; A previous setting of `refill-doit' didn't result in a refill,
206 ;; because it was a self-insert-command. Since the next command is
207 ;; something else, do the refill now.
208 (refill-fill-paragraph-at refill-doit
)
209 (setq refill-doit nil
)))
211 (defvar refill-saved-state nil
)
214 (define-minor-mode refill-mode
215 "Toggle automatic refilling (Refill mode).
216 With a prefix argument ARG, enable Refill mode if ARG is
217 positive, and disable it otherwise. If called from Lisp, enable
218 the mode if ARG is omitted or nil.
220 Refill mode is a buffer-local minor mode. When enabled, the
221 current paragraph is refilled as you edit. Self-inserting
222 characters only cause refilling if they would cause
225 For true \"word wrap\" behavior, use `visual-line-mode' instead."
226 ;; Not global, so no effect.
229 :keymap
'(("\177" . backward-delete-char-untabify
))
230 ;; Remove old state if necessary
231 (when refill-ignorable-overlay
232 (delete-overlay refill-ignorable-overlay
)
233 (kill-local-variable 'refill-ignorable-overlay
))
234 (when (local-variable-p 'refill-saved-state
)
235 (dolist (x refill-saved-state
)
236 (set (make-local-variable (car x
)) (cdr x
)))
237 (kill-local-variable 'refill-saved-state
))
240 (add-hook 'after-change-functions
'refill-after-change-function nil t
)
241 (add-hook 'post-command-hook
'refill-post-command-function nil t
)
242 (add-hook 'pre-command-hook
'refill-pre-command-function nil t
)
243 (set (make-local-variable 'refill-saved-state
)
244 (mapcar (lambda (s) (cons s
(symbol-value s
)))
245 '(fill-paragraph-function auto-fill-function
)))
246 ;; This provides the test for recursive paragraph filling.
247 (set (make-local-variable 'fill-paragraph-function
)
248 'refill-fill-paragraph
)
249 ;; When using justification, doing DEL on 2 spaces should remove
250 ;; both, otherwise, the subsequent refill will undo the DEL.
251 (set (make-local-variable 'backward-delete-char-untabify-method
)
253 (setq refill-ignorable-overlay
(make-overlay 1 1 nil nil t
))
254 (overlay-put refill-ignorable-overlay
'modification-hooks
255 '(refill-adjust-ignorable-overlay))
256 (overlay-put refill-ignorable-overlay
'insert-behind-hooks
257 '(refill-adjust-ignorable-overlay))
259 (remove-hook 'after-change-functions
'refill-after-change-function t
)
260 (remove-hook 'post-command-hook
'refill-post-command-function t
)
261 (kill-local-variable 'backward-delete-char-untabify-method
)))
265 ;;; refill.el ends here