blob: 8d275c4437b6cf1316e4116e93fc2df74c2ecc06 [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)
Tim Petersbc0e9102002-04-04 22:55:58 +000037 return False
Guido van Rossum504b0bf1999-01-02 21:28:54 +000038 if not self.engine.getprog():
Tim Petersbc0e9102002-04-04 22:55:58 +000039 return False
Guido van Rossum504b0bf1999-01-02 21:28:54 +000040 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()
Tim Petersbc0e9102002-04-04 22:55:58 +000051 return False
Guido van Rossum959c9371999-01-28 19:04:01 +000052 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")
Tim Petersbc0e9102002-04-04 22:55:58 +000058 return True
Guido van Rossum504b0bf1999-01-02 21:28:54 +000059 else:
60 text.bell()
Tim Petersbc0e9102002-04-04 22:55:58 +000061 return False
Guido van Rossum504b0bf1999-01-02 21:28:54 +000062
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)