I am trying to execute when the active buffer has changed by either
- Changing the buffer in the current window (e.g.
C-x <left>). - Switching to another window (
C-x o). - Switching to another frame.
Are their hooks suitable for detecting this?
What I figured out so far
Looking through the Standard Hooks I found the following options, none of which quite do what I want:
buffer-list-update-hookis called for both (1) and (2). It is however unsuitable, because it is run before the buffer is changed, while I want to know what thecurrent-bufferafter the change is.window-configuration-change-hookcan be used to detect a change of the buffer displayed in the current window, and it is run after the change, as needed. It is however not run forM-x other-window.mouse-leave-buffer-hookseems viable for detecting mouse-based window switching, but it gets called a bit often (for me four times upon switching windows with the mouse, three times before and once after switching), which requires additional logical to prevent multiple execution.post-command-hookwould be viable but a bit heavy handed, risking significant slow-down of the editor by even minor bugs.Since my action would need to use
with-selected-window, which triggers these hooks, care has to be taken to avoid endless loops where the hook triggers itself.