emacs-devel
[Top][All Lists]
Advanced

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

save-match-data woes


From: Juanma Barranquero
Subject: save-match-data woes
Date: Thu, 21 Feb 2002 12:36:58 +0100

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.

So I'm including patches to ielm.el and pp.el protecting the offending
code with save-match-data (the changes seem bigger because of
indentation, but in all cases is just a matter of adding a call to
save-match-data or equivalent).

Incidentally, the above example with ielm still does not work for me
(on a patched Emacs) if I get the second call to match-data via M-p,
because that invokes an offending comint function. AFAICS comint.el does
only a half-hearted attempt to preserve match data. It should be
modified also, but I won't try that unless someone says that it is OK to
do so.

Only somewhat related: several tiny patches I've sent on the past week
or so haven't received any comment. Should I assume silence is
equivalent to rejection, or lack of interest? (Not a complain, just
curiosity)


                                                           /L/e/k/t/u




2002-02-21  Juanma Barranquero  <address@hidden>

        * ielm.el (ielm-return): Protect match data.
        (ielm-is-whitespace): Ditto.
        (ielm-get-old-input): Ditto.

        * emacs-lisp/pp.el (pp-to-string): Ditto.
        (pp-eval-last-sexp): Ditto.



Index: ielm.el
===================================================================
RCS file: /cvsroot/emacs/emacs/lisp/ielm.el,v
retrieving revision 1.22
diff -u -r1.22 ielm.el
--- ielm.el     10 Oct 2000 17:27:38 -0000      1.22
+++ ielm.el     21 Feb 2002 11:03:35 -0000
@@ -225,7 +225,7 @@
          (if (and ielm-dynamic-multiline-inputs
                   (save-excursion
                     (beginning-of-line)
-                    (looking-at comint-prompt-regexp)))
+                    (save-match-data (looking-at comint-prompt-regexp))))
              (save-excursion
                (goto-char (ielm-pm))
                (newline 1)))
@@ -251,7 +251,8 @@
 
 (defun ielm-is-whitespace (string)
   "Return non-nil if STRING is all whitespace."
-  (or (string= string "") (string-match "\\`[ \t\n]+\\'" string)))
+  (or (string= string "")
+      (save-match-data (string-match "\\`[ \t\n]+\\'" string))))
 
 (defun ielm-format-errors (errlist)
   (let ((result ""))
@@ -473,11 +474,12 @@
 (defun ielm-get-old-input nil
   ;; Return the previous input surrounding point
   (save-excursion
-    (beginning-of-line)
-    (if (looking-at comint-prompt-regexp) nil
-      (re-search-backward comint-prompt-regexp))
-    (comint-skip-prompt)
-    (buffer-substring (point) (progn (forward-sexp 1) (point)))))
+    (save-match-data
+      (beginning-of-line)
+      (if (looking-at comint-prompt-regexp) nil
+        (re-search-backward comint-prompt-regexp))
+      (comint-skip-prompt)
+      (buffer-substring (point) (progn (forward-sexp 1) (point))))))
 
 ;;; User command
 
Index: emacs-lisp/pp.el
===================================================================
RCS file: /cvsroot/emacs/emacs/lisp/emacs-lisp/pp.el,v
retrieving revision 1.18
diff -u -r1.18 pp.el
--- emacs-lisp/pp.el    14 Feb 2002 16:47:11 -0000      1.18
+++ emacs-lisp/pp.el    21 Feb 2002 11:03:35 -0000
@@ -44,7 +44,7 @@
   (save-excursion
     (set-buffer (generate-new-buffer " pp-to-string"))
     (unwind-protect
-       (progn
+       (save-match-data
          (lisp-mode-variables nil)
          (set-syntax-table emacs-lisp-mode-syntax-table)
          (let ((print-escape-newlines pp-escape-newlines)
@@ -136,17 +136,18 @@
   (let ((stab (syntax-table)) (pt (point)) start exp)
     (set-syntax-table emacs-lisp-mode-syntax-table)
     (save-excursion
-      (forward-sexp -1)
-      ;; If first line is commented, ignore all leading comments:
-      (if (save-excursion (beginning-of-line) (looking-at "[ \t]*;"))
-         (progn
-           (setq exp (buffer-substring (point) pt))
-           (while (string-match "\n[ \t]*;+" exp start)
-             (setq start (1+ (match-beginning 0))
-                   exp (concat (substring exp 0 start)
-                               (substring exp (match-end 0)))))
-           (setq exp (read exp)))
-       (setq exp (read (current-buffer)))))
+      (save-match-data
+        (forward-sexp -1)
+        ;; If first line is commented, ignore all leading comments:
+        (if (save-excursion (beginning-of-line) (looking-at "[ \t]*;"))
+            (progn
+              (setq exp (buffer-substring (point) pt))
+              (while (string-match "\n[ \t]*;+" exp start)
+                (setq start (1+ (match-beginning 0))
+                      exp (concat (substring exp 0 start)
+                                  (substring exp (match-end 0)))))
+              (setq exp (read exp)))
+          (setq exp (read (current-buffer))))))
     (set-syntax-table stab)
     (if arg
        (insert (pp-to-string (eval exp)))




reply via email to

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