B €/g^¡&c@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ÚgetZhasattrÚ 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)&rz3Dialog 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)ZsuperÚ__init__rÚreplvar)Úselfr r ©Z __class__r r r%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. ú sel.firstNúsel.lastÚinsertT)rrÚindexrÚshow_hitÚok)rr ÚfirstÚlastr r r r5s    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©rr r r rLs 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_allrr r r rQs  z$ReplaceDialog.create_command_buttonsNcCs| d¡dS)zHandle the Find button.FN)Údo_find©rÚeventr r r r]szReplaceDialog.find_itcCs| |j¡r| ¡dS)z]Handle the Replace button. If the find is successful, then perform replace. N©r#rÚ do_replacer$r r r r 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. FNr&r$r r r r!is zReplaceDialog.default_commandcCsL|j ¡rDy| |¡}WqHtjk r@|j |d¡d}YqHXn|}|S)z.Expand replacement text if regular expression.zInvalid Replace ExpressionN)r ZisreZexpandÚreZerrorZ report_error)rÚmÚreplÚnewr r r Ú_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Úselú1.0ÚendÚhitréTF)Zwraprú%d.0ú%d.%dr)r Úgetprogrrr Ú search_textÚbellÚ tag_removeZstartZiswrapÚundo_block_startZsearch_forwardÚgroupr,ÚspanÚmark_setÚdeleterZlenÚundo_block_stoprÚclose)rr%Úprogr*r ÚresÚlineÚcolrrrr)ÚcharsZorigr+ÚiÚjr r r r"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. FNr3T)r r4r r5r6r:rr) rrr r@rAr)rDrErrr r r r#¼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.FrrNrr2r1T)r r4r rrrZ get_line_colrZmatchr,rr;r8r9r<rr=rr) rr?r rZposrrArBrCr)r+r r r r'Ð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-r.r/r0N)r r;r7Útag_addZseeZupdate_idletasks)rrrr r r r rðs   zReplaceDialog.show_hitcCs t ||¡|j ddd¡dS)z%Close the dialog and remove hit tags.r0r.r/N)rr>r r7r$r r r r>s zReplaceDialog.close©NrGrGrG)FrG)Ú__name__Z __module__Z __qualname__Ú__doc__ÚtitleZiconrrrrrr r!r,r"r#r'rr>Z __classcell__r r rr rs      ;  rc 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 ReplaceDialogz+r1z+%d+%di¯cSódSrGr r r r r r8óz)_replace_dialog..undo_block_startcSrQrGr r r r r r=rRz(_replace_dialog..undo_block_stopZgray)Zinactiveselectbackgroundrz"This is a sample sTring Plus MORE.cs(ˆ ˆdˆ¡tˆƒˆ ˆdˆ¡dS)Nr.)rFrr7r ©rMrNr r r Ú show_replace&sz%_replace_dialog..show_replacer)r Zcommand)ÚtkinterrKrLrMrNZ tkinter.ttkrOrPrJZmapZintZgeometryZsplitZpackr8r=rZ focus_set) ZparentrKrLrOrPZtopZxZyr8r=ZframerTZbuttonr rSr Ú_replace_dialog s&    rVZ__main__)Úmainzidlelib.idle_test.test_replaceiF)Z verbosityZexit)Úrun)rIr(rUrrZidlelib.searchbaserZidlelibrrrrVrHZunittestrWZidlelib.idle_test.htestrXr r r r Zs  o!