2007-03-06 10:05:38 -05:00
|
|
|
import sets, string
|
|
|
|
|
2007-06-14 06:10:20 -04:00
|
|
|
import color, method, minibuffer, mode2
|
|
|
|
from point2 import Point
|
2007-03-06 10:05:38 -05:00
|
|
|
|
2007-06-14 06:10:20 -04:00
|
|
|
class Replace(mode2.Fundamental):
|
2007-03-06 10:05:38 -05:00
|
|
|
'''This is the default mode'''
|
|
|
|
def __init__(self, w):
|
2007-06-14 06:10:20 -04:00
|
|
|
mode2.Fundamental.__init__(self, w)
|
2007-03-06 10:05:38 -05:00
|
|
|
|
|
|
|
self.actions = {}
|
|
|
|
self.bindings = {}
|
|
|
|
|
|
|
|
default_actions = ((ReplaceAll(), ('a', '!',)),
|
|
|
|
(ReplaceOne(), ('y', 'SPACE',)),
|
|
|
|
(SkipReplace(), ('n', 'DELETE',)),
|
|
|
|
(CancelReplace(), ('q', 'RETURN', 'C-]', 'C-n',
|
|
|
|
'C-p', 'C-a', 'C-e', 'C-f',
|
|
|
|
'C-b')))
|
|
|
|
|
|
|
|
# add the replace actions
|
|
|
|
for pair in default_actions:
|
|
|
|
(action, sequences) = pair
|
|
|
|
assert type(sequences) == type(()), repr(pair)
|
|
|
|
self.add_action_and_bindings(action, sequences)
|
|
|
|
|
|
|
|
_find_next(w, False)
|
|
|
|
_set_prompt(w.buffer.method, w.buffer.method.old_window)
|
|
|
|
|
|
|
|
def name(self):
|
|
|
|
return "Replace"
|
|
|
|
|
|
|
|
class ReplaceOne(method.Method):
|
|
|
|
def execute(self, window, **vargs):
|
|
|
|
m = window.buffer.method
|
|
|
|
old_window = m.old_window
|
|
|
|
_replace(m, old_window)
|
|
|
|
_find_next(window, True)
|
|
|
|
_finish(m, window, old_window)
|
|
|
|
|
|
|
|
class SkipReplace(method.Method):
|
|
|
|
def execute(self, window, **vargs):
|
|
|
|
m = window.buffer.method
|
|
|
|
old_window = m.old_window
|
|
|
|
_find_next(window, True)
|
|
|
|
_finish(m, window, old_window)
|
|
|
|
|
|
|
|
class ReplaceAll(method.Method):
|
|
|
|
def execute(self, window, **vargs):
|
|
|
|
m = window.buffer.method
|
|
|
|
old_window = m.old_window
|
|
|
|
while m.p1 is not None:
|
|
|
|
_replace(m, old_window)
|
|
|
|
_find_next(window, True)
|
|
|
|
_end(window)
|
|
|
|
window.application.set_error("Replace ended")
|
|
|
|
|
|
|
|
class CancelReplace(method.Method):
|
|
|
|
def execute(self, window, **vargs):
|
|
|
|
_end(window)
|
|
|
|
window.application.set_error("Replace cancelled")
|
|
|
|
|
|
|
|
def _set_prompt(m, window):
|
|
|
|
i = m.old_window.buffer.get_point_offset(m.p1)
|
|
|
|
s = m.old_window.buffer.make_string()
|
|
|
|
count = s[i:].count(m.before)
|
|
|
|
if count > 1:
|
|
|
|
window.application.mini_prompt = 'Replace %r with %r [ynaq] (%d occurances)?' % (m.before, m.after, count)
|
|
|
|
else:
|
|
|
|
window.application.mini_prompt = 'Replace %r with %r [ynaq] (1 occurance)?' % (m.before, m.after)
|
|
|
|
|
|
|
|
def _replace(m, old_window):
|
|
|
|
old_window.buffer.delete_string(m.p1, m.p2)
|
|
|
|
if m.after:
|
|
|
|
old_window.buffer.insert_string(m.p1, m.after)
|
|
|
|
|
|
|
|
def _find_next(window, move=False):
|
|
|
|
m = window.buffer.method
|
|
|
|
old_window = m.old_window
|
|
|
|
b = old_window.buffer
|
|
|
|
s = m.before
|
|
|
|
c = old_window.logical_cursor()
|
|
|
|
(x, y) = (c.x, c.y)
|
|
|
|
|
|
|
|
if move:
|
|
|
|
x += 1
|
|
|
|
l = b.lines[y][x:]
|
|
|
|
|
|
|
|
# for each line available
|
|
|
|
while y < len(b.lines):
|
|
|
|
if s in l:
|
|
|
|
# success
|
|
|
|
x = x + l.index(s)
|
|
|
|
x2 = x + len(s)
|
2007-06-14 06:10:20 -04:00
|
|
|
m.p1 = Point(x, y)
|
|
|
|
m.p2 = Point(x2, y)
|
2007-03-06 10:05:38 -05:00
|
|
|
old_window.goto(m.p1)
|
|
|
|
old_window.application.clear_highlighted_ranges()
|
|
|
|
old_window.application.add_highlighted_range(old_window, m.p1, m.p2)
|
|
|
|
#old_window.application.highlighted_range = [old_window, m.p1, m.p2]
|
|
|
|
_set_prompt(m, old_window)
|
|
|
|
return
|
|
|
|
elif y >= len(b.lines) - 1:
|
|
|
|
# failure
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
# keep trying
|
|
|
|
y += 1
|
|
|
|
l = b.lines[y]
|
|
|
|
x = 0
|
|
|
|
m.p1 = None
|
|
|
|
m.p2 = None
|
|
|
|
|
|
|
|
def _finish(m, window, old_window):
|
|
|
|
if m.p1 is None:
|
|
|
|
_end(window)
|
|
|
|
window.application.set_error("Replace ended")
|
|
|
|
else:
|
|
|
|
_set_prompt(m, old_window)
|
|
|
|
|
|
|
|
def _end(window):
|
|
|
|
window.application.close_mini_buffer()
|
|
|
|
#window.application.highlighted_range = []
|
|
|
|
window.application.clear_highlighted_ranges()
|
|
|
|
window.buffer.method.old_cursor = None
|
|
|
|
window.buffer.method.old_window = None
|