1 from Tkinter import * 2 3 from idlelib import SearchEngine 4 from idlelib.SearchDialogBase import SearchDialogBase 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): 14 pat = text.get("sel.first", "sel.last") 15 return _setup(text).open(text,pat) 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) 38 return False 39 if not self.engine.getprog(): 40 return False 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() 52 return False 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") 59 return True 60 else: 61 text.bell() 62 return False 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) 69