]> git.eshelyaron.com Git - emacs.git/commitdiff
; Fix 'thing-at-point' edge case involving overlapping matches
authorEshel Yaron <me@eshelyaron.com>
Sat, 10 Feb 2024 16:30:27 +0000 (17:30 +0100)
committerEshel Yaron <me@eshelyaron.com>
Sat, 10 Feb 2024 16:34:50 +0000 (17:34 +0100)
* lisp/thingatpt.el (thing-at-point-looking-at): When finding a match
that ends before point, continue searching from the beginning of that
match, not its end, in case the match we're looking is overlapping with
this one.
* test/lisp/thingatpt-tests.el
(thing-at-point-looking-at-overlapping-matches): New test.

lisp/thingatpt.el
test/lisp/thingatpt-tests.el

index b532bafff820799b9f0a7d2130fc6db7dfda0c44..83ddc640d35db3d66f6cb5cba154591af68ab0cd 100644 (file)
@@ -621,13 +621,14 @@ Optional argument DISTANCE limits search for REGEXP forward and
 back from point."
   (let* ((old (point))
          (beg (if distance (max (point-min) (- old distance)) (point-min)))
-         (end (and distance (min (point-max) (+ old distance))))
+         (end (if distance (min (point-max) (+ old distance))))
          prev match)
     (save-excursion
       (goto-char beg)
       (while (and (setq prev (point)
                         match (re-search-forward regexp end t))
                   (< (match-end 0) old))
+        (goto-char (match-beginning 0))
         ;; Avoid inflooping when `regexp' matches the empty string.
         (unless (< prev (point)) (forward-char))))
     (and match (<= (match-beginning 0) old (match-end 0)))))
index 56bc4fdc9dca162d21b34faf9afc5b9c3782bdd0..e50738f1122513c6b39cd216c52b0e15c425d981 100644 (file)
@@ -182,6 +182,13 @@ position to retrieve THING.")
       (should (thing-at-point-looking-at "2abcd"))
       (should (equal (match-data) m2)))))
 
+(ert-deftest thing-at-point-looking-at-overlapping-matches ()
+  (with-temp-buffer
+    (insert "foo.bar.baz")
+    (goto-char (point-max))
+    (should (thing-at-point-looking-at "[a-z]+\\.[a-z]+"))
+    (should (string= "bar.baz" (match-string 0)))))
+
 (ert-deftest test-symbol-thing-1 ()
   (with-temp-buffer
     (insert "foo bar zot")