
    6h&                   
   S SK Jr  S SKrS SKrS SKrSSKJr  SSKJrJ	r	J
r
JrJrJrJr  SSKJr  \R$                  r\" S5      r\" S5      r\" S	5      r S SKrS SKr\R                     " S
 S\
R4                  5      rS r " S S5      r " S S\\5      r " S S\5      r " S S\5      r  " S S\5      r!S"S jr"S"S jr#S r$S"S jr%\" S5      \" S5      \" S5      \" S5      S.r& " S  S!5      r'g! \\4 a    Sr Nf = f)#    )annotationsN   )_)
diffhelperencodingerrorpatchpycompatscmutilutil)
stringutils   # To remove '-' lines, make them ' ' lines (context).
# To remove '+' lines, delete them.
# Lines starting with # will be removed from the patch.
s,  #
# If the patch applies cleanly, the edited hunk will immediately be
# added to the record list. If it does not apply cleanly, a rejects file
# will be generated. You can use that when you try again. If all lines
# of the hunk are removed, then the edit is aborted and the hunk is left
# unchanged.
s   #
# If the patch applies cleanly, the edited patch will immediately
# be finalised. If it does not apply cleanly, rejects files will be
# generated. You can use those when you try again.
c                      \ rS rSrSrSrg)fallbackerrorB   zDError that indicates the client should try to fallback to text mode. N)__name__
__module____qualname____firstlineno____doc____static_attributes__r       3/usr/lib/python3/dist-packages/mercurial/crecord.pyr   r   B   s    Nr   r   c                D    [         =(       a    U R                  S5      S:H  $ )zReturn True if the user wants to use curses

This method returns True if curses is found (and that python is built with
it) and that the user has the correct flag for the ui.
s   chunkselectors   curses)curses	interface)uis    r   checkcursesr   I   s     Abll#34	AAr   c                  `    \ rS rSrSr\S 5       rS rS rS r	S r
S rS	 rSS
 jrSS jrSrg)	patchnodeR   zNabstract class for patch graph nodes
(i.e. patchroot, header, hunk, hunkline)
c                    gNr   r   selfs    r   contentpatchnode.contentW   s    r   c                    [        S5      eN&   method must be implemented by subclassNotImplementedErrorr$   s    r   
firstchildpatchnode.firstchild[       !"KLLr   c                    [        S5      er)   r+   r$   s    r   	lastchildpatchnode.lastchild^   r/   r   c                    [        S5      e)z8Return a list of all of the direct children of this noder*   r+   r$   s    r   allchildrenpatchnode.allchildrena   s    !"KLLr   c                    [        S5      e)z
Return the closest next item of the same type where there are no items
of different types between the current item and this closest item.
If no such item exists, return None.
r*   r+   r$   s    r   nextsiblingpatchnode.nextsiblinge        ""KLLr   c                    [        S5      e)z
Return the closest previous item of the same type where there are no
items of different types between the current item and this closest item.
If no such item exists, return None.
r*   r+   r$   s    r   prevsiblingpatchnode.prevsiblingm   r9   r   c                    [        S5      er)   r+   r$   s    r   
parentitempatchnode.parentitemu   r/   r   c                    U R                   nU(       a=  U(       a6  U R                  5       nUc!   U R                  5       R                  5       nU$ U$ U R	                  5       nUb  U$ U R                  5       nUb  U$  U R                  5       R                  5       nUb  U$ U R                  5       R                  5       R                  5       $ ! [         a    Sn Nf = f! [         a    Sn U$ f = f! [         a     gf = f)a@  
Try to return the next item closest to this item, regardless of item's
type (header, hunk, or hunkline).

If skipfolded == True, and the current item is folded, then the child
items that are hidden due to folding will be skipped when determining
the next item.

If it is not possible to get the next item, return None.
FN)foldedAttributeErrorr7   r>   r-   )r%   
skipfolded
itemfoldednextitemitems        r   rE   patchnode.nextitemx   s   	J *'')H$#0<<>H O8O ??$D ##%D
(446#K (335AACC9  	J	 & $#HO$. " s:   C C ="C1  +C1 CCC.-C.1
C>=C>Nc                    U R                  5       nUbN  UR                  5       nUb9  UR                  (       d(  UR                  5       nUb  UR                  (       d  U$ U$ U$ U R                  5       $ )z
Try to return the previous item closest to this item, regardless of
item's type (header, hunk, or hunkline).

If it is not possible to get the previous item, return None.
)r;   r1   rA   r>   )r%   rC   r;   prevsiblinglastchildprevsiblinglclcs        r   previtempatchnode.previtem   ss     &&("#.#8#8#: $0+:L:L"6"@"@"B#/.55**//""   r   r   TN)r   r   r   r   r   propertyr&   r-   r1   r4   r7   r;   r>   rE   rK   r   r   r   r   r    r    R   sJ      MMMMMM,\!r   r    c                      \ rS rSrSrS rSrg)r	      z0
list of header objects representing the patch.
c                D    U R                  U5        U  H	  nXl        M     g rN   )extendr	   )r%   
headerlistheaders      r   __init__patch.__init__   s    JFL r   r   N)r   r   r   r   r   rV   r   r   r   r   r	   r	      s     r   r	   c                  ^    \ rS rSrSrS r\S 5       rS rS r	S r
S rS	 rS
 rS rS rSrg)uiheader   z)
patchnode class wrapping a patch.header
c                    Xl         SU l        SU l        SU l        S U l        SU l        U R                   Vs/ s H  n[        X 5      PM     snU l        g s  snf NTF)nonuiheaderappliedpartialrA   r	   neverunfoldedhunksuihunk)r%   rU   hs      r   rV   uiheader.__init__   sU    !   
 "/3zz:z!fQoz:
:s   Ac                "    U R                  5       $ rN   )filenamer$   s    r   r&   uiheader.content   s    }}r   c                X    [        5       nU R                  U5        UR                  5       $ rN   stringioprettygetvaluer%   xs     r   	prettystruiheader.prettystr        JAzz|r   c                    [        U R                  5      nU R                  R                  U 5      nX!S-
  :  a  U R                  US-      nU$ g Nr   )lenr	   index)r%   numheadersinpatchindexofthisheader
nextheaders       r   r7   uiheader.nextsibling   sM    

