Skip to content

Instantly share code, notes, and snippets.

@Screwtapello
Created September 11, 2018 07:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Screwtapello/aaa147db6113df472c413ee5748258e2 to your computer and use it in GitHub Desktop.
Save Screwtapello/aaa147db6113df472c413ee5748258e2 to your computer and use it in GitHub Desktop.
declare-option -hidden int last_state_save_history_id 0
declare-option str state_save_path %sh{
echo ${XDG_DATA_HOME:-~/.local/share/}/kak/state-save/
}
define-command \
-docstring "Save the current selection state for this file" \
state-save \
%{ evaluate-commands %sh{
mkdir -p "$kak_opt_state_save_path"
state_file=$(printf "%s" "$kak_buffile" | sed -e 's|_|__|g' -e 's|/|_-|g')
echo "echo -debug saving state for $kak_buffile to $state_file: $kak_selections_desc"
printf "%s\n" "$kak_selections_desc" \
> "$kak_opt_state_save_path"/"$state_file"
}}
hook global WinCreate .* %{
set-option window last_state_save_history_id %val{history_id}
evaluate-commands %sh{
state_file=$(printf "%s" "$kak_buffile" | sed -e 's|_|__|g' -e 's|/|_-|g')
echo "echo -debug loading state for $kak_buffile from $state_file"
if [ -f "$kak_opt_state_save_path"/"$state_file" ]; then
printf "echo -debug select "
head -n 1 "$kak_opt_state_save_path"/"$state_file"
printf "select "
head -n 1 "$kak_opt_state_save_path"/"$state_file"
fi
}
hook window BufWritePost .* %{
set-option window last_state_save_history_id %val{history_id}
}
hook window NormalIdle .* %{ evaluate-commands %sh{
echo "echo -debug current history id: $kak_history_id"
echo "echo -debug last saved history id: $kak_opt_last_state_save_history_id"
if [ $kak_history_id = $kak_opt_last_state_save_history_id ]; then
printf "%s\n" "state-save"
fi
}}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment