3

Motivation: Using the defaults, Auto Fill mode seems not as useful as I might have hoped: If I insert a sentence in the middle of a paragraph, only the current line is re-filled. When I insert a sentence, I want the entire paragraph to be re-filled.

Question: How can I set auto-fill-function (or perhaps normal-auto-fill-function) in my .emacs file so that the paragraph is re-filled whenever a single line overflows? I tried setting it to fill-paragraph, but then I cannot insert any spaces at the end of a paragraph (e.g., to add another word).

More details: I primarily use Auto Fill mode in the AUCTeX major mode for LaTeX.

The built-in Emacs documentation for auto-fill-mode states:

When auto-fill-mode is on, the auto-fill-function variable is non-nil.

The value of normal-auto-fill-function specifies the function to use for auto-fill-function when turning Auto Fill mode on.

The documentation for the normal-auto-fill-function variable says that it is the function to use for auto-fill-function if Auto Fill mode is turned on, and that the initial value is do-auto-fill.

2 Answers2

2

You might like to try refill-mode. But in general, it's just tricky to make such a feature work well. Another approach is to only do the refill as part of the redisplay (i.e. without affecting the buffer's actual content). For that, try setting word-wrap or enabling visual-line-mode.

Stefan
  • 27,908
  • 4
  • 53
  • 82
1

For LaTeX files you can try (requires AUCTeX)

(add-hook 'LaTeX-mode-hook '(lambda ()
                  (setq auto-fill-function 'LaTeX-fill-paragraph)))

but use it with caution.

giordano
  • 8,087
  • 3
  • 23
  • 48
  • This works great for editing inside a paragraph, but refuses to let me insert a space (to start a new word) at the end of a paragraph. I'm having better luck with `(setq auto-fill-function (lambda () (if (looking-at "\n") (do-auto-fill) (LaTeX-fill-paragraph))))` – Matt Kramer May 22 '20 at 03:57