
    .hh=                        S r SSKrSSKJr  SSKJr  SSKJr   " S S5      r\R                  5         S r
S	 rS
 rS rS rS r\R                   " S5      rS r " S S5      r " S S5      r " S S5      r\S:X  a  SSKJr  \" SSSS9  gg)zFormat all or a selected region (line slice) of text.

Region formatting options: paragraph, comment block, indent, deindent,
comment, uncomment, tabify, and untabify.

File renamed from paragraph.py with functions added from editor.py.
    N)askyesno)
askinteger)idleConfc                   >    \ rS rSrSrS r\S 5       rS rS	S jr	Sr
g)
FormatParagraph   a  Format a paragraph, comment block, or selection to a max width.

Does basic, standard text formatting, and also understands Python
comment blocks. Thus, for editing Python source code, this
extension is really only suitable for reformatting these comment
blocks or triple-quoted strings.

Known problems with comment reformatting:
* If there is a selection marked, and the first line of the
  selection is not complete, the block will probably not be detected
  as comments, and will have the normal "text formatting" rules
  applied.
* If a comment block has leading whitespace that mixes tabs and
  spaces, they will not be considered part of the same block.
* Fancy comments, like this bulleted list, aren't handled :-)
c                     Xl         g Neditwinselfr   s     7/opt/python-3.13.8/usr/lib/python3.13/idlelib/format.py__init__FormatParagraph.__init__           c                 >    [         R                  " SSSSSS9U l        g )N
extensionsr   z	max-widthintH   )typedefault)r   	GetOption	max_width)clss    r   reloadFormatParagraph.reload"   s!     **<9J+6UBPr   c                     S U l         g r
   r   r   s    r   closeFormatParagraph.close'   s	    r   Nc                 x   Uc  U R                   OUnU R                  R                  nU R                  R                  5       u  pEU(       a$  U(       a  UR	                  XE5      n[        U5      nO[        X3R                  S5      5      u  pEpvU(       a  [        XbU5      nO[        Xb5      nUR                  SSS5        X:w  aU  UR                  SU5        UR                  5         UR                  XE5        UR                  XH5        UR                  5         OUR                  SU5        UR!                  S5        g)a  Formats paragraph to a max width specified in idleConf.

If text is selected, format_paragraph_event will start breaking lines
at the max width, starting from the beginning selection.

If no text is selected, format_paragraph_event uses the current
cursor location to determine the paragraph (lines of text surrounded
by blank lines) and formats it.

The length limit parameter is for testing with a known value.
insertsel1.0endbreak)r   r   textget_selection_indicesgetget_comment_headerfind_paragraphindexreformat_commentreformat_paragraph
tag_removemark_setundo_block_startdeleter$   undo_block_stopsee)	r   eventlimitr)   firstlastdatacomment_headernewdatas	            r   format_paragraph_event&FormatParagraph.format_paragraph_event*   s     #(-U||  ll88:T88E(D/5N #4H)=> .E&tNCG(5Gue,?MM(E*!!#KK$KK'  "MM(D)r   r   r
   )__name__
