Files
fish-shell/share/functions/fish_vi_cursor.fish
Johannes Altmanninger 495083249b Fix regression causing cursor shape commands to leak into noninteractive shell
As reported in
https://matrix.to/#/!YLTeaulxSDauOOxBoR:matrix.org/$CLuoHTdvcRj_8-HBBq0p-lmGWeix5khEtKEDxN2Ulfo

Running

	fish -C '
		fzf_key_bindings
		echo fish_vi_key_bindings >>~/.config/fish/config.fish
		fzf-history-widget
	'

and pressing "enter" will add escape sequences like "[2 q" (cursor shape)
to fish's command line.

This is because fzf-history-widget binds "enter" to a filter
that happens to be a fish script:

	set -lx FZF_DEFAULT_OPTS \
		... \
		"--bind='enter:become:string replace -a -- \n\t \n {2..} | string collect'" \
		'--with-shell='(status fish-path)\\ -c)

The above ~/.config/fish/config.fish (redundantly) runs "fish_vi_key_bindings"
even in *noninteractive* shells, then "fish_vi_cursor" will print cursor
sequences in its "fish_exit" handler.  The sequence is not printed to the
terminal but to fzf which doesn't parse CSI commands.

This is a regression introduced by a5dfa84f73 (fish_vi_cursor: skip if stdin
is not a tty, 2023-11-14). That commit wanted "fish -c read" to be able to
use Vi cursor.  This is a noninteractive shell, but inside "read" we are
"effectively interactive".  However "status is-interactive" does not tell
us that.

Let's use a more contained fix to make sure that we print escape sequences only
if either fish is interactive, or if we are evaluating an interactive read.

In general, "fish -c read" is prone to configuration errors, since we
recommend gating configuration (for bind etc) on "status is-interactive"
which will not run here.
2025-03-02 09:34:06 +01:00

35 lines
1.2 KiB
Fish

function fish_vi_cursor -d 'Set cursor shape for different vi modes'
set -q fish_cursor_unknown
or set -g fish_cursor_unknown block
function __fish_vi_cursor --argument-names varname
if not status is-interactive; and not status is-interactive-read
return
end
if not set -q $varname
switch $varname
case fish_cursor_insert
__fish_cursor_xterm line
case fish_cursor_replace_one fish_cursor_replace
__fish_cursor_xterm underscore
case '*'
__fish_cursor_xterm $fish_cursor_unknown
end
return
end
__fish_cursor_xterm $$varname
end
function fish_vi_cursor_handle --on-variable fish_bind_mode --on-event fish_postexec --on-event fish_focus_in --on-event fish_read
__fish_vi_cursor fish_cursor_$fish_bind_mode
end
function fish_vi_cursor_handle_preexec --on-event fish_preexec --on-event fish_exit
set -l varname fish_cursor_external
if not set -q $varname
set varname fish_cursor_default
end
__fish_vi_cursor $varname
end
end