[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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

From: Andreas Röhler
Subject: bug#14303: 24.3; Bug in comment-search-backward
Date: Fri, 17 May 2013 13:14:58 +0200
User-agent: Mozilla/5.0 (X11; Linux i686; rv:17.0) Gecko/20130329 Thunderbird/17.0.5

Am 17.05.2013 12:47, schrieb Leo Liu:
On 2013-05-16 15:12 +0800, Andreas Röhler wrote:
Yes, same thing as with beg-of-defun discussed elsewhere.

+ (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)))

This might find a comment-start inside a string.

Rely at (syntax-ppss)

if nt4 and nth8, goto char nth8

There is possibility of ending up in a string. How about something along
these lines? Thanks. Leo

diff --git a/lisp/newcomment.el b/lisp/newcomment.el
index d55feaa3..79cdc393 100644
--- a/lisp/newcomment.el
+++ b/lisp/newcomment.el
@@ -485,27 +485,30 @@ (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 beg)
+    (while (and (not found) (re-search-backward comment-start-skip limit t))
+      (setq beg (or (match-end 1) (match-beginning 0))
+           end (match-end 0))
+      (when (or (not comment-use-syntax)
+               (and (not (nth 8 (syntax-ppss beg)))
+                    (nth 4 (syntax-ppss end))))
+       (setq found t))
+      (goto-char beg))
+    (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.

The succession of things doesn't look right yet.

if (eq comment-use-syntax nil)

re-search-backward based solution

which is very seldom. Grep shows 4 cases.

Otherwise syntax-ppss - , prog1 and nth 4 goto char nth 8 - or so.
Make sure wrong regexp isn't called then/no wrong matches.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]