[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: with-current-buffer

From: Juri Linkov
Subject: Re: with-current-buffer
Date: Tue, 28 Jun 2005 03:04:14 +0300
User-agent: Gnus/5.110004 (No Gnus v0.4) Emacs/22.0.50 (gnu/linux)

>>   (with-current-buffer "c"
>>     (pop-to-buffer "b"))
>>   (message "%s" (buffer-name (current-buffer))))
>> At the end, it print "a" as the current buffer name, but actually
>> this command makes the buffer "b" current.  Shouldn't it print "b"?
> I think this isn't a bug -- the `with-current-buffer' form of course
> restores the current buffer to be "a" after the wrapped code is
> finished; why would you expect differently?
> As I understand it, what `pop-to-buffer' does is set the window-buffer
> of the selected window, in _addition_ to setting the current buffer,
> and the main emacs interaction loop always restores the current buffer
> to be the window-buffer of the selected window when you look for the
> next command.
> So in your example, `pop-to-buffer' sets the current buffer and the
> window-buffer-of-selected-widnow to be "b", then the
> `with-current-buffer' form (correctly) restores the current buffer to
> be "a" again, then you print your message [which says "current buffer
> is a"], then the interaction loop goes to read a command, which sets
> the current buffer based on the window state -- which is "b", because
> of pop-to-buffer's effect.

What causes trouble here is the fact that Emacs changes the current
buffer behind the scenes (somewhere in the main loop), so at the end
of the function it is not known what the buffer will be current after
the function finishes.  But sometimes it is desirable to perform some
actions on that buffer (e.g. run hooks).

Maybe with-current-buffer should keep track of switching buffers
inside with-current-buffer, so it could anticipate what a buffer will
be set as current and don't restore the old buffer.

Or maybe `pop-to-buffer inside with-current-buffer' is not a suitable
programming construct (I mean mostly for the `next-error' command
where this problem occurred) and it's better to replace it with more
explicit buffer switching that will restore the old buffer only when
there were no `pop-to-buffer' calls.

Juri Linkov

reply via email to

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