O JJ,,T2144$5$9:Jr   c                n    U R                   R                  U 5      nUS:  a  U R                   US-
     nU$ g Nr   r   )r	   ru   )r%   rw   previousheaders      r   r;   uiheader.prevsibling   s<     JJ,,T2q !ZZ(9A(=>N!!r   c                    g)zR
there is no 'real' parent item of a header that can be selected,
so return None.
Nr   r$   s    r   r>   uiheader.parentitem   s    
 r   c                T    [        U R                  5      S:  a  U R                  S   $ gDreturn the first child of this item, if one exists.  otherwise
None.r   Nrt   ra   r$   s    r   r-   uiheader.firstchild  s%     tzz?Q::a= r   c                T    [        U R                  5      S:  a  U R                  S   $ gCreturn the last child of this item, if one exists.  otherwise
None.r   Nr   r$   s    r   r1   uiheader.lastchild  s%     tzz?Q::b>!r   c                    U R                   $ z8return a list of all of the direct children of this node)ra   r$   s    r   r4   uiheader.allchildren  s    zzr   c                .    [        U R                  U5      $ rN   )getattrr]   r%   names     r   __getattr__uiheader.__getattr__  s    t''..r   )r^   rA   ra   r`   r]   r_   r	   N)r   r   r   r   r   rV   rO   r&   ro   r7   r;   r>   r-   r1   r4   r   r   r   r   r   rY   rY      sH    ;$  
/r   rY   c                  R    \ rS rSrSrS r\S 5       rS rS r	S r
S rS	 rS
 rSrg)
uihunklinei  z#represents a changed line in a hunkc                8    Xl         SU l        X l        SU l        g r\   )linetextr^   hunkrA   )r%   r   r   s      r   rV   uihunkline.__init__!  s     	 r   c                    U R                   $ rN   r   r$   s    r   r&   uihunkline.content*  s    }}r   c                    U R                   $ rN   r   r$   s    r   ro   uihunkline.prettystr.  s    }}r   c                    [        U R                  R                  5      nU R                  R                  R                  U 5      nX!S-
  :  a  U R                  R                  US-      nU$ g rs   )rt   r   changedlinesru   )r%   numlinesinhunkindexofthislinenextlines       r   r7   uihunkline.nextsibling1  s\    TYY334))0066t<a//yy--o.ABHOr   c                    U R                   R                  R                  U 5      nUS:  a  U R                   R                  US-
     nU$ g r{   )r   r   ru   )r%   r   previouslines      r   r;   uihunkline.prevsibling;  sF    ))0066t<Q9911/A2EFLr   c                    U R                   $ z%return the parent to the current item)r   r$   s    r   r>   uihunkline.parentitemC  s    yyr   c                    g)r   Nr   r$   s    r   r-   uihunkline.firstchildG       r   c                    g)r   Nr   r$   s    r   r1   uihunkline.lastchildM  r   r   )r^   rA   r   r   N)r   r   r   r   r   rV   rO   r&   ro   r7   r;   r>   r-   r1   r   r   r   r   r   r     s<    -  r   r   c                      \ rS rSrSrSrS r\S 5       rS r	S r
S rS	 rS
 rS rS rS rS r\rS rS rS rS rSrg)rb   iT  z9ui patch hunk, wraps a hunk and keep track of ui behavior   c                    Xl         UR                   Vs/ s H  n[        X05      PM     snU l        X l        U R
                  U l        SU l        SU l        SU l	        g s  snf r\   )
_hunkr   r   r   rU   removedoriginalremovedrA   r^   r_   )r%   r   rU   lines       r   rV   uihunk.__init__Y  sW    
@D		J	Z3	J#||   Ks   Ac                @    U R                   (       a  U R                   $ S$ r#   )procr$   s    r   r&   uihunk.contenth  s     IItyy.3.r   c                    [        U R                  R                  5      nU R                  R                  R                  U 5      nX!S-
  :  a  U R                  R                  US-      nU$ g rs   )rt   rU   ra   ru   )r%   numhunksinheaderindexofthishunknexthunks       r   r7   uihunk.nextsiblingl  s]    t{{001++++11$711{{((1)<=HOr   c                    U R                   R                  R                  U 5      nUS:  a  U R                   R                  US-
     nU$ g r{   )rU   ra   ru   )r%   r   previoushunks      r   r;   uihunk.prevsiblingv  sF    ++++11$7Q;;,,_q-@ALr   c                    U R                   $ r   )rU   r$   s    r   r>   uihunk.parentitem~  s    {{r   c                T    [        U R                  5      S:  a  U R                  S   $ gr   rt   r   r$   s    r   r-   uihunk.firstchild  s+     t  !A%$$Q''r   c                T    [        U R                  5      S:  a  U R                  S   $ gr   r   r$   s    r   r1   uihunk.lastchild  s+     t  !A%$$R((r   c                    U R                   $ r   )r   r$   s    r   r4   uihunk.allchildren  s       r   c                   [        U R                   Vs/ s H>  nUR                  (       d  M  UR                  5       R	                  S5      (       d  M<  UPM@     sn5      n[        U R                   Vs/ s H>  nUR                  (       d  M  UR                  5       R	                  S5      (       d  M<  UPM@     sn5      nX#4$ s  snf s  snf )zchangedlines -> (n+,n-)   +   -)rt   r   r^   ro   
startswith)r%   laddrems       r   countchangesuihunk.countchanges  s     ***A99 !"!9!9$!? *
  ***A99 !"!9!9$!? *
 xs"   C"CC3C"C2Cc                    U R                   U R                  -
  n[        U R                  5      [        U R                  5      -   U-   nU R                  (       a&  U R                  S   [
        R                  :X  a  US-  nX R                  -   nX R                  -   nU R                  U R                  peUS:w  a  US:X  a  US-  nUS:X  a  US:  a  US-  nSUUUUU R                  =(       a    SU R                  -   4-  nU$ )Nr   r   r   s   @@ -%d,%d +%d,%d @@%s
    )r   r   rt   beforeafterr   MISSING_NEWLINE_MARKERaddedfromlinetoliner   )r%   removedconvertedtocontext
contextlenfromlentolenr   r   
fromtolines           r   getfromtolineuihunk.getfromtoline  s    $($8$84<<$G! s4::.1JJ 	 ::$**R.J,M,MM!OJ||+ZZ'  ==$++&q=!|Azfqj!/II,4$))+3
 

 r   c                   U R                  5       u  U l        U l        UR                  U R	                  5       5        / nU R
                   Hf  nUR                  5       nUR                  (       a  UR                  U5        M7  UR                  S5      (       d  MO  UR                  SUSS  -   5        Mh     UR                  SR                  U R                  U-   U R                  -   5      5        g )Nr   r   r   r   )r   r   r   writer   r   ro   r^   appendr   joinr   r   )r%   fphunklinelistchangedlinechangedlinestrs        r   r   uihunk.write  s    #'#4#4#6 
DL
##%&  ,,K(224N""##N3**400##D>!"+=$=> - 	$++4tzzABCr   c                X    [        5       nU R                  U5        UR                  5       $ rN   ri   rm   s     r   ro   uihunk.prettystr  rq   r   c           	     L   / n/ nSnU R                    H  nUR                  nUR                  [        R                  :X  a  SnM1  UR                  (       aZ  UR                  S5      (       a  UR                  USS 5        Mn  UR                  S5      (       a  UR                  USS 5        M  M  UR                  S5      (       d  M  UR                  USS 5        UR                  USS 5        M     U Vs/ s H  nSU-  PM
     snU Vs/ s H  nSU-  PM
     sn-   nU(       a  U(       a  US	   SS	 US	'   U R                  n[        R                  " UR                  UR                  UR                  UR                  UR                  XxR                  5      $ s  snf s  snf )
aS  return a recordhunk which is the reverse of the hunk

        Assuming the displayed patch is diff(A, B) result. The returned hunk is
        intended to be applied to B, instead of A.

        For example, when A is "0
1
2
6
" and B is "0
3
4
5
6
", and
        the user made the following selection:

                 0
            [x] -1           [x]: selected
            [ ] -2           [ ]: not selected
            [x] +3
            [ ] +4
            [x] +5
                 6

        This function returns a hunk like:

                 0
                -3
                -4
                -5
                +1
                +4
                 6

        Note "4" was first deleted then added. That's because "4" exists in B
        side and "-4" must exist between "-3" and "-5" to make the patch
        applicable to B.
        FTr   r   Nr   s   -%ss   +%sr   )r   r   r   r   r^   r   r   r   patchmod
recordhunkrU   r   r   r   r   r   )	r%   delsaddsnoeolr   textr   r   rc   s	            r   reversehunkuihunk.reversehunk  sZ   > %%D==D}}
 A AA||??4((KKQR)__T**KKQR) +&&DH%DH% & %))Dq
D),FAVaZ,FFTBx}DHJJ""HHahh

AFFAHHdGG
 	
 *,Fs   7FF!c                .    [        U R                  U5      $ rN   )r   r   r   s     r   r   uihunk.__getattr__  s    tzz4((r   c                @    SU R                  5       U R                  4-  $ )Nz<hunk %r@%d>)rf   r   r$   s    r   __repr__uihunk.__repr__  s    $-- @@@r   )	r   r   r^   r   rA   rU   r   r_   r   N)r   r   r   r   r   
maxcontextrV   rO   r&   r7   r;   r>   r-   r1   r4   r   r   r   rk   ro   r   r   r   r   r   r   r   rb   rb   T  so    CJ / /!$ DD" F
6
p)Ar   rb   c                   [        U5      nU Vs/ s H&  n[        U[        R                  5      (       d  M$  UPM(     nn[	        U5      S:X  a  / 0 4$ U Vs/ s H  n[        U5      PM     nnU" XUS9n/ n	U H  n
U
R                  (       d  M  U
R                  5       (       d>  [	        U
R                   Vs/ s H  ofR                  (       d  M  UPM     sn5      S:  d  Mi  U	R                  U
5        SnU
R                   H`  nUR                  (       a1  U	R                  U5        U(       a  U=R                  U-  sl
        MC  ME  XR                  UR                  -
  -  nMb     M     X4$ s  snf s  snf s  snf )z:interactively filter patch chunks into applied-only chunksr   )	operation)list
