a
    åzeÂ  ã                   @   sz   d dl Z d dlmZmZmZ d dlmZ dd„ ZG dd„ dƒZdd	„ Zd
d„ Z	dd„ Z
edkrvd dlmZ eddd dS )é    N)Ú	StringVarÚ
BooleanVarÚTclError)Ú
messageboxc                 C   s   t | dƒst| ƒ| _| jS )NÚ_searchengine)ÚhasattrÚSearchEnginer   )Úroot© r
   ú*/usr/lib/python3.9/idlelib/searchengine.pyÚget   s    

r   c                   @   sŒ   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ Zdd„ Zd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d „ZdS )%r   c                 C   sR   || _ t|dƒ| _t|dƒ| _t|dƒ| _t|dƒ| _t|dƒ| _t|dƒ| _d S )NÚ FT)	r	   r   Úpatvarr   ÚrevarÚcasevarÚwordvarÚwrapvarÚbackvar)Úselfr	   r
   r
   r   Ú__init__   s    zSearchEngine.__init__c                 C   s
   | j  ¡ S ©N)r   r   ©r   r
   r
   r   Úgetpat%   s    zSearchEngine.getpatc                 C   s   | j  |¡ d S r   )r   Úset©r   Úpatr
   r
   r   Úsetpat(   s    zSearchEngine.setpatc                 C   s
   | j  ¡ S r   )r   r   r   r
   r
   r   Úisre+   s    zSearchEngine.isrec                 C   s
   | j  ¡ S r   )r   r   r   r
   r
   r   Úiscase.   s    zSearchEngine.iscasec                 C   s
   | j  ¡ S r   )r   r   r   r
   r
   r   Úisword1   s    zSearchEngine.iswordc                 C   s
   | j  ¡ S r   )r   r   r   r
   r
   r   Úiswrap4   s    zSearchEngine.iswrapc                 C   s
   | j  ¡ S r   )r   r   r   r
   r
   r   Úisback7   s    zSearchEngine.isbackc                 C   s    |   ¡ rt |¡}|  |¡ d S r   )r   ÚreÚescaper   r   r
   r
   r   Úsetcookedpat<   s    
zSearchEngine.setcookedpatc                 C   s.   |   ¡ }|  ¡ st |¡}|  ¡ r*d| }|S )Nz\b%s\b)r   r   r"   r#   r   r   r
   r
   r   ÚgetcookedpatC   s    
zSearchEngine.getcookedpatc              
   C   sŠ   |   ¡ }|s|  |d¡ d S |  ¡ }d}|  ¡ s:|tjB }zt ||¡}W n: tjy„ } z |  ||j|j	¡ W Y d }~d S d }~0 0 |S )NzEmpty regular expressionr   )
r   Úreport_errorr%   r   r"   Ú
IGNORECASEÚcompileÚerrorÚmsgÚpos)r   r   ÚflagsÚprogÚer
   r
   r   ÚgetprogK   s    
zSearchEngine.getprogNc                 C   sN   dt |ƒ }|r |d t |ƒ }|d ur8|d t |ƒ }tjd|| jd d S )NzError: z

Pattern: z	
Offset: zRegular expression error)Zmaster)Ústrr   Z	showerrorr	   )r   r   r*   Úcolr
   r
   r   r&   \   s    ÿzSearchEngine.report_errorr   c                 C   s”   |s|   ¡ }|sd S | j ¡ }t|ƒ\}}|  ¡ rb|r<|}n|}t|ƒ\}}	|  ||||	||¡}
n.|rl|}n|}t|ƒ\}}	|  ||||	||¡}
|
S r   )r/   r   r   Úget_selectionr!   Úget_line_colÚsearch_backwardÚsearch_forward)r   Útextr-   ÚokÚwrapÚfirstÚlastÚstartÚliner1   Úresr
   r
   r   Úsearch_textf   s$    
zSearchEngine.search_textc                 C   s°   d}|}|  d| d|d  ¡}	|	r¬| |	d d… |¡}
|
rT|sL|
 ¡ |krT||
fS |d }|rj||krjq¬d}d}|  d| d|d  ¡}	|	s |r d}d}d}|  dd¡}	q d S )Nr   ú%d.0é   éÿÿÿÿz1.0z2.0)r   ÚsearchÚend)r   r6   r-   r<   r1   r8   r7   ÚwrappedÚ	startlineÚcharsÚmr
   r
   r   r5   Ž   s(    zSearchEngine.search_forwardc                 C   sÈ   d}|}|  d| d|d  ¡}	t||	d d… |ƒ}
|
rP|sH|
 ¡ |k rP||
fS |d }|rf||k rfqÄd}|dkrž|sxqÄd}d}| d¡}tt| d¡ƒ\}}|  d| d|d  ¡}	t|	ƒd }q d S )Nr   r?   r@   rA   zend-1cÚ.)r   Úsearch_reverser;   ÚindexÚmapÚintÚsplitÚlen)r   r6   r-   r<   r1   r8   r7   rD   rE   rF   rG   r+   r
   r
   r   r4   ¤   s*    
zSearchEngine.search_backward)N)Nr   )r   )r   )Ú__name__Ú
__module__Ú__qualname__r   r   r   r   r   r   r    r!   r$   r%   r/   r&   r>   r5   r4   r
   r
   r
   r   r      s   


(
r   c                 C   sj   |   |¡}|sd S d }| ¡ \}}||k rf||krf|}||krF|d }|   ||¡}|sXqf| ¡ \}}q"|S )Nr@   )rB   Úspan)r-   rF   r1   rG   ÚfoundÚiÚjr
   r
   r   rI   ½   s    
rI   c                 C   sR   z|   d¡}|   d¡}W n ty2   d  }}Y n0 |sB|   d¡}|sJ|}||fS )Nz	sel.firstzsel.lastÚinsert)rJ   r   )r6   r9   r:   r
   r
   r   r2   Ô   s    

r2   c                 C   s   t t|  d¡ƒ\}}||fS )NrH   )rK   rL   rM   )rJ   r<   r1   r
   r
   r   r3   â   s    r3   Ú__main__)Úmainz#idlelib.idle_test.test_searchengineé   )Ú	verbosity)r"   Útkinterr   r   r   r   r   r   rI   r2   r3   rO   ZunittestrX   r
   r
   r
   r   Ú<module>   s    +