Path: utzoo!utgpu!water!watmath!clyde!bellcore!rutgers!mit-eddie!inria.inria.fr!shapiro From: shapiro@inria.inria.fr (Marc Shapiro) Newsgroups: comp.emacs Subject: Function needed: is point within regexp? Message-ID: <8807070659.AA07832@blueberry.inria.fr> Date: 7 Jul 88 11:15:39 GMT Sender: daemon@eddie.MIT.EDU Organization: INRIA, BP 105, 78153 Le Chesnay Cedex, France telephone +33(1)39-63-55-11, telex 697033 F, telecopy +33(1)39-63-53-30 Lines: 64 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]bc" and point is on the "d" of "zzabAbcd" then the function should return an error. If point is on the "A", then it should return t, and (match-beginning 0) and (match-end 0) should return the locations of the "a" and "d" 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.