emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: save-match-data woes


From: Stefan Monnier
Subject: Re: save-match-data woes
Date: Fri, 22 Feb 2002 11:43:03 -0500

> I was trying something with ielm (which I use a lot), and found that:
> 
> ELISP> (setq var "12334")
> "12334"
> ELISP> (string-match "34+" var)
> 3
> ELISP> (match-data)
> (3 5)
> 
> ELISP> (match-data)
> (0 0)
> 
> After digging around I've found that ielm has non-protected calls to
> string-match and looking-for, and is also calling pp-to-string, which
> does not preserve the match data either.
> 
> From a recent change to bibtex.el by Eli I suppose the intent is to make
> functions preserve match data unless their interface specifically says
> they don't.

I think this is a wrong approach.  The match-data need only be saved
in a few particular circumstances and I'd rather handle those cases
in a special way.

For ielm, the problem is that the user might invoke any random command
between two inputs, so fixing M-p and M-n is not enough.  Also fixing
"any random command" is just not possible since that command might not
even be part of Emacs.

I.e. it seems better to fix ielm to save the match data before calling
the pretty printer and to restore it before executing the next command.
See patch below.  Any objection ?


        Stefan


--- ielm.el.~1.22.~     Wed Oct 11 17:26:53 2000
+++ ielm.el     Fri Feb 22 11:40:15 2002
@@ -103,6 +103,9 @@
 (defvar *** nil
   "Third-most-recent value evaluated in IELM.")
 
+(defvar ielm-match-data nil
+  "Match data saved at the end of last command.")
+
 ;;; System variables
 
 (defvar ielm-working-buffer nil
@@ -313,6 +316,7 @@
                  (let ((*save *)
                        (**save **)
                        (***save ***))
+                   (set-match-data ielm-match-data)
                    (save-excursion
                      (set-buffer ielm-working-buffer)
                      (condition-case err
@@ -330,7 +334,8 @@
                        (error (setq ielm-result (ielm-format-error err))
                               (setq ielm-error-type "Eval error"))
                        (quit (setq ielm-result "Quit during evaluation")
-                             (setq ielm-error-type "Eval error")))))
+                             (setq ielm-error-type "Eval error"))))
+                   (setq ielm-match-data (match-data)))
                (setq ielm-error-type "IELM error")
                (setq ielm-result "More than one sexp in input"))))
 
@@ -451,6 +456,7 @@
   (make-local-variable '**)
   (setq *** nil)
   (make-local-variable '***)
+  (set (make-local-variable 'ielm-match-data) nil)
 
   ;; font-lock support
   (make-local-variable 'font-lock-defaults)




reply via email to

[Prev in Thread] Current Thread [Next in Thread]