from idlelib import SearchEngine
from idlelib.SearchDialogBase import SearchDialogBase
engine = SearchEngine.get(root)
if not hasattr(engine, "_replacedialog"):
engine._replacedialog = ReplaceDialog(root, engine)
dialog = engine._replacedialog
class ReplaceDialog(SearchDialogBase):
def __init__(self, root, engine):
SearchDialogBase.__init__(self, root, engine)
self.replvar = StringVar(root)
SearchDialogBase.open(self, text)
first = text.index("sel.first")
last = text.index("sel.last")
first = first or text.index("insert")
self.show_hit(first, last)
def create_entries(self):
SearchDialogBase.create_entries(self)
self.replent = self.make_entry("Replace with:", self.replvar)[0]
def create_command_buttons(self):
SearchDialogBase.create_command_buttons(self)
self.make_button("Find", self.find_it)
self.make_button("Replace", self.replace_it)
self.make_button("Replace+Find", self.default_command, 1)
self.make_button("Replace All", self.replace_all)
def find_it(self, event=None):
def replace_it(self, event=None):
if self.do_find(self.ok):
def default_command(self, event=None):
if self.do_find(self.ok):
if self.do_replace(): # Only find next match if replace succeeded.
# A bad re can cause it to fail.
def _replace_expand(self, m, repl):
""" Helper function for expanding a regular expression
in the replace field, if needed. """
self.engine.report_error(repl, 'Invalid Replace Expression')
def replace_all(self, event=None):
prog = self.engine.getprog()
repl = self.replvar.get()
res = self.engine.search_text(text, prog)
text.tag_remove("sel", "1.0", "end")
text.tag_remove("hit", "1.0", "end")
# XXX ought to replace circular instead of top-to-bottom when wrapping
res = self.engine.search_forward(text, prog, line, col, 0, ok)
chars = text.get("%d.0" % line, "%d.0" % (line+1))
new = self._replace_expand(m, repl)
first = "%d.%d" % (line, i)
last = "%d.%d" % (line, j)
text.mark_set("insert", last)
text.mark_set("insert", first)
self.show_hit(first, last)
if not self.engine.getprog():
res = self.engine.search_text(text, None, ok)
first = "%d.%d" % (line, i)
last = "%d.%d" % (line, j)
self.show_hit(first, last)
prog = self.engine.getprog()
first = pos = text.index("sel.first")
last = text.index("sel.last")
first = last = pos = text.index("insert")
line, col = SearchEngine.get_line_col(pos)
chars = text.get("%d.0" % line, "%d.0" % (line+1))
m = prog.match(chars, col)
new = self._replace_expand(m, self.replvar.get())
text.mark_set("insert", first)
self.show_hit(first, text.index("insert"))
def show_hit(self, first, last):
text.mark_set("insert", first)
text.tag_remove("sel", "1.0", "end")
text.tag_add("sel", first, last)
text.tag_remove("hit", "1.0", "end")
text.tag_add("hit", first)
text.tag_add("hit", first, last)
def close(self, event=None):
SearchDialogBase.close(self, event)
self.text.tag_remove("hit", "1.0", "end")
def _replace_dialog(parent):
root.title("Test ReplaceDialog")
width, height, x, y = list(map(int, re.split('[x+]', parent.geometry())))
root.geometry("+%d+%d"%(x, y + 150))
# mock undo delegator methods
text.undo_block_start = undo_block_start
text.undo_block_stop = undo_block_stop
text.insert("insert","This is a sample string.\n"*10)
text.tag_add(SEL, "1.0", END)
text.tag_remove(SEL, "1.0", END)
button = Button(root, text="Replace", command=show_replace)
if __name__ == '__main__':
from idlelib.idle_test.htest import run