
    QfD                    (   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"dS )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dS )r   z
    :param key: A `Keys` instance or text (one character).
    :param data: The received string on stdin. (Often vt100 escape codes.)
    Nkey
Keys | strdata
str | NonereturnNonec                    t          |t                    st          |          dk    sJ |t          |t                    r|j        }n|}|| _        || _        d S )Nr   )
isinstancer   lenvaluer   r   )selfr   r   s      Z/var/www/html/env/lib/python3.11/site-packages/prompt_toolkit/key_binding/key_processor.py__init__zKeyPress.__init__*   s\    #t$$5CA5<#t$$ y			    strc                @    | 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*    .)QQQQ49QQQQr%   otherobjectboolc                p    t          |t                    sdS | j        |j        k    o| j        |j        k    S )NF)r   r   r   r   )r"   r-   s     r#   __eq__zKeyPress.__eq__9   s6    %** 	5x59$@ej)@@r%   N)r   r   r   r   r   r   r   r&   )r-   r.   r   r/   )r*   
__module____qualname____doc__r$   r,   r1    r%   r#   r   r   $   si         

 
 
 
 
R R R RA A A A A 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d"S ).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.
    key_bindingsr   r   r   c                    || _         t          |           | _        t          |           | _        d | _        |                                  d S r2   )	_bindingsr   before_key_pressafter_key_press_flush_wait_taskreset)r"   r;   s     r#   r$   zKeyProcessor.__init__\   s>    % %d$T{{37

r%   c                    g | _         d | _        t                      | _        g | _        d | _        |                                 | _        | j                            d            d S r2   )	_previous_key_sequence_previous_handlerr   input_queue
key_bufferarg_process_process_coroutinesendr+   s    r#   rA   zKeyProcessor.resetf   sa    68#15 -2GG +-  $ #'--//$$T*****r%   key_presseslist[KeyPress]list[Binding]c                |    t          d |D                       }d | j                            |          D             S )zw
        For a list of :class:`KeyPress` instances. Give the matching handlers
        that would handle this.
        c              3  $   K   | ]}|j         V  d S r2   r   .0ks     r#   	<genexpr>z,KeyProcessor._get_matches.<locals>.<genexpr>~   $      00qQU000000r%   c                :    g | ]}|                                 |S r7   filterrR   bs     r#   
<listcomp>z-KeyProcessor._get_matches.<locals>.<listcomp>   s%    TTTaTTTTr%   )tupler=   get_bindings_for_keys)r"   rK   keyss      r#   _get_matcheszKeyProcessor._get_matchesy   sG    
 00K00000 UT4>??EETTTTr%   r/   c                    t          d |D                       }d | j                            |          D             }t          d |D                       S )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  $   K   | ]}|j         V  d S r2   rP   rQ   s     r#   rT   z:KeyProcessor._is_prefix_of_longer_match.<locals>.<genexpr>   rU   r%   c                    h | ]	}|j         
S r7   rW   rY   s     r#   	<setcomp>z:KeyProcessor._is_prefix_of_longer_match.<locals>.<setcomp>   s'     
 
 
AH
 
 
r%   c              3  *   K   | ]} |            V  d S r2   r7   )rR   fs     r#   rT   z:KeyProcessor._is_prefix_of_longer_match.<locals>.<genexpr>   s(      ((11133((((((r%   )r\   r=   get_bindings_starting_with_keysany)r"   rK   r^   filterss       r#   _is_prefix_of_longer_matchz'KeyProcessor._is_prefix_of_longer_match   sm    
 00K00000
 
