U
    dL                     @  s  d dl mZ d dlmZmZ d dlmZ d dlmZm	Z	m
Z
 d dlmZ d dlmZmZmZ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 ddl m!Z!m"Z"m#Z# ddl$m%Z% dddgZ&eZ'ddddZ(ddddZ)ddddZ*dS )    )annotations)DictUnion)get_app)Bufferindentunindent)CompleteEvent)
	Conditionemacs_insert_mode
emacs_modehas_arghas_selectionin_paste_modeis_multilineis_read_onlyshift_selection_modevi_search_direction_reversed)Binding)KeyPressEvent)Keys)SelectionType   )ConditionalKeyBindingsKeyBindingsKeyBindingsBase   )get_by_nameload_emacs_bindingsload_emacs_search_bindings#load_emacs_shift_selection_bindingsr   returnc                    s  t  } | jt}dddddd}dtd d	td
 d|dtd dtd dtd dtd dtd ddd|dtd d|dtd ddtd dd|dtd dd|dtd ddtd dd |dtd! dd"|dtd# dd|dtd$ dd%|dtd& dd'|dtd( d)td* d+td, d-d.d/ |d0td1 dd2d3d/ |d0td1 dd4t dtd5 dd6t dtd7 dd8|dtd9 dd:|dtd9 dd|dtd; dd<|dtd= d>td? d@t dtdA ddBtdC ddDtdE ddFtdG dHddddIdJ}dKddddLdM}dNddOfdPdQ}dRD ]}|| qddSt dddddTdU}dStdVd/ dddddWdX}tdYdZd[d\}	dd]||	@ dtd^ d]||	@ t @ dtd^ d_dNd`ddadbdc ddtj	ddd fdedf}
dddtj	ddd fdgdh}ddiddddjdk}ddFddddldm}ddn|dddddodp}ddq|dddddrds}ddddddtdu}dvddddwdx}dyt dddddzd{}dytddddd|d}}d~tddddtdddddd}ddtdddddd}ddddddd}ddddddd}dd|dddddd}dd6tdddddd}dd4tdddddd}t
| tS )z!
    Some e-macs extensions.
    escapeENoneeventr"   c                 S  s   dS )a\  
        By default, ignore escape key.

        (If we don't put this here, and Esc is followed by a key which sequence
        is not handled, we'll insert an Escape character in the input stream.
        Something we don't want and happens to easily in emacs mode.
        Further, people can always use ControlQ to do a quoted insert.)
        N r'   r(   r(   M/tmp/pip-unpacked-wheel-4x_7prb2/prompt_toolkit/key_binding/bindings/emacs.py_esc1   s    
