about summary refs log blame commit diff stats
path: root/edit.mu
blob: 3c206bbd54014a475bea70b84e242978de749fe7 (plain) (tree)
pre { line-height: 125%; }
td.linenos .normal { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
span.linenos { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
td.linenos .special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
span.linenos.special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
.highlight .hll { background-color: #ffffcc }
.highlight .c { color: #888888 } /* Comment */
.highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */
.highlight .k { color: #008800; font-weight: bold } /* Keyword */
.highlight .ch { color: #888888 } /* Comment.Hashbang */
.highlight .cm { color: #888888 } /* Comment.Multiline */
.highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */
.highlight .cpf { color: #888888 } /* Comment.PreprocFile */
.highlight .c1 { color: #888888 } /* Comment.Single */
.highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */
.highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .ges { font-weight: bold; font-style: italic } /* Generic.EmphStrong */
.highlight .gr { color: #aa0000 } /* Generic.Error */
.highlight .gh { color: #333333 } /* Generic.Heading */
.highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */
.highlight .go { color: #888888 } /* Generic.Output */
.highlight .gp { color: #555555 } /* Generic.Prompt */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #666666 } /* Generic.Subheading */
.highlight .gt { color: #aa0000 } /* Generic.Traceback */
.highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */
.highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */
.highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */
.highlight .kp { color: #008800 } /* Keyword.Pseudo */
.highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */
.highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */
.highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */
.highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */
.highlight .na { color: #336699 } /* Name.Attribute */
.highlight .nb { color: #003388 } /* Name.Builtin */
.highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */
.highlight .no { color: #003366; font-weight: bold } /* Name.Constant */
.highlight .nd { color: #555555 } /* Name.Decorator */
.highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */
.highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */
.highlight .nl { color: #336699; font-style: italic } /* Name.Label */
.highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */
.highlight .py { color: #336699; font-weight: bold } /* Name.Property */
.highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */
.highlight .nv { color: #336699 } /* Name.Variable */
.highlight .ow { color: #008800 } /* Operator.Word */
.highlight .w { color: #bbbbbb } /* Text.Whitespace */
.highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */
.highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */
.highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */
.highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */
.highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */
.highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */
.highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */
.highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */
.highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */
.highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */
.highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */
.highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */
.highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */
.highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */
.highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */
.highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */
.highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */
.highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */
.highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */
.highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */
.highlight .vc { color: #336699 } /* Name.Variable.Class */
.highlight .vg { color: #dd7700 } /* Name.Variable.Global */
.highlight .vi { color: #3333bb } /* Name.Variable.Instance */
.highlight .vm { color: #336699 } /* Name.Variable.Magic */
.highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
# Some slow but convenient helpers

# slow, iterative shift-left instruction
# preconditions: _nr >= 0, _dr > 0
fn repeated-shift-left nr: int, dr: int -> _/eax: int {
  var result/eax: int <- copy nr
  {
    compare dr, 0
    break-if-<=
    result <- shift-left 1
    decrement dr
    loop
  }
  return result
}

# slow, iterative shift-right instruction
# preconditions: _nr >= 0, _dr > 0
fn repeated-shift-right nr: int, dr: int -> _/eax: int {
  var result/eax: int <- copy nr
  {
    compare dr, 0
    break-if-<=
    result <- shift-right 1
    decrement dr
    loop
  }
  return result
}

fn abs n: int -> _/eax: int {
  var result/eax: int <- copy n
  {
    compare n, 0
    break-if->=
    result <- negate
  }
  return result
}
mu/commit/edit.mu?h=main&id=9897c8177bba26946a54513f5a8113a375f205f0'>9897c817 ^
a2a2bb4e ^
9897c817 ^





65be4e4c ^

9897c817 ^
44f9a160 ^





f6823642 ^



44f9a160 ^

9897c817 ^
7d154c49 ^



7d154c49 ^
44f9a160 ^
7d154c49 ^
91619b98 ^

ccd84d94 ^

7d154c49 ^


91619b98 ^

7d154c49 ^



91619b98 ^
7d154c49 ^
91619b98 ^
6179649e ^
9897c817 ^


ccd84d94 ^
c56cdd63 ^


9897c817 ^






44f9a160 ^


9897c817 ^












22a25708 ^


683961a7 ^
91619b98 ^
de2e0c7f ^
8d76b9ce ^
ccd84d94 ^


7d154c49 ^
ccd84d94 ^
596490f4 ^
91619b98 ^

eed47a86 ^
6179649e ^
91619b98 ^
a7f77285 ^
7b7aa5f6 ^
eed47a86 ^


7b7aa5f6 ^


91619b98 ^


7d154c49 ^
7b7aa5f6 ^
17c3d13e ^


78b4ac76 ^
eed47a86 ^
78b4ac76 ^
eed47a86 ^
78b4ac76 ^
ccd84d94 ^
e80834f3 ^


78b4ac76 ^
91619b98 ^
de2e0c7f ^
8d76b9ce ^
7b7aa5f6 ^



eed47a86 ^







e80834f3 ^


eed47a86 ^
e6a9f5dc ^



7ed2441b ^
e6a9f5dc ^






eed47a86 ^
7b7aa5f6 ^


91619b98 ^
58269c27 ^
7b7aa5f6 ^

e45c1ce2 ^



91619b98 ^


e45c1ce2 ^






91619b98 ^
e45c1ce2 ^

7b7aa5f6 ^
91619b98 ^
eed47a86 ^
7b7aa5f6 ^


ccd84d94 ^


eed47a86 ^


e80834f3 ^

eed47a86 ^


58269c27 ^

eed47a86 ^

d933f15b ^

bf80cea9 ^








58269c27 ^

eed47a86 ^
e80834f3 ^


eed47a86 ^
8d76b9ce ^


21c7573c ^
06a0bf49 ^
8d76b9ce ^
06a0bf49 ^
d933f15b ^
06a0bf49 ^




21c7573c ^
06a0bf49 ^
21c7573c ^


06a0bf49 ^
21c7573c ^






8d76b9ce ^
ccd84d94 ^
683961a7 ^


bf80cea9 ^



683961a7 ^

596490f4 ^
ccd84d94 ^
9fae6657 ^
37e87ffa ^
9897c817 ^
37e87ffa ^
37e87ffa ^


9897c817 ^
37e87ffa ^







9897c817 ^
37e87ffa ^
37e87ffa ^

9897c817 ^
37e87ffa ^






7b7aa5f6 ^
9897c817 ^
7b7aa5f6 ^
7b7aa5f6 ^


9897c817 ^
7b7aa5f6 ^






e45c1ce2 ^
9897c817 ^
e45c1ce2 ^
e45c1ce2 ^

9897c817 ^
e45c1ce2 ^











8f496ddc ^
44f9a160 ^


















65be4e4c ^
70179d1f ^
f6823642 ^

70179d1f ^


f2d7ad5d ^
70179d1f ^

f6823642 ^
f2d7ad5d ^
70179d1f ^
e552479a ^
c5182121 ^
f6823642 ^


c5182121 ^
f6823642 ^



c5182121 ^
f6823642 ^

ccd84d94 ^
f6823642 ^



ccd84d94 ^
683961a7 ^













f6823642 ^




af6d9314 ^
f6823642 ^






















8d76b9ce ^
f6823642 ^


8d76b9ce ^






f6823642 ^









af6d9314 ^

b66ee675 ^
f6823642 ^

b66ee675 ^

f6823642 ^
b66ee675 ^

f6823642 ^
b66ee675 ^

17c3d13e ^
b66ee675 ^


f6823642 ^

17f4b412 ^













b30424f3 ^
b66ee675 ^





00b788d4 ^
17f4b412 ^


17f4b412 ^

f6823642 ^
17c3d13e ^
f6823642 ^




b66ee675 ^


f6823642 ^


6bd57d3b ^
f6823642 ^



6bd57d3b ^
0392bc2c ^














f6823642 ^
0392bc2c ^

70179d1f ^
af6d9314 ^























70179d1f ^

c5182121 ^


e8019034 ^
f6823642 ^


af34d266 ^


f6823642 ^
af34d266 ^

f6823642 ^

8d76b9ce ^
f6823642 ^

8b9e9fb1 ^




8d76b9ce ^

ccd84d94 ^





56d0c162 ^

ccd84d94 ^

ccd84d94 ^
56d0c162 ^


de2e0c7f ^




de2e0c7f ^
de2e0c7f ^
f6823642 ^
de2e0c7f ^
56d0c162 ^




















de2e0c7f ^
8b9e9fb1 ^
c5182121 ^

8d76b9ce ^














d933f15b ^

8d76b9ce ^

44f9a160 ^

ec3aa9e0 ^


















ec3aa9e0 ^


c5182121 ^

dd0846dc ^






f2d7ad5d ^
dd0846dc ^

c5182121 ^
dd0846dc ^
c5182121 ^
dd0846dc ^

c5182121 ^







dd0846dc ^

f2d7ad5d ^
dd0846dc ^
c5182121 ^

f2d7ad5d ^
c5182121 ^












eed47a86 ^

dd0846dc ^

eed47a86 ^
a81e1f32 ^
eed47a86 ^

eed47a86 ^











dd0846dc ^


eed47a86 ^
a81e1f32 ^
eed47a86 ^

eed47a86 ^











dd0846dc ^


eed47a86 ^
a81e1f32 ^
eed47a86 ^

eed47a86 ^









af34d266 ^

dd0846dc ^


af34d266 ^




af34d266 ^













44f9a160 ^

dd0846dc ^

44f9a160 ^



44f9a160 ^







8b9e9fb1 ^
ccd84d94 ^
dd0846dc ^

ccd84d94 ^





ccd84d94 ^







58269c27 ^

dd0846dc ^

58269c27 ^




58269c27 ^









dd0846dc ^

58269c27 ^




58269c27 ^









dd0846dc ^


58269c27 ^




58269c27 ^








e80834f3 ^

dd0846dc ^


e80834f3 ^




e80834f3 ^








bf80cea9 ^

























cc9c7001 ^

dd0846dc ^


cc9c7001 ^



cc9c7001 ^

dd0846dc ^
cc9c7001 ^
cc9c7001 ^


dd0846dc ^
cc9c7001 ^





dd0846dc ^
cc9c7001 ^
cc9c7001 ^





8b9e9fb1 ^

dd0846dc ^

8b9e9fb1 ^



8b9e9fb1 ^







56d0c162 ^















































de2e0c7f ^

dd0846dc ^

de2e0c7f ^




de2e0c7f ^








7ed2441b ^



















8d76b9ce ^




























06a0bf49 ^




















d933f15b ^








































17c3d13e ^

dd0846dc ^

17c3d13e ^




17c3d13e ^







fae8242e ^

dd0846dc ^


fae8242e ^







fae8242e ^








17f4b412 ^























b30424f3 ^




00b788d4 ^




















b30424f3 ^












b24b0144 ^

dd0846dc ^


b24b0144 ^





b24b0144 ^








6bd57d3b ^

dd0846dc ^

6bd57d3b ^





6bd57d3b ^







ec3aa9e0 ^

dd0846dc ^



ec3aa9e0 ^






ec3aa9e0 ^










dd0846dc ^






ec3aa9e0 ^





ec3aa9e0 ^











dd0846dc ^



ec3aa9e0 ^






ec3aa9e0 ^











dd0846dc ^




ec3aa9e0 ^






ec3aa9e0 ^









0392bc2c ^

























af6d9314 ^
















































































65be4e4c ^
























f6823642 ^
f6823642 ^




























683961a7 ^









21c7573c ^

21c7573c ^
9680bcba ^


21c7573c ^


21c7573c ^


9680bcba ^
21c7573c ^








683961a7 ^


































f6823642 ^





















70179d1f ^
b5ef8468 ^
8f496ddc ^






65b55dbc ^





8f496ddc ^
65b55dbc ^







8cf49ee0 ^
8f496ddc ^
8cf49ee0 ^









65b55dbc ^





8cf49ee0 ^
8f496ddc ^


65b55dbc ^
8f496ddc ^


8cf49ee0 ^







65b55dbc ^





8f496ddc ^


8cf49ee0 ^
65b55dbc ^
8f496ddc ^


8cf49ee0 ^






65b55dbc ^





8f496ddc ^
65b55dbc ^
8cf49ee0 ^






65b55dbc ^





8cf49ee0 ^
65b55dbc ^
8cf49ee0 ^






65b55dbc ^





8cf49ee0 ^
65b55dbc ^
8cf49ee0 ^






65b55dbc ^





8cf49ee0 ^
65b55dbc ^
8f496ddc ^
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
                                                                                
 
             
                                                                       
              

                                 
               

                                                                              
                      
                                             
                                                                                                                                                                    













                                                                                                                                              
               

 
                                                   
                                                  
       

                                                                                                         




                         

 


                                                                            
                       
                                                                       


                                                          

                                      









                                        
 

                                                                          
                                 
                                                                                  

 




                                                                                                                                


                                                                       




                                                  
                                                    








                                                                                          
                                        





                                                                                        

                           
                                            





                                                                                                        



                                                                                     

                                                   
                            



                                                                    
                                                               
                                                                         
   

                                                

                                                        


                                                                    

                                     



                                                                    
        
   
                                      
                                                                                                     


                                                                                      
                                                                 


                                  






                                                                                                                      


                                          












                                


               
                                                                       
                                               
                       
         


                                                                       
                                                      
                                                                    
                            

                                                                                        
                                                           
                                                                  
                     
                               
                                   


                                                                                                                 


                                                       


                                          
                                                                            
                                


                                                                              
     
                                                                                 
                                         
                                                                                   
                                     
                                                                                             


                                                                                                                                                             
     
                                                                   
                                           
         



                                                               







                                                                                                


                                                                                                                                                               
       



                                                                     
                                                                 






                                                        
                         


                                                           
                                                                      
                                                                      

                                



                                                             


                                                                             






                                                                                      
                            

                                
                                               
                                                                    
                                                                    


                                                 


                                                                                      


                                                                               

                                                                                                     


                                                                                               

                                     

                                                            

                                                                                                                       








                                                                                                       

                                                                                                   
                                                                                    


                                                                                                                                                            
   


                                                                              
                                
                                                                      
                          
     
                                                                    




                                                      
                                                                   
                                           


                                                                      
                                                         






                                                                        
   
                 


                                                                               



                                                                                                           

                                                                                                   
                            
                                                       
 
 
                                                 
                                                 


                                         
                                                                                                        







                         
                                           
                                                 

                                          
                                                                                                        






                         
 
                                                           
                                                 


                                         
                                                                                                        






                         
 
                                            
                                                 

                                              
                                                                                                        











                                                    
 


















                                                                                                                                 
                                                              
 

                                                                              


                                                                       
                                    

                                               
                                    
                                                                                         
                              
                                           
                             


                                                               
     



                                                                                        
     

                                                               
     



                                                                                        
     













                                                                                        




                     
                                                                       






















                                                                                             
                      


                                                              






                                                                                









                                                                                                                 

                                                                
                                                                                 

               

                                                                                              
                           

                                                                                                 
                            

                                                                                          
     


                                                                                                               

                                                                                       













                                                                                                       
                          





                                                                                  
                               


                                                                                       

                                                                                                       
           
     




                                                                                           


                                                                                                 


                                                                                           
     



                                                                                                  
     














                                                                                                                       
                                                                                          

                                                                                                      
   























                                                                                                        

 


                                                                       
                                  


                                                                                             


                                                                       
                                

                                                                          

                                                           
         

                                                        




                                                                                                    

                                                              





                                                                       

                                      

                                                                                                                 
                                                                            


                                                                                                    




                                                                
                                                                                     
                                                        
         
   




















                                                                                                         
                               
                                                                                         

 














                                                                                                                 

                                                                            

 

                                                                             


















                                                                                  


                     

                                                  






                                                                                                                                
                   

                                     
       
                                                                     
   

                                     







                                                  

                                                                                                                                
                  
                                 

       
                                                                     












                                                                     

                                                   

                                                                                                                                
                  
                                                      

       











                                                                     


                                                                                                                                
                  
                                                          

       











                                                                     


                                                                                                                                
                  
                                 

       









                                                                     

                                                     


                                                                                                                               




                                   













                                                                     

                                                      

                                                                                                                               



                  







                                                                     
                                              
                                                  

                                                                                                                                





                   







                                                                     

                                                  

                                                                                                                                




                                         









                                                                     

                                                                                                                                




                                     









                                                                     


                                                                                                                                




                                     








                                                                     

                                                  


                                                                                                                                




                                                           








                                                                     

























                                                                                                                               

                                                 


                                                                                                                               



                  

                                                                     
               
                         


           
                        





                                                                     
            
                         





             

                                                         

                                                                                                                               



                  







                                                                     















































                                                                                                                               

                                                           

                                                                                                                                




                  








                                                                     



















                                                                                                                               




























                                                                                                                                




















                                                                                                                               








































                                                                                                                               

                                                  

                                                                                                                                




                              







                                                                     

                                                            


                                                                                                                                







                                          








                                                                     























                                                                                                                               




                                                                                                                               




















                                                                                                                               












                                                                     

                                                                           


                                                                                                                                





                                          








                                                                     

                                                  

                                                                                                                                





                             







                                                                     

                                                                                



                                                                                                                                






                                                                            










                                                                                  






                                                                                                                                





                             











                                                                                  



                                                                                                                                






                                                   











                                                                                  




                                                                                                                                






                                          









                                                                     

























                                                                                                                               
















































































                                                                                                                                
























                                                                                                                                              
                                                  




























                                                                                                                                              









                                                                      

                                                      
       


                                                                                    


                                                                                                                                 


                                                                                                                                                
                           








                         


































                                                                                                                                              





















                                                                                            
                                     
 






                                                                       





                                                       
              







                                                                                        
                              
                                                     









                                                                       





                                                       
                                                  


                                                           
                                                                         


                                       







                                                                       





                                                       


                                                            
                                                    
                                                                       


                                       






                                                                       





                                                       
                                                     
                                                                       






                                                                       





                                                       
                                                      
                                                                      






                                                                       





                                                       
                                                        
                                                                     






                                                                       





                                                       
                                                         
                                                                    
 
# Editor widget: takes a string and screen coordinates, modifying them in place.

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

scenario editor-initially-prints-string-to-screen [
  assume-screen 10:literal/width, 5:literal/height
  run [
    1:address:array:character <- new [abc]
    new-editor 1:address:array:character, screen:address, 0:literal/top, 0:literal/left, 10:literal/right
  ]
  screen-should-contain [
    .abc       .
    .          .
  ]
]

## In which we introduce the editor data structure, and show how it displays
## text to the screen.

container editor-data [
  # doubly linked list of characters (head contains a special sentinel)
  data:address:duplex-list
  # location of top-left of screen inside data (scrolling)
  top-of-screen:address:duplex-list
  # location before cursor inside data
  before-cursor:address:duplex-list

  screen:address:screen
  # raw bounds of display area on screen
  top:number
  left:number
  bottom:number
  right:number
  # raw screen coordinates of cursor
  cursor-row:number
  cursor-column:number

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

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

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

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

scenario editor-initially-prints-multiple-lines [
  assume-screen 5:literal/width, 3:literal/height
  run [
    s:address:array:character <- new [abc
def]
    new-editor s:address:array:character, screen:address, 0:literal/top, 0:literal/left, 5:literal/right
  ]
  screen-should-contain [
    .abc  .
    .def  .
    .     .
  ]
]

scenario editor-initially-handles-offsets [
  assume-screen 5:literal/width, 3:literal/height
  run [
    s:address:array:character <- new [abc]
    new-editor s:address:array:character, screen:address, 0:literal/top, 1:literal/left, 5:literal/right
  ]
  screen-should-contain [
    . abc .
    .     .
    .     .
  ]
]

scenario editor-initially-prints-multiple-lines-at-offset [
  assume-screen 5:literal/width, 3:literal/height
  run [
    s:address:array:character <- new [abc
def]
    new-editor s:address:array:character, screen:address, 0:literal/top, 1:literal/left, 5:literal/right
  ]
  screen-should-contain [
    . abc .
    . def .
    .     .
  ]
]

scenario editor-initially-wraps-long-lines [
  assume-screen 5:literal/width, 3:literal/height
  run [
    s:address:array:character <- new [abc def]
    new-editor s:address:array:character, screen:address, 0:literal/top, 0:literal/left, 5:literal/right
  ]
  screen-should-contain [
    .abc .
    .def  .
    .     .
  ]
  screen-should-contain-in-color, 245:literal/grey [
    .    .
    .     .
    .     .
  ]
]

scenario editor-initializes-empty-text [
  assume-screen 5:literal/width, 3:literal/height
  run [
    1:address:array:character <- new []
    2:address:editor-data <- new-editor 1:address:array:character, screen:address, 0:literal/top, 0:literal/left, 5:literal/right
    3:number <- get 2:address:editor-data/deref, cursor-row:offset
    4:number <- get 2:address:editor-data/deref, cursor-column:offset
  ]
  screen-should-contain [
    .     .
    .     .
    .     .
  ]
  memory-should-contain [
    3 <- 0  # cursor row
    4 <- 0  # cursor column
  ]
]

## handling events from the keyboard, mouse, touch screen, ...

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

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

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

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

recipe delete-before-cursor [
  default-space:address:array:location <- new location:type, 30:literal
  editor:address:editor-data <- next-ingredient
  before-cursor:address:address:duplex-list <- get-address editor:address:editor-data/deref, before-cursor:offset
  d:address:duplex-list <- get editor:address:editor-data/deref, data:offset
  # unless already at start
  at-start?:boolean <- equal before-cursor:address:address:duplex-list/deref, d:address:duplex-list
  reply-if at-start?:boolean
  # delete character
  prev:address:duplex-list <- prev-duplex before-cursor:address:address:duplex-list/deref
  remove-duplex before-cursor:address:address:duplex-list/deref
  # update cursor
  before-cursor:address:address:duplex-list/deref <- copy prev:address:duplex-list
  cursor-column:address:number <- get-address editor:address:editor-data/deref, cursor-column:offset
  cursor-column:address:number/deref <- subtract cursor-column:address:number/deref, 1:literal
#?   $print [delete-before-cursor: ], cursor-column:address:number/deref, [ 
#? ] #? 1
]

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

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

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

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

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

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

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

scenario editor-inserts-characters-into-empty-editor [
  assume-screen 10:literal/width, 5:literal/height
  1:address:array:character <- new []
  2:address:editor-data <- new-editor 1:address:array:character, screen:address, 0:literal/top, 0:literal/left, 5:literal/right
  assume-console [
    type [abc]
  ]
  run [
    event-loop screen:address, console:address, 2:address:editor-data
  ]
  screen-should-contain [
    .abc       .
    .          .
  ]
]

scenario editor-inserts-characters-at-cursor [
  assume-screen 10:literal/width, 5:literal/height
  1:address:array:character <- new [abc]
  2:address:editor-data <- new-editor 1:address:array:character, screen:address, 0:literal/top, 0:literal/left, 10:literal/right
  assume-console [
    type [0]
    left-click 0, 2
    type [d]
  ]
  run [
    event-loop screen:address, console:address, 2:address:editor-data
  ]
  screen-should-contain [
    .0adbc     .
    .          .
  ]
]

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

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

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

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

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

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

scenario editor-moves-cursor-after-inserting-characters [
  assume-screen 10:literal/width, 5:literal/height
  1:address:array:character <- new [abc]
  2:address:editor-data <- new-editor 1:address:array:character, screen:address, 0:literal/top, 0:literal/left, 5:literal/right
  assume-console [
    type [01]
  ]
  run [
    event-loop screen:address, console:address, 2:address:editor-data
  ]
  screen-should-contain [
    .01abc     .
    .          .
  ]
]

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

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

scenario editor-moves-cursor-down-after-inserting-newline [
  assume-screen 10:literal/width, 5:literal/height
  1:address:array:character <- new [abc]
  2:address:editor-data <- new-editor 1:address:array:character, screen:address, 0:literal/top, 0:literal/left, 10:literal/right
  assume-console [
    type [0
1]
  ]
  run [
    event-loop screen:address, console:address, 2:address:editor-data
  ]
  screen-should-contain [
    .0         .
    .1abc      .
    .          .
  ]
]

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

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

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

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

scenario editor-moves-cursor-right-with-key [
  assume-screen 10:literal/width, 5:literal/height
  1:address:array:character <- new [abc]
  2:address:editor-data <- new-editor 1:address:array:character, screen:address, 0:literal/top, 0:literal/left, 10:literal/right
  assume-console [
    press 65514  # right arrow
    type [0]
  ]
  run [
    event-loop screen:address, console:address, 2:address:editor-data
  ]
  screen-should-contain [
    .a0bc      .
    .          .
  ]
]

scenario editor-moves-cursor-to-next-line-with-right-arrow [
  assume-screen 10:literal/width, 5:literal/height
  1:address:array:character <- new [abc
d]
  2:address:editor-data <- new-editor 1:address:array:character, screen:address, 0:literal/top, 0:literal/left, 10:literal/right
  assume-console [
    press 65514  # right arrow
    press 65514  # right arrow
    press 65514  # right arrow
    press 65514  # right arrow - next line
    type [0]
  ]
  run [
    event-loop screen:address, console:address, 2:address:editor-data
  ]
  screen-should-contain [
    .abc       .
    .0d        .
    .          .
  ]
]

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

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

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

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

scenario editor-moves-cursor-left-with-key [
  assume-screen 10:literal/width, 5:literal/height
  1:address:array:character <- new [abc]
  2:address:editor-data <- new-editor 1:address:array:character, screen:address, 0:literal/top, 0:literal/left, 10:literal/right
  assume-console [
    left-click 0, 2
    press 65515  # left arrow
    type [0]
  ]
  run [
    event-loop screen:address, console:address, 2:address:editor-data
  ]
  screen-should-contain [
    .a0bc      .
    .          .
  ]
]

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

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

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

scenario editor-moves-cursor-to-previous-line-with-left-arrow-at-start-of-line-4 [
  assume-screen 10:literal/width, 5:literal/height
  # initialize editor with text containing an empty line
  1:address:array:character <- new [abc

d]
  2:address:editor-data <- new-editor 1:address:array:character, screen:address, 0:literal/top, 0:literal/left, 10:literal/right
  # position cursor right after empty line
  assume-console [
    left-click 2, 0
    press 65515  # left arrow
    type [0]
  ]
  run [
    event-loop screen:address, console:address, 2:address:editor-data
  ]
  screen-should-contain [
    .abc       .
    .0         .
    .d         .
    .          .
  ]
]

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

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

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

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

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

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

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

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

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

# set focus to first editor, reset it in later ones
recipe reset-focus [
  default-space:address:array:location <- new location:type, 30:literal
  editor:address:editor-data <- next-ingredient
  in-focus:address:boolean <- get-address editor:address:editor-data/deref, in-focus?:offset
  in-focus:address:boolean/deref <- copy 1:literal/true
  e:address:editor-data <- get editor:address:editor-data/deref, next-editor:offset
  {
    break-unless e:address:editor-data
#?     $print [resetting focus in ], e:address:editor-data, [ 
#? ] #? 1
    x:address:boolean <- get-address e:address:editor-data/deref, in-focus?:offset
#?     $print [ at ], x:address:boolean, [ 
#? ] #? 1
    x:address:boolean/deref <- copy 0:literal/false
    e:address:editor-data <- get e:address:editor-data/deref, next-editor:offset
    loop
  }
]

## helpers for drawing editor borders

recipe draw-box [
  default-space:address:array:location <- new location:type, 30:literal
  screen:address <- next-ingredient
  top:number <- next-ingredient
  left:number <- next-ingredient
  bottom:number <- next-ingredient
  right:number <- next-ingredient
  color:number, color-found?:boolean <- next-ingredient
  {
    # default color to white
    break-if color-found?:boolean
    color:number <- copy 245:literal/grey
  }
  # top border
  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
  # position cursor inside box
  move-cursor screen:address, top:number, left:number
  cursor-down screen:address
  cursor-right screen:address
]

recipe draw-horizontal [
  default-space:address:array:location <- new location:type, 30:literal
  screen:address <- next-ingredient
  row:number <- next-ingredient
  x:number <- next-ingredient
  right:number <- next-ingredient
  color:number, color-found?:boolean <- next-ingredient
  {
    # default color to white
    break-if color-found?:boolean
    color:number <- copy 245:literal/grey
  }
  move-cursor screen:address, row:number, x:number
  {
    continue?:boolean <- lesser-than x:number, right:number
    break-unless continue?:boolean
    print-character screen:address, 9472:literal/horizontal, color:number
    x:number <- add x:number, 1:literal
    loop
  }
]

recipe draw-vertical [
  default-space:address:array:location <- new location:type, 30:literal
  screen:address <- next-ingredient
  col:number <- next-ingredient
  x:number <- next-ingredient
  bottom:number <- next-ingredient
  color:number, color-found?:boolean <- next-ingredient
  {
    # default color to white
    break-if color-found?:boolean
    color:number <- copy 245:literal/grey
  }
  {
    continue?:boolean <- lesser-than x:number, bottom:number
    break-unless continue?:boolean
    move-cursor screen:address, x:number, col:number
    print-character screen:address, 9474:literal/vertical, color:number
    x:number <- add x:number, 1:literal
    loop
  }
]

recipe draw-top-left [
  default-space:address:array:location <- new location:type, 30:literal
  screen:address <- next-ingredient
  top:number <- next-ingredient
  left:number <- next-ingredient
  color:number, color-found?:boolean <- next-ingredient
  {
    # default color to white
    break-if color-found?:boolean
    color:number <- copy 245:literal/grey
  }
  move-cursor screen:address, top:number, left:number
  print-character screen:address, 9484:literal/down-right, color:number
]

recipe draw-top-right [
  default-space:address:array:location <- new location:type, 30:literal
  screen:address <- next-ingredient
  top:number <- next-ingredient
  right:number <- next-ingredient
  color:number, color-found?:boolean <- next-ingredient
  {
    # default color to white
    break-if color-found?:boolean
    color:number <- copy 245:literal/grey
  }
  move-cursor screen:address, top:number, right:number
  print-character screen:address, 9488:literal/down-left, color:number
]

recipe draw-bottom-left [
  default-space:address:array:location <- new location:type, 30:literal
  screen:address <- next-ingredient
  bottom:number <- next-ingredient
  left:number <- next-ingredient
  color:number, color-found?:boolean <- next-ingredient
  {
    # default color to white
    break-if color-found?:boolean
    color:number <- copy 245:literal/grey
  }
  move-cursor screen:address, bottom:number, left:number
  print-character screen:address, 9492:literal/up-right, color:number
]

recipe draw-bottom-right [
  default-space:address:array:location <- new location:type, 30:literal
  screen:address <- next-ingredient
  bottom:number <- next-ingredient
  right:number <- next-ingredient
  color:number, color-found?:boolean <- next-ingredient
  {
    # default color to white
    break-if color-found?:boolean
    color:number <- copy 245:literal/grey
  }
  move-cursor screen:address, bottom:number, right:number
  print-character screen:address, 9496:literal/up-left, color:number
]