"nLLTRR
 
 

 ((((((((r%   Generator[None, KeyPress, None]c              #  T  K   | j         }d}	 d}|rd}n%dV }|t          u rd}n|                    |           |r|                     |          }|rd}n|                     |          }d |D             }|r|}d}|s-|r+|                     |d         |dd                    |dd= n}|s{|syd}d}t          t          |          dd          D ]O}	|                     |d|	                   }|r.|                     |d         |d|	                    |d|	= d} nP|s|dd= )	z
        Coroutine implementing the key match algorithm. Key strokes are sent
        into this generator, and it calls the appropriate handlers.
        FTNc                :    g | ]}|                                 |S r7   )eager)rR   ms     r#   r[   z)KeyProcessor._process.<locals>.<listcomp>   s%     A A Aqqwwyy A A A Ar%   )key_sequencer   r   )rF   r9   appendr_   ri   _call_handlerranger    )
r"   bufferretryflushr   matchesis_prefix_of_longer_matcheager_matchesfoundis
             r#   rH   zKeyProcessor._process   s     
 1	'E 'ee&== EEMM#&&&  $'++F33 X05--040O0OPV0W0W- !B AG A A A  6+G05- 1 'W '&&wr{&KKKqqq		 3 '7 ' E!E #3v;;266 " ""&"3"3F2A2J"?"?" " ..wr{PRQRPR.TTT &rr
$(E!E	" ! '"2A2Jc1	'r%   F	key_pressr   firstc                v    |r| j                             |           dS | j                             |           dS )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)rE   
appendleftrq   )r"   r|   r}   s      r#   feedzKeyProcessor.feed   sF      	/''	22222##I.....r%   c                    |r)| j                             t          |                     dS | j                             |           dS )zG
        :param first: If true, insert before everything else.
        N)rE   
extendleftreversedextend)r"   rK   r}   s      r#   feed_multiplezKeyProcessor.feed_multiple   sN      	1''(=(=>>>>>##K00000r%   c                    t                      d fd}d	 fd}d} |            r |            }|t          u }|j        t          j        k    }|s|s j                                         	  j                            |           n7# t          $ r*  
                                                                    w xY w|s|s j                                          |            |s                                  dS dS )
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.
        r   r/   c                 v     j         rt          d j        D                       S t          j                  S )Nc              3  D   K   | ]}|j         t          j        k    |V  d S r2   r   r   CPRResponserQ   s     r#   rT   z?KeyProcessor.process_keys.<locals>.not_empty.<locals>.<genexpr>   s1      TT!%4CS:S:S1:S:S:S:STTr%   )is_donerg   rE   r/   )appr"   s   r#   	not_emptyz,KeyProcessor.process_keys.<locals>.not_empty   sB     { . TTd&6TTTTTTD,---r%   r   c                     j         r3d j        D             d         } j                            |            | S j                                        S )Nc                <    g | ]}|j         t          j        k    |S r7   r   rQ   s     r#   r[   z?KeyProcessor.process_keys.<locals>.get_next.<locals>.<listcomp>   s'    PPPQaet?O6O6Oq6O6O6Or%   r   )r   rE   removepopleft)cprr   r"   s    r#   get_nextz+KeyProcessor.process_keys.<locals>.get_next   sZ    { 2PP$"2PPPQRS '',,,
'//111r%   FNr   r/   )r   r   )r
   r9   r   r   r   r>   firerI   rJ   	ExceptionrA   empty_queuer?   _start_timeout)r"   r   r   is_flushr|   is_cprr   s   `     @r#   process_keyszKeyProcessor.process_keys   sr    ii		. 		. 		. 		. 		. 		. 		.	2 	2 	2 	2 	2 	2 	2 ikk 	, 

