
    Mh                       U d Z ddlmZ ddlZddlZddlZddlZddlmZm	Z	m
Z
mZmZmZmZ ddlmZ ddlmZ ddlmZmZmZ d	gZ ej        d
          Z ej        d          Z ej        d          Z ej        d          Z ej        d          Z ej        d          Z ee	edf          ej                               Z!de"d<    G d dee                   Z# G d d          Z$ G d d	          Z%dS )zB
The `Document` that implements all the text operations/querying.
    )annotationsN)CallableDictIterableListNoReturnPatterncast   )ClipboardData)vi_mode)	PasteModeSelectionStateSelectionTypeDocumentz ([a-zA-Z0-9_]+|[^a-zA-Z0-9_\s]+)z!^([a-zA-Z0-9_]+|[^a-zA-Z0-9_\s]+)z&^(([a-zA-Z0-9_]+|[^a-zA-Z0-9_\s]+)\s*)z([^\s]+)z	^([^\s]+)z^([^\s]+\s*)_DocumentCachezdict[str, _DocumentCache]_text_to_document_cachec                  >    e Zd ZdZd	dZeZeZeZeZeZ	eZ
eZeZeZdS )
_ImmutableLineListz
    Some protection for our 'lines' list, which is assumed to be immutable in the cache.
    (Useful for detecting obvious bugs.)
    aobjectkwreturnr   c                     t          d          )Nz$Attempt to modify an immutable list.)NotImplementedError)selfr   r   s      W/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/prompt_toolkit/document.py_errorz_ImmutableLineList._error6   s    !"HIII    N)r   r   r   r   r   r   )__name__
__module____qualname____doc__r   __setitem__appendclearextendinsertpopremovereversesort r   r   r   r   0   s_         
J J J J KFEFF
CFGDDDr   r   c                      e Zd ZddZdS )r   r   Nonec                "    d | _         d | _        d S N)linesline_indexesr   s    r   __init__z_DocumentCache.__init__E   s    04
 /3r   N)r   r/   )r    r!   r"   r5   r-   r   r   r   r   D   s(        3 3 3 3 3 3r   c                     e Zd ZdZdZ	 	 	 dydzdZd{dZd|dZed{d            Z	ed}d            Z
ed~d            Zed{d            Zed{d            Zed{d            Zed{d            Zed{d            Zed{d            Zedd            Zedd             Zedd!            Zed}d"            Zed{d#            Zed{d$            Zddd'Zedd(            Zedd)            Zed}d*            Zed}d+            Zdd.Zdd/Zdd2Zedd3            Z edd4            Z!dd6Z"	 	 	 	 ddd=Z#ddd>Z$	 	 	 ddd?Z%	 dddCZ&	 dddDZ'	 dddEZ(	 	 	 dddHZ)dddIZ*	 dddJZ+	 dddKZ,	 dddLZ-	 dddMZ.	 dddPZ/	 dddQZ0dddRZ1dddSZ2	 dddUZ3	 dddVZ4	 dddZZ5	 ddd\Z6	 ddd]Z7d}d^Z8d}d_Z9dddaZ:d}dbZ;d}dcZ<ddeZ=ddfZ>ddhZ?ddjZ@ddlZAeBjC        d8fddqZDd}drZEdddtZFdddvZGddwZHddxZIdS )r   a  
    This is a immutable class around the text and cursor position, and contains
    methods for querying this data, e.g. to give the text before the cursor.

    This class is usually instantiated by a :class:`~prompt_toolkit.buffer.Buffer`
    object, and accessed as the `document` property of that class.

    :param text: string
    :param cursor_position: int
    :param selection: :class:`.SelectionState`
    )_text_cursor_position
_selection_cache Ntextstrcursor_position
int | None	selectionSelectionState | Noner   r/   c           	     p   |=|t          |          k    s*J t          d|dt          |                                |t          |          }|| _        || _        || _        	 t
          | j                 | _        d S # t          $ r+ t                      | _        | j        t
          | j        <   Y d S w xY w)Nzcursor_position=z, len_text=)
lenAssertionErrorr7   r8   r9   r   r<   r:   KeyErrorr   )r   r<   r>   r@   s       r   r5   zDocument.__init__\   s     &/SYY*F*F*FJJJSYYJJI
 I
*F*F*F "!$iiO
 
 /#	=1$)<DKKK 	= 	= 	=(**DK15#DI....	=s   'B   1B54B5c                @    | j         j         d| j        d| j        dS )N(z, ))	__class__r    r<   r>   r4   s    r   __repr__zDocument.__repr__   s+    .)TTDITT4;OTTTTr   otherr   boolc                    t          |t                    sdS | j        |j        k    o| j        |j        k    o| j        |j        k    S )NF)
