
    ړil(                       U 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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 erd dlmZ ddlm Z  g dZ!eZ"de#d<   edd       Z$ddZ%ddZ&ddZ'ddZ(y)    )annotations)TYPE_CHECKING)get_app)Buffer)Document)DEFAULT_BUFFER)	Conditionemacs_insert_mode
emacs_mode	has_focushas_selectionvi_insert_mode)KeyBindings)get_by_name)KeyPressEvent)Keys   )document_is_multiline_python)	TypeAlias)PythonInput)load_python_bindingsload_sidebar_bindingsload_confirm_exit_bindingsr   Ec                     t               j                  } | j                  j                  }t	        | j
                  xr | xs |j                               S )a+  
    When the 'tab' key is pressed with only whitespace character before the
    cursor, do autocompletion. Otherwise, insert indentation.

    Except for the first character at the first line. Then always do a
    completion. It doesn't make sense to start the first line with
    indentation.
    )r   current_bufferdocumentcurrent_line_before_cursorbooltextisspace)bbefore_cursors     </tmp/pip-target-20744g9h/lib/python/ptpython/key_bindings.pytab_should_insert_whitespacer%   &   sE     		  AJJ99MI-H1F1F1HJJ    c                    t               }t         fd      }|j                  } |d      dd       } |d      d fd       }  |d      t        d              |d      d fd	       } |d
      d fd       } |d      d fd       } |d      d fd       } |d| t         z  t
        z        dd       }t        d fd       } |d| t         z  t        t        z  z  t        t              z  | z         |t        j                  t        j                  | t        z        dd              } |d| t         z  t        t        z  z  t        t              z  |z        d fd       } |d| t         j                        z  t        d       z        d fd       } |dt         j                              dd       }|S )z
    Custom key bindings.
    c                      j                   S Nshow_sidebarpython_inputs   r$   <lambda>z&load_python_bindings.<locals>.<lambda><       (A(A r&   zc-lc                L    | j                   j                  j                          y)zZ
        Clear whole screen and render again -- also when the sidebar is visible.
        N)apprendererclearevents    r$   _zload_python_bindings.<locals>._?   s    
 			  "r&   zc-zc                T    j                   r| j                  j                          yy)z
        Suspend.
        N)enable_system_bindingsr1   suspend_to_backgroundr5   r-   s    r$   r6   zload_python_bindings.<locals>._F   s#    
 ..II++- /r&   zc-wzbackward-kill-wordf2c                    j                    _         j                   r:| j                  j                  j                  j                  j
                         y| j                  j                  j                          y)z$
        Show/hide sidebar.
        N)r+   r1   layoutfocusptpython_layoutsidebar
focus_lastr:   s    r$   r6   zload_python_bindings.<locals>._R   sY    
 )5(A(A$A!$$II""<#?#?#G#GHII'')r&   f3c                &    j                          y)z*
        Select from the history.
        N)enter_historyr:   s    r$   r6   zload_python_bindings.<locals>._]   s    
 	""$r&   f4c                *    j                    _         y)z3
        Toggle between Vi and Emacs mode.
        N)vi_moder:   s    r$   r6   zload_python_bindings.<locals>._d   s    
 $0#7#77r&   f6c                *    j                    _         y)z,
        Enable/Disable paste mode.
        N)
paste_moder:   s    r$   r6   zload_python_bindings.<locals>._k   s    
 '3&=&="=r&   tabfilterc                N    | j                   j                  j                  d       y)zS
        When tab should insert whitespace, do that instead of completion.
        z    N)r1   r   insert_textr4   s    r$   r6   zload_python_bindings.<locals>._r   s     			  ,,V4r&   c                 B    t         j                  j                        S r)   )r   default_bufferr   r,   s   r$   is_multilinez*load_python_bindings.<locals>.is_multiline{   s    +L,G,G,P,PQQr&   enterc                    | j                   }|j                         r\t        |j                  j	                         t        |j                  j	                                     |_        |j                          yy)z7
        Accept input (for single line input).
        r    cursor_positionN)r   validater   r    rstriplenr   validate_and_handle)r5   r"   s     r$   r6   zload_python_bindings.<locals>._   sW       ::< "VV]]_c!&&--/6JAJ !!# r&   c                   | j                   }j                  xs d}d	d}j                  r|j                  d       y ||      r|j                  j
                  j                  dd      j                  d|dz
  z        rm|j                         r\t        |j
                  j                         t        |j
                  j                                     |_        |j                          yyt        |       y)
