Friday, May 10, 2013

Long ago, I used XEmacs, and its buffer-local faces to highlight the active window and modeline. When I switched to Emacs, I was sad to see that it didn't have buffer-local and window-local faces. It does have a separate face for active and inactive modelines though.

Today I learned that Emacs 23 has a buffer-local face remapping feature. I'm using this to highlight the active buffer (not window):

( require 'face-remap ) ( defvar highlight-focus:last-buffer nil ) ( defvar highlight-focus:cookie nil ) ( defvar highlight-focus:background "white" ) ( defvar highlight-focus:app-has-focus t ) ( defun highlight-focus:check () "Check if focus has changed, and if so, update remapping." ( let (( current-buffer ( and highlight-focus:app-has-focus ( current-buffer )))) ( unless ( eq highlight-focus:last-buffer current-buffer ) ( when ( and highlight-focus:last-buffer highlight-focus:cookie ) ( with-current-buffer highlight-focus:last-buffer ( face-remap-remove-relative highlight-focus:cookie ))) ( setq highlight-focus:last-buffer current-buffer ) ( when current-buffer ( setq highlight-focus:cookie ( face-remap-add-relative 'default :background highlight-focus:background )))))) ( defun highlight-focus:app-focus ( state ) ( setq highlight-focus:app-has-focus state ) ( highlight-focus:check )) ( defadvice other-window ( after highlight-focus activate ) ( highlight-focus:check )) ( defadvice select-window ( after highlight-focus activate ) ( highlight-focus:check )) ( defadvice select-frame ( after highlight-focus activate ) ( highlight-focus:check )) ( add-hook 'window-configuration-change-hook 'highlight-focus:check ) ( add-hook 'focus-in-hook ( lambda () ( highlight-focus:app-focus t ))) ( add-hook 'focus-out-hook ( lambda () ( highlight-focus:app-focus nil ))) ( provide 'highlight-focus )

There's some more tweaking I need to do but so far it seems to be working reasonably well.

Update: [2014-06-01] I updated the code to use Emacs 24.4's focus-in and focus-out hooks.

Update: [2017-10-26] Ram Krishnan has turned this into a package; see github/kriyative/highlight-focus.

Update: [2020-07-01] I now use and recommend auto-dim-other-buffers. It works really well, and is on MELPA!

Labels: emacs