isinstancer   r<   r>   r@   )r   rK   s     r   __eq__zDocument.__eq__   sP    %** 	5 I# 2$(==2%/1	
r   c                    | j         S )zThe document text.)r7   r4   s    r   r<   zDocument.text   s     zr   intc                    | j         S )zThe document cursor position.)r8   r4   s    r   r>   zDocument.cursor_position   s     $$r   c                    | j         S )z :class:`.SelectionState` object.)r9   r4   s    r   r@   zDocument.selection   s     r   c                0    |                      d          pdS )z1Return character under cursor or an empty string.r   r;   _get_char_relative_to_cursorr4   s    r   current_charzDocument.current_char   s     00339r9r   c                0    |                      d          pdS )z6Return character before the cursor or an empty string.r;   rU   r4   s    r   char_before_cursorzDocument.char_before_cursor   s     0044::r   c                *    | j         d | j                 S r1   r<   r>   r4   s    r   text_before_cursorzDocument.text_before_cursor   s    y14/122r   c                *    | j         | j        d          S r1   r\   r4   s    r   text_after_cursorzDocument.text_after_cursor   s    y-//00r   c                B    | j                             d          \  }}}|S )z1Text from the start of the line until the cursor.
)r]   
rpartition)r   _r<   s      r   current_line_before_cursorz#Document.current_line_before_cursor   s%     ,77==
1dr   c                B    | j                             d          \  }}}|S )z/Text from the cursor until the end of the line.ra   )r_   	partition)r   r<   rc   s      r   current_line_after_cursorz"Document.current_line_after_cursor   s%     +55d;;
ar   	list[str]c                    | j         j        1t          | j                            d                    | j         _        | j         j        S )z)
        Array of all the lines.
        Nra   )r:   r2   r   r<   splitr4   s    r   r2   zDocument.lines   s:     ;$ 249??43H3H I IDK{  r   	list[int]c                   | j         j        qt          t          | j                  }dg}|j        }d}|D ]}||dz   z  } ||           t          |          dk    r|                                 || j         _        | j         j        S )zG
        Array pointing to the start indexes of all the lines.
        Nr   r   )r:   r3   maprC   r2   r%   r)   )r   line_lengthsindexesr%   posline_lengths         r   _line_start_indexeszDocument._line_start_indexes   s     ;#+sDJ//L cG^FC+  {Q&s 7||a'.DK${''r   c                *    | j         | j        d         S )zY
        Array of the lines starting from the current line, until the last line.
        N)r2   cursor_position_rowr4   s    r   lines_from_currentzDocument.lines_from_current   s    
 z$24455r   c                *    t          | j                  S )zReturn the number of lines in this document. If the document ends
        with a trailing \n, that counts as the beginning of a new line.)rC   r2   r4   s    r   
line_countzDocument.line_count   s     4:r   c                     | j         | j        z   S )zuReturn the text on the line where the cursor is. (when the input
        consists of just one line, it equals `text`.)rd   rg   r4   s    r   current_linezDocument.current_line   s     .1OOOr   c                    | j         }t          |          t          |                                          z
  }|d|         S )z>The leading whitespace in the left margin of the current line.N)ry   rC   lstrip)r   ry   lengths      r   "leading_whitespace_in_current_linez+Document.leading_whitespace_in_current_line   sA     (\""S)<)<)>)>%?%??GVG$$r   r   offsetc                P    	 | j         | j        |z            S # t          $ r Y dS w xY w)zO
        Return character relative to cursor position, or empty string
        r;   )r<   r>   
IndexError)r   r~   s     r   rV   z%Document._get_char_relative_to_cursor   s>    	9T1F:;; 	 	 	22	s    
%%c                    | j         dk    S )z5
        True when we are at the first line.
        r   )rt   r4   s    r   on_first_linezDocument.on_first_line  s    
 '1,,r   c                (    | j         | j        dz
  k    S )z4
        True when we are at the last line.
        r   )rt   rw   r4   s    r   on_last_linezDocument.on_last_line  s    
 '4?Q+>>>r   c                @    |                      | j                  \  }}|S )z)
        Current row. (0-based.)
        _find_line_start_indexr>   )r   rowrc   s      r   rt   zDocument.cursor_position_row  s#    
 ,,T-ABBQ
