3
from SearchDialogBase import SearchDialogBase
7
engine = SearchEngine.get(root)
8
if not hasattr(engine, "_replacedialog"):
9
engine._replacedialog = ReplaceDialog(root, engine)
10
dialog = engine._replacedialog
13
class ReplaceDialog(SearchDialogBase):
15
title = "Replace Dialog"
18
def __init__(self, root, engine):
19
SearchDialogBase.__init__(self, root, engine)
20
self.replvar = StringVar(root)
23
SearchDialogBase.open(self, text)
25
first = text.index("sel.first")
29
last = text.index("sel.last")
32
first = first or text.index("insert")
34
self.show_hit(first, last)
37
def create_entries(self):
38
SearchDialogBase.create_entries(self)
39
self.replent = self.make_entry("Replace with:", self.replvar)
41
def create_command_buttons(self):
42
SearchDialogBase.create_command_buttons(self)
43
self.make_button("Find", self.find_it)
44
self.make_button("Replace", self.replace_it)
45
self.make_button("Replace+Find", self.default_command, 1)
46
self.make_button("Replace All", self.replace_all)
48
def find_it(self, event=None):
51
def replace_it(self, event=None):
52
if self.do_find(self.ok):
55
def default_command(self, event=None):
56
if self.do_find(self.ok):
60
def replace_all(self, event=None):
61
prog = self.engine.getprog()
64
repl = self.replvar.get()
66
res = self.engine.search_text(text, prog)
70
text.tag_remove("sel", "1.0", "end")
71
text.tag_remove("hit", "1.0", "end")
74
if self.engine.iswrap():
79
# XXX ought to replace circular instead of top-to-bottom when wrapping
80
text.undo_block_start()
82
res = self.engine.search_forward(text, prog, line, col, 0, ok)
86
chars = text.get("%d.0" % line, "%d.0" % (line+1))
90
first = "%d.%d" % (line, i)
91
last = "%d.%d" % (line, j)
93
text.mark_set("insert", last)
95
text.mark_set("insert", first)
97
text.delete(first, last)
99
text.insert(first, new)
102
text.undo_block_stop()
104
self.show_hit(first, last)
107
def do_find(self, ok=0):
108
if not self.engine.getprog():
111
res = self.engine.search_text(text, None, ok)
117
first = "%d.%d" % (line, i)
118
last = "%d.%d" % (line, j)
119
self.show_hit(first, last)
123
def do_replace(self):
124
prog = self.engine.getprog()
129
first = pos = text.index("sel.first")
130
last = text.index("sel.last")
134
first = last = pos = text.index("insert")
135
line, col = SearchEngine.get_line_col(pos)
136
chars = text.get("%d.0" % line, "%d.0" % (line+1))
137
m = prog.match(chars, col)
140
new = m.expand(self.replvar.get())
141
text.mark_set("insert", first)
142
text.undo_block_start()
144
text.delete(first, last)
146
text.insert(first, new)
147
text.undo_block_stop()
148
self.show_hit(first, text.index("insert"))
152
def show_hit(self, first, last):
154
text.mark_set("insert", first)
155
text.tag_remove("sel", "1.0", "end")
156
text.tag_add("sel", first, last)
157
text.tag_remove("hit", "1.0", "end")
159
text.tag_add("hit", first)
161
text.tag_add("hit", first, last)
163
text.update_idletasks()
165
def close(self, event=None):
166
SearchDialogBase.close(self, event)
167
self.text.tag_remove("hit", "1.0", "end")