
    MhQ                    >   d 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mZmZ ddlmZmZ e
rd	d
lmZ eZ g dZ!edgedeeedf         f         f         Z" G d d          Z#eeee$f         df         Z% G d de          Z& edee"e#f                   Z' G d de&          Z(d7dZ)dddd dfd8d(Z* G d) d*e&          Z+ G d+ d,e+          Z, G d- d.e+          Z-d9d1Z. G d2 d3e+          Z/ G d4 d5e+          Z0d6S ):aF  
Key bindings registry.

A `KeyBindings` object is a container that holds a list of key bindings. It has a
very efficient internal data structure for checking which key bindings apply
for a pressed key.

Typical usage::

    kb = KeyBindings()

    @kb.add(Keys.ControlX, Keys.ControlC, filter=INSERT)
    def handler(event):
        # Handle ControlX-ControlC key sequence.
        pass

It is also possible to combine multiple KeyBindings objects. We do this in the
default key bindings. There are some KeyBindings objects that contain the Emacs
bindings, while others contain the Vi bindings. They are merged together using
`merge_key_bindings`.

We also have a `ConditionalKeyBindings` object that can enable/disable a group of
key bindings at once.


It is also possible to add a filter to a function, before a key binding has
been assigned, through the `key_binding` decorator.::

    # First define a key handler with the `filter`.
    @key_binding(filter=condition)
    def my_key_binding(event):
        ...

    # Later, add it to the key bindings.
    kb.add(Keys.A, my_key_binding)
    )annotations)ABCMetaabstractmethodabstractproperty)isawaitable)
TYPE_CHECKINGAnyCallable	CoroutineHashableSequenceTupleTypeVarUnioncast)SimpleCache)FilterOrBoolNever	to_filter)KEY_ALIASESKeys   )KeyPressEvent)NotImplementedOrNoneBindingKeyBindingsBaseKeyBindingsConditionalKeyBindingsmerge_key_bindingsDynamicKeyBindingsGlobalOnlyKeyBindingsr   r   c                  8    e Zd ZdZdddd dfddZddZddZdS )r   z
    Key binding: (key sequence + handler + filter).
    (Immutable binding class.)

    :param record_in_macro: When True, don't record this key binding when a
        macro is recorded.
    TFc                    dS NT es    g/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/prompt_toolkit/key_binding/key_bindings.py<lambda>zBinding.<lambda>s       $     keystuple[Keys | str, ...]handlerKeyHandlerCallablefilterr   eager	is_globalsave_beforeCallable[[KeyPressEvent], bool]record_in_macroreturnNonec                    || _         || _        t          |          | _        t          |          | _        t          |          | _        || _        t          |          | _        d S N)r,   r.   r   r0   r1   r2   r3   r5   )selfr,   r.   r0   r1   r2   r3   r5   s           r(   __init__zBinding.__init__l   s]     	''u%%
