o
    ǋ|cQ                    @   s  d dl Zd dlZd dlZd dlZd dlZd dlZd dlZd dlZd dl	Z	d dl
Z
d dlT d dlmZ d dlmZ d dlmZ d dlmZ d dlmZ d dlmZ d d	lmZ d d
lmZ d dlmZ d dlmZ d dlmZ d dlmZ d dlmZ d dlmZ d dlmZ d dl m!Z! d dl"m#Z# d dlm$Z$ dZ%de&  Z'ejdkZ(dd Z)G dd dZ*dd Z+e,dZ-dd  Z.G d!d" d"Z/d#d$ Z0d%d&d'd(Z1d)d* Z2d+d, Z3d-d. Z4e5d/krd d0l6m7Z7 e7d1d2d3d4 d d5l8m9Z9 e9e4 dS dS )6    N)*)Font)	Scrollbar)simpledialog)
messagebox)idleConf)configdialog)grep)help)
help_about)macosx)MultiCallCreator)pyparse)query)replace)search)wheel_event)py_extensions)window   z (%s)darwinc                  C   sd   t j\} }}}}d| |f }|d|f 7 }|dkr"|d|f 7 }|S |dkr0|d|d |f 7 }|S )zYFormat sys.version_info to produce the Sphinx version string used to install the chm docsz%s%sz%s	candidatezrc%sfinalr   )sysversion_info)majorminormicrolevelserialrelease r!      /home/conda/feedstock_root/build_artifacts/python-split_1669104742391/_h_env_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehol/lib/python3.10/idlelib/editor.py_sphinx_version&   s   r#   c                   @   s4  e Zd ZddlmZ ddlmZmZ ddlm	Z	 ddl
mZmZ ddlmZ ddlmZ ddlmZ dd	lmZ dd
lmZ ddlmZ ddlmZ ddlmZmZmZmZ ddl m!Z! ddl"m#Z# e$% Z&dZ'dZ(dZ)dZ*dddZ+dddZ,dd Z-dd Z.dd Z/dd Z0dddZ1g d Z2d!d" Z3d#d$ Z4d%d& Z5d'd( Z6d)d* Z7dZ8d+d, Z9d-gZ:d.d/ Z;d0d1 Z<d2d3 Z=d4d5 Z>dd6d7Z?dd8d9Z@dd:d;ZAdd<d=ZBd>d? ZCd@dA ZDdBdC ZEddDdEZFddFdGZGdHdI ZHdJdK ZIdLdM ZJdNdO ZKdPdQ ZLdRdS ZMdTdU ZNdVdW ZOdXdY ZPdZd[ ZQd\d] ZRdd^d_ZSdd`daZTddbdcZUddde ZVdfdg ZWdhdi ZXdjdk ZYdldm ZZdndo Z[dpdq Z\drds Z]e^j_e^j` dt Zadudv Zbdwdx Zcdydz Zdd{d| Zed}d~ Zfdd Zgdd Zhdd ZidddZjdd Zkdd Zldd Zmdd Zndd Zodd Zpdd Zqdd ZrdddZsdd ZtdddZudd Zvdd Zwdd Zxdd Zydd Zzdd Z{dd Z|dd Z}dd Z~ddiZdd ZdddZdddZdd ZdddZdddZdd Zdd ZddÄ Zddń ZdddǄZddɄ Zdd˄ Zdd̈́ Zddτ Zddф Zddӄ ZddՄ ZdddׄZdS )EditorWindowr   )
Percolator)ColorDelegatorcolor_config)UndoDelegator)	IOBindingencoding)mainmenu)MultiStatusBar)AutoComplete)
AutoExpand)Calltip)CodeContext)LineNumbers)FormatParagraphFormatRegionIndentsRstrip)
ParenMatch)
ZoomHeightNTc                 C   s	  ddl m} tjd u rtjtjdd}tj	
dr:ddtjd d   }tjd	r.d
}n^d}tj||dd}nRtj	d d dkrdd l}	d}
dtj d}z	|	|	j|}
W n tyu   z	|	|	j|}
W n	 tyr   Y nw Y nw tj|
r~|
}ntj	dkrtjtjd}tj|}tj|r|t_tj	dkrdtj t_n
dtjd d  t_|| _|p|j}|| _t|| _tj|| jd | _}|r|j| _|j| j_ni | _i | j_t j!otjt j!d| _"d| _#t$| | _%}t&|dd | _'}t j(ddddd}ddd d|d!t j(ddd"ddd#}t)t*|fi | | _+}| j+| j_,| -  | .  | j/d$| j0 | j1d%| j2 t34 r]|1d%| j2 |1d&| j5 |1d'| j5 n|1d(| j5 |1d)t6 |1d*t6 |1d+t6 |1d,| j7 |1d-| j8 |1d.| j9 |1d/| j: |1d0| j; |1d1| j< |1d2| j= |1d3| j> |1d4| j? |1d5| j@ |1d6d7d8  |1d9| jA |1d:| jB |1d;| jC |1d<| jD |1d=| jE |1d>| jF |1d?| jG |1d@| jH |1dA| jI |1dB| jJ |1dC| jK | L|  | _M}|1dD|jN |1dE|jO |1dF|jP |1dG|jQ |1dH|jR |1dI|jS | T| }|1dJ|jU |1dK|jV |1dL| Wd |1dM| WdN |1dO| jX |1dP| jY |1dQ| jZ |r||j| < |r| |j[|< |1dR| j\ |1dS| jj] |1dT| j^ |1dU| j_ |1dV| j` | a  |jbtctddNdW |jedNdNdX |jfdNdNdX | jg|dY< |jhdNdtidZ |jj|d[< t k| jdd|d\< |jhdNdNtidZ |l  | m  t j(dd]d^d_d}| | _nd`| _o| jo| _p| q  trt das| j+db t _s| t  dc| _u| v| | _w}| x  | _y}|z| |j{|_{|j||_||}| j~ | |  | _}|| j dd| _| dd d | _d | _d | _|rtj|rtj|s||rde| _| |}| | n|| de| _|   | ~  |   |   | jdf}|r|dg}|d u rdh}|dkr|  |dN }|| _t| j tj| _tj| _tj| _|didj |dkdldmdn |dodp |dqdr |dsdrdtdu | | | j}|1di|j |1dk|j |1dv|j |1dw| | j |1dx| | j | | }|1dy|j |1ds|j || }|1dz|j |1d{|j |1d||j |1d}| | j | |  | _}|1do|j |1dq|j |1d~|j |1d| | j | jr| | | _|1d| jj n| ddd | jr| | | _t j(dddd_dr|   |1d| j d S | ddd d S )Nr   )ScriptBindingZDocz
index.htmllinuxzpython-docs-z%s.%s.%s   z/var/www/html/python/z/var/www/html/python/index.htmlz/usr/share/doc/win zSoftware\Python\PythonCore\z\Help\Main Python Documentationr   z0Resources/English.lproj/Documentation/index.htmlzfile://zhttps://docs.python.org/%d.%d/   )menuzrecent-files.lstvbar)namemainr$   widthinttypetext   ZnoneZwordprocessorheight)r@   padxwrapZhighlightthicknessrB   ZtabstylerH   ZWM_DELETE_WINDOW<<close-window>>z<Control-Button-1>z<2>z<3>z<MouseWheel>z
<Button-4>z
<Button-5>z<Configure>z<<cut>>z<<copy>>z	<<paste>>z<<center-insert>>z<<help>>z<<python-docs>>z<<about-idle>>z<<open-config-dialog>><<open-module>>z<<do-nothing>>c                 S   s   dS Nbreakr!   eventr!   r!   r"   <lambda>   s    z'EditorWindow.__init__.<locals>.<lambda>z<<select-all>>z<<remove-selection>>z<<find>>z<<find-again>>z<<find-in-files>>z<<find-selection>>z<<replace>><<goto-line>>z<<smart-backspace>>z<<newline-and-indent>>z<<smart-indent>>z<<indent-region>>z<<dedent-region>>z<<comment-region>>z<<uncomment-region>>z<<tabify-region>>z<<untabify-region>>z<<toggle-tabs>><<change-indentwidth>>z<Left>z<Right>   z<<del-word-left>>z<<del-word-right>>z<<beginning-of-line>>z<<open-new-window>>z<<close-all-windows>>z<<open-class-browser>>z<<open-path-browser>>z<<open-turtle-demo>>)sidefillexpand)Zweightcommand)rowcolumnZstickyZyscrollcommandfontIndentz
use-spacesboolr   blink_off_timeinsertofftime)2   i  i@KL FTr   endz<<autocomplete>>z	<Key-Tab>z<<try-open-completions>>z<KeyRelease-period>z<KeyRelease-slash>z<KeyRelease-backslash>z<<try-open-calltip>>z<KeyRelease-parenleft>z<<refresh-calltip>>z<KeyRelease-parenright>z<<paren-closed>>z<KeyRelease-bracketright>z<KeyRelease-braceright>z<<force-open-completions>>z<<expand-word>>z<<format-paragraph>>z<<flash-paren>>z<<check-module>>z<<run-module>>z<<run-custom>>z<<do-rstrip>>z<<force-open-calltip>>z<<zoom-height>>z<<toggle-code-context>>optionsz*ode*ontextdisabledzline-numbers-defaultz<<toggle-line-numbers>>*ine*umbers)Zidlelib.runscriptr8   r$   help_urlospathjoinr   base_prefixplatformcountr   isdirwinregwinver
QueryValueHKEY_CURRENT_USERFileNotFoundErrorHKEY_LOCAL_MACHINEisfilenormpathflistrootMenumenubarr   ZListedTopleveltopvarstkinter_varsZinversedictinstance_dictr   Zuserdirrecent_files_pathprompt_last_lineFrame
text_framer   r?   	GetOptionr   ZTextrF   Zfocused_widgetcreatemenubarapply_bindingsprotocolclosebindclose_eventr   ZisAquaTkright_menu_eventr   handle_winconfigcutcopypastecenter_insert_eventhelp_dialogpython_docsabout_dialogconfig_dialogopen_module_event
select_allremove_selection
find_eventfind_again_eventfind_in_files_eventfind_selection_eventreplace_eventgoto_line_eventsmart_backspace_eventnewline_and_indent_eventsmart_indent_eventr3   fregionindent_region_eventZdedent_region_eventZcomment_region_eventZuncomment_region_eventZtabify_region_eventZuntabify_region_eventr4   Ztoggle_tabs_eventZchange_indentwidth_eventmove_at_edge_if_selectiondel_word_leftdel_word_righthome_callbackdictnew_callbackZclose_all_callbackopen_module_browseropen_path_browseropen_turtle_demoset_status_barpackZLEFTZBOTHZrowconfigureZcolumnconfigurehandle_yviewZgridZNSEWsetGetFontZ	focus_set	set_widthusetabstabwidthindentwidthset_notabs_indentwidthhasattrr^   update_cursor_blinknum_context_linesr%   perr(   undoZinsertfilterundo_block_startundo_block_stopZset_saved_change_hooksaved_change_hookr)   ioZset_filename_change_hookfilename_change_hookZ	good_loadset_indentation_paramscolorcode_contextline_numbersexistsloadfileispythonsourceZset_filenameResetColorizerupdate_recent_files_listload_extensionsmenudictgetindexadd_separator	wmenu_endZregister_callbackpostwindowsmenur   Z
askintegerr   Zaskyesno	showerror	event_addr-   user_input_insert_tagsZautocomplete_eventZtry_open_completions_eventZforce_open_completions_eventr.   Zexpand_word_eventr2   Zformat_paragraph_eventr6   Zflash_paren_eventZparen_closed_eventZcheck_module_eventZrun_module_eventZrun_custom_eventr5   Z	do_rstripr/   ctipZtry_open_calltip_eventZrefresh_calltip_eventZforce_open_calltip_eventr7   Zzoom_height_eventallow_code_contextr0   Ztoggle_code_context_eventupdate_menu_stateallow_line_numbersr1   toggle_line_numbers_event)selfrv   filenamekeyrw   r8   ZdochomeZpyverZbasepathrn   ZdocfileZKEYrz   r   r?   rB   Ztext_optionsrF   r   indentsZ	usespacesr   r   r   	is_py_srcr>   ra   ZautocompleteZ
parenmatchZscriptbindingr   r!   r!   r"   __init__I   s  
























