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: Sun, 13 Mar 2011 11:22:50 -0700

> > But this has already been expressed (by several people), 
> > and rejected, in various discussions in address@hidden
> 
> I believe I was the one that last raised it in emacs-devel and, after 
> the explanations from Stefan Monnier and Stephen Turnbull, I was 
> satisfied.  I told you quite explicitly that I was satisfied.

So?  As I said, the warning was discussed, and getting rid of it was rejected.
You are convinced that that's a great decision; I am not.  No way of knowing
what most people felt or feel, and it doesn't matter because polls are not taken
anymore when deciding...  (Poor Emacs.)

> > IMO, this warning has produced _far_ more confusion than it 
> > has eliminated.  And that will no doubt continue to be the
> > case going forward.
> 
> Yes, I agree that this saga will continue for quite some time.  But I 
> wouldn't say that the warning has produced all this confusion.  The 
> confusion about how to use save-excursion correctly has existed for a 
> long time and continues to exist.  The warning is the 
> messenger, not the cause of the confusion.

I'm not convinced of the existence of this bogeyman about mass confusion
concerning how to use `save-excursion'.  I don't see such confusion.

But it's pretty _obvious_ that the warning message has produced a colossal
amount of confusion.  Everybody and her brother has a different understanding of
what the "danger" might be.

> > FWIW, I also agree with Andreas that a "warning" is for 
> > something serious.  A warning is not the same thing as in
> > informative message.  A warning _warns_ you about potential
> > danger/damage.  Alarmism eventually results in the Chicken
> > Little effect (aka Boy Cries "Wolf!").
> 
> This particular warning is in the 'suspicious category.
> It signals that the code is suspicious.

Only if you think that using `save-excursion' with `set-buffer' is suspicious,
which in general it is not.

Wrt the supposed danger, David K replied this to Stefan (who replied to me,
defining the danger)
http://lists.gnu.org/archive/html/emacs-devel/2010-01/msg00588.html:

>>> 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?
>>>
>>> (save-excursion
>>>   (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
>>>   ...
>>> )
>>
>> What is dangerous about it is that dependong on which buffer is current
>> before executing this code, the point-saving will either do something
>> or nothing.
>
> Wrong.  It will always restore the _original_ buffer and its point on
> exit of the save-excursion form.

David is correct of course.  And Stefan's explanation of the "danger" does not
indicate any danger, in any case.  Where's the beef?

> I for one would be very wary of using anybody's code that generates
> such warnings.

That is precisely one of the problems created by this misguided message.  Users
get freaked out seeing WARNINGs when they byte-compile the code.  They don't
understand the message (even seasoned developers differ in their guesses as to
its meaning), and the unknown scares them.

Even Stefan admits that the compiler is incapable of issuing such warnings only
when the code is in fact problematic in his eyes.  In plain English, it's stupid
and warns about situations that even Stefan admits are not a problem.  And much
of the `save-excursion' code that he would consider problematic is in fact
typically safe, sound, and sure.

IOW, the warnings are all over the map - "tales told by an idiot, full of sound
and fury, signifying nothing".

Whether or not every use of `save-excursion' with `set-buffer' is stylistically
neat or is the best choice in terms of software engineering is a different
matter.  And it is a matter that is not handled discriminately by this message,
in any case.

> The warnings would signal to me that the developers have not
> taken care to use the programming language correctly.

If they automatically signal that to you, then I would suggest that you might
not have taken sufficient care to learn the programming language correctly. ;-)

More seriously, RMS et al who created `save-excursion' were not idiots.  They
explicitly designed it to save and restore which buffer is current, in addition
to its point and mark.  Why, do you suppose?

And most users of it over the years have used it well - without any warnings.
If you automatically think that such a warning indicates bad code or inadequate
knowledge of Emacs Lisp then you are jumping to a wild, indiscriminate
conclusion, IMO.

> They have produced code that happens to work but 
> could break easily when pulled and stretched.

You cannot deduce that from the warnings issued.  They might have; they might
not have.  Not every use of `save-excursion' with `set-buffer' indicates
fragile, messy, or otherwise unwise code.

Far from it.  You yourself mentioned zillions of such uses in the code you
inherited, only a tiny minority of which you felt needed correcting.  And you
certainly didn't need a warning for each occurrence in order to search your code
for possible improvements.




reply via email to

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