U
    d'                    @  s"  d dl m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
mZmZmZmZmZ d dlmZ d dlmZmZmZ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#m$Z$m%Z%m&Z&m'Z'm(Z(m)Z)m*Z*m+Z+m,Z,m-Z-m.Z.m/Z/ d dl0m1Z1 d dl2m3Z3 d dl4m5Z5m6Z6 d dl7m8Z8m9Z9 d dl:m;Z; d dl<m=Z= d dl>m?Z?m@Z@mAZA ddlBmCZCmDZDmEZE ddlFmGZG ddgZHe6ZIejJZJeJd ZKG dd deZLG dd dZMe	eIgeMf ZNedeNdZOd d!d"d#d$ZPe	eIeMgdf ZQed%eQdZRd d&d"d'd(ZSd)d*d+dZTd)d*d,dZUdS )-    )annotationsN)Enum)
accumulate)CallableIterableListOptionalTupleTypeVarUnion)get_app)Bufferindentreshape_textunindent)ClipboardData)Document)Always	ConditionFilterhas_argis_read_onlyis_searching)in_paste_modeis_multilinevi_digraph_modevi_insert_modevi_insert_multiple_modevi_modevi_navigation_modevi_recording_macrovi_replace_modevi_replace_single_modevi_search_direction_reversedvi_selection_modevi_waiting_for_text_object_mode)Vt100Parser)DIGRAPHS)KeyPressKeyPressEvent)CharacterFind	InputMode)Keys)SearchDirection)	PasteModeSelectionStateSelectionType   )ConditionalKeyBindingsKeyBindingsKeyBindingsBase   )get_by_nameload_vi_bindingsload_vi_search_bindings
0123456789c                   @  s   e Zd ZdZdZdZdZdS )TextObjectType	EXCLUSIVE	INCLUSIVELINEWISEBLOCKN)__name__
__module____qualname__r;   r<   r=   r>    rB   rB   J/tmp/pip-unpacked-wheel-4x_7prb2/prompt_toolkit/key_binding/bindings/vi.pyr:   <   s   r:   c                   @  sz   e Zd ZdZdejfddddddZedd	d
dZdd	ddZ	dddddZ
dddddZdddddZdS )
TextObjectz
    Return struct for functions wrapped in ``text_object``.
    Both `start` and `end` are relative to the current cursor position.
    r   intr:   startendtypec                 C  s   || _ || _|| _d S NrF   )selfrG   rH   rI   rB   rB   rC   __init__I   s    zTextObject.__init__r0   returnc                 C  s.   | j tjkrtjS | j tjkr$tjS tjS d S rJ   )rI   r:   r=   r0   LINESr>   
CHARACTERSrK   rB   rB   rC   selection_typeP   s
    zTextObject.selection_typetuple[int, int]c                 C  s(   | j | jk r| j | jfS | j| j fS dS )zA
        Return a (start, end) tuple where start <= end.
        N)rG   rH   rQ   rB   rB   rC   sortedY   s    zTextObject.sortedr   )documentrN   c                 C  s   |   \}}|}| jtjkr<|||j d dkr<|d8 }| jtjkrP|d7 }| jtjkr|||j \}}||d|j }|||j \}}||t	|j
| |j }||fS )a2  
        Return a (start, end) tuple with start <= end that indicates the range
        operators should operate on.
        `buffer` is used to get start and end of line positions.

        This should return something that can be used in a slice, so the `end`
        position is *not* included.
        r5   r   )rT   rI   r:   r;   translate_index_to_positioncursor_positionr<   r=   translate_row_col_to_indexlenlines)rK   rU   rG   rH   docrowcolrB   rB   rC   operator_rangeb   s$    	
zTextObject.operator_ranger   )bufferrN   c                 C  sL   |  |j\}}||j7 }||j7 }|j|\}}|j|\}}||fS )z7
        Return a (start_line, end_line) pair.
        )r^   rU   rW   rV   )rK   r_   from_to_rB   rB   rC   get_line_numbers   s    

