diff --git a/powerline/bindings/vim/__init__.py b/powerline/bindings/vim/__init__.py index 4d59c985..6c2e4527 100644 --- a/powerline/bindings/vim/__init__.py +++ b/powerline/bindings/vim/__init__.py @@ -124,6 +124,14 @@ else: vim_get_func = VimFunc +def vim_get_autoload_func(f, rettype=None): + func = vim_get_func(f) + if not func: + vim.command('runtime! ' + f.replace('#', '/')[:f.rindex('#')] + '.vim') + func = vim_get_func(f) + return func + + if hasattr(vim, 'Function'): def vim_func_exists(f): try: @@ -145,6 +153,7 @@ if type(vim) is object: _getbufvar = vim_get_func('getbufvar') +_vim_exists = vim_get_func('exists', rettype='int') # It may crash on some old vim versions and I do not remember in which patch @@ -171,14 +180,20 @@ if hasattr(vim, 'vvars') and vim.vvars['version'] > 703: def vim_getwinvar(segment_info, varname): return _vim_to_python(segment_info['window'].vars[str(varname)]) + + def vim_global_exists(name): + try: + vim.vars[name] + except KeyError: + return False + else: + return True else: _vim_to_python_types = { dict: (lambda value: dict(((k, _vim_to_python(v)) for k, v in value.items()))), list: (lambda value: [_vim_to_python(i) for i in value]), } - _vim_exists = vim_get_func('exists', rettype='int') - def vim_getvar(varname): varname = 'g:' + varname if _vim_exists(varname): @@ -201,6 +216,13 @@ else: raise KeyError(varname) return result + def vim_global_exists(name): + return int(vim.eval('exists("g:' + name + '")')) + + +def vim_command_exists(name): + return _vim_exists(':' + name) + if sys.version_info < (3,): getbufvar = _getbufvar diff --git a/powerline/segments/vim/plugin/syntastic.py b/powerline/segments/vim/plugin/syntastic.py index 61324d45..c03edbdc 100644 --- a/powerline/segments/vim/plugin/syntastic.py +++ b/powerline/segments/vim/plugin/syntastic.py @@ -7,6 +7,7 @@ except ImportError: vim = object() from powerline.segments.vim import window_cached +from powerline.bindings.vim import vim_global_exists @window_cached @@ -21,8 +22,8 @@ def syntastic(pl, err_format='ERR:  {first_line} ({num}) ', warn_format='WARN Highlight groups used: ``syntastic.warning`` or ``warning``, ``syntastic.error`` or ``error``. ''' - if not int(vim.eval('exists("g:SyntasticLoclist")')): - return + if not vim_global_exists('SyntasticLoclist'): + return None has_errors = int(vim.eval('g:SyntasticLoclist.current().hasErrorsOrWarningsToDisplay()')) if not has_errors: return diff --git a/powerline/segments/vim/plugin/tagbar.py b/powerline/segments/vim/plugin/tagbar.py index 5daf5055..c7c74535 100644 --- a/powerline/segments/vim/plugin/tagbar.py +++ b/powerline/segments/vim/plugin/tagbar.py @@ -1,12 +1,11 @@ # vim:fileencoding=utf-8:noet from __future__ import (unicode_literals, division, absolute_import, print_function) -try: - import vim -except ImportError: - vim = object() - from powerline.segments.vim import window_cached +from powerline.bindings.vim import vim_command_exists, vim_get_autoload_func + + +currenttag = None @window_cached @@ -25,6 +24,12 @@ def current_tag(pl, flags='s'): .. _`official documentation`: https://github.com/majutsushi/tagbar/blob/master/doc/tagbar.txt ''' - if not int(vim.eval('exists(":Tagbar")')): - return None - return vim.eval('tagbar#currenttag("%s", "", "{0}")'.format(flags)) + global currenttag + if not currenttag: + if vim_command_exists('Tagbar'): + currenttag = vim_get_autoload_func('tagbar#currenttag') + if not currenttag: + return None + else: + return None + return currenttag('%s', '', flags) diff --git a/tests/vim.py b/tests/vim.py index 75404f6a..f3d84355 100644 --- a/tests/vim.py +++ b/tests/vim.py @@ -412,9 +412,11 @@ def _emul_bufnr(expr): @_vim -def _emul_exists(varname): - if varname.startswith('g:'): - return varname[2:] in vars +def _emul_exists(ident): + if ident.startswith('g:'): + return ident[2:] in vars + elif ident.startswith(':'): + return 0 raise NotImplementedError