
    d'                         d Z ddlZddlmZmZ ddlmZ ddlmZ ddZ	 G d de          Z
d	 Zed
k    r'ddlmZ  eddd           ddlmZ  ee           dS dS )zReplace dialog for IDLE. Inherits SearchDialogBase for GUI.
Uses idlelib.searchengine.SearchEngine for search capability.
Defines various replace related functions like replace, replace all,
and replace+find.
    N)	StringVarTclError)SearchDialogBase)searchenginec                     |                                  }t          j        |          }t          |d          st	          ||          |_        |j        }|                    | |           dS )zCreate or reuse a singleton ReplaceDialog instance.

    The singleton dialog saves user entries and preferences
    across instances.

    Args:
        text: Text widget containing the text to be searched.
    _replacedialog)insert_tagsN)_rootr   gethasattrReplaceDialogr   open)textr	   rootenginedialogs        !  /croot/python-split_1694437901252/_h_env_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_plac/lib/python3.11/idlelib/replace.pyreplacer      sj     ::<<Dd##F6+,, < -dF ; ;"F
KK+K.....    c                        e Zd ZdZdZdZ fdZddZd Zd Z	dd	Z
dd
ZddZd ZddZddZd Zd ZddZ xZS )r   z3Dialog for finding and replacing a pattern in text.zReplace DialogReplacec                     t                                          ||           t          |          | _        d| _        dS )a  Create search dialog for finding and replacing text.

        Uses SearchDialogBase as the basis for the GUI and a
        searchengine instance to prepare the search.

        Attributes:
            replvar: StringVar containing 'Replace with:' value.
            replent: Entry widget for replvar.  Created in
                create_entries().
            ok: Boolean used in searchengine.search_text to indicate
                whether the search includes the selection.
        N)super__init__r   replvarr	   )selfr   r   	__class__s      r   r   zReplaceDialog.__init__%   s:     	v&&& r   Nc                 R   t          j        | |           	 |                    d          }n# t          $ r d}Y nw xY w	 |                    d          }n# t          $ r d}Y nw xY w|p|                    d          }|p|}|                     ||           d| _        || _        dS )zMake dialog visible on top of others and ready to use.

        Also, highlight the currently selected text and set the
        search to include the current selection (self.ok).

        Args:
            text: Text widget being searched.
        	sel.firstNsel.lastinsertT)r   r   indexr   show_hitokr	   )r   r   r	   firstlasts        r   r   zReplaceDialog.open6   s     	dD)))	JJ{++EE 	 	 	EEE		::j))DD 	 	 	DDD	-H--}ueT"""&s   - << A A%$A%c                 z    t          j        |            |                     d| j                  d         | _        dS )z8Create base and additional label and text entry widgets.zReplace with:r   N)r   create_entries
make_entryr   replentr   s    r   r(   zReplaceDialog.create_entriesN   s3    '---EEaHr   c                 
   t          j        |            |                     d| j                   |                     d| j                   |                     d| j        d           |                     d| j                   dS )zCreate base and additional command buttons.

        The additional buttons are for Find, Replace,
        Replace+Find, and Replace All.
        Findr   zReplace+FindT)isdefzReplace AllN)r   create_command_buttonsmake_buttonfind_it
replace_itdefault_commandreplace_allr+   s    r   r/   z$ReplaceDialog.create_command_buttonsS   s     	/555...DO444)=TJJJ(899999r   c                 0    |                      d           dS )zHandle the Find button.FN)do_findr   events     r   r1   zReplaceDialog.find_it_   s    Ur   c                 f    |                      | j                  r|                                  dS dS )z]Handle the Replace button.

        If the find is successful, then perform replace.
        Nr6   r$   
do_replacer7   s     r   r2   zReplaceDialog.replace_itc   s:    
 <<   	OO	 	r   c                     |                      | j                  r+|                                 r|                      d           dS dS dS )zHandle the Replace+Find button as the default command.

        First performs a replace and then, if the replace was
        successful, a find next.
        FNr:   r7   s     r   r3   zReplaceDialog.default_commandk   sX     <<   	$   $U#####	$ 	$$ $r   c                     | j                                         rI	 |                    |          }n4# t          j        $ r  | j                             |d           d}Y nw xY w|}|S )z.Expand replacement text if regular expression.zInvalid Replace ExpressionN)r   isreexpandreerrorreport_error)r   mreplnews       r   _replace_expandzReplaceDialog._replace_expandv   sy    ; 	hhtnn8   ((/KLLL C
s   1 ,A A c                    | j                                         }|sdS | j                                        }| j        }| j                             ||          }|s|                                  dS |                    ddd           |                    ddd           |d         }|d                                         }| j         	                                rd}d}d}dx}	}
|
                                 | j                             ||||d	|
          x}r|\  }}|                    d|z  d|dz   z            }|                                }|                     ||          }|n|                                \  }}d||fz  }	d||fz  }
||k    r|                    d|
           nP|                    d|	           |	|
k    r|                    |	|
           |r|                    |	|| j                   |t'          |          z   }d	}| j                             ||||d	|
          x}|                                 |	r|
r|                     |	|
           |                                  dS )a  Handle the Replace All button.

        Search text for occurrences of the Find value and replace
        each of them.  The 'wrap around' value controls the start
        point for searching.  If wrap isn't set, then the searching
        starts at the first occurrence after the current selection;
        if wrap is set, the replacement starts at the first line.
        The replacement is always done top-to-bottom in the text.
        Nsel1.0endhitr      TF)wrapr$   %d.0%d.%dr!   )r   getprogr   r   r   search_textbell
tag_removestartiswrapundo_block_startsearch_forwardgrouprF   spanmark_setdeleter!   r	   lenundo_block_stopr#   close)r   r8   progrD   r   reslinecolr$   r%   r&   rC   charsorigrE   ijs                    r   r4   zReplaceDialog.replace_all   s    {""$$ 	F|!!yk%%dD11 	IIKKKFue,,,ue,,,1v!fllnn; 	DC[//dD#Eb 0 : : :c 	GD!HHVd]Fd1f,=>>E7799D&&q$//C{6688DAqtQi'EdAY&Dd{{h----h...D==KKt,,, >KKsD,<===c#hh,CB) [//dD#Eb 0 : : :c 	* 	 	'T 	'MM%&&&

r   Fc                 @   | j                                         sdS | j        }| j                             |d|          }|s|                                  dS |\  }}|                                \  }}d||fz  }d||fz  }	|                     ||	           d| _        dS )z|Search for and highlight next occurrence of pattern in text.

        No text replacement is done with this option.
        FNrO   T)r   rP   r   rQ   rR   rY   r#   r$   )
r   r$   r   r`   ra   rC   re   rf   r%   r&   s
             r   r6   zReplaceDialog.do_find   s    
 {""$$ 	5yk%%dD"55 	IIKKK5avvxx14)#$"eT"""tr   c                 r   | j                                         }|sdS | j        }	 |                    d          x}}|                    d          }n# t          $ r d}Y nw xY w|s|                    d          x}x}}t          j        |          \  }}|                    d|z  d|dz   z            }|                    ||          }	|sdS | 	                    |	| j
                                                  }
