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: Drew Adams
Subject: RE: `save-excursion' defeated by `set-buffer'
Date: Tue, 15 Mar 2011 08:59:48 -0700

> > Given the assumptions that I added (fairly, I hope), I 
> > would say that presumably the `save-excursion' is there
> > NOT ONLY to restore which buffer was current but also
> > restore point and mark in that buffer.  That's what
> > `save-excursion' DOES, and there is no reason not to
> > suppose that the original author did not intend that.
> 
> Actually, there is a lot of reason to suppose that the author 
> did *not* intend it: although the author most likely knew it
> also did such a save-point-and-mark, she most likely used
> save-excursion as a special-form that provides a super-set of
> what she needed.  That's the case for the enormous majority
> of save-excursion+set-buffer.

Even if that were true for some very old code, there is no reason to bring
`set-buffer' into it.

If someone is NOT interested in coming back to the same point and is interested
only in coming back to the same buffer, then s?he will nowadays naturally use
`save-current-buffer' or `with-current-buffer'.  No problem.

Misplaced authoring such as you describe is generally in old code.  It should be
enough for us to remind users, in the manual, of the existence of
`save-current-buffer' etc.  They will naturally use it to save and restore the
current buffer.  They won't even look at `save-excursion' for that (based on the
name etc.).

If someone uses `save-excursion', nowadays at least, it is purposefully to
return to the same point.  And that's correct usage.  We should not assume that
they are mistakenly using it only to restore the buffer, even if such an
assumption might be a reasonable test-guideline for someone like yourself who is
trying to clean up old code.

And even for such a search-and-destroy old code mission, looking for
`set-buffer' is by no means sufficient, as I'm sure you realize.  You might as
well just look for `save-excursion' and check to see if each occurrence really
needs to save point.

`set-buffer' is a red herring here - totally.  It is neither necessary nor
sufficient as an indicator of any problem.  It does not matter what happens
during a `save-excursion' excursion - it really doesn't.  `set-buffer',
`jump-off-the-deep-end', `go-fly-a-kite', or whatever.  It just does not matter.

`save-excursion' brings you back where you were, and that's all it does.  Don't
expect more, or less, from it.  If that's the behavior you want, then
`save-excursion' is called for.  If you do not want or need to come back, then
don't use `save-excursion'.




reply via email to

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