aboutsummaryrefslogtreecommitdiffstats
path: root/bundle/git-blamer/autoload/git_blamer.vim
blob: 9df9d802203aa78d0c41a82bf70188a4b1a27721 (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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
" 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('%')
	let t:git_blamer_window_number = winnr()
	let restore = ''

	if &wrap
		let restore = '| call setbufvar(' . l:buffer_number . ', "&wrap", 1)'
	endif

	setlocal scrollbind cursorbind nowrap

	" Open new window
	leftabove vnew

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

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

	" Delete empty first line
	1 delete

	setlocal nomodified nomodifiable

	" Move cursor to position in starting file
	call cursor(l:top_line, 0)
	normal! zt
	call cursor(l:line_number, 0)

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

	nnoremap <buffer> q :q<CR>
	nnoremap <buffer> <Enter> :call <SID>GitShow()<CR>

	" Restore starting file's scrollbind on exit
	autocmd BufWinLeave <buffer>
		\ execute b:git_blamer_restore
		\ | call s:FocusOriginalWindow()
endfunction

function! s:FocusOriginalWindow()
	augroup git_blamer
		autocmd!

		autocmd WinEnter *
			\ execute t:git_blamer_window_number . 'wincmd w'
			\ | call s:RemoveWindowFocusAutocmd()
	augroup END
endfunction

function! s:RemoveWindowFocusAutocmd()
	augroup git_blamer
		autocmd!
	augroup END
endfunction

function! s:GitShow()
	execute '!git show ' . s:CommitSHAFromLine('.')
endfunction

function! s:CommitSHAFromLine(line)
	let current_line = getline(a:line)
	return matchstr(current_line, '\v^[0-9a-f]+')
endfunction