\ddZddlZddlmZmZmZddlmZdZGddZdZ d Z d Z e d krdd l mZed ddSdS)z'Define SearchEngine for search dialogs.N) StringVar BooleanVarTclError) messageboxcXt|dst||_|jS)zReturn the singleton SearchEngine instance for the process. The single SearchEngine saves settings between dialog instances. If there is not a SearchEngine already, make one. _searchengine)hasattr SearchEnginer)roots %..\python\lib\idlelib\searchengine.pygetr s/ 4 ) )0)$//  cteZdZdZdZdZdZdZdZdZ dZ d Z d Z d Z d ZddZddZddZddZd S)r z#tj$r,}|||j|j Yd}~dSd}~wwxYw|S)z&Return compiled cooked search pattern.zEmpty regular expressionNr) r report_errorr3r&r/ IGNORECASEcompileerrormsgpos)rr!flagsproges r getprogzSearchEngine.getprogKskkmm    c#= > > >4!!{{}} *BM)E :c5))DDx      c15!% 0 0 044444  s)A??B:!B55B:Ncdt|z}|r|dzt|z}||dzt|z}tjd||jdS)NzError: z Pattern: z Offset: zRegular expression error)master)strr showerrorr )rr!r9cols r r5zSearchEngine.report_error\sw#c(("  1 %C0C ? $s3xx/C7"49 6 6 6 6 6 6rrc|s|}|sdS|j}t|\}}|r4|r|}n|}t |\}} ||||| ||} n3|r|}n|}t |\}} ||||| ||} | S)aReturn (lineno, matchobj) or None for forward/backward search. This function calls the right function with the right arguments. It directly return the result of that call. Text is a text widget. Prog is a precompiled pattern. The ok parameter is a bit complicated as it has two effects. If there is a selection, the search begin at either end, depending on the direction setting and ok, with ok meaning that the search starts with the selection. Otherwise, search begins at the insert mark. To aid progress, the search functions do not return an empty match at the starting position unless ok is True. N)r>rr get_selectionr- get_line_colsearch_backwardsearch_forward) rtextr<okwrapfirstlaststartlinerCress r search_textzSearchEngine.search_textfs$ <<>>D t|!!#D)) t ;;== G $U++ID#&&tT4dBGGCC $U++ID#%%dD$T2FFC rcrd}|}|d|zd|dzz} | r|| dd|} | r|s| |kr|| fS|dz}|r||krnEd}d}|d|zd|dzz} | s|rd}d}d}|dd} | dS)Nr%d.0z1.0z2.0)r searchend) rrIr<rOrCrKrJwrapped startlinecharsms r rHzSearchEngine.search_forwards $$q&(9:: / E#2#J,,A ##37N!8D 4)++CBHHVd]Fd1f,=>>E /T /.. / trcd}|}|d|zd|dzz} t|| dd|} | r|s| |kr|| fS|dz }|r||krnd}|dkrG|snvd}d}|d} t t | d\}}|d|zd|dzz} t| dz }dS)NrrSrTTrUzend-1c.)r search_reverserNindexmapintsplitlen) rrIr<rOrCrKrJrXrYrZr[r:s r rGzSearchEngine.search_backwards $$q&(9:: !tU3B3Z55A ##S7N!8D 4)++Bqyyjj**SYYs^^44 cHHVd]Fd1f,=>>Ee**q.C# !$trr)Nr)r)__name__ __module__ __qualname____doc__rrr"r$r&r)r+r-r1r3r>r5rQrHrGrr r r sFF / / /!!!   """""""""""" "6666&&&&P,rr c||}|sdSd}|\}}||krO||krI|}||kr|dz}|||}|sn#|\}}||kr||kI|S)a&Search backwards and return an re match object or None. This is done by searching forwards until there is no match. Prog: compiled re object with a search method returning a match. Chars: line of text, without \n. Col: stop index for the search; the limit for match.end(). NrT)rVspan)r<rZrCr[foundijs r r^r^s EA t E 6688DAq c''a3hh 66!A KKq ! !  vvxx1 c''a3hh Lrc |d}|d}n#t$rdx}}YnwxYw|s|d}|s|}||fS)zFReturn tuple of 'line.col' indexes from selection or insert mark. z sel.firstzsel.lastNinsert)r_r)rIrLrMs r rErEs ;''zz*%%  % 8$$  $;s *->>c`tt|d\}}||fS)z8Return (line, col) tuple of ints from 'line.col' string.r])r`rarb)r_rOrCs r rFrFs+CS))**ID# 9r__main__)mainz#idlelib.idle_test.test_searchengine) verbosity)rgr/tkinterrrrrr r r^rErFrdunittestrrrhrr rws-- 3333333333   ggggggggT.     zD .!<<<<<<r