Subject: fill-comment-paragraph fails (e.g.) in perl-mode POD

From: Bob Rogers <[email-masked]>
Date: Tue, 21 Jul 2009 23:49:51 -0400

To reproduce (in trunk or 23.0.96):

   1.  Stuff the following six lines of "code" (two of them blank) into
the "foo.pl" file:

=head1 Testing

This is a test case line with lots of stupid text.
This is a test case line with a C<#> and lots of stupid text.

=cut

   2.  "emacs -Q foo.pl"

   3.  Move the cursor to any point in the fourth line (the one with
"C<#>") and type "M-q".  This will produce the following user-unfriendly
error:

	Search failed: "\\(^\\|\\s-\\);?#+ *"

   This directed my attention to the following code near the end of
fill-comment-paragraph:

	    (save-excursion
	      (goto-char beg)
	      (if (looking-at fill-prefix)
		  nil
		(re-search-forward comment-start-skip)))

which seems to make two bad assumptions:  That comment-start-skip is
never nil (though its documentation asserts otherwise), and that it
matches before the end of the buffer (which is trivially false at EOB).
The attached patch removes these assumptions, and makes the "Search
failed" error go away.

   Unfortunately, this isn't enought to fix perl-mode:  Instead of
signalling an error, it does nothing, which is not right, either.  The
deeper problem there is that M-q in perl-mode doesn't distinguish
between POD syntax and Perl code proper, and tries to treat the "#" as
the start of a single-line comment, even though that is not possible in
POD.  But fixing that would require a more intrusive patch, and the
fill-comment-paragraph code is wrong in any case, so I submit this fix
separately on the chance that it might be considered suitable for 23.1.

					-- Bob Rogers
					   http://www.rgrjr.com/

Index: lisp/textmodes/fill.el
===================================================================
RCS file: /sources/emacs/emacs/lisp/textmodes/fill.el,v
retrieving revision 1.223
diff -c -r1.223 fill.el
*** lisp/textmodes/fill.el	8 Apr 2009 04:02:35 -0000	1.223
--- lisp/textmodes/fill.el	22 Jul 2009 03:44:20 -0000
***************
*** 976,986 ****
  	    (max comstart beg) end justify nil
  	    ;; Don't canonicalize spaces within the code just before
  	    ;; the comment.
! 	    (save-excursion
! 	      (goto-char beg)
! 	      (if (looking-at fill-prefix)
! 		  nil
! 		(re-search-forward comment-start-skip))))
  	   ;; Make sure we don't return nil.
  	   t))))))
  
--- 976,987 ----
  	    (max comstart beg) end justify nil
  	    ;; Don't canonicalize spaces within the code just before
  	    ;; the comment.
! 	    (and comment-start-skip
! 		 (save-excursion
! 		   (goto-char beg)
! 		   (if (looking-at fill-prefix)
! 		       nil
! 		       (re-search-forward comment-start-skip nil t)))))
  	   ;; Make sure we don't return nil.
  	   t))))))