zTextObject.get_line_numbersztuple[Document, ClipboardData]c                 C  sd   |  |j\}}||j7 }||j7 }| jtjkr8|d8 }t|j|t|| j	d}|
 \}}||fS )zA
        Turn text object into `ClipboardData` instance.
        r5   )original_cursor_positionrI   )r^   rU   rW   rI   r:   r=   r   textr/   rR   cut_selection)rK   r_   r`   ra   rU   new_documentclipboard_datarB   rB   rC   cut   s    

zTextObject.cutN)r?   r@   rA   __doc__r:   r;   rL   propertyrR   rT   r^   rc   ri   rB   rB   rB   rC   rD   C   s    	 rD   _TOF)boundr3   z%Callable[..., Callable[[_TOF], _TOF]])key_bindingsrN   c                   s.   t  ddddddddddd fdd	}|S )
zY
    Create a decorator that can be used to register Vi text object implementations.
    F)filterno_move_handlerno_selection_handlereager
Keys | strr   boolzCallable[[_TOF], _TOF])keysro   rp   rq   rr   rN   c                   s"   ddd fdd}|S )a  
        Register a text object function.

        Usage::

            @text_object('w', filter=..., no_move_handler=False)
            def handler(event):
                # Return a text object for this key.
                return TextObject(...)

        :param no_move_handler: Disable the move handler in navigation mode.
            (It's still active in selection mode.)
        rl   )text_object_funcrN   c                   s   j t@ dddd fdd}s^j t @ t@ dddd fdd}sj t @ t@ dddd fd	d
} S )Nro   rr   ENoneeventrN   c                   sb   | j j}t|jpd| jpd | _ | }|j}|d k	rJ|d k	rJ|| | d | j j_d | j j_d S Nr5   )appvi_statestroperator_argarg_argoperator_func)r{   r~   Ztext_objr   rv   rB   rC   _apply_operator_to_text_object   s    

zvcreate_text_object_decorator.<locals>.text_object_decorator.<locals>.decorator.<locals>._apply_operator_to_text_objectc                   s    | }| j  j|j7  _dS )zK
                    Move handler for navigation mode.
                    N)current_bufferrW   rG   )r{   text_objectr   rB   rC   _move_in_navigation_mode   s    zpcreate_text_object_decorator.<locals>.text_object_decorator.<locals>.decorator.<locals>._move_in_navigation_modec                   s    | }| j }|j}|dkr dS |jrv||j\}}||j7 }||j7 }||_||_|jtj	krlt
j|_qt
j|_n| j  j|j7  _dS )zJ
                    Move handler for selection mode.
                    N)r   selection_staterH   r^   rU   rW   rd   rI   r:   r=   r0   rO   rP   rG   )r{   r   buffr   rG   rH   r   rB   rC   _move_in_selection_mode   s    



zocreate_text_object_decorator.<locals>.text_object_decorator.<locals>.decorator.<locals>._move_in_selection_modeaddr%   r   r$   )rv   r   r   r   )rr   ro   rn   ru   rp   rq   r   rC   	decorator   s:      zNcreate_text_object_decorator.<locals>.text_object_decorator.<locals>.decoratorrB   )ro   rp   rq   rr   ru   r   rn   )rr   ro   ru   rp   rq   rC   text_object_decorator   s    Uz;create_text_object_decorator.<locals>.text_object_decoratorr   )rn   r   rB   r   rC   create_text_object_decorator   s    	 lr   _OFz#Callable[..., Callable[[_OF], _OF]]c                   s&   t  ddddddd fdd	}|S )
zK
    Create a decorator that can be used for registering Vi operators.
    Frw   rs   r   rt   zCallable[[_OF], _OF])ru   ro   rr   rN   c                   s   ddd fdd}|S )z
        Register a Vi operator.

        Usage::

            @operator('d', filter=...)
            def handler(event, text_object):
                # Do something with the text object here.
        r   )r   rN   c                   sd   j t @ t@ dddd fdd}j t @ t@ dddd fdd} S )	Nrw   rx   ry   rz   c                   s    | j j_| j| j j_dS )zE
                Handle operator in navigation mode.
                N)r}   r~   r   r   r   r{   r   rB   rC   _operator_in_navigation@  s    
zicreate_operator_decorator.<locals>.operator_decorator.<locals>.decorator.<locals>._operator_in_navigationc                   sj   | j }|j}|dk	rf|jtjkr(tj}n|jtjkr<tj}ntj}t	|j
|j |d} | | d|_dS )zD
                Handle operator in selection mode.
                NrI   )r   r   rI   r0   rO   r:   r=   r>   r<   rD   rd   rW   )r{   r   r   Ztext_obj_typer   r   rB   rC   _operator_in_selectionO  s    	

zhcreate_operator_decorator.<locals>.operator_decorator.<locals>.decorator.<locals>._operator_in_selectionr   )r   r   r   )rr   ro   rn   ru   r   rC   r   ?  s    
zHcreate_operator_decorator.<locals>.operator_decorator.<locals>.decoratorrB   )ro   rr   ru   r   r   )rr   ro   ru   rC   operator_decorator2  s    2z5create_operator_decorator.<locals>.operator_decoratorr   )rn   r   rB   r   rC   create_operator_decorator+  s     Ar   r4   rM   c                    s  t  } | j}tttdf tttgtf f }dt dd fdt dd fdt dd fd	t d
d fdtdd dd fg}|dtdt	d |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dddd }|dtddddd!d"}|d#td|d$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ddd,d-}|dtddddd.d/}|d0td|d1tddddd2d3}|d4tddddd5d6}td7d8d9d:}|d;t|@ dt	d< |d;|t @ dt	d< |d;| t@ ddddd=d>}|d?tddddd@dA}|d?tdddddBdC}|dDtt @ dddddEdF}|dGtt @ dddddHdI}|dJtt @ dddddKdL}|dMdMtt @ d|dNtt @ dddddOdP}|dQtdddddRdS}|dTdTtdddddUdV}|dWt
dddddXdY}|dZtt @ ddddd[d\}|d]tt @ ddddd^d_}td7d8d`da}|d]|t @ dddd7ddcddde |dG|t @ dddd fdfdg}|dhtt @ dddddidj}|dkdhtt @ dddddldm} |dht
t @ dddddndo}!|dkdht
t @ dddddpdq}"|drtdddddsdt}#|dutdddddvdw}$|dxtjdrtdddddydz}%|dxtjdutddddd{d|}&|d}tddddd~d}'|dtdddddd}(|dtt @ dddddd})|dtdd dddddd}*|dtdddddd}+|dtdddddd},|dt
dddddd}-|dtdddddd}.|dt
dddddd}/|dt
dddddd}0|dDdt
d|dDdt
dddddd}1|dWtdddddd}2|dtdddddd}3|ddtd|dtdddddd}4|dtdddddd}5|dtdddddd}6|ddtdddddd}7|ddtdddddd}8|dtt @ dddddd}9|dtt @ dddddd}:|dtdddddd};|dkddtt @ dddddd}<|dkddtt @ dddddd}=|dkddtt @ dddddd}>|dtddddddĄ}?|dtddddddǄ}@|dtddddddʄ}A|dtdddddd̈́}Bt| t| |tjtddddddτ}Cdd7d7ddМfdd҄}D|Ddbdb |DdbdӃ |Dddb |DddӃ dddddלfddل}E|D ]\}F}G}H|E|G|Hf|F  	q0dddddۜdd݄}Idxtjdddddۜdd߄}Jdddddۜdd}Kdddddۜdd}Ldkdddddۜdd}Mdddddd}Ndddddd}Odddddd}Pdddddd}Qdddddd}Rdddddd}Sdddddd}TdZdddddddd}UdDdddddddd}VdZdddddddd}WdDddddddd d}XdDdrddddddd}Ydddddd}Zdddddd	}[dddd7dddfdd}\dD ]D}]dD ]\}^}_|\|^|_|] q|\dd|]d |\dd|]d qdddddd}`dddddd}adtjddddd}bdtjddddd}cdtjddddd}dd tjdddd!d"}ed7dd#fd$d%}f|fdӃ |fdb d&d'dddd(d)}gdddӐd*dddd+d,}hdddӐd*dddd-d.}id/d0d1dddd2d3}jd4dddd5d6}kd7dddd8d9}ld:dddd;d<}md=dddddd>d?}n|d=tddddd@dA}odBddddddCdD}p|dBtdddddEdF}q|dGdtt
B d|dGdtt
B d|dGd;tt
B dddddHdI}r|dGdtt
B d|dGdtt
B dddddJdK}s|dGdGtt
B dddddLdM}tdNddddOdP}udQddddRdS}vdkdkddddTdU}wdkdVddddWdX}xdkdddddYdZ}ydkddddd[d\}zdkd]dddd^d_}{d`ddddadb}||d`tdddddcdd}}deD ].}~||~tt
B tB dddddfdg}q|dtt
B tB t@ dddddhdi}|tjtdddddjdk}|tjtdddddldm}|tjtdnd dddddodp}|d)tdddddqdr}|dstdddddtdu}|d'tdddddvdw}|d1tdddddxdy}|dtd|d#tdddddzd{}|d|d}tddddd~d}|d|dtdddddd}|dttB dddddd}td7d8dd}|tjt| @ dddddd}|tjt|@ dddddd}|dttB dddddd}|dtjtt @ dddddd}|dtt@ dddddd}|dtjtdbdddddd}t| tS (  z
    Vi extensions.

    # Overview of Readline Vi commands:
    # http://www.catonmat.net/download/bash-vi-editing-mode-cheat-sheet.pdf
    .)g?c                 S  s   t | dS )Nrot_13)codecsencodestringrB   rB   rC   <lambda>      z"load_vi_bindings.<locals>.<lambda>)r   uc                 S  s   |   S rJ   lowerr   rB   rB   rC   r     r   )r   Uc                 S  s   |   S rJ   upperr   rB   rB   rC   r     r   )r   ~c                 S  s   |   S rJ   swapcaser   rB   rB   rC   r     r   )r   c                   S  s
   t  jjS rJ   )r   r~   Ztilde_operatorrB   rB   rB   rC   r     r   c                 S  s   |   S rJ   r   r   rB   rB   rC   r     r   zc-vro   zquoted-insertescaperx   ry   rz   c                 S  sR   | j }| jj}|jtjtjfkr4| j|j	 7  _tj
|_t|jrN|  dS )z4
        Escape goes to vi navigation mode.
        N)r   r}   r~   
input_moder+   INSERTREPLACErW   rU   get_cursor_left_position
NAVIGATIONrt   r   exit_selection)r{   r_   r~   rB   rB   rC   _back_to_navigation  s    
z-load_vi_bindings.<locals>._back_to_navigationkc                 S  s   | j j| jd dS )z-
        Arrow up in selection mode.
        countN)r   Z	cursor_upr   r   rB   rB   rC   _up_in_selection  s    z*load_vi_bindings.<locals>._up_in_selectionjc                 S  s   | j j| jd dS )z/
        Arrow down in selection mode.
        r   N)r   cursor_downr   r   rB   rB   rC   _down_in_selection  s    z,load_vi_bindings.<locals>._down_in_selectionZupzc-pc                 S  s   | j j| jd dS )zA
        Arrow up and ControlP in navigation mode go up.
        r   Nr   Zauto_upr   r   rB   rB   rC   _up_in_navigation  s    z+load_vi_bindings.<locals>._up_in_navigationc                 S  s   | j j| jdd dS )zd
        Go up, but if we enter a new history entry, move to the start of the
        line.
        Tr   Z&go_to_start_of_line_if_history_changesNr   r   rB   rB   rC   _go_up  s     z load_vi_bindings.<locals>._go_upZdownzc-nc                 S  s   | j j| jd dS )z>
        Arrow down and Control-N in navigation mode.
        r   Nr   Z	auto_downr   r   rB   rB   rC   _go_down  s    z"load_vi_bindings.<locals>._go_downc                 S  s   | j j| jdd dS )z\
        Go down, but if we enter a new history entry, go to the start of the line.
        Tr   Nr   r   rB   rB   rC   	_go_down2  s     z#load_vi_bindings.<locals>._go_down2	backspacec                 S  s"   | j  j| j jj| jd7  _dS )z2
        In navigation-mode, move cursor.
        r   N)r   rW   rU   r   r   r   rB   rB   rC   _go_left  s    z"load_vi_bindings.<locals>._go_leftc                 S  s&   | j }|jr|  n|jdd d S )NT)Zselect_first)r   complete_stateZcomplete_nextstart_completionr{   brB   rB   rC   _complete_next  s    
z(load_vi_bindings.<locals>._complete_nextc                 S  s&   | j }|jr|  n|jdd dS )z4
        Control-P: To previous completion.
        T)Zselect_lastN)r   r   Zcomplete_previousr   r   rB   rB   rC   _complete_prev  s    
z(load_vi_bindings.<locals>._complete_prevc-gzc-yc                 S  s   d| j _dS )z,
        Accept current completion.
        N)r   r   r   rB   rB   rC   _accept_completion  s    z,load_vi_bindings.<locals>._accept_completionzc-ec                 S  s   | j   dS )zF
        Cancel completion. Go back to originally typed text.
        N)r   Zcancel_completionr   rB   rB   rC   _cancel_completion  s    z,load_vi_bindings.<locals>._cancel_completionrt   rM   c                   S  s
   t  jjS rJ   )r   r   is_returnablerB   rB   rB   rC   r     s    z'load_vi_bindings.<locals>.is_returnableenterzaccept-linec                 S  s0   | j }|j| jd | j|jjdd7  _dS )z3
        Go to the beginning of next line.
        r   TZafter_whitespaceN)r   r   r   rW   rU   get_start_of_line_positionr   rB   rB   rC   _start_of_next_line   s
    z-load_vi_bindings.<locals>._start_of_next_lineinsertc                 S  s   t j| jj_dS z*
        Pressing the Insert key.
        Nr+   r   r}   r~   r   r   rB   rB   rC   _insert_mode/  s    z&load_vi_bindings.<locals>._insert_modec                 S  s   t j| jj_dS r   )r+   r   r}   r~   r   r   rB   rB   rC   _navigation_mode6  s    z*load_vi_bindings.<locals>._navigation_modeac                 S  s(   | j  j| j j 7  _tj| jj_d S rJ   )	r   rW   rU   get_cursor_right_positionr+   r   r}   r~   r   r   rB   rB   rC   _a=  s    
zload_vi_bindings.<locals>._aAc                 S  s(   | j  j| j j 7  _tj| jj_d S rJ   )	r   rW   rU   get_end_of_line_positionr+   r   r}   r~   r   r   rB   rB   rC   _AF  s    
zload_vi_bindings.<locals>._ACc                 S  s6   | j }|j|j d}| jj| tj| jj	_
dS )z_
        Change to end of line.
        Same as 'c$' (which is implemented elsewhere.)
        r   N)r   deleterU   r   r}   	clipboardset_textr+   r   r~   r   r{   r_   deletedrB   rB   rC   _change_until_end_of_lineM  s    z3load_vi_bindings.<locals>._change_until_end_of_linecSc                 S  s^   | j }t|jjtj}| jj| | j	|jj
dd7  _	|j|j d tj| jj_dS )z%
        Change current line
        Tr   r   N)r   r   rU   current_liner0   rO   r}   r   set_datarW   r   r   r   r+   r   r~   r   )r{   r_   datarB   rB   rC   _change_current_lineY  s    
z.load_vi_bindings.<locals>._change_current_lineDc                 S  s*   | j }|j|j d}| jj| dS )zH
        Delete from cursor position until the end of the line.
        r   N)r   r   rU   r   r}   r   r   r   rB   rB   rC   _delete_until_end_of_linel  s    z3load_vi_bindings.<locals>._delete_until_end_of_linedc                 S  s   | j }|jj}d|d|jj }d||jj|jj| j  }d||jj| j d }|rp|rp|d }t|| t|t| t|d d|_| j	j
t|tj dS )z<
        Delete line. (Or the following 'n' lines.)
        
N )re   rW   )r   rU   rZ   joincursor_position_rowr   r   rY   lstripr}   r   r   r   r0   rO   )r{   r_   rZ   beforer   afterrB   rB   rC   _delete_lineu  s&     z&load_vi_bindings.<locals>._delete_linexc                 S  s   | j  }| jj| dS )zB
        Cut selection.
        ('x' is not an operator.)
        N)r   rf   r}   r   r   )r{   rh   rB   rB   rC   _cut  s    
zload_vi_bindings.<locals>._cutic                 S  s   t j| jj_d S rJ   r   r   rB   rB   rC   _i  s    zload_vi_bindings.<locals>._iIc                 S  s,   t j| jj_| j j| jjjdd7  _d S )NTr   )	r+   r   r}   r~   r   r   rW   rU   r   r   rB   rB   rC   _I  s    zload_vi_bindings.<locals>._Ic                  S  s    t  j} t| jo| jjtjkS rJ   )r   r   rt   r   rI   r0   r>   )r   rB   rB   rC   in_block_selection  s    z,load_vi_bindings.<locals>.in_block_selectionF)r{   r   rN   c                 S  s   | j }g }|r ddddd}nddddd}t|j D ](\}}||| |dkr>|||_q>||_tj| j	j
_|  dS )	z1
        Insert in block selection mode.
        rS   rE   )from_torN   c                 S  s   | d S r|   rB   r   rB   rB   rC   get_pos  s    zDload_vi_bindings.<locals>.insert_in_block_selection.<locals>.get_posc                 S  s   | d S )Nr   rB   r   rB   rB   rC   r     s    r   N)r   	enumeraterU   Zselection_rangesappendrW   multiple_cursor_positionsr+   ZINSERT_MULTIPLEr}   r~   r   r   )r{   r   r   Z	positionsr   r   r   rB   rB   rC   insert_in_block_selection  s    z3load_vi_bindings.<locals>.insert_in_block_selectionc                   s    | dd d S )NT)r   rB   r   )r  rB   rC   _append_after_block  s    z-load_vi_bindings.<locals>._append_after_blockJc                 S  s   t | jD ]}| j  q
dS )z
        Join lines.
        Nranger   r   Zjoin_next_liner{   r   rB   rB   rC   _join  s    zload_vi_bindings.<locals>._joinr   c                 S  s"   t | jD ]}| jjdd q
dS )z+
        Join lines without space.
         	separatorNr  r	  rB   rB   rC   _join_nospace  s    z'load_vi_bindings.<locals>._join_nospacec                 S  s   | j   dS )z&
        Join selected lines.
        Nr   Zjoin_selected_linesr   rB   rB   rC   _join_selection  s    z)load_vi_bindings.<locals>._join_selectionc                 S  s   | j jdd dS )z4
        Join selected lines without space.
        r  r  Nr  r   rB   rB   rC   _join_selection_nospace  s    z1load_vi_bindings.<locals>._join_selection_nospacepc                 S  s"   | j j| jj | jtjd dS )z
        Paste after
        r   Z
paste_modeN)r   paste_clipboard_datar}   r   get_datar   r.   VI_AFTERr   rB   rB   rC   _paste  s
    
z load_vi_bindings.<locals>._pastePc                 S  s"   | j j| jj | jtjd dS )z
        Paste before
        r  N)r   r  r}   r   r  r   r.   	VI_BEFOREr   rB   rB   rC   _paste_before  s
    
z'load_vi_bindings.<locals>._paste_before"c                 S  sB   | j d j}|tkr>| jjj|}|r>| jj|| j	t
jd dS )z,
        Paste from named register.
        r5   r  N)key_sequencer   vi_register_namesr}   r~   named_registersgetr   r  r   r.   r  r{   r   r   rB   rB   rC   _paste_register
  s      z)load_vi_bindings.<locals>._paste_registerc                 S  sB   | j d j}|tkr>| jjj|}|r>| jj|| j	t
jd dS )z5
        Paste (before) from named register.
        r5   r  N)r  r   r  r}   r~   r  r  r   r  r   r.   r  r   rB   rB   rC   _paste_register_before  s      z0load_vi_bindings.<locals>._paste_register_beforerc                 S  s   t j| jj_dS )z.
        Go to 'replace-single'-mode.
        N)r+   ZREPLACE_SINGLEr}   r~   r   r   rB   rB   rC   _replace$  s    z"load_vi_bindings.<locals>._replaceRc                 S  s   t j| jj_dS )z'
        Go to 'replace'-mode.
        N)r+   r   r}   r~   r   r   rB   rB   rC   _replace_mode+  s    z'load_vi_bindings.<locals>._replace_modesc                 S  s.   | j j| jd}| jj| tj| jj_	dS )z_
        Substitute with new text
        (Delete character(s) and go to insert mode.)
        r   N)
r   r   r   r}   r   r   r+   r   r~   r   r{   re   rB   rB   rC   _substitute2  s    z%load_vi_bindings.<locals>._substituter   c                 S  s   dS )NFrB   erB   rB   rC   r   <  r   )ro   Zsave_beforec                 S  s   t | jD ]}| j  q
d S rJ   )r  r   r   Zundor	  rB   rB   rC   _undo<  s    zload_vi_bindings.<locals>._undoVc                 S  s   | j jtjd dS )z(
        Start lines selection.
        rR   N)r   start_selectionr0   rO   r   rB   rB   rC   _visual_lineA  s    z&load_vi_bindings.<locals>._visual_linec                 S  s   | j jtjd dS )z-
        Enter block selection mode.
        r.  N)r   r/  r0   r>   r   rB   rB   rC   _visual_blockH  s    z'load_vi_bindings.<locals>._visual_blockc                 S  s4   | j j}|dk	r0|jtjkr&tj|_n
| j   dS )zn
        Exit line selection mode, or go from non line selection mode to line
        selection mode.
        N)r   r   rI   r0   rO   r   r{   r   rB   rB   rC   _visual_line2O  s
    
z'load_vi_bindings.<locals>._visual_line2vc                 S  s   | j jtjd dS )z1
        Enter character selection mode.
        r.  N)r   r/  r0   rP   r   rB   rB   rC   _visual]  s    z!load_vi_bindings.<locals>._visualc                 S  s4   | j j}|dk	r0|jtjkr&tj|_n
| j   dS )z}
        Exit character selection mode, or go from non-character-selection mode
        to character selection mode.
        N)r   r   rI   r0   rP   r   r2  rB   rB   rC   _visual2d  s
    
z"load_vi_bindings.<locals>._visual2c                 S  s4   | j j}|dk	r0|jtjkr&tj|_n
| j   dS )zq
        Exit block selection mode, or go from non block selection mode to block
        selection mode.
        N)r   r   rI   r0   r>   r   r2  rB   rB   rC   _visual_block2r  s
    
z(load_vi_bindings.<locals>._visual_block2wWc                 S  s(   | j }|jr$|jjtjkr$tj|j_dS )zP
        Switch from visual linewise mode to visual characterwise mode.
        N)r   r   rI   r0   rO   rP   r{   r_   rB   rB   rC   _visual_auto_word  s    z+load_vi_bindings.<locals>._visual_auto_wordc                 S  s>   | j }t| jt|jj}|r:| j j|d}| jj	| dS )z#
        Delete character.
        r   N)
r   minr   rY   rU   Zcurrent_line_after_cursorr   r}   r   r   r{   r   r   re   rB   rB   rC   _delete  s
    z!load_vi_bindings.<locals>._deleteXc                 S  s>   | j }t| jt|jj}|r:| j j|d}| jj	| d S )Nr   )
r   r<  r   rY   rU   Zcurrent_line_before_cursorZdelete_before_cursorr}   r   r   r=  rB   rB   rC   _delete_before_cursor  s
    z/load_vi_bindings.<locals>._delete_before_cursoryYc                 S  s4   d | jjjd| j }| jjt|t	j
 dS )z&
        Yank the whole line.
        r   N)r   r   rU   Zlines_from_currentr   r}   r   r   r   r0   rO   r(  rB   rB   rC   
_yank_line  s    z$load_vi_bindings.<locals>._yank_line+c                 S  s<   | j }| j|jj| jd7  _| j|jjdd7  _dS )z;
        Move to first non whitespace of next line
        r   Tr   N)r   rW   rU   get_cursor_down_positionr   r   r:  rB   rB   rC   
_next_line  s    
z$load_vi_bindings.<locals>._next_line-c                 S  s<   | j }| j|jj| jd7  _| j|jjdd7  _dS )z?
        Move to first non whitespace of previous line
        r   Tr   N)r   rW   rU   get_cursor_up_positionr   r   r:  rB   rB   rC   
_prev_line  s    
z$load_vi_bindings.<locals>._prev_line>c                 S  s$   | j }|jj}t|||| j  dS )z
        Indent lines.
        N)r   rU   r   r   r   )r{   r_   current_rowrB   rB   rC   _indent  s    z!load_vi_bindings.<locals>._indent<c                 S  s"   | j jj}t| j ||| j  dS )z!
        Unindent lines.
        N)r   rU   r   r   r   )r{   rK  rB   rB   rC   	_unindent  s    
z#load_vi_bindings.<locals>._unindentOc                 S  s"   | j jt  d tj| jj_dS )z:
        Open line above and enter insertion mode
        Zcopy_marginN)r   Zinsert_line_abover   r+   r   r}   r~   r   r   rB   rB   rC   _open_above  s    z%load_vi_bindings.<locals>._open_aboveoc                 S  s"   | j jt  d tj| jj_dS )z:
        Open line below and enter insertion mode
        rP  N)r   Zinsert_line_belowr   r+   r   r}   r~   r   r   rB   rB   rC   _open_below  s    z%load_vi_bindings.<locals>._open_belowr   c                 S  s4   | j }|jj}|dk	r0|dkr0|j| dd dS )zL
        Reverse case of current character and move cursor forward.
        Nr   T	overwrite)r   rU   Zcurrent_charinsert_textr   )r{   r_   r   rB   rB   rC   _reverse_case  s    z'load_vi_bindings.<locals>._reverse_casec                 S  s   | j }|dd  dS )z)
        Lowercase current line.
        c                 S  s   |   S rJ   r   r'  rB   rB   rC   r     r   z;load_vi_bindings.<locals>._lowercase_line.<locals>.<lambda>Nr   Ztransform_current_liner{   r   rB   rB   rC   _lowercase_line  s    z)load_vi_bindings.<locals>._lowercase_liner   c                 S  s   | j }|dd  dS )z)
        Uppercase current line.
        c                 S  s   |   S rJ   r   rX  rB   rB   rC   r     r   z;load_vi_bindings.<locals>._uppercase_line.<locals>.<lambda>NrY  rZ  rB   rB   rC   _uppercase_line  s    z)load_vi_bindings.<locals>._uppercase_linec                 S  s   | j }|dd  dS )z0
        Swap case of the current line.
        c                 S  s   |   S rJ   r   rX  rB   rB   rC   r     r   z:load_vi_bindings.<locals>._swapcase_line.<locals>.<lambda>NrY  rZ  rB   rB   rC   _swapcase_line   s    z(load_vi_bindings.<locals>._swapcase_line#c                 S  s8   | j }| jj}|j |_tj|_|j	|| j
dd dS )z9
        Go to previous occurrence of this word.
        Fr   include_current_positionN)r   r}   current_search_staterU   get_word_under_cursorre   r-   ZBACKWARD	directionapply_searchr   r{   r   search_staterB   rB   rC   _prev_occurence  s
    z)load_vi_bindings.<locals>._prev_occurence*c                 S  s8   | j }| jj}|j |_tj|_|j	|| j
dd dS )z5
        Go to next occurrence of this word.
        Fr_  N)r   r}   ra  rU   rb  re   r-   FORWARDrc  rd  r   re  rB   rB   rC   _next_occurance  s
    z)load_vi_bindings.<locals>._next_occurance(c                 S  s   d S rJ   rB   r   rB   rB   rC   _begin_of_sentence"  s    z,load_vi_bindings.<locals>._begin_of_sentence)c                 S  s   d S rJ   rB   r   rB   rB   rC   _end_of_sentence(  s    z*load_vi_bindings.<locals>._end_of_sentencec                 S  s   | j j  dS )zF
        Unknown key binding while waiting for a text object.
        N)r}   outputbellr   rB   rB   rC   _unknown_text_object1  s    z.load_vi_bindings.<locals>._unknown_text_object)delete_onlywith_registerrN   c                   sJ   rdt jd  f}nd  }|dt idddd fdd	}d
S )z
        Delete and change operators.

        :param delete_only: Create an operator that deletes, but doesn't go to insert mode.
        :param with_register: Copy the deleted text to this named register instead of the clipboard.
        r  Zcdro   rx   rD   ry   r{   r   rN   c                   sv   d }| j }|r"||\}}||_|rb|jrbrT| jd j}|tkrb|| jjj	|< n| jj
|  srtj| jj_d S r|   )r   ri   rU   re   r  r   r  r}   r~   r  r   r   r+   r   r   )r{   r   rh   r   rg   Zreg_namerr  rs  rB   rC   delete_or_change_operatorK  s    
z_load_vi_bindings.<locals>.create_delete_and_change_operators.<locals>.delete_or_change_operatorN)r,   Anyr   )rr  rs  Zhandler_keysrv  operatorru  rC   "create_delete_and_change_operators<  s
    
z<load_vi_bindings.<locals>.create_delete_and_change_operatorsTr   zCallable[[str], str]r   )ro   transform_funcr   rN   c                   s.   |d| t  @ idddd fdd}d S )Nro   rx   rD   ry   rt  c                   sR   | j }||j\}}||k rN||j| |j|   | j|jpH|j7  _dS )zX
            Apply transformation (uppercase, lowercase, rot13, swap case).
            N)r   r^   rU   Ztransform_regionrW   rH   rG   )r{   r   r   rG   rH   r{  rB   rC   rb   i  s    z=load_vi_bindings.<locals>.create_transform_handler.<locals>._)r   )ro   r{  r   rb   rx  r|  rC   create_transform_handlerf  s    z2load_vi_bindings.<locals>.create_transform_handlerrD   rt  c                 S  s(   | | j\}}|jr$| jj| dS )z-
        Yank operator. (Copy text.)
        N)ri   r   re   r}   r   r   )r{   r   rb   rh   rB   rB   rC   _yank  s    zload_vi_bindings.<locals>._yankc                 S  s6   | j d j}|tkr2|| j\}}|| jjj|< dS )z3
        Yank selection to named register.
        r5   N)r  r   r  ri   r   r}   r~   r  )r{   r   r   rb   rh   rB   rB   rC   _yank_to_register  s    z+load_vi_bindings.<locals>._yank_to_registerc                 S  s.   | j }||\}}t|||d | jd dS )z
        Indent.
        r5   r   N)r   rc   r   r   r{   r   r   r`   ra   rB   rB   rC   _indent_text_object  s    z-load_vi_bindings.<locals>._indent_text_objectc                 S  s.   | j }||\}}t|||d | jd dS )z
        Unindent.
        r5   r   N)r   rc   r   r   r  rB   rB   rC   _unindent_text_object  s    z/load_vi_bindings.<locals>._unindent_text_objectqc                 S  s$   | j }||\}}t||| dS )z
        Reshape text.
        N)r   rc   r   r  rB   rB   rC   _reshape  s    z"load_vi_bindings.<locals>._reshaper   c                 S  s   t | jjj| jdpdS )z.
        Move one word or token left.
        r   r   rD   r   rU   Zfind_start_of_previous_wordr   r   rB   rB   rC   _b  s    zload_vi_bindings.<locals>._bBc                 S  s   t | jjj| jddpdS )z.
        Move one non-blank word left
        Tr   WORDr   r  r   rB   rB   rC   _B  s     zload_vi_bindings.<locals>._B$c                 S  s   t | jj S )zL
        'c$', 'd$' and '$':  Delete/change/move until end of line.
        )rD   r   rU   r   r   rB   rB   rC   _dollar  s    z!load_vi_bindings.<locals>._dollarc                 S  s"   t | jjj| jdp| jj S )zO
        'word' forward. 'cw', 'dw', 'w': Delete/change/move one word.
        r   rD   r   rU   Zfind_next_word_beginningr   Zget_end_of_document_positionr   rB   rB   rC   _word_forward  s    
