David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 1 | from Tkinter import * |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 2 | |
Kurt B. Kaiser | 2d7f6a0 | 2007-08-22 23:01:33 +0000 | [diff] [blame] | 3 | from idlelib import SearchEngine |
| 4 | from idlelib.SearchDialogBase import SearchDialogBase |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 5 | |
| 6 | def _setup(text): |
| 7 | root = text._root() |
| 8 | engine = SearchEngine.get(root) |
| 9 | if not hasattr(engine, "_searchdialog"): |
| 10 | engine._searchdialog = SearchDialog(root, engine) |
| 11 | return engine._searchdialog |
| 12 | |
| 13 | def find(text): |
Chui Tey | 5a231c8f | 2002-11-06 02:18:45 +0000 | [diff] [blame] | 14 | pat = text.get("sel.first", "sel.last") |
| 15 | return _setup(text).open(text,pat) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 16 | |
| 17 | def find_again(text): |
| 18 | return _setup(text).find_again(text) |
| 19 | |
| 20 | def find_selection(text): |
| 21 | return _setup(text).find_selection(text) |
| 22 | |
| 23 | class SearchDialog(SearchDialogBase): |
| 24 | |
| 25 | def create_widgets(self): |
| 26 | f = SearchDialogBase.create_widgets(self) |
| 27 | self.make_button("Find", self.default_command, 1) |
| 28 | |
| 29 | def default_command(self, event=None): |
| 30 | if not self.engine.getprog(): |
| 31 | return |
| 32 | if self.find_again(self.text): |
| 33 | self.close() |
| 34 | |
| 35 | def find_again(self, text): |
| 36 | if not self.engine.getpat(): |
| 37 | self.open(text) |
Kurt B. Kaiser | 0f4402d | 2002-09-18 03:10:10 +0000 | [diff] [blame] | 38 | return False |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 39 | if not self.engine.getprog(): |
Kurt B. Kaiser | 0f4402d | 2002-09-18 03:10:10 +0000 | [diff] [blame] | 40 | return False |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 41 | res = self.engine.search_text(text) |
| 42 | if res: |
| 43 | line, m = res |
| 44 | i, j = m.span() |
| 45 | first = "%d.%d" % (line, i) |
| 46 | last = "%d.%d" % (line, j) |
| 47 | try: |
| 48 | selfirst = text.index("sel.first") |
| 49 | sellast = text.index("sel.last") |
| 50 | if selfirst == first and sellast == last: |
| 51 | text.bell() |
Kurt B. Kaiser | 0f4402d | 2002-09-18 03:10:10 +0000 | [diff] [blame] | 52 | return False |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 53 | except TclError: |
| 54 | pass |
| 55 | text.tag_remove("sel", "1.0", "end") |
| 56 | text.tag_add("sel", first, last) |
| 57 | text.mark_set("insert", self.engine.isback() and first or last) |
| 58 | text.see("insert") |
Kurt B. Kaiser | 0f4402d | 2002-09-18 03:10:10 +0000 | [diff] [blame] | 59 | return True |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 60 | else: |
| 61 | text.bell() |
Kurt B. Kaiser | 0f4402d | 2002-09-18 03:10:10 +0000 | [diff] [blame] | 62 | return False |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 63 | |
| 64 | def find_selection(self, text): |
| 65 | pat = text.get("sel.first", "sel.last") |
| 66 | if pat: |
| 67 | self.engine.setcookedpat(pat) |
| 68 | return self.find_again(text) |