diff --git a/README.mkd b/README.mkd index 4b8e175..1c8348c 100644 --- a/README.mkd +++ b/README.mkd @@ -63,6 +63,8 @@ nmap ]h :execute v:count1 . "GitGutterNextHunk" nmap [h :execute v:count1 . "GitGutterPrevHunk" ``` +Finally, you can force vim-gitgutter to update its signs across all buffers with `:GitGutterAll`. + See the customisation section below for how to change the defaults. diff --git a/doc/gitgutter.txt b/doc/gitgutter.txt index 30bc6ff..d747039 100644 --- a/doc/gitgutter.txt +++ b/doc/gitgutter.txt @@ -61,6 +61,12 @@ Commands for turning Git Gutter on and off: :GitGutterToggle *:GitGutterToggle* Explicitly turn Git Gutter on if it was off and vice versa. + :GitGutter *:GitGutter* + Update signs for the current buffer. + + :GitGutterAll *:GitGutterAll* + Update signs across all buffers. + Commands for turning line highlighting on and off (defaults to off): :GitGutterLineHighlightsEnable *:GitGutterLineHighlightsEnable* @@ -75,10 +81,10 @@ Commands for turning line highlighting on and off (defaults to off): Commands for jumping between marked hunks: :GitGutterNextHunk *:GitGutterNextHunk* - Jump to the next marked hunk. + Jump to the next marked hunk. Takes a count. :GitGutterPrevHunk *:GitGutterPrevHunk* - Jump to the previous marked hunk. + Jump to the previous marked hunk. Takes a count. =============================================================================== 5. CUSTOMISATION *GitGutterCustomisation* diff --git a/plugin/gitgutter.vim b/plugin/gitgutter.vim index 3291990..ed03839 100644 --- a/plugin/gitgutter.vim +++ b/plugin/gitgutter.vim @@ -44,19 +44,27 @@ endfunction " Utility {{{ function! s:is_active() - return g:gitgutter_enabled && s:exists_current_file() && s:is_in_a_git_repo() && s:is_tracked_by_git() + return g:gitgutter_enabled && s:exists_file() && s:is_in_a_git_repo() && s:is_tracked_by_git() endfunction function! s:current_file() - return expand("%:p") + return expand('%:p') endfunction -function! s:exists_current_file() - return strlen(s:current_file()) > 0 +function! s:set_file(file) + let s:file = a:file endfunction -function! s:directory_of_current_file() - return shellescape(expand("%:p:h")) +function! s:file() + return s:file +endfunction + +function! s:exists_file() + return strlen(s:file()) > 0 +endfunction + +function! s:directory_of_file() + return shellescape(fnamemodify(s:file(), ':h')) endfunction function! s:discard_stdout_and_stderr() @@ -70,19 +78,19 @@ function! s:discard_stdout_and_stderr() return s:discard endfunction -function! s:command_in_directory_of_current_file(cmd) - return 'cd ' . s:directory_of_current_file() . ' && ' . a:cmd +function! s:command_in_directory_of_file(cmd) + return 'cd ' . s:directory_of_file() . ' && ' . a:cmd endfunction function! s:is_in_a_git_repo() let cmd = 'git rev-parse' . s:discard_stdout_and_stderr() - call system(s:command_in_directory_of_current_file(cmd)) + call system(s:command_in_directory_of_file(cmd)) return !v:shell_error endfunction function! s:is_tracked_by_git() - let cmd = 'git ls-files --error-unmatch' . s:discard_stdout_and_stderr() . ' ' . shellescape(s:current_file()) - call system(s:command_in_directory_of_current_file(cmd)) + let cmd = 'git ls-files --error-unmatch' . s:discard_stdout_and_stderr() . ' ' . shellescape(s:file()) + call system(s:command_in_directory_of_file(cmd)) return !v:shell_error endfunction @@ -90,6 +98,10 @@ function! s:snake_case_to_camel_case(text) return substitute(a:text, '\v(.)(\a+)(_(.)(.+))?', '\u\1\l\2\u\4\l\5', '') endfunction +function! s:buffers() + return range(1, bufnr('$')) +endfunction + " }}} " Highlights and signs {{{ @@ -164,9 +176,9 @@ endfunction " Diff processing {{{ function! s:run_diff() - let cmd = 'git diff --no-ext-diff --no-color -U0 ' . shellescape(s:current_file()) . + let cmd = 'git diff --no-ext-diff --no-color -U0 ' . shellescape(s:file()) . \ ' | grep -e "^@@ "' - let diff = system(s:command_in_directory_of_current_file(cmd)) + let diff = system(s:command_in_directory_of_file(cmd)) return diff endfunction @@ -303,7 +315,7 @@ function! s:clear_signs(file_name) endif endfunction -" This assumes there are no GitGutter signs in the current file. +" This assumes there are no GitGutter signs in the file. " If this is untenable we could change the regexp to exclude GitGutter's " signs. function! s:find_other_signs(file_name) @@ -344,12 +356,12 @@ endfunction function! s:remember_sign(id, file_name) if has_key(s:sign_ids, a:file_name) - let sign_ids_for_current_file = s:sign_ids[a:file_name] - call add(sign_ids_for_current_file, a:id) + let sign_ids_for_file = s:sign_ids[a:file_name] + call add(sign_ids_for_file, a:id) else - let sign_ids_for_current_file = [a:id] + let sign_ids_for_file = [a:id] endif - let s:sign_ids[a:file_name] = sign_ids_for_current_file + let s:sign_ids[a:file_name] = sign_ids_for_file endfunction function! s:is_other_sign(line_number) @@ -358,33 +370,40 @@ endfunction function! s:add_dummy_sign() let last_line = line('$') - exe ":sign place " . s:dummy_sign_id . " line=" . (last_line + 1) . " name=GitGutterDummy file=" . s:current_file() + exe ":sign place " . s:dummy_sign_id . " line=" . (last_line + 1) . " name=GitGutterDummy file=" . s:file() endfunction " }}} " Public interface {{{ -function! GitGutter() +function! GitGutterAll() + for buffer_id in s:buffers() + call GitGutter(fnamemodify(bufname(buffer_id), ':p')) + endfor +endfunction +command GitGutterAll call GitGutterAll() + +function! GitGutter(file) + call s:set_file(a:file) if s:is_active() call s:init() let diff = s:run_diff() let s:hunks = s:parse_diff(diff) let modified_lines = s:process_hunks(s:hunks) - let file_name = s:current_file() if g:gitgutter_sign_column_always call s:add_dummy_sign() endif - call s:clear_signs(file_name) - call s:find_other_signs(file_name) - call s:show_signs(file_name, modified_lines) + call s:clear_signs(a:file) + call s:find_other_signs(a:file) + call s:show_signs(a:file, modified_lines) endif endfunction -command GitGutter call GitGutter() +command GitGutter call GitGutter(s:current_file()) function! GitGutterDisable() let g:gitgutter_enabled = 0 - call s:clear_signs(s:current_file()) + call s:clear_signs(s:file()) endfunction command GitGutterDisable call GitGutterDisable() @@ -455,7 +474,7 @@ function! GitGutterPrevHunk(count) endfunction command -count=1 GitGutterPrevHunk call GitGutterPrevHunk() -" Returns the git-diff hunks for the current file or an empty list if there +" Returns the git-diff hunks for the file or an empty list if there " aren't any hunks. " " The return value is a list of lists. There is one inner list per hunk. @@ -478,9 +497,9 @@ endfunction augroup gitgutter autocmd! - autocmd BufReadPost,BufWritePost,FileReadPost,FileWritePost * call GitGutter() + autocmd BufReadPost,BufWritePost,FileReadPost,FileWritePost * call GitGutter(s:current_file()) if !has('gui_win32') - autocmd FocusGained * call GitGutter() + autocmd FocusGained * call GitGutterAll() endif augroup END