z'load_vi_bindings.<locals>._word_forwardc                 S  s$   t | jjj| jddp | jj S )zO
        'WORD' forward. 'cW', 'dW', 'W': Delete/change/move one WORD.
        Tr  r  r   rB   rB   rC   _WORD_forward  s     
z'load_vi_bindings.<locals>._WORD_forwardr+  c                 S  s,   | j jj| jd}t|r |d ndtjdS )z0
        End of 'word': 'ce', 'de', 'e'
        r   r5   r   r   r   rU   Zfind_next_word_endingr   rD   r:   r<   r{   rH   rB   rB   rC   _end_of_word  s    z&load_vi_bindings.<locals>._end_of_wordc                 S  s.   | j jj| jdd}t|r"|d ndtjdS )z0
        End of 'WORD': 'cE', 'dE', 'E'
        Tr  r5   r   r   r  r  rB   rB   rC   _end_of_WORD  s
     z&load_vi_bindings.<locals>._end_of_WORDrp   c                 S  s   | j j \}}t||S )z+
        Inner 'word': ciw and diw
        r   rU   Zfind_boundaries_of_current_wordrD   r{   rG   rH   rB   rB   rC   _inner_word  s    z%load_vi_bindings.<locals>._inner_wordc                 S  s   | j jjdd\}}t||S )z'
        A 'word': caw and daw
        T)include_trailing_whitespacer  r  rB   rB   rC   _a_word  s    
