/Lib/idlelib/SearchDialog.py

http://unladen-swallow.googlecode.com/ · Python · 68 lines · 58 code · 10 blank · 0 comment · 14 complexity · 8f1b0568342ddd10ab7548a3ebb55029 MD5 · raw file

  1. from Tkinter import *
  2. import SearchEngine
  3. from SearchDialogBase import SearchDialogBase
  4. def _setup(text):
  5. root = text._root()
  6. engine = SearchEngine.get(root)
  7. if not hasattr(engine, "_searchdialog"):
  8. engine._searchdialog = SearchDialog(root, engine)
  9. return engine._searchdialog
  10. def find(text):
  11. pat = text.get("sel.first", "sel.last")
  12. return _setup(text).open(text,pat)
  13. def find_again(text):
  14. return _setup(text).find_again(text)
  15. def find_selection(text):
  16. return _setup(text).find_selection(text)
  17. class SearchDialog(SearchDialogBase):
  18. def create_widgets(self):
  19. f = SearchDialogBase.create_widgets(self)
  20. self.make_button("Find", self.default_command, 1)
  21. def default_command(self, event=None):
  22. if not self.engine.getprog():
  23. return
  24. if self.find_again(self.text):
  25. self.close()
  26. def find_again(self, text):
  27. if not self.engine.getpat():
  28. self.open(text)
  29. return False
  30. if not self.engine.getprog():
  31. return False
  32. res = self.engine.search_text(text)
  33. if res:
  34. line, m = res
  35. i, j = m.span()
  36. first = "%d.%d" % (line, i)
  37. last = "%d.%d" % (line, j)
  38. try:
  39. selfirst = text.index("sel.first")
  40. sellast = text.index("sel.last")
  41. if selfirst == first and sellast == last:
  42. text.bell()
  43. return False
  44. except TclError:
  45. pass
  46. text.tag_remove("sel", "1.0", "end")
  47. text.tag_add("sel", first, last)
  48. text.mark_set("insert", self.engine.isback() and first or last)
  49. text.see("insert")
  50. return True
  51. else:
  52. text.bell()
  53. return False
  54. def find_selection(self, text):
  55. pat = text.get("sel.first", "sel.last")
  56. if pat:
  57. self.engine.setcookedpat(pat)
  58. return self.find_again(text)