a
    åze&+  ã                   @   s¤   d dl Z d dlmZ G dd„ deƒZG dd„ dƒZG dd„ deƒZG d	d
„ d
eƒZG dd„ deƒZdd„ Ze	dkr d dl
mZ edddd d dlmZ eeƒ dS )é    N)Ú	Delegatorc                   @   s–   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	dZ
dd„ ZdZdd„ Zd#dd„Zd$dd„Zdd„ Zdd„ Zd%dd„Zdd „ Zd!d"„ ZdS )&ÚUndoDelegatoriè  c                 C   s   t  | ¡ |  ¡  d S ©N)r   Ú__init__Ú
reset_undo©Úself© r	   ú"/usr/lib/python3.9/idlelib/undo.pyr      s    
zUndoDelegator.__init__c                 C   sj   | j d ur(|  d¡ |  d¡ |  d¡ t | |¡ |d urf|  d| j¡ |  d| j¡ |  d| j¡ d S )Nz<<undo>>z<<redo>>z<<dump-undo-state>>)ÚdelegateZunbindr   ÚsetdelegateÚbindÚ
undo_eventÚ
redo_eventÚ
dump_event)r   r   r	   r	   r
   r      s    



zUndoDelegator.setdelegatec                 C   sv   ddl m } || jd | j… ƒ td| jdd td| jdd td| jdd td|  ¡ ƒ || j| jd … ƒ d	S )
Nr   )Úpprintzpointer:ú )Úendzsaved:z
can_merge:zget_saved():Úbreak)r   ÚundolistÚpointerÚprintÚsavedÚ	can_mergeÚ	get_saved)r   Úeventr   r	   r	   r
   r   (   s    zUndoDelegator.dump_eventc                 C   s&   d| _ d| _g | _d| _|  d¡ d S ©Néÿÿÿÿr   é   )Ú	was_savedr   r   Ú	undoblockÚ	set_savedr   r	   r	   r
   r   2   s
    zUndoDelegator.reset_undoc                 C   s&   |r| j | _nd| _d| _|  ¡  d S )Nr   F)r   r   r   Úcheck_saved)r   Úflagr	   r	   r
   r!   9   s
    
zUndoDelegator.set_savedc                 C   s   | j | jkS r   )r   r   r   r	   r	   r
   r   A   s    zUndoDelegator.get_savedNc                 C   s
   || _ d S r   )Úsaved_change_hook)r   Úhookr	   r	   r
   Úset_saved_change_hookF   s    z#UndoDelegator.set_saved_change_hookr   c                 C   s*   |   ¡ }|| jkr&|| _| jr&|  ¡  d S r   )r   r   r$   )r   Zis_savedr	   r	   r
   r"   K   s
    
zUndoDelegator.check_savedc                 C   s   |   t|||ƒ¡ d S r   )ÚaddcmdÚInsertCommand)r   ÚindexÚcharsÚtagsr	   r	   r
   ÚinsertR   s    zUndoDelegator.insertc                 C   s   |   t||ƒ¡ d S r   )r'   ÚDeleteCommand©r   Úindex1Úindex2r	   r	   r
   ÚdeleteU   s    zUndoDelegator.deletec                 C   s    | j dkrtƒ | _ | j  ¡  d S ©Nr   )r    ÚCommandSequenceÚ
bump_depthr   r	   r	   r
   Úundo_block_startc   s    
zUndoDelegator.undo_block_startc                 C   sN   | j  d¡dkrJ| j }d| _ t|ƒdkrJt|ƒdkr>| d¡}|  |d¡ d S r   )r    r4   ÚlenÚgetcmdr'   ©r   Úcmdr	   r	   r
   Úundo_block_stoph   s    
zUndoDelegator.undo_block_stopTc                 C   sÔ   |r|  | j¡ | jdkr*| j |¡ d S | jrX| jdkrX| j| jd  }| |¡rXd S |g| j| jd …< | j| jkr|d| _| jd | _t	| jƒ| j
krÂ| jd= | jd | _| jdkrÂ| jd | _d| _|  ¡  d S )Nr   r   r   T)Údor   r    Úappendr   r   r   Úmerger   r6   Úmax_undor"   )r   r9   ZexecuteZlastcmdr	   r	   r
   r'   t   s(    


zUndoDelegator.addcmdc                 C   sP   | j dkr|  ¡  dS | j| j d  }| | j¡ | j d | _ d| _|  ¡  dS )Nr   r   r   F)r   Úbellr   Úundor   r   r"   ©r   r   r9   r	   r	   r
   r   ‹   s    