z!load_vi_bindings.<locals>._a_wordc                 S  s   | j jjdd\}}t||S )z+
        Inner 'WORD': ciW and diW
        T)r  r  r  rB   rB   rC   _inner_WORD  s    
z%load_vi_bindings.<locals>._inner_WORDc                 S  s    | j jjddd\}}t||S )z'
        A 'WORD': caw and daw
        T)r  r  r  r  rB   rB   rC   _a_WORD  s
     
z!load_vi_bindings.<locals>._a_WORDc                 S  s(   | j j }| j jj| jd}t||S )z!
        Auto paragraph.
        r   )r   rU   start_of_paragraphend_of_paragraphr   rD   r  rB   rB   rC   
_paragraph  s    z$load_vi_bindings.<locals>._paragraph^c                 S  s   t | jjjddS )z9'c^', 'd^' and '^': Soft start of line, after whitespace.Tr   rD   r   rU   r   r   rB   rB   rC   _start_of_line%  s
    z(load_vi_bindings.<locals>._start_of_line0c                 S  s   t | jjjddS )z
        'c0', 'd0': Hard start of line, before whitespace.
        (The move '0' key is implemented elsewhere, because a '0' could also change the `arg`.)
        Fr   r  r   rB   rB   rC   _hard_start_of_line.  s
    z-load_vi_bindings.<locals>._hard_start_of_lineNz
