branch : pmacs2
This commit is contained in:
moculus 2007-07-06 19:53:47 +00:00
parent b4e7119363
commit 12a4b34c58
2 changed files with 40 additions and 26 deletions

View File

@ -19,7 +19,7 @@ class Replace(mode2.Fundamental):
m = w.buffer.method m = w.buffer.method
found = _find_next(m, move=False) found = _find_next(m, move=False)
if not found: 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 raise minibuffer.MiniBufferError
_set_prompt(m) _set_prompt(m)
def name(self): def name(self):
@ -45,17 +45,18 @@ class ReplaceAll(method.Method):
_replace(m) _replace(m)
_find_next(m, True) _find_next(m, True)
_end(w) _end(w)
w.application.set_error("Replace ended") w.set_error("Replace ended")
class CancelReplace(method.Method): class CancelReplace(method.Method):
def execute(self, w, **vargs): def execute(self, w, **vargs):
_end(w) _end(w)
w.application.set_error("Replace cancelled") w.set_error("Replace cancelled")
def _find_next(m, move=False): def _find_next(m, move=False):
s = m.before s = m.before
w = m.old_window 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: if newc:
(m.p1, m.p2) = newc (m.p1, m.p2) = newc
return True return True
@ -88,7 +89,7 @@ def _replace(m):
def _finish(m, w): def _finish(m, w):
if m.p1 is None: if m.p1 is None:
_end(w) _end(w)
w.application.set_error("Replace ended") w.set_error("Replace ended")
else: else:
_set_prompt(m) _set_prompt(m)

View File

@ -4,38 +4,51 @@ bg_color = 'black'
selected_color = 'magenta' selected_color = 'magenta'
unselected_color = 'yellow' unselected_color = 'yellow'
def find_ranges(s, w): def find_ranges(s, w, start=None, end=None):
if not w.buffer.lines:
return []
if start is None:
(x, y) = (0, 0) (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 = [] ranges = []
while y < len(w.buffer.lines): while y <= y2:
while x < len(w.buffer.lines[y]): if y == y2:
limit = x2
else:
limit = len(w.buffer.lines[y]) - 1
while x <= limit:
try: try:
i = w.buffer.lines[y].index(s, x) 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: except ValueError:
break break
x = i + len(s)
ranges.append([Point(i, y), Point(x, y), bg_color, unselected_color])
x = 0 x = 0
y += 1 y += 1
return ranges 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 app = w.application
c = w.logical_cursor() c = w.logical_cursor()
newc = None newc = None
ranges = find_ranges(s, w) ranges = find_ranges(s, w, start, end)
indices = range(0, len(ranges)) indices = range(0, len(ranges))
(x, y) = c.xy() (x, y) = c.xy()
if move:
offset = 1
else:
offset = 0
if direction == 'next': if direction == 'next':
if move: limit = Point(x - 1 + offset, y)
limit = Point(x, y)
else: else:
limit = Point(x - 1, y) limit = Point(x + len(s) + 1 - offset, y)
else:
if move:
limit = Point(x + len(s), y)
else:
limit = Point(x + len(s) + 1, y)
indices.reverse() indices.reverse()
for i in indices: for i in indices:
if ((direction == 'next' and ranges[i][0] > limit) or 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) app.add_highlighted_range(w, p1, p2, fg, bg)
return newc return newc
def find_previous(s, w, move=False): def find_previous(s, w, move=False, start=None, end=None):
return find(s, w, move, 'previous') return find(s, w, move, 'previous', start, end)
def find_next(s, w, move=False): def find_next(s, w, move=False, start=None, end=None):
return find(s, w, move, 'next') return find(s, w, move, 'next', start, end)