GNU bug report logs - #34214
25.3; minibuffer function help in lisp modes changes match-data

Previous Next

Package: emacs;

Reported by: "Miguel V. S. Frasson" <mvsfrasson <at> gmail.com>

Date: Sat, 26 Jan 2019 23:44:02 UTC

Severity: wishlist

Found in version 25.3

Fixed in version 28.1

Done: Lars Ingebrigtsen <larsi <at> gnus.org>

Bug is archived. No further changes may be made.

Full log


View this message in rfc822 format

From: Philipp Stephani <p.stephani2 <at> gmail.com>
To: "Miguel V. S. Frasson" <mvsfrasson <at> gmail.com>
Cc: 34214 <at> debbugs.gnu.org
Subject: bug#34214: 25.3; minibuffer function help in lisp modes changes match-data
Date: Sun, 27 Jan 2019 14:58:31 +0100
Am So., 27. Jan. 2019 um 00:44 Uhr schrieb Miguel V. S. Frasson
<mvsfrasson <at> gmail.com>:
>
> Programming an Emacs lisp program that uses match-data, debugging pieces
> by hand, I realized that managing matchs was a nightmare.  At first I
> thought that navigation commands like C-a or C-M-f were messing
> match-data (as one could think they use searching).  It could be.  But
> for sure, that very handy help line that shows function arguments are
> messing match data, making difficult to program Emacs lisp.
>
> How to reproduce:
>
> 1) emacs -Q
>
> 2) type or paste the following code on *scratch* buffer
>    (must be a lisp programming mode):
>
> (string-match "f" "foo")
> (match-string 0 "foo")
>
> Normal behavior
>
> 3) Go to end of string-match line, evaluate with C-x C-e
>    C-n   (or down) so that point goes to end of 2nd line
>    *Very quickly* (do not let help for match-string appear), type
>    C-b C-f   (or left right)
>    and evaluate:   C-x C-e
>    I get correct result "f"
>
> Now the bug
>
> 4) Go to end of string-match line, evaluate with C-x C-e
>    C-n    (getting to end of 2nd line)
>    C-b    (or left)
>    ...wait minibuffer function help for match-string show...
>    C-f    (or right)
>    C-x C-e
>    I get ERROR 'args-out-of-range "foo" 15 21'
>
> What I observed:
>
> Steps 3) and 4) are (almost) identical except for time spent between C-b
> and C-f commands. Error is probably due to a match-data change, probably
> caused by minibuffer function help mechanism.
>
> What I expect:
>
> No unnecessary side-effects like change match-data should happen while
> simply navigating through code.  Lisp modes should protect searches on
> background with save-match-data, because it makes a nightmare to
> evaluate code by hand.
>

Any function is allowed to change the match data, see
https://www.gnu.org/software/emacs/manual/html_node/elisp/Match-Data.html:
"Notice that all functions are allowed to overwrite the match data
unless they're explicitly documented not to do so.".
In general you almost always want to immediately bind the match
results to variables, like so:

(when (string-match "f" "foo")
  (let ((match (match-string 0 "foo")))
    ...
    match))

Evaluating the entire 'when' form will then work as intended.




This bug report was last modified 3 years and 237 days ago.

Previous Next


GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997,2003 nCipher Corporation Ltd, 1994-97 Ian Jackson.