Subject: Re: bug#67462: 30.0.50; prog-fill-reindent-defun does not respect buffer-local fill-paragraph-function

From: Jens Schmidt <[email-masked]>
Date: Sun, 26 Nov 2023 23:52:00 +0100

Dmitry Gutov <[email-masked]> writes:

> On 26/11/2023 23:08, Jens Schmidt via Bug reports for GNU Emacs,
> the Swiss army knife of text editors wrote:
>> diff --git a/lisp/progmodes/prog-mode.el b/lisp/progmodes/prog-mode.el
>> index 37c54a90f42..1bb2b678d94 100644
>> --- a/lisp/progmodes/prog-mode.el
>> +++ b/lisp/progmodes/prog-mode.el
>> @@ -163,7 +163,8 @@ prog-fill-reindent-defun
>>                   (treesit-parser-list)
>>                   (treesit-node-match-p
>>                    (treesit-node-at (point)) 'text t))))
>> -      (if (or treesit-text-node
>> +      (if (or (local-variable-p 'fill-paragraph-function)
>> +              treesit-text-node
>>                 (nth 8 (syntax-ppss))
>>                 (re-search-forward "\\s-*\\s<" (line-end-position) t))
>>             (fill-paragraph argument (region-active-p))
>
> I'm not sure this is going to work well because c-mode (for
> example) also has a buffer-local value of
> fill-paragraph-function.

Uh.  Good point.

> makefile-mode does look special in this regard. Perhaps it's one
> of those modes that don't really use indentation in the same way
> as an average "prog mode" does.
>
> Maybe makefile-mode-map should simply rebind "M-q" back to 'fill-paragraph'.

I tried a cursory grep '(defun .*-fill-paragraph' in lisp/progmodes/.el.
The following functions all seem to work outside of comments:

  cfengine-fill-paragraph
  f90-fill-paragraph
  fortran-fill-paragraph
  makefile-fill-paragraph
  octave-fill-paragraph

I could be wrong with that list, though, and I cannot tell how useful
these functions are outside of comments.  I just looked at docstrings or
for a prominent

  (or (fill-comment-paragraph justify)
      (do-something ...))

pattern.

Finally, there is `python-fill-paragraph', which tries to be smart
w.r.t. context.  Not sure how its behavior outside of comments compares
to the one provided by `prog-fill-reindent-defun'.