î e f9ã@sŒdZddlmZddlmZidd6dd6dd 6Zd ZGd d „d ƒZed krˆddl Z e j dddƒndS)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ú(ú)ú[ú]ú{ú}édc@sHeZdZdZdd1gfgZejdddddƒZejddd d d dd ƒZej ej ƒd ƒZ ejdddd dddƒZ dZ d2Zdd„Zdd„Zdd„Zdd„Zdd„Zd d!„Zd"d#d$„Zd%d&„Zd'd(„Zd)d*„Zd+d,„Zd-d.„Zd/d0„Zd"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. ZeditúShow surrounding parensú<>Ú extensionsÚstyleÚdefaultÚ expressionz flash-delayÚtypeÚintiôZhiliteÚbellÚbooléz<ú ú úcCsT||_|j|_|jj|j|jƒd|_d|_|j|jƒdS)Nr) ÚeditwinÚtextZbindÚRESTORE_VIRTUAL_EVENT_NAMEÚ restore_eventÚcounterÚis_restore_activeÚ set_styleÚSTYLE)Úselfr©r$ú7/opt/alt/python34/lib64/python3.4/idlelib/ParenMatch.pyÚ__init__;s     zParenMatch.__init__cCsC|js?x'|jD]}|jj|j|ƒqWd|_ndS)NT)r ÚRESTORE_SEQUENCESrZ event_addr)r#Úseqr$r$r%Úactivate_restoreGs zParenMatch.activate_restorecCsC|jr?x'|jD]}|jj|j|ƒqWd|_ndS)NF)r r'rZ event_deleter)r#r(r$r$r%Údeactivate_restoreMs zParenMatch.deactivate_restorecCs[||_|dkr0|j|_|j|_n'|dkrW|j|_|j|_ndS)Nrr)r"Úcreate_tag_defaultÚ create_tagÚset_timeout_lastÚ set_timeoutÚcreate_tag_expressionÚset_timeout_none)r#rr$r$r%r!Ss     zParenMatch.set_stylecCsWt|jdƒjƒ}|dkr2|jƒdS|jƒ|j|ƒ|jƒdS)NÚinsert)rrÚget_surrounding_bracketsÚwarn_mismatchedr)r,r-)r#ÚeventÚindicesr$r$r%Úflash_paren_event\s    zParenMatch.flash_paren_eventcCs™|jjdƒ}|tkr"dSt|jdƒ}|jƒsDdS|jt|dƒ}|dkrt|jƒdS|jƒ|j |ƒ|j ƒdS)Nz insert-1cT) rÚgetÚ_openersrrZ is_in_coder2r3r)r,r.)r#r4ZcloserZhpr5r$r$r%Úparen_closed_eventfs      zParenMatch.paren_closed_eventNcCs-|jjdƒ|jƒ|jd7_dS)NÚparenr)rZ tag_deleter*r)r#r4r$r$r%rvs zParenMatch.restore_eventcCs ||jkr|jƒndS)N)rr)r#Z timer_countr$r$r%Úhandle_restore_timer{szParenMatch.handle_restore_timercCs|jr|jjƒndS)N)ÚBELLrr)r#r$r$r%r3s zParenMatch.warn_mismatchedcCs1|jjd|dƒ|jjd|jƒdS)z'Highlight the single paren that matchesr:rN)rÚtag_addÚ tag_configÚ HILITE_CONFIG)r#r5r$r$r%r+†szParenMatch.create_tag_defaultcCsk|jj|dƒd kr-|dd}n |d}|jjd|d|ƒ|jjd|jƒdS) zHighlight the entire expressionrrrr z+1cr:rN)rrr )rr7r=r>r?)r#r5Z rightindexr$r$r%r/‹s  z ParenMatch.create_tag_expressioncCsP|jd7_||j|jjdƒdd„}|jjjt||ƒdS)zSHighlight will remain until user input turns it off or the insert has movedrr1cSsE||jjdƒkr(|j|ƒn|jjjt||ƒdS)Nr1)rÚindexr;rÚ text_frameÚafterÚ CHECK_DELAY)Úcallmer#Úcr@r$r$r%rDžsz+ParenMatch.set_timeout_none..callmeN)rrr@rrArBrC)r#rDr$r$r%r0—s zParenMatch.set_timeout_nonecCs;|jd7_|jjj|j||jdd„ƒdS)z7The last highlight created will be removed after .5 secrcSs |j|ƒS)N)r;)r#rEr$r$r%Ú­sz-ParenMatch.set_timeout_last..N)rrrArBÚ FLASH_DELAY)r#r$r$r%r-¦s zParenMatch.set_timeout_last)r r )rrrr)Ú__name__Ú __module__Ú __qualname__Ú__doc__ZmenudefsrZ GetOptionr"rGZ GetHighlightZ CurrentThemer?r<rr'r&r)r*r!r6r9rr;r3r+r/r0r-r$r$r$r%r s4         r Ú__main__Nz!idlelib.idle_test.test_parenmatchÚ verbosityé) rKZidlelib.HyperParserrZidlelib.configHandlerrr8rCr rHZunittestÚmainr$r$r$r%Ús¢