pmacs3/search.py

64 lines
1.7 KiB
Python
Raw Normal View History

from point2 import Point
bg_color = 'black'
selected_color = 'magenta'
unselected_color = 'yellow'
def find_ranges(s, w):
(x, y) = (0, 0)
ranges = []
while y < len(w.buffer.lines):
while x < len(w.buffer.lines[y]):
try:
i = w.buffer.lines[y].index(s, x)
x = i + len(s)
ranges.append([Point(i, y), Point(x, y), bg_color, unselected_color])
except ValueError:
break
x = 0
y += 1
return ranges
def find(s, w, move=False, direction='next'):
app = w.application
c = w.logical_cursor()
newc = None
ranges = find_ranges(s, w)
indices = range(0, len(ranges))
(x, y) = c.xy()
if direction == 'next':
if move:
limit = Point(x, y)
else:
limit = Point(x - 1, y)
else:
if move:
limit = Point(x + len(s), y)
else:
limit = Point(x + len(s) + 1, y)
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
def find_previous(s, w, move=False):
return find(s, w, move, 'previous')
def find_next(s, w, move=False):
return find(s, w, move, 'next')