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: Tue, 15 Mar 2011 10:35:25 -0400
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.0.50 (gnu/linux)

> pointing to other ways of delivering a task doesn't prove

> save-excursion+set-buffer

> is wrong.

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.

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).
- 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 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).

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


        Stefan


reply via email to

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