blob: 501b6d0067a37cd49fa180b8cbcf8760ad4fcd30 [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)
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 1
51 else:
52 text.bell()
53 return 0
54
55 def find_selection(self, text):
56 pat = text.get("sel.first", "sel.last")
57 if pat:
58 self.engine.setcookedpat(pat)
59 return self.find_again(text)