zUndoDelegator.undo_eventc                 C   sR   | j t| jƒkr|  ¡  dS | j| j  }| | j¡ | j d | _ d| _|  ¡  dS )Nr   r   F)r   r6   r   r?   Úredor   r   r"   rA   r	   r	   r
   r   –   s    zUndoDelegator.redo_event)N)N)T)Ú__name__Ú
__module__Ú__qualname__r>   r   r   r   r   r!   r   r$   r&   r   r"   r,   r1   r5   r:   r'   r   r   r	   r	   r	   r
   r      s$   



r   c                   @   sR   e Zd ZdZddd„Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	dd„ Z
dd„ ZdS )ÚCommandNc                 C   s,   i | _ i | _|| _|| _|| _|r(|| _d S r   )Úmarks_beforeÚmarks_afterr/   r0   r*   r+   )r   r/   r0   r*   r+   r	   r	   r
   r   §   s    zCommand.__init__c                 C   s>   | j j}| j| j| j| jf}| jd u r2|d d… }|t|ƒ S )Nr   )Ú	__class__rC   r/   r0   r*   r+   Úrepr)r   ÚsÚtr	   r	   r
   Ú__repr__°   s
    
zCommand.__repr__c                 C   s   d S r   r	   ©r   Útextr	   r	   r
   r;   ·   s    z
Command.doc                 C   s   d S r   r	   rN   r	   r	   r
   rB   º   s    zCommand.redoc                 C   s   d S r   r	   rN   r	   r	   r
   r@   ½   s    zCommand.undoc                 C   s   dS r2   r	   r8   r	   r	   r
   r=   À   s    zCommand.mergec                 C   s4   i }|  ¡ D ]"}|dkr|dkr| |¡||< q|S )Nr,   Úcurrent)Z
mark_namesr)   )r   rO   ÚmarksÚnamer	   r	   r
   Ú
save_marksÃ   s
    zCommand.save_marksc                 C   s"   |  ¡ D ]\}}| ||¡ qd S r   )ÚitemsÚmark_set)r   rO   rQ   rR   r)   r	   r	   r
   Ú	set_marksÊ   s    zCommand.set_marks)N)rC   rD   rE   r+   r   rM   r;   rB   r@   r=   rS   rV   r	   r	   r	   r
   rF   ¢   s   
	rF   c                   @   sN   e Zd Zddd„Zdd„ Zdd„ Zdd	„ Zd
d„ Zej	ej
 d Zdd„ ZdS )r(   Nc                 C   s   t  | |d ||¡ d S r   ©rF   r   )r   r/   r*   r+   r	   r	   r
   r   Ò   s    zInsertCommand.__init__c                 C   sv   |   |¡| _| | j¡| _| | jdd¡r6| d¡| _| | j| j| j¡ | d| jt| jƒf ¡| _	|   |¡| _
d S )Nú>úend-1cz%s+%dc)rS   rG   r)   r/   Úcomparer,   r*   r+   r6   r0   rH   rN   r	   r	   r
   r;   Õ   s    zInsertCommand.doc                 C   s>   |  d| j¡ | | j| j| j¡ |  || j¡ | d¡ d S ©Nr,   )rU   r/   r,   r*   r+   rV   rH   ÚseerN   r	   r	   r
   rB   à   s    zInsertCommand.redoc                 C   s:   |  d| j¡ | | j| j¡ |  || j¡ | d¡ d S r[   )rU   r/   r1   r0   rV   rG   r\   rN   r	   r	   r
   r@   ç   s    zInsertCommand.undoc                 C   s‚   | j |j urdS | j|jkr dS | j|jkr0dS t|jƒdkrBdS | jrh|  | jd ¡|  |j¡krhdS |j| _| j|j | _dS )NFr   r   T)rI   r0   r/   r+   r6   r*   Úclassifyr8   r	   r	   r
   r=   î   s    ÿzInsertCommand.mergeÚ_c                 C   s   || j v rdS |dkrdS dS )NÚalphanumericÚ
ÚnewlineÚpunctuation)r_   )r   Úcr	   r	   r
   r]      s
    
