
    fD                       d Z ddlmZ ddl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 dd	lmZ dd
lmZ ddlmZ ddlmZmZ e
rddlmZ ddlmZ g dZ G d d      Z	  edd      Z  G d d      Z! G d d      Z"y)a%  
An :class:`~.KeyProcessor` receives callbacks for the keystrokes parsed from
the input in the :class:`~prompt_toolkit.inputstream.InputStream` instance.

The `KeyProcessor` will according to the implemented keybindings call the
correct callbacks when new key presses are feed through `feed`.
    )annotationsN)Tasksleep)deque)TYPE_CHECKINGAny	Generator)get_app)EditingMode)vi_navigation_mode)Keys)Event   )BindingKeyBindingsBase)Application)Buffer)KeyProcessorKeyPressKeyPressEventc                  *    e Zd ZdZdddZddZd	dZy)
r   z
    :param key: A `Keys` instance or text (one character).
    :param data: The received string on stdin. (Often vt100 escape codes.)
    Nc                    t        |t              st        |      dk(  sJ |t        |t              r|j                  }n|}|| _        || _        y )Nr   )
isinstancer   lenvaluekeydata)selfr   r   s      c/var/www/cvtools/html/venv/lib/python3.12/site-packages/prompt_toolkit/key_binding/key_processor.py__init__zKeyPress.__init__*   sG    #t$CA55<#t$yy	    c                h    | j                   j                   d| j                  d| j                  dS )Nz(key=z, data=))	__class____name__r   r   r   s    r   __repr__zKeyPress.__repr__6   s.    ..))*%|7499-qQQr!   c                    t        |t              sy| j                  |j                  k(  xr | j                  |j                  k(  S )NF)r   r   r   r   )r   others     r   __eq__zKeyPress.__eq__9   s4    %*xx599$@ejj)@@r!   N)r   z
Keys | strr   
str | NonereturnNoner-   str)r)   objectr-   bool)r%   
__module____qualname____doc__r    r'   r*    r!   r   r   r   $   s    

RAr!   r   ?_Flush)r   c                      e Zd ZdZddZddZddZddZddZdddZ	dddZ
dd	Zdd
ZddZddZddZddZddZy)r   aP  
    Statemachine that receives :class:`KeyPress` instances and according to the
    key bindings in the given :class:`KeyBindings`, calls the matching handlers.

    ::

        p = KeyProcessor(key_bindings)

        # Send keys into the processor.
        p.feed(KeyPress(Keys.ControlX, ''))
        p.feed(KeyPress(Keys.ControlC, '')

        # Process all the keys in the queue.
        p.process_keys()

        # Now the ControlX-ControlC callback will be called if this sequence is
        # registered in the key bindings.

    :param key_bindings: `KeyBindingsBase` instance.
    c                    || _         t        |       | _        t        |       | _        d | _        | j                          y r+   )	_bindingsr   before_key_pressafter_key_press_flush_wait_taskreset)r   key_bindingss     r   r    zKeyProcessor.__init__\   s2    % %d$T{37

r!   c                    g | _         d | _        t               | _        g | _        d | _        | j                         | _        | j                  j                  d        y r+   )	_previous_key_sequence_previous_handlerr   input_queue
key_bufferarg_process_process_coroutinesendr&   s    r   r?   zKeyProcessor.resetf   sT    68#15 -2G +-  $ #'--/$$T*r!   c                    t        d |D              }| j                  j                  |      D cg c]  }|j                         s| c}S c c}w )zw
        For a list of :class:`KeyPress` instances. Give the matching handlers
        that would handle this.
        c              3  4   K   | ]  }|j                     y wr+   r   .0ks     r   	<genexpr>z,KeyProcessor._get_matches.<locals>.<genexpr>~        0qQUU0   )tupler;   get_bindings_for_keysfilter)r   key_presseskeysbs       r   _get_matcheszKeyProcessor._get_matchesy   sA    
 0K00  >>??ETaTTTs   AAc                    t        d |D              }| j                  j                  |      D ch c]  }|j                   }}t	        d |D              S c c}w )z
        For a list of :class:`KeyPress` instances. Return True if there is any
        handler that is bound to a suffix of this keys.
        c              3  4   K   | ]  }|j                     y wr+   rL   rM   s     r   rP   z:KeyProcessor._is_prefix_of_longer_match.<locals>.<genexpr>   rQ   rR   c              3  *   K   | ]  } |         y wr+   r6   )rN   fs     r   rP   z:KeyProcessor._is_prefix_of_longer_match.<locals>.<genexpr>   s     (113(s   )rS   r;   get_bindings_starting_with_keysrU   any)r   rV   rW   rX   filterss        r   _is_prefix_of_longer_matchz'KeyProcessor._is_prefix_of_longer_match   s[    
 0K00 #nnLLTR
