Subject: 24.3.50; focus-in-hook not running accurately when frame switches to terminal

From: Steven Edwards <[email-masked]>
Date: Sat, 15 Feb 2014 07:14:22 -0500

Starting from emacs -Q:

---
(server-start)
(defun rand/on-focus ()
  (message (concat (prin1-to-string window-system) (prin1-to-string
  (selected-frame)) (format-time-string "%H:%M:%S.%N"))))
(add-hook 'focus-in-hook 'rand/on-focus)
---

Open another graphical frame with C-x 5 2; switch it to *Messages* buffer.

Next open a non-graphical frame with emacsclient -nw.

You should now have three frames open.

Switch frames from one to the next.  (I'm using the awesome window
manager, so Super-J.)

You'll notice that when switching from one graphical frame to the next,
two messages appear, e.g.:

---
x#<frame *unsent mail to [email-masked]* 0xe88e40>06:40:47.325463985
x#<frame *Messages* 0x1123978>06:40:47.384926969
---

...but when cycling from a graphical frame to a terminal frame, nothing new
appears in *Messages*--until you switch to a graphical frame:

---
x#<frame *Messages* 0x1123978>06:44:37.889809270
x#<frame *Messages* 0x1123978>06:44:37.922339020
x#<frame *unsent mail to [email-masked]* 0xe88e40>06:44:37.922801690
---

If you perform any actions on the terminal frame after switching switch
from a graphical frame, *Messages* will receive one message--as if
focus-in-hooks are being called from focus-out-hooks:

---
x#<frame [email-masked] 0x352e880>06:56:51.097246751
---

Switching to a raphical frame after performing an action on the terminal
frame will finally send a message containing the terminal frame object,
but only after it loses focus:

---
nil#<frame F2 0x11e90c8>06:56:53.533491550
x#<frame [email-masked] 0x352e880>06:56:53.533998714
---

Possibly related, C-x 5 o skips over any terminal frames and C-x 5 1
won't delete terminal frames.

The use case for this is that I'd like to use different themes for X
frames and terminal frames.

I know the use of window-system is deprecated; (framep (selected-frame))
does not work either.

I hope this report helps.

Best,

Steven

In GNU Emacs 24.3.50.1 (x86_64-unknown-linux-gnu, GTK+ Version 2.20.1)
 of 2014-01-25 on Rand
Windowing system distributor `The X.Org Foundation', version 11.0.11004000
System Description:	Debian GNU/Linux 6.0.8 (squeeze)

Important settings:
  value of $LC_CTYPE: en_US.UTF-8
  value of $LANG: en_US.UTF-8
  locale-coding-system: utf-8-unix

Major mode: Emacs-Lisp

Minor modes in effect:
  tooltip-mode: t
  electric-indent-mode: t
  mouse-wheel-mode: t
  tool-bar-mode: t
  menu-bar-mode: t
  file-name-shadow-mode: t
  global-font-lock-mode: t
  font-lock-mode: t
  blink-cursor-mode: t
  auto-composition-mode: t
  auto-encryption-mode: t
  auto-compression-mode: t
  line-number-mode: t
  transient-mark-mode: t

Recent input:
C-x C-f . e m a c s <return> C-n C-n C-n C-n C-n C-n
C-n C-n C-n C-n C-n C-n C-n C-n C-n C-n C-n C-n C-n
C-n C-n C-n C-n C-n C-n C-n C-n C-n C-n C-n C-b C-x
C-e C-x b C-g C-x C-f . e m a c s . d / l i TAB c u
s TAB g l TAB <return> C-n C-n C-n C-n C-n C-n C-n
C-n C-n C-n C-n C-n C-n C-n C-n C-n C-n C-n C-n C-n
C-n C-n C-n C-n C-n C-n C-b C-x C-e C-n C-n C-n C-n
C-n C-n C-a C-b C-x C-e C-x 5 2 <switch-frame> M-x
i d o - m o d e <return> <switch-frame> C-x b m e <return>
<switch-frame> <switch-frame> <switch-frame> <switch-frame>
ESC [ > 1 ; 2 4 0 3 ; 0 c <switch-frame> <switch-frame>
<switch-frame> <switch-frame> <switch-frame> <switch-frame>
M-x r e p o r t - e m SPC SPC <return>

Recent messages:
x#<frame custom-global.el 0xe88e40>06:28:40.401223846
x#<frame *Messages* 0x1123978>06:28:40.435829590
x#<frame *Messages* 0x1123978>06:28:58.428023215
x#<frame *Messages* 0x1123978>06:28:58.482460360
x#<frame custom-global.el 0xe88e40>06:28:58.482890190
x#<frame custom-global.el 0xe88e40>06:29:07.640302435
x#<frame *Messages* 0x1123978>06:29:07.675357806
x#<frame *Messages* 0x1123978>06:29:20.079938338
x#<frame *Messages* 0x1123978>06:29:20.136675311
x#<frame custom-global.el 0xe88e40>06:29:20.137121846

Load-path shadows:
None found.

Features:
(shadow sort gnus-util mail-extr emacsbug message format-spec rfc822 mml
easymenu mml-sec mm-decode mm-bodies mm-encode mail-parse rfc2231
mailabbrev gmm-utils mailheader sendmail rfc2047 rfc2045 ietf-drums
mm-util help-fns mail-prsvr mail-utils xterm ido server time-date
tooltip electric uniquify ediff-hook vc-hooks lisp-float-type mwheel
x-win x-dnd tool-bar dnd fontset image regexp-opt fringe tabulated-list
newcomment lisp-mode prog-mode register page menu-bar rfn-eshadow timer
select scroll-bar mouse jit-lock font-lock syntax facemenu font-core
frame cham georgian utf-8-lang misc-lang vietnamese tibetan thai
tai-viet lao korean japanese hebrew greek romanian slovak czech european
ethiopic indian cyrillic chinese case-table epa-hook jka-cmpr-hook help
simple abbrev minibuffer nadvice loaddefs button faces cus-face macroexp
files text-properties overlay sha1 md5 base64 format env code-pages mule
custom widget hashtable-print-readable backquote make-network-process
dbusbind gfilenotify dynamic-setting font-render-setting move-toolbar
gtk x-toolkit x multi-tty emacs)