From 79db90349d89e432a100776586a1d73ee1ef4555 Mon Sep 17 00:00:00 2001 From: Johannes Altmanninger Date: Sun, 30 Oct 2022 18:52:02 +0100 Subject: [PATCH 1/5] rc repl tmux: show error when the repl pane is gone tmux-send-text would silently fail when the repl is no more. Let's instead print an error, pointing the user to the *debug* buffer which has tmux' stderr. --- rc/windowing/repl/tmux.kak | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/rc/windowing/repl/tmux.kak b/rc/windowing/repl/tmux.kak index 3238fbe7..a0450567 100644 --- a/rc/windowing/repl/tmux.kak +++ b/rc/windowing/repl/tmux.kak @@ -71,13 +71,14 @@ define-command -hidden tmux-send-text -params 0..1 -docstring %{ tmux-send-text [text]: Send text to the REPL pane. If no text is passed, then the selection is used } %{ - nop %sh{ + evaluate-commands %sh{ if [ $# -eq 0 ]; then tmux set-buffer -b kak_selection -- "${kak_selection}" else tmux set-buffer -b kak_selection -- "$1" fi - tmux paste-buffer -b kak_selection -t "$kak_opt_tmux_repl_id" + tmux paste-buffer -b kak_selection -t "$kak_opt_tmux_repl_id" || + echo 'fail tmux-send-text: failed to send text, see *debug* buffer for details' } } From 9891f111571bfdca10578804184b5455288bfc1b Mon Sep 17 00:00:00 2001 From: Johannes Altmanninger Date: Sun, 30 Oct 2022 21:12:17 +0100 Subject: [PATCH 2/5] rc repl tmux: remove bashisms --- rc/windowing/repl/tmux.kak | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/rc/windowing/repl/tmux.kak b/rc/windowing/repl/tmux.kak index a0450567..5c08b943 100644 --- a/rc/windowing/repl/tmux.kak +++ b/rc/windowing/repl/tmux.kak @@ -51,15 +51,13 @@ define-command -params 0..1 tmux-repl-set-pane -docstring %{ fi if [ $# -eq 0 ]; then curr_pane="$(tmux display-message -p '#{pane_id}')" - curr_pane_no="${curr_pane:1}" + curr_pane_no="${curr_pane#%}" tgt_pane=$((curr_pane_no+1)) else tgt_pane="$1" fi curr_win="$(tmux display-message -p '#{window_id}')" - curr_win_no="${curr_win:1}" - current=$(tmux list-panes -t $curr_win_no -F \#D) - if [[ "$current" =~ "%"$tgt_pane ]]; then + if tmux list-panes -t "$curr_win" -F \#D | grep -Fxq "%"$tgt_pane; then printf "set-option current tmux_repl_id '%s'" $(tmux display-message -p '#{session_id}:#{window_id}.')%$tgt_pane else echo 'fail The correct pane is not there. Activate using tmux-terminal-* or some other way' From 37dcba9089424f4409146a5b98961e9bfae95443 Mon Sep 17 00:00:00 2001 From: Johannes Altmanninger Date: Sun, 30 Oct 2022 19:08:50 +0100 Subject: [PATCH 3/5] rc repl tmux: do not record repl session/window since pane might move A pane's ID is immutable for the lifetime of the tmux server. Same with window/session IDs. When creating a new tmux repl, we record all three IDs to later use them to send text to the repl. The window/session IDs can be invalidating when a pane is moved to a different window/session (via "tmux move-pane", "tmux move-window" etc). This will cause repl-send-text to fail. Fix this by dropping the redundant and potentially incorrect window/session IDs. The immutable pane ID is enough. --- rc/windowing/repl/tmux.kak | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/rc/windowing/repl/tmux.kak b/rc/windowing/repl/tmux.kak index 5c08b943..885c75cf 100644 --- a/rc/windowing/repl/tmux.kak +++ b/rc/windowing/repl/tmux.kak @@ -19,7 +19,7 @@ define-command -hidden -params 1.. tmux-repl-impl %{ tmux_args="$1" shift tmux $tmux_args "$@" - printf "set-option current tmux_repl_id '%s'" $(tmux display-message -p '#{session_id}:#{window_id}.#{pane_id}') + printf "set-option current tmux_repl_id '%s'" $(tmux display-message -p '#{pane_id}') } } @@ -58,7 +58,7 @@ define-command -params 0..1 tmux-repl-set-pane -docstring %{ fi curr_win="$(tmux display-message -p '#{window_id}')" if tmux list-panes -t "$curr_win" -F \#D | grep -Fxq "%"$tgt_pane; then - printf "set-option current tmux_repl_id '%s'" $(tmux display-message -p '#{session_id}:#{window_id}.')%$tgt_pane + printf "set-option current tmux_repl_id '%s'" %$tgt_pane else echo 'fail The correct pane is not there. Activate using tmux-terminal-* or some other way' fi From cfea884cf2e80eeb0934e3b354e272d3b24093ac Mon Sep 17 00:00:00 2001 From: Johannes Altmanninger Date: Tue, 18 Oct 2022 20:02:16 +0200 Subject: [PATCH 4/5] rc windowing tmux: always spawn tmux-terminal-window in calling client The tmux-terminal-window command always spawns windows in the tmux session where the Kakoune session was started - even if the calling Kakoune client lives in a different tmux session. Fix this by always creating the window in the tmux session of the calling client. We already do the same for tmux-terminal-{horizontal,vertical}. I call tmux-terminal-impl with "new-window -a" (instead of "new-window"), so make sure the fix works for my use case. I considered retrieving the tmux session ID from the $TMUX environment variable but the tmux manpage only specifies that $TMUX contains "some internal data". --- rc/windowing/tmux.kak | 3 +++ 1 file changed, 3 insertions(+) diff --git a/rc/windowing/tmux.kak b/rc/windowing/tmux.kak index f5a8fda3..a433692c 100644 --- a/rc/windowing/tmux.kak +++ b/rc/windowing/tmux.kak @@ -18,6 +18,9 @@ define-command -hidden -params 2.. tmux-terminal-impl %{ tmux_args="$1" if [ "${1%%-*}" = split ]; then tmux_args="$tmux_args -t ${kak_client_env_TMUX_PANE}" + elif [ "${1%% *}" = new-window ]; then + session_id=$(tmux display-message -p -t ${kak_client_env_TMUX_PANE} '#{session_id}') + tmux_args="$tmux_args -t $session_id" fi shift # ideally we should escape single ';' to stop tmux from interpreting it as a new command From f69d8c3e12ef9e0030d46469481f344cc7081c0a Mon Sep 17 00:00:00 2001 From: Johannes Altmanninger Date: Sun, 30 Oct 2022 19:15:58 +0100 Subject: [PATCH 5/5] rc repl tmux: always spawn repl in calling client Just like the parent commit but for tmux-repl-* commands. Note that tmux-repl-set-pane without arguments is kind of broken; it increments the current pane ID, which only works in the most basic scenarios. We should probably replace it with something better, with menu completions etc. --- rc/windowing/repl/tmux.kak | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/rc/windowing/repl/tmux.kak b/rc/windowing/repl/tmux.kak index 885c75cf..5b776332 100644 --- a/rc/windowing/repl/tmux.kak +++ b/rc/windowing/repl/tmux.kak @@ -17,9 +17,15 @@ define-command -hidden -params 1.. tmux-repl-impl %{ exit fi tmux_args="$1" + if [ "${1%%-*}" = split ]; then + tmux_args="$tmux_args -t ${kak_client_env_TMUX_PANE}" + elif [ "${1%% *}" = new-window ]; then + session_id=$(tmux display-message -p -t ${kak_client_env_TMUX_PANE} '#{session_id}') + tmux_args="$tmux_args -t $session_id" + fi shift - tmux $tmux_args "$@" - printf "set-option current tmux_repl_id '%s'" $(tmux display-message -p '#{pane_id}') + repl_pane_id=$(tmux $tmux_args -P -F '#{pane_id}' "$@") + printf "set-option current tmux_repl_id '%s'" "$repl_pane_id" } } @@ -50,13 +56,12 @@ define-command -params 0..1 tmux-repl-set-pane -docstring %{ exit fi if [ $# -eq 0 ]; then - curr_pane="$(tmux display-message -p '#{pane_id}')" - curr_pane_no="${curr_pane#%}" + curr_pane_no="${kak_client_env_TMUX_PANE#%}" tgt_pane=$((curr_pane_no+1)) else tgt_pane="$1" fi - curr_win="$(tmux display-message -p '#{window_id}')" + curr_win="$(tmux display-message -t ${kak_client_env_TMUX_PANE} -p '#{window_id}')" if tmux list-panes -t "$curr_win" -F \#D | grep -Fxq "%"$tgt_pane; then printf "set-option current tmux_repl_id '%s'" %$tgt_pane else