A couple of years ago I lamented over the lack of frame focus hooks in Emacs. This prohibited us from implemented a feature like IntelliJ IDEA’s “auto-save on focus lost”, which was kind of frustrating as we’re generally assuming that everything is doable in Emacs!

All this changes in Emacs 24.4 with the introduction of two new hooks - focus-in-hook and focus-out-hook. The first is triggered when the current frame gains focus and the second when the frame loses focus. So, if we want to save our active buffer when the Emacs frame loses focus we can simply do it like this:

(add-hook 'focus-out-hook 'save-buffer)

Or you can go a step further and save all the buffers:

(add-hook 'focus-out-hook (lambda () (save-some-buffers t)))

I pretty sure you’ll find other creative ways to apply those new hooks to good use!