z!load_emacs_bindings.<locals>._esczc-abeginning-of-linezc-bbackward-charzc-deletefilterz	kill-wordzc-eend-of-linezc-fforward-charc-leftbackward-wordc-rightforward-wordzc-xryyankc-ybczcapitalize-worddflzdowncase-worduzuppercase-wordzyank-pop	backspacezbackward-kill-word\zdelete-horizontal-spacec-homebeginning-of-bufferc-endend-of-bufferzc-_c                 S  s   dS NFr(   er(   r(   r*   <lambda>S       z%load_emacs_bindings.<locals>.<lambda>)Zsave_beforer/   Zundozc-uc                 S  s   dS rF   r(   rG   r(   r(   r*   rI   W   rJ   <zbeginning-of-history>zend-of-history.zyank-last-arg_zyank-nth-arg#zinsert-commentzc-ozoperate-and-get-nextzc-qzquoted-insert(zstart-kbd-macro)zend-kbd-macrorH   zcall-last-kbd-macrozc-nc                 S  s   | j   dS )z
Next line.N)current_buffer	auto_downr)   r(   r(   r*   _nextm   s    z"load_emacs_bindings.<locals>._nextzc-pc                 S  s   | j j| jd dS )zPrevious line.countN)rR   auto_upargr)   r(   r(   r*   _prevr   s    z"load_emacs_bindings.<locals>._prevstr)r;   r"   c                   s.    t dd ddd fdd}dS )	ze
        Handle input of arguments.
        The first number needs to be preceded by escape.
        r.   r#   r$   r%   r&   c                   s   |    d S N)append_to_arg_countr)   r;   r(   r*   rN   }   s    z4load_emacs_bindings.<locals>.handle_digit.<locals>._N)r   )r;   rN   )handler]   r*   handle_digitw   s    
z)load_emacs_bindings.<locals>.handle_digit
0123456789-c                 S  s   | j dkr| d dS ) Nra   )Z_argr\   r)   r(   r(   r*   
_meta_dash   s    
z'load_emacs_bindings.<locals>._meta_dashc                   S  s   t  jjdkS )Nra   )r   key_processorrX   r(   r(   r(   r*   rI      rJ   c                 S  s   d| j j_dS )zp
        When '-' is typed again, after exactly '-' has been given as an
        argument, ignore this.
        ra   N)apprd   rX   r)   r(   r(   r*   _dash   s    z"load_emacs_bindings.<locals>._dashboolr!   c                   S  s
   t  jjS r[   )r   rR   is_returnabler(   r(   r(   r*   rh      s    z*load_emacs_bindings.<locals>.is_returnableenterzaccept-liner   int)buffcharrV   r"   c                 S  sJ   |dk r| j j|d| d}n| j j|d|d}|d k	rF|  j|7  _d S )Nr   T)Zin_current_linerV   )documentZfind_backwardsfindcursor_position)rk   rl   rV   matchr(   r(   r*   character_search   s      z-load_emacs_bindings.<locals>.character_searchzc-]c                   s    | j | j| j dS )z:When Ctl-] + a character is pressed. go to that character.NrR   datarX   r)   rq   r(   r*   
_goto_char   s    z'load_emacs_bindings.<locals>._goto_charc                   s    | j | j| j  dS )zLike Ctl-], but backwards.Nrr   r)   rt   r(   r*   _goto_char_backwards   s    z1load_emacs_bindings.<locals>._goto_char_backwardsac                 S  s   dS )zPrevious sentence.Nr(   r)   r(   r(   r*   _prev_sentence   s    z+load_emacs_bindings.<locals>._prev_sentencec                 S  s   dS )zMove to end of sentence.Nr(   r)   r(   r(   r*   _end_of_sentence   s    z-load_emacs_bindings.<locals>._end_of_sentencetc                 S  s   dS )z<
        Swap the last two words before the cursor.
        Nr(   r)   r(   r(   r*   _swap_characters   s    z-load_emacs_bindings.<locals>._swap_characters*c                 S  sH   | j }tddd}t|j|j|}ddd |D }|| dS )zR
        `meta-*`: Insert all possible completions of the preceding text.
        FT)Ztext_insertedZcompletion_requested c                 s  s   | ]}|j V  qd S r[   )text).0r;   r(   r(   r*   	<genexpr>   s     zGload_emacs_bindings.<locals>._insert_all_completions.<locals>.<genexpr>N)rR   r	   listZ	completerZget_completionsrm   joinZinsert_text)r'   rk   Zcomplete_eventZcompletionsZtext_to_insertr(   r(   r*   _insert_all_completions   s    z4load_emacs_bindings.<locals>._insert_all_completionsc                 S  s@   | j }|jjr(| j|jjdd7  _n| j|j 7  _dS )zc
        Move cursor back and forth between the start and end of the current
        line.
        FZafter_whitespaceN)rR   rm   Zis_cursor_at_the_end_of_linero   get_start_of_line_positionZget_end_of_line_positionr'   bufferr(   r(   r*   _toggle_start_end   s    z.load_emacs_bindings.<locals>._toggle_start_endzc-@c                 S  s   | j }|jr|jtjd dS )zN
        Start of the selection (if the current buffer is not empty).
        Zselection_typeN)rR   r~   start_selectionr   
