mirror of
https://github.com/powerline/powerline.git
synced 2025-07-26 23:35:04 +02:00
Use vim.vars and *.options if possible
This commit is contained in:
parent
f10729f637
commit
07a130ab25
@ -7,13 +7,7 @@ try:
|
|||||||
except ImportError:
|
except ImportError:
|
||||||
vim = {}
|
vim = {}
|
||||||
|
|
||||||
try:
|
if hasattr(vim, 'bindeval'):
|
||||||
_vim_globals = vim.bindeval('g:')
|
|
||||||
|
|
||||||
def vim_set_global_var(var, val):
|
|
||||||
'''Set a global var in vim using bindeval().'''
|
|
||||||
_vim_globals[var] = val
|
|
||||||
|
|
||||||
def vim_get_func(f, rettype=None):
|
def vim_get_func(f, rettype=None):
|
||||||
'''Return a vim function binding.'''
|
'''Return a vim function binding.'''
|
||||||
try:
|
try:
|
||||||
@ -23,16 +17,9 @@ try:
|
|||||||
return func
|
return func
|
||||||
except vim.error:
|
except vim.error:
|
||||||
return None
|
return None
|
||||||
except AttributeError:
|
else:
|
||||||
import json
|
import json
|
||||||
|
|
||||||
def vim_set_global_var(var, val): # NOQA
|
|
||||||
'''Set a global var in vim using vim.command().
|
|
||||||
|
|
||||||
This is a fallback function for older vim versions.
|
|
||||||
'''
|
|
||||||
vim.command('let g:{0}={1}'.format(var, json.dumps(val)))
|
|
||||||
|
|
||||||
class VimFunc(object):
|
class VimFunc(object):
|
||||||
'''Evaluate a vim function using vim.eval().
|
'''Evaluate a vim function using vim.eval().
|
||||||
|
|
||||||
@ -52,6 +39,36 @@ except AttributeError:
|
|||||||
|
|
||||||
vim_get_func = VimFunc
|
vim_get_func = VimFunc
|
||||||
|
|
||||||
|
|
||||||
|
if hasattr(vim, 'vars'):
|
||||||
|
def vim_getvar(varname):
|
||||||
|
return vim.vars[bytes(varname)]
|
||||||
|
elif hasattr(vim, 'bindeval'):
|
||||||
|
_vim_globals = vim.bindeval('g:')
|
||||||
|
|
||||||
|
def vim_getvar(varname): # NOQA
|
||||||
|
try:
|
||||||
|
return _vim_globals[bytes(varname)]
|
||||||
|
except (KeyError, IndexError):
|
||||||
|
raise KeyError(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)
|
||||||
|
|
||||||
|
if hasattr(vim, 'options'):
|
||||||
|
def vim_getbufoption(info, option):
|
||||||
|
return info['buffer'].options[option]
|
||||||
|
else:
|
||||||
|
def vim_getbufoption(info, option): # NOQA
|
||||||
|
return getbufvar(info['bufnr'], '&' + option)
|
||||||
|
|
||||||
|
|
||||||
if sys.version_info < (3,) or not hasattr(vim, 'bindeval'):
|
if sys.version_info < (3,) or not hasattr(vim, 'bindeval'):
|
||||||
getbufvar = vim_get_func('getbufvar')
|
getbufvar = vim_get_func('getbufvar')
|
||||||
else:
|
else:
|
||||||
|
@ -3,11 +3,11 @@
|
|||||||
from __future__ import absolute_import
|
from __future__ import absolute_import
|
||||||
|
|
||||||
import os
|
import os
|
||||||
from powerline.bindings.vim import getbufvar
|
from powerline.bindings.vim import vim_getbufoption
|
||||||
|
|
||||||
|
|
||||||
def help(matcher_info):
|
def help(matcher_info):
|
||||||
return str(getbufvar(matcher_info['bufnr'], '&buftype')) == 'help'
|
return str(vim_getbufoption(matcher_info, 'buftype')) == 'help'
|
||||||
|
|
||||||
|
|
||||||
def cmdwin(matcher_info):
|
def cmdwin(matcher_info):
|
||||||
@ -16,4 +16,4 @@ def cmdwin(matcher_info):
|
|||||||
|
|
||||||
|
|
||||||
def quickfix(matcher_info):
|
def quickfix(matcher_info):
|
||||||
return str(getbufvar(matcher_info['bufnr'], '&buftype')) == 'quickfix'
|
return str(vim_getbufoption(matcher_info, 'buftype')) == 'quickfix'
|
||||||
|
@ -8,7 +8,7 @@ try:
|
|||||||
except ImportError:
|
except ImportError:
|
||||||
vim = {} # NOQA
|
vim = {} # NOQA
|
||||||
|
|
||||||
from powerline.bindings.vim import vim_get_func, getbufvar
|
from powerline.bindings.vim import vim_get_func, getbufvar, vim_getbufoption
|
||||||
from powerline.theme import requires_segment_info
|
from powerline.theme import requires_segment_info
|
||||||
from powerline.lib import add_divider_highlight_group
|
from powerline.lib import add_divider_highlight_group
|
||||||
from powerline.lib.vcs import guess, tree_status
|
from powerline.lib.vcs import guess, tree_status
|
||||||
@ -94,7 +94,7 @@ def modified_indicator(pl, segment_info, text='+'):
|
|||||||
:param string text:
|
:param string text:
|
||||||
text to display if the current buffer is modified
|
text to display if the current buffer is modified
|
||||||
'''
|
'''
|
||||||
return text if int(getbufvar(segment_info['bufnr'], '&modified')) else None
|
return text if int(vim_getbufoption(segment_info, 'modified')) else None
|
||||||
|
|
||||||
|
|
||||||
@requires_segment_info
|
@requires_segment_info
|
||||||
@ -114,7 +114,7 @@ def readonly_indicator(pl, segment_info, text=''):
|
|||||||
:param string text:
|
:param string text:
|
||||||
text to display if the current buffer is read-only
|
text to display if the current buffer is read-only
|
||||||
'''
|
'''
|
||||||
return text if int(getbufvar(segment_info['bufnr'], '&readonly')) else None
|
return text if int(vim_getbufoption(segment_info, 'readonly')) else None
|
||||||
|
|
||||||
|
|
||||||
@requires_segment_info
|
@requires_segment_info
|
||||||
@ -189,7 +189,7 @@ def file_format(pl, segment_info):
|
|||||||
|
|
||||||
Divider highlight group used: ``background:divider``.
|
Divider highlight group used: ``background:divider``.
|
||||||
'''
|
'''
|
||||||
return getbufvar(segment_info['bufnr'], '&fileformat') or None
|
return vim_getbufoption(segment_info, 'fileformat') or None
|
||||||
|
|
||||||
|
|
||||||
@requires_segment_info
|
@requires_segment_info
|
||||||
@ -201,7 +201,7 @@ def file_encoding(pl, segment_info):
|
|||||||
|
|
||||||
Divider highlight group used: ``background:divider``.
|
Divider highlight group used: ``background:divider``.
|
||||||
'''
|
'''
|
||||||
return getbufvar(segment_info['bufnr'], '&fileencoding') or None
|
return vim_getbufoption(segment_info, 'fileencoding') or None
|
||||||
|
|
||||||
|
|
||||||
@requires_segment_info
|
@requires_segment_info
|
||||||
@ -213,7 +213,7 @@ def file_type(pl, segment_info):
|
|||||||
|
|
||||||
Divider highlight group used: ``background:divider``.
|
Divider highlight group used: ``background:divider``.
|
||||||
'''
|
'''
|
||||||
return getbufvar(segment_info['bufnr'], '&filetype') or None
|
return vim_getbufoption(segment_info, 'filetype') or None
|
||||||
|
|
||||||
|
|
||||||
@requires_segment_info
|
@requires_segment_info
|
||||||
@ -295,7 +295,7 @@ def branch(pl, segment_info, status_colors=False):
|
|||||||
Divider highlight group used: ``branch:divider``.
|
Divider highlight group used: ``branch:divider``.
|
||||||
'''
|
'''
|
||||||
name = segment_info['buffer'].name
|
name = segment_info['buffer'].name
|
||||||
skip = not (name and (not getbufvar(segment_info['bufnr'], '&buftype')))
|
skip = not (name and (not vim_getbufoption(segment_info, 'buftype')))
|
||||||
if not skip:
|
if not skip:
|
||||||
repo = guess(path=name)
|
repo = guess(path=name)
|
||||||
if repo is not None:
|
if repo is not None:
|
||||||
@ -317,7 +317,7 @@ def file_vcs_status(pl, segment_info):
|
|||||||
Highlight groups used: ``file_vcs_status``.
|
Highlight groups used: ``file_vcs_status``.
|
||||||
'''
|
'''
|
||||||
name = segment_info['buffer'].name
|
name = segment_info['buffer'].name
|
||||||
skip = not (name and (not getbufvar(segment_info['bufnr'], '&buftype')))
|
skip = not (name and (not vim_getbufoption(segment_info, 'buftype')))
|
||||||
if not skip:
|
if not skip:
|
||||||
repo = guess(path=name)
|
repo = guess(path=name)
|
||||||
if repo is not None:
|
if repo is not None:
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
from __future__ import absolute_import
|
from __future__ import absolute_import
|
||||||
|
|
||||||
from powerline.bindings.vim import vim_get_func
|
from powerline.bindings.vim import vim_get_func, vim_getvar
|
||||||
from powerline import Powerline
|
from powerline import Powerline
|
||||||
from powerline.lib import mergedicts
|
from powerline.lib import mergedicts
|
||||||
from powerline.matcher import gen_matcher_getter
|
from powerline.matcher import gen_matcher_getter
|
||||||
@ -13,18 +13,11 @@ if not hasattr(vim, 'bindeval'):
|
|||||||
import json
|
import json
|
||||||
|
|
||||||
|
|
||||||
vim_exists = vim_get_func('exists', rettype=int)
|
|
||||||
vim_getwinvar = vim_get_func('getwinvar')
|
|
||||||
vim_setwinvar = vim_get_func('setwinvar')
|
|
||||||
|
|
||||||
|
|
||||||
def _override_from(config, override_varname):
|
def _override_from(config, override_varname):
|
||||||
if vim_exists(override_varname):
|
try:
|
||||||
# FIXME vim.eval has problem with numeric types, vim.bindeval may be
|
overrides = vim_getvar(override_varname)
|
||||||
# absent (and requires converting values to python built-in types),
|
except KeyError:
|
||||||
# vim.eval with typed call like the one I implemented in frawor is slow.
|
return config
|
||||||
# Maybe eval(vime.eval('string({0})'.format(override_varname)))?
|
|
||||||
overrides = vim.eval(override_varname)
|
|
||||||
mergedicts(config, overrides)
|
mergedicts(config, overrides)
|
||||||
return config
|
return config
|
||||||
|
|
||||||
@ -64,14 +57,14 @@ class VimPowerline(Powerline):
|
|||||||
return True
|
return True
|
||||||
|
|
||||||
def load_main_config(self):
|
def load_main_config(self):
|
||||||
return _override_from(super(VimPowerline, self).load_main_config(), 'g:powerline_config_overrides')
|
return _override_from(super(VimPowerline, self).load_main_config(), 'powerline_config_overrides')
|
||||||
|
|
||||||
def load_theme_config(self, name):
|
def load_theme_config(self, name):
|
||||||
# Note: themes with non-[a-zA-Z0-9_] names are impossible to override
|
# Note: themes with non-[a-zA-Z0-9_] names are impossible to override
|
||||||
# (though as far as I know exists() won’t throw). Won’t fix, use proper
|
# (though as far as I know exists() won’t throw). Won’t fix, use proper
|
||||||
# theme names.
|
# theme names.
|
||||||
return _override_from(super(VimPowerline, self).load_theme_config(name),
|
return _override_from(super(VimPowerline, self).load_theme_config(name),
|
||||||
'g:powerline_theme_overrides__' + name)
|
'powerline_theme_overrides__' + name)
|
||||||
|
|
||||||
def get_local_themes(self, local_themes):
|
def get_local_themes(self, local_themes):
|
||||||
if not local_themes:
|
if not local_themes:
|
||||||
@ -82,9 +75,9 @@ class VimPowerline(Powerline):
|
|||||||
for key, val in local_themes.items()))
|
for key, val in local_themes.items()))
|
||||||
|
|
||||||
def get_config_paths(self):
|
def get_config_paths(self):
|
||||||
if vim_exists('g:powerline_config_path'):
|
try:
|
||||||
return [vim.eval('g:powerline_config_path')]
|
return [vim_getvar('powerline_config_path')]
|
||||||
else:
|
except KeyError:
|
||||||
return super(VimPowerline, self).get_config_paths()
|
return super(VimPowerline, self).get_config_paths()
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
@ -120,19 +113,22 @@ class VimPowerline(Powerline):
|
|||||||
r = (window, curwindow_id, window.number)
|
r = (window, curwindow_id, window.number)
|
||||||
return r
|
return r
|
||||||
else:
|
else:
|
||||||
|
_vim_getwinvar = staticmethod(vim_get_func('getwinvar'))
|
||||||
|
_vim_setwinvar = staticmethod(vim_get_func('setwinvar'))
|
||||||
|
|
||||||
def win_idx(self, window_id): # NOQA
|
def win_idx(self, window_id): # NOQA
|
||||||
r = None
|
r = None
|
||||||
for winnr, window in zip(count(1), vim.windows):
|
for winnr, window in zip(count(1), vim.windows):
|
||||||
curwindow_id = vim_getwinvar(winnr, 'powerline_window_id')
|
curwindow_id = self._vim_getwinvar(winnr, 'powerline_window_id')
|
||||||
if curwindow_id:
|
if curwindow_id:
|
||||||
curwindow_id = int(curwindow_id)
|
curwindow_id = int(curwindow_id)
|
||||||
else:
|
else:
|
||||||
curwindow_id = self.last_window_id
|
curwindow_id = self.last_window_id
|
||||||
self.last_window_id += 1
|
self.last_window_id += 1
|
||||||
vim_setwinvar(winnr, 'powerline_window_id', curwindow_id)
|
self._vim_setwinvar(winnr, 'powerline_window_id', curwindow_id)
|
||||||
statusline = self.window_statusline.format(curwindow_id)
|
statusline = self.window_statusline.format(curwindow_id)
|
||||||
if vim_getwinvar(winnr, '&statusline') != statusline:
|
if self._vim_getwinvar(winnr, '&statusline') != statusline:
|
||||||
vim_setwinvar(winnr, '&statusline', statusline)
|
self._vim_setwinvar(winnr, '&statusline', statusline)
|
||||||
if curwindow_id == window_id if window_id else window is vim.current.window:
|
if curwindow_id == window_id if window_id else window is vim.current.window:
|
||||||
assert r is None, "Non-unique window ID"
|
assert r is None, "Non-unique window ID"
|
||||||
r = (window, curwindow_id, winnr)
|
r = (window, curwindow_id, winnr)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user