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

From: Dmitry Gutov <[email-masked]>
Date: Fri, 1 Dec 2023 01:56:40 +0200

On 30/11/2023 22:40, Jens Schmidt wrote:
>> If so, perhaps we should offer a user option to let users choose which
>> method to use, because some might prefer the new behavior?
> I'd be fine with that, and also with making the new behavior the
> default.  A reference to that user option would also nicely complete the
> NEWS entry, which IMO currently is a bit weak in mentioning the possible
> consequences of the new feature.
> 
> Dmitry, what do you think?

I think we should try to see whether those custom functions are better, 
and in which cases.

makefile-mode is actually an odd case because while it inherits from 
prog-mode, it doesn't quite satisfy an informal definition of what 
should be prog modes: its value of indent-line-function is 
#'indent-to-left-margin, which looks more like a text mode.

As a consequence, prog-fill-reindent-defun usually does nothing good 
outside of strings and comments in makefile-mode. Since I'm guessing the 
idea of ceasing to derive from prog-mode right away is not going to fly, 
we can add exceptions like this (with a docstring update, of course):

diff --git a/lisp/progmodes/prog-mode.el b/lisp/progmodes/prog-mode.el
index 37c54a90f42..1927de7de2c 100644
--- a/lisp/progmodes/prog-mode.el
+++ b/lisp/progmodes/prog-mode.el
@@ -165,7 +165,10 @@ prog-fill-reindent-defun
                   (treesit-node-at (point)) 'text t))))
        (if (or treesit-text-node
                (nth 8 (syntax-ppss))
-              (re-search-forward "\\s-*\\s<" (line-end-position) t))
+              (re-search-forward "\\s-*\\s<" (line-end-position) t)
+              (memq indent-line-function
+                    '(indent-relative
+                      indent-to-left-margin)))
            (fill-paragraph argument (region-active-p))
          (beginning-of-defun)
          (let ((start (point)))

or even just this:

diff --git a/lisp/progmodes/prog-mode.el b/lisp/progmodes/prog-mode.el
index 37c54a90f42..0ff3701f8fc 100644
--- a/lisp/progmodes/prog-mode.el
+++ b/lisp/progmodes/prog-mode.el
@@ -165,7 +165,8 @@ prog-fill-reindent-defun
                   (treesit-node-at (point)) 'text t))))
        (if (or treesit-text-node
                (nth 8 (syntax-ppss))
-              (re-search-forward "\\s-*\\s<" (line-end-position) t))
+              (re-search-forward "\\s-*\\s<" (line-end-position) t)
+              (not beginning-of-defun-function))
            (fill-paragraph argument (region-active-p))
          (beginning-of-defun)
          (let ((start (point)))

because defun navigation is not set up there either.

fortran, octave and cfengine, however, could use rebinding or user 
options (I can't really form an opinion on which of the behaviors is 
more useful). cfengine doesn't set fill-paragraph-function either, so 
the required changes would be a little less trivial than for the others. 
But using the prog-mode's binding by default is probably better for 
consistency.