Subject: bug#15251: 24.3.50; do-auto-fill "continues" comment from inside a string

From: Dmitry Gutov <[email-masked]>
Date: Sun, 29 Sep 2013 06:59:17 +0300

Dmitry Gutov <[email-masked]> writes:

> In certain conditions, if I press SPC, and a string on the current line
> contains text matching `comment-start-skip', the filling is performed,
> and the newly created line starts with a comment.
>
> Examples (point is at |, fill-column is 70):
>
> ruby-mode:
>
> aa = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa#{}a" a|
>
> press SPC =>
>
> aa = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa#{}a"
>                                 #a
>
> js-mode:
>
> aa = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa//a" aa|
>
> press SPC =>
>
> aa = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa//a"
> //aa 

I've tracked this to misbehaving `comment-beginning'. Since we start
outside of string, and the comment char(s) are inside the string on the
same line, the check comparing the face at point to
`font-lock-string-face' doesn't work.

And in ruby-mode's case, since interpolations use a different face, even
putting a similar guard to check at `cs' won't help.

As I see it, we either have to use `syntax-ppss' here, or trust
font-lock. The latter means not skipping other checks even if
`comment-end-skip' is defined and doesn't match at point, and also
only trusting "let's assume ... if we're on the same line" when
font-lock-mode is disabled.

How does this change look? Seems to work fine.

(As an aside, I'm having hard time understanding which search is
supposed to have set the match data used by `(match-end 0)' there. Is it
`comment-search-backward'? Or `(looking-at comment-end-skip)'?)

=== modified file 'lisp/newcomment.el'
--- lisp/newcomment.el	2013-06-18 17:57:56 +0000
+++ lisp/newcomment.el	2013-09-29 03:41:18 +0000
@@ -526,12 +526,13 @@
 	      (and
 	       ;; For modes where comment-start and comment-end are the same,
 	       ;; the search above may have found a `ce' rather than a `cs'.
-	       (or (if comment-end-skip (not (looking-at comment-end-skip)))
-		   ;; Maybe font-lock knows that it's a `cs'?
+               (if comment-end-skip (not (looking-at comment-end-skip)))
+	       (or ;; Maybe font-lock knows that it's a `cs'?
 		   (eq (get-text-property (match-end 0) 'face)
 		       'font-lock-comment-face)
-		   (unless (eq (get-text-property (point) 'face)
-			       'font-lock-comment-face)
+		   (unless (or (eq (get-text-property (point) 'face)
+                                   'font-lock-comment-face)
+                               font-lock-mode)
 		     ;; Let's assume it's a `cs' if we're on the same line.
 		     (>= (line-end-position) pt)))
 	       ;; Make sure that PT is not past the end of the comment.