str | None)ci_startci_endinnerkeyrN   c                   sh   ddd fdd}|dkrNd dd	| d  dd	| nd |dd	| dS )
z
        Delete/Change string between this start and stop character. But keep these characters.
        This implements all the ci", ci<, ci{, ci(, di", di<, ca", ca<, ... combinations.
        rx   rD   rz   c                   s    kr.| j jjdd}| j jj dd}n | j j }| j j }|d k	r|d k	rrfdnd}t|d | || S tdS d S )NF)in_current_liner   r5   )r   rU   find_backwardsfindZfind_enclosing_bracket_leftZfind_enclosing_bracket_rightrD   )r{   rG   rH   offsetr  r  r  rB   rC   handlerC  s$       z?load_vi_bindings.<locals>.create_ci_ca_handles.<locals>.handlerNZaiTr  rB   )r  r  r  r  r  r   r  rC   create_ci_ca_handles:  s
    	z.load_vi_bindings.<locals>.create_ci_ca_handles)FT))r  r  )'r  )`r  )[])rM  rJ  ){})rk  rm  r  r  c                 S  s   | j jj| jdd}t|S )zi
        Move to previous blank-line separated section.
        Implements '{', 'c{', 'd{', 'y{'
        T)r   r   )r   rU   r  r   rD   r{   indexrB   rB   rC   _previous_sectiono  s
     z+load_vi_bindings.<locals>._previous_sectionc                 S  s   | j jj| jdd}t|S )ze
        Move to next blank-line separated section.
        Implements '}', 'c}', 'd}', 'y}'
        T)r   r   )r   rU   r  r   rD   r  rB   rB   rC   _next_sectionz  s
     z'load_vi_bindings.<locals>._next_sectionfc                 S  sH   t | jd| jj_| jjj| jd| jd}|r<t	|t