I F*H]d&66F -F -%**,,,',,Y7777    

  """  ,F ,$))+++) ikk 	,.  	"!!!!!	" 	"s   1B 4C c                x    t          | j                  }| j                                         d |D             }|S )zF
        Empty the input queue. Return the unprocessed input.
        c                <    g | ]}|j         t          j        k    |S r7   r   rQ   s     r#   r[   z,KeyProcessor.empty_queue.<locals>.<listcomp>(  s'    KKKQ$:J1J1Jq1J1J1Jr%   )listrE   clear)r"   rK   s     r#   r   zKeyProcessor.empty_queue   sE     4+,,    LK+KKKr%   handlerr   rp   c                T   t                      }|j        j        }t          |j        j                  }|j        j        }| j        }d | _        t          t          j
        |           ||| j        || j        k              }|                    |          r|j        j                                         ddlm}	 	 |                    |           |                     |           n$# |	$ r |j                                         Y nw xY w|r|                     |           || _        || _        |                                r^|j        j        r%|r#|j        j        }
|
|
                    |           |j        j        r#|r#|D ]"}|j        xj        |j        z  c_        d S d S d S d S )N)rG   rp   previous_key_sequence	is_repeatr   )EditReadOnlyBuffer)r
   emacs_stateis_recordingr/   vi_staterecording_registertemporary_navigation_moderG   r   weakrefrefrC   rD   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_recordingr   r   )r"   r   rp   r   was_recording_emacswas_recording_viwas_temporary_navigation_moderG   eventr   	recordingrS   s               r#   rr   zKeyProcessor._call_handler+  s   ii!o: ?@@(+(N%hK%"&"=$"88
 
 
 u%% 	:I$77999 	=<<<<<	LL((////! 	 	 	 JOO	
 ) 	7//666&2#!( ""$$ 		=+ 30C 3O=	($$\222|. =3C =% = =AL22af<222		= 		== = = == =s   <*C' 'DDr   r   c                    |j         }|j        }|j        }t                      rB|j        j        r8t          |j        j                  dk    r|xj        dz  c_        ||_        dS dS dS dS )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^  s     i!0   		5:		5 DM.//!33  A%   %5D!!!		5 		5 		5 		5 43r%   c                    |j         }|j        t          j        k    r!|j        j        | j        d|j        _        dS dS dS dS )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_funcrG   r   )r"   r   r   s      r#   r   z+KeyProcessor._leave_vi_temp_navigation_modes  sQ    
 i{~--|)1dh6F9>666 .-116F6Fr%   c                     t                      }|j        dS d fd}d fd j        r j                                         |                     |                       _        dS )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.
        Nr   r   c                 ~   K   t                     d{V  t          j                  dk    r               dS dS )zWait for timeout.Nr   )r   r    rF   )
flush_keysr"   timeouts   r#   waitz)KeyProcessor._start_timeout.<locals>.wait  sS       ..       4?##a''
 ('r%   c                 d                          t                                                       dS )zFlush keys.N)r   r9   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  s     ii.?F	 	 	 	 	 	 	 		  	  	  	  	  	    	+!((*** # : :4466 B Br%   c                    |                      t          t          j                  d           |                                  dS )zG
        Send SIGINT. Immediately call the SIGINT key handler.
        rP   T)r}   N)r   r   r   SIGINTr   r+   s    r#   send_sigintzKeyProcessor.send_sigint  s?     			(t{+++4	888r%   N)r;   r   r   r   r   )rK   rL   r   rM   )rK   rL   r   r/   )r   rj   )F)r|   r   r}   r/   r   r   )rK   rL   r}   r/   r   r   )r   rL   )r   r   rp   rL   r   r   )r   r   r   r   )r*   r4   r5   r6   r$   rA   r_   ri   rH   r   r   r   r   rr   r   r   r   r   r7   r%   r#   r   r   F   sL        *   + + + +&U U U U) ) ) )$9' 9' 9' 9'v
/ 
/ 
/ 
/ 
/1 1 1 1 19" 9" 9" 9"v	 	 	 	1= 1= 1= 1=f5 5 5 5*
? 
? 
? 
?C C C 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dS )&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.
    key_processor_ref#weakref.ReferenceType[KeyProcessor]rG   r   rp   rL   r   r   r/   r   r   c                r    || _         || _        || _        || _        || _        t                      | _        d S r2   )_key_processor_refrp   r   r   _argr
   _app)r"   r   rG   rp   r   r   s         r#   r$   zKeyPressEvent.__init__  s;     #4(%:" #	II			r%   r&   c                8    d| j         d| j        d| j        dS )NzKeyPressEvent(arg=z, key_sequence=z, is_repeat=r(   )rG   rp   r   r+   s    r#   r,   zKeyPressEvent.__repr__  s,    sDHsst?Pss`d`nssssr%   c                &    | j         d         j        S )Nro   )rp   r   r+   s    r#   r   zKeyPressEvent.data  s     $))r%   r   c                P    |                                  }|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Mr%   Application[Any]c                    | j         S )z3
        The current `Application` object.
        )r   r+   s    r#   r   zKeyPressEvent.app  s    
 yr%   r   c                    | j         j        S )z%
        The current buffer.
        )r   r   r+   s    r#   r   zKeyPressEvent.current_buffer  s    
 x&&r%   intc                v    | j         dk    rdS t          | j         pd          }t          |          dk    rd}|S )z&
        Repetition argument.
        -ro   r   i@B )r   r   )r"   results     r#   rG   zKeyPressEvent.arg  sF    
 92TY^!$$ 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    
 y$$r%   r   c                t    |dv sJ | j         }|dk    r||dk    sJ |}n||}n| | }|| j        _        dS )zb
        Add digit to the input argument.

        :param data: the typed digit as string
        z-0123456789r   N)r   r   rG   )r"   r   currentr   s       r#   append_to_arg_countz!KeyPressEvent.append_to_arg_count  sk     }$$$$)3;;?gnnn4FF_FF'''F!'r%   c                    | j         S )zFor backward-compatibility.)r   r+   s    r#   clizKeyPressEvent.cli  s     xr%   N)r   r   rG   r   rp   rL   r   rL   r   r/   r   r   r3   )r   r   )r   r   )r   r   )r   r   r   )r   r&   r   r   )r*   r4   r5   r6   r$   r,   propertyr   r   r   r   rG   r   r   r   r7   r%   r#   r   r     s>           $t t t t * * * X*    X    X ' ' ' X'    X % % % X%( ( ( (&    X  r%   r   )#r6   
__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   r9   r   r   r7   r%   r#   <module>r      s    # " " " " "                0 0 0 0 0 0 0 0 0 0 6 6 6 6 6 6 , , , , , , 9 9 9 9 9 9 $ $ $ $ $ $ & & & & & & 2 2 2 2 2 2 2 2 -666666,,,,,,  A A A A A A A A6 
#H	%	%	%_ _ _ _ _ _ _ _Df f f f f f f f f fr%   