B
    2*™\u  ã               @   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 for search capability.
Defines various replace related functions like replace, replace all,
replace+find.
é    N)Ú	StringVarÚTclError)ÚSearchDialogBase)Úsearchenginec             C   s<   |   ¡ }t |¡}t|dƒs(t||ƒ|_|j}| | ¡ dS )zvReturns a singleton ReplaceDialog instance.The single dialog
     saves user entries and preferences across instances.Ú_replacedialogN)Z_rootr   ÚgetÚhasattrÚReplaceDialogr   Úopen)ÚtextÚrootÚengineZdialog© r   ú:C:\ALexclude\prg\programme\Python37\Lib\idlelib\replace.pyÚreplace   s    

r   c               @   sˆ   e Zd ZdZdZ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dS )%r	   zReplace DialogÚReplacec             C   s   t  | ||¡ t|ƒ| _d S )N)r   Ú__init__r   Úreplvar)Úselfr   r   r   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Display the replace dialogz	sel.firstNzsel.lastÚinserté   )r   r
   Úindexr   Úshow_hitÚok)r   r   ÚfirstÚlastr   r   r   r
   !   s    

zReplaceDialog.openc             C   s"   t  | ¡ |  d| j¡d | _dS )z#Create label and text entry widgetszReplace with:r   N)r   Úcreate_entriesZ
make_entryr   Zreplent)r   r   r   r   r   1   s    
zReplaceDialog.create_entriesc             C   sH   t  | ¡ |  d| j¡ |  d| j¡ |  d| jd¡ |  d| j¡ d S )NZFindr   zReplace+Findr   zReplace All)r   Úcreate_command_buttonsZmake_buttonÚfind_itÚ
replace_itÚdefault_commandÚreplace_all)r   r   r   r   r   6   s
    
z$ReplaceDialog.create_command_buttonsNc             C   s   |   d¡ d S )Nr   )Údo_find)r   Úeventr   r   r   r   =   s    zReplaceDialog.find_itc             C   s   |   | j¡r|  ¡  d S )N)r"   r   Ú
do_replace)r   r#   r   r   r   r   @   s    zReplaceDialog.replace_itc             C   s"   |   | j¡r|  ¡ r|   d¡ dS )zReplace and find next.r   N)r"   r   r$   )r   r#   r   r   r   r    D   s    zReplaceDialog.default_commandc             C   sL   | j  ¡ rDy| |¡}W qH tjk
r@   | j  |d¡ d}Y qHX n|}|S )za Helper function for expanding a regular expression
            in the replace field, if needed. zInvalid Replace ExpressionN)r   ZisreÚexpandÚreÚerrorZreport_error)r   ÚmÚreplÚnewr   r   r   Ú_replace_expandK   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  ||||d|¡}|s´P |\}}| d| d|d  ¡}| ¡ }|  ||¡}|dkròP | ¡ \}}d	||f }	d	||f }
||kr.| d
|
¡ n4| d
|	¡ |	|
krP| |	|
¡ |rb| |	|¡ |t|ƒ }d}q˜W | ¡  |	r–|
r–|  |	|
¡ |  ¡  dS )z4Replace all instances of patvar with replvar in textNÚselz1.0ÚendÚhitr   r   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!   Y   sZ    




zReplaceDialog.replace_allr   c       
      C   sv   | j  ¡ sdS | j}| j  |d |¡}|s4|  ¡  dS |\}}| ¡ \}}d||f }d||f }	|  ||	¡ d| _dS )NFz%d.%dr   T)r   r/   r   r0   r1   r6   r   r   )
r   r   r   r=   r>   r(   rA   rB   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 )	NFz	sel.firstzsel.lastr   z%d.0r   r   T)r   r/   r   r   r   r   Zget_line_colr   Úmatchr+   r   r7   r4   r5   r8   r   r:   r   r   )r   r<   r   r   Úposr   r>   r?   r@   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 )zMHighlight text from 'first' to 'last'.
        'first', 'last' - Text indicesr   r,   z1.0r-   r.   N)r   r7   r2   Ú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 )Nr.   z1.0r-   )r   r;   r   r2   )r   r#   r   r   r   r;   É   s    zReplaceDialog.close)N)N)N)N)r   )N)Ú__name__Ú
__module__Ú__qualname__ÚtitleZiconr   r
   r   r   r   r   r    r+   r!   r"   r$   r   r;   r   r   r   r   r	      s   



2
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ú+r   z+%d+%dé¯   c               S   s   d S )Nr   r   r   r   r   r4   Ø   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)rE   r   r2   r   )rL   rM   r   r   r   Úshow_replaceç   s    z%_replace_dialog.<locals>.show_replacer   )r   Zcommand)ÚtkinterrJ   rK   rL   rM   Ztkinter.ttkrN   rO   rI   ÚmapÚintZgeometryÚsplitÚpackr4   r:   r   Z	focus_set)ÚparentrJ   rK   rN   rO   ÚtopÚxÚyr4   r:   ÚframerR   Zbuttonr   )rL   rM   r   r   Ú_replace_dialogÎ   s&    
 r]   Ú__main__)Úmainzidlelib.idle_test.test_replaceé   F)Ú	verbosityÚexit)Úrun)Ú__doc__r&   rS   r   r   Zidlelib.searchbaser   Zidlelibr   r   r	   r]   rF   Zunittestr_   Zidlelib.idle_test.htestrc   r   r   r   r   Ú<module>   s    7!