*** a/src/term.c	2015-01-10 13:26:55.000000000 +0900
--- b/src/term.c	2015-01-10 13:26:44.000000000 +0900
***************
*** 154,159 ****
--- 154,163 ----
  #endif /* HAVE_TGETENT */
  
  static int  detected_8bit = FALSE;	/* detected 8-bit terminal */
+ #if defined(FEAT_AUTOCMD) && (defined(UNIX) || defined(VMS))
+ static int  focus_mode = FALSE;   /* xterm's "focus reporting" availability */
+ static int  focus_state = FALSE;  /* TRUE if the terminal window gains focus */
+ #endif
  
  static struct builtin_term builtin_termcaps[] =
  {
***************
*** 1911,1916 ****
--- 1915,1941 ----
      }
  #endif
  
+ #if defined(FEAT_AUTOCMD) && (defined(UNIX) || defined(VMS))
+     /* focus reporting is supported by xterm compatible terminals and tmux. */
+     if (use_xterm_like_mouse(term))
+     {
+ 	char_u	name[2];
+ 
+ 	name[0] = (int)KS_EXTRA;
+ 
+ 	/* handle focus in event */
+ 	name[1] = (int)KE_FOCUSGAINED;
+ 	add_termcode(name, (char_u *)"\033[I", FALSE);
+ 
+ 	/* handle focus out event */
+ 	name[1] = (int)KE_FOCUSLOST;
+ 	add_termcode(name, (char_u *)"\033[O", FALSE);
+ 
+ 	focus_mode = TRUE;
+ 	focus_state = TRUE;
+     }
+ #endif
+ 
  #ifdef USE_TERM_CONSOLE
      /* DEFAULT_TERM indicates that it is the machine console. */
      if (STRCMP(term, DEFAULT_TERM) != 0)
***************
*** 3248,3253 ****
--- 3273,3283 ----
      {
  	out_str(T_TI);			/* start termcap mode */
  	out_str(T_KS);			/* start "keypad transmit" mode */
+ #if defined(FEAT_AUTOCMD) && (defined(UNIX) || defined(VMS))
+ 	/* enable xterm's forcus reporting mode */
+ 	if (focus_mode)
+ 	    out_str((char_u *)IF_EB("\033[?1004h", ESC_STR "[?1004h"));
+ #endif
  	out_flush();
  	termcap_active = TRUE;
  	screen_start();			/* don't know where cursor is now */
***************
*** 3296,3301 ****
--- 3326,3336 ----
  	    check_for_codes_from_term();
  	}
  #endif
+ #if defined(FEAT_AUTOCMD) && (defined(UNIX) || defined(VMS))
+ 	/* disable xterm's forcus reporting mode */
+ 	if (focus_mode)
+ 	    out_str((char_u *)IF_EB("\033[?1004l", ESC_STR "[?1004l"));
+ #endif
  	out_str(T_KE);			/* stop "keypad transmit" mode */
  	out_flush();
  	termcap_active = FALSE;
***************
*** 5220,5225 ****
--- 5255,5289 ----
  # endif /* !USE_ON_FLY_SCROLL */
  #endif /* FEAT_GUI */
  
+ #if defined(FEAT_AUTOCMD) && (defined(UNIX) || defined(VMS))
+ 	/*
+ 	 * Handle FocusIn/FocusOut event sequences reported by XTerm.
+ 	 * (CSI I/CSI O)
+ 	 */
+ 	if (focus_mode
+ # ifdef FEAT_GUI
+ 		&& !gui.in_use
+ # endif
+ 		&& key_name[0] == KS_EXTRA
+ 	   )
+ 	{
+ 	    if (key_name[1] == KE_FOCUSGAINED && !focus_state)
+ 	    {
+ 	        apply_autocmds(EVENT_FOCUSGAINED, NULL, NULL, FALSE, curbuf);
+ 	        did_cursorhold = TRUE;
+ 	        focus_state = TRUE;
+ 		key_name[1] = (int)KE_IGNORE;
+ 	    }
+ 	    else if (key_name[1] == KE_FOCUSLOST && focus_state)
+ 	    {
+ 	        apply_autocmds(EVENT_FOCUSLOST, NULL, NULL, FALSE, curbuf);
+ 	        did_cursorhold = TRUE;
+ 	        focus_state = FALSE;
+ 		key_name[1] = (int)KE_IGNORE;
+ 	    }
+ 	}
+ #endif
+ 
  	/*
  	 * Change <xHome> to <Home>, <xUp> to <Up>, etc.
  	 */