zEditorWindow.__init__c                 C   s   |    d S N)r   r   rP   r!   r!   r"   r   k     zEditorWindow.handle_winconfigc                 C   s^   | j }tt|jj|d|dg}| d|  }t||ddd}|| | _	d S )NZborderrI   r=   r[   )r[   0)
rF   summaptkZgetintZcgetZwinfo_widthr   measurerB   )r   rF   Zinner_paddingZpixel_widthZzero_char_widthr!   r!   r"   r   n  s   zEditorWindow.set_widthc                 C   s   | j  \}}| j| dS rM   )r   Zdefaultfilenamerv   new)r   rP   dirnamebasenamer!   r!   r"   r   {  s   zEditorWindow.new_callbackc                 C   s  |j d@ dkr|jdkrd S | jdr2| jdddr2| jdddr2t| jddd	 }n| jdd}tt	|D ]
}|| d
vrI nq?t	|}t| jddd	 }||krad}dt
| d }|j d	@ dkry| jddd nV| jds| jdd n| j| jdd| jdr| jdd n| jdd | j|}| jd}| j|d|r||}}| jddd | jd|| | jd| | jd dS )N   r   ZHomeZiomark<=zinsert lineendinsert linestart.rT   ) 	insertzinsert linestart+csel1.0ra   	sel.firstZ	my_anchor<sel.last>rN   )stateZkeysymrF   r   comparerC   splitr   rangelenstr
tag_removemark_settag_addsee)r   rP   ZinsertptlineZlineatdestfirstlastr!   r!   r"   r     sH   

