diff --git a/mode_replace.py b/mode_replace.py index ab8cb0c..8aafe05 100644 --- a/mode_replace.py +++ b/mode_replace.py @@ -19,7 +19,7 @@ class Replace(mode2.Fundamental): m = w.buffer.method found = _find_next(m, move=False) if not found: - w.application.set_error('%r was not found' % m.before) + w.set_error('%r was not found' % m.before) raise minibuffer.MiniBufferError _set_prompt(m) def name(self): @@ -45,17 +45,18 @@ class ReplaceAll(method.Method): _replace(m) _find_next(m, True) _end(w) - w.application.set_error("Replace ended") + w.set_error("Replace ended") class CancelReplace(method.Method): def execute(self, w, **vargs): _end(w) - w.application.set_error("Replace cancelled") + w.set_error("Replace cancelled") def _find_next(m, move=False): s = m.before w = m.old_window - newc = search.find_next(s, w, move) + c = w.logical_cursor() + newc = search.find_next(s, w, move, start=c.add(1, 0)) if newc: (m.p1, m.p2) = newc return True @@ -88,7 +89,7 @@ def _replace(m): def _finish(m, w): if m.p1 is None: _end(w) - w.application.set_error("Replace ended") + w.set_error("Replace ended") else: _set_prompt(m) diff --git a/search.py b/search.py index 3c0c354..5da9ae2 100644 --- a/search.py +++ b/search.py @@ -4,38 +4,51 @@ bg_color = 'black' selected_color = 'magenta' unselected_color = 'yellow' -def find_ranges(s, w): - (x, y) = (0, 0) +def find_ranges(s, w, start=None, end=None): + if not w.buffer.lines: + return [] + + if start is None: + (x, y) = (0, 0) + else: + (x, y) = start.xy() + if end is None: + (x2, y2) = (len(w.buffer.lines[-1]) - 1, len(w.buffer.lines) - 1) + else: + (x2, y2) = end.xy() + ranges = [] - while y < len(w.buffer.lines): - while x < len(w.buffer.lines[y]): + while y <= y2: + if y == y2: + limit = x2 + else: + limit = len(w.buffer.lines[y]) - 1 + while x <= limit: 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 = i + len(s) + ranges.append([Point(i, y), Point(x, y), bg_color, unselected_color]) x = 0 y += 1 return ranges -def find(s, w, move=False, direction='next'): +def find(s, w, move=False, direction='next', start=None, end=None): app = w.application c = w.logical_cursor() newc = None - ranges = find_ranges(s, w) + ranges = find_ranges(s, w, start, end) indices = range(0, len(ranges)) (x, y) = c.xy() - if direction == 'next': - if move: - limit = Point(x, y) - else: - limit = Point(x - 1, y) + if move: + offset = 1 else: - if move: - limit = Point(x + len(s), y) - else: - limit = Point(x + len(s) + 1, y) + offset = 0 + if direction == 'next': + limit = Point(x - 1 + offset, y) + else: + limit = Point(x + len(s) + 1 - offset, y) indices.reverse() for i in indices: if ((direction == 'next' and ranges[i][0] > limit) or @@ -56,8 +69,8 @@ def find(s, w, move=False, direction='next'): 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_previous(s, w, move=False, start=None, end=None): + return find(s, w, move, 'previous', start, end) -def find_next(s, w, move=False): - return find(s, w, move, 'next') +def find_next(s, w, move=False, start=None, end=None): + return find(s, w, move, 'next', start, end)