diff --git a/powerline/segments/vim/__init__.py b/powerline/segments/vim/__init__.py index 122c9e9a..9d054410 100644 --- a/powerline/segments/vim/__init__.py +++ b/powerline/segments/vim/__init__.py @@ -87,18 +87,27 @@ def window_cached(func): def mode(pl, segment_info, override=None): '''Return the current vim mode. + If mode (returned by ``mode()`` VimL function, see ``:h mode()`` in Vim) + consists of multiple characters and necessary mode is not known to powerline + then it will fall back to mode with last character(s) ignored. + :param dict override: dict for overriding default mode strings, e.g. ``{ 'n': 'NORM' }`` ''' mode = segment_info['mode'] if mode == 'nc': return None - if not override: - return vim_modes[mode] - try: - return override[mode] - except KeyError: - return vim_modes[mode] + while mode: + try: + if not override: + return vim_modes[mode] + try: + return override[mode] + except KeyError: + return vim_modes[mode] + except KeyError: + mode = mode[:-1] + return 'BUG' @window_cached diff --git a/tests/test_segments.py b/tests/test_segments.py index 05484fe1..4149e937 100644 --- a/tests/test_segments.py +++ b/tests/test_segments.py @@ -1159,6 +1159,8 @@ class TestVim(TestCase): self.assertEqual(self.vim.mode(pl=pl, segment_info=segment_info, override={'n': 'NORM'}), 'NORM') with vim_module._with('mode', 'i') as segment_info: self.assertEqual(self.vim.mode(pl=pl, segment_info=segment_info), 'INSERT') + with vim_module._with('mode', 'i\0') as segment_info: + self.assertEqual(self.vim.mode(pl=pl, segment_info=segment_info), 'INSERT') with vim_module._with('mode', chr(ord('V') - 0x40)) as segment_info: self.assertEqual(self.vim.mode(pl=pl, segment_info=segment_info), 'V-BLCK') self.assertEqual(self.vim.mode(pl=pl, segment_info=segment_info, override={'^V': 'VBLK'}), 'VBLK')