CHARACTERSr'   rk   r(   r(   r*   _start_selection   s    z-load_emacs_bindings.<locals>._start_selectionc-gc                 S  s   d| j _d| j _dS )zK
        Control + G: Cancel completion menu and validation state.
        N)rR   complete_stateZvalidation_errorr)   r(   r(   r*   _cancel   s    z$load_emacs_bindings.<locals>._cancelc                 S  s   | j   dS )#
        Cancel selection.
        N)rR   exit_selectionr)   r(   r(   r*   _cancel_selection   s    z.load_emacs_bindings.<locals>._cancel_selectionzc-wkc                 S  s   | j  }| jj| dS )z$
        Cut selected text.
        N)rR   cut_selectionre   	clipboardset_datar'   rs   r(   r(   r*   _cut  s    
z!load_emacs_bindings.<locals>._cutwc                 S  s   | j  }| jj| dS )z%
        Copy selected text.
        N)rR   Zcopy_selectionre   r   r   r   r(   r(   r*   _copy  s    
z"load_emacs_bindings.<locals>._copyleftc                 S  s(   | j }| j|jj| jdpd7  _dS )z3
        Cursor to start of previous word.
        rU   r   N)rR   ro   rm   Zfind_previous_word_beginningrX   r   r(   r(   r*   _start_of_word  s    z+load_emacs_bindings.<locals>._start_of_wordrightc                 S  s.   | j }| j|jj| jdp$|j 7  _dS )z/
        Cursor to start of next word.
        rU   N)rR   ro   rm   Zfind_next_word_beginningrX   Zget_end_of_document_positionr   r(   r(   r*   _start_next_word  s
    z-load_emacs_bindings.<locals>._start_next_word/c                 S  s&   | j }|jr|  n|jdd dS )z 
        M-/: Complete.
        T)Zselect_firstN)rR   r   Zcomplete_nextZstart_completion)r'   r:   r(   r(   r*   	_complete(  s    
z&load_emacs_bindings.<locals>._completec-cc                 S  sf   | j }| j|jjdd7  _|j \}}|j|\}}|j|\}}t|||d | jd dS )z'
        Indent selected text.
        Tr   r   rU   N)rR   ro   rm   r   selection_rangetranslate_index_to_positionr   rX   r'   r   from_torN   r(   r(   r*   _indent3  s    
z$load_emacs_bindings.<locals>._indentc                 S  sN   | j }|j \}}|j|\}}|j|\}}t|||d | jd dS )z)
        Unindent selected text.
        r   rU   N)rR   rm   r   r   r   rX   r   r(   r(   r*   	_unindentD  s
    z&load_emacs_bindings.<locals>._unindent)r   addr   r   r   r   r
   r   r   Anyr   r   )key_bindingsZinsert_moder+   rT   rY   r_   r;   rc   rf   rh   ru   rv   rx   ry   r{   r   r   r   r   r   r   r   r   r   r   r   r   r(   )rq   r^   r*   r   &   s    
	

	

c                  C  sF  t  } | j}ddlm} |d|j |d|j |d|j |d|j |d|j |d|j |d|j |d|j |d	|j	 |d
dd|j	 |dt
t @ d|j |dt
t @ d|j |dt
t@ d|j |dt
t@ d|j |dt
dddddd}|dt
dddddd}t| tS )Nr   )searchzc-rzc-sr   r   updownri   r#   T)eager?r.   r   nr$   r%   r&   c                 S  s   | j j| jjd| jd dS )zJump to next match.FZinclude_current_positionrV   NrR   Zapply_searchre   Zcurrent_search_staterX   r)   r(   r(   r*   
_jump_next  s
    z.load_emacs_search_bindings.<locals>._jump_nextNc                 S  s   | j j| jj d| jd dS )zJump to previous match.Fr   Nr   r)   r(   r(   r*   
_jump_prev  s
    z.load_emacs_search_bindings.<locals>._jump_prev)r   r   rb   r   Z start_reverse_incremental_searchZ start_forward_incremental_searchZabort_searchZreverse_incremental_searchZforward_incremental_searchZaccept_searchr   r   r   r   )r   r^   r   r   r   r(   r(   r*   r   T  s<    

c            	        sP  t  } | j}ddddd |dt d|dt d|d	t d|d
t d|dt d|dt d|dt d|dt d|dt d|dt dddd fdd}|dtd|dtd|d	td|d
td|dtd|dtd|dtd|dtd|dtd|dtdddd fdd}|tjtdddddd}|dtt@ dddddd}|dtdddddd}|dtdddddd}|d td|d!td|d"td|d#td|d$td|d%td|d&td|d'td|d(td|d)tddddd*d+}t| tS ),z?
    Bindings to select text with shift + cursor movements
    r$   r%   r&   c                 S  s   | j d j}|tjkr*| jj| jd dS |tjkrH| jj| jd dS tj	dtj