r   c                P    |                      | j                  \  }}| j        |z
  S )z,
        Current column. (0-based.)
        r   )r   rc   line_start_indexs      r   cursor_position_colzDocument.cursor_position_col  s/     #99$:NOO#&666r   indextuple[int, int]c                T    | j         }t          j        ||          dz
  }|||         fS )z
        For the index of a character at a certain line, calculate the index of
        the first character on that line.

        Return (row, index) tuple.
        r   )rr   bisectbisect_right)r   r   ro   rp   s       r   r   zDocument._find_line_start_index(  s2     *!'511A5GCL  r   c                D    |                      |          \  }}||z
  }||fS )z
        Given an index for the text, return the corresponding (row, col) tuple.
        (0-based. Returns (0, 0) for index=0.)
        )r   )r   r   r   	row_indexcols        r   translate_index_to_positionz$Document.translate_index_to_position4  s.     44U;;YiCxr   r   r   c           
        	 | j         |         }| j        |         }nK# t          $ r> |dk     r| j         d         }| j        d         }n| j         d         }| j        d         }Y nw xY w|t          dt	          |t          |                              z  }t          dt	          |t          | j                                      }|S )z
        Given a (row, col) tuple, return the corresponding index.
        (Row and col params are 0-based.)

        Negative row/col values are turned into zero.
        r   rY   )rr   r2   r   maxminrC   r<   )r   r   r   resultlines        r   translate_row_col_to_indexz#Document.translate_row_col_to_index?  s    		&-c2F:c?DD 	& 	& 	&Qww1!4z!}1"5z"~	& 	#aS#d)),,--- QFC	NN3344s    AA%$A%c                <    | j         t          | j                  k    S )z/True when the cursor is at the end of the text.)r>   rC   r<   r4   s    r   is_cursor_at_the_endzDocument.is_cursor_at_the_endX  s     #s49~~55r   c                    | j         dv S )z0True when the cursor is at the end of this line.)ra   r;   )rW   r4   s    r   is_cursor_at_the_end_of_linez%Document.is_cursor_at_the_end_of_line]  s      J..r   subc                T    | j                             || j                  | j        k    S )zM
        `True` when this substring is found at the cursor position.
        )r<   findr>   )r   r   s     r   has_match_at_current_positionz&Document.has_match_at_current_positionb  s%     y~~c4#788D<PPPr   Fr   in_current_lineinclude_current_positionignore_casecountc                   t          |t                    sJ |r| j        }n| j        }|st	          |          dk    rdS |dd         }|rt
          j        nd}t          j        t          j        |          ||          }	 t          |          D ]A\  }	}
|	dz   |k    r3|r|

                    d          c S |

                    d          dz   c S Bn# t          $ r Y nw xY wdS )z
        Find `text` after the cursor, return position relative to the cursor
        position. Return `None` if nothing was found.

        :param count: Find the n-th occurrence.
        r   Nr   )rN   rL   rg   r_   rC   re
IGNORECASEfinditerescape	enumeratestartStopIteration)r   r   r   r   r   r   r<   flagsiteratorimatchs              r   r   zDocument.findh  s    +t,,,,, 	*1DD)D' 	 4yyA~~tABBx!,3!;ry~~tU;;	%h// 2 25q5E>>/ 2${{1~~---${{1~~1111	 "2  	 	 	D	ts   5C 9C C 
C"!C"c                    |rt           j        nd}d t          j        t          j        |          | j        |          D             S )zu
        Find all occurrences of the substring. Return a list of absolute
        positions in the document.
        r   c                6    g | ]}|                                 S r-   )r   ).0r   s     r   
<listcomp>z%Document.find_all.<locals>.<listcomp>  s     QQQa		QQQr   )r   r   r   r   r<   )r   r   r   r   s       r   find_allzDocument.find_all  s@    
 "-3!QQ2;ry~~ty%#P#PQQQQr   c                ~   |r| j         ddd         }n| j        ddd         }|rt          j        nd}t          j        t          j        |ddd                   ||          }	 t          |          D ]6\  }}	|dz   |k    r(|	                    d           t          |          z
  c S 7n# t          $ r Y nw xY wdS )z
        Find `text` before the cursor, return position relative to the cursor
        position. Return `None` if nothing was found.

        :param count: Find the n-th occurrence.
        NrY   r   r   )
rd   r]   r   r   r   r   r   r   rC   r   )
r   r   r   r   r   before_cursorr   r   r   r   s
             r   find_backwardszDocument.find_backwards  s      	: ;DDbDAMM 3DDbD9M!,3!;ryTTrT33]EJJ	%h// 6 65q5E>>!KKNN?SXX5555 "6  	 	 	D	ts   &AB- +B- -
B:9B:WORDpatternPattern[str] | Nonec                    |                      ||          rdS | j        }|                     ||          pd}|t          |          |z   d         S )z
        Give the word before the cursor.
        If we have whitespace before the cursor this returns an empty string.

        :param pattern: (None or compiled regex). When given, use this regex
            pattern.
        r   r   r;   r   N)_is_word_before_cursor_completer]   find_start_of_previous_wordrC   )r   r   r   r]   r   s        r   get_word_before_cursorzDocument.get_word_before_cursor  sj     //T7/KK 	2!400dG0LLQPQ!#&8"9"9E"A"C"CDDr   c                    |r|                      ||          d u S | j        dk    p | j        dd                                          S )Nr   r;   rY   )r   r]   isspace)r   r   r   s      r   r   z(Document._is_word_before_cursor_complete  sZ      	33w3OOSWWW '2-W1H1M1U1U1W1Wr   c                   |r|rJ | j         ddd         }|r|}n|rt          }nt          }|                    |          }	 t	          |          D ]&\  }}|dz   |k    r|                    d           c S 'n# t          $ r Y nw xY wdS )z
        Return an index relative to the cursor position pointing to the start
        of the previous word. Return `None` if nothing was found.

        :param pattern: (None or compiled regex). When given, use this regex
            pattern.
        NrY   r   r   )r]   _FIND_BIG_WORD_RE_FIND_WORD_REr   r   endr   )	r   r   r   r   r]   regexr   r   r   s	            r   r   z$Document.find_start_of_previous_word  s     %W%%% "4TTrT: 	"EE 	"%EE!E>>"455	%h// ) )5q5E>>!IIaLL=((( ")  	 	 	D	ts   4A: 8A: :
