Subject: Tmux and Focus-In Events

From: Gustaf Waldemarson <[email-masked]>
Date: Mon, 29 Jul 2019 15:50:48 +0200

Hello,

I noticed a rather strange and somewhat hard to track down bug that's related to
tmux and terminal focus-in events.

Using Emacs 27 and tmux 2.1: If you start tmux with the option `tmux set-option
-g focus-events on` enabled and then inside tmux start an emacsclient, e.g.,
via:

```
emacs -Q --daemon=test && emacsclient --socket-name=test ."
```

Then Emacs starts and unexpectedly inserts the characters "[I]" in the buffer
that is opened. This only seem to happen the first time the client is started.

A quick bisection on the Emacs code-base lead me to this commit:

```
commit 6fdc3fac5658a7ab142c358cddd90f3db5665ef5
Author: Daniel Colascione <[email-masked]>
Date:   Fri Jun 8 22:47:27 2018 -0700
    Support terminal focus notifications

    * lisp/frame.el (handle-focus-in,handle-focus-out): Make event
    argument optional.
    (blink-cursor-check): Make sure that the current frame is a
    window-system frame before restarting the blink timer. TTY frames
    can get focus, but don't need a blink timer because the terminal
    will do the blinking.

    * lisp/term/xterm.el
    (xterm-handle-focus-in,xterm-handle-focus-out): New functions.
    (xterm-rxvt-function-map): Recognize focus notification sequences.
    (xterm--init-focus-tracking): New function.
    (terminal-init-xterm): Call it.
```

Apparently it seems like "\e[I" is the focus-in event, which is translated to
"[I]" by `electric-pair-mode`.

I'm not entirely sure where the bug is in this case however. Is it up to Emacs
to handle focus-in events from the terminal or is that something tmux itself
should handle in this case? Or is there some setting somewhere that should be
toggled to prevent this kind of thing from happening?

I originally observed this behavior in Konsole, but the same thing happen in
xterm itself as well, and possibly other terminal emulators.

In GNU Emacs 27.0.50 (build 1, x86_64-pc-linux-gnu, GTK+ Version 3.18.9)
 of 2019-07-29 built on E105888
Repository revision: a9593253e90aa58134833078ae1fc5505ae9f58c
Repository branch: master
System Description: Ubuntu 16.04.6 LTS

Recent messages:
Mark set
Quit
Mark set
Detected language: English [9 times]
Auto-saving...done
Saving file /home/guswal01/test.md...
Wrote /home/guswal01/test.md
Mark set [2 times]
(No changes need to be saved) [2 times]
Making completion list... [3 times]

Configured using:
 'configure --prefix=/home/guswal01/.local
 '--program-transform-name=s/^ctags$/ctags.emacs/''

Configured features:
XPM JPEG TIFF GIF PNG RSVG SOUND DBUS GSETTINGS GLIB NOTIFY INOTIFY
GNUTLS LIBXML2 FREETYPE HARFBUZZ XFT ZLIB TOOLKIT_SCROLL_BARS GTK3 X11
XDBE XIM THREADS PDUMPER LCMS2 GMP

Important settings:
  value of $LC_MONETARY: sv_SE.UTF-8
  value of $LC_NUMERIC: sv_SE.UTF-8
  value of $LC_TIME: sv_SE.UTF-8
  value of $LANG: en_US.UTF-8
  locale-coding-system: utf-8-unix

Major mode: Markdown

Minor modes in effect:
  orgtbl-mode: t
  shell-dirtrack-mode: t
  orgstruct-mode: t
  guess-language-mode: t
  poly-markdown-mode: t
  beacon-mode: t
  flyspell-mode: t
  flycheck-pos-tip-mode: t
  projectile-mode: t
  hes-mode: t
  global-linum-mode: t
  yas-global-mode: t
  yas-minor-mode: t
  global-company-mode: t
  company-mode: t
  global-anzu-mode: t
  anzu-mode: t
  perspeen-mode: t
  global-atomic-chrome-edit-mode: t
  global-undo-tree-mode: t
  undo-tree-mode: t
  which-key-mode: t
  anyclip-mode: t
  override-global-mode: t
  electric-pair-mode: t
  save-place-mode: t
  global-subword-mode: t
  subword-mode: t
  show-paren-mode: t
  winner-mode: t
  global-auto-revert-mode: t
  xterm-mouse-mode: t
  savehist-mode: t
  ido-everywhere: t
  tooltip-mode: t
  global-eldoc-mode: t
  eldoc-mode: t
  electric-indent-mode: t
  mouse-wheel-mode: t
  tool-bar-mode: t
  file-name-shadow-mode: t
  global-font-lock-mode: t
  font-lock-mode: t
  auto-composition-mode: t
  auto-encryption-mode: t
  auto-compression-mode: t
  column-number-mode: t
  line-number-mode: t
  transient-mark-mode: t

Load-path shadows:
None found.

