From b9109af709a0fa005ee8099c4add015dbfbc06c4 Mon Sep 17 00:00:00 2001 From: Davit Samvelyan Date: Sat, 22 Mar 2014 14:24:16 +0400 Subject: [PATCH] Added check for Vim 'hidden' option when trying to open result in the same buffer --- README.md | 7 +++++-- python/ycm/vimsupport.py | 13 +++++++++++-- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index eb77991f..702328a7 100644 --- a/README.md +++ b/README.md @@ -1339,8 +1339,11 @@ Default: `1` ### The `g:ycm_goto_buffer_command` option Defines where `GoTo*` commands result should be opened. -Can take one of the following values: `[ 'same-buffer', 'horizontal-split', 'vertical-split', 'new-tab' ]` -If this option is set to `'same-buffer'` but current buffer is modified then result will be opened in horizontal split. +Can take one of the following values: +`[ 'same-buffer', 'horizontal-split', 'vertical-split', 'new-tab' ]` +If this option is set to the `'same-buffer'` but current buffer can not +be switched (when buffer is modified and `nohidden` option is set), +then result will be opened in horizontal split. Default: `'same-buffer'` let g:ycm_goto_buffer_command = 'same-buffer' diff --git a/python/ycm/vimsupport.py b/python/ycm/vimsupport.py index 9bf5454a..65fc5618 100644 --- a/python/ycm/vimsupport.py +++ b/python/ycm/vimsupport.py @@ -72,7 +72,8 @@ def GetBufferOption( buffer_object, option ): def BufferModified( buffer_object ): - return bool( int( GetBufferOption( buffer_object, 'mod' ) ) ) + return buffer_object.options[ 'mod' ] + def GetUnsavedAndCurrentBufferData(): buffers_data = {} @@ -238,6 +239,14 @@ def VimExpressionToPythonType( vim_expression ): return result +def HiddenEnabled( buffer_object ): + return vim.options[ 'hid' ] + + +def BufferIsUsable( buffer_object ): + return not BufferModified( buffer_object ) or HiddenEnabled( buffer_object ) + + # Both |line| and |column| need to be 1-based def JumpToLocation( filename, line, column ): # Add an entry to the jumplist @@ -252,7 +261,7 @@ def JumpToLocation( filename, line, column ): # jumplist. user_command = user_options_store.Value( 'goto_buffer_command' ) command = BUFFER_COMMAND_MAP.get( user_command, 'edit' ) - if command == 'edit' and BufferModified( vim.current.buffer ): + if command == 'edit' and not BufferIsUsable( vim.current.buffer ): command = 'split' vim.command( 'keepjumps {0} {1}'.format( command, filename ) ) vim.current.window.cursor = ( line, column - 1 )