Subject: bug#71819: Incorrect substitution when undo replacing regexps

From: Stefan Kangas <[email-masked]>
Date: Fri, 28 Feb 2025 17:53:03 -0800

Eli Zaretskii <[email-masked]> writes:

> Ping!  Tino, can you please look into this issue?

Friendly ping.  Tino, could you please look into this issue that seems
to have been introduced by your commit 30c4f35a6fc8?

>
>> From: Juri Linkov <[email-masked]>
>> Cc: Tino Calancha <[email-masked]>,  Sab Pyrope
>>  <[email-masked]>,  [email-masked]
>> Date: Sun, 30 Jun 2024 19:25:11 +0300
>>
>> >> I encountered a bug when replacing regular expressions.
>> >> If you [u]ndo a previous change during the replacement process, subsequent changes to the \2 substitution
>> >> will use the old undone result.
>> >>
>> >> Text:
>> >> ---
>> >> Aaaaa. Bbbb.
>> >> Ccccc. Dddd.
>> >> Eeeee. Ffff.
>> >> Ggggg. Hhhh.
>> >> ---
>> >> Input regexp: \([.]\) \([A-Z]\)
>> >> Output regexp: \1  \2
>> >> Bug:
>> >> 1. Copy the text above to the buffer.
>> >> 2. Enter the regular expressions to the interactive regexp replacement [C-M-%].
>> >> 3. Click [y] twice until the Ffff is highlighted.
>> >> 4. Cancel the previous replacement via [u].
>> >> 5. Continue replacing [y] until the end.
>> >> 6. Notice that all sentences except the first and second now begin with an incorrect letter D, which is not
>> >> implied by the entered regexp, it should be F and H, as if it had been replaced, but without being undoed in
>> >> the middle of the process.
>> >> Output:
>> >> ---
>> >> Aaaaa.  Bbbb.
>> >> Ccccc.  Dddd.
>> >> Eeeee.  Dfff.
>> >> Ggggg.  Dhhh.
>> >> ---
>> >
>> > This is a regression between Emacs 26 and Emacs 27, perhaps as a side
>> > effect of fixing bug#36328.  Juri, could you please take a look?
>>
>> This is caused by the commit 30c4f35a6fc8 that introduced these lines:
>>
>>   (when regexp-flag
>>     (setq next-replacement (nth 4 elt)))
>>
>> Maybe Tino knows how to fix this (Cc'ed).
>>