2019-03-16 20:47:28 -03: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 21:21:31 -03:00
|
|
|
from tkinter import TclError
|
2000-08-14 22:13:23 -03:00
|
|
|
|
2016-05-28 14:22:31 -03:00
|
|
|
from idlelib import searchengine
|
|
|
|
from idlelib.searchbase import SearchDialogBase
|
2000-08-14 22:13:23 -03:00
|
|
|
|
|
|
|
def _setup(text):
|
2019-03-16 20:47:28 -03: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-14 22:13:23 -03:00
|
|
|
root = text._root()
|
2016-05-28 14:22:31 -03:00
|
|
|
engine = searchengine.get(root)
|
2000-08-14 22:13:23 -03:00
|
|
|
if not hasattr(engine, "_searchdialog"):
|
|
|
|
engine._searchdialog = SearchDialog(root, engine)
|
|
|
|
return engine._searchdialog
|
|
|
|
|
|
|
|
def find(text):
|
2019-03-16 20:47:28 -03: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-05 22:18:45 -04:00
|
|
|
pat = text.get("sel.first", "sel.last")
|
2016-05-17 19:18:37 -03:00
|
|
|
return _setup(text).open(text, pat) # Open is inherited from SDBase.
|
2000-08-14 22:13:23 -03:00
|
|
|
|
|
|
|
def find_again(text):
|
2019-03-16 20:47:28 -03: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-14 22:13:23 -03:00
|
|
|
return _setup(text).find_again(text)
|
|
|
|
|
|
|
|
def find_selection(text):
|
2019-03-16 20:47:28 -03: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-14 22:13:23 -03:00
|
|
|
return _setup(text).find_selection(text)
|
|
|
|
|
2016-08-31 01:50:55 -03:00
|
|
|
|
2000-08-14 22:13:23 -03:00
|
|
|
class SearchDialog(SearchDialogBase):
|
2019-03-16 20:47:28 -03:00
|
|
|
"Dialog for finding a pattern in text."
|
2000-08-14 22:13:23 -03:00
|
|
|
|
|
|
|
def create_widgets(self):
|
2019-03-16 20:47:28 -03:00
|
|
|
"Create the base search dialog and add a button for Find Next."
|
2015-05-14 19:10:50 -03:00
|
|
|
SearchDialogBase.create_widgets(self)
|
2019-03-16 20:47:28 -03: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-14 22:13:23 -03:00
|
|
|
|
|
|
|
def default_command(self, event=None):
|
2019-03-16 20:47:28 -03:00
|
|
|
"Handle the Find Next button as the default command."
|
2000-08-14 22:13:23 -03:00
|
|
|
if not self.engine.getprog():
|
|
|
|
return
|
2013-06-11 01:01:20 -03:00
|
|
|
self.find_again(self.text)
|
2000-08-14 22:13:23 -03:00
|
|
|
|
|
|
|
def find_again(self, text):
|
2019-03-16 20:47:28 -03: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 01:39:32 -03:00
|
|
|
If a search was previously run, the search dialog won't be
|
2019-03-16 20:47:28 -03: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-14 22:13:23 -03:00
|
|
|
if not self.engine.getpat():
|
|
|
|
self.open(text)
|
2002-09-18 00:10:10 -03:00
|
|
|
return False
|
2000-08-14 22:13:23 -03:00
|
|
|
if not self.engine.getprog():
|
2002-09-18 00:10:10 -03:00
|
|
|
return False
|
2000-08-14 22:13:23 -03: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-11 00:44:54 -03:00
|
|
|
self.bell()
|
2002-09-18 00:10:10 -03:00
|
|
|
return False
|
2000-08-14 22:13:23 -03: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 00:10:10 -03:00
|
|
|
return True
|
2000-08-14 22:13:23 -03:00
|
|
|
else:
|
2016-08-11 00:44:54 -03:00
|
|
|
self.bell()
|
2002-09-18 00:10:10 -03:00
|
|
|
return False
|
2000-08-14 22:13:23 -03:00
|
|
|
|
|
|
|
def find_selection(self, text):
|
2019-03-16 20:47:28 -03: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-14 22:13:23 -03:00
|
|
|
pat = text.get("sel.first", "sel.last")
|
|
|
|
if pat:
|
|
|
|
self.engine.setcookedpat(pat)
|
|
|
|
return self.find_again(text)
|
2014-05-27 04:30:54 -03:00
|
|
|
|
2016-05-17 19:18:37 -03:00
|
|
|
|
|
|
|
def _search_dialog(parent): # htest #
|
2016-07-10 21:21:31 -03:00
|
|
|
"Display search test box."
|
|
|
|
from tkinter import Toplevel, Text
|
2019-01-02 23:22:10 -04:00
|
|
|
from tkinter.ttk import Frame, Button
|
2016-07-10 21:21:31 -03:00
|
|
|
|
2019-01-02 23:22:10 -04:00
|
|
|
top = Toplevel(parent)
|
|
|
|
top.title("Test SearchDialog")
|
2016-07-10 18:28:10 -03:00
|
|
|
x, y = map(int, parent.geometry().split('+')[1:])
|
2019-01-02 23:22:10 -04:00
|
|
|
top.geometry("+%d+%d" % (x, y + 175))
|
|
|
|
|
|
|
|
frame = Frame(top)
|
|
|
|
frame.pack()
|
|
|
|
text = Text(frame, inactiveselectbackground='gray')
|
2014-05-27 04:30:54 -03:00
|
|
|
text.pack()
|
2016-05-17 19:18:37 -03:00
|
|
|
text.insert("insert","This is a sample string.\n"*5)
|
2014-05-27 04:30:54 -03:00
|
|
|
|
|
|
|
def show_find():
|
2016-07-10 21:21:31 -03:00
|
|
|
text.tag_add('sel', '1.0', 'end')
|
2016-05-17 19:18:37 -03:00
|
|
|
_setup(text).open(text)
|
2016-07-10 21:21:31 -03:00
|
|
|
text.tag_remove('sel', '1.0', 'end')
|
2014-05-27 04:30:54 -03:00
|
|
|
|
2019-01-02 23:22:10 -04:00
|
|
|
button = Button(frame, text="Search (selection ignored)", command=show_find)
|
2014-05-27 04:30:54 -03:00
|
|
|
button.pack()
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2018-06-19 21:11:24 -03:00
|
|
|
from unittest import main
|
|
|
|
main('idlelib.idle_test.test_search', verbosity=2, exit=False)
|
2016-07-10 18:28:10 -03:00
|
|
|
|
2014-05-27 04:30:54 -03:00
|
|
|
from idlelib.idle_test.htest import run
|
|
|
|
run(_search_dialog)
|