zEditorWindow.home_callbackc                 C   s   |  | j| _t| jdddd}tjdkr| jjddtd | jjdd	td | jjd
dtd | jjt	t
d |jt	t
d | jd| j | jddd | j| j d S )NrT   Zgrey75)rH   ZborderwidthZ
backgroundr   Z	_padding1z    )rU   rZ   zCol: ?r   zLn: ?)rU   rV   z<<set-line-and-column>>z<KeyRelease>z<ButtonRelease>)r,   rz   
status_barr   r   rk   	set_labelZRIGHTr   ZBOTTOMXrF   r   set_line_and_columnr   Z
after_idle)r   sepr!   r!   r"   r     s   
zEditorWindow.set_status_barc                 C   s>   | j td\}}| jdd|  | jdd|  d S )Nr   rZ   zCol: %sr   zLn: %s)rF   r   ZINSERTr   r  r  )r   rP   r   rZ   r!   r!   r"   r    s   z EditorWindow.set_line_and_column))fileZ_File)editZ_Edit)formatZF_ormat)runZ_Run)rc   Z_Options)r   Z_Window)r
   Z_Helpc                 C   s   | j }i  | _}| jD ]'\}}t|\}}t| | dd }t||d|d ||< }|j|||d qt rIt|ddd |d< }|jd|d	 | 	  t| j dd
| _
| jd jddd| j
d | jd t| _|   d S )NZ_menu_postcommandr   )r@   tearoffpostcommand)labelr>   	underlineZapple)r@   r  ZapplicationZIDLE)r  r>   r  r  r:   zRecent Files)r  r  r>   r
   )ry   r   
menu_specsprepstrgetattrrx   Zadd_cascader   Z
isCarbonTk
fill_menusrecent_files_menuZinsert_cascader   ENDbase_helpmenu_lengthreset_help_menu_entries)r   Zmbarr   r@   r  r  r  r>   r!   r!   r"   r     s,   
zEditorWindow.createmenubarc                 C   sJ   | j d }|d}|d u rd}|| jkr|| jd | t| d S )Nr   ra   rb   rT   )r   r   r   deleter   Zadd_windows_to_menu)r   r>   ra   r!   r!   r"   r     s   


zEditorWindow.postwindowsmenuc                 C      | j | }|j||d dS )z$Update label for menu item at index.)r  Nr   entryconfig)r   r>   r   r  menuitemr!   r!   r"   update_menu_label     
zEditorWindow.update_menu_labelc                 C   r  )z$Update state for menu item at index.r   Nr  )r   r>   r   r   r  r!   r!   r"   r     r  zEditorWindow.update_menu_statec                 G   sT   |dkrt |d }t| d| | d }d}|df}| jj|g|R   dS )zHandle scrollbar.Zmovetor   ra   @0,0ZscrollZunitsrN   )floatround	getlinenorF   yview)r   rP   argsZfractionlinesr!   r!   r"   r     s   zEditorWindow.handle_yviewc              	   C   s*  | j }|d|j d|j }z|dd|o||dd}W n ty+   d}Y nw |s;|ddd	 |d
| | jsB| 	  | j}|| _
tjd d dk}|rY|jdd | jD ]%}z|\}}	}
W n	 tyn   Y q\w |
d u rtq\t| |
 }|j||d q\||j|j |r| j jdd dS )N@,r   r   r   Fr   r   ra   r   r:   r;   Zarrow)Zcursorr   ZibeamrN   )rF   r   xyr   TclErrorr   r   rmenu
make_rmenurP   r   rk   Zconfigrmenu_specs
ValueErrorr  ZentryconfigureZtk_popupZx_rootZy_root)r   rP   rF   ZnewdexZin_selectionr-  Ziswinitemr  	eventnameZverify_stater   r!   r!   r"   r     sB   
zEditorWindow.right_menu_event)CloserK   Nc                 C   sf   t | jdd}| jD ]#}|d |d }}|d ur)| j|fdd}|j||d q
|  q
|| _d S )Nr   r  rT   c                 S      |  | d S r   event_generaterF   r2  r!   r!   r"   rX   /     z(EditorWindow.make_rmenu.<locals>.commandr  rX   )rx   rF   r/  add_commandr   r-  )r   r-  r1  r  r2  rX   r!   r!   r"   r.  *  s   


