Do stuff when selecting a window

不想你离开。 提交于 2019-12-22 09:57:04

问题


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)?


回答1:


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.




回答2:


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))


来源:https://stackoverflow.com/questions/20934770/do-stuff-when-selecting-a-window

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!