
   59*q                       S r SSKJr  SSKJr  SSKJr  S rS rS r	S r
 " S	 S
\5      rS r\S:X  a  SSKJr  \" SSSS9  SSKJr  \" \5        gg)zSearch dialog for Find, Find Again, and Find Selection
functionality.

Inherits from SearchDialogBase for GUI and uses searchengine
to prepare search pattern.
    )TclError)searchengine)SearchDialogBasec                     U R                  5       n[        R                  " U5      n[        US5      (       d  [	        X5      Ul        UR
                  $ )zReturn 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.
_searchdialog)_rootr   gethasattrSearchDialogr   )textrootengines      %/usr/lib/python3.13/idlelib/search.py_setupr      sE     ::<Dd#F6?+++D9    c                 Z    U R                  SS5      n[        U 5      R                  X5      $ )zOpen 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.
	sel.firstsel.last)r	   r   open)r   pats     r   findr      s)     ((;

+C$<T''r   c                 6    [        U 5      R                  U 5      $ )a;  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.
)r   
find_againr   s    r   r   r   &   s     $<""4((r   c                 6    [        U 5      R                  U 5      $ )aw  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.
)r   find_selectionr   s    r   r   r   1   s     $<&&t,,r   c                   4    \ rS rSrSrS rS	S jrS rS rSr	g)
r   >   z%Dialog for finding a pattern in text.c                 f    [         R                  " U 5        U R                  SU R                  SS9  g)z=Create the base search dialog and add a button for Find Next.z	Find NextT)isdefN)r   create_widgetsmake_buttondefault_command)selfs    r   r!   SearchDialog.create_widgetsA   s+    ''-d&:&:$Gr   Nc                 z    U R                   R                  5       (       d  gU R                  U R                  5        g)z3Handle the Find Next button as the default command.N)r   getprogr   r   )r$   events     r   r#   SearchDialog.default_commandG   s(    {{""$$		"r   c                    U R                   R                  5       (       d  U R                  U5        gU R                   R                  5       (       d  gU R                   R	                  U5      nU(       a  Uu  p4UR                  5       u  pVSX54-  nSX64-  n UR                  S5      n	UR                  S5      n
X:X  a  X:X  a  U R                  5         gUR                  SSS5        UR                  SXx5        UR                  SU R                   R                  5       =(       a    U=(       d    U5        UR                  S5        g	U R                  5         g! [         a     Nf = f)
a  Repeat the last search.

If no search was previously run, open a new search dialog.  In
this case, no search is done.

If a search was previously run, the search dialog won't be
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.
Fz%d.%dr   r   sel1.0endinsertT)r   getpatr   r'   search_textspanindexbellr   
tag_removetag_addmark_setisbacksee)r$   r   reslinemijfirstlastselfirstsellasts              r   r   SearchDialog.find_againM   s)     {{!!##IIdO{{""$$kk%%d+GD668DAti'EdY&D::k2**Z0$IIK  OOE5%0LL,MM(DKK$6$6$8$BU$JdKHHXIIK  s   <E 
E%$E%c                     UR                  SS5      nU(       a  U R                  R                  U5        U R                  U5      $ )a  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.
r   r   )r	   r   setcookedpatr   )r$   r   r   s      r   r   SearchDialog.find_selectiony   s8     hh{J/KK$$S)t$$r    )N)
__name__
__module____qualname____firstlineno____doc__r!   r#   r   r   __static_attributes__rF   r   r   r   r   >   s    +H#*X%r   r   c                   ^ SSK JnJn  SSKJnJn  U" U 5      nUR                  S5        [        [        U R                  5       R                  S5      SS 5      u  pgUR                  SXgS	-   4-  5        U" U5      nUR                  5         U" US
S9mTR                  5         TR                  SS5        U4S jn	U" USU	S9n
U
R                  5         g)zDisplay search test box.r   )ToplevelText)FrameButtonzTest SearchDialog+   Nz+%d+%d   gray)inactiveselectbackgroundr.   z}This is a sample string.
This is a sample string.
This is a sample string.
This is a sample string.
This is a sample string.
c                     > T R                  SSS5        [        T 5      R                  T 5        T R                  SSS5        g )Nr+   r,   r-   )r5   r   r   r4   r   s   r   	show_find!_search_dialog.<locals>.show_find   s5    UE5)t$ue,r   zSearch (selection ignored))r   command)tkinterrN   rO   tkinter.ttkrP   rQ   titlemapintgeometrysplitpackr.   )parentrN   rO   rP   rQ   topxyframerX   buttonr   s              @r   _search_dialogri      s    &)
6
CII!"sFOO%++C045DALLQCL()#JE	JJL7DIIKKK78-
 E <iPF
KKMr   __main__)mainzidlelib.idle_test.test_search   F)	verbosityexit)runN)rK   r[   r   idlelibr   idlelib.searchbaser   r   r   r   r   r   ri   rG   unittestrk   idlelib.idle_test.htestro   rF   r   r   <module>rt      sh       / 	(	)
-F%# F%R2 z	(AEB+ r   