From 191c71cf536ef2ffb16da4d1f1f1b4450d0c43ba Mon Sep 17 00:00:00 2001 From: ZyX Date: Tue, 29 Jul 2014 14:48:26 +0400 Subject: [PATCH] Do not let powerline trigger loading wrong python Used python version is controlled by `g:powerline_pycmd`. User configuration now has top priority: if `g:powerline_pyeval` is set powerline will not try to use `pyeval()` emulation in old Vim versions. Closes #937 as WONTFIX --- docs/source/overview.rst | 19 +++++++++ powerline/bindings/vim/plugin/powerline.vim | 45 ++++++++++++++++++--- powerline/vim.py | 5 ++- 3 files changed, 62 insertions(+), 7 deletions(-) diff --git a/docs/source/overview.rst b/docs/source/overview.rst index 5aedaf74..9ddc798a 100644 --- a/docs/source/overview.rst +++ b/docs/source/overview.rst @@ -127,6 +127,25 @@ hand: ``powerline`` is installed and run just like any other plugin using call vam#ActivateAddons(['powerline']) +.. note:: + If you use supplied :file:`powerline.vim` file to load powerline there are + additional configuration variables available: ``g:powerline_pycmd`` and + ``g:powerline_pyeval``. First sets command used to load powerline: expected + values are ``"py"`` and ``"py3"``. Second sets function used in statusline, + expected values are ``"pyeval"`` and ``"py3eval"``. + + If ``g:powerline_pycmd`` is set to the one of the expected values then + ``g:powerline_pyeval`` will be set accordingly. If it is set to some other + value then you must also set ``g:powerline_pyeval``. Powerline will not + check that Vim is compiled with Python support if you set + ``g:powerline_pycmd`` to an unexpected value. + + These values are to be used to specify the only Python that is to be loaded + if you have both versions: Vim may disable loading one python version if + other was already loaded. They should also be used if you have two python + versions able to load simultaneously, but with powerline installed only for + python-3 version. + Shell prompts ------------- diff --git a/powerline/bindings/vim/plugin/powerline.vim b/powerline/bindings/vim/plugin/powerline.vim index 3aa97853..15d37d05 100644 --- a/powerline/bindings/vim/plugin/powerline.vim +++ b/powerline/bindings/vim/plugin/powerline.vim @@ -3,7 +3,41 @@ if exists('g:powerline_loaded') endif let g:powerline_loaded = 1 -if !has('python') && !has('python3') +if exists('g:powerline_pycmd') + let s:pycmd = substitute(g:powerline_pycmd, '\v\C^(py)%[thon](3?)$', '\1\2', '') + if s:pycmd is# 'py' + let s:has_python = has('python') + let s:pyeval = get(g:, 'powerline_pyeval', 'pyeval') + elseif s:pycmd is# 'py3' + let s:has_python = has('python3') + let s:pyeval = 'py3eval' + let s:pyeval = get(g:, 'powerline_pyeval', 'py3eval') + else + if !exists('g:powerline_pyeval') + echohl ErrorMsg + echomsg 'g:powerline_pycmd was set to an unknown values, but g:powerline_pyeval' + echomsg 'was not set. You should either set g:powerline_pycmd to "py3" or "py",' + echomsg 'specify g:powerline_pyeval explicitly or unset both and let powerline' + echomsg 'figure them out.' + echohl None + finish + endif + let s:pyeval = g:powerline_pyeval + let s:has_python = 1 + endif +elseif has('python') + let s:has_python = 1 + let s:pycmd = 'py' + let s:pyeval = get(g:, 'powerline_pyeval', 'pyeval') +elseif has('python3') + let s:has_python = 1 + let s:pycmd = 'py3' + let s:pyeval = get(g:, 'powerline_pyeval', 'py3eval') +else + let s:has_python = 0 +endif + +if !s:has_python if !exists('g:powerline_no_python_error') echohl ErrorMsg echomsg 'You need vim compiled with Python 2.6, 2.7 or 3.2 and later support' @@ -13,9 +47,7 @@ if !has('python') && !has('python3') endif finish endif - -let s:pycmd = substitute(get(g:, 'powerline_pycmd', has('python') ? 'py' : 'py3'), '\v^(py)%[thon](3?)$', '\1\2', '') -let s:pyeval = get(g:, 'powerline_pyeval', s:pycmd.'eval') +unlet s:has_python let s:import_cmd = 'from powerline.vim import setup as powerline_setup' try @@ -81,9 +113,12 @@ finally endif endtry +let s:can_replace_pyeval = !exists('g:powerline_pyeval') + execute s:pycmd 'import vim' -execute s:pycmd 'powerline_setup(pyeval=vim.eval("s:pyeval"), pycmd=vim.eval("s:pycmd"))' +execute s:pycmd 'powerline_setup(pyeval=vim.eval("s:pyeval"), pycmd=vim.eval("s:pycmd"), can_replace_pyeval=int(vim.eval("s:can_replace_pyeval")))' execute s:pycmd 'del powerline_setup' +unlet s:can_replace_pyeval unlet s:pycmd unlet s:pyeval diff --git a/powerline/vim.py b/powerline/vim.py index a26f5e40..829961e9 100644 --- a/powerline/vim.py +++ b/powerline/vim.py @@ -155,16 +155,17 @@ class VimPowerline(Powerline): __main__.__dict__))) -def setup(pyeval=None, pycmd=None): +def setup(pyeval=None, pycmd=None, can_replace_pyeval=True): import sys import __main__ if not pyeval: pyeval = 'pyeval' if sys.version_info < (3,) else 'py3eval' + can_replace_pyeval = True if not pycmd: pycmd = 'python' if sys.version_info < (3,) else 'python3' # pyeval() and vim.bindeval were both introduced in one patch - if not hasattr(vim, 'bindeval'): + if not hasattr(vim, 'bindeval') and can_replace_pyeval: vim.command((''' function! PowerlinePyeval(e) {pycmd} powerline.pyeval()