about summary refs log tree commit diff stats
path: root/html/edit.mu.html
blob: 8481c441864112c0a4c16dffaa887e34aabe725d (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
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
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
<!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.mu</title>
<meta name="Generator" content="Vim/7.4">
<meta name="plugin-version" content="vim7.4_v1">
<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-family: monospace; color: #eeeeee; background-color: #080808; }
* { font-size: 1.05em; }
.muScenario { color: #00af00; }
.Delimiter { color: #a04060; }
.SalientComment { color: #00ffff; }
.Comment { color: #9090ff; }
.Constant { color: #00a0a0; }
.Special { color: #ff6060; }
.CommentedCode { color: #6c6c6c; }
.muControl { color: #c0a020; }
.muRecipe { color: #ff8700; }
-->
</style>

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

-->
</script>
</head>
<body>
<pre id='vimCodeElement'>
<span class="Comment"># Editor widget: takes a string and screen coordinates, modifying them in place.</span>

<span class="muRecipe">recipe</span> main [
  <span class="Constant">default-space</span>:address:array:location<span class="Special"> &lt;- </span>new location:type, <span class="Constant">30:literal</span>
  open-console
  width:number<span class="Special"> &lt;- </span>display-width
  height:number<span class="Special"> &lt;- </span>display-height
  <span class="Comment"># draw a line</span>
  divider:number, _<span class="Special"> &lt;- </span>divide-with-remainder width:number, <span class="Constant">2:literal</span>
  draw-vertical <span class="Constant">0:literal/screen</span>, divider:number, <span class="Constant">0:literal/top</span>, height:number
  <span class="Comment"># editor on the left</span>
  left:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[abcde]</span>
  left-editor:address:editor-data<span class="Special"> &lt;- </span>new-editor left:address:array:character, <span class="Constant">0:literal/screen</span>, <span class="Constant">0:literal/top</span>, <span class="Constant">0:literal/left</span>, <span class="Constant">5:literal/right</span> <span class="Comment">#divider:number/right</span>
  <span class="Comment"># editor on the right</span>
  right:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[def]</span>
  new-left:number<span class="Special"> &lt;- </span>add divider:number/right, <span class="Constant">1:literal</span>
  right-editor:address:editor-data<span class="Special"> &lt;- </span>new-editor right:address:array:character, <span class="Constant">0:literal/screen</span>, <span class="Constant">0:literal/top</span>, new-left:number, width:number
  <span class="Comment"># chain</span>
  x:address:address:editor-data<span class="Special"> &lt;- </span>get-address left-editor:address:editor-data/deref, next-editor:offset
  x:address:address:editor-data/deref<span class="Special"> &lt;- </span>copy right-editor:address:editor-data
  <span class="Comment"># initialize focus</span>
  reset-focus left-editor:address:editor-data
  cursor-row:number<span class="Special"> &lt;- </span>get left-editor:address:editor-data/deref, cursor-row:offset
  cursor-column:number<span class="Special"> &lt;- </span>get left-editor:address:editor-data/deref, cursor-column:offset
  move-cursor <span class="Constant">0:literal/screen</span>, cursor-row:number, cursor-column:number
  <span class="Comment"># and we're off!</span>
  event-loop <span class="Constant">0:literal/screen</span>, <span class="Constant">0:literal/events</span>, left-editor:address:editor-data
  close-console
]

<span class="muScenario">scenario</span> editor-initially-prints-string-to-screen [
  assume-screen <span class="Constant">10:literal/width</span>, <span class="Constant">5:literal/height</span>
  run [
    1:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[abc]</span>
    new-editor 1:address:array:character, screen:address, <span class="Constant">0:literal/top</span>, <span class="Constant">0:literal/left</span>, <span class="Constant">10:literal/right</span>
  ]
  screen-should-contain [
   <span class="Constant"> .abc       .</span>
   <span class="Constant"> .          .</span>
  ]
]

<span class="SalientComment">## In which we introduce the editor data structure, and show how it displays</span>
<span class="SalientComment">## text to the screen.</span>

container editor-data [
  <span class="Comment"># doubly linked list of characters (head contains a special sentinel)</span>
  data:address:duplex-list
  <span class="Comment"># location of top-left of screen inside data (scrolling)</span>
  top-of-screen:address:duplex-list
  <span class="Comment"># location before cursor inside data</span>
  before-cursor:address:duplex-list

  screen:address:screen
  <span class="Comment"># raw bounds of display area on screen</span>
  top:number
  left:number
  bottom:number
  right:number
  <span class="Comment"># raw screen coordinates of cursor</span>
  cursor-row:number
  cursor-column:number

  <span class="Comment"># pointer to another editor, responsible for a different area of screen.</span>
  <span class="Comment"># helps organize editors in a 'chain'.</span>
  next-editor:address:editor-data
  in-focus?:boolean  <span class="Comment"># set for the one editor in this chain currently being edited</span>
]

<span class="Comment"># editor:address, screen:address &lt;- new-editor s:address:array:character, screen:address, top:number, left:number, bottom:number</span>
<span class="Comment"># creates a new editor widget and renders its initial appearance to screen.</span>
<span class="Comment">#   top/left/right constrain the screen area available to the new editor.</span>
<span class="Comment">#   right is exclusive.</span>
<span class="muRecipe">recipe</span> new-editor [
  <span class="Constant">default-space</span>:address:array:location<span class="Special"> &lt;- </span>new location:type, <span class="Constant">30:literal</span>
  s:address:array:character<span class="Special"> &lt;- </span><span class="Constant">next-ingredient</span>
  screen:address<span class="Special"> &lt;- </span><span class="Constant">next-ingredient</span>
  <span class="Comment"># no clipping of bounds</span>
  top:number<span class="Special"> &lt;- </span><span class="Constant">next-ingredient</span>
  left:number<span class="Special"> &lt;- </span><span class="Constant">next-ingredient</span>
  right:number<span class="Special"> &lt;- </span><span class="Constant">next-ingredient</span>
  right:number<span class="Special"> &lt;- </span>subtract right:number, <span class="Constant">1:literal</span>
  result:address:editor-data<span class="Special"> &lt;- </span>new editor-data:type
  <span class="Comment"># initialize screen-related fields</span>
  sc:address:address:screen<span class="Special"> &lt;- </span>get-address result:address:editor-data/deref, screen:offset
  sc:address:address:screen/deref<span class="Special"> &lt;- </span>copy screen:address
  x:address:number<span class="Special"> &lt;- </span>get-address result:address:editor-data/deref, top:offset
  x:address:number/deref<span class="Special"> &lt;- </span>copy top:number
  x:address:number<span class="Special"> &lt;- </span>get-address result:address:editor-data/deref, left:offset
  x:address:number/deref<span class="Special"> &lt;- </span>copy left:number
  x:address:number<span class="Special"> &lt;- </span>get-address result:address:editor-data/deref, right:offset
  x:address:number/deref<span class="Special"> &lt;- </span>copy right:number
  <span class="Comment"># bottom = top (in case of early exit)</span>
  x:address:number<span class="Special"> &lt;- </span>get-address result:address:editor-data/deref, bottom:offset
  x:address:number/deref<span class="Special"> &lt;- </span>copy top:number
  <span class="Comment"># initialize cursor</span>
  x:address:number<span class="Special"> &lt;- </span>get-address result:address:editor-data/deref, cursor-row:offset
  x:address:number/deref<span class="Special"> &lt;- </span>copy top:number
  x:address:number<span class="Special"> &lt;- </span>get-address result:address:editor-data/deref, cursor-column:offset
<span class="CommentedCode">#?   $print left:number, [ </span>
<span class="CommentedCode">#? ] #? 1</span>
  x:address:number/deref<span class="Special"> &lt;- </span>copy left:number
  d:address:address:duplex-list<span class="Special"> &lt;- </span>get-address result:address:editor-data/deref, data:offset
  d:address:address:duplex-list/deref<span class="Special"> &lt;- </span>push-duplex <span class="Constant">167:literal/§</span>, <span class="Constant">0:literal/tail</span>
  y:address:address:duplex-list<span class="Special"> &lt;- </span>get-address result:address:editor-data/deref, before-cursor:offset
  y:address:address:duplex-list/deref<span class="Special"> &lt;- </span>copy d:address:address:duplex-list/deref
  init:address:address:duplex-list<span class="Special"> &lt;- </span>get-address result:address:editor-data/deref, top-of-screen:offset
  init:address:address:duplex-list/deref<span class="Special"> &lt;- </span>copy d:address:address:duplex-list/deref
  <span class="Comment"># set focus</span>
  <span class="Comment"># if using multiple editors, must call reset-focus after chaining them all</span>
  b:address:boolean<span class="Special"> &lt;- </span>get-address result:address:editor-data/deref, in-focus?:offset
  b:address:boolean/deref<span class="Special"> &lt;- </span>copy <span class="Constant">1:literal/true</span>
<span class="CommentedCode">#?   $print d:address:address:duplex-list/deref, [ </span>
<span class="CommentedCode">#? ] #? 1</span>
  <span class="Comment"># early exit if s is empty</span>
  <span class="muControl">reply-unless</span> s:address:array:character, result:address:editor-data
  len:number<span class="Special"> &lt;- </span>length s:address:array:character/deref
  <span class="muControl">reply-unless</span> len:number, result:address:editor-data
  idx:number<span class="Special"> &lt;- </span>copy <span class="Constant">0:literal</span>
  <span class="Comment"># now we can start appending the rest, character by character</span>
  curr:address:duplex-list<span class="Special"> &lt;- </span>copy init:address:address:duplex-list/deref
  <span class="Delimiter">{</span>
<span class="CommentedCode">#?     $print idx:number, [ vs ], len:number, [ </span>
<span class="CommentedCode">#? ] #? 1</span>
<span class="CommentedCode">#?     $print [append to ], curr:address:duplex-list, [ </span>
<span class="CommentedCode">#? ] #? 1</span>
    done?:boolean<span class="Special"> &lt;- </span>greater-or-equal idx:number, len:number
    <span class="muControl">break-if</span> done?:boolean
    c:character<span class="Special"> &lt;- </span>index s:address:array:character/deref, idx:number
<span class="CommentedCode">#?     $print [aa: ], c:character, [ </span>
<span class="CommentedCode">#? ] #? 1</span>
    insert-duplex c:character, curr:address:duplex-list
    <span class="Comment"># next iter</span>
    curr:address:duplex-list<span class="Special"> &lt;- </span>next-duplex curr:address:duplex-list
    idx:number<span class="Special"> &lt;- </span>add idx:number, <span class="Constant">1:literal</span>
    <span class="muControl">loop</span>
  <span class="Delimiter">}</span>
  <span class="Comment"># initialize cursor to top of screen</span>
  y:address:address:duplex-list<span class="Special"> &lt;- </span>get-address result:address:editor-data/deref, before-cursor:offset
  y:address:address:duplex-list/deref<span class="Special"> &lt;- </span>copy init:address:address:duplex-list/deref
  <span class="Comment"># perform initial rendering to screen</span>
  bottom:address:number<span class="Special"> &lt;- </span>get-address result:address:editor-data/deref, bottom:offset
  result:address:editor-data<span class="Special"> &lt;- </span>render result:address:editor-data
  <span class="muControl">reply</span> result:address:editor-data
]

<span class="muScenario">scenario</span> editor-initializes-without-data [
  assume-screen <span class="Constant">5:literal/width</span>, <span class="Constant">3:literal/height</span>
  run [
    1:address:editor-data<span class="Special"> &lt;- </span>new-editor <span class="Constant">0:literal/data</span>, screen:address, <span class="Constant">1:literal/top</span>, <span class="Constant">2:literal/left</span>, <span class="Constant">5:literal/right</span>
    2:editor-data<span class="Special"> &lt;- </span>copy 1:address:editor-data/deref
  ]
  memory-should-contain [
    <span class="Comment"># 2 &lt;- just the § sentinel</span>
    <span class="Comment"># 3 (top of screen) &lt;- the § sentinel</span>
    <span class="Comment"># 4 (before cursor) &lt;- the § sentinel</span>
    <span class="Comment"># 5 &lt;- screen</span>
    6<span class="Special"> &lt;- </span>1  <span class="Comment"># top</span>
    7<span class="Special"> &lt;- </span>2  <span class="Comment"># left</span>
    8<span class="Special"> &lt;- </span>1  <span class="Comment"># bottom</span>
    9<span class="Special"> &lt;- </span>4  <span class="Comment"># right  (inclusive)</span>
    10<span class="Special"> &lt;- </span>1  <span class="Comment"># cursor row</span>
    11<span class="Special"> &lt;- </span>2  <span class="Comment"># cursor column</span>
  ]
  screen-should-contain [
   <span class="Constant"> .     .</span>
   <span class="Constant"> .     .</span>
   <span class="Constant"> .     .</span>
  ]
]

<span class="muRecipe">recipe</span> render [
  <span class="Constant">default-space</span>:address:array:location<span class="Special"> &lt;- </span>new location:type, <span class="Constant">40:literal</span>
  editor:address:editor-data<span class="Special"> &lt;- </span><span class="Constant">next-ingredient</span>
<span class="CommentedCode">#?   $print [=== render</span>
<span class="CommentedCode">#? ] #? 2</span>
  screen:address<span class="Special"> &lt;- </span>get editor:address:editor-data/deref, screen:offset
  top:number<span class="Special"> &lt;- </span>get editor:address:editor-data/deref, top:offset
  left:number<span class="Special"> &lt;- </span>get editor:address:editor-data/deref, left:offset
  screen-height:number<span class="Special"> &lt;- </span>screen-height screen:address
  right:number<span class="Special"> &lt;- </span>get editor:address:editor-data/deref, right:offset
  hide-screen screen:address
  <span class="Comment"># traversing editor</span>
  curr:address:duplex-list<span class="Special"> &lt;- </span>get editor:address:editor-data/deref, top-of-screen:offset
  prev:address:duplex-list<span class="Special"> &lt;- </span>copy curr:address:duplex-list
  curr:address:duplex-list<span class="Special"> &lt;- </span>next-duplex curr:address:duplex-list
  <span class="Comment"># traversing screen</span>
  row:number<span class="Special"> &lt;- </span>copy top:number
  column:number<span class="Special"> &lt;- </span>copy left:number
  cursor-row:address:number<span class="Special"> &lt;- </span>get-address editor:address:editor-data/deref, cursor-row:offset
  cursor-column:address:number<span class="Special"> &lt;- </span>get-address editor:address:editor-data/deref, cursor-column:offset
  before-cursor:address:address:duplex-list<span class="Special"> &lt;- </span>get-address editor:address:editor-data/deref, before-cursor:offset
  move-cursor screen:address, row:number, column:number
  <span class="Delimiter">{</span>
<span class="Constant">    +next-character</span>
<span class="CommentedCode">#?     $print curr:address:duplex-list, [ </span>
<span class="CommentedCode">#? ] #? 1</span>
    <span class="muControl">break-unless</span> curr:address:duplex-list
    off-screen?:boolean<span class="Special"> &lt;- </span>greater-or-equal row:number, screen-height:number
    <span class="muControl">break-if</span> off-screen?:boolean
    <span class="Comment"># update editor-data.before-cursor</span>
    <span class="Comment"># Doing so at the start of each iteration ensures it stays one step behind</span>
    <span class="Comment"># the current character.</span>
    <span class="Delimiter">{</span>
      at-cursor-row?:boolean<span class="Special"> &lt;- </span>equal row:number, cursor-row:address:number/deref
      <span class="muControl">break-unless</span> at-cursor-row?:boolean
      at-cursor?:boolean<span class="Special"> &lt;- </span>equal column:number, cursor-column:address:number/deref
      <span class="muControl">break-unless</span> at-cursor?:boolean
      before-cursor:address:address:duplex-list/deref<span class="Special"> &lt;- </span>prev-duplex curr:address:duplex-list
<span class="CommentedCode">#?       new-prev:character &lt;- get before-cursor:address:address:duplex-list/deref/deref, value:offset #? 1</span>
<span class="CommentedCode">#?       $print [render 0: cursor adjusted to after ], new-prev:character, [(], cursor-row:address:number/deref, [, ], cursor-column:address:number/deref, [)</span>
<span class="CommentedCode">#? ] #? 1</span>
    <span class="Delimiter">}</span>
    c:character<span class="Special"> &lt;- </span>get curr:address:duplex-list/deref, value:offset
<span class="CommentedCode">#?     $print [rendering ], c:character, [ </span>
<span class="CommentedCode">#? ] #? 2</span>
    <span class="Delimiter">{</span>
      <span class="Comment"># newline? move to left rather than 0</span>
      newline?:boolean<span class="Special"> &lt;- </span>equal c:character, <span class="Constant">10:literal/newline</span>
      <span class="muControl">break-unless</span> newline?:boolean
      <span class="Comment"># adjust cursor if necessary</span>
      <span class="Delimiter">{</span>
        at-cursor-row?:boolean<span class="Special"> &lt;- </span>equal row:number, cursor-row:address:number/deref
        <span class="muControl">break-unless</span> at-cursor-row?:boolean
        left-of-cursor?:boolean<span class="Special"> &lt;- </span>lesser-than column:number, cursor-column:address:number/deref
        <span class="muControl">break-unless</span> left-of-cursor?:boolean
        cursor-column:address:number/deref<span class="Special"> &lt;- </span>copy column:number
        before-cursor:address:address:duplex-list/deref<span class="Special"> &lt;- </span>prev-duplex curr:address:duplex-list
<span class="CommentedCode">#?         new-prev:character &lt;- get before-cursor:address:address:duplex-list/deref/deref, value:offset #? 1</span>
<span class="CommentedCode">#?         $print [render 1: cursor adjusted to after ], new-prev:character, [(], cursor-row:address:number/deref, [, ], cursor-column:address:number/deref, [)</span>
<span class="CommentedCode">#? ] #? 1</span>
      <span class="Delimiter">}</span>
      <span class="Comment"># clear rest of line in this window</span>
<span class="CommentedCode">#?       $print row:number, [ ], column:number, [ ], right:number, [ </span>
<span class="CommentedCode">#? ] #? 1</span>
      <span class="Delimiter">{</span>
        done?:boolean<span class="Special"> &lt;- </span>greater-than column:number, right:number
        <span class="muControl">break-if</span> done?:boolean
        print-character screen:address, <span class="Constant">32:literal/space</span>
        column:number<span class="Special"> &lt;- </span>add column:number, <span class="Constant">1:literal</span>
<span class="CommentedCode">#?         $print column:number, [ </span>
<span class="CommentedCode">#? ] #? 1</span>
        <span class="muControl">loop</span>
      <span class="Delimiter">}</span>
      <span class="Comment"># skip to next line</span>
      row:number<span class="Special"> &lt;- </span>add row:number, <span class="Constant">1:literal</span>
      column:number<span class="Special"> &lt;- </span>copy left:number
      move-cursor screen:address, row:number, column:number
      curr:address:duplex-list<span class="Special"> &lt;- </span>next-duplex curr:address:duplex-list
      prev:address:duplex-list<span class="Special"> &lt;- </span>next-duplex prev:address:duplex-list
      <span class="muControl">loop</span> <span class="Constant">+next-character:label</span>
    <span class="Delimiter">}</span>
    <span class="Delimiter">{</span>
      <span class="Comment"># at right? more than one letter left in the line? wrap</span>
      at-right?:boolean<span class="Special"> &lt;- </span>equal column:number, right:number
      <span class="muControl">break-unless</span> at-right?:boolean
      next-node:address:duplex-list<span class="Special"> &lt;- </span>next-duplex curr:address:duplex-list
      <span class="muControl">break-unless</span> next-node:address:duplex-list
      next:character<span class="Special"> &lt;- </span>get next-node:address:duplex-list/deref, value:offset
      next-character-is-newline?:boolean<span class="Special"> &lt;- </span>equal next:character, <span class="Constant">10:literal/newline</span>
      <span class="muControl">break-if</span> next-character-is-newline?:boolean
      <span class="Comment"># wrap</span>
      print-character screen:address, <span class="Constant">8617:literal/loop-back-to-left</span>, <span class="Constant">245:literal/grey</span>
      column:number<span class="Special"> &lt;- </span>copy left:number
      row:number<span class="Special"> &lt;- </span>add row:number, <span class="Constant">1:literal</span>
      move-cursor screen:address, row:number, column:number
      <span class="Comment"># don't increment curr</span>
      <span class="muControl">loop</span> <span class="Constant">+next-character:label</span>
    <span class="Delimiter">}</span>
    print-character screen:address, c:character
    curr:address:duplex-list<span class="Special"> &lt;- </span>next-duplex curr:address:duplex-list
    prev:address:duplex-list<span class="Special"> &lt;- </span>next-duplex prev:address:duplex-list
    column:number<span class="Special"> &lt;- </span>add column:number, <span class="Constant">1:literal</span>
    <span class="muControl">loop</span>
  <span class="Delimiter">}</span>
  <span class="Comment"># bottom = row</span>
  bottom:address:number<span class="Special"> &lt;- </span>get-address editor:address:editor-data/deref, bottom:offset
  bottom:address:number/deref<span class="Special"> &lt;- </span>copy row:number
  <span class="Comment"># is cursor to the right of the last line? move to end</span>
  <span class="Delimiter">{</span>
    at-cursor-row?:boolean<span class="Special"> &lt;- </span>equal row:number, cursor-row:address:number/deref
    cursor-outside-line?:boolean<span class="Special"> &lt;- </span>lesser-or-equal column:number, cursor-column:address:number/deref
    before-cursor-on-same-line?:boolean<span class="Special"> &lt;- </span>and at-cursor-row?:boolean, cursor-outside-line?:boolean
    above-cursor-row?:boolean<span class="Special"> &lt;- </span>lesser-than row:number, cursor-row:address:number/deref
    before-cursor?:boolean<span class="Special"> &lt;- </span>or before-cursor-on-same-line?:boolean, above-cursor-row?:boolean
    <span class="muControl">break-unless</span> before-cursor?:boolean
<span class="CommentedCode">#?     $print [pointed after all text</span>
<span class="CommentedCode">#? ] #? 1</span>
    cursor-row:address:number/deref<span class="Special"> &lt;- </span>copy row:number
    cursor-column:address:number/deref<span class="Special"> &lt;- </span>copy column:number
<span class="CommentedCode">#?     $print [render: cursor moved to ], cursor-row:address:number/deref, [, ], cursor-column:address:number/deref, [ </span>
<span class="CommentedCode">#? ] #? 1</span>
    <span class="Comment"># line not wrapped but cursor outside bounds? wrap cursor</span>
    <span class="Delimiter">{</span>
      too-far-right?:boolean<span class="Special"> &lt;- </span>greater-than cursor-column:address:number/deref, right:number
      <span class="muControl">break-unless</span> too-far-right?:boolean
      cursor-column:address:number/deref<span class="Special"> &lt;- </span>copy left:number
      cursor-row:address:number/deref<span class="Special"> &lt;- </span>add cursor-row:address:number/deref, <span class="Constant">1:literal</span>
      above-screen-bottom?:boolean<span class="Special"> &lt;- </span>lesser-than cursor-row:address:number/deref, screen-height:number
      assert above-screen-bottom?:boolean, <span class="Constant">[unimplemented: wrapping cursor past bottom of screen]</span>
    <span class="Delimiter">}</span>
<span class="CommentedCode">#?     $print [now ], cursor-row:address:number/deref, [, ], cursor-column:address:number/deref, [ </span>
<span class="CommentedCode">#? ] #? 1</span>
    before-cursor:address:address:duplex-list/deref<span class="Special"> &lt;- </span>copy prev:address:duplex-list
<span class="CommentedCode">#?     new-prev:character &lt;- get before-cursor:address:address:duplex-list/deref/deref, value:offset #? 1</span>
<span class="CommentedCode">#?     $print [render Ω: cursor adjusted to after ], new-prev:character, [(], cursor-row:address:number/deref, [, ], cursor-column:address:number/deref, [)</span>
<span class="CommentedCode">#? ] #? 1</span>
  <span class="Delimiter">}</span>
<span class="CommentedCode">#?   $print [clearing ], row:number, [ ], column:number, [ ], right:number, [ </span>
<span class="CommentedCode">#? ] #? 2</span>
  <span class="Delimiter">{</span>
    <span class="Comment"># clear rest of current line</span>
    done?:boolean<span class="Special"> &lt;- </span>greater-or-equal row:number, screen-height:number
    <span class="muControl">break-if</span> done?:boolean
    <span class="Delimiter">{</span>
      line-done?:boolean<span class="Special"> &lt;- </span>greater-than column:number, right:number
      <span class="muControl">break-if</span> line-done?:boolean
      print-character screen:address, <span class="Constant">32:literal/space</span>
      column:number<span class="Special"> &lt;- </span>add column:number, <span class="Constant">1:literal</span>
      <span class="muControl">loop</span>
    <span class="Delimiter">}</span>
    <span class="Comment"># clear one more line just in case we just backspaced out of it</span>
    row:number<span class="Special"> &lt;- </span>add row:number, <span class="Constant">1:literal</span>
    column:number<span class="Special"> &lt;- </span>copy left:number
    done?:boolean<span class="Special"> &lt;- </span>greater-or-equal row:number, screen-height:number
    <span class="muControl">break-if</span> done?:boolean
    move-cursor screen:address, row:number, column:number
    <span class="Delimiter">{</span>
      line-done?:boolean<span class="Special"> &lt;- </span>greater-or-equal column:number, right:number
      <span class="muControl">break-if</span> line-done?:boolean
      print-character screen:address, <span class="Constant">32:literal/space</span>
      column:number<span class="Special"> &lt;- </span>add column:number, <span class="Constant">1:literal</span>
      <span class="muControl">loop</span>
    <span class="Delimiter">}</span>
  <span class="Delimiter">}</span>
  <span class="Comment"># update cursor</span>
  <span class="Delimiter">{</span>
    in-focus?:boolean<span class="Special"> &lt;- </span>get editor:address:editor-data/deref, in-focus?:offset
    <span class="muControl">break-unless</span> in-focus?:boolean
    cursor-inside-right-margin?:boolean<span class="Special"> &lt;- </span>lesser-or-equal cursor-column:address:number/deref, right:number
    assert cursor-inside-right-margin?:boolean, <span class="Constant">[cursor outside right margin]</span>
    cursor-inside-left-margin?:boolean<span class="Special"> &lt;- </span>greater-or-equal cursor-column:address:number/deref, left:number
    assert cursor-inside-left-margin?:boolean, <span class="Constant">[cursor outside left margin]</span>
    move-cursor screen:address, cursor-row:address:number/deref, cursor-column:address:number/deref
  <span class="Delimiter">}</span>
  show-screen screen:address
  <span class="muControl">reply</span> editor:address:editor-data/same-as-ingredient:0
]

<span class="muScenario">scenario</span> editor-initially-prints-multiple-lines [
  assume-screen <span class="Constant">5:literal/width</span>, <span class="Constant">3:literal/height</span>
  run [
    s:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[abc</span>
<span class="Constant">def]</span>
    new-editor s:address:array:character, screen:address, <span class="Constant">0:literal/top</span>, <span class="Constant">0:literal/left</span>, <span class="Constant">5:literal/right</span>
  ]
  screen-should-contain [
   <span class="Constant"> .abc  .</span>
   <span class="Constant"> .def  .</span>
   <span class="Constant"> .     .</span>
  ]
]

<span class="muScenario">scenario</span> editor-initially-handles-offsets [
  assume-screen <span class="Constant">5:literal/width</span>, <span class="Constant">3:literal/height</span>
  run [
    s:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[abc]</span>
    new-editor s:address:array:character, screen:address, <span class="Constant">0:literal/top</span>, <span class="Constant">1:literal/left</span>, <span class="Constant">5:literal/right</span>
  ]
  screen-should-contain [
   <span class="Constant"> . abc .</span>
   <span class="Constant"> .     .</span>
   <span class="Constant"> .     .</span>
  ]
]

<span class="muScenario">scenario</span> editor-initially-prints-multiple-lines-at-offset [
  assume-screen <span class="Constant">5:literal/width</span>, <span class="Constant">3:literal/height</span>
  run [
    s:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[abc</span>
<span class="Constant">def]</span>
    new-editor s:address:array:character, screen:address, <span class="Constant">0:literal/top</span>, <span class="Constant">1:literal/left</span>, <span class="Constant">5:literal/right</span>
  ]
  screen-should-contain [
   <span class="Constant"> . abc .</span>
   <span class="Constant"> . def .</span>
   <span class="Constant"> .     .</span>
  ]
]

<span class="muScenario">scenario</span> editor-initially-wraps-long-lines [
  assume-screen <span class="Constant">5:literal/width</span>, <span class="Constant">3:literal/height</span>
  run [
    s:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[abc def]</span>
    new-editor s:address:array:character, screen:address, <span class="Constant">0:literal/top</span>, <span class="Constant">0:literal/left</span>, <span class="Constant">5:literal/right</span>
  ]
  screen-should-contain [
   <span class="Constant"> .abc ↩.</span>
   <span class="Constant"> .def  .</span>
   <span class="Constant"> .     .</span>
  ]
  screen-should-contain-in-color, <span class="Constant">245:literal/grey</span> [
   <span class="Constant"> .    ↩.</span>
   <span class="Constant"> .     .</span>
   <span class="Constant"> .     .</span>
  ]
]

<span class="muScenario">scenario</span> editor-initializes-empty-text [
  assume-screen <span class="Constant">5:literal/width</span>, <span class="Constant">3:literal/height</span>
  run [
    1:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[]</span>
    2:address:editor-data<span class="Special"> &lt;- </span>new-editor 1:address:array:character, screen:address, <span class="Constant">0:literal/top</span>, <span class="Constant">0:literal/left</span>, <span class="Constant">5:literal/right</span>
    3:number<span class="Special"> &lt;- </span>get 2:address:editor-data/deref, cursor-row:offset
    4:number<span class="Special"> &lt;- </span>get 2:address:editor-data/deref, cursor-column:offset
  ]
  screen-should-contain [
   <span class="Constant"> .     .</span>
   <span class="Constant"> .     .</span>
   <span class="Constant"> .     .</span>
  ]
  memory-should-contain [
    3<span class="Special"> &lt;- </span>0  <span class="Comment"># cursor row</span>
    4<span class="Special"> &lt;- </span>0  <span class="Comment"># cursor column</span>
  ]
]

<span class="SalientComment">## handling events from the keyboard, mouse, touch screen, ...</span>

<span class="Comment"># Takes a chain of editors (chained using editor-data.next-editor), sends each</span>
<span class="Comment"># event from the console to each editor.</span>
<span class="muRecipe">recipe</span> event-loop [
  <span class="Constant">default-space</span>:address:array:location<span class="Special"> &lt;- </span>new location:type, <span class="Constant">30:literal</span>
  screen:address<span class="Special"> &lt;- </span><span class="Constant">next-ingredient</span>
  console:address<span class="Special"> &lt;- </span><span class="Constant">next-ingredient</span>
  editor:address:editor-data<span class="Special"> &lt;- </span><span class="Constant">next-ingredient</span>
  <span class="Delimiter">{</span>
    <span class="Comment"># send each event to each editor</span>
    e:event, console:address, found?:boolean, quit?:boolean<span class="Special"> &lt;- </span>read-event console:address
    <span class="muControl">loop-unless</span> found?:boolean
    <span class="muControl">break-if</span> quit?:boolean  <span class="Comment"># only in tests</span>
    trace <span class="Constant">[app]</span>, <span class="Constant">[next-event]</span>
<span class="CommentedCode">#?     $print [--- new event</span>
<span class="CommentedCode">#? ] #? 1</span>
    curr:address:editor-data<span class="Special"> &lt;- </span>copy editor:address:editor-data
    <span class="Delimiter">{</span>
      <span class="muControl">break-unless</span> curr:address:editor-data
      handle-event screen:address, console:address, curr:address:editor-data, e:event
      curr:address:editor-data<span class="Special"> &lt;- </span>get curr:address:editor-data/deref, next-editor:offset
      <span class="muControl">loop</span>
    <span class="Delimiter">}</span>
    <span class="Comment"># after each non-trivial event, render all editors</span>
    curr:address:editor-data<span class="Special"> &lt;- </span>copy editor:address:editor-data
    <span class="Delimiter">{</span>
      <span class="muControl">break-unless</span> curr:address:editor-data
      render curr:address:editor-data
      curr:address:editor-data<span class="Special"> &lt;- </span>get curr:address:editor-data/deref, next-editor:offset
      <span class="muControl">loop</span>
    <span class="Delimiter">}</span>
    <span class="Comment"># ..and position the cursor</span>
    curr:address:editor-data<span class="Special"> &lt;- </span>copy editor:address:editor-data
    <span class="Delimiter">{</span>
      <span class="muControl">break-unless</span> curr:address:editor-data
      <span class="Delimiter">{</span>
        in-focus?:boolean<span class="Special"> &lt;- </span>get curr:address:editor-data/deref, in-focus?:offset
        <span class="muControl">break-unless</span> in-focus?:boolean
        cursor-row:number<span class="Special"> &lt;- </span>get curr:address:editor-data/deref, cursor-row:offset
        cursor-column:number<span class="Special"> &lt;- </span>get curr:address:editor-data/deref, cursor-column:offset
        move-cursor screen:address, cursor-row:number, cursor-column:number
      <span class="Delimiter">}</span>
      curr:address:editor-data<span class="Special"> &lt;- </span>get curr:address:editor-data/deref, next-editor:offset
      <span class="muControl">loop</span>
    <span class="Delimiter">}</span>
    <span class="muControl">loop</span>
  <span class="Delimiter">}</span>
]

<span class="muRecipe">recipe</span> handle-event [
  <span class="Constant">default-space</span>:address:array:location<span class="Special"> &lt;- </span>new location:type, <span class="Constant">50:literal</span>
  screen:address<span class="Special"> &lt;- </span><span class="Constant">next-ingredient</span>
  console:address<span class="Special"> &lt;- </span><span class="Constant">next-ingredient</span>
  editor:address:editor-data<span class="Special"> &lt;- </span><span class="Constant">next-ingredient</span>
  e:event<span class="Special"> &lt;- </span><span class="Constant">next-ingredient</span>
  <span class="Comment"># 'touch' event</span>
  <span class="Delimiter">{</span>
    t:address:touch-event<span class="Special"> &lt;- </span>maybe-convert e:event, touch:variant
    <span class="muControl">break-unless</span> t:address:touch-event
    move-cursor-in-editor editor:address:editor-data, t:address:touch-event/deref
    <span class="muControl">reply</span>
  <span class="Delimiter">}</span>
  <span class="Comment"># other events trigger only if this editor is in focus</span>
<span class="CommentedCode">#?   $print [checking ], editor:address:editor-data, [ </span>
<span class="CommentedCode">#? ] #? 1</span>
<span class="CommentedCode">#?   x:address:boolean &lt;- get-address editor:address:editor-data/deref, in-focus?:offset #? 1</span>
<span class="CommentedCode">#?   $print [address of focus: ], x:address:boolean, [ </span>
<span class="CommentedCode">#? ] #? 1</span>
  in-focus?:address:boolean<span class="Special"> &lt;- </span>get-address editor:address:editor-data/deref, in-focus?:offset
<span class="CommentedCode">#?   $print [ at ], in-focus?:address:boolean, [ </span>
<span class="CommentedCode">#? ] #? 1</span>
  <span class="muControl">reply-unless</span> in-focus?:address:boolean/deref
<span class="CommentedCode">#?   $print [in focus: ], editor:address:editor-data, [ </span>
<span class="CommentedCode">#? ] #? 1</span>
  <span class="Comment"># typing a character</span>
  <span class="Delimiter">{</span>
    c:address:character<span class="Special"> &lt;- </span>maybe-convert e:event, text:variant
    <span class="muControl">break-unless</span> c:address:character
    <span class="Comment"># unless it's a backspace</span>
    <span class="Delimiter">{</span>
      backspace?:boolean<span class="Special"> &lt;- </span>equal c:address:character/deref, <span class="Constant">8:literal/backspace</span>
      <span class="muControl">break-unless</span> backspace?:boolean
      delete-before-cursor editor:address:editor-data
      <span class="muControl">reply</span>
    <span class="Delimiter">}</span>
    insert-at-cursor editor:address:editor-data, c:address:character/deref
    <span class="muControl">reply</span>
  <span class="Delimiter">}</span>
  <span class="Comment"># otherwise it's a special key to control the editor</span>
  k:address:number<span class="Special"> &lt;- </span>maybe-convert e:event, keycode:variant
  assert k:address:number, <span class="Constant">[event was of unknown type; neither keyboard nor mouse]</span>
  d:address:duplex-list<span class="Special"> &lt;- </span>get editor:address:editor-data/deref, data:offset
  before-cursor:address:address:duplex-list<span class="Special"> &lt;- </span>get-address editor:address:editor-data/deref, before-cursor:offset
  cursor-row:address:number<span class="Special"> &lt;- </span>get-address editor:address:editor-data/deref, cursor-row:offset
  cursor-column:address:number<span class="Special"> &lt;- </span>get-address editor:address:editor-data/deref, cursor-column:offset
  screen-height:number<span class="Special"> &lt;- </span>screen-height screen:address
  top:number<span class="Special"> &lt;- </span>get editor:address:editor-data/deref, top:offset
  <span class="Comment"># arrows; update cursor-row and cursor-column, leave before-cursor to 'render'.</span>
  <span class="Comment"># right arrow</span>
  <span class="Delimiter">{</span>
    move-to-next-character?:boolean<span class="Special"> &lt;- </span>equal k:address:number/deref, <span class="Constant">65514:literal/right-arrow</span>
    <span class="muControl">break-unless</span> move-to-next-character?:boolean
    <span class="Comment"># if not at end of text</span>
    old-cursor:address:duplex-list<span class="Special"> &lt;- </span>next-duplex before-cursor:address:address:duplex-list/deref
    <span class="muControl">break-unless</span> old-cursor:address:duplex-list
    <span class="Comment"># scan to next character</span>
    before-cursor:address:address:duplex-list/deref<span class="Special"> &lt;- </span>copy old-cursor:address:duplex-list
    <span class="Comment"># if crossed a newline, move cursor to start of next row</span>
    <span class="Delimiter">{</span>
      old-cursor-character:character<span class="Special"> &lt;- </span>get before-cursor:address:address:duplex-list/deref/deref, value:offset
      was-at-newline?:boolean<span class="Special"> &lt;- </span>equal old-cursor-character:character, <span class="Constant">10:literal/newline</span>
      <span class="muControl">break-unless</span> was-at-newline?:boolean
      cursor-row:address:number/deref<span class="Special"> &lt;- </span>add cursor-row:address:number/deref, <span class="Constant">1:literal</span>
      cursor-column:address:number/deref<span class="Special"> &lt;- </span>copy <span class="Constant">0:literal</span>
      <span class="Comment"># todo: what happens when cursor is too far down?</span>
      screen-height:number<span class="Special"> &lt;- </span>screen-height screen:address
      above-screen-bottom?:boolean<span class="Special"> &lt;- </span>lesser-than cursor-row:address:number/deref, screen-height:number
      assert above-screen-bottom?:boolean, <span class="Constant">[unimplemented: moving past bottom of screen]</span>
      <span class="muControl">reply</span>
    <span class="Delimiter">}</span>
    <span class="Comment"># if the line wraps, move cursor to start of next row</span>
    <span class="Delimiter">{</span>
      <span class="Comment"># if we're at the column just before the wrap indicator</span>
      right:number<span class="Special"> &lt;- </span>get editor:address:editor-data/deref, right:offset
      wrap-column:number<span class="Special"> &lt;- </span>subtract right:number, <span class="Constant">1:literal</span>
      at-wrap?:boolean<span class="Special"> &lt;- </span>equal cursor-column:address:number/deref, wrap-column:number
      <span class="muControl">break-unless</span> at-wrap?:boolean
      <span class="Comment"># and if character after next isn't newline</span>
<span class="CommentedCode">#?       $print [aaa] #? 1</span>
      new-cursor:address:duplex-list<span class="Special"> &lt;- </span>next-duplex old-cursor:address:duplex-list
      <span class="muControl">break-unless</span> new-cursor:address:duplex-list
      next:address:duplex-list<span class="Special"> &lt;- </span>next-duplex new-cursor:address:duplex-list
      <span class="muControl">break-unless</span> next:address:duplex-list
      next-character:character<span class="Special"> &lt;- </span>get next:address:duplex-list/deref, value:offset
      newline?:boolean<span class="Special"> &lt;- </span>equal next-character:character, <span class="Constant">10:literal/newline</span>
      <span class="muControl">break-if</span> newline?:boolean
      cursor-row:address:number/deref<span class="Special"> &lt;- </span>add cursor-row:address:number/deref, <span class="Constant">1:literal</span>
      cursor-column:address:number/deref<span class="Special"> &lt;- </span>copy <span class="Constant">0:literal</span>
      <span class="Comment"># todo: what happens when cursor is too far down?</span>
      above-screen-bottom?:boolean<span class="Special"> &lt;- </span>lesser-than cursor-row:address:number/deref, screen-height:number
      assert above-screen-bottom?:boolean, <span class="Constant">[unimplemented: moving past bottom of screen]</span>
      <span class="muControl">reply</span>
    <span class="Delimiter">}</span>
    <span class="Comment"># otherwise move cursor one character right</span>
    cursor-column:address:number/deref<span class="Special"> &lt;- </span>add cursor-column:address:number/deref, <span class="Constant">1:literal</span>
  <span class="Delimiter">}</span>
  <span class="Comment"># left arrow</span>
  <span class="Delimiter">{</span>
    move-to-previous-character?:boolean<span class="Special"> &lt;- </span>equal k:address:number/deref, <span class="Constant">65515:literal/left-arrow</span>
    <span class="muControl">break-unless</span> move-to-previous-character?:boolean
    <span class="Comment"># if not at start of text (before-cursor at § sentinel)</span>
    prev:address:duplex-list<span class="Special"> &lt;- </span>prev-duplex before-cursor:address:address:duplex-list/deref
    <span class="muControl">break-unless</span> prev:address:duplex-list
    <span class="Comment"># if cursor not at left margin, move one character left</span>
    <span class="Delimiter">{</span>
      at-left-margin?:boolean<span class="Special"> &lt;- </span>equal cursor-column:address:number/deref, <span class="Constant">0:literal</span>
      <span class="muControl">break-if</span> at-left-margin?:boolean
      cursor-column:address:number/deref<span class="Special"> &lt;- </span>subtract cursor-column:address:number/deref, <span class="Constant">1:literal</span>
      <span class="muControl">reply</span>
    <span class="Delimiter">}</span>
    <span class="Comment"># if at left margin, there's guaranteed to be a previous line, since we're</span>
    <span class="Comment"># not at start of text</span>
    <span class="Delimiter">{</span>
      <span class="Comment"># if before-cursor is at newline, figure out how long the previous line is</span>
      prevc:character<span class="Special"> &lt;- </span>get before-cursor:address:address:duplex-list/deref/deref, value:offset
      previous-character-is-newline?:boolean<span class="Special"> &lt;- </span>equal prevc:character, <span class="Constant">10:literal/newline</span>
      <span class="muControl">break-unless</span> previous-character-is-newline?:boolean
      <span class="Comment"># compute length of previous line</span>
      end-of-line:number<span class="Special"> &lt;- </span>previous-line-length before-cursor:address:address:duplex-list/deref, d:address:duplex-list
      cursor-row:address:number/deref<span class="Special"> &lt;- </span>subtract cursor-row:address:number/deref, <span class="Constant">1:literal</span>
      cursor-column:address:number/deref<span class="Special"> &lt;- </span>copy end-of-line:number
      <span class="muControl">reply</span>
    <span class="Delimiter">}</span>
    <span class="Comment"># if before-cursor is not at newline, we're just at a wrapped line</span>
    assert cursor-row:address:number/deref, <span class="Constant">[unimplemented: moving cursor above top of screen]</span>
    cursor-row:address:number/deref<span class="Special"> &lt;- </span>subtract cursor-row:address:number/deref, <span class="Constant">1:literal</span>
    right:number<span class="Special"> &lt;- </span>get editor:address:editor-data/deref, right:offset
    cursor-column:address:number/deref<span class="Special"> &lt;- </span>subtract right:number, <span class="Constant">1:literal</span>  <span class="Comment"># leave room for wrap icon</span>
  <span class="Delimiter">}</span>
  <span class="Comment"># down arrow</span>
  <span class="Delimiter">{</span>
    move-to-next-line?:boolean<span class="Special"> &lt;- </span>equal k:address:number/deref, <span class="Constant">65516:literal/down-arrow</span>
    <span class="muControl">break-unless</span> move-to-next-line?:boolean
    <span class="Comment"># todo: support scrolling</span>
    already-at-bottom?:boolean<span class="Special"> &lt;- </span>greater-or-equal cursor-row:address:number/deref, screen-height:number
    <span class="muControl">break-if</span> already-at-bottom?:boolean
<span class="CommentedCode">#?     $print [moving down</span>
<span class="CommentedCode">#? ] #? 1</span>
    cursor-row:address:number/deref<span class="Special"> &lt;- </span>add cursor-row:address:number/deref, <span class="Constant">1:literal</span>
    <span class="Comment"># that's it; render will adjust cursor-column as necessary</span>
  <span class="Delimiter">}</span>
  <span class="Comment"># up arrow</span>
  <span class="Delimiter">{</span>
    move-to-previous-line?:boolean<span class="Special"> &lt;- </span>equal k:address:number/deref, <span class="Constant">65517:literal/up-arrow</span>
    <span class="muControl">break-unless</span> move-to-previous-line?:boolean
    <span class="Comment"># todo: support scrolling</span>
    already-at-top?:boolean<span class="Special"> &lt;- </span>lesser-or-equal cursor-row:address:number/deref, top:number
    <span class="muControl">break-if</span> already-at-top?:boolean
<span class="CommentedCode">#?     $print [moving up</span>
<span class="CommentedCode">#? ] #? 1</span>
    cursor-row:address:number/deref<span class="Special"> &lt;- </span>subtract cursor-row:address:number/deref, <span class="Constant">1:literal</span>
    <span class="Comment"># that's it; render will adjust cursor-column as necessary</span>
  <span class="Delimiter">}</span>
]

<span class="muRecipe">recipe</span> move-cursor-in-editor [
  <span class="Constant">default-space</span>:address:array:location<span class="Special"> &lt;- </span>new location:type, <span class="Constant">30:literal</span>
  editor:address:editor-data<span class="Special"> &lt;- </span><span class="Constant">next-ingredient</span>
  t:touch-event<span class="Special"> &lt;- </span><span class="Constant">next-ingredient</span>
  <span class="Comment"># always reset focus to start</span>
  in-focus?:address:boolean<span class="Special"> &lt;- </span>get-address editor:address:editor-data/deref, in-focus?:offset
  in-focus?:address:boolean/deref<span class="Special"> &lt;- </span>copy <span class="Constant">0:literal/true</span>
  click-column:number<span class="Special"> &lt;- </span>get t:touch-event, column:offset
  left:number<span class="Special"> &lt;- </span>get editor:address:editor-data/deref, left:offset
  too-far-left?:boolean<span class="Special"> &lt;- </span>lesser-than click-column:number, left:number
  <span class="muControl">reply-if</span> too-far-left?:boolean
  right:number<span class="Special"> &lt;- </span>get editor:address:editor-data/deref, right:offset
  too-far-right?:boolean<span class="Special"> &lt;- </span>greater-than click-column:number, right:number
  <span class="muControl">reply-if</span> too-far-right?:boolean
<span class="CommentedCode">#?   $print [focus now at ], editor:address:editor-data, [ </span>
<span class="CommentedCode">#? ] #? 2</span>
  <span class="Comment"># click on this window; gain focus</span>
  in-focus?:address:boolean/deref<span class="Special"> &lt;- </span>copy <span class="Constant">1:literal/true</span>
  <span class="Comment"># update cursor</span>
  cursor-row:address:number<span class="Special"> &lt;- </span>get-address editor:address:editor-data/deref, cursor-row:offset
  cursor-row:address:number/deref<span class="Special"> &lt;- </span>get t:touch-event, row:offset
  cursor-column:address:number<span class="Special"> &lt;- </span>get-address editor:address:editor-data/deref, cursor-column:offset
  cursor-column:address:number/deref<span class="Special"> &lt;- </span>get t:touch-event, column:offset
<span class="CommentedCode">#?   $print [column is at: ], cursor-column:address:number, [ </span>
<span class="CommentedCode">#? ] #? 1</span>
]

<span class="muRecipe">recipe</span> insert-at-cursor [
  <span class="Constant">default-space</span>:address:array:location<span class="Special"> &lt;- </span>new location:type, <span class="Constant">30:literal</span>
  editor:address:editor-data<span class="Special"> &lt;- </span><span class="Constant">next-ingredient</span>
  c:character<span class="Special"> &lt;- </span><span class="Constant">next-ingredient</span>
<span class="CommentedCode">#?   $print [insert ], c:character, [ </span>
<span class="CommentedCode">#? ] #? 1</span>
  before-cursor:address:address:duplex-list<span class="Special"> &lt;- </span>get-address editor:address:editor-data/deref, before-cursor:offset
  d:address:duplex-list<span class="Special"> &lt;- </span>get editor:address:editor-data/deref, data:offset
  insert-duplex c:character, before-cursor:address:address:duplex-list/deref
  screen:address<span class="Special"> &lt;- </span>get editor:address:editor-data/deref, screen:offset
  cursor-row:address:number<span class="Special"> &lt;- </span>get-address editor:address:editor-data/deref, cursor-row:offset
  cursor-column:address:number<span class="Special"> &lt;- </span>get-address editor:address:editor-data/deref, cursor-column:offset
  <span class="Comment"># update cursor: if newline, move cursor to start of next line</span>
  <span class="Comment"># todo: bottom of screen</span>
  <span class="Delimiter">{</span>
    newline?:boolean<span class="Special"> &lt;- </span>equal c:character, <span class="Constant">10:literal/newline</span>
    <span class="muControl">break-unless</span> newline?:boolean
    cursor-row:address:number/deref<span class="Special"> &lt;- </span>add cursor-row:address:number/deref, <span class="Constant">1:literal</span>
    cursor-column:address:number/deref<span class="Special"> &lt;- </span>copy <span class="Constant">0:literal</span>
    <span class="muControl">reply</span>
  <span class="Delimiter">}</span>
  <span class="Comment"># if the line wraps at the cursor, move cursor to start of next row</span>
  <span class="Delimiter">{</span>
    <span class="Comment"># if we're at the column just before the wrap indicator</span>
    right:number<span class="Special"> &lt;- </span>get editor:address:editor-data/deref, right:offset
    wrap-column:number<span class="Special"> &lt;- </span>subtract right:number, <span class="Constant">1:literal</span>
<span class="CommentedCode">#?     $print [wrap? ], cursor-column:address:number/deref, [ vs ], wrap-column:number, [ </span>
<span class="CommentedCode">#? ] #? 1</span>
    at-wrap?:boolean<span class="Special"> &lt;- </span>greater-or-equal cursor-column:address:number/deref, wrap-column:number
    <span class="muControl">break-unless</span> at-wrap?:boolean
<span class="CommentedCode">#?     $print [wrap!</span>
<span class="CommentedCode">#? ] #? 1</span>
    cursor-column:address:number/deref<span class="Special"> &lt;- </span>subtract cursor-column:address:number/deref, wrap-column:number
    cursor-row:address:number/deref<span class="Special"> &lt;- </span>add cursor-row:address:number/deref, <span class="Constant">1:literal</span>
    <span class="Comment"># todo: what happens when cursor is too far down?</span>
    screen-height:number<span class="Special"> &lt;- </span>screen-height screen:address
    above-screen-bottom?:boolean<span class="Special"> &lt;- </span>lesser-than cursor-row:address:number/deref, screen-height:number
    assert above-screen-bottom?:boolean, <span class="Constant">[unimplemented: typing past bottom of screen]</span>
<span class="CommentedCode">#?     $print [return</span>
<span class="CommentedCode">#? ] #? 1</span>
    <span class="muControl">reply</span>
  <span class="Delimiter">}</span>
  <span class="Comment"># otherwise move cursor right</span>
  cursor-column:address:number/deref<span class="Special"> &lt;- </span>add cursor-column:address:number/deref, <span class="Constant">1:literal</span>
]

<span class="muRecipe">recipe</span> delete-before-cursor [
  <span class="Constant">default-space</span>:address:array:location<span class="Special"> &lt;- </span>new location:type, <span class="Constant">30:literal</span>
  editor:address:editor-data<span class="Special"> &lt;- </span><span class="Constant">next-ingredient</span>
  before-cursor:address:address:duplex-list<span class="Special"> &lt;- </span>get-address editor:address:editor-data/deref, before-cursor:offset
  d:address:duplex-list<span class="Special"> &lt;- </span>get editor:address:editor-data/deref, data:offset
  <span class="Comment"># unless already at start</span>
  at-start?:boolean<span class="Special"> &lt;- </span>equal before-cursor:address:address:duplex-list/deref, d:address:duplex-list
  <span class="muControl">reply-if</span> at-start?:boolean
  <span class="Comment"># delete character</span>
  prev:address:duplex-list<span class="Special"> &lt;- </span>prev-duplex before-cursor:address:address:duplex-list/deref
  remove-duplex before-cursor:address:address:duplex-list/deref
  <span class="Comment"># update cursor</span>
  before-cursor:address:address:duplex-list/deref<span class="Special"> &lt;- </span>copy prev:address:duplex-list
  cursor-column:address:number<span class="Special"> &lt;- </span>get-address editor:address:editor-data/deref, cursor-column:offset
  cursor-column:address:number/deref<span class="Special"> &lt;- </span>subtract cursor-column:address:number/deref, <span class="Constant">1:literal</span>
<span class="CommentedCode">#?   $print [delete-before-cursor: ], cursor-column:address:number/deref, [ </span>
<span class="CommentedCode">#? ] #? 1</span>
]

<span class="Comment"># takes a pointer 'curr' into the doubly-linked list and its sentinel, counts</span>
<span class="Comment"># the length of the previous line before the 'curr' pointer.</span>
<span class="muRecipe">recipe</span> previous-line-length [
  <span class="Constant">default-space</span>:address:array:location<span class="Special"> &lt;- </span>new location:type, <span class="Constant">30:literal</span>
  curr:address:duplex-list<span class="Special"> &lt;- </span><span class="Constant">next-ingredient</span>
  start:address:duplex-list<span class="Special"> &lt;- </span><span class="Constant">next-ingredient</span>
  result:number<span class="Special"> &lt;- </span>copy <span class="Constant">0:literal</span>
  <span class="muControl">reply-unless</span> curr:address:duplex-list, result:number
  at-start?:boolean<span class="Special"> &lt;- </span>equal curr:address:duplex-list, start:address:duplex-list
  <span class="muControl">reply-if</span> at-start?:boolean, result:number
  <span class="Delimiter">{</span>
    curr:address:duplex-list<span class="Special"> &lt;- </span>prev-duplex curr:address:duplex-list
    <span class="muControl">break-unless</span> curr:address:duplex-list
    at-start?:boolean<span class="Special"> &lt;- </span>equal curr:address:duplex-list, start:address:duplex-list
    <span class="muControl">break-if</span> at-start?:boolean
    c:character<span class="Special"> &lt;- </span>get curr:address:duplex-list/deref, value:offset
    at-newline?:boolean<span class="Special"> &lt;- </span>equal c:character <span class="Constant">10:literal/newline</span>
    <span class="muControl">break-if</span> at-newline?:boolean
    result:number<span class="Special"> &lt;- </span>add result:number, <span class="Constant">1:literal</span>
    <span class="muControl">loop</span>
  <span class="Delimiter">}</span>
  <span class="muControl">reply</span> result:number
]

<span class="muScenario">scenario</span> editor-handles-empty-event-queue [
  assume-screen <span class="Constant">10:literal/width</span>, <span class="Constant">5:literal/height</span>
<span class="CommentedCode">#?   3:number &lt;- get screen:address/deref, num-rows:offset #? 1</span>
<span class="CommentedCode">#?   $print [0: ], screen:address, [: ], 3:number, [ </span>
<span class="CommentedCode">#? ] #? 1</span>
  1:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[abc]</span>
<span class="CommentedCode">#?   $print [1: ], screen:address, [ </span>
<span class="CommentedCode">#? ] #? 1</span>
  2:address:editor-data<span class="Special"> &lt;- </span>new-editor 1:address:array:character, screen:address, <span class="Constant">0:literal/top</span>, <span class="Constant">0:literal/left</span>, <span class="Constant">10:literal/right</span>
  assume-console <span class="Constant">[]</span>
<span class="CommentedCode">#?   $print [8: ], screen:address, [ </span>
<span class="CommentedCode">#? ] #? 1</span>
  run [
    event-loop screen:address, console:address, 2:address:editor-data
  ]
<span class="CommentedCode">#?   $print [9: ], screen:address, [ </span>
<span class="CommentedCode">#? ] #? 1</span>
  screen-should-contain [
   <span class="Constant"> .abc       .</span>
   <span class="Constant"> .          .</span>
  ]
]

<span class="muScenario">scenario</span> editor-handles-mouse-clicks [
  assume-screen <span class="Constant">10:literal/width</span>, <span class="Constant">5:literal/height</span>
  1:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[abc]</span>
  2:address:editor-data<span class="Special"> &lt;- </span>new-editor 1:address:array:character, screen:address, <span class="Constant">0:literal/top</span>, <span class="Constant">0:literal/left</span>, <span class="Constant">10:literal/right</span>
  assume-console [
    left-click 0, 1  <span class="Comment"># on the 'b'</span>
  ]
  run [
    event-loop screen:address, console:address, 2:address:editor-data
    3:number<span class="Special"> &lt;- </span>get 2:address:editor-data/deref, cursor-row:offset
    4:number<span class="Special"> &lt;- </span>get 2:address:editor-data/deref, cursor-column:offset
  ]
  screen-should-contain [
   <span class="Constant"> .abc       .</span>
   <span class="Constant"> .          .</span>
  ]
  memory-should-contain [
    3<span class="Special"> &lt;- </span>0  <span class="Comment"># cursor is at row 0..</span>
    4<span class="Special"> &lt;- </span>1  <span class="Comment"># ..and column 1</span>
  ]
]

<span class="muScenario">scenario</span> editor-handles-mouse-clicks-outside-text [
  assume-screen <span class="Constant">10:literal/width</span>, <span class="Constant">5:literal/height</span>
  1:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[abc]</span>
  2:address:editor-data<span class="Special"> &lt;- </span>new-editor 1:address:array:character, screen:address, <span class="Constant">0:literal/top</span>, <span class="Constant">0:literal/left</span>, <span class="Constant">10:literal/right</span>
  assume-console [
    left-click 0, 7  <span class="Comment"># last line, to the right of text</span>
  ]
  run [
    event-loop screen:address, console:address, 2:address:editor-data
    3:number<span class="Special"> &lt;- </span>get 2:address:editor-data/deref, cursor-row:offset
    4:number<span class="Special"> &lt;- </span>get 2:address:editor-data/deref, cursor-column:offset
  ]
  memory-should-contain [
    3<span class="Special"> &lt;- </span>0  <span class="Comment"># cursor row</span>
    4<span class="Special"> &lt;- </span>3  <span class="Comment"># cursor column</span>
  ]
]

<span class="muScenario">scenario</span> editor-handles-mouse-clicks-outside-text-2 [
  assume-screen <span class="Constant">10:literal/width</span>, <span class="Constant">5:literal/height</span>
  1:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[abc</span>
<span class="Constant">def]</span>
  2:address:editor-data<span class="Special"> &lt;- </span>new-editor 1:address:array:character, screen:address, <span class="Constant">0:literal/top</span>, <span class="Constant">0:literal/left</span>, <span class="Constant">10:literal/right</span>
  assume-console [
    left-click 0, 7  <span class="Comment"># interior line, to the right of text</span>
  ]
  run [
    event-loop screen:address, console:address, 2:address:editor-data
    3:number<span class="Special"> &lt;- </span>get 2:address:editor-data/deref, cursor-row:offset
    4:number<span class="Special"> &lt;- </span>get 2:address:editor-data/deref, cursor-column:offset
  ]
  memory-should-contain [
    3<span class="Special"> &lt;- </span>0  <span class="Comment"># cursor row</span>
    4<span class="Special"> &lt;- </span>3  <span class="Comment"># cursor column</span>
  ]
]

<span class="muScenario">scenario</span> editor-handles-mouse-clicks-outside-text-3 [
  assume-screen <span class="Constant">10:literal/width</span>, <span class="Constant">5:literal/height</span>
  1:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[abc</span>
<span class="Constant">def]</span>
  2:address:editor-data<span class="Special"> &lt;- </span>new-editor 1:address:array:character, screen:address, <span class="Constant">0:literal/top</span>, <span class="Constant">0:literal/left</span>, <span class="Constant">10:literal/right</span>
  assume-console [
    left-click 2, 7  <span class="Comment"># below text</span>
  ]
  run [
    event-loop screen:address, console:address, 2:address:editor-data
    3:number<span class="Special"> &lt;- </span>get 2:address:editor-data/deref, cursor-row:offset
    4:number<span class="Special"> &lt;- </span>get 2:address:editor-data/deref, cursor-column:offset
  ]
  memory-should-contain [
    3<span class="Special"> &lt;- </span>1  <span class="Comment"># cursor row</span>
    4<span class="Special"> &lt;- </span>3  <span class="Comment"># cursor column</span>
  ]
]

<span class="muScenario">scenario</span> editor-handles-mouse-clicks-outside-column [
  assume-screen <span class="Constant">10:literal/width</span>, <span class="Constant">5:literal/height</span>
  1:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[abc]</span>
  <span class="Comment"># editor occupies only left half of screen</span>
  2:address:editor-data<span class="Special"> &lt;- </span>new-editor 1:address:array:character, screen:address, <span class="Constant">0:literal/top</span>, <span class="Constant">0:literal/left</span>, <span class="Constant">5:literal/right</span>
  assume-console [
    <span class="Comment"># click on right half of screen</span>
    left-click 3, 8
  ]
  run [
    event-loop screen:address, console:address, 2:address:editor-data
    3:number<span class="Special"> &lt;- </span>get 2:address:editor-data/deref, cursor-row:offset
    4:number<span class="Special"> &lt;- </span>get 2:address:editor-data/deref, cursor-column:offset
  ]
  screen-should-contain [
   <span class="Constant"> .abc       .</span>
   <span class="Constant"> .          .</span>
  ]
  memory-should-contain [
    3<span class="Special"> &lt;- </span>0  <span class="Comment"># no change to cursor row</span>
    4<span class="Special"> &lt;- </span>0  <span class="Comment"># ..or column</span>
  ]
]

<span class="muScenario">scenario</span> editor-inserts-characters-into-empty-editor [
  assume-screen <span class="Constant">10:literal/width</span>, <span class="Constant">5:literal/height</span>
  1:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[]</span>
  2:address:editor-data<span class="Special"> &lt;- </span>new-editor 1:address:array:character, screen:address, <span class="Constant">0:literal/top</span>, <span class="Constant">0:literal/left</span>, <span class="Constant">5:literal/right</span>
  assume-console [
    type <span class="Constant">[abc]</span>
  ]
  run [
    event-loop screen:address, console:address, 2:address:editor-data
  ]
  screen-should-contain [
   <span class="Constant"> .abc       .</span>
   <span class="Constant"> .          .</span>
  ]
]

<span class="muScenario">scenario</span> editor-inserts-characters-at-cursor [
  assume-screen <span class="Constant">10:literal/width</span>, <span class="Constant">5:literal/height</span>
  1:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[abc]</span>
  2:address:editor-data<span class="Special"> &lt;- </span>new-editor 1:address:array:character, screen:address, <span class="Constant">0:literal/top</span>, <span class="Constant">0:literal/left</span>, <span class="Constant">10:literal/right</span>
  assume-console [
    type <span class="Constant">[0]</span>
    left-click 0, 2
    type <span class="Constant">[d]</span>
  ]
  run [
    event-loop screen:address, console:address, 2:address:editor-data
  ]
  screen-should-contain [
   <span class="Constant"> .0adbc     .</span>
   <span class="Constant"> .          .</span>
  ]
]

<span class="muScenario">scenario</span> editor-inserts-characters-at-cursor-2 [
  assume-screen <span class="Constant">10:literal/width</span>, <span class="Constant">5:literal/height</span>
  1:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[abc]</span>
  2:address:editor-data<span class="Special"> &lt;- </span>new-editor 1:address:array:character, screen:address, <span class="Constant">0:literal/top</span>, <span class="Constant">0:literal/left</span>, <span class="Constant">10:literal/right</span>
  assume-console [
    left-click 0, 5  <span class="Comment"># right of last line</span>
    type <span class="Constant">[d]</span>  <span class="Comment"># should append</span>
  ]
  run [
    event-loop screen:address, console:address, 2:address:editor-data
  ]
  screen-should-contain [
   <span class="Constant"> .abcd      .</span>
   <span class="Constant"> .          .</span>
  ]
]

<span class="muScenario">scenario</span> editor-inserts-characters-at-cursor-3 [
  assume-screen <span class="Constant">10:literal/width</span>, <span class="Constant">5:literal/height</span>
  1:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[abc]</span>
  2:address:editor-data<span class="Special"> &lt;- </span>new-editor 1:address:array:character, screen:address, <span class="Constant">0:literal/top</span>, <span class="Constant">0:literal/left</span>, <span class="Constant">10:literal/right</span>
  assume-console [
    left-click 3, 5  <span class="Comment"># below all text</span>
    type <span class="Constant">[d]</span>  <span class="Comment"># should append</span>
  ]
  run [
    event-loop screen:address, console:address, 2:address:editor-data
  ]
  screen-should-contain [
   <span class="Constant"> .abcd      .</span>
   <span class="Constant"> .          .</span>
  ]
]

<span class="muScenario">scenario</span> editor-inserts-characters-at-cursor-4 [
  assume-screen <span class="Constant">10:literal/width</span>, <span class="Constant">5:literal/height</span>
  1:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[abc</span>
<span class="Constant">d]</span>
  2:address:editor-data<span class="Special"> &lt;- </span>new-editor 1:address:array:character, screen:address, <span class="Constant">0:literal/top</span>, <span class="Constant">0:literal/left</span>, <span class="Constant">10:literal/right</span>
  assume-console [
    left-click 3, 5  <span class="Comment"># below all text</span>
    type <span class="Constant">[e]</span>  <span class="Comment"># should append</span>
  ]
  run [
    event-loop screen:address, console:address, 2:address:editor-data
  ]
  screen-should-contain [
   <span class="Constant"> .abc       .</span>
   <span class="Constant"> .de        .</span>
   <span class="Constant"> .          .</span>
  ]
]

<span class="muScenario">scenario</span> editor-inserts-characters-at-cursor-5 [
  assume-screen <span class="Constant">10:literal/width</span>, <span class="Constant">5:literal/height</span>
  1:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[abc</span>
<span class="Constant">d]</span>
  2:address:editor-data<span class="Special"> &lt;- </span>new-editor 1:address:array:character, screen:address, <span class="Constant">0:literal/top</span>, <span class="Constant">0:literal/left</span>, <span class="Constant">10:literal/right</span>
  assume-console [
    left-click 3, 5  <span class="Comment"># below all text</span>
    type <span class="Constant">[ef]</span>  <span class="Comment"># should append multiple characters in order</span>
  ]
  run [
    event-loop screen:address, console:address, 2:address:editor-data
  ]
  screen-should-contain [
   <span class="Constant"> .abc       .</span>
   <span class="Constant"> .def       .</span>
   <span class="Constant"> .          .</span>
  ]
]

<span class="muScenario">scenario</span> editor-inserts-characters-at-cursor-6 [
  assume-screen <span class="Constant">10:literal/width</span>, <span class="Constant">5:literal/height</span>
  <span class="Comment"># text fills line</span>
  1:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[abcde]</span>
  2:address:editor-data<span class="Special"> &lt;- </span>new-editor 1:address:array:character, screen:address, <span class="Constant">0:literal/top</span>, <span class="Constant">0:literal/left</span>, <span class="Constant">5:literal/right</span>
  <span class="Comment"># position cursor at end</span>
  assume-console [
    left-click 3, 0
  ]
  run [
    event-loop screen:address, console:address, 2:address:editor-data
    3:number<span class="Special"> &lt;- </span>get 2:address:editor-data/deref, cursor-row:offset
    4:number<span class="Special"> &lt;- </span>get 2:address:editor-data/deref, cursor-column:offset
  ]
  <span class="Comment"># text shouldn't wrap</span>
  screen-should-contain [
   <span class="Constant"> .abcde     .</span>
   <span class="Constant"> .          .</span>
  ]
  <span class="Comment"># cursor should wrap</span>
  memory-should-contain [
    3<span class="Special"> &lt;- </span>1
    4<span class="Special"> &lt;- </span>0
  ]
]

<span class="muScenario">scenario</span> editor-wraps-line-on-insert [
  assume-screen <span class="Constant">5:literal/width</span>, <span class="Constant">3:literal/height</span>
  1:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[abcd]</span>
  2:address:editor-data<span class="Special"> &lt;- </span>new-editor 1:address:array:character, screen:address, <span class="Constant">0:literal/top</span>, <span class="Constant">0:literal/left</span>, <span class="Constant">5:literal/right</span>
  <span class="Comment"># type a letter</span>
  assume-console [
    type <span class="Constant">[e]</span>
  ]
  run [
    event-loop screen:address, console:address, 2:address:editor-data
  ]
  <span class="Comment"># no wrap yet</span>
  screen-should-contain [
<span class="Constant">    .eabcd.</span>
   <span class="Constant"> .     .</span>
  ]
  <span class="Comment"># type a second letter</span>
  assume-console [
    type <span class="Constant">[f]</span>
  ]
  run [
    event-loop screen:address, console:address, 2:address:editor-data
  ]
  <span class="Comment"># now wrap</span>
  screen-should-contain [
<span class="Constant">    .efab↩.</span>
   <span class="Constant"> .cd   .</span>
   <span class="Constant"> .     .</span>
  ]
]

<span class="muScenario">scenario</span> editor-moves-cursor-after-inserting-characters [
  assume-screen <span class="Constant">10:literal/width</span>, <span class="Constant">5:literal/height</span>
  1:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[abc]</span>
  2:address:editor-data<span class="Special"> &lt;- </span>new-editor 1:address:array:character, screen:address, <span class="Constant">0:literal/top</span>, <span class="Constant">0:literal/left</span>, <span class="Constant">5:literal/right</span>
  assume-console [
    type <span class="Constant">[01]</span>
  ]
  run [
    event-loop screen:address, console:address, 2:address:editor-data
  ]
  screen-should-contain [
   <span class="Constant"> .01abc     .</span>
   <span class="Constant"> .          .</span>
  ]
]

<span class="muScenario">scenario</span> editor-wraps-cursor-after-inserting-characters [
  assume-screen <span class="Constant">10:literal/width</span>, <span class="Constant">5:literal/height</span>
  1:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[abcde]</span>
  2:address:editor-data<span class="Special"> &lt;- </span>new-editor 1:address:array:character, screen:address, <span class="Constant">0:literal/top</span>, <span class="Constant">0:literal/left</span>, <span class="Constant">5:literal/right</span>
  assume-console [
    left-click 0, 4  <span class="Comment"># line is full; no wrap icon yet</span>
    type <span class="Constant">[f]</span>
  ]
  run [
    event-loop screen:address, console:address, 2:address:editor-data
    3:number<span class="Special"> &lt;- </span>get 2:address:editor-data/deref, cursor-row:offset
    4:number<span class="Special"> &lt;- </span>get 2:address:editor-data/deref, cursor-column:offset
  ]
  screen-should-contain [
   <span class="Constant"> .abcd↩     .</span>
   <span class="Constant"> .fe        .</span>
   <span class="Constant"> .          .</span>
  ]
  memory-should-contain [
    3<span class="Special"> &lt;- </span>1  <span class="Comment"># cursor row</span>
    4<span class="Special"> &lt;- </span>1  <span class="Comment"># cursor column</span>
  ]
]

<span class="muScenario">scenario</span> editor-wraps-cursor-after-inserting-characters-2 [
  assume-screen <span class="Constant">10:literal/width</span>, <span class="Constant">5:literal/height</span>
  1:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[abcde]</span>
  2:address:editor-data<span class="Special"> &lt;- </span>new-editor 1:address:array:character, screen:address, <span class="Constant">0:literal/top</span>, <span class="Constant">0:literal/left</span>, <span class="Constant">5:literal/right</span>
  assume-console [
    left-click 0, 3  <span class="Comment"># right before the wrap icon</span>
    type <span class="Constant">[f]</span>
  ]
  run [
    event-loop screen:address, console:address, 2:address:editor-data
    3:number<span class="Special"> &lt;- </span>get 2:address:editor-data/deref, cursor-row:offset
    4:number<span class="Special"> &lt;- </span>get 2:address:editor-data/deref, cursor-column:offset
  ]
  screen-should-contain [
   <span class="Constant"> .abcf↩     .</span>
   <span class="Constant"> .de        .</span>
   <span class="Constant"> .          .</span>
  ]
  memory-should-contain [
    3<span class="Special"> &lt;- </span>1  <span class="Comment"># cursor row</span>
    4<span class="Special"> &lt;- </span>0  <span class="Comment"># cursor column</span>
  ]
]

<span class="muScenario">scenario</span> editor-moves-cursor-down-after-inserting-newline [
  assume-screen <span class="Constant">10:literal/width</span>, <span class="Constant">5:literal/height</span>
  1:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[abc]</span>
  2:address:editor-data<span class="Special"> &lt;- </span>new-editor 1:address:array:character, screen:address, <span class="Constant">0:literal/top</span>, <span class="Constant">0:literal/left</span>, <span class="Constant">10:literal/right</span>
  assume-console [
    type <span class="Constant">[0</span>
<span class="Constant">1]</span>
  ]
  run [
    event-loop screen:address, console:address, 2:address:editor-data
  ]
  screen-should-contain [
   <span class="Constant"> .0         .</span>
   <span class="Constant"> .1abc      .</span>
   <span class="Constant"> .          .</span>
  ]
]

<span class="muScenario">scenario</span> editor-clears-previous-line-completely-after-inserting-newline [
  assume-screen <span class="Constant">10:literal/width</span>, <span class="Constant">5:literal/height</span>
  1:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[abcde]</span>
  2:address:editor-data<span class="Special"> &lt;- </span>new-editor 1:address:array:character, screen:address, <span class="Constant">0:literal/top</span>, <span class="Constant">0:literal/left</span>, <span class="Constant">5:literal/right</span>
  <span class="Comment"># press just a 'newline'</span>
  assume-console [
    type [
]
  ]
  run [
    event-loop screen:address, console:address, 2:address:editor-data
  ]
  <span class="Comment"># line should be fully cleared</span>
  screen-should-contain [
   <span class="Constant"> .          .</span>
   <span class="Constant"> .abcde     .</span>
   <span class="Constant"> .          .</span>
  ]
]

<span class="muScenario">scenario</span> editor-handles-backspace-key [
<span class="CommentedCode">#?   $print [=== new test</span>
<span class="CommentedCode">#? ] #? 1</span>
  assume-screen <span class="Constant">10:literal/width</span>, <span class="Constant">5:literal/height</span>
  1:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[abc]</span>
  2:address:editor-data<span class="Special"> &lt;- </span>new-editor 1:address:array:character, screen:address, <span class="Constant">0:literal/top</span>, <span class="Constant">0:literal/left</span>, <span class="Constant">10:literal/right</span>
<span class="CommentedCode">#?   $print [editor: ], 2:address:editor-data, [ </span>
<span class="CommentedCode">#? ] #? 1</span>
  assume-console [
    left-click 0, 1
    type <span class="Constant">[«]</span>
  ]
  3:event/backspace<span class="Special"> &lt;- </span>merge <span class="Constant">0:literal/text</span>, <span class="Constant">8:literal/backspace</span>, <span class="Constant">0:literal/dummy</span>, <span class="Constant">0:literal/dummy</span>
  replace-in-console <span class="Constant">171:literal/«</span>, 3:event/backspace
  run [
    event-loop screen:address, console:address, 2:address:editor-data
    4:number<span class="Special"> &lt;- </span>get 2:address:editor-data/deref, cursor-row:offset
    5:number<span class="Special"> &lt;- </span>get 2:address:editor-data/deref, cursor-column:offset
  ]
  screen-should-contain [
   <span class="Constant"> .bc        .</span>
   <span class="Constant"> .          .</span>
  ]
  memory-should-contain [
    4<span class="Special"> &lt;- </span>0
    5<span class="Special"> &lt;- </span>0
  ]
]

<span class="muScenario">scenario</span> editor-clears-last-line-on-backspace [
  assume-screen <span class="Constant">10:literal/width</span>, <span class="Constant">5:literal/height</span>
  <span class="Comment"># just one character in final line</span>
  1:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[abc</span>
<span class="Constant">d]</span>
  2:address:editor-data<span class="Special"> &lt;- </span>new-editor 1:address:array:character, screen:address, <span class="Constant">0:literal/top</span>, <span class="Constant">0:literal/left</span>, <span class="Constant">5:literal/right</span>
  assume-console [
    left-click 1, 0  <span class="Comment"># cursor at only character in final line</span>
    type <span class="Constant">[«]</span>
  ]
  3:event/backspace<span class="Special"> &lt;- </span>merge <span class="Constant">0:literal/text</span>, <span class="Constant">8:literal/backspace</span>, <span class="Constant">0:literal/dummy</span>, <span class="Constant">0:literal/dummy</span>
  replace-in-console <span class="Constant">171:literal/«</span>, 3:event/backspace
  run [
    event-loop screen:address, console:address, 2:address:editor-data
  ]
  screen-should-contain [
   <span class="Constant"> .abcd      .</span>
   <span class="Constant"> .          .</span>
  ]
]

<span class="muScenario">scenario</span> editor-handles-backspace-key-at-right-margin [
  assume-screen <span class="Constant">10:literal/width</span>, <span class="Constant">5:literal/height</span>
  <span class="Comment"># fill a line with text</span>
  1:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[abcde]</span>
  2:address:editor-data<span class="Special"> &lt;- </span>new-editor 1:address:array:character, screen:address, <span class="Constant">0:literal/top</span>, <span class="Constant">0:literal/left</span>, <span class="Constant">5:literal/right</span>
  <span class="Comment"># position cursor at end</span>
  assume-console [
    left-click 1, 3  <span class="Comment"># at end of text</span>
  ]
  run [
    event-loop screen:address, console:address, 2:address:editor-data
    3:number<span class="Special"> &lt;- </span>get 2:address:editor-data/deref, cursor-row:offset
    4:number<span class="Special"> &lt;- </span>get 2:address:editor-data/deref, cursor-column:offset
  ]
  <span class="Comment"># check that cursor wraps to next line</span>
  memory-should-contain [
    3<span class="Special"> &lt;- </span>1
    4<span class="Special"> &lt;- </span>0
  ]
  <span class="Comment"># now hit a backspace key</span>
  assume-console [
    type <span class="Constant">[«]</span>
  ]
  5:event/backspace<span class="Special"> &lt;- </span>merge <span class="Constant">0:literal/text</span>, <span class="Constant">8:literal/backspace</span>, <span class="Constant">0:literal/dummy</span>, <span class="Constant">0:literal/dummy</span>
  replace-in-console <span class="Constant">171:literal/«</span>, 5:event/backspace
  run [
    event-loop screen:address, console:address, 2:address:editor-data
    3:number<span class="Special"> &lt;- </span>get 2:address:editor-data/deref, cursor-row:offset
    4:number<span class="Special"> &lt;- </span>get 2:address:editor-data/deref, cursor-column:offset
  ]
  <span class="Comment"># cursor unwraps</span>
  memory-should-contain [
    3<span class="Special"> &lt;- </span>0
    4<span class="Special"> &lt;- </span>4
  ]
  screen-should-contain [
   <span class="Constant"> .abcd      .</span>
   <span class="Constant"> .          .</span>
  ]
]

<span class="muScenario">scenario</span> editor-moves-cursor-right-with-key [
  assume-screen <span class="Constant">10:literal/width</span>, <span class="Constant">5:literal/height</span>
  1:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[abc]</span>
  2:address:editor-data<span class="Special"> &lt;- </span>new-editor 1:address:array:character, screen:address, <span class="Constant">0:literal/top</span>, <span class="Constant">0:literal/left</span>, <span class="Constant">10:literal/right</span>
  assume-console [
    press 65514  <span class="Comment"># right arrow</span>
    type <span class="Constant">[0]</span>
  ]
  run [
    event-loop screen:address, console:address, 2:address:editor-data
  ]
  screen-should-contain [
   <span class="Constant"> .a0bc      .</span>
   <span class="Constant"> .          .</span>
  ]
]

<span class="muScenario">scenario</span> editor-moves-cursor-to-next-line-with-right-arrow [
  assume-screen <span class="Constant">10:literal/width</span>, <span class="Constant">5:literal/height</span>
  1:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[abc</span>
<span class="Constant">d]</span>
  2:address:editor-data<span class="Special"> &lt;- </span>new-editor 1:address:array:character, screen:address, <span class="Constant">0:literal/top</span>, <span class="Constant">0:literal/left</span>, <span class="Constant">10:literal/right</span>
  assume-console [
    press 65514  <span class="Comment"># right arrow</span>
    press 65514  <span class="Comment"># right arrow</span>
    press 65514  <span class="Comment"># right arrow</span>
    press 65514  <span class="Comment"># right arrow - next line</span>
    type <span class="Constant">[0]</span>
  ]
  run [
    event-loop screen:address, console:address, 2:address:editor-data
  ]
  screen-should-contain [
   <span class="Constant"> .abc       .</span>
   <span class="Constant"> .0d        .</span>
   <span class="Constant"> .          .</span>
  ]
]

<span class="muScenario">scenario</span> editor-moves-cursor-to-next-wrapped-line-with-right-arrow [
  assume-screen <span class="Constant">10:literal/width</span>, <span class="Constant">5:literal/height</span>
  1:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[abcdef]</span>
  2:address:editor-data<span class="Special"> &lt;- </span>new-editor 1:address:array:character, screen:address, <span class="Constant">0:literal/top</span>, <span class="Constant">0:literal/left</span>, <span class="Constant">5:literal/right</span>
  assume-console [
    left-click 0, 3
    press 65514  <span class="Comment"># right arrow</span>
  ]
  run [
    event-loop screen:address, console:address, 2:address:editor-data
    3:number<span class="Special"> &lt;- </span>get 2:address:editor-data/deref, cursor-row:offset
    4:number<span class="Special"> &lt;- </span>get 2:address:editor-data/deref, cursor-column:offset
  ]
  screen-should-contain [
   <span class="Constant"> .abcd↩     .</span>
   <span class="Constant"> .ef        .</span>
   <span class="Constant"> .          .</span>
  ]
  memory-should-contain [
    3<span class="Special"> &lt;- </span>1
    4<span class="Special"> &lt;- </span>0
  ]
]

<span class="muScenario">scenario</span> editor-does-not-wrap-cursor-when-line-does-not-wrap [
  assume-screen <span class="Constant">10:literal/width</span>, <span class="Constant">5:literal/height</span>
  1:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[abcde]</span>
  2:address:editor-data<span class="Special"> &lt;- </span>new-editor 1:address:array:character, screen:address, <span class="Constant">0:literal/top</span>, <span class="Constant">0:literal/left</span>, <span class="Constant">5:literal/right</span>
  assume-console [
    left-click 0, 3  <span class="Comment"># one before right, in the last line</span>
    press 65514  <span class="Comment"># right arrow</span>
  ]
  run [
    event-loop screen:address, console:address, 2:address:editor-data
    3:number<span class="Special"> &lt;- </span>get 2:address:editor-data/deref, cursor-row:offset
    4:number<span class="Special"> &lt;- </span>get 2:address:editor-data/deref, cursor-column:offset
  ]
  memory-should-contain [
    3<span class="Special"> &lt;- </span>0
    4<span class="Special"> &lt;- </span>4
  ]
]

<span class="muScenario">scenario</span> editor-does-not-wrap-cursor-when-line-does-not-wrap-2 [
  assume-screen <span class="Constant">10:literal/width</span>, <span class="Constant">5:literal/height</span>
  1:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[abcde</span>
<span class="Constant">f]</span>
  2:address:editor-data<span class="Special"> &lt;- </span>new-editor 1:address:array:character, screen:address, <span class="Constant">0:literal/top</span>, <span class="Constant">0:literal/left</span>, <span class="Constant">5:literal/right</span>
  assume-console [
    left-click 0, 3  <span class="Comment"># one before right, not the last line</span>
    press 65514  <span class="Comment"># right arrow</span>
  ]
  run [
    event-loop screen:address, console:address, 2:address:editor-data
    3:number<span class="Special"> &lt;- </span>get 2:address:editor-data/deref, cursor-row:offset
    4:number<span class="Special"> &lt;- </span>get 2:address:editor-data/deref, cursor-column:offset
  ]
  memory-should-contain [
    3<span class="Special"> &lt;- </span>0
    4<span class="Special"> &lt;- </span>4
  ]
]

<span class="muScenario">scenario</span> editor-moves-cursor-to-next-line-with-right-arrow-at-end-of-line [
  assume-screen <span class="Constant">10:literal/width</span>, <span class="Constant">5:literal/height</span>
  1:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[abc</span>
<span class="Constant">d]</span>
  2:address:editor-data<span class="Special"> &lt;- </span>new-editor 1:address:array:character, screen:address, <span class="Constant">0:literal/top</span>, <span class="Constant">0:literal/left</span>, <span class="Constant">10:literal/right</span>
  assume-console [
    left-click 0, 3
    press 65514  <span class="Comment"># right arrow - next line</span>
    type <span class="Constant">[0]</span>
  ]
  run [
    event-loop screen:address, console:address, 2:address:editor-data
  ]
  screen-should-contain [
   <span class="Constant"> .abc       .</span>
   <span class="Constant"> .0d        .</span>
   <span class="Constant"> .          .</span>
  ]
]

<span class="muScenario">scenario</span> editor-moves-cursor-left-with-key [
  assume-screen <span class="Constant">10:literal/width</span>, <span class="Constant">5:literal/height</span>
  1:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[abc]</span>
  2:address:editor-data<span class="Special"> &lt;- </span>new-editor 1:address:array:character, screen:address, <span class="Constant">0:literal/top</span>, <span class="Constant">0:literal/left</span>, <span class="Constant">10:literal/right</span>
  assume-console [
    left-click 0, 2
    press 65515  <span class="Comment"># left arrow</span>
    type <span class="Constant">[0]</span>
  ]
  run [
    event-loop screen:address, console:address, 2:address:editor-data
  ]
  screen-should-contain [
   <span class="Constant"> .a0bc      .</span>
   <span class="Constant"> .          .</span>
  ]
]

<span class="muScenario">scenario</span> editor-moves-cursor-to-previous-line-with-left-arrow-at-start-of-line [
  assume-screen <span class="Constant">10:literal/width</span>, <span class="Constant">5:literal/height</span>
  <span class="Comment"># initialize editor with two lines</span>
  1:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[abc</span>
<span class="Constant">d]</span>
  2:address:editor-data<span class="Special"> &lt;- </span>new-editor 1:address:array:character, screen:address, <span class="Constant">0:literal/top</span>, <span class="Constant">0:literal/left</span>, <span class="Constant">10:literal/right</span>
  <span class="Comment"># position cursor at start of second line (so there's no previous newline)</span>
  assume-console [
    left-click 1, 0
    press 65515  <span class="Comment"># left arrow</span>
    type <span class="Constant">[0]</span>
  ]
  run [
    event-loop screen:address, console:address, 2:address:editor-data
  ]
  screen-should-contain [
   <span class="Constant"> .abc0      .</span>
   <span class="Constant"> .d         .</span>
   <span class="Constant"> .          .</span>
  ]
]

<span class="muScenario">scenario</span> editor-moves-cursor-to-previous-line-with-left-arrow-at-start-of-line-2 [
  assume-screen <span class="Constant">10:literal/width</span>, <span class="Constant">5:literal/height</span>
  <span class="Comment"># initialize editor with three lines</span>
  1:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[abc</span>
<span class="Constant">def</span>
<span class="Constant">g]</span>
  2:address:editor-data<span class="Special"> &lt;- </span>new-editor 1:address:array:character, screen:address, <span class="Constant">0:literal/top</span>, <span class="Constant">0:literal/left</span>, <span class="Constant">10:literal/right</span>
  <span class="Comment"># position cursor further down (so there's a newline before the character at</span>
  <span class="Comment"># the cursor)</span>
  assume-console [
    left-click 2, 0
    press 65515  <span class="Comment"># left arrow</span>
    type <span class="Constant">[0]</span>
  ]
  run [
    event-loop screen:address, console:address, 2:address:editor-data
  ]
  screen-should-contain [
   <span class="Constant"> .abc       .</span>
   <span class="Constant"> .def0      .</span>
   <span class="Constant"> .g         .</span>
   <span class="Constant"> .          .</span>
  ]
]

<span class="muScenario">scenario</span> editor-moves-cursor-to-previous-line-with-left-arrow-at-start-of-line-3 [
  assume-screen <span class="Constant">10:literal/width</span>, <span class="Constant">5:literal/height</span>
  1:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[abc</span>
<span class="Constant">def</span>
<span class="Constant">g]</span>
  2:address:editor-data<span class="Special"> &lt;- </span>new-editor 1:address:array:character, screen:address, <span class="Constant">0:literal/top</span>, <span class="Constant">0:literal/left</span>, <span class="Constant">10:literal/right</span>
  <span class="Comment"># position cursor at start of text</span>
  assume-console [
    left-click 0, 0
    press 65515  <span class="Comment"># left arrow should have no effect</span>
    type <span class="Constant">[0]</span>
  ]
  run [
    event-loop screen:address, console:address, 2:address:editor-data
  ]
  screen-should-contain [
   <span class="Constant"> .0abc      .</span>
   <span class="Constant"> .def       .</span>
   <span class="Constant"> .g         .</span>
   <span class="Constant"> .          .</span>
  ]
]

<span class="muScenario">scenario</span> editor-moves-cursor-to-previous-line-with-left-arrow-at-start-of-line-4 [
  assume-screen <span class="Constant">10:literal/width</span>, <span class="Constant">5:literal/height</span>
  <span class="Comment"># initialize editor with text containing an empty line</span>
  1:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[abc</span>

d]
  2:address:editor-data<span class="Special"> &lt;- </span>new-editor 1:address:array:character, screen:address, <span class="Constant">0:literal/top</span>, <span class="Constant">0:literal/left</span>, <span class="Constant">10:literal/right</span>
  <span class="Comment"># position cursor right after empty line</span>
  assume-console [
    left-click 2, 0
    press 65515  <span class="Comment"># left arrow</span>
    type <span class="Constant">[0]</span>
  ]
  run [
    event-loop screen:address, console:address, 2:address:editor-data
  ]
  screen-should-contain [
   <span class="Constant"> .abc       .</span>
   <span class="Constant"> .0         .</span>
   <span class="Constant"> .d         .</span>
   <span class="Constant"> .          .</span>
  ]
]

<span class="muScenario">scenario</span> editor-moves-across-screen-lines-across-wrap-with-left-arrow [
  assume-screen <span class="Constant">10:literal/width</span>, <span class="Constant">5:literal/height</span>
  <span class="Comment"># initialize editor with text containing an empty line</span>
  1:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[abcdef]</span>
  2:address:editor-data<span class="Special"> &lt;- </span>new-editor 1:address:array:character, screen:address, <span class="Constant">0:literal/top</span>, <span class="Constant">0:literal/left</span>, <span class="Constant">5:literal/right</span>
  screen-should-contain [
   <span class="Constant"> .abcd↩     .</span>
   <span class="Constant"> .ef        .</span>
   <span class="Constant"> .          .</span>
  ]
  <span class="Comment"># position cursor right after empty line</span>
  assume-console [
    left-click 1, 0
    press 65515  <span class="Comment"># left arrow</span>
  ]
  run [
    event-loop screen:address, console:address, 2:address:editor-data
    3:number<span class="Special"> &lt;- </span>get 2:address:editor-data/deref, cursor-row:offset
    4:number<span class="Special"> &lt;- </span>get 2:address:editor-data/deref, cursor-column:offset
  ]
  memory-should-contain [
    3<span class="Special"> &lt;- </span>0  <span class="Comment"># previous row</span>
    4<span class="Special"> &lt;- </span>3  <span class="Comment"># end of wrapped line</span>
  ]
]

<span class="muScenario">scenario</span> editor-moves-to-previous-line-with-up-arrow [
  assume-screen <span class="Constant">10:literal/width</span>, <span class="Constant">5:literal/height</span>
  1:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[abc</span>
<span class="Constant">def]</span>
  2:address:editor-data<span class="Special"> &lt;- </span>new-editor 1:address:array:character, screen:address, <span class="Constant">0:literal/top</span>, <span class="Constant">0:literal/left</span>, <span class="Constant">10:literal/right</span>
  assume-console [
    left-click 1, 1
    press 65517  <span class="Comment"># up arrow</span>
  ]
  run [
    event-loop screen:address, console:address, 2:address:editor-data
    3:number<span class="Special"> &lt;- </span>get 2:address:editor-data/deref, cursor-row:offset
    4:number<span class="Special"> &lt;- </span>get 2:address:editor-data/deref, cursor-column:offset
  ]
  memory-should-contain [
    3<span class="Special"> &lt;- </span>0
    4<span class="Special"> &lt;- </span>1
  ]
]

<span class="muScenario">scenario</span> editor-moves-to-next-line-with-down-arrow [
  assume-screen <span class="Constant">10:literal/width</span>, <span class="Constant">5:literal/height</span>
  1:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[abc</span>
<span class="Constant">def]</span>
  2:address:editor-data<span class="Special"> &lt;- </span>new-editor 1:address:array:character, screen:address, <span class="Constant">0:literal/top</span>, <span class="Constant">0:literal/left</span>, <span class="Constant">10:literal/right</span>
  <span class="Comment"># cursor starts out at (0, 0)</span>
  assume-console [
    press 65516  <span class="Comment"># down arrow</span>
  ]
  run [
    event-loop screen:address, console:address, 2:address:editor-data
    3:number<span class="Special"> &lt;- </span>get 2:address:editor-data/deref, cursor-row:offset
    4:number<span class="Special"> &lt;- </span>get 2:address:editor-data/deref, cursor-column:offset
  ]
  <span class="Comment"># ..and ends at (1, 0)</span>
  memory-should-contain [
    3<span class="Special"> &lt;- </span>1
    4<span class="Special"> &lt;- </span>0
  ]
]

<span class="muScenario">scenario</span> editor-adjusts-column-at-previous-line [
  assume-screen <span class="Constant">10:literal/width</span>, <span class="Constant">5:literal/height</span>
  1:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[ab</span>
<span class="Constant">def]</span>
  2:address:editor-data<span class="Special"> &lt;- </span>new-editor 1:address:array:character, screen:address, <span class="Constant">0:literal/top</span>, <span class="Constant">0:literal/left</span>, <span class="Constant">10:literal/right</span>
  assume-console [
    left-click 1, 3
    press 65517  <span class="Comment"># up arrow</span>
  ]
  run [
    event-loop screen:address, console:address, 2:address:editor-data
    3:number<span class="Special"> &lt;- </span>get 2:address:editor-data/deref, cursor-row:offset
    4:number<span class="Special"> &lt;- </span>get 2:address:editor-data/deref, cursor-column:offset
  ]
  memory-should-contain [
    3<span class="Special"> &lt;- </span>0
    4<span class="Special"> &lt;- </span>2
  ]
]

<span class="muScenario">scenario</span> editor-adjusts-column-at-next-line [
  assume-screen <span class="Constant">10:literal/width</span>, <span class="Constant">5:literal/height</span>
  1:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[abc</span>
<span class="Constant">de]</span>
  2:address:editor-data<span class="Special"> &lt;- </span>new-editor 1:address:array:character, screen:address, <span class="Constant">0:literal/top</span>, <span class="Constant">0:literal/left</span>, <span class="Constant">10:literal/right</span>
  assume-console [
    left-click 0, 3
    press 65516  <span class="Comment"># down arrow</span>
  ]
  run [
    event-loop screen:address, console:address, 2:address:editor-data
    3:number<span class="Special"> &lt;- </span>get 2:address:editor-data/deref, cursor-row:offset
    4:number<span class="Special"> &lt;- </span>get 2:address:editor-data/deref, cursor-column:offset
  ]
  memory-should-contain [
    3<span class="Special"> &lt;- </span>1
    4<span class="Special"> &lt;- </span>2
  ]
]

<span class="muScenario">scenario</span> point-at-multiple-editors [
  assume-screen <span class="Constant">10:literal/width</span>, <span class="Constant">5:literal/height</span>
  <span class="Comment"># initialize an editor covering left half of screen</span>
  1:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[abc]</span>
  2:address:editor-data<span class="Special"> &lt;- </span>new-editor 1:address:array:character, screen:address, <span class="Constant">0:literal/top</span>, <span class="Constant">0:literal/left</span>, <span class="Constant">5:literal/right</span>
  3:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[def]</span>
  <span class="Comment"># chain new editor to it, covering the right half of the screen</span>
  4:address:address:editor-data<span class="Special"> &lt;- </span>get-address 2:address:editor-data/deref, next-editor:offset
  4:address:address:editor-data/deref<span class="Special"> &lt;- </span>new-editor 3:address:array:character, screen:address, <span class="Constant">0:literal/top</span>, <span class="Constant">5:literal/left</span>, <span class="Constant">10:literal/right</span>
  <span class="Comment"># type one letter in each of them</span>
  assume-console [
    left-click 0, 1
    left-click 0, 8
  ]
  run [
    event-loop screen:address, console:address, 2:address:editor-data
    5:number<span class="Special"> &lt;- </span>get 2:address:editor-data/deref, cursor-column:offset
    6:number<span class="Special"> &lt;- </span>get 4:address:address:editor-data/deref/deref, cursor-column:offset
  ]
  memory-should-contain [
    5<span class="Special"> &lt;- </span>1
    6<span class="Special"> &lt;- </span>8
  ]
]

<span class="muScenario">scenario</span> editors-chain-to-cover-multiple-columns [
  assume-screen <span class="Constant">10:literal/width</span>, <span class="Constant">5:literal/height</span>
  <span class="Comment"># initialize an editor covering left half of screen</span>
  1:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[abc]</span>
  2:address:editor-data<span class="Special"> &lt;- </span>new-editor 1:address:array:character, screen:address, <span class="Constant">0:literal/top</span>, <span class="Constant">0:literal/left</span>, <span class="Constant">5:literal/right</span>
  3:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[def]</span>
  <span class="Comment"># chain new editor to it, covering the right half of the screen</span>
  4:address:address:editor-data<span class="Special"> &lt;- </span>get-address 2:address:editor-data/deref, next-editor:offset
  4:address:address:editor-data/deref<span class="Special"> &lt;- </span>new-editor 3:address:array:character, screen:address, <span class="Constant">0:literal/top</span>, <span class="Constant">5:literal/left</span>, <span class="Constant">10:literal/right</span>
  reset-focus 2:address:editor-data
  <span class="Comment"># type one letter in each of them</span>
  assume-console [
    left-click 0, 1
    type <span class="Constant">[0]</span>
    left-click 0, 6
    type <span class="Constant">[1]</span>
  ]
  run [
    event-loop screen:address, console:address, 2:address:editor-data
    5:number<span class="Special"> &lt;- </span>get 2:address:editor-data/deref, cursor-column:offset
    6:number<span class="Special"> &lt;- </span>get 4:address:address:editor-data/deref/deref, cursor-column:offset
  ]
  screen-should-contain [
   <span class="Constant"> .a0bc d1ef .</span>
   <span class="Constant"> .          .</span>
  ]
  memory-should-contain [
    5<span class="Special"> &lt;- </span>2
    6<span class="Special"> &lt;- </span>7
  ]
  <span class="Comment"># show the cursor at the right window</span>
  run [
    screen:address<span class="Special"> &lt;- </span>print-character screen:address, <span class="Constant">9251:literal/␣</span>
  ]
  screen-should-contain [
   <span class="Constant"> .a0bc d1␣f .</span>
   <span class="Constant"> .          .</span>
  ]
]

<span class="muScenario">scenario</span> multiple-editors-cover-only-their-own-areas [
  assume-screen <span class="Constant">10:literal/width</span>, <span class="Constant">5:literal/height</span>
  run [
    <span class="Comment"># draw a divider</span>
    draw-vertical screen:address, <span class="Constant">5:literal/divider</span>, <span class="Constant">0:literal/top</span>, <span class="Constant">5:literal/height</span>
    <span class="Comment"># initialize editors on both sides of it and chain the two</span>
    1:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[abc]</span>
    2:address:editor-data<span class="Special"> &lt;- </span>new-editor 1:address:array:character, screen:address, <span class="Constant">0:literal/top</span>, <span class="Constant">0:literal/left</span>, <span class="Constant">5:literal/right</span>
    3:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[def]</span>
    4:address:address:editor-data<span class="Special"> &lt;- </span>get-address 2:address:editor-data/deref, next-editor:offset
    4:address:address:editor-data/deref<span class="Special"> &lt;- </span>new-editor 3:address:array:character, screen:address, <span class="Constant">0:literal/top</span>, <span class="Constant">6:literal/left</span>, <span class="Constant">10:literal/right</span>
  ]
  <span class="Comment"># divider isn't messed up</span>
  screen-should-contain [
   <span class="Constant"> .abc  │def .</span>
   <span class="Constant"> .     │    .</span>
   <span class="Constant"> .     │    .</span>
   <span class="Constant"> .     │    .</span>
   <span class="Constant"> .     │    .</span>
  ]
]

<span class="muScenario">scenario</span> editor-in-focus-keeps-cursor [
  assume-screen <span class="Constant">10:literal/width</span>, <span class="Constant">5:literal/height</span>
  <span class="Comment"># initialize an editor covering left half of screen</span>
  1:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[abc]</span>
  2:address:editor-data<span class="Special"> &lt;- </span>new-editor 1:address:array:character, screen:address, <span class="Constant">0:literal/top</span>, <span class="Constant">0:literal/left</span>, <span class="Constant">5:literal/right</span>
  3:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[def]</span>
  <span class="Comment"># chain new editor to it, covering the right half of the screen</span>
  4:address:address:editor-data<span class="Special"> &lt;- </span>get-address 2:address:editor-data/deref, next-editor:offset
  4:address:address:editor-data/deref<span class="Special"> &lt;- </span>new-editor 3:address:array:character, screen:address, <span class="Constant">0:literal/top</span>, <span class="Constant">5:literal/left</span>, <span class="Constant">10:literal/right</span>
  <span class="Comment"># initialize cursor</span>
  run [
    reset-focus 2:address:editor-data
    5:number<span class="Special"> &lt;- </span>get 2:address:editor-data/deref, cursor-row:offset
    6:number<span class="Special"> &lt;- </span>get 2:address:editor-data/deref, cursor-column:offset
    move-cursor screen:address, 5:number, 6:number
    screen:address<span class="Special"> &lt;- </span>print-character screen:address, <span class="Constant">9251:literal/␣</span>
  ]
  <span class="Comment"># is it at the right place?</span>
  screen-should-contain [
   <span class="Constant"> .␣bc  def  .</span>
   <span class="Constant"> .          .</span>
  ]
  <span class="Comment"># now try typing a letter</span>
  assume-console [
    type <span class="Constant">[z]</span>
  ]
  run [
    event-loop screen:address, console:address, 2:address:editor-data
    screen:address<span class="Special"> &lt;- </span>print-character screen:address, <span class="Constant">9251:literal/␣</span>
  ]
  <span class="Comment"># cursor should still be right</span>
  screen-should-contain [
   <span class="Constant"> .z␣bc def  .</span>
   <span class="Constant"> .          .</span>
  ]
]

<span class="Comment"># set focus to first editor, reset it in later ones</span>
<span class="muRecipe">recipe</span> reset-focus [
  <span class="Constant">default-space</span>:address:array:location<span class="Special"> &lt;- </span>new location:type, <span class="Constant">30:literal</span>
  editor:address:editor-data<span class="Special"> &lt;- </span><span class="Constant">next-ingredient</span>
  in-focus:address:boolean<span class="Special"> &lt;- </span>get-address editor:address:editor-data/deref, in-focus?:offset
  in-focus:address:boolean/deref<span class="Special"> &lt;- </span>copy <span class="Constant">1:literal/true</span>
  e:address:editor-data<span class="Special"> &lt;- </span>get editor:address:editor-data/deref, next-editor:offset
  <span class="Delimiter">{</span>
    <span class="muControl">break-unless</span> e:address:editor-data
<span class="CommentedCode">#?     $print [resetting focus in ], e:address:editor-data, [ </span>
<span class="CommentedCode">#? ] #? 1</span>
    x:address:boolean<span class="Special"> &lt;- </span>get-address e:address:editor-data/deref, in-focus?:offset
<span class="CommentedCode">#?     $print [ at ], x:address:boolean, [ </span>
<span class="CommentedCode">#? ] #? 1</span>
    x:address:boolean/deref<span class="Special"> &lt;- </span>copy <span class="Constant">0:literal/false</span>
    e:address:editor-data<span class="Special"> &lt;- </span>get e:address:editor-data/deref, next-editor:offset
    <span class="muControl">loop</span>
  <span class="Delimiter">}</span>
]

<span class="SalientComment">## helpers for drawing editor borders</span>

<span class="muRecipe">recipe</span> draw-box [
  <span class="Constant">default-space</span>:address:array:location<span class="Special"> &lt;- </span>new location:type, <span class="Constant">30:literal</span>
  screen:address<span class="Special"> &lt;- </span><span class="Constant">next-ingredient</span>
  top:number<span class="Special"> &lt;- </span><span class="Constant">next-ingredient</span>
  left:number<span class="Special"> &lt;- </span><span class="Constant">next-ingredient</span>
  bottom:number<span class="Special"> &lt;- </span><span class="Constant">next-ingredient</span>
  right:number<span class="Special"> &lt;- </span><span class="Constant">next-ingredient</span>
  color:number, color-found?:boolean<span class="Special"> &lt;- </span><span class="Constant">next-ingredient</span>
  <span class="Delimiter">{</span>
    <span class="Comment"># default color to white</span>
    <span class="muControl">break-if</span> color-found?:boolean
    color:number<span class="Special"> &lt;- </span>copy <span class="Constant">245:literal/grey</span>
  <span class="Delimiter">}</span>
  <span class="Comment"># top border</span>
  draw-horizontal screen:address, top:number, left:number, right:number, color:number
  draw-horizontal screen:address, bottom:number, left:number, right:number, color:number
  draw-vertical screen:address, left:number, top:number, bottom:number, color:number
  draw-vertical screen:address, right:number, top:number, bottom:number, color:number
  draw-top-left screen:address, top:number, left:number, color:number
  draw-top-right screen:address, top:number, right:number, color:number
  draw-bottom-left screen:address, bottom:number, left:number, color:number
  draw-bottom-right screen:address, bottom:number, right:number, color:number
  <span class="Comment"># position cursor inside box</span>
  move-cursor screen:address, top:number, left:number
  cursor-down screen:address
  cursor-right screen:address
]

<span class="muRecipe">recipe</span> draw-horizontal [
  <span class="Constant">default-space</span>:address:array:location<span class="Special"> &lt;- </span>new location:type, <span class="Constant">30:literal</span>
  screen:address<span class="Special"> &lt;- </span><span class="Constant">next-ingredient</span>
  row:number<span class="Special"> &lt;- </span><span class="Constant">next-ingredient</span>
  x:number<span class="Special"> &lt;- </span><span class="Constant">next-ingredient</span>
  right:number<span class="Special"> &lt;- </span><span class="Constant">next-ingredient</span>
  color:number, color-found?:boolean<span class="Special"> &lt;- </span><span class="Constant">next-ingredient</span>
  <span class="Delimiter">{</span>
    <span class="Comment"># default color to white</span>
    <span class="muControl">break-if</span> color-found?:boolean
    color:number<span class="Special"> &lt;- </span>copy <span class="Constant">245:literal/grey</span>
  <span class="Delimiter">}</span>
  move-cursor screen:address, row:number, x:number
  <span class="Delimiter">{</span>
    continue?:boolean<span class="Special"> &lt;- </span>lesser-than x:number, right:number
    <span class="muControl">break-unless</span> continue?:boolean
    print-character screen:address, <span class="Constant">9472:literal/horizontal</span>, color:number
    x:number<span class="Special"> &lt;- </span>add x:number, <span class="Constant">1:literal</span>
    <span class="muControl">loop</span>
  <span class="Delimiter">}</span>
]

<span class="muRecipe">recipe</span> draw-vertical [
  <span class="Constant">default-space</span>:address:array:location<span class="Special"> &lt;- </span>new location:type, <span class="Constant">30:literal</span>
  screen:address<span class="Special"> &lt;- </span><span class="Constant">next-ingredient</span>
  col:number<span class="Special"> &lt;- </span><span class="Constant">next-ingredient</span>
  x:number<span class="Special"> &lt;- </span><span class="Constant">next-ingredient</span>
  bottom:number<span class="Special"> &lt;- </span><span class="Constant">next-ingredient</span>
  color:number, color-found?:boolean<span class="Special"> &lt;- </span><span class="Constant">next-ingredient</span>
  <span class="Delimiter">{</span>
    <span class="Comment"># default color to white</span>
    <span class="muControl">break-if</span> color-found?:boolean
    color:number<span class="Special"> &lt;- </span>copy <span class="Constant">245:literal/grey</span>
  <span class="Delimiter">}</span>
  <span class="Delimiter">{</span>
    continue?:boolean<span class="Special"> &lt;- </span>lesser-than x:number, bottom:number
    <span class="muControl">break-unless</span> continue?:boolean
    move-cursor screen:address, x:number, col:number
    print-character screen:address, <span class="Constant">9474:literal/vertical</span>, color:number
    x:number<span class="Special"> &lt;- </span>add x:number, <span class="Constant">1:literal</span>
    <span class="muControl">loop</span>
  <span class="Delimiter">}</span>
]

<span class="muRecipe">recipe</span> draw-top-left [
  <span class="Constant">default-space</span>:address:array:location<span class="Special"> &lt;- </span>new location:type, <span class="Constant">30:literal</span>
  screen:address<span class="Special"> &lt;- </span><span class="Constant">next-ingredient</span>
  top:number<span class="Special"> &lt;- </span><span class="Constant">next-ingredient</span>
  left:number<span class="Special"> &lt;- </span><span class="Constant">next-ingredient</span>
  color:number, color-found?:boolean<span class="Special"> &lt;- </span><span class="Constant">next-ingredient</span>
  <span class="Delimiter">{</span>
    <span class="Comment"># default color to white</span>
    <span class="muControl">break-if</span> color-found?:boolean
    color:number<span class="Special"> &lt;- </span>copy <span class="Constant">245:literal/grey</span>
  <span class="Delimiter">}</span>
  move-cursor screen:address, top:number, left:number
  print-character screen:address, <span class="Constant">9484:literal/down-right</span>, color:number
]

<span class="muRecipe">recipe</span> draw-top-right [
  <span class="Constant">default-space</span>:address:array:location<span class="Special"> &lt;- </span>new location:type, <span class="Constant">30:literal</span>
  screen:address<span class="Special"> &lt;- </span><span class="Constant">next-ingredient</span>
  top:number<span class="Special"> &lt;- </span><span class="Constant">next-ingredient</span>
  right:number<span class="Special"> &lt;- </span><span class="Constant">next-ingredient</span>
  color:number, color-found?:boolean<span class="Special"> &lt;- </span><span class="Constant">next-ingredient</span>
  <span class="Delimiter">{</span>
    <span class="Comment"># default color to white</span>
    <span class="muControl">break-if</span> color-found?:boolean
    color:number<span class="Special"> &lt;- </span>copy <span class="Constant">245:literal/grey</span>
  <span class="Delimiter">}</span>
  move-cursor screen:address, top:number, right:number
  print-character screen:address, <span class="Constant">9488:literal/down-left</span>, color:number
]

<span class="muRecipe">recipe</span> draw-bottom-left [
  <span class="Constant">default-space</span>:address:array:location<span class="Special"> &lt;- </span>new location:type, <span class="Constant">30:literal</span>
  screen:address<span class="Special"> &lt;- </span><span class="Constant">next-ingredient</span>
  bottom:number<span class="Special"> &lt;- </span><span class="Constant">next-ingredient</span>
  left:number<span class="Special"> &lt;- </span><span class="Constant">next-ingredient</span>
  color:number, color-found?:boolean<span class="Special"> &lt;- </span><span class="Constant">next-ingredient</span>
  <span class="Delimiter">{</span>
    <span class="Comment"># default color to white</span>
    <span class="muControl">break-if</span> color-found?:boolean
    color:number<span class="Special"> &lt;- </span>copy <span class="Constant">245:literal/grey</span>
  <span class="Delimiter">}</span>
  move-cursor screen:address, bottom:number, left:number
  print-character screen:address, <span class="Constant">9492:literal/up-right</span>, color:number
]

<span class="muRecipe">recipe</span> draw-bottom-right [
  <span class="Constant">default-space</span>:address:array:location<span class="Special"> &lt;- </span>new location:type, <span class="Constant">30:literal</span>
  screen:address<span class="Special"> &lt;- </span><span class="Constant">next-ingredient</span>
  bottom:number<span class="Special"> &lt;- </span><span class="Constant">next-ingredient</span>
  right:number<span class="Special"> &lt;- </span><span class="Constant">next-ingredient</span>
  color:number, color-found?:boolean<span class="Special"> &lt;- </span><span class="Constant">next-ingredient</span>
  <span class="Delimiter">{</span>
    <span class="Comment"># default color to white</span>
    <span class="muControl">break-if</span> color-found?:boolean
    color:number<span class="Special"> &lt;- </span>copy <span class="Constant">245:literal/grey</span>
  <span class="Delimiter">}</span>
  move-cursor screen:address, bottom:number, right:number
  print-character screen:address, <span class="Constant">9496:literal/up-left</span>, color:number
]
</pre>
</body>
</html>
<!-- vim: set foldmethod=manual : -->