Subject: bug#70122: 29.3.50; transpose-regions can crash Emacs

From: Braun Gábor <[email-masked]>
Date: Tue, 16 Apr 2024 16:26:46 +0200

Hi Eli,

> > The issue I was unable to solve is that the functions
> > set_text_properties_1 and graft_intervals_into_buffer
> > record text property changes in undo history, but this is 
unwanted
> > here as transpose-regions handles undo history itself.
...
> 
> But this is not a new problem, right?  The code called
> set_text_properties_1 and graft_intervals_into_buffer before the
> changes as well, and had the same effect on undo history.  
Right?
> 
> If this is something caused by these changes, could you please 
explain
> the issue in more detail, with references to the relevant parts 
of the
> code?

I don't think it is a new problem, and the other branches have the 
same problem.

In detail, in all branches transpose-regions adds the undo entries 
early for its changes (calls to record_change),
then during making the changes already recorded in undo history
calls functions adding additional undo entries 
(set_text_properties_1, graft_intervals_into_buffer),
i.e. at a time when the buffer state and undo history does not 
match.  Luckily these entries are text property changes to a text 
deleted immediately by the following entries, so their effects are 
not visible, but this is due to the initial undo entries
recording change to a larger span of text than just the two 
swapped regions.

When not just the byte length but the character length of the 
swapped regions are the same, the original code didn't change text 
properties between the two regions, i.e., the text property 
changes were restricted to the swapped regions, and hence
restricting the initial undo entries to these region worked.

The new changes do make text property changes to the text between 
the swapped regions additionally (as interval positions need 
adjustment), which makes it harder for the undo entries,
and that's why I did what the other branches do.

All in all, the issue is not about correctness of code, but rather 
cleanness of code: separate responsibilities: which part of the 
code is responsible for (which) undo entries.

Best wishes,

	Gábor