emacs-devel
[Top][All Lists]
Advanced

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

Re: save-excursion again


From: Stefan Monnier
Subject: Re: save-excursion again
Date: Fri, 18 Jun 2010 14:37:04 -0400
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.0.50 (gnu/linux)

> When I first got involved with VM, there were a host of problems that
> were called "jumping cursor" issues.  You do something and the cursor
> ends up at a different place.  If you then delete a message, you end
> up deleting the wrong message.  Fixing these problems always involved
> adding plenty of save-excursion's so that the cursor doesn't move
> while work was going on.

Yes, that's what save-excursion is for.  But placing it right before
`set-buffer' is usually not the best answer.

> In fact, I would have rather liked a compiler that asked, do you
> really want to do set-buffer without a save-excursion first?  That is,
> set-buffer without save-excursion is dangerous, because it might lead
> to the "jumping cursor" problem.

Most of the "nasty jumping cursors" I've seen were due to code like:

  (save-excursion
    (set-buffer FOO)
    ...move around...)

Which should instead be written

  (with-current-buffer FOO
    (save-excursion
      ...move around...))

I.e. the save-excursion needs to be around the point-movement, not
around the buffer-change.

> So, I am a bit surprised that anybody should think of save-excursion
> as being a bad idea to be avoided.

It's not.

> I am still trying to understand this.
>> Indeed.  The problem being that the precise semantics of such
>> a construction is pretty subtle, so the byte-compiler can't do the proof
>> for you.  In some cases the proof is easy (e.g. there's no movement at
>> all in that piece of code), but often it's very difficult (tho my
>> experience might be made worse since I usually make such changes to code
>> with which I'm not familiar).  My approach is to basically replace all
>> such code with just `with-current-buffer', then let users find the
>> counter examples.
> I will assume you are joking.  But, why bother with any of this at
> all?  What is wrong with the original code in the first place? 

That the save-excursion in

  (save-excursion
    (set-buffer FOO)
    ...move around...)

will not undo the point movement in FOO (unless current-buffer is
already FOO to start with).


        Stefan



reply via email to

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