From a2f513b31d30ef16afbdd65cc4d06426429f8966 Mon Sep 17 00:00:00 2001 From: Daniel Fichtinger Date: Mon, 19 May 2025 17:57:38 -0400 Subject: [PATCH] AutoYADM commit: 2025-05-19 17:57:38 --- .config/fish/functions/kak-session.fish | 127 +++++++++++++++--------- 1 file changed, 82 insertions(+), 45 deletions(-) diff --git a/.config/fish/functions/kak-session.fish b/.config/fish/functions/kak-session.fish index f608e595..6a9881c1 100644 --- a/.config/fish/functions/kak-session.fish +++ b/.config/fish/functions/kak-session.fish @@ -1,51 +1,88 @@ function kak-session -w kak --description "kakoune where sessions are derived from parent git dirs" - # TODO(enricozb): https://github.com/enricozb/tabs.kak/issues/12 + # TODO(enricozb): https://github.com/enricozb/tabs.kak/issues/12 - set files # all arguments without a `-` prefix - set flags # all arguments with a `-` prefix + set files # all arguments without a `-` prefix + set flags # all arguments with a `-` prefix - for arg in $argv - if string match --quiet --regex '^-' -- $arg - set --append flags $arg + for arg in $argv + if string match --quiet --regex '^-' -- $arg + set --append flags $arg + else + set --append files $arg + end + end + + # set git_dir if there is at least one file + if set --query files[1] + set git_dir (git -C (dirname $files[1]) rev-parse --show-toplevel 2>/dev/null) + end + + function fdup + set target $argv[1] + set dir (path dirname (pwd)) + if test -n "$argv[2]" + set dir (path dirname "$argv[2]") + end + while test "$dir" != / + set result (fd --max-depth 1 -H -I -t f "$target" "$dir") + if test -n "$result" + echo (path dirname $result) + return 0 + end + set dir (path dirname $dir) + end + + return 1 + end + + set kakroot (fdup .kakroot $files[1]) + + if test -n "$kakroot" + set session_id (echo -n "$(path basename "$kakroot")" | tr --complement '[:alpha:]_-' '-') + # if there is no existing session + if not command kak -l | rg -xqF "$session_id" + set fifo_dir (mktemp -d --suffix=kak-init.XXXXXX) + set fifo "$fifo_dir/fifo" + mkfifo "$fifo" + + setsid kak -d -s "$session_id" -E "cd %[$kakroot]; echo -to-file $fifo ready" & + + # once $fifo has data, the kakoune server has initialized. + cat $fifo >/dev/null + + command rm -r "$fifo_dir" + end + + command kak -c "$session_id" $flags $files + else if [ -n "$git_dir" ] + # when in a git directory, share sessions + # the shared session id is {repo}__{branch} where repo is the remote repo name + # or the basename of the git directory. + if set remote (git -C $git_dir remote get-url origin 2>/dev/null) + set repo (basename -s .git $remote) + else + set repo (basename $git_dir) + end + set branch (git -C $git_dir symbolic-ref --short HEAD) + + set session_id (echo -n "$repo" | tr --complement '[:alpha:]_-' '-') + + # if there is no existing session + if not command kak -l | rg -xqF "$session_id" + set fifo_dir (mktemp -d --suffix=kak-init.XXXXXX) + set fifo "$fifo_dir/fifo" + mkfifo "$fifo" + + setsid kak -d -s "$session_id" -E "cd %[$git_dir]; echo -to-file $fifo ready" & + + # once $fifo has data, the kakoune server has initialized. + cat $fifo >/dev/null + + command rm -r "$fifo_dir" + end + + command kak -c "$session_id" $flags $files else - set --append files $arg + command kak $flags $files end - end - - # set git_dir if there is at least one file - if set --query files[1] - set git_dir (git -C (dirname $files[1]) rev-parse --show-toplevel 2>/dev/null) - end - - # when in a git directory, share sessions - if [ -n "$git_dir" ] - # the shared session id is {repo}__{branch} where repo is the remote repo name - # or the basename of the git directory. - if set remote (git -C $git_dir remote get-url origin 2>/dev/null) - set repo (basename -s .git $remote) - else - set repo (basename $git_dir) - end - set branch (git -C $git_dir symbolic-ref --short HEAD) - - set session_id (echo -n "$repo" | tr --complement '[:alpha:]_-' '-') - - # if there is no existing session - if not command kak -l | rg -xqF "$session_id" - set fifo_dir (mktemp -d --suffix=kak-init.XXXXXX) - set fifo "$fifo_dir/fifo" - mkfifo "$fifo" - - setsid kak -d -s "$session_id" -E "cd %[$git_dir]; echo -to-file $fifo ready" & - - # once $fifo has data, the kakoune server has initialized. - cat $fifo >/dev/null - - command rm -r "$fifo_dir" - end - - command kak -c "$session_id" $flags $files - else - command kak $flags $files - end end