Merge pull request #1011 from ZyX-I/old-vims

Improve Vim bindings
This commit is contained in:
Nikolai Aleksandrovich Pavlov 2014-08-24 13:57:08 +04:00
commit c2f35ca76e
4 changed files with 70 additions and 49 deletions

View File

@ -19,7 +19,9 @@ Python headers to be installed on your system. Please consult your
distribution's documentation for details on how to compile and install distribution's documentation for details on how to compile and install
packages. packages.
Vim version 7.3.661 or newer is recommended for performance reasons. Vim version 7.4 or newer is recommended for performance reasons, but Powerline
is known to work on vim-7.0.112 (some segments may not work though as it was not
actually tested).
.. _usage-terminal-emulators: .. _usage-terminal-emulators:

View File

@ -20,6 +20,7 @@ if exists('g:powerline_pycmd')
echomsg 'specify g:powerline_pyeval explicitly or unset both and let powerline' echomsg 'specify g:powerline_pyeval explicitly or unset both and let powerline'
echomsg 'figure them out.' echomsg 'figure them out.'
echohl None echohl None
unlet s:pycmd
finish finish
endif endif
let s:pyeval = g:powerline_pyeval let s:pyeval = g:powerline_pyeval
@ -45,24 +46,38 @@ if !s:has_python
echomsg 'details.' echomsg 'details.'
echohl None echohl None
endif endif
unlet s:has_python
finish finish
endif endif
unlet s:has_python unlet s:has_python
let s:import_cmd = 'from powerline.vim import VimPowerline' let s:import_cmd = 'from powerline.vim import VimPowerline'
function s:rcmd(s)
if !exists('s:pystr')
let s:pystr = a:s . "\n"
else
let s:pystr = s:pystr . a:s . "\n"
endif
endfunction
try try
let s:pystr = "try:\n" let s:can_replace_pyeval = !exists('g:powerline_pyeval')
let s:pystr .= " ".s:import_cmd."\n" call s:rcmd("try:")
let s:pystr .= "except ImportError:\n" call s:rcmd(" ".s:import_cmd."")
let s:pystr .= " import sys, vim\n" call s:rcmd("except ImportError:")
let s:pystr .= " sys.path.append(vim.eval('expand(\"<sfile>:h:h:h:h:h\")'))\n" call s:rcmd(" import sys, vim")
let s:pystr .= " ".s:import_cmd."\n" call s:rcmd(" sys.path.append(vim.eval('expand(\"<sfile>:h:h:h:h:h\")'))")
call s:rcmd(" ".s:import_cmd."")
call s:rcmd("import vim")
call s:rcmd("VimPowerline().setup(pyeval=vim.eval('s:pyeval'), pycmd=vim.eval('s:pycmd'), can_replace_pyeval=int(vim.eval('s:can_replace_pyeval')))")
call s:rcmd("del VimPowerline")
execute s:pycmd s:pystr execute s:pycmd s:pystr
unlet s:pystr unlet s:pystr
let s:launched = 1 let s:launched = 1
finally finally
unlet s:can_replace_pyeval
unlet s:import_cmd unlet s:import_cmd
if !exists('s:launched') if !exists('s:launched')
unlet s:pystr
echohl ErrorMsg echohl ErrorMsg
echomsg 'An error occurred while importing powerline package.' echomsg 'An error occurred while importing powerline package.'
echomsg 'This could be caused by invalid sys.path setting,' echomsg 'This could be caused by invalid sys.path setting,'
@ -75,54 +90,52 @@ finally
echomsg 'should set g:powerline_pycmd to "py3" to make it load correctly.' echomsg 'should set g:powerline_pycmd to "py3" to make it load correctly.'
endif endif
echohl None echohl None
let s:pystr = "def powerline_troubleshoot():\n" call s:rcmd( "def powerline_troubleshoot():")
let s:pystr .= " import sys\n" call s:rcmd( " import sys")
let s:pystr .= " if sys.version_info < (2, 6):\n" call s:rcmd( " if sys.version_info < (2, 6):")
let s:pystr .= " print('Too old python version: ' + sys.version + ' (first supported is 2.6)')\n" call s:rcmd( " print('Too old python version: ' + sys.version + ' (first supported is 2.6)')")
let s:pystr .= " elif sys.version_info[0] == 3 and sys.version_info[1] < 2:\n" call s:rcmd( " elif sys.version_info[0] == 3 and sys.version_info[1] < 2:")
let s:pystr .= " print('Too old python 3 version: ' + sys.version + ' (first supported is 3.2)')\n" call s:rcmd( " print('Too old python 3 version: ' + sys.version + ' (first supported is 3.2)')")
let s:pystr .= " try:\n" call s:rcmd( " try:")
let s:pystr .= " import powerline\n" call s:rcmd( " import powerline")
let s:pystr .= " except ImportError:\n" call s:rcmd( " except ImportError:")
let s:pystr .= " print('Unable to import powerline, is it installed?')\n" call s:rcmd( " print('Unable to import powerline, is it installed?')")
if expand('<sfile>')[:4] isnot# '/usr/' if strpart(expand('<sfile>'), 0, 5) !=# '/usr/'
let s:pystr .= " else:\n" call s:rcmd(" else:")
let s:pystr .= " import os\n" call s:rcmd(" import os")
let s:pystr .= " powerline_dir = os.path.dirname(os.path.realpath(powerline.__file__))\n" call s:rcmd(" powerline_dir = os.path.dirname(os.path.realpath(powerline.__file__))")
let s:pystr .= " this_dir = os.path.dirname(os.path.realpath(vim.eval('expand(\"<sfile>:p\")')))\n" call s:rcmd(" this_dir = os.path.dirname(os.path.realpath(vim.eval('expand(\"<sfile>:p\")')))")
let s:pystr .= " this_dir = os.path.dirname(os.path.dirname(os.path.dirname(this_dir)))\n" call s:rcmd(" this_dir = os.path.dirname(os.path.dirname(os.path.dirname(this_dir)))")
let s:pystr .= " if os.path.basename(this_dir) != 'powerline':\n" call s:rcmd(" if os.path.basename(this_dir) != 'powerline':")
let s:pystr .= " print('Check your installation:')\n" call s:rcmd(" print('Check your installation:')")
let s:pystr .= " print('this script is not in powerline[/bindings/vim/plugin] directory,')\n" call s:rcmd(" print('this script is not in powerline[/bindings/vim/plugin] directory,')")
let s:pystr .= " print('neither it is installed system-wide')\n" call s:rcmd(" print('neither it is installed system-wide')")
let s:pystr .= " this_dir = os.path.dirname(this_dir)\n" call s:rcmd(" this_dir = os.path.dirname(this_dir)")
let s:pystr .= " real_powerline_dir = os.path.realpath(powerline_dir)\n" call s:rcmd(" real_powerline_dir = os.path.realpath(powerline_dir)")
let s:pystr .= " real_this_dir = os.path.realpath(this_dir)\n" call s:rcmd(" real_this_dir = os.path.realpath(this_dir)")
let s:pystr .= " if real_this_dir != sys.path[-1]:\n" call s:rcmd(" if real_this_dir != sys.path[-1]:")
let s:pystr .= " print('Check your installation:')\n" call s:rcmd(" print('Check your installation:')")
let s:pystr .= " print('this script is symlinked somewhere where powerline is not present.')\n" call s:rcmd(" print('this script is symlinked somewhere where powerline is not present.')")
let s:pystr .= " elif real_powerline_dir != real_this_dir:\n" call s:rcmd(" elif real_powerline_dir != real_this_dir:")
let s:pystr .= " print('It appears that you have two powerline versions installed:')\n" call s:rcmd(" print('It appears that you have two powerline versions installed:')")
let s:pystr .= " print('one in ' + real_powerline_dir + ', other in ' + real_this_dir + '.')\n" call s:rcmd(" print('one in ' + real_powerline_dir + ', other in ' + real_this_dir + '.')")
let s:pystr .= " print('You should remove one of this. Check out troubleshooting section,')\n" call s:rcmd(" print('You should remove one of this. Check out troubleshooting section,')")
let s:pystr .= " print('it contains some information about the alternatives.')\n" call s:rcmd(" print('it contains some information about the alternatives.')")
endif endif
call s:rcmd( "try:")
call s:rcmd( " powerline_troubleshoot()")
call s:rcmd( "finally:")
call s:rcmd( " del powerline_troubleshoot")
execute s:pycmd s:pystr execute s:pycmd s:pystr
unlet s:pystr unlet s:pystr
unlet s:pycmd unlet s:pycmd
unlet s:pyeval unlet s:pyeval
delfunction s:rcmd
finish finish
else else
unlet s:launched unlet s:launched
endif endif
unlet s:pycmd
unlet s:pyeval
delfunction s:rcmd
endtry endtry
let s:can_replace_pyeval = !exists('g:powerline_pyeval')
execute s:pycmd 'import vim'
execute s:pycmd 'VimPowerline().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 VimPowerline'
unlet s:can_replace_pyeval
unlet s:pycmd
unlet s:pyeval

