
    ^hD                       S r SSKJr  SSKrSSKJrJr  SSKJr  SSK	J
r
JrJr  SSKJr  SSKJr  SS	KJr  SS
KJr  SSKJr  SSKJrJr  \
(       a  SSKJr  SSKJr  / SQr " S S5      r \" SSS9r  " S S5      r! " S S5      r"g)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                  :    \ rS rSrSrSS	S jjrS
S jrSS jrSrg)r   $   z
:param key: A `Keys` instance or text (one character).
:param data: The received string on stdin. (Often vt100 escape codes.)
Nc                    [        U[        5      (       d  [        U5      S:X  d   eUc$  [        U[        5      (       a  UR                  nOUnXl        X l        g )Nr   )
isinstancer   lenvaluekeydata)selfr   r   s      b/home/james-whalen/.local/lib/python3.13/site-packages/prompt_toolkit/key_binding/key_processor.py__init__KeyPress.__init__*   sI    #t$$CA55<#t$$yy	    c                l    U R                   R                   SU R                  < SU R                  < S3$ )Nz(key=z, data=))	__class____name__r   r   r   s    r    __repr__KeyPress.__repr__6   s.    ..))*%|7499-qQQr#   c                    [        U[        5      (       d  gU R                  UR                  :H  =(       a    U R                  UR                  :H  $ )NF)r   r   r   r   )r   others     r    __eq__KeyPress.__eq__9   s7    %**xx599$@ejj)@@r#   )r   r   N)r   z
Keys | strr   
str | NonereturnNoner1   str)r,   objectr1   bool)	r'   
__module____qualname____firstlineno____doc__r!   r)   r-   __static_attributes__ r#   r    r   r   $   s    

RAr#   r   ?_Flush)r   c                      \ rS rSrSrSS jrSS jrSS jrSS jrSS jr	SSS jjr
SSS	 jjrSS
 jrSS jrSS jrSS jrSS jrSS jrSS jrSrg)r   F   a  
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                ~    Xl         [        U 5      U l        [        U 5      U l        S U l        U R                  5         g r/   )	_bindingsr   before_key_pressafter_key_press_flush_wait_taskreset)r   key_bindingss     r    r!   KeyProcessor.__init__\   s0    % %d$T{37

r#   c                    / U l         S U l        [        5       U l        / U l        S U l        U R                  5       U l        U R                  R                  S 5        g r/   )	_previous_key_sequence_previous_handlerr   input_queue
key_bufferarg_process_process_coroutinesendr(   s    r    rF   KeyProcessor.resetf   sT    68#15 -2G +-  $ #'--/$$T*r#   c                    [        S U 5       5      nU R                  R                  U5       Vs/ s H  o3R                  5       (       d  M  UPM     sn$ s  snf )z_
For a list of :class:`KeyPress` instances. Give the matching handlers
that would handle this.
c              3  8   #    U  H  oR                   v   M     g 7fr/   r   .0ks     r    	<genexpr>,KeyProcessor._get_matches.<locals>.<genexpr>~        0KqUUK   )tuplerB   get_bindings_for_keysfilter)r   key_presseskeysbs       r    _get_matchesKeyProcessor._get_matchesy   sE    
 0K00  >>??ETEaETTTs   AAc                    [        S U 5       5      nU R                  R                  U5       Vs1 s H  o3R                  iM     nn[	        S U 5       5      $ s  snf )zx
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  8   #    U  H  oR                   v   M     g 7fr/   rU   rV   s     r    rY   :KeyProcessor._is_prefix_of_longer_match.<locals>.<genexpr>   r[   r\   c              3  .   #    U  H  o" 5       v   M     g 7fr/   r<   )rW   fs     r    rY   rg      s     (1133s   )r]   rB   get_bindings_starting_with_keysr_   any)r   r`   ra   rb   filterss        r    _is_prefix_of_longer_match'KeyProcessor._is_prefix_of_longer_match   s\    
 0K00 #nnLLTR
RHHR 	 

 ((((
s   Ac              #    #    U R                   nSn SnU(       a  SnO!Sv nU[        L a  SnOUR                  U5        U(       a  U R                  U5      nU(       a  SnOU R	                  U5      nU Vs/ s H  owR                  5       (       d  M  UPM     nnU(       a  UnSnU(       d#  U(       a  U R                  US   USS S9  USS2	 OuU(       dn  U(       dg  SnSn	[        [        U5      SS5       H=  n
U R                  USU
 5      nU(       d  M   U R                  US   USU
 S9  USU
2	 Sn	  O   U	(       d  USS2	 GM5  s  snf 7f)z
Coroutine implementing the key match algorithm. Key strokes are sent
into this generator, and it calls the appropriate handlers.
FTN)key_sequencer   r   )	rM   r>   appendrc   rm   eager_call_handlerranger   )r   bufferretryflushr   matchesis_prefix_of_longer_matchmeager_matchesfoundis              r    rO   KeyProcessor._process   sX    
 E&= EMM#& ++F305-040O0OPV0W- -4 AGqwwyG A +G05- 1W&&wr{&Kq	 37 E!E #3v;26"&"3"3F2AJ"?"7 ..wr{PRQR.T &rr
$(E! 7 !"2A2Jc . !Bs   A4E6EEA;E5Ec                    U(       a  U R                   R                  U5        gU R                   R                  U5        g)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)rL   
appendleftrr   )r   	key_pressfirsts      r    feedKeyProcessor.feed   s/     ''	2##I.r#   c                    U(       a%  U R                   R                  [        U5      5        gU R                   R                  U5        g)z7
:param first: If true, insert before everything else.
N)rL   
extendleftreversedextend)r   r`   r   s      r    feed_multipleKeyProcessor.feed_multiple   s4     ''(=>##K0r#   c                6  ^ ^ [        5       mSUU 4S jjnSUU 4S jjnSnU" 5       (       a  U" 5       nU[        L nUR                  [        R                  :H  nU(       d!  U(       d  T R
                  R                  5          T R                  R                  U5        U(       d!  U(       d  T R                  R                  5         U" 5       (       a  M  U(       d  T R                  5         gg! [         a"    T R                  5         T R                  5         e f = f)z
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                    > T R                   (       a  [        S TR                   5       5      $ [        TR                  5      $ )Nc              3  d   #    U  H&  oR                   [        R                  :X  d  M"  Uv   M(     g 7fr/   )r   r   CPRResponserV   s     r    rY   ?KeyProcessor.process_keys.<locals>.not_empty.<locals>.<genexpr>   s#     T&6%%4CSCS:S11&6s   !0	0)is_donerk   rL   r6   )appr   s   r    	not_empty,KeyProcessor.process_keys.<locals>.not_empty   s6     {{ Td&6&6TTTD,,--r#   c                   > TR                   (       a\  TR                   V s/ s H$  o R                  [        R                  :X  d  M"  U PM&     sn S   nTR                  R                  U5        U$ TR                  R                  5       $ s  sn f )Nr   )r   rL   r   r   r   removepopleft)rX   cprr   r   s     r    get_next+KeyProcessor.process_keys.<locals>.get_next   so    {{"&"2"2P"2Qeet?O?O6Oq"2PQRS  '',
''//11	 Qs   !B	B	FNr1   r6   )r1   r   )r
   r>   r   r   r   rC   firerP   rQ   	ExceptionrF   empty_queuerD   _start_timeout)r   r   r   is_flushr   is_cprr   s   `     @r    process_keysKeyProcessor.process_keys   s     i		. 		.	2 	2 kk 
I F*H]]d&6&66FF%%**,'',,Y7 F$$))+) kk. !    

  "s   C, ,,Dc                    [        U R                  5      nU R                  R                  5         U Vs/ s H$  o"R                  [        R
                  :w  d  M"  UPM&     nnU$ s  snf )z6
Empty the input queue. Return the unprocessed input.
)listrL   clearr   r   r   )r   r`   rX   s      r    r   KeyProcessor.empty_queue   sW     4++,  #.K+Q$:J:J1Jq+K Ls   !A#A#c                   [        5       nUR                  R                  n[        UR                  R
                  5      nUR                  R                  nU R                  nS U l        [        [        R                  " U 5      UUU R                  XR                  :H  S9nUR                  U5      (       a$  UR                  R                  R!                  5         SSKJn	   UR'                  U5        U R)                  U5        U(       a  U R/                  U5        X l        Xl        UR1                  5       (       a  UR                  R                  (       a1  U(       a*  UR                  R2                  n
U
b  U
R5                  U5        UR                  R
                  (       a;  U(       a3  U H,  nUR                  =R2                  UR6                  -  sl        M.     g g g g ! U	 a    UR*                  R-                  5          Nf = f)N)rN   rq   previous_key_sequence	is_repeatr   )EditReadOnlyBuffer)r
   emacs_stateis_recordingr6   vi_staterecording_registertemporary_navigation_moderN   r   weakrefrefrJ   rK   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   handlerrq   r   was_recording_emacswas_recording_viwas_temporary_navigation_moderN   eventr   	recordingrX   s               r    rt   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   !"G    HHc                "   UR                   nUR                  nUR                  n[        5       (       a\  UR                  R
                  (       a@  [        UR                  R                  5      S:  a  U=R                  S-  sl        XCl        gggg)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   $KeyProcessor._fix_vi_cursor_position^  s{     ii!!00   ::DMM../!3  A%  %5! 4 ; !r#   c                    UR                   nUR                  [        R                  :X  a8  UR                  R
                  c   U R                  c  SUR                  l        gggg)zn
If we're in Vi temporary navigation (normal) mode, return to
insert/replace mode after executing one action.
NF)r   editing_moder   VIr   operator_funcrN   r   )r   r   r   s      r    r   +KeyProcessor._leave_vi_temp_navigation_modes  sR    
 ii{~~-||))1dhh6F9>6 7G1 .r#   c                   ^ ^^ [        5       nUR                  mTc  gSUU U4S jjnSU 4S jjmT R                  (       a  T R                  R                  5         UR	                  U" 5       5      T l        g)z
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                 z   >#    [        T5      I Sh  vN   [        TR                  5      S:  a  T " 5         gg N&7f)zWait for timeout.Nr   )r   r   rM   )
flush_keysr   timeouts   r    wait)KeyProcessor._start_timeout.<locals>.wait  s5      .  4??#a' ( !s   ;9';c                 P   > T R                  [        5        T R                  5         g)zFlush keys.N)r   r>   r   r(   s   r    r   /KeyProcessor._start_timeout.<locals>.flush_keys  s    IIfr#   r1   r2   )r
   
timeoutlenrE   cancelcreate_background_task)r   r   r   r   r   s   `  @@r    r   KeyProcessor._start_timeout  s^     i..?	 		    !!((* # : :46 Br#   c                n    U R                  [        [        R                  S9SS9  U R	                  5         g)z7
Send SIGINT. Immediately call the SIGINT key handler.
rU   T)r   N)r   r   r   SIGINTr   r(   s    r    send_sigintKeyProcessor.send_sigint  s)     			(t{{+4	8r#   )
rB   rE   rK   rJ   rP   rD   rN   rC   rL   rM   N)rG   r   r1   r2   r   )r`   list[KeyPress]r1   zlist[Binding])r`   r   r1   r6   )r1   zGenerator[None, KeyPress, None])F)r   r   r   r6   r1   r2   )r`   r   r   r6   r1   r2   )r1   r   )r   r   rq   r   r1   r2   )r   r   r1   r2   )r'   r7   r8   r9   r:   r!   rF   rc   rm   rO   r   r   r   r   rt   r   r   r   r   r;   r<   r#   r    r   r   F   sS    *+&U)$9'v
/19"v	1=f5*
?CBr#   r   c                      \ rS rSrSr            SS jrSS jr\SS j5       r\SS j5       r	\SS j5       r
\SS j5       r\SS	 j5       r\SS
 j5       rSS jr\SS j5       rSrg)r   i  aX  
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                ^    Xl         X0l        X@l        XPl        X l        [        5       U l        g r/   )_key_processor_refrq   r   r   _argr
   _app)r   key_processor_refrN   rq   r   r   s         r    r!   KeyPressEvent.__init__  s+     #4(%:" #	I	r#   c                \    SU R                   < SU R                  < SU R                  < S3$ )NzKeyPressEvent(arg=z, key_sequence=z, is_repeat=r%   )rN   rq   r   r(   s    r    r)   KeyPressEvent.__repr__  s2    #DHH<t?P?P>SS_`d`n`n_qqrssr#   c                4    U R                   S   R                  $ )Nrp   )rq   r   r(   s    r    r   KeyPressEvent.data  s      $)))r#   c                B    U R                  5       nUc  [        S5      eU$ )Nz.KeyProcessor was lost. This should not happen.)r   r   )r   	processors     r    key_processorKeyPressEvent.key_processor  s(    ++-	LMMr#   c                    U R                   $ )z#