zEditorWindow.make_rmenuc                 C   s   |   S r   )rmenu_check_copyr   r!   r!   r"   rmenu_check_cut6  s   zEditorWindow.rmenu_check_cutc                 C   s2   z| j d}W n
 ty   Y dS w |rdS dS )Nr   rd   normalrF   r   r,  )r   Zindxr!   r!   r"   r;  9  s   zEditorWindow.rmenu_check_copyc                 C   s0   z| j jd| j d W dS  ty   Y dS w )Nztk::GetSelectionZ	CLIPBOARDrd   r>  )rF   r   callr,  r<  r!   r!   r"   rmenu_check_pasteA  s   zEditorWindow.rmenu_check_pastec                 C      t | j dS )zHandle Help 'About IDLE' event.rN   )r   ZAboutDialogrz   r   r!   r!   r"   r   I  s   zEditorWindow.about_dialogc                 C   s   t | jd dS )z&Handle Options 'Configure IDLE' event.ZSettingsrN   )r   ZConfigDialogrz   r   r!   r!   r"   r   O  s   zEditorWindow.config_dialogc                 C   s"   | j r| j }n| j}t| dS )zHandle Help 'IDLE Help' event.rN   )rw   rz   r
   Zshow_idlehelp)r   rP   parentr!   r!   r"   r   U  s
   
zEditorWindow.help_dialogc              
   C   sr   t jd d dkr1z	t| j W dS  ty0 } ztjdt|| j	d W Y d }~dS d }~ww t
| j dS )Nr:   r;   Document Start FailuretitlemessagerC  rN   )r   rk   rg   	startfilerf   OSErrorr   r   r   rF   
webbrowseropen)r   rP   whyr!   r!   r"   r   _  s   
zEditorWindow.python_docsc                 C      | j d dS )Nz<<Cut>>rN   rF   r6  r   r!   r!   r"   r   j     zEditorWindow.cutc                 C   s    | j dsd S | j d dS )Nr   z<<Copy>>rN   )rF   Z
tag_rangesr6  r   r!   r!   r"   r   n  s   zEditorWindow.copyc                 C   s   | j d | j d dS )Nz	<<Paste>>r   rN   )rF   r6  r   r   r!   r!   r"   r   u     zEditorWindow.pastec                 C   s.   | j ddd | j dd | j d dS )Nr   r   zend-1cr   rN   )rF   r   r   r   r   r!   r!   r"   r   z  s   zEditorWindow.select_allc                 C   s    | j ddd | j d dS )Nr   r   ra   r   rN   )rF   r   r   r   r!   r!   r"   r     s   zEditorWindow.remove_selectionc                    s*   | j j| j jd fdd}|S )zCursor move begins at start or end of selection

        When a left/right cursor key is pressed create and return to Tkinter a
        function which causes a cursor move from the associated edge of the
        selection.

        )zsel.first+1czsel.last-1cc                    sD   | j d@ dkr zd d   W d S  ty   Y d S w d S )NrG   r   r   r   )r   r,  rO   
edge_indexZedges_tableZself_text_indexZself_text_mark_setr!   r"   move_at_edge  s   z<EditorWindow.move_at_edge_if_selection.<locals>.move_at_edge)rF   r   r   )r   rR  rS  r!   rQ  r"   r     s
   z&EditorWindow.move_at_edge_if_selectionc                 C   rM  )Nz<Meta-Delete>rN   rN  r   r!   r!   r"   r     rO  zEditorWindow.del_word_leftc                 C   rM  )Nz<Meta-d>rN   rN  r   r!   r!   r"   r     rO  zEditorWindow.del_word_rightc                 C   rB  rM   )r   findrF   r   r!   r!   r"   r     rO  zEditorWindow.find_eventc                 C   rB  rM   )r   Z
find_againrF   r   r!   r!   r"   r     rO  zEditorWindow.find_again_eventc                 C   rB  rM   )r   Zfind_selectionrF   r   r!   r!   r"   r     rO  z!EditorWindow.find_selection_eventc                 C   s   t  | j| j| j dS rM   )r	   rF   r   rv   r   r!   r!   r"   r     s   z EditorWindow.find_in_files_eventc                 C   s   t  | j dS rM   )r   rF   r   r!   r!   r"   r     rO  zEditorWindow.replace_eventc                 C   sT   | j }t|ddj}|d ur(|ddd |d| d |d |   dS )	Nz
Go To Linez/Enter a positive integer
('big' = end of file):r   r   ra   r   .0rN   )rF   r   ZGotoresultr   r   r   r  )r   rP   rF   linenor!   r!   r"   r     s   
zEditorWindow.goto_line_eventc                 C   sp   z| j dd }W n ty   d}Y nw t| j dd|j}|dur6| jr0| j| |S | j	
| |S )zGet module name from user and open it.

        Return module path or None for calls by open_module_browser
        when latter is not invoked in named editor window.
        r   r   r<   zOpen ModulezAEnter the name of a Python module
to search on sys.path and open:N)rF   r   stripr,  r   Z
ModuleNamerV  rv   rK  r   r   )r   r@   	file_pathr!   r!   r"   open_module  s$   zEditorWindow.open_modulec                 C      |    dS rM   )rZ  r   r!   r!   r"   r        zEditorWindow.open_module_eventc                 C   sJ   | j j}| jjdkr|s|  }|d u rdS ddlm} || j| dS )NZPyShellEditorWindowrN   r   )browser)	r   r   	__class____name__rZ  idlelibr]  ZModuleBrowserrw   )r   rP   r   r]  r!   r!   r"   r     s   z EditorWindow.open_module_browserc                 C   s   ddl m} || j dS )Nr   )pathbrowserrN   )r`  ra  ZPathBrowserrw   )r   rP   ra  r!   r!   r"   r     rP  zEditorWindow.open_path_browserc                 C   s&   dd l }tjddg}|j|dd dS )Nr   z-cz,from turtledemo.__main__ import main; main()F)shellrN   )
subprocessr   
executablePopen)r   rP   rc  cmdr!   r!   r"   r     s   zEditorWindow.open_turtle_democ                 C   sV   |d ur'|dkr)| j dd|  | j ddd | j ddd |   d S d S d S )Nr   r   z%d.0r   r   ra   z
insert +1l)rF   r   r   r   center)r   rW  r!   r!   r"   gotoline  s   zEditorWindow.gotolinec                 C   s`   |rt j|r
dS t jt j|\}}t j|tv r dS | jdd}|	do/d|v S )NTr   z1.0 lineendz#!Zpython)
rg   rh   rm   splitextr   normcaser   rF   r   
startswith)r   r   baseextr   r!   r!   r"   r     s   zEditorWindow.ispythonsourcec                 C   s    | j r| j |  d | _ d S d S r   )rv   Zunregister_maybe_terminater<  r!   r!   r"   
close_hook  s   
zEditorWindow.close_hookc                 C   s
   || _ d S r   )rn  )r   rn  r!   r!   r"   set_close_hook     
