Mercurial > emacs
annotate lisp/textmodes/refill.el @ 63298:44c92b4ef11a
*** empty log message ***
author | Jason Rumney <jasonr@gnu.org> |
---|---|
date | Fri, 10 Jun 2005 23:04:18 +0000 |
parents | 6363fa77031a |
children | a8fa7c632ee4 02f1dbc4a199 |
rev | line source |
---|---|
32750 | 1 ;;; refill.el --- `auto-fill' by refilling paragraphs on changes |
2 | |
59709
73def390d890
(refill-post-command-function):
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
52401
diff
changeset
|
3 ;; Copyright (C) 2000, 2003, 2005 Free Software Foundation, Inc. |
32750 | 4 |
5 ;; Author: Dave Love <fx@gnu.org> | |
52290 | 6 ;; Maintainer: Miles Bader <miles@gnu.org> |
32750 | 7 ;; Keywords: wp |
8 | |
38401 | 9 ;; This file is part of GNU Emacs. |
10 | |
11 ;; GNU Emacs is free software; you can redistribute it and/or modify | |
32750 | 12 ;; it under the terms of the GNU General Public License as published by |
38401 | 13 ;; the Free Software Foundation; either version 2, or (at your option) |
14 ;; any later version. | |
15 | |
16 ;; GNU Emacs is distributed in the hope that it will be useful, | |
32750 | 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. | |
38401 | 20 |
32750 | 21 ;; You should have received a copy of the GNU General Public License |
38401 | 22 ;; along with GNU Emacs; see the file COPYING. If not, write to the |
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
32750 | 24 ;; Boston, MA 02111-1307, USA. |
25 | |
26 ;;; Commentary: | |
27 | |
28 ;; Provides a mode where paragraphs are refilled after changes in them | |
32761
f7e7454c16a0
Fix var names in doc.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
32750
diff
changeset
|
29 ;; (using `after-change-functions'). This gives something akin to typical |
32750 | 30 ;; word processor-style filling. We restrict refilling due to |
31 ;; self-insertion to the characters which trigger auto-fill. | |
32 | |
33 ;; It partly satisfies a todo item in enriched.el for some value of | |
34 ;; `without slowing down editing too much'. It doesn't attempt to do | |
35 ;; anything (using `window-size-change-functions'?) about resizing | |
36 ;; windows -- who cares? | |
37 | |
38 ;; This implementation is probably fragile and missing some special | |
39 ;; cases -- not extensively tested. Yanking paragraph breaks, for | |
40 ;; instance, won't DTRT by refilling all the relevant paragraphs. | |
41 | |
42 ;; You could do it a bit more efficiently (and robustly?) with just an | |
43 ;; auto-fill function, but that doesn't cope with changes other than | |
44 ;; through self-insertion. (Using auto-fill and after-change | |
45 ;; functions together didn't seem winning.) This could probably | |
46 ;; benefit from a less-general and faster `fill-paragraph-function', | |
47 ;; ideally as a primitive. | |
48 | |
49 ;; The work is done in a local post-command hook but only if | |
50 ;; `refill-doit' has been set by the after-change function. Using | |
32761
f7e7454c16a0
Fix var names in doc.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
32750
diff
changeset
|
51 ;; `post-command-hook' ensures simply that refilling only happens |
32750 | 52 ;; once per command. |
53 | |
54 ;; [Per Abrahamsen's maniac.el does a similar thing, but operates from | |
55 ;; post-command-hook. I don't understand the statement in it that | |
32761
f7e7454c16a0
Fix var names in doc.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
32750
diff
changeset
|
56 ;; after-change-functions don't work for this purpose; perhaps there was |
32750 | 57 ;; some Emacs bug at the time. ISTR maniac has problems with |
58 ;; whitespace at the end of paragraphs.] | |
59 | |
39894
b27f7112ce6f
(refill-mode): Bind DEL to backward-delete-char-untabify.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
38401
diff
changeset
|
60 ;;; Todo/Bugs: |
b27f7112ce6f
(refill-mode): Bind DEL to backward-delete-char-untabify.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
38401
diff
changeset
|
61 |
b27f7112ce6f
(refill-mode): Bind DEL to backward-delete-char-untabify.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
38401
diff
changeset
|
62 ;; - 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>
parents:
38401
diff
changeset
|
63 ;; 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>
parents:
38401
diff
changeset
|
64 ;; 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>
parents:
38401
diff
changeset
|
65 ;; Start with |
b27f7112ce6f
(refill-mode): Bind DEL to backward-delete-char-untabify.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
38401
diff
changeset
|
66 ;; I>< blabla |
b27f7112ce6f
(refill-mode): Bind DEL to backward-delete-char-untabify.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
38401
diff
changeset
|
67 ;; |
b27f7112ce6f
(refill-mode): Bind DEL to backward-delete-char-untabify.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
38401
diff
changeset
|
68 ;; and hit backspace. We end up with |
b27f7112ce6f
(refill-mode): Bind DEL to backward-delete-char-untabify.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
38401
diff
changeset
|
69 ;; |
b27f7112ce6f
(refill-mode): Bind DEL to backward-delete-char-untabify.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
38401
diff
changeset
|
70 ;; ><blabla |
b27f7112ce6f
(refill-mode): Bind DEL to backward-delete-char-untabify.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
38401
diff
changeset
|
71 ;; instead of |
b27f7112ce6f
(refill-mode): Bind DEL to backward-delete-char-untabify.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
38401
diff
changeset
|
72 ;; >< blabla |
b27f7112ce6f
(refill-mode): Bind DEL to backward-delete-char-untabify.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
38401
diff
changeset
|
73 ;; |
b27f7112ce6f
(refill-mode): Bind DEL to backward-delete-char-untabify.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
38401
diff
changeset
|
74 ;; Other example. Start with |
b27f7112ce6f
(refill-mode): Bind DEL to backward-delete-char-untabify.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
38401
diff
changeset
|
75 ;; |
b27f7112ce6f
(refill-mode): Bind DEL to backward-delete-char-untabify.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
38401
diff
changeset
|
76 ;; Foo bar blablabla asdgf |
b27f7112ce6f
(refill-mode): Bind DEL to backward-delete-char-untabify.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
38401
diff
changeset
|
77 ;; word>< asdfas dfasdfasd |
b27f7112ce6f
(refill-mode): Bind DEL to backward-delete-char-untabify.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
38401
diff
changeset
|
78 ;; asd asdfa sdfasd sdf |
b27f7112ce6f
(refill-mode): Bind DEL to backward-delete-char-untabify.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
38401
diff
changeset
|
79 ;; |
b27f7112ce6f
(refill-mode): Bind DEL to backward-delete-char-untabify.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
38401
diff
changeset
|
80 ;; and hit M-backspace. We end up with |
b27f7112ce6f
(refill-mode): Bind DEL to backward-delete-char-untabify.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
38401
diff
changeset
|
81 ;; |
b27f7112ce6f
(refill-mode): Bind DEL to backward-delete-char-untabify.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
38401
diff
changeset
|
82 ;; Foo bar blablabla asdgf |
b27f7112ce6f
(refill-mode): Bind DEL to backward-delete-char-untabify.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
38401
diff
changeset
|
83 ;; ><asdfas dfasdfasd asd |
b27f7112ce6f
(refill-mode): Bind DEL to backward-delete-char-untabify.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
38401
diff
changeset
|
84 ;; asdfa sdfasd sdf |
b27f7112ce6f
(refill-mode): Bind DEL to backward-delete-char-untabify.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
38401
diff
changeset
|
85 |
32750 | 86 ;;; Code: |
87 | |
59709
73def390d890
(refill-post-command-function):
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
52401
diff
changeset
|
88 (eval-when-compile (require 'cl)) |
73def390d890
(refill-post-command-function):
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
52401
diff
changeset
|
89 |
48415
3f0b3e6d23fb
(defgroup refill): New group.
Markus Rost <rost@math.uni-bielefeld.de>
parents:
42263
diff
changeset
|
90 (defgroup refill nil |
3f0b3e6d23fb
(defgroup refill): New group.
Markus Rost <rost@math.uni-bielefeld.de>
parents:
42263
diff
changeset
|
91 "Refilling paragraphs on changes." |
3f0b3e6d23fb
(defgroup refill): New group.
Markus Rost <rost@math.uni-bielefeld.de>
parents:
42263
diff
changeset
|
92 :group 'fill) |
3f0b3e6d23fb
(defgroup refill): New group.
Markus Rost <rost@math.uni-bielefeld.de>
parents:
42263
diff
changeset
|
93 |
33660
b22657305a2c
(refill-ignorable-overlay): New variable.
Miles Bader <miles@gnu.org>
parents:
32960
diff
changeset
|
94 (defvar refill-ignorable-overlay nil |
b22657305a2c
(refill-ignorable-overlay): New variable.
Miles Bader <miles@gnu.org>
parents:
32960
diff
changeset
|
95 "Portion of the most recently filled paragraph not needing filling. |
b22657305a2c
(refill-ignorable-overlay): New variable.
Miles Bader <miles@gnu.org>
parents:
32960
diff
changeset
|
96 This is used to optimize refilling.") |
b22657305a2c
(refill-ignorable-overlay): New variable.
Miles Bader <miles@gnu.org>
parents:
32960
diff
changeset
|
97 (make-variable-buffer-local 'refill-ignorable-overlay) |
b22657305a2c
(refill-ignorable-overlay): New variable.
Miles Bader <miles@gnu.org>
parents:
32960
diff
changeset
|
98 |
b22657305a2c
(refill-ignorable-overlay): New variable.
Miles Bader <miles@gnu.org>
parents:
32960
diff
changeset
|
99 (defun refill-adjust-ignorable-overlay (overlay afterp beg end &optional len) |
b22657305a2c
(refill-ignorable-overlay): New variable.
Miles Bader <miles@gnu.org>
parents:
32960
diff
changeset
|
100 "Adjust OVERLAY to not include the about-to-be-modified region." |
b22657305a2c
(refill-ignorable-overlay): New variable.
Miles Bader <miles@gnu.org>
parents:
32960
diff
changeset
|
101 (when (not afterp) |
32750 | 102 (save-excursion |
33660
b22657305a2c
(refill-ignorable-overlay): New variable.
Miles Bader <miles@gnu.org>
parents:
32960
diff
changeset
|
103 (goto-char beg) |
b22657305a2c
(refill-ignorable-overlay): New variable.
Miles Bader <miles@gnu.org>
parents:
32960
diff
changeset
|
104 (forward-line -1) |
b22657305a2c
(refill-ignorable-overlay): New variable.
Miles Bader <miles@gnu.org>
parents:
32960
diff
changeset
|
105 (if (<= (point) (overlay-start overlay)) |
b22657305a2c
(refill-ignorable-overlay): New variable.
Miles Bader <miles@gnu.org>
parents:
32960
diff
changeset
|
106 ;; Just get OVERLAY out of the way |
51335
816e3b31173a
(refill-adjust-ignorable-overlay): Don't hardcode pint-min == 1.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
51292
diff
changeset
|
107 (move-overlay overlay (point-min) (point-min)) |
49599
5ade352e8d1c
Trailing whitespace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
48415
diff
changeset
|
108 ;; Make overlay contain only the region |
33660
b22657305a2c
(refill-ignorable-overlay): New variable.
Miles Bader <miles@gnu.org>
parents:
32960
diff
changeset
|
109 (move-overlay overlay (overlay-start overlay) (point)))))) |
b22657305a2c
(refill-ignorable-overlay): New variable.
Miles Bader <miles@gnu.org>
parents:
32960
diff
changeset
|
110 |
b22657305a2c
(refill-ignorable-overlay): New variable.
Miles Bader <miles@gnu.org>
parents:
32960
diff
changeset
|
111 (defun refill-fill-paragraph-at (pos &optional arg) |
b22657305a2c
(refill-ignorable-overlay): New variable.
Miles Bader <miles@gnu.org>
parents:
32960
diff
changeset
|
112 "Like `fill-paragraph' at POS, but don't delete whitespace at paragraph end." |
51335
816e3b31173a
(refill-adjust-ignorable-overlay): Don't hardcode pint-min == 1.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
51292
diff
changeset
|
113 (save-excursion |
816e3b31173a
(refill-adjust-ignorable-overlay): Don't hardcode pint-min == 1.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
51292
diff
changeset
|
114 (goto-char pos) |
816e3b31173a
(refill-adjust-ignorable-overlay): Don't hardcode pint-min == 1.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
51292
diff
changeset
|
115 ;; FIXME: forward-paragraph seems to disregard `use-hard-newlines', |
816e3b31173a
(refill-adjust-ignorable-overlay): Don't hardcode pint-min == 1.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
51292
diff
changeset
|
116 ;; leading to excessive refilling and wrong choice of fill-prefix. |
816e3b31173a
(refill-adjust-ignorable-overlay): Don't hardcode pint-min == 1.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
51292
diff
changeset
|
117 ;; might be a bug in my paragraphs.el. |
816e3b31173a
(refill-adjust-ignorable-overlay): Don't hardcode pint-min == 1.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
51292
diff
changeset
|
118 (forward-paragraph) |
816e3b31173a
(refill-adjust-ignorable-overlay): Don't hardcode pint-min == 1.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
51292
diff
changeset
|
119 (skip-syntax-backward "-") |
816e3b31173a
(refill-adjust-ignorable-overlay): Don't hardcode pint-min == 1.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
51292
diff
changeset
|
120 (let ((end (point)) |
816e3b31173a
(refill-adjust-ignorable-overlay): Don't hardcode pint-min == 1.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
51292
diff
changeset
|
121 (beg (progn (backward-paragraph) (point))) |
816e3b31173a
(refill-adjust-ignorable-overlay): Don't hardcode pint-min == 1.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
51292
diff
changeset
|
122 (obeg (overlay-start refill-ignorable-overlay)) |
816e3b31173a
(refill-adjust-ignorable-overlay): Don't hardcode pint-min == 1.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
51292
diff
changeset
|
123 (oend (overlay-end refill-ignorable-overlay))) |
816e3b31173a
(refill-adjust-ignorable-overlay): Don't hardcode pint-min == 1.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
51292
diff
changeset
|
124 (unless (> beg pos) ;Don't fill if point is outside the paragraph. |
816e3b31173a
(refill-adjust-ignorable-overlay): Don't hardcode pint-min == 1.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
51292
diff
changeset
|
125 (goto-char pos) |
816e3b31173a
(refill-adjust-ignorable-overlay): Don't hardcode pint-min == 1.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
51292
diff
changeset
|
126 (if (and (>= beg obeg) (< beg oend)) |
816e3b31173a
(refill-adjust-ignorable-overlay): Don't hardcode pint-min == 1.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
51292
diff
changeset
|
127 ;; Limit filling to the modified tail of the paragraph. |
816e3b31173a
(refill-adjust-ignorable-overlay): Don't hardcode pint-min == 1.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
51292
diff
changeset
|
128 (let ( ;; When adaptive-fill-mode is enabled, the filling |
816e3b31173a
(refill-adjust-ignorable-overlay): Don't hardcode pint-min == 1.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
51292
diff
changeset
|
129 ;; functions will attempt to set the fill prefix from |
816e3b31173a
(refill-adjust-ignorable-overlay): Don't hardcode pint-min == 1.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
51292
diff
changeset
|
130 ;; the fake paragraph bounds we pass in, so set it |
816e3b31173a
(refill-adjust-ignorable-overlay): Don't hardcode pint-min == 1.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
51292
diff
changeset
|
131 ;; ourselves first, using the real paragraph bounds. |
816e3b31173a
(refill-adjust-ignorable-overlay): Don't hardcode pint-min == 1.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
51292
diff
changeset
|
132 (fill-prefix |
816e3b31173a
(refill-adjust-ignorable-overlay): Don't hardcode pint-min == 1.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
51292
diff
changeset
|
133 (if (and adaptive-fill-mode |
816e3b31173a
(refill-adjust-ignorable-overlay): Don't hardcode pint-min == 1.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
51292
diff
changeset
|
134 (or (null fill-prefix) (string= fill-prefix ""))) |
816e3b31173a
(refill-adjust-ignorable-overlay): Don't hardcode pint-min == 1.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
51292
diff
changeset
|
135 (fill-context-prefix beg end) |
816e3b31173a
(refill-adjust-ignorable-overlay): Don't hardcode pint-min == 1.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
51292
diff
changeset
|
136 fill-prefix)) |
816e3b31173a
(refill-adjust-ignorable-overlay): Don't hardcode pint-min == 1.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
51292
diff
changeset
|
137 ;; Turn off adaptive-fill-mode temporarily |
816e3b31173a
(refill-adjust-ignorable-overlay): Don't hardcode pint-min == 1.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
51292
diff
changeset
|
138 (adaptive-fill-mode nil)) |
816e3b31173a
(refill-adjust-ignorable-overlay): Don't hardcode pint-min == 1.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
51292
diff
changeset
|
139 (save-restriction |
816e3b31173a
(refill-adjust-ignorable-overlay): Don't hardcode pint-min == 1.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
51292
diff
changeset
|
140 (if use-hard-newlines |
816e3b31173a
(refill-adjust-ignorable-overlay): Don't hardcode pint-min == 1.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
51292
diff
changeset
|
141 (fill-region oend end arg) |
816e3b31173a
(refill-adjust-ignorable-overlay): Don't hardcode pint-min == 1.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
51292
diff
changeset
|
142 (fill-region-as-paragraph oend end arg))) |
816e3b31173a
(refill-adjust-ignorable-overlay): Don't hardcode pint-min == 1.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
51292
diff
changeset
|
143 (move-overlay refill-ignorable-overlay obeg (point))) |
816e3b31173a
(refill-adjust-ignorable-overlay): Don't hardcode pint-min == 1.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
51292
diff
changeset
|
144 ;; Fill the whole paragraph |
816e3b31173a
(refill-adjust-ignorable-overlay): Don't hardcode pint-min == 1.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
51292
diff
changeset
|
145 (save-restriction |
816e3b31173a
(refill-adjust-ignorable-overlay): Don't hardcode pint-min == 1.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
51292
diff
changeset
|
146 (if use-hard-newlines |
816e3b31173a
(refill-adjust-ignorable-overlay): Don't hardcode pint-min == 1.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
51292
diff
changeset
|
147 (fill-region beg end arg) |
816e3b31173a
(refill-adjust-ignorable-overlay): Don't hardcode pint-min == 1.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
51292
diff
changeset
|
148 (fill-region-as-paragraph beg end arg))) |
816e3b31173a
(refill-adjust-ignorable-overlay): Don't hardcode pint-min == 1.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
51292
diff
changeset
|
149 (move-overlay refill-ignorable-overlay beg (point))))))) |
33660
b22657305a2c
(refill-ignorable-overlay): New variable.
Miles Bader <miles@gnu.org>
parents:
32960
diff
changeset
|
150 |
b22657305a2c
(refill-ignorable-overlay): New variable.
Miles Bader <miles@gnu.org>
parents:
32960
diff
changeset
|
151 (defun refill-fill-paragraph (arg) |
b22657305a2c
(refill-ignorable-overlay): New variable.
Miles Bader <miles@gnu.org>
parents:
32960
diff
changeset
|
152 "Like `fill-paragraph' but don't delete whitespace at paragraph end." |
b22657305a2c
(refill-ignorable-overlay): New variable.
Miles Bader <miles@gnu.org>
parents:
32960
diff
changeset
|
153 (refill-fill-paragraph-at (point) arg)) |
32750 | 154 |
155 (defvar refill-doit nil | |
51292
c41cc5ded813
(refill-fill-paragraph-at): Avoid refilling the following paragraph.
Richard M. Stallman <rms@gnu.org>
parents:
50440
diff
changeset
|
156 "Non-nil tells `refill-post-command-function' to do its processing. |
32761
f7e7454c16a0
Fix var names in doc.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
32750
diff
changeset
|
157 Set by `refill-after-change-function' in `after-change-functions' and |
33660
b22657305a2c
(refill-ignorable-overlay): New variable.
Miles Bader <miles@gnu.org>
parents:
32960
diff
changeset
|
158 unset by `refill-post-command-function' in `post-command-hook', and |
b22657305a2c
(refill-ignorable-overlay): New variable.
Miles Bader <miles@gnu.org>
parents:
32960
diff
changeset
|
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
b22657305a2c
(refill-ignorable-overlay): New variable.
Miles Bader <miles@gnu.org>
parents:
32960
diff
changeset
|
168 (setq refill-doit end))) |
32750 | 169 |
170 (defun refill-post-command-function () | |
171 "Post-command function to do refilling (conditionally)." | |
33660
b22657305a2c
(refill-ignorable-overlay): New variable.
Miles Bader <miles@gnu.org>
parents:
32960
diff
changeset
|
172 (when refill-doit ; there was a change |
32750 | 173 ;; There's probably scope for more special cases here... |
59709
73def390d890
(refill-post-command-function):
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
52401
diff
changeset
|
174 (case this-command |
73def390d890
(refill-post-command-function):
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
52401
diff
changeset
|
175 (self-insert-command |
73def390d890
(refill-post-command-function):
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
52401
diff
changeset
|
176 ;; Treat self-insertion commands specially, since they don't |
73def390d890
(refill-post-command-function):
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
52401
diff
changeset
|
177 ;; always reset `refill-doit' -- for self-insertion commands that |
73def390d890
(refill-post-command-function):
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
52401
diff
changeset
|
178 ;; *don't* cause a refill, we want to leave it turned on so that |
73def390d890
(refill-post-command-function):
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
52401
diff
changeset
|
179 ;; any subsequent non-modification command will cause a refill. |
73def390d890
(refill-post-command-function):
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
52401
diff
changeset
|
180 (when (aref auto-fill-chars (char-before)) |
73def390d890
(refill-post-command-function):
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
52401
diff
changeset
|
181 ;; Respond to the same characters as auto-fill (other than |
73def390d890
(refill-post-command-function):
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
52401
diff
changeset
|
182 ;; newline, covered below). |
73def390d890
(refill-post-command-function):
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
52401
diff
changeset
|
183 (refill-fill-paragraph-at refill-doit) |
73def390d890
(refill-post-command-function):
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
52401
diff
changeset
|
184 (setq refill-doit nil))) |
73def390d890
(refill-post-command-function):
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
52401
diff
changeset
|
185 ((quoted-insert fill-paragraph fill-region) nil) |
73def390d890
(refill-post-command-function):
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
52401
diff
changeset
|
186 ((newline newline-and-indent open-line indent-new-comment-line |
73def390d890
(refill-post-command-function):
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
52401
diff
changeset
|
187 reindent-then-newline-and-indent) |
73def390d890
(refill-post-command-function):
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
52401
diff
changeset
|
188 ;; Don't zap what was just inserted. |
73def390d890
(refill-post-command-function):
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
52401
diff
changeset
|
189 (save-excursion |
73def390d890
(refill-post-command-function):
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
52401
diff
changeset
|
190 (beginning-of-line) ; for newline-and-indent |
73def390d890
(refill-post-command-function):
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
52401
diff
changeset
|
191 (skip-chars-backward "\n") |
73def390d890
(refill-post-command-function):
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
52401
diff
changeset
|
192 (save-restriction |
73def390d890
(refill-post-command-function):
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
52401
diff
changeset
|
193 (narrow-to-region (point-min) (point)) |
73def390d890
(refill-post-command-function):
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
52401
diff
changeset
|
194 (refill-fill-paragraph-at refill-doit))) |
73def390d890
(refill-post-command-function):
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
52401
diff
changeset
|
195 (widen) |
73def390d890
(refill-post-command-function):
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
52401
diff
changeset
|
196 (save-excursion |
73def390d890
(refill-post-command-function):
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
52401
diff
changeset
|
197 (skip-chars-forward "\n") |
73def390d890
(refill-post-command-function):
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
52401
diff
changeset
|
198 (save-restriction |
73def390d890
(refill-post-command-function):
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
52401
diff
changeset
|
199 (narrow-to-region (line-beginning-position) (point-max)) |
73def390d890
(refill-post-command-function):
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
52401
diff
changeset
|
200 (refill-fill-paragraph-at refill-doit)))) |
73def390d890
(refill-post-command-function):
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
52401
diff
changeset
|
201 (t |
73def390d890
(refill-post-command-function):
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
52401
diff
changeset
|
202 (refill-fill-paragraph-at refill-doit))) |
73def390d890
(refill-post-command-function):
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
52401
diff
changeset
|
203 (setq refill-doit nil))) |
33660
b22657305a2c
(refill-ignorable-overlay): New variable.
Miles Bader <miles@gnu.org>
parents:
32960
diff
changeset
|
204 |
b22657305a2c
(refill-ignorable-overlay): New variable.
Miles Bader <miles@gnu.org>
parents:
32960
diff
changeset
|
205 (defun refill-pre-command-function () |
b22657305a2c
(refill-ignorable-overlay): New variable.
Miles Bader <miles@gnu.org>
parents:
32960
diff
changeset
|
206 "Pre-command function to do refilling (conditionally)." |
b22657305a2c
(refill-ignorable-overlay): New variable.
Miles Bader <miles@gnu.org>
parents:
32960
diff
changeset
|
207 (when (and refill-doit (not (eq this-command 'self-insert-command))) |
b22657305a2c
(refill-ignorable-overlay): New variable.
Miles Bader <miles@gnu.org>
parents:
32960
diff
changeset
|
208 ;; A previous setting of `refill-doit' didn't result in a refill, |
b22657305a2c
(refill-ignorable-overlay): New variable.
Miles Bader <miles@gnu.org>
parents:
32960
diff
changeset
|
209 ;; because it was a self-insert-command. Since the next command is |
b22657305a2c
(refill-ignorable-overlay): New variable.
Miles Bader <miles@gnu.org>
parents:
32960
diff
changeset
|
210 ;; something else, do the refill now. |
b22657305a2c
(refill-ignorable-overlay): New variable.
Miles Bader <miles@gnu.org>
parents:
32960
diff
changeset
|
211 (refill-fill-paragraph-at refill-doit) |
32750 | 212 (setq refill-doit nil))) |
213 | |
59709
73def390d890
(refill-post-command-function):
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
52401
diff
changeset
|
214 (defvar refill-saved-state nil) |
32960
0756037b6de5
(refill-late-fill-paragraph-function): New
Dave Love <fx@gnu.org>
parents:
32767
diff
changeset
|
215 |
32750 | 216 ;;;###autoload |
217 (define-minor-mode refill-mode | |
218 "Toggle Refill minor mode. | |
219 With prefix arg, turn Refill mode on iff arg is positive. | |
220 | |
221 When Refill mode is on, the current paragraph will be formatted when | |
222 changes are made within it. Self-inserting characters only cause | |
223 refilling if they would cause auto-filling." | |
61288
6363fa77031a
(refill-mode): Specify :group.
Lute Kamstra <lute@gnu.org>
parents:
59709
diff
changeset
|
224 :group 'refill |
6363fa77031a
(refill-mode): Specify :group.
Lute Kamstra <lute@gnu.org>
parents:
59709
diff
changeset
|
225 :lighter " Refill" |
6363fa77031a
(refill-mode): Specify :group.
Lute Kamstra <lute@gnu.org>
parents:
59709
diff
changeset
|
226 :keymap '(("\177" . backward-delete-char-untabify)) |
42263
ea39d5799d38
(refill-mode): Don't barf when redundantly turning refill-mode off.
Miles Bader <miles@gnu.org>
parents:
39894
diff
changeset
|
227 ;; Remove old state if necessary |
ea39d5799d38
(refill-mode): Don't barf when redundantly turning refill-mode off.
Miles Bader <miles@gnu.org>
parents:
39894
diff
changeset
|
228 (when refill-ignorable-overlay |
ea39d5799d38
(refill-mode): Don't barf when redundantly turning refill-mode off.
Miles Bader <miles@gnu.org>
parents:
39894
diff
changeset
|
229 (delete-overlay refill-ignorable-overlay) |
ea39d5799d38
(refill-mode): Don't barf when redundantly turning refill-mode off.
Miles Bader <miles@gnu.org>
parents:
39894
diff
changeset
|
230 (kill-local-variable 'refill-ignorable-overlay)) |
59709
73def390d890
(refill-post-command-function):
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
52401
diff
changeset
|
231 (when (local-variable-p 'refill-saved-state) |
73def390d890
(refill-post-command-function):
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
52401
diff
changeset
|
232 (dolist (x refill-saved-state) |
73def390d890
(refill-post-command-function):
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
52401
diff
changeset
|
233 (set (make-local-variable (car x)) (cdr x))) |
73def390d890
(refill-post-command-function):
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
52401
diff
changeset
|
234 (kill-local-variable 'refill-saved-state)) |
32750 | 235 (if refill-mode |
32761
f7e7454c16a0
Fix var names in doc.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
32750
diff
changeset
|
236 (progn |
f7e7454c16a0
Fix var names in doc.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
32750
diff
changeset
|
237 (add-hook 'after-change-functions 'refill-after-change-function nil t) |
f7e7454c16a0
Fix var names in doc.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
32750
diff
changeset
|
238 (add-hook 'post-command-hook 'refill-post-command-function nil t) |
33660
b22657305a2c
(refill-ignorable-overlay): New variable.
Miles Bader <miles@gnu.org>
parents:
32960
diff
changeset
|
239 (add-hook 'pre-command-hook 'refill-pre-command-function nil t) |
59709
73def390d890
(refill-post-command-function):
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
52401
diff
changeset
|
240 (set (make-local-variable 'refill-saved-state) |
73def390d890
(refill-post-command-function):
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
52401
diff
changeset
|
241 (mapcar (lambda (s) (cons s (symbol-value s))) |
73def390d890
(refill-post-command-function):
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
52401
diff
changeset
|
242 '(fill-paragraph-function auto-fill-function))) |
42263
ea39d5799d38
(refill-mode): Don't barf when redundantly turning refill-mode off.
Miles Bader <miles@gnu.org>
parents:
39894
diff
changeset
|
243 ;; This provides the test for recursive paragraph filling. |
32761
f7e7454c16a0
Fix var names in doc.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
32750
diff
changeset
|
244 (set (make-local-variable 'fill-paragraph-function) |
f7e7454c16a0
Fix var names in doc.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
32750
diff
changeset
|
245 'refill-fill-paragraph) |
39894
b27f7112ce6f
(refill-mode): Bind DEL to backward-delete-char-untabify.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
38401
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>
parents:
38401
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>
parents:
38401
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>
parents:
38401
diff
changeset
|
249 'hungry) |
33660
b22657305a2c
(refill-ignorable-overlay): New variable.
Miles Bader <miles@gnu.org>
parents:
32960
diff
changeset
|
250 (setq refill-ignorable-overlay (make-overlay 1 1 nil nil t)) |
b22657305a2c
(refill-ignorable-overlay): New variable.
Miles Bader <miles@gnu.org>
parents:
32960
diff
changeset
|
251 (overlay-put refill-ignorable-overlay 'modification-hooks |
b22657305a2c
(refill-ignorable-overlay): New variable.
Miles Bader <miles@gnu.org>
parents:
32960
diff
changeset
|
252 '(refill-adjust-ignorable-overlay)) |
b22657305a2c
(refill-ignorable-overlay): New variable.
Miles Bader <miles@gnu.org>
parents:
32960
diff
changeset
|
253 (overlay-put refill-ignorable-overlay 'insert-behind-hooks |
b22657305a2c
(refill-ignorable-overlay): New variable.
Miles Bader <miles@gnu.org>
parents:
32960
diff
changeset
|
254 '(refill-adjust-ignorable-overlay)) |
32761
f7e7454c16a0
Fix var names in doc.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
32750
diff
changeset
|
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>
parents:
38401
diff
changeset
|
258 (kill-local-variable 'backward-delete-char-untabify-method))) |
32750 | 259 |
260 (provide 'refill) | |
261 | |
59709
73def390d890
(refill-post-command-function):
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
52401
diff
changeset
|
262 ;; arch-tag: 2c4ce9e8-1daa-4a3b-b6f8-fd6ac5bf6138 |
32750 | 263 ;;; refill.el ends here |