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

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

Re: `save-excursion' defeated by `set-buffer'


From: Eli Zaretskii
Subject: Re: `save-excursion' defeated by `set-buffer'
Date: Sat, 12 Mar 2011 14:28:17 +0200

> From: David Kastrup <address@hidden>
> Date: Sat, 12 Mar 2011 11:42:12 +0100
> 
> Eli Zaretskii <address@hidden> writes:
> 
> >> From: David Kastrup <address@hidden>
> >> Date: Sat, 12 Mar 2011 10:34:45 +0100
> >> 
> >> >   Warning: `save-excursion' will not preserve point in the other buffer
> >> >   set by `set-buffer'
> >> 
> >> Is there a particular reason that nobody is interested in letting the
> >> warning be about the case that is supposed to be problematic
> >
> > Huh?  That's what I tried to express in the above text.  Isn't that
> > _precisely_ the problematic case, when the other buffer is not the
> > current and the body of save-excursion moves point and/or mark there?
> 
> No, it isn't.  The likelilood that somebody uses `set-buffer' when he
> expects it not to change the current buffer is not exactly large.

And that is exactly the situation that my suggested warning is about.

> >> Warning: `save-excursion' will preserve point and mark in the current
> >>          buffer even if set-buffer does not actually change buffers.
> >
> > Your "warning" describes what `save-excursion' is supposed to do, at
> > least according to the ELisp manual:
> >
> >        The `save-excursion' special form saves the identity of the current
> >        buffer and the values of point and the mark in it, evaluates BODY,
> >        and finally restores the buffer and its saved values of point and
> >        the mark.
> >
> > So how could warning about the normal operation be TRT?
> 
> Very funny.  Of course the warning will be about a situation where every
> function works according to its specifications.

Very funny.  The warning should be about a situation where what the
function is supposed to do does not match what the programmer might
expect from the function, judging by the code the programmer actually
wrote.

Perhaps you could explain how saying that `save-excursion' _will_
preserve point and mark is supposed to help the programmer understand
her possible mistake, which is that `save-excursion' might _not_
preserve them.  Let's see:

  J. R. Hacker: Let's use `save-excursion' to preserve point and
                mark in both this and the other buffer.  [Hacks away.]
  Emacs: Warning: `save-excursion' will preserve point and mark in the
                current buffer even if set-buffer does not actually
                change buffers.
  J. R. Hacker: ?? Isn't that what I wanted, to preserve point and
                mark?  Why the warning???  Stupid Emacs!
                [Leaves the code as is.]

> Anyway, I suggest you get yourself up to speed by reading a few messages
> from Stefan Monnier about his rationale for introducing this warning.

Actually, I did.  Perhaps so should you.  Or, if you see some flaw in
my suggested text, how about pointing out when and how it could lead
J. R. Hacker to the wrong conclusion?



reply via email to

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