a
    lcJ                     @   s  d Z 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	Z	 ddl
mZmZ ddlmZmZmZmZmZmZ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  e e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+d"d# Z,d$d% Z-e d&d' Z.ed( d)d*d+Z/d,d- Z0ej1d.krdd/l2m3Z3 dd0l4m5Z5m6Z6m7Z7 e d1d2 Z8dS )3zU
Module to define and register Terminal IPython shortcuts with
:mod:`prompt_toolkit`
    N)Callable)get_app)DEFAULT_BUFFERSEARCH_BUFFER)	has_focushas_selection	Conditionvi_insert_modeemacs_insert_modehas_completionsvi_mode)!display_completions_like_readline)KeyBindings)named_commands)	InputModeViState)undocc                  C   s   t  jjj} |  p|  S N)r   current_bufferdocumentcurrent_line_before_cursorisspace)before r   V/var/www/html/django/DPS/env/lib/python3.9/site-packages/IPython/terminal/shortcuts.pycursor_in_leading_ws   s    r   c                 C   sj   | j }|j}|j|jd }|d}|d  }|j}|dur\|jr\|dkr\||j n
t	|  dS )z1
    Apply autosuggestion if at end of line.
    N
r    )
r   r   textcursor_positionsplitstrip
suggestioninsert_textncend_of_line)eventbdafter_cursorlinesZend_of_current_liner"   r   r   r   _apply_autosuggest$   s    
r+   c                    sX  t  }ttB }tddr&}nt}|jdttt	 @ |@ d| fdd}|jddttt	 @ |@ d| |dt
 |jd	ttt@ dt |jd
ttt@ dt |jdttt@ dt |jdttdt |jdttdt tdd }|jd|dt |jdttt	 @ |@ t@ dt |jdttt@ dtj |jdttdt tfdd}dd ttB tt@ }i i  fdd} fdd}	tdd }
|jd||@ |	d@ ddd  }|jd!||@ |	d@ dd"d  }|jd#||@ |	d@ dd$d  }|jd%||@ |
@ |fd&d@ |	d@ dd'd  }|jd(||@ |
@ |fd)d@ |	d@ dd*d  }|jd%||@ |
@ |d+@ dd,d  }|jd(||@ |
@ |d-@ dd.d  }|jd||@ |d/@ dd0d  }|jd!||@ |d/@ dd1d  }|jd#||@ |d/@ dd2d  }|jd3||@ |	d4@ d|jd5||@ |	d6@ d|jd7||@ |	d8@ d|jd%||@ |	d9@ d|jd(||@ |	d:@ dd;d  }|jd<||d=@ |@ |	d4@ d|jd<||d>@ |@ |	d6@ d|jd<||d?@ |@ |	d8@ d|jd<||d@@ |@ |	d9@ d|jd<||dA@ |@ |	d:@ ddBd  }jdCkr|jdDttt	 @ |@ t @ dt tjdEkr|jdFttt @ dt tfdGdH}ttt@ }|jdItt|t B @ ddJd  }|jdK||@ ddLd  }|jdM|ddNd  }|jddO||@ ddPd  }tj tj!tj"tj#tj$tj%dQ}|& D ] \}}|j|||@ d| qtj'tj'tj(tj)tj*tj#tj+tj,tj-tj.dR
}|& D ]"\}}|j|dS||@ i| qfdTdU}dVdW }j/dXkrTj0rTt1j2t3_4t5||t3_6|S )Yz8Set up the prompt_toolkit keyboard shortcuts for IPythonhandle_returnNenter)filterc                    s    t | j| jj  | j  d S r   )reformat_text_before_cursorr   r   validate_and_handler&   shellr   r   reformat_and_executeC   s    z6create_ipython_shortcuts.<locals>.reformat_and_executeescapezc-\zc-pzc-nzc-gzc-cc                   S   s
   t tdS )NSIGTSTP)hasattrsignalr   r   r   r   <lambda>[       z*create_ipython_shortcuts.<locals>.<lambda>zc-ztabzc-of2c                      s    j S r   )
