î
\á[\9  ã               @   sŒ   d  Z  d d l m Z d d l m Z i d d 6d d 6d d	 6Z d
 Z Gd d „  d ƒ Z e d k rˆ d d l	 Z	 e	 j
 d d d ƒn  d S)zòParenMatch -- An IDLE extension for parenthesis matching.

When you hit a right paren, the cursor should move briefly to the left
paren.  Paren here is used generically; the matching applies to
parentheses, square brackets, and curly braces.
é    )ÚHyperParser)ÚidleConfú(ú)ú[ú]ú{ú}éd   c               @   sH  e  Z d  Z d Z d d1 g f g Z e j d d  d d d ƒZ e j d d  d	 d
 d d d ƒZ e j	 e j
 ƒ  d ƒ Z e j d d  d d
 d d d ƒZ d Z d2 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& „  Z d' d( „  Z d) d* „  Z d+ d, „  Z d- d. „  Z d/ d0 „  Z d" S)3Ú
ParenMatcha¤  Highlight matching parentheses

    There are three supported style of paren matching, based loosely
    on the Emacs options.  The style is select based on the
    HILITE_STYLE attribute; it can be changed used the set_style
    method.

    The supported styles are:

    default -- When a right paren is typed, highlight the matching
        left paren for 1/2 sec.

    expression -- When a right paren is typed, highlight the entire
        expression from the left paren to the right paren.

    TODO:
        - extend IDLE with configuration dialog to change options
        - implement rest of Emacs highlight styles (see below)
        - print mismatch warning in IDLE status window

    Note: In Emacs, there are several styles of highlight where the
    matching paren is highlighted whenever the cursor is immediately
    to the right of a right paren.  I don't know how to do that in Tk,
    so I haven't bothered.
    ZeditúShow surrounding parensú<<flash-paren>>Ú
extensionsÚstyleÚdefaultÚ
expressionzflash-delayÚtypeÚintiô  ZhiliteÚbellÚboolé   z<<parenmatch-check-restore>>ú
<KeyPress>ú<ButtonPress>ú<Key-Return>ú<Key-BackSpace>c             C   sT   | |  _  | j |  _ | j j |  j |  j ƒ d |  _ d |  _ |  j |  j ƒ d  S)Nr   )	ÚeditwinÚtextZbindÚRESTORE_VIRTUAL_EVENT_NAMEÚrestore_eventÚcounterÚis_restore_activeÚ	set_styleÚSTYLE)Úselfr   © r$   ú(/usr/lib/python3.4/idlelib/ParenMatch.pyÚ__init__;   s    	
		zParenMatch.__init__c             C   sC   |  j  s? x' |  j D] } |  j j |  j | ƒ q Wd |  _  n  d  S)NT)r    ÚRESTORE_SEQUENCESr   Z	event_addr   )r#   Úseqr$   r$   r%   Úactivate_restoreG   s    	zParenMatch.activate_restorec             C   sC   |  j  r? x' |  j D] } |  j j |  j | ƒ q Wd |  _  n  d  S)NF)r    r'   r   Zevent_deleter   )r#   r(   r$   r$   r%   Údeactivate_restoreM   s    	zParenMatch.deactivate_restorec             C   s[   | |  _  | d k r0 |  j |  _ |  j |  _ n' | d k rW |  j |  _ |  j |  _ n  d  S)Nr   r   )r"   Úcreate_tag_defaultÚ
create_tagÚset_timeout_lastÚset_timeoutÚcreate_tag_expressionÚset_timeout_none)r#   r   r$   r$   r%   r!   S   s    	zParenMatch.set_stylec             C   sW   t  |  j d ƒ j ƒ  } | d  k r2 |  j ƒ  d  S|  j ƒ  |  j | ƒ |  j ƒ  d  S)NÚinsert)r   r   Úget_surrounding_bracketsÚwarn_mismatchedr)   r,   r-   )r#   ÚeventÚindicesr$   r$   r%   Úflash_paren_event\   s    

zParenMatch.flash_paren_eventc             C   s™   |  j  j d ƒ } | t k r" d  St |  j d ƒ } | j ƒ  sD d  S| j t | d ƒ } | d  k rt |  j ƒ  d  S|  j ƒ  |  j	 | ƒ |  j
 ƒ  d  S)Nz	insert-1cT)r   ÚgetÚ_openersr   r   Z
is_in_coder2   r3   r)   r,   r.   )r#   r4   ZcloserZhpr5   r$   r$   r%   Úparen_closed_eventf   s    

zParenMatch.paren_closed_eventNc             C   s-   |  j  j d ƒ |  j ƒ  |  j d 7_ d  S)NÚparenr   )r   Z
tag_deleter*   r   )r#   r4   r$   r$   r%   r   v   s    
zParenMatch.restore_eventc             C   s    | |  j  k r |  j ƒ  n  d  S)N)r   r   )r#   Ztimer_countr$   r$   r%   Úhandle_restore_timer{   s    zParenMatch.handle_restore_timerc             C   s   |  j  r |  j j ƒ  n  d  S)N)ÚBELLr   r   )r#   r$   r$   r%   r3      s    	zParenMatch.warn_mismatchedc             C   s1   |  j  j d | d ƒ |  j  j d |  j ƒ d S)z'Highlight the single paren that matchesr:   r   N)r   Útag_addÚ
tag_configÚHILITE_CONFIG)r#   r5   r$   r$   r%   r+   †   s    zParenMatch.create_tag_defaultc             C   sk   |  j  j | d ƒ d	 k r- | d d } n
 | d } |  j  j d | d | ƒ |  j  j d |  j ƒ d S)
zHighlight the entire expressionr   r   r   r	   z+1cr:   r   N)r   r   r	   )r   r7   r=   r>   r?   )r#   r5   Z
rightindexr$   r$   r%   r/   ‹   s
    
z ParenMatch.create_tag_expressionc             C   sP   |  j  d 7_  |  |  j  |  j j d ƒ d d „ } |  j j j t | | ƒ d S)zSHighlight will remain until user input turns it off
        or the insert has movedr   r1   c             S   sE   | | j  j d ƒ k r( | j | ƒ n | j j j t |  |  ƒ d  S)Nr1   )r   Úindexr;   r   Ú
text_frameÚafterÚCHECK_DELAY)Úcallmer#   Úcr@   r$   r$   r%   rD   ž   s    z+ParenMatch.set_timeout_none.<locals>.callmeN)r   r   r@   r   rA   rB   rC   )r#   rD   r$   r$   r%   r0   —   s    	zParenMatch.set_timeout_nonec             C   s;   |  j  d 7_  |  j j j |  j |  |  j  d d „ ƒ d S)z7The last highlight created will be removed after .5 secr   c             S   s   |  j  | ƒ S)N)r;   )r#   rE   r$   r$   r%   Ú<lambda>­   s    z-ParenMatch.set_timeout_last.<locals>.<lambda>N)r   r   rA   rB   ÚFLASH_DELAY)r#   r$   r$   r%   r-   ¦   s    zParenMatch.set_timeout_last)r   r   )r   r   r   r   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__Zmenudefsr   Z	GetOptionr"   rG   ZGetHighlightZCurrentThemer?   r<   r   r'   r&   r)   r*   r!   r6   r9   r   r;   r3   r+   r/   r0   r-   r$   r$   r$   r%   r      s4   	 	
r   Ú__main__Nz!idlelib.idle_test.test_parenmatchÚ	verbosityé   )rK   Zidlelib.HyperParserr   Zidlelib.configHandlerr   r8   rC   r   rH   ZunittestÚmainr$   r$   r$   r%   Ú<module>   s   ¢