help-gnu-emacs
[Top][All Lists]
Advanced

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

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


From: Eli Zaretskii
Subject: Re: point moved despite save-excursion, after deleting/reinserting region
Date: Thu, 18 Oct 2018 16:16:42 +0300

> From: "Garreau\, Alexandre" <galex-713@galex-713.eu>
> Cc: help-gnu-emacs@gnu.org
> Date: Thu, 18 Oct 2018 11:50:40 +0200
> 
> > 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.

What save-excursion does in this case is documented in the ELisp
manual:

     *Warning:* Ordinary insertion of text adjacent to the saved point
  value relocates the saved value, just as it relocates all markers.  More
  precisely, the saved value is a marker with insertion type ‘nil’.  *Note
  Marker Insertion Types::.  Therefore, when the saved point value is
  restored, it normally comes before the inserted text.

> > If that's what you want, why not record the position of point before
> > the delete/insert operation and restore it afterwards?
> 
> So is that the canonical way of doing?

It's the most direct method, AFAIK.

> that feels like defeating the purpose of `save-excursion' a little
> bit

As you can see above, it does not.  save-excursion's main use case is
that you don't edit text around point.  Maybe you should try using
replace-buffer-contents instead (you'll have to copy text to a
temporary buffer and edit it there).

> (I only wanted to produce a result for this src-block), and as this
> is more complex behavior than just “undoing backward” or anything
> part of core elisp, I feared side-effects I wouldn’t be aware of:
> for instance tho, it means my function will have the side-effects of
> moving all the markers (and maybe other similar things) that were
> inside that portion of text: it feels somewhat hardly like
> “restoring as before”.

You should be aware of the fact that point is also a marker, or at
least behaves like one.



reply via email to

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