[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: point moved despite save-excursion, after deleting/reinserting regio

From: Yuri Khan
Subject: Re: point moved despite save-excursion, after deleting/reinserting region
Date: Thu, 18 Oct 2018 18:32:24 +0700

On Thu, Oct 18, 2018 at 4:51 PM Garreau, Alexandre
<> wrote:

> > When you delete text, markers pointing into that text cannot follow,
> > they get relocated to the beginning/end of the deletion/insertion.
> > What else can Emacs do, given that it doesn't know your future
> > intentions?
> I thought something such as `save-excursion' would do that, or that it
> would be saved with the string, or something alike.

Imagine if that was true: that save-excursion could somehow magically
restore point across deletion of a fragment of text and subsequent
re-insertion of the same fragment.

Now comes the next guy (me!) and says, what if I re-insert not the
same fragment but a slight modification of it? Maybe I wrapped it in a
pair of XML tags. Or removed a pair of XML tags. Anyway, plz restore
the point Doing The Right Thing, kthx.

How would the hypothetical implementation of save-excursion do that?


You’d probably want to make buffer-substring to return not only the
buffer substring but also a representation of all markers within. You
can’t put them into the string itself because that will confuse the
algorithms that do the modification. You could probably put it into
properties, but then those who use buffer-substring-no-properties
lose. Further, insert would scan the inserted fragment and restore

The practical answer to my position-preserving modifications is, do
them directly in the buffer. If the modifications do not span the
point, point will be preserved by save-excursion.

Now returning to your modify-evaluate-undo scenario. How important is
it to you that the evaluation happens in the original buffer? Maybe
you could copy the fragment to a temporary buffer, modify and evaluate
it there, have the temporary buffer killed for you? This way, the
original buffer content is unmodified and the point is unmoved.
Moreover, the undo history is untouched.

reply via email to

[Prev in Thread] Current Thread [Next in Thread]