x11-terminal: Leverage the shell quoting of lists for perfect forwarding

Fixes x11-terminal forwarding
This commit is contained in:
Alex Leferry 2 2020-01-13 05:23:35 +01:00
parent b85365bff8
commit fed8f01e47

View File

@ -38,32 +38,15 @@ define-command x11-terminal -params 1.. -shell-completion -docstring '
x11-terminal <program> [<arguments>]: create a new terminal as an x11 window x11-terminal <program> [<arguments>]: create a new terminal as an x11 window
The program passed as argument will be executed in the new terminal' \ The program passed as argument will be executed in the new terminal' \
%{ %{
evaluate-commands %sh{ evaluate-commands -save-regs 'a' %{
if [ -z "${kak_opt_termcmd}" ]; then set-register a %arg{@}
echo "fail 'termcmd option is not set'" evaluate-commands %sh{
exit if [ -z "${kak_opt_termcmd}" ]; then
fi echo "fail 'termcmd option is not set'"
# join arguments into a single string, in which they're delimited exit
# by single quotes, and with single quotes inside transformed to '\'' fi
# so that sh -c "$args" will re-split the arguments properly setsid ${kak_opt_termcmd} "$kak_quoted_reg_a" < /dev/null > /dev/null 2>&1 &
# example: }
# $1 = ab
# $2 = foo bar
# $3 =
# $4 = foo'bar
# $args = 'ab' 'foo bar' '' 'foo'\''bar'
# would be nicer to do in a single sed/awk call but that's difficult
args=$(
for i in "$@"; do
# special case to preserve empty variables as sed will not touch these
if [ "$i" = '' ]; then
printf "'' "
else
printf %s "$i" | sed -e "s|'|'\\\\''|g; s|^|'|; s|$|' |"
fi
done
)
setsid ${kak_opt_termcmd} "$args" < /dev/null > /dev/null 2>&1 &
} }
} }