From c79adfa6021486fab375fac545d303528a41c702 Mon Sep 17 00:00:00 2001 From: ZyX Date: Tue, 9 Dec 2014 19:56:20 +0300 Subject: [PATCH] Support old Vims without +multibyte --- powerline/bindings/vim/__init__.py | 34 ++++++++++++++++++++++++++---- powerline/renderers/vim.py | 4 ++-- powerline/vim.py | 6 ++---- 3 files changed, 34 insertions(+), 10 deletions(-) diff --git a/powerline/bindings/vim/__init__.py b/powerline/bindings/vim/__init__.py index 6c2e4527..b00007f9 100644 --- a/powerline/bindings/vim/__init__.py +++ b/powerline/bindings/vim/__init__.py @@ -12,10 +12,36 @@ except ImportError: from powerline.lib.unicode import unicode -try: - vim_encoding = vim.eval('&encoding') -except AttributeError: - vim_encoding = 'utf-8' +if ( + hasattr(vim, 'options') + and hasattr(vim, 'vvars') + and vim.vvars['version'] > 703 +): + if sys.version_info < (3,): + def get_vim_encoding(): + return vim.options['encoding'] or 'ascii' + else: + def get_vim_encoding(): + return vim.options['encoding'].decode('ascii') or 'ascii' +elif hasattr(vim, 'eval'): + def get_vim_encoding(): + return vim.eval('&encoding') or 'ascii' +else: + def get_vim_encoding(): + return 'utf-8' + +get_vim_encoding.__doc__ = ( + '''Get encoding used for Vim strings + + :return: + Value of ``&encoding``. If it is empty (i.e. Vim is compiled + without +multibyte) returns ``'ascii'``. When building documentation + outputs ``'utf-8'`` unconditionally. + ''' +) + + +vim_encoding = get_vim_encoding() python_to_vim_types = { diff --git a/powerline/renderers/vim.py b/powerline/renderers/vim.py index 58c690dd..0656d0b6 100644 --- a/powerline/renderers/vim.py +++ b/powerline/renderers/vim.py @@ -5,7 +5,7 @@ import sys import vim -from powerline.bindings.vim import vim_get_func, vim_getoption, environ, current_tabpage +from powerline.bindings.vim import vim_get_func, vim_getoption, environ, current_tabpage, get_vim_encoding from powerline.renderer import Renderer from powerline.colorscheme import ATTR_BOLD, ATTR_ITALIC, ATTR_UNDERLINE from powerline.theme import Theme @@ -42,7 +42,7 @@ class VimRenderer(Renderer): self.hl_groups = {} self.prev_highlight = None self.strwidth_error_name = register_strwidth_error(self.strwidth) - self.encoding = vim.eval('&encoding') + self.encoding = get_vim_encoding() def shutdown(self): self.theme.shutdown() diff --git a/powerline/vim.py b/powerline/vim.py index 29c82bdf..df00db55 100644 --- a/powerline/vim.py +++ b/powerline/vim.py @@ -8,7 +8,7 @@ from itertools import count import vim -from powerline.bindings.vim import vim_get_func, vim_getvar +from powerline.bindings.vim import vim_get_func, vim_getvar, get_vim_encoding from powerline import Powerline, FailedUnicode from powerline.lib import mergedicts @@ -73,9 +73,7 @@ class VimPowerline(Powerline): self.setup_kwargs.setdefault('_local_themes', []).append((key, config)) return True - @staticmethod - def get_encoding(): - return vim.eval('&encoding') + get_encoding = staticmethod(get_vim_encoding) def load_main_config(self): return _override_from(super(VimPowerline, self).load_main_config(), 'powerline_config_overrides')