| 1 | n/a | from tkinter import * |
|---|
| 2 | n/a | |
|---|
| 3 | n/a | from idlelib import SearchEngine |
|---|
| 4 | n/a | from idlelib.SearchDialogBase import SearchDialogBase |
|---|
| 5 | n/a | import re |
|---|
| 6 | n/a | |
|---|
| 7 | n/a | |
|---|
| 8 | n/a | def replace(text): |
|---|
| 9 | n/a | root = text._root() |
|---|
| 10 | n/a | engine = SearchEngine.get(root) |
|---|
| 11 | n/a | if not hasattr(engine, "_replacedialog"): |
|---|
| 12 | n/a | engine._replacedialog = ReplaceDialog(root, engine) |
|---|
| 13 | n/a | dialog = engine._replacedialog |
|---|
| 14 | n/a | dialog.open(text) |
|---|
| 15 | n/a | |
|---|
| 16 | n/a | |
|---|
| 17 | n/a | class ReplaceDialog(SearchDialogBase): |
|---|
| 18 | n/a | |
|---|
| 19 | n/a | title = "Replace Dialog" |
|---|
| 20 | n/a | icon = "Replace" |
|---|
| 21 | n/a | |
|---|
| 22 | n/a | def __init__(self, root, engine): |
|---|
| 23 | n/a | SearchDialogBase.__init__(self, root, engine) |
|---|
| 24 | n/a | self.replvar = StringVar(root) |
|---|
| 25 | n/a | |
|---|
| 26 | n/a | def open(self, text): |
|---|
| 27 | n/a | SearchDialogBase.open(self, text) |
|---|
| 28 | n/a | try: |
|---|
| 29 | n/a | first = text.index("sel.first") |
|---|
| 30 | n/a | except TclError: |
|---|
| 31 | n/a | first = None |
|---|
| 32 | n/a | try: |
|---|
| 33 | n/a | last = text.index("sel.last") |
|---|
| 34 | n/a | except TclError: |
|---|
| 35 | n/a | last = None |
|---|
| 36 | n/a | first = first or text.index("insert") |
|---|
| 37 | n/a | last = last or first |
|---|
| 38 | n/a | self.show_hit(first, last) |
|---|
| 39 | n/a | self.ok = 1 |
|---|
| 40 | n/a | |
|---|
| 41 | n/a | def create_entries(self): |
|---|
| 42 | n/a | SearchDialogBase.create_entries(self) |
|---|
| 43 | n/a | self.replent = self.make_entry("Replace with:", self.replvar) |
|---|
| 44 | n/a | |
|---|
| 45 | n/a | def create_command_buttons(self): |
|---|
| 46 | n/a | SearchDialogBase.create_command_buttons(self) |
|---|
| 47 | n/a | self.make_button("Find", self.find_it) |
|---|
| 48 | n/a | self.make_button("Replace", self.replace_it) |
|---|
| 49 | n/a | self.make_button("Replace+Find", self.default_command, 1) |
|---|
| 50 | n/a | self.make_button("Replace All", self.replace_all) |
|---|
| 51 | n/a | |
|---|
| 52 | n/a | def find_it(self, event=None): |
|---|
| 53 | n/a | self.do_find(0) |
|---|
| 54 | n/a | |
|---|
| 55 | n/a | def replace_it(self, event=None): |
|---|
| 56 | n/a | if self.do_find(self.ok): |
|---|
| 57 | n/a | self.do_replace() |
|---|
| 58 | n/a | |
|---|
| 59 | n/a | def default_command(self, event=None): |
|---|
| 60 | n/a | if self.do_find(self.ok): |
|---|
| 61 | n/a | if self.do_replace(): # Only find next match if replace succeeded. |
|---|
| 62 | n/a | # A bad re can cause a it to fail. |
|---|
| 63 | n/a | self.do_find(0) |
|---|
| 64 | n/a | |
|---|
| 65 | n/a | def _replace_expand(self, m, repl): |
|---|
| 66 | n/a | """ Helper function for expanding a regular expression |
|---|
| 67 | n/a | in the replace field, if needed. """ |
|---|
| 68 | n/a | if self.engine.isre(): |
|---|
| 69 | n/a | try: |
|---|
| 70 | n/a | new = m.expand(repl) |
|---|
| 71 | n/a | except re.error: |
|---|
| 72 | n/a | self.engine.report_error(repl, 'Invalid Replace Expression') |
|---|
| 73 | n/a | new = None |
|---|
| 74 | n/a | else: |
|---|
| 75 | n/a | new = repl |
|---|
| 76 | n/a | |
|---|
| 77 | n/a | return new |
|---|
| 78 | n/a | |
|---|
| 79 | n/a | def replace_all(self, event=None): |
|---|
| 80 | n/a | prog = self.engine.getprog() |
|---|
| 81 | n/a | if not prog: |
|---|
| 82 | n/a | return |
|---|
| 83 | n/a | repl = self.replvar.get() |
|---|
| 84 | n/a | text = self.text |
|---|
| 85 | n/a | res = self.engine.search_text(text, prog) |
|---|
| 86 | n/a | if not res: |
|---|
| 87 | n/a | text.bell() |
|---|
| 88 | n/a | return |
|---|
| 89 | n/a | text.tag_remove("sel", "1.0", "end") |
|---|
| 90 | n/a | text.tag_remove("hit", "1.0", "end") |
|---|
| 91 | n/a | line = res[0] |
|---|
| 92 | n/a | col = res[1].start() |
|---|
| 93 | n/a | if self.engine.iswrap(): |
|---|
| 94 | n/a | line = 1 |
|---|
| 95 | n/a | col = 0 |
|---|
| 96 | n/a | ok = 1 |
|---|
| 97 | n/a | first = last = None |
|---|
| 98 | n/a | # XXX ought to replace circular instead of top-to-bottom when wrapping |
|---|
| 99 | n/a | text.undo_block_start() |
|---|
| 100 | n/a | while 1: |
|---|
| 101 | n/a | res = self.engine.search_forward(text, prog, line, col, 0, ok) |
|---|
| 102 | n/a | if not res: |
|---|
| 103 | n/a | break |
|---|
| 104 | n/a | line, m = res |
|---|
| 105 | n/a | chars = text.get("%d.0" % line, "%d.0" % (line+1)) |
|---|
| 106 | n/a | orig = m.group() |
|---|
| 107 | n/a | new = self._replace_expand(m, repl) |
|---|
| 108 | n/a | if new is None: |
|---|
| 109 | n/a | break |
|---|
| 110 | n/a | i, j = m.span() |
|---|
| 111 | n/a | first = "%d.%d" % (line, i) |
|---|
| 112 | n/a | last = "%d.%d" % (line, j) |
|---|
| 113 | n/a | if new == orig: |
|---|
| 114 | n/a | text.mark_set("insert", last) |
|---|
| 115 | n/a | else: |
|---|
| 116 | n/a | text.mark_set("insert", first) |
|---|
| 117 | n/a | if first != last: |
|---|
| 118 | n/a | text.delete(first, last) |
|---|
| 119 | n/a | if new: |
|---|
| 120 | n/a | text.insert(first, new) |
|---|
| 121 | n/a | col = i + len(new) |
|---|
| 122 | n/a | ok = 0 |
|---|
| 123 | n/a | text.undo_block_stop() |
|---|
| 124 | n/a | if first and last: |
|---|
| 125 | n/a | self.show_hit(first, last) |
|---|
| 126 | n/a | self.close() |
|---|
| 127 | n/a | |
|---|
| 128 | n/a | def do_find(self, ok=0): |
|---|
| 129 | n/a | if not self.engine.getprog(): |
|---|
| 130 | n/a | return False |
|---|
| 131 | n/a | text = self.text |
|---|
| 132 | n/a | res = self.engine.search_text(text, None, ok) |
|---|
| 133 | n/a | if not res: |
|---|
| 134 | n/a | text.bell() |
|---|
| 135 | n/a | return False |
|---|
| 136 | n/a | line, m = res |
|---|
| 137 | n/a | i, j = m.span() |
|---|
| 138 | n/a | first = "%d.%d" % (line, i) |
|---|
| 139 | n/a | last = "%d.%d" % (line, j) |
|---|
| 140 | n/a | self.show_hit(first, last) |
|---|
| 141 | n/a | self.ok = 1 |
|---|
| 142 | n/a | return True |
|---|
| 143 | n/a | |
|---|
| 144 | n/a | def do_replace(self): |
|---|
| 145 | n/a | prog = self.engine.getprog() |
|---|
| 146 | n/a | if not prog: |
|---|
| 147 | n/a | return False |
|---|
| 148 | n/a | text = self.text |
|---|
| 149 | n/a | try: |
|---|
| 150 | n/a | first = pos = text.index("sel.first") |
|---|
| 151 | n/a | last = text.index("sel.last") |
|---|
| 152 | n/a | except TclError: |
|---|
| 153 | n/a | pos = None |
|---|
| 154 | n/a | if not pos: |
|---|
| 155 | n/a | first = last = pos = text.index("insert") |
|---|
| 156 | n/a | line, col = SearchEngine.get_line_col(pos) |
|---|
| 157 | n/a | chars = text.get("%d.0" % line, "%d.0" % (line+1)) |
|---|
| 158 | n/a | m = prog.match(chars, col) |
|---|
| 159 | n/a | if not prog: |
|---|
| 160 | n/a | return False |
|---|
| 161 | n/a | new = self._replace_expand(m, self.replvar.get()) |
|---|
| 162 | n/a | if new is None: |
|---|
| 163 | n/a | return False |
|---|
| 164 | n/a | text.mark_set("insert", first) |
|---|
| 165 | n/a | text.undo_block_start() |
|---|
| 166 | n/a | if m.group(): |
|---|
| 167 | n/a | text.delete(first, last) |
|---|
| 168 | n/a | if new: |
|---|
| 169 | n/a | text.insert(first, new) |
|---|
| 170 | n/a | text.undo_block_stop() |
|---|
| 171 | n/a | self.show_hit(first, text.index("insert")) |
|---|
| 172 | n/a | self.ok = 0 |
|---|
| 173 | n/a | return True |
|---|
| 174 | n/a | |
|---|
| 175 | n/a | def show_hit(self, first, last): |
|---|
| 176 | n/a | text = self.text |
|---|
| 177 | n/a | text.mark_set("insert", first) |
|---|
| 178 | n/a | text.tag_remove("sel", "1.0", "end") |
|---|
| 179 | n/a | text.tag_add("sel", first, last) |
|---|
| 180 | n/a | text.tag_remove("hit", "1.0", "end") |
|---|
| 181 | n/a | if first == last: |
|---|
| 182 | n/a | text.tag_add("hit", first) |
|---|
| 183 | n/a | else: |
|---|
| 184 | n/a | text.tag_add("hit", first, last) |
|---|
| 185 | n/a | text.see("insert") |
|---|
| 186 | n/a | text.update_idletasks() |
|---|
| 187 | n/a | |
|---|
| 188 | n/a | def close(self, event=None): |
|---|
| 189 | n/a | SearchDialogBase.close(self, event) |
|---|
| 190 | n/a | self.text.tag_remove("hit", "1.0", "end") |
|---|