Merge remote-tracking branch 'zyx-i/less-viml' into develop

This commit is contained in:
Kim Silkebækken 2013-05-21 10:10:11 +02:00
commit 5710b7b37f
11 changed files with 307 additions and 192 deletions

View File

@ -91,8 +91,19 @@ Usage
Vim statusline Vim statusline
-------------- --------------
Add the following line to your :file:`vimrc`, where ``{repository_root}`` is the If installed using pip just use
absolute path to your Powerline installation directory:
.. code-block:: vim
python from powerline.vim import setup as powerline_setup
python powerline_setup()
python del powerline_setup
(replace ``python`` with ``python3`` if appropriate).
If you just cloned the repository add the following line to your :file:`vimrc`,
where ``{repository_root}`` is the absolute path to your Powerline installation
directory:
.. code-block:: vim .. code-block:: vim
@ -109,6 +120,9 @@ hand: ``powerline`` is installed and run just like any other plugin using
call vam#ActivateAddons(['powerline']) call vam#ActivateAddons(['powerline'])
Note: when using Gentoo ebuild you need to specify ``USE=vim`` to enable
powerline.
Shell prompts Shell prompts
------------- -------------

View File

@ -376,7 +376,10 @@ class Powerline(object):
current application. current application.
''' '''
self.shutdown_event.set() self.shutdown_event.set()
self.renderer.shutdown() try:
self.renderer.shutdown()
except AttributeError:
pass
functions = ( functions = (
self.on_main_change, self.on_main_change,
self.on_colors_change, self.on_colors_change,

View File

@ -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[str(varname)]
elif hasattr(vim, 'bindeval'):
_vim_globals = vim.bindeval('g:')
def vim_getvar(varname): # NOQA
try:
return _vim_globals[str(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:
@ -62,3 +79,21 @@ else:
if type(r) is bytes: if type(r) is bytes:
return r.decode('utf-8') return r.decode('utf-8')
return r 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', '')
+ '"')
environ = VimEnviron()

View File

@ -15,18 +15,18 @@ if ! has('python') && ! has('python3')
finish finish
endif endif
let s:powerline_pycmd = substitute(get(g:, 'powerline_pycmd', has('python') ? 'py' : 'py3'), let s:pycmd = substitute(get(g:, 'powerline_pycmd', has('python') ? 'py' : 'py3'),
\'\v^(py)%[thon](3?)$', '\1\2', '') \'\v^(py)%[thon](3?)$', '\1\2', '')
let s:powerline_pyeval = get(g:, 'powerline_pyeval', s:powerline_pycmd.'eval') let s:pyeval = get(g:, 'powerline_pyeval', s:pycmd.'eval')
let s:import_cmd = 'from powerline.vim import VimPowerline' let s:import_cmd = 'from powerline.vim import setup as powerline_setup'
try try
exec s:powerline_pycmd "try:\n" execute s:pycmd "try:\n"
\ ." ".s:import_cmd."\n" \ ." ".s:import_cmd."\n"
\ ."except ImportError:\n" \ ."except ImportError:\n"
\ ." import sys, vim\n" \ ." import sys, vim\n"
\ ." sys.path.append(vim.eval('expand(\"<sfile>:h:h:h:h:h\")'))\n" \ ." sys.path.append(vim.eval('expand(\"<sfile>:h:h:h:h:h\")'))\n"
\ ." ".s:import_cmd \ ." ".s:import_cmd
let s:launched = 1 let s:launched = 1
finally finally
if !exists('s:launched') if !exists('s:launched')
@ -40,56 +40,5 @@ finally
endif endif
endtry endtry
if !get(g:, 'powerline_debugging_pyeval') && exists('*'. s:powerline_pyeval) execute s:pycmd 'powerline_setup(pyeval=vim.eval("s:pyeval"), pycmd=vim.eval("s:pycmd"))'
let s:pyeval = function(s:powerline_pyeval) execute s:pycmd 'del powerline_setup'
else
exec s:powerline_pycmd 'import json, vim'
exec "function! s:pyeval(e)\n".
\ s:powerline_pycmd." vim.command('return ' + json.dumps(eval(vim.eval('a:e'))))\n".
\"endfunction"
endif
let s:last_window_id = 0
function! s:GetWinID(winnr)
let r = getwinvar(a:winnr, 'window_id')
if empty(r)
let r = s:last_window_id
let s:last_window_id += 1
call setwinvar(a:winnr, 'window_id', r)
endif
" Without this condition it triggers unneeded statusline redraw
if getwinvar(a:winnr, '&statusline') isnot# '%!Powerline('.r.')'
call setwinvar(a:winnr, '&statusline', '%!Powerline('.r.')')
endif
return r
endfunction
function! Powerline(window_id)
let winidx = index(map(range(1, winnr('$')), 's:GetWinID(v:val)'), a:window_id)
let current = w:window_id is# a:window_id
return s:pyeval('powerline.render('. a:window_id .', '. winidx .', '. current .')')
endfunction
function! PowerlineNew()
call map(range(1, winnr('$')), 's:GetWinID(v:val)')
endfunction
function! PowerlineRegisterCachePurgerEvent(event)
exec s:powerline_pycmd 'from powerline.segments.vim import launchevent as powerline_launchevent'
augroup Powerline
exec 'autocmd' a:event '*' s:powerline_pycmd.' powerline_launchevent("'.a:event.'")'
augroup END
endfunction
augroup Powerline
autocmd! ColorScheme * :exec s:powerline_pycmd 'powerline.reset_highlight()'
autocmd! VimEnter * :redrawstatus!
autocmd! VimLeavePre * :exec s:powerline_pycmd 'powerline.shutdown()'
augroup END
exec s:powerline_pycmd 'powerline = VimPowerline()'
exec s:powerline_pycmd 'del VimPowerline'
" Is immediately changed when PowerlineNew() function is run. Good for global
" value.
set statusline=%!PowerlineNew()
call PowerlineNew()

View File

@ -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'

View File

@ -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, environ
from powerline.renderer import Renderer from powerline.renderer import Renderer
from powerline.colorscheme import ATTR_BOLD, ATTR_ITALIC, ATTR_UNDERLINE from powerline.colorscheme import ATTR_BOLD, ATTR_ITALIC, ATTR_UNDERLINE
from powerline.theme import Theme from powerline.theme import Theme
@ -68,17 +68,19 @@ class VimRenderer(Renderer):
def get_segment_info(self, segment_info): def get_segment_info(self, segment_info):
return segment_info or self.segment_info return segment_info or self.segment_info
def render(self, window_id, winidx, current): def render(self, window, window_id, winnr):
'''Render all segments.''' '''Render all segments.'''
if current: if window is vim.current.window:
mode = vim_mode(1) mode = vim_mode(1)
mode = mode_translations.get(mode, mode) mode = mode_translations.get(mode, mode)
else: else:
mode = 'nc' mode = 'nc'
segment_info = { segment_info = {
'window': vim.windows[winidx], 'window': window,
'mode': mode, 'mode': mode,
'window_id': window_id, 'window_id': window_id,
'winnr': winnr,
'environ': environ,
} }
segment_info['buffer'] = segment_info['window'].buffer segment_info['buffer'] = segment_info['window'].buffer
segment_info['bufnr'] = segment_info['buffer'].number segment_info['bufnr'] = segment_info['buffer'].number

View File

@ -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
@ -51,29 +51,6 @@ bufeventfuncs = defaultdict(lambda: [])
defined_events = set() defined_events = set()
def purgeonevents_reg(func, events, is_buffer_event=False):
if is_buffer_event:
cureventfuncs = bufeventfuncs
else:
cureventfuncs = eventfuncs
for event in events:
if event not in defined_events:
vim.eval('PowerlineRegisterCachePurgerEvent("' + event + '")')
defined_events.add(event)
cureventfuncs[event].append(func)
def launchevent(event):
global eventfuncs
global bufeventfuncs
for func in eventfuncs[event]:
func()
if bufeventfuncs[event]:
buffer = vim.buffers[int(vim_funcs['expand']('<abuf>')) - 1]
for func in bufeventfuncs[event]:
func(buffer)
# TODO Remove cache when needed # TODO Remove cache when needed
def window_cached(func): def window_cached(func):
cache = {} cache = {}
@ -117,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
@ -137,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
@ -212,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
@ -224,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
@ -236,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
@ -318,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:
@ -340,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:

View File

@ -28,7 +28,7 @@ class ShellPowerline(Powerline):
def load_theme_config(self, name): def load_theme_config(self, name):
r = super(ShellPowerline, self).load_theme_config(name) r = super(ShellPowerline, self).load_theme_config(name)
if name in self.theme_option: if self.theme_option and name in self.theme_option:
mergedicts(r, self.theme_option[name]) mergedicts(r, self.theme_option[name])
return r return r

View File

@ -2,30 +2,31 @@
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
import vim import vim
from itertools import count
if not hasattr(vim, 'bindeval'):
vim_exists = vim_get_func('exists', rettype=int) import json
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)))? mergedicts(config, overrides)
overrides = vim.eval(override_varname)
mergedicts(config, overrides)
return config return config
class VimPowerline(Powerline): class VimPowerline(Powerline):
def __init__(self): def __init__(self, pyeval='PowerlinePyeval'):
super(VimPowerline, self).__init__('vim') super(VimPowerline, self).__init__('vim')
self.last_window_id = 1
self.window_statusline = '%!' + pyeval + '(\'powerline.statusline({0})\')'
def add_local_theme(self, key, config): def add_local_theme(self, key, config):
'''Add local themes at runtime (during vim session). '''Add local themes at runtime (during vim session).
@ -56,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() wont throw). Wont fix, use proper # (though as far as I know exists() wont throw). Wont 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:
@ -74,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
@ -93,3 +94,97 @@ class VimPowerline(Powerline):
# fine to ignore it: no renderer == no colors to reset == no need to # fine to ignore it: no renderer == no colors to reset == no need to
# do anything. # do anything.
pass pass
if all((hasattr(vim.current.window, attr) for attr in ('options', 'vars', 'number'))):
def win_idx(self, window_id):
r = None
for window in vim.windows:
try:
curwindow_id = window.vars['powerline_window_id']
except KeyError:
curwindow_id = self.last_window_id
self.last_window_id += 1
window.vars['powerline_window_id'] = curwindow_id
statusline = self.window_statusline.format(curwindow_id)
if window.options['statusline'] != statusline:
window.options['statusline'] = statusline
if curwindow_id == window_id if window_id else window is vim.current.window:
assert r is None, "Non-unique window ID"
r = (window, curwindow_id, window.number)
return r
else:
_vim_getwinvar = staticmethod(vim_get_func('getwinvar'))
_vim_setwinvar = staticmethod(vim_get_func('setwinvar'))
def win_idx(self, window_id): # NOQA
r = None
for winnr, window in zip(count(1), vim.windows):
curwindow_id = self._vim_getwinvar(winnr, 'powerline_window_id')
if curwindow_id:
curwindow_id = int(curwindow_id)
else:
curwindow_id = self.last_window_id
self.last_window_id += 1
self._vim_setwinvar(winnr, 'powerline_window_id', curwindow_id)
statusline = self.window_statusline.format(curwindow_id)
if self._vim_getwinvar(winnr, '&statusline') != statusline:
self._vim_setwinvar(winnr, '&statusline', statusline)
if curwindow_id == window_id if window_id else window is vim.current.window:
assert r is None, "Non-unique window ID"
r = (window, curwindow_id, winnr)
return r
def statusline(self, window_id):
window, window_id, winnr = self.win_idx(window_id) or (None, None, None)
if not window:
return 'No window {0}'.format(window_id)
return self.render(window, window_id, winnr)
def new_window(self):
window, window_id, winnr = self.win_idx(None)
return self.render(window, window_id, winnr)
if not hasattr(vim, 'bindeval'):
# Method for PowerlinePyeval function. Is here to reduce the number of
# requirements to __main__ globals to just one powerline object
# (previously it required as well vim and json)
@staticmethod
def pyeval():
import __main__
vim.command('return ' + json.dumps(eval(vim.eval('a:e'),
__main__.__dict__)))
def setup(pyeval=None, pycmd=None):
import sys
import __main__
if not pyeval:
pyeval = 'pyeval' if sys.version_info < (3,) else 'py3eval'
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'):
vim.command(('''
function! PowerlinePyeval(e)
{pycmd} powerline.pyeval()
endfunction
''').format(pycmd=pycmd))
pyeval = 'PowerlinePyeval'
powerline = VimPowerline(pyeval)
__main__.powerline = powerline
# Cannot have this in one line due to weird newline handling (in :execute
# context newline is considered part of the command in just the same cases
# when bar is considered part of the command (unless defining function
# inside :execute)). vim.command is :execute equivalent regarding this case.
vim.command('augroup Powerline')
vim.command(' autocmd! ColorScheme * :{pycmd} powerline.reset_highlight()'.format(pycmd=pycmd))
vim.command(' autocmd! VimEnter * :redrawstatus!')
vim.command(' autocmd! VimLeavePre * :{pycmd} powerline.shutdown()'.format(pycmd=pycmd))
vim.command('augroup END')
# Is immediately changed after new_window function is run. Good for global
# value.
vim.command('set statusline=%!{pyeval}(\'powerline.new_window()\')'.format(pyeval=pyeval))

