2007-03-06 10:05:38 -05:00
|
|
|
import sets, string
|
|
|
|
|
2007-07-06 14:34:14 -04:00
|
|
|
import color, method, minibuffer, mode2, search
|
2007-06-14 06:10:20 -04:00
|
|
|
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 = {}
|
|
|
|
|
2007-07-06 14:34:14 -04:00
|
|
|
self.add_action_and_bindings(ReplaceAll(), ('a', '!',))
|
|
|
|
self.add_action_and_bindings(ReplaceOne(), ('y', 'SPACE',))
|
|
|
|
self.add_action_and_bindings(SkipReplace(), ('n', 'DELETE',))
|
|
|
|
self.add_action_and_bindings(CancelReplace(), ('q', 'RETURN', 'C-]', 'C-n', 'C-p', 'C-a', 'C-e', 'C-f', 'C-b'))
|
2007-03-06 10:05:38 -05:00
|
|
|
|
2007-07-06 14:34:14 -04:00
|
|
|
m = w.buffer.method
|
|
|
|
found = _find_next(m, move=False)
|
|
|
|
if not found:
|
|
|
|
w.application.set_error('%r was not found' % m.before)
|
2007-06-18 14:50:48 -04:00
|
|
|
raise minibuffer.MiniBufferError
|
2007-07-06 14:34:14 -04:00
|
|
|
_set_prompt(m)
|
2007-03-06 10:05:38 -05:00
|
|
|
def name(self):
|
|
|
|
return "Replace"
|
|
|
|
|
|
|
|
class ReplaceOne(method.Method):
|
2007-06-14 08:41:55 -04:00
|
|
|
def execute(self, w, **vargs):
|
|
|
|
m = w.buffer.method
|
2007-07-06 14:34:14 -04:00
|
|
|
_replace(m)
|
|
|
|
_find_next(m, True)
|
|
|
|
_finish(m, w)
|
2007-03-06 10:05:38 -05:00
|
|
|
|
|
|
|
class SkipReplace(method.Method):
|
2007-06-14 08:41:55 -04:00
|
|
|
def execute(self, w, **vargs):
|
|
|
|
m = w.buffer.method
|
2007-07-06 14:34:14 -04:00
|
|
|
_find_next(m, True)
|
|
|
|
_finish(m, w)
|
2007-03-06 10:05:38 -05:00
|
|
|
|
|
|
|
class ReplaceAll(method.Method):
|
2007-06-14 08:41:55 -04:00
|
|
|
def execute(self, w, **vargs):
|
|
|
|
m = w.buffer.method
|
2007-03-06 10:05:38 -05:00
|
|
|
while m.p1 is not None:
|
2007-07-06 14:34:14 -04:00
|
|
|
_replace(m)
|
|
|
|
_find_next(m, True)
|
2007-06-14 08:41:55 -04:00
|
|
|
_end(w)
|
|
|
|
w.application.set_error("Replace ended")
|
2007-03-06 10:05:38 -05:00
|
|
|
|
|
|
|
class CancelReplace(method.Method):
|
2007-06-14 08:41:55 -04:00
|
|
|
def execute(self, w, **vargs):
|
|
|
|
_end(w)
|
|
|
|
w.application.set_error("Replace cancelled")
|
|
|
|
|
2007-07-06 14:34:14 -04:00
|
|
|
def _find_next(m, move=False):
|
|
|
|
s = m.before
|
|
|
|
w = m.old_window
|
2007-07-06 14:43:41 -04:00
|
|
|
newc = search.find_next(s, w, move)
|
2007-07-06 14:34:14 -04:00
|
|
|
if newc:
|
|
|
|
(m.p1, m.p2) = newc
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
(m.p1, m.p2) = (None, None)
|
|
|
|
return False
|
|
|
|
|
|
|
|
def _set_prompt(m):
|
|
|
|
w = m.old_window
|
2007-06-18 14:50:48 -04:00
|
|
|
if m.p1 is None:
|
|
|
|
w.application.mini_prompt = '%r was not found' % m.before
|
|
|
|
return
|
2007-06-14 08:41:55 -04:00
|
|
|
(x, y) = m.p1.xy()
|
|
|
|
count = 0
|
|
|
|
while y < len(w.buffer.lines):
|
|
|
|
count += w.buffer.lines[y][x:].count(m.before)
|
|
|
|
y += 1
|
|
|
|
x = 0
|
2007-03-06 10:05:38 -05:00
|
|
|
if count > 1:
|
2007-06-14 08:41:55 -04:00
|
|
|
p = 'Replace %r with %r [ynaq] (%d occurances)?' % (m.before, m.after, count)
|
2007-03-06 10:05:38 -05:00
|
|
|
else:
|
2007-06-14 08:41:55 -04:00
|
|
|
p = 'Replace %r with %r [ynaq] (1 occurance)?' % (m.before, m.after)
|
|
|
|
w.application.mini_prompt = p
|
2007-03-06 10:05:38 -05:00
|
|
|
|
2007-07-06 14:34:14 -04:00
|
|
|
def _replace(m):
|
|
|
|
m.old_window.buffer.delete(m.p1, m.p2)
|
2007-03-06 10:05:38 -05:00
|
|
|
if m.after:
|
2007-07-06 14:34:14 -04:00
|
|
|
m.old_window.buffer.insert_string(m.p1, m.after)
|
2007-03-06 10:05:38 -05:00
|
|
|
|
2007-07-06 14:34:14 -04:00
|
|
|
def _finish(m, w):
|
2007-03-06 10:05:38 -05:00
|
|
|
if m.p1 is None:
|
2007-06-14 08:41:55 -04:00
|
|
|
_end(w)
|
|
|
|
w.application.set_error("Replace ended")
|
2007-03-06 10:05:38 -05:00
|
|
|
else:
|
2007-07-06 14:34:14 -04:00
|
|
|
_set_prompt(m)
|
2007-03-06 10:05:38 -05:00
|
|
|
|
2007-06-14 08:41:55 -04:00
|
|
|
def _end(w):
|
|
|
|
w.application.close_mini_buffer()
|
|
|
|
w.application.clear_highlighted_ranges()
|
|
|
|
w.buffer.method.old_cursor = None
|
|
|
|
w.buffer.method.old_window = None
|
2007-06-18 14:50:48 -04:00
|
|
|
assert not w.application.mini_active
|