Path: utzoo!attcan!uunet!mcvax!inria!blueberry!shapiro
From: shapiro@blueberry.inria.fr (Marc Shapiro)
Newsgroups: comp.emacs
Subject: Function needed: is point within regexp?
Message-ID: <729@inria.UUCP>
Date: 7 Jul 88 06:53:58 GMT
Sender: news@inria.UUCP
Reply-To: shapiro@inria.inria.fr
Distribution: comp
Lines: 69

I am hacking a new, much-improved bibtex-mode for GNU Emacs.  For
this, I need a function which checks if point is *within* a certain
regular expression.

For instance, if the regexp is "^.*$" the function should return t,
and (match-beginning 0) and (match-end 0) should return the location
of the beginning and end of the current line, respectively.  If the
regexp is "ab[aA]ba" and point is on the "c" of "zzabAbac" then the
function should return an error.  If point ia on the "A", then it
should return t, and (match-beginning 0) and (match-end 0) should
return the locations of the first "a" and of "c" respectively.

Neither looking-at, nor re-search-forward, nor re-search-backward fill
the bill.

The function bibtex-enclosing-regexp enclosed below does the trick,
but in a very stupid way.  It moves backwards by an arbitrary number
of characters, and then repeatedly calls re-search-forward until a
match is found, and its boundaries enclose the original point.

Surely there is a better way to do this!  Any ideas?


						Marc Shapiro

INRIA, B.P. 105, 78153 Le Chesnay Cedex, France.  Tel.: +33 (1) 39-63-53-25
e-mail: shapiro@inria.inria.fr or: ...!mcvax!inria!shapiro


---- cut here --------------------------

(defvar bibtex-search-limit 1000
  "Search for the beginning and end of the current bibtex entry is limited to so many chars from point.")

(defun bibtex-enclosing-regexp (regexp)
  "Search for REGEXP enclosing point.
Point does not move; use match-beginning and match-end to learn the
extent of the regexp.

[Doesn't something like this exist already?]"

  (interactive "sRegexp: ")
  (let ((where (point))
	(right (min (point-max)
		  (+ (point) bibtex-search-limit)))
	(left (max (point-min)
		  (- (point) bibtex-search-limit))))
    (save-excursion
      (goto-char left)
      (re-search-forward regexp right nil 1)
      (if (> (match-beginning 0) where)
	  (signal 'search-failed (list regexp)))	  
      (while (<= (match-end 0) where)
	(re-search-forward regexp right nil 1)
	(if (> (match-beginning 0) where)
	    (signal 'search-failed (list regexp))))
      )))

--- the end ----------------------------

PS.  bibtex-enclosing-regexp could be made better by adding optional
arguments, for "left" and "right" limits, and by allowing it to return
nil if the regexp is not found, instead of signalling.  However, as
is, it works.

						Marc Shapiro

INRIA, B.P. 105, 78153 Le Chesnay Cedex, France.  Tel.: +33 (1) 39-63-53-25
e-mail: shapiro@inria.inria.fr or: ...!mcvax!inria!shapiro