Merge remote-tracking branch 'occivink/generic-term-command'

This commit is contained in:
Maxime Coste 2018-12-28 06:27:51 +11:00
commit 1c7b63709f
6 changed files with 236 additions and 128 deletions

12
rc/base/new-client.kak Normal file
View File

@ -0,0 +1,12 @@
define-command new -params .. -command-completion -docstring '
new [<commands>]: create a new kakoune client
The ''terminal'' alias is being used to determine the user''s preferred terminal emulator
The optional arguments are passed as commands to the new client' \
%{
try %{
terminal kak -c %val{session} -e "%arg{@}"
} catch %{
fail "The 'terminal' alias must be defined to use this command"
}
}

View File

@ -5,45 +5,58 @@ hook -group GNUscreen global KakBegin .* %sh{
[ -z "${STY}" ] && exit [ -z "${STY}" ] && exit
echo " echo "
alias global focus screen-focus alias global focus screen-focus
alias global new screen-new-vertical alias global terminal screen-terminal-vertical
" "
} }
define-command screen-new-vertical -params .. -command-completion -docstring "Split the current pane into two, left and right" %{ define-command screen-terminal-impl -hidden -params 3.. %{
nop %sh{
tty="$(ps -o tty ${kak_client_pid} | tail -n 1)"
screen -X eval \
'split -h' \
'focus down' \
"screen sh -c 'kak -c \"${kak_session}\" -e \"$*\" ;
screen -X remove'" \
< "/dev/$tty"
}
}
define-command screen-new-horizontal -params .. -command-completion -docstring "Split the current pane into two, top and bottom" %{
nop %sh{
tty="$(ps -o tty ${kak_client_pid} | tail -n 1)"
screen -X eval \
'split -v' \
'focus right' \
"screen sh -c 'kak -c \"${kak_session}\" -e \"$*\" ;
screen -X remove'" \
< "/dev/$tty"
}
}
define-command screen-new-window -params .. -command-completion -docstring "Create a new window" %{
nop %sh{ nop %sh{
tty="$(ps -o tty ${kak_client_pid} | tail -n 1)" tty="$(ps -o tty ${kak_client_pid} | tail -n 1)"
screen -X screen kak -c "${kak_session}" -e "$*" < "/dev/$tty" screen -X eval "$1" "$2"
shift 2
# 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
done
)
screen -X screen sh -c "${args} ; screen -X remove" < "/dev/$tty"
} }
} }
define-command -docstring %{screen-focus [<client>]: focus the given client define-command screen-terminal-vertical -params 1.. -shell-completion -docstring '
If no client is passed then the current one is used} \ screen-terminal-vertical <program> [<arguments>] [<arguments>]: create a new terminal as a screen pane
-params ..1 -client-completion \ The current pane is split into two, left and right
screen-focus %{ evaluate-commands %sh{ The program passed as argument will be executed in the new terminal' \
%{
screen-terminal-impl 'split -v' 'focus right' %arg{@}
}
define-command screen-terminal-horizontal -params 1.. -shell-completion -docstring '
screen-terminal-horizontal <program> [<arguments>]: create a new terminal as a screen pane
The current pane is split into two, top and bottom
The program passed as argument will be executed in the new terminal' \
%{
screen-terminal-impl 'split -h' 'focus down' %arg{@}
}
define-command screen-terminal-window -params 1.. -shell-completion -docstring '
screen-terminal-window <program> [<arguments>]: create a new terminal as a screen window
The program passed as argument will be executed in the new terminal' \
%{
nop %sh{
tty="$(ps -o tty ${kak_client_pid} | tail -n 1)"
screen -X screen "$@" < "/dev/$tty"
}
}
define-command screen-focus -params ..1 -client-completion -docstring '
screen-focus [<client>]: focus the given client
If no client is passed then the current one is used' \
%{
evaluate-commands %sh{
if [ $# -eq 1 ]; then if [ $# -eq 1 ]; then
printf %s\\n " printf %s\\n "
evaluate-commands -client '$1' focus evaluate-commands -client '$1' focus
@ -52,4 +65,5 @@ If no client is passed then the current one is used} \
tty="$(ps -o tty ${kak_client_pid} | tail -n 1)" tty="$(ps -o tty ${kak_client_pid} | tail -n 1)"
screen -X select "${kak_client_env_WINDOW}" < "/dev/$tty" screen -X select "${kak_client_env_WINDOW}" < "/dev/$tty"
fi fi
} } }
}

