a
    ñ»jW  ã                   @   s¸   d dl Z d dlZd dlZd dlZd dlZd dlmZ d dlm  m	Z	 d dl
m  mZ d dlmZ d dlmZ d dlmZ d dlmZmZ d dlmZmZ G dd„ deƒZd	d
„ ZdS )é    N)ÚReadlineError)Ú#make_key_press_from_key_description)Úlog)Úis_callableÚis_ironpython)Ú
ensure_strÚensure_unicodec                   @   sV  e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Ze	eƒZ
e	edƒŽ Ze	edƒŽ Ze	edƒƒZe	edƒƒZe	edƒƒZe	edƒƒZe	edƒƒZe	edƒƒZe	edƒƒZe	edƒƒZe	edƒƒZdd„ Zd}dd„Zdd„ Zdd„ Zd d!„ Zd"d#„ Zd$d%„ Zd&d'„ Zd(d)„ Zd*d+„ Zd,d-„ Z d.d/„ Z!d0d1„ Z"d2d3„ Z#d4d5„ Z$d6d7„ Z%d8d9„ Z&d:d;„ Z'd<d=„ Z(d>d?„ Z)d@dA„ Z*dBdC„ Z+dDdE„ Z,dFdG„ Z-dHdI„ Z.dJdK„ Z/dLdM„ Z0dNdO„ Z1dPdQ„ Z2dRdS„ Z3dTdU„ Z4dVdW„ Z5dXdY„ Z6dZd[„ Z7d\d]„ Z8d^d_„ Z9d`da„ Z:dbdc„ Z;ddde„ Z<dfdg„ Z=dhdi„ Z>djdk„ Z?dldm„ Z@dndo„ ZAdpdq„ ZBdrds„ ZCdtdu„ ZDdvdw„ ZEdxdy„ ZFdzd{„ ZGd|S )~ÚBaseModeÚbasec                 C   sª   d| _ || _i | _i | _d| _ d | _t d¡| _t 	¡ | _
d| _d| _d| _d| _d | _d| _d| _d| _d | _d | _d| _d	| _d
| _d| _d| _d| _g | _g | _d S )Nr   é   Ú z 	
"\'`@$><=;|&{(ÚonÚoffé   Té   z>>> )ÚargumentÚrlobjÚexit_dispatchÚkey_dispatchZprevargumentÚlineobjÚReadLineTextBufferÚl_bufferÚhistoryZLineHistoryÚ_historyÚcompleter_delimsÚshow_all_if_ambiguousÚmark_directoriesÚcomplete_filesystemÚ	completerÚbegidxÚendidxZtabstopÚstartup_hookÚpre_input_hookÚfirst_promptZcursor_sizeÚpromptÚenable_ipython_paste_for_pathsÚ"enable_ipython_paste_list_of_listsÚenable_win32_clipboardÚpaste_line_bufferZ
_sub_modes)Úselfr   © r*   ú@E:\soccer\ml_env\Lib\site-packages\pyreadline3/modes/basemode.pyÚ__init__   s4    
zBaseMode.__init__c                 C   s   dS )Nz
<BaseMode>r*   ©r)   r*   r*   r+   Ú__repr__E   s    zBaseMode.__repr__c                    s    ‡ fdd„}‡ fdd„}||fS )Nc                    s   t | jˆ ƒS ©N©Úgetattrr   r-   ©Úxr*   r+   ÚgI   s    zBaseMode._gs.<locals>.gc                    s   t | jˆ |ƒ d S r/   )Úsetattrr   )r)   Úqr2   r*   r+   ÚsL   s    zBaseMode._gs.<locals>.sr*   )r3   r4   r7   r*   r2   r+   Ú_gsH   s    zBaseMode._gsc                    s   ‡ fdd„}|S )Nc                    s   t | jˆ ƒS r/   r0   r-   r2   r*   r+   r4   R   s    zBaseMode._g.<locals>.gr*   )r3   r4   r*   r2   r+   Ú_gQ   s    zBaseMode._gc                 C   s   | j }d| _ |dkrd}|S )Nr   r   ©r   )r)   Úvalr*   r*   r+   Ú	_argresetW   s
    zBaseMode._argresetÚctrl_c_tap_time_intervalÚallow_ctrl_cÚ_print_promptÚ_update_lineÚconsoleÚprompt_begin_posÚprompt_end_posÚ_bellÚ