zInsertCommand.classify)N)rC   rD   rE   r   r;   rB   r@   r=   ÚstringÚascii_lettersÚdigitsr_   r]   r	   r	   r	   r
   r(   Ï   s   
r(   c                   @   s.   e Zd Zd
dd„Zdd„ Zdd„ Zdd	„ ZdS )r-   Nc                 C   s   t  | ||d d ¡ d S r   rW   r.   r	   r	   r
   r     s    zDeleteCommand.__init__c                 C   s   |   |¡| _| | j¡| _| jr0| | j¡| _n| | jd ¡| _| | jdd¡r^| d¡| _| | j| j¡| _| | j| j¡ |   |¡| _	d S )Nz +1crX   rY   )
rS   rG   r)   r/   r0   rZ   Úgetr*   r1   rH   rN   r	   r	   r
   r;     s    zDeleteCommand.doc                 C   s:   |  d| j¡ | | j| j¡ |  || j¡ | d¡ d S r[   )rU   r/   r1   r0   rV   rH   r\   rN   r	   r	   r
   rB     s    zDeleteCommand.redoc                 C   s:   |  d| j¡ | | j| j¡ |  || j¡ | d¡ d S r[   )rU   r/   r,   r*   rV   rG   r\   rN   r	   r	   r
   r@   $  s    zDeleteCommand.undo)N)rC   rD   rE   r   r;   rB   r@   r	   r	   r	   r
   r-     s   
r-   c                   @   sN   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ Zdd„ Zdd„ Z	ddd„Z
dS )r3   c                 C   s   g | _ d| _d S r2   )ÚcmdsÚdepthr   r	   r	   r
   r   0  s    zCommandSequence.__init__c                 C   s>   | j j}g }| jD ]}| d|f ¡ q|d d |¡ d S )Nz    %rz(
z,
z
))rI   rC   rh   r<   Újoin)r   rK   Zstrsr9   r	   r	   r
   rM   4  s
    
zCommandSequence.__repr__c                 C   s
   t | jƒS r   )r6   rh   r   r	   r	   r
   Ú__len__;  s    zCommandSequence.__len__c                 C   s   | j  |¡ d S r   )rh   r<   r8   r	   r	   r
   r<   >  s    zCommandSequence.appendc                 C   s
   | j | S r   )rh   )r   Úir	   r	   r
   r7   A  s    zCommandSequence.getcmdc                 C   s   | j D ]}| |¡ qd S r   )rh   rB   )r   rO   r9   r	   r	   r
   rB   D  s    
zCommandSequence.redoc                 C   s.   | j d d … }| ¡  |D ]}| |¡ qd S r   )rh   Úreverser@   )r   rO   rh   r9   r	   r	   r
   r@   H  s    zCommandSequence.undor   c                 C   s   | j | | _ | j S r   )ri   )r   Zincrr	   r	   r
   r4   N  s    zCommandSequence.bump_depthN)r   )rC   rD   rE   r   rM   rk   r<   r7   rB   r@   r4   r	   r	   r	   r
   r3   ,  s   r3   c                    s  ddl m}m}m} ddlm} || ƒ}| d¡ tt|  	¡  
d¡dd … ƒ\}}| 	d||d f ¡ ||d	d
}| ¡  | ¡  ||ƒ}	tƒ ‰ |	 ˆ ¡ ||d‡ fdd„d}
|
jdd ||d‡ fdd„d}|jdd ||d‡ fdd„d}|jdd d S )Nr   )ÚToplevelÚTextÚButton)Ú
PercolatorzTest UndoDelegatorú+r   z+%d+%dé¯   é
   )ZheightZUndoc                      s
   ˆ   d ¡S r   )r   r	   ©Údr	   r
   Ú<lambda>b  ó    z!_undo_delegator.<locals>.<lambda>)rO   ZcommandÚleft)ZsideZRedoc                      s
   ˆ   d ¡S r   )r   r	   ru   r	   r
   rw   d  rx   ZDumpc                      s
   ˆ   d ¡S r   )r   r	   ru   r	   r
   rw   f  rx   )Útkinterrn   ro   rp   Zidlelib.percolatorrq   ÚtitleÚmapÚintZgeometryÚsplitÚpackZ	focus_setr   Zinsertfilter)Úparentrn   ro   rp   rq   ZundowinÚxÚyrO   Úpr@   rB   Údumpr	   ru   r
   Ú_undo_delegatorS  s$    
 
r…   Ú__main__)Úmainzidlelib.idle_test.test_undoé   F)Ú	verbosityÚexit)Úrun)rd   Zidlelib.delegatorr   r   rF   r(   r-   r3   r…   rC   Zunittestr‡   Zidlelib.idle_test.htestr‹   r	   r	   r	   r
   Ú<module>   s    -9$'