Do stuff when selecting a window

2019-05-10 06:48发布

I've been looking through the available hooks, but none of them seems to be firing when you switch windows.

What I'm trying to do, is activating a minor mode for the selected window:

(defvar active-window (frame-selected-window))

(defun active-window-switch (&rest _)
  (when active-window
    (with-selected-window active-window
      (active-window-mode nil)))
  (setq active-window (frame-selected-window))
  (active-window-mode t))

(define-minor-mode active-window-mode
  "Minor mode to distinguish the selected window."
  :global nil :group 'active-window :init-value nil :lighter " Active")

(add-hook 'window-configuration-change-hook #'active-window-switch)

(provide 'active-window)

What hook or function to advice can I use instead of window-configuration-change-hook (which only fires when I create or quit windows)?

标签: emacs elisp
2条回答
虎瘦雄心在
2楼-- · 2019-05-10 07:38

You can try advising select-window:

(defadvice select-window (after select-window-and-do-stuff activate) 
    (do-stuff))

or, if you want to un-do your settings in the window you are leaving first:

(defadvice select-window (around select-window-and-do-stuff activate)
    (undo-stuff)
    ad-do-it 
    (do-stuff))
查看更多
来,给爷笑一个
3楼-- · 2019-05-10 07:41

select-window is an operation used internally in many cases, potentially thousands of times in a single command. You don't really care about the selected window all the time, but only when not running a command. So the better place to hook yourself is in post-command-hook.

查看更多
登录 后发表回答