aboutsummaryrefslogtreecommitdiffstats
path: root/bundle/git-blamer/autoload/git_blamer.vim
blob: 40ffddb66165b882aca8fa85f9d23858bf5a25f6 (plain)
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
" Open a new split with a `git blame` of the current file
" Inspired by Ben Orenstein
" https://github.com/r00k/dotfiles/blob/7874508b825fd754e4ec3259da65f324ab96c8ea/vimrc#L74

function! git_blamer#Blame()
	let l:top_line = line('w0') + &scrolloff
	let l:line_number = line('.')
	let l:buffer_name = shellescape(bufname('%'))
	let l:buffer_number = bufnr('%')

	setlocal scrollbind cursorbind

	" Open new window
	leftabove vnew

	let b:git_blamer_restore = 'call setbufvar(' . l:buffer_number . ', "&scrollbind", 0) |
		 \ call setbufvar(' . l:buffer_number . ', "&cursorbind", 0)'

	" Read in `git blame` output
	execute 'read !git blame -w ' . l:buffer_name

	" Delete empty first line
	1 delete

	setlocal nomodified nomodifiable

	" Move cursor to position in starting file
	execute l:top_line
	normal! zt
	call setpos('.', [0, l:line_number, 0, 0])

	setlocal noswapfile nowrap nolist nobuflisted buftype=nofile bufhidden=wipe
	setlocal scrollbind cursorbind

	" Restore starting file's scrollbind on exit
	autocmd BufWinLeave <buffer> execute b:git_blamer_restore
endfunction