1 | n/a | """Replace dialog for IDLE. Inherits SearchDialogBase for GUI. |
---|
2 | n/a | Uses idlelib.SearchEngine for search capability. |
---|
3 | n/a | Defines various replace related functions like replace, replace all, |
---|
4 | n/a | replace+find. |
---|
5 | n/a | """ |
---|
6 | n/a | import re |
---|
7 | n/a | |
---|
8 | n/a | from tkinter import StringVar, TclError |
---|
9 | n/a | |
---|
10 | n/a | from idlelib.searchbase import SearchDialogBase |
---|
11 | n/a | from idlelib import searchengine |
---|
12 | n/a | |
---|
13 | n/a | def replace(text): |
---|
14 | n/a | """Returns a singleton ReplaceDialog instance.The single dialog |
---|
15 | n/a | saves user entries and preferences across instances.""" |
---|
16 | n/a | root = text._root() |
---|
17 | n/a | engine = searchengine.get(root) |
---|
18 | n/a | if not hasattr(engine, "_replacedialog"): |
---|
19 | n/a | engine._replacedialog = ReplaceDialog(root, engine) |
---|
20 | n/a | dialog = engine._replacedialog |
---|
21 | n/a | dialog.open(text) |
---|
22 | n/a | |
---|
23 | n/a | |
---|
24 | n/a | class ReplaceDialog(SearchDialogBase): |
---|
25 | n/a | |
---|
26 | n/a | title = "Replace Dialog" |
---|
27 | n/a | icon = "Replace" |
---|
28 | n/a | |
---|
29 | n/a | def __init__(self, root, engine): |
---|
30 | n/a | SearchDialogBase.__init__(self, root, engine) |
---|
31 | n/a | self.replvar = StringVar(root) |
---|
32 | n/a | |
---|
33 | n/a | def open(self, text): |
---|
34 | n/a | """Display the replace dialog""" |
---|
35 | n/a | SearchDialogBase.open(self, text) |
---|
36 | n/a | try: |
---|
37 | n/a | first = text.index("sel.first") |
---|
38 | n/a | except TclError: |
---|
39 | n/a | first = None |
---|
40 | n/a | try: |
---|
41 | n/a | last = text.index("sel.last") |
---|
42 | n/a | except TclError: |
---|
43 | n/a | last = None |
---|
44 | n/a | first = first or text.index("insert") |
---|
45 | n/a | last = last or first |
---|
46 | n/a | self.show_hit(first, last) |
---|
47 | n/a | self.ok = 1 |
---|
48 | n/a | |
---|
49 | n/a | def create_entries(self): |
---|
50 | n/a | """Create label and text entry widgets""" |
---|
51 | n/a | SearchDialogBase.create_entries(self) |
---|
52 | n/a | self.replent = self.make_entry("Replace with:", self.replvar)[0] |
---|
53 | n/a | |
---|
54 | n/a | def create_command_buttons(self): |
---|
55 | n/a | SearchDialogBase.create_command_buttons(self) |
---|
56 | n/a | self.make_button("Find", self.find_it) |
---|
57 | n/a | self.make_button("Replace", self.replace_it) |
---|
58 | n/a | self.make_button("Replace+Find", self.default_command, 1) |
---|
59 | n/a | self.make_button("Replace All", self.replace_all) |
---|
60 | n/a | |
---|
61 | n/a | def find_it(self, event=None): |
---|
62 | n/a | self.do_find(0) |
---|
63 | n/a | |
---|
64 | n/a | def replace_it(self, event=None): |
---|
65 | n/a | if self.do_find(self.ok): |
---|
66 | n/a | self.do_replace() |
---|
67 | n/a | |
---|
68 | n/a | def default_command(self, event=None): |
---|
69 | n/a | "Replace and find next." |
---|
70 | n/a | if self.do_find(self.ok): |
---|
71 | n/a | if self.do_replace(): # Only find next match if replace succeeded. |
---|
72 | n/a | # A bad re can cause it to fail. |
---|
73 | n/a | self.do_find(0) |
---|
74 | n/a | |
---|
75 | n/a | def _replace_expand(self, m, repl): |
---|
76 | n/a | """ Helper function for expanding a regular expression |
---|
77 | n/a | in the replace field, if needed. """ |
---|
78 | n/a | if self.engine.isre(): |
---|
79 | n/a | try: |
---|
80 | n/a | new = m.expand(repl) |
---|
81 | n/a | except re.error: |
---|
82 | n/a | self.engine.report_error(repl, 'Invalid Replace Expression') |
---|
83 | n/a | new = None |
---|
84 | n/a | else: |
---|
85 | n/a | new = repl |
---|
86 | n/a | |
---|
87 | n/a | return new |
---|
88 | n/a | |
---|
89 | n/a | def replace_all(self, event=None): |
---|
90 | n/a | """Replace all instances of patvar with replvar in text""" |
---|
91 | n/a | prog = self.engine.getprog() |
---|
92 | n/a | if not prog: |
---|
93 | n/a | return |
---|
94 | n/a | repl = self.replvar.get() |
---|
95 | n/a | text = self.text |
---|
96 | n/a | res = self.engine.search_text(text, prog) |
---|
97 | n/a | if not res: |
---|
98 | n/a | self.bell() |
---|
99 | n/a | return |
---|
100 | n/a | text.tag_remove("sel", "1.0", "end") |
---|
101 | n/a | text.tag_remove("hit", "1.0", "end") |
---|
102 | n/a | line = res[0] |
---|
103 | n/a | col = res[1].start() |
---|
104 | n/a | if self.engine.iswrap(): |
---|
105 | n/a | line = 1 |
---|
106 | n/a | col = 0 |
---|
107 | n/a | ok = 1 |
---|
108 | n/a | first = last = None |
---|
109 | n/a | # XXX ought to replace circular instead of top-to-bottom when wrapping |
---|
110 | n/a | text.undo_block_start() |
---|
111 | n/a | while 1: |
---|
112 | n/a | res = self.engine.search_forward(text, prog, line, col, 0, ok) |
---|
113 | n/a | if not res: |
---|
114 | n/a | break |
---|
115 | n/a | line, m = res |
---|
116 | n/a | chars = text.get("%d.0" % line, "%d.0" % (line+1)) |
---|
117 | n/a | orig = m.group() |
---|
118 | n/a | new = self._replace_expand(m, repl) |
---|
119 | n/a | if new is None: |
---|
120 | n/a | break |
---|
121 | n/a | i, j = m.span() |
---|
122 | n/a | first = "%d.%d" % (line, i) |
---|
123 | n/a | last = "%d.%d" % (line, j) |
---|
124 | n/a | if new == orig: |
---|
125 | n/a | text.mark_set("insert", last) |
---|
126 | n/a | else: |
---|
127 | n/a | text.mark_set("insert", first) |
---|
128 | n/a | if first != last: |
---|
129 | n/a | text.delete(first, last) |
---|
130 | n/a | if new: |
---|
131 | n/a | text.insert(first, new) |
---|
132 | n/a | col = i + len(new) |
---|
133 | n/a | ok = 0 |
---|
134 | n/a | text.undo_block_stop() |
---|
135 | n/a | if first and last: |
---|
136 | n/a | self.show_hit(first, last) |
---|
137 | n/a | self.close() |
---|
138 | n/a | |
---|
139 | n/a | def do_find(self, ok=0): |
---|
140 | n/a | if not self.engine.getprog(): |
---|
141 | n/a | return False |
---|
142 | n/a | text = self.text |
---|
143 | n/a | res = self.engine.search_text(text, None, ok) |
---|
144 | n/a | if not res: |
---|
145 | n/a | self.bell() |
---|
146 | n/a | return False |
---|
147 | n/a | line, m = res |
---|
148 | n/a | i, j = m.span() |
---|
149 | n/a | first = "%d.%d" % (line, i) |
---|
150 | n/a | last = "%d.%d" % (line, j) |
---|
151 | n/a | self.show_hit(first, last) |
---|
152 | n/a | self.ok = 1 |
---|
153 | n/a | return True |
---|
154 | n/a | |
---|
155 | n/a | def do_replace(self): |
---|
156 | n/a | prog = self.engine.getprog() |
---|
157 | n/a | if not prog: |
---|
158 | n/a | return False |
---|
159 | n/a | text = self.text |
---|
160 | n/a | try: |
---|
161 | n/a | first = pos = text.index("sel.first") |
---|
162 | n/a | last = text.index("sel.last") |
---|
163 | n/a | except TclError: |
---|
164 | n/a | pos = None |
---|
165 | n/a | if not pos: |
---|
166 | n/a | first = last = pos = text.index("insert") |
---|
167 | n/a | line, col = searchengine.get_line_col(pos) |
---|
168 | n/a | chars = text.get("%d.0" % line, "%d.0" % (line+1)) |
---|
169 | n/a | m = prog.match(chars, col) |
---|
170 | n/a | if not prog: |
---|
171 | n/a | return False |
---|
172 | n/a | new = self._replace_expand(m, self.replvar.get()) |
---|
173 | n/a | if new is None: |
---|
174 | n/a | return False |
---|
175 | n/a | text.mark_set("insert", first) |
---|
176 | n/a | text.undo_block_start() |
---|
177 | n/a | if m.group(): |
---|
178 | n/a | text.delete(first, last) |
---|
179 | n/a | if new: |
---|
180 | n/a | text.insert(first, new) |
---|
181 | n/a | text.undo_block_stop() |
---|
182 | n/a | self.show_hit(first, text.index("insert")) |
---|
183 | n/a | self.ok = 0 |
---|
184 | n/a | return True |
---|
185 | n/a | |
---|
186 | n/a | def show_hit(self, first, last): |
---|
187 | n/a | """Highlight text from 'first' to 'last'. |
---|
188 | n/a | 'first', 'last' - Text indices""" |
---|
189 | n/a | text = self.text |
---|
190 | n/a | text.mark_set("insert", first) |
---|
191 | n/a | text.tag_remove("sel", "1.0", "end") |
---|
192 | n/a | text.tag_add("sel", first, last) |
---|
193 | n/a | text.tag_remove("hit", "1.0", "end") |
---|
194 | n/a | if first == last: |
---|
195 | n/a | text.tag_add("hit", first) |
---|
196 | n/a | else: |
---|
197 | n/a | text.tag_add("hit", first, last) |
---|
198 | n/a | text.see("insert") |
---|
199 | n/a | text.update_idletasks() |
---|
200 | n/a | |
---|
201 | n/a | def close(self, event=None): |
---|
202 | n/a | SearchDialogBase.close(self, event) |
---|
203 | n/a | self.text.tag_remove("hit", "1.0", "end") |
---|
204 | n/a | |
---|
205 | n/a | |
---|
206 | n/a | def _replace_dialog(parent): # htest # |
---|
207 | n/a | from tkinter import Toplevel, Text, END, SEL |
---|
208 | n/a | from tkinter.ttk import Button |
---|
209 | n/a | |
---|
210 | n/a | box = Toplevel(parent) |
---|
211 | n/a | box.title("Test ReplaceDialog") |
---|
212 | n/a | x, y = map(int, parent.geometry().split('+')[1:]) |
---|
213 | n/a | box.geometry("+%d+%d" % (x, y + 175)) |
---|
214 | n/a | |
---|
215 | n/a | # mock undo delegator methods |
---|
216 | n/a | def undo_block_start(): |
---|
217 | n/a | pass |
---|
218 | n/a | |
---|
219 | n/a | def undo_block_stop(): |
---|
220 | n/a | pass |
---|
221 | n/a | |
---|
222 | n/a | text = Text(box, inactiveselectbackground='gray') |
---|
223 | n/a | text.undo_block_start = undo_block_start |
---|
224 | n/a | text.undo_block_stop = undo_block_stop |
---|
225 | n/a | text.pack() |
---|
226 | n/a | text.insert("insert","This is a sample sTring\nPlus MORE.") |
---|
227 | n/a | text.focus_set() |
---|
228 | n/a | |
---|
229 | n/a | def show_replace(): |
---|
230 | n/a | text.tag_add(SEL, "1.0", END) |
---|
231 | n/a | replace(text) |
---|
232 | n/a | text.tag_remove(SEL, "1.0", END) |
---|
233 | n/a | |
---|
234 | n/a | button = Button(box, text="Replace", command=show_replace) |
---|
235 | n/a | button.pack() |
---|
236 | n/a | |
---|
237 | n/a | if __name__ == '__main__': |
---|
238 | n/a | import unittest |
---|
239 | n/a | unittest.main('idlelib.idle_test.test_replace', |
---|
240 | n/a | verbosity=2, exit=False) |
---|
241 | n/a | |
---|
242 | n/a | from idlelib.idle_test.htest import run |
---|
243 | n/a | run(_replace_dialog) |
---|