Features:
(shadow sort bbdb-message mail-extr emacsbug sendmail echistory chistory
ehelp ecb-compatibility ecb-layout ecb-create-layout ecb-compilation
ecb-common-browser ecb-navigate eieio-compat ecb-cedet-wrapper
semantic/db-mode semantic/db-find semantic/db-ref semantic/analyze
semantic/sort semantic/scope semantic/analyze/fcn semantic/db
semantic/format ezimage semantic/tag-ls semantic/find semantic/ctxt
semantic/util-modes semantic/util semantic semantic/tag semantic/lex
semantic/fw mode-local cedet ecb-mode-line ecb-face tree-buffer ecb-util
help-fns radix-tree term/tmux term/xterm xterm org-rmail org-mhe org-irc
org-info org-gnus nnir gnus-sum url url-proxy url-privacy url-expand
url-methods url-history mailcap shr svg xml dom gnus-group gnus-undo
gnus-start gnus-cloud nnimap nnmail mail-source utf7 netrc nnoo
parse-time gnus-spec gnus-int gnus-range message rfc822 mml mml-sec epa
epg epg-config mm-decode mm-bodies mm-encode mail-parse rfc2231
mailabbrev gmm-utils mailheader gnus-win gnus nnheader gnus-util rmail
rmail-loaddefs rfc2047 rfc2045 ietf-drums text-property-search
mail-utils mm-util mail-prsvr org-docview doc-view jka-compr image-mode
dired dired-loaddefs org-bibtex bibtex org-bbdb org-w3m org-table
ob-latex ob-plantuml ob-org ob-shell shell ob-gnuplot ob-python ob-ditaa
ob-dot org-element avl-tree org org-macro org-footnote org-pcomplete
pcomplete org-list org-faces org-entities time-date org-version
ob-emacs-lisp ob ob-tangle org-src ob-ref ob-lob ob-table ob-keys ob-exp
ob-comint ob-core ob-eval org-compat org-macs org-loaddefs cal-menu
calendar cal-loaddefs guess-language poly-markdown polymode derived
poly-lock polymode-base polymode-weave polymode-export polymode-compat
polymode-methods polymode-core format-spec polymode-classes eieio-custom
eieio-base smart-mode-line-dark-theme smart-mode-line rich-minority
beacon server auto-complete-config auto-complete popup form-feed paredit
nameless lisp-mnt flyspell ispell whitespace flycheck-pos-tip pos-tip
flycheck find-func rainbow-delimiters projectile grep ibuf-ext ibuffer
ibuffer-loaddefs highlight-escape-sequences linum yasnippet-snippets
yasnippet company-oddmuse company-keywords company-etags etags fileloop
generator company-gtags company-dabbrev-code company-dabbrev
company-files company-capf company-cmake company-xcode company-clang
company-semantic company-eclim company-template company-bbdb company
anzu perspeen perspeen-tab powerline powerline-separators
powerline-themes atomic-chrome websocket url-cookie url-domsuf bindat
let-alist color-theme-approximate delim-col hydra-examples windmove rect
hydra lv dap-mode dap-overlays lsp lsp-mode ewoc markdown-mode
edit-indirect color noutline outline tree-widget xref url-util spinner
pcase network-stream puny nsm rmc inline imenu ht f s em-glob esh-util
dash-functional dash flymake-proc flymake compile warnings thingatpt
project bbdb bbdb-site timezone cus-edit cus-start cus-load wid-edit
ace-link avy undo-tree diff which-key advice anyclip-mode diminish
use-package use-package-ensure use-package-delight use-package-diminish
use-package-bind-key bind-key use-package-core finder-inf
local-autoloads cwarn cc-mode cc-fonts cc-guess cc-menus cc-cmds
cc-styles cc-align cc-engine cc-vars cc-defs elec-pair saveplace
cap-words superword subword paren winner autorevert filenotify xt-mouse
tango-dark-theme savehist ido gud easy-mmode comint ansi-color ring
keybinds terminals autoinsert tex-site rx cl-extra help-mode
slime-autoloads edmacro kmacro info package easymenu browse-url
url-handlers url-parse auth-source cl-seq eieio eieio-core cl-macs
eieio-loaddefs password-cache json subr-x map url-vars seq byte-opt gv
bytecomp byte-compile cconv cl-loaddefs cl-lib tooltip eldoc electric
uniquify ediff-hook vc-hooks lisp-float-type mwheel term/x-win x-win
term/common-win x-dnd tool-bar dnd fontset image regexp-opt fringe
tabulated-list replace newcomment text-mode elisp-mode lisp-mode
prog-mode register page menu-bar rfn-eshadow isearch timer select
scroll-bar mouse jit-lock font-lock syntax facemenu font-core
term/tty-colors frame cl-generic cham georgian utf-8-lang misc-lang
vietnamese tibetan thai tai-viet lao korean japanese eucjp-ms cp51932
hebrew greek romanian slovak czech european ethiopic indian cyrillic
chinese composite charscript charprop case-table epa-hook jka-cmpr-hook
help simple abbrev obarray minibuffer cl-preloaded 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 threads dbusbind inotify lcms2 dynamic-setting
system-font-setting font-render-setting move-toolbar gtk x-toolkit x
multi-tty make-network-process emacs)

Memory information:
((conses 16 478739 54790)
 (symbols 48 43732 1)
 (strings 32 147104 5075)
 (string-bytes 1 5423337)
 (vectors 16 61266)
 (vector-slots 8 737765 36386)
 (floats 8 541 96)
 (intervals 56 2393 2891)
 (buffers 992 16)
 (heap 1024 31120 2033))