From 678acd32bccea4c37142931b6bf17a20343c1c68 Mon Sep 17 00:00:00 2001 From: Frank LENORMAND Date: Fri, 6 Nov 2015 10:13:19 +0300 Subject: [PATCH 1/3] Introduce `rc/tmux.kak` This commit adds a kak script dedicated to tmux support, and declares new command to interact with the current tmux session. Users are now able to create vertical panes or horizontal panes arbitrarily (as opposed to setting the `termcmd` variable to the proper command). The script also has its own `focus` function, used to jump from a pane to another. --- rc/tmux.kak | 71 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 rc/tmux.kak diff --git a/rc/tmux.kak b/rc/tmux.kak new file mode 100644 index 00000000..6d243ff5 --- /dev/null +++ b/rc/tmux.kak @@ -0,0 +1,71 @@ +# http://tmux.github.io/ +# ‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾ + +## Can be one of "horizontal", "vertical" or "window" +## When a new client is created, an horizontal pane, a vertical pane or a window will respectively be made +decl str tmux_default_direction "horizontal" + +## The default behaviour for the `new` command is to open an horizontal pane in a tmux session +hook global KakBegin .* %{ + %sh{ + if [ -n "$TMUX" ]; then + tmuxcmd="" + + case "${kak_opt_tmux_default_direction}" in + vertical) tmuxcmd="tmux split-window -v";; + window) tmuxcmd="tmux new-window";; + horizontal) tmuxcmd="tmux split-window -h";; + *) echo "invalid value: ${kak_opt_tmux_default_direction}" >&2;; + esac + + if [ -n "${tmuxcmd}" ]; then + echo "set global termcmd '${tmuxcmd}'" + fi + + echo "alias global focus focus-tmux" + fi + } +} + +## Temporarily override the default client creation command +def -hidden -shell-params tmux_override_termcmd %{ + %sh{ + if [ -z "$TMUX" ]; then + echo "echo -color Error This command is only available in a tmux session" + else + readonly cmd_override="$1" + readonly termcmd="${kak_opt_termcmd}" + + shift + echo " + set current termcmd '${cmd_override}' + eval new $@ + set current termcmd '${termcmd}' + " + fi + } +} + +def tmux-new-vertical -shell-params -command-completion -docstring "Create a new vertical pane in tmux" %{ + %sh{ + echo "eval %{tmux_override_termcmd 'tmux split-window -v' $@}" + } +} + +def tmux-new-horizontal -shell-params -command-completion -docstring "Create a new horizontal pane in tmux" %{ + %sh{ + echo "eval %{tmux_override_termcmd 'tmux split-window -h' $@}" + } +} + +def -docstring "focus given client" \ + -shell-params -client-completion \ + focus-tmux %{ %sh{ + if [ $# -gt 1 ]; then + echo "echo -color Error 'too many arguments, use focus [client]'" + elif [ $# -eq 1 ]; then + echo "eval -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 +} } From 284a40f681d71d41da5a1676cf688927ffd53f6d Mon Sep 17 00:00:00 2001 From: Frank LENORMAND Date: Fri, 6 Nov 2015 10:14:50 +0300 Subject: [PATCH 2/3] Update `rc/client.kak` to fit the tmux changes Trim the script to remove any reference to tmux, and reflect the changes made in the previous commit. --- rc/client.kak | 54 +++++++++++++++++++++++---------------------------- 1 file changed, 24 insertions(+), 30 deletions(-) diff --git a/rc/client.kak b/rc/client.kak index 69bdde1c..de93a0df 100644 --- a/rc/client.kak +++ b/rc/client.kak @@ -1,50 +1,44 @@ # termcmd should be set such as the next argument is the whole # command line to execute decl str termcmd %sh{ - if [ -n "$TMUX" ]; then - echo "'tmux split-window -h'" - else - for termcmd in 'termite -e ' \ - 'urxvt -e sh -c' \ - 'rxvt -e sh -c' \ - 'xterm -e sh -c' \ - 'roxterm -e sh -c' \ - 'mintty -e sh -c' \ - 'gnome-terminal -e ' \ - 'xfce4-terminal -e ' ; do - terminal=${termcmd%% *} - if which $terminal > /dev/null 2>&1; then - echo "'$termcmd'" - exit - fi - done - fi + for termcmd in 'termite -e ' \ + 'urxvt -e sh -c' \ + 'rxvt -e sh -c' \ + 'xterm -e sh -c' \ + 'roxterm -e sh -c' \ + 'mintty -e sh -c' \ + 'gnome-terminal -e ' \ + 'xfce4-terminal -e ' ; do + terminal=${termcmd%% *} + if which $terminal > /dev/null 2>&1; then + echo "'$termcmd'" + exit + fi + done } def -docstring 'create a new kak client for current session' \ -shell-params \ -command-completion \ new %{ %sh{ - if [ -z "${kak_opt_termcmd}" ]; then - echo "echo -color Error '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 & + if [ -z "${kak_opt_termcmd}" ]; then + echo "echo -color Error '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 & }} def -docstring 'focus given client' \ -shell-params -client-completion \ - focus %{ %sh{ + focus-default %{ %sh{ if [ $# -gt 1 ]; then echo "echo -color Error 'too many arguments, use focus [client]'" elif [ $# -eq 1 ]; then echo "eval -client '$1' focus" else - if [ -n "$kak_client_env_TMUX" ]; then - TMUX="$kak_client_env_TMUX" tmux select-pane -t "$kak_client_env_TMUX_PANE" > /dev/null - else - xdotool windowactivate $kak_client_env_WINDOWID > /dev/null - fi + xdotool windowactivate $kak_client_env_WINDOWID > /dev/null fi } } +## Create an alias to the default focus callback to allow overriding from custom scripts (i.e. tmux) +alias global focus focus-default From 15e24409009b88db9e13196777ba21fac4052633 Mon Sep 17 00:00:00 2001 From: Frank LENORMAND Date: Tue, 17 Nov 2015 11:18:19 +0300 Subject: [PATCH 3/3] Escape quotes in the restored `termcmd` variable --- rc/tmux.kak | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rc/tmux.kak b/rc/tmux.kak index 6d243ff5..9de65754 100644 --- a/rc/tmux.kak +++ b/rc/tmux.kak @@ -40,7 +40,7 @@ def -hidden -shell-params tmux_override_termcmd %{ echo " set current termcmd '${cmd_override}' eval new $@ - set current termcmd '${termcmd}' + set current termcmd '${termcmd//'/\\'}' " fi }