BBinclude_leading_whitespaceinclude_trailing_whitespacec                   | j         ddd         }| j        }dfd} ||                              |          } ||                              |          }sF|rD|rB| j        | j        dz
           }	| j        | j                 }
t
          j        d	z   }|	|v |
|v k    rd}|r|                    d           nd
|r|                    d          nd
fS )a  
        Return the relative boundaries (startpos, endpos) of the current word under the
        cursor. (This is at the current line, because line boundaries obviously
        don't belong to any word.)
        If not on a word, this returns (0,0)
        NrY   include_whitespacerL   r   Pattern[str]c                J    t           t          t          t          d| f         S )N))FF)FT)TF)TT)_FIND_CURRENT_WORD_RE1_FIND_CURRENT_WORD_INCLUDE_TRAILING_WHITESPACE_RE_FIND_CURRENT_BIG_WORD_RE5_FIND_CURRENT_BIG_WORD_INCLUDE_TRAILING_WHITESPACE_RE)r   r   s    r   	get_regexz;Document.find_boundaries_of_current_word.<locals>.get_regex  s.     5P8S	 
 '(* *r   r   0123456789_r   )r   rL   r   r   )rd   rg   searchr<   r>   stringascii_lettersr   )r   r   r   r   r]   r_   r   match_beforematch_afterc1c2alphabets    `          r   find_boundaries_of_current_wordz(Document.find_boundaries_of_current_word  s#    "<TTrTB :	* 	* 	* 	* 	* 	* !y!;<<CCDVWWi ;<<CCDUVV  	$ 	$ 	$4/!34B4/0B+m;HhB(N33# %17\a    a"-4KOOA1
 	
r   c                r    |                      |          \  }}| j        | j        |z   | j        |z            S )z
        Return the word, currently below the cursor.
        This returns an empty string when the cursor is on a whitespace region.
        )r   )r   r<   r>   )r   r   r   r   s       r   get_word_under_cursorzDocument.get_word_under_cursor  s@    
 99t9DD
sy-58Ls8RRSSr   c                l   |dk     r|                      | |          S |rt          nt          }|                    | j                  }	 t          |          D ]I\  }}|dk    r|                    d          dk    r|dz  }|dz   |k    r|                    d          c S Jn# t          $ r Y nw xY wdS )z
        Return an index relative to the cursor position pointing to the start
        of the next word. Return `None` if nothing was found.
        r   r   r   r   N)find_previous_word_beginningr   r   r   r_   r   r   r   r   r   r   r   r   r   r   s          r   find_next_word_beginningz!Document.find_next_word_beginning'  s     19944E64MMM%)<!!}>>$"899		%h// * *566ekk!nn11QJEq5E>> ;;q>>))) "*  	 	 	D	ts   
AB$ "B$ $
B10B1c                b   |dk     r|                      | |          S |r| j        }n| j        dd         }|rt          nt          }|                    |          }	 t          |          D ]0\  }}|dz   |k    r"|                    d          }	|r|	c S |	dz   c S 1n# t          $ r Y nw xY wdS )z
        Return an index relative to the cursor position pointing to the end
        of the next word. Return `None` if nothing was found.
        r   r   r   N)find_previous_word_endingr_   r   r   r   r   r   r   )
r   r   r   r   r<   r   iterabler   r   values
             r   find_next_word_endingzDocument.find_next_word_ending@  s     19911T1JJJ# 	.)DD)!""-D%)<!!}>>$''	%h// ) )5q5E>>!IIaLLE/ )$$qy((( ")  	 	 	D	ts   7B B B 
