forked from rafi/vim-config
-
Notifications
You must be signed in to change notification settings - Fork 109
/
Copy pathdifftools.vim
57 lines (51 loc) · 1.26 KB
/
difftools.vim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
" Diff Unsaved Changes
" ---
"
" Commands:
" - DiffOrig: Show diff of unsaved changes
if exists('g:loaded_difftools')
finish
endif
let g:loaded_difftools = 1
augroup plugin_difftools
autocmd!
autocmd BufWinLeave __diff call s:close_diff()
augroup END
function! s:open_diff()
" Open diff window and start comparison
let l:bnr = bufnr('%')
call setwinvar(winnr(), 'diff_origin', l:bnr)
vertical new __diff
let l:diff_bnr = bufnr('%')
nnoremap <buffer><silent> q :quit<CR>
setlocal buftype=nofile bufhidden=wipe
r ++edit #
0d_
diffthis
setlocal readonly
wincmd p
let b:diff_bnr = l:diff_bnr
nnoremap <buffer><silent> q :execute bufwinnr(b:diff_bnr) . 'q'<CR>
diffthis
endfunction
function! s:close_diff()
" Close diff window, switch to original window and disable diff
" Credits: https://github.com/chemzqm/vim-easygit
let wnr = +bufwinnr(+expand('<abuf>'))
let val = getwinvar(wnr, 'diff_origin')
if ! len(val) | return | endif
for i in range(1, winnr('$'))
if i == wnr | continue | endif
if len(getwinvar(i, 'diff_origin'))
return
endif
endfor
let wnr = bufwinnr(val)
if wnr > 0
execute wnr . 'wincmd w'
diffoff
endif
endfunction
" Display diff of unsaved changes
command! -nargs=0 DiffOrig call s:open_diff()
" vim: set ts=2 sw=2 tw=80 noet :