isinstancer   rU   rt   rY   r^   specialra   r   r   r   r   )r   chunkschunkselectorr   cheadersrc   	uiheadersretappliedhunklisthdr	fixoffsethnks                r   filterpatchr  !  s2   &\F !C&QJq(//$Bq&GC 7|q2v&-.g!gI. 
;CO;;;KKMMSSYY!DY))!Y!DEI""3'Iyy;;#**3/ !

i/
 ! syy!88I ! " !!7 D
 / "Es   #E+E+E06E5
E5
c                r   U R                  [        S5      5        [        XU5      n[        5       =pE[	        [
        S5      (       a$  [
        R                  " [
        R                  5      n [        R                  " 5          [        R                  " UR                  5        SSS5        UR                  b  UR                  e XELa%  [
        R
                  " [
        R                  U5        UR                  $ ! , (       d  f       N]= f! XELa&  [
        R
                  " [
        R                  U5        f f = f)z_
curses interface to get selection of chunks, and mark the applied flags
of the chosen chunks.
s   starting interactive selection
SIGTSTPN)r   r   curseschunkselectorobjecthasattrsignal	getsignalr  r   with_lc_ctyper   wrappermaininitexcopts)r   rT   r   r  origsigtstpsentinels         r   r  r  D  s    
 HHQ234'
	BM#X%Kvy!!&&v~~67!NN=--. "  ,''' - &MM&..+6 "! &MM&..+6 's$   ,D
 !C9"!D
 9
DD
 
,D6c                   ^ ^ UU 4S jnU$ )Nc                    > T" T/U Q70 UD6$ rN   r   )argskwargsftestfns     r   utestdecorator.<locals>.u[  s    )$)&))r   r   )r   r  r!  s   `` r   testdecoratorr#  Z  s    * Hr   c                   [        X!U5      n " S S5      nU" 5       Ul        U (       a  [        R                  R	                  U 5      (       av  [        U 5       nUR                  5        Vs/ s H  owR                  S5      PM     nnSSS5         UR                  WR                  S5      SS9(       a   UR                  $ M3  UR                  $ s  snf ! , (       d  f       NS= f)z]
test interface to get selection of chunks, and mark the applied flags
of the chosen chunks.
c                       \ rS rSrS rS rSrg)&testchunkselector.<locals>.dummystdscrih  c                    g rN   r   r$   s    r   clear,testchunkselector.<locals>.dummystdscr.cleari      r   c                    g rN   r   r$   s    r   refresh.testchunkselector.<locals>.dummystdscr.refreshl  r*  r   r   N)r   r   r   r   r(  r,  r   r   r   r   dummystdscrr&  h  s    		r   r.  
NTr   test)r  stdscrospathexistsopen	readlinesrstriphandlekeypressedpopr  )	r   r   rT   r   r  r.  testfrn   testcommandss	            r   testchunkselectorr=  a  s    
 (
	BM  '=M"''..((&\U49OO4EF4EqHHTN4ELF  --l.>.>q.A-M   G \s   C,CCC
C#s   Select hunks to applys   Select hunks to discards   Select hunks to keeps   Select hunks to record)s   applys   discards   keepNc                     \ rS rSrS7S jrS rS rS rS rS8S jr	S	 r
S
 rS rS rS rS7S jrS rS rS rS9S jrS r        S:S j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 rS r  S?S  jr! S@S! jr"S" r# SAS# jr$S$ r%S% r&S& r'S' r(S( r)S) r*S* r+S+ r,S9S, jr-S- r.S. r/S?S/ jr0S?S0 jr1S1 r2S2 r3S8S3 jr4S4 r5S5 r6S6r7g)Br  i  Nc                   [        U5      U l        X l        0 U l        S U l        / U l        U HC  nU R
                  R                  U5        U R
                  R                  UR                  5        ME     0 U l	        0 U l
        [        R                  " U R                  R                  SS5      5      nUSLU l        U R                  S   U l        S U l        SU l        S U l        SU l        SU l        SU l        SU l        S U l        SU l        SU l        SU l        S	U l        U[8        ;  a  [:        R<                  " S
U-  5      eX0l        S U l         g )Ns   uis   colorFr   r      r   r   Ts   unexpected operation: %s)!r	   rT   r   r  errorstr	chunklistr   rS   ra   
colorpairscolorpairnamesr   	parseboolconfigusecolorcurrentselecteditemlastapplieditemselecteditemstartlineselecteditemendlineheaderindentnumcharshunkindentnumcharshunklineindentnumcharsfirstlineofpadtoprintnumpadlinesnumstatuslineslinesprintedtopadsofarcommenttextwaslasttoggleallapplied_headermessagesr   ProgrammingErrorr   regex)r%   rT   r   r   rc   uicolors         r   rV   curseschunkselector.__init__  sL   
+	ANN!!!$NN!!!''*    &&tww~~eX'FGu, $(??1#5 # &'"#'  %&!"#&'# &'"   '(#  (,$ O+((+i7  #
r   c                R    U R                   nUR                  5       nUc  UnX l         g)a  
try to select the previous item to the current item that has the
most-indented level.  for example, if a hunk is selected, try to select
the last hunkline of the hunk prior to the selected hunk.  or, if
the first hunkline of a hunk is currently selected, then select the
hunk itself.
N)rH  rK   r%   currentitemrE   s      r   uparrowevent curseschunkselector.uparrowevent  s0     ..'') #H#+ r   c                    U R                   nUR                  5       nUc  UR                  5       nUc  UnX l         U R                  5         g)z
select (if possible) the previous item on the same level as the
currently selected item.  otherwise, select (if possible) the
parent-item of the currently selected item.
N)rH  r;   r>   recenterdisplayedarear[  s      r   uparrowshiftevent%curseschunkselector.uparrowshiftevent  sP     ..**,"--/H #H#+ ""$r   c                R    U R                   nUR                  5       nUc  UnX l         g)a7  
try to select the next item to the current item that has the
most-indented level.  for example, if a hunk is selected, select
the first hunkline of the selected hunk.  or, if the last hunkline of
a hunk is currently selected, then select the next hunk, if one exists,
or if not, the next header if one exists.
N)rH  rE   r[  s      r   downarrowevent"curseschunkselector.downarrowevent  s.     ..'')"H#+ r   c                    U R                   nUR                  5       nUc   UR                  5       R                  5       nUc  UnX l         U R	                  5         g! [         a    Sn N*f = f)z
select (if possible) the next item on the same level as the currently
selected item.  otherwise, select (if possible) the next item on the
same level as the parent item of the currently selected item.
N)rH  r7   r>   rB   r`  r[  s      r   downarrowshiftevent'curseschunkselector.downarrowshiftevent   st     ..**, &113??A "H#+ ""$ "   s   A A*)A*c                `  ^ U R                   mU4S jnTR                  5       nUb/  U" U5      (       d"  UR                  5       nUb  U" U5      (       d  M"  Uc  TnO5UR                  5       nUb"  UR                  (       a  U R	                  U5        X0l         U(       d  U R                  5         g g )Nc                .   > [        U [        T5      5      $ rN   )r   type)rF   r\  s    r   <lambda>2curseschunkselector.nextsametype.<locals>.<lambda>  s    
4k1B Cr   )rH  rE   r>   rA   togglefoldedr`  )r%   r1  sametyperE   parentr\  s        @r   nextsametype curseschunkselector.nextsametype  s    ..C'')"8H+=+=((*H "8H+=+= "H((*F!fmm!!&)#+ &&( r   c                    U R                   nUR                  5       nUR                  (       a  U R                  U5        Uc  UnX l         g)z<
select (if possible) the first of this item's child-items.
N)rH  r-   rA   rn  r[  s      r   rightarrowevent#curseschunkselector.rightarrowevent)  sG     ..))+ k*"H#+ r   c                    U R                   n[        U[        5      (       d!  UR                  (       d  U R	                  US9  gUR                  5       nUc"  UnUR                  (       d  U R	                  US9  X l         g)z
if the current item can be folded (i.e. it is an unfolded header or
hunk), then fold it.  otherwise try select (if possible) the parent
of this item.
rF   N)rH  r   r   rA   rn  r>   r[  s      r   leftarrowevent"curseschunkselector.leftarrowevent:  st     .. +z22%%!!{!3 ))+"H??!!x!0#+ r   c                    U R                   n[        U[        5      (       a!  UR                  (       d  U R	                  US9  g UR                  5       nUc  OUnM  Xl         g)zg
select the header of the current item (or fold current item if the
current item is already a header).
rw  N)rH  r   rY   rA   rn  r>   r[  s      r   leftarrowshiftevent'curseschunkselector.leftarrowshifteventS  sg    
 ..k8,,%%!!{!3 "--/H&  $/ r   c                    U R                   nU R                  nU R                  nX0R                  -   U R                  -
  S-
  nUS-   nUS-
  nX&:  a  U R                  X&-
  5        gX:  a  U R                  X-
  5        gg)z6scroll the screen to fully show the currently-selectedr   r   N)rJ  rK  rO  yscreensizerQ  scrolllines)r%   selstartselendpadstartpadendpadstartbufferedpadendbuffereds          r   updatescroll curseschunkselector.updatescrolli  s    --))--,,,t/B/BBQF $a<!"V45(X89 )r   c                    U =R                   U-  sl         U R                   S:  a  SU l         U R                   U R                  S-
  :  a  U R                  S-
  U l         gg)z>scroll the screen up (down) by numlines when numlines >0 (<0).r   r   N)rO  rP  )r%   numliness     r   r  curseschunkselector.scrolllines{  sZ    ""h."%%))*D&%%(8(81(<<)-)9)9A)=D& =r   c                    Uc  U R                   nXl        UR                  (       + Ul        [        U[        5      (       a  SUl        UR                  (       a5  UR                   H$  nSUl        UR                   H
  nSUl        M     M&     gUR                   H+  nSUl        SUl        UR                   H
  nSUl        M     M-     g[        U[        5      (       Ga  SUl        UR                   H  nUR                  Ul        M     UR                  R                   Vs/ s H  o"R                  PM     nnSU;  nSU;  nUR                  R                   Vs/ s H  o"R
                  PM     nnSU;   nU(       aC  UR                  R                  5       (       d#  SUR                  l        SUR                  l        ggSUR                  l        U=(       d    U(       + UR                  l        g[        U[        5      (       Ga  UR                  R                   V	s/ s H  oR                  PM     nn	SU;  nSU;  nU(       a#  SUR                  l        SUR                  l        OLU(       a#  SUR                  l        SUR                  l        O"SUR                  l        SUR                  l        UR                  R                  R                   Vs/ s H  o"R                  PM     n
nSU
;  nSU
;  nUR                  R                  R                   Vs/ s H  o"R
                  PM     nnSU;   nU(       aa  UR                  R                  R                  5       (       d7  SUR                  R                  l        SUR                  R                  l        ggSUR                  R                  l        U=(       d    U(       + UR                  R                  l        ggs  snf s  snf s  sn	f s  snf s  snf )zz
toggle the applied flag of the specified item.  if no item is specified,
toggle the flag of the currently selected item.
NFT)rH  rI  r^   r   rY   r_   ra   r   rb   rU   r  r   r   )r%   rF   r  hunklinesiblingappliedstatusallsiblingsappliednosiblingsappliedsiblingspartialstatussomesiblingspartiallnparentsiblingsappliednoparentsiblingsappliedallparentsiblingsappliedparentsiblingspartialsomeparentsiblingspartials                  r   toggleapplycurseschunkselector.toggleapply  s_   
 <++D#' <<'dH%% DL||::C"&CK$'$4$4+/( %5 &  ::C"'CK"'CK$'$4$4+0( %5	 & f%% DL --#'<<  . <@;;;L;L#M;LCKK;L #M&+/C&C%)-A%A<@KK<M<M$N<MS[[<M!$N"&*?"?
 !{{**,,*/DKK'*/DKK' - '+#'A/A+A # j))9=9O9O#P9O2JJ9O #P&+/C&C%)-A%A !$)		!$)		!#$(		!$)		!$(		!$(		! (,yy'7'7'='=%'='= " % ,03H+H#,15J,J$ (,yy'7'7'='=%'='= " % )-0E(E% 'yy''//11/4DII$$,/4DII$$, 2
 ,0		  (-M5M1M 		  (G *) $N %O" $Q%%s   #O'O,!O1O6O;c                0   U R                   (       a8  U R                   H'  nUR                  (       d  M  U R                  U5        M)     O7U R                   H'  nUR                  (       a  M  U R                  U5        M)     U R                   (       + U l         g)z%toggle the applied flag of all items.N)rT  rT   r^   r  )r%   rF   s     r   	toggleallcurseschunkselector.toggleall  sj    ''<<<$$T* ( |||$$T* ( ,0+G+G'G$r   c                    U R                    HB  nUR                  5        H+  nUR                  5        H  nU R                  U5        M     M-     MD     g)zH
Flip all selections. Every selected line is unselected and vice
versa.
N)rT   r4   r  )r%   rU   r   r   s       r   flipselections"curseschunkselector.flipselections  sF    
 ooF**, ,,.D$$T* / - &r   c                B   U R                   (       a  U R                  U R                   :X  a  U R                  5         gU R                   nU R                  nS HH  nUR                  5       nU(       a#  XB:w  a  UR                  5       nU(       a  XB:w  a  M  U(       a    OX!p!MJ     W(       d  gUnU R                  R                  (       + nXBR                  5       :w  aE  UR                  U:w  a  U R                  US9  UR                  5       nXBR                  5       :w  a  MD  gg)zGtoggle applied on or off for all items in range [lastapplied,
current].N)s   forwards   reverserw  )rI  rH  r  rE   r^   )r%   	startitemenditem	directionrE   desiredstates         r   toggleallbetween$curseschunkselector.toggleallbetween  s     $$''4+?+?? ((	**1I ))+Hx2#,,. x2!(w 2    33;;;**,,</  h /((*H **,,r   c                   Uc  U R                   nU(       d&  [        U[        5      (       a  UR                  (       a  [        U[        5      (       d  UR	                  5       =U l         nOUR                  (       a  SUl        [        U[        5      (       a-  UR                  5        H  nUR                  (       + Ul        M     [        U[        [        45      (       a  UR                  (       + Ul        gg)zEtoggle folded flag of specified item (defaults to currently
selected)NF)rH  r   rY   r`   r>   r4   rA   rb   )r%   rF   
foldparentchilds       r   rn   curseschunkselector.togglefolded!  s     <++D*T8449K9KdH--26//2CC(4##%*" $))!--/E'+{{?EL 0 dXv.//"kk/DK 0r   c                    UR                  5       u  p4U R                  nUR                  S5      n[        R                  " U5      nXVU-   U-  -
  nUSU-  -   $ )z
