help-gnu-emacs
[Top][All Lists]
Advanced

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

Re: Hook for "buffer switched"


From: Stefan Monnier
Subject: Re: Hook for "buffer switched"
Date: Wed, 15 Dec 2010 10:48:35 -0500
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.0.50 (gnu/linux)

>> > Is there any hook that is run when a buffer is switched?
>> I think generally the answer is "no", tho this question is ill-formed:
>> it's not clear what you mean by switching buffer: do you mean "change
>> the currently active buffer" (i.e. what is done by `set-buffer'), or
>> "change the buffer displayed in the current window", or "select
>> a different window showing a different buffer", or ...
> I would like to be able to handle the "change
> the currently active buffer" case.

You do realize that this can happen a thousand times within a single
command, right?  A lot of code does things like

  (with-current-buffer some-other-buf
    get-some-info-from-there)

which results in 2 changes of active buffer (once to some-other-buf and
another time to return to the previous buffer)?

It will also happen without any user action whenever Emacs receives
output from an "inferior process" (e.g. in *grep* or in *shell*, ...).
And it might do such a back&forth switch for every char received.

>> > If not, why not?
>> Maybe because to a large extent it's an ill-defined problem and once
>> they try to define it better, people seem to end up deciding they don't
>> really need to solve it any more?
> Or maybe because they are not able to solve it reliably.  For
> instance, I'd like Viper state to be global instead of buffer-local.
> For this to work, I would have to know whenever the current active
> buffer changes and switch Viper state when needed.  Or I could just
> prefer always switching to Normal state whenever I switch buffer.

[ I don't know Viper enough to understand what you intend to do, so
  take the following with a grain of salt. ]
Then I think you don't really care about the active buffer (in the sense
of set-buffer), but only about "the buffer in the selected window when
Emacs is waiting for user input".  So you can probably get what you want
via a post-command-hook that compares the current (window-buffer
(selected-window)) with the previous one.


        Stefan


reply via email to

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