1
0
Fork 0
dotfiles/.local/bin/kak-e

85 lines
2.2 KiB
Bash
Executable File

#!/bin/bash
if [[ "$1" == "--help" ]] ; then
cat << EOF
Usage: kak-e <<options>> <<files>>
Open files in an existing kakoune session or a new window for the given context, much like tabbed graphical editors like kate or gedit
OPTIONS
--new-window spawns a new window
--file <file> [+<line>[:<column>]] escapes a file
ENVOIRENMENT
SLATECTX_CONTEXT_NAME - context name, uses slatectx-get-context-name to generate default
XDG_RUNTIME_DIR - used for inferring the kakoune socket name
TERMINAL - will be called as $TERMINAL -e <<command>> for starting kakoune, defaults to foot
EOF
exit
fi
[ -z "$SLATECTX_CONTEXT_NAME" ] && SLATECTX_CONTEXT_NAME="$(slatectx-get-context-name)"
[ -z "$XDG_RUNTIME_DIR" ] && XDG_RUNTIME_DIR="/run/user/$(id -u)"
[ -z "$TERMINAL" ] && TERMINAL="foot"
# Clean up kakoune sessions to avoid running into dead sockets
kak -clear
new_window_launched=""
# Launch kakoune in a terminal in case it isn't already running
if ! [[ -e "$XDG_RUNTIME_DIR/kakoune/$SLATECTX_CONTEXT_NAME" ]] ; then
#The -e here is ugly but $TERMINAL and the xterm -e conventions are the only "standardized" way to launch a preferred terminal I'm aware of.
"$TERMINAL" -e kak -s "$SLATECTX_CONTEXT_NAME" &
new_window_launched="true"
sleep .5
fi
function edit() {
line="${2:-0}"
column="${3:-0}"
printf 'evaluate-commands -client "%%opt{current_client}" %%\0edit %%\0\0%s\0\0 %d %d\0' "$(realpath "$1")" "$line" "$column" | kak -p "$SLATECTX_CONTEXT_NAME"
}
staged_file=""
while [[ "$#" -gt 0 ]]; do
case "$1" in
--new-window)
if [[ -n "$new_window_launched" ]] ; then
new_window_launched=""
else
"$TERMINAL" -e kak -c "$SLATECTX_CONTEXT_NAME" &
fi
shift 1;;
--file)
staged_file="$2"
shift 2 ;;
+*)
if [[ -n "$staged_file" ]] ; then
line="$(lua - "$1" <<EOF
local i = ...
local line = (i or "+0"):match("^%+(%d*):?") or "0"
print(line)
EOF
)"
column="$(lua - "$1" <<EOF
local i = ...
local column = (i or "+0:0"):match("^%+%d*:(%d*)") or "0"
print(column)
EOF
)"
edit "$staged_file" "$line" "$column"
staged_file=""
fi
shift 1 ;;
*)
[[ -n "$staged_file" ]] && edit "$staged_file"
staged_file="$1"
shift 1 ;;
esac
done
[[ -n "$staged_file" ]] && edit "$staged_file" || true