The current `Application` object.
)r   r(   s    r    r   KeyPressEvent.app  s    
 yyr#   c                .    U R                   R                  $ )z
The current buffer.
)r   r   r(   s    r    r   KeyPressEvent.current_buffer  s    
 xx&&&r#   c                    U R                   S:X  a  g[        U R                   =(       d    S5      n[        U5      S:  a  SnU$ )z
Repetition argument.
-rp   r   i@B )r   int)r   results     r    rN   KeyPressEvent.arg  s=    
 99TYY^!$ v;'!Fr#   c                    U R                   SL$ )z6
True if repetition argument was explicitly provided.
N)r   r(   s    r    arg_presentKeyPressEvent.arg_present  s    
 yy$$r#   c                    US;   d   eU R                   nUS:X  a  Ub  US:X  d   eUnOUc  UnOU U 3nX0R                  l        g)zJ
Add digit to the input argument.

:param data: the typed digit as string
z-0123456789r   N)r   r   rN   )r   r   currentr   s       r    append_to_arg_count!KeyPressEvent.append_to_arg_count  s\     }$$$))3;?gn44F_Fy'F!'r#   c                    U R                   $ )zFor backward-compatibility.)r   r(   s    r    cliKeyPressEvent.cli  s     xxr#   )r   r   r   r   rq   r   N)r   z#weakref.ReferenceType[KeyProcessor]rN   r0   rq   r   r   r   r   r6   r1   r2   r3   )r1   r   )r1   zApplication[Any])r1   r   )r1   r   r   )r   r4   r1   r2   )r'   r7   r8   r9   r:   r!   r)   propertyr   r   r   r   rN   r   r  r  r;   r<   r#   r    r   r     s    >  %	
  .  
$t * *     ' '   % %(&  r#   r   )#r:   
__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   rG   r   r   prompt_toolkit.applicationr   r   r   __all__r   r>   r   r   r<   r#   r    <module>r     sx    #    0 0 6 , 9 $ & 26,A A6 
#H	%_ _Df fr#   