
                            S r SSKJr  SSK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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"r"SSK#r#SSK$r$SSK%r%SSK&r&Sr' " S S\(5      r) " S S\*5      r+S r,\+" \,5      \+l-        \+" SS9\+l.         " S S\*5      r/ " S S\*5      r0\#Rb                  " S5      r2 " S S\*5      r3 " S  S!\*5      r4S%S" jr5S%S# jr6S$ r7g)&zU
Data structures for the Buffer.
It holds the text, cursor position, history, etc...
    )unicode_literals   )AutoSuggest)ClipboardData)	Completer
CompletionCompleteEvent)Document)IncrementalSearchDirection)to_simple_filter)HistoryInMemoryHistory)SearchState)SelectionTypeSelectionState	PasteMode)Event)FastDictCache)ValidationError)rangeN)EditReadOnlyBufferAcceptActionBufferindentunindentreshape_textc                       \ rS rSrSrSrg)r   &   z/Attempt editing of read-only :class:`.Buffer`.  N)__name__
__module____qualname____firstlineno____doc____static_attributes__r       (lib/third_party/prompt_toolkit/buffer.pyr   r   &   s    6r&   r   c                   L    \ rS rSrSrS	S jr\S
S j5       r\S 5       r	S r
Srg)r   *   a5  
What to do when the input is accepted by the user.
(When Enter was pressed in the command line.)

:param handler: (optional) A callable which takes a
    :class:`~prompt_toolkit.interface.CommandLineInterface` and
    :class:`~prompt_toolkit.document.Document`. It is called when the user
    accepts input.
Nc                 :    Ub  [        U5      (       d   eXl        g N)callablehandler)selfr-   s     r'   __init__AcceptAction.__init__4   s    (7"3"333r&   c                 &   ^^ UU4S jn[        US9$ )z
Create an :class:`.AcceptAction` that runs the given handler in the
terminal.

:param render_cli_done: When True, render the interface in the 'Done'
        state first, then execute the function. If False, erase the
        interface instead.
c                 6   >^ ^ T R                  UU U4S jTS9  g )Nc                     > T" TT 5      $ r+   r   )bufferclir-   s   r'   <lambda>@AcceptAction.run_in_terminal.<locals>._handler.<locals>.<lambda>C   s    V(<r&   )render_cli_done)run_in_terminal)r5   r4   r-   r8   s   ``r'   _handler.AcceptAction.run_in_terminal.<locals>._handlerB   s     <o^r&   r-   )r   )clsr-   r8   r:   s    `` r'   r9   AcceptAction.run_in_terminal8   s    	_H--r&   c                 ,    [        U R                  5      $ )z/
True when there is something handling accept.
)boolr-   r.   s    r'   is_returnableAcceptAction.is_returnableF   s    
 DLL!!r&   c                     UR                  5       (       a3  U R                  (       a  U R                  X5        UR                  5         gg)z/
Validate buffer and handle the accept action.
N)validater-   append_to_history)r.   r5   r4   s      r'   validate_and_handle AcceptAction.validate_and_handleM   s5     ??||S)$$&	 r&   r<   r+   F)r    r!   r"   r#   r$   r/   classmethodr9   propertyrB   rG   r%   r   r&   r'   r   r   *   s9     . . " "'r&   r   c                 ~   ^ U R                  TR                  5        U4S jnU R                  R                  U5        g )Nc                  &   > T R                  5         g r+   )reset)r4   s   r'   reset_this_buffer3_return_document_handler.<locals>.reset_this_buffer^   s    r&   )set_return_valuedocumentpre_run_callablesappend)r5   r4   rO   s    ` r'   _return_document_handlerrU   X   s/    )  !23r&   r<   c                   $    \ rS rSrSrSrSrSrSrg)ValidationStateg   zDThe validation state of a buffer. This is set after the validation. VALIDINVALIDUNKNOWNr   N)	r    r!   r"   r#   r$   rY   rZ   r[   r%   r   r&   r'   rW   rW   g   s    KEGGr&   rW   c                   D    \ rS rSrSrS
S jrS rS rS r\	S 5       r
S	rg)CompletionStaten   z3
Immutable class that contains a completion state.
Nc                 <    Xl         U=(       d    / U l        X0l        g r+   )original_documentcurrent_completionscomplete_index)r.   r`   ra   rb   s       r'   r/   CompletionState.__init__r   s    !2 $7#<"  -r&   c                     U R                   R                  < SU R                  < S[        U R                  5      < SU R
                  < S3$ )N(z, <z> completions, index=))	__class__r    r`   lenra   rb   rA   s    r'   __repr__CompletionState.__repr__~   s9    NN##""C(@(@$A4CVCVX 	Xr&   c                 @    [        U R                  U R                  US9$ )zC
Create a new :class:`.CompletionState` object with the new index.
)rb   )r]   r`   ra   r.   indexs     r'   go_to_indexCompletionState.go_to_index   s      t55t7O7O`effr&   c                    U R                   c,  U R                  R                  U R                  R                  4$ U R                  R                  nU R                  R
                  nU R                  U R                      nUR                  S:X  a  UnOUSUR                   nXCR                  -   U-   n[        U5      [        UR                  5      -   nXV4$ )z=
Return (new_text, new_cursor_position) for this completion.
Nr   )	rb   r`   textcursor_positiontext_before_cursortext_after_cursorra   start_positionrh   )r.   original_text_before_cursororiginal_text_after_cursorcbeforenew_textnew_cursor_positions          r'   new_text_and_position%CompletionState.new_text_and_position   s     &))..0F0F0V0VVV*.*@*@*S*S')-)?)?)Q)Q&(()<)<=A1$445Fa6F6FG)CCH"%f+AFF";00r&   c                 P    U R                   b  U R                  U R                      $ g)zQ
Return the current completion, or return `None` when no completion is
selected.
N)rb   ra   rA   s    r'   current_completion"CompletionState.current_completion   s,     *++D,?,?@@ +r&   )rb   ra   r`   )NN)r    r!   r"   r#   r$   r/   ri   rn   r|   rK   r   r%   r   r&   r'   r]   r]   n   s3    
-X
g1( A Ar&   r]   z(\s+|".*?"|'.*?')c                   (    \ rS rSrSrSS jrS rSrg)YankNthArgState   zL
For yank-last-arg/yank-nth-arg: Keep track of where we are in the history.
c                 (    Xl         X0l        X l        g r+   )history_positionprevious_inserted_wordn)r.   r   r   r   s       r'   r/   YankNthArgState.__init__   s     0&<#r&   c                     U R                   R                  < SU R                  < SU R                  < SU R                  < S3$ )Nz(history_position=z, n=z, previous_inserted_word=rf   )rg   r    r   r   r   rA   s    r'   ri   YankNthArgState.__repr__   s0    NN##T%:%:DFF'') 	)r&   )r   r   r   N)r    )r    r!   r"   r#   r$   r/   ri   r%   r   r&   r'   r   r      s    
)r&   r   c                      \ rS rSrSrSSSSSSSSS\R                  SSSS4S jrSGS jrS r	S	 r
\S
 5       r\R                  S 5       r\S 5       r\R                  S 5       r\S 5       r\R                  S 5       rS rS r\S 5       r\R                  S 5       rSHS jrSIS jrS rS rS rSJS jrSJS jrSJS jrSJS jrSKS jrSKS jrSJS  jrSJS! jrSLS" jr SLS# jr!S$ r"S% r#SKS& jr$SKS' jr%S( r&SMS) jr'S* r(S+ r)S, r*S- r+S. r,SJS/ jr-SJS0 jr.SGS1 jr/SNS2 jr0\1Rd                  4S3 jr3SHS4 jr4S5 r5\6Rn                  S4S6 jr8SIS7 jr9SIS8 jr:SIS9 jr;SOS: jr<S; r=S< r>S= r?S> r@SPS? jrAS@ rBSQSA jrCSQSB jrDSC rESD rFSE rGSFrHg)Rr      aL  
