MCPcopy Index your code
hub / github.com/python/cpython / replace_all

Method replace_all

Lib/idlelib/replace.py:122–176  ·  view source on GitHub ↗

Handle the Replace All button. Search text for occurrences of the Find value and replace each of them. The 'wrap around' value controls the start point for searching. If wrap isn't set, then the searching starts at the first occurrence after the current selection;

(self, event=None)

Source from the content-addressed store, hash-verified

120 return new
121
122 def replace_all(self, event=None):
123 """Handle the Replace All button.
124
125 Search text for occurrences of the Find value and replace
126 each of them. The 'wrap around' value controls the start
127 point for searching. If wrap isn't set, then the searching
128 starts at the first occurrence after the current selection;
129 if wrap is set, the replacement starts at the first line.
130 The replacement is always done top-to-bottom in the text.
131 """
132 prog = self.engine.getprog()
133 if not prog:
134 return
135 repl = self.replvar.get()
136 text = self.text
137 res = self.engine.search_text(text, prog)
138 if not res:
139 self.bell()
140 return
141 text.tag_remove("sel", "1.0", "end")
142 text.tag_remove("hit", "1.0", "end")
143 line = res[0]
144 col = res[1].start()
145 if self.engine.iswrap():
146 line = 1
147 col = 0
148 ok = True
149 first = last = None
150 # XXX ought to replace circular instead of top-to-bottom when wrapping
151 text.undo_block_start()
152 while res := self.engine.search_forward(
153 text, prog, line, col, wrap=False, ok=ok):
154 line, m = res
155 chars = text.get("%d.0" % line, "%d.0" % (line+1))
156 orig = m.group()
157 new = self._replace_expand(m, repl)
158 if new is None:
159 break
160 i, j = m.span()
161 first = "%d.%d" % (line, i)
162 last = "%d.%d" % (line, j)
163 if new == orig:
164 text.mark_set("insert", last)
165 else:
166 text.mark_set("insert", first)
167 if first != last:
168 text.delete(first, last)
169 if new:
170 text.insert(first, new, self.insert_tags)
171 col = i + len(new)
172 ok = False
173 text.undo_block_stop()
174 if first and last:
175 self.show_hit(first, last)
176 self.close()
177
178 def do_find(self, ok=False):
179 """Search for and highlight next occurrence of pattern in text.

Callers

nothing calls this directly

Calls 15

_replace_expandMethod · 0.95
show_hitMethod · 0.95
closeMethod · 0.95
getprogMethod · 0.80
search_textMethod · 0.80
iswrapMethod · 0.80
search_forwardMethod · 0.80
getMethod · 0.45
bellMethod · 0.45
tag_removeMethod · 0.45
startMethod · 0.45
undo_block_startMethod · 0.45

Tested by

no test coverage detected