Subject: Re: bug#14303: 24.3; Bug in comment-search-backward

From: Andreas Röhler <[email-masked]>
Date: Wed, 15 May 2013 18:13:47 +0200

Am 15.05.2013 13:33, schrieb Leo Liu:
> On 2013-04-29 21:27 +0800, Leo Liu wrote:
>> Open a new buffer in octave-mode and insert the following line:
>>
>>    x="#abc"
>>
>> Move point to the end of the inserted line and
>>
>>    M-: (comment-search-backward)
>>
>> this moves point inside the string.
>
> So it is no longer fine for comment-search-backward to end up in a
> comment or string any more.
>
> How about something like this?
>
> diff --git a/lisp/newcomment.el b/lisp/newcomment.el
> index d55feaa3..65182c1b 100644
> --- a/lisp/newcomment.el
> +++ b/lisp/newcomment.el
> @@ -485,27 +485,25 @@ (defun comment-search-backward (&optional limit noerror)
>   Moves point to inside the comment and returns the position of the
>   comment-starter.  If no comment is found, moves point to LIMIT
>   and raises an error or returns nil if NOERROR is non-nil."
> -  ;; FIXME: If a comment-start appears inside a comment, we may erroneously
> -  ;; stop there.  This can be rather bad in general, but since
> -  ;; comment-search-backward is only used to find the comment-column (in
> -  ;; comment-set-column) and to find the comment-start string (via
> -  ;; comment-beginning) in indent-new-comment-line, it should be harmless.
> -  (if (not (re-search-backward comment-start-skip limit t))
> -      (unless noerror (error "No comment"))
> -    (beginning-of-line)
> -    (let* ((end (match-end 0))
> -	   (cs (comment-search-forward end t))
> -	   (pt (point)))
> -      (if (not cs)
> -	  (progn (beginning-of-line)
> -		 (comment-search-backward limit noerror))
> -	(while (progn (goto-char cs)
> -		      (comment-forward)
> -		      (and (< (point) end)
> -			   (setq cs (comment-search-forward end t))))
> -	  (setq pt (point)))
> -	(goto-char pt)
> -	cs))))
> +  (let (found end)
> +    (while (and (not found) (re-search-backward comment-start-skip limit t))
> +      (setq end (match-end 0))
> +      (or (nth 8 (syntax-ppss)) (setq found t)))
> +    (if (not found)
> +	(unless noerror (error "No comment"))
> +      (beginning-of-line)
> +      (let ((cs (comment-search-forward end t))
> +	    (pt (point)))
> +	(if (not cs)
> +	    (progn (beginning-of-line)
> +		   (comment-search-backward limit noerror))
> +	  (while (progn (goto-char cs)
> +			(comment-forward)
> +			(and (< (point) end)
> +			     (setq cs (comment-search-forward end t))))
> +	    (setq pt (point)))
> +	  (goto-char pt)
> +	  cs)))))
>
>   (defun comment-beginning ()
>     "Find the beginning of the enclosing comment.
>
>
>
>

syntax-ppss is reliable, while re-search-backward comment-start-skip might stop inside a string etc.

backward-line, end-of-line
if nt4 and nth8, goto char nth8

that's nearly all
as done consider limit of search, sure.

Watching with interest,

Andreas