Function: thing-at-point-looking-at

thing-at-point-looking-at is a byte-compiled function defined in thingatpt.el.gz.

Signature

(thing-at-point-looking-at REGEXP &optional DISTANCE)

Documentation

Return non-nil if point is in or just after a match for REGEXP.

Set the match data from the earliest such match ending at or after point.

Optional argument DISTANCE limits search for REGEXP forward and back from point.

Source Code

;; Defined in /usr/src/emacs/lisp/thingatpt.el.gz
;; The normal thingatpt mechanism doesn't work for complex regexps.
;; This should work for almost any regexp wherever we are in the
;; match.  To do a perfect job for any arbitrary regexp would mean
;; testing every position before point.  Regexp searches won't find
;; matches that straddle the start position so we search forwards once
;; and then back repeatedly and then back up a char at a time.

(defun thing-at-point-looking-at (regexp &optional distance)
  "Return non-nil if point is in or just after a match for REGEXP.
Set the match data from the earliest such match ending at or after
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)))))