2007-07-21 11:40:53 -04:00
|
|
|
import re, sets, string
|
|
|
|
|
2007-10-21 20:55:29 -04:00
|
|
|
import color, method, minibuffer, mode, searchutil
|
2007-10-21 20:50:11 -04:00
|
|
|
from point import Point
|
2007-07-21 11:40:53 -04:00
|
|
|
|
2008-04-09 17:35:26 -04:00
|
|
|
subgroup_re = re.compile(r'((?:\\\\)*)\\(0|[1-9][0-9]*)')
|
|
|
|
|
2007-07-21 11:40:53 -04:00
|
|
|
class ReplaceOne(method.Method):
|
|
|
|
def execute(self, w, **vargs):
|
|
|
|
m = w.buffer.method
|
|
|
|
_replace(m)
|
|
|
|
_find_next(m, False)
|
|
|
|
_finish(m, w)
|
|
|
|
|
2008-04-02 19:37:44 -04:00
|
|
|
class ReplaceDone(method.Method):
|
|
|
|
def execute(self, w, **vargs):
|
|
|
|
m = w.buffer.method
|
|
|
|
_replace(m)
|
|
|
|
_end(w)
|
|
|
|
w.set_error("Replace done")
|
|
|
|
|
2007-07-21 11:40:53 -04:00
|
|
|
class SkipReplace(method.Method):
|
|
|
|
def execute(self, w, **vargs):
|
|
|
|
m = w.buffer.method
|
2007-08-11 10:16:41 -04:00
|
|
|
_find_next(m, True)
|
2007-07-21 11:40:53 -04:00
|
|
|
_finish(m, w)
|
|
|
|
|
|
|
|
class ReplaceAll(method.Method):
|
|
|
|
def execute(self, w, **vargs):
|
|
|
|
m = w.buffer.method
|
|
|
|
while m.p1 is not None:
|
|
|
|
_replace(m)
|
|
|
|
_find_next(m, False)
|
|
|
|
_end(w)
|
|
|
|
w.set_error("Replace ended")
|
|
|
|
|
|
|
|
class CancelReplace(method.Method):
|
|
|
|
def execute(self, w, **vargs):
|
|
|
|
_end(w)
|
|
|
|
w.set_error("Replace cancelled")
|
|
|
|
|
|
|
|
def _find_next(m, move=False):
|
2007-08-11 11:38:29 -04:00
|
|
|
s = m.before
|
|
|
|
w = m.old_window
|
|
|
|
c = w.logical_cursor()
|
2007-07-21 11:40:53 -04:00
|
|
|
try:
|
2007-08-11 11:38:29 -04:00
|
|
|
if m.is_literal:
|
|
|
|
r = re.compile(searchutil.escape_literal(s))
|
|
|
|
else:
|
|
|
|
r = re.compile(s)
|
2007-07-21 11:40:53 -04:00
|
|
|
except:
|
|
|
|
(m.p1, m.p2) = (None, None)
|
|
|
|
return False
|
|
|
|
|
2007-07-21 18:58:17 -04:00
|
|
|
newc = searchutil.find_next(r, w, move, start=c.add(0, 0))
|
2007-07-21 11:40:53 -04:00
|
|
|
if newc:
|
2008-04-09 17:35:26 -04:00
|
|
|
(m.p1, m.p2, m.match) = newc
|
2007-07-21 11:40:53 -04:00
|
|
|
return True
|
|
|
|
else:
|
2008-04-09 17:35:26 -04:00
|
|
|
(m.p1, m.p2, m.match) = (None, None, None)
|
2007-07-21 11:40:53 -04:00
|
|
|
return False
|
|
|
|
|
2008-04-09 17:35:26 -04:00
|
|
|
def _get_before(m):
|
|
|
|
if m.match is None:
|
|
|
|
return m.before
|
|
|
|
else:
|
|
|
|
return m.match.group(0)
|
|
|
|
|
|
|
|
def _get_after(m):
|
|
|
|
if m.after is None:
|
|
|
|
return None
|
|
|
|
elif m.match is None:
|
|
|
|
return m.after
|
|
|
|
|
|
|
|
def _repl(match):
|
|
|
|
(pre, num) = (match.group(1), int(match.group(2)))
|
|
|
|
if num == 0 or m.match.lastindex and num <= m.match.lastindex:
|
|
|
|
return pre + m.match.group(num)
|
|
|
|
else:
|
|
|
|
return match.group(0)
|
|
|
|
|
|
|
|
return subgroup_re.sub(_repl, m.after)
|
|
|
|
|
2007-07-21 11:40:53 -04:00
|
|
|
def _set_prompt(m):
|
|
|
|
w = m.old_window
|
|
|
|
if m.p1 is None:
|
|
|
|
w.application.mini_prompt = '%r was not found' % m.before
|
|
|
|
return
|
|
|
|
(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
|
2008-04-09 17:35:26 -04:00
|
|
|
|
|
|
|
after = _get_after(m)
|
|
|
|
before = _get_before(m)
|
|
|
|
|
2007-07-21 11:40:53 -04:00
|
|
|
if count > 1:
|
2008-04-09 17:35:26 -04:00
|
|
|
p = 'Replace %r with %r [ynadq] (%d occurances)?' % (before, after, count)
|
2007-07-21 11:40:53 -04:00
|
|
|
else:
|
2008-04-09 17:35:26 -04:00
|
|
|
p = 'Replace %r with %r [ynadq] (1 occurance)?' % (before, after)
|
2007-07-21 11:40:53 -04:00
|
|
|
w.application.mini_prompt = p
|
|
|
|
|
|
|
|
def _replace(m):
|
|
|
|
m.old_window.buffer.delete(m.p1, m.p2)
|
|
|
|
if m.after:
|
2008-04-09 17:35:26 -04:00
|
|
|
after = _get_after(m)
|
|
|
|
m.old_window.buffer.insert_string(m.p1, after)
|
2007-07-21 11:40:53 -04:00
|
|
|
|
|
|
|
def _finish(m, w):
|
|
|
|
if m.p1 is None:
|
|
|
|
_end(w)
|
|
|
|
w.set_error("Replace ended")
|
|
|
|
else:
|
|
|
|
_set_prompt(m)
|
|
|
|
|
|
|
|
def _end(w):
|
|
|
|
w.application.close_mini_buffer()
|
2008-05-03 17:05:24 -04:00
|
|
|
w.application.clear_highlighted_ranges('search')
|
2007-07-21 11:40:53 -04:00
|
|
|
w.buffer.method.old_cursor = None
|
|
|
|
w.buffer.method.old_window = None
|
|
|
|
assert not w.application.mini_active
|
2007-10-19 02:41:33 -04:00
|
|
|
|
2008-04-18 23:32:08 -04:00
|
|
|
class Replace(mode.Fundamental):
|
|
|
|
modename = 'Replace'
|
|
|
|
actions = [ReplaceAll, ReplaceDone, ReplaceOne, SkipReplace, CancelReplace]
|
|
|
|
def __init__(self, w):
|
|
|
|
mode.Fundamental.__init__(self, w)
|
|
|
|
|
|
|
|
self.actions = {}
|
|
|
|
self.bindings = {}
|
|
|
|
self.add_bindings('replace-all', ('a', '!',))
|
|
|
|
self.add_bindings('replace-done', ('d',))
|
|
|
|
self.add_bindings('replace-one', ('y', 'SPACE',))
|
|
|
|
self.add_bindings('skip-replace', ('n', 'DELETE',))
|
2008-06-10 14:29:19 -04:00
|
|
|
self.add_bindings('cancel-replace', ('q', 'RETURN', 'C-]', 'C-n', 'C-p', 'C-a', 'C-e', 'C-f', 'C-b', 'C-g'))
|
2008-04-18 23:32:08 -04:00
|
|
|
|
|
|
|
m = w.buffer.method
|
|
|
|
found = _find_next(m, False)
|
|
|
|
if not found:
|
|
|
|
w.set_error('%r was not found' % m.before)
|
|
|
|
raise minibuffer.MiniBufferError
|
|
|
|
_set_prompt(m)
|
|
|
|
|
2007-10-19 02:41:33 -04:00
|
|
|
install = Replace.install
|