annotate CSP2/CSP2_env/env-d9b9114564458d9d-741b3de822f2aaca6c6caa4325c4afce/lib/python3.8/idlelib/search.py @ 69:33d812a61356

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