Subject: Re: bug#4655: 23.1.50; buffer-swap-text oddity

From: Stefan Monnier <[email-masked]>
Date: Wed, 07 Oct 2009 10:29:11 -0400

>> 2- save-excursion saves the "position and current buffer" as a marker.
>> Combine 1 and 2 together, and you'll see that save-excursion ends up
>> changing the current buffer.

> Oops. Yes, I see.
> Well, we could document it, but from my POV it looks more like a bug
> than a "feature".

Agreed.  This said, we have a problem.

If we consider the buffer-swap-text as a kind of "two-way copy&paste",
just with a more efficient implementation, then it would be OK for
save-excursion to not preserve the position, i.e. handle save-excursion
markers similarly to window-markers.

I see some problems with it, tho:
1- how to find all the save-excursion markers (we'd probably have to
   walk the specpdl list and compare the restore function with
   save_excursion_restore, which is very ugly).
2- if we reset those marker's position in buffer-swap-text, then
   (save-excursion (buffer-swap-text BUF) (dosomething) (buffer-swap-text BUF))
   would end up moving point to (point-min) for no good reason, which is
   rather inconvenient and might be worse than the OP.
3- we could change save-excursion itself to check the the markers still
   points to the same buffer.  That would solve the OP, but the cost of
   making all save-excursions more expensive (for the sake of a rare
   corner case).
4- should save-excursion care more about preserving the position inside
   the current text, or preserving the current buffer?
5- most uses of save-excursion are wrong, and the OP is among them.

I.e. I'd tend to prefer documenting it (in buffer-swap-text), and remind
people that they should usually use with-current-buffer rather than
save-excursion.

        Stefan "who thinks save-excursion is an ugly beast"