Skip to content

Commit c1db9ee

Browse files
committed
Highlight git-blame. (experimental)
1 parent f0644fe commit c1db9ee

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

plugin/git.vim

+43
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@ if !exists('g:git_bin')
1010
let g:git_bin = 'git'
1111
endif
1212

13+
if !exists('g:git_author_highlight')
14+
let g:git_author_highlight = { }
15+
endif
16+
17+
if !exists('g:git_highlight_blame')
18+
let g:git_highlight_blame = 0
19+
endif
20+
1321
nnoremap <Leader>gd :GitDiff<Enter>
1422
nnoremap <Leader>gD :GitDiff --cached<Enter>
1523
nnoremap <Leader>gs :GitStatus<Enter>
@@ -195,12 +203,47 @@ function! GitBlame()
195203
setlocal nomodifiable
196204
setlocal nowrap scrollbind
197205

206+
if g:git_highlight_blame
207+
call s:DoHighlightGitBlame()
208+
endif
209+
198210
wincmd p
199211
setlocal nowrap scrollbind
200212

201213
syncbind
202214
endfunction
203215

216+
" Experimental
217+
function! s:DoHighlightGitBlame()
218+
for l in range(1, line('$'))
219+
let line = getline(l)
220+
let [commit, author] = matchlist(line, '\(\x\+\) (\(.\{-}\)\s* \d\d\d\d-\d\d-\d\d')[1:2]
221+
let syntax_name = s:LoadSyntaxRuleFor({ 'author': author })
222+
execute 'syntax match' syntax_name '+\%' . l . 'l.*+'
223+
endfor
224+
endfunction
225+
226+
function! s:LoadSyntaxRuleFor(params)
227+
let author = a:params.author
228+
let name = 'gitBlameAuthor_' . substitute(author, '\s', '_', 'g')
229+
if (!hlID(name))
230+
if has_key(g:git_author_highlight, author)
231+
execute 'highlight' name g:git_author_highlight[author]
232+
else
233+
let [n1, n2] = [0, 1]
234+
for c in split(author, '\zs')
235+
let n1 = (n1 + char2nr(c)) % 8
236+
let n2 = (n2 + char2nr(c) * 3) % 8
237+
endfor
238+
if n1 == n2
239+
let n1 = (n2 + 1) % 8
240+
endif
241+
execute 'highlight' name printf('ctermfg=%d ctermbg=%d', n1, n2)
242+
endif
243+
endif
244+
return name
245+
endfunction
246+
204247
function! GitDoCommand(args)
205248
let git_output = s:SystemGit(a:args)
206249
let git_output = substitute(git_output, '\n*$', '', '')

0 commit comments

Comments
 (0)