o
    ˷eC                     @   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,e d$d% Z-d&ed' fd(d)Z.d*d+ Z/ej0d,krdd-l1m2Z2 dd.l3m4Z4m5Z5m6Z6 e d/d0 Z7dS dS )1zU
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   Q/var/www/ideatree/venv/lib/python3.10/site-packages/IPython/terminal/shortcuts.pycursor_in_leading_ws   s   r   c                    s  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}ttB tt@ }i i fdd}fdd}	|jd||@ |	d@ ddd }
|jd||@ |	d@ ddd }
|jd||@ |	d@ dd d }
|jd!||@ |d"@ |	d@ dd#d }
|jd$||@ |d%@ |	d@ dd&d }
|jd||@ |d'@ dd(d }
|jd||@ |d'@ dd)d }
|jd||@ |d'@ dd*d }
|jd+||@ |	d,@ d|jd-||@ |	d.@ d|jd/||@ |	d0@ d|jd!||@ |	d1@ d|jd$||@ |	d2@ dd3d }
|jd4||d5@ |@ |	d,@ d|jd4||d6@ |@ |	d.@ d|jd4||d7@ |@ |	d0@ d|jd4||d8@ |@ |	d1@ d|jd4||d9@ |@ |	d2@ dd:d }
jd;kr%|jd<ttt	 @ |@ t @ dt tjd=kr9|jd>ttt @ dt tfd?d@}ttt@ }dAdB  |jdCtt|t B @ d fdDd}
|jdE||@ d fdFd}
|jdG|ddHd }
|jddI||@ ddJd }
tj tj!tj"tj#tj$tj%dK}|& D ]\}}|j|||@ d| qtj'tj'tj(tj)tj*tj#tj+tj,tj-tj.dL
}|& D ]\}}|j|dM||@ i| qƇfdNdO}dPdQ }j/dRkrj0rt1j2t3_4t5||t3_6|S )Sz8Set 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eventshellr   r   reformat_and_execute3   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>K   s   
 z*create_ipython_shortcuts.<locals>.<lambda>zc-ztabzc-of2c                          j S r   )
auto_matchr   r#   r   r   r.   Y      z,create_ipython_shortcuts.<locals>.auto_matchc                    H   z|  W S  t y   Y nw t|   fdd}t|}|| < |S )Nc                        t  } t | jjjS r   )r   boolmatchr   r   r   appmr   r   _preceding_texth      zIcreate_ipython_shortcuts.<locals>.preceding_text.<locals>._preceding_textKeyErrorrecompiler   )patternr8   	condition)_preceding_text_cacher6   r   preceding_texta      

z0create_ipython_shortcuts.<locals>.preceding_textc                    r0   )Nc                     r1   r   )r   r2   r3   r   r   current_line_after_cursorr4   r6   r   r   _following_textw   r9   zIcreate_ipython_shortcuts.<locals>.following_text.<locals>._following_textr:   )r>   rD   r?   )_following_text_cacher6   r   following_textp   rB   z0create_ipython_shortcuts.<locals>.following_text(z	[,)}\]]|$c                 S      | j d | j   d S )N()r   insert_textcursor_leftr!   r   r   r   _      z#create_ipython_shortcuts.<locals>._[c                 S   rH   )N[]rJ   r!   r   r   r   rM      rN   {c                 S   rH   )N{}rJ   r!   r   r   r   rM      rN   "z^([^"]+|"[^"]*")*$c                 S   rH   )Nz""rJ   r!   r   r   r   rM         'z^([^']+|'[^']*')*$c                 S   rH   )Nz''rJ   r!   r   r   r   rM      rT   z.*(r|R)[\"'](-*)$c                 S   H   t d| jjj}|dpd}| jd|  | jt|d  d S )N.*(r|R)[\"'](-*)    rI      	r<   r3   r   r   r   grouprK   rL   lenr"   matchesdashesr   r   r   rM         c                 S   rV   )NrW   rX   rY   rP   rZ   r[   r^   r   r   r   rM      ra   c                 S   rV   )NrW   rX   rY   rR   rZ   r[   r^   r   r   r   rM      ra   )z^\)]z^\]}z^\}z^"z^'c                 S      | j   d S r   )r   cursor_rightr!   r   r   r   rM         	backspacez.*\($z.*\[$z.*\{$z.*"$z.*'$c                 S   s   | j   | j   d S r   )r   deletedelete_before_cursorr!   r   r   r   rM      s   
$readlinelikezc-iwin32zc-vc                      r-   r   ) emacs_bindings_in_vi_insert_moder   r#   r   r   ebivim  r/   z(create_ipython_shortcuts.<locals>.ebivimc                 S   s8   | j }|j}|d ur|jr||j d S t|  d S r   )r   
suggestiontextrK   ncend_of_liner"   bro   r   r   r   _apply_autosuggest  s
   z4create_ipython_shortcuts.<locals>._apply_autosuggestendc                        |  d S r   r   r!   ru   r   r   rM        c-ec                    rw   r   r   r!   rx   r   r   rM     ry   zc-fc                 S   s.   | j }|j}|r||j d S t|  d S r   )r   ro   rK   rp   rq   forward_charrs   r   r   r   rM     s
   fc                 S   sJ   | j }|j}|rtd|j}|tdd |D d d S t|  d S )Nz(\S+\s+)c                 s   s    | ]}|r|V  qd S r   r   ).0xr   r   r   	<genexpr>.  s    z6create_ipython_shortcuts.<locals>._.<locals>.<genexpr>rY   )	r   ro   r<   splitrp   rK   nextrq   forward_word)r"   rt   ro   tr   r   r   rM   (  s   )zc-azc-bzc-kzc-wzc-yzc-_)
)c-xrz   )r   e)r&   rt   )r&   c)r&   d)r&   h)r&   l)r&   u)r&   y)r&   .r   c                    s   t  } j|_ j|_| jS r   )r   ttimeoutlen
timeoutlen_input_mode)selfr5   r#   r   r   get_input_modeR  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 )NrX         z[{} q)
r   
NAVIGATIONREPLACEgetformatsysstdoutwriteflushr   )r   modeshapecursorr   r   r   set_input_modeY  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_pasterq   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)r$   kbinsert_modereturn_handlerr%   supports_suspendr.   focused_insertrA   rF   rM   rn   focused_insert_vikey_cmd_dictkeycmdkeys_cmd_dictkeysr   r   r   )ru   rE   r@   r$   r   create_ipython_shortcuts#   s  





	
	




		r   c              
   C   sf   |  t|jd |j }z||}| | W d S  ty2 } z| | W Y d }~d S d }~ww r   )rj   r]   rp   cursor_positionreformat_handlerrK   	Exception)bufferr   r$   rp   formatted_textr   r   r   r   r   i  s   
