Alpha Chen 10 years ago
parent c9a8770821
commit 20c2a16671

@ -1,157 +0,0 @@
# vim:fileencoding=utf-8:noet
import sys
import codecs
try:
import vim
except ImportError:
vim = {}
if hasattr(vim, 'bindeval'):
def vim_get_func(f, rettype=None):
'''Return a vim function binding.'''
try:
func = vim.bindeval('function("' + f + '")')
if sys.version_info >= (3,) and rettype is str:
return (lambda *args, **kwargs: func(*args, **kwargs).decode('utf-8', errors='replace'))
return func
except vim.error:
return None
else:
import json
class VimFunc(object):
'''Evaluate a vim function using vim.eval().
This is a fallback class for older vim versions.
'''
__slots__ = ('f', 'rettype')
def __init__(self, f, rettype=None):
self.f = f
self.rettype = rettype
def __call__(self, *args):
r = vim.eval(self.f + '(' + json.dumps(args)[1:-1] + ')')
if self.rettype:
return self.rettype(r)
return r
vim_get_func = VimFunc
# It may crash on some old vim versions and I do not remember in which patch
# I fixed this crash.
if hasattr(vim, 'vvars') and vim.vvars['version'] > 703:
_vim_to_python_types = {
vim.Dictionary: lambda value: dict(((key, _vim_to_python(value[key])) for key in value.keys())),
vim.List: lambda value: [_vim_to_python(item) for item in value],
vim.Function: lambda _: None,
}
_id = lambda value: value
def _vim_to_python(value):
return _vim_to_python_types.get(type(value), _id)(value)
def vim_getvar(varname):
return _vim_to_python(vim.vars[str(varname)])
def bufvar_exists(buffer, varname):
buffer = buffer or vim.current.buffer
return varname in buffer.vars
def vim_getwinvar(segment_info, varname):
return _vim_to_python(segment_info['window'].vars[str(varname)])
else:
_vim_exists = vim_get_func('exists', rettype=int)
def vim_getvar(varname): # NOQA
varname = 'g:' + varname
if _vim_exists(varname):
return vim.eval(varname)
else:
raise KeyError(varname)
def bufvar_exists(buffer, varname): # NOQA
if not buffer or buffer.number == vim.current.buffer.number:
return vim.eval('exists("b:{0}")'.format(varname))
else:
return vim.eval('has_key(getbufvar({0}, ""), {1})'
.format(buffer.number, varname))
def vim_getwinvar(segment_info, varname): # NOQA
result = vim.eval('getwinvar({0}, "{1}")'.format(segment_info['winnr'], varname))
if result == '':
if not int(vim.eval('has_key(getwinvar({0}, ""), "{1}")'.format(segment_info['winnr'], varname))):
raise KeyError(varname)
return result
if hasattr(vim, 'options'):
def vim_getbufoption(info, option):
return info['buffer'].options[str(option)]
else:
def vim_getbufoption(info, option): # NOQA
return getbufvar(info['bufnr'], '&' + option)
if sys.version_info < (3,) or not hasattr(vim, 'bindeval'):
getbufvar = vim_get_func('getbufvar')
else:
_getbufvar = vim_get_func('getbufvar')
def getbufvar(*args):
r = _getbufvar(*args)
if type(r) is bytes:
return r.decode('utf-8')
return r
class VimEnviron(object):
@staticmethod
def __getitem__(key):
return vim.eval('$' + key)
@staticmethod
def get(key, default=None):
return vim.eval('$' + key) or default
@staticmethod
def __setitem__(key, value):
return vim.command('let $' + key + '="'
+ value.replace('"', '\\"').replace('\\', '\\\\').replace('\n', '\\n').replace('\0', '')
+ '"')
if sys.version_info < (3,):
def buffer_name(buf):
return buf.name
else:
vim_bufname = vim_get_func('bufname')
def buffer_name(buf): # NOQA
try:
name = buf.name
except UnicodeDecodeError:
return vim_bufname(buf.number)
else:
return name.encode('utf-8') if name else None
vim_strtrans = vim_get_func('strtrans')
def powerline_vim_strtrans_error(e):
if not isinstance(e, UnicodeDecodeError):
raise NotImplementedError
# Assuming &encoding is utf-8 strtrans should not return anything but ASCII
# under current circumstances
text = vim_strtrans(e.object[e.start:e.end]).decode()
return (text, e.end)
codecs.register_error('powerline_vim_strtrans_error', powerline_vim_strtrans_error)
environ = VimEnviron()

