about summary refs log blame commit diff stats
path: root/html/edit/012-editor-undo.mu.html
blob: 56b85dbf0b115526a928c23643ad2fc5d687cc52 (plain) (tree)
1
2
3
4
5
6
7
8
9
10



                                                                                          
                                           

                                                
                                   

                                                                                         

                       
                                                                                                 
                                                                                            
                                      
                           
                              
                            
                              
                            
                             
                                   

                               

        





                               

       
                         










                                                                                                           





                                             
                                                                                                                         


                                                                                                                        




                                                                     






                                                                                                                        







                                                                     









                                                                                                                        





                                                                                         
                                              

                                 




                                                                                                    
                                                                                                  
                                                     
                                                                                                                             
                                                    





                                                                                                                             
                                                     
                                                                                                                                             





                                                                                                    
                                                                                                  
                                                     
                                                                                                                             
                                                    





                                                                                                                             
                                                     
                                                                                                                                             





                                                                 
                                           

                                                                                              
                                                                                                                                                                                     
                         
                  
                                          
   
                                      




                                     
                                        




                                                         
                                                                  



                                                                    
                                          

       
                                        



                                              
                                                                  





                                                                                                  

                                                                                                                                         

                                                                                                 



                                                                                                                                     

                                                                                                       
                                                    

                                                                                                                                               
                                                          
                                                                                                                     
                                                                     






                                                                                                                                           
                                                                                                    

                                                               




                                                                                                                                                                                                                                                                    




                                                                                              



                                                                                                                                         

                                                                                             


                                                                                                                                     
                                               





                                                                                                                                                                                                                                                                              





                                                                                                        
                                                                                                                                                 
                                           
                                                






                                                                                                                           



                                                                                       
                                                                                                                                               
                                                          

                                                                                                                                
                                                                                                                                 

                                                                                                                             
                                     





                                                                                                                                       



                                                                          
                                           

                                                                                              
                                                                                                                                                                                     
                         
                  
                                            
   
                                      




                                     
                                        




                                                            
                                                                  




                                                                            
                                           

                                                                                              
                                                                                                                                                                                      
                         

                                                     
                                            
   
                                      


                                              
                                                                  






                                              
                                        




                                                      
                                                                  



                                                                    
                                          

       
                                        



                                              
                                                                  




                                                                       
                                           

                                                                                              
                                                                                                                                                                                          
                         

                                         
                                                                               

               
                                      



                                              
                                                                  


                                                 

                                                                                                                                      
                         

                                                                                                      





                                     
                                        
   

                                                                                                                                      
                         

                                                                                                      




                                                      
                                                                  



                                                                
                                          

       
                                        



                                              
                                                                  






                                                             
                                           

                                                                                              
                                                                                                                                                                                      
                         
                  
                                            

                
                                      


                                              
                                                                  






                                              
                                        




                                                            
                                                                  



                                                                    
                                          

       
                                        



                                              
                                                                  





                                                                                       
                                                                                                                                               
                                                          

                                                                                                                                                                                                                              

                                                                               
                                                                                                                                 





                                                                                                                                      



                                                                   
                                           

                                                                                              
                                                                                                                                                                                     
                         
                  
                                            

                
                                      


                                              
                                                                  






                                              
                                        




                                                            
                                                                  



                                                                    
                                          

       
                                        



                                              
                                                                  




                                                                        
                                           

                                                                                              
                                                                                         

                                  
                                                                                                                                                             
                         
                  
                                          

                
                                      

                                                  
                                          
   
                                      




                                              
                                                                  





                                     
                                        






                                                      
                                                                  



                                                                                   
                                           

                                                                                              
                                                                                                                                                                                     
                         


                                                                                              
                                           
             
                                           

               
                                            
   
                                      



                                              
                                                                  

                                              

                                                                                                                                      
                         

                                                                                                      





                                     
                                        

                                                                              

                                                                                                                                      
                         

                                                                                                      




                                              
                                                                  






                                              
                                        

                                                           

                                                                                                                                      
                         

                                                                                                      



                                              
                                                                  






                                              
                                        

                                             

                                                                                                                                      
                         

                                                                                                      



                                              
                                                                  






                                              
                                        

                                                    

                                                                                                                                      
                         

                                                                                                      



                                              
                                                                  






                                              
                                        

                                                            

                                                                                                                                      
                         

                                                                                                      




                                              
                                                                  






                                              
                                        

                                                           

                                                                                                                                      
                         

                                                                                                      




                                              
                                                                  






                                                                
                                           

                                                                                              
                                                                                         

                                  
                                                                                                                                                             
                         

                                                
                                                                               
   
                                      




                                     
                                        

                                             

                                                                                                                                      
                         

                                                                                                      


                                                                    
                                          

       
                                        





                                              
                                                                  



                                                                                             


                                                                                                                                      

                                                                                            


                                                                                                                                     



                                                                                                        
                                                                                                                             
                                                    

                                                                                                                                       
                                                        

                                                                                                                   
                                                         



                                                                                                                         
                                                                                                  
                                  


                                                                                                                                                                                                                       




                                                                                       
                                                                                                                                       
                                                        
                                                                                                                                 





                                                                                                                                     



                                                                 
                                           


                                                                                             
                                                                                       

                                     
                                                                                                                                                            

                                                                                       
                                                                               

                     
                                      

                                                                                                                                      







                                               

                                                                                                      





                                     
                                        

                                                  

                                                                                                                                      
                         

                                                                                                      









                                                                    
                                          

       
                                        









                                                                     
                                           

                                                                                              
                                                                                         

                                  
                                                                                                                                                             
                         

                                                
                                                                               

                    
                                      




                                     
                                        

                                                  

                                                                                                                                      
                         

                                                                                                      


                                                                    
                                          

       
                                        





                                              
                                                                  



                                                                   
                                           

                                                                                              
                                                                                         

                                  
                                                                                                                                                             
                         

                                                
                                                                               

                  
                                      

                                                                                                                                      
                         

                                                                                                      





                                     
                                        

                                                  

                                                                                                                                      
                         

                                                                                                      


                                                                    
                                          

       
                                        





                                              
                                                                  



                                                                     
                                           

                                                                                              
                                                                                         

                                  
                                                                                                                                                             
                         

                                                
                                                                               

                    
                                      




                                     
                                        

                                                  

                                                                                                                                      
                         

                                                                                                      


                                                                    
                                          

       
                                        





                                              
                                                                  



                                                                 
                                           

                                                                                              
                                                                                       




                                
                                                                                                                                                             
                         



                                                
                                      




                                     
                                        











                                                                    
                                           

                                                                                              
                                                                                       




                                
                                                                                                                                                             
                         



                                                
                                      




                                     
                                        











                                                                 
                                           

                                                                                              
                                                                                       




                                
                                                                                                                                                             
                         




                                                            
                                      




                                     
                                        






                                                                
                                                                  



                                                                  
                                           

                                                                                              
                                                                                       




                                
                                                                                                                                                             
                         




                                                            
                                      




                                     
                                        






                                                                
                                                                  



                                                                 
                                           

                                                                                              
                                                                                         

                                  
                                                                                                                                                             
                         

                                                                       
                                                                               

                
                                      




                                     
                                        

                                                  

                                                                                                                                      
                         

                                                                                                      


                                                                    
                                          

       
                                        





                                              
                                                                  



                                                               
                                           

                                                                                              
                                                                                         

                                  
                                                                                                                                                             
                         

                                                                       
                                                                               

              
                                      




                                     
                                        

                                                  

                                                                                                                                      
                         

                                                                                                      


                                                                    
                                          

       
                                        





                                              
                                                                  



                                                                 
                                           

                                                                                              
                                                                                         

                                  
                                                                                                                                                             
                         

                                                                       
                                                                               

                
                                      




                                     
                                        

                                                  

                                                                                                                                      
                         

                                                                                                      


                                                                    
                                          

       
                                        





                                              
                                                                  



                                                              
                                           

                                                                                              
                                                                                         

                                  
                                                                                                                                                             
                         

                                                                       
                                                                               

             
                                      




                                     
                                        

                                                  

                                                                                                                                      
                         

                                                                                                      


                                                                    
                                          

       
                                        





                                              
                                                                  


   
                                                                                                
                                           

                                                                                              
                                                                                         

                                  
                                                                                                                                                             
                         

                                                
                                                                               



                     
                                      

                                                                                                                                      
                         

                                                                                                      





                                     
                                        

                                                   

                                                                                                                                      
                         

                                                                                                      





                                           
                                        

                                                             

                                                                                                                                      
                         

                                                                                                      





                                                              
                                           

                                                                                              
                                                                                         

                                  
                                                                                                                                                             
                         
                  
                                                                               

                
                                      




                                     
                                        

                                                           

                                                                                                                                      
                         

                                                                                                      


                                                                    
                                          

       
                                        





                                              
                                                                  




                                                                                       
                                                                                                                                       

                                                                                                                                 





                                                                                                                                    


                                  
                                                                                             
                                           

                                                                                                       
                                                                                                                                                                                     
                         
                  
                                            
                                                                               
                                          
   
                                      

                                                                                                                                      


                                              
                                                                  


                                              

                                                                                                      





                                     
                                        

                                                                                                                                        




                                                             
                                                                  


                                              

                                                                                                      





                                           
                                        

                                                                                                                                        




                                                                  
                                                                  


                                              

                                                                                                      





                                           
                                        

                                                                                                                                        




                                              
                                                                  


                                              

                                                                                                      





                                     
                                        

                                                                                                                                        




                                              
                                                                  


                                              

                                                                                                      





                                           
                                        

                                                                                                                                        




                                              
                                                                  

                                              
                                             
                         

                                                                                                      





                                           
                                        

                                                                                                                                        




                                              
                                                                  


                                              

                                                                                                      


   


                                                                             
                                           

                                                                                              
                                                                                                                                                                                     
                         

                                                                   
                                            


                   
                                      


                                              
                                                                  

                                              

                                                                                                                                      
                         

                                                                                                      





                                     
                                        
   

                                                                                                                                      
                         

                                                                                                      



                                              
                                                                  






                                              
                                        
   

                                                                                                                                      
                         

                                                                                                      



                                              
                                                                  





                                                                                                     
                                                                                                                                      



                                                                                                                                             




                                                                                                                                           

                                                                                                         
                                                      

                                                                                                                                                   
                                                            

                                                                                                                                       
                                                           

                                                                                                                                                 
                                                     




                                                                                                                                   
                                                                                                         

                                                                 



                                                                                                                                                                                                                                                                                                                           





                                                                                       
                                                                                                                                                 
                                                          
                                                                                                                                   
                                                      

                                                                                                                                     
                                
                                                                                                                                 






                                                                                                                                         




                                                                                       
                                                                                                                                                 
                                                          


                                                                                                                                  
                             
                                                                                                                                 





                                                                                                                                         





                                                                          
                                           

                                                                                              
                                                                                                                                                                                     
                         

                                                                                          
                                               
                                                                               




                   
                                      


                                              
                                                                  

                                              

                                                                                                                                      
                         

                                                                                                      





                                             
                                        
   

                                                                                                                                      
                         

                                                                                                      



                                              
                                                                  






                                               
                                        
   

                                                                                                                                      
                         

                                                                                                      



                                              
                                                                  






                                                  
                                        
   

                                                                                                                                      
                         

                                                                                                      



                                              
                                                                  






                                                  
                                        

                                                    

                                                                                                                                      
                         

                                                                                                      



                                              
                                                                  






                                               
                                        

                                                    

                                                                                                                                      
                         

                                                                                                      



                                              
                                                                  






                                              
                                        

                                                    

                                                                                                                                      
                         

                                                                                                      



                                              
                                                                  




                                                                                                  
                                                                                                                                      



                                                                                                                                       




                                                                                                                                           

                                                                                                         
                                                      

                                                                                                                                                   
                                                            

                                                                                                                                    
                                                           








                                                                                                                                              
                                                                                                      

                                                                 



                                                                                                                                                                                                                                                                                                                     






                                                                          
                                           

                                                                                              
                                                                                         
                                  
                                                                                                                                                             
                         

                                                                                          
                                                                               

                
                                      



                                              
                                                                  

                                              

                                                                                                                                      
                         

                                                                                                      





                                     
                                        




                                              
                                                                  

                                              

                                                                                                                                      
                         

                                                                                                      





                                     
                                        





                                                    
                                                                  

                                              

                                                                                                                                      
                         

                                                                                                      


                                                                    
                                          

       
                                        




                                              
                                                                  




                                                                                                       
                                                                                                                                      



                                                                                                                                        






                                                                                                                                                                                                                                                                                                                     






                                                                          
                                           

                                                                                              
                                                                                         
                                  
                                                                                                                                                             
                         

                                                                                          
                                                                               

                
                                      



                                              
                                                                  

                                              

                                                                                                                                      
                         

                                                                                                      





                                     
                                        




                                              
                                                                  

                                              

                                                                                                                                      
                         

                                                                                                      





                                     
                                        





                                                    
                                                                  

                                              

                                                                                                                                      
                         

                                                                                                      


                                                                    
                                          

       
                                        




                                              
                                                                  




                                                                                                         
                                                                                                                                      



                                                                                                                                        







                                                                                                                                                                                                                                                                                                                     



                                                               
                                                                            
                                           

                                                                                              
                                                                                                                                                                                     
                         

                                                                                          
                                            


                
                                      


                                              
                                                                  


                                              


       
                                     
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>Mu - edit/012-editor-undo.mu</title>
<meta name="Generator" content="Vim/7.4">
<meta name="plugin-version" content="vim7.4_v2">
<meta name="syntax" content="none">
<meta name="settings" content="use_css,pre_wrap,no_foldcolumn,expand_tabs,prevent_copy=">
<meta name="colorscheme" content="minimal">
<style type="text/css">
<!--
pre { white-space: pre-wrap; font-family: monospace; color: #eeeeee; background-color: #080808; }
body { font-size: 12pt; font-family: monospace; color: #eeeeee; background-color: #080808; }
* { font-size: 12pt; font-size: 1em; }
.muData { color: #ffff00; }
.muControl { color: #c0a020; }
.Special { color: #c00000; }
.Delimiter { color: #800080; }
.Comment { color: #9090ff; }
.Constant { color: #00a0a0; }
.SalientComment { color: #00ffff; }
.muRecipe { color: #ff8700; }
.muScenario { color: #00af00; }
-->
</style>

<script type='text/javascript'>
<!--

-->
</script>
</head>
<body>
<pre id='vimCodeElement'>
<span class="SalientComment">## undo/redo</span>

<span class="Comment"># for every undoable event, create a type of *operation* that contains all the</span>
<span class="Comment"># information needed to reverse it</span>
<span class="muData">exclusive-container</span> operation [
  typing:insert-operation
  move:move-operation
  delete:delete-operation
]

<span class="muData">container</span> insert-operation [
  before-row:num
  before-column:num
  before-top-of-screen:&amp;:duplex-list:char
  after-row:num
  after-column:num
  after-top-of-screen:&amp;:duplex-list:char
  <span class="Comment"># inserted text is from 'insert-from' until 'insert-until'; list doesn't have to terminate</span>
  insert-from:&amp;:duplex-list:char
  insert-until:&amp;:duplex-list:char
  tag:num  <span class="Comment"># event causing this operation; might be used to coalesce runs of similar events</span>
    <span class="Comment"># 0: no coalesce (enter+indent)</span>
    <span class="Comment"># 1: regular alphanumeric characters</span>
]

<span class="muData">container</span> move-operation [
  before-row:num
  before-column:num
  before-top-of-screen:&amp;:duplex-list:char
  after-row:num
  after-column:num
  after-top-of-screen:&amp;:duplex-list:char
  tag:num  <span class="Comment"># event causing this operation; might be used to coalesce runs of similar events</span>
    <span class="Comment"># 0: no coalesce (touch events, etc)</span>
    <span class="Comment"># 1: left arrow</span>
    <span class="Comment"># 2: right arrow</span>
    <span class="Comment"># 3: up arrow</span>
    <span class="Comment"># 4: down arrow</span>
]

<span class="muData">container</span> delete-operation [
  before-row:num
  before-column:num
  before-top-of-screen:&amp;:duplex-list:char
  after-row:num
  after-column:num
  after-top-of-screen:&amp;:duplex-list:char
  deleted-text:&amp;:duplex-list:char
  delete-from:&amp;:duplex-list:char
  delete-until:&amp;:duplex-list:char
  tag:num  <span class="Comment"># event causing this operation; might be used to coalesce runs of similar events</span>
    <span class="Comment"># 0: no coalesce (ctrl-k, ctrl-u)</span>
    <span class="Comment"># 1: backspace</span>
    <span class="Comment"># 2: delete</span>
]

<span class="Comment"># every editor accumulates a list of operations to undo/redo</span>
<span class="muData">container</span> editor [
  undo:&amp;:list:&amp;:operation
  redo:&amp;:list:&amp;:operation
]

<span class="Comment"># ctrl-z - undo operation</span>
<span class="muRecipe">after</span> <span class="Constant">&lt;handle-special-character&gt;</span> [
  <span class="Delimiter">{</span>
    undo?:bool <span class="Special">&lt;-</span> equal c, <span class="Constant">26/ctrl-z</span>
    <span class="muControl">break-unless</span> undo?
    undo:&amp;:list:&amp;:operation <span class="Special">&lt;-</span> get *editor, <span class="Constant">undo:offset</span>
    <span class="muControl">break-unless</span> undo
    op:&amp;:operation <span class="Special">&lt;-</span> first undo
    undo <span class="Special">&lt;-</span> rest undo
    *editor <span class="Special">&lt;-</span> put *editor, <span class="Constant">undo:offset</span>, undo
    redo:&amp;:list:&amp;:operation <span class="Special">&lt;-</span> get *editor, <span class="Constant">redo:offset</span>
    redo <span class="Special">&lt;-</span> push op, redo
    *editor <span class="Special">&lt;-</span> put *editor, <span class="Constant">redo:offset</span>, redo
<span class="Constant">    &lt;handle-undo&gt;</span>
    <span class="muControl">return</span> screen/same-as-ingredient:0, editor/same-as-ingredient:1, <span class="Constant">1/go-render</span>
  <span class="Delimiter">}</span>
]

<span class="Comment"># ctrl-y - redo operation</span>
<span class="muRecipe">after</span> <span class="Constant">&lt;handle-special-character&gt;</span> [
  <span class="Delimiter">{</span>
    redo?:bool <span class="Special">&lt;-</span> equal c, <span class="Constant">25/ctrl-y</span>
    <span class="muControl">break-unless</span> redo?
    redo:&amp;:list:&amp;:operation <span class="Special">&lt;-</span> get *editor, <span class="Constant">redo:offset</span>
    <span class="muControl">break-unless</span> redo
    op:&amp;:operation <span class="Special">&lt;-</span> first redo
    redo <span class="Special">&lt;-</span> rest redo
    *editor <span class="Special">&lt;-</span> put *editor, <span class="Constant">redo:offset</span>, redo
    undo:&amp;:list:&amp;:operation <span class="Special">&lt;-</span> get *editor, <span class="Constant">undo:offset</span>
    undo <span class="Special">&lt;-</span> push op, undo
    *editor <span class="Special">&lt;-</span> put *editor, <span class="Constant">undo:offset</span>, undo
<span class="Constant">    &lt;handle-redo&gt;</span>
    <span class="muControl">return</span> screen/same-as-ingredient:0, editor/same-as-ingredient:1, <span class="Constant">1/go-render</span>
  <span class="Delimiter">}</span>
]

<span class="Comment"># undo typing</span>

<span class="muScenario">scenario</span> editor-can-undo-typing [
  <span class="Constant">local-scope</span>
  <span class="Comment"># create an editor and type a character</span>
  assume-screen <span class="Constant">10/width</span>, <span class="Constant">5/height</span>
  e:&amp;:editor <span class="Special">&lt;-</span> new-editor <span class="Constant">[]</span>, screen, <span class="Constant">0/left</span>, <span class="Constant">10/right</span>
  editor-render screen, e
  assume-console [
    type <span class="Constant">[0]</span>
  ]
  editor-event-loop screen, console, e
  <span class="Comment"># undo</span>
  assume-console [
    press ctrl-z
  ]
  run [
    editor-event-loop screen, console, e
  ]
  <span class="Comment"># character should be gone</span>
  screen-should-contain [
   <span class="Constant"> .          .</span>
   <span class="Constant"> .          .</span>
<span class="Constant">    .╌╌╌╌╌╌╌╌╌╌.</span>
   <span class="Constant"> .          .</span>
  ]
  <span class="Comment"># cursor should be in the right place</span>
  assume-console [
    type <span class="Constant">[1]</span>
  ]
  run [
    editor-event-loop screen, console, e
  ]
  screen-should-contain [
   <span class="Constant"> .          .</span>
   <span class="Constant"> .1         .</span>
<span class="Constant">    .╌╌╌╌╌╌╌╌╌╌.</span>
   <span class="Constant"> .          .</span>
  ]
]

<span class="Comment"># save operation to undo</span>
<span class="muRecipe">after</span> <span class="Constant">&lt;insert-character-begin&gt;</span> [
  top-before:&amp;:duplex-list:char <span class="Special">&lt;-</span> get *editor, <span class="Constant">top-of-screen:offset</span>
  cursor-before:&amp;:duplex-list:char <span class="Special">&lt;-</span> get *editor, <span class="Constant">before-cursor:offset</span>
]
<span class="muRecipe">before</span> <span class="Constant">&lt;insert-character-end&gt;</span> [
  top-after:&amp;:duplex-list:char <span class="Special">&lt;-</span> get *editor, <span class="Constant">top-of-screen:offset</span>
  cursor-row:num <span class="Special">&lt;-</span> get *editor, <span class="Constant">cursor-row:offset</span>
  cursor-column:num <span class="Special">&lt;-</span> get *editor, <span class="Constant">cursor-column:offset</span>
  undo:&amp;:list:&amp;:operation <span class="Special">&lt;-</span> get *editor, <span class="Constant">undo:offset</span>
  <span class="Delimiter">{</span>
    <span class="Comment"># if previous operation was an insert, coalesce this operation with it</span>
    <span class="muControl">break-unless</span> undo
    op:&amp;:operation <span class="Special">&lt;-</span> first undo
    typing:insert-operation, is-insert?:bool <span class="Special">&lt;-</span> maybe-convert *op, <span class="Constant">typing:variant</span>
    <span class="muControl">break-unless</span> is-insert?
    previous-coalesce-tag:num <span class="Special">&lt;-</span> get typing, <span class="Constant">tag:offset</span>
    <span class="muControl">break-unless</span> previous-coalesce-tag
    before-cursor:&amp;:duplex-list:char <span class="Special">&lt;-</span> get *editor, <span class="Constant">before-cursor:offset</span>
    insert-until:&amp;:duplex-list:char <span class="Special">&lt;-</span> next before-cursor
    typing <span class="Special">&lt;-</span> put typing, <span class="Constant">insert-until:offset</span>, insert-until
    typing <span class="Special">&lt;-</span> put typing, <span class="Constant">after-row:offset</span>, cursor-row
    typing <span class="Special">&lt;-</span> put typing, <span class="Constant">after-column:offset</span>, cursor-column
    typing <span class="Special">&lt;-</span> put typing, <span class="Constant">after-top-of-screen:offset</span>, top-after
    *op <span class="Special">&lt;-</span> merge <span class="Constant">0/insert-operation</span>, typing
    <span class="muControl">break</span> <span class="Constant">+done-adding-insert-operation</span>
  <span class="Delimiter">}</span>
  <span class="Comment"># if not, create a new operation</span>
  insert-from:&amp;:duplex-list:char <span class="Special">&lt;-</span> next cursor-before
  insert-to:&amp;:duplex-list:char <span class="Special">&lt;-</span> next insert-from
  op:&amp;:operation <span class="Special">&lt;-</span> new <span class="Constant">operation:type</span>
  *op <span class="Special">&lt;-</span> merge <span class="Constant">0/insert-operation</span>, save-row/before, save-column/before, top-before, cursor-row/after, cursor-column/after, top-after, insert-from, insert-to, <span class="Constant">1/coalesce</span>
  editor <span class="Special">&lt;-</span> add-operation editor, op
<span class="Constant">  +done-adding-insert-operation</span>
]

<span class="Comment"># enter operations never coalesce with typing before or after</span>
<span class="muRecipe">after</span> <span class="Constant">&lt;insert-enter-begin&gt;</span> [
  cursor-row-before:num <span class="Special">&lt;-</span> copy cursor-row
  cursor-column-before:num <span class="Special">&lt;-</span> copy cursor-column
  top-before:&amp;:duplex-list:char <span class="Special">&lt;-</span> get *editor, <span class="Constant">top-of-screen:offset</span>
  cursor-before:&amp;:duplex-list:char <span class="Special">&lt;-</span> get *editor, <span class="Constant">before-cursor:offset</span>
]
<span class="muRecipe">before</span> <span class="Constant">&lt;insert-enter-end&gt;</span> [
  top-after:&amp;:duplex-list:char <span class="Special">&lt;-</span> get *editor, <span class="Constant">top-of-screen:offset</span>
  cursor-row:num <span class="Special">&lt;-</span> get *editor, <span class="Constant">cursor-row:offset</span>
  cursor-column:num <span class="Special">&lt;-</span> get *editor, <span class="Constant">cursor-row:offset</span>
  <span class="Comment"># never coalesce</span>
  insert-from:&amp;:duplex-list:char <span class="Special">&lt;-</span> next cursor-before
  before-cursor:&amp;:duplex-list:char <span class="Special">&lt;-</span> get *editor, <span class="Constant">before-cursor:offset</span>
  insert-to:&amp;:duplex-list:char <span class="Special">&lt;-</span> next before-cursor
  op:&amp;:operation <span class="Special">&lt;-</span> new <span class="Constant">operation:type</span>
  *op <span class="Special">&lt;-</span> merge <span class="Constant">0/insert-operation</span>, cursor-row-before, cursor-column-before, top-before, cursor-row/after, cursor-column/after, top-after, insert-from, insert-to, <span class="Constant">0/never-coalesce</span>
  editor <span class="Special">&lt;-</span> add-operation editor, op
]

<span class="Comment"># Everytime you add a new operation to the undo stack, be sure to clear the</span>
<span class="Comment"># redo stack, because it's now obsolete.</span>
<span class="Comment"># Beware: since we're counting cursor moves as operations, this means just</span>
<span class="Comment"># moving the cursor can lose work on the undo stack.</span>
<span class="muRecipe">def</span> add-operation editor:&amp;:editor, op:&amp;:operation<span class="muRecipe"> -&gt; </span>editor:&amp;:editor [
  <span class="Constant">local-scope</span>
  <span class="Constant">load-ingredients</span>
  undo:&amp;:list:&amp;:operation <span class="Special">&lt;-</span> get *editor, <span class="Constant">undo:offset</span>
  undo <span class="Special">&lt;-</span> push op undo
  *editor <span class="Special">&lt;-</span> put *editor, <span class="Constant">undo:offset</span>, undo
  redo:&amp;:list:&amp;:operation <span class="Special">&lt;-</span> get *editor, <span class="Constant">redo:offset</span>
  redo <span class="Special">&lt;-</span> copy<span class="Constant"> 0</span>
  *editor <span class="Special">&lt;-</span> put *editor, <span class="Constant">redo:offset</span>, redo
  <span class="muControl">return</span> editor/same-as-ingredient:0
]

<span class="muRecipe">after</span> <span class="Constant">&lt;handle-undo&gt;</span> [
  <span class="Delimiter">{</span>
    typing:insert-operation, is-insert?:bool <span class="Special">&lt;-</span> maybe-convert *op, <span class="Constant">typing:variant</span>
    <span class="muControl">break-unless</span> is-insert?
    start:&amp;:duplex-list:char <span class="Special">&lt;-</span> get typing, <span class="Constant">insert-from:offset</span>
    end:&amp;:duplex-list:char <span class="Special">&lt;-</span> get typing, <span class="Constant">insert-until:offset</span>
    <span class="Comment"># assert cursor-row/cursor-column/top-of-screen match after-row/after-column/after-top-of-screen</span>
    before-cursor:&amp;:duplex-list:char <span class="Special">&lt;-</span> prev start
    *editor <span class="Special">&lt;-</span> put *editor, <span class="Constant">before-cursor:offset</span>, before-cursor
    remove-between before-cursor, end
    cursor-row <span class="Special">&lt;-</span> get typing, <span class="Constant">before-row:offset</span>
    *editor <span class="Special">&lt;-</span> put *editor, <span class="Constant">cursor-row:offset</span>, cursor-row
    cursor-column <span class="Special">&lt;-</span> get typing, <span class="Constant">before-column:offset</span>
    *editor <span class="Special">&lt;-</span> put *editor, <span class="Constant">cursor-column:offset</span>, cursor-column
    top:&amp;:duplex-list:char <span class="Special">&lt;-</span> get typing, <span class="Constant">before-top-of-screen:offset</span>
    *editor <span class="Special">&lt;-</span> put *editor, <span class="Constant">top-of-screen:offset</span>, top
  <span class="Delimiter">}</span>
]

<span class="muScenario">scenario</span> editor-can-undo-typing-multiple [
  <span class="Constant">local-scope</span>
  <span class="Comment"># create an editor and type multiple characters</span>
  assume-screen <span class="Constant">10/width</span>, <span class="Constant">5/height</span>
  e:&amp;:editor <span class="Special">&lt;-</span> new-editor <span class="Constant">[]</span>, screen, <span class="Constant">0/left</span>, <span class="Constant">10/right</span>
  editor-render screen, e
  assume-console [
    type <span class="Constant">[012]</span>
  ]
  editor-event-loop screen, console, e
  <span class="Comment"># undo</span>
  assume-console [
    press ctrl-z
  ]
  run [
    editor-event-loop screen, console, e
  ]
  <span class="Comment"># all characters must be gone</span>
  screen-should-contain [
   <span class="Constant"> .          .</span>
   <span class="Constant"> .          .</span>
<span class="Constant">    .╌╌╌╌╌╌╌╌╌╌.</span>
   <span class="Constant"> .          .</span>
  ]
]

<span class="muScenario">scenario</span> editor-can-undo-typing-multiple-2 [
  <span class="Constant">local-scope</span>
  <span class="Comment"># create an editor with some text</span>
  assume-screen <span class="Constant">10/width</span>, <span class="Constant">5/height</span>
  e:&amp;:editor <span class="Special">&lt;-</span> new-editor <span class="Constant">[a]</span>, screen, <span class="Constant">0/left</span>, <span class="Constant">10/right</span>
  editor-render screen, e
  <span class="Comment"># type some characters</span>
  assume-console [
    type <span class="Constant">[012]</span>
  ]
  editor-event-loop screen, console, e
  screen-should-contain [
   <span class="Constant"> .          .</span>
   <span class="Constant"> .012a      .</span>
<span class="Constant">    .╌╌╌╌╌╌╌╌╌╌.</span>
   <span class="Constant"> .          .</span>
  ]
  <span class="Comment"># undo</span>
  assume-console [
    press ctrl-z
  ]
  run [
    editor-event-loop screen, console, e
  ]
  <span class="Comment"># back to original text</span>
  screen-should-contain [
   <span class="Constant"> .          .</span>
   <span class="Constant"> .a         .</span>
<span class="Constant">    .╌╌╌╌╌╌╌╌╌╌.</span>
   <span class="Constant"> .          .</span>
  ]
  <span class="Comment"># cursor should be in the right place</span>
  assume-console [
    type <span class="Constant">[3]</span>
  ]
  run [
    editor-event-loop screen, console, e
  ]
  screen-should-contain [
   <span class="Constant"> .          .</span>
   <span class="Constant"> .3a        .</span>
<span class="Constant">    .╌╌╌╌╌╌╌╌╌╌.</span>
   <span class="Constant"> .          .</span>
  ]
]

<span class="muScenario">scenario</span> editor-can-undo-typing-enter [
  <span class="Constant">local-scope</span>
  <span class="Comment"># create an editor with some text</span>
  assume-screen <span class="Constant">10/width</span>, <span class="Constant">5/height</span>
  e:&amp;:editor <span class="Special">&lt;-</span> new-editor <span class="Constant">[  abc]</span>, screen, <span class="Constant">0/left</span>, <span class="Constant">10/right</span>
  editor-render screen, e
  <span class="Comment"># new line</span>
  assume-console [
    left-click<span class="Constant"> 1</span>,<span class="Constant"> 8</span>
    press enter
  ]
  editor-event-loop screen, console, e
  screen-should-contain [
   <span class="Constant"> .          .</span>
   <span class="Constant"> .  abc     .</span>
   <span class="Constant"> .          .</span>
<span class="Constant">    .╌╌╌╌╌╌╌╌╌╌.</span>
   <span class="Constant"> .          .</span>
  ]
  <span class="Comment"># line is indented</span>
  3:num/<span class="Special">raw</span> <span class="Special">&lt;-</span> get *e, <span class="Constant">cursor-row:offset</span>
  4:num/<span class="Special">raw</span> <span class="Special">&lt;-</span> get *e, <span class="Constant">cursor-column:offset</span>
  memory-should-contain [
   <span class="Constant"> 3</span> <span class="Special">&lt;-</span><span class="Constant"> 2</span>
   <span class="Constant"> 4</span> <span class="Special">&lt;-</span><span class="Constant"> 2</span>
  ]
  <span class="Comment"># undo</span>
  assume-console [
    press ctrl-z
  ]
  run [
    editor-event-loop screen, console, e
  ]
  3:num/<span class="Special">raw</span> <span class="Special">&lt;-</span> get *e, <span class="Constant">cursor-row:offset</span>
  4:num/<span class="Special">raw</span> <span class="Special">&lt;-</span> get *e, <span class="Constant">cursor-column:offset</span>
  memory-should-contain [
   <span class="Constant"> 3</span> <span class="Special">&lt;-</span><span class="Constant"> 1</span>
   <span class="Constant"> 4</span> <span class="Special">&lt;-</span><span class="Constant"> 5</span>
  ]
  <span class="Comment"># back to original text</span>
  screen-should-contain [
   <span class="Constant"> .          .</span>
   <span class="Constant"> .  abc     .</span>
<span class="Constant">    .╌╌╌╌╌╌╌╌╌╌.</span>
   <span class="Constant"> .          .</span>
  ]
  <span class="Comment"># cursor should be at end of line</span>
  assume-console [
    type <span class="Constant">[1]</span>
  ]
  run [
    editor-event-loop screen, console, e
  ]
  screen-should-contain [
   <span class="Constant"> .          .</span>
   <span class="Constant"> .  abc1    .</span>
<span class="Constant">    .╌╌╌╌╌╌╌╌╌╌.</span>
   <span class="Constant"> .          .</span>
  ]
]

<span class="Comment"># redo typing</span>

<span class="muScenario">scenario</span> editor-redo-typing [
  <span class="Constant">local-scope</span>
  <span class="Comment"># create an editor, type something, undo</span>
  assume-screen <span class="Constant">10/width</span>, <span class="Constant">5/height</span>
  e:&amp;:editor <span class="Special">&lt;-</span> new-editor <span class="Constant">[a]</span>, screen, <span class="Constant">0/left</span>, <span class="Constant">10/right</span>
  editor-render screen, e
  assume-console [
    type <span class="Constant">[012]</span>
    press ctrl-z
  ]
  editor-event-loop screen, console, e
  screen-should-contain [
   <span class="Constant"> .          .</span>
   <span class="Constant"> .a         .</span>
<span class="Constant">    .╌╌╌╌╌╌╌╌╌╌.</span>
   <span class="Constant"> .          .</span>
  ]
  <span class="Comment"># redo</span>
  assume-console [
    press ctrl-y
  ]
  run [
    editor-event-loop screen, console, e
  ]
  <span class="Comment"># all characters must be back</span>
  screen-should-contain [
   <span class="Constant"> .          .</span>
   <span class="Constant"> .012a      .</span>
<span class="Constant">    .╌╌╌╌╌╌╌╌╌╌.</span>
   <span class="Constant"> .          .</span>
  ]
  <span class="Comment"># cursor should be in the right place</span>
  assume-console [
    type <span class="Constant">[3]</span>
  ]
  run [
    editor-event-loop screen, console, e
  ]
  screen-should-contain [
   <span class="Constant"> .          .</span>
   <span class="Constant"> .0123a     .</span>
<span class="Constant">    .╌╌╌╌╌╌╌╌╌╌.</span>
   <span class="Constant"> .          .</span>
  ]
]

<span class="muRecipe">after</span> <span class="Constant">&lt;handle-redo&gt;</span> [
  <span class="Delimiter">{</span>
    typing:insert-operation, is-insert?:bool <span class="Special">&lt;-</span> maybe-convert *op, <span class="Constant">typing:variant</span>
    <span class="muControl">break-unless</span> is-insert?
    before-cursor <span class="Special">&lt;-</span> get *editor, <span class="Constant">before-cursor:offset</span>
    insert-from:&amp;:duplex-list:char <span class="Special">&lt;-</span> get typing, <span class="Constant">insert-from:offset</span>  <span class="Comment"># ignore insert-to because it's already been spliced away</span>
    <span class="Comment"># assert insert-to matches next(before-cursor)</span>
    insert-range before-cursor, insert-from
    <span class="Comment"># assert cursor-row/cursor-column/top-of-screen match after-row/after-column/after-top-of-screen</span>
    cursor-row <span class="Special">&lt;-</span> get typing, <span class="Constant">after-row:offset</span>
    *editor <span class="Special">&lt;-</span> put *editor, <span class="Constant">cursor-row:offset</span>, cursor-row
    cursor-column <span class="Special">&lt;-</span> get typing, <span class="Constant">after-column:offset</span>
    *editor <span class="Special">&lt;-</span> put *editor, <span class="Constant">cursor-column:offset</span>, cursor-column
    top:&amp;:duplex-list:char <span class="Special">&lt;-</span> get typing, <span class="Constant">after-top-of-screen:offset</span>
    *editor <span class="Special">&lt;-</span> put *editor, <span class="Constant">top-of-screen:offset</span>, top
  <span class="Delimiter">}</span>
]

<span class="muScenario">scenario</span> editor-redo-typing-empty [
  <span class="Constant">local-scope</span>
  <span class="Comment"># create an editor, type something, undo</span>
  assume-screen <span class="Constant">10/width</span>, <span class="Constant">5/height</span>
  e:&amp;:editor <span class="Special">&lt;-</span> new-editor <span class="Constant">[]</span>, screen, <span class="Constant">0/left</span>, <span class="Constant">10/right</span>
  editor-render screen, e
  assume-console [
    type <span class="Constant">[012]</span>
    press ctrl-z
  ]
  editor-event-loop screen, console, e
  screen-should-contain [
   <span class="Constant"> .          .</span>
   <span class="Constant"> .          .</span>
<span class="Constant">    .╌╌╌╌╌╌╌╌╌╌.</span>
   <span class="Constant"> .          .</span>
  ]
  <span class="Comment"># redo</span>
  assume-console [
    press ctrl-y
  ]
  run [
    editor-event-loop screen, console, e
  ]
  <span class="Comment"># all characters must be back</span>
  screen-should-contain [
   <span class="Constant"> .          .</span>
   <span class="Constant"> .012       .</span>
<span class="Constant">    .╌╌╌╌╌╌╌╌╌╌.</span>
   <span class="Constant"> .          .</span>
  ]
  <span class="Comment"># cursor should be in the right place</span>
  assume-console [
    type <span class="Constant">[3]</span>
  ]
  run [
    editor-event-loop screen, console, e
  ]
  screen-should-contain [
   <span class="Constant"> .          .</span>
   <span class="Constant"> .0123      .</span>
<span class="Constant">    .╌╌╌╌╌╌╌╌╌╌.</span>
   <span class="Constant"> .          .</span>
  ]
]

<span class="muScenario">scenario</span> editor-work-clears-redo-stack [
  <span class="Constant">local-scope</span>
  <span class="Comment"># create an editor with some text, do some work, undo</span>
  assume-screen <span class="Constant">10/width</span>, <span class="Constant">5/height</span>
  contents:text <span class="Special">&lt;-</span> new <span class="Constant">[abc</span>
<span class="Constant">def</span>
<span class="Constant">ghi]</span>
  e:&amp;:editor <span class="Special">&lt;-</span> new-editor contents, screen, <span class="Constant">0/left</span>, <span class="Constant">10/right</span>
  editor-render screen, e
  assume-console [
    type <span class="Constant">[1]</span>
    press ctrl-z
  ]
  editor-event-loop screen, console, e
  <span class="Comment"># do some more work</span>
  assume-console [
    type <span class="Constant">[0]</span>
  ]
  editor-event-loop screen, console, e
  screen-should-contain [
   <span class="Constant"> .          .</span>
   <span class="Constant"> .0abc      .</span>
   <span class="Constant"> .def       .</span>
   <span class="Constant"> .ghi       .</span>
<span class="Constant">    .╌╌╌╌╌╌╌╌╌╌.</span>
  ]
  <span class="Comment"># redo</span>
  assume-console [
    press ctrl-y
  ]
  run [
    editor-event-loop screen, console, e
  ]
  <span class="Comment"># nothing should happen</span>
  screen-should-contain [
   <span class="Constant"> .          .</span>
   <span class="Constant"> .0abc      .</span>
   <span class="Constant"> .def       .</span>
   <span class="Constant"> .ghi       .</span>
<span class="Constant">    .╌╌╌╌╌╌╌╌╌╌.</span>
  ]
]

<span class="muScenario">scenario</span> editor-can-redo-typing-and-enter-and-tab [
  <span class="Constant">local-scope</span>
  <span class="Comment"># create an editor</span>
  assume-screen <span class="Constant">10/width</span>, <span class="Constant">5/height</span>
  e:&amp;:editor <span class="Special">&lt;-</span> new-editor <span class="Constant">[]</span>, screen, <span class="Constant">0/left</span>, <span class="Constant">10/right</span>
  editor-render screen, e
  <span class="Comment"># insert some text and tabs, hit enter, some more text and tabs</span>
  assume-console [
    press tab
    type <span class="Constant">[ab]</span>
    press tab
    type <span class="Constant">[cd]</span>
    press enter
    press tab
    type <span class="Constant">[efg]</span>
  ]
  editor-event-loop screen, console, e
  screen-should-contain [
   <span class="Constant"> .          .</span>
   <span class="Constant"> .  ab  cd  .</span>
   <span class="Constant"> .    efg   .</span>
<span class="Constant">    .╌╌╌╌╌╌╌╌╌╌.</span>
   <span class="Constant"> .          .</span>
  ]
  3:num/<span class="Special">raw</span> <span class="Special">&lt;-</span> get *e, <span class="Constant">cursor-row:offset</span>
  4:num/<span class="Special">raw</span> <span class="Special">&lt;-</span> get *e, <span class="Constant">cursor-column:offset</span>
  memory-should-contain [
   <span class="Constant"> 3</span> <span class="Special">&lt;-</span><span class="Constant"> 2</span>
   <span class="Constant"> 4</span> <span class="Special">&lt;-</span><span class="Constant"> 7</span>
  ]
  <span class="Comment"># undo</span>
  assume-console [
    press ctrl-z
  ]
  run [
    editor-event-loop screen, console, e
  ]
  <span class="Comment"># typing in second line deleted, but not indent</span>
  3:num/<span class="Special">raw</span> <span class="Special">&lt;-</span> get *e, <span class="Constant">cursor-row:offset</span>
  4:num/<span class="Special">raw</span> <span class="Special">&lt;-</span> get *e, <span class="Constant">cursor-column:offset</span>
  memory-should-contain [
   <span class="Constant"> 3</span> <span class="Special">&lt;-</span><span class="Constant"> 2</span>
   <span class="Constant"> 4</span> <span class="Special">&lt;-</span><span class="Constant"> 2</span>
  ]
  screen-should-contain [
   <span class="Constant"> .          .</span>
   <span class="Constant"> .  ab  cd  .</span>
   <span class="Constant"> .          .</span>
<span class="Constant">    .╌╌╌╌╌╌╌╌╌╌.</span>
   <span class="Constant"> .          .</span>
  ]
  <span class="Comment"># undo again</span>
  assume-console [
    press ctrl-z
  ]
  run [
    editor-event-loop screen, console, e
  ]
  <span class="Comment"># indent and newline deleted</span>
  3:num/<span class="Special">raw</span> <span class="Special">&lt;-</span> get *e, <span class="Constant">cursor-row:offset</span>
  4:num/<span class="Special">raw</span> <span class="Special">&lt;-</span> get *e, <span class="Constant">cursor-column:offset</span>
  memory-should-contain [
   <span class="Constant"> 3</span> <span class="Special">&lt;-</span><span class="Constant"> 1</span>
   <span class="Constant"> 4</span> <span class="Special">&lt;-</span><span class="Constant"> 8</span>
  ]
  screen-should-contain [
   <span class="Constant"> .          .</span>
   <span class="Constant"> .  ab  cd  .</span>
<span class="Constant">    .╌╌╌╌╌╌╌╌╌╌.</span>
   <span class="Constant"> .          .</span>
  ]
  <span class="Comment"># undo again</span>
  assume-console [
    press ctrl-z
  ]
  run [
    editor-event-loop screen, console, e
  ]
  <span class="Comment"># empty screen</span>
  3:num/<span class="Special">raw</span> <span class="Special">&lt;-</span> get *e, <span class="Constant">cursor-row:offset</span>
  4:num/<span class="Special">raw</span> <span class="Special">&lt;-</span> get *e, <span class="Constant">cursor-column:offset</span>
  memory-should-contain [
   <span class="Constant"> 3</span> <span class="Special">&lt;-</span><span class="Constant"> 1</span>
   <span class="Constant"> 4</span> <span class="Special">&lt;-</span><span class="Constant"> 0</span>
  ]
  screen-should-contain [
   <span class="Constant"> .          .</span>
   <span class="Constant"> .          .</span>
<span class="Constant">    .╌╌╌╌╌╌╌╌╌╌.</span>
   <span class="Constant"> .          .</span>
  ]
  <span class="Comment"># redo</span>
  assume-console [
    press ctrl-y
  ]
  run [
    editor-event-loop screen, console, e
  ]
  <span class="Comment"># first line inserted</span>
  3:num/<span class="Special">raw</span> <span class="Special">&lt;-</span> get *e, <span class="Constant">cursor-row:offset</span>
  4:num/<span class="Special">raw</span> <span class="Special">&lt;-</span> get *e, <span class="Constant">cursor-column:offset</span>
  memory-should-contain [
   <span class="Constant"> 3</span> <span class="Special">&lt;-</span><span class="Constant"> 1</span>
   <span class="Constant"> 4</span> <span class="Special">&lt;-</span><span class="Constant"> 8</span>
  ]
  screen-should-contain [
   <span class="Constant"> .          .</span>
   <span class="Constant"> .  ab  cd  .</span>
<span class="Constant">    .╌╌╌╌╌╌╌╌╌╌.</span>
   <span class="Constant"> .          .</span>
  ]
  <span class="Comment"># redo again</span>
  assume-console [
    press ctrl-y
  ]
  run [
    editor-event-loop screen, console, e
  ]
  <span class="Comment"># newline and indent inserted</span>
  3:num/<span class="Special">raw</span> <span class="Special">&lt;-</span> get *e, <span class="Constant">cursor-row:offset</span>
  4:num/<span class="Special">raw</span> <span class="Special">&lt;-</span> get *e, <span class="Constant">cursor-column:offset</span>
  memory-should-contain [
   <span class="Constant"> 3</span> <span class="Special">&lt;-</span><span class="Constant"> 2</span>
   <span class="Constant"> 4</span> <span class="Special">&lt;-</span><span class="Constant"> 2</span>
  ]
  screen-should-contain [
   <span class="Constant"> .          .</span>
   <span class="Constant"> .  ab  cd  .</span>
   <span class="Constant"> .          .</span>
<span class="Constant">    .╌╌╌╌╌╌╌╌╌╌.</span>
   <span class="Constant"> .          .</span>
  ]
  <span class="Comment"># redo again</span>
  assume-console [
    press ctrl-y
  ]
  run [
    editor-event-loop screen, console, e
  ]
  <span class="Comment"># indent and newline deleted</span>
  3:num/<span class="Special">raw</span> <span class="Special">&lt;-</span> get *e, <span class="Constant">cursor-row:offset</span>
  4:num/<span class="Special">raw</span> <span class="Special">&lt;-</span> get *e, <span class="Constant">cursor-column:offset</span>
  memory-should-contain [
   <span class="Constant"> 3</span> <span class="Special">&lt;-</span><span class="Constant"> 2</span>
   <span class="Constant"> 4</span> <span class="Special">&lt;-</span><span class="Constant"> 7</span>
  ]
  screen-should-contain [
   <span class="Constant"> .          .</span>
   <span class="Constant"> .  ab  cd  .</span>
   <span class="Constant"> .    efg   .</span>
<span class="Constant">    .╌╌╌╌╌╌╌╌╌╌.</span>
   <span class="Constant"> .          .</span>
  ]
]

<span class="Comment"># undo cursor movement and scroll</span>

<span class="muScenario">scenario</span> editor-can-undo-touch [
  <span class="Constant">local-scope</span>
  <span class="Comment"># create an editor with some text</span>
  assume-screen <span class="Constant">10/width</span>, <span class="Constant">5/height</span>
  contents:text <span class="Special">&lt;-</span> new <span class="Constant">[abc</span>
<span class="Constant">def</span>
<span class="Constant">ghi]</span>
  e:&amp;:editor <span class="Special">&lt;-</span> new-editor contents, screen, <span class="Constant">0/left</span>, <span class="Constant">10/right</span>
  editor-render screen, e
  <span class="Comment"># move the cursor</span>
  assume-console [
    left-click<span class="Constant"> 3</span>,<span class="Constant"> 1</span>
  ]
  editor-event-loop screen, console, e
  <span class="Comment"># undo</span>
  assume-console [
    press ctrl-z
  ]
  run [
    editor-event-loop screen, console, e
  ]
  <span class="Comment"># click undone</span>
  3:num/<span class="Special">raw</span> <span class="Special">&lt;-</span> get *e, <span class="Constant">cursor-row:offset</span>
  4:num/<span class="Special">raw</span> <span class="Special">&lt;-</span> get *e, <span class="Constant">cursor-column:offset</span>
  memory-should-contain [
   <span class="Constant"> 3</span> <span class="Special">&lt;-</span><span class="Constant"> 1</span>
   <span class="Constant"> 4</span> <span class="Special">&lt;-</span><span class="Constant"> 0</span>
  ]
  <span class="Comment"># cursor should be in the right place</span>
  assume-console [
    type <span class="Constant">[1]</span>
  ]
  run [
    editor-event-loop screen, console, e
  ]
  screen-should-contain [
   <span class="Constant"> .          .</span>
   <span class="Constant"> .1abc      .</span>
   <span class="Constant"> .def       .</span>
   <span class="Constant"> .ghi       .</span>
<span class="Constant">    .╌╌╌╌╌╌╌╌╌╌.</span>
  ]
]

<span class="muRecipe">after</span> <span class="Constant">&lt;move-cursor-begin&gt;</span> [
  cursor-row-before:num <span class="Special">&lt;-</span> get *editor, <span class="Constant">cursor-row:offset</span>
  cursor-column-before:num <span class="Special">&lt;-</span> get *editor, <span class="Constant">cursor-column:offset</span>
  top-before:&amp;:duplex-list:char <span class="Special">&lt;-</span> get *editor, <span class="Constant">top-of-screen:offset</span>
]
<span class="muRecipe">before</span> <span class="Constant">&lt;move-cursor-end&gt;</span> [
  top-after:&amp;:duplex-list:char <span class="Special">&lt;-</span> get *editor, <span class="Constant">top-of-screen:offset</span>
  cursor-row:num <span class="Special">&lt;-</span> get *editor, <span class="Constant">cursor-row:offset</span>
  cursor-column:num <span class="Special">&lt;-</span> get *editor, <span class="Constant">cursor-column:offset</span>
  <span class="Delimiter">{</span>
    <span class="muControl">break-unless</span> undo-coalesce-tag
    <span class="Comment"># if previous operation was also a move, and also had the same coalesce</span>
    <span class="Comment"># tag, coalesce with it</span>
    undo:&amp;:list:&amp;:operation <span class="Special">&lt;-</span> get *editor, <span class="Constant">undo:offset</span>
    <span class="muControl">break-unless</span> undo
    op:&amp;:operation <span class="Special">&lt;-</span> first undo
    move:move-operation, is-move?:bool <span class="Special">&lt;-</span> maybe-convert *op, <span class="Constant">move:variant</span>
    <span class="muControl">break-unless</span> is-move?
    previous-coalesce-tag:num <span class="Special">&lt;-</span> get move, <span class="Constant">tag:offset</span>
    coalesce?:bool <span class="Special">&lt;-</span> equal undo-coalesce-tag, previous-coalesce-tag
    <span class="muControl">break-unless</span> coalesce?
    move <span class="Special">&lt;-</span> put move, <span class="Constant">after-row:offset</span>, cursor-row
    move <span class="Special">&lt;-</span> put move, <span class="Constant">after-column:offset</span>, cursor-column
    move <span class="Special">&lt;-</span> put move, <span class="Constant">after-top-of-screen:offset</span>, top-after
    *op <span class="Special">&lt;-</span> merge <span class="Constant">1/move-operation</span>, move
    <span class="muControl">break</span> <span class="Constant">+done-adding-move-operation</span>
  <span class="Delimiter">}</span>
  op:&amp;:operation <span class="Special">&lt;-</span> new <span class="Constant">operation:type</span>
  *op <span class="Special">&lt;-</span> merge <span class="Constant">1/move-operation</span>, cursor-row-before, cursor-column-before, top-before, cursor-row/after, cursor-column/after, top-after, undo-coalesce-tag
  editor <span class="Special">&lt;-</span> add-operation editor, op
<span class="Constant">  +done-adding-move-operation</span>
]

<span class="muRecipe">after</span> <span class="Constant">&lt;handle-undo&gt;</span> [
  <span class="Delimiter">{</span>
    move:move-operation, is-move?:bool <span class="Special">&lt;-</span> maybe-convert *op, <span class="Constant">move:variant</span>
    <span class="muControl">break-unless</span> is-move?
    <span class="Comment"># assert cursor-row/cursor-column/top-of-screen match after-row/after-column/after-top-of-screen</span>
    cursor-row <span class="Special">&lt;-</span> get move, <span class="Constant">before-row:offset</span>
    *editor <span class="Special">&lt;-</span> put *editor, <span class="Constant">cursor-row:offset</span>, cursor-row
    cursor-column <span class="Special">&lt;-</span> get move, <span class="Constant">before-column:offset</span>
    *editor <span class="Special">&lt;-</span> put *editor, <span class="Constant">cursor-column:offset</span>, cursor-column
    top:&amp;:duplex-list:char <span class="Special">&lt;-</span> get move, <span class="Constant">before-top-of-screen:offset</span>
    *editor <span class="Special">&lt;-</span> put *editor, <span class="Constant">top-of-screen:offset</span>, top
  <span class="Delimiter">}</span>
]

<span class="muScenario">scenario</span> editor-can-undo-scroll [
  <span class="Constant">local-scope</span>
  <span class="Comment"># screen has 1 line for menu + 3 lines</span>
  assume-screen <span class="Constant">5/width</span>, <span class="Constant">4/height</span>
  <span class="Comment"># editor contains a wrapped line</span>
  contents:text <span class="Special">&lt;-</span> new <span class="Constant">[a</span>
<span class="Constant">b</span>
<span class="Constant">cdefgh]</span>
  e:&amp;:editor <span class="Special">&lt;-</span> new-editor contents, screen, <span class="Constant">0/left</span>, <span class="Constant">5/right</span>
  <span class="Comment"># position cursor at end of screen and try to move right</span>
  assume-console [
    left-click<span class="Constant"> 3</span>,<span class="Constant"> 3</span>
    press right-arrow
  ]
  editor-event-loop screen, console, e
  3:num/<span class="Special">raw</span> <span class="Special">&lt;-</span> get *e, <span class="Constant">cursor-row:offset</span>
  4:num/<span class="Special">raw</span> <span class="Special">&lt;-</span> get *e, <span class="Constant">cursor-column:offset</span>
  <span class="Comment"># screen scrolls</span>
  screen-should-contain [
   <span class="Constant"> .     .</span>
   <span class="Constant"> .b    .</span>
<span class="Constant">    .cdef↩.</span>
   <span class="Constant"> .gh   .</span>
  ]
  memory-should-contain [
   <span class="Constant"> 3</span> <span class="Special">&lt;-</span><span class="Constant"> 3</span>
   <span class="Constant"> 4</span> <span class="Special">&lt;-</span><span class="Constant"> 0</span>
  ]
  <span class="Comment"># undo</span>
  assume-console [
    press ctrl-z
  ]
  run [
    editor-event-loop screen, console, e
  ]
  <span class="Comment"># cursor moved back</span>
  3:num/<span class="Special">raw</span> <span class="Special">&lt;-</span> get *e, <span class="Constant">cursor-row:offset</span>
  4:num/<span class="Special">raw</span> <span class="Special">&lt;-</span> get *e, <span class="Constant">cursor-column:offset</span>
  memory-should-contain [
   <span class="Constant"> 3</span> <span class="Special">&lt;-</span><span class="Constant"> 3</span>
   <span class="Constant"> 4</span> <span class="Special">&lt;-</span><span class="Constant"> 3</span>
  ]
  <span class="Comment"># scroll undone</span>
  screen-should-contain [
   <span class="Constant"> .     .</span>
   <span class="Constant"> .a    .</span>
   <span class="Constant"> .b    .</span>
<span class="Constant">    .cdef↩.</span>
  ]
  <span class="Comment"># cursor should be in the right place</span>
  assume-console [
    type <span class="Constant">[1]</span>
  ]
  run [
    editor-event-loop screen, console, e
  ]
  screen-should-contain [
   <span class="Constant"> .     .</span>
   <span class="Constant"> .b    .</span>
<span class="Constant">    .cde1↩.</span>
   <span class="Constant"> .fgh  .</span>
  ]
]

<span class="muScenario">scenario</span> editor-can-undo-left-arrow [
  <span class="Constant">local-scope</span>
  <span class="Comment"># create an editor with some text</span>
  assume-screen <span class="Constant">10/width</span>, <span class="Constant">5/height</span>
  contents:text <span class="Special">&lt;-</span> new <span class="Constant">[abc</span>
<span class="Constant">def</span>
<span class="Constant">ghi]</span>
  e:&amp;:editor <span class="Special">&lt;-</span> new-editor contents, screen, <span class="Constant">0/left</span>, <span class="Constant">10/right</span>
  editor-render screen, e
  <span class="Comment"># move the cursor</span>
  assume-console [
    left-click<span class="Constant"> 3</span>,<span class="Constant"> 1</span>
    press left-arrow
  ]
  editor-event-loop screen, console, e
  <span class="Comment"># undo</span>
  assume-console [
    press ctrl-z
  ]
  run [
    editor-event-loop screen, console, e
  ]
  <span class="Comment"># cursor moves back</span>
  3:num/<span class="Special">raw</span> <span class="Special">&lt;-</span> get *e, <span class="Constant">cursor-row:offset</span>
  4:num/<span class="Special">raw</span> <span class="Special">&lt;-</span> get *e, <span class="Constant">cursor-column:offset</span>
  memory-should-contain [
   <span class="Constant"> 3</span> <span class="Special">&lt;-</span><span class="Constant"> 3</span>
   <span class="Constant"> 4</span> <span class="Special">&lt;-</span><span class="Constant"> 1</span>
  ]
  <span class="Comment"># cursor should be in the right place</span>
  assume-console [
    type <span class="Constant">[1]</span>
  ]
  run [
    editor-event-loop screen, console, e
  ]
  screen-should-contain [
   <span class="Constant"> .          .</span>
   <span class="Constant"> .abc       .</span>
   <span class="Constant"> .def       .</span>
   <span class="Constant"> .g1hi      .</span>
<span class="Constant">    .╌╌╌╌╌╌╌╌╌╌.</span>
  ]
]

<span class="muScenario">scenario</span> editor-can-undo-up-arrow [
  <span class="Constant">local-scope</span>
  <span class="Comment"># create an editor with some text</span>
  assume-screen <span class="Constant">10/width</span>, <span class="Constant">5/height</span>
  contents:text <span class="Special">&lt;-</span> new <span class="Constant">[abc</span>
<span class="Constant">def</span>
<span class="Constant">ghi]</span>
  e:&amp;:editor <span class="Special">&lt;-</span> new-editor contents, screen, <span class="Constant">0/left</span>, <span class="Constant">10/right</span>
  editor-render screen, e
  <span class="Comment"># move the cursor</span>
  assume-console [
    left-click<span class="Constant"> 3</span>,<span class="Constant"> 1</span>
    press up-arrow
  ]
  editor-event-loop screen, console, e
  3:num/<span class="Special">raw</span> <span class="Special">&lt;-</span> get *e, <span class="Constant">cursor-row:offset</span>
  4:num/<span class="Special">raw</span> <span class="Special">&lt;-</span> get *e, <span class="Constant">cursor-column:offset</span>
  memory-should-contain [
   <span class="Constant"> 3</span> <span class="Special">&lt;-</span><span class="Constant"> 2</span>
   <span class="Constant"> 4</span> <span class="Special">&lt;-</span><span class="Constant"> 1</span>
  ]
  <span class="Comment"># undo</span>
  assume-console [
    press ctrl-z
  ]
  run [
    editor-event-loop screen, console, e
  ]
  <span class="Comment"># cursor moves back</span>
  3:num/<span class="Special">raw</span> <span class="Special">&lt;-</span> get *e, <span class="Constant">cursor-row:offset</span>
  4:num/<span class="Special">raw</span> <span class="Special">&lt;-</span> get *e, <span class="Constant">cursor-column:offset</span>
  memory-should-contain [
   <span class="Constant"> 3</span> <span class="Special">&lt;-</span><span class="Constant"> 3</span>
   <span class="Constant"> 4</span> <span class="Special">&lt;-</span><span class="Constant"> 1</span>
  ]
  <span class="Comment"># cursor should be in the right place</span>
  assume-console [
    type <span class="Constant">[1]</span>
  ]
  run [
    editor-event-loop screen, console, e
  ]
  screen-should-contain [
   <span class="Constant"> .          .</span>
   <span class="Constant"> .abc       .</span>
   <span class="Constant"> .def       .</span>
   <span class="Constant"> .g1hi      .</span>
<span class="Constant">    .╌╌╌╌╌╌╌╌╌╌.</span>
  ]
]

<span class="muScenario">scenario</span> editor-can-undo-down-arrow [
  <span class="Constant">local-scope</span>
  <span class="Comment"># create an editor with some text</span>
  assume-screen <span class="Constant">10/width</span>, <span class="Constant">5/height</span>
  contents:text <span class="Special">&lt;-</span> new <span class="Constant">[abc</span>
<span class="Constant">def</span>
<span class="Constant">ghi]</span>
  e:&amp;:editor <span class="Special">&lt;-</span> new-editor contents, screen, <span class="Constant">0/left</span>, <span class="Constant">10/right</span>
  editor-render screen, e
  <span class="Comment"># move the cursor</span>
  assume-console [
    left-click<span class="Constant"> 2</span>,<span class="Constant"> 1</span>
    press down-arrow
  ]
  editor-event-loop screen, console, e
  <span class="Comment"># undo</span>
  assume-console [
    press ctrl-z
  ]
  run [
    editor-event-loop screen, console, e
  ]
  <span class="Comment"># cursor moves back</span>
  3:num/<span class="Special">raw</span> <span class="Special">&lt;-</span> get *e, <span class="Constant">cursor-row:offset</span>
  4:num/<span class="Special">raw</span> <span class="Special">&lt;-</span> get *e, <span class="Constant">cursor-column:offset</span>
  memory-should-contain [
   <span class="Constant"> 3</span> <span class="Special">&lt;-</span><span class="Constant"> 2</span>
   <span class="Constant"> 4</span> <span class="Special">&lt;-</span><span class="Constant"> 1</span>
  ]
  <span class="Comment"># cursor should be in the right place</span>
  assume-console [
    type <span class="Constant">[1]</span>
  ]
  run [
    editor-event-loop screen, console, e
  ]
  screen-should-contain [
   <span class="Constant"> .          .</span>
   <span class="Constant"> .abc       .</span>
   <span class="Constant"> .d1ef      .</span>
   <span class="Constant"> .ghi       .</span>
<span class="Constant">    .╌╌╌╌╌╌╌╌╌╌.</span>
  ]
]

<span class="muScenario">scenario</span> editor-can-undo-ctrl-f [
  <span class="Constant">local-scope</span>
  <span class="Comment"># create an editor with multiple pages of text</span>
  assume-screen <span class="Constant">10/width</span>, <span class="Constant">5/height</span>
  contents:text <span class="Special">&lt;-</span> new <span class="Constant">[a</span>
<span class="Constant">b</span>
<span class="Constant">c</span>
<span class="Constant">d</span>
<span class="Constant">e</span>
<span class="Constant">f]</span>
  e:&amp;:editor <span class="Special">&lt;-</span> new-editor contents, screen, <span class="Constant">0/left</span>, <span class="Constant">10/right</span>
  editor-render screen, e
  <span class="Comment"># scroll the page</span>
  assume-console [
    press ctrl-f
  ]
  editor-event-loop screen, console, e
  <span class="Comment"># undo</span>
  assume-console [
    press ctrl-z
  ]
  run [
    editor-event-loop screen, console, e
  ]
  <span class="Comment"># screen should again show page 1</span>
  screen-should-contain [
   <span class="Constant"> .          .</span>
   <span class="Constant"> .a         .</span>
   <span class="Constant"> .b         .</span>
   <span class="Constant"> .c         .</span>
   <span class="Constant"> .d         .</span>
  ]
]

<span class="muScenario">scenario</span> editor-can-undo-page-down [
  <span class="Constant">local-scope</span>
  <span class="Comment"># create an editor with multiple pages of text</span>
  assume-screen <span class="Constant">10/width</span>, <span class="Constant">5/height</span>
  contents:text <span class="Special">&lt;-</span> new <span class="Constant">[a</span>
<span class="Constant">b</span>
<span class="Constant">c</span>
<span class="Constant">d</span>
<span class="Constant">e</span>
<span class="Constant">f]</span>
  e:&amp;:editor <span class="Special">&lt;-</span> new-editor contents, screen, <span class="Constant">0/left</span>, <span class="Constant">10/right</span>
  editor-render screen, e
  <span class="Comment"># scroll the page</span>
  assume-console [
    press page-down
  ]
  editor-event-loop screen, console, e
  <span class="Comment"># undo</span>
  assume-console [
    press ctrl-z
  ]
  run [
    editor-event-loop screen, console, e
  ]
  <span class="Comment"># screen should again show page 1</span>
  screen-should-contain [
   <span class="Constant"> .          .</span>
   <span class="Constant"> .a         .</span>
   <span class="Constant"> .b         .</span>
   <span class="Constant"> .c         .</span>
   <span class="Constant"> .d         .</span>
  ]
]

<span class="muScenario">scenario</span> editor-can-undo-ctrl-b [
  <span class="Constant">local-scope</span>
  <span class="Comment"># create an editor with multiple pages of text</span>
  assume-screen <span class="Constant">10/width</span>, <span class="Constant">5/height</span>
  contents:text <span class="Special">&lt;-</span> new <span class="Constant">[a</span>
<span class="Constant">b</span>
<span class="Constant">c</span>
<span class="Constant">d</span>
<span class="Constant">e</span>
<span class="Constant">f]</span>
  e:&amp;:editor <span class="Special">&lt;-</span> new-editor contents, screen, <span class="Constant">0/left</span>, <span class="Constant">10/right</span>
  editor-render screen, e
  <span class="Comment"># scroll the page down and up</span>
  assume-console [
    press page-down
    press ctrl-b
  ]
  editor-event-loop screen, console, e
  <span class="Comment"># undo</span>
  assume-console [
    press ctrl-z
  ]
  run [
    editor-event-loop screen, console, e
  ]
  <span class="Comment"># screen should again show page 2</span>
  screen-should-contain [
   <span class="Constant"> .          .</span>
   <span class="Constant"> .d         .</span>
   <span class="Constant"> .e         .</span>
   <span class="Constant"> .f         .</span>
<span class="Constant">    .╌╌╌╌╌╌╌╌╌╌.</span>
  ]
]

<span class="muScenario">scenario</span> editor-can-undo-page-up [
  <span class="Constant">local-scope</span>
  <span class="Comment"># create an editor with multiple pages of text</span>
  assume-screen <span class="Constant">10/width</span>, <span class="Constant">5/height</span>
  contents:text <span class="Special">&lt;-</span> new <span class="Constant">[a</span>
<span class="Constant">b</span>
<span class="Constant">c</span>
<span class="Constant">d</span>
<span class="Constant">e</span>
<span class="Constant">f]</span>
  e:&amp;:editor <span class="Special">&lt;-</span> new-editor contents, screen, <span class="Constant">0/left</span>, <span class="Constant">10/right</span>
  editor-render screen, e
  <span class="Comment"># scroll the page down and up</span>
  assume-console [
    press page-down
    press page-up
  ]
  editor-event-loop screen, console, e
  <span class="Comment"># undo</span>
  assume-console [
    press ctrl-z
  ]
  run [
    editor-event-loop screen, console, e
  ]
  <span class="Comment"># screen should again show page 2</span>
  screen-should-contain [
   <span class="Constant"> .          .</span>
   <span class="Constant"> .d         .</span>
   <span class="Constant"> .e         .</span>
   <span class="Constant"> .f         .</span>
<span class="Constant">    .╌╌╌╌╌╌╌╌╌╌.</span>
  ]
]

<span class="muScenario">scenario</span> editor-can-undo-ctrl-a [
  <span class="Constant">local-scope</span>
  <span class="Comment"># create an editor with some text</span>
  assume-screen <span class="Constant">10/width</span>, <span class="Constant">5/height</span>
  contents:text <span class="Special">&lt;-</span> new <span class="Constant">[abc</span>
<span class="Constant">def</span>
<span class="Constant">ghi]</span>
  e:&amp;:editor <span class="Special">&lt;-</span> new-editor contents, screen, <span class="Constant">0/left</span>, <span class="Constant">10/right</span>
  editor-render screen, e
  <span class="Comment"># move the cursor, then to start of line</span>
  assume-console [
    left-click<span class="Constant"> 2</span>,<span class="Constant"> 1</span>
    press ctrl-a
  ]
  editor-event-loop screen, console, e
  <span class="Comment"># undo</span>
  assume-console [
    press ctrl-z
  ]
  run [
    editor-event-loop screen, console, e
  ]
  <span class="Comment"># cursor moves back</span>
  3:num/<span class="Special">raw</span> <span class="Special">&lt;-</span> get *e, <span class="Constant">cursor-row:offset</span>
  4:num/<span class="Special">raw</span> <span class="Special">&lt;-</span> get *e, <span class="Constant">cursor-column:offset</span>
  memory-should-contain [
   <span class="Constant"> 3</span> <span class="Special">&lt;-</span><span class="Constant"> 2</span>
   <span class="Constant"> 4</span> <span class="Special">&lt;-</span><span class="Constant"> 1</span>
  ]
  <span class="Comment"># cursor should be in the right place</span>
  assume-console [
    type <span class="Constant">[1]</span>
  ]
  run [
    editor-event-loop screen, console, e
  ]
  screen-should-contain [
   <span class="Constant"> .          .</span>
   <span class="Constant"> .abc       .</span>
   <span class="Constant"> .d1ef      .</span>
   <span class="Constant"> .ghi       .</span>
<span class="Constant">    .╌╌╌╌╌╌╌╌╌╌.</span>
  ]
]

<span class="muScenario">scenario</span> editor-can-undo-home [
  <span class="Constant">local-scope</span>
  <span class="Comment"># create an editor with some text</span>
  assume-screen <span class="Constant">10/width</span>, <span class="Constant">5/height</span>
  contents:text <span class="Special">&lt;-</span> new <span class="Constant">[abc</span>
<span class="Constant">def</span>
<span class="Constant">ghi]</span>
  e:&amp;:editor <span class="Special">&lt;-</span> new-editor contents, screen, <span class="Constant">0/left</span>, <span class="Constant">10/right</span>
  editor-render screen, e
  <span class="Comment"># move the cursor, then to start of line</span>
  assume-console [
    left-click<span class="Constant"> 2</span>,<span class="Constant"> 1</span>
    press home
  ]
  editor-event-loop screen, console, e
  <span class="Comment"># undo</span>
  assume-console [
    press ctrl-z
  ]
  run [
    editor-event-loop screen, console, e
  ]
  <span class="Comment"># cursor moves back</span>
  3:num/<span class="Special">raw</span> <span class="Special">&lt;-</span> get *e, <span class="Constant">cursor-row:offset</span>
  4:num/<span class="Special">raw</span> <span class="Special">&lt;-</span> get *e, <span class="Constant">cursor-column:offset</span>
  memory-should-contain [
   <span class="Constant"> 3</span> <span class="Special">&lt;-</span><span class="Constant"> 2</span>
   <span class="Constant"> 4</span> <span class="Special">&lt;-</span><span class="Constant"> 1</span>
  ]
  <span class="Comment"># cursor should be in the right place</span>
  assume-console [
    type <span class="Constant">[1]</span>
  ]
  run [
    editor-event-loop screen, console, e
  ]
  screen-should-contain [
   <span class="Constant"> .          .</span>
   <span class="Constant"> .abc       .</span>
   <span class="Constant"> .d1ef      .</span>
   <span class="Constant"> .ghi       .</span>
<span class="Constant">    .╌╌╌╌╌╌╌╌╌╌.</span>
  ]
]

<span class="muScenario">scenario</span> editor-can-undo-ctrl-e [
  <span class="Constant">local-scope</span>
  <span class="Comment"># create an editor with some text</span>
  assume-screen <span class="Constant">10/width</span>, <span class="Constant">5/height</span>
  contents:text <span class="Special">&lt;-</span> new <span class="Constant">[abc</span>
<span class="Constant">def</span>
<span class="Constant">ghi]</span>
  e:&amp;:editor <span class="Special">&lt;-</span> new-editor contents, screen, <span class="Constant">0/left</span>, <span class="Constant">10/right</span>
  editor-render screen, e
  <span class="Comment"># move the cursor, then to start of line</span>
  assume-console [
    left-click<span class="Constant"> 2</span>,<span class="Constant"> 1</span>
    press ctrl-e
  ]
  editor-event-loop screen, console, e
  <span class="Comment"># undo</span>
  assume-console [
    press ctrl-z
  ]
  run [
    editor-event-loop screen, console, e
  ]
  <span class="Comment"># cursor moves back</span>
  3:num/<span class="Special">raw</span> <span class="Special">&lt;-</span> get *e, <span class="Constant">cursor-row:offset</span>
  4:num/<span class="Special">raw</span> <span class="Special">&lt;-</span> get *e, <span class="Constant">cursor-column:offset</span>
  memory-should-contain [
   <span class="Constant"> 3</span> <span class="Special">&lt;-</span><span class="Constant"> 2</span>
   <span class="Constant"> 4</span> <span class="Special">&lt;-</span><span class="Constant"> 1</span>
  ]
  <span class="Comment"># cursor should be in the right place</span>
  assume-console [
    type <span class="Constant">[1]</span>
  ]
  run [
    editor-event-loop screen, console, e
  ]
  screen-should-contain [
   <span class="Constant"> .          .</span>
   <span class="Constant"> .abc       .</span>
   <span class="Constant"> .d1ef      .</span>
   <span class="Constant"> .ghi       .</span>
<span class="Constant">    .╌╌╌╌╌╌╌╌╌╌.</span>
  ]
]

<span class="muScenario">scenario</span> editor-can-undo-end [
  <span class="Constant">local-scope</span>
  <span class="Comment"># create an editor with some text</span>
  assume-screen <span class="Constant">10/width</span>, <span class="Constant">5/height</span>
  contents:text <span class="Special">&lt;-</span> new <span class="Constant">[abc</span>
<span class="Constant">def</span>
<span class="Constant">ghi]</span>
  e:&amp;:editor <span class="Special">&lt;-</span> new-editor contents, screen, <span class="Constant">0/left</span>, <span class="Constant">10/right</span>
  editor-render screen, e
  <span class="Comment"># move the cursor, then to start of line</span>
  assume-console [
    left-click<span class="Constant"> 2</span>,<span class="Constant"> 1</span>
    press end
  ]
  editor-event-loop screen, console, e
  <span class="Comment"># undo</span>
  assume-console [
    press ctrl-z
  ]
  run [
    editor-event-loop screen, console, e
  ]
  <span class="Comment"># cursor moves back</span>
  3:num/<span class="Special">raw</span> <span class="Special">&lt;-</span> get *e, <span class="Constant">cursor-row:offset</span>
  4:num/<span class="Special">raw</span> <span class="Special">&lt;-</span> get *e, <span class="Constant">cursor-column:offset</span>
  memory-should-contain [
   <span class="Constant"> 3</span> <span class="Special">&lt;-</span><span class="Constant"> 2</span>
   <span class="Constant"> 4</span> <span class="Special">&lt;-</span><span class="Constant"> 1</span>
  ]
  <span class="Comment"># cursor should be in the right place</span>
  assume-console [
    type <span class="Constant">[1]</span>
  ]
  run [
    editor-event-loop screen, console, e
  ]
  screen-should-contain [
   <span class="Constant"> .          .</span>
   <span class="Constant"> .abc       .</span>
   <span class="Constant"> .d1ef      .</span>
   <span class="Constant"> .ghi       .</span>
<span class="Constant">    .╌╌╌╌╌╌╌╌╌╌.</span>
  ]
]

<span class="muScenario">scenario</span> editor-can-undo-multiple-arrows-in-the-same-direction [
  <span class="Constant">local-scope</span>
  <span class="Comment"># create an editor with some text</span>
  assume-screen <span class="Constant">10/width</span>, <span class="Constant">5/height</span>
  contents:text <span class="Special">&lt;-</span> new <span class="Constant">[abc</span>
<span class="Constant">def</span>
<span class="Constant">ghi]</span>
  e:&amp;:editor <span class="Special">&lt;-</span> new-editor contents, screen, <span class="Constant">0/left</span>, <span class="Constant">10/right</span>
  editor-render screen, e
  <span class="Comment"># move the cursor</span>
  assume-console [
    left-click<span class="Constant"> 2</span>,<span class="Constant"> 1</span>
    press right-arrow
    press right-arrow
    press up-arrow
  ]
  editor-event-loop screen, console, e
  3:num/<span class="Special">raw</span> <span class="Special">&lt;-</span> get *e, <span class="Constant">cursor-row:offset</span>
  4:num/<span class="Special">raw</span> <span class="Special">&lt;-</span> get *e, <span class="Constant">cursor-column:offset</span>
  memory-should-contain [
   <span class="Constant"> 3</span> <span class="Special">&lt;-</span><span class="Constant"> 1</span>
   <span class="Constant"> 4</span> <span class="Special">&lt;-</span><span class="Constant"> 3</span>
  ]
  <span class="Comment"># undo</span>
  assume-console [
    press ctrl-z
  ]
  run [
    editor-event-loop screen, console, e
  ]
  <span class="Comment"># up-arrow is undone</span>
  3:num/<span class="Special">raw</span> <span class="Special">&lt;-</span> get *e, <span class="Constant">cursor-row:offset</span>
  4:num/<span class="Special">raw</span> <span class="Special">&lt;-</span> get *e, <span class="Constant">cursor-column:offset</span>
  memory-should-contain [
   <span class="Constant"> 3</span> <span class="Special">&lt;-</span><span class="Constant"> 2</span>
   <span class="Constant"> 4</span> <span class="Special">&lt;-</span><span class="Constant"> 3</span>
  ]
  <span class="Comment"># undo again</span>
  assume-console [
    press ctrl-z
  ]
  run [
    editor-event-loop screen, console, e
  ]
  <span class="Comment"># both right-arrows are undone</span>
  3:num/<span class="Special">raw</span> <span class="Special">&lt;-</span> get *e, <span class="Constant">cursor-row:offset</span>
  4:num/<span class="Special">raw</span> <span class="Special">&lt;-</span> get *e, <span class="Constant">cursor-column:offset</span>
  memory-should-contain [
   <span class="Constant"> 3</span> <span class="Special">&lt;-</span><span class="Constant"> 2</span>
   <span class="Constant"> 4</span> <span class="Special">&lt;-</span><span class="Constant"> 1</span>
  ]
]

<span class="Comment"># redo cursor movement and scroll</span>

<span class="muScenario">scenario</span> editor-redo-touch [
  <span class="Constant">local-scope</span>
  <span class="Comment"># create an editor with some text, click on a character, undo</span>
  assume-screen <span class="Constant">10/width</span>, <span class="Constant">5/height</span>
  contents:text <span class="Special">&lt;-</span> new <span class="Constant">[abc</span>
<span class="Constant">def</span>
<span class="Constant">ghi]</span>
  e:&amp;:editor <span class="Special">&lt;-</span> new-editor contents, screen, <span class="Constant">0/left</span>, <span class="Constant">10/right</span>
  editor-render screen, e
  assume-console [
    left-click<span class="Constant"> 3</span>,<span class="Constant"> 1</span>
    press ctrl-z
  ]
  editor-event-loop screen, console, e
  <span class="Comment"># redo</span>
  assume-console [
    press ctrl-y
  ]
  run [
    editor-event-loop screen, console, e
  ]
  <span class="Comment"># cursor moves to left-click</span>
  3:num/<span class="Special">raw</span> <span class="Special">&lt;-</span> get *e, <span class="Constant">cursor-row:offset</span>
  4:num/<span class="Special">raw</span> <span class="Special">&lt;-</span> get *e, <span class="Constant">cursor-column:offset</span>
  memory-should-contain [
   <span class="Constant"> 3</span> <span class="Special">&lt;-</span><span class="Constant"> 3</span>
   <span class="Constant"> 4</span> <span class="Special">&lt;-</span><span class="Constant"> 1</span>
  ]
  <span class="Comment"># cursor should be in the right place</span>
  assume-console [
    type <span class="Constant">[1]</span>
  ]
  run [
    editor-event-loop screen, console, e
  ]
  screen-should-contain [
   <span class="Constant"> .          .</span>
   <span class="Constant"> .abc       .</span>
   <span class="Constant"> .def       .</span>
   <span class="Constant"> .g1hi      .</span>
<span class="Constant">    .╌╌╌╌╌╌╌╌╌╌.</span>
  ]
]

<span class="muRecipe">after</span> <span class="Constant">&lt;handle-redo&gt;</span> [
  <span class="Delimiter">{</span>
    move:move-operation, is-move?:bool <span class="Special">&lt;-</span> maybe-convert *op, <span class="Constant">move:variant</span>
    <span class="muControl">break-unless</span> is-move?
    <span class="Comment"># assert cursor-row/cursor-column/top-of-screen match after-row/after-column/after-top-of-screen</span>
    cursor-row <span class="Special">&lt;-</span> get move, <span class="Constant">after-row:offset</span>
    *editor <span class="Special">&lt;-</span> put *editor, <span class="Constant">cursor-row:offset</span>, cursor-row
    cursor-column <span class="Special">&lt;-</span> get move, <span class="Constant">after-column:offset</span>
    *editor <span class="Special">&lt;-</span> put *editor, <span class="Constant">cursor-column:offset</span>, cursor-column
    top:&amp;:duplex-list:char <span class="Special">&lt;-</span> get move, <span class="Constant">after-top-of-screen:offset</span>
    *editor <span class="Special">&lt;-</span> put *editor, <span class="Constant">top-of-screen:offset</span>, top
  <span class="Delimiter">}</span>
]

<span class="muScenario">scenario</span> editor-separates-undo-insert-from-undo-cursor-move [
  <span class="Constant">local-scope</span>
  <span class="Comment"># create an editor, type some text, move the cursor, type some more text</span>
  assume-screen <span class="Constant">10/width</span>, <span class="Constant">5/height</span>
  e:&amp;:editor <span class="Special">&lt;-</span> new-editor <span class="Constant">[]</span>, screen, <span class="Constant">0/left</span>, <span class="Constant">10/right</span>
  editor-render screen, e
  assume-console [
    type <span class="Constant">[abc]</span>
    left-click<span class="Constant"> 1</span>,<span class="Constant"> 1</span>
    type <span class="Constant">[d]</span>
  ]
  editor-event-loop screen, console, e
  3:num/<span class="Special">raw</span> <span class="Special">&lt;-</span> get *e, <span class="Constant">cursor-row:offset</span>
  4:num/<span class="Special">raw</span> <span class="Special">&lt;-</span> get *e, <span class="Constant">cursor-column:offset</span>
  screen-should-contain [
   <span class="Constant"> .          .</span>
   <span class="Constant"> .adbc      .</span>
<span class="Constant">    .╌╌╌╌╌╌╌╌╌╌.</span>
   <span class="Constant"> .          .</span>
  ]
  memory-should-contain [
   <span class="Constant"> 3</span> <span class="Special">&lt;-</span><span class="Constant"> 1</span>
   <span class="Constant"> 4</span> <span class="Special">&lt;-</span><span class="Constant"> 2</span>
  ]
  <span class="Comment"># undo</span>
  assume-console [
    press ctrl-z
  ]
  run [
    editor-event-loop screen, console, e
    3:num/<span class="Special">raw</span> <span class="Special">&lt;-</span> get *e, <span class="Constant">cursor-row:offset</span>
    4:num/<span class="Special">raw</span> <span class="Special">&lt;-</span> get *e, <span class="Constant">cursor-column:offset</span>
  ]
  <span class="Comment"># last letter typed is deleted</span>
  screen-should-contain [
   <span class="Constant"> .          .</span>
   <span class="Constant"> .abc       .</span>
<span class="Constant">    .╌╌╌╌╌╌╌╌╌╌.</span>
   <span class="Constant"> .          .</span>
  ]
  memory-should-contain [
   <span class="Constant"> 3</span> <span class="Special">&lt;-</span><span class="Constant"> 1</span>
   <span class="Constant"> 4</span> <span class="Special">&lt;-</span><span class="Constant"> 1</span>
  ]
  <span class="Comment"># undo again</span>
  assume-console [
    press ctrl-z
  ]
  run [
    editor-event-loop screen, console, e
    3:num/<span class="Special">raw</span> <span class="Special">&lt;-</span> get *e, <span class="Constant">cursor-row:offset</span>
    4:num/<span class="Special">raw</span> <span class="Special">&lt;-</span> get *e, <span class="Constant">cursor-column:offset</span>
  ]
  <span class="Comment"># no change to screen; cursor moves</span>
  screen-should-contain [
   <span class="Constant"> .          .</span>
   <span class="Constant"> .abc       .</span>
<span class="Constant">    .╌╌╌╌╌╌╌╌╌╌.</span>
   <span class="Constant"> .          .</span>
  ]
  memory-should-contain [
   <span class="Constant"> 3</span> <span class="Special">&lt;-</span><span class="Constant"> 1</span>
   <span class="Constant"> 4</span> <span class="Special">&lt;-</span><span class="Constant"> 3</span>
  ]
  <span class="Comment"># undo again</span>
  assume-console [
    press ctrl-z
  ]
  run [
    editor-event-loop screen, console, e
    3:num/<span class="Special">raw</span> <span class="Special">&lt;-</span> get *e, <span class="Constant">cursor-row:offset</span>
    4:num/<span class="Special">raw</span> <span class="Special">&lt;-</span> get *e, <span class="Constant">cursor-column:offset</span>
  ]
  <span class="Comment"># screen empty</span>
  screen-should-contain [
   <span class="Constant"> .          .</span>
   <span class="Constant"> .          .</span>
<span class="Constant">    .╌╌╌╌╌╌╌╌╌╌.</span>
   <span class="Constant"> .          .</span>
  ]
  memory-should-contain [
   <span class="Constant"> 3</span> <span class="Special">&lt;-</span><span class="Constant"> 1</span>
   <span class="Constant"> 4</span> <span class="Special">&lt;-</span><span class="Constant"> 0</span>
  ]
  <span class="Comment"># redo</span>
  assume-console [
    press ctrl-y
  ]
  run [
    editor-event-loop screen, console, e
    3:num/<span class="Special">raw</span> <span class="Special">&lt;-</span> get *e, <span class="Constant">cursor-row:offset</span>
    4:num/<span class="Special">raw</span> <span class="Special">&lt;-</span> get *e, <span class="Constant">cursor-column:offset</span>
  ]
  <span class="Comment"># first insert</span>
  screen-should-contain [
   <span class="Constant"> .          .</span>
   <span class="Constant"> .abc       .</span>
<span class="Constant">    .╌╌╌╌╌╌╌╌╌╌.</span>
   <span class="Constant"> .          .</span>
  ]
  memory-should-contain [
   <span class="Constant"> 3</span> <span class="Special">&lt;-</span><span class="Constant"> 1</span>
   <span class="Constant"> 4</span> <span class="Special">&lt;-</span><span class="Constant"> 3</span>
  ]
  <span class="Comment"># redo again</span>
  assume-console [
    press ctrl-y
  ]
  run [
    editor-event-loop screen, console, e
    3:num/<span class="Special">raw</span> <span class="Special">&lt;-</span> get *e, <span class="Constant">cursor-row:offset</span>
    4:num/<span class="Special">raw</span> <span class="Special">&lt;-</span> get *e, <span class="Constant">cursor-column:offset</span>
  ]
  <span class="Comment"># cursor moves</span>
  screen-should-contain [
   <span class="Constant"> .          .</span>
   <span class="Constant"> .abc       .</span>
<span class="Constant">    .╌╌╌╌╌╌╌╌╌╌.</span>
   <span class="Constant"> .          .</span>
  ]
  <span class="Comment"># cursor moves</span>
  memory-should-contain [
   <span class="Constant"> 3</span> <span class="Special">&lt;-</span><span class="Constant"> 1</span>
   <span class="Constant"> 4</span> <span class="Special">&lt;-</span><span class="Constant"> 1</span>
  ]
  <span class="Comment"># redo again</span>
  assume-console [
    press ctrl-y
  ]
  run [
    editor-event-loop screen, console, e
    3:num/<span class="Special">raw</span> <span class="Special">&lt;-</span> get *e, <span class="Constant">cursor-row:offset</span>
    4:num/<span class="Special">raw</span> <span class="Special">&lt;-</span> get *e, <span class="Constant">cursor-column:offset</span>
  ]
  <span class="Comment"># second insert</span>
  screen-should-contain [
   <span class="Constant"> .          .</span>
   <span class="Constant"> .adbc      .</span>
<span class="Constant">    .╌╌╌╌╌╌╌╌╌╌.</span>
   <span class="Constant"> .          .</span>
  ]
  memory-should-contain [
   <span class="Constant"> 3</span> <span class="Special">&lt;-</span><span class="Constant"> 1</span>
   <span class="Constant"> 4</span> <span class="Special">&lt;-</span><span class="Constant"> 2</span>
  ]
]

<span class="Comment"># undo backspace</span>

<span class="muScenario">scenario</span> editor-can-undo-and-redo-backspace [
  <span class="Constant">local-scope</span>
  <span class="Comment"># create an editor</span>
  assume-screen <span class="Constant">10/width</span>, <span class="Constant">5/height</span>
  e:&amp;:editor <span class="Special">&lt;-</span> new-editor <span class="Constant">[]</span>, screen, <span class="Constant">0/left</span>, <span class="Constant">10/right</span>
  editor-render screen, e
  <span class="Comment"># insert some text and hit backspace</span>
  assume-console [
    type <span class="Constant">[abc]</span>
    press backspace
    press backspace
  ]
  editor-event-loop screen, console, e
  screen-should-contain [
   <span class="Constant"> .          .</span>
   <span class="Constant"> .a         .</span>
<span class="Constant">    .╌╌╌╌╌╌╌╌╌╌.</span>
   <span class="Constant"> .          .</span>
  ]
  3:num/<span class="Special">raw</span> <span class="Special">&lt;-</span> get *e, <span class="Constant">cursor-row:offset</span>
  4:num/<span class="Special">raw</span> <span class="Special">&lt;-</span> get *e, <span class="Constant">cursor-column:offset</span>
  memory-should-contain [
   <span class="Constant"> 3</span> <span class="Special">&lt;-</span><span class="Constant"> 1</span>
   <span class="Constant"> 4</span> <span class="Special">&lt;-</span><span class="Constant"> 1</span>
  ]
  <span class="Comment"># undo</span>
  assume-console [
    press ctrl-z
  ]
  run [
    editor-event-loop screen, console, e
  ]
  3:num/<span class="Special">raw</span> <span class="Special">&lt;-</span> get *e, <span class="Constant">cursor-row:offset</span>
  4:num/<span class="Special">raw</span> <span class="Special">&lt;-</span> get *e, <span class="Constant">cursor-column:offset</span>
  memory-should-contain [
   <span class="Constant"> 3</span> <span class="Special">&lt;-</span><span class="Constant"> 1</span>
   <span class="Constant"> 4</span> <span class="Special">&lt;-</span><span class="Constant"> 3</span>
  ]
  screen-should-contain [
   <span class="Constant"> .          .</span>
   <span class="Constant"> .abc       .</span>
<span class="Constant">    .╌╌╌╌╌╌╌╌╌╌.</span>
   <span class="Constant"> .          .</span>
  ]
  <span class="Comment"># redo</span>
  assume-console [
    press ctrl-y
  ]
  run [
    editor-event-loop screen, console, e
  ]
  3:num/<span class="Special">raw</span> <span class="Special">&lt;-</span> get *e, <span class="Constant">cursor-row:offset</span>
  4:num/<span class="Special">raw</span> <span class="Special">&lt;-</span> get *e, <span class="Constant">cursor-column:offset</span>
  memory-should-contain [
   <span class="Constant"> 3</span> <span class="Special">&lt;-</span><span class="Constant"> 1</span>
   <span class="Constant"> 4</span> <span class="Special">&lt;-</span><span class="Constant"> 1</span>
  ]
  screen-should-contain [
   <span class="Constant"> .          .</span>
   <span class="Constant"> .a         .</span>
<span class="Constant">    .╌╌╌╌╌╌╌╌╌╌.</span>
   <span class="Constant"> .          .</span>
  ]
]

<span class="Comment"># save operation to undo</span>
<span class="muRecipe">after</span> <span class="Constant">&lt;backspace-character-begin&gt;</span> [
  top-before:&amp;:duplex-list:char <span class="Special">&lt;-</span> get *editor, <span class="Constant">top-of-screen:offset</span>
]
<span class="muRecipe">before</span> <span class="Constant">&lt;backspace-character-end&gt;</span> [
  <span class="Delimiter">{</span>
    <span class="muControl">break-unless</span> backspaced-cell  <span class="Comment"># backspace failed; don't add an undo operation</span>
    top-after:&amp;:duplex-list:char <span class="Special">&lt;-</span> get *editor, <span class="Constant">top-of-screen:offset</span>
    cursor-row:num <span class="Special">&lt;-</span> get *editor, <span class="Constant">cursor-row:offset</span>
    cursor-column:num <span class="Special">&lt;-</span> get *editor, <span class="Constant">cursor-row:offset</span>
    before-cursor:&amp;:duplex-list:char <span class="Special">&lt;-</span> get *editor, <span class="Constant">before-cursor:offset</span>
    undo:&amp;:list:&amp;:operation <span class="Special">&lt;-</span> get *editor, <span class="Constant">undo:offset</span>
    <span class="Delimiter">{</span>
      <span class="Comment"># if previous operation was an insert, coalesce this operation with it</span>
      <span class="muControl">break-unless</span> undo
      op:&amp;:operation <span class="Special">&lt;-</span> first undo
      deletion:delete-operation, is-delete?:bool <span class="Special">&lt;-</span> maybe-convert *op, <span class="Constant">delete:variant</span>
      <span class="muControl">break-unless</span> is-delete?
      previous-coalesce-tag:num <span class="Special">&lt;-</span> get deletion, <span class="Constant">tag:offset</span>
      coalesce?:bool <span class="Special">&lt;-</span> equal previous-coalesce-tag, <span class="Constant">1/coalesce-backspace</span>
      <span class="muControl">break-unless</span> coalesce?
      deletion <span class="Special">&lt;-</span> put deletion, <span class="Constant">delete-from:offset</span>, before-cursor
      backspaced-so-far:&amp;:duplex-list:char <span class="Special">&lt;-</span> get deletion, <span class="Constant">deleted-text:offset</span>
      insert-range backspaced-cell, backspaced-so-far
      deletion <span class="Special">&lt;-</span> put deletion, <span class="Constant">deleted-text:offset</span>, backspaced-cell
      deletion <span class="Special">&lt;-</span> put deletion, <span class="Constant">after-row:offset</span>, cursor-row
      deletion <span class="Special">&lt;-</span> put deletion, <span class="Constant">after-column:offset</span>, cursor-column
      deletion <span class="Special">&lt;-</span> put deletion, <span class="Constant">after-top-of-screen:offset</span>, top-after
      *op <span class="Special">&lt;-</span> merge <span class="Constant">2/delete-operation</span>, deletion
      <span class="muControl">break</span> <span class="Constant">+done-adding-backspace-operation</span>
    <span class="Delimiter">}</span>
    <span class="Comment"># if not, create a new operation</span>
    op:&amp;:operation <span class="Special">&lt;-</span> new <span class="Constant">operation:type</span>
    deleted-until:&amp;:duplex-list:char <span class="Special">&lt;-</span> next before-cursor
    *op <span class="Special">&lt;-</span> merge <span class="Constant">2/delete-operation</span>, save-row/before, save-column/before, top-before, cursor-row/after, cursor-column/after, top-after, backspaced-cell/deleted, before-cursor/delete-from, deleted-until, <span class="Constant">1/coalesce-backspace</span>
    editor <span class="Special">&lt;-</span> add-operation editor, op
<span class="Constant">    +done-adding-backspace-operation</span>
  <span class="Delimiter">}</span>
]

<span class="muRecipe">after</span> <span class="Constant">&lt;handle-undo&gt;</span> [
  <span class="Delimiter">{</span>
    deletion:delete-operation, is-delete?:bool <span class="Special">&lt;-</span> maybe-convert *op, <span class="Constant">delete:variant</span>
    <span class="muControl">break-unless</span> is-delete?
    anchor:&amp;:duplex-list:char <span class="Special">&lt;-</span> get deletion, <span class="Constant">delete-from:offset</span>
    <span class="muControl">break-unless</span> anchor
    deleted:&amp;:duplex-list:char <span class="Special">&lt;-</span> get deletion, <span class="Constant">deleted-text:offset</span>
    old-cursor:&amp;:duplex-list:char <span class="Special">&lt;-</span> last deleted
    insert-range anchor, deleted
    <span class="Comment"># assert cursor-row/cursor-column/top-of-screen match after-row/after-column/after-top-of-screen</span>
    before-cursor <span class="Special">&lt;-</span> copy old-cursor
    cursor-row <span class="Special">&lt;-</span> get deletion, <span class="Constant">before-row:offset</span>
    *editor <span class="Special">&lt;-</span> put *editor, <span class="Constant">cursor-row:offset</span>, cursor-row
    cursor-column <span class="Special">&lt;-</span> get deletion, <span class="Constant">before-column:offset</span>
    *editor <span class="Special">&lt;-</span> put *editor, <span class="Constant">cursor-column:offset</span>, cursor-column
    top:&amp;:duplex-list:char <span class="Special">&lt;-</span> get deletion, <span class="Constant">before-top-of-screen:offset</span>
    *editor <span class="Special">&lt;-</span> put *editor, <span class="Constant">top-of-screen:offset</span>, top
  <span class="Delimiter">}</span>
]

<span class="muRecipe">after</span> <span class="Constant">&lt;handle-redo&gt;</span> [
  <span class="Delimiter">{</span>
    deletion:delete-operation, is-delete?:bool <span class="Special">&lt;-</span> maybe-convert *op, <span class="Constant">delete:variant</span>
    <span class="muControl">break-unless</span> is-delete?
    start:&amp;:duplex-list:char <span class="Special">&lt;-</span> get deletion, <span class="Constant">delete-from:offset</span>
    end:&amp;:duplex-list:char <span class="Special">&lt;-</span> get deletion, <span class="Constant">delete-until:offset</span>
    data:&amp;:duplex-list:char <span class="Special">&lt;-</span> get *editor, <span class="Constant">data:offset</span>
    remove-between start, end
    <span class="Comment"># assert cursor-row/cursor-column/top-of-screen match after-row/after-column/after-top-of-screen</span>
    cursor-row <span class="Special">&lt;-</span> get deletion, <span class="Constant">after-row:offset</span>
    *editor <span class="Special">&lt;-</span> put *editor, <span class="Constant">cursor-row:offset</span>, cursor-row
    cursor-column <span class="Special">&lt;-</span> get deletion, <span class="Constant">after-column:offset</span>
    *editor <span class="Special">&lt;-</span> put *editor, <span class="Constant">cursor-column:offset</span>, cursor-column
    top:&amp;:duplex-list:char <span class="Special">&lt;-</span> get deletion, <span class="Constant">before-top-of-screen:offset</span>
    *editor <span class="Special">&lt;-</span> put *editor, <span class="Constant">top-of-screen:offset</span>, top
  <span class="Delimiter">}</span>
]

<span class="Comment"># undo delete</span>

<span class="muScenario">scenario</span> editor-can-undo-and-redo-delete [
  <span class="Constant">local-scope</span>
  <span class="Comment"># create an editor</span>
  assume-screen <span class="Constant">10/width</span>, <span class="Constant">5/height</span>
  e:&amp;:editor <span class="Special">&lt;-</span> new-editor <span class="Constant">[]</span>, screen, <span class="Constant">0/left</span>, <span class="Constant">10/right</span>
  editor-render screen, e
  <span class="Comment"># insert some text and hit delete and backspace a few times</span>
  assume-console [
    type <span class="Constant">[abcdef]</span>
    left-click<span class="Constant"> 1</span>,<span class="Constant"> 2</span>
    press delete
    press backspace
    press delete
    press delete
  ]
  editor-event-loop screen, console, e
  screen-should-contain [
   <span class="Constant"> .          .</span>
   <span class="Constant"> .af        .</span>
<span class="Constant">    .╌╌╌╌╌╌╌╌╌╌.</span>
   <span class="Constant"> .          .</span>
  ]
  3:num/<span class="Special">raw</span> <span class="Special">&lt;-</span> get *e, <span class="Constant">cursor-row:offset</span>
  4:num/<span class="Special">raw</span> <span class="Special">&lt;-</span> get *e, <span class="Constant">cursor-column:offset</span>
  memory-should-contain [
   <span class="Constant"> 3</span> <span class="Special">&lt;-</span><span class="Constant"> 1</span>
   <span class="Constant"> 4</span> <span class="Special">&lt;-</span><span class="Constant"> 1</span>
  ]
  <span class="Comment"># undo deletes</span>
  assume-console [
    press ctrl-z
  ]
  run [
    editor-event-loop screen, console, e
  ]
  3:num/<span class="Special">raw</span> <span class="Special">&lt;-</span> get *e, <span class="Constant">cursor-row:offset</span>
  4:num/<span class="Special">raw</span> <span class="Special">&lt;-</span> get *e, <span class="Constant">cursor-column:offset</span>
  memory-should-contain [
   <span class="Constant"> 3</span> <span class="Special">&lt;-</span><span class="Constant"> 1</span>
   <span class="Constant"> 4</span> <span class="Special">&lt;-</span><span class="Constant"> 1</span>
  ]
  screen-should-contain [
   <span class="Constant"> .          .</span>
   <span class="Constant"> .adef      .</span>
<span class="Constant">    .╌╌╌╌╌╌╌╌╌╌.</span>
   <span class="Constant"> .          .</span>
  ]
  <span class="Comment"># undo backspace</span>
  assume-console [
    press ctrl-z
  ]
  run [
    editor-event-loop screen, console, e
  ]
  3:num/<span class="Special">raw</span> <span class="Special">&lt;-</span> get *e, <span class="Constant">cursor-row:offset</span>
  4:num/<span class="Special">raw</span> <span class="Special">&lt;-</span> get *e, <span class="Constant">cursor-column:offset</span>
  memory-should-contain [
   <span class="Constant"> 3</span> <span class="Special">&lt;-</span><span class="Constant"> 1</span>
   <span class="Constant"> 4</span> <span class="Special">&lt;-</span><span class="Constant"> 2</span>
  ]
  screen-should-contain [
   <span class="Constant"> .          .</span>
   <span class="Constant"> .abdef     .</span>
<span class="Constant">    .╌╌╌╌╌╌╌╌╌╌.</span>
   <span class="Constant"> .          .</span>
  ]
  <span class="Comment"># undo first delete</span>
  assume-console [
    press ctrl-z
  ]
  run [
    editor-event-loop screen, console, e
  ]
  3:num/<span class="Special">raw</span> <span class="Special">&lt;-</span> get *e, <span class="Constant">cursor-row:offset</span>
  4:num/<span class="Special">raw</span> <span class="Special">&lt;-</span> get *e, <span class="Constant">cursor-column:offset</span>
  memory-should-contain [
   <span class="Constant"> 3</span> <span class="Special">&lt;-</span><span class="Constant"> 1</span>
   <span class="Constant"> 4</span> <span class="Special">&lt;-</span><span class="Constant"> 2</span>
  ]
  screen-should-contain [
   <span class="Constant"> .          .</span>
   <span class="Constant"> .abcdef    .</span>
<span class="Constant">    .╌╌╌╌╌╌╌╌╌╌.</span>
   <span class="Constant"> .          .</span>
  ]
  <span class="Comment"># redo first delete</span>
  assume-console [
    press ctrl-y
  ]
  run [
    editor-event-loop screen, console, e
  ]
  <span class="Comment"># first line inserted</span>
  3:num/<span class="Special">raw</span> <span class="Special">&lt;-</span> get *e, <span class="Constant">cursor-row:offset</span>
  4:num/<span class="Special">raw</span> <span class="Special">&lt;-</span> get *e, <span class="Constant">cursor-column:offset</span>
  memory-should-contain [
   <span class="Constant"> 3</span> <span class="Special">&lt;-</span><span class="Constant"> 1</span>
   <span class="Constant"> 4</span> <span class="Special">&lt;-</span><span class="Constant"> 2</span>
  ]
  screen-should-contain [
   <span class="Constant"> .          .</span>
   <span class="Constant"> .abdef     .</span>
<span class="Constant">    .╌╌╌╌╌╌╌╌╌╌.</span>
   <span class="Constant"> .          .</span>
  ]
  <span class="Comment"># redo backspace</span>
  assume-console [
    press ctrl-y
  ]
  run [
    editor-event-loop screen, console, e
  ]
  <span class="Comment"># first line inserted</span>
  3:num/<span class="Special">raw</span> <span class="Special">&lt;-</span> get *e, <span class="Constant">cursor-row:offset</span>
  4:num/<span class="Special">raw</span> <span class="Special">&lt;-</span> get *e, <span class="Constant">cursor-column:offset</span>
  memory-should-contain [
   <span class="Constant"> 3</span> <span class="Special">&lt;-</span><span class="Constant"> 1</span>
   <span class="Constant"> 4</span> <span class="Special">&lt;-</span><span class="Constant"> 1</span>
  ]
  screen-should-contain [
   <span class="Constant"> .          .</span>
   <span class="Constant"> .adef      .</span>
<span class="Constant">    .╌╌╌╌╌╌╌╌╌╌.</span>
   <span class="Constant"> .          .</span>
  ]
  <span class="Comment"># redo deletes</span>
  assume-console [
    press ctrl-y
  ]
  run [
    editor-event-loop screen, console, e
  ]
  <span class="Comment"># first line inserted</span>
  3:num/<span class="Special">raw</span> <span class="Special">&lt;-</span> get *e, <span class="Constant">cursor-row:offset</span>
  4:num/<span class="Special">raw</span> <span class="Special">&lt;-</span> get *e, <span class="Constant">cursor-column:offset</span>
  memory-should-contain [
   <span class="Constant"> 3</span> <span class="Special">&lt;-</span><span class="Constant"> 1</span>
   <span class="Constant"> 4</span> <span class="Special">&lt;-</span><span class="Constant"> 1</span>
  ]
  screen-should-contain [
   <span class="Constant"> .          .</span>
   <span class="Constant"> .af        .</span>
<span class="Constant">    .╌╌╌╌╌╌╌╌╌╌.</span>
   <span class="Constant"> .          .</span>
  ]
]

<span class="muRecipe">after</span> <span class="Constant">&lt;delete-character-begin&gt;</span> [
  top-before:&amp;:duplex-list:char <span class="Special">&lt;-</span> get *editor, <span class="Constant">top-of-screen:offset</span>
]
<span class="muRecipe">before</span> <span class="Constant">&lt;delete-character-end&gt;</span> [
  <span class="Delimiter">{</span>
    <span class="muControl">break-unless</span> deleted-cell  <span class="Comment"># delete failed; don't add an undo operation</span>
    top-after:&amp;:duplex-list:char <span class="Special">&lt;-</span> get *editor, <span class="Constant">top-of-screen:offset</span>
    cursor-row:num <span class="Special">&lt;-</span> get *editor, <span class="Constant">cursor-row:offset</span>
    cursor-column:num <span class="Special">&lt;-</span> get *editor, <span class="Constant">cursor-column:offset</span>
    before-cursor:&amp;:duplex-list:char <span class="Special">&lt;-</span> get *editor, <span class="Constant">before-cursor:offset</span>
    undo:&amp;:list:&amp;:operation <span class="Special">&lt;-</span> get *editor, <span class="Constant">undo:offset</span>
    <span class="Delimiter">{</span>
      <span class="Comment"># if previous operation was an insert, coalesce this operation with it</span>
      <span class="muControl">break-unless</span> undo
      op:&amp;:operation <span class="Special">&lt;-</span> first undo
      deletion:delete-operation, is-delete?:bool <span class="Special">&lt;-</span> maybe-convert *op, <span class="Constant">delete:variant</span>
      <span class="muControl">break-unless</span> is-delete?
      previous-coalesce-tag:num <span class="Special">&lt;-</span> get deletion, <span class="Constant">tag:offset</span>
      coalesce?:bool <span class="Special">&lt;-</span> equal previous-coalesce-tag, <span class="Constant">2/coalesce-delete</span>
      <span class="muControl">break-unless</span> coalesce?
      delete-until:&amp;:duplex-list:char <span class="Special">&lt;-</span> next before-cursor
      deletion <span class="Special">&lt;-</span> put deletion, <span class="Constant">delete-until:offset</span>, delete-until
      deleted-so-far:&amp;:duplex-list:char <span class="Special">&lt;-</span> get deletion, <span class="Constant">deleted-text:offset</span>
      deleted-so-far <span class="Special">&lt;-</span> append deleted-so-far, deleted-cell
      deletion <span class="Special">&lt;-</span> put deletion, <span class="Constant">deleted-text:offset</span>, deleted-so-far
      deletion <span class="Special">&lt;-</span> put deletion, <span class="Constant">after-row:offset</span>, cursor-row
      deletion <span class="Special">&lt;-</span> put deletion, <span class="Constant">after-column:offset</span>, cursor-column
      deletion <span class="Special">&lt;-</span> put deletion, <span class="Constant">after-top-of-screen:offset</span>, top-after
      *op <span class="Special">&lt;-</span> merge <span class="Constant">2/delete-operation</span>, deletion
      <span class="muControl">break</span> <span class="Constant">+done-adding-delete-operation</span>
    <span class="Delimiter">}</span>
    <span class="Comment"># if not, create a new operation</span>
    op:&amp;:operation <span class="Special">&lt;-</span> new <span class="Constant">operation:type</span>
    deleted-until:&amp;:duplex-list:char <span class="Special">&lt;-</span> next before-cursor
    *op <span class="Special">&lt;-</span> merge <span class="Constant">2/delete-operation</span>, save-row/before, save-column/before, top-before, cursor-row/after, cursor-column/after, top-after, deleted-cell/deleted, before-cursor/delete-from, deleted-until, <span class="Constant">2/coalesce-delete</span>
    editor <span class="Special">&lt;-</span> add-operation editor, op
<span class="Constant">    +done-adding-delete-operation</span>
  <span class="Delimiter">}</span>
]

<span class="Comment"># undo ctrl-k</span>

<span class="muScenario">scenario</span> editor-can-undo-and-redo-ctrl-k [
  <span class="Constant">local-scope</span>
  <span class="Comment"># create an editor</span>
  assume-screen <span class="Constant">10/width</span>, <span class="Constant">5/height</span>
  contents:text <span class="Special">&lt;-</span> new <span class="Constant">[abc</span>
<span class="Constant">def]</span>
  e:&amp;:editor <span class="Special">&lt;-</span> new-editor contents, screen, <span class="Constant">0/left</span>, <span class="Constant">10/right</span>
  editor-render screen, e
  <span class="Comment"># insert some text and hit delete and backspace a few times</span>
  assume-console [
    left-click<span class="Constant"> 1</span>,<span class="Constant"> 1</span>
    press ctrl-k
  ]
  editor-event-loop screen, console, e
  screen-should-contain [
   <span class="Constant"> .          .</span>
   <span class="Constant"> .a         .</span>
   <span class="Constant"> .def       .</span>
<span class="Constant">    .╌╌╌╌╌╌╌╌╌╌.</span>
   <span class="Constant"> .          .</span>
  ]
  3:num/<span class="Special">raw</span> <span class="Special">&lt;-</span> get *e, <span class="Constant">cursor-row:offset</span>
  4:num/<span class="Special">raw</span> <span class="Special">&lt;-</span> get *e, <span class="Constant">cursor-column:offset</span>
  memory-should-contain [
   <span class="Constant"> 3</span> <span class="Special">&lt;-</span><span class="Constant"> 1</span>
   <span class="Constant"> 4</span> <span class="Special">&lt;-</span><span class="Constant"> 1</span>
  ]
  <span class="Comment"># undo</span>
  assume-console [
    press ctrl-z
  ]
  run [
    editor-event-loop screen, console, e
  ]
  screen-should-contain [
   <span class="Constant"> .          .</span>
   <span class="Constant"> .abc       .</span>
   <span class="Constant"> .def       .</span>
<span class="Constant">    .╌╌╌╌╌╌╌╌╌╌.</span>
   <span class="Constant"> .          .</span>
  ]
  3:num/<span class="Special">raw</span> <span class="Special">&lt;-</span> get *e, <span class="Constant">cursor-row:offset</span>
  4:num/<span class="Special">raw</span> <span class="Special">&lt;-</span> get *e, <span class="Constant">cursor-column:offset</span>
  memory-should-contain [
   <span class="Constant"> 3</span> <span class="Special">&lt;-</span><span class="Constant"> 1</span>
   <span class="Constant"> 4</span> <span class="Special">&lt;-</span><span class="Constant"> 1</span>
  ]
  <span class="Comment"># redo</span>
  assume-console [
    press ctrl-y
  ]
  run [
    editor-event-loop screen, console, e
  ]
  <span class="Comment"># first line inserted</span>
  screen-should-contain [
   <span class="Constant"> .          .</span>
   <span class="Constant"> .a         .</span>
   <span class="Constant"> .def       .</span>
<span class="Constant">    .╌╌╌╌╌╌╌╌╌╌.</span>
   <span class="Constant"> .          .</span>
  ]
  3:num/<span class="Special">raw</span> <span class="Special">&lt;-</span> get *e, <span class="Constant">cursor-row:offset</span>
  4:num/<span class="Special">raw</span> <span class="Special">&lt;-</span> get *e, <span class="Constant">cursor-column:offset</span>
  memory-should-contain [
   <span class="Constant"> 3</span> <span class="Special">&lt;-</span><span class="Constant"> 1</span>
   <span class="Constant"> 4</span> <span class="Special">&lt;-</span><span class="Constant"> 1</span>
  ]
  <span class="Comment"># cursor should be in the right place</span>
  assume-console [
    type <span class="Constant">[1]</span>
  ]
  run [
    editor-event-loop screen, console, e
  ]
  screen-should-contain [
   <span class="Constant"> .          .</span>
   <span class="Constant"> .a1        .</span>
   <span class="Constant"> .def       .</span>
<span class="Constant">    .╌╌╌╌╌╌╌╌╌╌.</span>
   <span class="Constant"> .          .</span>
  ]
]

<span class="muRecipe">after</span> <span class="Constant">&lt;delete-to-end-of-line-begin&gt;</span> [
  top-before:&amp;:duplex-list:char <span class="Special">&lt;-</span> get *editor, <span class="Constant">top-of-screen:offset</span>
]
<span class="muRecipe">before</span> <span class="Constant">&lt;delete-to-end-of-line-end&gt;</span> [
  <span class="Delimiter">{</span>
    <span class="muControl">break-unless</span> deleted-cells  <span class="Comment"># delete failed; don't add an undo operation</span>
    top-after:&amp;:duplex-list:char <span class="Special">&lt;-</span> get *editor, <span class="Constant">top-of-screen:offset</span>
    cursor-row:num <span class="Special">&lt;-</span> get *editor, <span class="Constant">cursor-row:offset</span>
    cursor-column:num <span class="Special">&lt;-</span> get *editor, <span class="Constant">cursor-column:offset</span>
    deleted-until:&amp;:duplex-list:char <span class="Special">&lt;-</span> next before-cursor
    op:&amp;:operation <span class="Special">&lt;-</span> new <span class="Constant">operation:type</span>
    *op <span class="Special">&lt;-</span> merge <span class="Constant">2/delete-operation</span>, save-row/before, save-column/before, top-before, cursor-row/after, cursor-column/after, top-after, deleted-cells/deleted, before-cursor/delete-from, deleted-until, <span class="Constant">0/never-coalesce</span>
    editor <span class="Special">&lt;-</span> add-operation editor, op
<span class="Constant">    +done-adding-delete-operation</span>
  <span class="Delimiter">}</span>
]

<span class="Comment"># undo ctrl-u</span>

<span class="muScenario">scenario</span> editor-can-undo-and-redo-ctrl-u [
  <span class="Constant">local-scope</span>
  <span class="Comment"># create an editor</span>
  assume-screen <span class="Constant">10/width</span>, <span class="Constant">5/height</span>
  contents:text <span class="Special">&lt;-</span> new <span class="Constant">[abc</span>
<span class="Constant">def]</span>
  e:&amp;:editor <span class="Special">&lt;-</span> new-editor contents, screen, <span class="Constant">0/left</span>, <span class="Constant">10/right</span>
  editor-render screen, e
  <span class="Comment"># insert some text and hit delete and backspace a few times</span>
  assume-console [
    left-click<span class="Constant"> 1</span>,<span class="Constant"> 2</span>
    press ctrl-u
  ]
  editor-event-loop screen, console, e
  screen-should-contain [
   <span class="Constant"> .          .</span>
   <span class="Constant"> .c         .</span>
   <span class="Constant"> .def       .</span>
<span class="Constant">    .╌╌╌╌╌╌╌╌╌╌.</span>
   <span class="Constant"> .          .</span>
  ]
  3:num/<span class="Special">raw</span> <span class="Special">&lt;-</span> get *e, <span class="Constant">cursor-row:offset</span>
  4:num/<span class="Special">raw</span> <span class="Special">&lt;-</span> get *e, <span class="Constant">cursor-column:offset</span>
  memory-should-contain [
   <span class="Constant"> 3</span> <span class="Special">&lt;-</span><span class="Constant"> 1</span>
   <span class="Constant"> 4</span> <span class="Special">&lt;-</span><span class="Constant"> 0</span>
  ]
  <span class="Comment"># undo</span>
  assume-console [
    press ctrl-z
  ]
  run [
    editor-event-loop screen, console, e
  ]
  screen-should-contain [
   <span class="Constant"> .          .</span>
   <span class="Constant"> .abc       .</span>
   <span class="Constant"> .def       .</span>
<span class="Constant">    .╌╌╌╌╌╌╌╌╌╌.</span>
   <span class="Constant"> .          .</span>
  ]
  3:num/<span class="Special">raw</span> <span class="Special">&lt;-</span> get *e, <span class="Constant">cursor-row:offset</span>
  4:num/<span class="Special">raw</span> <span class="Special">&lt;-</span> get *e, <span class="Constant">cursor-column:offset</span>
  memory-should-contain [
   <span class="Constant"> 3</span> <span class="Special">&lt;-</span><span class="Constant"> 1</span>
   <span class="Constant"> 4</span> <span class="Special">&lt;-</span><span class="Constant"> 2</span>
  ]
  <span class="Comment"># redo</span>
  assume-console [
    press ctrl-y
  ]
  run [
    editor-event-loop screen, console, e
  ]
  <span class="Comment"># first line inserted</span>
  screen-should-contain [
   <span class="Constant"> .          .</span>
   <span class="Constant"> .c         .</span>
   <span class="Constant"> .def       .</span>
<span class="Constant">    .╌╌╌╌╌╌╌╌╌╌.</span>
   <span class="Constant"> .          .</span>
  ]
  3:num/<span class="Special">raw</span> <span class="Special">&lt;-</span> get *e, <span class="Constant">cursor-row:offset</span>
  4:num/<span class="Special">raw</span> <span class="Special">&lt;-</span> get *e, <span class="Constant">cursor-column:offset</span>
  memory-should-contain [
   <span class="Constant"> 3</span> <span class="Special">&lt;-</span><span class="Constant"> 1</span>
   <span class="Constant"> 4</span> <span class="Special">&lt;-</span><span class="Constant"> 0</span>
  ]
  <span class="Comment"># cursor should be in the right place</span>
  assume-console [
    type <span class="Constant">[1]</span>
  ]
  run [
    editor-event-loop screen, console, e
  ]
  screen-should-contain [
   <span class="Constant"> .          .</span>
   <span class="Constant"> .1c        .</span>
   <span class="Constant"> .def       .</span>
<span class="Constant">    .╌╌╌╌╌╌╌╌╌╌.</span>
   <span class="Constant"> .          .</span>
  ]
]

<span class="muRecipe">after</span> <span class="Constant">&lt;delete-to-start-of-line-begin&gt;</span> [
  top-before:&amp;:duplex-list:char <span class="Special">&lt;-</span> get *editor, <span class="Constant">top-of-screen:offset</span>
]
<span class="muRecipe">before</span> <span class="Constant">&lt;delete-to-start-of-line-end&gt;</span> [
  <span class="Delimiter">{</span>
    <span class="muControl">break-unless</span> deleted-cells  <span class="Comment"># delete failed; don't add an undo operation</span>
    top-after:&amp;:duplex-list:char <span class="Special">&lt;-</span> get *editor, <span class="Constant">top-of-screen:offset</span>
    op:&amp;:operation <span class="Special">&lt;-</span> new <span class="Constant">operation:type</span>
    before-cursor:&amp;:duplex-list:char <span class="Special">&lt;-</span> get *editor, <span class="Constant">before-cursor:offset</span>
    deleted-until:&amp;:duplex-list:char <span class="Special">&lt;-</span> next before-cursor
    cursor-row:num <span class="Special">&lt;-</span> get *editor, <span class="Constant">cursor-row:offset</span>
    cursor-column:num <span class="Special">&lt;-</span> get *editor, <span class="Constant">cursor-column:offset</span>
    *op <span class="Special">&lt;-</span> merge <span class="Constant">2/delete-operation</span>, save-row/before, save-column/before, top-before, cursor-row/after, cursor-column/after, top-after, deleted-cells/deleted, before-cursor/delete-from, deleted-until, <span class="Constant">0/never-coalesce</span>
    editor <span class="Special">&lt;-</span> add-operation editor, op
<span class="Constant">    +done-adding-delete-operation</span>
  <span class="Delimiter">}</span>
]

<span class="muScenario">scenario</span> editor-can-undo-and-redo-ctrl-u-2 [
  <span class="Constant">local-scope</span>
  <span class="Comment"># create an editor</span>
  assume-screen <span class="Constant">10/width</span>, <span class="Constant">5/height</span>
  e:&amp;:editor <span class="Special">&lt;-</span> new-editor <span class="Constant">[]</span>, screen, <span class="Constant">0/left</span>, <span class="Constant">10/right</span>
  editor-render screen, e
  <span class="Comment"># insert some text and hit delete and backspace a few times</span>
  assume-console [
    type <span class="Constant">[abc]</span>
    press ctrl-u
    press ctrl-z
  ]
  editor-event-loop screen, console, e
  screen-should-contain [
   <span class="Constant"> .          .</span>
   <span class="Constant"> .abc       .</span>
<span class="Constant">    .╌╌╌╌╌╌╌╌╌╌.</span>
   <span class="Constant"> .          .</span>
  ]
]
</pre>
</body>
</html>
<!-- vim: set foldmethod=manual : -->