From 318cbd9392c6c11b60c2b05161be562c783adbf2 Mon Sep 17 00:00:00 2001 From: Daniel Fichtinger Date: Sat, 17 May 2025 15:55:24 -0400 Subject: [PATCH] AutoYADM commit: 2025-05-17 15:55:24 --- .config/fish/functions/kak-session.fish | 60 +++++++++++++++++++------ .config/fish/kak-session.fish | 51 +++++++++++++++++++++ .config/kak/scripts/kak-session.sh | 31 +++++++++++++ 3 files changed, 128 insertions(+), 14 deletions(-) create mode 100644 .config/fish/kak-session.fish create mode 100755 .config/kak/scripts/kak-session.sh diff --git a/.config/fish/functions/kak-session.fish b/.config/fish/functions/kak-session.fish index c2fadb7d..f5c87d20 100644 --- a/.config/fish/functions/kak-session.fish +++ b/.config/fish/functions/kak-session.fish @@ -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 diff --git a/.config/fish/kak-session.fish b/.config/fish/kak-session.fish new file mode 100644 index 00000000..48d3ef28 --- /dev/null +++ b/.config/fish/kak-session.fish @@ -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 diff --git a/.config/kak/scripts/kak-session.sh b/.config/kak/scripts/kak-session.sh new file mode 100755 index 00000000..7e0362bc --- /dev/null +++ b/.config/kak/scripts/kak-session.sh @@ -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