Subject: Re: bug#73018: 31.0.50; wdired + replace-regexp only modifies the visible portion of the buffer

From: Stefan Monnier <[email-masked]>
Date: Sun, 15 Sep 2024 09:04:40 -0400

>> > We should probably use something like
>> >
>> >     ptrdiff_t
>> >     search_regs_last_reg (void)
>> >     {
>> >       ptrdiff_t i = search_regs.num_regs - 1;
>> >       while (i >= 0 && search_regs.start[i] < 0)
>> >         i--;
>> >       return i;
>> >     }
>> >
>> > instead of `search_regs.num_regs`.
>> 
>> And maybe we should consider changing `struct re_search` to keep track
>> of the last non-nil subgroup.  Currently, things like `match-data` loop
>> through all the elements of `search_regs` whose size depends on the
>> size (in number of subgroups) of the largest regexp we have ever matched
>> so far, rather than the actual number of subgroups currently in use, so
>> there's some wasted work there.  Maybe the cost of maintaining the "last
>> reg" would pay off.
>
> Since this is a regression in Emacs 30, I'd like to solve it on the
> release branch.  Can you suggest the safest fix you can come up with
> for that purpose?

Oh, yes: just remove the check.  Since `search_regs.num_regs` keeps
track only of the size of the array (rather than the part of it
currently in use), and the array is never shrunk, it changes *very* few
times in an Emacs session, so this check we have triggers
*extremely* rarely.

The "correct" change I suggest above would definitely not be good for
the release branch since it will likely catch more cases (and thus
introduce regressions where code used to "work" but now signals an
error).

        Stefan