Monday, April 8, 2013

Emacs keyboard delay when switching frame focus

I recently ran into an annoying issue with Emacs; I'm not sure what initiated this problem, but it might be that the default value of focus-follows-mouse changed. Or Ubuntu changed.

Here's the issue that I was seeing:

  • I have multiple Emacs frames, and I like to switch between them often.
  • Switching between them by using Alt-Tab, or clicking, would result in no problems.
  • However, using the interactive function other-frame (via a keybinding) results in a problem that there's an approximately 2 second delay from switching focus to when the keyboard would accept input again. Even worse, Emacs seems to have forgotten what modifier keys I had down, so when it did accept keyboard input, it wouldn't know that I still had the control key held down.
I tried various ways of debugging this to see if anything was going on (xev, strace) but ultimately I figured it out by reading M-x describe-function other-frame. That reveals the following text:


To make this command work properly, you must tell Emacs
how the system (or the window manager) generally handles
focus-switching between windows.  If moving the mouse onto a window
selects it (gives it focus), set `focus-follows-mouse' to t.
Otherwise, that variable should be nil.

When I M-x describe focus-follows-mouse, it shows t, which is wrong. So I instead added the following to my .emacs:

(setq focus-follows-mouse nil)

and now everything is back to normal. Why did that setting break things? Not sure. Maybe Emacs didn't call something it should have called assuming X would take care of it. Or the other way around. Anyhow, it's much better now.

Some details on my setup:
  • Ubuntu 12.04 Precise Pangolin
  • Emacs 23.3.