from 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
)
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
):
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))
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
= m
.expand(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")