From c57805a926baba9012c3e5dac659900ffb7f1a5a Mon Sep 17 00:00:00 2001 From: Marco Rebhan Date: Sun, 20 Nov 2022 02:13:34 +0100 Subject: [PATCH] Handle spawning iTerm and screen commands with arguments containing newlines Before, sed would add quotes to every line of the single multiline argument, causing the final quoted argument to "split ... command" or screen to contain unquoted newlines such as this (from kakoune-cr): tell application "iTerm" tell current session of current window tell (split vertically with same profile command "env PATH='...' 'sh' '-c' '' ' export KAKOUNE_SESSION=$1' ' export KAKOUNE_CLIENT=$2' ' shift 3' '' ' [ $# = 0 ] && set \"$SHELL\"' '' ' \"sh\"' ' ' '--' '51909' 'client0' 'terminal' ") to select end tell end tell Instead of using sed to do this which operates on single lines at a time, simply use printf to insert ' quotes before and after the entire argument. --- rc/windowing/iterm.kak | 18 +++--------------- rc/windowing/screen.kak | 6 +----- 2 files changed, 4 insertions(+), 20 deletions(-) diff --git a/rc/windowing/iterm.kak b/rc/windowing/iterm.kak index 3db623b7..438c7fce 100644 --- a/rc/windowing/iterm.kak +++ b/rc/windowing/iterm.kak @@ -15,11 +15,7 @@ define-command -hidden -params 2.. iterm-terminal-split-impl %{ # join the arguments as one string for the shell execution (see x11.kak) args=$( for i in "$@"; do - if [ "$i" = '' ]; then - printf "'' " - else - printf %s "$i" | sed -e "s|'|'\\\\''|g; s|^|'|; s|$|' |" - fi + printf "'%s' " "$(printf %s "$i" | sed "s|'|'\\\\''|g")" done ) @@ -69,11 +65,7 @@ The program passed as argument will be executed in the new terminal'\ # see above args=$( for i in "$@"; do - if [ "$i" = '' ]; then - printf "'' " - else - printf %s "$i" | sed -e "s|'|'\\\\''|g; s|^|'|; s|$|' |" - fi + printf "'%s' " "$(printf %s "$i" | sed "s|'|'\\\\''|g")" done ) escaped=$(printf %s "$args" | sed -e 's|\\|\\\\|g; s|"|\\"|g') @@ -96,11 +88,7 @@ The program passed as argument will be executed in the new terminal'\ # see above args=$( for i in "$@"; do - if [ "$i" = '' ]; then - printf "'' " - else - printf %s "$i" | sed -e "s|'|'\\\\''|g; s|^|'|; s|$|' |" - fi + printf "'%s' " "$(printf %s "$i" | sed "s|'|'\\\\''|g")" done ) escaped=$(printf %s "$args" | sed -e 's|\\|\\\\|g; s|"|\\"|g') diff --git a/rc/windowing/screen.kak b/rc/windowing/screen.kak index f9050886..b59b7588 100644 --- a/rc/windowing/screen.kak +++ b/rc/windowing/screen.kak @@ -17,11 +17,7 @@ define-command screen-terminal-impl -hidden -params 3.. %{ # see x11.kak for what this achieves args=$( for i in "$@"; do - if [ "$i" = '' ]; then - printf "'' " - else - printf %s "$i" | sed -e "s|'|'\\\\''|g; s|^|'|; s|$|' |" - fi + printf "'%s' " "$(printf %s "$i" | sed "s|'|'\\\\''|g")" done ) screen -X screen sh -c "${args} ; screen -X remove" < "/dev/$tty"