AHH
 

 ((((
s   Ac              #  *  K   | j                   }d}	 d}|rd}n d}|t        u rd}n|j                  |       |r| j                  |      }|rd}n| j	                  |      }|D cg c]  }|j                         s| }}|r|}d}|s!|r| j                  |d   |dd        |dd= nb|s`|s^d}d}	t        t        |      dd      D ]:  }
| j                  |d|
       }|s| j                  |d   |d|
        |d|
= d}	 n |	s|dd= c c}w w)z
        Coroutine implementing the key match algorithm. Key strokes are sent
        into this generator, and it calls the appropriate handlers.
        FTN)key_sequencer   r   )	rE   r8   appendrY   ra   eager_call_handlerranger   )r   bufferretryflushr   matchesis_prefix_of_longer_matchmeager_matchesfoundis              r   rG   zKeyProcessor._process   s]    
 E&= EMM#& ++F305-040O0OPV0W- -4 Aqqwwy A A +G05- 1W&&wr{&Kq	 37 E!E #3v;26 ""&"3"3F2AJ"?" ..wr{PRQR.T &rr
$(E!" !"2A2Jc . !Bs   A$D&D<D A"D#0Dc                v    |r| j                   j                  |       y| j                   j                  |       y)z
        Add a new :class:`KeyPress` to the input queue.
        (Don't forget to call `process_keys` in order to process the queue.)

        :param first: If true, insert before everything else.
        N)rD   
appendleftre   )r   	key_pressfirsts      r   feedzKeyProcessor.feed   s/     ''	2##I.r!   c                    |r%| j                   j                  t        |             y| j                   j                  |       y)zG
        :param first: If true, insert before everything else.
        N)rD   
extendleftreversedextend)r   rV   ru   s      r   feed_multiplezKeyProcessor.feed_multiple   s4     ''(=>##K0r!   c                    t               d fd}d fd}d} |       r |       }|t        u }|j                  t        j                  k(  }|s|s j
                  j                          	  j                  j                  |       |s|s j                  j                           |       r|s j                          yy# t        $ r"  j                           j                           w xY w)a,  
        Process all the keys in the `input_queue`.
        (To be called after `feed`.)

        Note: because of the `feed`/`process_keys` separation, it is
              possible to call `feed` from inside a key binding.
              This function keeps looping until the queue is empty.
        c                 ~     j                   rt        d j                  D              S t        j                        S )Nc              3  \   K   | ]$  }|j                   t        j                  k(  s!| & y wr+   )r   r   CPRResponserM   s     r   rP   z?KeyProcessor.process_keys.<locals>.not_empty.<locals>.<genexpr>   s!     T!%%4CSCS:S1Ts   ",,)is_doner_   rD   r2   )appr   s   r   	not_emptyz,KeyProcessor.process_keys.<locals>.not_empty   s6     {{ Td&6&6TTTD,,--r!   c                 
   j                   rXj                  D  cg c]"  } | j                  t        j                  k(  s!| $ c} d   }j                  j                  |       |S j                  j                         S c c} w )Nr   )r   rD   r   r   r   removepopleft)rO   cprr   r   s     r   get_nextz+KeyProcessor.process_keys.<locals>.get_next   sk    {{"&"2"2PQaeet?O?O6OqPQRS  '',