bell_styleÚ_clear_afterÚ_update_prompt_posc                 C   s   t ‚d S r/   ©ÚNotImplementedError)r)   Úkeyinfor*   r*   r+   Úprocess_keyeventt   s    zBaseMode.process_keyeventr   c                 C   sš   d| j _| jrJd| _| jrJz|  ¡  W n" tyH   tdƒ t ¡  Y n0 | j  ¡  || _	| j
r–z|  
¡  W n( ty”   tdƒ t ¡  d | _
Y n0 d S )NéÿÿÿÿFzstartup hook failedzpre_input_hook failed)r   Zselection_markr#   r!   ÚBaseExceptionÚprintÚ	tracebackÚ	print_excZ
reset_liner$   r"   )r)   r$   r*   r*   r+   Úreadline_setupw   s$    
zBaseMode.readline_setupc                 C   s
   d| _ dS )zŠEvery bindable command should call this function for cleanup.
        Except those that want to set argument to a non-zero value.
        r   Nr:   r-   r*   r*   r+   Úfinalize�   s    zBaseMode.finalizec                 C   s   | j  t |¡¡ d S r/   )r   Úadd_historyr   r   )r)   Útextr*   r*   r+   rS   •   s    zBaseMode.add_historyc                 C   sˆ   dd| j f g}| dd| jf ¡ | dd| jf ¡ | d¡ d}| |d ¡ tdd	„ | j ¡ D ƒƒ}|D ]}| || ¡ qp|S )
Nz	%-20s: %szshow all if ambigousr   rE   z'------------- key bindings ------------z%-7s %-7s %-7s %-15s %-15s )ÚControlZMetaZShiftzKeycode/charÚFunctionc                 S   s0   g | ](\}}|d  |d |d |d |j f‘qS )r   r   é   é   )Ú__name__)Ú.0ÚkÚvr*   r*   r+   Ú
<listcomp>¢   ó    z2BaseMode.rl_settings_to_string.<locals>.<listcomp>)r   Úappendr   rE   Úsortedr   Úitems)r)   ÚoutZtablepatZbindingsÚkeyr*   r*   r+   Úrl_settings_to_stringš   s    
ÿzBaseMode.rl_settings_to_stringc                 C   sf   t |ƒs6td||f ƒ td||t|ƒt| jƒf ƒ‚t| ¡ ƒ ¡ }td||j	f ƒ || j
|< dS )ú0setup the mapping from key to call the function.z,Trying to bind non method to keystroke:%s,%sz2Trying to bind non method to keystroke:%s,%s,%s,%sz>>>%s -> %s<<<N)r   rN   r   ÚtypeÚ	_bind_keyr   ÚlowerÚtupler   rY   r   )r)   rc   ÚfuncrJ   r*   r*   r+   rg   ¨   s    ÿÿzBaseMode._bind_keyc                 C   s   t | ¡ ƒ ¡ }d| j|< dS )re   N)r   rh   ri   r   )r)   rc   rJ   r*   r*   r+   Ú_bind_exit_key´   s    zBaseMode._bind_exit_keyc                 C   s   t ‚dS )zLWhen in vi command mode, this causes a switch to emacs editing
        mode.NrH   ©r)   Úer*   r*   r+   Úinit_editing_mode¹   s    zBaseMode.init_editing_modec              	   C   s  g }| j j| _| j j| _| j j}| jrþ| jdkr`|  jd8  _|| j | jv r&|  jd7  _q`q&td || j| j… ¡ƒ}t	dt
