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

From: Stefan Monnier <[email-masked]>
Date: Fri, 23 May 2025 11:04:48 -0400

> Given that this is a first version, I have not added any reference to
> the manuals.  If you think it makes sense, please let me know and I'll
> modify the patch accordingly.

Maybe a short version of the explanation you give below would be good to
have in the manual (tho Eli suggests a URL instead, so maybe that's
good enough?).

> +(defun fill-paragraph-semlf (&optional justify)
> +  "Fill paragraph at or after point using semantic linefeeds.
> +
> +This function ensures that a newline character follows every
> +sentence, as punctuated by a period (.), exclamation mark (!), or
> +question mark (?).

This seems inaccurate: it just uses whichever definition of sentence is
used by `forward-sentence`, so it may ignore some of those chars or pay
attention to others.

> +If JUSTIFY is non-nil (interactively, with prefix argument), justify as
> +well.  If `sentence-end-double-space' is non-nil, then period followed
> +by one space does not end a sentence, so don't break a line there.  The
> +variable `fill-column' controls the width for filling."

I'd move the "The" to the last line.  🙂

> +  (interactive "P")
> +  (save-excursion
> +    (let ((end (progn
> +		 (fill-forward-paragraph 1)
> +		 (backward-word)
> +		 (end-of-line)
> +		 (point)))
> +	  (start (progn
> +		   (fill-forward-paragraph -1)
> +		   (forward-word)
> +		   (beginning-of-line)
> +		   (point)))
> +	  pfx)
> +      (with-restriction start end
> +	(let ((fill-column (point-max)))
> +	  (setq pfx (or (fill-region-as-paragraph (point-min) (point-max)) "")))
> +	(goto-char (point-min))
> +	(while (not (eobp))
> +	  (let ((fill-prefix pfx))
> +	    (fill-region-as-paragraph (point)
> +				      (progn (forward-sentence) (point))
> +				      justify))
> +	  (when (and (> (point) (line-beginning-position))
> +		     (< (point) (line-end-position)))
> +	    (delete-horizontal-space)
> +	    (newline)
> +	    (insert pfx))))))
> +  t)

Please try and separate it into a `fill-region-semlf` function and then
another one which applies it to a paragraph, so that it can also be used
to fill a specific user-specified region (or the whole buffer).

        Stefan