2013-03-11 10:40:09 +01:00
|
|
|
# vim:fileencoding=utf-8:noet
|
2013-02-24 11:03:14 +01:00
|
|
|
_log = []
|
2013-05-19 10:14:06 +02:00
|
|
|
vars = {}
|
2013-02-24 11:03:14 +01:00
|
|
|
_window = 0
|
|
|
|
_mode = 'n'
|
|
|
|
_buf_purge_events = set()
|
2013-05-19 10:14:06 +02:00
|
|
|
options = {
|
2013-03-10 10:08:01 +01:00
|
|
|
'paste': 0,
|
|
|
|
'ambiwidth': 'single',
|
|
|
|
}
|
2013-02-24 11:03:14 +01:00
|
|
|
_last_bufnr = 0
|
|
|
|
_highlights = {}
|
|
|
|
|
|
|
|
|
2013-04-03 23:44:28 +02:00
|
|
|
_thread_id = None
|
2013-02-24 11:03:14 +01:00
|
|
|
|
|
|
|
|
2013-04-03 23:44:28 +02:00
|
|
|
def _set_thread_id():
|
|
|
|
global _thread_id
|
|
|
|
from threading import current_thread
|
|
|
|
_thread_id = current_thread().ident
|
|
|
|
|
|
|
|
|
|
|
|
# Assuming import is done from the main thread
|
|
|
|
_set_thread_id()
|
2013-02-24 11:03:14 +01:00
|
|
|
|
|
|
|
|
2013-04-03 23:44:28 +02:00
|
|
|
def _vim(func):
|
2013-02-24 11:03:14 +01:00
|
|
|
from functools import wraps
|
2013-04-03 23:44:28 +02:00
|
|
|
from threading import current_thread
|
2013-02-24 11:03:14 +01:00
|
|
|
|
|
|
|
@wraps(func)
|
2013-03-03 17:40:35 +01:00
|
|
|
def f(*args, **kwargs):
|
2013-04-03 23:44:28 +02:00
|
|
|
global _thread_id
|
|
|
|
if _thread_id != current_thread().ident:
|
|
|
|
raise RuntimeError('Accessing vim from separate threads is not allowed')
|
2013-02-24 11:03:14 +01:00
|
|
|
_log.append((func.__name__, args))
|
2013-03-03 17:40:35 +01:00
|
|
|
return func(*args, **kwargs)
|
2013-02-24 11:03:14 +01:00
|
|
|
|
|
|
|
return f
|
|
|
|
|
|
|
|
|
2013-04-03 23:44:28 +02:00
|
|
|
class _Buffers(object):
|
|
|
|
@_vim
|
|
|
|
def __init__(self):
|
|
|
|
self.d = {}
|
|
|
|
|
|
|
|
@_vim
|
|
|
|
def __getitem__(self, item):
|
|
|
|
return self.d[item]
|
|
|
|
|
|
|
|
@_vim
|
|
|
|
def __setitem__(self, item, value):
|
|
|
|
self.d[item] = value
|
|
|
|
|
|
|
|
@_vim
|
|
|
|
def __contains__(self, item):
|
|
|
|
return item in self.d
|
|
|
|
|
|
|
|
@_vim
|
|
|
|
def __nonzero__(self):
|
|
|
|
return not not self.d
|
|
|
|
|
|
|
|
@_vim
|
|
|
|
def keys(self):
|
|
|
|
return self.d.keys()
|
|
|
|
|
|
|
|
@_vim
|
|
|
|
def pop(self, *args, **kwargs):
|
|
|
|
return self.d.pop(*args, **kwargs)
|
|
|
|
|
|
|
|
|
|
|
|
buffers = _Buffers()
|
|
|
|
|
|
|
|
|
|
|
|
class _Windows(object):
|
|
|
|
@_vim
|
|
|
|
def __init__(self):
|
|
|
|
self.l = []
|
|
|
|
|
|
|
|
@_vim
|
|
|
|
def __getitem__(self, item):
|
|
|
|
return self.l[item]
|
|
|
|
|
|
|
|
@_vim
|
|
|
|
def __setitem__(self, item, value):
|
|
|
|
self.l[item] = value
|
|
|
|
|
|
|
|
@_vim
|
|
|
|
def __len__(self):
|
|
|
|
return len(self.l)
|
|
|
|
|
|
|
|
@_vim
|
|
|
|
def __iter__(self):
|
|
|
|
return iter(self.l)
|
|
|
|
|
|
|
|
@_vim
|
|
|
|
def __nonzero__(self):
|
|
|
|
return not not self.l
|
|
|
|
|
|
|
|
@_vim
|
|
|
|
def pop(self, *args, **kwargs):
|
|
|
|
return self.l.pop(*args, **kwargs)
|
|
|
|
|
|
|
|
@_vim
|
|
|
|
def append(self, *args, **kwargs):
|
|
|
|
return self.l.append(*args, **kwargs)
|
|
|
|
|
|
|
|
@_vim
|
|
|
|
def index(self, *args, **kwargs):
|
|
|
|
return self.l.index(*args, **kwargs)
|
|
|
|
|
|
|
|
|
|
|
|
windows = _Windows()
|
|
|
|
|
|
|
|
|
|
|
|
@_vim
|
|
|
|
def _buffer():
|
|
|
|
return windows[_window - 1].buffer.number
|
|
|
|
|
|
|
|
|
2013-03-24 02:45:52 +01:00
|
|
|
def _construct_result(r):
|
|
|
|
import sys
|
|
|
|
if sys.version_info < (3,):
|
|
|
|
return r
|
|
|
|
else:
|
|
|
|
if type(r) is str:
|
|
|
|
return r.encode('utf-8')
|
|
|
|
elif type(r) is dict or type(r) is list:
|
|
|
|
raise NotImplementedError
|
|
|
|
return r
|
|
|
|
|
|
|
|
|
|
|
|
def _str_func(func):
|
|
|
|
from functools import wraps
|
2013-03-24 16:59:56 +01:00
|
|
|
|
2013-03-24 02:45:52 +01:00
|
|
|
@wraps(func)
|
|
|
|
def f(*args, **kwargs):
|
|
|
|
return _construct_result(func(*args, **kwargs))
|
|
|
|
return f
|
|
|
|
|
|
|
|
|
2013-03-03 17:40:35 +01:00
|
|
|
def _log_print():
|
|
|
|
import sys
|
|
|
|
for entry in _log:
|
|
|
|
sys.stdout.write(repr(entry) + '\n')
|
|
|
|
|
|
|
|
|
2013-04-03 23:44:28 +02:00
|
|
|
@_vim
|
2013-02-24 11:03:14 +01:00
|
|
|
def command(cmd):
|
|
|
|
if cmd.startswith('let g:'):
|
|
|
|
import re
|
|
|
|
varname, value = re.compile(r'^let g:(\w+)\s*=\s*(.*)').match(cmd).groups()
|
2013-05-19 10:14:06 +02:00
|
|
|
vars[varname] = value
|
2013-02-24 11:03:14 +01:00
|
|
|
elif cmd.startswith('hi '):
|
|
|
|
sp = cmd.split()
|
|
|
|
_highlights[sp[1]] = sp[2:]
|
|
|
|
else:
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
|
2013-04-03 23:44:28 +02:00
|
|
|
@_vim
|
2013-02-24 11:03:14 +01:00
|
|
|
def eval(expr):
|
|
|
|
if expr.startswith('g:'):
|
2013-05-19 10:14:06 +02:00
|
|
|
return vars[expr[2:]]
|
2013-02-24 11:03:14 +01:00
|
|
|
elif expr.startswith('&'):
|
2013-05-19 10:14:06 +02:00
|
|
|
return options[expr[1:]]
|
2013-02-24 11:03:14 +01:00
|
|
|
elif expr.startswith('PowerlineRegisterCachePurgerEvent'):
|
|
|
|
_buf_purge_events.add(expr[expr.find('"') + 1:expr.rfind('"') - 1])
|
|
|
|
return "0"
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
|
2013-04-03 23:44:28 +02:00
|
|
|
@_vim
|
2013-02-24 11:03:14 +01:00
|
|
|
def bindeval(expr):
|
|
|
|
if expr == 'g:':
|
2013-05-19 10:14:06 +02:00
|
|
|
return vars
|
2013-05-23 16:22:49 +02:00
|
|
|
elif expr == '{}':
|
|
|
|
return {}
|
|
|
|
elif expr == '[]':
|
|
|
|
return []
|
2013-02-24 11:03:14 +01:00
|
|
|
import re
|
|
|
|
match = re.compile(r'^function\("([^"\\]+)"\)$').match(expr)
|
|
|
|
if match:
|
|
|
|
return globals()['_emul_' + match.group(1)]
|
|
|
|
else:
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
|
2013-04-03 23:44:28 +02:00
|
|
|
@_vim
|
2013-03-24 02:45:52 +01:00
|
|
|
@_str_func
|
2013-02-24 11:03:14 +01:00
|
|
|
def _emul_mode(*args):
|
|
|
|
if args and args[0]:
|
|
|
|
return _mode
|
|
|
|
else:
|
|
|
|
return _mode[0]
|
|
|
|
|
|
|
|
|
2013-04-03 23:44:28 +02:00
|
|
|
@_vim
|
2013-03-24 02:45:52 +01:00
|
|
|
@_str_func
|
2013-02-24 11:03:14 +01:00
|
|
|
def _emul_getbufvar(bufnr, varname):
|
|
|
|
if varname[0] == '&':
|
2013-04-13 17:28:19 +02:00
|
|
|
if bufnr == '%':
|
|
|
|
bufnr = buffers[_buffer()].number
|
2013-04-03 23:44:28 +02:00
|
|
|
if bufnr not in buffers:
|
2013-02-24 11:03:14 +01:00
|
|
|
return ''
|
|
|
|
try:
|
2013-05-19 10:14:06 +02:00
|
|
|
return buffers[bufnr].options[varname[1:]]
|
2013-02-24 11:03:14 +01:00
|
|
|
except KeyError:
|
|
|
|
try:
|
2013-05-19 10:14:06 +02:00
|
|
|
return options[varname[1:]]
|
2013-02-24 11:03:14 +01:00
|
|
|
except KeyError:
|
|
|
|
return ''
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
|
2013-04-03 23:44:28 +02:00
|
|
|
@_vim
|
2013-03-24 02:45:52 +01:00
|
|
|
@_str_func
|
2013-02-24 11:03:14 +01:00
|
|
|
def _emul_getwinvar(winnr, varname):
|
2013-05-19 10:14:06 +02:00
|
|
|
return windows[winnr].vars[varname]
|
2013-02-24 11:03:14 +01:00
|
|
|
|
|
|
|
|
2013-04-03 23:44:28 +02:00
|
|
|
@_vim
|
2013-02-24 11:03:14 +01:00
|
|
|
def _emul_setwinvar(winnr, varname, value):
|
2013-05-19 10:14:06 +02:00
|
|
|
windows[winnr].vars[varname] = value
|
2013-02-24 11:03:14 +01:00
|
|
|
|
|
|
|
|
2013-04-03 23:44:28 +02:00
|
|
|
@_vim
|
2013-02-24 11:03:14 +01:00
|
|
|
def _emul_virtcol(expr):
|
|
|
|
if expr == '.':
|
|
|
|
return windows[_window - 1].cursor[1] + 1
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
|
2013-04-03 23:44:28 +02:00
|
|
|
@_vim
|
2013-03-24 02:45:52 +01:00
|
|
|
@_str_func
|
2013-02-24 11:03:14 +01:00
|
|
|
def _emul_fnamemodify(path, modstring):
|
|
|
|
import os
|
|
|
|
_modifiers = {
|
|
|
|
'~': lambda path: path.replace(os.environ['HOME'], '~') if path.startswith(os.environ['HOME']) else path,
|
|
|
|
'.': lambda path: (lambda tpath: path if tpath[:3] == '..' + os.sep else tpath)(os.path.relpath(path)),
|
|
|
|
't': lambda path: os.path.basename(path),
|
|
|
|
'h': lambda path: os.path.dirname(path),
|
|
|
|
}
|
|
|
|
|
|
|
|
for mods in modstring.split(':')[1:]:
|
|
|
|
path = _modifiers[mods](path)
|
|
|
|
return path
|
|
|
|
|
|
|
|
|
2013-04-03 23:44:28 +02:00
|
|
|
@_vim
|
2013-03-24 02:45:52 +01:00
|
|
|
@_str_func
|
2013-02-24 11:03:14 +01:00
|
|
|
def _emul_expand(expr):
|
|
|
|
if expr == '<abuf>':
|
|
|
|
return _buffer()
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
|
2013-04-03 23:44:28 +02:00
|
|
|
@_vim
|
2013-02-24 11:03:14 +01:00
|
|
|
def _emul_bufnr(expr):
|
|
|
|
if expr == '$':
|
|
|
|
return _last_bufnr
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
|
2013-04-03 23:44:28 +02:00
|
|
|
@_vim
|
2013-02-24 11:03:14 +01:00
|
|
|
def _emul_exists(varname):
|
|
|
|
if varname.startswith('g:'):
|
2013-05-19 10:14:06 +02:00
|
|
|
return varname[2:] in vars
|
2013-02-24 11:03:14 +01:00
|
|
|
raise NotImplementedError
|
|
|
|
|
2013-03-14 19:38:40 +01:00
|
|
|
|
2013-04-03 23:44:28 +02:00
|
|
|
@_vim
|
2013-03-13 16:49:53 +01:00
|
|
|
def _emul_line2byte(line):
|
|
|
|
buflines = _buf_lines[_buffer()]
|
|
|
|
if line == len(buflines) + 1:
|
|
|
|
return sum((len(s) for s in buflines)) + 1
|
|
|
|
raise NotImplementedError
|
2013-02-24 11:03:14 +01:00
|
|
|
|
2013-05-19 10:14:06 +02:00
|
|
|
|
2013-02-24 11:03:14 +01:00
|
|
|
_window_ids = [None]
|
|
|
|
_window_id = 0
|
|
|
|
|
|
|
|
|
|
|
|
class _Window(object):
|
|
|
|
def __init__(self, buffer=None, cursor=(1, 0), width=80):
|
|
|
|
global _window_id
|
|
|
|
self.cursor = cursor
|
|
|
|
self.width = width
|
2013-05-17 06:51:33 +02:00
|
|
|
self.number = len(windows) + 1
|
2013-02-24 11:03:14 +01:00
|
|
|
if buffer:
|
|
|
|
if type(buffer) is _Buffer:
|
|
|
|
self.buffer = buffer
|
|
|
|
else:
|
|
|
|
self.buffer = _Buffer(**buffer)
|
|
|
|
else:
|
|
|
|
self.buffer = _Buffer()
|
|
|
|
windows.append(self)
|
|
|
|
_window_id += 1
|
|
|
|
_window_ids.append(_window_id)
|
2013-05-19 10:14:06 +02:00
|
|
|
self.options = {}
|
|
|
|
self.vars = {}
|
2013-02-24 11:03:14 +01:00
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return '<window ' + str(windows.index(self)) + '>'
|
|
|
|
|
|
|
|
|
|
|
|
_buf_lines = {}
|
|
|
|
_undostate = {}
|
|
|
|
_undo_written = {}
|
|
|
|
|
|
|
|
|
|
|
|
class _Buffer(object):
|
|
|
|
def __init__(self, name=None):
|
|
|
|
global _last_bufnr
|
|
|
|
import os
|
|
|
|
_last_bufnr += 1
|
|
|
|
bufnr = _last_bufnr
|
|
|
|
self.number = bufnr
|
|
|
|
self.name = os.path.abspath(name) if name else None
|
2013-05-19 10:14:06 +02:00
|
|
|
self.vars = {}
|
|
|
|
self.options = {
|
2013-04-13 17:21:11 +02:00
|
|
|
'modified': 0,
|
|
|
|
'readonly': 0,
|
|
|
|
'fileformat': 'unix',
|
|
|
|
'filetype': '',
|
|
|
|
'buftype': '',
|
|
|
|
'fileencoding': 'utf-8',
|
2013-04-13 17:28:19 +02:00
|
|
|
'textwidth': 80,
|
2013-04-13 17:21:11 +02:00
|
|
|
}
|
2013-02-24 11:03:14 +01:00
|
|
|
_buf_lines[bufnr] = ['']
|
|
|
|
from copy import copy
|
|
|
|
_undostate[bufnr] = [copy(_buf_lines[bufnr])]
|
|
|
|
_undo_written[bufnr] = len(_undostate[bufnr])
|
|
|
|
buffers[bufnr] = self
|
|
|
|
|
|
|
|
def __getitem__(self, line):
|
|
|
|
return _buf_lines[self.number][line]
|
|
|
|
|
|
|
|
def __setitem__(self, line, value):
|
2013-05-19 10:14:06 +02:00
|
|
|
self.options['modified'] = 1
|
2013-02-24 11:03:14 +01:00
|
|
|
_buf_lines[self.number][line] = value
|
|
|
|
from copy import copy
|
|
|
|
_undostate[self.number].append(copy(_buf_lines[self.number]))
|
|
|
|
|
|
|
|
def __setslice__(self, *args):
|
2013-05-19 10:14:06 +02:00
|
|
|
self.options['modified'] = 1
|
2013-02-24 11:03:14 +01:00
|
|
|
_buf_lines[self.number].__setslice__(*args)
|
|
|
|
from copy import copy
|
|
|
|
_undostate[self.number].append(copy(_buf_lines[self.number]))
|
|
|
|
|
|
|
|
def __getslice__(self, *args):
|
|
|
|
return _buf_lines[self.number].__getslice__(*args)
|
|
|
|
|
|
|
|
def __len__(self):
|
|
|
|
return len(_buf_lines[self.number])
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return '<buffer ' + str(self.name) + '>'
|
|
|
|
|
|
|
|
def __del__(self):
|
|
|
|
bufnr = self.number
|
2013-05-19 10:14:06 +02:00
|
|
|
if _buf_lines:
|
2013-02-24 11:03:14 +01:00
|
|
|
_buf_lines.pop(bufnr)
|
2013-05-19 11:17:30 +02:00
|
|
|
if _undostate:
|
2013-02-24 11:03:14 +01:00
|
|
|
_undostate.pop(bufnr)
|
2013-05-19 11:17:30 +02:00
|
|
|
if _undo_written:
|
2013-02-24 11:03:14 +01:00
|
|
|
_undo_written.pop(bufnr)
|
|
|
|
|
|
|
|
|
2013-03-13 16:49:53 +01:00
|
|
|
class _Current(object):
|
|
|
|
@property
|
|
|
|
def buffer(self):
|
|
|
|
return buffers[_buffer()]
|
|
|
|
|
2013-05-17 06:51:33 +02:00
|
|
|
@property
|
|
|
|
def window(self):
|
|
|
|
return windows[_window - 1]
|
|
|
|
|
2013-03-13 16:49:53 +01:00
|
|
|
|
|
|
|
current = _Current()
|
|
|
|
|
|
|
|
|
2013-03-02 16:05:08 +01:00
|
|
|
_dict = None
|
2013-02-24 11:03:14 +01:00
|
|
|
|
|
|
|
|
2013-04-03 23:44:28 +02:00
|
|
|
@_vim
|
2013-03-02 16:05:08 +01:00
|
|
|
def _init():
|
|
|
|
global _dict
|
2013-02-24 11:03:14 +01:00
|
|
|
|
2013-03-02 16:05:08 +01:00
|
|
|
if _dict:
|
|
|
|
return _dict
|
2013-02-24 11:03:14 +01:00
|
|
|
|
2013-03-02 16:05:08 +01:00
|
|
|
_dict = {}
|
2013-02-24 11:03:14 +01:00
|
|
|
for varname, value in globals().items():
|
|
|
|
if varname[0] != '_':
|
2013-03-02 16:05:08 +01:00
|
|
|
_dict[varname] = value
|
2013-02-24 11:03:14 +01:00
|
|
|
_new()
|
2013-03-02 16:05:08 +01:00
|
|
|
return _dict
|
2013-02-24 11:03:14 +01:00
|
|
|
|
|
|
|
|
2013-04-03 23:44:28 +02:00
|
|
|
@_vim
|
2013-02-24 11:03:14 +01:00
|
|
|
def _get_segment_info():
|
|
|
|
mode_translations = {
|
2013-04-13 17:21:11 +02:00
|
|
|
chr(ord('V') - 0x40): '^V',
|
|
|
|
chr(ord('S') - 0x40): '^S',
|
|
|
|
}
|
2013-02-24 11:03:14 +01:00
|
|
|
mode = _mode
|
|
|
|
mode = mode_translations.get(mode, mode)
|
|
|
|
return {
|
|
|
|
'window': windows[_window - 1],
|
|
|
|
'buffer': buffers[_buffer()],
|
|
|
|
'bufnr': _buffer(),
|
|
|
|
'window_id': _window_ids[_window],
|
|
|
|
'mode': mode,
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-04-03 23:44:28 +02:00
|
|
|
@_vim
|
2013-02-24 11:03:14 +01:00
|
|
|
def _launch_event(event):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2013-04-03 23:44:28 +02:00
|
|
|
@_vim
|
2013-02-24 11:03:14 +01:00
|
|
|
def _start_mode(mode):
|
|
|
|
global _mode
|
|
|
|
if mode == 'i':
|
|
|
|
_launch_event('InsertEnter')
|
|
|
|
elif _mode == 'i':
|
|
|
|
_launch_event('InsertLeave')
|
|
|
|
_mode = mode
|
|
|
|
|
|
|
|
|
2013-04-03 23:44:28 +02:00
|
|
|
@_vim
|
2013-02-24 11:03:14 +01:00
|
|
|
def _undo():
|
|
|
|
if len(_undostate[_buffer()]) == 1:
|
|
|
|
return
|
|
|
|
_undostate[_buffer()].pop(-1)
|
|
|
|
_buf_lines[_buffer()] = _undostate[_buffer()][-1]
|
2013-05-19 10:14:06 +02:00
|
|
|
buf = current.buffer
|
2013-02-24 11:03:14 +01:00
|
|
|
if _undo_written[_buffer()] == len(_undostate[_buffer()]):
|
2013-05-19 10:14:06 +02:00
|
|
|
buf.options['modified'] = 0
|
2013-02-24 11:03:14 +01:00
|
|
|
|
|
|
|
|
2013-04-03 23:44:28 +02:00
|
|
|
@_vim
|
2013-02-24 11:03:14 +01:00
|
|
|
def _edit(name=None):
|
|
|
|
global _last_bufnr
|
|
|
|
if _buffer() and buffers[_buffer()].name is None:
|
|
|
|
buf = buffers[_buffer()]
|
|
|
|
buf.name = name
|
|
|
|
else:
|
|
|
|
buf = _Buffer(name)
|
|
|
|
windows[_window - 1].buffer = buf
|
|
|
|
|
|
|
|
|
2013-04-03 23:44:28 +02:00
|
|
|
@_vim
|
2013-02-24 11:03:14 +01:00
|
|
|
def _new(name=None):
|
|
|
|
global _window
|
|
|
|
_Window(buffer={'name': name})
|
|
|
|
_window = len(windows)
|
|
|
|
|
|
|
|
|
2013-05-17 06:51:33 +02:00
|
|
|
@_vim
|
|
|
|
def _split():
|
|
|
|
global _window
|
|
|
|
_Window(buffer=buffers[_buffer()])
|
|
|
|
_window = len(windows)
|
|
|
|
|
|
|
|
|
2013-04-03 23:44:28 +02:00
|
|
|
@_vim
|
2013-02-24 11:03:14 +01:00
|
|
|
def _del_window(winnr):
|
|
|
|
win = windows.pop(winnr - 1)
|
|
|
|
_window_ids.pop(winnr)
|
|
|
|
return win
|
|
|
|
|
|
|
|
|
2013-04-03 23:44:28 +02:00
|
|
|
@_vim
|
2013-02-24 11:03:14 +01:00
|
|
|
def _close(winnr, wipe=True):
|
|
|
|
global _window
|
|
|
|
win = _del_window(winnr)
|
|
|
|
if _window == winnr:
|
|
|
|
_window = len(windows)
|
|
|
|
if wipe:
|
|
|
|
for w in windows:
|
|
|
|
if w.buffer.number == win.buffer.number:
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
_bw(win.buffer.number)
|
|
|
|
if not windows:
|
|
|
|
_Window()
|
|
|
|
|
|
|
|
|
2013-04-03 23:44:28 +02:00
|
|
|
@_vim
|
2013-02-24 11:03:14 +01:00
|
|
|
def _bw(bufnr=None):
|
|
|
|
bufnr = bufnr or _buffer()
|
|
|
|
winnr = 1
|
|
|
|
for win in windows:
|
|
|
|
if win.buffer.number == bufnr:
|
|
|
|
_close(winnr, wipe=False)
|
|
|
|
winnr += 1
|
|
|
|
buffers.pop(bufnr)
|
|
|
|
if not buffers:
|
|
|
|
_Buffer()
|
|
|
|
_b(max(buffers.keys()))
|
|
|
|
|
|
|
|
|
2013-04-03 23:44:28 +02:00
|
|
|
@_vim
|
2013-02-24 11:03:14 +01:00
|
|
|
def _b(bufnr):
|
|
|
|
windows[_window - 1].buffer = buffers[bufnr]
|
|
|
|
|
|
|
|
|
2013-04-03 23:44:28 +02:00
|
|
|
@_vim
|
2013-02-24 11:03:14 +01:00
|
|
|
def _set_cursor(line, col):
|
|
|
|
windows[_window - 1].cursor = (line, col)
|
|
|
|
if _mode == 'n':
|
|
|
|
_launch_event('CursorMoved')
|
|
|
|
elif _mode == 'i':
|
|
|
|
_launch_event('CursorMovedI')
|
|
|
|
|
|
|
|
|
2013-04-03 23:44:28 +02:00
|
|
|
@_vim
|
2013-02-24 11:03:14 +01:00
|
|
|
def _get_buffer():
|
|
|
|
return buffers[_buffer()]
|
|
|
|
|
|
|
|
|
2013-04-03 23:44:28 +02:00
|
|
|
@_vim
|
2013-02-24 11:03:14 +01:00
|
|
|
def _set_bufoption(option, value, bufnr=None):
|
2013-05-19 10:14:06 +02:00
|
|
|
buffers[bufnr or _buffer()].options[option] = value
|
2013-03-02 21:02:07 +01:00
|
|
|
if option == 'filetype':
|
|
|
|
_launch_event('FileType')
|
|
|
|
|
|
|
|
|
|
|
|
class _WithNewBuffer(object):
|
|
|
|
def __init__(self, func, *args, **kwargs):
|
|
|
|
self.call = lambda: func(*args, **kwargs)
|
|
|
|
|
|
|
|
def __enter__(self):
|
|
|
|
self.call()
|
|
|
|
self.bufnr = _buffer()
|
|
|
|
return _get_segment_info()
|
|
|
|
|
|
|
|
def __exit__(self, *args):
|
|
|
|
_bw(self.bufnr)
|
|
|
|
|
|
|
|
|
2013-04-03 23:44:28 +02:00
|
|
|
@_vim
|
2013-03-02 21:02:07 +01:00
|
|
|
def _set_dict(d, new, setfunc=None):
|
|
|
|
if not setfunc:
|
|
|
|
def setfunc(k, v):
|
|
|
|
d[k] = v
|
|
|
|
|
|
|
|
old = {}
|
|
|
|
na = []
|
|
|
|
for k, v in new.items():
|
|
|
|
try:
|
|
|
|
old[k] = d[k]
|
|
|
|
except KeyError:
|
|
|
|
na.append(k)
|
|
|
|
setfunc(k, v)
|
|
|
|
return old, na
|
|
|
|
|
|
|
|
|
|
|
|
class _WithBufOption(object):
|
|
|
|
def __init__(self, **new):
|
|
|
|
self.new = new
|
|
|
|
|
|
|
|
def __enter__(self):
|
2013-05-19 10:14:06 +02:00
|
|
|
self.buffer = buffers[_buffer()]
|
|
|
|
self.old = _set_dict(self.buffer.options, self.new, _set_bufoption)[0]
|
2013-03-02 21:02:07 +01:00
|
|
|
|
|
|
|
def __exit__(self, *args):
|
2013-05-19 10:14:06 +02:00
|
|
|
self.buffer.options.update(self.old)
|
2013-03-02 21:02:07 +01:00
|
|
|
|
|
|
|
|
|
|
|
class _WithMode(object):
|
|
|
|
def __init__(self, new):
|
|
|
|
self.new = new
|
|
|
|
|
|
|
|
def __enter__(self):
|
|
|
|
self.old = _mode
|
|
|
|
_start_mode(self.new)
|
|
|
|
return _get_segment_info()
|
|
|
|
|
|
|
|
def __exit__(self, *args):
|
|
|
|
_start_mode(self.old)
|
|
|
|
|
|
|
|
|
|
|
|
class _WithDict(object):
|
|
|
|
def __init__(self, d, **new):
|
|
|
|
self.new = new
|
|
|
|
self.d = d
|
|
|
|
|
|
|
|
def __enter__(self):
|
|
|
|
self.old, self.na = _set_dict(self.d, self.new)
|
|
|
|
|
|
|
|
def __exit__(self, *args):
|
|
|
|
self.d.update(self.old)
|
|
|
|
for k in self.na:
|
|
|
|
self.d.pop(k)
|
|
|
|
|
|
|
|
|
2013-05-17 06:51:33 +02:00
|
|
|
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
|
|
|
|
|
|
|
|
|
2013-04-03 23:44:28 +02:00
|
|
|
@_vim
|
2013-03-02 21:02:07 +01:00
|
|
|
def _with(key, *args, **kwargs):
|
|
|
|
if key == 'buffer':
|
|
|
|
return _WithNewBuffer(_edit, *args, **kwargs)
|
2013-05-17 06:51:33 +02:00
|
|
|
elif key == 'bufname':
|
|
|
|
return _WithBufName(*args, **kwargs)
|
2013-03-02 21:02:07 +01:00
|
|
|
elif key == 'mode':
|
|
|
|
return _WithMode(*args, **kwargs)
|
|
|
|
elif key == 'bufoptions':
|
|
|
|
return _WithBufOption(**kwargs)
|
|
|
|
elif key == 'options':
|
2013-05-19 10:14:06 +02:00
|
|
|
return _WithDict(options, **kwargs)
|
2013-03-02 21:02:07 +01:00
|
|
|
elif key == 'globals':
|
2013-05-19 10:14:06 +02:00
|
|
|
return _WithDict(vars, **kwargs)
|
2013-05-17 06:51:33 +02:00
|
|
|
elif key == 'split':
|
|
|
|
return _WithSplit()
|