@ -1,89 +0,0 @@
if exists('g:powerline_loaded')
finish
endif
let g:powerline_loaded = 1
if !has('python') && !has('python3')
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'
echomsg 'for Powerline to work. Please consult the documentation for more'
echomsg 'details.'
echohl None
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')
let s:import_cmd = 'from powerline.vim import setup as powerline_setup'
try
let s:pystr = "try:\n"
let s:pystr .= " ".s:import_cmd."\n"
let s:pystr .= "except ImportError:\n"
let s:pystr .= " import sys, vim\n"
let s:pystr .= " sys.path.append(vim.eval('expand(\"<sfile>:h:h:h:h:h\")'))\n"
let s:pystr .= " ".s:import_cmd."\n"
execute s:pycmd s:pystr
unlet s:pystr
let s:launched = 1
finally
unlet s:import_cmd
if !exists('s:launched')
echohl ErrorMsg
echomsg 'An error occurred while importing powerline package.'
echomsg 'This could be caused by invalid sys.path setting,'
echomsg 'or by an incompatible Python version (powerline requires'
echomsg 'Python 2.6, 2.7 or 3.2 and later to work). Please consult'
echomsg 'the troubleshooting section in the documentation for'
echomsg 'possible solutions.'
echohl None
let s:pystr = "def powerline_troubleshoot():\n"
let s:pystr .= " import sys\n"
let s:pystr .= " if sys.version_info < (2, 6):\n"
let s:pystr .= " print('Too old python version: ' + sys.version + ' (first supported is 2.6)')\n"
let s:pystr .= " elif sys.version_info[0] == 3 and sys.version_info[1] < 2:\n"
let s:pystr .= " print('Too old python 3 version: ' + sys.version + ' (first supported is 3.2)')\n"
let s:pystr .= " try:\n"
let s:pystr .= " import powerline\n"
let s:pystr .= " except ImportError:\n"
let s:pystr .= " print('Unable to import powerline, is it installed?')\n"
if expand('<sfile>')[:4] isnot# '/usr/'
let s:pystr .= " else:\n"
let s:pystr .= " import os\n"
let s:pystr .= " powerline_dir = os.path.dirname(os.path.realpath(powerline.__file__))\n"
let s:pystr .= " this_dir = os.path.dirname(os.path.realpath(vim.eval('expand(\"<sfile>:p\")')))\n"
let s:pystr .= " this_dir = os.path.dirname(os.path.dirname(os.path.dirname(this_dir)))\n"
let s:pystr .= " if os.path.basename(this_dir) != 'powerline':\n"
let s:pystr .= " print('Check your installation:')\n"
let s:pystr .= " print('this script is not in powerline[/bindings/vim/plugin] directory,')\n"
let s:pystr .= " print('neither it is installed system-wide')\n"
let s:pystr .= " this_dir = os.path.dirname(this_dir)\n"
let s:pystr .= " real_powerline_dir = os.path.realpath(powerline_dir)\n"
let s:pystr .= " real_this_dir = os.path.realpath(this_dir)\n"
let s:pystr .= " if real_this_dir != sys.path[-1]:\n"
let s:pystr .= " print('Check your installation:')\n"
let s:pystr .= " print('this script is symlinked somewhere where powerline is not present.')\n"
let s:pystr .= " elif real_powerline_dir != real_this_dir:\n"
let s:pystr .= " print('It appears that you have two powerline versions installed:')\n"
let s:pystr .= " print('one in ' + real_powerline_dir + ', other in ' + real_this_dir + '.')\n"
let s:pystr .= " print('You should remove one of this. Check out troubleshooting section,')\n"
let s:pystr .= " print('it contains some information about the alternatives.')\n"
endif
execute s:pycmd s:pystr
unlet s:pystr
unlet s:pycmd
unlet s:pyeval
finish
else
unlet s:launched
endif
endtry
execute s:pycmd 'import vim'
execute s:pycmd 'powerline_setup(pyeval=vim.eval("s:pyeval"), pycmd=vim.eval("s:pycmd"))'
execute s:pycmd 'del powerline_setup'
unlet s:pycmd
unlet s:pyeval

@ -238,6 +238,11 @@ nmap <leader>vw<leader>i <plug>VimwikiDiaryGenerateLinks
" NERDCommenter " NERDCommenter
let NERDSpaceDelims = 1 let NERDSpaceDelims = 1
" Powerline
python from powerline.vim import setup as powerline_setup
python powerline_setup()
python del powerline_setup
" gui stuff " gui stuff
set guioptions-=T set guioptions-=T
set guifont=Consolas:h9:cANSI set guifont=Consolas:h9:cANSI

Loading…
Cancel
Save