comparison lisp/textmodes/refill.el @ 33660:b22657305a2c

(refill-ignorable-overlay): New variable. (refill-adjust-ignorable-overlay): New function. (refill-pre-command-function): New function. (refill-mode): Add `refill-pre-command-function' to `pre-command-hook'. Initialize/cleanup `refill-ignorable-overlay'. (refill-post-command-function): Don't reset refill-doit in the case where a self-insertion command doesn't case a refill. Use `refill-fill-paragraph-at', getting position from `refill-doit'. (refill-after-change-function): Set `refill-doit' to END. (refill-fill-paragraph-at): New function, mostly from old refill-fill-paragraph. Use `refill-ignorable-overlay' to fill only the paragraph's tail if possible. Update `refill-ignorable-overlay'. Don't leave point inside the fill-prefix. (refill-fill-paragraph): Use `refill-fill-paragraph-at'.
author Miles Bader <miles@gnu.org>
date Tue, 21 Nov 2000 01:10:34 +0000
parents 0756037b6de5
children a65a0f325369
comparison
equal deleted inserted replaced
33659:4af2de0f7326 33660:b22657305a2c
54 ;; some Emacs bug at the time. ISTR maniac has problems with 54 ;; some Emacs bug at the time. ISTR maniac has problems with
55 ;; whitespace at the end of paragraphs.] 55 ;; whitespace at the end of paragraphs.]
56 56
57 ;;; Code: 57 ;;; Code:
58 58
59 (defun refill-fill-paragraph (arg) 59 (defvar refill-ignorable-overlay nil
60 "Like `fill-paragraph' but don't delete whitespace at paragraph end." 60 "Portion of the most recently filled paragraph not needing filling.
61 ;; Should probably use a text property indicating previously-filled 61 This is used to optimize refilling.")
62 ;; stuff to avoid filling before the point of change. 62 (make-variable-buffer-local 'refill-ignorable-overlay)
63 (let ((before (point))) 63
64 (defun refill-adjust-ignorable-overlay (overlay afterp beg end &optional len)
65 "Adjust OVERLAY to not include the about-to-be-modified region."
66 (when (not afterp)
67 (message "adjust: %s-%s" beg end)
64 (save-excursion 68 (save-excursion
69 (goto-char beg)
70 (forward-line -1)
71 (if (<= (point) (overlay-start overlay))
72 ;; Just get OVERLAY out of the way
73 (move-overlay overlay 1 1)
74 ;; Make overlay contain only the region
75 (move-overlay overlay (overlay-start overlay) (point))))))
76
77 (defun refill-fill-paragraph-at (pos &optional arg)
78 "Like `fill-paragraph' at POS, but don't delete whitespace at paragraph end."
79 (let (fill-pfx)
80 (save-excursion
81 (goto-char pos)
65 (forward-paragraph) 82 (forward-paragraph)
66 (skip-syntax-backward "-") 83 (skip-syntax-backward "-")
67 (let ((end (point)) 84 (let ((end (point))
68 (beg (progn (backward-paragraph) (point)))) 85 (beg (progn (backward-paragraph) (point)))
69 (goto-char before) 86 (obeg (overlay-start refill-ignorable-overlay))
70 (save-restriction 87 (oend (overlay-end refill-ignorable-overlay)))
71 (if use-hard-newlines 88 (goto-char pos)
72 (fill-region beg end arg) 89 (if (and (>= beg obeg) (< beg oend))
73 (fill-region-as-paragraph beg end arg))))))) 90 ;; Limit filling to the modified tail of the paragraph.
91 (let (;; When adaptive-fill-mode is enabled, the filling
92 ;; functions will attempt to set the fill prefix from
93 ;; the fake paragraph bounds we pass in, so set it
94 ;; ourselves first, using the real paragraph bounds.
95 (fill-prefix
96 (if (and adaptive-fill-mode
97 (or (null fill-prefix) (string= fill-prefix "")))
98 (fill-context-prefix beg end)
99 fill-prefix))
100 ;; Turn off adaptive-fill-mode temporarily
101 (adaptive-fill-mode nil))
102 (message "refill-at %s: %s-%s" pos oend end)
103 (save-restriction
104 (if use-hard-newlines
105 (fill-region oend end arg)
106 (fill-region-as-paragraph oend end arg)))
107 (setq fill-pfx fill-prefix)
108 (move-overlay refill-ignorable-overlay obeg (point)))
109 ;; Fill the whole paragraph
110 (setq fill-pfx
111 (save-restriction
112 (if use-hard-newlines
113 (fill-region beg end arg)
114 (fill-region-as-paragraph beg end arg))))
115 (move-overlay refill-ignorable-overlay beg (point)))))
116 (skip-line-prefix fill-pfx)))
117
118 (defun refill-fill-paragraph (arg)
119 "Like `fill-paragraph' but don't delete whitespace at paragraph end."
120 (refill-fill-paragraph-at (point) arg))
74 121
75 (defvar refill-doit nil 122 (defvar refill-doit nil
76 "Non-nil means that `refill-post-command-function' does its processing. 123 "Non-nil means that `refill-post-command-function' does its processing.
77 Set by `refill-after-change-function' in `after-change-functions' and 124 Set by `refill-after-change-function' in `after-change-functions' and
78 unset by `refill-post-command-function' in `post-command-hook'. This 125 unset by `refill-post-command-function' in `post-command-hook', and
126 sometimes `refill-pre-command-function' in `pre-command-hook'. This
79 ensures refilling is only done once per command that causes a change, 127 ensures refilling is only done once per command that causes a change,
80 regardless of the number of after-change calls from commands doing 128 regardless of the number of after-change calls from commands doing
81 complex processing.") 129 complex processing.")
82 (make-variable-buffer-local 'refill-doit) 130 (make-variable-buffer-local 'refill-doit)
83 131
84 (defun refill-after-change-function (beg end len) 132 (defun refill-after-change-function (beg end len)
85 "Function for `after-change-functions' which just sets `refill-doit'." 133 "Function for `after-change-functions' which just sets `refill-doit'."
86 (unless undo-in-progress 134 (unless undo-in-progress
87 (setq refill-doit t))) 135 (setq refill-doit end)))
88 136
89 (defun refill-post-command-function () 137 (defun refill-post-command-function ()
90 "Post-command function to do refilling (conditionally)." 138 "Post-command function to do refilling (conditionally)."
91 (when refill-doit ; there was a change 139 (when refill-doit ; there was a change
92 ;; There's probably scope for more special cases here... 140 ;; There's probably scope for more special cases here...
93 (cond 141 (if (eq this-command 'self-insert-command)
94 ((eq this-command 'self-insert-command) 142 ;; Treat self-insertion commands specially, since they don't
95 ;; Respond to the same characters as auto-fill (other than 143 ;; always reset `refill-doit' -- for self-insertion commands that
96 ;; newline, covered below). 144 ;; *don't* cause a refill, we want to leave it turned on so that
97 (if (aref auto-fill-chars (char-before)) 145 ;; any subsequent non-modification command will cause a refill.
98 (refill-fill-paragraph nil))) 146 (when (aref auto-fill-chars (char-before))
99 ((or (eq this-command 'quoted-insert) 147 ;; Respond to the same characters as auto-fill (other than
100 (eq this-command 'fill-paragraph) 148 ;; newline, covered below).
101 (eq this-command 'fill-region)) 149 (refill-fill-paragraph-at refill-doit)
102 nil) 150 (setq refill-doit nil))
103 ((or (eq this-command 'newline) 151 (cond
104 (eq this-command 'newline-and-indent) 152 ((or (eq this-command 'quoted-insert)
105 (eq this-command 'open-line)) 153 (eq this-command 'fill-paragraph)
106 ;; Don't zap what was just inserted. 154 (eq this-command 'fill-region))
107 (save-excursion 155 nil)
108 (beginning-of-line) ; for newline-and-indent 156 ((or (eq this-command 'newline)
109 (skip-chars-backward "\n") 157 (eq this-command 'newline-and-indent)
110 (save-restriction 158 (eq this-command 'open-line))
111 (narrow-to-region (point-min) (point)) 159 ;; Don't zap what was just inserted.
112 (refill-fill-paragraph nil))) 160 (save-excursion
113 (widen) 161 (beginning-of-line) ; for newline-and-indent
114 (save-excursion 162 (skip-chars-backward "\n")
115 (skip-chars-forward "\n") 163 (save-restriction
116 (save-restriction 164 (narrow-to-region (point-min) (point))
117 (narrow-to-region (line-beginning-position) (point-max)) 165 (refill-fill-paragraph-at refill-doit)))
118 (refill-fill-paragraph nil)))) 166 (widen)
119 (t (refill-fill-paragraph nil))) 167 (save-excursion
168 (skip-chars-forward "\n")
169 (save-restriction
170 (narrow-to-region (line-beginning-position) (point-max))
171 (refill-fill-paragraph-at refill-doit))))
172 (t
173 (refill-fill-paragraph-at refill-doit)))
174 (setq refill-doit nil))))
175
176 (defun refill-pre-command-function ()
177 "Pre-command function to do refilling (conditionally)."
178 (when (and refill-doit (not (eq this-command 'self-insert-command)))
179 ;; A previous setting of `refill-doit' didn't result in a refill,
180 ;; because it was a self-insert-command. Since the next command is
181 ;; something else, do the refill now.
182 (refill-fill-paragraph-at refill-doit)
120 (setq refill-doit nil))) 183 (setq refill-doit nil)))
121 184
122 (defvar refill-late-fill-paragraph-function nil) 185 (defvar refill-late-fill-paragraph-function nil)
123 186
124 ;;;###autoload 187 ;;;###autoload
134 (make-local-variable 'fill-paragraph-function) 197 (make-local-variable 'fill-paragraph-function)
135 (if refill-mode 198 (if refill-mode
136 (progn 199 (progn
137 (add-hook 'after-change-functions 'refill-after-change-function nil t) 200 (add-hook 'after-change-functions 'refill-after-change-function nil t)
138 (add-hook 'post-command-hook 'refill-post-command-function nil t) 201 (add-hook 'post-command-hook 'refill-post-command-function nil t)
202 (add-hook 'pre-command-hook 'refill-pre-command-function nil t)
139 (set (make-local-variable 'refill-late-fill-paragraph-function) 203 (set (make-local-variable 'refill-late-fill-paragraph-function)
140 fill-paragraph-function) 204 fill-paragraph-function)
141 (set (make-local-variable 'fill-paragraph-function) 205 (set (make-local-variable 'fill-paragraph-function)
142 'refill-fill-paragraph) 206 'refill-fill-paragraph)
207 (setq refill-ignorable-overlay (make-overlay 1 1 nil nil t))
208 (overlay-put refill-ignorable-overlay 'modification-hooks
209 '(refill-adjust-ignorable-overlay))
210 (overlay-put refill-ignorable-overlay 'insert-behind-hooks
211 '(refill-adjust-ignorable-overlay))
143 (auto-fill-mode 0)) 212 (auto-fill-mode 0))
144 (remove-hook 'after-change-functions 'refill-after-change-function t) 213 (remove-hook 'after-change-functions 'refill-after-change-function t)
145 (remove-hook 'post-command-hook 'refill-post-command-function t) 214 (remove-hook 'post-command-hook 'refill-post-command-function t)
215 (delete-overlay refill-ignorable-overlay)
146 (setq fill-paragraph-function refill-late-fill-paragraph-function))) 216 (setq fill-paragraph-function refill-late-fill-paragraph-function)))
147 217
148 (provide 'refill) 218 (provide 'refill)
149 219
150 ;;; refill.el ends here 220 ;;; refill.el ends here