B,+B,c                8   |dk     r|                      | |          S |rt          nt          }|                    | j        ddd                   }	 t          |          D ]&\  }}|dz   |k    r|                    d           c S 'n# t          $ r Y nw xY wdS )z
        Return an index relative to the cursor position pointing to the start
        of the previous word. Return `None` if nothing was found.
        r   r   NrY   r   )r   r   r   r   r]   r   r   r   r   s          r   r   z%Document.find_previous_word_beginning`  s     19900vD0III%)<!!}>>$"9$$B$"?@@	%h// ) )5q5E>>!IIaLL=((( ")  	 	 	D	ts   4B
 B
 

BBc                   |dk     r|                      | |          S | j        dd         | j        ddd         z   }|rt          nt          }|                    |          }	 t          |          D ]M\  }}|dk    r|                    d          dk    r|dz  }|dz   |k    r|                    d           dz   c S Nn# t          $ r Y nw xY wdS )z
        Return an index relative to the cursor position pointing to the end
        of the previous word. Return `None` if nothing was found.
        r   r   Nr   rY   )	r   r_   r]   r   r   r   r   r   r   )r   r   r   r]   r   r   r   r   s           r   r   z"Document.find_previous_word_endingu  s    199--UF-FFF!3BQB7$:QRVRVTVRV:WW%)<!!}>>"455		%h// / /566ekk!nn11QJEq5E>>!KKNN?Q.... "/  	 	 	D	ts   %AC C 
CC
match_funcCallable[[str], bool]c                    d}t          | j        | j        dz   d                   D ]"\  }} ||          r
d|z   }|dz  }|dk    r n#|S )zn
        Look downwards for empty lines.
        Return the line index, relative to the current line.
        Nr   r   r   r2   rt   r   r   r   r   r   r   s         r   find_next_matching_linez Document.find_next_matching_line  sw     $TZ0H10L0N0N%OPP 	 	KE4z$ U
zz  r   c                    d}t          | j        d| j                 ddd                   D ]"\  }} ||          r
d|z
  }|dz  }|dk    r n#|S )zl
        Look upwards for empty lines.
        Return the line index, relative to the current line.
        NrY   r   r   r   r   s         r   find_previous_matching_linez$Document.find_previous_matching_line  s}     $TZ0J$2J0J%KDDbD%QRR 	 	KE4z$ e
zz  r   c                f    |dk     r|                      |           S t          | j        |           S )z4
        Relative position for cursor left.
        r   )get_cursor_right_positionr   r   r   r   s     r   get_cursor_left_positionz!Document.get_cursor_left_position  s9     199115&999D,e4444r   c                ~    |dk     r|                      |           S t          |t          | j                            S )z5
        Relative position for cursor_right.
        r   )r   r   rC   rg   r   s     r   r   z"Document.get_cursor_right_position  s>     19900%8885#d<==>>>r   preferred_columnc                    |dk    sJ || j         n|}|                     t          d| j        |z
            |          | j        z
  S )a  
        Return the relative cursor position (character index) where we would be if the
        user pressed the arrow-up button.

        :param preferred_column: When given, go to this column instead of
                                 staying at the current column.
        r   Nr   )r   r   r   rt   r>   r   r   r   columns       r   get_cursor_up_positionzDocument.get_cursor_up_position  se     zzzz(8(@D$$FV 	
 ++At/%788&  "#	
r   c                t    |dk    sJ || j         n|}|                     | j        |z   |          | j        z
  S )a  
        Return the relative cursor position (character index) where we would be if the
        user pressed the arrow-down button.

        :param preferred_column: When given, go to this column instead of
                                 staying at the current column.
        r   )r   r   rt   r>   r   s       r   get_cursor_down_positionz!Document.get_cursor_down_position  sT     zzzz(8(@D$$FV 	
 ++D,Du,LfUU"#	
r   left_chright_chend_posc                8   | j         |k    rdS |t          | j                  }n"t          t          | j                  |          }d}t	          | j        dz   |          D ]8}| j        |         }||k    r|dz  }n||k    r|dz  }|dk    r|| j        z
  c S 9dS )z
        Find the right bracket enclosing current position. Return the relative
        position to the cursor position.

        When `end_pos` is given, don't look past the position.
        r   Nr   )rW   rC   r<   r   ranger>   )r   r  r  r  stackr   cs          r   find_enclosing_bracket_rightz%Document.find_enclosing_bracket_right  s     ((1?$)nnGG#di..'22G t+a/99 		0 		0A	!AG||
h
zz4/////  tr   	start_posc                    | j         |k    rdS |d}nt          d|          }d}t          | j        dz
  |dz
  d          D ]8}| j        |         }||k    r|dz  }n||k    r|dz  }|dk    r|| j        z
  c S 9dS )z
        Find the left bracket enclosing current position. Return the relative
        position to the cursor position.

        When `start_pos` is given, don't look past the position.
        r   Nr   rY   )rW   r   r  r>   r<   )r   r  r  r	  r  r   r  s          r   find_enclosing_bracket_leftz$Document.find_enclosing_bracket_left  s     ''1IIAy))I t+a/QCC 		0 		0A	!AH}}
g
zz4/////  tr   c                    dD ]`}|d         }|d         }| j         |k    r|                     |||          pdc S | j         |k    r|                     |||          pdc S adS )z
        Return relative cursor position of matching [, (, { or < bracket.

        When `start_pos` or `end_pos` are given. Don't look past the positions.
        )z()z[]z{}z<>r   r   )r  )r	  )rW   r  r  )r   r	  r  pairABs         r   find_matching_bracket_positionz'Document.find_matching_bracket_position4  s     + 	X 	XDQAQA A%%88Aw8OOTSTTTT"a''771	7RRWVWWWW ( qr   c                    | j          S )z0Relative position for the start of the document.)r>   r4   s    r   get_start_of_document_positionz'Document.get_start_of_document_positionH  s    $$$r   c                :    t          | j                  | j        z
  S )z.Relative position for the end of the document.)rC   r<   r>   r4   s    r   get_end_of_document_positionz%Document.get_end_of_document_positionL  s    49~~ 444r   after_whitespacec                    |r@| j         }t          |          t          |                                          z
  | j        z
  S t          | j                   S )z-Relative position for the start of this line.)ry   rC   r{   r   rd   )r   r  ry   s      r   get_start_of_line_positionz#Document.get_start_of_line_positionP  s_     	9,LL!!l))++,,-*+ 78888r   c                *    t          | j                  S )z+Relative position for the end of this line.)rC   rg   r4   s    r   get_end_of_line_positionz!Document.get_end_of_line_position\  s    41222r   c                d    t          | j                                                  | j        z
  dz
  S )zR
        Relative position for the last non blank character of this line.
        r   )rC   ry   rstripr   r4   s    r   'last_non_blank_of_current_line_positionz0Document.last_non_blank_of_current_line_position`  s.     4$++--..1IIAMMr   r   c                ~    t          | j                  }| j        }t          dt	          ||                    }||z
  S )z
        Return the relative cursor position for this column at the current
        line. (It will stay between the boundaries of the line in case of a
        larger number.)
        r   )rC   ry   r   r   r   )r   r   rq   current_columns       r   get_column_cursor_positionz#Document.get_column_cursor_positionf  s@     $+,,1QK0011&&r   c                |    | j         r$t          | j        | j         j        g          \  }}n| j        | j        }}||fS )z
        Return (from, to) tuple of the selection.
        start and end position are included.

        This doesn't take the selection type into account. Use
        `selection_ranges` instead.
        )r@   sortedr>   original_cursor_position)r   from_tos      r   selection_rangezDocument.selection_ranger  sO     > 	C%t~'NO IE22 ,d.B2Ebyr   Iterable[tuple[int, int]]c           	   #    K   | j         rt          | j        | j         j        g          \  }}| j         j        t
          j        k    r|                     |          \  }}|                     |          \  }}t          ||g          \  }}| j        }t                      r|dz  }t          ||dz             D ]Y}t          ||                   }	||	k    r<|                     ||          |                     |t          |	|                    fV  ZdS | j         j        t
          j        k    rt          d| j                            dd|          dz             }| j                            d|          dk    r| j                            d|          }nt          | j                  dz
  }t                      r|dz  }||fV  dS dS )aE  
        Return a list of `(from, to)` tuples for the selection or none if
        nothing was selected. The upper boundary is not included.

        This will yield several (from, to) tuples in case of a BLOCK selection.
        This will return zero ranges, like (8,8) for empty lines in a block
        selection.
        r   r   ra   N)r@   r!  r>   r"  typer   BLOCKr   r2   r   r  rC   r   r   LINESr   r<   rfindr   )
