refill.el 10.1 KB
Newer Older
Dave Love's avatar
Dave Love committed
1 2
;;; refill.el --- `auto-fill' by refilling paragraphs on changes

3
;; Copyright (C) 2000, 2003  Free Software Foundation, Inc.
Dave Love's avatar
Dave Love committed
4 5 6 7

;; Author: Dave Love <fx@gnu.org>
;; Keywords: wp

Gerd Moellmann's avatar
Gerd Moellmann committed
8 9 10
;; This file is part of GNU Emacs.

;; GNU Emacs is free software; you can redistribute it and/or modify
Dave Love's avatar
Dave Love committed
11
;; it under the terms of the GNU General Public License as published by
Gerd Moellmann's avatar
Gerd Moellmann committed
12 13 14 15
;; the Free Software Foundation; either version 2, or (at your option)
;; any later version.

;; GNU Emacs is distributed in the hope that it will be useful,
Dave Love's avatar
Dave Love committed
16 17 18
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.
Gerd Moellmann's avatar
Gerd Moellmann committed
19

Dave Love's avatar
Dave Love committed
20
;; You should have received a copy of the GNU General Public License
Gerd Moellmann's avatar
Gerd Moellmann committed
21 22
;; along with GNU Emacs; see the file COPYING.  If not, write to the
;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Dave Love's avatar
Dave Love committed
23 24 25 26 27
;; Boston, MA 02111-1307, USA.

;;; Commentary:

;; Provides a mode where paragraphs are refilled after changes in them
Stefan Monnier's avatar
Stefan Monnier committed
28
;; (using `after-change-functions').  This gives something akin to typical
Dave Love's avatar
Dave Love committed
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
;; word processor-style filling.  We restrict refilling due to
;; self-insertion to the characters which trigger auto-fill.

;; It partly satisfies a todo item in enriched.el for some value of
;; `without slowing down editing too much'.  It doesn't attempt to do
;; anything (using `window-size-change-functions'?) about resizing
;; windows -- who cares?

;; This implementation is probably fragile and missing some special
;; cases -- not extensively tested.  Yanking paragraph breaks, for
;; instance, won't DTRT by refilling all the relevant paragraphs.

