www.delorie.com/gnu/docs/elisp-manual-21/elisp_579.html   search  
Buy the book!

GNU Emacs Lisp Reference Manual

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

34.6.4 Saving and Restoring the Match Data

When you call a function that may do a search, you may need to save and restore the match data around that call, if you want to preserve the match data from an earlier search for later use. Here is an example that shows the problem that arises if you fail to save the match data:

(re-search-forward "The \\(cat \\)")
     => 48
(foo)                   ; Perhaps foo does
                        ;   more searching.
(match-end 0)
     => 61              ; Unexpected result---not 48!

You can save and restore the match data with save-match-data:

Macro: save-match-data body...
This macro executes body, saving and restoring the match data around it.

You could use set-match-data together with match-data to imitate the effect of the special form save-match-data. Here is how:

(let ((data (match-data)))
      ...   ; Ok to change the original match data.
    (set-match-data data)))

Emacs automatically saves and restores the match data when it runs process filter functions (see section 37.9.2 Process Filter Functions) and process sentinels (see section 37.10 Sentinels: Detecting Process Status Changes).

  webmaster   donations   bookstore     delorie software   privacy  
  Copyright 2003   by The Free Software Foundation     Updated Jun 2003