zEditorWindow.set_close_hookc                 C   s2   | j r	| j |  |   | j|  |   d S r   )rv   Zfilename_changed_editr   rz   Zupdate_windowlist_registryr   r<  r!   r!   r"   r     s
   z!EditorWindow.filename_change_hookc                 C   sD   | j rd S | | jjr|  | _ | j r | jj| j | jd d S d S )N)filterZafter)r   r   r   r   r&   r   Zinsertfilterafterr   r<  r!   r!   r"   _addcolorizer  s   
zEditorWindow._addcolorizerc                 C   s,   | j sd S | j   | j| j  d | _ d S r   )r   Zremovecolorsr   Zremovefilterr<  r!   r!   r"   _rmcolorizer   s
   

zEditorWindow._rmcolorizerc                 C   sL   |    |   t| j | jdur| j  | jdur$| j  dS dS )zUpdate the color themeN)	rs  rr  r$   r'   rF   r   Zupdate_highlight_colorsr   Zupdate_colorsr<  r!   r!   r"   r   '  s   


zEditorWindow.ResetColorizer_c                 C   sp   | d| ||}|r|| jv r| d|d | d||kr)|d| n|d|d  || d S )NERRORz
 wordstart
r   z+1c)r   r   
IDENTCHARSr   r   )r   rF   poscharr!   r!   r"   colorize_syntax_error6  s   
z"EditorWindow.colorize_syntax_errorc                 C   s4   t jddddd}|sd| jd< dS t j| jd< dS )	z&Update the cursor blink configuration.rA   r$   zcursor-blinkr]   rD   r   r_   N)r   r   rF   r^   )r   Zcursorblinkr!   r!   r"   r   A  s   z EditorWindow.update_cursor_blinkc                 C   sN   | j dur
| j   | jdur| j  t| jdd}|| jd< |   dS )z.Update the text widgets' font if it is changedNrA   r$   r[   )r   Zupdate_fontr   r   r   rw   rF   r   )r   Znew_fontr!   r!   r"   	ResetFontK  s   




zEditorWindow.ResetFontc                 C   s|   t   | j_}| D ]\}}| jj|g|R   q|  D ]}t |}|r;| D ]\}}| jj|g|R   q,qdS )z/Remove the keybindings before they are changed.N)	r   GetCurrentKeySetr+   default_keydefsitemsrF   Zevent_deleteget_standard_extension_namesGetExtensionBindings)r   keydefsrP   keylistextensionNamexkeydefsr!   r!   r"   RemoveKeybindings\  s   
zEditorWindow.RemoveKeybindingsc                 C   s>  t   | j_}|   |  D ]}t |}|r| | qi }| jjD ]!}i ||d < |d D ]}|rE|d ||d  t|d d < q1q%| j	D ]R}| j	| }|
t}|du r[qJ|d7 }td|D ]7}	||	dkr||	d}
|
r||	d}d}||v r||| v r|| | }|rt||}
|j|	|
d qdqJdS )	z-Update the keybindings after they are changedr   rT   NrX   acceleratorr  r<   )r  )r   r|  r+   r}  r   r  r  menudefsr  r   r   r  r   rE   Z	entrycgetget_acceleratorr  )r   r  r  r  ZmenuEventDictr>   r1  ZmenubarItemra   r   ZaccelZitemNamerP   r!   r!   r"   ApplyKeybindingsh  sH   

 



zEditorWindow.ApplyKeybindingsc                 C   s"   | j stjddddd| _dS dS )zCUpdate the indentwidth if changed and not using tabs in this windowrA   r\   z
num-spacesrC   rD   N)r   r   r   r   r<  r!   r!   r"   r     s
   
z#EditorWindow.set_notabs_indentwidthc                 C   s|   t  }| jd }|t}|| jkr|| jd | |r7|  |D ]}| |d }|j	|d |d q$|| jd< dS )z3Update the additional help entries on the Help menur
   rT   r   r9  N)
r   ZGetAllExtraHelpSourcesListr   r   r  r  r  r   "_EditorWindow__extra_help_callbackr:  )r   Z	help_listZhelpmenuZhelpmenu_lengthentryrf  r!   r!   r"   r    s   


z$EditorWindow.reset_help_menu_entriesc                       |f fdd	}|S )zCCreate a callback with the helpfile value frozen at definition timec              
      s   |  dstj| } tjd d dkr;zt|  W d S  ty: } ztj	dt
| jd W Y d }~d S d }~ww t|  d S )N)Zwwwhttpr:   r;   rD  rE  )rk  rg   rh   ru   r   rk   rH  rI  r   r   r   rF   rJ  rK  )helpfilerL  r<  r!   r"   display_extra_help  s   

z>EditorWindow.__extra_help_callback.<locals>.display_extra_helpr!   )r   r  r  r!   r<  r"   Z__extra_help_callback  s   z"EditorWindow.__extra_help_callbackc              
      s  g }| j }|r)tj|r)t|dddd}| }W d   n1 s$w   Y  |rBtj|d }||v r<|| |d| g  |D ]}d|v sVtj|dd	 s[ 	| qF fd
d|D }d}|dt
| }|rzt|dddd}|| W d   n1 sw   Y  W n* ty } zt| jddsd| j_tjdd| d| jd W Y d}~nd}~ww | jjD ]+}	|	j}
|
dt t|D ]\}}| }|	|}|
j|| d | |dd qqdS )z/Load and update the recent files list and menusrutf_8r   )r*   errorsNrv  r    rb   c                    s   g | ]}| vr|qS r!   r!   ).0rh   Z	bad_pathsr!   r"   
<listcomp>  s    z9EditorWindow.update_recent_files_list.<locals>.<listcomp>Z1234567890ABCDEFGHIJKwrecentfiles_messageFTzIDLE Warningz)Cannot save Recent Files list to disk.
  z