View File

@ -6,45 +6,56 @@ hook global KakBegin .* %sh{
if [ -n "$TMUX" ]; then if [ -n "$TMUX" ]; then
echo " echo "
alias global focus tmux-focus alias global focus tmux-focus
alias global new tmux-new-horizontal alias global terminal tmux-terminal-horizontal
" "
fi fi
} }
## Temporarily override the default client creation command define-command -hidden -params 2.. tmux-terminal-impl %{
define-command -hidden -params 1.. tmux-new-impl %{
evaluate-commands %sh{ evaluate-commands %sh{
tmux=${kak_client_env_TMUX:-$TMUX} tmux=${kak_client_env_TMUX:-$TMUX}
if [ -z "$tmux" ]; then if [ -z "$tmux" ]; then
echo "echo -markup '{Error}This command is only available in a tmux session'" echo "fail 'This command is only available in a tmux session'"
exit exit
fi fi
tmux_args="$1" tmux_args="$1"
shift shift
if [ $# -ne 0 ]; then kakoune_params="-e '$@'"; fi # ideally we should escape single ';' to stop tmux from interpreting it as a new command
TMUX=$tmux tmux $tmux_args "env TMPDIR='${TMPDIR}' kak -c ${kak_session} ${kakoune_params}" < /dev/null > /dev/null 2>&1 & # but that's probably too rare to care
TMUX=$tmux tmux $tmux_args env TMPDIR="$TMPDIR" "$@" < /dev/null > /dev/null 2>&1 &
} }
} }
define-command tmux-new-vertical -params .. -command-completion -docstring "Split the current pane into two, top and bottom" %{ define-command tmux-terminal-vertical -params 1.. -shell-completion -docstring '
tmux-new-impl 'split-window -v' %arg{@} tmux-terminal-vertical <program> [<arguments>]: create a new terminal as a tmux pane
The current pane is split into two, top and bottom
The program passed as argument will be executed in the new terminal' \
%{
tmux-terminal-impl 'split-window -v' %arg{@}
}
define-command tmux-terminal-horizontal -params 1.. -shell-completion -docstring '
tmux-terminal-horizontal <program> [<arguments>]: create a new terminal as a tmux pane
The current pane is split into two, left and right
The program passed as argument will be executed in the new terminal' \
%{
tmux-terminal-impl 'split-window -h' %arg{@}
}
define-command tmux-terminal-window -params 1.. -shell-completion -docstring '
tmux-terminal-window <program> [<arguments>] [<arguments>]: create a new terminal as a tmux window
The program passed as argument will be executed in the new terminal' \
%{
tmux-terminal-impl 'new-window' %arg{@}
} }
define-command tmux-new-horizontal -params .. -command-completion -docstring "Split the current pane into two, left and right" %{ define-command tmux-focus -params ..1 -client-completion -docstring '
tmux-new-impl 'split-window -h' %arg{@} tmux-focus [<client>]: focus the given client
If no client is passed then the current one is used' \
%{
evaluate-commands %sh{
if [ $# -eq 1 ]; then
printf "evaluate-commands -client '%s' focus" "$1"
elif [ -n "${kak_client_env_TMUX}" ]; then
TMUX="${kak_client_env_TMUX}" tmux select-pane -t "${kak_client_env_TMUX_PANE}" > /dev/null
fi
}
} }
define-command tmux-new-window -params .. -command-completion -docstring "Create a new window" %{
tmux-new-impl 'new-window' %arg{@}
}
define-command -docstring %{tmux-focus [<client>]: focus the given client
If no client is passed then the current one is used} \
-params ..1 -client-completion \
tmux-focus %{ evaluate-commands %sh{
if [ $# -eq 1 ]; then
printf %s\\n "evaluate-commands -client '$1' focus"
elif [ -n "${kak_client_env_TMUX}" ]; then
TMUX="${kak_client_env_TMUX}" tmux select-pane -t "${kak_client_env_TMUX_PANE}" > /dev/null
fi
} }

View File

@ -22,29 +22,51 @@ A shell command is appended to the one set in this option at runtime} \
done done
} }
define-command -docstring %{x11-new [<command>]: create a new kak client for the current session define-command x11-terminal -params 1.. -shell-completion -docstring '
The optional arguments will be passed as arguments to the new client} \ x11-terminal <program> [<arguments>]: create a new terminal as an x11 window
-params .. \ The program passed as argument will be executed in the new terminal' \
-command-completion \ %{
x11-new %{ evaluate-commands %sh{ evaluate-commands %sh{
if [ -z "${kak_opt_termcmd}" ]; then if [ -z "${kak_opt_termcmd}" ]; then
echo "echo -markup '{Error}termcmd option is not set'" echo "fail 'termcmd option is not set'"
exit exit
fi fi
if [ $# -ne 0 ]; then kakoune_params="-e '$@'"; fi # join arguments into a single string, in which they're delimited
setsid ${kak_opt_termcmd} "kak -c ${kak_session} ${kakoune_params}" < /dev/null > /dev/null 2>&1 & # by single quotes, and with single quotes inside transformed to '\''
}} # so that sh -c "$args" will re-split the arguments properly
# 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 won't 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 &
}
}
define-command -docstring %{x11-focus [<client>]: focus a given client's window define-command x11-focus -params ..1 -client-completion -docstring '
If no client is passed, then the current client is used} \ x11-focus [<kakoune_client>]: focus a given client''s window
-params ..1 -client-completion \ If no client is passed, then the current client is used' \
x11-focus %{ evaluate-commands %sh{ %{
evaluate-commands %sh{
if [ $# -eq 1 ]; then if [ $# -eq 1 ]; then
printf %s\\n "evaluate-commands -client '$1' focus" printf "evaluate-commands -client '%s' focus" "$1"
else else
xdotool windowactivate $kak_client_env_WINDOWID > /dev/null xdotool windowactivate $kak_client_env_WINDOWID > /dev/null
fi fi
} } }
}
alias global focus x11-focus alias global focus x11-focus
alias global new x11-new alias global terminal x11-terminal

