o ­=?h'ã@s”dZddlZddlmZmZddlmZddlmZddd„Z Gdd „d eƒZ d d „Z e d krHdd l mZeddddddlmZee ƒdSdS)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ƒst||ƒ|_|j}|j||ddS)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. Ú_replacedialog)Ú insert_tagsN)Z_rootrÚgetÚhasattrÚ ReplaceDialogrÚopen)ÚtextrÚrootÚengineZdialog©rú6/opt/alt/python310/lib64/python3.10/idlelib/replace.pyÚreplaces    rcs–eZdZdZdZdZ‡fdd„Zd dd„Zd d „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ÚReplacecs"tƒ ||¡t|ƒ|_d|_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Úreplvarr)Úselfr r©Ú __class__rrr%s  zReplaceDialog.__init__NcCsŠt ||¡z| d¡}Wn tyd}Ynwz| d¡}Wn ty+d}Ynw|p2| d¡}|p6|}| ||¡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Úokr)rr rÚfirstÚlastrrrr 6s  ÿ ÿ  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©rrrrr!Ns 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_allr"rrrr#Ss z$ReplaceDialog.create_command_buttonscCs| d¡dS)zHandle the Find button.FN)Údo_find©rÚeventrrrr$_szReplaceDialog.find_itcCs| |j¡r | ¡dSdS)z]Handle the Replace button. If the find is successful, then perform replace. N©r(rÚ do_replacer)rrrr%cs  ÿzReplaceDialog.replace_itcCs*| |j¡r| ¡r| d¡dSdSdS)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)rrrr&ks ýzReplaceDialog.default_commandcCsL|j ¡r"z| |¡}W|Stjy!|j |d¡d}Y|Sw|}|S)z.Expand replacement text if regular expression.zInvalid Replace ExpressionN)rZisreÚexpandÚreÚerrorZ report_error)rÚmÚreplÚnewrrrÚ_replace_expandvs  úúzReplaceDialog._replace_expandcCs²|j ¡}|s dS|j ¡}|j}|j ||¡}|s | ¡dS| ddd¡| ddd¡|d}|d ¡}|j  ¡rAd}d}d}d} } |  ¡|jj ||||d |d }rÅ|\}} | d |d |d¡} |   ¡} |  | |¡}|durxnM|  ¡\}}d ||f} d ||f} || kr•| d | ¡n| d | ¡| | kr¥| | | ¡|r¯| | ||j¡|t|ƒ}d }|jj ||||d |d }sY| ¡| 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_removeÚstartZiswrapÚundo_block_startZsearch_forwardÚgroupr3ÚspanÚmark_setÚdeleterrÚlenÚundo_block_stoprÚclose)rr*Úprogr1r ÚresÚlineÚcolrrr r0ÚcharsZorigr2ÚiÚjrrrr'ƒs^      ÿ       ì  ÿ  zReplaceDialog.replace_allFc Csv|j ¡sdS|j}|j |d|¡}|s| ¡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. FNr:T)rr;r r<r=rBrr) rrr rIrJr0rMrNrr rrrr(»s     zReplaceDialog.do_findc Cs|j ¡}|s dS|j}z| d¡}}| d¡}Wn ty%d}Ynw|s1| d¡}}}t |¡\}}| d|d|d¡}| ||¡} |sNdS|  | |j  ¡¡} | dur]dS|  d|¡|  ¡|   ¡rq| ||¡| r{| || |j¡| ¡| || d¡¡d|_dS) z6Replace search pattern in text with replacement value.FrrNrr9r8T)rr;r rrrZ get_line_colrÚmatchr3rrCr@rArDrrrFrr) rrHr rÚposr rJrKrLr0r2rrrr,Ïs<  ÿ   zReplaceDialog.do_replacecCsv|j}| d|¡| ddd¡| d||¡| ddd¡||kr)| 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'. rr4r5r6r7N)r rCr>Útag_addZseeZupdate_idletasks)rrr r rrrrïs   zReplaceDialog.show_hitcCs&t ||¡|j ddd¡d|_dS)z%Close the dialog and remove hit tags.r7r5r6N)rrGr r>rr)rrrrGs  zReplaceDialog.close©N)F)Ú__name__Ú __module__Ú __qualname__Ú__doc__ÚtitleZiconrr r!r#r$r%r&r3r'r(r,rrGÚ __classcell__rrrrr s"     8 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ú+r8z+%d+%dé¯cSódSrRrrrrrr@óz)_replace_dialog..undo_block_startcSrarRrrrrrrFrbz(_replace_dialog..undo_block_stopZgray)Zinactiveselectbackgroundrz"This is a sample sTring Plus MORE.cs(ˆ ˆdˆ¡tˆƒˆ ˆdˆ¡dS)Nr5)rQrr>r©r[r\r rrÚ show_replace&sz%_replace_dialog..show_replacer)r Zcommand)ÚtkinterrYrZr[r\Z tkinter.ttkr]r^rWÚmapÚintZgeometryÚsplitZpackr@rFrZ focus_set) ÚparentrYrZr]r^ÚtopÚxÚyr@rFÚframerdZbuttonrrcrÚ_replace_dialog s&     rnÚ__main__)Úmainzidlelib.idle_test.test_replaceéF)Ú verbosityÚexit)ÚrunrR)rVr.rerrZidlelib.searchbaserZidlelibrrr rnrSZunittestrpZidlelib.idle_test.htestrtrrrrÚs   o!   û