Select OK to continue.rE  r   )r  rX   r  )r~   rg   rh   r   rK  	readlinesabspathremover   appendr   
writelinesrI  r  rw   r  r   showwarningrF   rz   r}   r  r  r  	enumeraterstrip#_EditorWindow__recent_file_callbackr:  )r   new_fileZrf_listrY  Zrf_list_filerh   ZulcharsZrf_fileerrinstancer>   i	file_namecallbackr!   r  r"   r     sl   


	
z%EditorWindow.update_recent_files_listc                    r  )Nc                    s    j j| d d S )N)ZeditFile)r   rK  )Z
fn_closurer<  r!   r"   open_recent_file  s   z=EditorWindow.__recent_file_callback.<locals>.open_recent_filer!   )r   r  r  r!   r<  r"   Z__recent_file_callback  s   z#EditorWindow.__recent_file_callbackc                 C   s   |   }|  }|r|r|d | t }n|r|}n|r|}nd}|p&|p&|}|  s3d| }d| }| j| | j| d S )Nz - untitledz*%s*z*%s)short_title
long_title_py_version	get_savedrz   Zwm_titleZwm_iconname)r   ZshortlongrF  Ziconr!   r!   r"   r     s   zEditorWindow.saved_change_hookc                 C   s
   | j  S r   )r   r  r<  r!   r!   r"   r    rp  zEditorWindow.get_savedc                 C   s   | j | d S r   )r   	set_saved)r   flagr!   r!   r"   r    s   zEditorWindow.set_savedc                 C   s   | j   d S r   )r   
reset_undor<  r!   r!   r"   r    r8  zEditorWindow.reset_undoc                 C   s   | j j}|rtj|S dS )Nr  )r   r   rg   rh   r   )r   r   r!   r!   r"   r    s   zEditorWindow.short_titlec                 C   s   | j jpdS )Nr<   )r   r   r<  r!   r!   r"   r    r   zEditorWindow.long_titlec                 C   r[  rM   )rg  r   r!   r!   r"   r     r\  z EditorWindow.center_insert_eventr   c                 C   sH   | j }|  \}}| |}|| }td||d  }|t| d S )NrT   r=   )rF   getwindowlinesr$  maxr%  r"  )r   markrF   rz   botrW  rH   Znewtopr!   r!   r"   rg    s   
zEditorWindow.centerc                 C   sN   | j }| d}| d}||kr#| dkr#t|d }|| d }||fS )Nr!  z@0,65535rT   rH   )rF   r$  Zwinfo_heightrC   )r   rF   rz   r  rH   r!   r!   r"   r    s   

zEditorWindow.getwindowlinesc                 C   s   | j }tt||S r   )rF   rC   r"  r   )r   r  rF   r!   r!   r"   r$    s   zEditorWindow.getlinenoc                 C   s(   | j  }td|}ttt| S )zReturn (width, height, x, y)z(\d+)x(\d+)\+(-?\d+)\+(-?\d+))rz   Zwm_geometryrematchlistr   rC   groups)r   Zgeommr!   r!   r"   get_geometry"  s   
zEditorWindow.get_geometryc                 C   r[  rM   )r   r   r!   r!   r"   r   (  r\  zEditorWindow.close_eventc                 C   sH   | j r"|  s| j dkr| j  | j  | j  | j  S d S )Nr>  )r   r  rz   r   Z	deiconifylowerZlift	maybesaver<  r!   r!   r"   r  ,  s   



zEditorWindow.maybesavec                 C   s8   z|   }t|dkr|   |W S  ty   Y d S w )Ncancel)r  r   _closeAttributeError)r   Zreplyr!   r!   r"   r   5  s   zEditorWindow.closec                 C   s   | j jr| j| j jd t| j |   | j   d | _ d | _| j	r,| j	  d | _	d | _
d | _| j  d | _| j  | jrH|   d S d S )N)r  )r   r   r   r   Zunregister_callbackr   unload_extensionsr   r   r   rF   r|   r   rz   Zdestroyrn  r<  r!   r!   r"   r  >  s$   



zEditorWindow._closec                 C   s   i | _ |   d S r   )
extensionsload_standard_extensionsr<  r!   r!   r"   r   R  s   zEditorWindow.load_extensionsc                 C   s0   t | j D ]}t|dr|  qi | _d S )Nr   )r  r  valuesr   r   )r   insr!   r!   r"   r  V  s
   

zEditorWindow.unload_extensionsc                 C   s@   |   D ]}z| | W q   tdt| t  Y qd S )NzFailed to load extension)r  load_extensionprintrepr	traceback	print_exc)r   r@   r!   r!   r"   r  \  s   z%EditorWindow.load_standard_extensionsc                 C   s   t jddS )NT)Zeditor_only)r   ZGetExtensionsr<  r!   r!   r"   r  d  r   z)EditorWindow.get_standard_extension_namesZZzDummyZzzdummyc           	   	   C   sN  | j ||}zztjd| td}W n ttfy#   t|}Y nw W n ty2   td|  w t||}t	
|}t|drI| |j| || }|| j|< |r| | |D ]I}|dd}|d d dkry|dd  }|d d dksk|d	d  d
kr|d d	 }|d	d  d
ks|d }t||r| j|t|| q[d S d S )Nr   )packagez
Failed to import extension: r  -rt  rT   r   rb   r   Z_event)extfilesr   	importlibimport_module__package__ImportError	TypeErrorr  r  r   r  r   r  r  r  r   r   rF   r   )	r   r@   Zfnamemodclsr  r  ZveventZ
methodnamer!   r!   r"   r  k  sD   






zEditorWindow.load_extensionc                 C   sH   |d u r| j j}| j}||_| D ]\}}|r!|j|g|R   qd S r   )r+   r}  rF   r  r~  r   )r   r  rF   rP   r  r!   r!   r"   r     s   zEditorWindow.apply_bindingsc              	   C   s   |du r| j j}|du r| j j}| j}| j}|D ]Z\}}||}|s$q|D ]K}|s/|  q&|\}	}
|	dd dk}|rC|	dd }	t|	\}}	t||
}||
fdd}|rh| 	|
t
}|j|	||||d q&|j|	|||d q&qdS )zAdd appropriate entries to the menus and submenus

        Menus that are absent or None in self.menudict are ignored.
        NrT   !c                 S   r4  r   r5  r7  r!   r!   r"   rX     r8  z(EditorWindow.fill_menus.<locals>.command)r  r  rX   r  variable)r  r  rX   r  )r+   r  r}  r   rF   r   r   r  r  get_var_objZ
