a  ze&@sdZddlZddlmZmZddlmZddlmZddZ Gdd d eZ d d Z e d krdd l mZeddddddlmZee dS)zReplace 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) StringVarTclError)SearchDialogBase) searchenginecCs<|}t|}t|ds(t|||_|j}||dS)zCreate 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)_rootrgethasattr ReplaceDialogropen)textrootengineZdialogrC/opt/bitninja-python-dojo/embedded/lib/python3.9/idlelib/replace.pyreplaces    rcseZdZdZdZdZfddZddZdd Zd d Z d d dZ d!ddZ d"ddZ ddZ d#ddZd$ddZddZddZd%ddZZS)&r z3Dialog for finding and replacing a pattern in text.zReplace DialogReplacecst||t||_dS)aCreate 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__rreplvar)selfr r __class__rrr%s zReplaceDialog.__init__cCst||z|d}Wnty0d}Yn0z|d}WntyVd}Yn0|pd|d}|pl|}|||d|_dS)zMake 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.firstNsel.lastinsertT)rr indexrshow_hitok)rr firstlastrrrr 5s      zReplaceDialog.opencCs"t||d|jd|_dS)z8Create base and additional label and text entry widgets.z Replace with:rN)rcreate_entriesZ make_entryrZreplentrrrrr!Ls zReplaceDialog.create_entriescCsJt||d|j|d|j|jd|jdd|d|jdS)zCreate base and additional command buttons. The additional buttons are for Find, Replace, Replace+Find, and Replace All. ZFindrz Replace+FindT)Zisdefz Replace AllN)rcreate_command_buttonsZ make_buttonfind_it replace_itdefault_command replace_allr"rrrr#Qs  z$ReplaceDialog.create_command_buttonsNcCs|ddS)zHandle the Find button.FN)do_findreventrrrr$]szReplaceDialog.find_itcCs||jr|dS)z]Handle the Replace button. If the find is successful, then perform replace. Nr(r do_replacer)rrrr%as zReplaceDialog.replace_itcCs"||jr|r|ddS)zHandle 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&is zReplaceDialog.default_commandcCsJ|jrBz||}WqFtjy>|j|dd}YqF0n|}|S)z.Expand replacement text if regular expression.zInvalid Replace ExpressionN)rZisreexpandreerrorZ report_error)rmreplnewrrr_replace_expandts  zReplaceDialog._replace_expandcCs|j}|sdS|j}|j}|j||}|s@|dS|ddd|ddd|d}|d}|j rd}d}d}d} } | |jj ||||d |d }|sqx|\}} |d |d |d} | } | | |}|durqx| \}}d ||f} d ||f} || kr2|d | n4|d | | | krT|| | |rf|| ||t|}d }q|| r| r|| | |dS)aHandle 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. Nsel1.0endhitrTF)wrapr%d.0%d.%dr)rgetprogrrr search_textbell tag_removestartZiswrapundo_block_startZsearch_forwardgroupr3spanmark_setdeleterlenundo_block_stoprclose)rr*progr1r reslinecolrrr r0charsZorigr2ijrrrr's\                zReplaceDialog.replace_allFc Csv|jsdS|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. FNr;T)rr<r r=r>rCrr) rrr rJrKr0rNrOrr rrrr(s     zReplaceDialog.do_findc Cs|j}|sdS|j}z|d}}|d}WntyJd}Yn0|sb|d}}}t|\}}|d|d|d}|||} |sdS| | |j } | durdS| d|| | r|||| r||| ||||dd|_dS) z6Replace search pattern in text with replacement value.FrrNrr:r8T)rr<r rrrZ get_line_colrmatchr3rrDrArBrErrGrr) rrIr rposr rKrLrMr0r2rrrr,s:       zReplaceDialog.do_replacecCsv|j}|d||ddd|d|||ddd||krR|d|n|d|||d|dS)aHighlight 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 rDr?tag_addZseeZupdate_idletasks)rrr r rrrrs   zReplaceDialog.show_hitcCs t|||jddddS)z%Close the dialog and remove hit tags.r7r5r6N)rrHr r?r)rrrrHs zReplaceDialog.close)N)N)N)N)F)N)__name__ __module__ __qualname____doc__titleZiconrr r!r#r$r%r&r3r'r(r,rrH __classcell__rrrrr s      ;  r c sddlm}m}mmddlm}m}||}|dt t | ddd\}}| d||dfd d }d d } ||} | || d d|_| _ ddfdd} || d| d} | dS)Nr)ToplevelTextENDSEL)FrameButtonzTest ReplaceDialog+r8z+%d+%dcSsdSNrrrrrrAsz)_replace_dialog..undo_block_startcSsdSrarrrrrrGsz(_replace_dialog..undo_block_stopZgray)Zinactiveselectbackgroundrz"This is a sample sTring Plus MORE.cs(dtddS)Nr5)rRrr?rr[r\r rr show_replace&sz%_replace_dialog..show_replacer)r Zcommand)tkinterrYrZr[r\Z tkinter.ttkr]r^rWmapintZgeometrysplitpackrArGrZ focus_set) parentrYrZr]r^topxyrArGframercZbuttonrrbr_replace_dialog s&    rn__main__)mainzidlelib.idle_test.test_replaceF) verbosityexit)run)rVr.rdrrZidlelib.searchbaserZidlelibrrr rnrSZunittestrpZidlelib.idle_test.htestrtrrrrs  o!