;; You could do it a bit more efficiently (and robustly?) with just an
;; auto-fill function, but that doesn't cope with changes other than
;; through self-insertion.  (Using auto-fill and after-change
;; functions together didn't seem winning.)  This could probably
;; benefit from a less-general and faster `fill-paragraph-function',
;; ideally as a primitive.

;; The work is done in a local post-command hook but only if
;; `refill-doit' has been set by the after-change function.  Using
Stefan Monnier's avatar
Stefan Monnier committed
50
;; `post-command-hook' ensures simply that refilling only happens
Dave Love's avatar
Dave Love committed
51 52 53 54
;; once per command.

;; [Per Abrahamsen's maniac.el does a similar thing, but operates from
;; post-command-hook.  I don't understand the statement in it that
Stefan Monnier's avatar
Stefan Monnier committed
55
;; after-change-functions don't work for this purpose; perhaps there was
Dave Love's avatar
Dave Love committed
56 57 58
;; some Emacs bug at the time.  ISTR maniac has problems with
;; whitespace at the end of paragraphs.]

59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
;;; Todo/Bugs:

;; - When deleting the first word on a line, the space after that word tends
;;   to become part of the fill-prefix, causing either wrong filling of the
;;   remaining text, or causing the cursor to move unexpectedly.  Ex:
;;   Start with
;;                I>< blabla
;;
;;   and hit backspace.  We end up with
;;
;;                 ><blabla
;;   instead of
;;                >< blabla
;;
;;   Other example.  Start with
;;
;;     Foo bar blablabla asdgf
;;     word>< asdfas dfasdfasd
;;     asd asdfa sdfasd sdf
;;
;;   and hit M-backspace.  We end up with
;;
;;     Foo bar blablabla asdgf
;;      ><asdfas dfasdfasd asd
;;      asdfa sdfasd sdf

Dave Love's avatar
Dave Love committed
85 86
;;; Code:

Markus Rost's avatar
Markus Rost committed
87 88 89 90
(defgroup refill nil
  "Refilling paragraphs on changes."
  :group 'fill)

91 92 93 94 95 96 97 98 99 100 101 102 103
(defvar refill-ignorable-overlay nil
  "Portion of the most recently filled paragraph not needing filling.
This is used to optimize refilling.")
(make-variable-buffer-local 'refill-ignorable-overlay)

(defun refill-adjust-ignorable-overlay (overlay afterp beg end &optional len)
  "Adjust OVERLAY to not include the about-to-be-modified region."
  (when (not afterp)
    (save-excursion
      (goto-char beg)
      (forward-line -1)
      (if (<= (point) (overlay-start overlay))
	  ;; Just get OVERLAY out of the way
104
	  (move-overlay overlay (point-min) (point-min))
105
	;; Make overlay contain only the region
106 107 108 109
	(move-overlay overlay (overlay-start overlay) (point))))))

(defun refill-fill-paragraph-at (pos &optional arg)
  "Like `fill-paragraph' at POS, but don't delete whitespace at paragraph end."
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
  (save-excursion
    (goto-char pos)
    ;; FIXME: forward-paragraph seems to disregard `use-hard-newlines',
    ;; leading to excessive refilling and wrong choice of fill-prefix.
    ;; might be a bug in my paragraphs.el.
    (forward-paragraph)
    (skip-syntax-backward "-")
    (let ((end (point))
	  (beg (progn (backward-paragraph) (point)))
	  (obeg (overlay-start refill-ignorable-overlay))
	  (oend (overlay-end refill-ignorable-overlay)))
      (unless (> beg pos)      ;Don't fill if point is outside the paragraph.
	(goto-char pos)
	(if (and (>= beg obeg) (< beg oend))
	    ;; Limit filling to the modified tail of the paragraph.
	    (let ( ;; When adaptive-fill-mode is enabled, the filling
		  ;; functions will attempt to set the fill prefix from
		  ;; the fake paragraph bounds we pass in, so set it
		  ;; ourselves first, using the real paragraph bounds.
		  (fill-prefix
		   (if (and adaptive-fill-mode
			    (or (null fill-prefix) (string= fill-prefix "")))
		       (fill-context-prefix beg end)
		     fill-prefix))
		  ;; Turn off adaptive-fill-mode temporarily
		  (adaptive-fill-mode nil))
	      (save-restriction
		(if use-hard-newlines
		    (fill-region oend end arg)
		  (fill-region-as-paragraph oend end arg)))
	      (move-overlay refill-ignorable-overlay obeg (point)))
	  ;; Fill the whole paragraph
	  (save-restriction
	    (if use-hard-newlines
		(fill-region beg end arg)
	      (fill-region-as-paragraph beg end arg)))
	  (move-overlay refill-ignorable-overlay beg (point)))))))
147 148 149 150

(defun refill-fill-paragraph (arg)
  "Like `fill-paragraph' but don't delete whitespace at paragraph end."
  (refill-fill-paragraph-at (point) arg))
Dave Love's avatar
Dave Love committed
151 152

(defvar refill-doit nil
153
  "Non-nil tells `refill-post-command-function' to do its processing.
Stefan Monnier's avatar
Stefan Monnier committed
154
Set by `refill-after-change-function' in `after-change-functions' and
155 156
unset by `refill-post-command-function' in `post-command-hook', and
sometimes `refill-pre-command-function' in `pre-command-hook'.  This
Dave Love's avatar
Dave Love committed
157 158 159 160 161 162 163 164
ensures refilling is only done once per command that causes a change,
regardless of the number of after-change calls from commands doing
complex processing.")
(make-variable-buffer-local 'refill-doit)

(defun refill-after-change-function (beg end len)
  "Function for `after-change-functions' which just sets `refill-doit'."
  (unless undo-in-progress
165
    (setq refill-doit end)))
Dave Love's avatar
Dave Love committed
166 167 168

(defun refill-post-command-function ()
  "Post-command function to do refilling (conditionally)."
169
  (when refill-doit ; there was a change
Dave Love's avatar
Dave Love committed
170
    ;; There's probably scope for more special cases here...
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
    (if (eq this-command 'self-insert-command)
	;; Treat self-insertion commands specially, since they don't
	;; always reset `refill-doit' -- for self-insertion commands that
	;; *don't* cause a refill, we want to leave it turned on so that
	;; any subsequent non-modification command will cause a refill.
	(when (aref auto-fill-chars (char-before))
	  ;; Respond to the same characters as auto-fill (other than
	  ;; newline, covered below).
	  (refill-fill-paragraph-at refill-doit)
	  (setq refill-doit nil))
      (cond
       ((or (eq this-command 'quoted-insert)
	    (eq this-command 'fill-paragraph)
	    (eq this-command 'fill-region))
	nil)
       ((or (eq this-command 'newline)
	    (eq this-command 'newline-and-indent)
	    (eq this-command 'open-line))
	;; Don't zap what was just inserted.
	(save-excursion
	  (beginning-of-line)		; for newline-and-indent
	  (skip-chars-backward "\n")
	  (save-restriction
	    (narrow-to-region (point-min) (point))
	    (refill-fill-paragraph-at refill-doit)))
	(widen)
	(save-excursion
	  (skip-chars-forward "\n")
	  (save-restriction
	    (narrow-to-region (line-beginning-position) (point-max))
	    (refill-fill-paragraph-at refill-doit))))
       (t
	(refill-fill-paragraph-at refill-doit)))
      (setq refill-doit nil))))

(defun refill-pre-command-function ()
  "Pre-command function to do refilling (conditionally)."
  (when (and refill-doit (not (eq this-command 'self-insert-command)))
    ;; A previous setting of `refill-doit' didn't result in a refill,
    ;; because it was a self-insert-command.  Since the next command is
    ;; something else, do the refill now.
    (refill-fill-paragraph-at refill-doit)
Dave Love's avatar
Dave Love committed
213 214
    (setq refill-doit nil)))

215 216
(defvar refill-late-fill-paragraph-function nil)

Dave Love's avatar
Dave Love committed
217 218 219 220 221 222 223 224
;;;###autoload
(define-minor-mode refill-mode
  "Toggle Refill minor mode.
With prefix arg, turn Refill mode on iff arg is positive.

When Refill mode is on, the current paragraph will be formatted when
changes are made within it.  Self-inserting characters only cause
refilling if they would cause auto-filling."
225
  nil " Refill" '(("\177" . backward-delete-char-untabify))
226 227 228 229
  ;; Remove old state if necessary
  (when refill-ignorable-overlay
    (delete-overlay refill-ignorable-overlay)
    (kill-local-variable 'refill-ignorable-overlay))
230
  (when (local-variable-p 'refill-late-fill-paragraph-function)
231 232
    (setq fill-paragraph-function refill-late-fill-paragraph-function)
    (kill-local-variable 'refill-late-fill-paragraph-function))
Dave Love's avatar
Dave Love committed
233
  (if refill-mode
Stefan Monnier's avatar
Stefan Monnier committed
234 235 236
      (progn
	(add-hook 'after-change-functions 'refill-after-change-function nil t)
	(add-hook 'post-command-hook 'refill-post-command-function nil t)
237
	(add-hook 'pre-command-hook 'refill-pre-command-function nil t)
238 239
	(set (make-local-variable 'refill-late-fill-paragraph-function)
	     fill-paragraph-function)
240
	;; This provides the test for recursive paragraph filling.
Stefan Monnier's avatar
Stefan Monnier committed
241 242
	(set (make-local-variable 'fill-paragraph-function)
	     'refill-fill-paragraph)
243 244 245 246
	;; When using justification, doing DEL on 2 spaces should remove
	;; both, otherwise, the subsequent refill will undo the DEL.
	(set (make-local-variable 'backward-delete-char-untabify-method)
	     'hungry)
247 248 249 250 251
	(setq refill-ignorable-overlay (make-overlay 1 1 nil nil t))
	(overlay-put refill-ignorable-overlay 'modification-hooks
		     '(refill-adjust-ignorable-overlay))
	(overlay-put refill-ignorable-overlay 'insert-behind-hooks
		     '(refill-adjust-ignorable-overlay))
Stefan Monnier's avatar
Stefan Monnier committed
252
	(auto-fill-mode 0))
Dave Love's avatar
Dave Love committed
253 254
    (remove-hook 'after-change-functions 'refill-after-change-function t)
    (remove-hook 'post-command-hook 'refill-post-command-function t)
255
    (kill-local-variable 'backward-delete-char-untabify-method)))
Dave Love's avatar
Dave Love committed
256 257 258 259

(provide 'refill)

;;; refill.el ends here