|ƒ ƒ d}z|  t
|ƒ|¡}W n ty¸   Y qèY n0 |d7 }|du rÎqèqŽ|rŽ||vrŽ| |¡ qŽqŽt	dttt
|ƒƒ ƒ | jdk�r |�s | jdk�rP|  jd8  _|| j dv �r|  jd7  _�qP�qtd || j| j… ¡ƒ}t	d	t
|ƒ ƒ ttt
t tj |¡d
 d¡ ¡ƒƒ}| jdk�rêg }|D ]0}tj |¡�rØ| |tj ¡ n
| |¡ �q´|}t	dttt
|ƒƒ ƒ |S )z|Return a list of possible completions for the string ending at the point.
        Also set begidx and endidx in the process.r   r   r   zcomplete text="%s"Nztext completions=<%s>r   z 	
zfile complete text="%s"Ú*Úasciizfnames=<%s>)r   Úpointr   r    Zline_bufferr   r   r   Újoinr   r   Ú
IndexErrorr_   ÚlistÚmapr   ÚglobÚosÚpathÚ
expanduserÚencoder   ÚisdirÚsep)r)   ÚcompletionsÚbufrT   ÚiÚrZmcÚfr*   r*   r+   Ú_get_completionsÁ   s^    



þÿzBaseMode._get_completionsc                 C   sÚ   |sd S | j  d¡ ttt|ƒƒ}| j  ¡ \}}tdt|d |d  ƒƒ}tt t	t|ƒƒ| ¡ƒ}t
|ƒD ]T}d}t
|ƒD ]6}	|	| | }
|
t|ƒk r|| j  ||
  |d ¡¡ q|| j  d¡ qltrÎtj| _|  ¡  d S )NÚ
r   r   )rA   ÚwriteÚmaxru   ÚlenÚsizeÚintÚmathÚceilÚfloatÚrangeÚljustr   ÚsysÚps1r$   r?   )r)   r}   ZwmaxÚwÚhÚcolsÚrowsÚrowr7   Úcolr   r*   r*   r+   Ú_display_completionsù   s"    zBaseMode._display_completionsc                 C   s¤   |   ¡ }|r�t|ƒ}t|ƒdkrddd„ |D ƒ}| jj}|| j| j| j…< |t|ƒ | j| j  | j_t|ƒdkr˜| jdkr†|  |¡ q˜|  	¡  n|  	¡  |  
¡  dS )z¤Attempt to perform completion on the text before point. The
        actual completion performed is application-specific. The default is
        filename completion.r   c                 S   s   g | ]}|‘qS r*   r*   ©rZ   Úcr*   r*   r+   r]     r^   z%BaseMode.complete.<locals>.<listcomp>r   r   N)r‚   Úcommonprefixr†   r   rq   r   r    r   r–   rD   rR   )r)   rm   r}   ZcprefixÚreprq   r*   r*   r+   Úcomplete  s    

zBaseMode.completec                 C   s   |   ¡ }|  |¡ |  ¡  dS )z7List the possible completions of the text before point.N)r‚   r–   rR   )r)   rm   r}   r*   r*   r+   Úpossible_completions!  s    
zBaseMode.possible_completionsc                 C   sf   |   ¡ }| j}| j}|D ]:}dd„ |D ƒ}| d¡ || j||…< |t|ƒ7 }|}q|| _|  ¡  dS )zoInsert all completions of the text before point that would have
        been generated by possible-completions.c                 S   s   g | ]}|‘qS r*   r*   r—   r*   r*   r+   r]   .  r^   z/BaseMode.insert_completions.<locals>.<listcomp>ú N)r‚   r   r    r_   r   r†   Zline_cursorrR   )r)   rm   r}   ÚbÚcomprš   r*   r*   r+   Úinsert_completions'  s    
