8 from SearchDialogBase
import SearchDialogBase
12 engine
= SearchEngine
.get(root
)
13 if not hasattr(engine
, "_replacedialog"):
14 engine
._replacedialog
= ReplaceDialog(root
, engine
)
15 dialog
= engine
._replacedialog
18 class ReplaceDialog(SearchDialogBase
):
20 title
= "Replace Dialog"
23 def __init__(self
, root
, engine
):
24 SearchDialogBase
.__init
__(self
, root
, engine
)
25 self
.replvar
= StringVar(root
)
28 SearchDialogBase
.open(self
, text
)
30 first
= text
.index("sel.first")
34 last
= text
.index("sel.last")
37 first
= first
or text
.index("insert")
39 self
.show_hit(first
, last
)
42 def create_entries(self
):
43 SearchDialogBase
.create_entries(self
)
44 self
.replent
= self
.make_entry("Replace with:", self
.replvar
)
46 def create_command_buttons(self
):
47 SearchDialogBase
.create_command_buttons(self
)
48 self
.make_button("Find", self
.find_it
)
49 self
.make_button("Replace", self
.replace_it
)
50 self
.make_button("Replace+Find", self
.default_command
, 1)
51 self
.make_button("Replace All", self
.replace_all
)
53 def find_it(self
, event
=None):
56 def replace_it(self
, event
=None):
57 if self
.do_find(self
.ok
):
60 def default_command(self
, event
=None):
61 if self
.do_find(self
.ok
):
65 def replace_all(self
, event
=None):
66 prog
= self
.engine
.getprog()
69 repl
= self
.replvar
.get()
71 res
= self
.engine
.search_text(text
, prog
)
75 text
.tag_remove("sel", "1.0", "end")
76 text
.tag_remove("hit", "1.0", "end")
79 if self
.engine
.iswrap():
84 # XXX ought to replace circular instead of top-to-bottom when wrapping
86 res
= self
.engine
.search_forward(text
, prog
, line
, col
, 0, ok
)
90 chars
= text
.get("%d.0" % line
, "%d.0" % (line
+1))
92 new
= re
.pcre_expand(m
, repl
)
94 first
= "%d.%d" % (line
, i
)
95 last
= "%d.%d" % (line
, j
)
97 text
.mark_set("insert", last
)
99 text
.mark_set("insert", first
)
101 text
.delete(first
, last
)
103 text
.insert(first
, new
)
107 self
.show_hit(first
, last
)
110 def do_find(self
, ok
=0):
111 if not self
.engine
.getprog():
114 res
= self
.engine
.search_text(text
, None, ok
)
120 first
= "%d.%d" % (line
, i
)
121 last
= "%d.%d" % (line
, j
)
122 self
.show_hit(first
, last
)
126 def do_replace(self
):
127 prog
= self
.engine
.getprog()
132 first
= pos
= text
.index("sel.first")
133 last
= text
.index("sel.last")
137 first
= last
= pos
= text
.index("insert")
138 line
, col
= SearchEngine
.get_line_col(pos
)
139 chars
= text
.get("%d.0" % line
, "%d.0" % (line
+1))
140 m
= prog
.match(chars
, col
)
143 new
= re
.pcre_expand(m
, self
.replvar
.get())
144 text
.mark_set("insert", first
)
146 text
.delete(first
, last
)
148 text
.insert(first
, new
)
149 self
.show_hit(first
, text
.index("insert"))
153 def show_hit(self
, first
, last
):
155 text
.mark_set("insert", first
)
156 text
.tag_remove("sel", "1.0", "end")
157 text
.tag_add("sel", first
, last
)
158 text
.tag_remove("hit", "1.0", "end")
160 text
.tag_add("hit", first
)
162 text
.tag_add("hit", first
, last
)
164 text
.update_idletasks()
166 def close(self
, event
=None):
167 SearchDialogBase
.close(self
, event
)
168 self
.text
.tag_remove("hit", "1.0", "end")