# https://sw.kovidgoyal.net/kitty
# The following assumes the "splits" layout.

hook global ModuleLoaded kitty %{
    define-command kitty-launch-impl -params 3.. %{
        nop %sh{
            location=$1
            type=$2
            shift 2

            match=""
            if [ -n "$kak_client_env_KITTY_WINDOW_ID" ]; then
                match="--match=window_id:$kak_client_env_KITTY_WINDOW_ID"
            fi

            listen=""
            if [ -n "$kak_client_env_KITTY_LISTEN_ON" ]; then
                listen="--to=$kak_client_env_KITTY_LISTEN_ON"
            fi

            kitty @ $listen launch --no-response --location=$location --type=$type --cwd="$PWD" $match "$@"
        }
    } -hidden

    define-command kitty-terminal-horizontal -params 1.. %{
        kitty-launch-impl "vsplit" "window" %arg{@}
    }
    complete-command kitty-terminal-horizontal shell

    define-command kitty-terminal-vertical -params 1.. %{
        kitty-launch-impl "hsplit" "window" %arg{@}
    }
    complete-command kitty-terminal-vertical shell

    define-command kitty-terminal-overlay -params 1.. %{
        kitty-launch-impl "default" "overlay" %arg{@}
    }
    complete-command kitty-terminal-overlay shell
}
