]> git.eshelyaron.com Git - emacs.git/commitdiff
; Ensure 'thing-at-point-looking-at' finds full match
authorEshel Yaron <me@eshelyaron.com>
Wed, 31 Jan 2024 20:37:18 +0000 (21:37 +0100)
committerEshel Yaron <me@eshelyaron.com>
Thu, 1 Feb 2024 11:00:13 +0000 (12:00 +0100)
* lisp/thingatpt.el (thing-at-point-looking-at): Regexp-search from
the beginning forward, instead of the other way around.

* test/lisp/thingatpt-tests.el (thing-at-point-test-data): Add tests.

(Bug#68762)

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

index 323d3d1cf6c9ca418f86e66d18d966b90fb15ad1..b532bafff820799b9f0a7d2130fc6db7dfda0c44 100644 (file)
@@ -619,36 +619,19 @@ point.
 
 Optional argument DISTANCE limits search for REGEXP forward and
 back from point."
-  (save-excursion
-    (let ((old-point (point))
-         (forward-bound (and distance (+ (point) distance)))
-         (backward-bound (and distance (- (point) distance)))
-         match prev-pos new-pos)
-      (and (looking-at regexp)
-          (>= (match-end 0) old-point)
-          (setq match (point)))
-      ;; Search back repeatedly from end of next match.
-      ;; This may fail if next match ends before this match does.
-      (re-search-forward regexp forward-bound 'limit)
-      (setq prev-pos (point))
-      (while (and (setq new-pos (re-search-backward regexp backward-bound t))
-                  ;; Avoid inflooping with some regexps, such as "^",
-                  ;; matching which never moves point.
-                  (< new-pos prev-pos)
-                 (or (> (match-beginning 0) old-point)
-                     (and (looking-at regexp)  ; Extend match-end past search start
-                          (>= (match-end 0) old-point)
-                          (setq match (point))))))
-      (if (not match) nil
-       (goto-char match)
-       ;; Back up a char at a time in case search skipped
-       ;; intermediate match straddling search start pos.
-       (while (and (not (bobp))
-                   (progn (backward-char 1) (looking-at regexp))
-                   (>= (match-end 0) old-point)
-                   (setq match (point))))
-       (goto-char match)
-       (looking-at regexp)))))
+  (let* ((old (point))
+         (beg (if distance (max (point-min) (- old distance)) (point-min)))
+         (end (and 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))
+        ;; Avoid inflooping when `regexp' matches the empty string.
+        (unless (< prev (point)) (forward-char))))
+    (and match (<= (match-beginning 0) old (match-end 0)))))
+
 
 ;;   Email addresses
 (defvar thing-at-point-email-regexp
index ba51f375cc65373548659e4aed0b5b53d14bd17b..56bc4fdc9dca162d21b34faf9afc5b9c3782bdd0 100644 (file)
@@ -92,6 +92,8 @@
     ("1@example.com" 1 email "1@example.com")
     ;; email addresses user portion containing dots
     ("foo.bar@example.com" 1 email "foo.bar@example.com")
+    ("foo.bar@example.com" 5 email "foo.bar@example.com")
+    ("  fo.ba@example.com" 6 email "fo.ba@example.com")
     (".foobar@example.com" 1 email nil)
     (".foobar@example.com" 2 email "foobar@example.com")
     ;; email addresses domain portion containing dots and dashes