jpayne@68
|
1 """Search dialog for Find, Find Again, and Find Selection
|
jpayne@68
|
2 functionality.
|
jpayne@68
|
3
|
jpayne@68
|
4 Inherits from SearchDialogBase for GUI and uses searchengine
|
jpayne@68
|
5 to prepare search pattern.
|
jpayne@68
|
6 """
|
jpayne@68
|
7 from tkinter import TclError
|
jpayne@68
|
8
|
jpayne@68
|
9 from idlelib import searchengine
|
jpayne@68
|
10 from idlelib.searchbase import SearchDialogBase
|
jpayne@68
|
11
|
jpayne@68
|
12 def _setup(text):
|
jpayne@68
|
13 """Return the new or existing singleton SearchDialog instance.
|
jpayne@68
|
14
|
jpayne@68
|
15 The singleton dialog saves user entries and preferences
|
jpayne@68
|
16 across instances.
|
jpayne@68
|
17
|
jpayne@68
|
18 Args:
|
jpayne@68
|
19 text: Text widget containing the text to be searched.
|
jpayne@68
|
20 """
|
jpayne@68
|
21 root = text._root()
|
jpayne@68
|
22 engine = searchengine.get(root)
|
jpayne@68
|
23 if not hasattr(engine, "_searchdialog"):
|
jpayne@68
|
24 engine._searchdialog = SearchDialog(root, engine)
|
jpayne@68
|
25 return engine._searchdialog
|
jpayne@68
|
26
|
jpayne@68
|
27 def find(text):
|
jpayne@68
|
28 """Open the search dialog.
|
jpayne@68
|
29
|
jpayne@68
|
30 Module-level function to access the singleton SearchDialog
|
jpayne@68
|
31 instance and open the dialog. If text is selected, it is
|
jpayne@68
|
32 used as the search phrase; otherwise, the previous entry
|
jpayne@68
|
33 is used. No search is done with this command.
|
jpayne@68
|
34 """
|
jpayne@68
|
35 pat = text.get("sel.first", "sel.last")
|
jpayne@68
|
36 return _setup(text).open(text, pat) # Open is inherited from SDBase.
|
jpayne@68
|
37
|
jpayne@68
|
38 def find_again(text):
|
jpayne@68
|
39 """Repeat the search for the last pattern and preferences.
|
jpayne@68
|
40
|
jpayne@68
|
41 Module-level function to access the singleton SearchDialog
|
jpayne@68
|
42 instance to search again using the user entries and preferences
|
jpayne@68
|
43 from the last dialog. If there was no prior search, open the
|
jpayne@68
|
44 search dialog; otherwise, perform the search without showing the
|
jpayne@68
|
45 dialog.
|
jpayne@68
|
46 """
|
jpayne@68
|
47 return _setup(text).find_again(text)
|
jpayne@68
|
48
|
jpayne@68
|
49 def find_selection(text):
|
jpayne@68
|
50 """Search for the selected pattern in the text.
|
jpayne@68
|
51
|
jpayne@68
|
52 Module-level function to access the singleton SearchDialog
|
jpayne@68
|
53 instance to search using the selected text. With a text
|
jpayne@68
|
54 selection, perform the search without displaying the dialog.
|
jpayne@68
|
55 Without a selection, use the prior entry as the search phrase
|
jpayne@68
|
56 and don't display the dialog. If there has been no prior
|
jpayne@68
|
57 search, open the search dialog.
|
jpayne@68
|
58 """
|
jpayne@68
|
59 return _setup(text).find_selection(text)
|
jpayne@68
|
60
|
jpayne@68
|
61
|
jpayne@68
|
62 class SearchDialog(SearchDialogBase):
|
jpayne@68
|
63 "Dialog for finding a pattern in text."
|
jpayne@68
|
64
|
jpayne@68
|
65 def create_widgets(self):
|
jpayne@68
|
66 "Create the base search dialog and add a button for Find Next."
|
jpayne@68
|
67 SearchDialogBase.create_widgets(self)
|
jpayne@68
|
68 # TODO - why is this here and not in a create_command_buttons?
|
jpayne@68
|
69 self.make_button("Find Next", self.default_command, isdef=True)
|
jpayne@68
|
70
|
jpayne@68
|
71 def default_command(self, event=None):
|
jpayne@68
|
72 "Handle the Find Next button as the default command."
|
jpayne@68
|
73 if not self.engine.getprog():
|
jpayne@68
|
74 return
|
jpayne@68
|
75 self.find_again(self.text)
|
jpayne@68
|
76
|
jpayne@68
|
77 def find_again(self, text):
|
jpayne@68
|
78 """Repeat the last search.
|
jpayne@68
|
79
|
jpayne@68
|
80 If no search was previously run, open a new search dialog. In
|
jpayne@68
|
81 this case, no search is done.
|
jpayne@68
|
82
|
jpayne@68
|
83 If a search was previously run, the search dialog won't be
|
jpayne@68
|
84 shown and the options from the previous search (including the
|
jpayne@68
|
85 search pattern) will be used to find the next occurrence
|
jpayne@68
|
86 of the pattern. Next is relative based on direction.
|
jpayne@68
|
87
|
jpayne@68
|
88 Position the window to display the located occurrence in the
|
jpayne@68
|
89 text.
|
jpayne@68
|
90
|
jpayne@68
|
91 Return True if the search was successful and False otherwise.
|
jpayne@68
|
92 """
|
jpayne@68
|
93 if not self.engine.getpat():
|
jpayne@68
|
94 self.open(text)
|
jpayne@68
|
95 return False
|
jpayne@68
|
96 if not self.engine.getprog():
|
jpayne@68
|
97 return False
|
jpayne@68
|
98 res = self.engine.search_text(text)
|
jpayne@68
|
99 if res:
|
jpayne@68
|
100 line, m = res
|
jpayne@68
|
101 i, j = m.span()
|
jpayne@68
|
102 first = "%d.%d" % (line, i)
|
jpayne@68
|
103 last = "%d.%d" % (line, j)
|
jpayne@68
|
104 try:
|
jpayne@68
|
105 selfirst = text.index("sel.first")
|
jpayne@68
|
106 sellast = text.index("sel.last")
|
jpayne@68
|
107 if selfirst == first and sellast == last:
|
jpayne@68
|
108 self.bell()
|
jpayne@68
|
109 return False
|
jpayne@68
|
110 except TclError:
|
jpayne@68
|
111 pass
|
jpayne@68
|
112 text.tag_remove("sel", "1.0", "end")
|
jpayne@68
|
113 text.tag_add("sel", first, last)
|
jpayne@68
|
114 text.mark_set("insert", self.engine.isback() and first or last)
|
jpayne@68
|
115 text.see("insert")
|
jpayne@68
|
116 return True
|
jpayne@68
|
117 else:
|
jpayne@68
|
118 self.bell()
|
jpayne@68
|
119 return False
|
jpayne@68
|
120
|
jpayne@68
|
121 def find_selection(self, text):
|
jpayne@68
|
122 """Search for selected text with previous dialog preferences.
|
jpayne@68
|
123
|
jpayne@68
|
124 Instead of using the same pattern for searching (as Find
|
jpayne@68
|
125 Again does), this first resets the pattern to the currently
|
jpayne@68
|
126 selected text. If the selected text isn't changed, then use
|
jpayne@68
|
127 the prior search phrase.
|
jpayne@68
|
128 """
|
jpayne@68
|
129 pat = text.get("sel.first", "sel.last")
|
jpayne@68
|
130 if pat:
|
jpayne@68
|
131 self.engine.setcookedpat(pat)
|
jpayne@68
|
132 return self.find_again(text)
|
jpayne@68
|
133
|
jpayne@68
|
134
|
jpayne@68
|
135 def _search_dialog(parent): # htest #
|
jpayne@68
|
136 "Display search test box."
|
jpayne@68
|
137 from tkinter import Toplevel, Text
|
jpayne@68
|
138 from tkinter.ttk import Frame, Button
|
jpayne@68
|
139
|
jpayne@68
|
140 top = Toplevel(parent)
|
jpayne@68
|
141 top.title("Test SearchDialog")
|
jpayne@68
|
142 x, y = map(int, parent.geometry().split('+')[1:])
|
jpayne@68
|
143 top.geometry("+%d+%d" % (x, y + 175))
|
jpayne@68
|
144
|
jpayne@68
|
145 frame = Frame(top)
|
jpayne@68
|
146 frame.pack()
|
jpayne@68
|
147 text = Text(frame, inactiveselectbackground='gray')
|
jpayne@68
|
148 text.pack()
|
jpayne@68
|
149 text.insert("insert","This is a sample string.\n"*5)
|
jpayne@68
|
150
|
jpayne@68
|
151 def show_find():
|
jpayne@68
|
152 text.tag_add('sel', '1.0', 'end')
|
jpayne@68
|
153 _setup(text).open(text)
|
jpayne@68
|
154 text.tag_remove('sel', '1.0', 'end')
|
jpayne@68
|
155
|
jpayne@68
|
156 button = Button(frame, text="Search (selection ignored)", command=show_find)
|
jpayne@68
|
157 button.pack()
|
jpayne@68
|
158
|
jpayne@68
|
159 if __name__ == '__main__':
|
jpayne@68
|
160 from unittest import main
|
jpayne@68
|
161 main('idlelib.idle_test.test_search', verbosity=2, exit=False)
|
jpayne@68
|
162
|
jpayne@68
|
163 from idlelib.idle_test.htest import run
|
jpayne@68
|
164 run(_search_dialog)
|