Emacs-lisp hooks for detecting change of active buffer?
Emacs27.1 introduce a new variable called `window-buffer-change-functions'
Judging from the comments, the answer to this question is “No, there is no such hook.”
Additionally, some of the hooks mentioned in my question, are also triggered by changes, which are not user-visible, such as temporary changes due to with-current-buffer
and with-selected-window
.
However, using post-command-hook
has proven to be a non-issue for performance, since the required state-check is cheap.
Alternative
Probably obvious, but stated for completeness.
Store state information in a global variable, in a frame-parameter, in a window-parameter or in a buffer-local variable, whichever is most applicable to the use-case. In my use-case, this necessary unique state is was defined by
current-buffer
,current-window
, and in one caseline-beginning-position
.*In
post-command-hook
, check if the state has changed, possibly skipping even that, ifthis-command
isself-insert-command
.- If it has, perform the intended action and update the stored state.
* line-number-at-pos
is unsuitable, because it counts lines by iterating over the buffer from point-min
to point
, making it generally cheap, but not cheap enough to be executed after every typed character.