Subject: bug#78561: [PATCH] Add semantic linefeed support for paragraph filling

From: Stefan Monnier <[email-masked]>
Date: Tue, 27 May 2025 12:36:10 -0400

>> Yes, I've had this in my TODO for many years.  🙁
> I'll try to implement it :)

For some definition of "it".

>>> So, do we really want a specific `fill-region-semlf' function?
>> Actually, I think I want a `fill-region-as-paragraph-semlf'.
>> And maybe then some way to tell the `fill.el` code to use that instead
>> of the default `fill-region-as-paragraph`.
> OK, so here is my plan:

That plan has a much larger scope than semantic linefeed, so
I recommend you first finish the current patch and take on that
plan afterwards.
What I'm asking is a fairly small refactoring of the code you sent,
I think.

> First, I'll give a closer look to all the "fill" functions that Emacs
> currently provides to understand how every thing fits together and what
> is and what is not already supported.

Sounds good.  I can give you a quick summary of what I know.
`fill-paragraph-function`s usually serve as either:

- Some way for the major mode to teach the fill code how to stay within
  a major-mode-specific notion of paragraph (e.g. fill within comments
  or within strings).  This use has made somewhat obsolete by
  `fill-paragraph-forward-function` but it's still pretty common.

- Some way for the major mode to teach the fill code about special
  line-wrapping conventions (e.g. add a trailing \ on the previous line
  to fill C strings, add a leading SPC or TAB to fill rfc822 headers,
  ..) or to fine tune (adaptive-)fill-prefix.

- Actually change where we break lines for example to fill code in
  a "smart" way (like `smie-auto-fill` does).

- It can also be used to prevent filling in some parts
  (e.g. titles/headlines).

> Then, I'll send a new patch to support applying a
> `fill-paragraph-function' on a region "as-paragraph" (similar to the
> behavior of `fill-region-as-paragraph') and "per-paragraph" (similar to
> executing `fill-paragraph' for each paragraph within the region).

I think in general these are impossible because we don't know what the
`fill-paragraph-function` actually does.  My TODO is instead to provide
different hooks for the above different uses (some hooks already exist
such as `comment-line-break-function` but often they need to be
generalized) to try and make `fill-paragraph-function` effectively
obsolete (without necessarily marking it officially as obsolete).

        Stefan