The core data structure that holds the text and cursor position of the
current input line and implements all text manupulations on top of it. It
also implements the history, undo stack and the completion state.

:param completer: :class:`~prompt_toolkit.completion.Completer` instance.
:param history: :class:`~prompt_toolkit.history.History` instance.
:param tempfile_suffix: Suffix to be appended to the tempfile for the 'open
                       in editor' function.

Events:

:param on_text_changed: When the buffer text changes. (Callable on None.)
:param on_text_insert: When new text is inserted. (Callable on None.)
:param on_cursor_position_changed: When the cursor moves. (Callable on None.)

Filters:

:param is_multiline: :class:`~prompt_toolkit.filters.SimpleFilter` to
    indicate whether we should consider this buffer a multiline input. If
    so, key bindings can decide to insert newlines when pressing [Enter].
    (Instead of accepting the input.)
:param complete_while_typing: :class:`~prompt_toolkit.filters.SimpleFilter`
    instance. Decide whether or not to do asynchronous autocompleting while
    typing.
:param enable_history_search: :class:`~prompt_toolkit.filters.SimpleFilter`
    to indicate when up-arrow partial string matching is enabled. It is
    adviced to not enable this at the same time as `complete_while_typing`,
    because when there is an autocompletion found, the up arrows usually
    browse through the completions, rather than through the history.
:param read_only: :class:`~prompt_toolkit.filters.SimpleFilter`. When True,
    changes will not be allowed.
