
    BNe4(                        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 g dZeZ edd            Z!ddZ"ddZ#ddZ$d dZ%dS )!    )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)PythonInput)load_python_bindingsload_sidebar_bindingsload_confirm_exit_bindingsreturnboolc                     t                      j        } | j        j        }t	          | j        o| p|                                          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_cursorr   textisspace)bbefore_cursors     /home/feoh/.cache/pypoetry/virtualenvs/veilid-python-play-bIboZq2W-py3.11/lib/python3.11/site-packages/ptpython/key_bindings.pytab_should_insert_whitespacer$   $   sC     			 AJ9MI-H1F1F1H1HJJJ    python_inputr   r   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r&   s   r#   <lambda>z&load_python_bindings.<locals>.<lambda>:   
    (A r%   zc-leventEr   Nonec                B    | j         j                                         dS )zZ
        Clear whole screen and render again -- also when the sidebar is visible.
        N)apprendererclearr/   s    r#   _zload_python_bindings.<locals>._=   s!    
 		  """""r%   zc-zc                L    j         r| j                                         dS dS )z
        Suspend.
        N)enable_system_bindingsr3   suspend_to_backgroundr/   r&   s    r#   r7   zload_python_bindings.<locals>._D   s4    
 . 	.I++-----	. 	.r%   zc-wzbackward-kill-wordf2c                    j          _         j         r+| j        j                            j        j                   dS | j        j                                         dS )z$
        Show/hide sidebar.
        N)r+   r3   layoutfocusptpython_layoutsidebar
focus_lastr;   s    r#   r7   zload_python_bindings.<locals>._P   s`    
 )5(A$A!$ 	*I""<#?#GHHHHHI'')))))r%   f3c                0                                      dS )z*
        Select from the history.
        N)enter_historyr;   s    r#   r7   zload_python_bindings.<locals>._[   s    
 	""$$$$$r%   f4c                "    j          _         dS )z3
        Toggle between Vi and Emacs mode.
        N)vi_moder;   s    r#   r7   zload_python_bindings.<locals>._b   s    
 $0#77r%   f6c                "    j          _         dS )z,
        Enable/Disable paste mode.
        N)
paste_moder;   s    r#   r7   zload_python_bindings.<locals>._i   s    
 '3&="=r%   tabfilterc                D    | j         j                            d           dS )zS
        When tab should insert whitespace, do that instead of completion.
        z    N)r3   r   insert_textr6   s    r#   r7   zload_python_bindings.<locals>._p   s#     		 ,,V44444r%   r   c                 6    t           j        j                  S r)   )r   default_bufferr   r,   s   r#   is_multilinez*load_python_bindings.<locals>.is_multiliney   s    +L,G,PQQQr%   enterc                
   | j         }|                                rgt          |j                                        t          |j                                                            |_        |                                 dS dS )z7
        Accept input (for single line input).
        r   cursor_positionN)r   validater   r   rstriplenr   validate_and_handle)r/   r!   s     r#   r7   zload_python_bindings.<locals>._}   sy      ::<< 	$ "V]]__c!&--//6J6J  AJ !!#####	$ 	$r%   c                   | j         }j        pd}dd}j        r|                    d           dS  ||          r|j        j                            dd	                              d|d
z
  z            r}|                                rgt          |j        
                                t          |j        
                                                    |_        |                                 dS dS t          |           dS )z
        Behaviour of the Enter key.

        Auto indent after newline/Enter.
        (When not in Vi navigation mode, and when multiline is enabled.)
        i'  r!   r   r   r   c                V    | j         j        }|dk    p|                                o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   s/     :/D2:F$,,.."ET5EFr%   r_    r^   r   rV   N)r!   r   r   r   )r   accept_input_on_enterrK   rP   r   r   replaceendswithrX   r   rY   rZ   r[   auto_newline)r/   r!   empty_lines_requiredra   r&   s       r#   r7   zload_python_bindings.<locals>._   s     +AJU	G 	G 	G 	G " 	MM$Z]] 	qz66sB??HH(1,- 
  
 	
 zz|| (%#afmmoo:N:N  
 %%'''''( ( OOOOOr%   c-dc                 4    t                      j        j         S r)   )r   r   r    r%   r#   r-   z&load_python_bindings.<locals>.<lambda>   s    		(-- r%   c                    j         r2d_        j        j                            j        j                   dS | j                            t                     dS )zC
        Override Control-D exit, to ask for confirmation.
        T)	exceptionN)	confirm_exitshow_exit_confirmationr3   r>   r?   r@   exit_confirmationexitEOFErrorr;   s    r#   r7   zload_python_bindings.<locals>._   si     $ 	/ 37L/#)),>     INNXN.....r%   c-cc                H    | j                             t          d           dS )z&Abort when Control-C has been pressed.zclass:abortingrl   styleN)r3   rp   KeyboardInterruptr6   s    r#   r7   zload_python_bindings.<locals>._   s$     		!2:JKKKKKr%   r/   r0   r   r1   r   r   )r   r	   addr   r   r$   r   r
   r   r   r   EscapeEnterr   rR   )r&   bindingssidebar_visiblehandler7   rS   s   `     r#   r   r   4   s    }}H A A A ABBO\FVE]]# # # ]# VE]]. . . . . ]. FF5MM+233444VD\\* * * * * \* VD\\% % % % % \% VD\\8 8 8 8 8 \8 VD\\> > > > > \> V&-7:VV  5 5 5 5 R R R R R YR V.--/ N
#
#$ -	   VDK_,<z,IJJJ$ $ $ KJ $ V.--/ N
#
#$ 	             D V
L/
0
01
. .
 

	 	 	/ / / / /	 	/ VE)L$?@@AAAL L L BA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%   uprM   zc-pkr/   r0   r   r1   c                6    j         dz
  j        z  _         dS )zGo to previous option.r   Nselected_option_indexoption_countr;   s    r#   r7   z load_sidebar_bindings.<locals>._   &     .2%.&***r%   downzc-njc                6    j         dz   j        z  _         dS )zGo to next option.r   Nr   r;   s    r#   r7   z load_sidebar_bindings.<locals>._   r   r%   rightlrb   c                >    j         }|                                 dS )z%Select next value for current option.N)selected_optionactivate_nextr/   optionr&   s     r#   r7   z load_sidebar_bindings.<locals>._   s%    
 -r%   lefthc                >    j         }|                                 dS )z)Select previous value for current option.N)r   activate_previousr   s     r#   r7   z load_sidebar_bindings.<locals>._  s%     -  """""r%   rr   rh   rT   escapec                R    d_         | j        j                                         dS )zHide sidebar.FN)r+   r3   r>   rB   r;   s    r#   r7   z load_sidebar_bindings.<locals>._	  s*     %*!	##%%%%%r%   rw   )r   ry   r	   )r&   r|   r~   r}   r7   s   `    r#   r   r      sq    }}H\F A A A ABBOVD)))VE/***VC(((& & & & & )( +* *)& VF?+++VE/***VC(((& & & & & )( +* ,+& VGO,,,VC(((VC(((     )( )( -,
 VF?+++VC(((# # # # # )( ,+#
 VE/***VE/***VE/***VGO,,,VH_---& & & & & .- -, +* +* +*
&
 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 r%   yrM   YrT   rh   r/   r0   r   r1   c                H    | j                             t          d           dS )z
        Really quit.
        zclass:exitingrt   N)r3   rp   rq   r6   s    r#   r7   z%load_confirm_exit_bindings.<locals>._  s"     		AAAAAr%   c                R    d_         j        j                                         dS )z
        Cancel exit.
        FN)rn   r3   r>   focus_previousr;   s    r#   r7   z%load_confirm_exit_bindings.<locals>._)  s+    
 /4+..00000r%   rw   )r   ry   r	   r   Any)r&   r|   r~   confirmation_visibler7   s   `    r#   r   r     s     }}H\F$%P%P%P%PQQVC,---VC,---VG0111VE.///B B B 0/ 21 .- .-B VDH12221 1 1 1 1 321 Or%   bufferr   r1   c                   | j         }| j        j        r |d           dS | j        j                                        } |d           |                                                    d          }|r
|dd         n|}|D ]"}|                                r ||           " |dd         dk    rt          d          D ]} |d           dS dS )zG
    Insert \n at the cursor position. Also add necessary padding.
    r_   z pass   N:rb   )rP   r   current_line_after_cursorr   rY   re   r    range)r   rP   current_lineunindentcurrent_line2cxs          r#   rf   rf   4  s    $K0 !D AHHJJD  &&((11':: -5FQRR((, 	 	Ayy{{ A ##1XX ! !C     $#! !r%   Nrx   )r&   r   r   r   )r   r   r   r1   )&
__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   r&   r   __all__r0   r$   r   r   r   rf   rj   r%   r#   <module>r      s   " " " " " "             . . . . . . ( ( ( ( ( ( , , , , , , / / / / / /                3 2 2 2 2 2 J J J J J J B B B B B B $ $ $ $ $ $ / / / / / / *))))))    K K K Kh h h hV4 4 4 4n   <! ! ! ! ! !r%   