Add initial vim binding functions and segments
Each segment is a function that can accept parameters like the branch or readonly symbol for overriding the defaults.
This commit is contained in:
parent
3be7dc2a40
commit
0f5a497834
|
@ -0,0 +1,47 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
|
||||
import vim
|
||||
|
||||
try:
|
||||
_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):
|
||||
'''Return a vim function binding.
|
||||
'''
|
||||
try:
|
||||
return vim.bindeval('function("' + f + '")')
|
||||
except vim.error:
|
||||
return None
|
||||
except AttributeError:
|
||||
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):
|
||||
'''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
|
|
@ -0,0 +1,5 @@
|
|||
# flake8: noqa
|
||||
|
||||
from core import (mode, modified_indicator, paste_indicator,
|
||||
readonly_indicator, branch, file_directory, file_name, file_format,
|
||||
file_encoding, file_type, line_percent, line_current, col_current)
|
|
@ -0,0 +1,144 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
|
||||
import vim
|
||||
|
||||
from bindings import vim_get_func
|
||||
|
||||
vim_funcs = {
|
||||
'col': vim_get_func('col', rettype=int),
|
||||
'expand': vim_get_func('expand'),
|
||||
'line': vim_get_func('line', rettype=int),
|
||||
'mode': vim_get_func('mode'),
|
||||
'vcs': {
|
||||
'fugitive': vim_get_func('fugitive#head'),
|
||||
},
|
||||
}
|
||||
|
||||
vim_modes = {
|
||||
'n': 'NORMAL',
|
||||
'no': 'N·OPER',
|
||||
'v': 'VISUAL',
|
||||
'V': 'V·LINE',
|
||||
'': 'V·BLCK',
|
||||
's': 'SELECT',
|
||||
'S': 'S·LINE',
|
||||
'': 'S·BLCK',
|
||||
'i': 'INSERT',
|
||||
'R': 'REPLACE',
|
||||
'Rv': 'V·RPLCE',
|
||||
'c': 'COMMND',
|
||||
'cv': 'VIM EX',
|
||||
'ce': 'EX',
|
||||
'r': 'PROMPT',
|
||||
'rm': 'MORE',
|
||||
'r?': 'CONFIRM',
|
||||
'!': 'SHELL',
|
||||
}
|
||||
|
||||
|
||||
def mode(override=None):
|
||||
'''Return the current vim mode.
|
||||
|
||||
This function returns a tuple with the shorthand mode and the mode expanded
|
||||
into a descriptive string. The longer string can be overridden by providing
|
||||
a dict with the mode and the new string::
|
||||
|
||||
mode = mode({ 'n': 'NORM' })
|
||||
'''
|
||||
mode = vim_funcs['mode']()
|
||||
|
||||
try:
|
||||
return override[mode]
|
||||
except TypeError:
|
||||
return vim_modes[mode]
|
||||
|
||||
|
||||
def modified_indicator(text=u'+'):
|
||||
'''Return a file modified indicator.
|
||||
'''
|
||||
return text if int(vim.eval('&modified')) else None
|
||||
|
||||
|
||||
def paste_indicator(text='PASTE'):
|
||||
'''Return a paste mode indicator.
|
||||
'''
|
||||
return text if int(vim.eval('&paste')) else None
|
||||
|
||||
|
||||
def readonly_indicator(text=u'⭤'):
|
||||
'''Return a read-only indicator.
|
||||
'''
|
||||
return text if int(vim.eval('&readonly')) else None
|
||||
|
||||
|
||||
def branch(symbol=u'⭠'):
|
||||
'''Return VCS branch.
|
||||
|
||||
TODO: Expand this function to handle several VCS plugins.
|
||||
'''
|
||||
branch = None
|
||||
try:
|
||||
branch = vim_funcs['vcs']['fugitive'](5)
|
||||
except vim.error:
|
||||
vim_funcs['vcs']['fugitive'] = None
|
||||
except TypeError:
|
||||
pass
|
||||
|
||||
return symbol + ' ' + branch if branch else None
|
||||
|
||||
|
||||
def file_directory():
|
||||
'''Return file directory (head component of the file path).
|
||||
'''
|
||||
return vim_funcs['expand']('%:~:.:h')
|
||||
|
||||
|
||||
def file_name():
|
||||
'''Return file name (tail component of the file path).
|
||||
'''
|
||||
return vim_funcs['expand']('%:~:.:t')
|
||||
|
||||
|
||||
def file_format():
|
||||
'''Return file format (i.e. line ending type).
|
||||
|
||||
Returns None for unknown or missing file format.
|
||||
'''
|
||||
return vim.eval('&fileformat') or None
|
||||
|
||||
|
||||
def file_encoding():
|
||||
'''Return file encoding/character set.
|
||||
|
||||
Returns None for unknown or missing file encoding.
|
||||
'''
|
||||
return vim.eval('&fileencoding') or None
|
||||
|
||||
|
||||
def file_type():
|
||||
'''Return file type.
|
||||
|
||||
Returns None for unknown file types.
|
||||
'''
|
||||
return vim.eval('&filetype') or None
|
||||
|
||||
|
||||
def line_percent():
|
||||
'''Return the cursor position in the file as a percentage.
|
||||
'''
|
||||
line_current = vim_funcs['line']('.')
|
||||
line_last = vim_funcs['line']('$')
|
||||
|
||||
return line_current * 100 // line_last
|
||||
|
||||
|
||||
def line_current():
|
||||
'''Return the current cursor line.
|
||||
'''
|
||||
return vim_funcs['line']('.')
|
||||
|
||||
|
||||
def col_current():
|
||||
'''Return the current cursor column.
|
||||
'''
|
||||
return vim_funcs['col']('.')
|
Loading…
Reference in New Issue