jdS t	dS dS )z
        Go to next occurrence of character. Typing 'fx' will move the
        cursor to the next occurrence of character. 'x'.
        FTr  r   r   r   Nr*   r   r}   r~   last_character_findr   rU   r  r   rD   r:   r<   r{   matchrB   rB   rC   _next_occurence  s      z)load_vi_bindings.<locals>._next_occurenceFc                 S  s2   t | jd| jj_t| jjj| jd| j	dp.dS )z
        Go to previous occurrence of character. Typing 'Fx' will move the
        cursor to the previous occurrence of character. 'x'.
        Tr  r   )
r*   r   r}   r~   r  rD   r   rU   r  r   r   rB   rB   rC   _previous_occurance  s      z-load_vi_bindings.<locals>._previous_occurancetc                 S  sL   t | jd| jj_| jjj| jd| jd}|r@t	|d t
jdS t	dS dS )zQ
        Move right to the next occurrence of c, then one char backward.
        FTr  r5   r   r   Nr  r  rB   rB   rC   _t  s      zload_vi_bindings.<locals>._tTc                 S  s>   t | jd| jj_| jjj| jd| jd}t	|r8|d ndS )zS
        Move left to the previous occurrence of c, then one char forward.
        Tr  r5   r   )
r*   r   r}   r~   r  r   rU   r  r   rD   r  rB   rB   rC   _T  s      zload_vi_bindings.<locals>._T)reverserN   c                   s(    r
dndddd fdd}dS )	z.
        Create ',' and ';' commands.
        ,;rx   rD   rz   c                   s   d}| j j}tj}|jrj|jj}|jj} r2| }|rN| jjj	|d| j
d}n| jjj|d| j
d}tj}|rzt||dS tdS dS )zB
            Repeat the last 'f'/'F'/'t'/'T' command.
            r   Tr  r   N)r}   r~   r:   r;   r  	character	backwardsr   rU   r  r   r  r<   rD   )r{   posr~   rI   charr  r  rB   rC   rb     s.        z+load_vi_bindings.<locals>.repeat.<locals>._NrB   )r  rb   r  r  rC   repeat  s    z load_vi_bindings.<locals>.repeathleftc                 S  s   t | jjj| jdS )z:
        Implements 'ch', 'dh', 'h': Cursor left.
        r   )rD   r   rU   r   r   r   rB   rB   rC   _left  s    zload_vi_bindings.<locals>._left)rp   rq   c                 S  s   t | jjj| jdtjdS )z<
        Implements 'cj', 'dj', 'j', ... Cursor up.
        r   r   )rD   r   rU   rE  r   r:   r=   r   rB   rB   rC   _down  s    zload_vi_bindings.<locals>._downc                 S  s   t | jjj| jdtjdS )z<
        Implements 'ck', 'dk', 'k', ... Cursor up.
        r   r   )rD   r   rU   rH  r   r:   r=   r   rB   rB   rC   _up  s    zload_vi_bindings.<locals>._uplr   rightc                 S  s   t | jjj| jdS )zL
        Implements 'cl', 'dl', 'l', 'c ', 'd ', ' '. Cursor right.
        r   )rD   r   rU   r   r   r   rB   rB   rC   _right  s    z load_vi_bindings.<locals>._rightHc                 S  sV   | j jj}| j}|r:|jr:|j|jjddd|j }nt	|jj
 }t|tjdS )zz
        Moves to the start of the visible region. (Below the scroll offset.)
        Implements 'cH', 'dH', 'H'.
        T)Zafter_scroll_offsetr   r   )r}   layoutcurrent_windowr   render_inforU   rX   Zfirst_visible_linerW   rY   text_before_cursorrD   r:   r=   r{   r8  r   r  rB   rB   rC   _top_of_screen  s    

 	z(load_vi_bindings.<locals>._top_of_screenMc                 S  sR   | j jj}| j}|r6|jr6|j|j d|j }nt	|jj
 }t|tjdS )zp
        Moves cursor to the vertical center of the visible region.
        Implements 'cM', 'dM', 'M'.
        r   r   )r}   r  r  r   r  rU   rX   Zcenter_visible_linerW   rY   r  rD   r:   r=   r  rB   rB   rC   _middle_of_screen&  s    

 	z+load_vi_bindings.<locals>._middle_of_screenLc                 S  sT   | j jj}| j}|r:|jr:|j|jjddd|j }nt	|jj
}t|tjdS )zT
        Moves to the end of the visible region. (Above the scroll offset.)
        T)Zbefore_scroll_offsetr   r   )r}   r  r  r   r  rU   rX   Zlast_visible_linerW   rY   Ztext_after_cursorrD   r:   r=   r  rB   rB   rC   _end_of_screen>  s    

 	z(load_vi_bindings.<locals>._end_of_screennc                 S  s.   | j }| jj}|j|d| jd}t||j S )z
        Search next.
        Fr`  r   r   r}   ra  Zget_search_positionr   rD   rW   r{   r   rf  rW   rB   rB   rC   _search_nextU  s      z&load_vi_bindings.<locals>._search_nextc                 S  s    | j j}| jj|d| jd dS )zR
        Search next in navigation mode. (This goes through the history.)
        Fr  Nr}   ra  r   rd  r   r{   rf  rB   rB   rC   _search_next2b  s      z'load_vi_bindings.<locals>._search_next2Nc                 S  s0   | j }| jj}|j| d| jd}t||j S )z"
        Search previous.
        Fr  r  r  rB   rB   rC   _search_previousm  s      z*load_vi_bindings.<locals>._search_previousc                 S  s"   | j j}| jj| d| jd dS )zV
        Search previous in navigation mode. (This goes through the history.)
        Fr  Nr  r  rB   rB   rC   _search_previous2z  s      z+load_vi_bindings.<locals>._search_previous2zc                 S  s   | j }|jj| jjj_dS )zd
        Scrolls the window to makes the current line the first line in the visible region.
        N)r   rU   r   r}   r  r  vertical_scrollr   rB   rB   rC   _scroll_top  s    z%load_vi_bindings.<locals>._scroll_topc                 S  s   d| j jj_dS )zc
        Scrolls the window to makes the current line the last line in the visible region.
        r   N)r}   r  r  r  r   rB   rB   rC   _scroll_bottom  s    	z(load_vi_bindings.<locals>._scroll_bottomc                 S  s~   | j jj}| j}|rz|jrz|j}|jd }td|jjd }d}|dkrt|	|}|| |k rt||7 }|d8 }q@qtq@||_
dS )z9
        Center Window vertically around cursor.
        r1   r   r5   N)r}   r  r  r   r  Zwindow_heightmaxrU   r   Zget_height_for_liner  )r{   r8  r   infoZscroll_heightrA  heightZline_heightrB   rB   rC   _scroll_center  s    




z(load_vi_bindings.<locals>._scroll_center%c                 S  s   | j }| jrjd| j  k r"dkr`n n:|jt| j|jj d d d}t||jj t	j
dS tdS n$|j }|rt|t	jdS tdS dS )z
        Implements 'c%', 'd%', '%, 'y%' (Move to corresponding bracket.)
        If an 'arg' has been given, go this this % position in the file.
        r   d   r5   r   N)r   r   r   rU   rX   rE   
