[vim/vim] cannot enable focus events without setting `<FocusGained>` and `<FocusLost>` (Issue #9296)

121 views
Skip to first unread message

lacygoill

unread,
Dec 7, 2021, 11:58:31 PM12/7/21
to vim/vim, Subscribed

Steps to reproduce

Open a gnome terminal, and run this shell command:

TERM=gnome vim -Nu NONE -S <(cat <<'EOF'
    vim9script
    execute "set t_fe=\<Esc>[?1004h"
    execute "set t_fd=\<Esc>[?1004l"
    echoraw(&t_fe)
    autocmd FocusLost * echomsg 'FocusLost is fired'
    &ttimeoutlen = 100
EOF
)

E349 is given:

E349: No identifier under cursor

Focus a different window (e.g. web browser), then focus back the Vim terminal window.

E349 is given a second time:

E349: No identifier under cursor

Expected behavior

No error is given. This message is echo'ed:

FocusLost is fired

Because according to :help xterm-focus-event, setting 't_fe' and 't_fd' is all that is needed to enable the focus event tracking feature:

If your terminal does support this but Vim does not recognize the terminal,
you may have to set the options yourself: >
let &t_fe = "<Esc>[?1004h"
let &t_fd = "<Esc>[?1004l"

Operating system

Ubuntu 20.04.3 LTS

Version of Vim

8.2 Included patches: 1-3757

Additional Context

The issue can be fixed by setting <FocusGained> and <FocusLost>:

TERM=gnome vim -Nu NONE -S <(cat <<'EOF'
    vim9script
    execute "set t_fe=\<Esc>[?1004h"
    execute "set t_fd=\<Esc>[?1004l"
    execute "set <FocusGained>=\<Esc>[I"
    execute "set <FocusLost>=\<Esc>[O"
    echoraw(&t_fe)
    autocmd FocusLost * echomsg 'FocusLost is fired'
    &ttimeoutlen = 100
EOF
)

There are 2 possibilities:

  • this is a bug: when setting 't_fe', <FocusGained> and <FocusLost> should be automatically set as well
  • this is not a bug: the documentation should be updated to tell the user that setting 't_fe' and 't_fd' is not enough; they also have to set <FocusGained> and <FocusLost>

IMO, it's a bug. Vim should be able to set <FocusGained> and <FocusLost> automatically.
For example, that's what it does for 'ttymouse':

vim -Nu NONE -S <(cat <<'EOF'
    vim9script
    &term = 'xterm-256color'
    execute('set! termcap')->split('\n')->writefile('/tmp/termcap1.log')
    &ttymouse = 'sgr'
    execute('set! termcap')->split('\n')->writefile('/tmp/termcap2.log')
    edit /tmp/termcap1.log
    split /tmp/termcap2.log
    windo diffthis
EOF
)

In the upper window, notice how Vim automatically set <SgrMouse> and <SgrMouseRelease> when we set 'ttymouse' to sgr:

<SgrMouseRelease> ^[[<*m
<SgrMouse>  ^[[<*M

Why can't Vim do the same when we set 't_fe'?


If this is not a bug, then at least the documentation should be updated. As a suggestion, here is a patch:

diff --git a/runtime/doc/term.txt b/runtime/doc/term.txt
index 3333a3ab6..bc42233bb 100644
--- a/runtime/doc/term.txt
+++ b/runtime/doc/term.txt
@@ -150,12 +150,14 @@ terminal that tmux is running in): >
         " Enable focus event tracking, see  :help xterm-focus-event
         let &t_fe = "\<Esc>[?1004h"
         let &t_fd = "\<Esc>[?1004l"
+        execute "set <FocusGained>=\<Esc>[I"
+        execute "set <FocusLost>=\<Esc>[O"
 
         " Enable modified arrow keys, see  :help arrow_modifiers
-        execute "silent! set <xUp>=\<Esc>[@;*A"
-        execute "silent! set <xDown>=\<Esc>[@;*B"
-        execute "silent! set <xRight>=\<Esc>[@;*C"
-        execute "silent! set <xLeft>=\<Esc>[@;*D"
+        execute "set <xUp>=\<Esc>[@;*A"
+        execute "set <xDown>=\<Esc>[@;*B"
+        execute "set <xRight>=\<Esc>[@;*C"
+        execute "set <xLeft>=\<Esc>[@;*D"
     endif
 <
 							*cs7-problem*
@@ -606,6 +608,8 @@ If your terminal does support this but Vim does not recognize the terminal,
 you may have to set the options yourself: >
 	let &t_fe = "\<Esc>[?1004h"
 	let &t_fd = "\<Esc>[?1004l"
+	execute "set <FocusGained>=\<Esc>[I"
+	execute "set <FocusLost>=\<Esc>[O"
 If this causes garbage to show when Vim starts up then it doesn't work.
 
 							*termcap-colors*


You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub.
Triage notifications on the go with GitHub Mobile for iOS or Android.

Bram Moolenaar

unread,
Dec 8, 2021, 2:51:28 PM12/8/21
to vim/vim, Subscribed

We should update the help to mention setting <FocusGained> and <FocusLost>.

The use_xterm_like_mouse() checks for "screen", "tmux", "stterm". Perhaps it should also check for "gnome"?
Is $TERM usually just "gnome" or could there be an addition?

I side effect is that 'ttymouse' will default to "xterm". Is that OK?

lacygoill

unread,
Dec 8, 2021, 3:08:12 PM12/8/21
to vim/vim, Subscribed

The use_xterm_like_mouse() checks for "screen", "tmux", "stterm". Perhaps it should also check for "gnome"?

I used gnome because I suppose it's the default terminal on Ubuntu. But the same issue probably applies to Konsole, xfce, and other popular terminals. I need to do more tests to be sure, but I suspect the suggested settings would work in all popular terminals.

I don't know why there is a link between the focus event tracking feature and the mouse codes. Maybe the code should handle both features in separate functions?

Is $TERM usually just "gnome" or could there be an addition?

Here is the list of all terminal names mathing "gnome" on my machine:

$ toe -as | grep gnome
*-----+-:       gnome           GNOME Terminal
*-----+-:       gnome+pcfkeys   VTE's variation on xterm+pcfkeys
*-----+-:       gnome-2007      GNOME Terminal snapshot 2.18.1
*-----+-:       gnome-2008      GNOME Terminal snapshot 2.22.3
*-----+-:       gnome-2012      GNOME Terminal 3.6.0
*-----+-:       gnome-256color  GNOME Terminal with xterm 256-colors
*-----+-:       gnome-fc5       GNOME Terminal
*-----*-:       gnome-rh62      Gnome terminal
*-----*-:       gnome-rh72      GNOME Terminal
*-----+-:       gnome-rh80      GNOME Terminal
*-----+-:       gnome-rh90      GNOME Terminal
*-----+-:       screen-bce.gnome        screen optimized for GNOME-Terminal
*-----+-:       screen.gnome    screen in GNOME Terminal

A side effect is that 'ttymouse' will default to "xterm". Is that OK?

In gnome-terminal and xfce4-terminal, the default value is empty. Switching to xterm should be OK, but I would need to make more tests to be sure.

Bram Moolenaar

unread,
Dec 8, 2021, 4:02:16 PM12/8/21
to vim/vim, Subscribed

I don't anticipate it will cause problems, let's just recognize gnome and see if someone has a problem with it.

Christian Brabandt

unread,
Mar 12, 2022, 11:51:59 AM3/12/22
to vim/vim, Subscribed

Closed #9296.


Reply to this email directly, view it on GitHub.
Triage notifications on the go with GitHub Mobile for iOS or Android.

You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/issue/9296/issue_event/6229971816@github.com>

Christian Brabandt

unread,
Mar 12, 2022, 11:51:59 AM3/12/22
to vim/vim, Subscribed

fixed by 48873ae


Reply to this email directly, view it on GitHub.
Triage notifications on the go with GitHub Mobile for iOS or Android.

You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/issues/9296/1065917476@github.com>

Reply all
Reply to author
Forward
0 new messages