__module____qualname____firstlineno____doc__r   classmethodr   r!   r>   __static_attributes__ r   r   r   r      s,      P P$r   r   c                    [        [        UR                  S5      5      u  p#U R                  SU-  SU-  5      nU R	                  SU-  SS5      (       aZ  [        U5      (       aJ  US-   nU R                  SU-  SU-  5      nU R	                  SU-  SS5      (       a  [        U5      (       a  MJ  Un[        U5      n[        U5      n[        U5      U:X  aR  [        XGS 5      (       d@  US-   nU R                  SU-  SU-  5      n[        U5      U:X  a  [        XGS 5      (       d  M@  SU-  nUS-
  nU R                  SU-  SU-  5      nUS:  ag  [        U5      U:X  aX  [        XGS 5      (       dF  US-
  nU R                  SU-  SU-  5      nUS:  a#  [        U5      U:X  a  [        XGS 5      (       d  MF  SUS-   -  n	XX`R                  X5      4$ )	zReturns the start/stop indices enclosing the paragraph that mark is in.

Also returns the comment format string, if any, and paragraph of text
between the start/stop indices.
.z%d.0z%d.end<r'      Nr   )mapr   splitr+   compareis_all_whiter,   len)
r)   marklinenocollinefirst_linenor<   comment_header_lenr:   r9   s
             r   r-   r-   S   s    c4::c?+KF88FVOX%67D ,,vU
3
3T8J8J!xxF):; ,,vU
3
3T8J8J L'-N^, T
"N
2t$7899!xxF):; T
"N
2t$7899 F?D AF88FVOX%67D
1* &6t$7899!xxF):;	 1* &6t$7899 fQhE(===r   c                    U R                  S5      nSn[        U5      nX4:  a0  [        X#   5      (       a  US-   nX4:  a  [        X#   5      (       a  M  X4:  a  U $ [        X#   5      nUS-   U:  a&  [        X#S-      5      (       d  [        X#S-      5      nOUnUSU nUnX4:  a  [        X#   5      (       d  [        R                   " SX#   5      n	[        S[        U	5      S5       H  n
X   nU(       d  M  [        X-   R                  5       5      U:  a&  X:w  a!  UR                  UR                  5       5        UnX-   S-   nU
S-   [        U	5      :  d  Mp  XS-      S:w  d  M}  US-   nM     US-   nX4:  a  [        X#   5      (       d  M  UR                  UR                  5       5        UR                  X#S 5        SR                  U5      $ )z3Return data reformatted to specified width (limit).
r   rK   Nz(\s+)    )rM   rP   rO   
get_indentrerange
expandtabsappendrstripextendjoin)r;   r8   linesinindent1indent2newpartialwordsjwords               r   r0   r0   x   s   JJtE	AE
A
%L**aC %L**v"GsQw|EA#J//UQ3Z(
)CG
%UX..58,q#e*a(A8DGN..01E9%

7>>+,!ns*GsSZEA#J#$5!C- ) aC %UX.. JJw~~ JJuRy99S>r   c                 :  ^^ [        T5      mSR                  U4S jU R                  S5       5       5      n [        U[        T5      -
  S5      n[	        X5      nUR                  S5      nSnUS   (       d  SnUSS nSR                  U4S jU 5       5      U-   $ )z?Return data reformatted to specified width with comment header.rX   c              3   ,   >#    U  H	  oTS  v   M     g 7fr
   rG   ).0rT   lcs     r   	<genexpr>#reformat_comment.<locals>.<genexpr>   s     <+;4"#Y+;s       Nc              3   .   >#    U  H
  nTU-   v   M     g 7fr
   rG   )ro   rT   r<   s     r   rq   rr      s     =WT^D(Ws   )rP   rb   rM   maxr0   )r;   r8   r<   format_widthr=   block_suffixrp   s     `   @r   r/   r/      s     
^	B99<4::d+;<<D us>22B7L 4GmmD!G
 L2;#2,99=W==LLr   c                 4    [         R                  " SU 5      SL$ )z/Return True if line is empty or all whitespace.z^\s*$N)r\   matchrT   s    r   rO   rO      s     88Hd#4//r   c                 L    [         R                  " SU 5      R                  5       $ )z/Return the initial space or tab indent of line.z	^([ \t]*)r\   r{   groupr|   s    r   r[   r[      s    88L$'--//r   c                 Z    [         R                  " SU 5      nUc  gUR                  S5      $ )zReturn string with leading whitespace and '#' from line or ''.

A null return indicates that the line is not a comment line. A non-
null return, such as '    #', will be used to find the other lines of
a comment block with the same  indent.
z^([ \t]*#*)rt   rK   r~   )rT   ms     r   r,   r,      s)     	&Ay771:r   z[ \t]*c                     [         R                  U 5      nUR                  5       [        UR	                  5       R                  U5      5      4$ )zReturn a line's indentation as (# chars, effective # of spaces).

The effective # of spaces is the length after properly "expanding"
the tabs into spaces, as done by str.expandtabs(tabwidth).
)_line_indent_rer{   r'   rP   r   r^   )rT   tabwidthr   s      r   get_line_indentr      s;     	d#A557C	,,X6777r   c                   l    \ rS rSrSrS rS rS rSS jrSS jr	SS	 jr
SS
 jrSS jrSS jrS rSrg)FormatRegion   zFormat selected text (region).c                     Xl         g r
   r   r   s     r   r   FormatRegion.__init__   r   r   c                 d   U R                   R                  nU R                   R                  5       u  p#U(       a0  U(       a)  UR                  US-   5      nUR                  US-   5      nO"UR                  S5      nUR                  S5      nUR	                  XE5      nUR                  S5      nXEXg4$ )aA  Return line information about the selected text region.

If text is selected, the first and last indices will be
for the selection.  If there is no text selected, the
indices will be the current cursor location.

Return a tuple containing (first index, last index,
    string representation of text, list of text lines).
z
 linestartz-1c lineend +1czinsert linestartzinsert lineend +1crX   )r   r)   r*   r.   r+   rM   )r   r)   r9   r:   headtailcharsrc   s           r   
get_regionFormatRegion.get_region   s     ||  ll88:T::el23D::d%667D::01D::23D$D!5''r   c                 r   U R                   R                  nSR                  U5      nXc:X  a  UR                  5         gUR	                  SSS5        UR                  SU5        UR                  5         UR                  X5        UR                  X5        UR                  5         UR                  SUS5        g)a!  Replace the text between the given indices.

Args:
    head: Starting index of text to replace.
    tail: Ending index of text to replace.
    chars: Expected to be string of current text
        between head and tail.
    lines: List of new lines to insert between head
        and tail.
rX   Nr%   r&   r'   r$   )r   r)   rb   bellr1   r2   r3   r4   r$   r5   tag_add)r   r   r   r   rc   r)   newcharss          r   
set_regionFormatRegion.set_region   s     ||  99U#IIKue,h%DD#UD(+r   Nc                 T   U R                  5       u  p#pE[        [        U5      5       Hk  nXV   nU(       d  M  [        XpR                  R
                  5      u  pXR                  R                  -   n	U R                  R                  U	5      XxS -   XV'   Mm     U R                  X#XE5        g)z$Indent region by indentwidth spaces.Nr(   )	r   r]   rP   r   r   r   indentwidth_make_blanksr   
r   r7   r   r   r   rc   posrT   raw	effectives
             r   indent_region_event FormatRegion.indent_region_event  s    #'??#4 EU$C:Dt!0||7L7L!M%(@(@@	!\\66yADJN
 % 	E1r   c                 h   U R                  5       u  p#pE[        [        U5      5       Hu  nXV   nU(       d  M  [        XpR                  R
                  5      u  p[        XR                  R                  -
  S5      n	U R                  R                  U	5      XxS -   XV'   Mw     U R                  X#XE5        g)z$Dedent region by indentwidth spaces.r   Nr(   )
r   r]   rP   r   r   r   rw   r   r   r   r   s
             r   dedent_region_event FormatRegion.dedent_region_event  s    #'??#4 EU$C:Dt!0||7L7L!M	LL,D,D DaH	!\\66yADJN
 % 	E1r   c                     U R                  5       u  p#pE[        [        U5      S-
  5       H  nXV   nSU-   XV'   M     U R                  X#XE5        g)zbComment out each line in region.

## is appended to the beginning of each line to comment it out.
rK   ##r(   r   r]   rP   r   r   r7   r   r   r   rc   r   rT   s           r   comment_region_event!FormatRegion.comment_region_event   sQ    
 $(??#4 EUa(C:DEJ ) 	E1r   c                     U R                  5       u  p#pE[        [        U5      5       H1  nXV   nU(       d  M  USS S:X  a  USS nOUSS S:X  a  USS nXuU'   M3     U R                  X#XE5        g)zUncomment each line in region.

Remove ## or # in the first positions of a line.  If the comment
is not in the beginning position, this command will have no effect.
NrY   r   rK   #r(   r   r   s           r   uncomment_region_event#FormatRegion.uncomment_region_event,  s     $(??#4 EU$C:DBQx4ABxbqSABx#J % 	E1r   c                    U R                  5       u  p#pEU R                  5       nUc  g[        [        U5      5       H<  nXW   nU(       d  M  [	        X5      u  p[        X5      u  pSU-  SU-  -   XS -   XW'   M>     U R                  X#XE5        g)z@Convert leading spaces to tabs for each line in selected region.N	rZ   r(   )r   _asktabwidthr]   rP   r   divmodr   )r   r7   r   r   r   rc   r   r   rT   r   r   ntabsnspacess                r   tabify_region_event FormatRegion.tabify_region_event?  s    #'??#4 E$$&U$C:Dt!0!@!'	!<!E\C'M9DJF
 % 	E1r   c                     U R                  5       u  p#pEU R                  5       nUc  g[        [        U5      5       H  nXW   R	                  U5      XW'   M     U R                  X#XE5        g)z.Expand tabs to spaces for each line in region.Nr(   )r   r   r]   rP   r^   r   )r   r7   r   r   r   rc   r   r   s           r   untabify_region_event"FormatRegion.untabify_region_eventN  sb    #'??#4 E$$&U$C..x8EJ %E1r   c           	      n    [        SSU R                  R                  U R                  R                  SSS9$ )zReturn value for tab width.z	Tab widthzColumns per tab? (2-16)rY      parentinitialvalueminvaluemaxvalue)r   r   r)   r   r    s    r   r   FormatRegion._asktabwidthY  s5    %<<$$11 	r   r   r
   )r@   rA   rB   rC   rD   r   r   r   r   r   r   r   r   r   r   rF   rG   r   r   r   r      s7    $(,,0


&	r   r   c                   *    \ rS rSrSrS rS rS rSrg)Indentsid  zChange future indents.c                     Xl         g r
   r   r   s     r   r   Indents.__init__g  r   r   c                     U R                   nUR                  n[        SSSU   -   S-   SU   -   S-   S-   UR                  S9(       a  U(       + Ul        S	Ul        g
)NzToggle tabsz
Turn tabs )onoffz?
Indent width )zwill bez
remains atz 8.z!
 Note: a tab is always 8 columns)r      r(   )r   usetabsr   r)   r   )r   r7   r   r   s       r   toggle_tabs_eventIndents.toggle_tabs_eventj  st    ,,//]733 !'01389 33 \\# #*kGO #$Gr   c           	          U R                   n[        SSUR                  UR                  SSS9nU(       a&  X2R                  :w  a  UR                  (       d  X2l        g)NzIndent widthz6New indent width (2-16)
(Always use 8 when using tabs)rY   r   r   r(   )r   r   r)   r   r   )r   r7   r   rh   s       r   change_indentwidth_event Indents.change_indentwidth_eventz  sP    ,, K &22 3---goo"%r   r   N)	r@   rA   rB   rC   rD   r   r   r   rF   rG   r   r   r   r   d  s     r   r   c                   $    \ rS rSrS rSS jrSrg)Rstripi  c                     Xl         g r
   r   r   s     r   r   Rstrip.__init__  r   r   Nc                    U R                   R                  nU R                   R                  nUR                  5         [	        [        UR                  S5      5      5      n[        SU5       H_  nUR                  SU-  SU-  5      n[        U5      n[        UR                  5       5      nX:  d  MF  UR                  SXX4-  SU-  5        Ma     UR                  S5      S:X  a  [        U R                   S5      (       dg  UR                  S	5      S
:  aR  UR                  S5      S:X  a=  UR                  S5        UR                  S	5      S
:  a  UR                  S5      S:X  a  M=  UR                  5         g )Nr'   rK   z%i.0z%i.endz%i.%izend-2crX   interpzend-1cr&   zend-3c)r   r)   undor3   r   floatr.   r]   r+   rP   r`   r4   hasattrr5   )	r   r7   r)   r   end_linecurtxtr   cuts	            r   	do_rstripRstrip.do_rstrip  s*   ||  ||  uTZZ./0H%C((6C<C8Cc(Ccjjl#C yGsj0(S.A & HHX$&DLL(33::h'%/xx)T1H% ::h'%/xx)T1 	r   r   r
   )r@   rA   rB   rC   r   r   rF   rG   r   r   r   r     s    r   r   __main__)mainzidlelib.idle_test.test_formatrY   F)	verbosityexit)rD   r\   tkinter.messageboxr   tkinter.simpledialogr   idlelib.configr   r   r   r-   r0   r/   rO   r[   r,   compiler   r   r   r   r   r@   unittestr   rG   r   r   <module>r      s    
 ' + #@ @F    ">J"HM,0
0	 **Y'8M M`! !H @ z	(AEB r   