annotate lisp/textmodes/refill.el @ 39892:ef399b9ffc2b

(2C-mode): Don't use make-local-hook.
author Stefan Monnier <monnier@iro.umontreal.ca>
date Sat, 13 Oct 2001 19:12:07 +0000
parents 64d8d90d180d
children b27f7112ce6f
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
32750
dd36de87245d *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
1 ;;; refill.el --- `auto-fill' by refilling paragraphs on changes
dd36de87245d *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
2
dd36de87245d *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
3 ;; Copyright (C) 2000 Free Software Foundation, Inc.
dd36de87245d *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
4
dd36de87245d *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
5 ;; Author: Dave Love <fx@gnu.org>
dd36de87245d *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
6 ;; Keywords: wp
dd36de87245d *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
7
38401
64d8d90d180d Fix license commentary.
Gerd Moellmann <gerd@gnu.org>
parents: 33736
diff changeset
8 ;; This file is part of GNU Emacs.
64d8d90d180d Fix license commentary.
Gerd Moellmann <gerd@gnu.org>
parents: 33736
diff changeset
9
64d8d90d180d Fix license commentary.
Gerd Moellmann <gerd@gnu.org>
parents: 33736
diff changeset
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
32750
dd36de87245d *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
11 ;; it under the terms of the GNU General Public License as published by
38401
64d8d90d180d Fix license commentary.
Gerd Moellmann <gerd@gnu.org>
parents: 33736
diff changeset
12 ;; the Free Software Foundation; either version 2, or (at your option)
64d8d90d180d Fix license commentary.
Gerd Moellmann <gerd@gnu.org>
parents: 33736
diff changeset
13 ;; any later version.
64d8d90d180d Fix license commentary.
Gerd Moellmann <gerd@gnu.org>
parents: 33736
diff changeset
14
64d8d90d180d Fix license commentary.
Gerd Moellmann <gerd@gnu.org>
parents: 33736
diff changeset
15 ;; GNU Emacs is distributed in the hope that it will be useful,
32750
dd36de87245d *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
dd36de87245d *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
dd36de87245d *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
18 ;; GNU General Public License for more details.
38401
64d8d90d180d Fix license commentary.
Gerd Moellmann <gerd@gnu.org>
parents: 33736
diff changeset
19
32750
dd36de87245d *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
20 ;; You should have received a copy of the GNU General Public License
38401
64d8d90d180d Fix license commentary.
Gerd Moellmann <gerd@gnu.org>
parents: 33736
diff changeset
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
64d8d90d180d Fix license commentary.
Gerd Moellmann <gerd@gnu.org>
parents: 33736
diff changeset
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
32750
dd36de87245d *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
23 ;; Boston, MA 02111-1307, USA.
dd36de87245d *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
24
dd36de87245d *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
25 ;;; Commentary:
dd36de87245d *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
26
dd36de87245d *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
27 ;; 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
28 ;; (using `after-change-functions'). This gives something akin to typical
32750
dd36de87245d *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
29 ;; word processor-style filling. We restrict refilling due to
dd36de87245d *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
30 ;; self-insertion to the characters which trigger auto-fill.
dd36de87245d *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
31
dd36de87245d *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
32 ;; It partly satisfies a todo item in enriched.el for some value of
dd36de87245d *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
33 ;; `without slowing down editing too much'. It doesn't attempt to do
dd36de87245d *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
34 ;; anything (using `window-size-change-functions'?) about resizing
dd36de87245d *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
35 ;; windows -- who cares?
dd36de87245d *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
36
dd36de87245d *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
37 ;; This implementation is probably fragile and missing some special
dd36de87245d *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
38 ;; cases -- not extensively tested. Yanking paragraph breaks, for
dd36de87245d *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
39 ;; instance, won't DTRT by refilling all the relevant paragraphs.
dd36de87245d *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
40
dd36de87245d *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
41 ;; You could do it a bit more efficiently (and robustly?) with just an
dd36de87245d *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
42 ;; auto-fill function, but that doesn't cope with changes other than
dd36de87245d *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
43 ;; through self-insertion. (Using auto-fill and after-change
dd36de87245d *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
44 ;; functions together didn't seem winning.) This could probably
dd36de87245d *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
45 ;; benefit from a less-general and faster `fill-paragraph-function',
dd36de87245d *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
46 ;; ideally as a primitive.
dd36de87245d *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
47
dd36de87245d *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
48 ;; The work is done in a local post-command hook but only if
dd36de87245d *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
49 ;; `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
50 ;; `post-command-hook' ensures simply that refilling only happens
32750
dd36de87245d *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
51 ;; once per command.
dd36de87245d *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
52
dd36de87245d *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
53 ;; [Per Abrahamsen's maniac.el does a similar thing, but operates from
dd36de87245d *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
54 ;; 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
55 ;; after-change-functions don't work for this purpose; perhaps there was
32750
dd36de87245d *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
56 ;; some Emacs bug at the time. ISTR maniac has problems with
dd36de87245d *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
57 ;; whitespace at the end of paragraphs.]
dd36de87245d *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
58
dd36de87245d *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
59 ;;; Code:
dd36de87245d *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
60
33660
b22657305a2c (refill-ignorable-overlay): New variable.
Miles Bader <miles@gnu.org>
parents: 32960
diff changeset
61 (defvar refill-ignorable-overlay nil
b22657305a2c (refill-ignorable-overlay): New variable.
Miles Bader <miles@gnu.org>
parents: 32960
diff changeset
62 "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
63 This is used to optimize refilling.")
b22657305a2c (refill-ignorable-overlay): New variable.
Miles Bader <miles@gnu.org>
parents: 32960
diff changeset
64 (make-variable-buffer-local 'refill-ignorable-overlay)
b22657305a2c (refill-ignorable-overlay): New variable.
Miles Bader <miles@gnu.org>
parents: 32960
diff changeset
65
b22657305a2c (refill-ignorable-overlay): New variable.
Miles Bader <miles@gnu.org>
parents: 32960
diff changeset
66 (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
67 "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
68 (when (not afterp)
32750
dd36de87245d *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
69 (save-excursion
33660
b22657305a2c (refill-ignorable-overlay): New variable.
Miles Bader <miles@gnu.org>
parents: 32960
diff changeset
70 (goto-char beg)
b22657305a2c (refill-ignorable-overlay): New variable.
Miles Bader <miles@gnu.org>
parents: 32960
diff changeset
71 (forward-line -1)
b22657305a2c (refill-ignorable-overlay): New variable.
Miles Bader <miles@gnu.org>
parents: 32960
diff changeset
72 (if (<= (point) (overlay-start overlay))
b22657305a2c (refill-ignorable-overlay): New variable.
Miles Bader <miles@gnu.org>
parents: 32960
diff changeset
73 ;; Just get OVERLAY out of the way
b22657305a2c (refill-ignorable-overlay): New variable.
Miles Bader <miles@gnu.org>
parents: 32960
diff changeset
74 (move-overlay overlay 1 1)
b22657305a2c (refill-ignorable-overlay): New variable.
Miles Bader <miles@gnu.org>
parents: 32960
diff changeset
75 ;; Make overlay contain only the region
b22657305a2c (refill-ignorable-overlay): New variable.
Miles Bader <miles@gnu.org>
parents: 32960
diff changeset
76 (move-overlay overlay (overlay-start overlay) (point))))))
b22657305a2c (refill-ignorable-overlay): New variable.
Miles Bader <miles@gnu.org>
parents: 32960
diff changeset
77
b22657305a2c (refill-ignorable-overlay): New variable.
Miles Bader <miles@gnu.org>
parents: 32960
diff changeset
78 (defun refill-fill-paragraph-at (pos &optional arg)
b22657305a2c (refill-ignorable-overlay): New variable.
Miles Bader <miles@gnu.org>
parents: 32960
diff changeset
79 "Like `fill-paragraph' at POS, but don't delete whitespace at paragraph end."
b22657305a2c (refill-ignorable-overlay): New variable.
Miles Bader <miles@gnu.org>
parents: 32960
diff changeset
80 (let (fill-pfx)
b22657305a2c (refill-ignorable-overlay): New variable.
Miles Bader <miles@gnu.org>
parents: 32960
diff changeset
81 (save-excursion
b22657305a2c (refill-ignorable-overlay): New variable.
Miles Bader <miles@gnu.org>
parents: 32960
diff changeset
82 (goto-char pos)
32750
dd36de87245d *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
83 (forward-paragraph)
dd36de87245d *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
84 (skip-syntax-backward "-")
dd36de87245d *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
85 (let ((end (point))
33660
b22657305a2c (refill-ignorable-overlay): New variable.
Miles Bader <miles@gnu.org>
parents: 32960
diff changeset
86 (beg (progn (backward-paragraph) (point)))
b22657305a2c (refill-ignorable-overlay): New variable.
Miles Bader <miles@gnu.org>
parents: 32960
diff changeset
87 (obeg (overlay-start refill-ignorable-overlay))
b22657305a2c (refill-ignorable-overlay): New variable.
Miles Bader <miles@gnu.org>
parents: 32960
diff changeset
88 (oend (overlay-end refill-ignorable-overlay)))
b22657305a2c (refill-ignorable-overlay): New variable.
Miles Bader <miles@gnu.org>
parents: 32960
diff changeset
89 (goto-char pos)
b22657305a2c (refill-ignorable-overlay): New variable.
Miles Bader <miles@gnu.org>
parents: 32960
diff changeset
90 (if (and (>= beg obeg) (< beg oend))
b22657305a2c (refill-ignorable-overlay): New variable.
Miles Bader <miles@gnu.org>
parents: 32960
diff changeset
91 ;; Limit filling to the modified tail of the paragraph.
b22657305a2c (refill-ignorable-overlay): New variable.
Miles Bader <miles@gnu.org>
parents: 32960
diff changeset
92 (let (;; When adaptive-fill-mode is enabled, the filling
b22657305a2c (refill-ignorable-overlay): New variable.
Miles Bader <miles@gnu.org>
parents: 32960
diff changeset
93 ;; functions will attempt to set the fill prefix from
b22657305a2c (refill-ignorable-overlay): New variable.
Miles Bader <miles@gnu.org>
parents: 32960
diff changeset
94 ;; the fake paragraph bounds we pass in, so set it
b22657305a2c (refill-ignorable-overlay): New variable.
Miles Bader <miles@gnu.org>
parents: 32960
diff changeset
95 ;; ourselves first, using the real paragraph bounds.
b22657305a2c (refill-ignorable-overlay): New variable.
Miles Bader <miles@gnu.org>
parents: 32960
diff changeset
96 (fill-prefix
b22657305a2c (refill-ignorable-overlay): New variable.
Miles Bader <miles@gnu.org>
parents: 32960
diff changeset
97 (if (and adaptive-fill-mode
b22657305a2c (refill-ignorable-overlay): New variable.
Miles Bader <miles@gnu.org>
parents: 32960
diff changeset
98 (or (null fill-prefix) (string= fill-prefix "")))
b22657305a2c (refill-ignorable-overlay): New variable.
Miles Bader <miles@gnu.org>
parents: 32960
diff changeset
99 (fill-context-prefix beg end)
b22657305a2c (refill-ignorable-overlay): New variable.
Miles Bader <miles@gnu.org>
parents: 32960
diff changeset
100 fill-prefix))
b22657305a2c (refill-ignorable-overlay): New variable.
Miles Bader <miles@gnu.org>
parents: 32960
diff changeset
101 ;; Turn off adaptive-fill-mode temporarily
b22657305a2c (refill-ignorable-overlay): New variable.
Miles Bader <miles@gnu.org>
parents: 32960
diff changeset
102 (adaptive-fill-mode nil))
b22657305a2c (refill-ignorable-overlay): New variable.
Miles Bader <miles@gnu.org>
parents: 32960
diff changeset
103 (save-restriction
b22657305a2c (refill-ignorable-overlay): New variable.
Miles Bader <miles@gnu.org>
parents: 32960
diff changeset
104 (if use-hard-newlines
b22657305a2c (refill-ignorable-overlay): New variable.
Miles Bader <miles@gnu.org>
parents: 32960
diff changeset
105 (fill-region oend end arg)
b22657305a2c (refill-ignorable-overlay): New variable.
Miles Bader <miles@gnu.org>
parents: 32960
diff changeset
106 (fill-region-as-paragraph oend end arg)))
b22657305a2c (refill-ignorable-overlay): New variable.
Miles Bader <miles@gnu.org>
parents: 32960
diff changeset
107 (setq fill-pfx fill-prefix)
b22657305a2c (refill-ignorable-overlay): New variable.
Miles Bader <miles@gnu.org>
parents: 32960
diff changeset
108 (move-overlay refill-ignorable-overlay obeg (point)))
b22657305a2c (refill-ignorable-overlay): New variable.
Miles Bader <miles@gnu.org>
parents: 32960
diff changeset
109 ;; Fill the whole paragraph
b22657305a2c (refill-ignorable-overlay): New variable.
Miles Bader <miles@gnu.org>
parents: 32960
diff changeset
110 (setq fill-pfx
b22657305a2c (refill-ignorable-overlay): New variable.
Miles Bader <miles@gnu.org>
parents: 32960
diff changeset
111 (save-restriction
b22657305a2c (refill-ignorable-overlay): New variable.
Miles Bader <miles@gnu.org>
parents: 32960
diff changeset
112 (if use-hard-newlines
b22657305a2c (refill-ignorable-overlay): New variable.
Miles Bader <miles@gnu.org>
parents: 32960
diff changeset
113 (fill-region beg end arg)
b22657305a2c (refill-ignorable-overlay): New variable.
Miles Bader <miles@gnu.org>
parents: 32960
diff changeset
114 (fill-region-as-paragraph beg end arg))))
b22657305a2c (refill-ignorable-overlay): New variable.
Miles Bader <miles@gnu.org>
parents: 32960
diff changeset
115 (move-overlay refill-ignorable-overlay beg (point)))))
b22657305a2c (refill-ignorable-overlay): New variable.
Miles Bader <miles@gnu.org>
parents: 32960
diff changeset
116 (skip-line-prefix fill-pfx)))
b22657305a2c (refill-ignorable-overlay): New variable.
Miles Bader <miles@gnu.org>
parents: 32960
diff changeset
117
b22657305a2c (refill-ignorable-overlay): New variable.
Miles Bader <miles@gnu.org>
parents: 32960
diff changeset
118 (defun refill-fill-paragraph (arg)
b22657305a2c (refill-ignorable-overlay): New variable.
Miles Bader <miles@gnu.org>
parents: 32960
diff changeset
119 "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
120 (refill-fill-paragraph-at (point) arg))
32750
dd36de87245d *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
121
dd36de87245d *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
122 (defvar refill-doit nil
dd36de87245d *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
123 "Non-nil means that `refill-post-command-function' does its processing.
32761
f7e7454c16a0 Fix var names in doc.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 32750
diff changeset
124 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
125 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
126 sometimes `refill-pre-command-function' in `pre-command-hook'. This
32750
dd36de87245d *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
127 ensures refilling is only done once per command that causes a change,
dd36de87245d *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
128 regardless of the number of after-change calls from commands doing
dd36de87245d *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
129 complex processing.")
dd36de87245d *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
130 (make-variable-buffer-local 'refill-doit)
dd36de87245d *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
131
dd36de87245d *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
132 (defun refill-after-change-function (beg end len)
dd36de87245d *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
133 "Function for `after-change-functions' which just sets `refill-doit'."
dd36de87245d *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
134 (unless undo-in-progress
33660
b22657305a2c (refill-ignorable-overlay): New variable.
Miles Bader <miles@gnu.org>
parents: 32960
diff changeset
135 (setq refill-doit end)))
32750
dd36de87245d *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
136
dd36de87245d *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
137 (defun refill-post-command-function ()
dd36de87245d *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
138 "Post-command function to do refilling (conditionally)."
33660
b22657305a2c (refill-ignorable-overlay): New variable.
Miles Bader <miles@gnu.org>
parents: 32960
diff changeset
139 (when refill-doit ; there was a change
32750
dd36de87245d *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
140 ;; There's probably scope for more special cases here...
33660
b22657305a2c (refill-ignorable-overlay): New variable.
Miles Bader <miles@gnu.org>
parents: 32960
diff changeset
141 (if (eq this-command 'self-insert-command)
b22657305a2c (refill-ignorable-overlay): New variable.
Miles Bader <miles@gnu.org>
parents: 32960
diff changeset
142 ;; Treat self-insertion commands specially, since they don't
b22657305a2c (refill-ignorable-overlay): New variable.
Miles Bader <miles@gnu.org>
parents: 32960
diff changeset
143 ;; always reset `refill-doit' -- for self-insertion commands that
b22657305a2c (refill-ignorable-overlay): New variable.
Miles Bader <miles@gnu.org>
parents: 32960
diff changeset
144 ;; *don't* cause a refill, we want to leave it turned on so that
b22657305a2c (refill-ignorable-overlay): New variable.
Miles Bader <miles@gnu.org>
parents: 32960
diff changeset
145 ;; any subsequent non-modification command will cause a refill.
b22657305a2c (refill-ignorable-overlay): New variable.
Miles Bader <miles@gnu.org>
parents: 32960
diff changeset
146 (when (aref auto-fill-chars (char-before))
b22657305a2c (refill-ignorable-overlay): New variable.
Miles Bader <miles@gnu.org>
parents: 32960
diff changeset
147 ;; Respond to the same characters as auto-fill (other than
b22657305a2c (refill-ignorable-overlay): New variable.
Miles Bader <miles@gnu.org>
parents: 32960
diff changeset
148 ;; newline, covered below).
b22657305a2c (refill-ignorable-overlay): New variable.
Miles Bader <miles@gnu.org>
parents: 32960
diff changeset
149 (refill-fill-paragraph-at refill-doit)
b22657305a2c (refill-ignorable-overlay): New variable.
Miles Bader <miles@gnu.org>
parents: 32960
diff changeset
150 (setq refill-doit nil))
b22657305a2c (refill-ignorable-overlay): New variable.
Miles Bader <miles@gnu.org>
parents: 32960
diff changeset
151 (cond
b22657305a2c (refill-ignorable-overlay): New variable.
Miles Bader <miles@gnu.org>
parents: 32960
diff changeset
152 ((or (eq this-command 'quoted-insert)
b22657305a2c (refill-ignorable-overlay): New variable.
Miles Bader <miles@gnu.org>
parents: 32960
diff changeset
153 (eq this-command 'fill-paragraph)
b22657305a2c (refill-ignorable-overlay): New variable.
Miles Bader <miles@gnu.org>
parents: 32960
diff changeset
154 (eq this-command 'fill-region))
b22657305a2c (refill-ignorable-overlay): New variable.
Miles Bader <miles@gnu.org>
parents: 32960
diff changeset
155 nil)
b22657305a2c (refill-ignorable-overlay): New variable.
Miles Bader <miles@gnu.org>
parents: 32960
diff changeset
156 ((or (eq this-command 'newline)
b22657305a2c (refill-ignorable-overlay): New variable.
Miles Bader <miles@gnu.org>
parents: 32960
diff changeset
157 (eq this-command 'newline-and-indent)
b22657305a2c (refill-ignorable-overlay): New variable.
Miles Bader <miles@gnu.org>
parents: 32960
diff changeset
158 (eq this-command 'open-line))
b22657305a2c (refill-ignorable-overlay): New variable.
Miles Bader <miles@gnu.org>
parents: 32960
diff changeset
159 ;; Don't zap what was just inserted.
b22657305a2c (refill-ignorable-overlay): New variable.
Miles Bader <miles@gnu.org>
parents: 32960
diff changeset
160 (save-excursion
b22657305a2c (refill-ignorable-overlay): New variable.
Miles Bader <miles@gnu.org>
parents: 32960
diff changeset
161 (beginning-of-line) ; for newline-and-indent
b22657305a2c (refill-ignorable-overlay): New variable.
Miles Bader <miles@gnu.org>
parents: 32960
diff changeset
162 (skip-chars-backward "\n")
b22657305a2c (refill-ignorable-overlay): New variable.
Miles Bader <miles@gnu.org>
parents: 32960
diff changeset
163 (save-restriction
b22657305a2c (refill-ignorable-overlay): New variable.
Miles Bader <miles@gnu.org>
parents: 32960
diff changeset
164 (narrow-to-region (point-min) (point))
b22657305a2c (refill-ignorable-overlay): New variable.
Miles Bader <miles@gnu.org>
parents: 32960
diff changeset
165 (refill-fill-paragraph-at refill-doit)))
b22657305a2c (refill-ignorable-overlay): New variable.
Miles Bader <miles@gnu.org>
parents: 32960
diff changeset
166 (widen)
b22657305a2c (refill-ignorable-overlay): New variable.
Miles Bader <miles@gnu.org>
parents: 32960
diff changeset
167 (save-excursion
b22657305a2c (refill-ignorable-overlay): New variable.
Miles Bader <miles@gnu.org>
parents: 32960
diff changeset
168 (skip-chars-forward "\n")
b22657305a2c (refill-ignorable-overlay): New variable.
Miles Bader <miles@gnu.org>
parents: 32960
diff changeset
169 (save-restriction
b22657305a2c (refill-ignorable-overlay): New variable.
Miles Bader <miles@gnu.org>
parents: 32960
diff changeset
170 (narrow-to-region (line-beginning-position) (point-max))
b22657305a2c (refill-ignorable-overlay): New variable.
Miles Bader <miles@gnu.org>
parents: 32960
diff changeset
171 (refill-fill-paragraph-at refill-doit))))
b22657305a2c (refill-ignorable-overlay): New variable.
Miles Bader <miles@gnu.org>
parents: 32960
diff changeset
172 (t
b22657305a2c (refill-ignorable-overlay): New variable.
Miles Bader <miles@gnu.org>
parents: 32960
diff changeset
173 (refill-fill-paragraph-at refill-doit)))
b22657305a2c (refill-ignorable-overlay): New variable.
Miles Bader <miles@gnu.org>
parents: 32960
diff changeset
174 (setq refill-doit nil))))
b22657305a2c (refill-ignorable-overlay): New variable.
Miles Bader <miles@gnu.org>
parents: 32960
diff changeset
175
b22657305a2c (refill-ignorable-overlay): New variable.
Miles Bader <miles@gnu.org>
parents: 32960
diff changeset
176 (defun refill-pre-command-function ()
b22657305a2c (refill-ignorable-overlay): New variable.
Miles Bader <miles@gnu.org>
parents: 32960
diff changeset
177 "Pre-command function to do refilling (conditionally)."
b22657305a2c (refill-ignorable-overlay): New variable.
Miles Bader <miles@gnu.org>
parents: 32960
diff changeset
178 (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
179 ;; 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
180 ;; 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
181 ;; something else, do the refill now.
b22657305a2c (refill-ignorable-overlay): New variable.
Miles Bader <miles@gnu.org>
parents: 32960
diff changeset
182 (refill-fill-paragraph-at refill-doit)
32750
dd36de87245d *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
183 (setq refill-doit nil)))
dd36de87245d *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
184
32960
0756037b6de5 (refill-late-fill-paragraph-function): New
Dave Love <fx@gnu.org>
parents: 32767
diff changeset
185 (defvar refill-late-fill-paragraph-function nil)
0756037b6de5 (refill-late-fill-paragraph-function): New
Dave Love <fx@gnu.org>
parents: 32767
diff changeset
186
32750
dd36de87245d *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
187 ;;;###autoload
dd36de87245d *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
188 (define-minor-mode refill-mode
dd36de87245d *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
189 "Toggle Refill minor mode.
dd36de87245d *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
190 With prefix arg, turn Refill mode on iff arg is positive.
dd36de87245d *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
191
dd36de87245d *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
192 When Refill mode is on, the current paragraph will be formatted when
dd36de87245d *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
193 changes are made within it. Self-inserting characters only cause
dd36de87245d *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
194 refilling if they would cause auto-filling."
dd36de87245d *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
195 nil " Refill" nil
dd36de87245d *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
196 ;; This provides the test for recursive paragraph filling.
dd36de87245d *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
197 (make-local-variable 'fill-paragraph-function)
dd36de87245d *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
198 (if refill-mode
32761
f7e7454c16a0 Fix var names in doc.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 32750
diff changeset
199 (progn
f7e7454c16a0 Fix var names in doc.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 32750
diff changeset
200 (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
201 (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
202 (add-hook 'pre-command-hook 'refill-pre-command-function nil t)
32960
0756037b6de5 (refill-late-fill-paragraph-function): New
Dave Love <fx@gnu.org>
parents: 32767
diff changeset
203 (set (make-local-variable 'refill-late-fill-paragraph-function)
0756037b6de5 (refill-late-fill-paragraph-function): New
Dave Love <fx@gnu.org>
parents: 32767
diff changeset
204 fill-paragraph-function)
32761
f7e7454c16a0 Fix var names in doc.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 32750
diff changeset
205 (set (make-local-variable 'fill-paragraph-function)
f7e7454c16a0 Fix var names in doc.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 32750
diff changeset
206 'refill-fill-paragraph)
33660
b22657305a2c (refill-ignorable-overlay): New variable.
Miles Bader <miles@gnu.org>
parents: 32960
diff changeset
207 (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
208 (overlay-put refill-ignorable-overlay 'modification-hooks
b22657305a2c (refill-ignorable-overlay): New variable.
Miles Bader <miles@gnu.org>
parents: 32960
diff changeset
209 '(refill-adjust-ignorable-overlay))
b22657305a2c (refill-ignorable-overlay): New variable.
Miles Bader <miles@gnu.org>
parents: 32960
diff changeset
210 (overlay-put refill-ignorable-overlay 'insert-behind-hooks
b22657305a2c (refill-ignorable-overlay): New variable.
Miles Bader <miles@gnu.org>
parents: 32960
diff changeset
211 '(refill-adjust-ignorable-overlay))
32761
f7e7454c16a0 Fix var names in doc.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 32750
diff changeset
212 (auto-fill-mode 0))
32750
dd36de87245d *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
213 (remove-hook 'after-change-functions 'refill-after-change-function t)
dd36de87245d *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
214 (remove-hook 'post-command-hook 'refill-post-command-function t)
33660
b22657305a2c (refill-ignorable-overlay): New variable.
Miles Bader <miles@gnu.org>
parents: 32960
diff changeset
215 (delete-overlay refill-ignorable-overlay)
32960
0756037b6de5 (refill-late-fill-paragraph-function): New
Dave Love <fx@gnu.org>
parents: 32767
diff changeset
216 (setq fill-paragraph-function refill-late-fill-paragraph-function)))
32750
dd36de87245d *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
217
dd36de87245d *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
218 (provide 'refill)
dd36de87245d *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
219
dd36de87245d *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
220 ;;; refill.el ends here