unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* crazy interaction between buffer-locality and function-locality of variables
@ 2008-11-02  3:34 Ami Fischman
  2008-11-02 16:57 ` Stefan Monnier
  2008-11-02 23:19 ` crazy interaction between buffer-locality and function-locality of variables Richard M. Stallman
  0 siblings, 2 replies; 7+ messages in thread
From: Ami Fischman @ 2008-11-02  3:34 UTC (permalink / raw)
  To: emacs-devel

[-- Attachment #1: Type: text/plain, Size: 1516 bytes --]

This snippet:

(make-local-variable 'ami-wcb-bug-foo)
(defun ami-wcb-bug (ami-wcb-bug-foo)
  (with-temp-buffer
    ami-wcb-bug-foo))
(ami-wcb-bug 'bar)

eval'd in a *scratch* buffer results in this error: (void-variable
ami-wcb-bug-foo)
because ami-wcb-bug-foo is not bound in the temporary buffer, because the
same name has been declared local in *scratch*, and the function-call
binding happens in that buffer.  A similar situation for let-binding is
described in elisp.info's "11.10.1 Introduction to Buffer-Local Variables".
This happens in 22.1.1 and in CVS HEAD.

Is the behavior of binding function arguments variables subject to the
locality of the current buffer at function call time intentional?  It makes
writing functions that respond to asynchronous events require using local
variables with names that are globally unique (prefixed with package name,
presumably), which is a PITA.  An example of where this bit me: gnus makes
'timestamp buffer-local in *Summary* buffers, and emacs-jabber has a
function that uses "timestamp" as the name of one of its arguments and then
calls with-temp-buffer inside that function.  So most of the time receiving
jabber messages works just fine, but if the user happens to have point in a
*Summary* buffer then message reciept triggers a void-variable error.

It seems to me that this behavior is broken, but if it's to be kept there
needs to be strong guidance against making variables whose names /aren't/
prefixed with the package name buffer-local.

Cheers,
-Ami

[-- Attachment #2: Type: text/html, Size: 1699 bytes --]

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: crazy interaction between buffer-locality and function-locality of variables
  2008-11-02  3:34 crazy interaction between buffer-locality and function-locality of variables Ami Fischman
@ 2008-11-02 16:57 ` Stefan Monnier
  2008-11-02 17:43   ` Ami Fischman
  2008-11-02 23:19 ` crazy interaction between buffer-locality and function-locality of variables Richard M. Stallman
  1 sibling, 1 reply; 7+ messages in thread
From: Stefan Monnier @ 2008-11-02 16:57 UTC (permalink / raw)
  To: Ami Fischman; +Cc: emacs-devel

> Is the behavior of binding function arguments variables subject to the
> locality of the current buffer at function call time intentional?  It makes

Yes.  It's not great, but there's no easy fix for it.

> writing functions that respond to asynchronous events require using
> local variables with names that are globally unique (prefixed with
> package name, presumably), which is a PITA.  An example of where this
> bit me: gnus makes 'timestamp buffer-local in *Summary* buffers, and
> emacs-jabber has a function that uses "timestamp" as the name of one
> of its arguments and then calls with-temp-buffer inside that function.

Indeed, it's a Gnus bug.  Please report it there.

> It seems to me that this behavior is broken, but if it's to be kept

It's partly broken, but other behaviors are broken in other ways.
The only good solution is to be careful not to mix them up (except when
it's really wanted, such as with default-directory which is often
let-bound).

> there needs to be strong guidance against making variables whose names
> /aren't/ prefixed with the package name buffer-local.

Agreed.  Where would you like to see it?  In the coding conventions?


        Stefan




^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: crazy interaction between buffer-locality and function-locality of variables
  2008-11-02 16:57 ` Stefan Monnier
@ 2008-11-02 17:43   ` Ami Fischman
  2008-11-02 20:05     ` Stefan Monnier
  0 siblings, 1 reply; 7+ messages in thread
From: Ami Fischman @ 2008-11-02 17:43 UTC (permalink / raw)
  To: Stefan Monnier; +Cc: emacs-devel

> > It seems to me that this behavior is broken, but if it's to be kept
> It's partly broken, but other behaviors are broken in other ways.
> The only good solution is to be careful not to mix them up (except when
> it's really wanted, such as with default-directory which is often
> let-bound).

My point was that the only way to avoid mixing the behaviors is to
make variable names unwieldy.  As an async-event-handler-writer all
function arguments and local variables must be
globally-uniquely-named, and as a major-mode writer all buffer-local
variables must be globally-uniquely-named.  The latter is perhaps not
too terrible a burden, but the former would make coding in elisp
unbearable IMO.  I'll report this to the gnus devel list.

> > there needs to be strong guidance against making variables whose names
> > /aren't/ prefixed with the package name buffer-local.
> Agreed.  Where would you like to see it?  In the coding conventions?

At least also at the same place as the warning about the conflict
between buffer-locals and let-binding in elisp.info that I pointed to
in my original email.  But also this should be a great big warning in
the defun docs of the make-variable-*-local and make-local-variable
since it's the authors using those functions (and friends?  I don't
have a complete list) that must shoulder the burden of avoiding
collisions.

-a




^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: crazy interaction between buffer-locality and function-locality of variables
  2008-11-02 17:43   ` Ami Fischman
@ 2008-11-02 20:05     ` Stefan Monnier
  2008-11-02 20:31       ` crazy interaction between buffer-locality and function-localityof variables Drew Adams
  0 siblings, 1 reply; 7+ messages in thread
From: Stefan Monnier @ 2008-11-02 20:05 UTC (permalink / raw)
  To: Ami Fischman; +Cc: emacs-devel

> My point was that the only way to avoid mixing the behaviors is to
> make variable names unwieldy.  As an async-event-handler-writer all
> function arguments and local variables must be
> globally-uniquely-named, and as a major-mode writer all buffer-local
> variables must be globally-uniquely-named.

Actually, function arguments (and typical local variables) don't need to
be globally unique: they just need to use a name space that's not used
for buffer-local variables.

In Emacs we get this behavior as follows: buffer-local variables should
use a package prefix.

> The latter is perhaps not too terrible a burden, but the former would
> make coding in elisp unbearable IMO.

Indeed, which is why the convention moves the responsability to the
buffer-local side.

>> > there needs to be strong guidance against making variables whose names
>> > /aren't/ prefixed with the package name buffer-local.
>> Agreed.  Where would you like to see it?  In the coding conventions?

> At least also at the same place as the warning about the conflict
> between buffer-locals and let-binding in elisp.info that I pointed to
> in my original email.  But also this should be a great big warning in
> the defun docs of the make-variable-*-local and make-local-variable
> since it's the authors using those functions (and friends?  I don't
> have a complete list) that must shoulder the burden of avoiding
> collisions.

Hmm... at the place you mentioned in the elisp.info, we're kind of far
from discussing naming conventions, but I'll try and add a cross-ref.


        Stefan




^ permalink raw reply	[flat|nested] 7+ messages in thread

* RE: crazy interaction between buffer-locality and function-localityof variables
  2008-11-02 20:05     ` Stefan Monnier
@ 2008-11-02 20:31       ` Drew Adams
  0 siblings, 0 replies; 7+ messages in thread
From: Drew Adams @ 2008-11-02 20:31 UTC (permalink / raw)
  To: 'Stefan Monnier', 'Ami Fischman'; +Cc: emacs-devel

> Hmm... at the place you mentioned in the elisp.info, we're kind of far
> from discussing naming conventions, but I'll try and add a cross-ref.

I'm not following this closely, but an illustrative example of the problem, in
the doc, would help understanding. It's presumably the _reason_ for the naming
convention (that is, the problem the convention tries to avoid) that it is
important to understand.





^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: crazy interaction between buffer-locality and function-locality of variables
  2008-11-02  3:34 crazy interaction between buffer-locality and function-locality of variables Ami Fischman
  2008-11-02 16:57 ` Stefan Monnier
@ 2008-11-02 23:19 ` Richard M. Stallman
  2008-11-02 23:59   ` Ami Fischman
  1 sibling, 1 reply; 7+ messages in thread
From: Richard M. Stallman @ 2008-11-02 23:19 UTC (permalink / raw)
  To: Ami Fischman; +Cc: emacs-devel

    (make-local-variable 'ami-wcb-bug-foo)
    (defun ami-wcb-bug (ami-wcb-bug-foo)
      (with-temp-buffer
	ami-wcb-bug-foo))
    (ami-wcb-bug 'bar)

    eval'd in a *scratch* buffer results in this error: (void-variable
    ami-wcb-bug-foo)

This is intentional.  It is more coherent than the other possible
behaviors.

    Is the behavior of binding function arguments variables subject to the
    locality of the current buffer at function call time intentional?

That's a confusing way to describe things.  The current buffer
when the function is called has no direct effect on the function.
What does have an effect is switching buffers within the function.

Normally this is not a problem, because the sort of names that are
used for function arguments are never made buffer-local.  But you
encountered an exception.

      An example of where this bit me: gnus makes
    'timestamp buffer-local in *Summary* buffers, and emacs-jabber has a
    function that uses "timestamp" as the name of one of its arguments and then
    calls with-temp-buffer inside that function.

We should probably change the name of that variable in Gnus.
to something like `gnus-timestamp'.




^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: crazy interaction between buffer-locality and function-locality of variables
  2008-11-02 23:19 ` crazy interaction between buffer-locality and function-locality of variables Richard M. Stallman
@ 2008-11-02 23:59   ` Ami Fischman
  0 siblings, 0 replies; 7+ messages in thread
From: Ami Fischman @ 2008-11-02 23:59 UTC (permalink / raw)
  To: rms; +Cc: emacs-devel

>    Is the behavior of binding function arguments variables subject to the
>    locality of the current buffer at function call time intentional?
> That's a confusing way to describe things.

I intentionally described the behavior I observed in a way as
confusing as I found the behavior to be, hoping to draw attention to
the deficiency I considered in the behavior.

> The current buffer
> when the function is called has no direct effect on the function.
> What does have an effect is switching buffers within the function.

This distinction lacks a difference to me.  Buffer-switching is a
standard idiom of elisp and normally considered an implementation
detail of a function, not part of its external contract.  If including
buffer-switching in a function's code unbinds variables bound in that
function then that promotes the presence of buffer-switching from
implementation detail to externally-visible effect.  Put another way,
a function that uses buffer-switching is always at the mercy of the
buffers in which it is invoked, to avoid making local variables with
the same names as the function's arguments or let-bound variables.
This is what I was hoping was unintentional and fixable, though you
and Stefan have now told me that it is the lesser evil (and by
implication here to stay).

> We should probably change the name of that variable in Gnus.
> to something like `gnus-timestamp'.

I have posted a request for something similar to the gnus mailing list:
http://thread.gmane.org/gmane.emacs.gnus.general/67732

-a




^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2008-11-02 23:59 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2008-11-02  3:34 crazy interaction between buffer-locality and function-locality of variables Ami Fischman
2008-11-02 16:57 ` Stefan Monnier
2008-11-02 17:43   ` Ami Fischman
2008-11-02 20:05     ` Stefan Monnier
2008-11-02 20:31       ` crazy interaction between buffer-locality and function-localityof variables Drew Adams
2008-11-02 23:19 ` crazy interaction between buffer-locality and function-locality of variables Richard M. Stallman
2008-11-02 23:59   ` Ami Fischman

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).