r   r#  r$  	from_linefrom_columnto_line	to_columnr2   lrq   s
             r   selection_rangeszDocument.selection_ranges  s      > '	 %t~'NO IE2 ~"m&999)-)I)I%)P)P&	;%)%E%Eb%I%I")/i0H)I)I&Y
99 #NIy'A+66 	 	A"%eAh--K"k11 ;;A{KK ;; !3{I#>#>    		 	 >&-*===49??4E#B#BQ#FGGEy~~dB//144!Y^^D"55 ^^a/ 99 !GBRiO'	  '	 r   tuple[int, int] | Nonec                @   | j         r| j        |         }|                     |d          }|                     |t          |                    }t	          | j        | j         j        g          \  }}t          ||          }t          ||          }||k    r| j         j	        t          j        k    r|}|}n| j         j	        t          j        k    r|                     |          \  }	}
|                     |          \  }	}t	          |
|g          \  }
}|
t          |          k    rdS |                     ||
          }|                     ||          }|                     |          \  }	}|                     |          \  }	}t                      r|dz  }||fS dS )aB  
        If the selection spans a portion of the given line, return a (from, to) tuple.

        The returned upper boundary is not included in the selection, so
        `(0, 0)` is an empty selection.  `(0, 1)`, is a one character selection.

        Returns None if the selection doesn't cover this line at all.
        r   Nr   )r@   r2   r   rC   r!  r>   r"  r   r   r(  r   r*  r)  r   r   )r   r   r   	row_startrow_endr#  r$  intersection_startintersection_endrc   col1col2r-  r/  s                 r   selection_range_at_linez Document.selection_range_at_line  s    > &	.:c?D77Q??I55c3t99EEG%t~'NO IE2
 "%Y!6!6"7B//!%555>&-*===)2&'.$$^(M,???">>uEEGAt">>rBBGAt!'t!5!5JD$c$ii''#t)-)H)Hd)S)S&'+'F'FsD'Q'Q$!%!A!ABT!U!U;#??@PQQ9 99 #NI"I--tr   tuple[Document, ClipboardData]c                ~   | j         r%g }g }| j        }d}|                                 D ]S\  }}|dk    r|}|                    | j        ||                    |                    | j        ||                    |}T|                    | j        |d                    d                    |          }d                    |          }| j         j        t          j        k    r|	                    d          r
