1# Author: Daniel
2# License: UNLICENSE
3# Depends: [builtin git.kak], [kak-toggle (optional)]
4
5# the purpose of this plugin is keep Kakoune's built-in git diff highlighting
6# updated as changes are made to the file. The easiest way to do this would be
7# to just run :git update-diff on NormalIdle .* but that's super inefficient.
8# so instead we'll track via %val{timestamp}. we'll have a buffer scoped option
9# git_diff_timestamp which will always be set to the value of %val{timestamp} when
10# we last ran update-diff. So if the timestamp has changed, we'll run update-diff.
11# This way while we still need _something_ to run on NormalIdle it won't be the entire
12# perl script
13
14# NOTE: there is an issue where an empty line-specs option causes the entire column to disappear;
15# which results in a jarring visual change b/c the window width changes all of a sudden.
16# I patched the builtin git.kak
17# so git_diff_flags always starts with a "dummy" flag: `0|.`
18# (it's impossible for a flag to be visible at line 0) but because it's not _empty_,
19# the column is still drawn (it's just blank)
20# IF YOU WANT THIS TOO, open the builtin git.kak,
21# find this line in the update_diff() function:
22# `$flags = $ENV{"kak_timestamp"};`
23# and add this line immediately after it:
24# `$flags .= " 0|.";`
25
26declare-option -hidden int git_diff_timestamp 0
27
28define-command git-diff-watch-enable %{
29 # always enable first
30 git show-diff
31 hook -group git-diff-watch buffer NormalIdle .* %{
32 evaluate-commands %sh{
33 # only run update-diff if the file has actually changed
34 if [ "$kak_timestamp" != "$kak_opt_git_diff_timestamp" ]; then
35 echo "git update-diff"
36 printf 'set-option buffer git_diff_timestamp %s\n' "$kak_timestamp"
37 fi
38 }
39 }
40
41}
42
43define-command git-diff-watch-disable %{
44 remove-hooks buffer git-diff-watch
45 git hide-diff
46}
47
48# using https://codeberg.org/ficd/kak-toggle
49register-toggle toggle-git-diff-watch false git-diff-watch-enable git-diff-watch-disable