add whitespace to the end of a string in order to make it fill
the screen in the x direction.  the current cursor position is
taken into account when making this calculation.  the string can span
multiple lines.
   r   )getyxxscreensize
expandtabsr   colwidth)r%   instrwindowyxstartwidthstrwidth	numspacess           r   alignstringcurseschunkselector.alignstring5  s^     LLN	    #$$U+/589	ti'''r   c                f   UR                  S5      n[        R                  " SS UR                  S5      5      nUb  UnOPUb  U R                  U   nO=Uc  SnUc  SnX44U R
                  ;   a  U R
                  X44   nOU R                  X45      nUc  / nUS:  a  U H  nX-  nM	     O2[        R                  [        R                  4 H  nX;   d  M
  X-  nM     U R                  R                  5       u  pSnU
(       a,  [        U5      nUR                  S5      n[        U5      nUU-
  nU(       a&  UR                  [        R                   " U5      U5        X-  nU
(       aT  U[        R"                  -  nU(       a2  [%        W5       H#  nUR'                  [        R(                  U5        M%     US	W-  -  nU	(       aB  U(       a%  U R+                  SU5      nUR                  UU5        OU R+                  X5      nUU-  nU[        U5      -   U R,                  -  nU =R.                  U-  sl        U$ )
a  
print the string, text, with the specified colors and attributes, to
the specified curses window object.

the foreground and background colors are of the form
curses.color_xxxx, where xxxx is one of: [black, blue, cyan, green,
magenta, red, white, yellow].  if pairname is provided, a color
pair will be looked up in the self.colorpairnames dictionary.

attrlist is a list containing text attributes in the form of
curses.a_xxxx, where xxxx can be: [bold, dim, normal, standout,
underline].

if align == True, whitespace is added to the printed string such that
the string stretches to the right border of the window.

if showwhtspc == True, trailing whitespace of a string is highlighted.
r  s   [\x00-\x08\x0a-\x1f]c           	     z    S[         R                  " [        [        U R	                  5       5      S-   5      5      -   $ )N   ^@   )r
   sysbyteschrordgroupms    r   rl  1curseschunkselector.printstring.<locals>.<lambda>h  s(    dX..s3qwwy>B3F/GHHr      
r      r   s    
r   )r  resubstriprD  rC  getcolorpairr   A_UNDERLINEA_BOLDchunkpadr  rt   r8  addstrr   strfromlocal	A_REVERSErangeaddchACS_CKBOARDr  r  rR  )r%   r  r   fgcolorbgcolorpairpairnameattrlisttowinalign
showwhtspc	colorpairtextattrr  r  toriglenstrippedlennumtrailingspaceswscolorpairiextrawhitespacelinesprinteds                          r   printstringcurseschunkselector.printstringD  s   @ q!vv$HJJu
 I!++H5I!T__4 OOW,>?	 --g?	Hs?$%	 % $//?')I @ MM'')	$iG;;v&Dd)K '+ 5MM(//5yA		#f&6&66K01ALL!3!3[A 2)))A"&"2"23"?oy9 #'"2"21"= A QD,<,<<##|3#r   c           
     J   U R                   R                  n[        S5      n[        S5      n[        [	        U5      [	        U5      5      nSUU(       a  UOU4-  n[
        U R                     S[        S5      [        S5      [        S5      [        S5      U[        S	5      /nU$ )
z-> [str]. return segmentss   space/enter: selects   space/enter: deselects   %-*sr   s   [x]=selected **=collapseds
   c: confirms   q: aborts    arrow keys: move/expand/collapses   ?: help)rH  r^   r   maxrt   rU  r   )r%   selectedspaceselectspacedeselectspacelenselectedlabelsegmentss          r   _getstatuslinesegments*curseschunkselector._getstatuslinesegments  s    ++33./23 s;']);<%M;#
 

 DNN+*+mkN12jM	
 r   c                   U R                   b  U R                   [        S5      /nOU R                  5       nU R                  n/ nUnU Hx  n[        R
                  " U5      nSSU=(       a    US   S;  -   -  nXF-   [        U5      -   U:  a  UR                  U5        UnMY  US==   Xu-   -  ss'   XF[        U5      -   -  nMz     [        U5      U R                  :w  a@  [        U5      U l        U R                  R                  U R                  U R                  5        U Vs/ s H&  n[        R                  " XR                  S-
  5      PM(     sn$ s  snf )z<() -> [str]. return short help used in the top status windows   Press any key to continuer   r   r   s   -[r   )rA  r   r  r  r   r  rt   r   rQ  	statuswinresizer   ellipsis)	r%   linesr  r  	lastwidthswsepr   s	            r   _getstatuslines#curseschunkselector._getstatuslines  s-   ==$]]A&B$CDE 224H$$EEI%%a(a1#:1U):;<=3s8+u4LLO !I"I(ISX-I  u:,,,"%e*DNN!!$"5"5t7G7GHFKLe
##A'7'7!';<eLLLs   !-Ec           	        U R                   R                  5         U R                  R                  5         U R                  n U R	                  5        H  nU" U R                   USS9  M     U R                   R                  5         U R                  b  g  U R                  5         U R                  5         U R                  R                  U R                  SU R                  SU R                  U R                  -
  U R                  S-
  5        g ! [        R                   a     Nf = f! [        R                   a     g f = f)N   legendr  r   r   )r  eraser  r  r  r,  r   r   rA  	printitemr  rO  rQ  r~  r  )r%   r  r   s      r   updatescreen curseschunkselector.updatescreen  s
   &&	,,.DNND9E /NN""$ ==$	NNMM!!**##  4#6#66  1$ || 		" || 		s&   AD A8D' D$#D$'D>=D>c                p   UR                   (       a,  [        U[        5      (       d  UR                  (       a  SnOSnOSn UR                  (       a/  US-  n[        U[
        5      (       a  UR                  nX#S-   -  nU$ US-  n[        U[
        5      (       a  US-  nU$ ! [         a	    US-  n U$ f = f)z`
create a string to prefix a line with which indicates whether 'item'
is applied and/or folded.
s   [~]s   [x]s   [ ]s   **r   s     )r^   r   r   r_   rA   rY   
changetyperB   )r%   rF   checkbox
filestatuss       r   getstatusprefixstring)curseschunkselector.getstatusprefixstring  s     <<dJ//DLL!!H	{{E!dH--!%JT 11H  E!dH--%H   	H	s   >B" B" "B54B5c           	        SnUR                  5       nU R                  R                  U5      nUS:w  a<  UR                  (       d+  XPR	                  U R
                  SU R                  -  USS9-  nU R                  U=(       a    S=(       d    S[        R                  /S9nSn	U R                  U5      n
UR                  (       a  U(       a  UR                  S	5      nXS   -   nOXR                  5       -   nXPR	                  U R
                  XUS
9-  nUR                  (       a  U(       aL  [        W5      S:  a=  USS  H4  nSU	[        U
5      -   -  U-   nXPR	                  U R
                  XUS
9-  nM6     U$ )z
print the header to the pad.  if countlines is True, don't print
anything, but just count the number of lines which would be printed.
r   r      _Fr  r     selected   normalr   r  r  r  r  r   Nr   )ro   rB  ru   rA   r  r  r  r  r   r  r  splitrf   rt   )r%   rU   r  r  ignorefoldingoutstrr   
chunkindexr  indentnumcharsr  textlistlinestrr   s                 r   printheadercurseschunkselector.printheader  so    !^^))&1
?6==&&td&6&66e5 '  F %%)k6Y&-- & 
	 --f5}}zz%(H!,G!22G""MM7% # 
 	
 }}8}q $QRLD"ns8}&DELG..we /  F ) r   c                   SnUR                   R                  R                  U5      nUS:w  a+  XPR                  U R                  SU R
                  -  USS9-  nU R                  U=(       a    S=(       d    S[        R                  /S9nU R                  U5      nSU R                  -  U-   n	S	UR                  5       R                  S
5      -   n
XPR                  U R                  XSS9-  nXPR                  U R                  XUS9-  nUR                  (       a	  U(       d  U$ UR                   H=  nSU R                  [!        U5      -   -  U-   nXPR                  U R                  XS9-  nM?     U$ )z!includes start/end line indicatorr   r   r   Fr  r  r  r  s      r  r  r  )rU   ra   ru   r  r  r  r  r   r  r  rM  r   r  rA   r   rN  rt   )r%   r   r  r  r  r  	hunkindexr  r  
lineprefixfrtoliner   r  s                r   printhunklinesbefore(curseschunkselector.printhunklinesbeforeF  s    KK%%++D1	>&&td&6&66e5 '  F %%)k6Y&-- & 
	
 --d3D333h>
D..066u==""MM:% # 
 	
 	""MM85 # 
 	
 ;;}M KKD33c(mCDtK  &&t}}g&KKF	   r   c                    SnUR                   (       a	  U(       d  U$ U R                  U5      nUR                   H=  nSU R                  [	        U5      -   -  U-   nX@R                  U R                  XrS9-  nM?     U$ )Nr   r   r  )rA   r  r   rN  rt   r  r  )r%   r   r  r  r  r  r   r  s           r   printhunklinesafter'curseschunkselector.printhunklinesafterr  s{    ;;}M --d3JJD33c(mCDtK  &&t}}g&KKF	  r   c           	        SnU R                  U5      nUR                  5       R                  S5      nU(       a  U R                  SS9nOqUR	                  S5      (       a  U R                  SS9nOKUR	                  S5      (       a  U R                  SS9nO%UR	                  S	5      (       a  U R                  S
S9nSU R
                  -  U-   nX@R                  U R                  XSS9-  nX@R                  U R                  UWUSS9-  nU$ )Nr   r  r  r   r      additionr      deletion   \r  r   Fr  T)r  r  r  )r  ro   r  r  r   rN  r  r  )	r%   r  r  r  r  r  r  r  r  s	            r   printhunkchangedline(curseschunkselector.printhunkchangedline  s    --h7$$&,,U3 )){);I%%)){);I%%)){);I&&))y)9ID777(B
""MM:% # 
 	
 	""MM7%D # 
 	
 r   c                    Uc  U R                   nU(       a  SU l        / nU R                  XX5US9  SR                  U5      $ )z
use __printitem() to print the the specified item.applied.
if item is not specified, then print the entire patch.
(hiding folded elements, etc. -- see __printitem() docstring)
r   r  r   )rT   rR  _curseschunkselector__printitemr   )r%   rF   r  recursechildrenr  r  s         r   r  curseschunkselector.printitem  sM     <??D*+D' 	 	
 xxr   c                    U R                   R                  5       u  p[        SU R                  U R                  -
  5      nU R                  U R                  S-  -   nX:  =(       d    X:  $ )Nr      )r  r  minrO  r~  )r%   r  r   minymaxys        r   outofdisplayedarea&curseschunkselector.outofdisplayedarea  s_    }}""$ 1d0043C3CCD))D,<,<q,@@x#18#r   c                    XR                   L nU(       a?  U(       a8  U R                  U l        U R                  USS9nU R                  U-   S-
  U l        U$ )NFr-  r   )rH  rR  rJ  getnumlinesdisplayedrK  )r%   rF   r-  r  selecteditemliness        r   handleselection#curseschunkselector.handleselection  sc    333)-)D)DD& $ 9 9e !: ! **->>B $ r   c           	        U(       a  U R                  5       (       a  gU R                  X5      n[        U[        5      (       a#  U(       a  U H  nU R	                  XrX4U5        M     [        U[
        5      (       aN  UR                  U R                  XXRS95        U(       a&  UR                   H  nU R	                  XX4U5        M     U$ [        U[        5      (       a  UR                  R                  (       a  U(       am  UR                  U R                  XXRS95        U(       aE  UR                   H  n	U R	                  XX4U5        M     UR                  U R                  XUS95        U$ [        U[        5      (       aA  UR                   R                  (       a  U(       a  UR                  U R#                  XUS95        U$ )aC  
recursive method for printing out patch/header/hunk/hunk-line data to
screen.  also returns a string with all of the content of the displayed
patch (not including coloring, etc.).

if ignorefolding is True, then folded items are printed out.

if recursechildren is False, then only print the item without its
child items.
N)r  r  r  )r4  r:  r   r	   r,  rY   r   r  ra   rb   rU   rA   r  r   r"  r   r   r)  )
r%   rF   r  r-  r  r  r  r	  r  r   s
             r   __printitemcurseschunkselector.__printitem  s    T,,..''> dE""C$$OU  
 dH%%MM  % ! 
 ::C$$OU &< 5 f%%## MM))% * 
 **A$$/5 + ,, -   j))!!mMM))$)F r   c                X    U R                  XUSS9n[        U5      U R                  -  nU$ )a!  
return the number of lines which would be displayed if the item were
to be printed to the display.  the item will not be printed to the
display (pad).
if no item is given, assume the entire patch.
if ignorefolding is True, folded items will be unfolded when counting
the number of lines.
Fr  )r  rt   r  )r%   rF   r  r-  patchdisplaystringr  s         r   r8  (curseschunkselector.getnumlinesdisplayed  s<     "^^ , 
 )*d.>.>>r   c                    [         R                  " U R                  5      u  U l        U l        U R
                  R                  U R                  U R                  5        U R                  SS9S-   U l	        [        R                  " U R                  U R                  5      U l        g! [        R                   a     gf = f)zhandle window resizingTr  r   N)r   termsizer   r  r~  r  r  rQ  r8  rP  r   newpadr  r   )r%   nframes      r   sigwinchhandler#curseschunkselector.sigwinchhandler  s    	181A1A$''1J.Dd.NN!!$"5"5t7G7GH#88t8LqPD"MM$*:*:D<L<LMDM|| 		s   B$B' 'B>=B>c                   Ub  X0R                   ;   a  U R                   U   nOUc  SnUc  SnX4U R                  ;   a  U R                  X4   nO[        U R                  5      S-   nU R                  (       ac  [        R
                  " XaU5        [        R                  " U5      =oPR                  X4'   Ub#  [        R                  " U5      U R                   U'   O:SnUb$  US:X  a  [        R                  nXpR                   U'   U=oPR                  X4'   Uc  / nUS:  a  U H  nXX-  nM	     U$ [        R                  [        R                  4 H  n	X;   d  M
  XY-  nM     U$ )a,  
get a curses color pair, adding it to self.colorpairs if it is not
already defined.  an optional string, name, can be passed as a shortcut
for referring to the color-pair.  by default, if no arguments are
specified, the white foreground / black background color-pair is
returned.

it is expected that this function will be used exclusively for
initializing color pairs, and not curses.init_pair().

attrlist is used to 'flavor' the returned color-pair.  this information
is not stored in self.colorpairs.  it contains attribute values like
curses.A_BOLD.
r   r   r   r  r  )
rD  rC  rt   rG  r   	init_pair
color_pairr  r  r  )
r%   r  r  r   r  r  	pairindexcvalr  
textattribs
             r   r   curseschunkselector.getcolorpair#  sm   $ $*=*="=++D1I!T__4 OOW,>?	014	==$$YA )))45I *! '4:4E4Ei4P++D1D';.#)#3#3D48++D1FJJI0B C Hs?$%	 %   &116==A
)+I B r   c                (    U R                   " U0 UD6  g)zsame as getcolorpair.N)r  )r%   r  r  s      r   initcolorpair!curseschunkselector.initcolorpair^  s    4*6*r   c                (   [        S5      n[        R                  " U R                  SSS5      nUR	                  S5      nUS/U R                  U R
                  -
  [        U5      -
  S-
  -  -   n U H  nU R                  X$SS9  M     UR                  5          U R                  R                  S5         UR                  5         S	S	S	5        g	! [        R                   a     N\f = f! , (       d  f       g	= f! [        R                   a     g	f = f)
z<print a help window to the screen.  exit after any keypress.s              [press any key to return to the patch-display]

The curses hunk selector allows you to interactively choose among the
changes you have made, and confirm only those changes you select for
further processing by the command you are running (such as commit,
shelve, or revert). After confirming the selected changes, the
unselected changes are still present in your working copy, so you can
use the hunk selector multiple times to split large changes into
smaller changesets. the following are valid keystrokes:

              x [space] : (un-)select item ([~]/[x] = partly/fully applied)
                [enter] : (un-)select item and go to next item of same type
                      A : (un-)select all items
                      X : (un-)select all items between current and most-recent
    up/down-arrow [k/j] : go to previous/next unfolded item
        pgup/pgdn [K/J] : go to previous/next item of same type
 right/left-arrow [l/h] : go to child item / parent item
 shift-left-arrow   [H] : go to parent header / fold selected header
                      g : go to the top
                      G : go to the bottom
                      f : fold / unfold item, hiding/revealing its children
                      F : fold / unfold parent item and all of its ancestors
                 ctrl-l : scroll the selected line to the top of the screen
                      m : edit / resume editing the commit message
                      e : edit the currently selected hunk
                      a : toggle all selections
                      c : confirm selected changes
                      r : review/edit and confirm selected changes
                      / : regex search for code or filename
                      n : next search result for code or filename
                      N : previous search result for code or filename
                      q : quit without confirming (no changes will be made)
                      ? : help (what you're currently reading)r   r  r   r   r  r     crecordN)r   r   newwinr~  r  rQ  rt   r  r   r,  r   timeblockedsectiongetkey)r%   helptexthelpwin	helplinesr   s        r   
helpwindowcurseschunkselector.helpwindowb  s     B"
H -- 0 0!Q:NN5)	t222S^CaG!
 
		!   C " 		++J7  87	 || 		 87|| 		sB   0C C: 5C)C: C&%C&)
C73C: 7C: :DDc                r   [         R                  " 5         [         R                  " 5         U R                  R	                  U R
                  U R                  R                  5       5      U l        [         R                  " 5         U R                  R                  5         U R                  R                  S5        g)z?Create a temporary commit message editing window on the screen.r   N)r   rawdef_prog_moder   editrS  usernamecbreakr2  r,  keypadr$   s    r   commitMessageWindow'curseschunkselector.commitMessageWindow  sn     	

77<<(8(8$'':J:J:LM1r   c                    U R                   S   U l        U R                  n UR                  5       nUc  OUnM  Xl        g)zD
Handle 'g' to navigate to the top most file in the ncurses window.
r   N)rT   rH  r>   r[  s      r   handlefirstlineevent(curseschunkselector.handlefirstlineevent  sL     $(??1#5 .."--/H&  $/ r   c                    U R                   nUR                  5       nUb  UR                  5       nUc  OUnUb  M  Xl         U R                  5         g)a}  
Handle 'G' to navigate to the bottom most file/hunk/line depending
on the whether the fold is active or not.

If the bottom most file is folded, it navigates to that file and
stops there. If the bottom most file is unfolded, it navigates to
the bottom most hunk in that file and stops there. If the bottom most
hunk is unfolded, it navigates to the bottom most line in that hunk.
N)rH  rE   r`  r[  s      r   handlelastlineevent'curseschunkselector.handlelastlineevent  s[     ..'')""++-H& " $/ ""$r   c                   [         R                  " U R                  SSS5      n UR                  S5      nU H  nU R	                  X$SS9  M     U R                  R                  5         UR                  5          U R                  R                  S5         [        U R                  R                  5       5      nSSS5        U$ ! [         R
                   a     Nf = f! , (       d  f       W$ = f! [         a    Sn U$ f = f)zEdisplay an informational window, then wait for and return a
keypress.r   r  r  r  rU  N)r   rV  r~  r  r  r   r2  r,  r   rW  r  getch
ValueError)r%   
windowtext
confirmwinr  r   responses         r   confirmationwindow&curseschunkselector.confirmationwindow  s     ]]4#3#3Q1=
	$$U+E  K H  		++J7t{{0023 8
  || 		
 87
   	H	sA   *C :C/ $C9C/ CC
C,'C/ ,C/ /C?>C?c                    [        S5      nU R                  R                  S5         U R                  U5      nSSS5        Wc  SnUR	                  5       R                  S5      (       a  gg! , (       d  f       N9= f)zHask for 'y' to be pressed to confirm selected. return True if
confirmed.s  If you answer yes to the following, your currently chosen patch chunks
will be loaded into an editor. To modify the patch, make the changes in your
editor and save. To accept the current patch as-is, close the editor without
saving.

note: don't add/remove lines unless you also modify the range information.
      failing to follow this rule will result in the commit aborting.

are you sure you want to review/edit and confirm the selected changes [yn]?
rU  NrF  r  TF)r   r   rW  rs  lowerr   )r%   confirmtextrr  s      r   reviewcommit curseschunkselector.reviewcommit  sn     	
 WW''
3..{;H 4H>>&&s++ 43s   A,,
A:c                B    U R                  SS9  U R                  5         g)z
once we scrolled with pg up pg down we can be pointing outside of the
display zone. we print the patch with towin=False to compute the
location of the selected item even though it is outside of the displayed
zone and then update the scroll.
Fr  N)r  r  r$   s    r   r`  )curseschunkselector.recenterdisplayedarea  s     	U#r   c                p   S nS nUc  U R                   n[        U[        5      (       a  g[        U[        5      (       a  UR	                  5       n[        U[
        5      (       d  gUR	                  5       R                  R                  U5      nUR                  UR                  pvU" X5      nUc  U(       d  U" U 5        gUR                  n	U	R                  R                  U5      n
U	R                  SU
 nU	R                  U
S-   S nUS   nUR                   Vs/ s H  n[        X5      PM     nn[        U Vs/ s H  oR                  PM     sn5      n[        U Vs/ s H  oR                  PM     sn5      nUU-
  UU-
  -
  nU H  nU=R                  U-  sl        M     U H
  nSUl        M     X-   U-   U	l        U R                  5       (       a  X/-   U-   U	l        Xl         [        U	R                  5      U:  a  U	R                  U   U l         U(       d	  U" U 5        ggs  snf s  snf s  snf )z#
edit the currently selected chunk
c                P   U R                  SS9S-   U l        [        R                  " U R                  U R                  5      U l        U R                  5         U R                  R                  5         U R                  R                  5         U R                  R                  S5        g )NTrC  r   )r8  rP  r   rE  r  r  r  r2  r,  r  rd  r$   s    r   updateui0curseschunkselector.toggleedit.<locals>.updateui  sy    #88t8LqPD"MM$*:*:D<L<LMDMKK!NN""$KKq!r   c                   Uc@  U R                   R                  [        S5      5        U R                   R                  S5        g UR                  R	                  5       (       a@  U R                   R                  [        S5      5        U R                   R                  S5        g [        5       nUR                  [        [        -   5        UR                  R                  U5        UR                  U5         U R                   R                  UR                  5       SSS9n U R                  R                  5         U R                  R!                  5         UR#                  5        Vs/ s H   nUR%                  S5      (       a  M  US-   PM"     nn[&        R(                  " U5      $ ! [        R                   aO  nUR                  U l         S nAU R                  R                  5         U R                  R!                  5         g S nAff = f! U R                  R                  5         U R                  R!                  5         f = fs  snf )Ns    cannot edit patch for whole filer  s!   cannot edit patch for binary filer   s   diff)action   #)r   r   r   rU   binaryrj   diffhelptexthunkhelptextra  rl   r   AbortmessagerA  r2  r(  r,  
splitlinesr   r   
parsepatch)r%   chunkr	   excr   s        r   editpatchwitheditor;curseschunkselector.toggleedit.<locals>.editpatchwitheditor  s   }a CDEe$||""$$a DEFe$ JEKK|34LLu%KK&U^^%5s7K
 !!###%
 ",,..Dt, u.  
 &&u-- ;;  #!!###% !!###%s6   6)F% &I	I%H9H
H HH 6INr   r   F)rH  r   rY   r   r>   rb   ra   ru   r   r   rU   sumr   rA   
emptypatchrt   )r%   rF   r1  r~  r  	itemindexbeforeaddedbeforeremoved
newpatchesrU   editedhunkindexhunksbefore
hunksafternewpatchheaderrc   newhunksnewadded
newremovedoffsets                      r   
toggleeditcurseschunkselector.toggleedit  s   
	" 	.D <++DdH%%dJ''??$D$'' OO%++11$7	%)ZZ](4
 ,,,,T2ll#3O4\\/A"5"78
#A/=/C/CD/C!F1%/CD2A23X6X))X67
[(Z--GHAHHH AAH "-
:??&/*<FL#) v||y('-||I'>D$TN ! E26s   H)=H."H3c                d    U R                   nU(       d  gU H  nUR                  (       d  M    g   gr\   )rT   ra   )r%   rF   rU   s      r   r  curseschunkselector.emptypatchZ  s,    F|||  r   c                   [         R                  " SU R                  U R                  S-
  S5      nUR	                  S5        [         R
                  " 5         [         R                  " S5        UR                  5       =(       d    S U l        [         R                  " 5         [         R                  " S5        U R                  U R                  5      (       dF  U R                  U[        S5      SSS9  UR                  5       S;  a   UR                  5       S;  a  M  AU R                  R                  5         U R                  R!                  5         g )	Nr   r   /s   Pattern not found (press ENTER)r  Fr  r  r/  	KEY_ENTER)r   rV  r  r~  echocharechocurs_setgetstrrW  noecho
showsearchr  r   rX  r2  r(  r,  )r%   wins     r   handlesearch curseschunkselector.handlesearchc  s    mmAt//1A1AA1EqISZZ\)T
tzz**45"	   **,&99 **,&99r   c                   U(       d  g U(       a  SOSn[        U R                  U5      " SS9nS n[        R                  " U5      nU(       aA  UR	                  UR
                  5      nU(       a  X@l        O[        XC5      " SS9nU(       a  MA  U R                  nU(       a.  U(       a'  SUl        UR                  5       nU(       a	  U(       a  M'  U$ )NrE   rK   F)rC   )r   rH  r  compilesearchr&   rA   r>   )r%   rW  forwardmoveattrr\  matchesunfoldeds          r   r  curseschunkselector.showsearch{  s    !(:jd66A
 

5!ll;#6#67G+6(!+8EJK k ++(#HO**,H (( r   c                   U R                   (       d  g U R                  U R                   US9(       d  [        R                  " SU R                  U R
                  S-
  S5      nU R                  X1SSS9  UR                  5       S;  a   UR                  5       S;  a  M  AU R                  R                  5         U R                  R                  5         g g )Nr  r   r   r  Fr  r  )rW  r  r   rV  r  r~  r  rX  r2  r(  r,  )r%   
failuremsgr  r  s       r   searchdirection#curseschunkselector.searchdirection  s    zztzz7;--4#3#3T5E5E5I1MCSyN**,&99 **,&99KKKK! <r   c                6    U R                  [        S5      SS9  g )Ns$   Next pattern not found (press ENTER)Tr  r  r   r$   s    r   handlenextsearch$curseschunkselector.handlenextsearch  s     56 	 	
r   c                6    U R                  [        S5      SS9  g )Ns(   Previous pattern not found (press ENTER)Fr  r  r$   s    r   handleprevsearch$curseschunkselector.handleprevsearch  s     9:E 	 	
r   c                L   US;   a  U R                  5         g!US;   a  U R                  5         g!US;   a  U R                  5         g!US;   a  U R                  5         g!US;   a  U R	                  5         g!US;   a  U R                  5         g!US;   a  U R                  5         g!US;   a  [        R                  " [        S	5      5      eUS
;   a  U R                  5         g!US;   a  gUS;   a&  U R                  5       (       a  SU R                  S'   gg!U(       a  US;   a  SU R                  S'   gUS;   a  U R                  5         g!US;   a   U R                  5         U R                  US9  g!US;   a  U R                  5         g!US;   a  U R!                  5         g!US;   a  U R#                  US9  g!US;   a  U R%                  5         g!US;   a  U R%                  SS9  g!US;   a  U R'                  5         g!US;   a  U R)                  5         g!US;   a  U R+                  5         g!US;   a  U R-                  5         g!US;   a  U R/                  5         g!US;   a  U R1                  5         g!US;   aE  U R3                  5         U R4                  R7                  5         U R4                  R9                  5         g!U[:        R<                  R?                  S 5      4;   aP  U RA                  U RB                  5        U R4                  R7                  5         U R4                  R9                  5         g!g!)"zL
Perform actions based on pressed keys.

Return true to exit the main loop.
)kKEY_UP)K	KEY_PPAGE)jKEY_DOWN)J	KEY_NPAGE)r   	KEY_RIGHT)rc   KEY_LEFT)H	KEY_SLEFT)qs	   user quit)a)r  T)rs   review)R) rn   r  r0  )X)A)e)r  )F)r  r  )r  )rF  )N)gKEY_HOME)GKEY_END)?LN)"r]  ra  rd  rg  rt  rx  r{  r   CanceledErrorr   r  rx  r  r  rq  r  r  r  rn  re  r  r  r  rh  rk  r\  r2  r(  r,  r   asciictrlr  rJ  )r%   
keypressedr1  s      r   r9  $curseschunkselector.handlekeypressed  s    (--""$,,!--$$&--  ",,!--$$&5 %%ao665 !5 5   ""'+		)$ # jE)#'DIIi :%..4(5 !!#5 NN5 OOO&5 5 .5 $$&5 5 !!#5 !!#,,%%'++$$&5 OOKKKK!FLL--c233 T778KKKK! 4r   c                v   [        5       =p#[        [        S5      (       a/  [        R                  " [        R                  U R                  5      n U R                  U5      X#La&  [        R                  " [        R                  U5        $ $ ! X#La&  [        R                  " [        R                  U5        f f = f)z@
method to be wrapped by curses.wrapper() for selecting chunks.
SIGWINCH)r  r  r  r  rH  _main)r%   r2  origsigwinchr  s       r   r  curseschunkselector.main  s    
 #)(*6:&&!==$:N:NOL	=::f%+foo|< ,|+foo|< ,s   B ,B8c                   Xl         S U l        U R                   R                  5       u  U l        U l        [
        R                  " 5          [
        R                  " 5         U R                   R                  5          [
        R                  " S5        U R                  S S SS9  U R                  [
        R                  [
        R                  SS9  U R                  [
        R                  S SS9  U R                  [
        R                   S SS9  U R                  [
        R                  [
        R"                  SS9  [
        R$                  " U R&                  SSS5      U l        U R(                  R+                  S	5        [
        R,                  " S
U R                  5      U l        U R1                  S	S9S
-   U l         [
        R,                  " U R2                  U R                  5      U l        U R1                  U R8                  SS9U l         U R=                  5          U R>                  RA                  S5         U R(                  RC                  5       nS S S 5        U RD                  b	  S U l"        Mf   U RG                  W5      (       a  OM  U RH                  S:w  aC  [J        RL                  " SSU RH                  5      nUS:w  a  U RH                  U RN                  S'   g g g ! [
        R                   a    SU l	         GNf = f! [
        R                   a     GNf = f! [
        R                   a    [5        [7        S5      5      U l         g f = f! , (       d  f       GN= f! [
        R                   a    Sn GNf = f)NFr   r  r%  r  r'  r&  r  Tr   rC  s&   this diff is too large to be displayedr7  rU  s   foobarr   s   (?m)^\s.*(\n|$)s   message)(r2  r  getmaxyxr~  r  r   start_coloruse_default_colorsr   rG  r(  r  rR  COLOR_WHITECOLOR_MAGENTA	COLOR_REDCOLOR_GREEN
COLOR_BLUErV  rQ  r  rd  rE  r  r8  rP  r   r   rH  rK  r  r   rW  rX  rA  r9  rS  r  r  r  )r%   r2  r  whitespaceremoveds       r   r  curseschunkselector._main  s    -1[[-A-A-C*$*	"%%' 		OOA 	4I6 4 4; 	 	
 	6++TD6--t+F 1 1	 	 	
  t':':Aq!Dd# a)9)9:  4444H1L	"MM$*:*:D<L<LMDM $(#<#<$$e $= $
  'WW//
;!%!6!6!8J <==,$(DM -
 $$Z00  s" "#S$*:*:! !C'(,(8(8		*% (	 #A || 	"!DM	" || 		: || 	(;<DL 		 <;
 << '&
's`   K ;L  ;0L M  7MM  K=<K= LL-M
M
MM   M:9M:)rB  r  rD  rC  rS  rH  rA  rO  rL  rT   rM  rN  r  rI  rR  rP  rQ  r   r  rW  rK  rJ  r  r2  r   rG  rT  r  r~  rN   )F)NF)NNNNNTTF)FTF)TF)FT)NFTTrM   )NFT)NNNN)8r   r   r   r   rV   r]  ra  rd  rg  rq  rt  rx  r{  r  r  r  r  r  r  rn  r  r  r  r  r  r  r  r  r"  r)  r  r4  r:  r,  r8  rH  r  rR  r\  re  rh  rk  rs  rx  r`  r  r  r  r  r  r  r  r9  r  r  r   r   r   r   r  r    sW   BH,&%&,$%,)&,",2/,:$>\|
H+(+T*((& cJ0M0>D AF+\ ?D*X4 KO ($ CG@F ?C& ?C9v+5n/ %.*0Wr04"



E"N=O9r   r  rN   )(
__future__r   r3  r  r  i18nr    r   r   r   r	   r   r
   r   r   utilsr   rj   r  r  patchhelptextr   curses.asciiImportErrorrB   r  r   r   r    r   rY   r   rb   r  r  r#  r=  rU  r  r   r   r   <module>r     sB   # 	 	     ==   
LL
OEKK OBl! l!^	 It 	 N/y N/b3 3lJAY JAZ "F,6 (),-&'
%
&	N9 N9K 	^$ Fs   C5 5
DD