View File

@ -19,36 +19,42 @@ class TestConfig(TestCase):
def test_vim(self): def test_vim(self):
from powerline.vim import VimPowerline from powerline.vim import VimPowerline
cfg_path = os.path.join(os.path.dirname(os.path.dirname(__file__)), 'powerline', 'config_files') cfg_path = os.path.join(os.path.dirname(os.path.dirname(__file__)), 'powerline', 'config_files')
buffers = ((('bufoptions',), {'buftype': 'help'}), (('buffer', '[Command Line]'), {}), (('bufoptions',), {'buftype': 'quickfix'})) buffers = ((('bufoptions',), {'buftype': 'help'}), (('bufname', '[Command Line]'), {}), (('bufoptions',), {'buftype': 'quickfix'}))
with open(os.path.join(cfg_path, 'config.json'), 'r') as f: with open(os.path.join(cfg_path, 'config.json'), 'r') as f:
self.assertEqual(len(buffers), len(json.load(f)['ext']['vim']['local_themes'])) self.assertEqual(len(buffers), len(json.load(f)['ext']['vim']['local_themes']))
outputs = {} outputs = {}
i = 0 i = 0
mode = None
with VimPowerline() as powerline: with vim_module._with('split'):
def check_output(*args): with VimPowerline() as powerline:
out = powerline.render(*args + (0 if mode == 'nc' else 1,)) def check_output(mode, args, kwargs):
if out in outputs: if mode == 'nc':
self.fail('Duplicate in set #{0} for mode {1!r} (previously defined in set #{2} for mode {3!r})'.format(i, mode, *outputs[out])) window = vim_module.windows[0]
outputs[out] = (i, mode) window_id = 2
else:
vim_module._start_mode(mode)
window = vim_module.current.window
window_id = 1
winnr = window.number
out = powerline.render(window, window_id, winnr)
if out in outputs:
self.fail('Duplicate in set #{0} ({1}) for mode {2!r} (previously defined in set #{3} ({4!r}) for mode {5!r})'.format(i, (args, kwargs), mode, *outputs[out]))
outputs[out] = (i, (args, kwargs), mode)
with vim_module._with('buffer', 'foo.txt'): with vim_module._with('bufname', '/tmp/foo.txt'):
with vim_module._with('globals', powerline_config_path=cfg_path): with vim_module._with('globals', powerline_config_path=cfg_path):
exclude = set(('no', 'v', 'V', VBLOCK, 's', 'S', SBLOCK, 'R', 'Rv', 'c', 'cv', 'ce', 'r', 'rm', 'r?', '!')) exclude = set(('no', 'v', 'V', VBLOCK, 's', 'S', SBLOCK, 'R', 'Rv', 'c', 'cv', 'ce', 'r', 'rm', 'r?', '!'))
try: try:
for mode in ['n', 'nc', 'no', 'v', 'V', VBLOCK, 's', 'S', SBLOCK, 'i', 'R', 'Rv', 'c', 'cv', 'ce', 'r', 'rm', 'r?', '!']: for mode in ['n', 'nc', 'no', 'v', 'V', VBLOCK, 's', 'S', SBLOCK, 'i', 'R', 'Rv', 'c', 'cv', 'ce', 'r', 'rm', 'r?', '!']:
if mode != 'nc': check_output(mode, None, None)
vim_module._start_mode(mode) for args, kwargs in buffers:
check_output(1, 0) i += 1
for args, kwargs in buffers: if mode in exclude:
i += 1 continue
if mode in exclude: with vim_module._with(*args, **kwargs):
continue check_output(mode, args, kwargs)
with vim_module._with(*args, **kwargs): finally:
check_output(1, 0) vim_module._start_mode('n')
finally:
vim_module._start_mode('n')
def test_tmux(self): def test_tmux(self):
from powerline.segments import common from powerline.segments import common

