2009-06-23 08:45:04 -04:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# encoding: utf-8
|
|
|
|
|
|
|
|
import vim
|
|
|
|
import string
|
2009-06-28 08:51:27 -04:00
|
|
|
import re
|
|
|
|
|
2009-07-04 06:14:13 -04:00
|
|
|
import os
|
|
|
|
|
2009-07-01 11:11:33 -04:00
|
|
|
def debug(s):
|
|
|
|
f = open("/tmp/file.txt","a")
|
|
|
|
f.write(s+'\n')
|
|
|
|
f.close()
|
|
|
|
|
2009-07-03 04:59:55 -04:00
|
|
|
class Buffer(object):
|
|
|
|
def _replace(self, start, end, content, first_line, last_line):
|
2009-07-02 05:58:46 -04:00
|
|
|
|
2009-07-03 04:59:55 -04:00
|
|
|
text = content[:]
|
|
|
|
if len(text) == 1:
|
|
|
|
arr = [ first_line + text[0] + last_line ]
|
|
|
|
new_end = start + Position(0,len(text[0]))
|
|
|
|
else:
|
|
|
|
arr = [ first_line + text[0] ] + \
|
|
|
|
text[1:-1] + \
|
|
|
|
[ text[-1] + last_line ]
|
|
|
|
new_end = Position(start.line + len(text)-1, len(text[-1]))
|
|
|
|
|
|
|
|
self[start.line:end.line+1] = arr
|
2009-07-02 05:58:46 -04:00
|
|
|
|
2009-07-03 04:59:55 -04:00
|
|
|
return new_end
|
|
|
|
|
|
|
|
class TextBuffer(Buffer):
|
|
|
|
def __init__(self, textblock):
|
|
|
|
# We do not use splitlines() here because it handles cases like 'text\n'
|
|
|
|
# differently than we want it here
|
|
|
|
self._lines = textblock.replace('\r','').split('\n')
|
2009-07-02 05:58:46 -04:00
|
|
|
|
2009-07-03 04:59:55 -04:00
|
|
|
def calc_end(self, start):
|
|
|
|
text = self._lines[:]
|
|
|
|
if len(text) == 1:
|
|
|
|
new_end = start + Position(0,len(text[0]))
|
|
|
|
else:
|
|
|
|
new_end = Position(start.line + len(text)-1, len(text[-1]))
|
|
|
|
return new_end
|
|
|
|
|
|
|
|
def replace_text( self, start, end, content ):
|
|
|
|
first_line = self[start.line][:start.col]
|
|
|
|
last_line = self[end.line][end.col:]
|
|
|
|
return self._replace( start, end, content, first_line, last_line)
|
|
|
|
|
|
|
|
def __getitem__(self, a):
|
|
|
|
return self._lines.__getitem__(a)
|
|
|
|
def __setitem__(self, a, b):
|
|
|
|
return self._lines.__setitem__(a,b)
|
|
|
|
def __repr__(self):
|
|
|
|
return repr('\n'.join(self._lines))
|
|
|
|
def __str__(self):
|
|
|
|
return '\n'.join(self._lines)
|
|
|
|
|
|
|
|
class VimBuffer(Buffer):
|
|
|
|
def __init__(self, before, after):
|
|
|
|
self._bf = before
|
|
|
|
self._af = after
|
|
|
|
def __getitem__(self, a):
|
|
|
|
return vim.current.buffer[a]
|
|
|
|
def __setitem__(self, a, b):
|
|
|
|
if isinstance(a,slice):
|
|
|
|
vim.current.buffer[a.start:a.stop] = b
|
|
|
|
else:
|
|
|
|
vim.current.buffer[a] = b
|
|
|
|
def __repr__(self):
|
|
|
|
return "VimBuffer()"
|
2009-07-02 05:58:46 -04:00
|
|
|
|
2009-07-03 04:59:55 -04:00
|
|
|
def replace_text( self, start, end, content ):
|
|
|
|
return self._replace( start, end, content, self._bf, self._af)
|
2009-07-02 05:48:35 -04:00
|
|
|
|
2009-07-02 02:25:58 -04:00
|
|
|
class Position(object):
|
|
|
|
def __init__(self, line, col):
|
|
|
|
self.line = line
|
|
|
|
self.col = col
|
|
|
|
|
|
|
|
def col():
|
|
|
|
def fget(self):
|
|
|
|
return self._col
|
|
|
|
def fset(self, value):
|
|
|
|
self._col = value
|
|
|
|
return locals()
|
|
|
|
col = property(**col())
|
|
|
|
|
|
|
|
def line():
|
|
|
|
doc = "Zero base line numbers"
|
|
|
|
def fget(self):
|
|
|
|
return self._line
|
|
|
|
def fset(self, value):
|
|
|
|
self._line = value
|
|
|
|
return locals()
|
|
|
|
line = property(**line())
|
|
|
|
|
2009-07-02 08:22:13 -04:00
|
|
|
def __add__(self,pos):
|
|
|
|
if not isinstance(pos,Position):
|
|
|
|
raise TypeError("unsupported operand type(s) for +: " \
|
|
|
|
"'Position' and %s" % type(pos))
|
|
|
|
|
|
|
|
return Position(self.line + pos.line, self.col + pos.col)
|
|
|
|
|
|
|
|
def __sub__(self,pos):
|
|
|
|
if not isinstance(pos,Position):
|
|
|
|
raise TypeError("unsupported operand type(s) for +: " \
|
|
|
|
"'Position' and %s" % type(pos))
|
|
|
|
|
|
|
|
return Position(self.line - pos.line, self.col - pos.col)
|
|
|
|
|
2009-07-06 04:31:12 -04:00
|
|
|
def __cmp__(self, other):
|
|
|
|
s = self._line, self._col
|
|
|
|
o = other._line, other._col
|
|
|
|
return cmp(s,o)
|
|
|
|
|
2009-07-02 08:22:13 -04:00
|
|
|
def __repr__(self):
|
|
|
|
return "(%i,%i)" % (self._line, self._col)
|
|
|
|
|
2009-07-06 04:31:12 -04:00
|
|
|
class Range(object):
|
|
|
|
def __init__(self, start, end):
|
|
|
|
self._s = start
|
|
|
|
self._e = end
|
|
|
|
|
|
|
|
def __contains__(self, pos):
|
|
|
|
return self._s <= pos < self._e
|
|
|
|
|
|
|
|
def start():
|
|
|
|
def fget(self):
|
|
|
|
return self._s
|
|
|
|
def fset(self, value):
|
|
|
|
self._s = value
|
|
|
|
return locals()
|
|
|
|
start = property(**start())
|
|
|
|
|
|
|
|
def end():
|
|
|
|
def fget(self):
|
|
|
|
return self._e
|
|
|
|
def fset(self, value):
|
|
|
|
self._e = value
|
|
|
|
return locals()
|
|
|
|
end = property(**end())
|
2009-07-06 04:40:07 -04:00
|
|
|
|
2009-07-06 04:31:12 -04:00
|
|
|
def __repr__(self):
|
|
|
|
return "(%s -> %s)" % (self._s, self._e)
|
|
|
|
|
2009-07-01 09:29:14 -04:00
|
|
|
class TextObject(object):
|
2009-07-01 14:03:29 -04:00
|
|
|
"""
|
|
|
|
This base class represents any object in the text
|
|
|
|
that has a span in any ways
|
|
|
|
"""
|
2009-07-03 07:54:35 -04:00
|
|
|
# A simple tabstop with default value
|
2009-07-06 15:45:30 -04:00
|
|
|
_TABSTOP = re.compile(r'''\${(\d+)[:}]''')
|
2009-07-03 07:54:35 -04:00
|
|
|
# A mirror or a tabstop without default value.
|
|
|
|
_MIRROR_OR_TS = re.compile(r'\$(\d+)')
|
2009-07-03 10:13:39 -04:00
|
|
|
# A mirror or a tabstop without default value.
|
2009-07-03 11:50:52 -04:00
|
|
|
_TRANSFORMATION = re.compile(r'\${(\d+)/(.*?)/(.*?)/([a-zA-z]*)}')
|
2009-07-03 04:59:55 -04:00
|
|
|
|
|
|
|
def __init__(self, parent, start, end, initial_text):
|
2009-07-01 09:29:14 -04:00
|
|
|
self._start = start
|
|
|
|
self._end = end
|
2009-07-03 04:59:55 -04:00
|
|
|
|
2009-07-02 09:41:58 -04:00
|
|
|
self._parent = parent
|
|
|
|
|
|
|
|
self._children = []
|
2009-07-03 04:59:55 -04:00
|
|
|
self._tabstops = {}
|
2009-07-02 09:41:58 -04:00
|
|
|
|
|
|
|
if parent is not None:
|
|
|
|
parent.add_child(self)
|
|
|
|
|
2009-07-03 07:54:35 -04:00
|
|
|
self._has_parsed = False
|
|
|
|
|
|
|
|
self._current_text = initial_text
|
2009-07-06 04:40:07 -04:00
|
|
|
|
2009-07-06 04:31:12 -04:00
|
|
|
def span(self):
|
|
|
|
return Range(self._start, self._end)
|
|
|
|
span = property(span)
|
2009-07-03 04:59:55 -04:00
|
|
|
|
2009-07-04 17:01:23 -04:00
|
|
|
def __cmp__(self, other):
|
2009-07-06 04:31:12 -04:00
|
|
|
return cmp(self._start, other._start)
|
2009-07-04 17:01:23 -04:00
|
|
|
|
2009-07-03 05:39:46 -04:00
|
|
|
def _do_update(self):
|
|
|
|
pass
|
2009-07-03 04:59:55 -04:00
|
|
|
|
2009-07-03 10:13:39 -04:00
|
|
|
def update(self, change_buffer):
|
2009-07-03 07:54:35 -04:00
|
|
|
if not self._has_parsed:
|
|
|
|
self._current_text = TextBuffer(self._parse(self._current_text))
|
|
|
|
|
2009-07-04 17:01:23 -04:00
|
|
|
for idx,c in enumerate(self._children):
|
2009-07-03 05:39:46 -04:00
|
|
|
oldend = Position(c.end.line, c.end.col)
|
2009-07-03 04:59:55 -04:00
|
|
|
|
2009-07-03 10:13:39 -04:00
|
|
|
moved_lines, moved_cols = c.update(self._current_text)
|
2009-07-04 18:26:19 -04:00
|
|
|
self._move_textobjects_behind(c.start, oldend, moved_lines,
|
|
|
|
moved_cols, idx)
|
|
|
|
|
2009-07-03 04:59:55 -04:00
|
|
|
|
2009-07-03 05:39:46 -04:00
|
|
|
self._do_update()
|
|
|
|
|
2009-07-04 06:14:13 -04:00
|
|
|
new_end = change_buffer.replace_text(self.start, self.end,
|
|
|
|
self._current_text)
|
2009-07-03 04:59:55 -04:00
|
|
|
|
|
|
|
moved_lines = new_end.line - self._end.line
|
|
|
|
moved_cols = new_end.col - self._end.col
|
|
|
|
|
|
|
|
self._end = new_end
|
|
|
|
|
|
|
|
return moved_lines, moved_cols
|
|
|
|
|
2009-07-04 17:01:23 -04:00
|
|
|
def _move_textobjects_behind(self, start, end, lines, cols, obj_idx):
|
2009-07-03 04:59:55 -04:00
|
|
|
if lines == 0 and cols == 0:
|
|
|
|
return
|
|
|
|
|
2009-07-04 17:15:37 -04:00
|
|
|
for idx,m in enumerate(self._children[obj_idx+1:]):
|
2009-07-03 04:59:55 -04:00
|
|
|
delta_lines = 0
|
|
|
|
delta_cols_begin = 0
|
|
|
|
delta_cols_end = 0
|
|
|
|
|
2009-07-03 05:39:46 -04:00
|
|
|
if m.start.line > end.line:
|
2009-07-03 04:59:55 -04:00
|
|
|
delta_lines = lines
|
|
|
|
elif m.start.line == end.line:
|
|
|
|
if m.start.col >= end.col:
|
|
|
|
if lines:
|
|
|
|
delta_lines = lines
|
|
|
|
delta_cols_begin = cols
|
|
|
|
if m.start.line == m.end.line:
|
|
|
|
delta_cols_end = cols
|
|
|
|
m.start.line += delta_lines
|
|
|
|
m.end.line += delta_lines
|
|
|
|
m.start.col += delta_cols_begin
|
|
|
|
m.end.col += delta_cols_end
|
|
|
|
|
2009-07-03 06:23:30 -04:00
|
|
|
def _get_start_end(self, val, start_pos, end_pos):
|
|
|
|
def _get_pos(s, pos):
|
|
|
|
line_idx = s[:pos].count('\n')
|
|
|
|
line_start = s[:pos].rfind('\n') + 1
|
2009-07-03 07:54:35 -04:00
|
|
|
start_in_line = pos - line_start
|
2009-07-03 06:23:30 -04:00
|
|
|
return Position(line_idx, start_in_line)
|
|
|
|
|
|
|
|
return _get_pos(val, start_pos), _get_pos(val, end_pos)
|
|
|
|
|
2009-07-03 04:59:55 -04:00
|
|
|
|
|
|
|
def _handle_tabstop(self, m, val):
|
|
|
|
|
2009-07-06 15:45:30 -04:00
|
|
|
def _find_closingbracket(v,start_pos):
|
|
|
|
bracks_open = 1
|
|
|
|
for idx, c in enumerate(v[start_pos:]):
|
|
|
|
if c == '{':
|
|
|
|
if v[idx+start_pos-1] != '\\':
|
|
|
|
bracks_open += 1
|
|
|
|
elif c == '}':
|
|
|
|
if v[idx+start_pos-1] != '\\':
|
|
|
|
bracks_open -= 1
|
|
|
|
if not bracks_open:
|
|
|
|
return start_pos+idx+1
|
|
|
|
|
|
|
|
start_pos = m.start()
|
|
|
|
end_pos = _find_closingbracket(val, start_pos+2)
|
|
|
|
|
|
|
|
def_text = val[m.end():end_pos-1]
|
|
|
|
|
2009-07-03 06:23:30 -04:00
|
|
|
start, end = self._get_start_end(val,start_pos,end_pos)
|
|
|
|
|
2009-07-03 07:54:35 -04:00
|
|
|
ts = TabStop(self, start, end, def_text)
|
2009-07-03 04:59:55 -04:00
|
|
|
|
2009-07-06 15:45:30 -04:00
|
|
|
self.add_tabstop(int(m.group(1)),ts)
|
|
|
|
|
|
|
|
return val[:start_pos] + (end_pos-start_pos)*" " + val[end_pos:]
|
2009-07-03 04:59:55 -04:00
|
|
|
|
2009-07-03 06:05:34 -04:00
|
|
|
def _get_tabstop(self,no):
|
|
|
|
if no in self._tabstops:
|
|
|
|
return self._tabstops[no]
|
|
|
|
if self._parent:
|
|
|
|
return self._parent._get_tabstop(no)
|
|
|
|
|
2009-07-03 04:59:55 -04:00
|
|
|
def _handle_ts_or_mirror(self, m, val):
|
2009-07-03 07:54:35 -04:00
|
|
|
no = int(m.group(1))
|
2009-07-03 04:59:55 -04:00
|
|
|
|
2009-07-03 06:23:30 -04:00
|
|
|
start_pos, end_pos = m.span()
|
|
|
|
start, end = self._get_start_end(val,start_pos,end_pos)
|
2009-07-03 04:59:55 -04:00
|
|
|
|
2009-07-03 06:05:34 -04:00
|
|
|
ts = self._get_tabstop(no)
|
|
|
|
if ts is not None:
|
2009-07-03 10:13:39 -04:00
|
|
|
Mirror(self, ts, start, end)
|
2009-07-03 05:39:46 -04:00
|
|
|
else:
|
2009-07-03 07:54:35 -04:00
|
|
|
ts = TabStop(self, start, end)
|
2009-07-03 05:39:46 -04:00
|
|
|
self.add_tabstop(no,ts)
|
2009-07-03 04:59:55 -04:00
|
|
|
|
2009-07-03 10:13:39 -04:00
|
|
|
def _handle_transformation(self, m, val):
|
|
|
|
no = int(m.group(1))
|
2009-07-03 11:50:52 -04:00
|
|
|
search = m.group(2)
|
|
|
|
replace = m.group(3)
|
|
|
|
options = m.group(4)
|
2009-07-03 10:13:39 -04:00
|
|
|
|
|
|
|
start_pos, end_pos = m.span()
|
|
|
|
start, end = self._get_start_end(val,start_pos,end_pos)
|
|
|
|
|
2009-07-06 15:16:02 -04:00
|
|
|
Transformation(self, no, start, end, search, replace, options)
|
2009-07-03 10:13:39 -04:00
|
|
|
|
|
|
|
|
2009-07-03 04:59:55 -04:00
|
|
|
def add_tabstop(self,no, ts):
|
|
|
|
self._tabstops[no] = ts
|
|
|
|
|
|
|
|
def _parse(self, val):
|
2009-07-03 07:54:35 -04:00
|
|
|
self._has_parsed = True
|
|
|
|
|
|
|
|
if not len(val):
|
|
|
|
return val
|
|
|
|
|
|
|
|
for m in self._TABSTOP.finditer(val):
|
2009-07-06 15:45:30 -04:00
|
|
|
val = self._handle_tabstop(m,val)
|
2009-07-03 07:54:35 -04:00
|
|
|
|
2009-07-03 10:13:39 -04:00
|
|
|
for m in self._TRANSFORMATION.finditer(val):
|
|
|
|
self._handle_transformation(m,val)
|
|
|
|
# Replace the whole definition with spaces
|
|
|
|
s, e = m.span()
|
|
|
|
val = val[:s] + (e-s)*" " + val[e:]
|
|
|
|
|
|
|
|
|
2009-07-03 07:54:35 -04:00
|
|
|
for m in self._MIRROR_OR_TS.finditer(val):
|
|
|
|
self._handle_ts_or_mirror(m,val)
|
|
|
|
# Replace the whole definition with spaces
|
|
|
|
s, e = m.span()
|
|
|
|
val = val[:s] + (e-s)*" " + val[e:]
|
|
|
|
|
2009-07-03 04:59:55 -04:00
|
|
|
|
|
|
|
return val
|
|
|
|
|
2009-07-02 09:41:58 -04:00
|
|
|
def add_child(self,c):
|
|
|
|
self._children.append(c)
|
2009-07-04 17:01:23 -04:00
|
|
|
self._children.sort()
|
2009-07-01 09:29:14 -04:00
|
|
|
|
2009-07-02 03:49:42 -04:00
|
|
|
def parent():
|
|
|
|
doc = "The parent TextObject this TextObject resides in"
|
|
|
|
def fget(self):
|
|
|
|
return self._parent
|
|
|
|
def fset(self, value):
|
|
|
|
self._parent = value
|
|
|
|
return locals()
|
|
|
|
parent = property(**parent())
|
2009-07-02 10:02:02 -04:00
|
|
|
|
2009-07-01 09:29:14 -04:00
|
|
|
def start(self):
|
|
|
|
return self._start
|
2009-07-02 13:19:47 -04:00
|
|
|
start = property(start)
|
2009-07-01 09:29:14 -04:00
|
|
|
|
|
|
|
def end(self):
|
|
|
|
return self._end
|
2009-07-02 13:19:47 -04:00
|
|
|
end = property(end)
|
2009-07-01 11:11:33 -04:00
|
|
|
|
2009-07-02 10:20:13 -04:00
|
|
|
class ChangeableText(TextObject):
|
|
|
|
def __init__(self, parent, start, end, initial = ""):
|
2009-07-03 04:59:55 -04:00
|
|
|
TextObject.__init__(self, parent, start, end, initial)
|
2009-07-01 09:20:40 -04:00
|
|
|
|
2009-07-02 10:20:13 -04:00
|
|
|
def _set_text(self, text):
|
2009-07-03 04:59:55 -04:00
|
|
|
self._current_text = TextBuffer(text)
|
2009-07-02 15:24:56 -04:00
|
|
|
|
2009-07-03 06:05:34 -04:00
|
|
|
# Now, we can have no more childen
|
|
|
|
self._children = []
|
|
|
|
|
2009-07-02 10:20:13 -04:00
|
|
|
def current_text():
|
|
|
|
def fget(self):
|
2009-07-03 04:59:55 -04:00
|
|
|
return str(self._current_text)
|
2009-07-02 10:20:13 -04:00
|
|
|
def fset(self, text):
|
|
|
|
self._set_text(text)
|
|
|
|
return locals()
|
|
|
|
current_text = property(**current_text())
|
|
|
|
|
|
|
|
|
2009-07-03 05:39:46 -04:00
|
|
|
class Mirror(ChangeableText):
|
|
|
|
"""
|
|
|
|
A Mirror object mirrors a TabStop that is, text is repeated here
|
|
|
|
"""
|
2009-07-03 07:54:35 -04:00
|
|
|
def __init__(self, parent, ts, start, end):
|
2009-07-03 05:39:46 -04:00
|
|
|
ChangeableText.__init__(self, parent, start, end)
|
2009-07-03 06:05:34 -04:00
|
|
|
|
2009-07-03 05:39:46 -04:00
|
|
|
self._ts = ts
|
2009-07-03 06:05:34 -04:00
|
|
|
|
2009-07-03 05:39:46 -04:00
|
|
|
def _do_update(self):
|
|
|
|
self.current_text = self._ts.current_text
|
2009-07-01 14:14:54 -04:00
|
|
|
|
2009-07-03 05:39:46 -04:00
|
|
|
def __repr__(self):
|
|
|
|
return "Mirror(%s -> %s)" % (self._start, self._end)
|
2009-07-02 10:20:13 -04:00
|
|
|
|
2009-07-03 10:13:39 -04:00
|
|
|
class CleverReplace(object):
|
|
|
|
"""
|
|
|
|
This class mimics TextMates replace syntax
|
|
|
|
"""
|
2009-07-03 11:50:52 -04:00
|
|
|
_DOLLAR = re.compile(r"\$(\d+)", re.DOTALL)
|
|
|
|
_SIMPLE_CASEFOLDINGS = re.compile(r"\\([ul].)", re.DOTALL)
|
|
|
|
_LONG_CASEFOLDINGS = re.compile(r"\\([UL].*?)\\E", re.DOTALL)
|
2009-07-04 18:26:19 -04:00
|
|
|
_CONDITIONAL = re.compile(r"\(\?(\d+):(.*?)(?<!\\)\)", re.DOTALL)
|
|
|
|
|
|
|
|
_UNESCAPE = re.compile(r'\\[^ntrab]')
|
2009-07-03 10:13:39 -04:00
|
|
|
|
|
|
|
def __init__(self, s):
|
|
|
|
self._s = s
|
|
|
|
|
|
|
|
def _scase_folding(self, m):
|
|
|
|
if m.group(1)[0] == 'u':
|
|
|
|
return m.group(1)[-1].upper()
|
|
|
|
else:
|
|
|
|
return m.group(1)[-1].lower()
|
|
|
|
def _lcase_folding(self, m):
|
|
|
|
if m.group(1)[0] == 'U':
|
|
|
|
return m.group(1)[1:].upper()
|
|
|
|
else:
|
|
|
|
return m.group(1)[1:].lower()
|
|
|
|
|
2009-07-04 18:26:19 -04:00
|
|
|
def _unescape(self, v):
|
|
|
|
return self._UNESCAPE.subn(lambda m: m.group(0)[-1], v)[0]
|
2009-07-03 11:50:52 -04:00
|
|
|
def replace(self, match):
|
2009-07-03 10:13:39 -04:00
|
|
|
start, end = match.span()
|
|
|
|
|
|
|
|
tv = self._s
|
|
|
|
|
|
|
|
# Replace all $? with capture groups
|
|
|
|
tv = self._DOLLAR.subn(lambda m: match.group(int(m.group(1))), tv)[0]
|
|
|
|
|
2009-07-03 11:50:52 -04:00
|
|
|
def _conditional(m):
|
|
|
|
args = m.group(2).split(':')
|
|
|
|
# TODO: the returned string should be checked for conditionals
|
|
|
|
if match.group(int(m.group(1))):
|
2009-07-04 18:26:19 -04:00
|
|
|
return self._unescape(args[0])
|
2009-07-03 11:50:52 -04:00
|
|
|
elif len(args) > 1:
|
2009-07-04 18:26:19 -04:00
|
|
|
return self._unescape(args[1])
|
2009-07-03 11:50:52 -04:00
|
|
|
else:
|
|
|
|
return ""
|
|
|
|
|
2009-07-03 10:13:39 -04:00
|
|
|
# Replace CaseFoldings
|
|
|
|
tv = self._SIMPLE_CASEFOLDINGS.subn(self._scase_folding, tv)[0]
|
|
|
|
tv = self._LONG_CASEFOLDINGS.subn(self._lcase_folding, tv)[0]
|
2009-07-03 11:50:52 -04:00
|
|
|
tv = self._CONDITIONAL.subn(_conditional, tv)[0]
|
2009-07-03 10:13:39 -04:00
|
|
|
|
2009-07-03 11:50:52 -04:00
|
|
|
rv = tv.decode("string-escape")
|
2009-07-03 10:13:39 -04:00
|
|
|
|
|
|
|
return rv
|
|
|
|
|
|
|
|
class Transformation(Mirror):
|
2009-07-03 11:50:52 -04:00
|
|
|
def __init__(self, parent, ts, start, end, s, r, options):
|
2009-07-03 10:13:39 -04:00
|
|
|
Mirror.__init__(self, parent, ts, start, end)
|
|
|
|
|
2009-07-03 11:50:52 -04:00
|
|
|
flags = 0
|
|
|
|
self._match_this_many = 1
|
|
|
|
if options:
|
|
|
|
if "g" in options:
|
|
|
|
self._match_this_many = 0
|
|
|
|
if "i" in options:
|
|
|
|
flags |= re.IGNORECASE
|
2009-07-03 10:13:39 -04:00
|
|
|
|
2009-07-03 11:50:52 -04:00
|
|
|
self._find = re.compile(s, flags)
|
|
|
|
self._replace = CleverReplace(r)
|
2009-07-03 10:13:39 -04:00
|
|
|
|
|
|
|
def _do_update(self):
|
|
|
|
if isinstance(self._ts,int):
|
|
|
|
self._ts = self._parent._get_tabstop(self._ts)
|
|
|
|
|
|
|
|
t = self._ts.current_text
|
2009-07-03 11:50:52 -04:00
|
|
|
t = self._find.subn(self._replace.replace, t, self._match_this_many)[0]
|
2009-07-03 10:13:39 -04:00
|
|
|
self.current_text = t
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return "Transformation(%s -> %s)" % (self._start, self._end)
|
|
|
|
|
|
|
|
|
2009-07-02 15:24:56 -04:00
|
|
|
|
2009-07-02 10:20:13 -04:00
|
|
|
class TabStop(ChangeableText):
|
2009-07-01 14:03:29 -04:00
|
|
|
"""
|
|
|
|
This is the most important TextObject. A TabStop is were the cursor
|
|
|
|
comes to rest when the user taps through the Snippet.
|
|
|
|
"""
|
2009-07-03 07:54:35 -04:00
|
|
|
def __init__(self, parent, start, end, default_text = ""):
|
2009-07-02 10:20:13 -04:00
|
|
|
ChangeableText.__init__(self, parent, start, end, default_text)
|
2009-07-01 09:20:40 -04:00
|
|
|
|
2009-07-02 15:24:56 -04:00
|
|
|
def __repr__(self):
|
2009-07-03 04:59:55 -04:00
|
|
|
return "TabStop(%s -> %s, %s)" % (self._start, self._end,
|
|
|
|
repr(self._current_text))
|
2009-07-02 15:24:56 -04:00
|
|
|
|
2009-07-03 07:54:35 -04:00
|
|
|
def select(self, start):
|
|
|
|
lineno, col = start.line, start.col
|
2009-07-01 14:27:28 -04:00
|
|
|
|
2009-07-02 02:25:58 -04:00
|
|
|
newline = lineno + self._start.line
|
|
|
|
newcol = self._start.col
|
2009-07-01 17:09:20 -04:00
|
|
|
|
2009-07-01 14:27:28 -04:00
|
|
|
if newline == lineno:
|
2009-07-01 17:09:20 -04:00
|
|
|
newcol += col
|
2009-07-01 14:27:28 -04:00
|
|
|
|
2009-07-02 02:25:58 -04:00
|
|
|
vim.current.window.cursor = newline + 1, newcol
|
2009-07-04 15:58:13 -04:00
|
|
|
|
2009-07-04 11:36:06 -04:00
|
|
|
if len(self.current_text) == 0:
|
|
|
|
if newcol == 0:
|
|
|
|
vim.command(r'call feedkeys("\<Esc>i")')
|
|
|
|
else:
|
|
|
|
vim.command(r'call feedkeys("\<Esc>a")')
|
|
|
|
else:
|
2009-07-03 07:54:35 -04:00
|
|
|
# Select the word
|
|
|
|
# Depending on the current mode and position, we
|
|
|
|
# might need to move escape out of the mode and this
|
|
|
|
# will move our cursor one left
|
2009-07-01 14:27:28 -04:00
|
|
|
if newcol != 0 and vim.eval("mode()") == 'i':
|
|
|
|
move_one_right = "l"
|
|
|
|
else:
|
|
|
|
move_one_right = ""
|
|
|
|
|
2009-07-04 11:36:06 -04:00
|
|
|
if len(self.current_text) <= 1:
|
2009-07-03 07:54:35 -04:00
|
|
|
do_select = ""
|
|
|
|
else:
|
|
|
|
do_select = "%il" % (len(self.current_text)-1)
|
|
|
|
|
|
|
|
vim.command(r'call feedkeys("\<Esc>%sv%s\<c-g>")' %
|
|
|
|
(move_one_right, do_select))
|
2009-07-01 14:27:28 -04:00
|
|
|
|
|
|
|
|
2009-07-01 11:11:33 -04:00
|
|
|
class SnippetInstance(TextObject):
|
2009-07-01 14:03:29 -04:00
|
|
|
"""
|
|
|
|
A Snippet instance is an instance of a Snippet Definition. That is,
|
|
|
|
when the user expands a snippet, a SnippetInstance is created to
|
|
|
|
keep track of the corresponding TextObjects. The Snippet itself is
|
|
|
|
also a TextObject because it has a start an end
|
|
|
|
"""
|
|
|
|
|
2009-07-03 04:59:55 -04:00
|
|
|
def __init__(self, start, end, initial_text, text_before, text_after):
|
|
|
|
TextObject.__init__(self, None, start, end, initial_text)
|
2009-07-01 04:39:46 -04:00
|
|
|
|
2009-07-01 14:03:29 -04:00
|
|
|
self._cts = None
|
2009-07-02 09:45:25 -04:00
|
|
|
self._tab_selected = False
|
2009-07-03 04:59:55 -04:00
|
|
|
|
|
|
|
self._vb = VimBuffer(text_before, text_after)
|
|
|
|
|
2009-07-03 07:54:35 -04:00
|
|
|
self._current_text = TextBuffer(self._parse(initial_text))
|
|
|
|
|
2009-07-04 09:01:19 -04:00
|
|
|
TextObject.update(self, self._vb)
|
|
|
|
|
2009-07-04 11:36:06 -04:00
|
|
|
def tab_selected(self):
|
|
|
|
return self._tab_selected
|
|
|
|
tab_selected = property(tab_selected)
|
|
|
|
|
2009-07-04 09:01:19 -04:00
|
|
|
def update(self, buf, cur):
|
2009-07-03 04:59:55 -04:00
|
|
|
|
2009-07-04 09:01:19 -04:00
|
|
|
TextObject.update(self, buf)
|
|
|
|
|
|
|
|
cts = self._tabstops[self._cts]
|
2009-07-04 10:08:14 -04:00
|
|
|
|
2009-07-04 09:01:19 -04:00
|
|
|
cursor = self.start + cts.end
|
|
|
|
if cts.end.line != 0:
|
|
|
|
cursor.col -= self.start.col
|
|
|
|
lineno, col = cursor.line, cursor.col
|
|
|
|
|
|
|
|
vim.current.window.cursor = lineno +1, col
|
2009-06-28 16:22:19 -04:00
|
|
|
|
2009-07-02 09:41:58 -04:00
|
|
|
def has_tabs(self):
|
|
|
|
return len(self._children) > 0
|
2009-07-01 11:11:33 -04:00
|
|
|
|
2009-07-02 03:49:42 -04:00
|
|
|
|
2009-07-01 14:03:29 -04:00
|
|
|
def select_next_tab(self, backwards = False):
|
|
|
|
if self._cts == 0:
|
2009-07-06 05:47:27 -04:00
|
|
|
return False
|
2009-07-01 14:03:29 -04:00
|
|
|
|
|
|
|
if backwards:
|
|
|
|
cts_bf = self._cts
|
|
|
|
|
|
|
|
if self._cts == 0:
|
|
|
|
self._cts = max(self._tabstops.keys())
|
2009-06-28 16:22:19 -04:00
|
|
|
else:
|
2009-07-01 14:03:29 -04:00
|
|
|
self._cts -= 1
|
|
|
|
if self._cts <= 0:
|
|
|
|
self._cts = cts_bf
|
|
|
|
else:
|
|
|
|
# All tabs handled?
|
|
|
|
if self._cts is None:
|
2009-06-28 16:22:19 -04:00
|
|
|
self._cts = 1
|
2009-07-01 14:03:29 -04:00
|
|
|
else:
|
|
|
|
self._cts += 1
|
2009-07-01 04:39:46 -04:00
|
|
|
|
2009-07-01 14:03:29 -04:00
|
|
|
if self._cts not in self._tabstops:
|
|
|
|
self._cts = 0
|
|
|
|
if 0 not in self._tabstops:
|
|
|
|
return False
|
2009-07-01 11:11:33 -04:00
|
|
|
|
2009-07-01 14:03:29 -04:00
|
|
|
ts = self._tabstops[self._cts]
|
2009-06-28 16:22:19 -04:00
|
|
|
|
2009-07-03 07:54:35 -04:00
|
|
|
ts.select(self._start)
|
2009-07-01 11:11:33 -04:00
|
|
|
|
2009-07-02 09:45:25 -04:00
|
|
|
self._tab_selected = True
|
2009-07-06 05:47:27 -04:00
|
|
|
return self._cts
|
2009-07-01 14:03:29 -04:00
|
|
|
|
2009-07-04 09:01:19 -04:00
|
|
|
def backspace(self,count, previous_cp):
|
2009-07-06 11:44:04 -04:00
|
|
|
debug("In backspace")
|
|
|
|
debug("count: %s" % (count))
|
|
|
|
|
2009-07-02 08:22:13 -04:00
|
|
|
cts = self._tabstops[self._cts]
|
|
|
|
cts.current_text = cts.current_text[:-count]
|
2009-07-01 09:20:40 -04:00
|
|
|
|
2009-07-04 09:01:19 -04:00
|
|
|
self.update(self._vb, previous_cp)
|
2009-07-03 04:59:55 -04:00
|
|
|
|
2009-07-04 09:01:19 -04:00
|
|
|
def chars_entered(self, chars, cur):
|
2009-07-01 14:03:29 -04:00
|
|
|
cts = self._tabstops[self._cts]
|
2009-07-01 11:11:33 -04:00
|
|
|
|
2009-07-02 09:45:25 -04:00
|
|
|
if self._tab_selected:
|
2009-07-02 08:22:13 -04:00
|
|
|
cts.current_text = chars
|
2009-07-02 09:45:25 -04:00
|
|
|
self._tab_selected = False
|
2009-07-01 14:03:29 -04:00
|
|
|
else:
|
2009-07-02 08:22:13 -04:00
|
|
|
cts.current_text += chars
|
2009-07-01 09:20:40 -04:00
|
|
|
|
2009-07-04 09:01:19 -04:00
|
|
|
self.update(self._vb, cur)
|
2009-07-01 09:20:40 -04:00
|
|
|
|
2009-06-28 08:51:27 -04:00
|
|
|
|
2009-07-03 04:59:55 -04:00
|
|
|
class Snippet(object):
|
2009-07-04 11:59:50 -04:00
|
|
|
_INDENT = re.compile(r"^[ \t]*")
|
|
|
|
|
2009-07-04 15:58:13 -04:00
|
|
|
def __init__(self,trigger,value, descr):
|
2009-06-23 08:45:04 -04:00
|
|
|
self._t = trigger
|
|
|
|
self._v = value
|
2009-07-04 15:58:13 -04:00
|
|
|
self._d = descr
|
|
|
|
|
|
|
|
def description(self):
|
|
|
|
return self._d
|
|
|
|
description = property(description)
|
2009-06-28 08:51:27 -04:00
|
|
|
|
2009-06-23 08:45:04 -04:00
|
|
|
def trigger(self):
|
|
|
|
return self._t
|
2009-07-02 13:19:47 -04:00
|
|
|
trigger = property(trigger)
|
2009-06-28 08:51:27 -04:00
|
|
|
|
2009-06-23 08:45:04 -04:00
|
|
|
|
2009-06-28 16:22:19 -04:00
|
|
|
def launch(self, before, after):
|
2009-07-02 08:22:13 -04:00
|
|
|
lineno, col = vim.current.window.cursor
|
|
|
|
start = Position(lineno-1,col - len(self._t))
|
|
|
|
end = Position(lineno-1,col)
|
2009-07-02 09:41:58 -04:00
|
|
|
|
2009-07-03 04:59:55 -04:00
|
|
|
line = vim.current.line
|
2009-07-02 09:41:58 -04:00
|
|
|
|
2009-07-03 04:59:55 -04:00
|
|
|
text_before = line[:start.col]
|
|
|
|
text_after = line[end.col:]
|
2009-07-01 04:39:46 -04:00
|
|
|
|
2009-07-06 04:40:07 -04:00
|
|
|
indent = self._INDENT.match(text_before).group(0)
|
2009-07-04 11:59:50 -04:00
|
|
|
v = self._v
|
2009-07-06 04:40:07 -04:00
|
|
|
if len(indent):
|
2009-07-04 15:58:13 -04:00
|
|
|
lines = self._v.splitlines()
|
2009-07-06 04:40:07 -04:00
|
|
|
v = lines[0]
|
|
|
|
if len(lines) > 1:
|
|
|
|
v += os.linesep + \
|
|
|
|
os.linesep.join([indent + l for l in lines[1:]])
|
2009-07-04 15:58:13 -04:00
|
|
|
|
2009-07-04 11:59:50 -04:00
|
|
|
s = SnippetInstance(start, end, v, text_before, text_after)
|
2009-07-02 09:41:58 -04:00
|
|
|
|
|
|
|
if s.has_tabs():
|
2009-06-28 16:22:19 -04:00
|
|
|
s.select_next_tab()
|
|
|
|
return s
|
2009-07-02 08:22:13 -04:00
|
|
|
else:
|
2009-07-03 04:59:55 -04:00
|
|
|
vim.current.window.cursor = s.end.line + 1, s.end.col
|
2009-06-28 08:51:27 -04:00
|
|
|
|
2009-07-06 02:22:46 -04:00
|
|
|
class VimState(object):
|
2009-07-04 06:45:35 -04:00
|
|
|
def __init__(self):
|
|
|
|
self._abs_pos = None
|
|
|
|
self._moved = Position(0,0)
|
|
|
|
|
2009-07-05 16:25:01 -04:00
|
|
|
self._lines = None
|
|
|
|
self._dlines = None
|
|
|
|
self._cols = None
|
|
|
|
self._dcols = None
|
2009-07-06 04:31:12 -04:00
|
|
|
self._lline = None
|
2009-07-05 16:25:01 -04:00
|
|
|
self._text_changed = None
|
|
|
|
|
2009-07-06 02:22:46 -04:00
|
|
|
def update(self):
|
2009-07-04 06:45:35 -04:00
|
|
|
line, col = vim.current.window.cursor
|
|
|
|
line -= 1
|
|
|
|
abs_pos = Position(line,col)
|
|
|
|
if self._abs_pos:
|
|
|
|
self._moved = abs_pos - self._abs_pos
|
|
|
|
self._abs_pos = abs_pos
|
|
|
|
|
2009-07-06 02:22:46 -04:00
|
|
|
# Update buffer infos
|
2009-07-05 16:25:01 -04:00
|
|
|
cols = len(vim.current.buffer[line])
|
|
|
|
if self._cols:
|
|
|
|
self._dcols = cols - self._cols
|
|
|
|
self._cols = cols
|
|
|
|
|
|
|
|
lines = len(vim.current.buffer)
|
|
|
|
if self._lines:
|
|
|
|
self._dlines = lines - self._lines
|
|
|
|
self._lines = lines
|
|
|
|
|
2009-07-06 02:22:46 -04:00
|
|
|
# Check if the buffer has changed in any ways
|
|
|
|
self._text_changed = False
|
2009-07-06 04:31:12 -04:00
|
|
|
# does it have more lines?
|
|
|
|
if self._dlines:
|
|
|
|
self._text_changed = True
|
|
|
|
# did we stay in the same line and it has more columns now?
|
|
|
|
elif not self.moved.line and self._dcols:
|
|
|
|
self._text_changed = True
|
|
|
|
# If the length didn't change but we moved a column, check if
|
|
|
|
# the char under the cursor has changed (might be one char tab).
|
|
|
|
elif self.moved.col == 1:
|
|
|
|
debug("self._ll: %s" % (self._lline))
|
|
|
|
debug("vim.current.buffer[line]: %s" % (vim.current.buffer[line]))
|
|
|
|
|
|
|
|
self._text_changed = self._lline != vim.current.buffer[line]
|
|
|
|
self._lline = vim.current.buffer[line]
|
2009-07-06 02:22:46 -04:00
|
|
|
|
2009-07-05 16:25:01 -04:00
|
|
|
def buf_changed(self):
|
|
|
|
return self._text_changed
|
|
|
|
buf_changed = property(buf_changed)
|
|
|
|
|
|
|
|
def nr_lines(self):
|
|
|
|
return self._lines
|
|
|
|
nr_lines = property(nr_lines)
|
|
|
|
|
|
|
|
def cols_in_line(self):
|
|
|
|
return self._cols
|
|
|
|
cols_in_line = property(cols_in_line)
|
|
|
|
|
2009-07-04 06:45:35 -04:00
|
|
|
def pos(self):
|
|
|
|
return self._abs_pos
|
|
|
|
pos = property(pos)
|
|
|
|
|
2009-07-04 09:01:19 -04:00
|
|
|
def ppos(self):
|
|
|
|
if not self.has_moved:
|
|
|
|
return self.pos
|
|
|
|
return self.pos - self.moved
|
|
|
|
ppos = property(ppos)
|
|
|
|
|
2009-07-04 06:45:35 -04:00
|
|
|
def moved(self):
|
|
|
|
return self._moved
|
|
|
|
moved = property(moved)
|
|
|
|
|
|
|
|
def has_moved(self):
|
|
|
|
return bool(self._moved.line or self._moved.col)
|
|
|
|
has_moved = property(has_moved)
|
|
|
|
|
2009-06-23 08:45:04 -04:00
|
|
|
class SnippetManager(object):
|
|
|
|
def __init__(self):
|
2009-06-28 16:22:19 -04:00
|
|
|
self.reset()
|
2009-07-04 09:01:19 -04:00
|
|
|
|
2009-07-06 02:22:46 -04:00
|
|
|
self._vstate = VimState()
|
2009-07-05 16:25:01 -04:00
|
|
|
self._accept_input = False
|
2009-07-04 06:45:35 -04:00
|
|
|
|
2009-07-06 04:31:12 -04:00
|
|
|
self._expect_move_wo_change = False
|
|
|
|
|
2009-06-28 16:22:19 -04:00
|
|
|
|
2009-07-04 06:14:13 -04:00
|
|
|
def _load_snippets_from(self, ft, fn):
|
|
|
|
cs = None
|
|
|
|
cv = ""
|
2009-07-04 15:58:13 -04:00
|
|
|
cdescr = ""
|
2009-07-04 06:14:13 -04:00
|
|
|
for line in open(fn):
|
|
|
|
if line.startswith("#"):
|
|
|
|
continue
|
|
|
|
if line.startswith("snippet"):
|
|
|
|
cs = line.split()[1]
|
2009-07-04 15:58:13 -04:00
|
|
|
left = line.find('"')
|
|
|
|
if left != -1:
|
|
|
|
right = line.rfind('"')
|
|
|
|
cdescr = line[left+1:right]
|
2009-07-04 06:14:13 -04:00
|
|
|
continue
|
|
|
|
if cs != None:
|
|
|
|
if line.startswith("endsnippet"):
|
|
|
|
cv = cv[:-1] # Chop the last newline
|
2009-07-04 15:58:13 -04:00
|
|
|
l = self._snippets[ft].get(cs,[])
|
|
|
|
l.append(Snippet(cs,cv,cdescr))
|
|
|
|
self._snippets[ft][cs] = l
|
2009-07-04 06:14:13 -04:00
|
|
|
cv = ""
|
2009-07-04 15:58:13 -04:00
|
|
|
cdescr = ""
|
2009-07-04 06:14:13 -04:00
|
|
|
cs = None
|
|
|
|
continue
|
|
|
|
else:
|
|
|
|
cv += line
|
|
|
|
|
|
|
|
|
|
|
|
def _load_snippets_for(self, ft):
|
|
|
|
self._snippets[ft] = {}
|
|
|
|
for p in vim.eval("&runtimepath").split(','):
|
|
|
|
fn = p + os.path.sep + "PySnippets" + os.path.sep + \
|
|
|
|
"%s.snippets" % ft
|
|
|
|
if os.path.exists(fn):
|
|
|
|
self._load_snippets_from(ft, fn)
|
|
|
|
|
|
|
|
|
2009-06-28 16:22:19 -04:00
|
|
|
def reset(self):
|
2009-07-01 04:39:46 -04:00
|
|
|
self._snippets = {}
|
2009-06-28 16:22:19 -04:00
|
|
|
self._current_snippets = []
|
2009-06-28 08:51:27 -04:00
|
|
|
|
2009-07-05 16:25:01 -04:00
|
|
|
def add_snippet(self, trigger, value, descr):
|
2009-07-04 06:14:13 -04:00
|
|
|
if "all" not in self._snippets:
|
|
|
|
self._snippets["all"] = {}
|
2009-07-04 15:58:13 -04:00
|
|
|
l = self._snippets["all"].get(trigger,[])
|
|
|
|
l.append(Snippet(trigger,value, descr))
|
|
|
|
self._snippets["all"][trigger] = l
|
|
|
|
|
|
|
|
def _find_snippets(self, ft, trigger):
|
|
|
|
snips = self._snippets.get(ft,None)
|
|
|
|
if not snips:
|
|
|
|
return []
|
|
|
|
|
|
|
|
return snips.get(trigger, [])
|
2009-06-28 08:51:27 -04:00
|
|
|
|
2009-07-01 14:03:29 -04:00
|
|
|
def try_expand(self, backwards = False):
|
2009-07-04 06:14:13 -04:00
|
|
|
ft = vim.eval("&filetype")
|
|
|
|
if len(ft) and ft not in self._snippets:
|
|
|
|
self._load_snippets_for(ft)
|
|
|
|
if "all" not in self._snippets:
|
|
|
|
self._load_snippets_for("all")
|
|
|
|
|
2009-07-05 16:25:01 -04:00
|
|
|
self._accept_input = False
|
|
|
|
self._expect_move_wo_change = False
|
2009-07-04 06:14:13 -04:00
|
|
|
|
2009-06-28 16:22:19 -04:00
|
|
|
if len(self._current_snippets):
|
2009-07-01 14:03:29 -04:00
|
|
|
cs = self._current_snippets[-1]
|
2009-07-05 16:25:01 -04:00
|
|
|
self._expect_move_wo_change = True
|
2009-07-01 14:03:29 -04:00
|
|
|
if not cs.select_next_tab(backwards):
|
2009-07-06 05:35:18 -04:00
|
|
|
|
|
|
|
# HACK: only jump to end if there is no zero defined. This
|
|
|
|
# TODO: this jump should be inside select_next_tab or even
|
|
|
|
# better: when the snippet is launched and no parent snippet is
|
|
|
|
# defined, a $0 should be appended to the end of it and this
|
|
|
|
# extra code should be ignored Jump to the end of the snippet
|
|
|
|
# and enter insert mode
|
2009-07-04 11:36:06 -04:00
|
|
|
cs = self._current_snippets[-1]
|
2009-07-06 05:35:18 -04:00
|
|
|
if 0 not in cs._tabstops:
|
|
|
|
vim.current.window.cursor = cs.end.line +1, cs.end.col
|
|
|
|
vim.command(r'call feedkeys("\<Esc>a")')
|
2009-07-06 05:38:19 -04:00
|
|
|
self._current_snippets.pop()
|
2009-07-04 11:36:06 -04:00
|
|
|
|
|
|
|
return True
|
2009-07-06 11:44:04 -04:00
|
|
|
# else:
|
|
|
|
# vim.command(":au CursorMoved * py PySnipSnippets.normal_mode_moved()")
|
2009-07-04 06:45:35 -04:00
|
|
|
|
2009-07-06 02:22:46 -04:00
|
|
|
self._vstate.update()
|
2009-07-05 16:25:01 -04:00
|
|
|
self._accept_input = True
|
2009-07-04 10:08:14 -04:00
|
|
|
return True
|
2009-06-28 16:22:19 -04:00
|
|
|
|
2009-06-23 08:45:04 -04:00
|
|
|
dummy,col = vim.current.window.cursor
|
2009-07-04 06:14:13 -04:00
|
|
|
if col == 0:
|
2009-07-04 10:08:14 -04:00
|
|
|
return False
|
2009-07-04 06:14:13 -04:00
|
|
|
|
|
|
|
line = vim.current.line
|
2009-06-23 08:45:04 -04:00
|
|
|
|
|
|
|
if col > 0 and line[col-1] in string.whitespace:
|
2009-07-04 10:08:14 -04:00
|
|
|
return False
|
2009-06-23 08:45:04 -04:00
|
|
|
|
|
|
|
# Get the word to the left of the current edit position
|
|
|
|
before,after = line[:col], line[col:]
|
|
|
|
|
|
|
|
word = before.split()[-1]
|
2009-07-04 15:58:13 -04:00
|
|
|
snippets = []
|
2009-07-04 06:14:13 -04:00
|
|
|
if len(ft):
|
2009-07-04 15:58:13 -04:00
|
|
|
snippets += self._find_snippets(ft, word)
|
|
|
|
snippets += self._find_snippets("all", word)
|
2009-07-04 06:14:13 -04:00
|
|
|
|
2009-07-04 15:58:13 -04:00
|
|
|
if not len(snippets):
|
2009-07-04 06:14:13 -04:00
|
|
|
# No snippet found
|
2009-07-04 10:08:14 -04:00
|
|
|
return False
|
2009-07-04 15:58:13 -04:00
|
|
|
elif len(snippets) == 1:
|
|
|
|
snippet, = snippets
|
|
|
|
else:
|
|
|
|
display = repr(
|
|
|
|
[ "%i: %s" % (i+1,s.description) for i,s in
|
|
|
|
enumerate(snippets)
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
|
|
|
rv = vim.eval("inputlist(%s)" % display)
|
|
|
|
if rv is None:
|
|
|
|
return True
|
|
|
|
rv = int(rv)
|
|
|
|
snippet = snippets[rv-1]
|
2009-07-04 06:14:13 -04:00
|
|
|
|
2009-07-05 16:25:01 -04:00
|
|
|
self._expect_move_wo_change = True
|
2009-07-04 06:14:13 -04:00
|
|
|
s = snippet.launch(before.rstrip()[:-len(word)], after)
|
2009-07-04 06:45:35 -04:00
|
|
|
|
2009-07-06 02:22:46 -04:00
|
|
|
self._vstate.update()
|
2009-07-04 06:14:13 -04:00
|
|
|
if s is not None:
|
|
|
|
self._current_snippets.append(s)
|
2009-07-05 16:25:01 -04:00
|
|
|
self._accept_input = True
|
2009-07-06 11:44:04 -04:00
|
|
|
# vim.command(":au CursorMoved * py PySnipSnippets.normal_mode_moved()")
|
2009-07-06 02:22:46 -04:00
|
|
|
|
2009-07-01 04:39:46 -04:00
|
|
|
|
2009-07-04 10:08:14 -04:00
|
|
|
return True
|
|
|
|
|
2009-06-28 16:22:19 -04:00
|
|
|
def cursor_moved(self):
|
2009-07-05 07:20:58 -04:00
|
|
|
debug("Cursor moved")
|
2009-07-05 12:46:08 -04:00
|
|
|
|
2009-07-06 02:22:46 -04:00
|
|
|
self._vstate.update()
|
2009-07-05 16:25:01 -04:00
|
|
|
|
2009-07-06 02:22:46 -04:00
|
|
|
debug("self._vstate._dlines: %s" % (self._vstate._dlines))
|
|
|
|
debug("self._vstate._dcols: %s" % (self._vstate._dcols))
|
|
|
|
debug("self._vstate.buf_changed: %s" % (self._vstate.buf_changed))
|
|
|
|
if not self._vstate.buf_changed and not self._expect_move_wo_change:
|
2009-07-05 16:25:01 -04:00
|
|
|
# Cursor moved without input.
|
|
|
|
self._accept_input = False
|
2009-07-06 04:40:07 -04:00
|
|
|
|
2009-07-06 04:31:12 -04:00
|
|
|
# Did we leave the snippet with this movement?
|
|
|
|
debug("Checking if we left the snippet")
|
|
|
|
debug("self._vstate.pos: %s" % (self._vstate.pos))
|
|
|
|
|
|
|
|
if len(self._current_snippets):
|
|
|
|
cs = self._current_snippets[-1]
|
|
|
|
debug("cs.span: %s" % (cs.span))
|
|
|
|
|
|
|
|
is_inside = self._vstate.pos in cs.span
|
|
|
|
|
|
|
|
debug("is_inside: %s" % (is_inside))
|
2009-07-01 09:20:40 -04:00
|
|
|
|
2009-07-06 04:31:12 -04:00
|
|
|
if not is_inside:
|
|
|
|
self._current_snippets.pop()
|
2009-07-06 04:40:07 -04:00
|
|
|
|
2009-07-06 04:31:12 -04:00
|
|
|
if not self._accept_input:
|
|
|
|
return
|
|
|
|
|
|
|
|
if self._vstate.buf_changed and len(self._current_snippets):
|
2009-07-06 02:22:46 -04:00
|
|
|
if 0 <= self._vstate.moved.line <= 1:
|
2009-07-01 09:20:40 -04:00
|
|
|
cs = self._current_snippets[-1]
|
|
|
|
|
2009-07-02 05:58:46 -04:00
|
|
|
# Detect a carriage return
|
2009-07-06 02:22:46 -04:00
|
|
|
if self._vstate.moved.col < 0 and self._vstate.moved.line == 1:
|
2009-07-03 05:46:42 -04:00
|
|
|
# Hack, remove a line in vim, because we are going to
|
|
|
|
# overwrite the old line range with the new snippet value.
|
|
|
|
# After the expansion, we put the cursor were the user left
|
|
|
|
# it. This action should be completely transparent for the
|
|
|
|
# user
|
|
|
|
cache_pos = vim.current.window.cursor
|
2009-07-06 02:22:46 -04:00
|
|
|
del vim.current.buffer[self._vstate.pos.line-1]
|
|
|
|
cs.chars_entered('\n', self._vstate)
|
2009-07-03 05:46:42 -04:00
|
|
|
vim.current.window.cursor = cache_pos
|
2009-07-06 02:22:46 -04:00
|
|
|
elif self._vstate.moved.col < 0: # Some deleting was going on
|
|
|
|
cs.backspace(-self._vstate.moved.col, self._vstate)
|
2009-07-01 09:20:40 -04:00
|
|
|
else:
|
|
|
|
line = vim.current.line
|
2009-07-04 09:01:19 -04:00
|
|
|
|
2009-07-06 02:22:46 -04:00
|
|
|
chars = line[self._vstate.pos.col - self._vstate.moved.col:
|
|
|
|
self._vstate.pos.col]
|
|
|
|
cs.chars_entered(chars, self._vstate)
|
2009-07-01 11:11:33 -04:00
|
|
|
|
2009-07-06 02:22:46 -04:00
|
|
|
self._vstate.update()
|
2009-06-28 16:22:19 -04:00
|
|
|
|
2009-07-05 16:25:01 -04:00
|
|
|
self._expect_move_wo_change = False
|
|
|
|
|
2009-06-28 16:22:19 -04:00
|
|
|
def entered_insert_mode(self):
|
2009-07-06 11:44:04 -04:00
|
|
|
debug("Entered insert mode")
|
2009-07-06 12:34:12 -04:00
|
|
|
|
2009-07-06 10:55:48 -04:00
|
|
|
self._vstate.update()
|
|
|
|
debug("self._vstate.has_moved: %s" % (self._vstate.has_moved))
|
2009-07-04 11:36:06 -04:00
|
|
|
if len(self._current_snippets) and \
|
2009-07-06 10:55:48 -04:00
|
|
|
self._vstate.has_moved:
|
|
|
|
# not self._current_snippets[-1].tab_selected and \
|
|
|
|
|
2009-07-04 11:36:06 -04:00
|
|
|
self._current_snippets = []
|
|
|
|
|
2009-07-06 12:34:12 -04:00
|
|
|
def backspace(self):
|
2009-07-06 11:44:04 -04:00
|
|
|
# BS was called in select mode
|
2009-07-06 12:34:12 -04:00
|
|
|
|
2009-07-06 11:44:04 -04:00
|
|
|
if len(self._current_snippets) and \
|
2009-07-06 12:34:12 -04:00
|
|
|
self._current_snippets[-1].tab_selected:
|
2009-07-06 11:44:04 -04:00
|
|
|
# This only happens when a default value is delted using backspace
|
|
|
|
vim.command(r'call feedkeys("i")')
|
|
|
|
cs = self._current_snippets[-1]
|
|
|
|
cs.chars_entered('', self._vstate)
|
|
|
|
self._vstate.update()
|
|
|
|
else:
|
|
|
|
vim.command(r'call feedkeys("\<BS>")')
|
2009-06-23 08:45:04 -04:00
|
|
|
|
|
|
|
PySnipSnippets = SnippetManager()
|
2009-07-01 14:14:54 -04:00
|
|
|
|