B
    ^ Çc¡&  ã               @   sŽ   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	„ d	eƒZ
d
d„ ZedkrŠddlmZ edddd ddlmZ eeƒ dS )zÑReplace 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             C   s<   |   ¡ }t |¡}t|dƒs(t||ƒ|_|j}| | ¡ dS )zÔCreate 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.
    Ú_replacedialogN)Z_rootr   ÚgetÚhasattrÚReplaceDialogr   Úopen)ÚtextÚrootÚengineZdialog© r   ú lib/python3.7/idlelib/replace.pyÚreplace   s    	

r   c                   s”   e Zd ZdZdZdZ‡ fdd„Zdd„ Zdd	„ Zd
d„ Z	d dd„Z
d!dd„Zd"dd„Zdd„ Zd#dd„Zd$dd„Zdd„ Zdd„ Zd%dd„Z‡  ZS )&r	   z3Dialog for finding and replacing a pattern in text.zReplace DialogÚReplacec                s   t ƒ  ||¡ t|ƒ| _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   Úreplvar)Úselfr   r   )Ú	__class__r   r   r   %   s    zReplaceDialog.__init__c             C   sˆ   t  | |¡ y| d¡}W n tk
r2   d}Y nX y| d¡}W n tk
rZ   d}Y nX |ph| d¡}|pp|}|  ||¡ d| _dS )zøMake 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.
        z	sel.firstNzsel.lastÚinsertT)r   r
   Úindexr   Úshow_hitÚok)r   r   ÚfirstÚlastr   r   r   r
   5   s    	

zReplaceDialog.openc             C   s"   t  | ¡ |  d| j¡d | _dS )z8Create base and additional label and text entry widgets.zReplace with:r   N)r   Úcreate_entriesZ
make_entryr   Zreplent)r   r   r   r   r   L   s    
zReplaceDialog.create_entriesc             C   sJ   t  | ¡ |  d| j¡ |  d| j¡ | jd| jdd |  d| j¡ dS )z’Create base and additional command buttons.

        The additional buttons are for Find, Replace,
        Replace+Find, and Replace All.
        ZFindr   zReplace+FindT)ZisdefzReplace AllN)r   Úcreate_command_buttonsZmake_buttonÚfind_itÚ
replace_itÚdefault_commandÚreplace_all)r   r   r   r   r   Q   s
    
z$ReplaceDialog.create_command_buttonsNc             C   s   |   d¡ dS )zHandle the Find button.FN)Údo_find)r   Úeventr   r   r   r   ]   s    zReplaceDialog.find_itc             C   s   |   | j¡r|  ¡  dS )z]Handle the Replace button.

        If the find is successful, then perform replace.
        N)r#   r   Ú
do_replace)r   r$   r   r   r   r    a   s    zReplaceDialog.replace_itc             C   s"   |   | j¡r|  ¡ r|   d¡ dS )zŸHandle the Replace+Find button as the default command.

        First performs a replace and then, if the replace was
        successful, a find next.
        FN)r#   r   r%   )r   r$   r   r   r   r!   i   s    zReplaceDialog.default_commandc             C   sL   | j  ¡ rDy| |¡}W qH tjk
r@   | j  |d¡ d}Y qHX n|}|S )z.Expand replacement text if regular expression.zInvalid Replace ExpressionN)r   ZisreÚexpandÚreÚerrorZreport_error)r   ÚmÚreplÚnewr   r   r   Ú_replace_expandt   s    
zReplaceDialog._replace_expandc             C   s¤  | 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 j||||d	|d
}|s¶P |\}}| d| d|d  ¡}| ¡ }|  ||¡}|dkrôP | ¡ \}}d||f }	d||f }
||kr0| d|
¡ n4| d|	¡ |	|
krR| |	|
¡ |rd| |	|¡ |t|ƒ }d	}q˜W | ¡  |	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Úselz1.0ÚendÚhitr   é   TF)Zwrapr   z%d.0z%d.%dr   )r   Úgetprogr   r   r   Úsearch_textÚbellÚ
tag_removeÚstartZiswrapÚundo_block_startZsearch_forwardÚgroupr,   ÚspanÚmark_setÚdeleter   ÚlenÚundo_block_stopr   Úclose)r   r$   Úprogr*   r   ÚresÚlineÚcolr   r   r   r)   ÚcharsZorigr+   ÚiÚjr   r   r   r"      s\    






