diff --git a/python/ycm/tests/client/command_request_test.py b/python/ycm/tests/client/command_request_test.py index 764d4732..25667b52 100644 --- a/python/ycm/tests/client/command_request_test.py +++ b/python/ycm/tests/client/command_request_test.py @@ -107,7 +107,11 @@ class GoToResponse_QuickFix_test( object ): ] ) vim_command.assert_has_exact_calls( [ call( 'botright copen' ), - call( 'au WinLeave q' ), + call( 'augroup ycmquickfix' ), + call( 'autocmd! * ' ), + call( 'autocmd WinLeave ' + 'if bufnr( "%" ) == expand( "" ) | q | endif' ), + call( 'augroup END' ), call( 'doautocmd User YcmQuickFixOpened' ) ] ) set_fitting_height.assert_called_once_with() diff --git a/python/ycm/tests/vimsupport_test.py b/python/ycm/tests/vimsupport_test.py index b8e2e664..6fde2a36 100644 --- a/python/ycm/tests/vimsupport_test.py +++ b/python/ycm/tests/vimsupport_test.py @@ -165,7 +165,11 @@ def OpenLocationList_test( vim_command, fitting_height, variable_exists ): vimsupport.OpenLocationList( focus = False, autoclose = True ) vim_command.assert_has_exact_calls( [ call( 'lopen' ), - call( 'au WinLeave q' ), + call( 'augroup ycmlocation' ), + call( 'autocmd! * ' ), + call( 'autocmd WinLeave ' + 'if bufnr( "%" ) == expand( "" ) | q | endif' ), + call( 'augroup END' ), call( 'doautocmd User YcmLocationOpened' ), call( 'silent! wincmd p' ) ] ) diff --git a/python/ycm/vimsupport.py b/python/ycm/vimsupport.py index a320d8db..2b24ea7f 100644 --- a/python/ycm/vimsupport.py +++ b/python/ycm/vimsupport.py @@ -341,9 +341,7 @@ def OpenLocationList( focus = False, autoclose = False ): SetFittingHeightForCurrentWindow() if autoclose: - # This autocommand is automatically removed when the location list window is - # closed. - vim.command( 'au WinLeave q' ) + AutoCloseOnCurrentBuffer( 'ycmlocation' ) if VariableExists( '#User#YcmLocationOpened' ): vim.command( 'doautocmd User YcmLocationOpened' ) @@ -368,9 +366,7 @@ def OpenQuickFixList( focus = False, autoclose = False ): SetFittingHeightForCurrentWindow() if autoclose: - # This autocommand is automatically removed when the quickfix window is - # closed. - vim.command( 'au WinLeave q' ) + AutoCloseOnCurrentBuffer( 'ycmquickfix' ) if VariableExists( '#User#YcmQuickFixOpened' ): vim.command( 'doautocmd User YcmQuickFixOpened' ) @@ -1238,3 +1234,13 @@ def VimVersionAtLeast( version_string ): return actual_major_and_minor > matching_major_and_minor return GetBoolValue( "has( 'patch{0}' )".format( patch ) ) + + +def AutoCloseOnCurrentBuffer( name ): + """Create an autocommand group with name |name| on the current buffer that + automatically closes it when leaving its window.""" + vim.command( 'augroup {}'.format( name ) ) + vim.command( 'autocmd! * ' ) + vim.command( 'autocmd WinLeave ' + 'if bufnr( "%" ) == expand( "" ) | q | endif' ) + vim.command( 'augroup END' )