From 754bbf7a25a8dda49b5d08ef0d0443bbf5af0e36 Mon Sep 17 00:00:00 2001 From: Craig Jennings Date: Sun, 7 Apr 2024 13:41:34 -0500 Subject: new repository --- devdocs/elisp/saving-match-data.html | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 devdocs/elisp/saving-match-data.html (limited to 'devdocs/elisp/saving-match-data.html') diff --git a/devdocs/elisp/saving-match-data.html b/devdocs/elisp/saving-match-data.html new file mode 100644 index 00000000..9aaa895b --- /dev/null +++ b/devdocs/elisp/saving-match-data.html @@ -0,0 +1,19 @@ +

Saving and Restoring the Match Data

When you call a function that may 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)                   ; 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. The return value is the value of the last form in body.

+

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)))
+  (unwind-protect
+      …   ; 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 Filter Functions) and process sentinels (see Sentinels).

+

+ Copyright © 1990-1996, 1998-2022 Free Software Foundation, Inc.
Licensed under the GNU GPL license.
+ https://www.gnu.org/software/emacs/manual/html_node/elisp/Saving-Match-Data.html +

+
-- cgit v1.2.3