[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: Wed, 6 Jan 2010 00:57:27 -0800

> > Show me a single case (in existing code) where (save-excursion
> > (set-buffer FOO) ...) wouldn't better be written some other way.
> That does not justify a bogus warning.
> Show me a single case where
> (setq a ...) (setq a ...)
> wouldn't be better be written some other way (with "better" meaning a
> completely subjective measure). Yet we don't warn about it.
> We don't press matters of style lightly.
> Or more serious, a single case where
> (sort xxx ...) or (nconc xxx ...) outside of a (setq xxx ...) wouldn't
> better be written some other way.
> We don't warn about those either.

Yes, and besides that argument, and without arguing about the "best" style in
which to code things, just what is inherently wrong or dangerous (meriting a
warning) with code like the following, to do some work in other buffers yet
return to the original buffer AND restore its point and mark?

  (set-buffer FOO)
  ; Pick up some info in FOO, & perhaps assign it to a var
  (set-buffer BAR)
  ; Calculate something in BAR, & perhaps record it too
; Use the gathered info to do something in the
; original buffer at the saved point.

The Elisp manual says: 

"The `save-excursion' special form is the standard way to switch
buffers or move point within one part of a program and avoid
affecting the rest of the program.  It is used more than 4000
times in the Lisp sources of Emacs."

The _standard_ way to _switch buffers_ and/or move point. Imagine that.

It of course does _not_ switch buffers on its own, which means that it is
standard to use it with some buffer-changing command such as `set-buffer' (or
`with-current-buffer' or ...).

It is not the only way. It is not always the best way. But it is one way. And it
has even been called "the standard" way. Since Day 1.

The manual also says:

"The way to designate a current buffer in a Lisp program is by calling

_The way_. Again, that sounds pretty definitive, but the context tones that down
a bit by mentioning some additional ways. None of those alternatives is
presented as _the_ generally preferred way, however. No preference is indicated.

The gist is that `save-excursion' + `set-buffer' is a not unreasonable way to
switch buffers temporarily to do something without affecting the original
context (current buffer, point, mark). A far cry, at least, from warning users
that such code is somehow abhorrent or inherently unsafe.

No one is arguing that one should always use `(save-excursion
(set-buffer...)...)' in preference to other approaches. It's just that a good
case for issuing a _warning_ for that has not been made. The only arguments made
have been in terms of user misunderstanding or preferred coding style. And those
things are better dealt with clearly, at sufficient length to promote
understanding, in the Elisp manual.

reply via email to

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