# Author: Daniel # License: UNLICENSE # Depends: [builtin git.kak], [kak-toggle (optional)] # the purpose of this plugin is keep Kakoune's built-in git diff highlighting # updated as changes are made to the file. The easiest way to do this would be # to just run :git update-diff on NormalIdle .* but that's super inefficient. # so instead we'll track via %val{timestamp}. we'll have a buffer scoped option # git_diff_timestamp which will always be set to the value of %val{timestamp} when # we last ran update-diff. So if the timestamp has changed, we'll run update-diff. # This way while we still need _something_ to run on NormalIdle it won't be the entire # perl script # NOTE: there is an issue where an empty line-specs option causes the entire column to disappear; # which results in a jarring visual change b/c the window width changes all of a sudden. # I patched the builtin git.kak # so git_diff_flags always starts with a "dummy" flag: `0|.` # (it's impossible for a flag to be visible at line 0) but because it's not _empty_, # the column is still drawn (it's just blank) # IF YOU WANT THIS TOO, open the builtin git.kak, # find this line in the update_diff() function: # `$flags = $ENV{"kak_timestamp"};` # and add this line immediately after it: # `$flags .= " 0|.";` declare-option -hidden int git_diff_timestamp 0 define-command git-diff-watch-enable %{ # always enable first git show-diff hook -group git-diff-watch buffer NormalIdle .* %{ evaluate-commands %sh{ # only run update-diff if the file has actually changed if [ "$kak_timestamp" != "$kak_opt_git_diff_timestamp" ]; then echo "git update-diff" printf 'set-option buffer git_diff_timestamp %s\n' "$kak_timestamp" fi } } } define-command git-diff-watch-disable %{ remove-hooks buffer git-diff-watch git hide-diff } # using https://codeberg.org/ficd/kak-toggle register-toggle toggle-git-diff-watch false git-diff-watch-enable git-diff-watch-disable