* textmodes/bibtex.el (bibtex-include-OPTkey)
[bpt/emacs.git] / lisp / textmodes / refill.el
CommitLineData
aaaf7be7
DL
1;;; refill.el --- `auto-fill' by refilling paragraphs on changes
2
ceb4c4d3
TTN
3;; Copyright (C) 2000, 2002, 2003, 2004, 2005,
4;; 2006 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
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
2be7dabc
GM
14;; the Free Software Foundation; either version 2, or (at your option)
15;; any later version.
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
2be7dabc 23;; along with GNU Emacs; see the file COPYING. If not, write to the
4fc5845f
LK
24;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25;; Boston, MA 02110-1301, USA.
aaaf7be7
DL
26
27;;; Commentary:
28
29;; Provides a mode where paragraphs are refilled after changes in them
7850e188 30;; (using `after-change-functions'). This gives something akin to typical
aaaf7be7
DL
31;; word processor-style filling. We restrict refilling due to
32;; self-insertion to the characters which trigger auto-fill.
33
34;; It partly satisfies a todo item in enriched.el for some value of
35;; `without slowing down editing too much'. It doesn't attempt to do
36;; anything (using `window-size-change-functions'?) about resizing
37;; windows -- who cares?
38
39;; This implementation is probably fragile and missing some special
40;; cases -- not extensively tested. Yanking paragraph breaks, for
41;; instance, won't DTRT by refilling all the relevant paragraphs.
42
43;; You could do it a bit more efficiently (and robustly?) with just an
44;; auto-fill function, but that doesn't cope with changes other than
45;; through self-insertion. (Using auto-fill and after-change
46;; functions together didn't seem winning.) This could probably
47;; benefit from a less-general and faster `fill-paragraph-function',
48;; ideally as a primitive.
49
50;; The work is done in a local post-command hook but only if
51;; `refill-doit' has been set by the after-change function. Using
7850e188 52;; `post-command-hook' ensures simply that refilling only happens
aaaf7be7
DL
53;; once per command.
54
55;; [Per Abrahamsen's maniac.el does a similar thing, but operates from
56;; post-command-hook. I don't understand the statement in it that
7850e188 57;; after-change-functions don't work for this purpose; perhaps there was
aaaf7be7
DL
58;; some Emacs bug at the time. ISTR maniac has problems with
59;; whitespace at the end of paragraphs.]
60
d1a27f1d
SM
61;;; Todo/Bugs:
62
63;; - When deleting the first word on a line, the space after that word tends
64;; to become part of the fill-prefix, causing either wrong filling of the
65;; remaining text, or causing the cursor to move unexpectedly. Ex:
66;; Start with
67;; I>< blabla
68;;
69;; and hit backspace. We end up with
70;;
71;; ><blabla
72;; instead of
73;; >< blabla
74;;
75;; Other example. Start with
76;;
77;; Foo bar blablabla asdgf
78;; word>< asdfas dfasdfasd
79;; asd asdfa sdfasd sdf
80;;
81;; and hit M-backspace. We end up with
82;;
83;; Foo bar blablabla asdgf
84;; ><asdfas dfasdfasd asd
85;; asdfa sdfasd sdf
86
aaaf7be7
DL
87;;; Code:
88
04138be8
SM
89(eval-when-compile (require 'cl))
90
e4caa91a
MR
91(defgroup refill nil
92 "Refilling paragraphs on changes."
93 :group 'fill)
94
867092e9
MB
95(defvar refill-ignorable-overlay nil
96 "Portion of the most recently filled paragraph not needing filling.
97This is used to optimize refilling.")
98(make-variable-buffer-local 'refill-ignorable-overlay)
99
100(defun refill-adjust-ignorable-overlay (overlay afterp beg end &optional len)
101 "Adjust OVERLAY to not include the about-to-be-modified region."
102 (when (not afterp)
867092e9
MB
103 (save-excursion
104 (goto-char beg)
105 (forward-line -1)
106 (if (<= (point) (overlay-start overlay))
107 ;; Just get OVERLAY out of the way
ad2feb08 108 (move-overlay overlay (point-min) (point-min))
db95369b 109 ;; Make overlay contain only the region
867092e9
MB
110 (move-overlay overlay (overlay-start overlay) (point))))))
111
112(defun refill-fill-paragraph-at (pos &optional arg)
113 "Like `fill-paragraph' at POS, but don't delete whitespace at paragraph end."
ad2feb08
SM
114 (save-excursion
115 (goto-char pos)
116 ;; FIXME: forward-paragraph seems to disregard `use-hard-newlines',
117 ;; leading to excessive refilling and wrong choice of fill-prefix.
118 ;; might be a bug in my paragraphs.el.
119 (forward-paragraph)
120 (skip-syntax-backward "-")
121 (let ((end (point))
122 (beg (progn (backward-paragraph) (point)))
123 (obeg (overlay-start refill-ignorable-overlay))
124 (oend (overlay-end refill-ignorable-overlay)))
125 (unless (> beg pos) ;Don't fill if point is outside the paragraph.
126 (goto-char pos)
127 (if (and (>= beg obeg) (< beg oend))
128 ;; Limit filling to the modified tail of the paragraph.
129 (let ( ;; When adaptive-fill-mode is enabled, the filling
130 ;; functions will attempt to set the fill prefix from
131 ;; the fake paragraph bounds we pass in, so set it
132 ;; ourselves first, using the real paragraph bounds.
133 (fill-prefix
134 (if (and adaptive-fill-mode
135 (or (null fill-prefix) (string= fill-prefix "")))
136 (fill-context-prefix beg end)
137 fill-prefix))
138 ;; Turn off adaptive-fill-mode temporarily
139 (adaptive-fill-mode nil))
140 (save-restriction
141 (if use-hard-newlines
142 (fill-region oend end arg)
143 (fill-region-as-paragraph oend end arg)))
144 (move-overlay refill-ignorable-overlay obeg (point)))
145 ;; Fill the whole paragraph
146 (save-restriction
147 (if use-hard-newlines
148 (fill-region beg end arg)
149 (fill-region-as-paragraph beg end arg)))
150 (move-overlay refill-ignorable-overlay beg (point)))))))
867092e9
MB
151
152(defun refill-fill-paragraph (arg)
153 "Like `fill-paragraph' but don't delete whitespace at paragraph end."
154 (refill-fill-paragraph-at (point) arg))
aaaf7be7
DL
155
156(defvar refill-doit nil
8b4703c4 157 "Non-nil tells `refill-post-command-function' to do its processing.
7850e188 158Set by `refill-after-change-function' in `after-change-functions' and
867092e9
MB
159unset by `refill-post-command-function' in `post-command-hook', and
160sometimes `refill-pre-command-function' in `pre-command-hook'. This
aaaf7be7
DL
161ensures refilling is only done once per command that causes a change,
162regardless of the number of after-change calls from commands doing
163complex processing.")
164(make-variable-buffer-local 'refill-doit)
165
166(defun refill-after-change-function (beg end len)
167 "Function for `after-change-functions' which just sets `refill-doit'."
168 (unless undo-in-progress
867092e9 169 (setq refill-doit end)))
aaaf7be7
DL
170
171(defun refill-post-command-function ()
172 "Post-command function to do refilling (conditionally)."
867092e9 173 (when refill-doit ; there was a change
aaaf7be7 174 ;; There's probably scope for more special cases here...
04138be8
SM
175 (case this-command
176 (self-insert-command
177 ;; Treat self-insertion commands specially, since they don't
178 ;; always reset `refill-doit' -- for self-insertion commands that
179 ;; *don't* cause a refill, we want to leave it turned on so that
180 ;; any subsequent non-modification command will cause a refill.
181 (when (aref auto-fill-chars (char-before))
182 ;; Respond to the same characters as auto-fill (other than
183 ;; newline, covered below).
184 (refill-fill-paragraph-at refill-doit)
185 (setq refill-doit nil)))
186 ((quoted-insert fill-paragraph fill-region) nil)
187 ((newline newline-and-indent open-line indent-new-comment-line
188 reindent-then-newline-and-indent)
189 ;; Don't zap what was just inserted.
190 (save-excursion
191 (beginning-of-line) ; for newline-and-indent
192 (skip-chars-backward "\n")
193 (save-restriction
194 (narrow-to-region (point-min) (point))
195 (refill-fill-paragraph-at refill-doit)))
196 (widen)
197 (save-excursion
198 (skip-chars-forward "\n")
199 (save-restriction
200 (narrow-to-region (line-beginning-position) (point-max))
201 (refill-fill-paragraph-at refill-doit))))
202 (t
203 (refill-fill-paragraph-at refill-doit)))
204 (setq refill-doit nil)))
867092e9
MB
205
206(defun refill-pre-command-function ()
207 "Pre-command function to do refilling (conditionally)."
208 (when (and refill-doit (not (eq this-command 'self-insert-command)))
209 ;; A previous setting of `refill-doit' didn't result in a refill,
210 ;; because it was a self-insert-command. Since the next command is
211 ;; something else, do the refill now.
212 (refill-fill-paragraph-at refill-doit)
aaaf7be7
DL
213 (setq refill-doit nil)))
214
04138be8 215(defvar refill-saved-state nil)
10ee3d79 216
aaaf7be7
DL
217;;;###autoload
218(define-minor-mode refill-mode
219 "Toggle Refill minor mode.
220With prefix arg, turn Refill mode on iff arg is positive.
221
222When Refill mode is on, the current paragraph will be formatted when
223changes are made within it. Self-inserting characters only cause
224refilling if they would cause auto-filling."
6de274aa
LK
225 :group 'refill
226 :lighter " Refill"
227 :keymap '(("\177" . backward-delete-char-untabify))
f5bd4bf2
MB
228 ;; Remove old state if necessary
229 (when refill-ignorable-overlay
230 (delete-overlay refill-ignorable-overlay)
231 (kill-local-variable 'refill-ignorable-overlay))
04138be8
SM
232 (when (local-variable-p 'refill-saved-state)
233 (dolist (x refill-saved-state)
234 (set (make-local-variable (car x)) (cdr x)))
235 (kill-local-variable 'refill-saved-state))
aaaf7be7 236 (if refill-mode
7850e188
SM
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)
867092e9 240 (add-hook 'pre-command-hook 'refill-pre-command-function nil t)
04138be8
SM
241 (set (make-local-variable 'refill-saved-state)
242 (mapcar (lambda (s) (cons s (symbol-value s)))
243 '(fill-paragraph-function auto-fill-function)))
f5bd4bf2 244 ;; This provides the test for recursive paragraph filling.
7850e188
SM
245 (set (make-local-variable 'fill-paragraph-function)
246 'refill-fill-paragraph)
d1a27f1d
SM
247 ;; When using justification, doing DEL on 2 spaces should remove
248 ;; both, otherwise, the subsequent refill will undo the DEL.
249 (set (make-local-variable 'backward-delete-char-untabify-method)
250 'hungry)
867092e9
MB
251 (setq refill-ignorable-overlay (make-overlay 1 1 nil nil t))
252 (overlay-put refill-ignorable-overlay 'modification-hooks
253 '(refill-adjust-ignorable-overlay))
254 (overlay-put refill-ignorable-overlay 'insert-behind-hooks
255 '(refill-adjust-ignorable-overlay))
7850e188 256 (auto-fill-mode 0))
aaaf7be7
DL
257 (remove-hook 'after-change-functions 'refill-after-change-function t)
258 (remove-hook 'post-command-hook 'refill-post-command-function t)
d1a27f1d 259 (kill-local-variable 'backward-delete-char-untabify-method)))
aaaf7be7
DL
260
261(provide 'refill)
262
04138be8 263;; arch-tag: 2c4ce9e8-1daa-4a3b-b6f8-fd6ac5bf6138
aaaf7be7 264;;; refill.el ends here