1
1
mirror of https://github.com/mawww/kakoune.git synced 2024-12-25 12:36:11 +03:00

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
echo "
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" %{
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" %{
define-command screen-terminal-impl -hidden -params 3.. %{
nop %sh{
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
If no client is passed then the current one is used} \
-params ..1 -client-completion \
screen-focus %{ evaluate-commands %sh{
define-command screen-terminal-vertical -params 1.. -shell-completion -docstring '
screen-terminal-vertical <program> [<arguments>] [<arguments>]: create a new terminal as a screen pane
The current pane is split into two, left and right
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
printf %s\\n "
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)"
screen -X select "${kak_client_env_WINDOW}" < "/dev/$tty"
fi
} }
}
}

View File

@ -6,45 +6,56 @@ hook global KakBegin .* %sh{
if [ -n "$TMUX" ]; then
echo "
alias global focus tmux-focus
alias global new tmux-new-horizontal
alias global terminal tmux-terminal-horizontal
"
fi
}
## Temporarily override the default client creation command
define-command -hidden -params 1.. tmux-new-impl %{
define-command -hidden -params 2.. tmux-terminal-impl %{
evaluate-commands %sh{
tmux=${kak_client_env_TMUX:-$TMUX}
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
fi
tmux_args="$1"
shift
if [ $# -ne 0 ]; then kakoune_params="-e '$@'"; fi
TMUX=$tmux tmux $tmux_args "env TMPDIR='${TMPDIR}' kak -c ${kak_session} ${kakoune_params}" < /dev/null > /dev/null 2>&1 &
# ideally we should escape single ';' to stop tmux from interpreting it as a new command
# 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" %{
tmux-new-impl 'split-window -v' %arg{@}
define-command tmux-terminal-vertical -params 1.. -shell-completion -docstring '
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" %{
tmux-new-impl 'split-window -h' %arg{@}
define-command tmux-focus -params ..1 -client-completion -docstring '
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
}
define-command -docstring %{x11-new [<command>]: create a new kak client for the current session
The optional arguments will be passed as arguments to the new client} \
-params .. \
-command-completion \
x11-new %{ evaluate-commands %sh{
define-command x11-terminal -params 1.. -shell-completion -docstring '
x11-terminal <program> [<arguments>]: create a new terminal as an x11 window
The program passed as argument will be executed in the new terminal' \
%{
evaluate-commands %sh{
if [ -z "${kak_opt_termcmd}" ]; then
echo "echo -markup '{Error}termcmd option is not set'"
echo "fail 'termcmd option is not set'"
exit
fi
if [ $# -ne 0 ]; then kakoune_params="-e '$@'"; fi
setsid ${kak_opt_termcmd} "kak -c ${kak_session} ${kakoune_params}" < /dev/null > /dev/null 2>&1 &
}}
# join arguments into a single string, in which they're delimited
# 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
If no client is passed, then the current client is used} \
-params ..1 -client-completion \
x11-focus %{ evaluate-commands %sh{
define-command x11-focus -params ..1 -client-completion -docstring '
x11-focus [<kakoune_client>]: focus a given client''s window
If no client is passed, then the current client is used' \
%{
evaluate-commands %sh{
if [ $# -eq 1 ]; then
printf %s\\n "evaluate-commands -client '$1' focus"
printf "evaluate-commands -client '%s' focus" "$1"
else
xdotool windowactivate $kak_client_env_WINDOWID > /dev/null
fi
} }
}
}
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{
if [ "$TERM_PROGRAM" = "iTerm.app" ] && [ -z "$TMUX" ]; then
echo "
alias global new iterm-new-vertical
alias global focus iterm-focus
"
fi
}
define-command -hidden -params 1.. iterm-new-split-impl %{
define-command -hidden -params 2.. iterm-terminal-split-impl %{
nop %sh{
direction="$1"
shift
if [ $# -gt 0 ]; then kakoune_params="-e \\\"$*\\\""; fi
cmd="env PATH='${PATH}' TMPDIR='${TMPDIR}' kak -c '${kak_session}' ${kakoune_params}"
# 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
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 \
-e "tell application \"iTerm\"" \
-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" %{
iterm-new-split-impl 'vertically' %arg{@}
define-command iterm-terminal-vertical -params 1.. -shell-completion -docstring '
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" %{
iterm-new-split-impl 'horizontally' %arg{@}
}
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 %{
define-command iterm-terminal-tab -params 1.. -shell-completion -docstring '
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'\
%{
nop %sh{
if [ $# -gt 0 ]; then kakoune_params="-e \\\"$*\\\""; fi
cmd="env PATH='${PATH}' TMPDIR='${TMPDIR}' kak -c '${kak_session}' ${kakoune_params}"
# see above
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 \
-e "tell application \"iTerm\"" \
-e " tell current window" \
@ -51,13 +80,23 @@ All optional arguments are forwarded to the new kak client} \
}
}
define-command -params .. -command-completion \
-docstring %{iterm-new-window [<arguments>]: create a new window
All optional arguments are forwarded to the new kak client} \
iterm-new-window %{
define-command iterm-terminal-window -params 1.. -shell-completion -docstring '
iterm-terminal-window <program> [<arguments>]: create a new terminal as an iterm window
The program passed as argument will be executed in the new terminal'\
%{
nop %sh{
if [ $# -gt 0 ]; then kakoune_params="-e \\\"$*\\\""; fi
cmd="env PATH='${PATH}' TMPDIR='${TMPDIR}' kak -c '${kak_session}' ${kakoune_params}"
# see above
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 \
-e "tell application \"iTerm\"" \
-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 \
-docstring %{iterm-focus [<client>]: focus the given client
If no client is passed then the current one is used} \
iterm-focus %{ evaluate-commands %sh{
define-command iterm-focus -params ..1 -client-completion -docstring '
iterm-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 %s\\n "evaluate-commands -client '$1' focus"
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{
if [ "$TERM" = "xterm-kitty" ] && [ -z "$TMUX" ]; then
echo "
alias global new kitty-new
alias global new-tab kitty-new-tab
alias global terminal kitty-terminal
alias global terminal-tab kitty-terminal-tab
alias global focus kitty-focus
alias global repl kitty-repl
alias global send-text kitty-send-text
@ -12,42 +12,51 @@ hook -group kitty-hooks global KakBegin .* %sh{
fi
}
define-command -docstring %{kitty-new [<command>]: create a new kak client for the current session
Optional arguments are passed as arguments to the new client} \
-params .. \
-command-completion \
kitty-new %{ nop %sh{
kitty @ new-window --no-response --window-type $kak_opt_kitty_window_type "$(command -v kak 2>/dev/null)" -c "${kak_session}" -e "$*"
}}
define-command kitty-terminal -params 1.. -shell-completion -docstring '
kitty-terminal <program> [<arguments>]: create a new terminal as a kitty window
The program passed as argument will be executed in the new terminal' \
%{
nop %sh{
kitty @ new-window --no-response --window-type $kak_opt_kitty_window_type "$@"
}
}
define-command -docstring %{kitty-new-tab [<arguments>]: create a new tab
All optional arguments are forwarded to the new kak client} \
-params .. \
-command-completion \
kitty-new-tab %{ nop %sh{
kitty @ new-window --no-response --new-tab "$(command -v kak 2>/dev/null)" -c "${kak_session}" -e "$*"
}}
define-command kitty-terminal-tab -params 1.. -shell-completion -docstring '
kitty-terminal-tab <program> [<arguments>]: create a new terminal as kitty tab
The program passed as argument will be executed in the new terminal' \
%{
nop %sh{
kitty @ new-window --no-response --new-tab "$@"
}
}
define-command -params ..1 -client-completion \
-docstring %{kitty-focus [<client>]: focus the given client
If no client is passed then the current one is used} \
kitty-focus %{ evaluate-commands %sh{
define-command kitty-focus -params ..1 -client-completion -docstring '
kitty-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 %s\\n "evaluate-commands -client '$1' focus"
printf "evaluate-commands -client '%s' focus" "$1"
else
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
fi
}}
}
}
define-command -docstring %{kitty-repl [<arguments>]: create a new window for repl interaction
All optional parameters are forwarded to the new window} \
-params .. \
-shell-completion \
kitty-repl %{ evaluate-commands %sh{
if [ $# -eq 0 ]; then cmd="${SHELL:-/bin/sh}"; else cmd="$*"; fi
define-command kitty-repl -params .. -shell-completion -docstring '
kitty-repl [<arguments>]: create a new window for repl interaction
All optional parameters are forwarded to the new window' \
%{
nop %sh{
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 &
}}
}
}
define-command kitty-send-text -docstring "send the selected text to the repl window" %{
nop %sh{