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: Stefan Monnier
Subject: Re: `save-excursion' defeated by `set-buffer'
Date: Fri, 11 Mar 2011 10:52:14 -0500
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.0.50 (gnu/linux)

> is there an example, where save-excursion will fail, ie not restore the
> buffer due to a set-buffer afterwards?

`save-excursion' is not the form to use to save the current buffer: that's
what save-current-buffer is for.  What `save-excursion' does is preserve
point (and mark), and some people tend to think of it as "undoes all
point movement", which is sadly only true for the current buffer but not
all buffers.

So (save-excursion (goto-char BAR)) is pretty much a no-op.

But (save-excursion (set-buffer FOO) (goto-char BAR)) is either:
- the same as (save-excursion (goto-char BAR)), in case FOO is already current.
- an inefficient form of (with-current-buffer FOO (goto-char BAR)).
I still haven't found any code out there where this behavior is what
is actually wanted and expected by the programmer.

In more than 90% of the cases, the intended meaning is
(with-current-buffer FOO (goto-char BAR)) and the behavior if FOO is
current (to additionally preserve point) is harmless, so the warning
simply points out an inefficiency.

In the remaining cases, FOO is almost always current, but when it's not
the resulting behavior is a bug.  I.e. the intended code is
(with-current-buffer FOO (save-excursion (goto-char BAR))).


        Stefan


reply via email to

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