| 
									
										
										
										
											2019-03-16 19:29:33 -04:00
										 |  |  | """Search dialog for Find, Find Again, and Find Selection
 | 
					
						
							|  |  |  |    functionality. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    Inherits from SearchDialogBase for GUI and uses searchengine | 
					
						
							|  |  |  |    to prepare search pattern. | 
					
						
							|  |  |  | """
 | 
					
						
							| 
									
										
										
										
											2016-07-10 20:21:31 -04:00
										 |  |  | from tkinter import TclError | 
					
						
							| 
									
										
										
										
											2000-08-15 01:13:23 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-28 13:22:31 -04:00
										 |  |  | from idlelib import searchengine | 
					
						
							|  |  |  | from idlelib.searchbase import SearchDialogBase | 
					
						
							| 
									
										
										
										
											2000-08-15 01:13:23 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | def _setup(text): | 
					
						
							| 
									
										
										
										
											2019-03-16 19:29:33 -04:00
										 |  |  |     """Return the new or existing singleton SearchDialog instance.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     The singleton dialog saves user entries and preferences | 
					
						
							|  |  |  |     across instances. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Args: | 
					
						
							|  |  |  |         text: Text widget containing the text to be searched. | 
					
						
							|  |  |  |     """
 | 
					
						
							| 
									
										
										
										
											2000-08-15 01:13:23 +00:00
										 |  |  |     root = text._root() | 
					
						
							| 
									
										
										
										
											2016-05-28 13:22:31 -04:00
										 |  |  |     engine = searchengine.get(root) | 
					
						
							| 
									
										
										
										
											2000-08-15 01:13:23 +00:00
										 |  |  |     if not hasattr(engine, "_searchdialog"): | 
					
						
							|  |  |  |         engine._searchdialog = SearchDialog(root, engine) | 
					
						
							|  |  |  |     return engine._searchdialog | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def find(text): | 
					
						
							| 
									
										
										
										
											2019-03-16 19:29:33 -04:00
										 |  |  |     """Open the search dialog.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Module-level function to access the singleton SearchDialog | 
					
						
							|  |  |  |     instance and open the dialog.  If text is selected, it is | 
					
						
							|  |  |  |     used as the search phrase; otherwise, the previous entry | 
					
						
							|  |  |  |     is used.  No search is done with this command. | 
					
						
							|  |  |  |     """
 | 
					
						
							| 
									
										
										
										
											2002-11-06 02:18:45 +00:00
										 |  |  |     pat = text.get("sel.first", "sel.last") | 
					
						
							| 
									
										
										
										
											2016-05-17 18:18:37 -04:00
										 |  |  |     return _setup(text).open(text, pat)  # Open is inherited from SDBase. | 
					
						
							| 
									
										
										
										
											2000-08-15 01:13:23 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | def find_again(text): | 
					
						
							| 
									
										
										
										
											2019-03-16 19:29:33 -04:00
										 |  |  |     """Repeat the search for the last pattern and preferences.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Module-level function to access the singleton SearchDialog | 
					
						
							|  |  |  |     instance to search again using the user entries and preferences | 
					
						
							|  |  |  |     from the last dialog.  If there was no prior search, open the | 
					
						
							|  |  |  |     search dialog; otherwise, perform the search without showing the | 
					
						
							|  |  |  |     dialog. | 
					
						
							|  |  |  |     """
 | 
					
						
							| 
									
										
										
										
											2000-08-15 01:13:23 +00:00
										 |  |  |     return _setup(text).find_again(text) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def find_selection(text): | 
					
						
							| 
									
										
										
										
											2019-03-16 19:29:33 -04:00
										 |  |  |     """Search for the selected pattern in the text.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Module-level function to access the singleton SearchDialog | 
					
						
							|  |  |  |     instance to search using the selected text.  With a text | 
					
						
							|  |  |  |     selection, perform the search without displaying the dialog. | 
					
						
							|  |  |  |     Without a selection, use the prior entry as the search phrase | 
					
						
							|  |  |  |     and don't display the dialog.  If there has been no prior | 
					
						
							|  |  |  |     search, open the search dialog. | 
					
						
							|  |  |  |     """
 | 
					
						
							| 
									
										
										
										
											2000-08-15 01:13:23 +00:00
										 |  |  |     return _setup(text).find_selection(text) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-31 00:50:55 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-08-15 01:13:23 +00:00
										 |  |  | class SearchDialog(SearchDialogBase): | 
					
						
							| 
									
										
										
										
											2019-03-16 19:29:33 -04:00
										 |  |  |     "Dialog for finding a pattern in text." | 
					
						
							| 
									
										
										
										
											2000-08-15 01:13:23 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def create_widgets(self): | 
					
						
							| 
									
										
										
										
											2019-03-16 19:29:33 -04:00
										 |  |  |         "Create the base search dialog and add a button for Find Next." | 
					
						
							| 
									
										
										
										
											2015-05-14 18:10:50 -04:00
										 |  |  |         SearchDialogBase.create_widgets(self) | 
					
						
							| 
									
										
										
										
											2019-03-16 19:29:33 -04:00
										 |  |  |         # TODO - why is this here and not in a create_command_buttons? | 
					
						
							|  |  |  |         self.make_button("Find Next", self.default_command, isdef=True) | 
					
						
							| 
									
										
										
										
											2000-08-15 01:13:23 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def default_command(self, event=None): | 
					
						
							| 
									
										
										
										
											2019-03-16 19:29:33 -04:00
										 |  |  |         "Handle the Find Next button as the default command." | 
					
						
							| 
									
										
										
										
											2000-08-15 01:13:23 +00:00
										 |  |  |         if not self.engine.getprog(): | 
					
						
							|  |  |  |             return | 
					
						
							| 
									
										
										
										
											2013-06-10 23:01:20 -05:00
										 |  |  |         self.find_again(self.text) | 
					
						
							| 
									
										
										
										
											2000-08-15 01:13:23 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def find_again(self, text): | 
					
						
							| 
									
										
										
										
											2019-03-16 19:29:33 -04:00
										 |  |  |         """Repeat the last search.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         If no search was previously run, open a new search dialog.  In | 
					
						
							|  |  |  |         this case, no search is done. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-03 09:51:15 +05:30
										 |  |  |         If a search was previously run, the search dialog won't be | 
					
						
							| 
									
										
										
										
											2019-03-16 19:29:33 -04:00
										 |  |  |         shown and the options from the previous search (including the | 
					
						
							|  |  |  |         search pattern) will be used to find the next occurrence | 
					
						
							|  |  |  |         of the pattern.  Next is relative based on direction. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         Position the window to display the located occurrence in the | 
					
						
							|  |  |  |         text. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         Return True if the search was successful and False otherwise. | 
					
						
							|  |  |  |         """
 | 
					
						
							| 
									
										
										
										
											2000-08-15 01:13:23 +00:00
										 |  |  |         if not self.engine.getpat(): | 
					
						
							|  |  |  |             self.open(text) | 
					
						
							| 
									
										
										
										
											2002-09-18 03:10:10 +00:00
										 |  |  |             return False | 
					
						
							| 
									
										
										
										
											2000-08-15 01:13:23 +00:00
										 |  |  |         if not self.engine.getprog(): | 
					
						
							| 
									
										
										
										
											2002-09-18 03:10:10 +00:00
										 |  |  |             return False | 
					
						
							| 
									
										
										
										
											2000-08-15 01:13:23 +00:00
										 |  |  |         res = self.engine.search_text(text) | 
					
						
							|  |  |  |         if res: | 
					
						
							|  |  |  |             line, m = res | 
					
						
							|  |  |  |             i, j = m.span() | 
					
						
							|  |  |  |             first = "%d.%d" % (line, i) | 
					
						
							|  |  |  |             last = "%d.%d" % (line, j) | 
					
						
							|  |  |  |             try: | 
					
						
							|  |  |  |                 selfirst = text.index("sel.first") | 
					
						
							|  |  |  |                 sellast = text.index("sel.last") | 
					
						
							|  |  |  |                 if selfirst == first and sellast == last: | 
					
						
							| 
									
										
										
										
											2016-08-10 23:44:54 -04:00
										 |  |  |                     self.bell() | 
					
						
							| 
									
										
										
										
											2002-09-18 03:10:10 +00:00
										 |  |  |                     return False | 
					
						
							| 
									
										
										
										
											2000-08-15 01:13:23 +00:00
										 |  |  |             except TclError: | 
					
						
							|  |  |  |                 pass | 
					
						
							|  |  |  |             text.tag_remove("sel", "1.0", "end") | 
					
						
							|  |  |  |             text.tag_add("sel", first, last) | 
					
						
							|  |  |  |             text.mark_set("insert", self.engine.isback() and first or last) | 
					
						
							|  |  |  |             text.see("insert") | 
					
						
							| 
									
										
										
										
											2002-09-18 03:10:10 +00:00
										 |  |  |             return True | 
					
						
							| 
									
										
										
										
											2000-08-15 01:13:23 +00:00
										 |  |  |         else: | 
					
						
							| 
									
										
										
										
											2016-08-10 23:44:54 -04:00
										 |  |  |             self.bell() | 
					
						
							| 
									
										
										
										
											2002-09-18 03:10:10 +00:00
										 |  |  |             return False | 
					
						
							| 
									
										
										
										
											2000-08-15 01:13:23 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def find_selection(self, text): | 
					
						
							| 
									
										
										
										
											2019-03-16 19:29:33 -04:00
										 |  |  |         """Search for selected text with previous dialog preferences.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         Instead of using the same pattern for searching (as Find | 
					
						
							|  |  |  |         Again does), this first resets the pattern to the currently | 
					
						
							|  |  |  |         selected text.  If the selected text isn't changed, then use | 
					
						
							|  |  |  |         the prior search phrase. | 
					
						
							|  |  |  |         """
 | 
					
						
							| 
									
										
										
										
											2000-08-15 01:13:23 +00:00
										 |  |  |         pat = text.get("sel.first", "sel.last") | 
					
						
							|  |  |  |         if pat: | 
					
						
							|  |  |  |             self.engine.setcookedpat(pat) | 
					
						
							|  |  |  |         return self.find_again(text) | 
					
						
							| 
									
										
										
										
											2014-05-27 03:30:54 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-17 18:18:37 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | def _search_dialog(parent):  # htest # | 
					
						
							| 
									
										
										
										
											2016-07-10 20:21:31 -04:00
										 |  |  |     "Display search test box." | 
					
						
							|  |  |  |     from tkinter import Toplevel, Text | 
					
						
							| 
									
										
										
										
											2019-01-02 22:04:06 -05:00
										 |  |  |     from tkinter.ttk import Frame, Button | 
					
						
							| 
									
										
										
										
											2016-07-10 20:21:31 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-02 22:04:06 -05:00
										 |  |  |     top = Toplevel(parent) | 
					
						
							|  |  |  |     top.title("Test SearchDialog") | 
					
						
							| 
									
										
										
										
											2016-07-10 17:28:10 -04:00
										 |  |  |     x, y = map(int, parent.geometry().split('+')[1:]) | 
					
						
							| 
									
										
										
										
											2019-01-02 22:04:06 -05:00
										 |  |  |     top.geometry("+%d+%d" % (x, y + 175)) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     frame = Frame(top) | 
					
						
							|  |  |  |     frame.pack() | 
					
						
							|  |  |  |     text = Text(frame, inactiveselectbackground='gray') | 
					
						
							| 
									
										
										
										
											2014-05-27 03:30:54 -04:00
										 |  |  |     text.pack() | 
					
						
							| 
									
										
										
										
											2016-05-17 18:18:37 -04:00
										 |  |  |     text.insert("insert","This is a sample string.\n"*5) | 
					
						
							| 
									
										
										
										
											2014-05-27 03:30:54 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def show_find(): | 
					
						
							| 
									
										
										
										
											2016-07-10 20:21:31 -04:00
										 |  |  |         text.tag_add('sel', '1.0', 'end') | 
					
						
							| 
									
										
										
										
											2016-05-17 18:18:37 -04:00
										 |  |  |         _setup(text).open(text) | 
					
						
							| 
									
										
										
										
											2016-07-10 20:21:31 -04:00
										 |  |  |         text.tag_remove('sel', '1.0', 'end') | 
					
						
							| 
									
										
										
										
											2014-05-27 03:30:54 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-02 22:04:06 -05:00
										 |  |  |     button = Button(frame, text="Search (selection ignored)", command=show_find) | 
					
						
							| 
									
										
										
										
											2014-05-27 03:30:54 -04:00
										 |  |  |     button.pack() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | if __name__ == '__main__': | 
					
						
							| 
									
										
										
										
											2018-06-19 19:12:52 -04:00
										 |  |  |     from unittest import main | 
					
						
							|  |  |  |     main('idlelib.idle_test.test_search', verbosity=2, exit=False) | 
					
						
							| 
									
										
										
										
											2016-07-10 17:28:10 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-27 03:30:54 -04:00
										 |  |  |     from idlelib.idle_test.htest import run | 
					
						
							|  |  |  |     run(_search_dialog) |