auto_matchr   r2   r   r   r=   i   s    z,create_ipython_shortcuts.<locals>.auto_matchc                 S   sJ   d}d}|t |k rF|| }|| kr,| }n|dkr<|d7 }|d7 }q|S )NTr   \   )len)quotebufpairedicr   r   r   all_quotes_pairedm   s    
z3create_ipython_shortcuts.<locals>.all_quotes_pairedc                    sP   v r S t r&fdd}nt  fdd}t|}|< |S )Nc                     s   t  } | jjj}t |S r   )r   r   r   r   boolappbefore_cursor)patternr   r   _preceding_text   s    
zIcreate_ipython_shortcuts.<locals>.preceding_text.<locals>._preceding_textc                     s   t  } | jjj}t |S r   )r   r   r   r   rG   matchrH   mr   r   rL      s    
)callablerecompiler   )rK   rL   	condition)_preceding_text_cache)rO   rK   r   preceding_text}   s    
z0create_ipython_shortcuts.<locals>.preceding_textc                    sH   z
|  W S  t y   Y n0 t|   fdd}t|}|| < |S )Nc                     s   t  } t | jjjS r   )r   rG   rM   r   r   current_line_after_cursor)rI   rN   r   r   _following_text   s    zIcreate_ipython_shortcuts.<locals>.following_text.<locals>._following_text)KeyErrorrQ   rR   r   )rK   rW   rS   )_following_text_cacherN   r   following_text   s    

