
    .                         d Z ddlmZ ddlmZ ddlmZ ddl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ZddlZdZ G d de      Z G d de      Z G d de      Zy)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                   $    e Zd ZdZddZd Z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        j                  t        f      sJ |t        |t        j                        sJ |t        |t              r|j                  n|}|| _        || _        y N)
isinstancesix	text_typer   namekeydata)selfr   r   s      =lib/third_party/prompt_toolkit/key_binding/input_processor.py__init__zKeyPress.__init__!   sZ    #s3444|z$>>><)#s3388D	    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__+   s%    NN##TXXtyy: 	:r   c                 j    | j                   |j                   k(  xr | j                  |j                  k(  S r   )r   r   )r   others     r   __eq__zKeyPress.__eq__/   s'    xx599$@ejj)@@r   r   )r   
__module____qualname____doc__r   r!   r$    r   r   r   r      s    :Ar   r   c                   Z    e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd ZddZd Zy)r   aw  
    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                     t        |t              sJ || _        || _        t	        |       | _        t	        |       | _        t               | _        g | _	        d| _
        g | _        | j                          y )NF)r   r
   	_registry_cli_refr   beforeKeyPressafterKeyPressr   input_queue
key_bufferrecord_macromacroreset)r   registrycli_refs      r   r   zInputProcessor.__init__I   sg    (L111!#Dk"4[ !7  "


r   c                     d | _         d | _        | j                         | _        | j                  j	                  d        d | _        y r   )_previous_key_sequence_previous_handler_process_process_coroutinesendargr    s    r   r3   zInputProcessor.reset_   s=    &*#!%"&--/$$T* r   c                      d| _         g | _        y)z Start recording macro. TN)r1   r2   r    s    r   start_macrozInputProcessor.start_macroj   s     
r   c                     d| _         y)z End recording macro. FN)r1   r    s    r   	end_macrozInputProcessor.end_macroo   s
    !r   c                 H    | j                   D ]  }| j                  |        y r   )r2   feed)r   ks     r   
call_macrozInputProcessor.call_macros   s    AIIaL r   c                     t        d |D              }| j                         }| 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   .0rC   s     r   	<genexpr>z.InputProcessor._get_matches.<locals>.<genexpr>|        0KqQUUK   )tupler,   r+   get_bindings_for_keysfilter)r   key_presseskeysclibs        r   _get_matcheszInputProcessor._get_matchesw   sU    
 0K00mmo  >>??EWEaRUEWWWs    AAc                     t        d |D              }| j                         t        d | j                  j	                  |      D              }t        f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   4   K   | ]  }|j                     y wr   rG   rH   s     r   rJ   z<InputProcessor._is_prefix_of_longer_match.<locals>.<genexpr>   rK   rL   c              3   4   K   | ]  }|j                     y wr   )rO   )rI   rS   s     r   rJ   z<InputProcessor._is_prefix_of_longer_match.<locals>.<genexpr>   s     ](\1ahh(\rL   c              3   .   K   | ]  } |        y wr   r(   )rI   frR   s     r   rJ   z<InputProcessor._is_prefix_of_longer_match.<locals>.<genexpr>   s     +7a1S67s   )rM   r,   setr+   get_bindings_starting_with_keysany)r   rP   rQ   filtersrR   s       @r   _is_prefix_of_longer_matchz)InputProcessor._is_prefix_of_longer_match   sU    
 0K00mmo ](V(VW[(\]] +7+++r   c              #     K   | j                   }d}	 |rd}n|j                  d       |r| j                  |      }| j                  |      }|D cg c]$  }|j	                  | j                               s#|& }}|r|}d}|s|r| j                  |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	   )	r0   appendr^   rT   eagerr,   _call_handlerr   len)	r   bufferretryis_prefix_of_longer_matchmatchesmeager_matchesfoundis	            r   r9   zInputProcessor._process   s>    
 u& ,0,K,KF,S)++F3 -4 PGqqwwt}}7OG P +G05- 1W&&wr{&Hq	 37 E!E #3v;26"&"3"3F2AJ"?" ..wr{PRQR.T &rr
$(E! 7 !"2A2JO  !Qs   AD$D7D;AD0Dc                 ^    t        |t              sJ | 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.)
        N)r   r   r/   rb   )r   	key_presss     r   rB   zInputProcessor.feed   s(    
 )X...	*r   c                    | j                   r| j                   j                         }|j                  t        j                  k7  r| j
                  j                          | j                  j                  |       |j                  t        j                  k7  r| j                  j                          | j                   r| j                         }|r|j                          yy)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.
        N)r/   popleftr   r   CPRResponser-   firer:   r;   r.   r,   
