functions/fish_vi_cursor: Simplify detection

This was quite famously rather complicated.

We drop a bunch of cases - we can't handle tmux-starting-terminals
100% accurately, so we just don't try. It should be quite rare that
somebody starts a different terminal from tmux.

We drop the `tput` since it is useless (like terminfo in general for
feature-detection, because everyone claims to be xterm).

So we just check if we are in konsole, iTerm, vte or genuine-xterm.

Fixes #3696.

See #3481.
This commit is contained in:
Fabian Homborg 2019-04-28 12:07:38 +02:00
parent 787ef3e558
commit c22af0d8c7

View file

@ -1,17 +1,5 @@
function fish_vi_cursor -d 'Set cursor shape for different vi modes' function fish_vi_cursor -d 'Set cursor shape for different vi modes'
# Check hard if we are in a supporting terminal. # Check hard if we are in a supporting terminal.
#
# Challenges here are term-in-a-terms (emacs ansi-term does not support this, tmux does),
# that we can only figure out if we are in konsole/iterm/vte via exported variables,
# and ancient xterm versions.
#
# tmux defaults to $TERM = screen, but can do this if it is in a supporting terminal.
# Unfortunately, we can only detect this via the exported variables, so we miss some cases.
#
# We will also miss some cases of terminal-stacking,
# e.g. tmux started in suckless' st (no support) started in konsole.
# But since tmux in konsole seems rather common and that case so uncommon,
# we will just fail there (though it seems that tmux or st swallow it anyway).
# If we're not interactive, there is effectively no bind mode. # If we're not interactive, there is effectively no bind mode.
if not status is-interactive if not status is-interactive
@ -22,56 +10,32 @@ function fish_vi_cursor -d 'Set cursor shape for different vi modes'
return return
end end
# vte-based terms set $TERM = xterm*, but only gained support relatively recently. # vte-based terms set $TERM = xterm*, but only gained support in 2015.
# From https://bugzilla.gnome.org/show_bug.cgi?id=720821, it appears it was version 0.40.0 # From https://bugzilla.gnome.org/show_bug.cgi?id=720821, it appears it was version 0.40.0
if set -q VTE_VERSION if set -q VTE_VERSION
and test "$VTE_VERSION" -lt 4000 2>/dev/null and test "$VTE_VERSION" -lt 4000 2>/dev/null
return return
end end
# Similarly, XTerm can do it since v280. # Similarly, genuine XTerm can do it since v280.
# Other terminals that set TERM=xterm don't set XTERM_VERSION.
if set -q XTERM_VERSION if set -q XTERM_VERSION
and not test (string replace -r "XTerm\((\d+)\)" '$1' -- "$XTERM_VERSION") -ge 280 2>/dev/null and not test (string replace -r "XTerm\((\d+)\)" '$1' -- "$XTERM_VERSION") -ge 280 2>/dev/null
return return
end end
# We use the `tput` here just to see if terminfo thinks we can change the cursor. # We need one of these terms.
# We cannot use that sequence directly as it's not the correct one for konsole and iTerm, # It would be lovely if we could rely on terminfo, but:
# and because we may want to change the cursor even though terminfo says we can't (tmux). # - The "Ss" entry isn't a thing in macOS' old and crusty terminfo
if begin # - It is set for xterm, and everyone and their dog claims to be xterm
not command -sq tput; or not tput Ss >/dev/null 2>/dev/null #
end # So we just don't care about $TERM _at all_ - it is useless for our purposes.
# Whitelist tmux... #
and not begin # Note: Previous versions also checked $TMUX, and made sure that then $TERM was screen* or tmux*.
set -q TMUX # We don't care, since we *cannot* handle term-in-a-terms 100% correctly.
# ...in a supporting term... if not set -q KONSOLE_PROFILE_NAME
and begin and not set -q ITERM_PROFILE
set -q KONSOLE_PROFILE_NAME and not set -q VTE_VERSION # which version is already checked above
or set -q ITERM_PROFILE and not set -q XTERM_VERSION
or set -q VTE_VERSION # which version is already checked above
or set -q XTERM_VERSION
end
# .. unless an unsupporting terminal has been started in tmux inside a supporting one
and begin string match -q "screen*" -- $TERM
or string match -q "tmux*" -- $TERM
end
end
and not string match -q "konsole*" -- $TERM
or begin
# TERM = xterm is special because plenty of things claim to be it, but aren't fully compatible
# This includes old vte-terms (without $VTE_VERSION), old xterms (without $XTERM_VERSION or < 280)
# and maybe other stuff.
# This needs to be kept _at least_ as long as Ubuntu 14.04 is still a thing
# because that ships a gnome-terminal without support and without $VTE_VERSION.
string match -q 'xterm*' -- $TERM
and not begin set -q KONSOLE_PROFILE_NAME
or set -q ITERM_PROFILE
or set -q VTE_VERSION # which version is already checked above
or set -q XTERM_VERSION
end
end
return return
end end