zBaseMode.insert_completionsc                 C   s   |   ¡  dS )aw  Similar to complete, but replaces the word to be completed with a
        single match from the list of possible completions. Repeated
        execution of menu-complete steps through the list of possible
        completions, inserting each match in turn. At the end of the list of
        completions, the bell is rung (subject to the setting of bell-style)
        and the original text is restored. An argument of n moves n
        positions forward in the list of matches; a negative argument may be
        used to move backward through the list. This command is intended to
        be bound to TAB, but is unbound by default.N©rR   rl   r*   r*   r+   Úmenu_complete6  s    
zBaseMode.menu_completec                 C   s   | j  || j¡ |  ¡  dS )z"Insert text into the command line.N)r   Úinsert_textÚargument_resetrR   )r)   Ústringr*   r*   r+   r£   D  s    zBaseMode.insert_textc                 C   s   | j  ¡  |  ¡  dS ©z&Move to the start of the current line.N)r   Úbeginning_of_linerR   rl   r*   r*   r+   r§   I  s    
zBaseMode.beginning_of_linec                 C   s   | j  ¡  |  ¡  dS ©zMove to the end of the line.N)r   Úend_of_linerR   rl   r*   r*   r+   r©   N  s    
zBaseMode.end_of_linec                 C   s   | j  | j¡ |  ¡  dS ©zMove forward a character.N)r   Úforward_charr¤   rR   rl   r*   r*   r+   r«   S  s    zBaseMode.forward_charc                 C   s   | j  | j¡ |  ¡  dS ©zMove back a character.N)r   Úbackward_charr¤   rR   rl   r*   r*   r+   r­   X  s    zBaseMode.backward_charc                 C   s   | j  | j¡ |  ¡  dS ©z[Move forward to the end of the next word. Words are composed of
        letters and digits.N)r   Úforward_wordr¤   rR   rl   r*   r*   r+   r¯   ]  s    zBaseMode.forward_wordc                 C   s   | j  | j¡ |  ¡  dS ©ziMove back to the start of the current or previous word. Words are
        composed of letters and digits.N)r   Úbackward_wordr¤   rR   rl   r*   r*   r+   r±   c  s    zBaseMode.backward_wordc                 C   s   | j  | j¡ |  ¡  dS r®   )r   Úforward_word_endr¤   rR   rl   r*   r*   r+   r²   i  s    zBaseMode.forward_word_endc                 C   s   | j  | j¡ |  ¡  dS r®   )r   Úbackward_word_endr¤   rR   rl   r*   r*   r+   r³   o  s    zBaseMode.backward_word_endc                 C   s   | j  ¡  |  ¡  dS r¦   )r   Ú"beginning_of_line_extend_selectionrR   rl   r*   r*   r+   r´   v  s    
z+BaseMode.beginning_of_line_extend_selectionc                 C   s   | j  ¡  |  ¡  dS r¨   )r   Úend_of_line_extend_selectionrR   rl   r*   r*   r+   rµ   {  s    
z%BaseMode.end_of_line_extend_selectionc                 C   s   | j  | j¡ |  ¡  dS rª   )r   Úforward_char_extend_selectionr¤   rR   rl   r*   r*   r+   r¶   €  s    z&BaseMode.forward_char_extend_selectionc                 C   s   | j  | j¡ |  ¡  dS r¬   )r   Úbackward_char_extend_selectionr¤   rR   rl   r*   r*   r+   r·   …  s    z'BaseMode.backward_char_extend_selectionc                 C   s   | j  | j¡ |  ¡  dS r®   )r   Úforward_word_extend_selectionr¤   rR   rl   r*   r*   r+   r¸   Š  s    z&BaseMode.forward_word_extend_selectionc                 C   s   | j  | j¡ |  ¡  dS r°   )r   Úbackward_word_extend_selectionr¤   rR   rl   r*   r*   r+   r¹   �  s    z'BaseMode.backward_word_extend_selectionc                 C   s   | j  | j¡ |  ¡  dS r®   ©r   Ú!forward_word_end_extend_selectionr¤   rR   rl   r*   r*   r+   r»   –  s    z*BaseMode.forward_word_end_extend_selectionc                 C   s   | j  | j¡ |  ¡  dS r®   rº   rl   r*   r*   r+   Ú"backward_word_end_extend_selectionœ  s    z+BaseMode.backward_word_end_extend_selectionc                 C   s   | j  ¡  |  ¡  dS )z…Uppercase the current (or following) word. With a negative
        argument, uppercase the previous word, but do not move the cursor.N)r   Úupcase_wordrR   rl   r*   r*   r+   r½   ¤  s    
