[Top][All Lists]

[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 09:00:37 -0700

> There is no proof that save-excursion+set-buffer is wrong, because it
> just is not wrong: it's just a combination of function that performs
> something.


> That's why the byte-compiler emits a warning and not an
> error message.

A warning about what?  There is not only no proof that save-excursion+set-buffer
is wrong - it is not at all wrong.  As you admit.

> What there is, OTOH, is damning evidence that:
> - it can be replaced by with-current-buffer in more than 90% of the
>   cases (which is more concise and more efficient).

Cases where?  In old Emacs source code?

Really, you don't have to tell people to replace `save-excursion+set-buffer'
with `with-current-buffer', especially since such a replacement might not be

Just tell people of the existence of `with-current-buffer' and
`save-current-buffer', and they will naturally use those for their intended
purpose.  People will not naturally use `save-excursion' nowadays just to save
the current buffer.

> - in the remaining cases it is usually hiding an error and that error
>   can be fixed by using (with-current-buffer <foo> 
>  (save-excursion ...)).

In the remaining cases in old Emacs source code?  See above.

> - in my more than 10 years of maintaining Emacs I have not found
>   a single case where the semantics of save-excursion+set-buffer is
>   indeed exactly what we're after (i.e. where it's used without
>   introducing a bug and without introducing an inefficiency fixable by
>   with-current-buffer).

You are fixated on `set-buffer'.  `save-excursion' doesn't care what you do
while excursioning.  You can `set-buffer' all you want or not change buffers at
all.  It simply doesn't matter.  All that's important about `save-excursion' is
what it does: bring you back.

> So the warning was introduced to help Elisp coders improve their code.
> That's what byte-compiler warnings are for.

It won't help, but will hurt, at least with users writing new code.  If someone
uses `save-excursion' nowadays it is only in order to do what `save-excursion'
does: return you where you were.  There is nothing wrong with calling
`set-buffer' (or anything else) inside a `save-excursion'.

Your real message should be just to remind people that `save-excursion' does not
do more than it does - e.g., remove any misconceptions that it saves and
restores positions in other buffers.

reply via email to

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