[comp.emacs] match-beginning/match-end error

johnd@Stardent.COM (John R. Dennis @stardent) (02/24/90)

I went to use the emerge facility (emerge.el) that was posted a while back by
Dale R. Worley. But the elisp code generated an error that I tracked down to
the fact that a regular expression search had been done that included optional
matches. To detect if there was a match for a particular sub-expression the
elisp code was relying on the fact that (match-beginning n) would return nil
if there was no match for sub-expression n. We are using GNU emacs version
18.50. I looked at the source in search.c and tracked the problem to:

static Lisp_Object
match_limit (num, beginningp)
     Lisp_Object num;
     int beginningp;
{
  register int n;

  CHECK_NUMBER (num, 0);
  n = XINT (num);
  if (n < 0 || n >= RE_NREGS)
    args_out_of_range (num, make_number (RE_NREGS));
  if (search_regs.start[n] < 0)
    error ("No match for \\(...\\) register %d", n);
  return (make_number ((beginningp) ? search_regs.start[n]
		                    : search_regs.end[n]));
}

If there is no match it generates an error. Now I have often had problems in
the past when trying to write elisp code that had optional matches in regular
expression searches because there was no way to tell whether a match was valid
or not. It had always seemed to me that the match functions should return nil
in this cases and not an error. 

Now it also seems to me that the code that Dale wrote must have been relying
on the 'return nil' behavior to work which implies that either an earlier or
later version of emacs (than 18.50) must have returned nil when there was no
match.

I replaced the error code in the match_limit function above with return(Qnil)
and Dale's code worked wonderfully. But I'm a little worried whether I'm going
to break other elisp code that relies on the 'generate error' behavior.

Can someone explain what the behavior should be and why there seems to be a
difference between the version Dale must have been using and the one we are
running with. Somehow I can't really believe that something as fundamental as
match-beginning and match-end would have changed their return values anytime
in the recent past. By the way it also seems to me that there is no other way
to detect whether a match exists other than to rely on the return from the
match functions (if there were one would not have to change the built-in
functions written in C).
-- 
John Dennis
E     Mail: johnd@Stardent.COM | {uunet,convex,decvax}!stardent!johnd
Snail Mail: Stardent Computer, 85 Wells Avenue Newton, MA 02159
Voice Mail: <Work>(617) 964-6228 x243 <Home>(508)366-7976