''//11	 Qs
   "B B FNr-   r2   )r-   r   )r
   r8   r   r   r   r<   firerH   rI   	Exceptionr?   empty_queuer=   _start_timeout)r   r   r   is_flushrt   is_cprr   s   `     @r   process_keyszKeyProcessor.process_keys   s     i		.	2 k 
I F*H]]d&6&66FF%%**,'',,Y7 F$$))+) k. !    

  "s   /C +C/c                    t        | j                        }| j                  j                          |D cg c]"  }|j                  t        j
                  k7  s!|$ }}|S c c}w )zF
        Empty the input queue. Return the unprocessed input.
        )listrD   clearr   r   r   )r   rV   rO   s      r   r   zKeyProcessor.empty_queue   sX     4++,  #.KQ$:J:J1JqKK Ls   "AAc                   t               }|j                  j                  }t        |j                  j
                        }|j                  j                  }| j                  }d | _        t        t        j                  |       ||| j                  || j                  k(        }|j                  |      r$|j                  j                  j!                          ddlm}	 	 |j'                  |       | j)                  |       |r| j/                  |       || _        || _        |j1                         r|j                  j                  r+|r)|j                  j2                  }
|
|
j5                  |       |j                  j
                  r4|r1|D ]+  }|j                  xj2                  |j6                  z  c_        - y y y y # |	$ r |j*                  j-                          Y w xY w)N)rF   rd   previous_key_sequence	is_repeatr   )EditReadOnlyBuffer)r
   emacs_stateis_recordingr2   vi_staterecording_registertemporary_navigation_moderF   r   weakrefrefrB   rC   save_beforer   current_buffersave_to_undo_stackprompt_toolkit.bufferr   call_fix_vi_cursor_positionoutputbell_leave_vi_temp_navigation_moderecord_in_macrocurrent_recordingrz   r   )r   handlerrd   r   was_recording_emacswas_recording_viwas_temporary_navigation_moderF   eventr   	recordingrO   s               r   rg   zKeyProcessor._call_handler+  s   i!oo:: ? ?@(+(N(N%hhKK%"&"="=$"8"88
 u%II$$779 	=	LL((/ )//6&2#!( ""$++0COO==	($$\2||..3C% =ALL22aff<2= 4D. % " 	 JJOO	s   "F? ?G! G!c                   |j                   }|j                  }|j                  }t               rW|j                  j
                  r@t        |j                  j                        dkD  r|xj                  dz  c_        ||_        yyyy)z
        After every command, make sure that if we are in Vi navigation mode, we
        never put the cursor after the last character of a line. (Unless it's
        an empty line.)
        r   r   N)	r   r   preferred_columnr   documentis_cursor_at_the_end_of_liner   current_linecursor_position)r   r   r   buffr   s        r   r   z$KeyProcessor._fix_vi_cursor_position^  sz     ii!!00  ::DMM../!3  A%  %5D! 4 ; !r!   c                    |j                   }|j                  t        j                  k(  r6|j                  j
                  | j                  d|j                  _        yyyy)z
        If we're in Vi temporary navigation (normal) mode, return to
        insert/replace mode after executing one action.
        NF)r   editing_moder   VIr   operator_funcrF   r   )r   r   r   s      r   r   z+KeyProcessor._leave_vi_temp_navigation_modes  sR    
 ii{~~-||))1dhh6F9>6 7G1 .r!   c                     t               }|j                  yd fd}d fd j                  r j                  j                          |j	                   |              _        y)a%  
        Start auto flush timeout. Similar to Vim's `timeoutlen` option.

        Start a background coroutine with a timer. When this timeout expires
        and no key was pressed in the meantime, we flush all data in the queue
        and call the appropriate key binding handlers.
        Nc                 x   K   t               d{    t        j                        dkD  r          yy7 %w)zWait for timeout.Nr   )r   r   rE   )
flush_keysr   timeouts   r   waitz)KeyProcessor._start_timeout.<locals>.wait  s6      .  4??#a' ( !s   :8&:c                 P     j                  t                j                          y)zFlush keys.N)rv   r8   r   r&   s   r   r   z/KeyProcessor._start_timeout.<locals>.flush_keys  s    IIfr!   r-   r.   )r
   
timeoutlenr>   cancelcreate_background_task)r   r   r   r   r   s   `  @@r   r   zKeyProcessor._start_timeout  sY     i..?		    !!((* # : :46 Br!   c                z    | j                  t        t        j                        d       | j	                          y)zG
        Send SIGINT. Immediately call the SIGINT key handler.
        rL   T)ru   N)rv   r   r   SIGINTr   r&   s    r   send_sigintzKeyProcessor.send_sigint  s)     			(t{{+4	8r!   N)r@   r   r-   r.   r   )rV   list[KeyPress]r-   zlist[Binding])rV   r   r-   r2   )r-   zGenerator[None, KeyPress, None])F)rt   r   ru   r2   r-   r.   )rV   r   ru   r2   r-   r.   )r-   r   )r   r   rd   r   r-   r.   )r   r   r-   r.   )r%   r3   r4   r5   r    r?   rY   ra   rG   rv   r{   r   r   rg   r   r   r   r   r6   r!   r   r   r   F   sS    *+&U)$9'v