r   c                        fdd}|S )Nc           	         s$  | j }|j}|jr|jj}|r|| dS |  dS |jdkr%|j}n|jd|j } 	|\}}|j|jd }d}|
 sJt||  d}|jsj|j|j|  ksj jrc|d|  dS |d dS |dkr|jr|syt||  |  dS  jr|d|  dS |d dS )zCWhen the user presses return, insert a newline or execute the code.NrZ   FT

incomplete)r   r   complete_statecurrent_completionapply_completioncancel_completion
line_countrp   r   check_completestripr   on_last_linecursor_position_rowempty_line_count_at_the_end
autoindentrK   accept_handlerr    )	r"   rt   r   cc
check_textstatusindentafter_cursorreformattedr#   r   r   newline_or_executet  s@   


z4newline_or_execute_outer.<locals>.newline_or_executer   )r$   r   r   r#   r   r   r  s   .r   c                 C      | 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_upr!   r   r   r   r     rg   r   c                 C   r   )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_downr!   r   r   r   r     rg   r   c                 C   s   | j }|jr|  d S d S r   )r   r   r   r"   rt   r   r   r   r     s   r   c                 C   s$   | j }|jr|  d S |  d S r   )r   r   r   resetr   r   r   r   r     s   r   c                 C   s*   | j jjr| j   d S | jjt d S r   )r   r   rp   r   r5   layoutfocusr   r!   r   r   r   r     s   
r   c                 C   re   r   )r5   suspend_to_backgroundr!   r   r   r   r     s   r   c                 C   s4   t tdd}|durtdtj dS 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   Quit)r   r)   oskillr   r   exit)r"   sigquitr   r   r   r     s   r   c                 C   s   | j d d S )N    )r   rK   r!   r   r   r   r        r   c                 C   sb   t jdtdd | j}|jj}|jdd |jdd |jj}||kr/|| }|j|d dS 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.rX   )
stacklevelT)copy_marginrZ   )countN)	warningswarnDeprecationWarningr   r   cursor_position_colnewline	cursor_uprf   )r"   rt   cursor_start_poscursor_end_pospos_diffr   r   r   newline_with_copy_margin  s   
r	  return).Nc                    r   )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|p%d  dd dS )z9insert a newline after the cursor indented appropriately.Nr    r   F)move_cursor)r   r   r   r   rp   r   r   rK   )r"   rt   r   rp   rM   r   inputsplitterr   r   newline_autoindent  s   z4newline_autoindent_outer.<locals>.newline_autoindentr   )r  r  r   r  r   r     s   
r   c                 C   s   | j j  d S r   )r5   r   open_in_editorr!   r   r   r   r     r   r   rl   )TryNext)ClipboardEmptywin32_clipboard_gettkinter_clipboard_getc                 C   sn   zt  }W n% ty!   zt }W n ttfy   Y Y d S w Y n
 ty*   Y d S w | j|dd d S )N	r   )r  r  r  r  r   rK   replace)r"   rp   r   r   r   r     s   

r   )8__doc__r   r)   r   r<   r   typingr   "prompt_toolkit.application.currentr   prompt_toolkit.enumsr   r   prompt_toolkit.filtersr   r   r   r	   r
   r   r   .prompt_toolkit.key_binding.bindings.completionr   prompt_toolkit.key_bindingr   #prompt_toolkit.key_binding.bindingsr   rq   #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   IPython.core.errorr  IPython.lib.clipboardr  r  r  r   r   r   r   r   <module>   sR    $  H	3		