BooleanVarZadd_checkbuttonr:  )r   r  r  r   rF   ZmnameZ	entrylistr>   r  r  r2  Zcheckbuttonr  r  rX   varr!   r!   r"   r    s@   


zEditorWindow.fill_menusc                 C   s"   |  |}|r| }|S t|r   )r  r   	NameError)r   r@   r  valuer!   r!   r"   getvar  s
   
zEditorWindow.getvarc                 C   s&   |  ||}|r|| d S t|r   )r  r   r  )r   r@   r  vartyper  r!   r!   r"   setvar  s   zEditorWindow.setvarc                 C   s,   | j |}|s|r|| j | j |< }|S r   )r|   r   rF   )r   r@   r  r  r!   r!   r"   r    s   zEditorWindow.get_var_objc                 C   s(   | j r| jd|pd| j|v S dS )NZTODOSTRINGrT   )r   rF   tag_prevrangeZ	tag_names)r   Z
text_indexr!   r!   r"   is_char_in_string  s
   zEditorWindow.is_char_in_stringc                 C   s8   z| j d}| j d}||fW S  ty   Y dS w )Nr   r   NNr?  )r   r  r  r!   r!   r"   get_selection_indices  s   
z"EditorWindow.get_selection_indicesc                 C   s   | j d pt}t|S )Ntabs)rF   TK_TABWIDTH_DEFAULTrC   )r   currentr!   r!   r"   get_tk_tabwidth  s   zEditorWindow.get_tk_tabwidthc              	   C   sF   | j }|  |kr!|jdd|d d|jd| }|j|d d S d S )Nr[   r   z
-displayofn)r  )rF   r  r   r@  ZmasterZ	configure)r   ZnewtabwidthrF   Zpixelsr!   r!   r"   set_tk_tabwidth  s   zEditorWindow.set_tk_tabwidthc                 C   sP   |r |r |   }d|  krdkrn n|| _| j| jkr d| _| | j d S )Nr=   r   F)guess_indentr   r   r   r  )r   r   Zguessr  r!   r!   r"   r     s   z#EditorWindow.set_indentation_paramsc           
      C   sD  | j }|  \}}|r|r||| |d| dS |dd}|dkr9|dddr3|d dS |  dS |d d	vrF|d dS | j}t|	|}|d
ksVJ |d | j
 | j
 }d
}		 |d d }|	d }	t|	|}||ks~|d d	vrnqc|  |d|	 d ||k r|dd||  | j |  dS )Nr   rN   r   r<   r   r   z	insert-1crb    	r   rT   Tz
insert-%dcr   )rF   r  r  r   r   r   Zbellr   r   
expandtabsr   r   r   r   r   )
r   rP   rF   r  r  charsr   ZhaveZwantZncharsdeletedr!   r!   r"   r     sH   

z"EditorWindow.smart_backspace_eventc           
      C   s   | j }|  \}}|  zm|r1|r1t|t|kr%| j|W |  S ||| |d| |	dd}t
|| j\}}|t|krN| || j  n!| jrTd}nt|| j}| j}	d|	||	   }|d|| j |d W |  dS |  w )Nr   r   r   r   rN   )rF   r  r   
index2liner   r   r   r  r   r   get_line_indentr   r   reindent_tor   r   r  r   r   r   )
r   rP   rF   r  r  prefixrawZ	effectiveZpadr  r!   r!   r"   r   0  s.   

zEditorWindow.smart_indent_eventc              	   C   sJ  | j }|  \}}|  z|r|r||| |d| |dd}dt|}}||k rD|| dv rD|d7 }||k rD|| dv s6||kr\|dd| j W |	d |
  dS |d| }d}|r~|d	 dv r~|dd	 }|d7 }|r~|d	 dv sl|r|d
| d |ddv r|d |ddv s|dd| j t|d}	t| j| j}
| js| jD ],}t|	| d}t|d }||d}|
| |
| |}|dus|dkr nq|
|pd n|dd}|r|d }nd}||d}|
| |
d |
 }|tjkrk|tjkrnB|tjkr,|d|| j n3|tjkr:| |
   n%|tj!krX|
" dkrP|d|| j n| |
#  nJ d|f W |	d |
  dS |
$ }|d|| j |
% r| &| n|r|
' r| (| W |	d |
  dS |	d |
  w )a  Insert a newline and indentation after Enter keypress event.

        Properly position the cursor on the new line based on information
        from the current line.  This takes into account if the current line
        is a shell prompt, is empty, has selected text, contains a block
        opener, contains a block closer, is a continuation line, or
        is inside a string.
        r   r   r   r  rT   rv  rN   Nrb   zinsert - %d charsrU  Zconsoler   zbogus continuation type %r))rF   r  r   r  r   r   r   r   r   r   r   r  r   r   ZParserr   r   r   r   r  r  Zset_codeZfind_good_parse_start_build_char_in_string_funcZset_lor  Zget_continuation_typeZC_NONEZC_STRING_FIRST_LINEZC_STRING_NEXT_LINESZ	C_BRACKETr  Zcompute_bracket_indentZC_BACKSLASHZget_num_lines_in_stmtZcompute_backslash_indentZget_base_indent_stringZis_block_openerr   Zis_block_closerr   )r   rP   rF   r  r  r   r  r  indentlnor+  contextZstartatZstartatindexZrawtextZbodr  r   r!   r!   r"   r   S  s   	
U











