about summary refs log tree commit diff stats
path: root/sandbox/012-editor-undo.mu
blob: 69afd2076f517d3084d606a5302856e54db2df53 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
pre { line-height: 125%; }
td.linenos .normal { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
span.linenos { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
td.linenos .special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
span.linenos.special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
.highlight .hll { background-color: #ffffcc }
.highlight .c { color: #888888 } /* Comment */
.highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */
.highlight .k { color: #008800; font-weight: bold } /* Keyword */
.highlight .ch { color: #888888 } /* Comment.Hashbang */
.highlight .cm { color: #888888 } /* Comment.Multiline */
.highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */
.highlight .cpf { color: #888888 } /* Comment.PreprocFile */
.highlight .c1 { color: #888888 } /* Comment.Single */
.highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */
.highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .ges { font-weight: bold; font-style: italic } /* Generic.EmphStrong */
.highlight .gr { color: #aa0000 } /* Generic.Error */
.highlight .gh { color: #333333 } /* Generic.Heading */
.highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */
.highlight .go { color: #888888 } /* Generic.Output */
.highlight .gp { color: #555555 } /* Generic.Prompt */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #666666 } /* Generic.Subheading */
.highlight .gt { color: #aa0000 } /* Generic.Traceback */
.highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */
.highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */
.highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */
.highlight .kp { color: #008800 } /* Keyword.Pseudo */
.highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */
.highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */
.highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */
.highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */
.highlight .na { color: #336699 } /* Name.Attribute */
.highlight .nb { color: #003388 } /* Name.Builtin */
.highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */
.highlight .no { color: #003366; font-weight: bold } /* Name.Constant */
.highlight .nd { color: #555555 } /* Name.Decorator */
.highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */
.highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */
.highlight .nl { color: #336699; font-style: italic } /* Name.Label */
.highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */
.highlight .py { color: #336699; font-weight: bold } /* Name.Property */
.highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */
.highlight .nv { color: #336699 } /* Name.Variable */
.highlight .ow { color: #008800 } /* Operator.Word */
.highlight .w { color: #bbbbbb } /* Text.Whitespace */
.highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */
.highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */
.highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */
.highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */
.highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */
.highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */
.highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */
.highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */
.highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */
.highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */
.highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */
.highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */
.highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */
.highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */
.highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */
.highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */
.highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */
.highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */
.highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */
.highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */
.highlight .vc { color: #336699 } /* Name.Variable.Class */
.highlight .vg { color: #dd7700 } /* Name.Variable.Global */
.highlight .vi { color: #3333bb } /* Name.Variable.Instance */
.highlight .vm { color: #336699 } /* Name.Variable.Magic */
.highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
# http://rosettacode.org/wiki/N-queens_problem
# port of the Arc solution at http://arclanguage.org/item?id=19743
# run with tracing turned on:
#   ./mu --trace nqueens.mu

container square [
  rank:num
  file:num
]

def nqueens n:num, queens:&:list:square -> result:num [
  local-scope
  load-ingredients
  # if 'queens' is already long enough, print it and return
  added-so-far:num <- length queens
  {
    done?:bool <- greater-or-equal added-so-far, n
    break-unless done?
    stash queens
    return 1
  }
  # still work to do
  next-rank:num <- copy 0
  {
    break-unless queens
    first:square <- first queens
    existing-rank:num <- get first, rank:offset
    next-rank <- add existing-rank, 1
  }
  result <- copy 0
  next-file:num <- copy 0
  {
    done?:bool <- greater-or-equal next-file, n
    break-if done?
    curr:square <- merge next-rank, next-file
    {
      curr-conflicts?:bool <- conflict? curr, queens
      break-if curr-conflicts?
      new-queens:&:list:square <- push curr, queens
      sub-result:num <- nqueens n, new-queens
      result <- add result, sub-result
    }
    next-file <- add next-file, 1
    loop
  }
]

def conflict? curr:square, queens:&:list:square -> result:bool [
  local-scope
  load-ingredients
  result1:bool <- conflicting-file? curr, queens
  reply-if result1, result1
  result2:bool <- conflicting-diagonal? curr, queens
  reply result2
]

def conflicting-file? curr:square, queens:&:list:square -> result:bool [
  local-scope
  load-ingredients
  curr-file:num <- get curr, file:offset
  {
    break-unless queens
    q:square <- first queens
    qfile:num <- get q, file:offset
    file-match?:bool <- equal curr-file, qfile
    reply-if file-match?, 1/conflict-found
    queens <- rest queens
    loop
  }
  reply 0/no-conflict-found
]

def conflicting-diagonal? curr:square, queens:&:list:square -> result:bool [
  local-scope
  load-ingredients
  curr-rank:num <- get curr, rank:offset
  curr-file:num <- get curr, file:offset
  {
    break-unless queens
    q:square <- first queens
    qrank:num <- get q, rank:offset
    qfile:num <- get q, file:offset
    rank-delta:num <- subtract qrank, curr-rank
    file-delta:num <- subtract qfile, curr-file
    rank-delta <- abs rank-delta
    file-delta <- abs file-delta
    diagonal-match?:bool <- equal rank-delta, file-delta
    reply-if diagonal-match?, 1/conflict-found
    queens <- rest queens
    loop
  }
  reply 0/no-conflict-found
]

def main [
  nqueens 4
  $dump-trace [app]
]
696'>1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907
## undo/redo

# for every undoable event, create a type of *operation* that contains all the
# information needed to reverse it
exclusive-container operation [
  typing:insert-operation
  move:move-operation
  delete:delete-operation
]

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

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

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

# every editor accumulates a list of operations to undo/redo
container editor [
  undo:&:list:&:operation
  redo:&:list:&:operation
]

# ctrl-z - undo operation
after <handle-special-character> [
  {
    undo?:bool <- equal c, 26/ctrl-z
    break-unless undo?
    undo:&:list:&:operation <- get *editor, undo:offset
    break-unless undo
    op:&:operation <- first undo
    undo <- rest undo
    *editor <- put *editor, undo:offset, undo
    redo:&:list:&:operation <- get *editor, redo:offset
    redo <- push op, redo
    *editor <- put *editor, redo:offset, redo
    <handle-undo>
    return true/go-render
  }
]

# ctrl-y - redo operation
after <handle-special-character> [
  {
    redo?:bool <- equal c, 25/ctrl-y
    break-unless redo?
    redo:&:list:&:operation <- get *editor, redo:offset
    break-unless redo
    op:&:operation <- first redo
    redo <- rest redo
    *editor <- put *editor, redo:offset, redo
    undo:&:list:&:operation <- get *editor, undo:offset
    undo <- push op, undo
    *editor <- put *editor, undo:offset, undo
    <handle-redo>
    return true/go-render
  }
]

# undo typing

scenario editor-can-undo-typing [
  local-scope
  # create an editor and type a character
  assume-screen 10/width, 5/height
  e:&:editor <- new-editor [], 0/left, 10/right
  editor-render screen, e
  assume-console [
    type [0]
  ]
  editor-event-loop screen, console, e
  # undo
  assume-console [
    press ctrl-z
  ]
  run [
    editor-event-loop screen, console, e
  ]
  # character should be gone
  screen-should-contain [
    .          .
    .          .
    .┈┈┈┈┈┈┈┈┈┈.
    .          .
  ]
  # cursor should be in the right place
  assume-console [
    type [1]
  ]
  run [
    editor-event-loop screen, console, e
  ]
  screen-should-contain [
    .          .
    .1         .
    .┈┈┈┈┈┈┈┈┈┈.
    .          .
  ]
]

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

# enter operations never coalesce with typing before or after
after <begin-insert-enter> [
  cursor-row-before:num <- copy cursor-row
  cursor-column-before:num <- copy cursor-column
  top-before:&:duplex-list:char <- get *editor, top-of-screen:offset
  cursor-before:&:duplex-list:char <- get *editor, before-cursor:offset
]
before <end-insert-enter> [
  top-after:&:duplex-list:char <- get *editor, top-of-screen:offset
  cursor-row:num <- get *editor, cursor-row:offset
  cursor-column:num <- get *editor, cursor-row:offset
  # never coalesce
  insert-from:&:duplex-list:char <- next cursor-before
  before-cursor:&:duplex-list:char <- get *editor, before-cursor:offset
  insert-to:&:duplex-list:char <- next before-cursor
  op:&:operation <- new operation:type
  *op <- merge 0/insert-operation, cursor-row-before, cursor-column-before, top-before, cursor-row/after, cursor-column/after, top-after, insert-from, insert-to, 0/never-coalesce
  editor <- add-operation editor, op
]

# Everytime you add a new operation to the undo stack, be sure to clear the
# redo stack, because it's now obsolete.
# Beware: since we're counting cursor moves as operations, this means just
# moving the cursor can lose work on the undo stack.
def add-operation editor:&:editor, op:&:operation -> editor:&:editor [
  local-scope
  load-inputs
  undo:&:list:&:operation <- get *editor, undo:offset
  undo <- push op undo
  *editor <- put *editor, undo:offset, undo
  redo:&:list:&:operation <- get *editor, redo:offset
  redo <- copy null
  *editor <- put *editor, redo:offset, redo
]

after <handle-undo> [
  {
    typing:insert-operation, is-insert?:bool <- maybe-convert *op, typing:variant
    break-unless is-insert?
    start:&:duplex-list:char <- get typing, insert-from:offset
    end:&:duplex-list:char <- get typing, insert-until:offset
    # assert cursor-row/cursor-column/top-of-screen match after-row/after-column/after-top-of-screen
    before-cursor:&:duplex-list:char <- prev start
    *editor <- put *editor, before-cursor:offset, before-cursor
    remove-between before-cursor, end
    cursor-row <- get typing, before-row:offset
    *editor <- put *editor, cursor-row:offset, cursor-row
    cursor-column <- get typing, before-column:offset
    *editor <- put *editor, cursor-column:offset, cursor-column
    top:&:duplex-list:char <- get typing, before-top-of-screen:offset
    *editor <- put *editor, top-of-screen:offset, top
  }
]

scenario editor-can-undo-typing-multiple [
  local-scope
  # create an editor and type multiple characters
  assume-screen 10/width, 5/height
  e:&:editor <- new-editor [], 0/left, 10/right
  editor-render screen, e
  assume-console [
    type [012]
  ]
  editor-event-loop screen, console, e
  # undo
  assume-console [
    press ctrl-z
  ]
  run [
    editor-event-loop screen, console, e
  ]
  # all characters must be gone
  screen-should-contain [
    .          .
    .          .
    .┈┈┈┈┈┈┈┈┈┈.
    .          .
  ]
]

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

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

# redo typing

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

after <handle-redo> [
  {
    typing:insert-operation, is-insert?:bool <- maybe-convert *op, typing:variant
    break-unless is-insert?
    before-cursor <- get *editor, before-cursor:offset
    insert-from:&:duplex-list:char <- get typing, insert-from:offset  # ignore insert-to because it's already been spliced away
    # assert insert-to matches next(before-cursor)
    splice before-cursor, insert-from
    # assert cursor-row/cursor-column/top-of-screen match after-row/after-column/after-top-of-screen
    cursor-row <- get typing, after-row:offset
    *editor <- put *editor, cursor-row:offset, cursor-row
    cursor-column <- get typing, after-column:offset
    *editor <- put *editor, cursor-column:offset, cursor-column
    top:&:duplex-list:char <- get typing, after-top-of-screen:offset
    *editor <- put *editor, top-of-screen:offset, top
  }
]

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

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

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

# undo cursor movement

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

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

after <handle-undo> [
  {
    move:move-operation, is-move?:bool <- maybe-convert *op, move:variant
    break-unless is-move?
    # assert cursor-row/cursor-column/top-of-screen match after-row/after-column/after-top-of-screen
    cursor-row <- get move, before-row:offset
    *editor <- put *editor, cursor-row:offset, cursor-row
    cursor-column <- get move, before-column:offset
    *editor <- put *editor, cursor-column:offset, cursor-column
    top:&:duplex-list:char <- get move, before-top-of-screen:offset
    *editor <- put *editor, top-of-screen:offset, top
  }
]

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

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

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

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

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

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

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

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

# redo cursor movement

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

after <handle-redo> [
  {
    move:move-operation, is-move?:bool <- maybe-convert *op, move:variant
    break-unless is-move?
    # assert cursor-row/cursor-column/top-of-screen match after-row/after-column/after-top-of-screen
    cursor-row <- get move, after-row:offset
    *editor <- put *editor, cursor-row:offset, cursor-row
    cursor-column <- get move, after-column:offset
    *editor <- put *editor, cursor-column:offset, cursor-column
    top:&:duplex-list:char <- get move, after-top-of-screen:offset
    *editor <- put *editor, top-of-screen:offset, top
  }
]

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

# undo backspace

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

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

after <handle-undo> [
  {
    deletion:delete-operation, is-delete?:bool <- maybe-convert *op, delete:variant
    break-unless is-delete?
    anchor:&:duplex-list:char <- get deletion, delete-from:offset
    break-unless anchor
    deleted:&:duplex-list:char <- get deletion, deleted-text:offset
    old-cursor:&:duplex-list:char <- last deleted
    splice anchor, deleted
    # assert cursor-row/cursor-column/top-of-screen match after-row/after-column/after-top-of-screen
    before-cursor <- copy old-cursor
    cursor-row <- get deletion, before-row:offset
    *editor <- put *editor, cursor-row:offset, cursor-row
    cursor-column <- get deletion, before-column:offset
    *editor <- put *editor, cursor-column:offset, cursor-column
    top:&:duplex-list:char <- get deletion, before-top-of-screen:offset
    *editor <- put *editor, top-of-screen:offset, top
  }
]

after <handle-redo> [
  {
    deletion:delete-operation, is-delete?:bool <- maybe-convert *op, delete:variant
    break-unless is-delete?
    start:&:duplex-list:char <- get deletion, delete-from:offset
    end:&:duplex-list:char <- get deletion, delete-until:offset
    data:&:duplex-list:char <- get *editor, data:offset
    remove-between start, end
    # assert cursor-row/cursor-column/top-of-screen match after-row/after-column/after-top-of-screen
    cursor-row <- get deletion, after-row:offset
    *editor <- put *editor, cursor-row:offset, cursor-row
    cursor-column <- get deletion, after-column:offset
    *editor <- put *editor, cursor-column:offset, cursor-column
    top:&:duplex-list:char <- get deletion, before-top-of-screen:offset
    *editor <- put *editor, top-of-screen:offset, top
  }
]

# undo delete

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

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

# undo ctrl-k

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

after <begin-delete-to-end-of-line> [
  top-before:&:duplex-list:char <- get *editor, top-of-screen:offset
]
before <end-delete-to-end-of-line> [
  {
    break-unless deleted-cells  # delete failed; don't add an undo operation
    top-after:&:duplex-list:char <- get *editor, top-of-screen:offset
    cursor-row:num <- get *editor, cursor-row:offset
    cursor-column:num <- get *editor, cursor-column:offset
    deleted-until:&:duplex-list:char <- next before-cursor
    op:&:operation <- new operation:type
    *op <- merge 2/delete-operation, 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, 0/never-coalesce
    editor <- add-operation editor, op
    +done-adding-delete-operation
  }
]

# undo ctrl-u

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

after <begin-delete-to-start-of-line> [
  top-before:&:duplex-list:char <- get *editor, top-of-screen:offset
]
before <end-delete-to-start-of-line> [
  {
    break-unless deleted-cells  # delete failed; don't add an undo operation
    top-after:&:duplex-list:char <- get *editor, top-of-screen:offset
    op:&:operation <- new operation:type
    before-cursor:&:duplex-list:char <- get *editor, before-cursor:offset
    deleted-until:&:duplex-list:char <- next before-cursor
    cursor-row:num <- get *editor, cursor-row:offset
    cursor-column:num <- get *editor, cursor-column:offset
    *op <- merge 2/delete-operation, 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, 0/never-coalesce
    editor <- add-operation editor, op
    +done-adding-delete-operation
  }
]

scenario editor-can-undo-and-redo-ctrl-u-2 [
  local-scope
  # create an editor
  assume-screen 10/width, 5/height
  e:&:editor <- new-editor [], 0/left, 10/right
  editor-render screen, e
  # insert some text and hit delete and backspace a few times
  assume-console [
    type [abc]
    press ctrl-u
    press ctrl-z
  ]
  editor-event-loop screen, console, e
  screen-should-contain [
    .          .
    .abc       .
    .┈┈┈┈┈┈┈┈┈┈.
    .          .
  ]
]