diff options
| author | Alex Leferry 2 <alexherbo2@gmail.com> | 2020-06-22 00:26:40 +0200 |
|---|---|---|
| committer | Alex Leferry 2 <alexherbo2@gmail.com> | 2020-06-22 00:26:40 +0200 |
| commit | 52e8a0db24ffde813a762be05fee1f3879f7997a (patch) | |
| tree | fac0f6ca9f708706c9510e08bd44dbf2181ea9b7 | |
| parent | b42e0713ab94d85c2e6a1cbba58204ecccd2dc80 (diff) | |
Refactor the way to connect to the same terminal window
| -rwxr-xr-x | bin/kak-connect | 21 | ||||
| -rw-r--r-- | rc/connect.kak | 15 | ||||
| -rw-r--r-- | rc/env/kakoune.env | 4 | ||||
| -rwxr-xr-x | rc/paths/commands/attach | 17 | ||||
| -rwxr-xr-x | rc/paths/commands/edit | 10 | ||||
| -rwxr-xr-x | rc/paths/commands/get | 4 |
6 files changed, 42 insertions, 29 deletions
diff --git a/bin/kak-connect b/bin/kak-connect index 41b589f..9612ee3 100755 --- a/bin/kak-connect +++ b/bin/kak-connect @@ -3,7 +3,7 @@ # Environment variables ──────────────────────────────────────────────────────── XDG_DATA_HOME=${XDG_DATA_HOME:-~/.local/share} -CONNECT_SCRIPT_PATH=$XDG_DATA_HOME/kak/connect/script.sh +KAKOUNE_CONNECT_SCRIPT=$XDG_DATA_HOME/kak/connect/script.sh # Main ───────────────────────────────────────────────────────────────────────── @@ -14,7 +14,7 @@ main() { edit "$@" # Try to connect to an existing session from a previous :connect-detach command. - elif test -t 1 -a -e "$CONNECT_SCRIPT_PATH"; then + elif test -t 1 -a -e "$KAKOUNE_CONNECT_SCRIPT"; then attach # Create a session and attach if there is a terminal. @@ -40,19 +40,12 @@ EOF # connect-detach allows to run CLI commands in the same terminal window. kak -c "$kak_session" -e " alias global terminal connect-detach - set-option global connect_environment %{ - # Unset KAKOUNE_CLIENT when attached to a terminal. - # Motivation: GUI apps can connect to the client. - # Example: :connect-shell dolphin - if test -t 1; then - KAKOUNE_CLIENT='' - fi - } + set-option global connect_attach yes connect-terminal $@ " # Attach the connect terminal command. # Act as a “boot loader”. - if test -e "$CONNECT_SCRIPT_PATH"; then + if test -e "$KAKOUNE_CONNECT_SCRIPT"; then attach fi @@ -71,9 +64,9 @@ EOF # Functions ──────────────────────────────────────────────────────────────────── attach() { - mv "$CONNECT_SCRIPT_PATH" "$CONNECT_SCRIPT_PATH~" - sh "$CONNECT_SCRIPT_PATH~" - rm -f "$CONNECT_SCRIPT_PATH~" + mv "$KAKOUNE_CONNECT_SCRIPT" "$KAKOUNE_CONNECT_SCRIPT~" + sh "$KAKOUNE_CONNECT_SCRIPT~" + rm -f "$KAKOUNE_CONNECT_SCRIPT~" } main "$@" diff --git a/rc/connect.kak b/rc/connect.kak index c551675..49de712 100644 --- a/rc/connect.kak +++ b/rc/connect.kak @@ -2,6 +2,7 @@ declare-option -hidden str connect_path %sh(dirname "$kak_source") declare-option str connect_environment provide-module connect %{ + declare-option -docstring 'Attach to terminal' bool connect_attach no declare-option -docstring 'Path to connect data' str connect_data_path %sh{ # Environment variables XDG_DATA_HOME=${XDG_DATA_HOME:-~/.local/share} @@ -10,19 +11,19 @@ provide-module connect %{ } define-command connect-terminal -params .. -shell-completion -docstring 'Connect a terminal' %{ terminal sh -c %{ - kak_opt_prelude=$1 kak_opt_connect_path=$2 kak_opt_connect_environment=$3 kak_session=$4 kak_client=$5 kak_client_env_SHELL=$6 + kak_opt_prelude=$1 kak_opt_connect_path=$2 kak_opt_connect_data_path=$3 kak_opt_connect_attach=$4 kak_opt_connect_environment=$5 kak_session=$6 kak_client=$7 kak_client_env_SHELL=$8 . "$kak_opt_connect_path/env/default.env" . "$kak_opt_connect_path/env/overrides.env" . "$kak_opt_connect_path/env/kakoune.env" . "$kak_opt_connect_path/env/git.env" eval "$kak_opt_connect_environment" - shift 6 + shift 8 "${@:-$SHELL}" - } -- %opt{prelude} %opt{connect_path} %opt{connect_environment} %val{session} %val{client} %val{client_env_SHELL} %arg{@} + } -- %opt{prelude} %opt{connect_path} %opt{connect_data_path} %opt{connect_attach} %opt{connect_environment} %val{session} %val{client} %val{client_env_SHELL} %arg{@} } define-command connect-shell -params 1.. -shell-completion -docstring 'Connect a shell' %{ nop %sh{ - # kak_opt_prelude kak_opt_connect_path kak_opt_connect_environment kak_session kak_client kak_client_env_SHELL + # kak_opt_prelude kak_opt_connect_path kak_opt_connect_data_path kak_opt_connect_attach kak_opt_connect_environment kak_session kak_client kak_client_env_SHELL . "$kak_opt_connect_path/env/default.env" . "$kak_opt_connect_path/env/overrides.env" . "$kak_opt_connect_path/env/kakoune.env" @@ -36,6 +37,12 @@ provide-module connect %{ mkdir -p "$kak_opt_connect_data_path" } echo -to-file "%opt{connect_data_path}/script.sh" -quoting shell %arg{@} + # Delete the script + hook -once global ClientCreate %val{client} %{ + nop %sh{ + rm -f "$kak_opt_connect_data_path/script.sh~" + } + } quit! } alias global t connect-terminal diff --git a/rc/env/kakoune.env b/rc/env/kakoune.env index 9e94893..e077e60 100644 --- a/rc/env/kakoune.env +++ b/rc/env/kakoune.env @@ -1,3 +1,7 @@ export KAKOUNE_SESSION=$kak_session export KAKOUNE_CLIENT=$kak_client export KAKOUNE_PRELUDE=$kak_opt_prelude +export KAKOUNE_CONNECT=$kak_opt_connect_path +export KAKOUNE_CONNECT_DATA=$kak_opt_connect_data_path +export KAKOUNE_CONNECT_SCRIPT=$kak_opt_connect_data_path/script.sh +export KAKOUNE_CONNECT_ATTACH=$kak_opt_connect_attach diff --git a/rc/paths/commands/attach b/rc/paths/commands/attach index d95b786..12df22c 100755 --- a/rc/paths/commands/attach +++ b/rc/paths/commands/attach @@ -1,9 +1,14 @@ #!/bin/sh -script_path=$XDG_DATA_HOME/kak/connect/script.sh - -if test -e "$script_path"; then - mv "$script_path" "$script_path~" - sh "$script_path~" - rm -f "$script_path~" +if test -e "$KAKOUNE_CONNECT_SCRIPT"; then + mv "$KAKOUNE_CONNECT_SCRIPT" "$KAKOUNE_CONNECT_SCRIPT~" + sh "$KAKOUNE_CONNECT_SCRIPT~" + # Note: + # This script is deleted by :connect-detach when reattaching to the session. + # If the script still exists, sh <connect-script> did not reattach. + # Mitigate the need to manually reattach. + if test -e "$KAKOUNE_CONNECT_SCRIPT~"; then + rm -f "$KAKOUNE_CONNECT_SCRIPT~" + edit + fi fi diff --git a/rc/paths/commands/edit b/rc/paths/commands/edit index ad6243d..ee39c19 100755 --- a/rc/paths/commands/edit +++ b/rc/paths/commands/edit @@ -40,7 +40,15 @@ commands="$commands; focus" regex=${regex#|} kak_quoted_regex=$(kak_escape "$regex") -send "$commands" +# Attach to terminal. +# Note: Ensure there is a terminal. +# Motivation: GUI apps can connect to the client. +# Example: :connect-shell dolphin +if test -t 1 -a "$KAKOUNE_CONNECT_ATTACH" = true; then + kak -c "$KAKOUNE_SESSION" -e "$commands" +else + send "$commands" +fi if test "$wait" = true; then state=$(mktemp -d) diff --git a/rc/paths/commands/get b/rc/paths/commands/get index 7bb6b0a..8089991 100755 --- a/rc/paths/commands/get +++ b/rc/paths/commands/get @@ -1,9 +1,5 @@ #!/bin/sh -# Environment variables -# Set a default client value -KAKOUNE_CLIENT=${KAKOUNE_CLIENT:-client0} - state=$(mktemp -d) trap 'rm -Rf "$state"' EXIT mkfifo "$state/fifo" |
