
    .                         S r SSKJr  SSKJr  SSKJr  SSK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rSSKrSr " S S\5      r " S S\5      r " S S\5      rg)a)  
An :class:`~.InputProcessor` receives callbacks for the keystrokes parsed from
the input in the :class:`~prompt_toolkit.inputstream.InputStream` instance.

The `InputProcessor` will according to the implemented keybindings call the
correct callbacks when new key presses are feed through `feed`.
    )unicode_literals)EditReadOnlyBuffer)ViNavigationMode)KeysKey)Event   )BaseRegistry)deque)rangeN)InputProcessorKeyPressc                   .    \ rS rSrSrSS jrS rS 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[        R                  [        45      (       d   eUb!  [        U[        R                  5      (       d   eUc#  [        U[        5      (       a  UR                  OUnXl        X l        g N)
isinstancesix	text_typer   namekeydata)selfr   r   s      =lib/third_party/prompt_toolkit/key_binding/input_processor.py__init__KeyPress.__init__!   s_    #s34444|z$>>>><)#s33388D	    c                 n    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__+   s%    NN##TXXtyy: 	:r   c                 t    U R                   UR                   :H  =(       a    U R                  UR                  :H  $ r   )r   r   )r   others     r   __eq__KeyPress.__eq__/   s'    xx599$@ejj)@@r   )r   r   r   )	r!   
__module____qualname____firstlineno____doc__r   r#   r'   __static_attributes__ r   r   r   r      s    :Ar   r   c                   d    \ rS rSrSrS rS rS rS rS r	S r
S	 rS
 rS rS rSS jrS rSrg)r   3   a?  
Statemachine that receives :class:`KeyPress` instances and according to the
key bindings in the given :class:`Registry`, calls the matching handlers.

::

    p = InputProcessor(registry)

    # 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 registry.

:param registry: `BaseRegistry` instance.
:param cli_ref: weakref to `CommandLineInterface`.
c                     [        U[        5      (       d   eXl        X l        [	        U 5      U l        [	        U 5      U l        [        5       U l        / U l	        SU l
        / U l        U R                  5         g )NF)r   r
   	_registry_cli_refr   beforeKeyPressafterKeyPressr   input_queue
key_bufferrecord_macromacroreset)r   registrycli_refs      r   r   InputProcessor.__init__I   sf    (L1111!#Dk"4[ !7  "


r   c                     S U l         S U l        U R                  5       U l        U R                  R	                  S 5        S U l        g r   )_previous_key_sequence_previous_handler_process_process_coroutinesendargr"   s    r   r:   InputProcessor.reset_   s=    &*#!%"&--/$$T* r   c                      SU l         / U l        g)zStart recording macro. TN)r8   r9   r"   s    r   start_macroInputProcessor.start_macroj   s     
r   c                     SU l         g)zEnd recording macro. FN)r8   r"   s    r   	end_macroInputProcessor.end_macroo   s
    !r   c                 L    U R                    H  nU R                  U5        M     g r   )r9   feed)r   ks     r   
call_macroInputProcessor.call_macros   s    AIIaL r   c                     [        S U 5       5      nU R                  5       nU R                  R                  U5       Vs/ s H  oDR	                  U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   .0rN   s     r   	<genexpr>.InputProcessor._get_matches.<locals>.<genexpr>|        0KqUUK   )tupler3   r2   get_bindings_for_keysfilter)r   key_presseskeysclibs        r   _get_matchesInputProcessor._get_matchesw   sS    
 0K00mmo  >>??EWEaRUEWWWs    A&A&c                    ^ [        S U 5       5      nU R                  5       m[        S U R                  R	                  U5       5       5      n[        U4S jU 5       5      $ )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   rS   rT   s     r   rV   <InputProcessor._is_prefix_of_longer_match.<locals>.<genexpr>   rX   rY   c              3   8   #    U  H  oR                   v   M     g 7fr   )r\   )rU   r`   s     r   rV   re      s     ](\1hh(\rY   c              3   2   >#    U  H  o" T5      v   M     g 7fr   r.   )rU   fr_   s     r   rV   re      s     +7a1S667s   )rZ   r3   setr2   get_bindings_starting_with_keysany)r   r]   r^   filtersr_   s       @r   _is_prefix_of_longer_match)InputProcessor._is_prefix_of_longer_match   sU    
 0K00mmo ](V(VW[(\]] +7+++r   c              #     #    U R                   nSn U(       a  SnOUR                  Sv 5        U(       Ga   U R                  U5      nU R                  U5      nU Vs/ s H*  oUR	                  U R                  5       5      (       d  M(  UPM,     nnU(       a  UnSnU(       d   U(       a  U R                  US   U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M(  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	   )	r7   appendrm   ra   eagerr3   _call_handlerr   len)	r   bufferretryis_prefix_of_longer_matchmatchesmeager_matchesfoundis	            r   rA   InputProcessor._process   s=    
 u& ,0,K,KF,S)++F3 -4 PGqwwt}}7OG P +G05- 1W&&wr{&Hq	 37 E!E #3v;26"&"3"3F2AJ"?"7 ..wr{PRQR.T &rr
$(E! 7 !"2A2JO  !Qs   AD?'D:D:A8D?
5D?c                 h    [        U[        5      (       d   eU R                  R                  U5        g)zv
Add a new :class:`KeyPress` to the input queue.
(Don't forget to call `process_keys` in order to process the queue.)
N)r   r   r6   rr   )r   	key_presss     r   rM   InputProcessor.feed   s+    
 )X....	*r   c                    U R                   (       a  U R                   R                  5       nUR                  [        R                  :w  a  U R
                  R                  5         U R                  R                  U5        UR                  [        R                  :w  a  U R                  R                  5         U R                   (       a  M  U R                  5       nU(       a  UR                  5         gg)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.
N)r6   popleftr   r   CPRResponser4   firerB   rC   r5   r3   
invalidate)r   r   r_   s      r   process_keysInputProcessor.process_keys   s     ((002I}} 0 00##((*##((3}} 0 00""'')  mmoNN r   Nc                    U R                   nU R                  nS U l        [        [        R                  " U 5      XBU R
                  XR                  :H  S9nUR                  nUR                  U5      (       a!  U(       a  UR                  R                  5          UR                  U5        U R                  U5        X l        Xl        U R                   (       a$  U(       a  U R                  R                  U5        g g g ! [         a     NNf = f)N)rD   rq   previous_key_sequence	is_repeat)r8   rD   KeyPressEventweakrefrefr?   r@   r_   save_beforecurrent_buffersave_to_undo_stackcall_fix_vi_cursor_positionr   r9   extend)r   handlerrq   was_recordingrD   eventr_   s          r   rt   InputProcessor._call_handler   s    ))hhKK3"&"="="8"88; iiu%%#113	LL((/ '3#!( JJl+ "/ " 	 	s   "C> >
D
Dc                 Z   U R                  5       nU(       a  UR                  nUR                  n[        5       " UR                  5      (       a\  UR
                  R                  (       a@  [        UR
                  R                  5      S:  a  U=R                  S-  sl	        XCl        g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)
r3   r   preferred_columnr   r_   documentis_cursor_at_the_end_of_lineru   current_linecursor_position)r   r   r_   buffr   s        r   r   &InputProcessor._fix_vi_cursor_position  s     mmo%%D#44 "599--MM>>223a7$$)$ )9% 8 ? .	 r   )r3   r@   r?   rB   r2   r5   rD   r4   r6   r7   r9   r8   r   )r!   r)   r*   r+   r,   r   r:   rG   rJ   rO   ra   rm   rA   rM   r   rt   r   r-   r.   r   r   r   r   3   sF    *,	
"	X,"/'b+2 ,D9r   r   c                       \ rS rSrSr  SS jrS r\S 5       r\S 5       r	\S 5       r
\S	 5       r\S
 5       r\S 5       rS rSrg)r   i  a\  
Key press event, delivered to key bindings.

:param input_processor_ref: Weak reference to the `InputProcessor`.
: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.
Nc                 @    Xl         X0l        X@l        XPl        X l        g r   )_input_processor_refrq   r   r   _arg)r   input_processor_refrD   rq   r   r   s         r   r   KeyPressEvent.__init__'  s     $7!(%:" #	r   c                 \    SU R                   < SU R                  < SU R                  < S3$ )NzKeyPressEvent(arg=z, key_sequence=z, is_repeat=r   )rD   rq   r   r"   s    r   r#   KeyPressEvent.__repr__2  s!    $++T^^= 	=r   c                 4    U R                   S   R                  $ )Nrp   )rq   r   r"   s    r   r   KeyPressEvent.data6  s      $)))r   c                 "    U R                  5       $ r   )r   r"   s    r   input_processorKeyPressEvent.input_processor:  s    ((**r   c                 6    U R                   R                  5       $ )z
Command line interface.
)r   r3   r"   s    r   r_   KeyPressEvent.cli>  s    
 ##,,..r   c                 .    U R                   R                  $ )z
The current buffer.
)r_   r   r"   s    r   r   KeyPressEvent.current_bufferE  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   rD   KeyPressEvent.argL  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   rD   )r   r   currentr   s       r   append_to_arg_count!KeyPressEvent.append_to_arg_countc  sX     }$$$))3;?gn44F_F&-F#) r   )r   r   r   rq   r   )NNNF)r!   r)   r*   r+   r,   r   r#   propertyr   r   r_   r   rD   r   r   r-   r.   r   r   r   r     s     DH27	= * * + + / / ' '   % %*r   r   )r,   
__future__r   prompt_toolkit.bufferr   prompt_toolkit.filters.clir   prompt_toolkit.keysr   r   prompt_toolkit.utilsr   r;   r
   collectionsr   	six.movesr   r   r   __all__objectr   r   r   r.   r   r   <module>r      s\    ( 4 7 ) & "    
Av A.f9V f9TW*F W*r   