"9--&(99r+   eventr   c                D   |                                }t          |          rSt          t          t          t          df         |          dfd}j                             |                       d S |t          k    rj                                         d S d S )Nr   r6   r7   c                 h   K    d {V } | t           k    rj                                         d S d S r9   )NotImplementedapp
invalidate)result	awaitabler<   s    r(   bg_taskzBinding.call.<locals>.bg_task   sE      (^++I((***** ,+r+   r6   r7   )	r.   r   r   r   r	   r@   create_background_taskr?   rA   )r:   r<   rB   rD   rC   s    `  @r(   callzBinding.call~   s    e$$ v 	#YsC1G'GH&QQI+ + + + + + +
 I,,WWYY77777~%%I  """"" &%r+   strc                @    | j         j         d| j        d| j        dS )Nz(keys=z
, handler=))	__class____name__r,   r.   r:   s    r(   __repr__zBinding.__repr__   s,    ~&VVdiVVT\VVV	
r+   N)r,   r-   r.   r/   r0   r   r1   r   r2   r   r3   r4   r5   r   r6   r7   )r<   r   r6   r7   )r6   rH   )rL   
__module____qualname____doc__r;   rG   rN   r%   r+   r(   r   r   c   sr           $#"'8F(,: : : : :$# # # #"
 
 
 
 
 
r+   r   .c                  r    e Zd ZdZedd            Zedd            Zedd	            Zedd
            Z	dS )r   z&
    Interface for a KeyBindings.
    r6   r   c                    dS )zk
        For cache invalidation. - This should increase every time that
        something changes.
        r   r%   rM   s    r(   _versionzKeyBindingsBase._version   s	     qr+   r,   	KeysTuplelist[Binding]c                    g S )z
        Return a list of key bindings that can handle these keys.
        (This return also inactive bindings, so the `filter` still has to be
        called, for checking it.)

        :param keys: tuple of keys.
        r%   r:   r,   s     r(   get_bindings_for_keysz%KeyBindingsBase.get_bindings_for_keys   s	     	r+   c                    g S )2  
        Return a list of key bindings that handle a key sequence starting with
        `keys`. (It does only return bindings for which the sequences are
        longer than `keys`. And like `get_bindings_for_keys`, it also includes
        inactive bindings.)

        :param keys: tuple of keys.
        r%   rX   s     r(   get_bindings_starting_with_keysz/KeyBindingsBase.get_bindings_starting_with_keys   s	     	r+   c                    g S )z
        List of `Binding` objects.
        (These need to be exposed, so that `KeyBindings` objects can be merged
        together.)
        r%   rM   s    r(   bindingszKeyBindingsBase.bindings   s	     	r+   Nr6   r   r,   rU   r6   rV   r6   rV   )
rL   rO   rP   rQ   r   rT   r   rY   r\   r^   r%   r+   r(   r   r      s                 ^ 	 	 	 ^	      r+   r   )	metaclassT)boundc                      e Zd ZdZd dZd dZed!d            Zed"d	            Zd
ddd d
dd#dZ	d$dZ
e	Ze
Zd%dZd%dZdS )&r   a  
    A container for a set of key bindings.

    Example usage::

        kb = KeyBindings()

        @kb.add('c-t')
        def _(event):
            print('Control-T pressed')

        @kb.add('c-a', 'c-b')
        def _(event):
            print('Control-A pressed, followed by Control-B')

        @kb.add('c-x', filter=is_searching)
        def _(event):
            print('Control-X pressed')  # Works only if we are searching.

    r6   r7   c                v    g | _         t          d          | _        t          d          | _        d| _        d S )Ni'  )maxsizei  r   )	_bindingsr   _get_bindings_for_keys_cache&_get_bindings_starting_with_keys_cache_KeyBindings__versionrM   s    r(   r;   zKeyBindings.__init__   sD    (*&&& 	)
 %%% 	3 r+   c                    | xj         dz  c_         | j                                         | j                                         d S )Nr   )rk   ri   clearrj   rM   s    r(   _clear_cachezKeyBindings._clear_cache   sB    !)//111399;;;;;r+   rV   c                    | j         S r9   )rh   rM   s    r(   r^   zKeyBindings.bindings   
    ~r+   r   c                    | j         S r9   )rk   rM   s    r(   rT   zKeyBindings._version   rp   r+   TFc                    dS r$   r%   r&   s    r(   r)   zKeyBindings.<lambda>   r*   r+   r0   r1   r2   r3   r5   r,   
Keys | strr0   r   r1   r2   r3   r4   r5   Callable[[T], T]c                    sJ t          d D                       t          t                    rdd}nd fd}|S )a  
        Decorator for adding a key bindings.

        :param filter: :class:`~prompt_toolkit.filters.Filter` to determine
            when this key binding is active.
        :param eager: :class:`~prompt_toolkit.filters.Filter` or `bool`.
            When True, ignore potential longer matches when this key binding is
            hit. E.g. when there is an active eager key binding for Ctrl-X,
            execute the handler immediately and ignore the key binding for
            Ctrl-X Ctrl-E of which it is a prefix.
        :param is_global: When this key bindings is added to a `Container` or
            `Control`, make it a global (always active) binding.
        :param save_before: Callable that takes an `Event` and returns True if
            we should save the current buffer, before handling the event.
            (That's the default.)
        :param record_in_macro: Record these key bindings when a macro is
            being recorded. (True by default.)
        c              3  4   K   | ]}t          |          V  d S r9   
_parse_key.0ks     r(   	<genexpr>z"KeyBindings.add.<locals>.<genexpr>  s(      11qZ]]111111r+   funcrc   r6   c                    | S r9   r%   )r~   s    r(   	decoratorz"KeyBindings.add.<locals>.decorator  s    r+   c                   t          | t                    r}j                            t          | j        | j        t                    z  t                    | j        z  t                    | j        z  | j	        | j
                             nAj                            t          t          t          |                                                                 | S Nrs   )
isinstancer   r^   appendr.   r0   r   r1   r2   r3   r5   r   r/   rn   )r~   r1   r0   r2   r,   r5   r3   r:   s    r(   r   z"KeyBindings.add.<locals>.decorator!  s    dG,, M((  L#';61B1B#B"+E"2"2TZ"?&/	&:&:T^&K(,(8,0,@  
 
 
 
 M((  !3T::#)"'&/(3,;  
 
 
 !!###r+   )r~   rc   r6   rc   )tupler   r   )r:   r0   r1   r2   r3   r5   r,   r   s   ``````` r(   addzKeyBindings.add   s    6 11D11111fe$$ %	    
           < r+   argsKeys | str | KeyHandlerCallablec                D   d}t          |d                   rOt          |          dk    sJ |d         }| j        D ])}|j        |k    r| j                            |           d}*nt          |          dk    sJ t          t          t          t          t          f                  |          }t          d |D                       }| j        D ])}|j        |k    r| j                            |           d}*|r|                                  dS t          d|          )aX  
        Remove a key binding.

        This expects either a function that was given to `add` method as
        parameter or a sequence of key bindings.

        Raises `ValueError` when no bindings was found.

        Usage::

            remove(handler)  # Pass handler.
            remove('c-x', 'c-a')  # Or pass the key bindings.
        Fr   r   Tc              3  4   K   | ]}t          |          V  d S r9   rx   rz   s     r(   r}   z%KeyBindings.remove.<locals>.<genexpr>`  s(      551A555555r+   zBinding not found: N)callablelenr^   r.   remover   r   r   r   rH   r   r,   rn   
ValueError)r:   r   foundfunctionbr,   s         r(   r   zKeyBindings.removeA  s<    DG 	!t99>>>>AwH ] ! !9((M((+++ E! t99q====eD#I./66D 5555555D] ! !6T>>M((+++ E 	A ?8??@@@r+   rU   c                J     d fd} j                             |          S )z
        Return a list of key bindings that can handle this key.
        (This return also inactive bindings, so the `filter` still has to be
        called, for checking it.)

        :param keys: tuple of keys.
        r6   rV   c                 p   g } j         D ]}t                    t          |j                  k    rgd}d}t          |j                  D ]4\  }}||k    r|t          j        k    rd} n|t          j        k    r|dz  }5|r|                     ||f           t          | d           } d | D             S )NTr   Fr   c                    | d          S Nr   r%   )items    r(   r)   z@KeyBindings.get_bindings_for_keys.<locals>.get.<locals>.<lambda>  s    d1gX r+   keyc                    g | ]
}|d          S )r   r%   )r{   r   s     r(   
<listcomp>zBKeyBindings.get_bindings_for_keys.<locals>.get.<locals>.<listcomp>  s    ///DG///r+   )r^   r   r,   zipr   r	   r   sorted)rB   r   match	any_countijr,   r:   s         r(   getz.KeyBindings.get_bindings_for_keys.<locals>.getz  s    02F] 6 6t99AF++ E !I #AFD 1 1 + +166a48mm$)E!E==%NI 6y!n555 F(=(=>>>F//////r+   ra   )ri   r   r:   r,   r   s   `` r(   rY   z!KeyBindings.get_bindings_for_keysq  sB    	0 	0 	0 	0 	0 	0 	00 044T3???r+   c                J     d fd} j                             |          S )r[   r6   rV   c                    g } j         D ]u}t                    t          |j                  k     rNd}t          |j                  D ]\  }}||k    r|t          j        k    rd} n |r|                     |           v| S )NTF)r^   r   r,   r   r   r	   r   )rB   r   r   r   r   r,   r:   s        r(   r   z8KeyBindings.get_bindings_starting_with_keys.<locals>.get  s    F] ) )t99s16{{** E #AFD 1 1 " "166a48mm$)E!E )a(((Mr+   ra   )rj   r   r   s   `` r(   r\   z+KeyBindings.get_bindings_starting_with_keys  sB    	 	 	 	 	 	 	 :>>tSIIIr+   NrE   ra   r_   )r,   rt   r0   r   r1   r   r2   r   r3   r4   r5   r   r6   ru   )r   r   r6   r7   r`   )rL   rO   rP   rQ   r;   rn   propertyr^   rT   r   r   add_bindingremove_bindingrY   r\   r%   r+   r(   r   r      s        *   < < < <
    X    X  $#"'8F(,F F F F F FP*A *A *A *AZ KN!@ !@ !@ !@FJ J J J J Jr+   r   r   rt   r6   
str | Keysc                    t          | t                    r| S t          j        | |           } | dk    rd} 	 t          |           S # t          $ r Y nw xY wt          |           dk    rt	          d|            | S )zC
    Replace key by alias and verify whether it's a valid one.
    space r   zInvalid key: )r   r   r   r   r   r   r   s    r(   ry   ry     s    
 #t 
 /#s
#
#C g~~Cyy    3xx1}}...///Js   A 
AATFc                    dS r$   r%   )r<   s    r(   r)   r)     r*   r+   r0   r   r1   r2   r3   r4   r5   'Callable[[KeyHandlerCallable], Binding]c                     t                    sJ t                      t                    t                    t                    dd fd}|S )	z
    Decorator that turn a function into a `Binding` object. This can be added
    to a `KeyBindings` object when a key binding is assigned.
    Nr%   r   r/   r6   r   c           	     0    t          |           S r   )r   )r   r1   r0   r2   r,   r5   r3   s    r(   r   zkey_binding.<locals>.decorator  s/    #+
 
 
 	
r+   )r   r/   r6   r   )r   r   )r0   r1   r2   r3   r5   r   r,   s   ````` @r(   key_bindingr     s     (;"7"7vFeE)$$IK00OD	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 r+   c                  b    e Zd ZdZddZddZedd            Zedd	            ZddZ	ddZ
dS )_ProxyzH
    Common part for ConditionalKeyBindings and _MergedKeyBindings.
    r6   r7   c                :    t                      | _        d| _        d S )Nr%   )r   
_bindings2_last_versionrM   s    r(   r;   z_Proxy.__init__  s    +6==')r+   c                    t           )zy
        If `self._last_version` is outdated, then this should update
        the version and `self._bindings2`.
        )NotImplementedErrorrM   s    r(   _update_cachez_Proxy._update_cache  s
    
 "!r+   rV   c                B    |                                   | j        j        S r9   )r   r   r^   rM   s    r(   r^   z_Proxy.bindings  s    ''r+   r   c                8    |                                   | j        S r9   )r   r   rM   s    r(   rT   z_Proxy._version  s    !!r+   r,   rU   c                ^    |                                   | j                            |          S r9   )r   r   rY   rX   s     r(   rY   z_Proxy.get_bindings_for_keys	  s*    44T:::r+   c                ^    |                                   | j                            |          S r9   )r   r   r\   rX   s     r(   r\   z&_Proxy.get_bindings_starting_with_keys  s*    >>tDDDr+   NrE   ra   r_   r`   )rL   rO   rP   rQ   r;   r   r   r^   rT   rY   r\   r%   r+   r(   r   r     s         * * * *
" " " " ( ( ( X( " " " X"; ; ; ;E E E E E Er+   r   c                  &    e Zd ZdZ	 ddd	Zdd
ZdS )r   a  
    Wraps around a `KeyBindings`. Disable/enable all the key bindings according to
    the given (additional) filter.::

        @Condition
        def setting_is_true():
            return True  # or False

        registry = ConditionalKeyBindings(key_bindings, setting_is_true)

    When new key bindings are added to this object. They are also
    enable/disabled according to the given `filter`.

    :param registries: List of :class:`.KeyBindings` objects.
    :param filter: :class:`~prompt_toolkit.filters.Filter` object.
    Tkey_bindingsr   r0   r   r6   r7   c                p    t                               |            || _        t          |          | _        d S r9   )r   r;   r   r   r0   )r:   r   r0   s      r(   r;   zConditionalKeyBindings.__init__$  s1     	(''r+   c                @   | j         j        }| j        |k    rt                      }| j         j        D ][}|j                            t          |j        |j        | j	        |j	        z  |j
        |j        |j        |j                             \|| _        || _        dS dS )zBIf the original key bindings was changed. Update our copy version.)r,   r.   r0   r1   r2   r3   r5   N)r   rT   r   r   r^   r   r   r,   r.   r0   r1   r2   r3   r5   r   r:   expected_version	bindings2r   s       r(   r   z$ConditionalKeyBindings._update_cache,  s    ,5!111#I &/  "))V !	#{QX5g"#+$%M()(9  
 
 
 
 (DO!1D% 21r+   N)T)r   r   r0   r   r6   r7   rE   rL   rO   rP   rQ   r;   r   r%   r+   r(   r   r     sR         $ EI( ( ( ( (2 2 2 2 2 2r+   r   c                  "    e Zd ZdZd	dZd
dZdS )_MergedKeyBindingsa  
    Merge multiple registries of key bindings into one.

    This class acts as a proxy to multiple :class:`.KeyBindings` objects, but
    behaves as if this is just one bigger :class:`.KeyBindings`.

    :param registries: List of :class:`.KeyBindings` objects.
    
registriesSequence[KeyBindingsBase]r6   r7   c                H    t                               |            || _        d S r9   )r   r;   r   )r:   r   s     r(   r;   z_MergedKeyBindings.__init__O  s    $r+   c                    t          d | j        D                       }| j        |k    rGt                      }| j        D ]!}|j                            |j                   "|| _        || _        dS dS )c
        If one of the original registries was changed. Update our merged
        version.
        c              3  $   K   | ]}|j         V  d S r9   )rT   )r{   rs     r(   r}   z3_MergedKeyBindings._update_cache.<locals>.<genexpr>X  s$       E E E E E E E Er+   N)r   r   r   r   r^   extendr   )r:   r   r   regs       r(   r   z _MergedKeyBindings._update_cacheS  s    
 ! E ET_ E E EEE!111#I 8 8"))#,7777'DO!1D 21r+   N)r   r   r6   r7   rE   r   r%   r+   r(   r   r   E  sF         % % % %2 2 2 2 2 2r+   r   r^   r   c                     t          |           S )z
    Merge multiple :class:`.Keybinding` objects together.

    Usage::

        bindings = merge_key_bindings([bindings1, bindings2, ...])
    )r   )r^   s    r(   r   r   d  s     h'''r+   c                  "    e Zd ZdZd	dZd
dZdS )r    z
    KeyBindings class that can dynamically returns any KeyBindings.

    :param get_key_bindings: Callable that returns a :class:`.KeyBindings` instance.
    get_key_bindings$Callable[[], KeyBindingsBase | None]r6   r7   c                V    || _         d| _        d | _        t                      | _        d S r   )r   _DynamicKeyBindings__version_last_child_versionr   _dummy)r:   r   s     r(   r;   zDynamicKeyBindings.__init__v  s(     0#' !mmr+   c                    |                                  p| j        }t          |t                    sJ t	          |          |j        f}|| _        || _        d S r9   )r   r   r   r   idrT   r   r   )r:   r   versions      r(   r   z DynamicKeyBindings._update_cache|  sZ    ,,..=$+,88888\""L$99&$r+   N)r   r   r6   r7   rE   r   r%   r+   r(   r    r    o  sF         $ $ $ $% % % % % %r+   r    c                  "    e Zd ZdZd	dZd
dZdS )r!   zf
    Wrapper around a :class:`.KeyBindings` object that only exposes the global
    key bindings.
    r   r   r6   r7   c                H    t                               |            || _        d S r9   )r   r;   r   )r:   r   s     r(   r;   zGlobalOnlyKeyBindings.__init__  s"    (r+   c                    | j         j        }| j        |k    r[t                      }| j         j        D ]0}|                                r|j                            |           1|| _        || _        dS dS )r   N)r   rT   r   r   r^   r2   r   r   r   s       r(   r   z#GlobalOnlyKeyBindings._update_cache  s    
  ,5!111#I&/ 1 1;;== 1&--a000'DO!1D 21r+   N)r   r   r6   r7   rE   r   r%   r+   r(   r!   r!     sF         
) ) ) )2 2 2 2 2 2r+   r!   N)r   rt   r6   r   )r0   r   r1   r   r2   r   r3   r4   r5   r   r6   r   )r^   r   r6   r   )1rQ   
__future__r   abcr   r   r   inspectr   typingr   r	   r
   r   r   r   r   r   r   r   prompt_toolkit.cacher   prompt_toolkit.filtersr   r   r   prompt_toolkit.keysr   r   key_processorr   objectr   __all__r/   r   rH   rU   r   rc   r   ry   r   r   r   r   r   r    r!   r%   r+   r(   <module>r      s  # #J # " " " " " 9 9 9 9 9 9 9 9 9 9                              - , , , , , A A A A A A A A A A 1 1 1 1 1 1 1 1 ",,,,,, "	 	 	 	
 )C6L,L"M
MNP /
 /
 /
 /
 /
 /
 /
 /
f %c	"C'(	+ + + + + + + + +` GCu/89:::_J _J _J _J _J/ _J _J _JD   :  #4F4F$(    D#E #E #E #E #E_ #E #E #EL02 02 02 02 02V 02 02 02f2 2 2 2 2 2 2 2>( ( ( (% % % % % % % %,2 2 2 2 2F 2 2 2 2 2r+   