line_countrD   rW   r:   r=   Zfind_matching_bracket_positionr<   )r{   r_   Zabsolute_indexr  rB   rB   rC   _goto_corresponding_bracket  s      


z5load_vi_bindings.<locals>._goto_corresponding_bracket|c                 S  s   t | jj| jd S )z
        Move to the n-th column (you may specify the argument n by typing it on
        number keys, for example, 20|).
        r5   )rD   r   rU   Zget_column_cursor_positionr   r   rB   rB   rC   
_to_column  s    z$load_vi_bindings.<locals>._to_columnc                 S  sF   | j j}| jr0t|| jd d|j tjdS t|	 tjdS dS )z_
        Go to the start of the very first line.
        Implements 'gg', 'cgg', 'ygg'
        r5   r   r   N)
r   rU   r   rD   rX   r   rW   r:   r=   Zget_start_of_document_position)r{   r   rB   rB   rC   _goto_first_line  s     z*load_vi_bindings.<locals>._goto_first_linerb   c                 S  s   t | jj tjdS )zQ
        Go to last non-blank of line.
        'g_', 'cg_', 'yg_', etc..
        r   )rD   r   rU   Z'last_non_blank_of_current_line_positionr:   r<   r   rB   rB   rC   _goto_last_line  s    
z)load_vi_bindings.<locals>._goto_last_linec                 S  s0   | j jj| jd}t|dk	r$|d ndtjdS )zZ
        Go to last character of previous word.
        'ge', 'cge', 'yge', etc..
        r   Nr5   r   r   r   rU   Zfind_previous_word_endingr   rD   r:   r<   r{   Zprev_endrB   rB   rC   _ge  s     zload_vi_bindings.<locals>._gec                 S  s2   | j jj| jdd}t|dk	r&|d ndtjdS )zZ
        Go to last character of previous WORD.
        'gE', 'cgE', 'ygE', etc..
        Tr  Nr5   r   r   r  r  rB   rB   rC   _gE
  s      zload_vi_bindings.<locals>._gEmc                 S  sd   | j jj}| j}|r\|jr\|jj}|jjdd}|tt	|d t
|jj7 }t|tjdS tdS )zY
        Like g0, but half a screenwidth to the right. (Or as much as possible.)
        Fr   r1   r   r   )r}   r  r  r   r  Zwindow_widthrU   r   rE   r<  rY   r   rD   r:   r<   )r{   r8  r   widthrG   rB   rB   rC   _gm  s    

zload_vi_bindings.<locals>._gmGc                 S  s,   | j }t|j|jjd d|j tjdS )zL
        Go to the end of the document. (If no arg has been given.)
        r5   r   r   )r   rD   rU   rX   r  rW   r:   r=   )r{   bufrB   rB   rC   
_last_line'  s    z$load_vi_bindings.<locals>._last_linec                 S  s   | j | jd  dS )zh
        If an argument is given, move to this line in the  history. (for
        example, 15G)
        r5   N)r   Zgo_to_historyr   r   rB   rB   rC   _to_nth_history_line7  s    z.load_vi_bindings.<locals>._to_nth_history_lineZ	123456789c                 S  s   |  | j dS )zK
            Always handle numerics in navigation mode as arg.
            NZappend_to_arg_countr   r   rB   rB   rC   r   A  s    
zload_vi_bindings.<locals>._argc                 S  s   |  | j dS )z9
        Zero when an argument was already give.
        Nr	  r   rB   rB   rC   _0_argM  s    z load_vi_bindings.<locals>._0_argc                 S  s   | j j| jdd dS )z1
        Insert data at cursor position.
        TrT  N)r   rV  r   r   rB   rB   rC   _insert_textZ  s    z&load_vi_bindings.<locals>._insert_textc                 S  s2   | j j| jdd | j  jd8  _tj| jj_dS )z>
        Replace single character at cursor position.
        TrT  r5   N)	r   rV  r   rW   r+   r   r}   r~   r   r   rB   rB   rC   _replace_singlea  s    z)load_vi_bindings.<locals>._replace_singlec                 S  s   | j  S rJ   )Z	is_repeatr*  rB   rB   rC   r   m  r   c                 S  s   | j }|j}g }d}|jD ]&}||||  || j |}q|||d  dd t|jD }d||_||_| jd7  _dS )z
        Insert data at multiple cursor positions at once.
        (Usually a result of pressing 'I' or 'A' in block-selection mode.)
        r   Nc                 S  s   g | ]\}}|| d  qS )r5   rB   ).0r   r  rB   rB   rC   
