Subject: Re: bug#58175: 29.0.50; M-x window-swap-states during an active mark leaves behind a region overlay

From: Stefan Monnier <[email-masked]>
Date: Tue, 04 Oct 2022 16:27:13 -0400

>>  > Looks like the internal-region-overlay window parameter, which is
>>  > important for correct workings of region-highlight, isn't copied
>>  > correctly to the other window as part of swapping state, because its
>>  > value ends up as "overlay N in no buffer", i.e. the overlay's buffer
>>  > is lost in transition.
>> 
>> If you want a window parameter to get copied when swapping window states,
>> you have to explicitly mark it as "persistent".  For example with:
>> 
>> (push '(internal-region-overlay . t) window-persistent-parameters)

Hmm... I must say I misunderstood the report when I read it originally.
Now that I see a bit more clearly what it's about I wonder why we'd have
to do something special (w.r.t `internal-region-overlay`) for
`window-swap-states` compared to what we do (i.e. nothing at all) when
we do `set-window-buffer`.

More specifically, AFAICT the code that uses `internal-region-overlay`
just tries to reuse that info to try and reduce memory churn, but it
should work correctly even when `internal-region-overlay` points to the
wrong buffer or even if it's not an overlay at all.

IOW, I suspect the bug is in `redisplay-(un)highlight-region-function`
and adding `internal-region-overlay` to `window-persistent-parameters`
would likely just cover it for that use-case but it could
reoccur elsewhere.

        Stefan