Commit | Line | Data |
---|---|---|
aaaf7be7 DL |
1 | ;;; refill.el --- `auto-fill' by refilling paragraphs on changes |
2 | ||
f2e3589a | 3 | ;; Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, |
114f9c96 | 4 | ;; 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc. |
aaaf7be7 DL |
5 | |
6 | ;; Author: Dave Love <fx@gnu.org> | |
a2515a78 | 7 | ;; Maintainer: Miles Bader <miles@gnu.org> |
aaaf7be7 DL |
8 | ;; Keywords: wp |
9 | ||
2be7dabc GM |
10 | ;; This file is part of GNU Emacs. |
11 | ||
1fecc8fe | 12 | ;; GNU Emacs is free software: you can redistribute it and/or modify |
aaaf7be7 | 13 | ;; it under the terms of the GNU General Public License as published by |
1fecc8fe GM |
14 | ;; the Free Software Foundation, either version 3 of the License, or |
15 | ;; (at your option) any later version. | |
2be7dabc GM |
16 | |
17 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
aaaf7be7 DL |
18 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
19 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
20 | ;; GNU General Public License for more details. | |
2be7dabc | 21 | |
aaaf7be7 | 22 | ;; You should have received a copy of the GNU General Public License |
1fecc8fe | 23 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
aaaf7be7 DL |
24 | |
25 | ;;; Commentary: | |
26 | ||
27 | ;; Provides a mode where paragraphs are refilled after changes in them | |
7850e188 | 28 | ;; (using `after-change-functions'). This gives something akin to typical |
aaaf7be7 DL |
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 | |
7850e188 | 50 | ;; `post-command-hook' ensures simply that refilling only happens |
aaaf7be7 DL |
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 | |
7850e188 | 55 | ;; after-change-functions don't work for this purpose; perhaps there was |
aaaf7be7 DL |
56 | ;; some Emacs bug at the time. ISTR maniac has problems with |
57 | ;; whitespace at the end of paragraphs.] | |
58 | ||
d1a27f1d SM |
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 | ||
aaaf7be7 DL |
85 | ;;; Code: |
86 | ||
04138be8 SM |
87 | (eval-when-compile (require 'cl)) |
88 | ||
e4caa91a MR |
89 | (defgroup refill nil |
90 | "Refilling paragraphs on changes." | |
91 | :group 'fill) | |
92 | ||
867092e9 MB |
93 | (defvar refill-ignorable-overlay nil |
94 | "Portion of the most recently filled paragraph not needing filling. | |
95 | This is used to optimize refilling.") | |
96 | (make-variable-buffer-local 'refill-ignorable-overlay) | |
97 | ||
98 | (defun refill-adjust-ignorable-overlay (overlay afterp beg end &optional len) | |
99 | "Adjust OVERLAY to not include the about-to-be-modified region." | |
100 | (when (not afterp) | |
867092e9 MB |
101 | (save-excursion |
102 | (goto-char beg) | |
103 | (forward-line -1) | |
104 | (if (<= (point) (overlay-start overlay)) | |
105 | ;; Just get OVERLAY out of the way | |
ad2feb08 | 106 | (move-overlay overlay (point-min) (point-min)) |
db95369b | 107 | ;; Make overlay contain only the region |
867092e9 MB |
108 | (move-overlay overlay (overlay-start overlay) (point)))))) |
109 | ||
110 | (defun refill-fill-paragraph-at (pos &optional arg) | |
111 | "Like `fill-paragraph' at POS, but don't delete whitespace at paragraph end." | |
ad2feb08 SM |
112 | (save-excursion |
113 | (goto-char pos) | |
114 | ;; FIXME: forward-paragraph seems to disregard `use-hard-newlines', | |
115 | ;; leading to excessive refilling and wrong choice of fill-prefix. | |
116 | ;; might be a bug in my paragraphs.el. | |
117 | (forward-paragraph) | |
118 | (skip-syntax-backward "-") | |
119 | (let ((end (point)) | |
120 | (beg (progn (backward-paragraph) (point))) | |
121 | (obeg (overlay-start refill-ignorable-overlay)) | |
122 | (oend (overlay-end refill-ignorable-overlay))) | |
123 | (unless (> beg pos) ;Don't fill if point is outside the paragraph. | |
124 | (goto-char pos) | |
125 | (if (and (>= beg obeg) (< beg oend)) | |
126 | ;; Limit filling to the modified tail of the paragraph. | |
127 | (let ( ;; When adaptive-fill-mode is enabled, the filling | |
128 | ;; functions will attempt to set the fill prefix from | |
129 | ;; the fake paragraph bounds we pass in, so set it | |
130 | ;; ourselves first, using the real paragraph bounds. | |
131 | (fill-prefix | |
132 | (if (and adaptive-fill-mode | |
133 | (or (null fill-prefix) (string= fill-prefix ""))) | |
134 | (fill-context-prefix beg end) | |
135 | fill-prefix)) | |
136 | ;; Turn off adaptive-fill-mode temporarily | |
137 | (adaptive-fill-mode nil)) | |
138 | (save-restriction | |
139 | (if use-hard-newlines | |
140 | (fill-region oend end arg) | |
141 | (fill-region-as-paragraph oend end arg))) | |
142 | (move-overlay refill-ignorable-overlay obeg (point))) | |
143 | ;; Fill the whole paragraph | |
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))))))) | |
867092e9 MB |
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)) | |
aaaf7be7 DL |
153 | |
154 | (defvar refill-doit nil | |
8b4703c4 | 155 | "Non-nil tells `refill-post-command-function' to do its processing. |
7850e188 | 156 | Set by `refill-after-change-function' in `after-change-functions' and |
867092e9 MB |
157 | unset by `refill-post-command-function' in `post-command-hook', and |
158 | sometimes `refill-pre-command-function' in `pre-command-hook'. This | |
aaaf7be7 DL |
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 | |
867092e9 | 167 | (setq refill-doit end))) |
aaaf7be7 DL |
168 | |
169 | (defun refill-post-command-function () | |
170 | "Post-command function to do refilling (conditionally)." | |
867092e9 | 171 | (when refill-doit ; there was a change |
aaaf7be7 | 172 | ;; There's probably scope for more special cases here... |
04138be8 SM |
173 | (case this-command |
174 | (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 | ((quoted-insert fill-paragraph fill-region) nil) | |
185 | ((newline newline-and-indent open-line indent-new-comment-line | |
186 | reindent-then-newline-and-indent) | |
187 | ;; Don't zap what was just inserted. | |
188 | (save-excursion | |
189 | (beginning-of-line) ; for newline-and-indent | |
190 | (skip-chars-backward "\n") | |
191 | (save-restriction | |
192 | (narrow-to-region (point-min) (point)) | |
193 | (refill-fill-paragraph-at refill-doit))) | |
194 | (widen) | |
195 | (save-excursion | |
196 | (skip-chars-forward "\n") | |
197 | (save-restriction | |
198 | (narrow-to-region (line-beginning-position) (point-max)) | |
199 | (refill-fill-paragraph-at refill-doit)))) | |
200 | (t | |
201 | (refill-fill-paragraph-at refill-doit))) | |
202 | (setq refill-doit nil))) | |
867092e9 MB |
203 | |
204 | (defun refill-pre-command-function () | |
205 | "Pre-command function to do refilling (conditionally)." | |
206 | (when (and refill-doit (not (eq this-command 'self-insert-command))) | |
207 | ;; A previous setting of `refill-doit' didn't result in a refill, | |
208 | ;; because it was a self-insert-command. Since the next command is | |
209 | ;; something else, do the refill now. | |
210 | (refill-fill-paragraph-at refill-doit) | |
aaaf7be7 DL |
211 | (setq refill-doit nil))) |
212 | ||
04138be8 | 213 | (defvar refill-saved-state nil) |
10ee3d79 | 214 | |
aaaf7be7 DL |
215 | ;;;###autoload |
216 | (define-minor-mode refill-mode | |
217 | "Toggle Refill minor mode. | |
3ecd3a56 | 218 | With prefix arg, turn Refill mode on if arg is positive, otherwise turn it off. |
aaaf7be7 DL |
219 | |
220 | When Refill mode is on, the current paragraph will be formatted when | |
221 | changes are made within it. Self-inserting characters only cause | |
222 | refilling if they would cause auto-filling." | |
6de274aa LK |
223 | :group 'refill |
224 | :lighter " Refill" | |
225 | :keymap '(("\177" . backward-delete-char-untabify)) | |
f5bd4bf2 MB |
226 | ;; Remove old state if necessary |
227 | (when refill-ignorable-overlay | |
228 | (delete-overlay refill-ignorable-overlay) | |
229 | (kill-local-variable 'refill-ignorable-overlay)) | |
04138be8 SM |
230 | (when (local-variable-p 'refill-saved-state) |
231 | (dolist (x refill-saved-state) | |
232 | (set (make-local-variable (car x)) (cdr x))) | |
233 | (kill-local-variable 'refill-saved-state)) | |
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) |
04138be8 SM |
239 | (set (make-local-variable 'refill-saved-state) |
240 | (mapcar (lambda (s) (cons s (symbol-value s))) | |
241 | '(fill-paragraph-function auto-fill-function))) | |
f5bd4bf2 | 242 | ;; This provides the test for recursive paragraph filling. |
7850e188 SM |
243 | (set (make-local-variable 'fill-paragraph-function) |
244 | 'refill-fill-paragraph) | |
d1a27f1d SM |
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) | |
867092e9 MB |
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)) | |
7850e188 | 254 | (auto-fill-mode 0)) |
aaaf7be7 DL |
255 | (remove-hook 'after-change-functions 'refill-after-change-function t) |
256 | (remove-hook 'post-command-hook 'refill-post-command-function t) | |
d1a27f1d | 257 | (kill-local-variable 'backward-delete-char-untabify-method))) |
aaaf7be7 DL |
258 | |
259 | (provide 'refill) | |
260 | ||
04138be8 | 261 | ;; arch-tag: 2c4ce9e8-1daa-4a3b-b6f8-fd6ac5bf6138 |
aaaf7be7 | 262 | ;;; refill.el ends here |