Move listers to powerline.listers.vim module

This commit is contained in:
ZyX 2014-08-16 12:39:27 +04:00
parent 9ba37d9a62
commit 642ced97bc
4 changed files with 112 additions and 98 deletions

View File

@ -4,6 +4,7 @@
"left": [
{
"type": "segment_list",
"module": "powerline.listers.vim",
"name": "tabbuflister",
"segments": [
{

View File

104
powerline/listers/vim.py Normal file
View File

@ -0,0 +1,104 @@
# vim:fileencoding=utf-8:noet
from __future__ import unicode_literals, absolute_import, division
try:
import vim
except ImportError:
vim = {} # NOQA
from powerline.theme import requires_segment_info
from powerline.bindings.vim import (current_tabpage, list_tabpages)
def tabpage_updated_segment_info(segment_info, tabpage):
segment_info = segment_info.copy()
window = tabpage.window
buffer = window.buffer
segment_info.update(
tabpage=tabpage,
tabnr=tabpage.number,
window=window,
winnr=window.number,
window_id=int(window.vars.get('powerline_window_id', -1)),
buffer=buffer,
bufnr=buffer.number,
)
return segment_info
@requires_segment_info
def tablister(pl, segment_info):
'''List all tab pages in segment_info format
Specifically generates a list of segment info dictionaries with ``window``,
``winnr``, ``window_id``, ``buffer`` and ``bufnr`` keys set to tab-local
ones and additional ``tabpage`` and ``tabnr`` keys.
Sets segment ``mode`` to either ``tab`` (for current tab page) or ``nc``
(for all other tab pages).
Works best with vim-7.4 or later: earlier versions miss tabpage object and
thus window objects are not available as well.
'''
cur_tabpage = current_tabpage()
cur_tabnr = cur_tabpage.number
def add_multiplier(tabpage, dct):
dct['priority_multiplier'] = 1 + (0.001 * abs(tabpage.number - cur_tabnr))
return dct
return [
(
tabpage_updated_segment_info(segment_info, tabpage),
add_multiplier(tabpage, {'mode': ('tab' if tabpage == cur_tabpage else 'nc')})
)
for tabpage in list_tabpages()
]
def buffer_updated_segment_info(segment_info, buffer):
segment_info = segment_info.copy()
segment_info.update(
window=None,
winnr=None,
window_id=None,
buffer=buffer,
bufnr=buffer.number,
)
return segment_info
@requires_segment_info
def bufferlister(pl, segment_info):
'''List all buffers in segment_info format
Specifically generates a list of segment info dictionaries with ``buffer``
and ``bufnr`` keys set to buffer-specific ones, ``window``, ``winnr`` and
``window_id`` keys unset.
Sets segment ``mode`` to either ``buf`` (for current buffer) or ``nc``
(for all other buffers).
'''
cur_buffer = vim.current.buffer
cur_bufnr = cur_buffer.number
def add_multiplier(buffer, dct):
dct['priority_multiplier'] = 1 + (0.001 * abs(buffer.number - cur_bufnr))
return dct
return [
(
buffer_updated_segment_info(segment_info, buffer),
add_multiplier(buffer, {'mode': ('tab' if buffer == cur_buffer else 'nc')})
)
for buffer in vim.buffers
]
@requires_segment_info
def tabbuflister(*args, **kwargs):
if len(list_tabpages()) == 1:
return bufferlister(*args, **kwargs)
else:
return tablister(*args, **kwargs)

View File

@ -4,15 +4,13 @@ from __future__ import unicode_literals, absolute_import, division
import os
import re
try:
import vim
except ImportError:
vim = {} # NOQA
try:
from __builtin__ import xrange as range
except ImportError:
pass
from collections import defaultdict
from powerline.bindings.vim import (vim_get_func, getbufvar, vim_getbufoption,
buffer_name, vim_getwinvar,
@ -23,7 +21,11 @@ from powerline.lib import add_divider_highlight_group
from powerline.lib.vcs import guess, tree_status
from powerline.lib.humanize_bytes import humanize_bytes
from powerline.lib import wraps_saveargs as wraps
from collections import defaultdict
try:
from __builtin__ import xrange as range
except ImportError:
pass
vim_funcs = {
@ -629,96 +631,3 @@ def single_tab(pl, single_text='Bufs', multiple_text='Tabs'):
'contents': multiple_text,
'highlight_group': ['many_tabs'],
}]
def tabpage_updated_segment_info(segment_info, tabpage):
segment_info = segment_info.copy()
window = tabpage.window
buffer = window.buffer
segment_info.update(
tabpage=tabpage,
tabnr=tabpage.number,
window=window,
winnr=window.number,
window_id=int(window.vars.get('powerline_window_id', -1)),
buffer=buffer,
bufnr=buffer.number,
)
return segment_info
@requires_segment_info
def tablister(pl, segment_info):
'''List all tab pages in segment_info format
Specifically generates a list of segment info dictionaries with ``window``,
``winnr``, ``window_id``, ``buffer`` and ``bufnr`` keys set to tab-local
ones and additional ``tabpage`` and ``tabnr`` keys.
Sets segment ``mode`` to either ``tab`` (for current tab page) or ``nc``
(for all other tab pages).
Works best with vim-7.4 or later: earlier versions miss tabpage object and
thus window objects are not available as well.
'''
cur_tabpage = current_tabpage()
cur_tabnr = cur_tabpage.number
def add_multiplier(tabpage, dct):
dct['priority_multiplier'] = 1 + (0.001 * abs(tabpage.number - cur_tabnr))
return dct
return [
(
tabpage_updated_segment_info(segment_info, tabpage),
add_multiplier(tabpage, {'mode': ('tab' if tabpage == cur_tabpage else 'nc')})
)
for tabpage in list_tabpages()
]
def buffer_updated_segment_info(segment_info, buffer):
segment_info = segment_info.copy()
segment_info.update(
window=None,
winnr=None,
window_id=None,
buffer=buffer,
bufnr=buffer.number,
)
return segment_info
@requires_segment_info
def bufferlister(pl, segment_info):
'''List all buffers in segment_info format
Specifically generates a list of segment info dictionaries with ``buffer``
and ``bufnr`` keys set to buffer-specific ones, ``window``, ``winnr`` and
``window_id`` keys unset.
Sets segment ``mode`` to either ``buf`` (for current buffer) or ``nc``
(for all other buffers).
'''
cur_buffer = vim.current.buffer
cur_bufnr = cur_buffer.number
def add_multiplier(buffer, dct):
dct['priority_multiplier'] = 1 + (0.001 * abs(buffer.number - cur_bufnr))
return dct
return [
(
buffer_updated_segment_info(segment_info, buffer),
add_multiplier(buffer, {'mode': ('tab' if buffer == cur_buffer else 'nc')})
)
for buffer in vim.buffers
]
@requires_segment_info
def tabbuflister(*args, **kwargs):
if len(list_tabpages()) == 1:
return bufferlister(*args, **kwargs)
else:
return tablister(*args, **kwargs)