New solution
You can view your last changes with the :changes
command. So you can fecth your most recent line change with a regex and then apply the line to matchadd()
as suggested by @joeytwiddle.
Here is the code :
function! DiffWithPrevious()
call clearmatches()
redir => message
silent changes
redir END
let line = matchstr(message, '\v\n\s{4}1[^0-9]*\zs\d+\ze')
highlight TemporalDiff ctermbg=green guibg=green
let m = matchadd('TemporalDiff', '\%'.line.'l')
endfunction
Note :
This function only add a new highlight without removing the old one, so you'd have to remove the old one first. With the clearmatches
function you can remove the matches before adding a new one. Careful, it will remove ALL matches. If you want more granularity, you can save your match and remove it manually :
e.g.
function! DiffWithPrevious()
call matchdelete(m)
...
let m = matchadd('TemporalDiff', '\%'.line.'l')
endfunction
- After some tests, I found out it only works for one-line change.
References :
Old solution
Here is a possible solution, mainly inspired by Diff current buffer and the original file :
function! DiffWithPrevious()
undo
write
redo
let filetype=&ft
diffthis
vnew | r # | normal! 1Gdd
diffthis
exe "setlocal bt=nofile bh=wipe nobl noswf ro ft=" . filetype
endfunction
The idea is to diff the file with the file on the system, so you undo your last change, write it, redo the las change and execute the diff.
I think this should do the job for time-to-time temporal diff visualisations.
matchadd()
, but slightly more tricky is working out which parts of the file have changed.