AutoYADM commit: 2025-05-17 15:55:24

This commit is contained in:
Daniel Fichtinger 2025-05-17 15:55:24 -04:00
parent 8535769aa8
commit 318cbd9392
3 changed files with 128 additions and 14 deletions

View file

@ -1,19 +1,51 @@
function kak-session
function _kak_connect_or_create
set -l session_name $argv[1]
set argv $argv[2..]
set -l socket_paths \
"$XDG_RUNTIME_DIR/kakoune/$session_name" \
"{TMPDIR}/kakoune-$USER/$session_name"
function kak -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
# we treat the first file as the target
set -l targ $argv[1]
set -l root ""
set -l gitdir (env -C (path dirname $targ) git rev-parse --show-toplevel)
set session_name (string replace -r '[^a-zA-A0-9_-]' '_' (path basename $gitdir))
end
# handle file:line syntax, TODO later
# 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
_kak_connect_or_create $session_name $argv
# 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

View file

@ -0,0 +1,51 @@
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

View file

@ -0,0 +1,31 @@
#!/bin/sh
root="$(
set -e
gitdir=$(git rev-parse --git-dir 2>/dev/null)
set +e
cd "$gitdir"/.. # This is horrible.
git rev-parse --show-toplevel
)"
session_name="$(printf %s ${root##*/} | sed s/[^a-zA-Z0-9_-]/_/g)"
if printf %s "$1" | grep -q '^[^+-][^:]\+:[0-9]\+'; then
file=${1%:*}
pos=${1#*:}
shift
set -- "+$pos" "$file" "$@"
fi
# "kak -l" hangs occasionally, so check the socket directly
if [ -n "$session_name" ] && {
[ -e "${XDG_RUNTIME_DIR}/kakoune/$session_name" ] ||
[ -e "${TMPDIR:-/tmp}/kakoune-$USER/$session_name" ]
} ; then
# Force-reload if we get a file argument (useful for .git/COMMIT_EDITMSG).
if [ $# -gt 0 ] && ! printf %s\\n "$@" | grep -q ^-e; then
set -- -e 'try edit!' "$@"
fi
exec kak -c $session_name "$@"
else
exec kak -s "$session_name" "$@"
fi