Nr   Fc                    [        U5      n[        U5      n[        U5      n[        U5      nUb  [        U[        5      (       d   eUb  [        U[        5      (       d   eUb  [        U[        5      (       d   eUb  [        U5      (       d   eUb  [        U5      (       d   eUb  [        U5      (       d   eXl        X l        X@l        XPl	        Xl
        X`l        Xpl        Xl        Xl        SU l        Uc
  [!        5       OUU l        SU l        ['        X5      U l        ['        X5      U l        ['        X5      U l        [/        [0        SS9U l        U R5                  U	S9  g )Nr   
   )size)initial_document)r   
isinstancer   r   r   r,   	completerauto_suggest	validatortempfile_suffixaccept_actionis_multilinecomplete_while_typingenable_history_search	read_only
text_widthr   history_Buffer__cursor_positionr   on_text_changedon_text_inserton_cursor_position_changedr   r
   _document_cacherN   )r.   r   r   r   r   r   r   r   r   r   r   r   r   r   r   s                  r'   r/   Buffer.__init__   s]    !11F G'5 01F G$Y/	  Jy)$D$DDD#z,'L'LLL*Wg">">>>&(?*C*CCC%.)A)AAA)1X>X5Y5YYY"(".* )%:"%:"" 
 -4O(!"  %T;#D9*/*Q'  -XB?

$4
5r&   c                 2   Ub  [        U[        5      (       d   eU(       a  U R                  5         U=(       d
    [        5       nUR                  U l        SU l        [        R                  U l        SU l	        / U l
        SU l        SU l        SU l        SU l        SU l        SU l        / U l        / U l        U R&                  R(                  SS U l        U R*                  R-                  UR.                  5        [1        U R*                  5      S-
  U l        g)zB
:param append_to_history: Append current input to history first.
Nr   )r   r
   rF   rr   r   validation_errorrW   r[   validation_stateselection_statemultiple_cursor_positionspreferred_columncomplete_stateyank_nth_arg_statedocument_before_paste
suggestionhistory_search_text_undo_stack_redo_stackr   strings_working_linesrT   rq   rh   _Buffer__working_index)r.   r   rF   s      r'   rN   Buffer.reset  s     ':6F+Q+QQQ""$+9xz!1!A!A !% / 7 7  $
 *,& !% # #' &*"  $(   #ll2215""#3#8#89"4#6#67!;r&   c                     U R                   nU R                  nX2   nXU'   [        U5      [        U5      :w  a  gX:w  a  gg)z>set text at current working_index. Return whether it changed. TF)working_indexr   rh   )r.   valuer   working_linesoriginal_values        r'   	_set_textBuffer._set_textL  sL    **++&5',m$ u:^,, $r&   c                 D    U R                   n[        SU5      U l         X:g  $ )z0Set cursor position. Return whether it changed. r   )r   max)r.   r   original_positions      r'   _set_cursor_positionBuffer._set_cursor_position`  s%     22!$Q))r&   c                 4    U R                   U R                     $ r+   )r   r   rA   s    r'   rq   Buffer.textg  s    ""4#5#566r&   c                 *   [        U[        R                  5      (       d
   SU-  5       eU R                  [	        U5      ::  d   eU R                  5       (       a
  [        5       eU R                  U5      nU(       a  U R                  5         SU l	        gg)z
Setting text. (When doing this, make sure that the cursor_position is
valid for this text. text/cursor_position should be consistent at any time,
otherwise set a Document instead.)
zGot %rN)
r   six	text_typerr   rh   r   r   r   _text_changedr   r.   r   changeds      r'   rq   r   k  s     %//AE1AA/##s5z111 >>$&&..'  (,D$	 r&   c                     U R                   $ r+   )r   rA   s    r'   rr   Buffer.cursor_position  s    %%%r&   c                     [        U[        5      (       d   eU[        U R                  5      ::  d   eU R	                  U5      nU(       a  U R                  5         gg)z
Setting cursor position.
N)r   intrh   rq   r   _cursor_position_changedr   s      r'   rr   r     sQ    
 %%%%%DII&&&++E2))+ r&   c                     U R                   $ r+   )r   rA   s    r'   r   Buffer.working_index  s    ###r&   c                 R    U R                   U:w  a  Xl         U R                  5         g g r+   )r   r   r.   r   s     r'   r   r     s'    5(#(   )r&   c                     S U l         [        R                  U l        S U l        S U l        S U l        S U l        S U l        S U l	        U R                  R                  5         g r+   )r   rW   r[   r   r   r   r   r   r   r   r   firerA   s    r'   r   Buffer._text_changed  s[     $ / 7 7""&%)"# $ 	!!#r&   c                     S U l         [        R                  U l        S U l        S U l        S U l        S U l        U R                  R                  5         g r+   )
r   rW   r[   r   r   r   r   r   r   r   rA   s    r'   r   Buffer._cursor_position_changed  sN     $ / 7 7""&%)" !% 	'',,.r&   c                 b    U R                   U R                  U R                  U R                  4   $ )zx
Return :class:`~prompt_toolkit.document.Document` instance from the
current text, cursor position and selection state.
)r   rq   rr   r   rA   s    r'   rR   Buffer.document  s3     ##IIt++T-A-AAC 	Cr&   c                 &    U R                  U5        g)z
Set :class:`~prompt_toolkit.document.Document` instance.

This will set both the text and cursor position at the same time, but
atomically. (Change events will be triggered only after both have been set.)
N)set_documentr   s     r'   rR   r     s     	% r&   c                 H   [        U[        5      (       d   eU(       d  U R                  5       (       a
  [        5       eU R	                  UR
                  5      nU R                  UR                  5      nU(       a  U R                  5         U(       a  U R                  5         gg)a4  
Set :class:`~prompt_toolkit.document.Document` instance. Like the
``document`` property, but accept an ``bypass_readonly`` argument.

:param bypass_readonly: When True, don't raise an
                        :class:`.EditReadOnlyBuffer` exception, even
                        when the buffer is read-only.
N)
r   r
   r   r   r   rq   r   rr   r   r   )r.   r   bypass_readonlytext_changedcursor_position_changeds        r'   r   Buffer.set_document  s     %**** 4>>#3#3$&& ~~ejj1"&";";E<Q<Q"R  "))+ #r&   c                 >   U R                   (       aL  U R                   S   S   U R                  :X  a,  U R                   S   S   U R                  4U R                   S'   O1U R                   R                  U R                  U R                  45        U(       a  / U l        gg)za
Safe current state (input text and cursor position), so that we can
restore it by calling undo.
r   r   N)r   rq   rr   rT   r   )r.   clear_redo_stacks     r'   save_to_undo_stackBuffer.save_to_undo_stack  s      0 0 4Q 7499 D$($4$4R$8$;T=Q=Q#RDR ##TYY0D0D$EF !D r&   c                     U R                   R                  S5      nU H  n U" X4   5      X4'   M     SR                  U5      $ ! [         a     M2  f = f)a  
Transforms the text on a range of lines.
When the iterator yield an index not in the range of lines that the
document contains, it skips them silently.

To uppercase some lines::

    new_text = transform_lines(range(5,10), lambda text: text.upper())

:param line_index_iterator: Iterator of line numbers (int)
:param transform_callback: callable that takes the original text of a
                           line, and return the new text for this line.

:returns: The new text.

)rq   split
IndexErrorjoin)r.   line_index_iteratortransform_callbacklinesrm   s        r'   transform_linesBuffer.transform_lines  sZ    " 		% )E1%,? ) yy  s   A
AAc                    U R                   nUR                  UR                  5       -   nUR                  UR                  5       -   nUR                  SU U" UR                  X4 5      -   UR                  US -   U l        g)z
Apply the given transformation function to the current line.

:param transform_callback: callable that takes a string and return a new string.
N)rR   rr   get_start_of_line_positionget_end_of_line_positionrq   )r.   r   rR   abs        r'   transform_current_lineBuffer.transform_current_line  s}     ==$$x'J'J'LL$$x'H'H'JJMM"1x}}Q123MM!" 		r&   c                     X:  d   eSR                  U R                  SU U" U R                  X 5      -   U R                  US -   /5      U l        g)z
Transform a part of the input string.

:param from_: (int) start position.
:param to: (int) end position.
:param transform_callback: Callable which accepts a string and returns
    the transformed string.
r   N)r   rq   )r.   from_tor   s       r'   transform_regionBuffer.transform_region&  sY     zzGGIIfutyy234IIbcN
 	r&   r   c                 \    U =R                   U R                  R                  US9-  sl         g Ncount)rr   rR   get_cursor_left_positionr.   r   s     r'   cursor_leftBuffer.cursor_left7  s%     F FU F SSr&   c                 \    U =R                   U R                  R                  US9-  sl         g r   )rr   rR   get_cursor_right_positionr   s     r'   cursor_rightBuffer.cursor_right:  s%     G Ge G TTr&   c                     U R                   =(       d    U R                  R                  nU =R                  U R                  R	                  XS9-  sl        X l         g)z9(for multiline edit). Move cursor to the previous line.  r   r   N)r   rR   cursor_position_colrr   get_cursor_up_positionr.   r   original_columns      r'   	cursor_upBuffer.cursor_up=  sO    //T4==3T3T D D !E !; 	; !0r&   c                     U R                   =(       d    U R                  R                  nU =R                  U R                  R	                  XS9-  sl        X l         g)z5(for multiline edit). Move cursor to the next line.  r  N)r   rR   r	  rr   get_cursor_down_positionr  s      r'   cursor_downBuffer.cursor_downF  sO    //T4==3T3T F F !G !; 	; !0r&   c                 F   U R                   (       a  U R                  US9  gU R                  R                  S:  a  U R	                  US9  gU R
                  (       dE  U R                  US9  U(       a.  U =R                  U R                  R                  5       -  sl        ggg)z}
If we're not on the first line (of a multiline input) go a line up,
otherwise go back in history. (If nothing is selected.)
r   r   N)	r   complete_previousrR   cursor_position_rowr  r   history_backwardrr   r   r.   r   &go_to_start_of_line_if_history_changess      r'   auto_upBuffer.auto_upO  s    
 """/]]..2NNN'%%!!!. 6$$(P(P(RR$ 6	 &r&   c                 t   U R                   (       a  U R                  US9  gU R                  R                  U R                  R                  S-
  :  a  U R                  US9  gU R                  (       dE  U R                  US9  U(       a.  U =R                  U R                  R                  5       -  sl        ggg)z
If we're not on the last line (of a multiline input) go a line down,
otherwise go forward in history. (If nothing is selected.)
r   r   N)
r   complete_nextrR   r  
line_countr  r   history_forwardrr   r   r  s      r'   	auto_downBuffer.auto_down_  s    
 U+]]..1I1IA1MM5)%%  u - 6$$(P(P(RR$ 6	 &r&   c                 B   US:  d   eSnU R                   S:  a  U R                  U R                   U-
  U R                    nU R                  SU R                   U-
   U R                  U R                   S -   nU R                   [        U5      -
  n[        X45      U l        U$ )zR
Delete specified number of characters before cursor and return the
deleted text.
r   r   N)rr   rq   rh   r
   rR   )r.   r   deletedrz   r{   s        r'   delete_before_cursorBuffer.delete_before_cursoro  s    
 zz!#ii 4 4u <T=Q=QRGyy!>$"6"6">?$))DL`L`LaBbbH"&"6"6W"E %XCDMr&   c                    U R                   [        U R                  5      :  a_  U R                  R                  SU nU R                  SU R                    U R                  U R                   [        U5      -   S -   U l        U$ g)zD
Delete specified number of characters and Return the deleted text.
Nr   )rr   rh   rq   rR   rt   )r.   r   r"  s      r'   deleteBuffer.delete  su     #dii.0mm55fu=G		"74#7#78		$..W=>?@DINr&   c                 >   U R                   R                  (       d  U =R                  U R                   R                  5       -  sl        U R	                  5         U R                   R
                  U-   U R                   R                  R                  S5      -   U l        gg)z[
Join the next line to the current one by deleting the line ending after
the current line.
 N)	rR   on_last_linerr   r   r&  rs   rt   lstriprq   )r.   	separators     r'   join_next_lineBuffer.join_next_line  ss    
 }}))  DMM$J$J$LL KKM 99IE88??DEDI *r&   c                    U R                   (       d   e[        U R                  U R                   R                  /5      u  p#U R                  SU nU R                  X# R                  5       nU R                  US nU Vs/ s H  owR                  S5      U-   PM     nn[        USR                  U5      -   U-   [        USR                  USS 5      -   5      S-
  S9U l
        gs  snf )z
Join the selected lines.
Nr)  r   r   r   rq   rr   )r   sortedrr   original_cursor_positionrq   
splitlinesr+  r
   r   rh   rR   )r.   r,  r   r   ry   r   afterls           r'   join_selected_linesBuffer.join_selected_lines  s     #### D00$2F2F2_2_`a	6E"		%#..0		"# 5::Eq#*E: !frwwu~&=&E14VbggeCRj>Q5Q1RUV1VX ;s   ?C$c                     U R                   nUS:  aR  U R                  US-
     nU R                  US-
     nU R                  SUS-
   U-   U-   U R                  US -   U l        gg)z1
Swap the last two characters before the cursor.
   r   N)rr   rq   )r.   posr   r   s       r'   swap_characters_before_cursor$Buffer.swap_characters_before_cursor  sn     ""!8		#'"A		#'"A		&3q5)A-1DIIcdOCDI	 r&   c                 x    U[        U R                  5      :  a!  Xl        [        U R                  5      U l        gg)z!
Go to this item in the history.
N)rh   r   r   rq   rr   rl   s     r'   go_to_historyBuffer.go_to_history  s2     3t**++!&#&tyy>D  ,r&   c                 X   U R                   (       a  [        U R                   R                  5      nU R                   R                  c  SnONU R                   R                  US-
  :X  a  SnU(       a  gO&[	        US-
  U R                   R                  U-   5      nU R                  U5        gg)zL
Browse to the next completions.
(Does nothing if there are no completion.)
Nr   r   )r   rh   ra   rb   mingo_to_completion)r.   r   disable_wrap_aroundcompletions_countrm   s        r'   r  Buffer.complete_next  s    
  #D$7$7$K$K L""119$$337H17LL& ' -a/1D1D1S1SV[1[\!!%( r&   c                 N   U R                   (       a  U R                   R                  S:X  a  SnU(       a  gO]U R                   R                  c#  [        U R                   R                  5      S-
  nO#[	        SU R                   R                  U-
  5      nU R                  U5        gg)zP
Browse to the previous completions.
(Does nothing if there are no completion.)
r   Nr   )r   rb   rh   ra   r   rB  )r.   r   rC  rm   s       r'   r  Buffer.complete_previous  s    
 ""11Q6& '$$33;D//CCDqHAt22AAEIJ!!%( r&   c                 X    U R                   (       a  U R                  S5        SU l         gg)z2
Cancel completion, go back to the original text.
N)r   rB  rA   s    r'   cancel_completionBuffer.cancel_completion  s(     !!$'"&D r&   c           	         U(       a	  U(       a   eUcJ  U R                   (       a7  [        U R                   R                  U R                  [	        SS95      5      nO/ nU(       ai  [        U R                  US9U l        U(       a  U R                  S5        gU(       a  U R                  [        U5      S-
  5        gU R                  S5        gSU l        g)zC
Start completions. (Generate list of completions and initialize.)
NT)completion_requested)r`   ra   r   r   )	r   listget_completionsrR   r	   r]   r   rB  rh   )r.   completionsgo_to_first
go_to_lasts       r'   set_completionsBuffer.set_completions  s      J// ~~"4>>#A#AMM!t<$ 
 ! "1"&--$/#1D %%a(%%c+&6&:;%%d+ #'Dr&   c           
      :   [        5       n/ nU R                  R                  R                  5       n[	        U R
                  5       H  u  pE[	        UR                  S5      5       H  u  pgUR                  5       nU(       d  M  UR                  U5      (       d  M6  Xq;  d  M=  UR                  U5        X@R                  :X  a	  SUS-   -  nOSUS-   < SUS-   < 3nUR                  [        U[        U5      * US95        M     M     U R                  USSS2   S	9  g)
zR
Start a completion based on all the other lines in the document and the
history.
r   zCurrent, line %sr   zHistory z, line )ru   display_metaNr   )rO  )setrR   current_line_before_cursorr+  	enumerater   r   strip
startswithaddr   rT   r   rh   rR  )	r.   found_completionsrO  current_lineistringjr5  rU  s	            r'   start_history_lines_completion%Buffer.start_history_lines_completion  s   
  E }}??FFH"4#6#67IA!&,,t"45GGI1l331)--a0  2 22+=1+EL 3 EFaC1+ML#**:,/,=+=)5,7 8 6 8& 	TrT):;r&   c                     Ub  [        U[        5      (       d   eU R                  (       d   eU R                  R                  U5      nUR	                  5       u  p4[        X45      U l        X l        g)z;
Select a completion from the list of current completions.
N)r   r   r   rn   r|   r
   rR   )r.   rm   staterz   r{   s        r'   rB  Buffer.go_to_completion/  si     }
5# 6 666"""" ##//6 ).(C(C(E% ? $r&   c                     [        U[        5      (       d   eU R                  (       a  U R                  S5        SU l        U R	                  UR
                  * 5        U R                  UR                  5        g)z
Insert a given completion.
N)r   r   r   rB  r#  ru   insert_textrq   )r.   
completions     r'   apply_completionBuffer.apply_completion@  sc     *j1111 !!$'" 	!!:#<#<"<=)r&   c                 |    U R                  5       (       a   U R                  c  U R                  U l        ggSU l        g)zSet `history_search_text`. N)r   r   rq   rA   s    r'   _set_history_searchBuffer._set_history_searchO  s8    %%''''/+/99( 0 (,D$r&   c                 |    U R                   SL =(       d(    U R                  U   R                  U R                   5      $ )zn
True when the current entry matches the history search.
(when we don't have history search, it's also True.)
N)r   r   rZ  )r.   r^  s     r'   _history_matchesBuffer._history_matchesW  s=    
 ((D0 L##A&11$2J2JK	Mr&   c                 \   U R                  5         Sn[        U R                  S-   [        U R                  5      5       H.  nU R                  U5      (       a  X0l        US-  nSnUS:X  d  M.    O   U(       a5  SU l        U =R                  U R                  R                  5       -  sl        gg)zT
Move forwards through the history.

:param count: Amount of items to move forward.
Fr   Tr   N)	rl  r   r   rh   r   ro  rr   rR   r   r.   r   found_somethingr^  s       r'   r  Buffer.history_forward_  s     	  "  t))A-s43F3F/GHA$$Q''%&"
"&z I #$D   DMM$J$J$LL  r&   c                    U R                  5         Sn[        U R                  S-
  SS5       H.  nU R                  U5      (       a  X0l        US-  nSnUS:X  d  M.    O   U(       a  [	        U R
                  5      U l        gg)z!
Move backwards through history.
Fr   r   Tr   N)rl  r   r   ro  rh   rq   rr   rr  s       r'   r  Buffer.history_backwardw  s~     	  "  t))A-r26A$$Q''%&"
"&z 7 #&tyy>D  r&   c                    Ub  [        U[        5      (       d   e[        U R                  5      (       d  gU R                  c  [        U(       a  SOSS9nOU R                  nUb  Xl        UR                  S-
  nU* [        U R                  5      :  a  SnU R                  U   n[        R                  U5       Vs/ s H  ofR                  5       PM     nnU Vs/ s H  of(       d  M  UPM     nn XsR                     nUR                  (       a$  U R                  [        UR                  5      5        U R                  U5        Xl        XCl        X0l        gs  snf s  snf ! [         a    Sn Nqf = f)aA  
Pick nth word from previous history entry (depending on current
`yank_nth_arg_state`) and insert it at current position. Rotate through
history if called repeatedly. If no `n` has been given, take the first
argument. (The second word.)

:param n: (None or int), The index of the word from the previous line
    to take.
Nr   r   )r   r   )r   r   rh   r   r   r   r   r   _QUOTED_WORDS_REr   rY  r   r   r#  rg  )	r.   r   _yank_last_argrd  new_poslinewwordswords	            r'   yank_nth_argBuffer.yank_nth_arg  sH    yJq#....4<<   ""*#NbBE++E=G ((1,8c$,,''G ||G$$4$:$:4$@A$@q$@A!'EqQE'	>D
 ''%%c%*F*F&GH (,$!("'! B'  	D	s$   <E
E)E2E# #E21E2c                 $    U R                  USS9  g)zX
Like `yank_nth_arg`, but if no argument has been given, yank the last
word by default.
T)r   ry  N)r  )r.   r   s     r'   yank_last_argBuffer.yank_last_arg  s    
 	Ad3r&   c                 :    [        U R                  U5      U l        g)zB
Take the current cursor position as the start of this selection.
N)r   rr   r   )r.   selection_types     r'   start_selectionBuffer.start_selection  s      .d.B.BNSr&   c                 f    U R                   R                  5       u  p#U(       a  X l         SU l        U$ )zA
Copy selected text and return :class:`.ClipboardData` instance.
N)rR   cut_selectionr   )r.   _cutnew_documentclipboard_datas       r'   copy_selectionBuffer.copy_selection  s/     (,}}'B'B'D$(M#r&   c                      U R                  SS9$ )zC
Delete selected text and return :class:`.ClipboardData` instance.
T)r  )r  rA   s    r'   r  Buffer.cut_selection  s     """--r&   c                     [        U[        5      (       d   eU[        R                  [        R                  [        R
                  4;   d   eU R                  nU R                  R                  XUS9U l        X@l        g)z%
Insert the data from the clipboard.
)
paste_moder   N)	r   r   r   	VI_BEFOREVI_AFTEREMACSrR   paste_clipboard_datar   )r.   datar  r   r`   s        r'   r  Buffer.paste_clipboard_data  sj     $....i1193E3EyWWWW MM::4^c:d &7"r&   c                     U(       a)  U R                  SU R                  R                  -   5        gU R                  S5        g)z/
Insert a line ending at the current position.
r   N)rg  rR   "leading_whitespace_in_current_line)r.   copy_margins     r'   newlineBuffer.newline  s2     TDMM$T$TTUT"r&   c                     U(       a  U R                   R                  S-   nOSnU =R                  U R                   R                  5       -  sl        U R	                  U5        U =R                  S-  sl        g)z*
Insert a new line above the current one.
r   r   N)rR   r  rr   r   rg  r.   r  inserts      r'   insert_line_aboveBuffer.insert_line_above  s]     ]]EELFF H H JJ !r&   c                     U(       a  SU R                   R                  -   nOSnU =R                  U R                   R                  5       -  sl        U R	                  U5        g)z*
Insert a new line below the current one.
r   N)rR   r  rr   r   rg  r  s      r'   insert_line_belowBuffer.insert_line_below   sL     DMMLLLFF F F HH r&   c                    U R                   nU R                  nU(       aI  XVU[        U5      -    nSU;   a  USUR                  S5       nUSU U-   XV[        U5      -   S -   U l         OUSU U-   XVS -   U l         U(       a  U =R                  [        U5      -  sl        U(       a  U R                  R                  5         gg)z
Insert characters at cursor position.

:param fire_event: Fire `on_text_insert` event. This is mainly used to
    trigger autocompletion while typing.
r   N)rq   rr   rh   findr   r   )r.   r  	overwritemove_cursor
fire_eventotextocposoverwritten_texts           r'   rg  Buffer.insert_text  s     		$$   %53t9+<=''#34P5E5J5J45P#Q fu,uSAQ=R5R5S/TTDIfu,uV}<DI  CI-  $$& r&   c                 $   U R                   (       a  U R                   R                  5       u  pXR                  :w  a@  U R                  R	                  U R                  U R
                  45        [        XS9U l        g U R                   (       a  M~  g g )Nrr   )r   poprq   r   rT   rr   r
   rR   r.   rq   r:  s      r'   undoBuffer.undo*  so    
 ((,,.IDyy   ''D4H4H(IJ !) C r&   c                     U R                   (       a:  U R                  SS9  U R                   R                  5       u  p[        XS9U l        g g )NF)r   r  )r   r   r  r
   rR   r  s      r'   redoBuffer.redo:  sE    ##U#; ((,,.ID$T?DM r&   c                    U R                   [        R                  :w  a  U R                   [        R                  :H  $ U R                  (       a&   U R                  R                  U R                  5        [        R                  U l         SU l        g! [         a_  nUR                  n[        [        SU5      [        U R                  5      5      U l        [        R                  U l         Xl         SnAgSnAff = f)z
Returns `True` if valid.
r   NFT)r   rW   r[   rY   r   rE   rR   r   rr   rA  r   rh   rq   rZ   r   )r.   err   s      r'   rE   Buffer.validateC  s       O$;$;;((O,A,AAA >>	''6 !0 5 5 $ # "#"3"3'*3q/+BC		N'S$(7(?(?%()%s   %B 
C9AC44C9c                    U R                  5       (       d  gU R                  (       a^  [        U R                  5      (       a  U R                  S   U R                  :w  a&  U R                  R	                  U R                  5        ggg)zA
Append the current input to the history.
(Only if valid input.)
Nr   )rE   rq   rh   r   rT   rA   s    r'   rF   Buffer.append_to_history]  s[     }} 99c$,,//4<<3Ctyy3PLL		* 4Q9r&   c                 h  ^ ^^	^
^ [        U[        5      (       d   e[        U[        5      (       a  US:  d   eUR                  mUR                  m	UR                  5       m
U	U
UU U4S jnT R                  nT R                  n[        U5       H  nU" XV5      nUc    gUu  pVM     XVR                  4$ )z
Execute search. Return (working_index, cursor_position) tuple when this
search is applied. Returns `None` when this text cannot be found.
r   c                 j  > T[         R                  :X  a  UR                  TTTS9nUb%  U [        UR                  UR
                  U-   5      4$ [        U S-   [        TR                  5      S-   5       Hb  nU[        TR                  5      -  n[        TR                  U   S5      nUR                  TSTS9nUc  MJ  U[        UR                  U5      4s  $    gUR                  TTS9nUb%  U [        UR                  UR
                  U-   5      4$ [        U S-
  SS5       H  nU[        TR                  5      -  n[        TR                  U   [        TR                  U   5      5      nUR                  TTS9nUc  M_  U[        UR                  [        UR                  5      U-   5      4s  $    g)	z@
Do search one time.
Return (working_index, document) or `None`
)include_current_positionignore_caseNr   r   T)r  r   )
r   FORWARDr  r
   rq   rr   r   rh   r   find_backwards)	r   rR   	new_indexr^  	directionr  r  r.   rq   s	       r'   search_once#Buffer._search.<locals>.search_oncew  s   
 6>>>$MM2J* * ,	 ()$X]]H4L4Ly4XY[ [ #=1#4c$:M:M6NQR6RSS!4!455#+D,?,?,BA#F$,MM$QU>I %2 %K	$0$%xy'I#JJ T %33k 4 3	 ()$X]]H4L4Ly4XY[ [ #=1#4b"=S!4!455#+D,?,?,BCH[H[\]H^D_#`$,$;$; k %< %;	$0$%xs8==?QT]?]'^#__ >r&   N)
r   r   r   rq   r  r  r   rR   r   rr   )r.   search_stater  r   r  r   rR   _resultr  r  rq   s   ` `      @@@r'   _searchBuffer._searchk  s    
 ,4444%%%%!)33   **	"..0+	` +	`\ **==uA 9F~*0'x  7788r&   c                     U R                  USS9nUc  U R                  $ Uu  p4X0R                  :X  a  U R                  nOSn[	        U R
                  U   XES9$ )z
Return a :class:`~prompt_toolkit.document.Document` instance that has
the text/cursor position for this search, if we would apply it. This
will be used in the
:class:`~prompt_toolkit.layout.controls.BufferControl` to display
feedback while searching.
T)r  N)	selection)r  rR   r   r   r
   r   )r.   r  search_resultr   rr   r  s         r'   document_for_searchBuffer.document_for_search  sk     \DQ == -:*M  2 22 00	 	D//>+B Br&   c                 L    U R                  XUS9nUc  U R                  $ Uu  pVU$ )z
Get the cursor position for this search.
(This operation won't change the `working_index`. It's won't go through
the history. Vi text objects can't span multiple items.)
r  r   )r  rr   r.   r  r  r   r  r   rr   s          r'   get_search_positionBuffer.get_search_position  s@     SX % Z  '''-:*M""r&   c                 L    U R                  XUS9nUb  Uu  pVXPl        X`l        gg)zQ
Apply search. If something is found, set `working_index` and
`cursor_position`.
r  N)r  r   rr   r  s          r'   apply_searchBuffer.apply_search  s?    
 SX % Z $-:*M!.#2  %r&   c                     S U l         g r+   )r   rA   s    r'   exit_selectionBuffer.exit_selection  s
    #r&   c                 n  ^ ^ T R                  5       (       a
  [        5       e[        R                  " T R                  5      u  nm[
        R                  " UT R                  R                  S5      5        [
        R                  " U5        UR                  UU 4S j5      nU(       ag  [        TS5       nUR                  5       R                  S5      nUR                  S5      (       a  USS n[        U[!        U5      S9T l        SSS5        [
        R$                  " T5        g! , (       d  f       N%= f)zi
Open code in editor.

:param cli: :class:`~prompt_toolkit.interface.CommandLineInterface`
    instance.
zutf-8c                  &   > TR                  T 5      $ r+   )_open_file_in_editor)filenamer.   s   r'   r6   'Buffer.open_in_editor.<locals>.<lambda>  s    T-F-Fx-Pr&   rbr   Nr   r0  )r   r   tempfilemkstempr   oswriterq   encodecloser9   openreaddecodeendswithr
   rh   rR   remove)r.   r5   
descriptorsuccesfrq   r  s   `     @r'   open_in_editorBuffer.open_in_editor  s     >>$&&  (//0D0DE
H
TYY--g67
 $$%PQ h%vvxw/ ==&&9D ($'I!/ & 			( &%s   4AD&&
D4c                    [         R                  R                  S5      n[         R                  R                  S5      nUUSSSSS/nU H)  nU(       d  M   [        R                  " XQ/5      nUS:H  s  $    g	! [
         a     M;  f = f)
zK
Call editor executable.

Return True when we received a zero return code.
VISUALEDITORz/usr/bin/editorz/usr/bin/nanoz/usr/bin/picoz/usr/bin/viz/usr/bin/emacsr   F)r  environget
subprocesscallOSError)r.   r  visualeditoreditorsr  
returncodes          r'   r  Buffer._open_file_in_editor  s     ))  

 Aq!+!!?J%?*	  	  s   A88
BB)"__cursor_position__working_indexr   r   r   r   r   r   r   r   r   rr   rR   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rq   r   r   r   r   r   r   )NFrI   )Tr   )r   F)r)  )TFr+   )FTT)Fr   )Tr   )Ir    r!   r"   r#   r$   r   IGNOREr/   rN   r   r   rK   rq   setterrr   r   r   r   rR   r   r   r   r   r   r  r  r  r  r  r  r#  r&  r-  r6  r;  r>  r  r  rI  rR  ra  rB  ri  rl  ro  r  r  r  r  r   
CHARACTERSr  r  r  r   r  r  r  r  r  rg  r  r  rE   rF   r  r  r  r  r  r  r  r%   r   r&   r'   r   r      s%    B "&D$#5',t+22e!%dW[36j8<x(* 7 7 
[[, ,* & & 
, 
, $ $ ! !
$/$ C C __! !,:"  8"TU00S S &
FX(
D2)&)$''><@$"*,MM02*0(d4 .;-E-E T	. 5>OO1 7#"
!'< @4+C9JB0#3$$L!r&   r   c                    ^ U R                   R                  n[        X5      nU R                  UU4S j5      n[	        U[	        U5      R                  US5      5      U l         U =R                  U R                   R                  SS9-  sl        g)z+
Indent text of a :class:`.Buffer` object.
c                    > ST-  U -   $ Nz    r   )r5  r   s    r'   r6   indent.<locals>.<lambda>9  s    FUNQ<Nr&   r   Tafter_whitespaceNrR   r  r   r   r
   translate_row_col_to_indexrr   r   )r4   from_rowto_rowr   current_row
line_rangerz   s      `   r'   r   r   1  s~     //55Kx(J %%j2NOH55k1EGFO
 fooHHZ^H__r&   c                 "  ^ U R                   R                  n[        X5      nU4S jnU R                  XV5      n[	        U[	        U5      R                  US5      5      U l         U =R                  U R                   R                  SS9-  sl        g)z-
Unindent text of a :class:`.Buffer` object.
c                 v   > ST-  nU R                  U5      (       a  U [        U5      S  $ U R                  5       $ r  )rZ  rh   r+  )rq   r  r   s     r'   	transformunindent.<locals>.transformI  s8    %??6""F%%;;= r&   r   Tr
  Nr  )r4   r  r  r   r  r  r  rz   s      `    r'   r   r   B  s     //55Kx(J! %%j<H55k1EGFO
 fooHHZ^H__r&   c           	      (   U R                   R                  S5      nUSU nX2S-   S nX1US-    nU(       Ga\  [        R                  " SUS   5      R	                  5       nUS   SU R                  SS5      nSR                  U5      R                  5       n	U R                  =(       d    S[        U5      -
  n
U/nSnU	 Hy  nU(       aP  [        U5      U-   S-   U
:  a%  UR                  S5        UR                  U5        SnOUR                  S	5        US-  nUR                  U5        U[        U5      -  nM{     US
   S:w  a  UR                  S5        [        SR                  XK-   U-   5      [        SR                  XK-   5      5      S9U l        gg)zY
Reformat text, taking the width into account.
`to_row` is included.
(Vi 'gq' operator.)
TNr   z^\s*r   r   r   P   r)  r   r0  )rq   r3  researchendreplacer   r   r   rh   rT   r
   rR   )r4   r  r  r   lines_beforelines_afterlines_to_reformatlengthr   r}  widthreshaped_textcurrent_widthr|  s                 r'   r   r   Z  s    KK""4(E(#L
$Kvz27$5a$89==?"1%gv.66tR@ )*002 ""(bCK7Aq6M)A-5!((.!((0$%M!((-!Q&M  #SV#M  $  & #5CD(D EFH; r&   r  )8r$   
__future__r   r   r   	clipboardr   rh  r   r   r	   rR   r
   enumsr   filtersr   r   r   r   r  r   r  r   r   r   utilsr   cacher   
validationr   	six.movesr   r  r  r   r  r  __all__	Exceptionr   objectr   rU   RETURN_DOCUMENTr  rW   r]   compilerx  r   r   r   r   r   r   r&   r'   <module>r0     s    ( % $ < <  - % - % ? ?    '  	 	 
  7 7+'6 +'\4  ,,DE "40 f 6Af 6Ar ::67 )f )uV up#`"`0*Hr&   