zBaseMode.upcase_wordc                 C   s   | j  ¡  |  ¡  dS )z…Lowercase the current (or following) word. With a negative
        argument, lowercase the previous word, but do not move the cursor.N)r   Údowncase_wordrR   rl   r*   r*   r+   r¾   ª  s    
zBaseMode.downcase_wordc                 C   s   | j  ¡  |  ¡  dS )z‡Capitalize the current (or following) word. With a negative
        argument, capitalize the previous word, but do not move the cursor.N)r   Úcapitalize_wordrR   rl   r*   r*   r+   r¿   °  s    
zBaseMode.capitalize_wordc                 C   s   | j  ¡  |  ¡  dS )zhClear the screen and redraw the current line, leaving the current
        line at the top of the screen.N)rA   ÚpagerR   rl   r*   r*   r+   Úclear_screen¸  s    
zBaseMode.clear_screenc                 C   s   |   ¡  dS )z6Refresh the current line. By default, this is unbound.Nr¡   rl   r*   r*   r+   Úredraw_current_line¾  s    zBaseMode.redraw_current_linec                 C   s   |   ¡  dS )a  Accept the line regardless of where the cursor is. If this line
        is non-empty, it may be added to the history list for future recall
        with add_history(). If this line is a modified history line, the
        history line is restored to its original state.Tr¡   rl   r*   r*   r+   Úaccept_lineÂ  s    zBaseMode.accept_linec                 C   s   | j  | j¡ |  ¡  dS )zÉDelete the character at point. If point is at the beginning of
        the line, there are no characters in the line, and the last
        character typed was not bound to delete-char, then return EOF.N)r   Údelete_charr¤   rR   rl   r*   r*   r+   rÄ   Ê  s    zBaseMode.delete_charc                 C   s   | j  | j¡ |  ¡  dS ©zyDelete the character behind the cursor. A numeric argument means
        to kill the characters instead of deleting them.N)r   Úbackward_delete_charr¤   rR   rl   r*   r*   r+   rÆ   Ñ  s    zBaseMode.backward_delete_charc                 C   s   | j  | j¡ |  ¡  dS rÅ   )r   Úbackward_delete_wordr¤   rR   rl   r*   r*   r+   rÇ   ×  s    zBaseMode.backward_delete_wordc                 C   s   | j  | j¡ |  ¡  dS rÅ   )r   Úforward_delete_wordr¤   rR   rl   r*   r*   r+   rÈ   Ý  s    zBaseMode.forward_delete_wordc                 C   s   | j  ¡  |  ¡  dS )zEDelete all spaces and tabs around point. By default, this is unbound.N)r   Údelete_horizontal_spacerR   rl   r*   r*   r+   rÉ   ã  s    
z BaseMode.delete_horizontal_spacec                 C   s,   |j r t|j ƒdkr |  |j ¡ |  ¡  dS )zInsert yourself.r   N)ÚcharÚordr£   rR   rl   r*   r*   r+   Úself_insertè  s    ÿÿzBaseMode.self_insertc                 C   sV   | j rJt d¡}| d¡d  d¡ d¡}tdttt|ƒƒ ƒ |  	|¡ |  
¡  dS )zzPaste windows clipboard.
        Assume single line strip other lines and end of line markers and trailing spaces
        Frƒ   r   úzpaste: >%s<N)r'   Ú	clipboardÚget_clipboard_text_and_convertÚsplitÚstripr   rt   ru   rË   r£   rR   ©r)   rm   Útxtr*   r*   r+   Úpasteò  s    

