3

I have turn-on-auto-fill as part of my text-mode-hook. This works great for new documents, but when I open a buffer that is not filled with hard breaks I rarely want to start doing so to a single paragraph just because I make one edit to the buffer.

Is there a way to scan the whole buffer after opening to see whether it already is auto-filled? And turn on auto-fill-mode in that case but leave it off otherwise?

I've searched EmacsWiki and MELPA Stable and didn't see anything relevant.

  • 1
    Since there's no one, objective way to decide "file is/is not already auto-filled," you'll need to decide on some heuristics that Emacs can use to decide if the file is "sufficiently" auto-filled to count it as such. For example: if any paragraph has a newline within it, does it count? If any paragraph has, say, more than 80 characters between newlines, does it count? Once you've decided what heuristic to use, it shouldn't be hard to tell Emacs to apply it. – Dan Sep 8 '16 at 18:53
  • The heuristic I had in mind is if you ran fill-region on the whole buffer, would the contents change? If no, then the buffer is already auto-filled, and auto-fill-mode should be enabled. – Michael Hoffman Sep 8 '16 at 19:23
  • Unfortunately, that won't work: fill-region will modify the buffer even if it makes no actual changes. Aside from that, if your fill-column value differs even a little from that used when the file was last modified, you'll get changes even though it's already been auto-filled. – Dan Sep 8 '16 at 19:25
  • 2
    @MichaelHoffman what you can do is to write a function that count char length per line and compare to the value of fill-colomn – Xah Lee Sep 8 '16 at 20:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.