z%EditorWindow.newline_and_indent_eventc                 C   s   || j fdd}|S )Nc                 S   s   ||d|   S )Nz+%dcr!   )offsetZ_startindexZ_icisr!   r!   r"   inner  s   z6EditorWindow._build_char_in_string_func.<locals>.inner)r  )r   Z
startindexr  r!   r!   r"   r    s   
z'EditorWindow._build_char_in_string_funcc                 C   s.   | j rt|| j\}}d| d|  S d| S )Nr   r   )r   divmodr   )r   r  ZntabsZnspacesr!   r!   r"   _make_blanks  s   zEditorWindow._make_blanksc                 C   sN   | j }|  |dddr|dd |r!|d| || j |  d S )Nr   z!=r   )rF   r   r   r  r   r  r   r   )r   rZ   rF   r!   r!   r"   r    s   zEditorWindow.reindent_toc                 C   sV   t | j| j \}}|r#|r#t|| j\}}t|| j\}}|| S d }}|| S Nr   )IndentSearcherrF   r   r  r  )r   openerZindentedr  ZindentsmallZindentlarger!   r!   r"   r    s   zEditorWindow.guess_indentc                 C   sN   | j d u rd S | j jr| j   d}n| j   d}| jdd| dd d S )NZShowZHiderc   re   z Line Numbers)r>   r   r  )r   Zis_shownZhide_sidebarZshow_sidebarr  )r   rP   Z
menu_labelr!   r!   r"   r     s   



z&EditorWindow.toggle_line_numbers_event)NNNNr   )r   r  )T)r_  
__module____qualname__Zidlelib.percolatorr%   Zidlelib.colorizerr&   r'   Zidlelib.undor(   Zidlelib.iomenur)   r*   r`  r+   Zidlelib.statusbarr,   Zidlelib.autocompleter-   Zidlelib.autoexpandr.   Zidlelib.calltipr/   Zidlelib.codecontextr0   Zidlelib.sidebarr1   Zidlelib.formatr2   r3   r4   r5   Zidlelib.parenmatchr6   Zidlelib.zoomheightr7   r   getfilesystemencodingZfilesystemencodingrf   r   r   r   r   r   r   r   r   r   r  r  r   r   r  r   r   r-  r   r/  r.  r=  r;  rA  r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rZ  r   r   r   r   rh  r   rn  ro  r   rr  rs  r   stringascii_lettersdigitsrw  rz  r   r{  r  r  r   r  r  r   r  r   r  r  r  r  r  r   rg  r  r$  r  r   r  r   r  r   r  r  r  r  r  r   r  r  r  r  r  r  r  r  r   r   r   r   r  r  r  r  r   r!   r!   r!   r"   r$   2   s    
  
$+

%









			
$
.


		

	%


	+#x
	r$   c                 C   s   t t| S r   )rC   r"  )r   r!   r!   r"   r    r   r  z[ \t]*c                 C   s$   t | }| t| |fS )zReturn a line's indentation as (# chars, effective # of spaces).

    The effective # of spaces is the length after properly "expanding"
    the tabs into spaces, as done by str.expandtabs(tabwidth).
    )_line_indent_rer  ra   r   groupr  )r   r   r  r!   r!   r"   r    s   
r  c                   @   s8   e Zd Zdd Zdd ZejejdfddZdd	 Z	d
S )r  c                 C   s(   || _ || _d | _| _d  | _| _d S r  )rF   r   r  finishedblkopenlineindentedline)r   rF   r   r!   r!   r"   r   !  s   zIndentSearcher.__init__c                 C   sL   | j rdS | jd  }| _t|d }| j|ddrdS | j||d S )Nr<   rT   rU  z>=ra   z lineend+1c)r  r  r  rF   r   r   )r   r  r  r!   r!   r"   readline'  s   zIndentSearcher.readline)classdefforiftrywhilec	           	      C   sJ   | j rd S ||kr||v r|| _d S ||kr!| jr#|| _d| _ d S d S d S )NrT   )r  r  r  )	r   rE   tokenstartra   r   INDENTNAMEZOPENERSr!   r!   r"   
tokeneater0  s   

zIndentSearcher.tokeneaterc              	   C   sl   t j}| jt _z$zt | j}|D ]}| j|  qW n t jtfy&   Y nw W |t _n|t _w | j| j	fS r   )
tokenizetabsizer   generate_tokensr  r  
TokenErrorSyntaxErrorr  r  )r   Zsave_tabsizetokensr  r!   r!   r"   r  <  s   zIndentSearcher.runN)
r_  r  r  r   r  r  r  r  r  r  r!   r!   r!   r"   r    s    

r  c                 C   s6   |  d}|dkr| d | | |d d   } || fS )Nrt  r   rT   )rT  )sr  r!   r!   r"   r  N  s   
r  []/)ZbracketleftZbracketrightZslashc                 C   s   |  |}|rt r|dv rdS |d }tddd |}tddd |}td	d|}td
d|}tdd|}tdd|}tdd|}tdd|}tdd|}|S )N>   rR   rL   rS   r<   r   z-[a-z]\bc                 S   s   |    S r   )r  upperr  r!   r!   r"   rQ   g  s    z!get_accelerator.<locals>.<lambda>z\b\w+\bc                 S   s   t |  |  S r   )keynamesr   r  r  r!   r!   r"   rQ   h  s    zKey-ZCancelz
Ctrl-BreakzControl-zCtrl-r  +z><r   r   r   )r   r   Z	isCocoaTkr  sub)r  r2  r  r  r!   r!   r"   r  ]  s   
r  c                 C   s4   | j }|ddd |ddd |ddd d S )	NZtcl_wordBreakAfterza br   r   Ztcl_wordcharsz\wZtcl_nonwordcharsz\W)r   r@  )rw   r   r!   r!   r"   fixwordbreakss  s   r#  c                 C   s~   | }t | tjdd  rtjd }nd }t|d  t||d}|j}d|d< tdD ]}|dd| t	| d  q,d S )	NrT   )rw   r   
   rH      r   z  rv  )
r#  r   argvr   ZsetupAppr$   rF   r   r   r   )rC  rw   r   r	  rF   r  r!   r!   r"   _editor_window|  s   r'  __main__)rA   zidlelib.idle_test.test_editorr=   F)	verbosityexit)r  ):Zimportlib.abcr  importlib.utilrg   rk   r  r   r   r  r  rJ  tkinterZtkinter.fontr   Ztkinter.ttkr   r   r   Zidlelib.configr   r`  r   r	   r
   r   r   Zidlelib.multicallr   r   r   r   r   Zidlelib.treer   Zidlelib.utilr   r   r  Zpython_versionr  r   r#   r$   r  compiler  r  r  r  r   r  r#  r'  r_  ZunittestrA   Zidlelib.idle_test.htestr  r!   r!   r!   r"   <module>   s~    
           d

4
	