zBaseMode.pastec                 C   sŠ   t  d¡}| jr~t d¡}| |¡}dd„ |D ƒ}|dgkrz|  |d ¡ |  | j 	¡ ¡ |dd… | _
td	| j
 ƒ d
S dS |  ¡  dS )zVPaste windows clipboard as multiline code.
        Removes any empty lines in the codez?
Fc                 S   s   g | ]}|  ¡ d kr|‘qS )r   )rÑ   )rZ   r”   r*   r*   r+   r]     r^   z1BaseMode.paste_mulitline_code.<locals>.<listcomp>r   r   r   Nzmulti: >%s<T)ÚreÚcompiler'   rÎ   rÏ   rÐ   r£   rS   r   Úcopyr(   r   rR   )r)   rm   ÚregrÓ   Útr*   r*   r+   Úpaste_mulitline_codeý  s    



zBaseMode.paste_mulitline_codec                 C   s^   | j rRt | j¡}| jrHt|ƒdk rHd|vrHd|vrH| dd¡ dd¡}|  |¡ |  ¡  dS )	a  Paste windows clipboard. If enable_ipython_paste_list_of_lists is
        True then try to convert tabseparated data to repr of list of lists or
        repr of array.
        If enable_ipython_paste_for_paths==True then change \ to / and spaces
        to \spacei,  ú	rƒ   ú\ú/r�   z\ N)	r'   rÎ   rÏ   r&   r%   r†   Úreplacer£   rR   rÒ   r*   r*   r+   Úipython_paste  s    ÿ
zBaseMode.ipython_pastec                 C   s   | j  ¡  |  ¡  dS ©z5Copy the text in the region to the windows clipboard.N)r   Úcopy_region_to_clipboardrR   rl   r*   r*   r+   rá     s    
z!BaseMode.copy_region_to_clipboardc                 C   s   | j  ¡  |  ¡  dS rà   )r   Úcopy_selection_to_clipboardrR   rl   r*   r*   r+   râ   $  s    
z$BaseMode.copy_selection_to_clipboardc                 C   s   | j  ¡  |  ¡  dS rà   )r   Úcut_selection_to_clipboardrR   rl   r*   r*   r+   rã   )  s    
z#BaseMode.cut_selection_to_clipboardc                 C   s0   t ƒ  d |  ¡ ¡}t |ƒ |  ¡  |  ¡  dS )a  Print all of the functions and their key bindings to the Readline
        output stream. If a numeric argument is supplied, the output is
        formatted in such a way that it can be made part of an inputrc
        file. This command is unbound by default.rƒ   N)rN   rr   rd   r?   rR   rÒ   r*   r*   r+   Údump_functions.  s
    zBaseMode.dump_functionsN)r   )HrY   Ú
__module__Ú__qualname__Úmoder,   r.   r8   r9   r<   Úpropertyr¤   r=   r>   r?   r@   rA   rB   rC   rD   rE   rF   rG   rK   rQ   rR   rS   rd   rg   rk   rn   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Ô   rÚ   rß   rá   râ   rã   rä   r*   r*   r*   r+   r	      sˆ   &	
8
r	   c                 C   sx   | sdS | d }| D ]^}t t|ƒƒD ]L}|d|d …  ¡ |d|d …  ¡ kr$|d|… }|dkrl  dS  qq$q|S )zGGiven a list of pathnames, returns the longest common leading componentr   r   Nr   )rŒ   r†   rh   )ÚmÚprefixÚitemr   r*   r*   r+   r™   :  s    (r™   )rv   r‰   rw   rÕ   rŽ   Zpyreadline3.clipboardrÎ   Zpyreadline3.lineeditor.historyZ
lineeditorr   Zpyreadline3.lineeditor.lineobjr   Zpyreadline3.errorr   Zpyreadline3.keysymsr   Zpyreadline3.loggerr   Zpyreadline3.py3k_compatr   r   Zpyreadline3.unicode_helperr   r   Úobjectr	   r™   r*   r*   r*   r+   Ú<module>   s$       "