blob: 0f0cb189f6b7ed5db5c4d3c08d7954b6dcf275e2 [file] [log] [blame]
Guido van Rossum504b0bf1999-01-02 21:28:54 +00001from Tkinter import *
2import SearchEngine
3from SearchDialogBase import SearchDialogBase
4
5
6def _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
13def find(text):
14 return _setup(text).open(text)
15
16def find_again(text):
17 return _setup(text).find_again(text)
18
19def find_selection(text):
20 return _setup(text).find_selection(text)
21
22class SearchDialog(SearchDialogBase):
23
24 def create_widgets(self):
25 f = SearchDialogBase.create_widgets(self)
26 self.make_button("Find", self.default_command, 1)
27
28 def default_command(self, event=None):
29 if not self.engine.getprog():
30 return
31 if self.find_again(self.text):
32 self.close()
33
34 def find_again(self, text):
35 if not self.engine.getpat():
36 self.open(text)
37 return 0
38 if not self.engine.getprog():
39 return 0
40 res = self.engine.search_text(text)
41 if res:
42 line, m = res
43 i, j = m.span()
44 first = "%d.%d" % (line, i)
45 last = "%d.%d" % (line, j)
Guido van Rossum959c9371999-01-28 19:04:01 +000046 try:
47 selfirst = text.index("sel.first")
48 sellast = text.index("sel.last")
49 if selfirst == first and sellast == last:
50 text.bell()
51 return 0
52 except TclError:
53 pass
Guido van Rossum504b0bf1999-01-02 21:28:54 +000054 text.tag_remove("sel", "1.0", "end")
55 text.tag_add("sel", first, last)
56 text.mark_set("insert", self.engine.isback() and first or last)
57 text.see("insert")
58 return 1
59 else:
60 text.bell()
61 return 0
62
63 def find_selection(self, text):
64 pat = text.get("sel.first", "sel.last")
65 if pat:
66 self.engine.setcookedpat(pat)
67 return self.find_again(text)