View File

@ -6,18 +6,30 @@
hook global KakBegin .* %sh{ hook global KakBegin .* %sh{
if [ "$TERM_PROGRAM" = "iTerm.app" ] && [ -z "$TMUX" ]; then if [ "$TERM_PROGRAM" = "iTerm.app" ] && [ -z "$TMUX" ]; then
echo " echo "
alias global new iterm-new-vertical
alias global focus iterm-focus alias global focus iterm-focus
" "
fi fi
} }
define-command -hidden -params 1.. iterm-new-split-impl %{ define-command -hidden -params 2.. iterm-terminal-split-impl %{
nop %sh{ nop %sh{
direction="$1" direction="$1"
shift shift
if [ $# -gt 0 ]; then kakoune_params="-e \\\"$*\\\""; fi # join the arguments as one string for the shell execution (see x11.kak)
cmd="env PATH='${PATH}' TMPDIR='${TMPDIR}' kak -c '${kak_session}' ${kakoune_params}" args=$(
for i in "$@"; do
if [ "$i" = '' ]; then
printf "'' "
else
printf %s "$i" | sed -e "s|'|'\\\\''|g; s|^|'|; s|$|' |"
fi
done
)
# go through another round of escaping for osascript
# \ -> \\
# " -> \"
escaped=$(printf %s "$args" | sed -e 's|\|\\\\|g; s|"|\\"|g')
cmd="env PATH='${PATH}' TMPDIR='${TMPDIR}' $escaped"
osascript \ osascript \
-e "tell application \"iTerm\"" \ -e "tell application \"iTerm\"" \
-e " tell current session of current window" \ -e " tell current session of current window" \
@ -27,21 +39,38 @@ define-command -hidden -params 1.. iterm-new-split-impl %{
} }
} }
define-command iterm-new-vertical -params .. -command-completion -docstring "Split the current pane into two, top and bottom" %{ define-command iterm-terminal-vertical -params 1.. -shell-completion -docstring '
iterm-new-split-impl 'vertically' %arg{@} iterm-terminal-vertical <program> [<arguments>]: create a new terminal as an iterm pane
The current pane is split into two, top and bottom
The program passed as argument will be executed in the new terminal'\
%{
iterm-terminal-split-impl 'vertically' %arg{@}
}
define-command iterm-terminal-horizontal -params 1.. -shell-completion -docstring '
iterm-terminal-horizontal <program> [<arguments>]: create a new terminal as an iterm pane
The current pane is split into two, left and right
The program passed as argument will be executed in the new terminal'\
%{
iterm-terminal-split-impl 'horizontally' %arg{@}
} }
define-command iterm-new-horizontal -params .. -command-completion -docstring "Split the current pane into two, left and right" %{ define-command iterm-terminal-tab -params 1.. -shell-completion -docstring '
iterm-new-split-impl 'horizontally' %arg{@} iterm-terminal-tab <program> [<arguments>]: create a new terminal as an iterm tab
} The program passed as argument will be executed in the new terminal'\
%{
define-command -params .. -command-completion \
-docstring %{iterm-new-tab [<arguments>]: create a new tab
All optional arguments are forwarded to the new kak client} \
iterm-new-tab %{
nop %sh{ nop %sh{
if [ $# -gt 0 ]; then kakoune_params="-e \\\"$*\\\""; fi # see above
cmd="env PATH='${PATH}' TMPDIR='${TMPDIR}' kak -c '${kak_session}' ${kakoune_params}" args=$(
for i in "$@"; do
if [ "$i" = '' ]; then
printf "'' "
else
printf %s "$i" | sed -e "s|'|'\\\\''|g; s|^|'|; s|$|' |"
fi
done
)
escaped=$(printf %s "$args" | sed -e 's|\|\\\\|g; s|"|\\"|g')
cmd="env PATH='${PATH}' TMPDIR='${TMPDIR}' $escaped"
osascript \ osascript \
-e "tell application \"iTerm\"" \ -e "tell application \"iTerm\"" \
-e " tell current window" \ -e " tell current window" \
@ -51,13 +80,23 @@ All optional arguments are forwarded to the new kak client} \
} }
} }
define-command -params .. -command-completion \ define-command iterm-terminal-window -params 1.. -shell-completion -docstring '
-docstring %{iterm-new-window [<arguments>]: create a new window iterm-terminal-window <program> [<arguments>]: create a new terminal as an iterm window
All optional arguments are forwarded to the new kak client} \ The program passed as argument will be executed in the new terminal'\
iterm-new-window %{ %{
nop %sh{ nop %sh{
if [ $# -gt 0 ]; then kakoune_params="-e \\\"$*\\\""; fi # see above
cmd="env PATH='${PATH}' TMPDIR='${TMPDIR}' kak -c '${kak_session}' ${kakoune_params}" args=$(
for i in "$@"; do
if [ "$i" = '' ]; then
printf "'' "
else
printf %s "$i" | sed -e "s|'|'\\\\''|g; s|^|'|; s|$|' |"
fi
done
)
escaped=$(printf %s "$args" | sed -e 's|\|\\\\|g; s|"|\\"|g')
cmd="env PATH='${PATH}' TMPDIR='${TMPDIR}' $escaped"
osascript \ osascript \
-e "tell application \"iTerm\"" \ -e "tell application \"iTerm\"" \
-e " create window with default profile command \"${cmd}\"" \ -e " create window with default profile command \"${cmd}\"" \
@ -65,10 +104,11 @@ All optional arguments are forwarded to the new kak client} \
} }
} }
define-command -params ..1 -client-completion \ define-command iterm-focus -params ..1 -client-completion -docstring '
-docstring %{iterm-focus [<client>]: focus the given client iterm-focus [<client>]: focus the given client
If no client is passed then the current one is used} \ If no client is passed then the current one is used' \
iterm-focus %{ evaluate-commands %sh{ %{
evaluate-commands %sh{
if [ $# -eq 1 ]; then if [ $# -eq 1 ]; then
printf %s\\n "evaluate-commands -client '$1' focus" printf %s\\n "evaluate-commands -client '$1' focus"
else else

View File

@ -3,8 +3,8 @@ declare-option -docstring %{window type that kitty creates on new and repl calls
hook -group kitty-hooks global KakBegin .* %sh{ hook -group kitty-hooks global KakBegin .* %sh{
if [ "$TERM" = "xterm-kitty" ] && [ -z "$TMUX" ]; then if [ "$TERM" = "xterm-kitty" ] && [ -z "$TMUX" ]; then
echo " echo "
alias global new kitty-new alias global terminal kitty-terminal
alias global new-tab kitty-new-tab alias global terminal-tab kitty-terminal-tab
alias global focus kitty-focus alias global focus kitty-focus
alias global repl kitty-repl alias global repl kitty-repl
alias global send-text kitty-send-text alias global send-text kitty-send-text
@ -12,42 +12,51 @@ hook -group kitty-hooks global KakBegin .* %sh{
fi fi
} }
define-command -docstring %{kitty-new [<command>]: create a new kak client for the current session define-command kitty-terminal -params 1.. -shell-completion -docstring '
Optional arguments are passed as arguments to the new client} \ kitty-terminal <program> [<arguments>]: create a new terminal as a kitty window
-params .. \ The program passed as argument will be executed in the new terminal' \
-command-completion \ %{
kitty-new %{ nop %sh{ nop %sh{
kitty @ new-window --no-response --window-type $kak_opt_kitty_window_type "$(command -v kak 2>/dev/null)" -c "${kak_session}" -e "$*" kitty @ new-window --no-response --window-type $kak_opt_kitty_window_type "$@"
}} }
}
define-command -docstring %{kitty-new-tab [<arguments>]: create a new tab define-command kitty-terminal-tab -params 1.. -shell-completion -docstring '
All optional arguments are forwarded to the new kak client} \ kitty-terminal-tab <program> [<arguments>]: create a new terminal as kitty tab
-params .. \ The program passed as argument will be executed in the new terminal' \
-command-completion \ %{
kitty-new-tab %{ nop %sh{ nop %sh{
kitty @ new-window --no-response --new-tab "$(command -v kak 2>/dev/null)" -c "${kak_session}" -e "$*" kitty @ new-window --no-response --new-tab "$@"
}} }
}
define-command -params ..1 -client-completion \ define-command kitty-focus -params ..1 -client-completion -docstring '
-docstring %{kitty-focus [<client>]: focus the given client kitty-focus [<client>]: focus the given client
If no client is passed then the current one is used} \ If no client is passed then the current one is used' \
kitty-focus %{ evaluate-commands %sh{ %{
evaluate-commands %sh{
if [ $# -eq 1 ]; then if [ $# -eq 1 ]; then
printf %s\\n "evaluate-commands -client '$1' focus" printf "evaluate-commands -client '%s' focus" "$1"
else else
kitty @ focus-tab --no-response -m=id:$kak_client_env_KITTY_WINDOW_ID kitty @ focus-tab --no-response -m=id:$kak_client_env_KITTY_WINDOW_ID
kitty @ focus-window --no-response -m=id:$kak_client_env_KITTY_WINDOW_ID kitty @ focus-window --no-response -m=id:$kak_client_env_KITTY_WINDOW_ID
fi fi
}} }
}
define-command -docstring %{kitty-repl [<arguments>]: create a new window for repl interaction define-command kitty-repl -params .. -shell-completion -docstring '
All optional parameters are forwarded to the new window} \ kitty-repl [<arguments>]: create a new window for repl interaction
-params .. \ All optional parameters are forwarded to the new window' \
-shell-completion \ %{
kitty-repl %{ evaluate-commands %sh{ nop %sh{
if [ $# -eq 0 ]; then cmd="${SHELL:-/bin/sh}"; else cmd="$*"; fi if [ $# -eq 0 ]; then
cmd="${SHELL:-/bin/sh}"
else
cmd="$*"
fi
kitty @ new-window --no-response --window-type $kak_opt_kitty_window_type --title kak_repl_window --cwd "$PWD" $cmd < /dev/null > /dev/null 2>&1 & kitty @ new-window --no-response --window-type $kak_opt_kitty_window_type --title kak_repl_window --cwd "$PWD" $cmd < /dev/null > /dev/null 2>&1 &
}} }
}
define-command kitty-send-text -docstring "send the selected text to the repl window" %{ define-command kitty-send-text -docstring "send the selected text to the repl window" %{
nop %sh{ nop %sh{