dtjdtjdtjdtjd	tjd
tjdi}zt|| }W n tk
r   Y nX t|tr||  dS )z
        Used for the shift selection mode. When called with
        a shift + movement key press event, moves the cursor
        as if shift is not pressed.
        r   rU   Nr-   r1   r,   r0   r3   r5   rC   rE   )key_sequencekeyr   ZShiftUprR   rW   rX   Z	ShiftDownrS   Z	ShiftLeftZ
ShiftRightZ	ShiftHomeZShiftEndZControlShiftLeftZControlShiftRightZControlShiftHomeZControlShiftEndr   KeyError
isinstancer   call)r'   r   Zkey_to_commandZbindingr(   r(   r*   unshift_move  s<    

        
z9load_emacs_shift_selection_bindings.<locals>.unshift_movezs-leftr.   zs-rightzs-upzs-downzs-homezs-endzc-s-leftz	c-s-rightzc-s-homezc-s-endc                   sR   | j }|jrN|jtjd |jdk	r.|j  |j} |  |j|krN|  dS )z8
        Start selection with shift + movement.
        r   N)	rR   r~   r   r   r   selection_stateZenter_shift_modero   r   )r'   rk   Zoriginal_positionr   r(   r*   r     s    


z=load_emacs_shift_selection_bindings.<locals>._start_selectionc                   s2    |  | j }|jdk	r.|j|jjkr.|  dS )z&
        Extend the selection
        N)rR   r   ro   Zoriginal_cursor_positionr   r   r   r(   r*   _extend_selection  s
    
z>load_emacs_shift_selection_bindings.<locals>._extend_selectionc                 S  s   | j   td|  dS )z4
        Replace selection by what is typed
        zself-insertN)rR   r   r   r   r)   r(   r(   r*   _replace_selection  s    
z?load_emacs_shift_selection_bindings.<locals>._replace_selectionri   c                 S  s    | j   | j jt  d dS )z2
        A newline replaces the selection
        )Zcopy_marginN)rR   r   newliner   r)   r(   r(   r*   _newline  s    
z5load_emacs_shift_selection_bindings.<locals>._newliner@   c                 S  s   | j   dS )z#
        Delete selection.
        N)rR   r   r)   r(   r(   r*   _delete
  s    z4load_emacs_shift_selection_bindings.<locals>._deleter9   c                 S  s&   | j }|jr|  td|  dS )zS
        In shift selection mode, yanking (pasting) replace the selection.
        r8   N)rR   r   r   r   r   r   r(   r(   r*   _yank  s    z2load_emacs_shift_selection_bindings.<locals>._yankr   r   r   r   homeendr2   r4   rB   rD   c                 S  s(   | j   | jd }| jj|dd dS )r   r   T)firstN)rR   r   r   rd   feed)r'   Z	key_pressr(   r(   r*   r     s    

z4load_emacs_shift_selection_bindings.<locals>._cancel)	r   r   r   r   r   r   r   r   r   )	r   r^   r   r   r   r   r   r   r   r(   r   r*   r      sZ    %(









(












$	N)+
__future__r   typingr   r   Z"prompt_toolkit.application.currentr   Zprompt_toolkit.bufferr   r   r   Zprompt_toolkit.completionr	   Zprompt_toolkit.filtersr
   r   r   r   r   r   r   r   r   r   Z'prompt_toolkit.key_binding.key_bindingsr   Z(prompt_toolkit.key_binding.key_processorr   Zprompt_toolkit.keysr   Zprompt_toolkit.selectionr   r   r   r   r   Znamed_commandsr   __all__r$   r   r   r    r(   r(   r(   r*   <module>   s*   0  0B