zReplaceDialog.replace_allFc       
      C   sv   | j  ¡ sdS | j}| j  |d|¡}|s4|  ¡  dS |\}}| ¡ \}}d||f }d||f }	|  ||	¡ d| _dS )z|Search for and highlight next occurrence of pattern in text.

        No text replacement is done with this option.
        FNz%d.%dT)r   r1   r   r2   r3   r8   r   r   )
r   r   r   r?   r@   r)   rC   rD   r   r   r   r   r   r#   ¼   s    
zReplaceDialog.do_findc             C   s  | j  ¡ }|sdS | j}y| d¡ }}| d¡}W n tk
rL   d}Y nX |sd| d¡ } }}t |¡\}}| d| d|d  ¡}| ||¡}	|sždS |  	|	| j
 ¡ ¡}
|
dkr¼dS | d|¡ | ¡  |	 ¡ rä| ||¡ |
rô| ||
¡ | ¡  |  || d¡¡ d| _dS )	z6Replace search pattern in text with replacement value.Fz	sel.firstzsel.lastNr   z%d.0r0   T)r   r1   r   r   r   r   Zget_line_colr   Úmatchr,   r   r9   r6   r7   r:   r   r<   r   r   )r   r>   r   r   Úposr   r@   rA   rB   r)   r+   r   r   r   r%   Ð   s:    

zReplaceDialog.do_replacec             C   sv   | j }| d|¡ | ddd¡ | d||¡ | ddd¡ ||krR| 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   r-   z1.0r.   r/   N)r   r9   r4   Útag_addZseeZupdate_idletasks)r   r   r   r   r   r   r   r   ð   s    
zReplaceDialog.show_hitc             C   s    t  | |¡ | j ddd¡ dS )z%Close the dialog and remove hit tags.r/   z1.0r.   N)r   r=   r   r4   )r   r$   r   r   r   r=     s    zReplaceDialog.close)N)N)N)N)F)N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__ÚtitleZiconr   r
   r   r   r   r    r!   r,   r"   r#   r%   r   r=   Ú__classcell__r   r   )r   r   r	      s    



;
 r	   c                sî   ddl m}m}m‰ m‰ ddlm}m} || ƒ}| d¡ t	t
|  ¡  d¡dd … ƒ\}}| d||d f ¡ d	d
„ }dd„ }	||ƒ}
|
 ¡  ||
dd‰|ˆ_|	ˆ_ˆ ¡  ˆ dd¡ ˆ ¡  ‡ ‡‡fdd„}||
d|d}| ¡  d S )Nr   )ÚToplevelÚTextÚENDÚSEL)ÚFrameÚButtonzTest ReplaceDialogú+r0   z+%d+%dé¯   c               S   s   d S )Nr   r   r   r   r   r6     s    z)_replace_dialog.<locals>.undo_block_startc               S   s   d S )Nr   r   r   r   r   r<     s    z(_replace_dialog.<locals>.undo_block_stopZgray)Zinactiveselectbackgroundr   z"This is a sample sTring
Plus MORE.c                  s(   ˆ  ˆdˆ ¡ tˆƒ ˆ ˆdˆ ¡ d S )Nz1.0)rG   r   r4   r   )rP   rQ   r   r   r   Úshow_replace&  s    z%_replace_dialog.<locals>.show_replacer   )r   Zcommand)ÚtkinterrN   rO   rP   rQ   Ztkinter.ttkrR   rS   rL   ÚmapÚintZgeometryÚsplitZpackr6   r<   r   Z	focus_set)ÚparentrN   rO   rR   rS   ÚtopÚxÚyr6   r<   ÚframerV   Zbuttonr   )rP   rQ   r   r   Ú_replace_dialog  s&    
 r`   Ú__main__)Úmainzidlelib.idle_test.test_replaceé   F)Ú	verbosityÚexit)Úrun)rK   r'   rW   r   r   Zidlelib.searchbaser   Zidlelibr   r   r	   r`   rH   Zunittestrb   Zidlelib.idle_test.htestrf   r   r   r   r   Ú<module>   s    o!