B €/g^¡&ã@sŽdZddlZddlmZmZddlmZddlmZdd„Z Gdd „d eƒZ d d „Z e d krŠdd l mZeddddddlmZee ƒdS)zÑReplace dialog for IDLE. Inherits SearchDialogBase for GUI. Uses idlelib.searchengine.SearchEngine for search capability. Defines various replace related functions like replace, replace all, and replace+find. éN)Ú StringVarÚTclError)ÚSearchDialogBase)Ú searchenginecCs<| ¡}t |¡}t|dƒs(t||ƒ|_|j}| |¡dS)zÔCreate or reuse a singleton ReplaceDialog instance. The singleton dialog saves user entries and preferences across instances. Args: text: Text widget containing the text to be searched. Ú_replacedialogN)Z_rootrÚgetÚhasattrÚ ReplaceDialogrÚopen)ÚtextÚrootÚengineZdialog©rú'/usr/lib64/python3.7/idlelib/replace.pyÚreplaces    rcs”eZdZdZdZdZ‡fdd„Zdd„Zdd „Zd d „Z d d d„Z d!dd„Z d"dd„Z dd„Z d#dd„Zd$dd„Zdd„Zdd„Zd%dd„Z‡ZS)&r z3Dialog for finding and replacing a pattern in text.zReplace DialogÚReplacecstƒ ||¡t|ƒ|_dS)aãCreate search dialog for finding and replacing text. Uses SearchDialogBase as the basis for the GUI and a searchengine instance to prepare the search. Attributes: replvar: StringVar containing 'Replace with:' value. replent: Entry widget for replvar. Created in create_entries(). ok: Boolean used in searchengine.search_text to indicate whether the search includes the selection. N)ÚsuperÚ__init__rÚreplvar)Úselfr r )Ú __class__rrr%s zReplaceDialog.__init__cCsˆt ||¡y| d¡}Wntk r2d}YnXy| d¡}Wntk rZd}YnX|ph| d¡}|pp|}| ||¡d|_dS)zøMake dialog visible on top of others and ready to use. Also, highlight the currently selected text and set the search to include the current selection (self.ok). Args: text: Text widget being searched. z sel.firstNzsel.lastÚinsertT)rr ÚindexrÚshow_hitÚok)rr ÚfirstÚlastrrrr 5s    zReplaceDialog.opencCs"t |¡| d|j¡d|_dS)z8Create base and additional label and text entry widgets.z Replace with:rN)rÚcreate_entriesZ make_entryrZreplent)rrrrrLs zReplaceDialog.create_entriescCsJt |¡| d|j¡| d|j¡|jd|jdd| d|j¡dS)z’Create base and additional command buttons. The additional buttons are for Find, Replace, Replace+Find, and Replace All. ZFindrz Replace+FindT)Zisdefz Replace AllN)rÚcreate_command_buttonsZ make_buttonÚfind_itÚ replace_itÚdefault_commandÚ replace_all)rrrrrQs  z$ReplaceDialog.create_command_buttonsNcCs| d¡dS)zHandle the Find button.FN)Údo_find)rÚeventrrrr]szReplaceDialog.find_itcCs| |j¡r| ¡dS)z]Handle the Replace button. If the find is successful, then perform replace. N)r#rÚ do_replace)rr$rrrr as zReplaceDialog.replace_itcCs"| |j¡r| ¡r| d¡dS)zŸHandle the Replace+Find button as the default command. First performs a replace and then, if the replace was successful, a find next. FN)r#rr%)rr$rrrr!is zReplaceDialog.default_commandcCsL|j ¡rDy| |¡}WqHtjk r@|j |d¡d}YqHXn|}|S)z.Expand replacement text if regular expression.zInvalid Replace ExpressionN)r ZisreÚexpandÚreÚerrorZ report_error)rÚmÚreplÚnewrrrÚ_replace_expandts  zReplaceDialog._replace_expandcCs¤|j ¡}|sdS|j ¡}|j}|j ||¡}|s@| ¡dS| ddd¡| ddd¡|d}|d ¡}|j  ¡r‚d}d}d}d} } |  ¡xà|jj ||||d |d }|s¶P|\}} | d |d |d¡} |   ¡} |  | |¡}|dkrôP|  ¡\}}d ||f} d ||f} || kr0| d | ¡n4| d | ¡| | krR| | | ¡|rd| | |¡|t|ƒ}d }q˜W| ¡| r˜| r˜| | | ¡| ¡dS)a¸Handle the Replace All button. Search text for occurrences of the Find value and replace each of them. The 'wrap around' value controls the start point for searching. If wrap isn't set, then the searching starts at the first occurrence after the current selection; if wrap is set, the replacement starts at the first line. The replacement is always done top-to-bottom in the text. NÚselz1.0ÚendÚhitréTF)Zwraprz%d.0z%d.%dr)r Úgetprogrrr Ú search_textÚbellÚ tag_removeÚstartZiswrapÚundo_block_startZsearch_forwardÚgroupr,ÚspanÚmark_setÚdeleterÚlenÚundo_block_stoprÚclose)rr$Úprogr*r ÚresÚlineÚcolrrrr)ÚcharsZorigr+ÚiÚjrrrr"s\                 zReplaceDialog.replace_allFc Csv|j ¡sdS|j}|j |d|¡}|s4| ¡dS|\}}| ¡\}}d||f}d||f} | || ¡d|_dS)z|Search for and highlight next occurrence of pattern in text. No text replacement is done with this option. FNz%d.%dT)r r1r r2r3r8rr) rrr r?r@r)rCrDrrrrrr#¼s     zReplaceDialog.do_findc Cs|j ¡}|sdS|j}y| d¡}}| d¡}Wntk rLd}YnX|sd| d¡}}}t |¡\}}| d|d|d¡}| ||¡} |sždS|  | |j  ¡¡} | dkr¼dS|  d|¡|  ¡|   ¡rä| ||¡| rô| || ¡| ¡| || d¡¡d|_dS) z6Replace search pattern in text with replacement value.Fz sel.firstzsel.lastNrz%d.0r0T)r r1r rrrZ get_line_colrÚmatchr,rr9r6r7r:rr<rr) rr>r rÚposrr@rArBr)r+rrrr%Ðs:      zReplaceDialog.do_replacecCsv|j}| d|¡| ddd¡| d||¡| ddd¡||krR| d|¡n| d||¡| d¡| ¡dS)aŠHighlight text between first and last indices. Text is highlighted via the 'hit' tag and the marked section is brought into view. The colors from the 'hit' tag aren't currently shown when the text is displayed. This is due to the 'sel' tag being added first, so the colors in the 'sel' config are seen instead of the colors for 'hit'. rr-z1.0r.r/N)r r9r4Útag_addZseeZupdate_idletasks)rrrr rrrrðs   zReplaceDialog.show_hitcCs t ||¡|j ddd¡dS)z%Close the dialog and remove hit tags.r/z1.0r.N)rr=r r4)rr$rrrr=s zReplaceDialog.close)N)N)N)N)F)N)Ú__name__Ú __module__Ú __qualname__Ú__doc__ÚtitleZiconrr rrrr r!r,r"r#r%rr=Ú __classcell__rr)rrr s      ;  r c sîddlm}m}m‰m‰ddlm}m}||ƒ}| d¡t t |  ¡  d¡dd…ƒ\}}|  d||df¡d d „}d d „} ||ƒ} |   ¡|| d d‰|ˆ_| ˆ_ˆ  ¡ˆ dd¡ˆ ¡‡‡‡fdd„} || d| d} |   ¡dS)Nr)ÚToplevelÚTextÚENDÚSEL)ÚFrameÚButtonzTest ReplaceDialogú+r0z+%d+%dé¯cSsdS)Nrrrrrr6sz)_replace_dialog..undo_block_startcSsdS)Nrrrrrr<sz(_replace_dialog..undo_block_stopZgray)Zinactiveselectbackgroundrz"This is a sample sTring Plus MORE.cs(ˆ ˆdˆ¡tˆƒˆ ˆdˆ¡dS)Nz1.0)rGrr4r)rPrQr rrÚ show_replace&sz%_replace_dialog..show_replacer)r Zcommand)ÚtkinterrNrOrPrQZ tkinter.ttkrRrSrLÚmapÚintZgeometryÚsplitZpackr6r<rZ focus_set) ÚparentrNrOrRrSÚtopÚxÚyr6r<ÚframerVZbuttonr)rPrQr rÚ_replace_dialog s&    r`Ú__main__)Úmainzidlelib.idle_test.test_replaceéF)Ú verbosityÚexit)Úrun)rKr'rWrrZidlelib.searchbaserZidlelibrrr r`rHZunittestrbZidlelib.idle_test.htestrfrrrrÚs  o!