Avoid Windows command prompt popups when possible.

Before this commit some Windows users saw the command prompt pop
up briefly, and/or the taskbar flicker, every time the plugin ran.

Now the plugin will use xolox's vim-shell and vim-misc, if they are
available and we are on Windows, to execute external commands.  Xolox's
clever plugins avoid the command prompt popup and taskbar flicker.

Windows users with those plugins installed can opt out by setting a
variable in their vimrc.

Many thanks to @suxpert for the initial code.
This commit is contained in:
Andy Stewart
2014-06-19 10:45:40 +02:00
parent 7ac9d059f8
commit a2aa2b1100
5 changed files with 47 additions and 7 deletions

View File

@@ -48,6 +48,8 @@ Add `Plugin 'airblade/vim-gitgutter'` to your `~/.vimrc` and then:
* either within Vim: `:PluginInstall` * either within Vim: `:PluginInstall`
* or in your shell: `vim +PluginInstall +qall` * or in your shell: `vim +PluginInstall +qall`
If you are on Windows you may find the command prompt pops up briefly every time vim-gitgutter runs. You can avoid this by installing both [vim-misc](https://github.com/xolox/vim-misc) and [vim-shell](https://github.com/xolox/vim-shell). If you have those two plugins but don't want vim-gitgutter to use them, you can opt out with `let g:gitgutter_avoid_cmd_prompt_on_windows = 0` in your `~/.vimrc`.
### Usage ### Usage

View File

@@ -31,12 +31,12 @@ function! diff#run_diff(realtime, use_external_grep)
let cmd .= '))' let cmd .= '))'
if a:realtime if a:realtime
let diff = system(utility#command_in_directory_of_file(cmd), utility#buffer_contents()) let diff = utility#system(utility#command_in_directory_of_file(cmd), utility#buffer_contents())
else else
let diff = system(utility#command_in_directory_of_file(cmd)) let diff = utility#system(utility#command_in_directory_of_file(cmd))
endif endif
if v:shell_error if utility#shell_error()
" A shell error indicates the file is not tracked by git (unless something " A shell error indicates the file is not tracked by git (unless something
" bizarre is going on). " bizarre is going on).
throw 'diff failed' throw 'diff failed'

View File

@@ -160,7 +160,7 @@ function! gitgutter#stage_hunk()
let diff_for_hunk = diff#generate_diff_for_hunk(current_hunk, 1) let diff_for_hunk = diff#generate_diff_for_hunk(current_hunk, 1)
" apply the diff " apply the diff
call system(utility#command_in_directory_of_file('git apply --cached --unidiff-zero - '), diff_for_hunk) call utility#system(utility#command_in_directory_of_file('git apply --cached --unidiff-zero - '), diff_for_hunk)
" refresh gitgutter's view of buffer " refresh gitgutter's view of buffer
silent execute "GitGutter" silent execute "GitGutter"
@@ -183,7 +183,7 @@ function! gitgutter#revert_hunk()
let diff_for_hunk = diff#generate_diff_for_hunk(current_hunk, 1) let diff_for_hunk = diff#generate_diff_for_hunk(current_hunk, 1)
" apply the diff " apply the diff
call system(utility#command_in_directory_of_file('git apply --reverse --unidiff-zero - '), diff_for_hunk) call utility#system(utility#command_in_directory_of_file('git apply --reverse --unidiff-zero - '), diff_for_hunk)
" reload file " reload file
silent edit silent edit

View File

@@ -1,4 +1,7 @@
let s:file = '' let s:file = ''
let s:using_xolox_shell = -1
let s:exit_code = 0
function! utility#is_active() function! utility#is_active()
return g:gitgutter_enabled && utility#exists_file() return g:gitgutter_enabled && utility#exists_file()
@@ -14,7 +17,7 @@ endfunction
function! utility#shellescape(arg) function! utility#shellescape(arg)
if a:arg =~ '^[A-Za-z0-9_/.-]\+$' if a:arg =~ '^[A-Za-z0-9_/.-]\+$'
return a:arg return a:arg
elseif &shell =~# 'cmd' elseif &shell =~# 'cmd' || utility#using_xolox_shell()
return '"' . substitute(substitute(a:arg, '"', '""', 'g'), '%', '"%"', 'g') . '"' return '"' . substitute(substitute(a:arg, '"', '""', 'g'), '%', '"%"', 'g') . '"'
else else
return shellescape(a:arg) return shellescape(a:arg)
@@ -68,10 +71,44 @@ function! utility#buffer_contents()
return join(getbufline(s:file, 1, '$'), eol) . eol return join(getbufline(s:file, 1, '$'), eol) . eol
endfunction endfunction
function! utility#shell_error()
return utility#using_xolox_shell() ? s:exit_code : v:shell_error
endfunction
function! utility#using_xolox_shell()
if s:using_xolox_shell == -1
if !g:gitgutter_avoid_cmd_prompt_on_windows
let s:using_xolox_shell = 0
" Although xolox/vim-shell works on both windows and unix we only want to use
" it on windows.
elseif has('win32') || has('win64') || has('win32unix')
let s:using_xolox_shell = exists('g:xolox#misc#version') && exists('g:xolox#shell#version')
else
let s:using_xolox_shell = 0
endif
endif
return s:using_xolox_shell
endfunction
function! utility#system(cmd, ...)
if utility#using_xolox_shell()
let options = {'command': a:cmd, 'check': 0}
if a:0 > 0
let options['stdin'] = a:1
endif
let ret = xolox#misc#os#exec(options)
let output = join(ret.stdout, "\n")
let s:exit_code = ret.exit_code
else
let output = (a:0 == 0) ? system(a:cmd) : system(a:cmd, a:1)
endif
return output
endfunction
function! utility#file_relative_to_repo_root() function! utility#file_relative_to_repo_root()
let file_path_relative_to_repo_root = getbufvar(s:file, 'gitgutter_repo_relative_path') let file_path_relative_to_repo_root = getbufvar(s:file, 'gitgutter_repo_relative_path')
if empty(file_path_relative_to_repo_root) if empty(file_path_relative_to_repo_root)
let dir_path_relative_to_repo_root = system(utility#command_in_directory_of_file('git rev-parse --show-prefix')) let dir_path_relative_to_repo_root = utility#system(utility#command_in_directory_of_file('git rev-parse --show-prefix'))
let dir_path_relative_to_repo_root = utility#strip_trailing_new_line(dir_path_relative_to_repo_root) let dir_path_relative_to_repo_root = utility#strip_trailing_new_line(dir_path_relative_to_repo_root)
let file_path_relative_to_repo_root = dir_path_relative_to_repo_root . utility#filename() let file_path_relative_to_repo_root = dir_path_relative_to_repo_root . utility#filename()
call setbufvar(s:file, 'gitgutter_repo_relative_path', file_path_relative_to_repo_root) call setbufvar(s:file, 'gitgutter_repo_relative_path', file_path_relative_to_repo_root)

View File

@@ -41,6 +41,7 @@ call s:set('g:gitgutter_sign_modified_removed', '~_')
call s:set('g:gitgutter_diff_args', '') call s:set('g:gitgutter_diff_args', '')
call s:set('g:gitgutter_escape_grep', 0) call s:set('g:gitgutter_escape_grep', 0)
call s:set('g:gitgutter_map_keys', 1) call s:set('g:gitgutter_map_keys', 1)
call s:set('g:gitgutter_avoid_cmd_prompt_on_windows', 1)
call highlight#define_sign_column_highlight() call highlight#define_sign_column_highlight()
call highlight#define_highlights() call highlight#define_highlights()