/19"v	1=f5*
?CBr!   r   c                      e Zd ZdZ	 	 	 	 	 	 	 	 	 	 	 	 ddZddZedd       Zedd       Zedd       Z	edd       Z
edd       Zedd	       Zdd
Zedd       Zy)r   at  
    Key press event, delivered to key bindings.

    :param key_processor_ref: Weak reference to the `KeyProcessor`.
    :param arg: Repetition argument.
    :param key_sequence: List of `KeyPress` instances.
    :param previouskey_sequence: Previous list of `KeyPress` instances.
    :param is_repeat: True when the previous event was delivered to the same handler.
    c                h    || _         || _        || _        || _        || _        t               | _        y r+   )_key_processor_refrd   r   r   _argr
   _app)r   key_processor_refrF   rd   r   r   s         r   r    zKeyPressEvent.__init__  s5     #4(%:" #	I	r!   c                V    d| j                   d| j                  d| j                  dS )NzKeyPressEvent(arg=z, key_sequence=z, is_repeat=r#   )rF   rd   r   r&   s    r   r'   zKeyPressEvent.__repr__  s2    #DHH<t?P?P>SS_`d`n`n_qqrssr!   c                4    | j                   d   j                  S )Nrc   )rd   r   r&   s    r   r   zKeyPressEvent.data  s      $)))r!   c                @    | j                         }|t        d      |S )Nz.KeyProcessor was lost. This should not happen.)r   r   )r   	processors     r   key_processorzKeyPressEvent.key_processor  s(    ++-	LMMr!   c                    | j                   S )z3
        The current `Application` object.
        )r   r&   s    r   r   zKeyPressEvent.app  s    
 yyr!   c                .    | j                   j                  S )z%
        The current buffer.
        )r   r   r&   s    r   r   zKeyPressEvent.current_buffer  s    
 xx&&&r!   c                x    | j                   dk(  ryt        | j                   xs d      }t        |      dk\  rd}|S )z&
        Repetition argument.
        -rc   r   i@B )r   int)r   results     r   rF   zKeyPressEvent.arg  s=    
 99TYY^!$ v;'!Fr!   c                    | j                   duS )zF
        True if repetition argument was explicitly provided.
        N)r   r&   s    r   arg_presentzKeyPressEvent.arg_present  s    
 yy$$r!   c                    |dv sJ | j                   }|dk(  r||dk(  sJ |}n||}n| | }|| j                  _        y)zb
        Add digit to the input argument.

        :param data: the typed digit as string
        z-0123456789r   N)r   r   rF   )r   r   currentr   s       r   append_to_arg_countz!KeyPressEvent.append_to_arg_count  s^     }$$$))3;?gn44F_Fy'F!'r!   c                    | j                   S )zFor backward-compatibility.)r   r&   s    r   clizKeyPressEvent.cli  s     xxr!   N)r   z#weakref.ReferenceType[KeyProcessor]rF   r,   rd   r   r   r   r   r2   r-   r.   r/   )r-   r   )r-   zApplication[Any])r-   r   )r-   r   r   )r   r0   r-   r.   )r%   r3   r4   r5   r    r'   propertyr   r   r   r   rF   r   r   r   r6   r!   r   r   r     s    >  %	
  .  
$t * *     ' '   % %(&  r!   r   )#r5   
__future__r   r   asyncior   r   collectionsr   typingr   r   r	   "prompt_toolkit.application.currentr
   prompt_toolkit.enumsr   prompt_toolkit.filters.appr   prompt_toolkit.keysr   prompt_toolkit.utilsr   r@   r   r   prompt_toolkit.applicationr   r   r   __all__r   r8   r   r   r6   r!   r   <module>r      sx    #    0 0 6 , 9 $ & 26,A A6 
#H	%_ _Df fr!   