More CL cleanups and reduction of use of cl.el.
[bpt/emacs.git] / lisp / textmodes / refill.el
CommitLineData
aaaf7be7
DL
1;;; refill.el --- `auto-fill' by refilling paragraphs on changes
2
acaf905b 3;; Copyright (C) 2000-2012 Free Software Foundation, Inc.
aaaf7be7
DL
4
5;; Author: Dave Love <fx@gnu.org>
a2515a78 6;; Maintainer: Miles Bader <miles@gnu.org>
aaaf7be7
DL
7;; Keywords: wp
8
2be7dabc
GM
9;; This file is part of GNU Emacs.
10
1fecc8fe 11;; GNU Emacs is free software: you can redistribute it and/or modify
aaaf7be7 12;; it under the terms of the GNU General Public License as published by
1fecc8fe
GM
13;; the Free Software Foundation, either version 3 of the License, or
14;; (at your option) any later version.
2be7dabc
GM
15
16;; GNU Emacs is distributed in the hope that it will be useful,
aaaf7be7
DL
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.
2be7dabc 20
aaaf7be7 21;; You should have received a copy of the GNU General Public License
1fecc8fe 22;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
aaaf7be7
DL
23
24;;; Commentary:
25
26;; Provides a mode where paragraphs are refilled after changes in them
7850e188 27;; (using `after-change-functions'). This gives something akin to typical
aaaf7be7
DL
28;; word processor-style filling. We restrict refilling due to
29;; self-insertion to the characters which trigger auto-fill.
30
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?
35
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.
39
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.
46
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
7850e188 49;; `post-command-hook' ensures simply that refilling only happens
aaaf7be7
DL
50;; once per command.
51
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
7850e188 54;; after-change-functions don't work for this purpose; perhaps there was
aaaf7be7
DL
55;; some Emacs bug at the time. ISTR maniac has problems with
56;; whitespace at the end of paragraphs.]
57
d1a27f1d
SM
58;;; Todo/Bugs:
59
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:
63;; Start with
64;; I>< blabla
65;;
66;; and hit backspace. We end up with
67;;
68;; ><blabla
69;; instead of
70;; >< blabla
71;;
72;; Other example. Start with
73;;
74;; Foo bar blablabla asdgf
75;; word>< asdfas dfasdfasd
76;; asd asdfa sdfasd sdf
77;;
78;; and hit M-backspace. We end up with
79;;
80;; Foo bar blablabla asdgf
81;; ><asdfas dfasdfasd asd
82;; asdfa sdfasd sdf
83
aaaf7be7
DL
84;;; Code:
85
e4caa91a
MR
86(defgroup refill nil
87 "Refilling paragraphs on changes."
88 :group 'fill)
89
867092e9
MB
90(defvar refill-ignorable-overlay nil
91 "Portion of the most recently filled paragraph not needing filling.
92This is used to optimize refilling.")
93(make-variable-buffer-local 'refill-ignorable-overlay)
94
95(defun refill-adjust-ignorable-overlay (overlay afterp beg end &optional len)
96 "Adjust OVERLAY to not include the about-to-be-modified region."
97 (when (not afterp)
867092e9
MB
98 (save-excursion
99 (goto-char beg)
100 (forward-line -1)
101 (if (<= (point) (overlay-start overlay))
102 ;; Just get OVERLAY out of the way
ad2feb08 103 (move-overlay overlay (point-min) (point-min))
db95369b 104 ;; Make overlay contain only the region
867092e9
MB
105 (move-overlay overlay (overlay-start overlay) (point))))))
106
107(defun refill-fill-paragraph-at (pos &optional arg)
108 "Like `fill-paragraph' at POS, but don't delete whitespace at paragraph end."
ad2feb08
SM
109 (save-excursion
110 (goto-char pos)
111 ;; FIXME: forward-paragraph seems to disregard `use-hard-newlines',
112 ;; leading to excessive refilling and wrong choice of fill-prefix.
113 ;; might be a bug in my paragraphs.el.
114 (forward-paragraph)
115 (skip-syntax-backward "-")
116 (let ((end (point))
117 (beg (progn (backward-paragraph) (point)))
118 (obeg (overlay-start refill-ignorable-overlay))
119 (oend (overlay-end refill-ignorable-overlay)))
120 (unless (> beg pos) ;Don't fill if point is outside the paragraph.
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 (move-overlay refill-ignorable-overlay obeg (point)))
140 ;; Fill the whole paragraph
141 (save-restriction
142 (if use-hard-newlines
143 (fill-region beg end arg)
144 (fill-region-as-paragraph beg end arg)))
145 (move-overlay refill-ignorable-overlay beg (point)))))))
867092e9
MB
146
147(defun refill-fill-paragraph (arg)
148 "Like `fill-paragraph' but don't delete whitespace at paragraph end."
149 (refill-fill-paragraph-at (point) arg))
aaaf7be7
DL
150
151(defvar refill-doit nil
8b4703c4 152 "Non-nil tells `refill-post-command-function' to do its processing.
7850e188 153Set by `refill-after-change-function' in `after-change-functions' and
867092e9
MB
154unset by `refill-post-command-function' in `post-command-hook', and
155sometimes `refill-pre-command-function' in `pre-command-hook'. This
aaaf7be7
DL
156ensures refilling is only done once per command that causes a change,
157regardless of the number of after-change calls from commands doing
158complex processing.")
159(make-variable-buffer-local 'refill-doit)
160
161(defun refill-after-change-function (beg end len)
162 "Function for `after-change-functions' which just sets `refill-doit'."
163 (unless undo-in-progress
867092e9 164 (setq refill-doit end)))
aaaf7be7
DL
165
166(defun refill-post-command-function ()
167 "Post-command function to do refilling (conditionally)."
867092e9 168 (when refill-doit ; there was a change
aaaf7be7 169 ;; There's probably scope for more special cases here...
a464a6c7
SM
170 (pcase this-command
171 (`self-insert-command
04138be8
SM
172 ;; Treat self-insertion commands specially, since they don't
173 ;; always reset `refill-doit' -- for self-insertion commands that
174 ;; *don't* cause a refill, we want to leave it turned on so that
175 ;; any subsequent non-modification command will cause a refill.
176 (when (aref auto-fill-chars (char-before))
177 ;; Respond to the same characters as auto-fill (other than
178 ;; newline, covered below).
179 (refill-fill-paragraph-at refill-doit)
180 (setq refill-doit nil)))
a464a6c7
SM
181 ((or `quoted-insert `fill-paragraph `fill-region) nil)
182 ((or `newline `newline-and-indent `open-line `indent-new-comment-line
183 `reindent-then-newline-and-indent)
04138be8
SM
184 ;; Don't zap what was just inserted.
185 (save-excursion
186 (beginning-of-line) ; for newline-and-indent
187 (skip-chars-backward "\n")
188 (save-restriction
189 (narrow-to-region (point-min) (point))
190 (refill-fill-paragraph-at refill-doit)))
191 (widen)
192 (save-excursion
193 (skip-chars-forward "\n")
194 (save-restriction
195 (narrow-to-region (line-beginning-position) (point-max))
196 (refill-fill-paragraph-at refill-doit))))
a464a6c7 197 (_
04138be8
SM
198 (refill-fill-paragraph-at refill-doit)))
199 (setq refill-doit nil)))
867092e9
MB
200
201(defun refill-pre-command-function ()
202 "Pre-command function to do refilling (conditionally)."
203 (when (and refill-doit (not (eq this-command 'self-insert-command)))
204 ;; A previous setting of `refill-doit' didn't result in a refill,
205 ;; because it was a self-insert-command. Since the next command is
206 ;; something else, do the refill now.
207 (refill-fill-paragraph-at refill-doit)
aaaf7be7
DL
208 (setq refill-doit nil)))
209
04138be8 210(defvar refill-saved-state nil)
10ee3d79 211
aaaf7be7
DL
212;;;###autoload
213(define-minor-mode refill-mode
ac6c8639
CY
214 "Toggle automatic refilling (Refill mode).
215With a prefix argument ARG, enable Refill mode if ARG is
216positive, and disable it otherwise. If called from Lisp, enable
217the mode if ARG is omitted or nil.
aaaf7be7 218
ac6c8639
CY
219Refill mode is a buffer-local minor mode. When enabled, the
220current paragraph is refilled as you edit. Self-inserting
221characters only cause refilling if they would cause
222auto-filling.
223
224For true \"word wrap\" behavior, use `visual-line-mode' instead."
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
263;;; refill.el ends here