2007-07-09 17:47:15 -04:00
|
|
|
import re
|
|
|
|
import regex
|
2007-07-06 14:34:14 -04:00
|
|
|
from point2 import Point
|
|
|
|
|
|
|
|
bg_color = 'black'
|
|
|
|
selected_color = 'magenta'
|
|
|
|
unselected_color = 'yellow'
|
|
|
|
|
2007-07-09 17:47:15 -04:00
|
|
|
class IllegalPatternError(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def escape_literal(s):
|
|
|
|
return regex.meta_chars.sub(r'\\\1', s)
|
|
|
|
|
2007-07-09 18:31:59 -04:00
|
|
|
def find_ranges(r, w, start=None, end=None):
|
2007-07-06 15:53:47 -04:00
|
|
|
if not w.buffer.lines:
|
|
|
|
return []
|
|
|
|
|
|
|
|
if start is None:
|
|
|
|
(x, y) = (0, 0)
|
|
|
|
else:
|
|
|
|
(x, y) = start.xy()
|
|
|
|
if end is None:
|
2007-07-11 04:55:54 -04:00
|
|
|
(x2, y2) = (len(w.buffer.lines[-1]), len(w.buffer.lines) - 1)
|
2007-07-06 15:53:47 -04:00
|
|
|
else:
|
|
|
|
(x2, y2) = end.xy()
|
2007-07-09 17:47:15 -04:00
|
|
|
|
2007-07-06 14:34:14 -04:00
|
|
|
ranges = []
|
2007-07-06 15:53:47 -04:00
|
|
|
while y <= y2:
|
|
|
|
if y == y2:
|
|
|
|
limit = x2
|
|
|
|
else:
|
2007-07-11 04:55:54 -04:00
|
|
|
limit = len(w.buffer.lines[y])
|
2007-07-09 17:47:15 -04:00
|
|
|
for m in r.finditer(w.buffer.lines[y], x, limit):
|
|
|
|
if len(m.group(0)) == 0:
|
2007-07-09 18:31:59 -04:00
|
|
|
raise IllegalPatternError, "zero-width match found"
|
2007-07-09 17:47:15 -04:00
|
|
|
ranges.append([Point(m.start(), y), Point(m.end(), y), bg_color, unselected_color])
|
2007-07-06 14:34:14 -04:00
|
|
|
x = 0
|
|
|
|
y += 1
|
|
|
|
return ranges
|
|
|
|
|
2007-07-09 18:31:59 -04:00
|
|
|
def find(r, w, move=False, direction='next', start=None, end=None):
|
2007-07-06 14:34:14 -04:00
|
|
|
app = w.application
|
|
|
|
c = w.logical_cursor()
|
|
|
|
newc = None
|
2007-07-09 18:31:59 -04:00
|
|
|
ranges = find_ranges(r, w, start, end)
|
2007-07-06 14:34:14 -04:00
|
|
|
indices = range(0, len(ranges))
|
|
|
|
(x, y) = c.xy()
|
2007-07-06 15:53:47 -04:00
|
|
|
if move:
|
|
|
|
offset = 1
|
|
|
|
else:
|
|
|
|
offset = 0
|
2007-07-06 14:34:14 -04:00
|
|
|
if direction == 'next':
|
2007-07-06 15:53:47 -04:00
|
|
|
limit = Point(x - 1 + offset, y)
|
2007-07-06 14:34:14 -04:00
|
|
|
else:
|
2007-07-09 18:31:59 -04:00
|
|
|
limit = Point(x + 1 - offset, y)
|
|
|
|
for stuff in app.highlighted_ranges:
|
|
|
|
(wz, p1, p2, fg, bg) = stuff
|
|
|
|
if p1.y == c.y and p1.x == c.x:
|
|
|
|
limit = Point(x + 1 + p2.x - p1.x - offset, y)
|
|
|
|
break
|
2007-07-06 14:34:14 -04:00
|
|
|
indices.reverse()
|
|
|
|
for i in indices:
|
|
|
|
if ((direction == 'next' and ranges[i][0] > limit) or
|
|
|
|
(direction != 'next' and ranges[i][1] < limit)):
|
|
|
|
ranges[i][3] = selected_color
|
|
|
|
newc = (ranges[i][0], ranges[i][1])
|
|
|
|
break
|
|
|
|
if ranges and not newc:
|
|
|
|
return None
|
|
|
|
app.clear_highlighted_ranges()
|
|
|
|
if newc:
|
|
|
|
w.goto(newc[0])
|
|
|
|
for (p1, p2, fg, bg) in ranges:
|
|
|
|
if p1 < w.first:
|
|
|
|
continue
|
|
|
|
elif p2 > w.last:
|
|
|
|
break
|
|
|
|
app.add_highlighted_range(w, p1, p2, fg, bg)
|
|
|
|
return newc
|
|
|
|
|
2007-07-09 18:31:59 -04:00
|
|
|
def find_previous(r, w, move=False, start=None, end=None):
|
|
|
|
return find(r, w, move, 'previous', start, end)
|
|
|
|
def find_next(r, w, move=False, start=None, end=None):
|
|
|
|
return find(r, w, move, 'next', start, end)
|