View File

@ -18,6 +18,10 @@ except ImportError:
vim_mode = vim_get_func('mode', rettype=str) vim_mode = vim_get_func('mode', rettype=str)
if int(vim.eval('v:version')) >= 702:
_vim_mode = vim_mode
vim_mode = lambda: _vim_mode(1)
mode_translations = { mode_translations = {
chr(ord('V') - 0x40): '^V', chr(ord('V') - 0x40): '^V',
chr(ord('S') - 0x40): '^S', chr(ord('S') - 0x40): '^S',
@ -89,7 +93,7 @@ class VimRenderer(Renderer):
segment_info = self.segment_info.copy() segment_info = self.segment_info.copy()
if window is vim.current.window: if window is vim.current.window:
mode = vim_mode(1) mode = vim_mode()
mode = mode_translations.get(mode, mode) mode = mode_translations.get(mode, mode)
else: else:
mode = 'nc' mode = 'nc'

View File

@ -229,6 +229,8 @@ def command(cmd):
def eval(expr): def eval(expr):
if expr.startswith('g:'): if expr.startswith('g:'):
return vars[expr[2:]] return vars[expr[2:]]
elif expr.startswith('v:'):
return vvars[expr[2:]]
elif expr.startswith('&'): elif expr.startswith('&'):
return options[expr[1:]] return options[expr[1:]]
elif expr.startswith('$'): elif expr.startswith('$'):