|dd         }t          ||          t          || j         j                  fS | t          d          fS )z
        Return a (:class:`.Document`, :class:`.ClipboardData`) tuple, where the
        document represents the new document when the selection is cut, and the
        clipboard data, represents whatever has to be put on the clipboard.
        r   Nra   r;   rY   r\   )r@   r>   r1  r%   r<   joinr(  r   r*  endswithr   r   )	r   	cut_partsremaining_partsnew_cursor_positionlast_tor#  r$  cut_textremaining_texts	            r   cut_selectionzDocument.cut_selection  sT    > 	+I O"&"6G!2244  	ra<<*/'&&ty'?@@@  58!4555""49WXX#6777yy++HWW_55N ~"m&999h>O>OPT>U>U9#CRC= n>QRRRh(;<< 
 r****r   datar   
paste_moder   c                   |t           j        k    }|t           j        k    }|j        t          j        k    r|r;| j        d| j        dz            |j        |z  z   | j        | j        dz   d         z   }n| j        |j        |z  z   | j	        z   }| j        t          |j                  |z  z   }|r|dz  }n|j        t          j        k    r| j        }|rt| j        d|         |j        g|z  z   | j        |d         z   }	d                    |	          }t          d                    | j        d|                             |z   }n}| j        d|dz            |j        g|z  z   | j        |dz   d         z   }	t          d                    | j        d|dz                                |z   dz   }d                    |	          }n|j        t          j        k    r| j        dd         }	| j        }
| j        |rdndz   }t#          |j                            d                    D ]z\  }}||
z   }|t          |	          k    r|	                    d           |	|                             |          |	|<   |	|         d|         ||z  z   |	|         |d         z   |	|<   {d                    |	          }| j        |rdndz   }t+          ||          S )a  
        Return a new :class:`.Document` instance which contains the result if
        we would paste this data at the current cursor position.

        :param paste_mode: Where to paste. (Before/after/emacs.)
        :param count: When >1, Paste multiple times.
        Nr   ra   r;   r   r\   )r   	VI_BEFOREVI_AFTERr(  r   
CHARACTERSr<   r>   r]   r_   rC   r*  rt   r2   r=  r)  r   r   rj   r%   ljustr   )r   rF  rG  r   beforeafternew_textrA  r0  r2   
start_linestart_columnr   r   r   s                  r   paste_clipboard_datazDocument.paste_clipboard_data  s     y22i009000 	I8 4q 889i%'(i 4q 8 : :;<  +di%.??$BXX  #'"6TY%9O"O )#q(#Y----(A ,
2A2$)u)<<tz!""~M99U++&)"''$*RaR.*A*A&B&BQ&F##
7QU7+tykE.AADJqSTuwwDWW&)"''$*Wq1uW2E*F*F&G&G!&Ka&O#99U++Y----JqqqME1J3F7IqqJL$TY__T%:%:;; 
 
4JCJJ&&LL$$$$U|11,??e%L,/Ul#El<==12 e yy''H"&"6v:L!!1"MX7JKKKKr   c                h    d}| j         ddd         D ]}|r|                                r|dz  } |S )zJ
        Return number of empty lines at the end of the document.
        r   NrY   r   )r2   r   )r   r   r   s      r   empty_line_count_at_the_endz$Document.empty_line_count_at_the_endP  sO     Jttt$ 	 	D 4<<>> 
r   rM  c                    dd}|                      ||          }|r.|rdnd}t          d|                     | 	          |z             S | j         S )zX
        Return the start of the current paragraph. (Relative cursor position.)
        r<   r=   r   rL   c                0    |  p|                                  S r1   r   r<   s    r   r   z/Document.start_of_paragraph.<locals>.match_funcb      8-t||~~-r   r   r   r   r   r   Nr<   r=   r   rL   )r   r   r   r>   )r   r   rM  r   
