provide-module local-kakrc %—
declare-option -docstring %{
Path to the list of trusted directories.
List must be a text file with one path per line.
Defaults to %exp{%val{config}/local_kakrc_trusted.txt}
} str local_kakrc_trusted %exp{%val{config}/local_kakrc_trusted.txt}
declare-option -docstring %{
Whether to automatically load .kakrc if it's trusted.
Otherwise, you must load manually with local-kakrc-load.
} bool local_kakrc_autoload false
# ensures that the file exists
define-command -hidden local-kakrc-ensure-file %{
nop %sh{
file="$kak_opt_local_kakrc_trusted"
dir="$(dirname "$file")"
if [ ! -f "$file" ]; then
[ ! -d "$dir" ] || mkdir -p "$dir"
base="$(basename "$file")"
# add to gitignore by default
printf '%s\n' "$base" >>"$dir/.gitignore"
touch "$file"
fi
}
}
define-command -docstring %{
Local the .kakrc if it exists and is trusted.
If it exists but isn't trusted, notify the user.
} local-kakrc-load %{
# first we check if the .kakrc exists
evaluate-commands %sh{
if [ -f .kakrc ]; then
if grep -qFx "$PWD" "$kak_opt_local_kakrc_trusted"; then
echo "source .kakrc"
else
echo "info -title 'local-kakrc' 'Local .kakrc found, but directory is untrusted.'"
fi
fi
}
}
define-command -docstring %{
local-kakrc-add-trusted [
]: Trust a directory. If none is
provided, the current directory is used.
} -params 0..1 local-kakrc-add-trusted %{
evaluate-commands %sh{
if [ "$#" -eq 0 ]; then
arg="$PWD"
else
arg="$(realpath "$1")"
fi
echo "local-kakrc-ensure-file">"$kak_command_fifo"
printf 'info -title "local-kakrc" "Adding %s to trusted"' "$arg"
printf '%s\n' "$arg">>"$kak_opt_local_kakrc_trusted"
}
}
define-command -docstring %{
local-kakrc-add-trusted []: Untrust a directory. If none is
provided, the current directory is used.
} -params 0..1 local-kakrc-rm-trusted %{
evaluate-commands %sh{
if [ "$#" -eq 0 ]; then
arg="$PWD"
else
arg="$(realpath "$1")"
fi
if [ ! -f "$kak_opt_local_kakrc_trusted" ]; then
echo "fail local_kakrc_trusted not found!"
exit 1
fi
temp="$(mktemp)"
touch "$temp"
# check if path is in file
if grep -qFx "$arg" "$kak_opt_local_kakrc_trusted"; then
# perform removal
grep -vxF "$arg" "$kak_opt_local_kakrc_trusted" >"$temp"
mv -f "$temp" "$kak_opt_local_kakrc_trusted"
else
echo "info -title 'local-kakrc' 'No such trusted directory!'"
rm "$temp" 2>/dev/null
fi
}
}
complete-command local-kakrc-rm-trusted shell-script-candidates %{
cat "$kak_opt_local_kakrc_trusted"
}
complete-command local-kakrc-add-trusted shell-script-candidates %{
find . -type d -name .git -prune -o -type d -print |
awk -F/ '$0 != "." { sub("^./", "", $0); print }'
}
# syntax highlighting for .kakrc
hook global BufCreate (.*/)?(\.kakrc) %{
set-option buffer filetype kak
}
hook global -once KakBegin .* %{
evaluate-commands %sh{
if [ "$kak_opt_local_kakrc_autoload" = 'true' ]; then
local-kakrc-load
fi
}
}
—