51 lines
1.6 KiB
Fish
51 lines
1.6 KiB
Fish
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
|
|
|
|
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
|
|
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
|
|
|
|
# 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"__"$branch" | 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
|