z
        Behaviour of the Enter key.

        Auto indent after newline/Enter.
        (When not in Vi navigation mode, and when multiline is enabled.)
        i'  c                h    | j                   j                  }|dk(  xs |j                         xr d|vS )ziwe consider the cursor at the end when there is no text after
            the cursor, or only whitespace. 
)r   text_after_cursorr!   )r"   r    s     r$   
at_the_endz3load_python_bindings.<locals>._.<locals>.at_the_end   s2     :://D2:F$,,."ET5EFr&   r^    r]   r   rU   N)r"   r   returnr   )r   accept_input_on_enterrJ   rO   r   r    replaceendswithrW   r   rX   rY   rZ   auto_newline)r5   r"   empty_lines_requiredr`   r-   s       r$   r6   zload_python_bindings.<locals>._   s       +AAJU	G ""MM$]qzz66sB?HH(1,- 

 zz|%#affmmo:N
 %%'  Or&   c-dc                 @    t               j                  j                   S r)   )r   r   r     r&   r$   r.   z&load_python_bindings.<locals>.<lambda>   s    	((--- r&   c                    j                   rAd_        j                  j                  j	                  j
                  j                         y| j                  j                  t               y)zC
        Override Control-D exit, to ask for confirmation.
        T)	exceptionN)	confirm_exitshow_exit_confirmationr1   r=   r>   r?   exit_confirmationexitEOFErrorr:   s    r$   r6   zload_python_bindings.<locals>._   sU     $$ 37L/##)),,>> IINNXN.r&   c-cc                F    | j                   j                  t        d       y)z&Abort when Control-C has been pressed.zclass:abortingrl   styleN)r1   rp   KeyboardInterruptr4   s    r$   r6   zload_python_bindings.<locals>._   s     			!2:JKr&   r5   r   rb   Nonerb   r   )r   r	   addr   r   r%   r   r
   r   r   r   EscapeEnterr   rQ   )r-   bindingssidebar_visiblehandler6   rR   s   `     r$   r   r   6   sJ    }H ABO\\FE]# # E]. . F5M+234D\* * D\% % D\8 8 D\> > &-7:VV55 R R .--/ N
#$ -	 DKK_,<z,IJ$ K$ .--/ N
#$ 	  D 
L//
01
.

	/	/ E)L$?$?@AL BL Or&   c                ,    t               }|j                  }t         fd      } |d|       |d|       |d|      d fd                     } |d|       |d|       |d	|      d fd
                     } |d|       |d|       |d|      d fd                     } |d|       |d|      d fd              } |d|       |d|       |d|       |d|       |d|      d fd                                   }|S )z:
    Load bindings for the navigation in the sidebar.
    c                      j                   S r)   r*   r,   s   r$   r.   z'load_sidebar_bindings.<locals>.<lambda>   r/   r&   uprL   zc-pkc                H    j                   dz
  j                  z  _         y)zGo to previous option.r   Nselected_option_indexoption_countr:   s    r$   r6   z load_sidebar_bindings.<locals>._   &     ..2%%.&*r&   downzc-njc                H    j                   dz   j                  z  _         y)zGo to next option.r   Nr   r:   s    r$   r6   z load_sidebar_bindings.<locals>._   r   r&   rightlra   c                >    j                   }|j                          y)z%Select next value for current option.N)selected_optionactivate_nextr5   optionr-   s     r$   r6   z load_sidebar_bindings.<locals>._   s    
 --r&   lefthc                >    j                   }|j                          y)z)Select previous value for current option.N)r   activate_previousr   s     r$   r6   z load_sidebar_bindings.<locals>._  s     --  "r&   rr   rh   rS   escapec                \    d_         | j                  j                  j                          y)zHide sidebar.FN)r+   r1   r=   rA   r:   s    r$   r6   z load_sidebar_bindings.<locals>._  s#     %*!		##%r&   rw   )r   rz   r	   )r-   r}   r   r~   r6   s   `    r$   r   r      sQ    }H\\F ABOD)E/*C(& ) + *& F?+E/*C(& ) + ,& GO,C(C( ) ) -
 F?+C(# ) ,#
 E/*E/*E/*GO,H_-& . - + + +
&
 Or&   c                    t               }|j                  }t         fd      } |d|       |d|       |d|       |d|      d	d                            } |t        j                  |      d	 fd       }|S )
zH
    Handle yes/no key presses when the exit confirmation is shown.
    c                      j                   S r)   )rn   r,   s   r$   r.   z,load_confirm_exit_bindings.<locals>.<lambda>  s    \-P-P r&   yrL   YrS   rh   c                F    | j                   j                  t        d       y)z
        Really quit.
        zclass:exitingrt   N)r1   rp   rq   r4   s    r$   r6   z%load_confirm_exit_bindings.<locals>._!  s     			Ar&   c                \    d_         j                  j                  j                          y)z
        Cancel exit.
        FN)rn   r1   r=   focus_previousr:   s    r$   r6   z%load_confirm_exit_bindings.<locals>._+  s%    
 /4+..0r&   rw   )r   rz   r	   r   Any)r-   r}   r   confirmation_visibler6   s   `    r$   r   r     s     }H\\F$%PQC,-C,-G01E./B 0 2 . .B DHH121 31 Or&   c                   | j                   }| j                  j                  r	 |d       y| j                  j                  j	                         } |d       |j	                         j                  d      }|r|dd n|}|D ]  }|j                         r	 ||        n |dd dk(  rt        d      D ]
  } |d        yy)zG
    Insert \n at the cursor position. Also add necessary padding.
    r^   z pass   N:ra   )rO   r   current_line_after_cursorr   rX   re   r!   range)bufferrO   current_lineunindentcurrent_line2cxs          r$   rf   rf   6  s     $$K00D AAHHJD  &&(11': -5QR(, 	Ayy{A		 #1X !C ! $r&   Nry   )r-   r   rb   r   )r   r   rb   rx   ))
__future__r   typingr   prompt_toolkit.applicationr   prompt_toolkit.bufferr   prompt_toolkit.documentr   prompt_toolkit.enumsr   prompt_toolkit.filtersr	   r
   r   r   r   r   prompt_toolkit.key_bindingr   2prompt_toolkit.key_binding.bindings.named_commandsr   (prompt_toolkit.key_binding.key_processorr   prompt_toolkit.keysr   utilsr   typing_extensionsr   r-   r   __all__r   __annotations__r%   r   r   r   rf   rj   r&   r$   <module>r      s{    "   . ( , /  3 J B $ /+) 9  K KhV4n<!r&   