|
dS |                    d|           |                                 |	                                r|                    ||           |
r|                    ||
| j                   |                                 |                     ||                    d                     d| _        dS )	z6Replace search pattern in text with replacement value.Fr   r    Nr!   rN   rL   T)r   rP   r   r"   r   r   get_line_colr   matchrF   r   rZ   rV   rX   r[   r!   r	   r]   r#   r$   )r   r_   r   r%   posr&   ra   rb   rc   rC   rE   s              r   r;   zReplaceDialog.do_replace   s   {""$$ 	5y	**[111EC::j))DD 	 	 	CCC	 	6!%H!5!55E5D3 -c22	c$$q&(9::JJuc"" 	5""1dl&6&6&8&899;5h&&&7799 	%KKt$$$ 	6KKsD$4555eTZZ11222ts   ,A A"!A"c                    | j         }|                    d|           |                    ddd           |                    d||           |                    ddd           ||k    r|                    d|           n|                    d||           |                    d           |                                 dS )a  Highlight text between first and last indices.

        Text is highlighted via the 'hit' tag and the marked
        section is brought into view.

        The colors from the 'hit' tag aren't currently shown
        when the text is displayed.  This is due to the 'sel'
        tag being added first, so the colors in the 'sel'
        config are seen instead of the colors for 'hit'.
        r!   rH   rI   rJ   rK   N)r   rZ   rS   tag_addseeupdate_idletasks)r   r%   r&   r   s       r   r#   zReplaceDialog.show_hit   s     yh&&&ue,,,UE4(((ue,,,D==LL&&&&LLt,,,r   c                 v    t          j        | |           | j                            ddd           d| _        dS )z%Close the dialog and remove hit tags.rK   rI   rJ   N)r   r^   r   rS   r	   r7   s     r   r^   zReplaceDialog.close  s=    tU+++	UE5111r   N)F)__name__
__module____qualname____doc__titleiconr   r   r(   r/   r1   r2   r3   rF   r4   r6   r;   r#   r^   __classcell__)r   s   @r   r   r      s$       99ED         "' ' ' '0I I I

: 
: 
:      	$ 	$ 	$ 	$  6 6 6 6p   (  @     .               r   r   c                    ddl m}m}mm ddlm}m}  ||           }|                    d           t          t          |                                                     d          dd                    \  }}|                    d||dz   fz             d	 }d
 }	 ||          }
|
                                  ||
d          |_        |	_                                                             dd                                            fd} ||
d|          }|                                 d S )Nr   )ToplevelTextENDSEL)FrameButtonzTest ReplaceDialog+rL   z+%d+%d   c                      d S rq    r   r   r   rV   z)_replace_dialog.<locals>.undo_block_start      r   c                      d S rq   r   r   r   r   r]   z(_replace_dialog.<locals>.undo_block_stop  r   r   gray)inactiveselectbackgroundr!   z"This is a sample sTring
Plus MORE.c                                           d            t                                         d            d S )NrI   )rm   r   rS   )r|   r}   r   s   r   show_replacez%_replace_dialog.<locals>.show_replace&  s@    S%%%%UC(((((r   r   )r   command)tkinterrz   r{   r|   r}   tkinter.ttkr~   r   rv   mapintgeometrysplitpackrV   r]   r!   	focus_set)parentrz   r{   r~   r   topxyrV   r]   framer   buttonr|   r}   r   s                @@@r   _replace_dialogr     s   000000000000))))))))
(6

CII"###sFOO%%++C00455DAqLLQCL()))     E#JJE	JJLLL4777D,D*DIIKKKKK>???NN) ) ) ) ) ) )
 VE	<@@@F
KKMMMMMr   __main__)mainzidlelib.idle_test.test_replace   F)	verbosityexit)runrq   )ru   r@   r   r   r   idlelib.searchbaser   idlelibr   r   r   r   rr   unittestr   idlelib.idle_test.htestr   r   r   r   <module>r      s   
 
			 ' ' ' ' ' ' ' ' / / / / / /            / / / /"k  k  k  k  k $ k  k  k \  B zD	)QUCCCC++++++C r   