20

I am trying to execute when the active buffer has changed by either

  1. Changing the buffer in the current window (e.g. C-x <left>).
  2. Switching to another window (C-x o).
  3. 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-hook is 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 the current-buffer after the change is.

  • window-configuration-change-hook can 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 for M-x other-window.

  • mouse-leave-buffer-hook seems 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-hook would 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.

6
  • 1
    The only other thing I can suggest is recording your current location stuff with the pre-command-hook and comparing that data to that which becomes avaialable when running the post-command-hook and then put in conditions in your function attached to the latter hook that either exit or continue processing to achieve the desired effect. I'm surprised the buffer-list-update-hook is insufficient with certain conditions attached, including, but not limited to using the pre-command-hook to record the relevant data at the beginning of the command, but if you've already experimented and ....
    – lawlist
    Nov 23, 2017 at 17:27
  • 2
    With the pre-command-hook, you would be recording things like: (selected-window), this-command, (selected-frame), (current-buffer). And, compare that result to the same inquiry on whichever hook you attach your custom function to -- e.g., using the post-command-hook or buffer-list-update-hook or .... Keep in mind that this-command is short-lived, so you want to record it in a separate variable (probably global). Your conditions on the latter hook could compare the recorded this-command to a list of certain functions that justify taking further action.
    – lawlist
    Nov 23, 2017 at 17:31
  • @lawlist When using my own global state-variables, using buffer-list-update-hook' does indeed become viable (e.g. by setting a flag, that tells post-command-hook' that an action is necessary). Since I now also do some checks, when merely swichting lines, I need to use `post-command-hook' anyway. Seems like simpler hooks aren't available.
    – kdb
    Nov 23, 2017 at 17:53
  • Perhaps you should edit your question to include a description of why you are trying to do this. Even better would be to post the code you're trying to execute. Nov 25, 2017 at 4:33
  • 2
    Seems that window-state-change-hook is what you want.
    – updogliu
    Mar 31, 2021 at 12:02

3 Answers 3

11

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 case line-beginning-position.*

  • In post-command-hook, check if the state has changed, possibly skipping even that, if this-command is self-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.

9

Emacs27.1 introduce a new variable called `window-buffer-change-functions'

4
0

It looks like other packages have already solved this problem, but I'm posting here for people who may arrive on this page in their search for answers.

The treemacs package uses a minor mode to toggle something called treemacs-follow-mode and a timer to call the desired update function after buffer-list-update-hook: https://github.com/Alexander-Miller/treemacs/blob/master/src/elisp/treemacs-follow-mode.el#L86C15-L86C15

And for #3: Update when changing frames, I used the after-focus-change-function like powerline does: https://github.com/milkypostman/powerline/blob/master/powerline.el#L575

New contributor
CodeAwareness is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.