invalidate)r   ro   rR   s      r   process_keyszInputProcessor.process_keys   s     ((002I}} 0 00##((*##((3}} 0 00""'')  mmoNN r   Nc                    | j                   }| j                  }d | _        t        t        j                  |       ||| j
                  || j                  k(        }|j                  }|j                  |      r|r|j                  j                          	 |j                  |       | j                  |       || _        || _        | j                   r|r| j                  j                  |       y y y # t        $ r Y Ew xY w)N)r<   ra   previous_key_sequence	is_repeat)r1   r<   KeyPressEventweakrefrefr7   r8   rR   save_beforecurrent_buffersave_to_undo_stackcall_fix_vi_cursor_positionr   r2   extend)r   handlerra   was_recordingr<   eventrR   s          r   rd   zInputProcessor._call_handler   s    ))hhKK3\"&"="=$"8"88; iiu%#113	LL((/ '3#!( JJl+ "/ " 	 	s   "C0 0	C<;C<c                 <   | j                         }|r|j                  }|j                  } t               |j                        rW|j
                  j                  r@t        |j
                  j                        dkD  r|xj                  dz  c_	        ||_        y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   rR   documentis_cursor_at_the_end_of_linere   current_linecursor_position)r   r   rR   buffr   s        r   r   z&InputProcessor._fix_vi_cursor_position  s     mmo%%D#44" "599-MM>>223a7$$)$ )9% 8 ? .	 r   r   )r   r%   r&   r'   r   r3   r>   r@   rD   rT   r^   r9   rB   ru   rd   r   r(   r   r   r   r   3   sF    *,	
"	X,"/'b+2 ,D9r   r   c                       e Zd ZdZ	 	 ddZd Zed        Zed        Zed        Z	ed        Z
ed	        Zed
        Zd Zy)ry   ax  
    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                 J    || _         || _        || _        || _        || _        y r   )_input_processor_refra   rw   rx   _arg)r   input_processor_refr<   ra   rw   rx   s         r   r   zKeyPressEvent.__init__'  s*    $7!(%:" #	r   c                 V    d| j                   d| j                  d| j                  dS )NzKeyPressEvent(arg=z, key_sequence=z, is_repeat=r   )r<   ra   rx   r    s    r   r!   zKeyPressEvent.__repr__2  s!    $++T^^= 	=r   c                 4    | j                   d   j                  S )Nr`   )ra   r   r    s    r   r   zKeyPressEvent.data6  s      $)))r   c                 "    | j                         S r   )r   r    s    r   input_processorzKeyPressEvent.input_processor:  s    ((**r   c                 6    | j                   j                         S )z)
        Command line interface.
        )r   r,   r    s    r   rR   zKeyPressEvent.cli>  s    
 ##,,..r   c                 .    | j                   j                  S )z%
        The current buffer.
        )rR   r}   r    s    r   r}   zKeyPressEvent.current_bufferE  s    
 xx&&&r   c                 x    | j                   dk(  ryt        | j                   xs d      }t        |      dk\  rd}|S )z&
        Repetition argument.
        -r`   r	   i@B )r   int)r   results     r   r<   zKeyPressEvent.argL  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   r<   )r   r   currentr   s       r   append_to_arg_countz!KeyPressEvent.append_to_arg_countc  sZ     }$$$))3;?gn44F_F&-F#) r   )NNNF)r   r%   r&   r'   r   r!   propertyr   r   rR   r}   r<   r   r   r(   r   r   ry   ry     s     DH27	= * * + + / / ' '   % %*r   ry   )r'   
__future__r   prompt_toolkit.bufferr   prompt_toolkit.filters.clir   prompt_toolkit.keysr   r   prompt_toolkit.utilsr   r4   r
   collectionsr   	six.movesr   rz   r   __all__objectr   r   ry   r(   r   r   <module>r      s\    ( 4 7 ) & "    
Av A.f9V f9TW*F W*r   