z0create_ipython_shortcuts.<locals>.following_textc                  S   sR   t  } | jjj}|dddd}tdd|}tdd|}d|v pNd|v  S )Nz\"r   z\'z%(?:\"\"\"[\s\S]*\"\"\"|'''[\s\S]*''')z(?:"[^"]*["\n]|'[^']*['\n])"')r   r   r   text_before_cursorreplacerQ   sub)rI   sr   r   r   not_inside_unclosed_string   s    
z<create_ipython_shortcuts.<locals>.not_inside_unclosed_string(z	[,)}\]]|$c                 S   s   | j d | j   d S )N()r   r#   cursor_leftr1   r   r   r   _   s    z#create_ipython_shortcuts.<locals>._[c                 S   s   | j d | j   d S )N[]rd   r1   r   r   r   rf      s    {c                 S   s   | j d | j   d S )N{}rd   r1   r   r   r   rf      s    r[   c                    s
    d| S )Nr[   r   linerF   r   r   r9      r:   c                 S   s   | j d | j   d S )Nz""rd   r1   r   r   r   rf      s    	r\   c                    s
    d| S )Nr\   r   rk   rm   r   r   r9      r:   c                 S   s   | j d | j   d S )Nz''rd   r1   r   r   r   rf      s    	z^.*""$c                 S   s   | j d | j d d S )Nz""""   rd   r1   r   r   r   rf      s    z^.*''$c                 S   s   | j d | j d d S )Nz''''rn   rd   r1   r   r   r   rf      s    z.*(r|R)[\"'](-*)$c                 S   sH   t d| jjj}|dpd}| jd|  | jt|d  d S )N.*(r|R)[\"'](-*)   r   rc   r?   	rQ   rM   r   r   r   groupr#   re   r@   r&   matchesdashesr   r   r   rf      s    c                 S   sH   t d| jjj}|dpd}| jd|  | jt|d  d S )Nro   rp   r   rh   r?   rq   rs   r   r   r   rf      s    c                 S   sH   t d| jjj}|dpd}| jd|  | jt|d  d S )Nro   rp   r   rj   r?   rq   rs   r   r   r   rf     s    )z^\)]z^\]}z^\}z^"z^'c                 S   s   | j   d S r   )r   cursor_rightr1   r   r   r   rf     s    	backspacez.*\($z.*\[$z.*\{$z.*"$z.*'$c                 S   s   | j   | j   d S r   )r   deletedelete_before_cursorr1   r   r   r   rf     s    $
readlinelikezc-iwin32zc-vc                      s    j S r   ) emacs_bindings_in_vi_insert_moder   r2   r   r   ebivimP  s    z(create_ipython_shortcuts.<locals>.ebivimendc                 S   s   t |  d S r   r+   r1   r   r   r   rf   V  s    c-ec                 S   s   t |  d S r   r   r1   r   r   r   rf   Z  s    zc-fc                 S   s,   | j }|j}|r||j n
t|  d S r   )r   r"   r#   r   r$   forward_char)r&   r'   r"   r   r   r   rf   ^  s
    fc                 S   sH   | j }|j}|r:td|j}|tdd |D d n
t|  d S )Nz(\S+\s+)c                 s   s   | ]}|r|V  qd S r   r   ).0xr   r   r   	<genexpr>m  r:   z6create_ipython_shortcuts.<locals>._.<locals>.<genexpr>r   )	r   r"   rQ   r    r   r#   nextr$   forward_word)r&   r'   r"   tr   r   r   rf   g  s    )zc-azc-bzc-kzc-wzc-yzc-_)
)c-xr   )r   e)r5   r'   )r5   rE   )r5   r(   )r5   h)r5   l)r5   u)r5   y)r5   .r.   c                    s   t  } j|_ j|_| jS r   )r   ttimeoutlen
timeoutlen_input_mode)selfrI   r2   r   r   get_input_mode  s    z0create_ipython_shortcuts.<locals>.get_input_modec                 S   sB   t jdt jdi|d}d|}tj| tj  || _	d S )Nrp         z[{} q)
r   
NAVIGATIONREPLACEgetformatsysstdoutwriteflushr   )r   modeshapecursorr   r   r   set_input_mode  s
    

z0create_ipython_shortcuts.<locals>.set_input_modevi)7r   r	   r
   getattrr,   newline_or_execute_outeraddr   r   r   quit'previous_history_or_previous_completionnext_history_or_next_completionr   dismiss_completionreset_bufferr   reset_search_bufferr   suspend_to_bgr   indent_buffernewline_autoindent_outerinput_transformer_manageropen_input_in_editordisplay_completionsr   r   platformr   	win_paster$   beginning_of_linebackward_char	kill_linebackward_kill_wordyankundoitemsedit_and_executebackward_wordcapitalize_word	kill_worddowncase_worduppercase_wordyank_popyank_last_argediting_modemodal_cursorr   INSERTr   r   property
input_mode)r3   kbinsert_modeZreturn_handlerr4   Zsupports_suspendr=   Zfocused_insertrU   rZ   ra   rf   r   Zfocused_insert_viZkey_cmd_dictkeycmdZkeys_cmd_dictkeysr   r   r   )rY   rT   rF   r3   r   create_ipython_shortcuts3   s   








	
	






		r   c              
   C   sf   |  t|jd |j }z||}| | W n. ty` } z| | W Y d }~n
d }~0 0 d S r   )r|   r@   r   r   reformat_handlerr#   	Exception)bufferr   r3   r   formatted_textr   r   r   r   r/     s    
r/   c                    s    fdd}|S )Nc           	         s  | j }|j}|jr6|jj}|r*|| n|  dS |jdkrH|j}n|jd|j } 	|\}}|j|jd }d}|
 st||  d}|js|j|j|  ksЈ jr|d|  n
|d dS |dkr|jr|st||  |  n" jr|d|  n
|d dS )zCWhen the user presses return, insert a newline or execute the code.Nr?   FTr   
incomplete)r   r   complete_statecurrent_completionapply_completioncancel_completion
line_countr   r   check_completer!   r/   on_last_linecursor_position_rowempty_line_count_at_the_end
autoindentr#   accept_handlerr0   )	r&   r'   r(   ccZ
check_textstatusindentr)   Zreformattedr2   r   r   newline_or_execute  s>    


z4newline_or_execute_outer.<locals>.newline_or_executer   )r3   r   r   r2   r   r     s    .r   c                 C   s   | j   dS )z
    Control-P in vi edit mode on readline is history next, unlike default prompt toolkit.

    If completer is open this still select previous completion.
    N)r   auto_upr1   r   r   r   r     s    r   c                 C   s   | j   dS )z
    Control-N in vi edit mode on readline is history previous, unlike default prompt toolkit.

    If completer is open this still select next completion.
    N)r   	auto_downr1   r   r   r   r     s    r   c                 C   s   | j }|jr|  d S r   )r   r   r   r&   r'   r   r   r   r     s    r   c                 C   s"   | j }|jr|  n|  d S r   )r   r   r   resetr   r   r   r   r     s    
r   c                 C   s(   | j jjr| j   n| jjt d S r   )r   r   r   r   rI   layoutfocusr   r1   r   r   r   r     s    
r   c                 C   s   | j   d S r   )rI   suspend_to_backgroundr1   r   r   r   r   
  s    r   c                 C   s2   t tdd}|dur$tdtj n
td dS )z
    On platforms that support SIGQUIT, send SIGQUIT to the current process.
    On other platforms, just exit the process with a message.
    SIGQUITNr   ZQuit)r   r8   oskillr   r   exit)r&   Zsigquitr   r   r   r     s    r   c                 C   s   | j d d S )N    )r   r#   r1   r   r   r   r     s    r   c                 C   s^   t jdtdd | j}|jj}|jdd |jdd |jj}||krZ|| }|j|d dS )	z
    DEPRECATED since IPython 6.0

    See :any:`newline_autoindent_outer` for a replacement.

    Preserve margin and cursor position when using
    Control-O to insert a newline in EMACS mode
    z`newline_with_copy_margin(event)` is deprecated since IPython 6.0. see `newline_autoindent_outer(shell)(event)` for a replacement.rp   )
stacklevelT)copy_marginr?   )countN)	warningswarnDeprecationWarningr   r   cursor_position_colnewline	cursor_upry   )r&   r'   Zcursor_start_posZcursor_end_posZpos_diffr   r   r   newline_with_copy_margin  s    
r   ).N)returnc                    s    fdd}|S )a  
    Return a function suitable for inserting a indented newline after the cursor.

    Fancier version of deprecated ``newline_with_copy_margin`` which should
    compute the correct indentation of the inserted line. That is to say, indent
    by 4 extra space after a function definition, class definition, context
    manager... And dedent by 4 space after ``pass``, ``return``, ``raise ...``.
    c                    sZ   | j }|j}|jr|  |jd|j d } |\}}|jdd|pJd  dd dS )z9insert a newline after the cursor indented appropriately.Nr    r   F)move_cursor)r   r   r   r   r   r   r   r#   )r&   r'   r(   r   rf   r   inputsplitterr   r   newline_autoindent<  s    z4newline_autoindent_outer.<locals>.newline_autoindentr   )r   r  r   r   r   r   2  s    
r   c                 C   s   | j j  d S r   )rI   r   open_in_editorr1   r   r   r   r   J  s    r   r~   )TryNext)ClipboardEmptywin32_clipboard_gettkinter_clipboard_getc                 C   sn   z
t  }W nJ tyB   z
t }W n ttfy<   Y Y d S 0 Y n tyT   Y d S 0 | j|dd d S )N	r   )r  r  r  r  r   r#   r^   )r&   r   r   r   r   r   T  s    

r   )9__doc__r   r8   r   rQ   r   typingr   Z"prompt_toolkit.application.currentr   prompt_toolkit.enumsr   r   prompt_toolkit.filtersr   r   r   r	   r
   r   r   Z.prompt_toolkit.key_binding.bindings.completionr   Zprompt_toolkit.key_bindingr   Z#prompt_toolkit.key_binding.bindingsr   r$   #prompt_toolkit.key_binding.vi_stater   r   IPython.utils.decoratorsr   r   r+   r   r/   r   r   r   r   r   r   r   r   r   r   r   r   r   IPython.core.errorr  ZIPython.lib.clipboardr  r  r  r   r   r   r   r   <module>   sP   $  w	3		
