Alternative tmux-terminal-impl with popup support

I’m sharing my tmux-window user mode setup. It overrides default tmux-terminal-impl in order to have popup support and passes some critical env vars which original implementation fails to do.

hook global ModuleLoaded tmux %{
    define-command -override -hidden -params 2.. tmux-terminal-impl %{
        evaluate-commands %sh{
            editor="${kak_client_env_EDITOR:-kak}"
            git_editor="${kak_client_env_GIT_EDITOR:-$editor}"
            target_pane="$kak_client_env_TMUX_PANE"
            tmux_cmd="$1"
            shift
            case "${tmux_cmd%% *}" in
                display-popup)
                    # -e switch is not yet supported here
                    # https://github.com/tmux/tmux/issues/2737#issuecomment-860628080
                    tmux $tmux_cmd -t "$target_pane" -d "$PWD" "$@" < /dev/null > /dev/null 2>&1 &
                ;;
                split-window)
                    tmux $tmux_cmd -t "$target_pane" -c "$PWD" -e PWD="$PWD" -e EDITOR="$editor" -e GIT_EDITOR="$git_editor" "$@" < /dev/null > /dev/null 2>&1 &
                ;;
                *)
                    tmux $tmux_cmd -c "$PWD" -e PWD="$PWD" -e EDITOR="$editor" -e GIT_EDITOR="$git_editor" "$@" < /dev/null > /dev/null 2>&1 &
                ;;
            esac
        }
    }

    define-command -hidden tmux-terminal-vertical-f -params 1.. \
    %{
        tmux-terminal-impl 'split-window -v -f' %arg{@}
    }

    define-command -hidden tmux-terminal-horizontal-f -params 1.. \
    %{
        tmux-terminal-impl 'split-window -h -f' %arg{@}
    }

    define-command -hidden tmux-terminal-window-a -params 1.. \
    %{
        tmux-terminal-impl 'new-window -a' %arg{@}
    }

    # https://github.com/mawww/kakoune/issues/3878
    define-command tmux-terminal-popup -params 1.. -shell-completion -docstring '
    tmux-terminal-popup <program> [<arguments>]: create a new terminal as a tmux popup
    The program passed as argument will be executed in the new popup' \
    %{
        tmux-terminal-impl 'display-popup -E -w 60% -h 75%' %arg{@}
    }

    # popup used by connect module
    alias global popup tmux-terminal-popup

    # if you're like me don't like polluting system clipboard
    hook global RegisterModified '"' %{
        nop %sh{ tmux setb -b kak -- "$kak_main_reg_dquote" }
    }
}

declare-user-mode tmux-window
map global tmux-window h -docstring 'split -h' \
': alias buffer terminal tmux-terminal-horizontal; new unalias buffer terminal<ret>'
map global tmux-window H -docstring 'split -h -f' \
': alias buffer terminal tmux-terminal-horizontal-f; new unalias buffer terminal<ret>'
map global tmux-window v -docstring 'split -v' \
': alias buffer terminal tmux-terminal-vertical; new unalias buffer terminal<ret>'
map global tmux-window V -docstring 'split -v -f' \
': alias buffer terminal tmux-terminal-vertical-f; new unalias buffer terminal<ret>'
map global tmux-window t -docstring 'new tab after' \
': alias buffer terminal tmux-terminal-window-a; new unalias buffer terminal<ret>'
map global tmux-window T -docstring 'new tab at end' \
': alias buffer terminal tmux-terminal-window; new unalias buffer terminal<ret>'
map global tmux-window w %{: nop %sh{tmux last-pane -Z}<ret>} -docstring 'Last window'
map global tmux-window o %{: nop %sh{tmux kill-pane -a}<ret>} -docstring 'Only window'
map global tmux-window r %{: nop %sh{tmux resize-mode}<ret>} -docstring 'Resize mode'
map global tmux-window z %{: nop %sh{tmux resize-pane -Z}<ret>} -docstring 'Zoom window'

For completeness here is resize-mode in my tmux.conf:

# resize mode
bind-key -Tresize F1 lsk -Tresize
bind-key -Tresize h resize-pane -L 2
bind-key -Tresize l resize-pane -R 2
bind-key -Tresize j resize-pane -D 2
bind-key -Tresize k resize-pane -U 2
bind-key -Tresize e select-layout -E \; set -u key-table \; set -u prefix
bind-key -Tresize Any set -u key-table \; set -u prefix
set-option -s command-alias[101] resize-mode='set prefix None ; set key-table resize ; if -F "#{pane_in_mode}" "send-keys -X cancel"'

# mode indicator
user_key_table="#[fg=colour255,bg=magenta,bold]#{?#{&&:#{!=:#{client_key_table},root},#{!=:#{client_key_table},prefix}}, #{client_key_table} ,}#[default]"
set-option -g status-right "${user_key_table}"

This looks really nice, have you considered opening a PR with these changes?

I have switched to GitHub - alexherbo2/tmux.kak: tmux integration for Kakoune

I think alexherbo2’s approach is much cleaner than this one.