B
    \                 @   sN  d 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 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#ed d! Z$ed" d#d$d%Z%d&d' Z&ej'd(krJdd)l(m)Z) dd*l*m+Z+m,Z,m-Z- ed+d, Z.dS )-zU
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)undocc              C   s   t  jjj} |  p|  S )N)r   current_bufferdocumentZcurrent_line_before_cursorisspace)Zbefore r   9lib/python3.7/site-packages/IPython/terminal/shortcuts.pycursor_in_leading_ws   s    r   c             C   s  t  }ttB }t| ddr&| | }nt| }|j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 | jdkr|jdttt	 @ |@ t @ dt tjdkr|jdttt @ dt |S )z8Set up the prompt_toolkit keyboard shortcuts for IPythonhandle_returnNZenter)filterzc-\zc-pzc-nzc-gzc-cc               S   s
   t tdS )NSIGTSTP)hasattrsignalr   r   r   r   <lambda>>   s    z*create_ipython_shortcuts.<locals>.<lambda>zc-zZtabzc-of2Zreadlinelikezc-iwin32zc-v)r   r	   r
   getattrr   newline_or_execute_outeraddr   r   r   
force_exit'previous_history_or_previous_completionnext_history_or_next_completionr   dismiss_completionreset_bufferr   reset_search_bufferr   suspend_to_bgr   indent_buffernewline_autoindent_outerZinput_transformer_manageropen_input_in_editorZdisplay_completionsr   sysplatformr   	win_paste)shellZkbZinsert_modeZreturn_handlerZsupports_suspendr   r   r   create_ipython_shortcuts   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
s|j|j|  ks jr|d|  n
|d dS |dkr|jr|  n  jr|d|  n
|d dS )zCWhen the user presses return, insert a newline or execute the code.N   
Z
incomplete)r   r   complete_stateZcurrent_completionZapply_completioncancel_completionZ
line_counttextcursor_positioncheck_completeZon_last_lineZcursor_position_rowZempty_line_count_at_the_endZ
autoindentinsert_textZaccept_handlerZvalidate_and_handle)eventbdZccZ
check_textZstatusindent)r.   r   r   newline_or_executeZ   s.    


z4newline_or_execute_outer.<locals>.newline_or_executer   )r.   r<   r   )r.   r   r   Y   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   Zauto_up)r8   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   Z	auto_down)r8   r   r   r   r#      s    r#   c             C   s   | j }|jr|  d S )N)r   r2   r3   )r8   r9   r   r   r   r$      s    r$   c             C   s"   | j }|jr|  n|  d S )N)r   r2   r3   reset)r8   r9   r   r   r   r%      s    
r%   c             C   s(   | j jjr| j   n| jjt d S )N)r   r   r4   r=   appZlayoutZfocusr   )r8   r   r   r   r&      s    
r&   c             C   s   | j   d S )N)r>   Zsuspend_to_background)r8   r   r   r   r'      s    r'   c             C   s   t d dS )z3
    Force exit (with a non-zero return value)
    ZQuitN)r+   exit)r8   r   r   r   r!      s    r!   c             C   s   | j d d S )Nz    )r   r7   )r8   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.   )
stacklevelT)Zcopy_marginr0   )countN)	warningswarnDeprecationWarningr   r   Zcursor_position_colnewlineZ	cursor_upZcursor_right)r8   r9   Zcursor_start_posZcursor_end_posZpos_diffr   r   r   newline_with_copy_margin   s    

rG   ).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.Nr1    r   F)Zmove_cursor)r   r   r2   r3   r4   r5   r6   r7   )r8   r9   r:   r4   _r;   )inputsplitterr   r   newline_autoindent   s    z4newline_autoindent_outer.<locals>.newline_autoindentr   )rK   rL   r   )rK   r   r)      s    
r)   c             C   s   d| j j_| j j  d S )Nz.py)r>   r   Ztempfile_suffixZopen_in_editor)r8   r   r   r   r*      s    
r*   r   )TryNext)ClipboardEmptywin32_clipboard_gettkinter_clipboard_getc             C   sn   y
t  }W nJ tk
rB   y
t }W n ttfk
r<   d S X Y n tk
rT   d S X | j|dd d S )N	z    )rO   rM   rP   rN   r   r7   replace)r8   r4   r   r   r   r-      s    


r-   )/__doc__rC   r   r+   typingr   Z"prompt_toolkit.application.currentr   Zprompt_toolkit.enumsr   r   Zprompt_toolkit.filtersr   r   r   r	   r
   r   r   Z.prompt_toolkit.key_binding.bindings.completionr   Zprompt_toolkit.key_bindingr   ZIPython.utils.decoratorsr   r   r/   r   r"   r#   r$   r%   r&   r'   r!   r(   rG   r)   r*   r,   ZIPython.core.errorrM   ZIPython.lib.clipboardrN   rO   rP   r-   r   r   r   r   <module>   s:   $:)		