View File

@ -1,10 +1,10 @@
# vim:fileencoding=utf-8:noet # vim:fileencoding=utf-8:noet
_log = [] _log = []
_g = {} vars = {}
_window = 0 _window = 0
_mode = 'n' _mode = 'n'
_buf_purge_events = set() _buf_purge_events = set()
_options = { options = {
'paste': 0, 'paste': 0,
'ambiwidth': 'single', 'ambiwidth': 'single',
} }
@ -151,7 +151,7 @@ def command(cmd):
if cmd.startswith('let g:'): if cmd.startswith('let g:'):
import re import re
varname, value = re.compile(r'^let g:(\w+)\s*=\s*(.*)').match(cmd).groups() varname, value = re.compile(r'^let g:(\w+)\s*=\s*(.*)').match(cmd).groups()
_g[varname] = value vars[varname] = value
elif cmd.startswith('hi '): elif cmd.startswith('hi '):
sp = cmd.split() sp = cmd.split()
_highlights[sp[1]] = sp[2:] _highlights[sp[1]] = sp[2:]
@ -162,9 +162,9 @@ def command(cmd):
@_vim @_vim
def eval(expr): def eval(expr):
if expr.startswith('g:'): if expr.startswith('g:'):
return _g[expr[2:]] return vars[expr[2:]]
elif expr.startswith('&'): elif expr.startswith('&'):
return _options[expr[1:]] return options[expr[1:]]
elif expr.startswith('PowerlineRegisterCachePurgerEvent'): elif expr.startswith('PowerlineRegisterCachePurgerEvent'):
_buf_purge_events.add(expr[expr.find('"') + 1:expr.rfind('"') - 1]) _buf_purge_events.add(expr[expr.find('"') + 1:expr.rfind('"') - 1])
return "0" return "0"
@ -174,7 +174,7 @@ def eval(expr):
@_vim @_vim
def bindeval(expr): def bindeval(expr):
if expr == 'g:': if expr == 'g:':
return _g return vars
import re import re
match = re.compile(r'^function\("([^"\\]+)"\)$').match(expr) match = re.compile(r'^function\("([^"\\]+)"\)$').match(expr)
if match: if match:
@ -201,10 +201,10 @@ def _emul_getbufvar(bufnr, varname):
if bufnr not in buffers: if bufnr not in buffers:
return '' return ''
try: try:
return _buf_options[bufnr][varname[1:]] return buffers[bufnr].options[varname[1:]]
except KeyError: except KeyError:
try: try:
return _options[varname[1:]] return options[varname[1:]]
except KeyError: except KeyError:
return '' return ''
raise NotImplementedError raise NotImplementedError
@ -213,12 +213,12 @@ def _emul_getbufvar(bufnr, varname):
@_vim @_vim
@_str_func @_str_func
def _emul_getwinvar(winnr, varname): def _emul_getwinvar(winnr, varname):
return _win_scopes[winnr][varname] return windows[winnr].vars[varname]
@_vim @_vim
def _emul_setwinvar(winnr, varname, value): def _emul_setwinvar(winnr, varname, value):
_win_scopes[winnr][varname] = value windows[winnr].vars[varname] = value
@_vim @_vim
@ -262,7 +262,7 @@ def _emul_bufnr(expr):
@_vim @_vim
def _emul_exists(varname): def _emul_exists(varname):
if varname.startswith('g:'): if varname.startswith('g:'):
return varname[2:] in _g return varname[2:] in vars
raise NotImplementedError raise NotImplementedError
@ -273,10 +273,9 @@ def _emul_line2byte(line):
return sum((len(s) for s in buflines)) + 1 return sum((len(s) for s in buflines)) + 1
raise NotImplementedError raise NotImplementedError
_window_ids = [None] _window_ids = [None]
_window_id = 0 _window_id = 0
_win_scopes = [None]
_win_options = [None]
class _Window(object): class _Window(object):
@ -284,6 +283,7 @@ class _Window(object):
global _window_id global _window_id
self.cursor = cursor self.cursor = cursor
self.width = width self.width = width
self.number = len(windows) + 1
if buffer: if buffer:
if type(buffer) is _Buffer: if type(buffer) is _Buffer:
self.buffer = buffer self.buffer = buffer
@ -294,15 +294,13 @@ class _Window(object):
windows.append(self) windows.append(self)
_window_id += 1 _window_id += 1
_window_ids.append(_window_id) _window_ids.append(_window_id)
_win_scopes.append({}) self.options = {}
_win_options.append({}) self.vars = {}
def __repr__(self): def __repr__(self):
return '<window ' + str(windows.index(self)) + '>' return '<window ' + str(windows.index(self)) + '>'
_buf_scopes = {}
_buf_options = {}
_buf_lines = {} _buf_lines = {}
_undostate = {} _undostate = {}
_undo_written = {} _undo_written = {}
@ -316,8 +314,8 @@ class _Buffer(object):
bufnr = _last_bufnr bufnr = _last_bufnr
self.number = bufnr self.number = bufnr
self.name = os.path.abspath(name) if name else None self.name = os.path.abspath(name) if name else None
_buf_scopes[bufnr] = {} self.vars = {}
_buf_options[bufnr] = { self.options = {
'modified': 0, 'modified': 0,
'readonly': 0, 'readonly': 0,
'fileformat': 'unix', 'fileformat': 'unix',
@ -336,13 +334,13 @@ class _Buffer(object):
return _buf_lines[self.number][line] return _buf_lines[self.number][line]
def __setitem__(self, line, value): def __setitem__(self, line, value):
_buf_options[self.number]['modified'] = 1 self.options['modified'] = 1
_buf_lines[self.number][line] = value _buf_lines[self.number][line] = value
from copy import copy from copy import copy
_undostate[self.number].append(copy(_buf_lines[self.number])) _undostate[self.number].append(copy(_buf_lines[self.number]))
def __setslice__(self, *args): def __setslice__(self, *args):
_buf_options[self.number]['modified'] = 1 self.options['modified'] = 1
_buf_lines[self.number].__setslice__(*args) _buf_lines[self.number].__setslice__(*args)
from copy import copy from copy import copy
_undostate[self.number].append(copy(_buf_lines[self.number])) _undostate[self.number].append(copy(_buf_lines[self.number]))
@ -358,12 +356,12 @@ class _Buffer(object):
def __del__(self): def __del__(self):
bufnr = self.number bufnr = self.number
if _buf_options: if _buf_lines:
_buf_options.pop(bufnr)
_buf_lines.pop(bufnr) _buf_lines.pop(bufnr)
if _undostate:
_undostate.pop(bufnr) _undostate.pop(bufnr)
if _undo_written:
_undo_written.pop(bufnr) _undo_written.pop(bufnr)
_buf_scopes.pop(bufnr)
class _Current(object): class _Current(object):
@ -371,6 +369,10 @@ class _Current(object):
def buffer(self): def buffer(self):
return buffers[_buffer()] return buffers[_buffer()]
@property
def window(self):
return windows[_window - 1]
current = _Current() current = _Current()
@ -431,8 +433,9 @@ def _undo():
return return
_undostate[_buffer()].pop(-1) _undostate[_buffer()].pop(-1)
_buf_lines[_buffer()] = _undostate[_buffer()][-1] _buf_lines[_buffer()] = _undostate[_buffer()][-1]
buf = current.buffer
if _undo_written[_buffer()] == len(_undostate[_buffer()]): if _undo_written[_buffer()] == len(_undostate[_buffer()]):
_buf_options[_buffer()]['modified'] = 0 buf.options['modified'] = 0
@_vim @_vim
@ -453,11 +456,16 @@ def _new(name=None):
_window = len(windows) _window = len(windows)
@_vim
def _split():
global _window
_Window(buffer=buffers[_buffer()])
_window = len(windows)
@_vim @_vim
def _del_window(winnr): def _del_window(winnr):
win = windows.pop(winnr - 1) win = windows.pop(winnr - 1)
_win_scopes.pop(winnr)
_win_options.pop(winnr)
_window_ids.pop(winnr) _window_ids.pop(winnr)
return win return win
@ -513,7 +521,7 @@ def _get_buffer():
@_vim @_vim
def _set_bufoption(option, value, bufnr=None): def _set_bufoption(option, value, bufnr=None):
_buf_options[bufnr or _buffer()][option] = value buffers[bufnr or _buffer()].options[option] = value
if option == 'filetype': if option == 'filetype':
_launch_event('FileType') _launch_event('FileType')
@ -553,11 +561,11 @@ class _WithBufOption(object):
self.new = new self.new = new
def __enter__(self): def __enter__(self):
self.bufnr = _buffer() self.buffer = buffers[_buffer()]
self.old = _set_dict(_buf_options[self.bufnr], self.new, _set_bufoption)[0] self.old = _set_dict(self.buffer.options, self.new, _set_bufoption)[0]
def __exit__(self, *args): def __exit__(self, *args):
_buf_options[self.bufnr].update(self.old) self.buffer.options.update(self.old)
class _WithMode(object): class _WithMode(object):
@ -587,15 +595,41 @@ class _WithDict(object):
self.d.pop(k) self.d.pop(k)
class _WithSplit(object):
def __enter__(self):
_split()
def __exit__(self, *args):
_close(2, wipe=False)
class _WithBufName(object):
def __init__(self, new):
self.new = new
def __enter__(self):
buffer = buffers[_buffer()]
self.buffer = buffer
self.old = buffer.name
buffer.name = self.new
def __exit__(self, *args):
self.buffer.name = self.old
@_vim @_vim
def _with(key, *args, **kwargs): def _with(key, *args, **kwargs):
if key == 'buffer': if key == 'buffer':
return _WithNewBuffer(_edit, *args, **kwargs) return _WithNewBuffer(_edit, *args, **kwargs)
elif key == 'bufname':
return _WithBufName(*args, **kwargs)
elif key == 'mode': elif key == 'mode':
return _WithMode(*args, **kwargs) return _WithMode(*args, **kwargs)
elif key == 'bufoptions': elif key == 'bufoptions':
return _WithBufOption(**kwargs) return _WithBufOption(**kwargs)
elif key == 'options': elif key == 'options':
return _WithDict(_options, **kwargs) return _WithDict(options, **kwargs)
elif key == 'globals': elif key == 'globals':
return _WithDict(_g, **kwargs) return _WithDict(vars, **kwargs)
elif key == 'split':
return _WithSplit()