line_indexadds         r   start_of_paragraphzDocument.start_of_paragraph]  s    
	. 	. 	. 	. 55! 6 
 

  	)$!!1Cq$55ZK5HH3NOOO(((r   rN  c                    dd}|                      ||          }|r-|rdnd}t          d|                     |	          |z
            S t          | j                  S )zV
        Return the end of the current paragraph. (Relative cursor position.)
        r<   r=   r   rL   c                0    |  p|                                  S r1   rW  rX  s    r   r   z-Document.end_of_paragraph.<locals>.match_funct  rY  r   rZ  r   r   r[  Nr\  )r   r   r   rC   r_   )r   r   rN  r   r]  r^  s         r   end_of_paragraphzDocument.end_of_paragrapho  s}    
	. 	. 	. 	. 11Zu1UU
 	/#!!!Cq$77j7IICOPPPt-...r   c                J    t          | j        |z   | j        | j                  S )z
        Create a new document, with this text inserted after the buffer.
        It keeps selection ranges and cursor position in sync.
        r<   r>   r@   )r   r<   r>   r@   )r   r<   s     r   insert_afterzDocument.insert_after  s0    
 T! 0n
 
 
 	
r   c                    | j         }|r+t          |j        t          |          z   |j                  }t          || j        z   | j        t          |          z   |          S )z
        Create a new document, with this text inserted before the buffer.
        It keeps selection ranges and cursor position in sync.
        )r"  r(  rd  )r@   r   r"  rC   r(  r   r<   r>   )r   r<   selection_states      r   insert_beforezDocument.insert_before  sx    
 . 	,)8)Qd))*$)  O 	! 03t99<%
 
 
 	
r   )r;   NN)r<   r=   r>   r?   r@   rA   r   r/   )r   r=   )rK   r   r   rL   )r   rQ   )r   rA   )r   rh   )r   rk   )r   )r~   rQ   r   r=   )r   rL   )r   rQ   r   r   )r   rQ   r   rQ   r   rQ   )r   r=   r   rL   )FFFr   )r   r=   r   rL   r   rL   r   rL   r   rQ   r   r?   )F)r   r=   r   rL   r   rk   )FFr   )
r   r=   r   rL   r   rL   r   rQ   r   r?   )FN)r   rL   r   r   r   r=   )r   rL   r   r   r   rL   )r   FN)r   rQ   r   rL   r   r   r   r?   )FFF)r   rL   r   rL   r   rL   r   r   )r   rL   r   r=   )r   F)r   rQ   r   rL   r   r?   )Fr   F)r   rL   r   rQ   r   rL   r   r?   )r   )r   r   r   rQ   r   r?   )r   rQ   r   rQ   )r   N)r   rQ   r   r?   r   rQ   r1   )r  r=   r  r=   r  r?   r   r?   )r  r=   r  r=   r	  r?   r   r?   )NN)r	  r?   r  r?   r   rQ   )r  rL   r   rQ   )r   rQ   r   rQ   )r   r   )r   r&  )r   rQ   r   r2  )r   r;  )rF  r   rG  r   r   rQ   r   r   )r   rQ   rM  rL   r   rQ   )r   rQ   rN  rL   r   rQ   )r<   r=   r   r   )Jr    r!   r"   r#   	__slots__r5   rJ   rO   propertyr<   r>   r@   rW   rZ   r]   r_   rd   rg   r2   rr   ru   rw   ry   r}   rV   r   r   rt   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r  r  r  r  r  r  r  r%  r1  r:  rE  r   EMACSrR  rT  r_  rb  re  rh  r-   r   r   r   r   M   s       
 
 FI &*+/	= = = = =NU U U U
 
 
 
    X % % % X%    X : : : X: ; ; ; X; 3 3 3 X3 1 1 1 X1    X
    X
 ! ! ! X! ( ( ( X(6 6 6 6 X6    X
 P P P XP
 % % % X%     - - - X- ? ? ? X?    X 7 7 7 X7
! 
! 
! 
!	 	 	 	   2 6 6 6 X6 / / / X/Q Q Q Q !&).!' ' ' ' 'RR R R R R !&!    < BFE E E E E( BF     RV    F +0,1	)
 )
 )
 )
 )
VT T T T T ,1    4 TY    B ,1    , ,1    8 ?@    ( ?@    &5 5 5 5 5? ? ? ? ? >B
 
 
 
 
. >B
 
 
 
 
* BF    D DH    D CG    (% % % %5 5 5 5
9 
9 
9 
9 
93 3 3 3N N N N
' 
' 
' 
'   *0  0  0  0 d0 0 0 0d"+ "+ "+ "+N !*	?L ?L ?L ?L ?LB   ) ) ) ) )$/ / / / /$	
 	
 	
 	

 
 
 
 
 
r   )&r#   
__future__r   r   r   r   weakreftypingr   r   r   r   r   r	   r
   	clipboardr   filtersr   r@   r   r   r   __all__compiler   r   r   r   r   r   r=   WeakValueDictionaryr   __annotations__r   r   r   r-   r   r   <module>ru     s     # " " " " "  				   J J J J J J J J J J J J J J J J J J $ $ $ $ $ $       ? ? ? ? ? ? ? ? ? ?  
>??"
#GHH 4>BJ-5 5 1 BJ{++ &BJ|44 8B
?8S8S 5 6:T	G!!6 6         c   (3 3 3 3 3 3 3 3Q
 Q
 Q
 Q
 Q
 Q
 Q
 Q
 Q
 Q
r   