Subject: bug#15004: 24.3; fill-paragraph on perl-mode indented comment at start of buffer

From: Mauro Aranda <[email-masked]>
Date: Wed, 21 Sep 2022 09:32:53 -0300

Kevin Ryde <[email-masked]> writes:

 > Starting from emacs -Q,
 >
 >  (progn
 >    (switch-to-buffer "xx")
 >    (erase-buffer)
 >    (insert " # aa aa\n")
 >    (insert " # bb bb\n")
 >    (insert " # cc cc\n")
 >    (insert " # dd dd\n")
 >    (goto-char (point-min))
 >    (perl-mode)
 >    (fill-paragraph nil))
 >
 > gives
 >
 >  # aa aa
 >  # bb bb cc cc dd dd
 >
 > whereas I hoped the fill-paragraph would include the first line too,
 >
 >  # aa aa bb bb cc cc dd dd

I can reproduce this on current master.

 > The problem is only if the lines are at the start of the buffer.

Indeed, but that seems to be out of sheer luck: somehow the narrowing
goes from the newline previous to the start of comment when the lines
are not at the start of the buffer.

 > I couldn't tell where it might go wrong except that it might be related
 > to perl-mode using comment-start-skip "\\(^\\|\\s-\\);?#+ *" which has a
 > \\(\\) group to demand preceding whitespace if not at start of line.
 > If you change it to
 >
 >   (set (make-local-variable 'comment-start-skip) ";?#+[ \t]*")
 >
 > then the fill correctly includes the first line.
 >

I don't know why comment-start-skip is set this way in perl-mode.  It
tries to do something more than just match the start of a comment and
skip to the body, and that confuses me.

Maybe it can be simplified to just "#+[ \t]*".  Light testing shows no
problem with doing that.  In fact, by no demanding preceding whitespace,
the following works as expected:

sub foo {
     $bar = shift;#Just testing.
}

With point anywhere inside the function, C-M-q moves the comment to
comment-column.