<listcomp>  s    zKload_vi_bindings.<locals>._insert_text_multiple_cursors.<locals>.<listcomp>r  r5   )r   re   r  r  r   r  r   rW   )r{   r   original_textre   r  p2new_cursor_positionsrB   rB   rC   _insert_text_multiple_cursorsj  s    

z7load_vi_bindings.<locals>._insert_text_multiple_cursorsc           	      S  s   | j }|j}d}g }d}|jD ]N}|dkrV||d  dkrV||||d   d}n||||  |}q|||d  |rdd |dd	 D }tt|}d
||_||_| jd8  _n| jj	
  dS )z4
        Backspace, using multiple cursors.
        Fr   r5   r   TNc                 S  s   g | ]}t |qS rB   rY   r  partrB   rB   rC   r    s     zMload_vi_bindings.<locals>._delete_before_multiple_cursors.<locals>.<listcomp>r  )r   re   r  r  listr   r   rW   r}   ro  rp  )	r{   r   r  deleted_somethingre   r  r  lengthsr  rB   rB   rC   _delete_before_multiple_cursors  s&    
z9load_vi_bindings.<locals>._delete_before_multiple_cursorsr   c           	      S  s   | j }|j}d}g }g }d}|jD ]@}||||  |t|ksP|| dkrV|}q"|d }d}q"|||d  |rdd |dd	 D }tt|}d
||_||_n| jj	
  dS )z1
        Delete, using multiple cursors.
        Fr   r   r5   TNc                 S  s   g | ]}t |qS rB   r  r  rB   rB   rC   r    s     zLload_vi_bindings.<locals>._delete_after_multiple_cursors.<locals>.<listcomp>r  r  )r   re   r  r  rY   r  r   r   r}   ro  rp  )	r{   r   r  r  re   r  r  r  r  rB   rB   rC   _delete_after_multiple_cursors  s&    
z8load_vi_bindings.<locals>._delete_after_multiple_cursorsc                 S  s`   | j }g }|jD ]*}|j|d dkr0|d8 }|| q||_|jjdkr\| jd8  _dS )z`
        Move all cursors to the left.
        (But keep all cursors on the same line.)
        r5   r   N)r   r  rU   rV   r  Zcursor_position_colrW   )r{   r   new_positionsr  rB   rB   rC   _left_multiple  s    
z(load_vi_bindings.<locals>._left_multiplec                 S  sl   | j }g }|jD ]:}|j|\}}|t|jj| k r@|d7 }|| q||_|jjsh| jd7  _dS )za
        Move all cursors to the right.
        (But keep all cursors on the same line.)
        r5   N)	r   r  rU   rV   rY   rZ   r  Zis_cursor_at_the_end_of_linerW   )r{   r   r  r  r\   columnrB   rB   rC   _right_multiple  s    
z)load_vi_bindings.<locals>._right_multiplec                 S  s   dS )zN
        Ignore all up/down key presses when in multiple cursor mode.
        NrB   r   rB   rB   rC   _updown_multiple  s    z*load_vi_bindings.<locals>._updown_multiplezc-xzc-lc                 S  s   | j   dS )z
        Pressing the ControlX - ControlL sequence in Vi mode does line
        completion based on the other lines in the document and the history.
        N)r   Zstart_history_lines_completionr   rB   rB   rC   _complete_line  s    z(load_vi_bindings.<locals>._complete_linezc-fc                 S  s   dS )z&
        Complete file names.
        NrB   r   rB   rB   rC   _complete_filename	  s    z,load_vi_bindings.<locals>._complete_filenamezc-kc                 S  s   d| j j_dS )z'
        Go into digraph mode.
        TN)r}   r~   waiting_for_digraphr   rB   rB   rC   _digraph  s    z"load_vi_bindings.<locals>._digraphc                   S  s   t  jjd k	S rJ   )r   r~   digraph_symbol1rB   rB   rB   rC   digraph_symbol_1_given  s    z0load_vi_bindings.<locals>.digraph_symbol_1_givenc                 S  s   | j | jj_dS )z'
        First digraph symbol.
        N)r   r}   r~   r%  r   rB   rB   rC   	_digraph1  s    z#load_vi_bindings.<locals>._digraph1c                 S  s   zz6| j jjpd| jf}|tkr.|ddd }t| }W n  tk
rX   | j j  Y n0X | j jj	t
jk}| jjt||d d| j j_W 5 d| j j_d| j j_X dS )z!
        Insert digraph.
        FNr  r  rT  )r}   r~   r#  r%  r   r'   KeyErrorro  rp  r   r+   r   r   rV  chr)r{   codesymbolrU  rB   rB   rC   _create_digraph#  s    
z)load_vi_bindings.<locals>._create_digraphzc-oc                 S  s   d| j j_dS )z<
        Go into normal mode for one single action.
        TN)r}   r~   Ztemporary_navigation_moder   rB   rB   rC   _quick_normal_mode=  s    z,load_vi_bindings.<locals>._quick_normal_modec                 S  s,   | j d j}|tkr(| jj}||_d|_dS )z(
        Start recording macro.
        r5   r  N)r  r   r  r}   r~   recording_registercurrent_recording)r{   r   r~   rB   rB   rC   _start_macroD  s
    z&load_vi_bindings.<locals>._start_macroc                 S  s0   | j j}|jr,t|j|j|j< d|_d|_dS )z'
        Stop recording macro.
        Nr  )r}   r~   r.  r   r/  r  )r{   r~   rB   rB   rC   _stop_macroP  s    z%load_vi_bindings.<locals>._stop_macro@)ro   Zrecord_in_macroc                 S  s~   | j d j}z| jjj| }W n tk
r4   Y dS X g }t|j}||j	 |
  t| jD ]}| jjj|dd qbdS )al  
        Execute macro.

        Notice that we pass `record_in_macro=False`. This ensures that the `@x`
        keys don't appear in the recording itself. This function inserts the
        body of the called macro back into the KeyProcessor, so these keys will
        be added later on to the macro of their handlers have
        `record_in_macro=True`.
        r5   NT)first)r  r   r}   r~   r  r(  r&   r  feedre   flushr  r   Zkey_processorZfeed_multiple)r{   r   macroru   parserrb   rB   rB   rC   _execute_macro_  s    
z(load_vi_bindings.<locals>._execute_macro)F)F)N)r3   r   r	   r   r   r   r   r   r   r6   r$   r   r   r   r,   rw  r   r   r%   r   r!   r"   r   r   r    r2   r   )rn   handleZTransformFunctionZvi_transform_functionsr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r
  r  r  r  r  r  r!  r"  r$  r&  r)  r,  r0  r1  r3  r5  r6  r7  r;  r>  r@  rC  rF  rI  rL  rN  rQ  rS  rW  r[  r\  r]  rg  rj  rl  rn  rq  rz  r}  r   r  funcr~  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r   r  r  r  r  r  r   r
  r  r  r  r  r  r  r  r   r!  r"  r$  r&  r'  r,  r-  r0  r1  r8  rB   )r  ry  r   rC   r7   v  s    





	











 






	

















%



							$&
	


$		

		





"#

c                  C  s.  t  } | j}ddlm} tdddd}|dttB t @ d|j |d	ttB t@ d|j |d
|j |d	ttB t @ d|j	 |dttB t@ d|j	 |d|j	 |dt
d|j |dt
d|j |d
t
d|j |d|j |d|j |d|d|j |d|j t| tS )Nr5   )searchrt   rM   c                   S  s   t  jjdkS )z-Returns True when the search buffer is empty.r  )r   r   re   rB   rB   rB   rC   search_buffer_is_empty  s    z7load_vi_search_bindings.<locals>.search_buffer_is_empty/r   r   zc-szc-rr   zc-cr   r   r   )r3   r   r  r;  r   r   r$   r#   Z start_forward_incremental_searchZ start_reverse_incremental_searchr   Zaccept_searchZreverse_incremental_searchZforward_incremental_searchZabort_searchr2   r   )rn   r9  r;  r<  rB   rB   rC   r8     sN    

)V
__future__r   r   r   enumr   	itertoolsr   typingr   r   r   r   r	   r
   r   Z"prompt_toolkit.application.currentr   Zprompt_toolkit.bufferr   r   r   r   Zprompt_toolkit.clipboardr   Zprompt_toolkit.documentr   Zprompt_toolkit.filtersr   r   r   r   r   r   Zprompt_toolkit.filters.appr   r   r   r   r   r   r   r    r!   r"   r#   r$   r%   Z!prompt_toolkit.input.vt100_parserr&   Z#prompt_toolkit.key_binding.digraphsr'   Z(prompt_toolkit.key_binding.key_processorr(   r)   Z#prompt_toolkit.key_binding.vi_stater*   r+   Zprompt_toolkit.keysr,   Zprompt_toolkit.searchr-   Zprompt_toolkit.selectionr.   r/   r0   rn   r2   r3   r4   Znamed_commandsr6   __all__rx   ascii_lowercaser  r:   rD   ZTextObjectFunctionrl   r   ZOperatorFunctionr   r   r7   r8   rB   rB   rB   rC   <module>   sd   $ <iwK              