Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do stuff when selecting a window

Tags:

emacs

elisp

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

like image 273
katspaugh Avatar asked Jan 21 '26 06:01

katspaugh


2 Answers

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.

like image 90
Stefan Avatar answered Jan 22 '26 20:01

Stefan


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))
like image 35
Alex Vorobiev Avatar answered Jan 22 '26 20:01

Alex Vorobiev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!