about summary refs log tree commit diff stats
path: root/html/edit/003-shortcuts.mu.html
blob: 023f8448706e03a113dd83432be472e218456820 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
generated by cgit-pink 1.4.1-2-gfad0 (git 2.36.2.497.gbbea4dcf42) at 2024-11-28 03:36:29 +0000
 


' href='#n2685'>2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>Mu - edit/003-shortcuts.mu</title>
<meta name="Generator" content="Vim/7.4">
<meta name="plugin-version" content="vim7.4_v1">
<meta name="syntax" content="none">
<meta name="settings" content="use_css,pre_wrap,no_foldcolumn,expand_tabs,prevent_copy=">
<meta name="colorscheme" content="minimal">
<style type="text/css">
<!--
pre { white-space: pre-wrap; font-family: monospace; color: #eeeeee; background-color: #080808; }
body { font-family: monospace; color: #eeeeee; background-color: #080808; }
* { font-size: 1.05em; }
.muControl { color: #c0a020; }
.muRecipe { color: #ff8700; }
.muScenario { color: #00af00; }
.Special { color: #ff6060; }
.Comment { color: #9090ff; }
.Constant { color: #00a0a0; }
.SalientComment { color: #00ffff; }
.Delimiter { color: #a04060; }
-->
</style>

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

-->
</script>
</head>
<body>
<pre id='vimCodeElement'>
<span class="SalientComment">## special shortcuts for manipulating the editor</span>
<span class="Comment"># Some keys on the keyboard generate unicode characters, others generate</span>
<span class="Comment"># terminfo key codes. We need to modify different places in the two cases.</span>

<span class="Comment"># tab - insert two spaces</span>

<span class="muScenario">scenario</span> editor-inserts-two-spaces-on-tab [
  assume-screen <span class="Constant">10/width</span>, <span class="Constant">5/height</span>
  <span class="Comment"># just one character in final line</span>
  <span class="Constant">1</span>:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[ab</span>
<span class="Constant">cd]</span>
  <span class="Constant">2</span>:address:editor-data<span class="Special"> &lt;- </span>new-editor <span class="Constant">1</span>:address:array:character, screen:address:screen, <span class="Constant">0/left</span>, <span class="Constant">5/right</span>
  assume-console [
    press tab
  ]
  run [
    editor-event-loop screen:address:screen, console:address:console, <span class="Constant">2</span>:address:editor-data
  ]
  screen-should-contain [
   <span class="Constant"> .          .</span>
   <span class="Constant"> .  ab      .</span>
   <span class="Constant"> .cd        .</span>
  ]
]

<span class="muRecipe">after</span> <span class="Constant">&lt;handle-special-character&gt;</span> [
  <span class="Delimiter">{</span>
    tab?:boolean<span class="Special"> &lt;- </span>equal *c, <span class="Constant">9/tab</span>
    <span class="muControl">break-unless</span> tab?
<span class="Constant">    &lt;insert-character-begin&gt;</span>
    editor, screen, go-render?:boolean<span class="Special"> &lt;- </span>insert-at-cursor editor, <span class="Constant">32/space</span>, screen
    editor, screen, go-render?:boolean<span class="Special"> &lt;- </span>insert-at-cursor editor, <span class="Constant">32/space</span>, screen
<span class="Constant">    &lt;insert-character-end&gt;</span>
    go-render?<span class="Special"> &lt;- </span>copy <span class="Constant">1/true</span>
    <span class="muControl">reply</span>
  <span class="Delimiter">}</span>
]

<span class="Comment"># backspace - delete character before cursor</span>

<span class="muScenario">scenario</span> editor-handles-backspace-key [
  assume-screen <span class="Constant">10/width</span>, <span class="Constant">5/height</span>
  <span class="Constant">1</span>:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[abc]</span>
  <span class="Constant">2</span>:address:editor-data<span class="Special"> &lt;- </span>new-editor <span class="Constant">1</span>:address:array:character, screen:address:screen, <span class="Constant">0/left</span>, <span class="Constant">10/right</span>
  editor-render screen, <span class="Constant">2</span>:address:editor-data
<span class="Constant">  $clear-trace</span>
  assume-console [
    left-click <span class="Constant">1</span>, <span class="Constant">1</span>
    press backspace
  ]
  run [
    editor-event-loop screen:address:screen, console:address:console, <span class="Constant">2</span>:address:editor-data
    <span class="Constant">4</span>:number<span class="Special"> &lt;- </span>get *<span class="Constant">2</span>:address:editor-data, <span class="Constant">cursor-row:offset</span>
    <span class="Constant">5</span>:number<span class="Special"> &lt;- </span>get *<span class="Constant">2</span>:address:editor-data, <span class="Constant">cursor-column:offset</span>
  ]
  screen-should-contain [
   <span class="Constant"> .          .</span>
   <span class="Constant"> .bc        .</span>
<span class="Constant">    .┈┈┈┈┈┈┈┈┈┈.</span>
   <span class="Constant"> .          .</span>
  ]
  memory-should-contain [
    <span class="Constant">4</span><span class="Special"> &lt;- </span><span class="Constant">1</span>
    <span class="Constant">5</span><span class="Special"> &lt;- </span><span class="Constant">0</span>
  ]
  check-trace-count-for-label <span class="Constant">3</span>, <span class="Constant">[print-character]</span>  <span class="Comment"># length of original line to overwrite</span>
]

<span class="muRecipe">after</span> <span class="Constant">&lt;handle-special-character&gt;</span> [
  <span class="Delimiter">{</span>
    delete-previous-character?:boolean<span class="Special"> &lt;- </span>equal *c, <span class="Constant">8/backspace</span>
    <span class="muControl">break-unless</span> delete-previous-character?
<span class="Constant">    &lt;backspace-character-begin&gt;</span>
    editor, screen, go-render?:boolean, backspaced-cell:address:duplex-list:character<span class="Special"> &lt;- </span>delete-before-cursor editor, screen
<span class="Constant">    &lt;backspace-character-end&gt;</span>
    <span class="muControl">reply</span>
  <span class="Delimiter">}</span>
]

<span class="Comment"># return values:</span>
<span class="Comment">#   go-render? - whether caller needs to update the screen</span>
<span class="Comment">#   backspaced-cell - value deleted (or 0 if nothing was deleted) so we can save it for undo, etc.</span>
<span class="muRecipe">recipe</span> delete-before-cursor editor:address:editor-data, screen:address:screen<span class="muRecipe"> -&gt; </span>editor:address:editor-data, screen:address:screen, go-render?:boolean, backspaced-cell:address:duplex-list:character [
  <span class="Constant">local-scope</span>
  <span class="Constant">load-ingredients</span>
  before-cursor:address:address:duplex-list:character<span class="Special"> &lt;- </span>get-address *editor, <span class="Constant">before-cursor:offset</span>
  <span class="Comment"># if at start of text (before-cursor at § sentinel), return</span>
  prev:address:duplex-list:character<span class="Special"> &lt;- </span>prev-duplex *before-cursor
  go-render?, backspaced-cell<span class="Special"> &lt;- </span>copy <span class="Constant">0/no-more-render</span>, <span class="Constant">0/nothing-deleted</span>
  <span class="muControl">reply-unless</span> prev
  trace <span class="Constant">10</span>, <span class="Constant">[app]</span>, <span class="Constant">[delete-before-cursor]</span>
  original-row:number<span class="Special"> &lt;- </span>get *editor, <span class="Constant">cursor-row:offset</span>
  editor, scroll?:boolean<span class="Special"> &lt;- </span>move-cursor-coordinates-left editor
  backspaced-cell:address:duplex-list:character<span class="Special"> &lt;- </span>copy *before-cursor
  remove-duplex *before-cursor  <span class="Comment"># will also neatly trim next/prev pointers in backspaced-cell/*before-cursor</span>
  *before-cursor<span class="Special"> &lt;- </span>copy prev
  go-render?<span class="Special"> &lt;- </span>copy <span class="Constant">1/true</span>
  <span class="muControl">reply-if</span> scroll?
  screen-width:number<span class="Special"> &lt;- </span>screen-width screen
  cursor-row:number<span class="Special"> &lt;- </span>get *editor, <span class="Constant">cursor-row:offset</span>
  cursor-column:number<span class="Special"> &lt;- </span>get *editor, <span class="Constant">cursor-column:offset</span>
  <span class="Comment"># did we just backspace over a newline?</span>
  same-row?:boolean<span class="Special"> &lt;- </span>equal cursor-row, original-row
  go-render?<span class="Special"> &lt;- </span>copy <span class="Constant">1/true</span>
  <span class="muControl">reply-unless</span> same-row?
  left:number<span class="Special"> &lt;- </span>get *editor, <span class="Constant">left:offset</span>
  right:number<span class="Special"> &lt;- </span>get *editor, <span class="Constant">right:offset</span>
  curr:address:duplex-list:character<span class="Special"> &lt;- </span>next-duplex *before-cursor
  screen<span class="Special"> &lt;- </span>move-cursor screen, cursor-row, cursor-column
  curr-column:number<span class="Special"> &lt;- </span>copy cursor-column
  <span class="Delimiter">{</span>
    <span class="Comment"># hit right margin? give up and let caller render</span>
    at-right?:boolean<span class="Special"> &lt;- </span>greater-or-equal curr-column, right
    go-render?<span class="Special"> &lt;- </span>copy <span class="Constant">1/true</span>
    <span class="muControl">reply-if</span> at-right?
    <span class="muControl">break-unless</span> curr
    <span class="Comment"># newline? done.</span>
    currc:character<span class="Special"> &lt;- </span>get *curr, <span class="Constant">value:offset</span>
    at-newline?:boolean<span class="Special"> &lt;- </span>equal currc, <span class="Constant">10/newline</span>
    <span class="muControl">break-if</span> at-newline?
    screen<span class="Special"> &lt;- </span>print-character screen, currc
    curr-column<span class="Special"> &lt;- </span>add curr-column, <span class="Constant">1</span>
    curr<span class="Special"> &lt;- </span>next-duplex curr
    <span class="muControl">loop</span>
  <span class="Delimiter">}</span>
  <span class="Comment"># we're guaranteed not to be at the right margin</span>
  screen<span class="Special"> &lt;- </span>print-character screen, <span class="Constant">32/space</span>
  go-render?<span class="Special"> &lt;- </span>copy <span class="Constant">0/false</span>
]

<span class="muRecipe">recipe</span> move-cursor-coordinates-left editor:address:editor-data<span class="muRecipe"> -&gt; </span>editor:address:editor-data, go-render?:boolean [
  <span class="Constant">local-scope</span>
  <span class="Constant">load-ingredients</span>
  before-cursor:address:duplex-list:character<span class="Special"> &lt;- </span>get *editor, <span class="Constant">before-cursor:offset</span>
  cursor-row:address:number<span class="Special"> &lt;- </span>get-address *editor, <span class="Constant">cursor-row:offset</span>
  cursor-column:address:number<span class="Special"> &lt;- </span>get-address *editor, <span class="Constant">cursor-column:offset</span>
  left:number<span class="Special"> &lt;- </span>get *editor, <span class="Constant">left:offset</span>
  <span class="Comment"># if not at left margin, move one character left</span>
  <span class="Delimiter">{</span>
    at-left-margin?:boolean<span class="Special"> &lt;- </span>equal *cursor-column, left
    <span class="muControl">break-if</span> at-left-margin?
    trace <span class="Constant">10</span>, <span class="Constant">[app]</span>, <span class="Constant">[decrementing cursor column]</span>
    *cursor-column<span class="Special"> &lt;- </span>subtract *cursor-column, <span class="Constant">1</span>
    go-render?<span class="Special"> &lt;- </span>copy <span class="Constant">0/false</span>
    <span class="muControl">reply</span>
  <span class="Delimiter">}</span>
  <span class="Comment"># if at left margin, we must move to previous row:</span>
  top-of-screen?:boolean<span class="Special"> &lt;- </span>equal *cursor-row, <span class="Constant">1</span>  <span class="Comment"># exclude menu bar</span>
  go-render?:boolean<span class="Special"> &lt;- </span>copy <span class="Constant">0/false</span>
  <span class="Delimiter">{</span>
    <span class="muControl">break-if</span> top-of-screen?
    *cursor-row<span class="Special"> &lt;- </span>subtract *cursor-row, <span class="Constant">1</span>
  <span class="Delimiter">}</span>
  <span class="Delimiter">{</span>
    <span class="muControl">break-unless</span> top-of-screen?
<span class="Constant">    &lt;scroll-up&gt;</span>
    go-render?<span class="Special"> &lt;- </span>copy <span class="Constant">1/true</span>
  <span class="Delimiter">}</span>
  <span class="Delimiter">{</span>
    <span class="Comment"># case 1: if previous character was newline, figure out how long the previous line is</span>
    previous-character:character<span class="Special"> &lt;- </span>get *before-cursor, <span class="Constant">value:offset</span>
    previous-character-is-newline?:boolean<span class="Special"> &lt;- </span>equal previous-character, <span class="Constant">10/newline</span>
    <span class="muControl">break-unless</span> previous-character-is-newline?
    <span class="Comment"># compute length of previous line</span>
    trace <span class="Constant">10</span>, <span class="Constant">[app]</span>, <span class="Constant">[switching to previous line]</span>
    d:address:duplex-list:character<span class="Special"> &lt;- </span>get *editor, <span class="Constant">data:offset</span>
    end-of-line:number<span class="Special"> &lt;- </span>previous-line-length before-cursor, d
    *cursor-column<span class="Special"> &lt;- </span>add left, end-of-line
    <span class="muControl">reply</span>
  <span class="Delimiter">}</span>
  <span class="Comment"># case 2: if previous-character was not newline, we're just at a wrapped line</span>
  trace <span class="Constant">10</span>, <span class="Constant">[app]</span>, <span class="Constant">[wrapping to previous line]</span>
  right:number<span class="Special"> &lt;- </span>get *editor, <span class="Constant">right:offset</span>
  *cursor-column<span class="Special"> &lt;- </span>subtract right, <span class="Constant">1</span>  <span class="Comment"># leave room for wrap icon</span>
]

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

<span class="muScenario">scenario</span> editor-clears-last-line-on-backspace [
  assume-screen <span class="Constant">10/width</span>, <span class="Constant">5/height</span>
  <span class="Comment"># just one character in final line</span>
  <span class="Constant">1</span>:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[ab</span>
<span class="Constant">cd]</span>
  <span class="Constant">2</span>:address:editor-data<span class="Special"> &lt;- </span>new-editor <span class="Constant">1</span>:address:array:character, screen:address:screen, <span class="Constant">0/left</span>, <span class="Constant">10/right</span>
  assume-console [
    left-click <span class="Constant">2</span>, <span class="Constant">0</span>  <span class="Comment"># cursor at only character in final line</span>
    press backspace
  ]
  run [
    editor-event-loop screen:address:screen, console:address:console, <span class="Constant">2</span>:address:editor-data
    <span class="Constant">4</span>:number<span class="Special"> &lt;- </span>get *<span class="Constant">2</span>:address:editor-data, <span class="Constant">cursor-row:offset</span>
    <span class="Constant">5</span>:number<span class="Special"> &lt;- </span>get *<span class="Constant">2</span>:address:editor-data, <span class="Constant">cursor-column:offset</span>
  ]
  screen-should-contain [
   <span class="Constant"> .          .</span>
   <span class="Constant"> .abcd      .</span>
<span class="Constant">    .┈┈┈┈┈┈┈┈┈┈.</span>
   <span class="Constant"> .          .</span>
  ]
  memory-should-contain [
    <span class="Constant">4</span><span class="Special"> &lt;- </span><span class="Constant">1</span>
    <span class="Constant">5</span><span class="Special"> &lt;- </span><span class="Constant">2</span>
  ]
]

<span class="muScenario">scenario</span> editor-joins-and-wraps-lines-on-backspace [
  assume-screen <span class="Constant">10/width</span>, <span class="Constant">5/height</span>
  <span class="Comment"># initialize editor with two long-ish but non-wrapping lines</span>
  <span class="Constant">1</span>:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[abc def</span>
<span class="Constant">ghi jkl]</span>
  <span class="Constant">2</span>:address:editor-data<span class="Special"> &lt;- </span>new-editor <span class="Constant">1</span>:address:array:character, screen:address:screen, <span class="Constant">0/left</span>, <span class="Constant">10/right</span>
  editor-render screen, <span class="Constant">2</span>:address:editor-data
<span class="Constant">  $clear-trace</span>
  <span class="Comment"># position the cursor at the start of the second and hit backspace</span>
  assume-console [
    left-click <span class="Constant">2</span>, <span class="Constant">0</span>
    press backspace
  ]
  run [
    editor-event-loop screen:address:screen, console:address:console, <span class="Constant">2</span>:address:editor-data
  ]
  <span class="Comment"># resulting single line should wrap correctly</span>
  screen-should-contain [
   <span class="Constant"> .          .</span>
   <span class="Constant"> .abc defgh↩.</span>
   <span class="Constant"> .i jkl     .</span>
<span class="Constant">    .┈┈┈┈┈┈┈┈┈┈.</span>
   <span class="Constant"> .          .</span>
  ]
]

<span class="muScenario">scenario</span> editor-wraps-long-lines-on-backspace [
  assume-screen <span class="Constant">10/width</span>, <span class="Constant">5/height</span>
  <span class="Comment"># initialize editor in part of the screen with a long line</span>
  <span class="Constant">1</span>:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[abc def ghij]</span>
  <span class="Constant">2</span>:address:editor-data<span class="Special"> &lt;- </span>new-editor <span class="Constant">1</span>:address:array:character, screen:address:screen, <span class="Constant">0/left</span>, <span class="Constant">8/right</span>
  editor-render screen, <span class="Constant">2</span>:address:editor-data
  <span class="Comment"># confirm that it wraps</span>
  screen-should-contain [
   <span class="Constant"> .          .</span>
   <span class="Constant"> .abc def↩  .</span>
   <span class="Constant"> . ghij     .</span>
   <span class="Constant"> .┈┈┈┈┈┈┈┈  .</span>
  ]
<span class="Constant">  $clear-trace</span>
  <span class="Comment"># position the cursor somewhere in the middle of the top screen line and hit backspace</span>
  assume-console [
    left-click <span class="Constant">1</span>, <span class="Constant">4</span>
    press backspace
  ]
  run [
    editor-event-loop screen:address:screen, console:address:console, <span class="Constant">2</span>:address:editor-data
  ]
  <span class="Comment"># resulting single line should wrap correctly and not overflow its bounds</span>
  screen-should-contain [
   <span class="Constant"> .          .</span>
   <span class="Constant"> .abcdef ↩  .</span>
   <span class="Constant"> .ghij      .</span>
   <span class="Constant"> .┈┈┈┈┈┈┈┈  .</span>
   <span class="Constant"> .          .</span>
  ]
]

<span class="Comment"># delete - delete character at cursor</span>

<span class="muScenario">scenario</span> editor-handles-delete-key [
  assume-screen <span class="Constant">10/width</span>, <span class="Constant">5/height</span>
  <span class="Constant">1</span>:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[abc]</span>
  <span class="Constant">2</span>:address:editor-data<span class="Special"> &lt;- </span>new-editor <span class="Constant">1</span>:address:array:character, screen:address:screen, <span class="Constant">0/left</span>, <span class="Constant">10/right</span>
  editor-render screen, <span class="Constant">2</span>:address:editor-data
<span class="Constant">  $clear-trace</span>
  assume-console [
    press delete
  ]
  run [
    editor-event-loop screen:address:screen, console:address:console, <span class="Constant">2</span>:address:editor-data
  ]
  screen-should-contain [
   <span class="Constant"> .          .</span>
   <span class="Constant"> .bc        .</span>
<span class="Constant">    .┈┈┈┈┈┈┈┈┈┈.</span>
   <span class="Constant"> .          .</span>
  ]
  check-trace-count-for-label <span class="Constant">3</span>, <span class="Constant">[print-character]</span>  <span class="Comment"># length of original line to overwrite</span>
<span class="Constant">  $clear-trace</span>
  assume-console [
    press delete
  ]
  run [
    editor-event-loop screen:address:screen, console:address:console, <span class="Constant">2</span>:address:editor-data
  ]
  screen-should-contain [
   <span class="Constant"> .          .</span>
   <span class="Constant"> .c         .</span>
<span class="Constant">    .┈┈┈┈┈┈┈┈┈┈.</span>
   <span class="Constant"> .          .</span>
  ]
  check-trace-count-for-label <span class="Constant">2</span>, <span class="Constant">[print-character]</span>  <span class="Comment"># new length to overwrite</span>
]

<span class="muRecipe">after</span> <span class="Constant">&lt;handle-special-key&gt;</span> [
  <span class="Delimiter">{</span>
    delete-next-character?:boolean<span class="Special"> &lt;- </span>equal *k, <span class="Constant">65522/delete</span>
    <span class="muControl">break-unless</span> delete-next-character?
<span class="Constant">    &lt;delete-character-begin&gt;</span>
    editor, screen, go-render?:boolean, deleted-cell:address:duplex-list:character<span class="Special"> &lt;- </span>delete-at-cursor editor, screen
<span class="Constant">    &lt;delete-character-end&gt;</span>
    <span class="muControl">reply</span>
  <span class="Delimiter">}</span>
]

<span class="muRecipe">recipe</span> delete-at-cursor editor:address:editor-data, screen:address:screen<span class="muRecipe"> -&gt; </span>editor:address:editor-data, screen:address:screen, go-render?:boolean, deleted-cell:address:duplex-list:character [
  <span class="Constant">local-scope</span>
  <span class="Constant">load-ingredients</span>
  before-cursor:address:address:duplex-list:character<span class="Special"> &lt;- </span>get-address *editor, <span class="Constant">before-cursor:offset</span>
  deleted-cell:address:duplex-list:character<span class="Special"> &lt;- </span>next-duplex *before-cursor
  go-render?<span class="Special"> &lt;- </span>copy <span class="Constant">0/false</span>
  <span class="muControl">reply-unless</span> deleted-cell
  currc:character<span class="Special"> &lt;- </span>get *deleted-cell, <span class="Constant">value:offset</span>
  remove-duplex deleted-cell
  deleted-newline?:boolean<span class="Special"> &lt;- </span>equal currc, <span class="Constant">10/newline</span>
  go-render?<span class="Special"> &lt;- </span>copy <span class="Constant">1/true</span>
  <span class="muControl">reply-if</span> deleted-newline?
  <span class="Comment"># wasn't a newline? render rest of line</span>
  curr:address:duplex-list:character<span class="Special"> &lt;- </span>next-duplex *before-cursor  <span class="Comment"># refresh after remove-duplex above</span>
  cursor-row:address:number<span class="Special"> &lt;- </span>get-address *editor, <span class="Constant">cursor-row:offset</span>
  cursor-column:address:number<span class="Special"> &lt;- </span>get-address *editor, <span class="Constant">cursor-column:offset</span>
  screen<span class="Special"> &lt;- </span>move-cursor screen, *cursor-row, *cursor-column
  curr-column:number<span class="Special"> &lt;- </span>copy *cursor-column
  screen-width:number<span class="Special"> &lt;- </span>screen-width screen
  <span class="Delimiter">{</span>
    <span class="Comment"># hit right margin? give up and let caller render</span>
    at-right?:boolean<span class="Special"> &lt;- </span>greater-or-equal curr-column, screen-width
    go-render?<span class="Special"> &lt;- </span>copy <span class="Constant">1/true</span>
    <span class="muControl">reply-if</span> at-right?
    <span class="muControl">break-unless</span> curr
    <span class="Comment"># newline? done.</span>
    currc:character<span class="Special"> &lt;- </span>get *curr, <span class="Constant">value:offset</span>
    at-newline?:boolean<span class="Special"> &lt;- </span>equal currc, <span class="Constant">10/newline</span>
    <span class="muControl">break-if</span> at-newline?
    screen<span class="Special"> &lt;- </span>print-character screen, currc
    curr-column<span class="Special"> &lt;- </span>add curr-column, <span class="Constant">1</span>
    curr<span class="Special"> &lt;- </span>next-duplex curr
    <span class="muControl">loop</span>
  <span class="Delimiter">}</span>
  <span class="Comment"># we're guaranteed not to be at the right margin</span>
  screen<span class="Special"> &lt;- </span>print-character screen, <span class="Constant">32/space</span>
  go-render?<span class="Special"> &lt;- </span>copy <span class="Constant">0/false</span>
]

<span class="Comment"># right arrow</span>

<span class="muScenario">scenario</span> editor-moves-cursor-right-with-key [
  assume-screen <span class="Constant">10/width</span>, <span class="Constant">5/height</span>
  <span class="Constant">1</span>:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[abc]</span>
  <span class="Constant">2</span>:address:editor-data<span class="Special"> &lt;- </span>new-editor <span class="Constant">1</span>:address:array:character, screen:address:screen, <span class="Constant">0/left</span>, <span class="Constant">10/right</span>
  editor-render screen, <span class="Constant">2</span>:address:editor-data
<span class="Constant">  $clear-trace</span>
  assume-console [
    press right-arrow
    type <span class="Constant">[0]</span>
  ]
  run [
    editor-event-loop screen:address:screen, console:address:console, <span class="Constant">2</span>:address:editor-data
  ]
  screen-should-contain [
   <span class="Constant"> .          .</span>
   <span class="Constant"> .a0bc      .</span>
<span class="Constant">    .┈┈┈┈┈┈┈┈┈┈.</span>
   <span class="Constant"> .          .</span>
  ]
  check-trace-count-for-label <span class="Constant">3</span>, <span class="Constant">[print-character]</span>  <span class="Comment"># 0 and following characters</span>
]

<span class="muRecipe">after</span> <span class="Constant">&lt;handle-special-key&gt;</span> [
  <span class="Delimiter">{</span>
    move-to-next-character?:boolean<span class="Special"> &lt;- </span>equal *k, <span class="Constant">65514/right-arrow</span>
    <span class="muControl">break-unless</span> move-to-next-character?
    <span class="Comment"># if not at end of text</span>
    next-cursor:address:duplex-list:character<span class="Special"> &lt;- </span>next-duplex *before-cursor
    <span class="muControl">break-unless</span> next-cursor
    <span class="Comment"># scan to next character</span>
<span class="Constant">    &lt;move-cursor-begin&gt;</span>
    *before-cursor<span class="Special"> &lt;- </span>copy next-cursor
    editor, go-render?:boolean<span class="Special"> &lt;- </span>move-cursor-coordinates-right editor, screen-height
    screen<span class="Special"> &lt;- </span>move-cursor screen, *cursor-row, *cursor-column
    undo-coalesce-tag:number<span class="Special"> &lt;- </span>copy <span class="Constant">2/right-arrow</span>
<span class="Constant">    &lt;move-cursor-end&gt;</span>
    <span class="muControl">reply</span>
  <span class="Delimiter">}</span>
]

<span class="muRecipe">recipe</span> move-cursor-coordinates-right editor:address:editor-data, screen-height:number<span class="muRecipe"> -&gt; </span>editor:address:editor-data, go-render?:boolean [
  <span class="Constant">local-scope</span>
  <span class="Constant">load-ingredients</span>
  before-cursor:address:duplex-list:character<span class="Special"> &lt;- </span>get *editor <span class="Constant">before-cursor:offset</span>
  cursor-row:address:number<span class="Special"> &lt;- </span>get-address *editor, <span class="Constant">cursor-row:offset</span>
  cursor-column:address:number<span class="Special"> &lt;- </span>get-address *editor, <span class="Constant">cursor-column:offset</span>
  left:number<span class="Special"> &lt;- </span>get *editor, <span class="Constant">left:offset</span>
  right:number<span class="Special"> &lt;- </span>get *editor, <span class="Constant">right:offset</span>
  <span class="Comment"># if crossed a newline, move cursor to start of next row</span>
  <span class="Delimiter">{</span>
    old-cursor-character:character<span class="Special"> &lt;- </span>get *before-cursor, <span class="Constant">value:offset</span>
    was-at-newline?:boolean<span class="Special"> &lt;- </span>equal old-cursor-character, <span class="Constant">10/newline</span>
    <span class="muControl">break-unless</span> was-at-newline?
    *cursor-row<span class="Special"> &lt;- </span>add *cursor-row, <span class="Constant">1</span>
    *cursor-column<span class="Special"> &lt;- </span>copy left
    below-screen?:boolean<span class="Special"> &lt;- </span>greater-or-equal *cursor-row, screen-height  <span class="Comment"># must be equal</span>
    go-render?<span class="Special"> &lt;- </span>copy <span class="Constant">0/false</span>
    <span class="muControl">reply-unless</span> below-screen?
<span class="Constant">    &lt;scroll-down&gt;</span>
    *cursor-row<span class="Special"> &lt;- </span>subtract *cursor-row, <span class="Constant">1</span>  <span class="Comment"># bring back into screen range</span>
    go-render?<span class="Special"> &lt;- </span>copy <span class="Constant">1/true</span>
    <span class="muControl">reply</span>
  <span class="Delimiter">}</span>
  <span class="Comment"># if the line wraps, move cursor to start of next row</span>
  <span class="Delimiter">{</span>
    <span class="Comment"># if we're at the column just before the wrap indicator</span>
    wrap-column:number<span class="Special"> &lt;- </span>subtract right, <span class="Constant">1</span>
    at-wrap?:boolean<span class="Special"> &lt;- </span>equal *cursor-column, wrap-column
    <span class="muControl">break-unless</span> at-wrap?
    <span class="Comment"># and if next character isn't newline</span>
    next:address:duplex-list:character<span class="Special"> &lt;- </span>next-duplex before-cursor
    <span class="muControl">break-unless</span> next
    next-character:character<span class="Special"> &lt;- </span>get *next, <span class="Constant">value:offset</span>
    newline?:boolean<span class="Special"> &lt;- </span>equal next-character, <span class="Constant">10/newline</span>
    <span class="muControl">break-if</span> newline?
    *cursor-row<span class="Special"> &lt;- </span>add *cursor-row, <span class="Constant">1</span>
    *cursor-column<span class="Special"> &lt;- </span>copy left
    below-screen?:boolean<span class="Special"> &lt;- </span>greater-or-equal *cursor-row, screen-height  <span class="Comment"># must be equal</span>
    <span class="muControl">reply-unless</span> below-screen?, editor/same-as-ingredient:<span class="Constant">0</span>, <span class="Constant">0/no-more-render</span>
<span class="Constant">    &lt;scroll-down&gt;</span>
    *cursor-row<span class="Special"> &lt;- </span>subtract *cursor-row, <span class="Constant">1</span>  <span class="Comment"># bring back into screen range</span>
    go-render?<span class="Special"> &lt;- </span>copy <span class="Constant">1/true</span>
    <span class="muControl">reply</span>
  <span class="Delimiter">}</span>
  <span class="Comment"># otherwise move cursor one character right</span>
  *cursor-column<span class="Special"> &lt;- </span>add *cursor-column, <span class="Constant">1</span>
  go-render?<span class="Special"> &lt;- </span>copy <span class="Constant">0/false</span>
]

<span class="muScenario">scenario</span> editor-moves-cursor-to-next-line-with-right-arrow [
  assume-screen <span class="Constant">10/width</span>, <span class="Constant">5/height</span>
  <span class="Constant">1</span>:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[abc</span>
<span class="Constant">d]</span>
  <span class="Constant">2</span>:address:editor-data<span class="Special"> &lt;- </span>new-editor <span class="Constant">1</span>:address:array:character, screen:address:screen, <span class="Constant">0/left</span>, <span class="Constant">10/right</span>
  editor-render screen, <span class="Constant">2</span>:address:editor-data
<span class="Constant">  $clear-trace</span>
  <span class="Comment"># type right-arrow a few times to get to start of second line</span>
  assume-console [
    press right-arrow
    press right-arrow
    press right-arrow
    press right-arrow  <span class="Comment"># next line</span>
  ]
  run [
    editor-event-loop screen:address:screen, console:address:console, <span class="Constant">2</span>:address:editor-data
  ]
  check-trace-count-for-label <span class="Constant">0</span>, <span class="Constant">[print-character]</span>
  <span class="Comment"># type something and ensure it goes where it should</span>
  assume-console [
    type <span class="Constant">[0]</span>
  ]
  run [
    editor-event-loop screen:address:screen, console:address:console, <span class="Constant">2</span>:address:editor-data
  ]
  screen-should-contain [
   <span class="Constant"> .          .</span>
   <span class="Constant"> .abc       .</span>
   <span class="Constant"> .0d        .</span>
<span class="Constant">    .┈┈┈┈┈┈┈┈┈┈.</span>
   <span class="Constant"> .          .</span>
  ]
  check-trace-count-for-label <span class="Constant">2</span>, <span class="Constant">[print-character]</span>  <span class="Comment"># new length of second line</span>
]

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

<span class="muScenario">scenario</span> editor-moves-cursor-to-next-wrapped-line-with-right-arrow [
  assume-screen <span class="Constant">10/width</span>, <span class="Constant">5/height</span>
  <span class="Constant">1</span>:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[abcdef]</span>
  <span class="Constant">2</span>:address:editor-data<span class="Special"> &lt;- </span>new-editor <span class="Constant">1</span>:address:array:character, screen:address:screen, <span class="Constant">0/left</span>, <span class="Constant">5/right</span>
  editor-render screen, <span class="Constant">2</span>:address:editor-data
<span class="Constant">  $clear-trace</span>
  assume-console [
    left-click <span class="Constant">1</span>, <span class="Constant">3</span>
    press right-arrow
  ]
  run [
    editor-event-loop screen:address:screen, console:address:console, <span class="Constant">2</span>:address:editor-data
    <span class="Constant">3</span>:number<span class="Special"> &lt;- </span>get *<span class="Constant">2</span>:address:editor-data, <span class="Constant">cursor-row:offset</span>
    <span class="Constant">4</span>:number<span class="Special"> &lt;- </span>get *<span class="Constant">2</span>:address:editor-data, <span class="Constant">cursor-column:offset</span>
  ]
  screen-should-contain [
   <span class="Constant"> .          .</span>
   <span class="Constant"> .abcd↩     .</span>
   <span class="Constant"> .ef        .</span>
   <span class="Constant"> .┈┈┈┈┈     .</span>
   <span class="Constant"> .          .</span>
  ]
  memory-should-contain [
    <span class="Constant">3</span><span class="Special"> &lt;- </span><span class="Constant">2</span>
    <span class="Constant">4</span><span class="Special"> &lt;- </span><span class="Constant">0</span>
  ]
  check-trace-count-for-label <span class="Constant">0</span>, <span class="Constant">[print-character]</span>
]

<span class="muScenario">scenario</span> editor-moves-cursor-to-next-wrapped-line-with-right-arrow-2 [
  assume-screen <span class="Constant">10/width</span>, <span class="Constant">5/height</span>
  <span class="Comment"># line just barely wrapping</span>
  <span class="Constant">1</span>:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[abcde]</span>
  <span class="Constant">2</span>:address:editor-data<span class="Special"> &lt;- </span>new-editor <span class="Constant">1</span>:address:array:character, screen:address:screen, <span class="Constant">0/left</span>, <span class="Constant">5/right</span>
  editor-render screen, <span class="Constant">2</span>:address:editor-data
<span class="Constant">  $clear-trace</span>
  <span class="Comment"># position cursor at last character before wrap and hit right-arrow</span>
  assume-console [
    left-click <span class="Constant">1</span>, <span class="Constant">3</span>
    press right-arrow
  ]
  run [
    editor-event-loop screen:address:screen, console:address:console, <span class="Constant">2</span>:address:editor-data
    <span class="Constant">3</span>:number<span class="Special"> &lt;- </span>get *<span class="Constant">2</span>:address:editor-data, <span class="Constant">cursor-row:offset</span>
    <span class="Constant">4</span>:number<span class="Special"> &lt;- </span>get *<span class="Constant">2</span>:address:editor-data, <span class="Constant">cursor-column:offset</span>
  ]
  memory-should-contain [
    <span class="Constant">3</span><span class="Special"> &lt;- </span><span class="Constant">2</span>
    <span class="Constant">4</span><span class="Special"> &lt;- </span><span class="Constant">0</span>
  ]
  <span class="Comment"># now hit right arrow again</span>
  assume-console [
    press right-arrow
  ]
  run [
    editor-event-loop screen:address:screen, console:address:console, <span class="Constant">2</span>:address:editor-data
    <span class="Constant">3</span>:number<span class="Special"> &lt;- </span>get *<span class="Constant">2</span>:address:editor-data, <span class="Constant">cursor-row:offset</span>
    <span class="Constant">4</span>:number<span class="Special"> &lt;- </span>get *<span class="Constant">2</span>:address:editor-data, <span class="Constant">cursor-column:offset</span>
  ]
  memory-should-contain [
    <span class="Constant">3</span><span class="Special"> &lt;- </span><span class="Constant">2</span>
    <span class="Constant">4</span><span class="Special"> &lt;- </span><span class="Constant">1</span>
  ]
  check-trace-count-for-label <span class="Constant">0</span>, <span class="Constant">[print-character]</span>
]

<span class="muScenario">scenario</span> editor-moves-cursor-to-next-wrapped-line-with-right-arrow-3 [
  assume-screen <span class="Constant">10/width</span>, <span class="Constant">5/height</span>
  <span class="Constant">1</span>:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[abcdef]</span>
  <span class="Constant">2</span>:address:editor-data<span class="Special"> &lt;- </span>new-editor <span class="Constant">1</span>:address:array:character, screen:address:screen, <span class="Constant">1/left</span>, <span class="Constant">6/right</span>
  editor-render screen, <span class="Constant">2</span>:address:editor-data
<span class="Constant">  $clear-trace</span>
  assume-console [
    left-click <span class="Constant">1</span>, <span class="Constant">4</span>
    press right-arrow
  ]
  run [
    editor-event-loop screen:address:screen, console:address:console, <span class="Constant">2</span>:address:editor-data
    <span class="Constant">3</span>:number<span class="Special"> &lt;- </span>get *<span class="Constant">2</span>:address:editor-data, <span class="Constant">cursor-row:offset</span>
    <span class="Constant">4</span>:number<span class="Special"> &lt;- </span>get *<span class="Constant">2</span>:address:editor-data, <span class="Constant">cursor-column:offset</span>
  ]
  screen-should-contain [
   <span class="Constant"> .          .</span>
   <span class="Constant"> . abcd↩    .</span>
   <span class="Constant"> . ef       .</span>
   <span class="Constant"> . ┈┈┈┈┈    .</span>
   <span class="Constant"> .          .</span>
  ]
  memory-should-contain [
    <span class="Constant">3</span><span class="Special"> &lt;- </span><span class="Constant">2</span>
    <span class="Constant">4</span><span class="Special"> &lt;- </span><span class="Constant">1</span>
  ]
  check-trace-count-for-label <span class="Constant">0</span>, <span class="Constant">[print-character]</span>
]

<span class="muScenario">scenario</span> editor-moves-cursor-to-next-line-with-right-arrow-at-end-of-line [
  assume-screen <span class="Constant">10/width</span>, <span class="Constant">5/height</span>
  <span class="Constant">1</span>:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[abc</span>
<span class="Constant">d]</span>
  <span class="Constant">2</span>:address:editor-data<span class="Special"> &lt;- </span>new-editor <span class="Constant">1</span>:address:array:character, screen:address:screen, <span class="Constant">0/left</span>, <span class="Constant">10/right</span>
  editor-render screen, <span class="Constant">2</span>:address:editor-data
<span class="Constant">  $clear-trace</span>
  <span class="Comment"># move to end of line, press right-arrow, type a character</span>
  assume-console [
    left-click <span class="Constant">1</span>, <span class="Constant">3</span>
    press right-arrow
    type <span class="Constant">[0]</span>
  ]
  run [
    editor-event-loop screen:address:screen, console:address:console, <span class="Constant">2</span>:address:editor-data
  ]
  <span class="Comment"># new character should be in next line</span>
  screen-should-contain [
   <span class="Constant"> .          .</span>
   <span class="Constant"> .abc       .</span>
   <span class="Constant"> .0d        .</span>
<span class="Constant">    .┈┈┈┈┈┈┈┈┈┈.</span>
   <span class="Constant"> .          .</span>
  ]
  check-trace-count-for-label <span class="Constant">2</span>, <span class="Constant">[print-character]</span>
]

<span class="Comment"># todo: ctrl-right: next word-end</span>

<span class="Comment"># left arrow</span>

<span class="muScenario">scenario</span> editor-moves-cursor-left-with-key [
  assume-screen <span class="Constant">10/width</span>, <span class="Constant">5/height</span>
  <span class="Constant">1</span>:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[abc]</span>
  <span class="Constant">2</span>:address:editor-data<span class="Special"> &lt;- </span>new-editor <span class="Constant">1</span>:address:array:character, screen:address:screen, <span class="Constant">0/left</span>, <span class="Constant">10/right</span>
  editor-render screen, <span class="Constant">2</span>:address:editor-data
<span class="Constant">  $clear-trace</span>
  assume-console [
    left-click <span class="Constant">1</span>, <span class="Constant">2</span>
    press left-arrow
    type <span class="Constant">[0]</span>
  ]
  run [
    editor-event-loop screen:address:screen, console:address:console, <span class="Constant">2</span>:address:editor-data
  ]
  screen-should-contain [
   <span class="Constant"> .          .</span>
   <span class="Constant"> .a0bc      .</span>
<span class="Constant">    .┈┈┈┈┈┈┈┈┈┈.</span>
   <span class="Constant"> .          .</span>
  ]
  check-trace-count-for-label <span class="Constant">3</span>, <span class="Constant">[print-character]</span>
]

<span class="muRecipe">after</span> <span class="Constant">&lt;handle-special-key&gt;</span> [
  <span class="Delimiter">{</span>
    move-to-previous-character?:boolean<span class="Special"> &lt;- </span>equal *k, <span class="Constant">65515/left-arrow</span>
    <span class="muControl">break-unless</span> move-to-previous-character?
    trace <span class="Constant">10</span>, <span class="Constant">[app]</span>, <span class="Constant">[left arrow]</span>
    <span class="Comment"># if not at start of text (before-cursor at § sentinel)</span>
    prev:address:duplex-list:character<span class="Special"> &lt;- </span>prev-duplex *before-cursor
    go-render?<span class="Special"> &lt;- </span>copy <span class="Constant">0/false</span>
    <span class="muControl">reply-unless</span> prev
<span class="Constant">    &lt;move-cursor-begin&gt;</span>
    editor, go-render?<span class="Special"> &lt;- </span>move-cursor-coordinates-left editor
    *before-cursor<span class="Special"> &lt;- </span>copy prev
    undo-coalesce-tag:number<span class="Special"> &lt;- </span>copy <span class="Constant">1/left-arrow</span>
<span class="Constant">    &lt;move-cursor-end&gt;</span>
    <span class="muControl">reply</span>
  <span class="Delimiter">}</span>
]

<span class="muScenario">scenario</span> editor-moves-cursor-to-previous-line-with-left-arrow-at-start-of-line [
  assume-screen <span class="Constant">10/width</span>, <span class="Constant">5/height</span>
  <span class="Comment"># initialize editor with two lines</span>
  <span class="Constant">1</span>:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[abc</span>
<span class="Constant">d]</span>
  <span class="Constant">2</span>:address:editor-data<span class="Special"> &lt;- </span>new-editor <span class="Constant">1</span>:address:array:character, screen:address:screen, <span class="Constant">0/left</span>, <span class="Constant">10/right</span>
  editor-render screen, <span class="Constant">2</span>:address:editor-data
<span class="Constant">  $clear-trace</span>
  <span class="Comment"># position cursor at start of second line (so there's no previous newline)</span>
  assume-console [
    left-click <span class="Constant">2</span>, <span class="Constant">0</span>
    press left-arrow
  ]
  run [
    editor-event-loop screen:address:screen, console:address:console, <span class="Constant">2</span>:address:editor-data
    <span class="Constant">3</span>:number<span class="Special"> &lt;- </span>get *<span class="Constant">2</span>:address:editor-data, <span class="Constant">cursor-row:offset</span>
    <span class="Constant">4</span>:number<span class="Special"> &lt;- </span>get *<span class="Constant">2</span>:address:editor-data, <span class="Constant">cursor-column:offset</span>
  ]
  memory-should-contain [
    <span class="Constant">3</span><span class="Special"> &lt;- </span><span class="Constant">1</span>
    <span class="Constant">4</span><span class="Special"> &lt;- </span><span class="Constant">3</span>
  ]
  check-trace-count-for-label <span class="Constant">0</span>, <span class="Constant">[print-character]</span>
]

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

<span class="muScenario">scenario</span> editor-moves-cursor-to-previous-line-with-left-arrow-at-start-of-line-3 [
  assume-screen <span class="Constant">10/width</span>, <span class="Constant">5/height</span>
  <span class="Constant">1</span>:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[abc</span>
<span class="Constant">def</span>
<span class="Constant">g]</span>
  <span class="Constant">2</span>:address:editor-data<span class="Special"> &lt;- </span>new-editor <span class="Constant">1</span>:address:array:character, screen:address:screen, <span class="Constant">0/left</span>, <span class="Constant">10/right</span>
  editor-render screen, <span class="Constant">2</span>:address:editor-data
<span class="Constant">  $clear-trace</span>
  <span class="Comment"># position cursor at start of text, press left-arrow, then type a character</span>
  assume-console [
    left-click <span class="Constant">1</span>, <span class="Constant">0</span>
    press left-arrow
    type <span class="Constant">[0]</span>
  ]
  run [
    editor-event-loop screen:address:screen, console:address:console, <span class="Constant">2</span>:address:editor-data
  ]
  <span class="Comment"># left-arrow should have had no effect</span>
  screen-should-contain [
   <span class="Constant"> .          .</span>
   <span class="Constant"> .0abc      .</span>
   <span class="Constant"> .def       .</span>
   <span class="Constant"> .g         .</span>
<span class="Constant">    .┈┈┈┈┈┈┈┈┈┈.</span>
  ]
  check-trace-count-for-label <span class="Constant">4</span>, <span class="Constant">[print-character]</span>  <span class="Comment"># length of first line</span>
]

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

d]
  <span class="Constant">2</span>:address:editor-data<span class="Special"> &lt;- </span>new-editor <span class="Constant">1</span>:address:array:character, screen:address:screen, <span class="Constant">0/left</span>, <span class="Constant">10/right</span>
  editor-render screen, <span class="Constant">2</span>:address:editor-data
<span class="Constant">  $clear-trace</span>
  <span class="Comment"># position cursor right after empty line</span>
  assume-console [
    left-click <span class="Constant">3</span>, <span class="Constant">0</span>
    press left-arrow
    type <span class="Constant">[0]</span>
  ]
  run [
    editor-event-loop screen:address:screen, console:address:console, <span class="Constant">2</span>:address:editor-data
  ]
  screen-should-contain [
   <span class="Constant"> .          .</span>
   <span class="Constant"> .abc       .</span>
   <span class="Constant"> .0         .</span>
   <span class="Constant"> .d         .</span>
<span class="Constant">    .┈┈┈┈┈┈┈┈┈┈.</span>
  ]
  check-trace-count-for-label <span class="Constant">1</span>, <span class="Constant">[print-character]</span>  <span class="Comment"># just the '0'</span>
]

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

<span class="Comment"># todo: ctrl-left: previous word-start</span>

<span class="Comment"># up arrow</span>

<span class="muScenario">scenario</span> editor-moves-to-previous-line-with-up-arrow [
  assume-screen <span class="Constant">10/width</span>, <span class="Constant">5/height</span>
  <span class="Constant">1</span>:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[abc</span>
<span class="Constant">def]</span>
  <span class="Constant">2</span>:address:editor-data<span class="Special"> &lt;- </span>new-editor <span class="Constant">1</span>:address:array:character, screen:address:screen, <span class="Constant">0/left</span>, <span class="Constant">10/right</span>
  editor-render screen, <span class="Constant">2</span>:address:editor-data
<span class="Constant">  $clear-trace</span>
  assume-console [
    left-click <span class="Constant">2</span>, <span class="Constant">1</span>
    press up-arrow
  ]
  run [
    editor-event-loop screen:address:screen, console:address:console, <span class="Constant">2</span>:address:editor-data
    <span class="Constant">3</span>:number<span class="Special"> &lt;- </span>get *<span class="Constant">2</span>:address:editor-data, <span class="Constant">cursor-row:offset</span>
    <span class="Constant">4</span>:number<span class="Special"> &lt;- </span>get *<span class="Constant">2</span>:address:editor-data, <span class="Constant">cursor-column:offset</span>
  ]
  memory-should-contain [
    <span class="Constant">3</span><span class="Special"> &lt;- </span><span class="Constant">1</span>
    <span class="Constant">4</span><span class="Special"> &lt;- </span><span class="Constant">1</span>
  ]
  check-trace-count-for-label <span class="Constant">0</span>, <span class="Constant">[print-character]</span>
  assume-console [
    type <span class="Constant">[0]</span>
  ]
  run [
    editor-event-loop screen:address:screen, console:address:console, <span class="Constant">2</span>:address:editor-data
  ]
  screen-should-contain [
   <span class="Constant"> .          .</span>
   <span class="Constant"> .a0bc      .</span>
   <span class="Constant"> .def       .</span>
<span class="Constant">    .┈┈┈┈┈┈┈┈┈┈.</span>
   <span class="Constant"> .          .</span>
  ]
]

<span class="muRecipe">after</span> <span class="Constant">&lt;handle-special-key&gt;</span> [
  <span class="Delimiter">{</span>
    move-to-previous-line?:boolean<span class="Special"> &lt;- </span>equal *k, <span class="Constant">65517/up-arrow</span>
    <span class="muControl">break-unless</span> move-to-previous-line?
<span class="Constant">    &lt;move-cursor-begin&gt;</span>
    editor, go-render?<span class="Special"> &lt;- </span>move-to-previous-line editor
    undo-coalesce-tag:number<span class="Special"> &lt;- </span>copy <span class="Constant">3/up-arrow</span>
<span class="Constant">    &lt;move-cursor-end&gt;</span>
    <span class="muControl">reply</span>
  <span class="Delimiter">}</span>
]

<span class="muRecipe">recipe</span> move-to-previous-line editor:address:editor-data<span class="muRecipe"> -&gt; </span>editor:address:editor-data, go-render?:boolean [
  <span class="Constant">local-scope</span>
  <span class="Constant">load-ingredients</span>
  cursor-row:address:number<span class="Special"> &lt;- </span>get-address *editor, <span class="Constant">cursor-row:offset</span>
  cursor-column:address:number<span class="Special"> &lt;- </span>get-address *editor, <span class="Constant">cursor-column:offset</span>
  before-cursor:address:address:duplex-list:character<span class="Special"> &lt;- </span>get-address *editor, <span class="Constant">before-cursor:offset</span>
  left:number<span class="Special"> &lt;- </span>get *editor, <span class="Constant">left:offset</span>
  right:number<span class="Special"> &lt;- </span>get *editor, <span class="Constant">right:offset</span>
  already-at-top?:boolean<span class="Special"> &lt;- </span>lesser-or-equal *cursor-row, <span class="Constant">1/top</span>
  <span class="Delimiter">{</span>
    <span class="Comment"># if cursor not at top, move it</span>
    <span class="muControl">break-if</span> already-at-top?
    <span class="Comment"># if not at newline, move to start of line (previous newline)</span>
    <span class="Comment"># then scan back another line</span>
    <span class="Comment"># if either step fails, give up without modifying cursor or coordinates</span>
    curr:address:duplex-list:character<span class="Special"> &lt;- </span>copy *before-cursor
    <span class="Delimiter">{</span>
      old:address:duplex-list:character<span class="Special"> &lt;- </span>copy curr
      c2:character<span class="Special"> &lt;- </span>get *curr, <span class="Constant">value:offset</span>
      at-newline?:boolean<span class="Special"> &lt;- </span>equal c2, <span class="Constant">10/newline</span>
      <span class="muControl">break-if</span> at-newline?
      curr:address:duplex-list:character<span class="Special"> &lt;- </span>before-previous-line curr, editor
      no-motion?:boolean<span class="Special"> &lt;- </span>equal curr, old
      go-render?<span class="Special"> &lt;- </span>copy <span class="Constant">0/false</span>
      <span class="muControl">reply-if</span> no-motion?
    <span class="Delimiter">}</span>
    <span class="Delimiter">{</span>
      old<span class="Special"> &lt;- </span>copy curr
      curr<span class="Special"> &lt;- </span>before-previous-line curr, editor
      no-motion?:boolean<span class="Special"> &lt;- </span>equal curr, old
      go-render?<span class="Special"> &lt;- </span>copy <span class="Constant">0/false</span>
      <span class="muControl">reply-if</span> no-motion?
    <span class="Delimiter">}</span>
    *before-cursor<span class="Special"> &lt;- </span>copy curr
    *cursor-row<span class="Special"> &lt;- </span>subtract *cursor-row, <span class="Constant">1</span>
    <span class="Comment"># scan ahead to right column or until end of line</span>
    target-column:number<span class="Special"> &lt;- </span>copy *cursor-column
    *cursor-column<span class="Special"> &lt;- </span>copy left
    <span class="Delimiter">{</span>
      done?:boolean<span class="Special"> &lt;- </span>greater-or-equal *cursor-column, target-column
      <span class="muControl">break-if</span> done?
      curr:address:duplex-list:character<span class="Special"> &lt;- </span>next-duplex *before-cursor
      <span class="muControl">break-unless</span> curr
      currc:character<span class="Special"> &lt;- </span>get *curr, <span class="Constant">value:offset</span>
      at-newline?:boolean<span class="Special"> &lt;- </span>equal currc, <span class="Constant">10/newline</span>
      <span class="muControl">break-if</span> at-newline?
      <span class="Comment">#</span>
      *before-cursor<span class="Special"> &lt;- </span>copy curr
      *cursor-column<span class="Special"> &lt;- </span>add *cursor-column, <span class="Constant">1</span>
      <span class="muControl">loop</span>
    <span class="Delimiter">}</span>
    go-render?<span class="Special"> &lt;- </span>copy <span class="Constant">0/false</span>
    <span class="muControl">reply</span>
  <span class="Delimiter">}</span>
  <span class="Delimiter">{</span>
    <span class="Comment"># if cursor already at top, scroll up</span>
    <span class="muControl">break-unless</span> already-at-top?
<span class="Constant">    &lt;scroll-up&gt;</span>
    go-render?<span class="Special"> &lt;- </span>copy <span class="Constant">1/true</span>
    <span class="muControl">reply</span>
  <span class="Delimiter">}</span>
]

<span class="muScenario">scenario</span> editor-adjusts-column-at-previous-line [
  assume-screen <span class="Constant">10/width</span>, <span class="Constant">5/height</span>
  <span class="Constant">1</span>:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[ab</span>
<span class="Constant">def]</span>
  <span class="Constant">2</span>:address:editor-data<span class="Special"> &lt;- </span>new-editor <span class="Constant">1</span>:address:array:character, screen:address:screen, <span class="Constant">0/left</span>, <span class="Constant">10/right</span>
  editor-render screen, <span class="Constant">2</span>:address:editor-data
<span class="Constant">  $clear-trace</span>
  assume-console [
    left-click <span class="Constant">2</span>, <span class="Constant">3</span>
    press up-arrow
  ]
  run [
    editor-event-loop screen:address:screen, console:address:console, <span class="Constant">2</span>:address:editor-data
    <span class="Constant">3</span>:number<span class="Special"> &lt;- </span>get *<span class="Constant">2</span>:address:editor-data, <span class="Constant">cursor-row:offset</span>
    <span class="Constant">4</span>:number<span class="Special"> &lt;- </span>get *<span class="Constant">2</span>:address:editor-data, <span class="Constant">cursor-column:offset</span>
  ]
  memory-should-contain [
    <span class="Constant">3</span><span class="Special"> &lt;- </span><span class="Constant">1</span>
    <span class="Constant">4</span><span class="Special"> &lt;- </span><span class="Constant">2</span>
  ]
  check-trace-count-for-label <span class="Constant">0</span>, <span class="Constant">[print-character]</span>
  assume-console [
    type <span class="Constant">[0]</span>
  ]
  run [
    editor-event-loop screen:address:screen, console:address:console, <span class="Constant">2</span>:address:editor-data
  ]
  screen-should-contain [
   <span class="Constant"> .          .</span>
   <span class="Constant"> .ab0       .</span>
   <span class="Constant"> .def       .</span>
<span class="Constant">    .┈┈┈┈┈┈┈┈┈┈.</span>
   <span class="Constant"> .          .</span>
  ]
]

<span class="muScenario">scenario</span> editor-adjusts-column-at-empty-line [
  assume-screen <span class="Constant">10/width</span>, <span class="Constant">5/height</span>
  <span class="Constant">1</span>:address:array:character<span class="Special"> &lt;- </span>new [
def]
  <span class="Constant">2</span>:address:editor-data<span class="Special"> &lt;- </span>new-editor <span class="Constant">1</span>:address:array:character, screen:address:screen, <span class="Constant">0/left</span>, <span class="Constant">10/right</span>
  editor-render screen, <span class="Constant">2</span>:address:editor-data
<span class="Constant">  $clear-trace</span>
  assume-console [
    left-click <span class="Constant">2</span>, <span class="Constant">3</span>
    press up-arrow
  ]
  run [
    editor-event-loop screen:address:screen, console:address:console, <span class="Constant">2</span>:address:editor-data
    <span class="Constant">3</span>:number<span class="Special"> &lt;- </span>get *<span class="Constant">2</span>:address:editor-data, <span class="Constant">cursor-row:offset</span>
    <span class="Constant">4</span>:number<span class="Special"> &lt;- </span>get *<span class="Constant">2</span>:address:editor-data, <span class="Constant">cursor-column:offset</span>
  ]
  memory-should-contain [
    <span class="Constant">3</span><span class="Special"> &lt;- </span><span class="Constant">1</span>
    <span class="Constant">4</span><span class="Special"> &lt;- </span><span class="Constant">0</span>
  ]
  check-trace-count-for-label <span class="Constant">0</span>, <span class="Constant">[print-character]</span>
  assume-console [
    type <span class="Constant">[0]</span>
  ]
  run [
    editor-event-loop screen:address:screen, console:address:console, <span class="Constant">2</span>:address:editor-data
  ]
  screen-should-contain [
   <span class="Constant"> .          .</span>
   <span class="Constant"> .0         .</span>
   <span class="Constant"> .def       .</span>
<span class="Constant">    .┈┈┈┈┈┈┈┈┈┈.</span>
   <span class="Constant"> .          .</span>
  ]
]

<span class="muScenario">scenario</span> editor-moves-to-previous-line-from-left-margin [
  assume-screen <span class="Constant">10/width</span>, <span class="Constant">5/height</span>
  <span class="Comment"># start out with three lines</span>
  <span class="Constant">1</span>:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[abc</span>
<span class="Constant">def</span>
<span class="Constant">ghi]</span>
  <span class="Constant">2</span>:address:editor-data<span class="Special"> &lt;- </span>new-editor <span class="Constant">1</span>:address:array:character, screen:address:screen, <span class="Constant">0/left</span>, <span class="Constant">10/right</span>
  editor-render screen, <span class="Constant">2</span>:address:editor-data
<span class="Constant">  $clear-trace</span>
  <span class="Comment"># click on the third line and hit up-arrow, so you end up just after a newline</span>
  assume-console [
    left-click <span class="Constant">3</span>, <span class="Constant">0</span>
    press up-arrow
  ]
  run [
    editor-event-loop screen:address:screen, console:address:console, <span class="Constant">2</span>:address:editor-data
    <span class="Constant">3</span>:number<span class="Special"> &lt;- </span>get *<span class="Constant">2</span>:address:editor-data, <span class="Constant">cursor-row:offset</span>
    <span class="Constant">4</span>:number<span class="Special"> &lt;- </span>get *<span class="Constant">2</span>:address:editor-data, <span class="Constant">cursor-column:offset</span>
  ]
  memory-should-contain [
    <span class="Constant">3</span><span class="Special"> &lt;- </span><span class="Constant">2</span>
    <span class="Constant">4</span><span class="Special"> &lt;- </span><span class="Constant">0</span>
  ]
  check-trace-count-for-label <span class="Constant">0</span>, <span class="Constant">[print-character]</span>
  assume-console [
    type <span class="Constant">[0]</span>
  ]
  run [
    editor-event-loop screen:address:screen, console:address:console, <span class="Constant">2</span>:address:editor-data
  ]
  screen-should-contain [
   <span class="Constant"> .          .</span>
   <span class="Constant"> .abc       .</span>
   <span class="Constant"> .0def      .</span>
   <span class="Constant"> .ghi       .</span>
<span class="Constant">    .┈┈┈┈┈┈┈┈┈┈.</span>
  ]
]

<span class="Comment"># down arrow</span>

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

<span class="muRecipe">after</span> <span class="Constant">&lt;handle-special-key&gt;</span> [
  <span class="Delimiter">{</span>
    move-to-next-line?:boolean<span class="Special"> &lt;- </span>equal *k, <span class="Constant">65516/down-arrow</span>
    <span class="muControl">break-unless</span> move-to-next-line?
<span class="Constant">    &lt;move-cursor-begin&gt;</span>
    editor, go-render?<span class="Special"> &lt;- </span>move-to-next-line editor, screen-height
    undo-coalesce-tag:number<span class="Special"> &lt;- </span>copy <span class="Constant">4/down-arrow</span>
<span class="Constant">    &lt;move-cursor-end&gt;</span>
    <span class="muControl">reply</span>
  <span class="Delimiter">}</span>
]

<span class="muRecipe">recipe</span> move-to-next-line editor:address:editor-data, screen-height:number<span class="muRecipe"> -&gt; </span>editor:address:editor-data, go-render?:boolean [
  <span class="Constant">local-scope</span>
  <span class="Constant">load-ingredients</span>
  cursor-row:address:number<span class="Special"> &lt;- </span>get-address *editor, <span class="Constant">cursor-row:offset</span>
  cursor-column:address:number<span class="Special"> &lt;- </span>get-address *editor, <span class="Constant">cursor-column:offset</span>
  before-cursor:address:address:duplex-list:character<span class="Special"> &lt;- </span>get-address *editor, <span class="Constant">before-cursor:offset</span>
  left:number<span class="Special"> &lt;- </span>get *editor, <span class="Constant">left:offset</span>
  right:number<span class="Special"> &lt;- </span>get *editor, <span class="Constant">right:offset</span>
  last-line:number<span class="Special"> &lt;- </span>subtract screen-height, <span class="Constant">1</span>
  already-at-bottom?:boolean<span class="Special"> &lt;- </span>greater-or-equal *cursor-row, last-line
  <span class="Delimiter">{</span>
    <span class="Comment"># if cursor not at bottom, move it</span>
    <span class="muControl">break-if</span> already-at-bottom?
    <span class="Comment"># scan to start of next line, then to right column or until end of line</span>
    max:number<span class="Special"> &lt;- </span>subtract right, left
    next-line:address:duplex-list:character<span class="Special"> &lt;- </span>before-start-of-next-line *before-cursor, max
    <span class="Delimiter">{</span>
      <span class="Comment"># already at end of buffer? try to scroll up (so we can see more</span>
      <span class="Comment"># warnings or sandboxes below)</span>
      no-motion?:boolean<span class="Special"> &lt;- </span>equal next-line, *before-cursor
      <span class="muControl">break-unless</span> no-motion?
      scroll?:boolean<span class="Special"> &lt;- </span>greater-than *cursor-row, <span class="Constant">1</span>
      <span class="muControl">break-if</span> scroll?, <span class="Constant">+try-to-scroll:label</span>
      go-render?<span class="Special"> &lt;- </span>copy <span class="Constant">0/false</span>
      <span class="muControl">reply</span>
    <span class="Delimiter">}</span>
    *cursor-row<span class="Special"> &lt;- </span>add *cursor-row, <span class="Constant">1</span>
    *before-cursor<span class="Special"> &lt;- </span>copy next-line
    target-column:number<span class="Special"> &lt;- </span>copy *cursor-column
    *cursor-column<span class="Special"> &lt;- </span>copy left
    <span class="Delimiter">{</span>
      done?:boolean<span class="Special"> &lt;- </span>greater-or-equal *cursor-column, target-column
      <span class="muControl">break-if</span> done?
      curr:address:duplex-list:character<span class="Special"> &lt;- </span>next-duplex *before-cursor
      <span class="muControl">break-unless</span> curr
      currc:character<span class="Special"> &lt;- </span>get *curr, <span class="Constant">value:offset</span>
      at-newline?:boolean<span class="Special"> &lt;- </span>equal currc, <span class="Constant">10/newline</span>
      <span class="muControl">break-if</span> at-newline?
      <span class="Comment">#</span>
      *before-cursor<span class="Special"> &lt;- </span>copy curr
      *cursor-column<span class="Special"> &lt;- </span>add *cursor-column, <span class="Constant">1</span>
      <span class="muControl">loop</span>
    <span class="Delimiter">}</span>
    go-render?<span class="Special"> &lt;- </span>copy <span class="Constant">0/false</span>
    <span class="muControl">reply</span>
  <span class="Delimiter">}</span>
<span class="Constant">  +try-to-scroll</span>
<span class="Constant">  &lt;scroll-down&gt;</span>
  go-render?<span class="Special"> &lt;- </span>copy <span class="Constant">1/true</span>
]

<span class="muScenario">scenario</span> editor-adjusts-column-at-next-line [
  assume-screen <span class="Constant">10/width</span>, <span class="Constant">5/height</span>
  <span class="Constant">1</span>:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[abc</span>
<span class="Constant">de]</span>
  <span class="Constant">2</span>:address:editor-data<span class="Special"> &lt;- </span>new-editor <span class="Constant">1</span>:address:array:character, screen:address:screen, <span class="Constant">0/left</span>, <span class="Constant">10/right</span>
  editor-render screen, <span class="Constant">2</span>:address:editor-data
<span class="Constant">  $clear-trace</span>
  assume-console [
    left-click <span class="Constant">1</span>, <span class="Constant">3</span>
    press down-arrow
  ]
  run [
    editor-event-loop screen:address:screen, console:address:console, <span class="Constant">2</span>:address:editor-data
    <span class="Constant">3</span>:number<span class="Special"> &lt;- </span>get *<span class="Constant">2</span>:address:editor-data, <span class="Constant">cursor-row:offset</span>
    <span class="Constant">4</span>:number<span class="Special"> &lt;- </span>get *<span class="Constant">2</span>:address:editor-data, <span class="Constant">cursor-column:offset</span>
  ]
  memory-should-contain [
    <span class="Constant">3</span><span class="Special"> &lt;- </span><span class="Constant">2</span>
    <span class="Constant">4</span><span class="Special"> &lt;- </span><span class="Constant">2</span>
  ]
  check-trace-count-for-label <span class="Constant">0</span>, <span class="Constant">[print-character]</span>
  assume-console [
    type <span class="Constant">[0]</span>
  ]
  run [
    editor-event-loop screen:address:screen, console:address:console, <span class="Constant">2</span>:address:editor-data
  ]
  screen-should-contain [
   <span class="Constant"> .          .</span>
   <span class="Constant"> .abc       .</span>
   <span class="Constant"> .de0       .</span>
<span class="Constant">    .┈┈┈┈┈┈┈┈┈┈.</span>
   <span class="Constant"> .          .</span>
  ]
]

<span class="Comment"># ctrl-a/home - move cursor to start of line</span>

<span class="muScenario">scenario</span> editor-moves-to-start-of-line-with-ctrl-a [
  assume-screen <span class="Constant">10/width</span>, <span class="Constant">5/height</span>
  <span class="Constant">1</span>:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[123</span>
<span class="Constant">456]</span>
  <span class="Constant">2</span>:address:editor-data<span class="Special"> &lt;- </span>new-editor <span class="Constant">1</span>:address:array:character, screen:address:screen, <span class="Constant">0/left</span>, <span class="Constant">10/right</span>
  editor-render screen, <span class="Constant">2</span>:address:editor-data
<span class="Constant">  $clear-trace</span>
  <span class="Comment"># start on second line, press ctrl-a</span>
  assume-console [
    left-click <span class="Constant">2</span>, <span class="Constant">3</span>
    press ctrl-a
  ]
  run [
    editor-event-loop screen:address:screen, console:address:console, <span class="Constant">2</span>:address:editor-data
    <span class="Constant">4</span>:number<span class="Special"> &lt;- </span>get *<span class="Constant">2</span>:address:editor-data, <span class="Constant">cursor-row:offset</span>
    <span class="Constant">5</span>:number<span class="Special"> &lt;- </span>get *<span class="Constant">2</span>:address:editor-data, <span class="Constant">cursor-column:offset</span>
  ]
  <span class="Comment"># cursor moves to start of line</span>
  memory-should-contain [
    <span class="Constant">4</span><span class="Special"> &lt;- </span><span class="Constant">2</span>
    <span class="Constant">5</span><span class="Special"> &lt;- </span><span class="Constant">0</span>
  ]
  check-trace-count-for-label <span class="Constant">0</span>, <span class="Constant">[print-character]</span>
]

<span class="muRecipe">after</span> <span class="Constant">&lt;handle-special-character&gt;</span> [
  <span class="Delimiter">{</span>
    move-to-start-of-line?:boolean<span class="Special"> &lt;- </span>equal *c, <span class="Constant">1/ctrl-a</span>
    <span class="muControl">break-unless</span> move-to-start-of-line?
<span class="Constant">    &lt;move-cursor-begin&gt;</span>
    move-to-start-of-line editor
    undo-coalesce-tag:number<span class="Special"> &lt;- </span>copy <span class="Constant">0/never</span>
<span class="Constant">    &lt;move-cursor-end&gt;</span>
    go-render?<span class="Special"> &lt;- </span>copy <span class="Constant">0/false</span>
    <span class="muControl">reply</span>
  <span class="Delimiter">}</span>
]

<span class="muRecipe">after</span> <span class="Constant">&lt;handle-special-key&gt;</span> [
  <span class="Delimiter">{</span>
    move-to-start-of-line?:boolean<span class="Special"> &lt;- </span>equal *k, <span class="Constant">65521/home</span>
    <span class="muControl">break-unless</span> move-to-start-of-line?
<span class="Constant">    &lt;move-cursor-begin&gt;</span>
    move-to-start-of-line editor
    undo-coalesce-tag:number<span class="Special"> &lt;- </span>copy <span class="Constant">0/never</span>
<span class="Constant">    &lt;move-cursor-end&gt;</span>
    go-render?<span class="Special"> &lt;- </span>copy <span class="Constant">0/false</span>
    <span class="muControl">reply</span>
  <span class="Delimiter">}</span>
]

<span class="muRecipe">recipe</span> move-to-start-of-line editor:address:editor-data [
  <span class="Constant">local-scope</span>
  <span class="Constant">load-ingredients</span>
  <span class="Comment"># update cursor column</span>
  left:number<span class="Special"> &lt;- </span>get *editor, <span class="Constant">left:offset</span>
  cursor-column:address:number<span class="Special"> &lt;- </span>get-address *editor, <span class="Constant">cursor-column:offset</span>
  *cursor-column<span class="Special"> &lt;- </span>copy left
  <span class="Comment"># update before-cursor</span>
  before-cursor:address:address:duplex-list:character<span class="Special"> &lt;- </span>get-address *editor, <span class="Constant">before-cursor:offset</span>
  init:address:duplex-list:character<span class="Special"> &lt;- </span>get *editor, <span class="Constant">data:offset</span>
  <span class="Comment"># while not at start of line, move </span>
  <span class="Delimiter">{</span>
    at-start-of-text?:boolean<span class="Special"> &lt;- </span>equal *before-cursor, init
    <span class="muControl">break-if</span> at-start-of-text?
    prev:character<span class="Special"> &lt;- </span>get **before-cursor, <span class="Constant">value:offset</span>
    at-start-of-line?:boolean<span class="Special"> &lt;- </span>equal prev, <span class="Constant">10/newline</span>
    <span class="muControl">break-if</span> at-start-of-line?
    *before-cursor<span class="Special"> &lt;- </span>prev-duplex *before-cursor
    assert *before-cursor, <span class="Constant">[move-to-start-of-line tried to move before start of text]</span>
    <span class="muControl">loop</span>
  <span class="Delimiter">}</span>
]

<span class="muScenario">scenario</span> editor-moves-to-start-of-line-with-ctrl-a-2 [
  assume-screen <span class="Constant">10/width</span>, <span class="Constant">5/height</span>
  <span class="Constant">1</span>:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[123</span>
<span class="Constant">456]</span>
  <span class="Constant">2</span>:address:editor-data<span class="Special"> &lt;- </span>new-editor <span class="Constant">1</span>:address:array:character, screen:address:screen, <span class="Constant">0/left</span>, <span class="Constant">10/right</span>
  editor-render screen, <span class="Constant">2</span>:address:editor-data
<span class="Constant">  $clear-trace</span>
  <span class="Comment"># start on first line (no newline before), press ctrl-a</span>
  assume-console [
    left-click <span class="Constant">1</span>, <span class="Constant">3</span>
    press ctrl-a
  ]
  run [
    editor-event-loop screen:address:screen, console:address:console, <span class="Constant">2</span>:address:editor-data
    <span class="Constant">4</span>:number<span class="Special"> &lt;- </span>get *<span class="Constant">2</span>:address:editor-data, <span class="Constant">cursor-row:offset</span>
    <span class="Constant">5</span>:number<span class="Special"> &lt;- </span>get *<span class="Constant">2</span>:address:editor-data, <span class="Constant">cursor-column:offset</span>
  ]
  <span class="Comment"># cursor moves to start of line</span>
  memory-should-contain [
    <span class="Constant">4</span><span class="Special"> &lt;- </span><span class="Constant">1</span>
    <span class="Constant">5</span><span class="Special"> &lt;- </span><span class="Constant">0</span>
  ]
  check-trace-count-for-label <span class="Constant">0</span>, <span class="Constant">[print-character]</span>
]

<span class="muScenario">scenario</span> editor-moves-to-start-of-line-with-home [
  assume-screen <span class="Constant">10/width</span>, <span class="Constant">5/height</span>
  <span class="Constant">1</span>:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[123</span>
<span class="Constant">456]</span>
  <span class="Constant">2</span>:address:editor-data<span class="Special"> &lt;- </span>new-editor <span class="Constant">1</span>:address:array:character, screen:address:screen, <span class="Constant">0/left</span>, <span class="Constant">10/right</span>
<span class="Constant">  $clear-trace</span>
  <span class="Comment"># start on second line, press 'home'</span>
  assume-console [
    left-click <span class="Constant">2</span>, <span class="Constant">3</span>
    press home
  ]
  run [
    editor-event-loop screen:address:screen, console:address:console, <span class="Constant">2</span>:address:editor-data
    <span class="Constant">3</span>:number<span class="Special"> &lt;- </span>get *<span class="Constant">2</span>:address:editor-data, <span class="Constant">cursor-row:offset</span>
    <span class="Constant">4</span>:number<span class="Special"> &lt;- </span>get *<span class="Constant">2</span>:address:editor-data, <span class="Constant">cursor-column:offset</span>
  ]
  <span class="Comment"># cursor moves to start of line</span>
  memory-should-contain [
    <span class="Constant">3</span><span class="Special"> &lt;- </span><span class="Constant">2</span>
    <span class="Constant">4</span><span class="Special"> &lt;- </span><span class="Constant">0</span>
  ]
  check-trace-count-for-label <span class="Constant">0</span>, <span class="Constant">[print-character]</span>
]

<span class="muScenario">scenario</span> editor-moves-to-start-of-line-with-home-2 [
  assume-screen <span class="Constant">10/width</span>, <span class="Constant">5/height</span>
  <span class="Constant">1</span>:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[123</span>
<span class="Constant">456]</span>
  <span class="Constant">2</span>:address:editor-data<span class="Special"> &lt;- </span>new-editor <span class="Constant">1</span>:address:array:character, screen:address:screen, <span class="Constant">0/left</span>, <span class="Constant">10/right</span>
  editor-render screen, <span class="Constant">2</span>:address:editor-data
<span class="Constant">  $clear-trace</span>
  <span class="Comment"># start on first line (no newline before), press 'home'</span>
  assume-console [
    left-click <span class="Constant">1</span>, <span class="Constant">3</span>
    press home
  ]
  run [
    editor-event-loop screen:address:screen, console:address:console, <span class="Constant">2</span>:address:editor-data
    <span class="Constant">3</span>:number<span class="Special"> &lt;- </span>get *<span class="Constant">2</span>:address:editor-data, <span class="Constant">cursor-row:offset</span>
    <span class="Constant">4</span>:number<span class="Special"> &lt;- </span>get *<span class="Constant">2</span>:address:editor-data, <span class="Constant">cursor-column:offset</span>
  ]
  <span class="Comment"># cursor moves to start of line</span>
  memory-should-contain [
    <span class="Constant">3</span><span class="Special"> &lt;- </span><span class="Constant">1</span>
    <span class="Constant">4</span><span class="Special"> &lt;- </span><span class="Constant">0</span>
  ]
  check-trace-count-for-label <span class="Constant">0</span>, <span class="Constant">[print-character]</span>
]

<span class="Comment"># ctrl-e/end - move cursor to end of line</span>

<span class="muScenario">scenario</span> editor-moves-to-end-of-line-with-ctrl-e [
  assume-screen <span class="Constant">10/width</span>, <span class="Constant">5/height</span>
  <span class="Constant">1</span>:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[123</span>
<span class="Constant">456]</span>
  <span class="Constant">2</span>:address:editor-data<span class="Special"> &lt;- </span>new-editor <span class="Constant">1</span>:address:array:character, screen:address:screen, <span class="Constant">0/left</span>, <span class="Constant">10/right</span>
  editor-render screen, <span class="Constant">2</span>:address:editor-data
<span class="Constant">  $clear-trace</span>
  <span class="Comment"># start on first line, press ctrl-e</span>
  assume-console [
    left-click <span class="Constant">1</span>, <span class="Constant">1</span>
    press ctrl-e
  ]
  run [
    editor-event-loop screen:address:screen, console:address:console, <span class="Constant">2</span>:address:editor-data
    <span class="Constant">4</span>:number<span class="Special"> &lt;- </span>get *<span class="Constant">2</span>:address:editor-data, <span class="Constant">cursor-row:offset</span>
    <span class="Constant">5</span>:number<span class="Special"> &lt;- </span>get *<span class="Constant">2</span>:address:editor-data, <span class="Constant">cursor-column:offset</span>
  ]
  <span class="Comment"># cursor moves to end of line</span>
  memory-should-contain [
    <span class="Constant">4</span><span class="Special"> &lt;- </span><span class="Constant">1</span>
    <span class="Constant">5</span><span class="Special"> &lt;- </span><span class="Constant">3</span>
  ]
  check-trace-count-for-label <span class="Constant">0</span>, <span class="Constant">[print-character]</span>
  <span class="Comment"># editor inserts future characters at cursor</span>
  assume-console [
    type <span class="Constant">[z]</span>
  ]
  run [
    editor-event-loop screen:address:screen, console:address:console, <span class="Constant">2</span>:address:editor-data
    <span class="Constant">4</span>:number<span class="Special"> &lt;- </span>get *<span class="Constant">2</span>:address:editor-data, <span class="Constant">cursor-row:offset</span>
    <span class="Constant">5</span>:number<span class="Special"> &lt;- </span>get *<span class="Constant">2</span>:address:editor-data, <span class="Constant">cursor-column:offset</span>
  ]
  memory-should-contain [
    <span class="Constant">4</span><span class="Special"> &lt;- </span><span class="Constant">1</span>
    <span class="Constant">5</span><span class="Special"> &lt;- </span><span class="Constant">4</span>
  ]
  screen-should-contain [
   <span class="Constant"> .          .</span>
   <span class="Constant"> .123z      .</span>
   <span class="Constant"> .456       .</span>
<span class="Constant">    .┈┈┈┈┈┈┈┈┈┈.</span>
   <span class="Constant"> .          .</span>
  ]
  check-trace-count-for-label <span class="Constant">1</span>, <span class="Constant">[print-character]</span>
]

<span class="muRecipe">after</span> <span class="Constant">&lt;handle-special-character&gt;</span> [
  <span class="Delimiter">{</span>
    move-to-end-of-line?:boolean<span class="Special"> &lt;- </span>equal *c, <span class="Constant">5/ctrl-e</span>
    <span class="muControl">break-unless</span> move-to-end-of-line?
<span class="Constant">    &lt;move-cursor-begin&gt;</span>
    move-to-end-of-line editor
    undo-coalesce-tag:number<span class="Special"> &lt;- </span>copy <span class="Constant">0/never</span>
<span class="Constant">    &lt;move-cursor-end&gt;</span>
    go-render?<span class="Special"> &lt;- </span>copy <span class="Constant">0/false</span>
    <span class="muControl">reply</span>
  <span class="Delimiter">}</span>
]

<span class="muRecipe">after</span> <span class="Constant">&lt;handle-special-key&gt;</span> [
  <span class="Delimiter">{</span>
    move-to-end-of-line?:boolean<span class="Special"> &lt;- </span>equal *k, <span class="Constant">65520/end</span>
    <span class="muControl">break-unless</span> move-to-end-of-line?
<span class="Constant">    &lt;move-cursor-begin&gt;</span>
    move-to-end-of-line editor
    undo-coalesce-tag:number<span class="Special"> &lt;- </span>copy <span class="Constant">0/never</span>
<span class="Constant">    &lt;move-cursor-end&gt;</span>
    go-render?<span class="Special"> &lt;- </span>copy <span class="Constant">0/false</span>
    <span class="muControl">reply</span>
  <span class="Delimiter">}</span>
]

<span class="muRecipe">recipe</span> move-to-end-of-line editor:address:editor-data [
  <span class="Constant">local-scope</span>
  <span class="Constant">load-ingredients</span>
  before-cursor:address:address:duplex-list:character<span class="Special"> &lt;- </span>get-address *editor, <span class="Constant">before-cursor:offset</span>
  cursor-column:address:number<span class="Special"> &lt;- </span>get-address *editor, <span class="Constant">cursor-column:offset</span>
  <span class="Comment"># while not at start of line, move </span>
  <span class="Delimiter">{</span>
    next:address:duplex-list:character<span class="Special"> &lt;- </span>next-duplex *before-cursor
    <span class="muControl">break-unless</span> next  <span class="Comment"># end of text</span>
    nextc:character<span class="Special"> &lt;- </span>get *next, <span class="Constant">value:offset</span>
    at-end-of-line?:boolean<span class="Special"> &lt;- </span>equal nextc, <span class="Constant">10/newline</span>
    <span class="muControl">break-if</span> at-end-of-line?
    *before-cursor<span class="Special"> &lt;- </span>copy next
    *cursor-column<span class="Special"> &lt;- </span>add *cursor-column, <span class="Constant">1</span>
    <span class="muControl">loop</span>
  <span class="Delimiter">}</span>
]

<span class="muScenario">scenario</span> editor-moves-to-end-of-line-with-ctrl-e-2 [
  assume-screen <span class="Constant">10/width</span>, <span class="Constant">5/height</span>
  <span class="Constant">1</span>:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[123</span>
<span class="Constant">456]</span>
  <span class="Constant">2</span>:address:editor-data<span class="Special"> &lt;- </span>new-editor <span class="Constant">1</span>:address:array:character, screen:address:screen, <span class="Constant">0/left</span>, <span class="Constant">10/right</span>
  editor-render screen, <span class="Constant">2</span>:address:editor-data
<span class="Constant">  $clear-trace</span>
  <span class="Comment"># start on second line (no newline after), press ctrl-e</span>
  assume-console [
    left-click <span class="Constant">2</span>, <span class="Constant">1</span>
    press ctrl-e
  ]
  run [
    editor-event-loop screen:address:screen, console:address:console, <span class="Constant">2</span>:address:editor-data
    <span class="Constant">4</span>:number<span class="Special"> &lt;- </span>get *<span class="Constant">2</span>:address:editor-data, <span class="Constant">cursor-row:offset</span>
    <span class="Constant">5</span>:number<span class="Special"> &lt;- </span>get *<span class="Constant">2</span>:address:editor-data, <span class="Constant">cursor-column:offset</span>
  ]
  <span class="Comment"># cursor moves to end of line</span>
  memory-should-contain [
    <span class="Constant">4</span><span class="Special"> &lt;- </span><span class="Constant">2</span>
    <span class="Constant">5</span><span class="Special"> &lt;- </span><span class="Constant">3</span>
  ]
  check-trace-count-for-label <span class="Constant">0</span>, <span class="Constant">[print-character]</span>
]

<span class="muScenario">scenario</span> editor-moves-to-end-of-line-with-end [
  assume-screen <span class="Constant">10/width</span>, <span class="Constant">5/height</span>
  <span class="Constant">1</span>:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[123</span>
<span class="Constant">456]</span>
  <span class="Constant">2</span>:address:editor-data<span class="Special"> &lt;- </span>new-editor <span class="Constant">1</span>:address:array:character, screen:address:screen, <span class="Constant">0/left</span>, <span class="Constant">10/right</span>
  editor-render screen, <span class="Constant">2</span>:address:editor-data
<span class="Constant">  $clear-trace</span>
  <span class="Comment"># start on first line, press 'end'</span>
  assume-console [
    left-click <span class="Constant">1</span>, <span class="Constant">1</span>
    press end
  ]
  run [
    editor-event-loop screen:address:screen, console:address:console, <span class="Constant">2</span>:address:editor-data
    <span class="Constant">3</span>:number<span class="Special"> &lt;- </span>get *<span class="Constant">2</span>:address:editor-data, <span class="Constant">cursor-row:offset</span>
    <span class="Constant">4</span>:number<span class="Special"> &lt;- </span>get *<span class="Constant">2</span>:address:editor-data, <span class="Constant">cursor-column:offset</span>
  ]
  <span class="Comment"># cursor moves to end of line</span>
  memory-should-contain [
    <span class="Constant">3</span><span class="Special"> &lt;- </span><span class="Constant">1</span>
    <span class="Constant">4</span><span class="Special"> &lt;- </span><span class="Constant">3</span>
  ]
  check-trace-count-for-label <span class="Constant">0</span>, <span class="Constant">[print-character]</span>
]

<span class="muScenario">scenario</span> editor-moves-to-end-of-line-with-end-2 [
  assume-screen <span class="Constant">10/width</span>, <span class="Constant">5/height</span>
  <span class="Constant">1</span>:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[123</span>
<span class="Constant">456]</span>
  <span class="Constant">2</span>:address:editor-data<span class="Special"> &lt;- </span>new-editor <span class="Constant">1</span>:address:array:character, screen:address:screen, <span class="Constant">0/left</span>, <span class="Constant">10/right</span>
  editor-render screen, <span class="Constant">2</span>:address:editor-data
<span class="Constant">  $clear-trace</span>
  <span class="Comment"># start on second line (no newline after), press 'end'</span>
  assume-console [
    left-click <span class="Constant">2</span>, <span class="Constant">1</span>
    press end
  ]
  run [
    editor-event-loop screen:address:screen, console:address:console, <span class="Constant">2</span>:address:editor-data
    <span class="Constant">3</span>:number<span class="Special"> &lt;- </span>get *<span class="Constant">2</span>:address:editor-data, <span class="Constant">cursor-row:offset</span>
    <span class="Constant">4</span>:number<span class="Special"> &lt;- </span>get *<span class="Constant">2</span>:address:editor-data, <span class="Constant">cursor-column:offset</span>
  ]
  <span class="Comment"># cursor moves to end of line</span>
  memory-should-contain [
    <span class="Constant">3</span><span class="Special"> &lt;- </span><span class="Constant">2</span>
    <span class="Constant">4</span><span class="Special"> &lt;- </span><span class="Constant">3</span>
  ]
  check-trace-count-for-label <span class="Constant">0</span>, <span class="Constant">[print-character]</span>
]

<span class="Comment"># ctrl-u - delete text from start of line until (but not at) cursor</span>

<span class="muScenario">scenario</span> editor-deletes-to-start-of-line-with-ctrl-u [
  assume-screen <span class="Constant">10/width</span>, <span class="Constant">5/height</span>
  <span class="Constant">1</span>:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[123</span>
<span class="Constant">456]</span>
  <span class="Constant">2</span>:address:editor-data<span class="Special"> &lt;- </span>new-editor <span class="Constant">1</span>:address:array:character, screen:address:screen, <span class="Constant">0/left</span>, <span class="Constant">10/right</span>
  <span class="Comment"># start on second line, press ctrl-u</span>
  assume-console [
    left-click <span class="Constant">2</span>, <span class="Constant">2</span>
    press ctrl-u
  ]
  run [
    editor-event-loop screen:address:screen, console:address:console, <span class="Constant">2</span>:address:editor-data
  ]
  <span class="Comment"># cursor deletes to start of line</span>
  screen-should-contain [
   <span class="Constant"> .          .</span>
   <span class="Constant"> .123       .</span>
   <span class="Constant"> .6         .</span>
<span class="Constant">    .┈┈┈┈┈┈┈┈┈┈.</span>
   <span class="Constant"> .          .</span>
  ]
]

<span class="muRecipe">after</span> <span class="Constant">&lt;handle-special-character&gt;</span> [
  <span class="Delimiter">{</span>
    delete-to-start-of-line?:boolean<span class="Special"> &lt;- </span>equal *c, <span class="Constant">21/ctrl-u</span>
    <span class="muControl">break-unless</span> delete-to-start-of-line?
<span class="Constant">    &lt;delete-to-start-of-line-begin&gt;</span>
    deleted-cells:address:duplex-list:character<span class="Special"> &lt;- </span>delete-to-start-of-line editor
<span class="Constant">    &lt;delete-to-start-of-line-end&gt;</span>
    go-render?<span class="Special"> &lt;- </span>copy <span class="Constant">1/true</span>
    <span class="muControl">reply</span>
  <span class="Delimiter">}</span>
]

<span class="muRecipe">recipe</span> delete-to-start-of-line editor:address:editor-data<span class="muRecipe"> -&gt; </span>result:address:duplex-list:character [
  <span class="Constant">local-scope</span>
  <span class="Constant">load-ingredients</span>
  <span class="Comment"># compute range to delete</span>
  init:address:duplex-list:character<span class="Special"> &lt;- </span>get *editor, <span class="Constant">data:offset</span>
  before-cursor:address:address:duplex-list:character<span class="Special"> &lt;- </span>get-address *editor, <span class="Constant">before-cursor:offset</span>
  start:address:duplex-list:character<span class="Special"> &lt;- </span>copy *before-cursor
  end:address:duplex-list:character<span class="Special"> &lt;- </span>next-duplex *before-cursor
  <span class="Delimiter">{</span>
    at-start-of-text?:boolean<span class="Special"> &lt;- </span>equal start, init
    <span class="muControl">break-if</span> at-start-of-text?
    curr:character<span class="Special"> &lt;- </span>get *start, <span class="Constant">value:offset</span>
    at-start-of-line?:boolean<span class="Special"> &lt;- </span>equal curr, <span class="Constant">10/newline</span>
    <span class="muControl">break-if</span> at-start-of-line?
    start<span class="Special"> &lt;- </span>prev-duplex start
    assert start, <span class="Constant">[delete-to-start-of-line tried to move before start of text]</span>
    <span class="muControl">loop</span>
  <span class="Delimiter">}</span>
  <span class="Comment"># snip it out</span>
  result:address:duplex-list:character<span class="Special"> &lt;- </span>next-duplex start
  remove-duplex-between start, end
  <span class="Comment"># adjust cursor</span>
  *before-cursor<span class="Special"> &lt;- </span>copy start
  left:number<span class="Special"> &lt;- </span>get *editor, <span class="Constant">left:offset</span>
  cursor-column:address:number<span class="Special"> &lt;- </span>get-address *editor, <span class="Constant">cursor-column:offset</span>
  *cursor-column<span class="Special"> &lt;- </span>copy left
]

<span class="muScenario">scenario</span> editor-deletes-to-start-of-line-with-ctrl-u-2 [
  assume-screen <span class="Constant">10/width</span>, <span class="Constant">5/height</span>
  <span class="Constant">1</span>:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[123</span>
<span class="Constant">456]</span>
  <span class="Constant">2</span>:address:editor-data<span class="Special"> &lt;- </span>new-editor <span class="Constant">1</span>:address:array:character, screen:address:screen, <span class="Constant">0/left</span>, <span class="Constant">10/right</span>
  <span class="Comment"># start on first line (no newline before), press ctrl-u</span>
  assume-console [
    left-click <span class="Constant">1</span>, <span class="Constant">2</span>
    press ctrl-u
  ]
  run [
    editor-event-loop screen:address:screen, console:address:console, <span class="Constant">2</span>:address:editor-data
  ]
  <span class="Comment"># cursor deletes to start of line</span>
  screen-should-contain [
   <span class="Constant"> .          .</span>
   <span class="Constant"> .3         .</span>
   <span class="Constant"> .456       .</span>
<span class="Constant">    .┈┈┈┈┈┈┈┈┈┈.</span>
   <span class="Constant"> .          .</span>
  ]
]

<span class="muScenario">scenario</span> editor-deletes-to-start-of-line-with-ctrl-u-3 [
  assume-screen <span class="Constant">10/width</span>, <span class="Constant">5/height</span>
  <span class="Constant">1</span>:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[123</span>
<span class="Constant">456]</span>
  <span class="Constant">2</span>:address:editor-data<span class="Special"> &lt;- </span>new-editor <span class="Constant">1</span>:address:array:character, screen:address:screen, <span class="Constant">0/left</span>, <span class="Constant">10/right</span>
  <span class="Comment"># start past end of line, press ctrl-u</span>
  assume-console [
    left-click <span class="Constant">1</span>, <span class="Constant">3</span>
    press ctrl-u
  ]
  run [
    editor-event-loop screen:address:screen, console:address:console, <span class="Constant">2</span>:address:editor-data
  ]
  <span class="Comment"># cursor deletes to start of line</span>
  screen-should-contain [
   <span class="Constant"> .          .</span>
   <span class="Constant"> .          .</span>
   <span class="Constant"> .456       .</span>
<span class="Constant">    .┈┈┈┈┈┈┈┈┈┈.</span>
   <span class="Constant"> .          .</span>
  ]
]

<span class="muScenario">scenario</span> editor-deletes-to-start-of-final-line-with-ctrl-u [
  assume-screen <span class="Constant">10/width</span>, <span class="Constant">5/height</span>
  <span class="Constant">1</span>:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[123</span>
<span class="Constant">456]</span>
  <span class="Constant">2</span>:address:editor-data<span class="Special"> &lt;- </span>new-editor <span class="Constant">1</span>:address:array:character, screen:address:screen, <span class="Constant">0/left</span>, <span class="Constant">10/right</span>
  <span class="Comment"># start past end of final line, press ctrl-u</span>
  assume-console [
    left-click <span class="Constant">2</span>, <span class="Constant">3</span>
    press ctrl-u
  ]
  run [
    editor-event-loop screen:address:screen, console:address:console, <span class="Constant">2</span>:address:editor-data
  ]
  <span class="Comment"># cursor deletes to start of line</span>
  screen-should-contain [
   <span class="Constant"> .          .</span>
   <span class="Constant"> .123       .</span>
   <span class="Constant"> .          .</span>
<span class="Constant">    .┈┈┈┈┈┈┈┈┈┈.</span>
   <span class="Constant"> .          .</span>
  ]
]

<span class="Comment"># ctrl-k - delete text from cursor to end of line (but not the newline)</span>

<span class="muScenario">scenario</span> editor-deletes-to-end-of-line-with-ctrl-k [
  assume-screen <span class="Constant">10/width</span>, <span class="Constant">5/height</span>
  <span class="Constant">1</span>:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[123</span>
<span class="Constant">456]</span>
  <span class="Constant">2</span>:address:editor-data<span class="Special"> &lt;- </span>new-editor <span class="Constant">1</span>:address:array:character, screen:address:screen, <span class="Constant">0/left</span>, <span class="Constant">10/right</span>
  <span class="Comment"># start on first line, press ctrl-k</span>
  assume-console [
    left-click <span class="Constant">1</span>, <span class="Constant">1</span>
    press ctrl-k
  ]
  run [
    editor-event-loop screen:address:screen, console:address:console, <span class="Constant">2</span>:address:editor-data
  ]
  <span class="Comment"># cursor deletes to end of line</span>
  screen-should-contain [
   <span class="Constant"> .          .</span>
   <span class="Constant"> .1         .</span>
   <span class="Constant"> .456       .</span>
<span class="Constant">    .┈┈┈┈┈┈┈┈┈┈.</span>
   <span class="Constant"> .          .</span>
  ]
]

<span class="muRecipe">after</span> <span class="Constant">&lt;handle-special-character&gt;</span> [
  <span class="Delimiter">{</span>
    delete-to-end-of-line?:boolean<span class="Special"> &lt;- </span>equal *c, <span class="Constant">11/ctrl-k</span>
    <span class="muControl">break-unless</span> delete-to-end-of-line?
<span class="Constant">    &lt;delete-to-end-of-line-begin&gt;</span>
    deleted-cells:address:duplex-list:character<span class="Special"> &lt;- </span>delete-to-end-of-line editor
<span class="Constant">    &lt;delete-to-end-of-line-end&gt;</span>
    go-render?<span class="Special"> &lt;- </span>copy <span class="Constant">1/true</span>
    <span class="muControl">reply</span>
  <span class="Delimiter">}</span>
]

<span class="muRecipe">recipe</span> delete-to-end-of-line editor:address:editor-data<span class="muRecipe"> -&gt; </span>result:address:duplex-list:character [
  <span class="Constant">local-scope</span>
  <span class="Constant">load-ingredients</span>
  <span class="Comment"># compute range to delete</span>
  start:address:duplex-list:character<span class="Special"> &lt;- </span>get *editor, <span class="Constant">before-cursor:offset</span>
  end:address:duplex-list:character<span class="Special"> &lt;- </span>next-duplex start
  <span class="Delimiter">{</span>
    at-end-of-text?:boolean<span class="Special"> &lt;- </span>equal end, <span class="Constant">0/null</span>
    <span class="muControl">break-if</span> at-end-of-text?
    curr:character<span class="Special"> &lt;- </span>get *end, <span class="Constant">value:offset</span>
    at-end-of-line?:boolean<span class="Special"> &lt;- </span>equal curr, <span class="Constant">10/newline</span>
    <span class="muControl">break-if</span> at-end-of-line?
    end<span class="Special"> &lt;- </span>next-duplex end
    <span class="muControl">loop</span>
  <span class="Delimiter">}</span>
  <span class="Comment"># snip it out</span>
  result<span class="Special"> &lt;- </span>next-duplex start
  remove-duplex-between start, end
]

<span class="muScenario">scenario</span> editor-deletes-to-end-of-line-with-ctrl-k-2 [
  assume-screen <span class="Constant">10/width</span>, <span class="Constant">5/height</span>
  <span class="Constant">1</span>:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[123</span>
<span class="Constant">456]</span>
  <span class="Constant">2</span>:address:editor-data<span class="Special"> &lt;- </span>new-editor <span class="Constant">1</span>:address:array:character, screen:address:screen, <span class="Constant">0/left</span>, <span class="Constant">10/right</span>
  <span class="Comment"># start on second line (no newline after), press ctrl-k</span>
  assume-console [
    left-click <span class="Constant">2</span>, <span class="Constant">1</span>
    press ctrl-k
  ]
  run [
    editor-event-loop screen:address:screen, console:address:console, <span class="Constant">2</span>:address:editor-data
  ]
  <span class="Comment"># cursor deletes to end of line</span>
  screen-should-contain [
   <span class="Constant"> .          .</span>
   <span class="Constant"> .123       .</span>
   <span class="Constant"> .4         .</span>
<span class="Constant">    .┈┈┈┈┈┈┈┈┈┈.</span>
   <span class="Constant"> .          .</span>
  ]
]

<span class="muScenario">scenario</span> editor-deletes-to-end-of-line-with-ctrl-k-3 [
  assume-screen <span class="Constant">10/width</span>, <span class="Constant">5/height</span>
  <span class="Constant">1</span>:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[123</span>
<span class="Constant">456]</span>
  <span class="Constant">2</span>:address:editor-data<span class="Special"> &lt;- </span>new-editor <span class="Constant">1</span>:address:array:character, screen:address:screen, <span class="Constant">0/left</span>, <span class="Constant">10/right</span>
  <span class="Comment"># start at end of line</span>
  assume-console [
    left-click <span class="Constant">1</span>, <span class="Constant">2</span>
    press ctrl-k
  ]
  run [
    editor-event-loop screen:address:screen, console:address:console, <span class="Constant">2</span>:address:editor-data
  ]
  <span class="Comment"># cursor deletes just last character</span>
  screen-should-contain [
   <span class="Constant"> .          .</span>
   <span class="Constant"> .12        .</span>
   <span class="Constant"> .456       .</span>
<span class="Constant">    .┈┈┈┈┈┈┈┈┈┈.</span>
   <span class="Constant"> .          .</span>
  ]
]

<span class="muScenario">scenario</span> editor-deletes-to-end-of-line-with-ctrl-k-4 [
  assume-screen <span class="Constant">10/width</span>, <span class="Constant">5/height</span>
  <span class="Constant">1</span>:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[123</span>
<span class="Constant">456]</span>
  <span class="Constant">2</span>:address:editor-data<span class="Special"> &lt;- </span>new-editor <span class="Constant">1</span>:address:array:character, screen:address:screen, <span class="Constant">0/left</span>, <span class="Constant">10/right</span>
  <span class="Comment"># start past end of line</span>
  assume-console [
    left-click <span class="Constant">1</span>, <span class="Constant">3</span>
    press ctrl-k
  ]
  run [
    editor-event-loop screen:address:screen, console:address:console, <span class="Constant">2</span>:address:editor-data
  ]
  <span class="Comment"># cursor deletes nothing</span>
  screen-should-contain [
   <span class="Constant"> .          .</span>
   <span class="Constant"> .123       .</span>
   <span class="Constant"> .456       .</span>
<span class="Constant">    .┈┈┈┈┈┈┈┈┈┈.</span>
   <span class="Constant"> .          .</span>
  ]
]

<span class="muScenario">scenario</span> editor-deletes-to-end-of-line-with-ctrl-k-5 [
  assume-screen <span class="Constant">10/width</span>, <span class="Constant">5/height</span>
  <span class="Constant">1</span>:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[123</span>
<span class="Constant">456]</span>
  <span class="Constant">2</span>:address:editor-data<span class="Special"> &lt;- </span>new-editor <span class="Constant">1</span>:address:array:character, screen:address:screen, <span class="Constant">0/left</span>, <span class="Constant">10/right</span>
  <span class="Comment"># start at end of text</span>
  assume-console [
    left-click <span class="Constant">2</span>, <span class="Constant">2</span>
    press ctrl-k
  ]
  run [
    editor-event-loop screen:address:screen, console:address:console, <span class="Constant">2</span>:address:editor-data
  ]
  <span class="Comment"># cursor deletes just the final character</span>
  screen-should-contain [
   <span class="Constant"> .          .</span>
   <span class="Constant"> .123       .</span>
   <span class="Constant"> .45        .</span>
<span class="Constant">    .┈┈┈┈┈┈┈┈┈┈.</span>
   <span class="Constant"> .          .</span>
  ]
]

<span class="muScenario">scenario</span> editor-deletes-to-end-of-line-with-ctrl-k-6 [
  assume-screen <span class="Constant">10/width</span>, <span class="Constant">5/height</span>
  <span class="Constant">1</span>:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[123</span>
<span class="Constant">456]</span>
  <span class="Constant">2</span>:address:editor-data<span class="Special"> &lt;- </span>new-editor <span class="Constant">1</span>:address:array:character, screen:address:screen, <span class="Constant">0/left</span>, <span class="Constant">10/right</span>
  <span class="Comment"># start past end of text</span>
  assume-console [
    left-click <span class="Constant">2</span>, <span class="Constant">3</span>
    press ctrl-k
  ]
  run [
    editor-event-loop screen:address:screen, console:address:console, <span class="Constant">2</span>:address:editor-data
  ]
  <span class="Comment"># cursor deletes nothing</span>
  screen-should-contain [
   <span class="Constant"> .          .</span>
   <span class="Constant"> .123       .</span>
   <span class="Constant"> .456       .</span>
<span class="Constant">    .┈┈┈┈┈┈┈┈┈┈.</span>
   <span class="Constant"> .          .</span>
  ]
]

<span class="Comment"># cursor-down can scroll if necessary</span>

<span class="muScenario">scenario</span> editor-can-scroll-down-using-arrow-keys [
  <span class="Comment"># screen has 1 line for menu + 3 lines</span>
  assume-screen <span class="Constant">10/width</span>, <span class="Constant">4/height</span>
  <span class="Comment"># initialize editor with &gt;3 lines</span>
  <span class="Constant">1</span>:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[a</span>
<span class="Constant">b</span>
<span class="Constant">c</span>
<span class="Constant">d]</span>
  <span class="Constant">2</span>:address:editor-data<span class="Special"> &lt;- </span>new-editor <span class="Constant">1</span>:address:array:character, screen:address:screen, <span class="Constant">0/left</span>, <span class="Constant">10/right</span>
  screen-should-contain [
   <span class="Constant"> .          .</span>
   <span class="Constant"> .a         .</span>
   <span class="Constant"> .b         .</span>
   <span class="Constant"> .c         .</span>
  ]
  <span class="Comment"># position cursor at last line, then try to move further down</span>
  assume-console [
    left-click <span class="Constant">3</span>, <span class="Constant">0</span>
    press down-arrow
  ]
  run [
    editor-event-loop screen:address:screen, console:address:console, <span class="Constant">2</span>:address:editor-data
  ]
  <span class="Comment"># screen slides by one line</span>
  screen-should-contain [
   <span class="Constant"> .          .</span>
   <span class="Constant"> .b         .</span>
   <span class="Constant"> .c         .</span>
   <span class="Constant"> .d         .</span>
  ]
]

<span class="muRecipe">after</span> <span class="Constant">&lt;scroll-down&gt;</span> [
  trace <span class="Constant">10</span>, <span class="Constant">[app]</span>, <span class="Constant">[scroll down]</span>
  top-of-screen:address:address:duplex-list:character<span class="Special"> &lt;- </span>get-address *editor, <span class="Constant">top-of-screen:offset</span>
  left:number<span class="Special"> &lt;- </span>get *editor, <span class="Constant">left:offset</span>
  right:number<span class="Special"> &lt;- </span>get *editor, <span class="Constant">right:offset</span>
  max:number<span class="Special"> &lt;- </span>subtract right, left
  old-top:address:duplex-list:character<span class="Special"> &lt;- </span>copy *top-of-screen
  *top-of-screen<span class="Special"> &lt;- </span>before-start-of-next-line *top-of-screen, max
  no-movement?:boolean<span class="Special"> &lt;- </span>equal old-top, *top-of-screen
  go-render?<span class="Special"> &lt;- </span>copy <span class="Constant">0/false</span>
  <span class="muControl">reply-if</span> no-movement?
]

<span class="Comment"># takes a pointer into the doubly-linked list, scans ahead at most 'max'</span>
<span class="Comment"># positions until the next newline</span>
<span class="Comment"># beware: never return null pointer.</span>
<span class="muRecipe">recipe</span> before-start-of-next-line original:address:duplex-list:character, max:number<span class="muRecipe"> -&gt; </span>curr:address:duplex-list:character [
  <span class="Constant">local-scope</span>
  <span class="Constant">load-ingredients</span>
  count:number<span class="Special"> &lt;- </span>copy <span class="Constant">0</span>
  curr:address:duplex-list:character<span class="Special"> &lt;- </span>copy original
  <span class="Comment"># skip the initial newline if it exists</span>
  <span class="Delimiter">{</span>
    c:character<span class="Special"> &lt;- </span>get *curr, <span class="Constant">value:offset</span>
    at-newline?:boolean<span class="Special"> &lt;- </span>equal c, <span class="Constant">10/newline</span>
    <span class="muControl">break-unless</span> at-newline?
    curr<span class="Special"> &lt;- </span>next-duplex curr
    count<span class="Special"> &lt;- </span>add count, <span class="Constant">1</span>
  <span class="Delimiter">}</span>
  <span class="Delimiter">{</span>
    <span class="muControl">reply-unless</span> curr, original
    done?:boolean<span class="Special"> &lt;- </span>greater-or-equal count, max
    <span class="muControl">break-if</span> done?
    c:character<span class="Special"> &lt;- </span>get *curr, <span class="Constant">value:offset</span>
    at-newline?:boolean<span class="Special"> &lt;- </span>equal c, <span class="Constant">10/newline</span>
    <span class="muControl">break-if</span> at-newline?
    curr<span class="Special"> &lt;- </span>next-duplex curr
    count<span class="Special"> &lt;- </span>add count, <span class="Constant">1</span>
    <span class="muControl">loop</span>
  <span class="Delimiter">}</span>
  <span class="muControl">reply-unless</span> curr, original
  <span class="muControl">reply</span> curr
]

<span class="muScenario">scenario</span> editor-scrolls-down-past-wrapped-line-using-arrow-keys [
  <span class="Comment"># screen has 1 line for menu + 3 lines</span>
  assume-screen <span class="Constant">10/width</span>, <span class="Constant">4/height</span>
  <span class="Comment"># initialize editor with a long, wrapped line and more than a screen of</span>
  <span class="Comment"># other lines</span>
  <span class="Constant">1</span>:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[abcdef</span>
<span class="Constant">g</span>
<span class="Constant">h</span>
<span class="Constant">i]</span>
  <span class="Constant">2</span>:address:editor-data<span class="Special"> &lt;- </span>new-editor <span class="Constant">1</span>:address:array:character, screen:address:screen, <span class="Constant">0/left</span>, <span class="Constant">5/right</span>
  screen-should-contain [
   <span class="Constant"> .          .</span>
   <span class="Constant"> .abcd↩     .</span>
   <span class="Constant"> .ef        .</span>
   <span class="Constant"> .g         .</span>
  ]
  <span class="Comment"># position cursor at last line, then try to move further down</span>
  assume-console [
    left-click <span class="Constant">3</span>, <span class="Constant">0</span>
    press down-arrow
  ]
  run [
    editor-event-loop screen:address:screen, console:address:console, <span class="Constant">2</span>:address:editor-data
  ]
  <span class="Comment"># screen shows partial wrapped line</span>
  screen-should-contain [
   <span class="Constant"> .          .</span>
   <span class="Constant"> .ef        .</span>
   <span class="Constant"> .g         .</span>
   <span class="Constant"> .h         .</span>
  ]
]

<span class="muScenario">scenario</span> editor-scrolls-down-past-wrapped-line-using-arrow-keys-2 [
  <span class="Comment"># screen has 1 line for menu + 3 lines</span>
  assume-screen <span class="Constant">10/width</span>, <span class="Constant">4/height</span>
  <span class="Comment"># editor starts with a long line wrapping twice</span>
  <span class="Constant">1</span>:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[abcdefghij</span>
<span class="Constant">k</span>
<span class="Constant">l</span>
<span class="Constant">m]</span>
  <span class="Constant">2</span>:address:editor-data<span class="Special"> &lt;- </span>new-editor <span class="Constant">1</span>:address:array:character, screen:address:screen, <span class="Constant">0/left</span>, <span class="Constant">5/right</span>
  <span class="Comment"># position cursor at last line, then try to move further down</span>
  assume-console [
    left-click <span class="Constant">3</span>, <span class="Constant">0</span>
    press down-arrow
  ]
  run [
    editor-event-loop screen:address:screen, console:address:console, <span class="Constant">2</span>:address:editor-data
  ]
  <span class="Comment"># screen shows partial wrapped line containing a wrap icon</span>
  screen-should-contain [
   <span class="Constant"> .          .</span>
   <span class="Constant"> .efgh↩     .</span>
   <span class="Constant"> .ij        .</span>
   <span class="Constant"> .k         .</span>
  ]
  <span class="Comment"># scroll down again</span>
  assume-console [
    press down-arrow
  ]
  run [
    editor-event-loop screen:address:screen, console:address:console, <span class="Constant">2</span>:address:editor-data
  ]
  <span class="Comment"># screen shows partial wrapped line</span>
  screen-should-contain [
   <span class="Constant"> .          .</span>
   <span class="Constant"> .ij        .</span>
   <span class="Constant"> .k         .</span>
   <span class="Constant"> .l         .</span>
  ]
]

<span class="muScenario">scenario</span> editor-scrolls-down-when-line-wraps [
  <span class="Comment"># screen has 1 line for menu + 3 lines</span>
  assume-screen <span class="Constant">5/width</span>, <span class="Constant">4/height</span>
  <span class="Comment"># editor contains a long line in the third line</span>
  <span class="Constant">1</span>:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[a</span>
<span class="Constant">b</span>
<span class="Constant">cdef]</span>
  <span class="Constant">2</span>:address:editor-data<span class="Special"> &lt;- </span>new-editor <span class="Constant">1</span>:address:array:character, screen:address:screen, <span class="Constant">0/left</span>, <span class="Constant">5/right</span>
  <span class="Comment"># position cursor at end, type a character</span>
  assume-console [
    left-click <span class="Constant">3</span>, <span class="Constant">4</span>
    type <span class="Constant">[g]</span>
  ]
  run [
    editor-event-loop screen:address:screen, console:address:console, <span class="Constant">2</span>:address:editor-data
    <span class="Constant">3</span>:number<span class="Special"> &lt;- </span>get *<span class="Constant">2</span>:address:editor-data, <span class="Constant">cursor-row:offset</span>
    <span class="Constant">4</span>:number<span class="Special"> &lt;- </span>get *<span class="Constant">2</span>:address:editor-data, <span class="Constant">cursor-column:offset</span>
  ]
  <span class="Comment"># screen scrolls</span>
  screen-should-contain [
   <span class="Constant"> .     .</span>
   <span class="Constant"> .b    .</span>
<span class="Constant">    .cdef↩.</span>
   <span class="Constant"> .g    .</span>
  ]
  memory-should-contain [
    <span class="Constant">3</span><span class="Special"> &lt;- </span><span class="Constant">3</span>
    <span class="Constant">4</span><span class="Special"> &lt;- </span><span class="Constant">1</span>
  ]
]

<span class="muScenario">scenario</span> editor-scrolls-down-on-newline [
  assume-screen <span class="Constant">5/width</span>, <span class="Constant">4/height</span>
  <span class="Comment"># position cursor after last line and type newline</span>
  <span class="Constant">1</span>:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[a</span>
<span class="Constant">b</span>
<span class="Constant">c]</span>
  <span class="Constant">2</span>:address:editor-data<span class="Special"> &lt;- </span>new-editor <span class="Constant">1</span>:address:array:character, screen:address:screen, <span class="Constant">0/left</span>, <span class="Constant">5/right</span>
  assume-console [
    left-click <span class="Constant">3</span>, <span class="Constant">4</span>
    type [
]
  ]
  run [
    editor-event-loop screen:address:screen, console:address:console, <span class="Constant">2</span>:address:editor-data
    <span class="Constant">3</span>:number<span class="Special"> &lt;- </span>get *<span class="Constant">2</span>:address:editor-data, <span class="Constant">cursor-row:offset</span>
    <span class="Constant">4</span>:number<span class="Special"> &lt;- </span>get *<span class="Constant">2</span>:address:editor-data, <span class="Constant">cursor-column:offset</span>
  ]
  <span class="Comment"># screen scrolls</span>
  screen-should-contain [
   <span class="Constant"> .     .</span>
   <span class="Constant"> .b    .</span>
   <span class="Constant"> .c    .</span>
   <span class="Constant"> .     .</span>
  ]
  memory-should-contain [
    <span class="Constant">3</span><span class="Special"> &lt;- </span><span class="Constant">3</span>
    <span class="Constant">4</span><span class="Special"> &lt;- </span><span class="Constant">0</span>
  ]
]

<span class="muScenario">scenario</span> editor-scrolls-down-on-right-arrow [
  <span class="Comment"># screen has 1 line for menu + 3 lines</span>
  assume-screen <span class="Constant">5/width</span>, <span class="Constant">4/height</span>
  <span class="Comment"># editor contains a wrapped line</span>
  <span class="Constant">1</span>:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[a</span>
<span class="Constant">b</span>
<span class="Constant">cdefgh]</span>
  <span class="Constant">2</span>:address:editor-data<span class="Special"> &lt;- </span>new-editor <span class="Constant">1</span>:address:array:character, screen:address:screen, <span class="Constant">0/left</span>, <span class="Constant">5/right</span>
  <span class="Comment"># position cursor at end of screen and try to move right</span>
  assume-console [
    left-click <span class="Constant">3</span>, <span class="Constant">3</span>
    press right-arrow
  ]
  run [
    editor-event-loop screen:address:screen, console:address:console, <span class="Constant">2</span>:address:editor-data
    <span class="Constant">3</span>:number<span class="Special"> &lt;- </span>get *<span class="Constant">2</span>:address:editor-data, <span class="Constant">cursor-row:offset</span>
    <span class="Constant">4</span>:number<span class="Special"> &lt;- </span>get *<span class="Constant">2</span>:address:editor-data, <span class="Constant">cursor-column:offset</span>
  ]
  <span class="Comment"># screen scrolls</span>
  screen-should-contain [
   <span class="Constant"> .     .</span>
   <span class="Constant"> .b    .</span>
<span class="Constant">    .cdef↩.</span>
   <span class="Constant"> .gh   .</span>
  ]
  memory-should-contain [
    <span class="Constant">3</span><span class="Special"> &lt;- </span><span class="Constant">3</span>
    <span class="Constant">4</span><span class="Special"> &lt;- </span><span class="Constant">0</span>
  ]
]

<span class="muScenario">scenario</span> editor-scrolls-down-on-right-arrow-2 [
  <span class="Comment"># screen has 1 line for menu + 3 lines</span>
  assume-screen <span class="Constant">5/width</span>, <span class="Constant">4/height</span>
  <span class="Comment"># editor contains more lines than can fit on screen</span>
  <span class="Constant">1</span>:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[a</span>
<span class="Constant">b</span>
<span class="Constant">c</span>
<span class="Constant">d]</span>
  <span class="Constant">2</span>:address:editor-data<span class="Special"> &lt;- </span>new-editor <span class="Constant">1</span>:address:array:character, screen:address:screen, <span class="Constant">0/left</span>, <span class="Constant">5/right</span>
  <span class="Comment"># position cursor at end of screen and try to move right</span>
  assume-console [
    left-click <span class="Constant">3</span>, <span class="Constant">3</span>
    press right-arrow
  ]
  run [
    editor-event-loop screen:address:screen, console:address:console, <span class="Constant">2</span>:address:editor-data
    <span class="Constant">3</span>:number<span class="Special"> &lt;- </span>get *<span class="Constant">2</span>:address:editor-data, <span class="Constant">cursor-row:offset</span>
    <span class="Constant">4</span>:number<span class="Special"> &lt;- </span>get *<span class="Constant">2</span>:address:editor-data, <span class="Constant">cursor-column:offset</span>
  ]
  <span class="Comment"># screen scrolls</span>
  screen-should-contain [
   <span class="Constant"> .     .</span>
   <span class="Constant"> .b    .</span>
   <span class="Constant"> .c    .</span>
   <span class="Constant"> .d    .</span>
  ]
  memory-should-contain [
    <span class="Constant">3</span><span class="Special"> &lt;- </span><span class="Constant">3</span>
    <span class="Constant">4</span><span class="Special"> &lt;- </span><span class="Constant">0</span>
  ]
]

<span class="muScenario">scenario</span> editor-scrolls-at-end-on-down-arrow [
  assume-screen <span class="Constant">10/width</span>, <span class="Constant">5/height</span>
  <span class="Constant">1</span>:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[abc</span>
<span class="Constant">de]</span>
  <span class="Constant">2</span>:address:editor-data<span class="Special"> &lt;- </span>new-editor <span class="Constant">1</span>:address:array:character, screen:address:screen, <span class="Constant">0/left</span>, <span class="Constant">10/right</span>
  editor-render screen, <span class="Constant">2</span>:address:editor-data
<span class="Constant">  $clear-trace</span>
  <span class="Comment"># try to move down past end of text</span>
  assume-console [
    left-click <span class="Constant">2</span>, <span class="Constant">0</span>
    press down-arrow
  ]
  run [
    editor-event-loop screen:address:screen, console:address:console, <span class="Constant">2</span>:address:editor-data
    <span class="Constant">3</span>:number<span class="Special"> &lt;- </span>get *<span class="Constant">2</span>:address:editor-data, <span class="Constant">cursor-row:offset</span>
    <span class="Constant">4</span>:number<span class="Special"> &lt;- </span>get *<span class="Constant">2</span>:address:editor-data, <span class="Constant">cursor-column:offset</span>
  ]
  <span class="Comment"># screen should scroll, moving cursor to end of text</span>
  memory-should-contain [
    <span class="Constant">3</span><span class="Special"> &lt;- </span><span class="Constant">1</span>
    <span class="Constant">4</span><span class="Special"> &lt;- </span><span class="Constant">2</span>
  ]
  assume-console [
    type <span class="Constant">[0]</span>
  ]
  run [
    editor-event-loop screen:address:screen, console:address:console, <span class="Constant">2</span>:address:editor-data
  ]
  screen-should-contain [
   <span class="Constant"> .          .</span>
   <span class="Constant"> .de0       .</span>
<span class="Constant">    .┈┈┈┈┈┈┈┈┈┈.</span>
   <span class="Constant"> .          .</span>
  ]
  <span class="Comment"># try to move down again</span>
<span class="Constant">  $clear-trace</span>
  assume-console [
    left-click <span class="Constant">2</span>, <span class="Constant">0</span>
    press down-arrow
  ]
  run [
    editor-event-loop screen:address:screen, console:address:console, <span class="Constant">2</span>:address:editor-data
    <span class="Constant">3</span>:number<span class="Special"> &lt;- </span>get *<span class="Constant">2</span>:address:editor-data, <span class="Constant">cursor-row:offset</span>
    <span class="Constant">4</span>:number<span class="Special"> &lt;- </span>get *<span class="Constant">2</span>:address:editor-data, <span class="Constant">cursor-column:offset</span>
  ]
  <span class="Comment"># screen stops scrolling because cursor is already at top</span>
  memory-should-contain [
    <span class="Constant">3</span><span class="Special"> &lt;- </span><span class="Constant">1</span>
    <span class="Constant">4</span><span class="Special"> &lt;- </span><span class="Constant">3</span>
  ]
  check-trace-count-for-label <span class="Constant">0</span>, <span class="Constant">[print-character]</span>
  assume-console [
    type <span class="Constant">[1]</span>
  ]
  run [
    editor-event-loop screen:address:screen, console:address:console, <span class="Constant">2</span>:address:editor-data
  ]
  screen-should-contain [
   <span class="Constant"> .          .</span>
   <span class="Constant"> .de01      .</span>
<span class="Constant">    .┈┈┈┈┈┈┈┈┈┈.</span>
   <span class="Constant"> .          .</span>
  ]
]

<span class="muScenario">scenario</span> editor-combines-page-and-line-scroll [
  <span class="Comment"># screen has 1 line for menu + 3 lines</span>
  assume-screen <span class="Constant">10/width</span>, <span class="Constant">4/height</span>
  <span class="Comment"># initialize editor with a few pages of lines</span>
  <span class="Constant">1</span>:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[a</span>
<span class="Constant">b</span>
<span class="Constant">c</span>
<span class="Constant">d</span>
<span class="Constant">e</span>
<span class="Constant">f</span>
<span class="Constant">g]</span>
  <span class="Constant">2</span>:address:editor-data<span class="Special"> &lt;- </span>new-editor <span class="Constant">1</span>:address:array:character, screen:address:screen, <span class="Constant">0/left</span>, <span class="Constant">5/right</span>
  <span class="Comment"># scroll down one page and one line</span>
  assume-console [
    press page-down
    left-click <span class="Constant">3</span>, <span class="Constant">0</span>
    press down-arrow
  ]
  run [
    editor-event-loop screen:address:screen, console:address:console, <span class="Constant">2</span>:address:editor-data
  ]
  <span class="Comment"># screen scrolls down 3 lines</span>
  screen-should-contain [
   <span class="Constant"> .          .</span>
   <span class="Constant"> .d         .</span>
   <span class="Constant"> .e         .</span>
   <span class="Constant"> .f         .</span>
  ]
]

<span class="Comment"># cursor-up can scroll if necessary</span>

<span class="muScenario">scenario</span> editor-can-scroll-up-using-arrow-keys [
  <span class="Comment"># screen has 1 line for menu + 3 lines</span>
  assume-screen <span class="Constant">10/width</span>, <span class="Constant">4/height</span>
  <span class="Comment"># initialize editor with &gt;3 lines</span>
  <span class="Constant">1</span>:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[a</span>
<span class="Constant">b</span>
<span class="Constant">c</span>
<span class="Constant">d]</span>
  <span class="Constant">2</span>:address:editor-data<span class="Special"> &lt;- </span>new-editor <span class="Constant">1</span>:address:array:character, screen:address:screen, <span class="Constant">0/left</span>, <span class="Constant">10/right</span>
  screen-should-contain [
   <span class="Constant"> .          .</span>
   <span class="Constant"> .a         .</span>
   <span class="Constant"> .b         .</span>
   <span class="Constant"> .c         .</span>
  ]
  <span class="Comment"># position cursor at top of second page, then try to move up</span>
  assume-console [
    press page-down
    press up-arrow
  ]
  run [
    editor-event-loop screen:address:screen, console:address:console, <span class="Constant">2</span>:address:editor-data
  ]
  <span class="Comment"># screen slides by one line</span>
  screen-should-contain [
   <span class="Constant"> .          .</span>
   <span class="Constant"> .b         .</span>
   <span class="Constant"> .c         .</span>
   <span class="Constant"> .d         .</span>
  ]
]

<span class="muRecipe">after</span> <span class="Constant">&lt;scroll-up&gt;</span> [
  trace <span class="Constant">10</span>, <span class="Constant">[app]</span>, <span class="Constant">[scroll up]</span>
  top-of-screen:address:address:duplex-list:character<span class="Special"> &lt;- </span>get-address *editor, <span class="Constant">top-of-screen:offset</span>
  old-top:address:duplex-list:character<span class="Special"> &lt;- </span>copy *top-of-screen
  *top-of-screen<span class="Special"> &lt;- </span>before-previous-line *top-of-screen, editor
  no-movement?:boolean<span class="Special"> &lt;- </span>equal old-top, *top-of-screen
  go-render?<span class="Special"> &lt;- </span>copy <span class="Constant">0/false</span>
  <span class="muControl">reply-if</span> no-movement?
]

<span class="Comment"># takes a pointer into the doubly-linked list, scans back to before start of</span>
<span class="Comment"># previous *wrapped* line</span>
<span class="Comment"># beware: never return null pointer</span>
<span class="muRecipe">recipe</span> before-previous-line curr:address:duplex-list:character, editor:address:editor-data<span class="muRecipe"> -&gt; </span>curr:address:duplex-list:character [
  <span class="Constant">local-scope</span>
  <span class="Constant">load-ingredients</span>
  c:character<span class="Special"> &lt;- </span>get *curr, <span class="Constant">value:offset</span>
  <span class="Comment"># compute max, number of characters to skip</span>
  <span class="Comment">#   1 + len%(width-1)</span>
  <span class="Comment">#   except rotate second term to vary from 1 to width-1 rather than 0 to width-2</span>
  left:number<span class="Special"> &lt;- </span>get *editor, <span class="Constant">left:offset</span>
  right:number<span class="Special"> &lt;- </span>get *editor, <span class="Constant">right:offset</span>
  max-line-length:number<span class="Special"> &lt;- </span>subtract right, left, <span class="Constant">-1/exclusive-right</span>, <span class="Constant">1/wrap-icon</span>
  sentinel:address:duplex-list:character<span class="Special"> &lt;- </span>get *editor, <span class="Constant">data:offset</span>
  len:number<span class="Special"> &lt;- </span>previous-line-length curr, sentinel
  <span class="Delimiter">{</span>
    <span class="muControl">break-if</span> len
    <span class="Comment"># empty line; just skip this newline</span>
    prev:address:duplex-list:character<span class="Special"> &lt;- </span>prev-duplex curr
    <span class="muControl">reply-unless</span> prev, curr
    <span class="muControl">reply</span> prev
  <span class="Delimiter">}</span>
  _, max:number<span class="Special"> &lt;- </span>divide-with-remainder len, max-line-length
  <span class="Comment"># remainder 0 =&gt; scan one width-worth</span>
  <span class="Delimiter">{</span>
    <span class="muControl">break-if</span> max
    max<span class="Special"> &lt;- </span>copy max-line-length
  <span class="Delimiter">}</span>
  max<span class="Special"> &lt;- </span>add max, <span class="Constant">1</span>
  count:number<span class="Special"> &lt;- </span>copy <span class="Constant">0</span>
  <span class="Comment"># skip 'max' characters</span>
  <span class="Delimiter">{</span>
    done?:boolean<span class="Special"> &lt;- </span>greater-or-equal count, max
    <span class="muControl">break-if</span> done?
    prev:address:duplex-list:character<span class="Special"> &lt;- </span>prev-duplex curr
    <span class="muControl">break-unless</span> prev
    curr<span class="Special"> &lt;- </span>copy prev
    count<span class="Special"> &lt;- </span>add count, <span class="Constant">1</span>
    <span class="muControl">loop</span>
  <span class="Delimiter">}</span>
  <span class="muControl">reply</span> curr
]

<span class="muScenario">scenario</span> editor-scrolls-up-past-wrapped-line-using-arrow-keys [
  <span class="Comment"># screen has 1 line for menu + 3 lines</span>
  assume-screen <span class="Constant">10/width</span>, <span class="Constant">4/height</span>
  <span class="Comment"># initialize editor with a long, wrapped line and more than a screen of</span>
  <span class="Comment"># other lines</span>
  <span class="Constant">1</span>:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[abcdef</span>
<span class="Constant">g</span>
<span class="Constant">h</span>
<span class="Constant">i]</span>
  <span class="Constant">2</span>:address:editor-data<span class="Special"> &lt;- </span>new-editor <span class="Constant">1</span>:address:array:character, screen:address:screen, <span class="Constant">0/left</span>, <span class="Constant">5/right</span>
  screen-should-contain [
   <span class="Constant"> .          .</span>
   <span class="Constant"> .abcd↩     .</span>
   <span class="Constant"> .ef        .</span>
   <span class="Constant"> .g         .</span>
  ]
  <span class="Comment"># position cursor at top of second page, just below wrapped line</span>
  assume-console [
    press page-down
  ]
  run [
    editor-event-loop screen:address:screen, console:address:console, <span class="Constant">2</span>:address:editor-data
  ]
  screen-should-contain [
   <span class="Constant"> .          .</span>
   <span class="Constant"> .g         .</span>
   <span class="Constant"> .h         .</span>
   <span class="Constant"> .i         .</span>
  ]
  <span class="Comment"># now move up one line</span>
  assume-console [
    press up-arrow
  ]
  run [
    editor-event-loop screen:address:screen, console:address:console, <span class="Constant">2</span>:address:editor-data
  ]
  <span class="Comment"># screen shows partial wrapped line</span>
  screen-should-contain [
   <span class="Constant"> .          .</span>
   <span class="Constant"> .ef        .</span>
   <span class="Constant"> .g         .</span>
   <span class="Constant"> .h         .</span>
  ]
]

<span class="muScenario">scenario</span> editor-scrolls-up-past-wrapped-line-using-arrow-keys-2 [
  <span class="Comment"># screen has 1 line for menu + 4 lines</span>
  assume-screen <span class="Constant">10/width</span>, <span class="Constant">5/height</span>
  <span class="Comment"># editor starts with a long line wrapping twice, occupying 3 of the 4 lines</span>
  <span class="Constant">1</span>:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[abcdefghij</span>
<span class="Constant">k</span>
<span class="Constant">l</span>
<span class="Constant">m]</span>
  <span class="Constant">2</span>:address:editor-data<span class="Special"> &lt;- </span>new-editor <span class="Constant">1</span>:address:array:character, screen:address:screen, <span class="Constant">0/left</span>, <span class="Constant">5/right</span>
  <span class="Comment"># position cursor at top of second page</span>
  assume-console [
    press page-down
  ]
  run [
    editor-event-loop screen:address:screen, console:address:console, <span class="Constant">2</span>:address:editor-data
  ]
  screen-should-contain [
   <span class="Constant"> .          .</span>
   <span class="Constant"> .k         .</span>
   <span class="Constant"> .l         .</span>
   <span class="Constant"> .m         .</span>
   <span class="Constant"> .┈┈┈┈┈     .</span>
  ]
  <span class="Comment"># move up one line</span>
  assume-console [
    press up-arrow
  ]
  run [
    editor-event-loop screen:address:screen, console:address:console, <span class="Constant">2</span>:address:editor-data
  ]
  <span class="Comment"># screen shows partial wrapped line</span>
  screen-should-contain [
   <span class="Constant"> .          .</span>
   <span class="Constant"> .ij        .</span>
   <span class="Constant"> .k         .</span>
   <span class="Constant"> .l         .</span>
   <span class="Constant"> .m         .</span>
  ]
  <span class="Comment"># move up a second line</span>
  assume-console [
    press up-arrow
  ]
  run [
    editor-event-loop screen:address:screen, console:address:console, <span class="Constant">2</span>:address:editor-data
  ]
  <span class="Comment"># screen shows partial wrapped line</span>
  screen-should-contain [
   <span class="Constant"> .          .</span>
   <span class="Constant"> .efgh↩     .</span>
   <span class="Constant"> .ij        .</span>
   <span class="Constant"> .k         .</span>
   <span class="Constant"> .l         .</span>
  ]
  <span class="Comment"># move up a third line</span>
  assume-console [
    press up-arrow
  ]
  run [
    editor-event-loop screen:address:screen, console:address:console, <span class="Constant">2</span>:address:editor-data
  ]
  <span class="Comment"># screen shows partial wrapped line</span>
  screen-should-contain [
   <span class="Constant"> .          .</span>
   <span class="Constant"> .abcd↩     .</span>
   <span class="Constant"> .efgh↩     .</span>
   <span class="Constant"> .ij        .</span>
   <span class="Constant"> .k         .</span>
  ]
]

<span class="Comment"># same as editor-scrolls-up-past-wrapped-line-using-arrow-keys but length</span>
<span class="Comment"># slightly off, just to prevent over-training</span>
<span class="muScenario">scenario</span> editor-scrolls-up-past-wrapped-line-using-arrow-keys-3 [
  <span class="Comment"># screen has 1 line for menu + 3 lines</span>
  assume-screen <span class="Constant">10/width</span>, <span class="Constant">4/height</span>
  <span class="Comment"># initialize editor with a long, wrapped line and more than a screen of</span>
  <span class="Comment"># other lines</span>
  <span class="Constant">1</span>:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[abcdef</span>
<span class="Constant">g</span>
<span class="Constant">h</span>
<span class="Constant">i]</span>
  <span class="Constant">2</span>:address:editor-data<span class="Special"> &lt;- </span>new-editor <span class="Constant">1</span>:address:array:character, screen:address:screen, <span class="Constant">0/left</span>, <span class="Constant">6/right</span>
  screen-should-contain [
   <span class="Constant"> .          .</span>
   <span class="Constant"> .abcde↩    .</span>
   <span class="Constant"> .f         .</span>
   <span class="Constant"> .g         .</span>
  ]
  <span class="Comment"># position cursor at top of second page, just below wrapped line</span>
  assume-console [
    press page-down
  ]
  run [
    editor-event-loop screen:address:screen, console:address:console, <span class="Constant">2</span>:address:editor-data
  ]
  screen-should-contain [
   <span class="Constant"> .          .</span>
   <span class="Constant"> .g         .</span>
   <span class="Constant"> .h         .</span>
   <span class="Constant"> .i         .</span>
  ]
  <span class="Comment"># now move up one line</span>
  assume-console [
    press up-arrow
  ]
  run [
    editor-event-loop screen:address:screen, console:address:console, <span class="Constant">2</span>:address:editor-data
  ]
  <span class="Comment"># screen shows partial wrapped line</span>
  screen-should-contain [
   <span class="Constant"> .          .</span>
   <span class="Constant"> .f         .</span>
   <span class="Constant"> .g         .</span>
   <span class="Constant"> .h         .</span>
  ]
]

<span class="Comment"># check empty lines</span>
<span class="muScenario">scenario</span> editor-scrolls-up-past-wrapped-line-using-arrow-keys-4 [
  assume-screen <span class="Constant">10/width</span>, <span class="Constant">4/height</span>
  <span class="Comment"># initialize editor with some lines around an empty line</span>
  <span class="Constant">1</span>:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[a</span>
<span class="Constant">b</span>

c
d
e]
  <span class="Constant">2</span>:address:editor-data<span class="Special"> &lt;- </span>new-editor <span class="Constant">1</span>:address:array:character, screen:address:screen, <span class="Constant">0/left</span>, <span class="Constant">6/right</span>
  assume-console [
    press page-down
  ]
  run [
    editor-event-loop screen:address:screen, console:address:console, <span class="Constant">2</span>:address:editor-data
  ]
  screen-should-contain [
   <span class="Constant"> .          .</span>
   <span class="Constant"> .          .</span>
   <span class="Constant"> .c         .</span>
   <span class="Constant"> .d         .</span>
  ]
  assume-console [
    press page-down
  ]
  run [
    editor-event-loop screen:address:screen, console:address:console, <span class="Constant">2</span>:address:editor-data
  ]
  screen-should-contain [
   <span class="Constant"> .          .</span>
   <span class="Constant"> .d         .</span>
   <span class="Constant"> .e         .</span>
   <span class="Constant"> .┈┈┈┈┈┈    .</span>
  ]
  assume-console [
    press page-up
  ]
  run [
    editor-event-loop screen:address:screen, console:address:console, <span class="Constant">2</span>:address:editor-data
  ]
  screen-should-contain [
   <span class="Constant"> .          .</span>
   <span class="Constant"> .          .</span>
   <span class="Constant"> .c         .</span>
   <span class="Constant"> .d         .</span>
  ]
]

<span class="muScenario">scenario</span> editor-scrolls-up-on-left-arrow [
  <span class="Comment"># screen has 1 line for menu + 3 lines</span>
  assume-screen <span class="Constant">5/width</span>, <span class="Constant">4/height</span>
  <span class="Comment"># editor contains &gt;3 lines</span>
  <span class="Constant">1</span>:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[a</span>
<span class="Constant">b</span>
<span class="Constant">c</span>
<span class="Constant">d</span>
<span class="Constant">e]</span>
  <span class="Constant">2</span>:address:editor-data<span class="Special"> &lt;- </span>new-editor <span class="Constant">1</span>:address:array:character, screen:address:screen, <span class="Constant">0/left</span>, <span class="Constant">5/right</span>
  <span class="Comment"># position cursor at top of second page</span>
  assume-console [
    press page-down
  ]
  run [
    editor-event-loop screen:address:screen, console:address:console, <span class="Constant">2</span>:address:editor-data
  ]
  screen-should-contain [
   <span class="Constant"> .     .</span>
   <span class="Constant"> .c    .</span>
   <span class="Constant"> .d    .</span>
   <span class="Constant"> .e    .</span>
  ]
  <span class="Comment"># now try to move left</span>
  assume-console [
    press left-arrow
  ]
  run [
    editor-event-loop screen:address:screen, console:address:console, <span class="Constant">2</span>:address:editor-data
    <span class="Constant">3</span>:number<span class="Special"> &lt;- </span>get *<span class="Constant">2</span>:address:editor-data, <span class="Constant">cursor-row:offset</span>
    <span class="Constant">4</span>:number<span class="Special"> &lt;- </span>get *<span class="Constant">2</span>:address:editor-data, <span class="Constant">cursor-column:offset</span>
  ]
  <span class="Comment"># screen scrolls</span>
  screen-should-contain [
   <span class="Constant"> .     .</span>
   <span class="Constant"> .b    .</span>
   <span class="Constant"> .c    .</span>
   <span class="Constant"> .d    .</span>
  ]
  memory-should-contain [
    <span class="Constant">3</span><span class="Special"> &lt;- </span><span class="Constant">1</span>
    <span class="Constant">4</span><span class="Special"> &lt;- </span><span class="Constant">1</span>
  ]
]

<span class="muScenario">scenario</span> editor-can-scroll-up-to-start-of-file [
  <span class="Comment"># screen has 1 line for menu + 3 lines</span>
  assume-screen <span class="Constant">10/width</span>, <span class="Constant">4/height</span>
  <span class="Comment"># initialize editor with &gt;3 lines</span>
  <span class="Constant">1</span>:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[a</span>
<span class="Constant">b</span>
<span class="Constant">c</span>
<span class="Constant">d]</span>
  <span class="Constant">2</span>:address:editor-data<span class="Special"> &lt;- </span>new-editor <span class="Constant">1</span>:address:array:character, screen:address:screen, <span class="Constant">0/left</span>, <span class="Constant">10/right</span>
  screen-should-contain [
   <span class="Constant"> .          .</span>
   <span class="Constant"> .a         .</span>
   <span class="Constant"> .b         .</span>
   <span class="Constant"> .c         .</span>
  ]
  <span class="Comment"># position cursor at top of second page, then try to move up to start of</span>
  <span class="Comment"># text</span>
  assume-console [
    press page-down
    press up-arrow
    press up-arrow
  ]
  run [
    editor-event-loop screen:address:screen, console:address:console, <span class="Constant">2</span>:address:editor-data
  ]
  <span class="Comment"># screen slides by one line</span>
  screen-should-contain [
   <span class="Constant"> .          .</span>
   <span class="Constant"> .a         .</span>
   <span class="Constant"> .b         .</span>
   <span class="Constant"> .c         .</span>
  ]
  <span class="Comment"># try to move up again</span>
  assume-console [
    press up-arrow
  ]
  run [
    editor-event-loop screen:address:screen, console:address:console, <span class="Constant">2</span>:address:editor-data
  ]
  <span class="Comment"># screen remains unchanged</span>
  screen-should-contain [
   <span class="Constant"> .          .</span>
   <span class="Constant"> .a         .</span>
   <span class="Constant"> .b         .</span>
   <span class="Constant"> .c         .</span>
  ]
]

<span class="Comment"># ctrl-f/page-down - render next page if it exists</span>

<span class="muScenario">scenario</span> editor-can-scroll [
  assume-screen <span class="Constant">10/width</span>, <span class="Constant">4/height</span>
  <span class="Constant">1</span>:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[a</span>
<span class="Constant">b</span>
<span class="Constant">c</span>
<span class="Constant">d]</span>
  <span class="Constant">2</span>:address:editor-data<span class="Special"> &lt;- </span>new-editor <span class="Constant">1</span>:address:array:character, screen:address:screen, <span class="Constant">0/left</span>, <span class="Constant">10/right</span>
  screen-should-contain [
   <span class="Constant"> .          .</span>
   <span class="Constant"> .a         .</span>
   <span class="Constant"> .b         .</span>
   <span class="Constant"> .c         .</span>
  ]
  <span class="Comment"># scroll down</span>
  assume-console [
    press page-down
  ]
  run [
    editor-event-loop screen:address:screen, console:address:console, <span class="Constant">2</span>:address:editor-data
  ]
  <span class="Comment"># screen shows next page</span>
  screen-should-contain [
   <span class="Constant"> .          .</span>
   <span class="Constant"> .c         .</span>
   <span class="Constant"> .d         .</span>
<span class="Constant">    .┈┈┈┈┈┈┈┈┈┈.</span>
  ]
]

<span class="muRecipe">after</span> <span class="Constant">&lt;handle-special-character&gt;</span> [
  <span class="Delimiter">{</span>
    page-down?:boolean<span class="Special"> &lt;- </span>equal *c, <span class="Constant">6/ctrl-f</span>
    <span class="muControl">break-unless</span> page-down?
    top-of-screen:address:address:duplex-list:character<span class="Special"> &lt;- </span>get-address *editor, <span class="Constant">top-of-screen:offset</span>
    old-top:address:duplex-list:character<span class="Special"> &lt;- </span>copy *top-of-screen
<span class="Constant">    &lt;move-cursor-begin&gt;</span>
    page-down editor
    undo-coalesce-tag:number<span class="Special"> &lt;- </span>copy <span class="Constant">0/never</span>
<span class="Constant">    &lt;move-cursor-end&gt;</span>
    no-movement?:boolean<span class="Special"> &lt;- </span>equal *top-of-screen, old-top
    go-render?<span class="Special"> &lt;- </span>not no-movement?
    <span class="muControl">reply</span>
  <span class="Delimiter">}</span>
]

<span class="muRecipe">after</span> <span class="Constant">&lt;handle-special-key&gt;</span> [
  <span class="Delimiter">{</span>
    page-down?:boolean<span class="Special"> &lt;- </span>equal *k, <span class="Constant">65518/page-down</span>
    <span class="muControl">break-unless</span> page-down?
    top-of-screen:address:address:duplex-list:character<span class="Special"> &lt;- </span>get-address *editor, <span class="Constant">top-of-screen:offset</span>
    old-top:address:duplex-list:character<span class="Special"> &lt;- </span>copy *top-of-screen
<span class="Constant">    &lt;move-cursor-begin&gt;</span>
    page-down editor
    undo-coalesce-tag:number<span class="Special"> &lt;- </span>copy <span class="Constant">0/never</span>
<span class="Constant">    &lt;move-cursor-end&gt;</span>
    no-movement?:boolean<span class="Special"> &lt;- </span>equal *top-of-screen, old-top
    go-render?<span class="Special"> &lt;- </span>not no-movement?
    <span class="muControl">reply</span>
  <span class="Delimiter">}</span>
]

<span class="Comment"># page-down skips entire wrapped lines, so it can't scroll past lines</span>
<span class="Comment"># taking up the entire screen</span>
<span class="muRecipe">recipe</span> page-down editor:address:editor-data<span class="muRecipe"> -&gt; </span>editor:address:editor-data [
  <span class="Constant">local-scope</span>
  <span class="Constant">load-ingredients</span>
  <span class="Comment"># if editor contents don't overflow screen, do nothing</span>
  bottom-of-screen:address:duplex-list:character<span class="Special"> &lt;- </span>get *editor, <span class="Constant">bottom-of-screen:offset</span>
  <span class="muControl">reply-unless</span> bottom-of-screen
  <span class="Comment"># if not, position cursor at final character</span>
  before-cursor:address:address:duplex-list:character<span class="Special"> &lt;- </span>get-address *editor, <span class="Constant">before-cursor:offset</span>
  *before-cursor<span class="Special"> &lt;- </span>prev-duplex bottom-of-screen
  <span class="Comment"># keep one line in common with previous page</span>
  <span class="Delimiter">{</span>
    last:character<span class="Special"> &lt;- </span>get **before-cursor, <span class="Constant">value:offset</span>
    newline?:boolean<span class="Special"> &lt;- </span>equal last, <span class="Constant">10/newline</span>
    <span class="muControl">break-unless</span> newline?:boolean
    *before-cursor<span class="Special"> &lt;- </span>prev-duplex *before-cursor
  <span class="Delimiter">}</span>
  <span class="Comment"># move cursor and top-of-screen to start of that line</span>
  move-to-start-of-line editor
  top-of-screen:address:address:duplex-list:character<span class="Special"> &lt;- </span>get-address *editor, <span class="Constant">top-of-screen:offset</span>
  *top-of-screen<span class="Special"> &lt;- </span>copy *before-cursor
]

<span class="muScenario">scenario</span> editor-does-not-scroll-past-end [
  assume-screen <span class="Constant">10/width</span>, <span class="Constant">4/height</span>
  <span class="Constant">1</span>:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[a</span>
<span class="Constant">b]</span>
  <span class="Constant">2</span>:address:editor-data<span class="Special"> &lt;- </span>new-editor <span class="Constant">1</span>:address:array:character, screen:address:screen, <span class="Constant">0/left</span>, <span class="Constant">10/right</span>
  editor-render screen, <span class="Constant">2</span>:address:editor-data
  screen-should-contain [
   <span class="Constant"> .          .</span>
   <span class="Constant"> .a         .</span>
   <span class="Constant"> .b         .</span>
<span class="Constant">    .┈┈┈┈┈┈┈┈┈┈.</span>
  ]
  <span class="Comment"># scroll down</span>
  assume-console [
    press page-down
  ]
  run [
    editor-event-loop screen:address:screen, console:address:console, <span class="Constant">2</span>:address:editor-data
  ]
  <span class="Comment"># screen remains unmodified</span>
  screen-should-contain [
   <span class="Constant"> .          .</span>
   <span class="Constant"> .a         .</span>
   <span class="Constant"> .b         .</span>
<span class="Constant">    .┈┈┈┈┈┈┈┈┈┈.</span>
  ]
]

<span class="muScenario">scenario</span> editor-starts-next-page-at-start-of-wrapped-line [
  <span class="Comment"># screen has 1 line for menu + 3 lines for text</span>
  assume-screen <span class="Constant">10/width</span>, <span class="Constant">4/height</span>
  <span class="Comment"># editor contains a long last line</span>
  <span class="Constant">1</span>:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[a</span>
<span class="Constant">b</span>
<span class="Constant">cdefgh]</span>
  <span class="Comment"># editor screen triggers wrap of last line</span>
  <span class="Constant">2</span>:address:editor-data<span class="Special"> &lt;- </span>new-editor <span class="Constant">1</span>:address:array:character, screen:address:screen, <span class="Constant">0/left</span>, <span class="Constant">4/right</span>
  <span class="Comment"># some part of last line is not displayed</span>
  screen-should-contain [
   <span class="Constant"> .          .</span>
   <span class="Constant"> .a         .</span>
   <span class="Constant"> .b         .</span>
   <span class="Constant"> .cde↩      .</span>
  ]
  <span class="Comment"># scroll down</span>
  assume-console [
    press page-down
  ]
  run [
    editor-event-loop screen:address:screen, console:address:console, <span class="Constant">2</span>:address:editor-data
  ]
  <span class="Comment"># screen shows entire wrapped line</span>
  screen-should-contain [
   <span class="Constant"> .          .</span>
   <span class="Constant"> .cde↩      .</span>
   <span class="Constant"> .fgh       .</span>
   <span class="Constant"> .┈┈┈┈      .</span>
  ]
]

<span class="muScenario">scenario</span> editor-starts-next-page-at-start-of-wrapped-line-2 [
  <span class="Comment"># screen has 1 line for menu + 3 lines for text</span>
  assume-screen <span class="Constant">10/width</span>, <span class="Constant">4/height</span>
  <span class="Comment"># editor contains a very long line that occupies last two lines of screen</span>
  <span class="Comment"># and still has something left over</span>
  <span class="Constant">1</span>:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[a</span>
<span class="Constant">bcdefgh]</span>
  <span class="Constant">2</span>:address:editor-data<span class="Special"> &lt;- </span>new-editor <span class="Constant">1</span>:address:array:character, screen:address:screen, <span class="Constant">0/left</span>, <span class="Constant">4/right</span>
  <span class="Comment"># some part of last line is not displayed</span>
  screen-should-contain [
   <span class="Constant"> .          .</span>
   <span class="Constant"> .a         .</span>
   <span class="Constant"> .bcd↩      .</span>
   <span class="Constant"> .efg↩      .</span>
  ]
  <span class="Comment"># scroll down</span>
  assume-console [
    press page-down
  ]
  run [
    editor-event-loop screen:address:screen, console:address:console, <span class="Constant">2</span>:address:editor-data
  ]
  <span class="Comment"># screen shows entire wrapped line</span>
  screen-should-contain [
   <span class="Constant"> .          .</span>
   <span class="Constant"> .bcd↩      .</span>
   <span class="Constant"> .efg↩      .</span>
   <span class="Constant"> .h         .</span>
  ]
]

<span class="Comment"># ctrl-b/page-up - render previous page if it exists</span>

<span class="muScenario">scenario</span> editor-can-scroll-up [
  assume-screen <span class="Constant">10/width</span>, <span class="Constant">4/height</span>
  <span class="Constant">1</span>:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[a</span>
<span class="Constant">b</span>
<span class="Constant">c</span>
<span class="Constant">d]</span>
  <span class="Constant">2</span>:address:editor-data<span class="Special"> &lt;- </span>new-editor <span class="Constant">1</span>:address:array:character, screen:address:screen, <span class="Constant">0/left</span>, <span class="Constant">10/right</span>
  screen-should-contain [
   <span class="Constant"> .          .</span>
   <span class="Constant"> .a         .</span>
   <span class="Constant"> .b         .</span>
   <span class="Constant"> .c         .</span>
  ]
  <span class="Comment"># scroll down</span>
  assume-console [
    press page-down
  ]
  run [
    editor-event-loop screen:address:screen, console:address:console, <span class="Constant">2</span>:address:editor-data
  ]
  <span class="Comment"># screen shows next page</span>
  screen-should-contain [
   <span class="Constant"> .          .</span>
   <span class="Constant"> .c         .</span>
   <span class="Constant"> .d         .</span>
<span class="Constant">    .┈┈┈┈┈┈┈┈┈┈.</span>
  ]
  <span class="Comment"># scroll back up</span>
  assume-console [
    press page-up
  ]
  run [
    editor-event-loop screen:address:screen, console:address:console, <span class="Constant">2</span>:address:editor-data
  ]
  <span class="Comment"># screen shows original page again</span>
  screen-should-contain [
   <span class="Constant"> .          .</span>
   <span class="Constant"> .a         .</span>
   <span class="Constant"> .b         .</span>
   <span class="Constant"> .c         .</span>
  ]
]

<span class="muRecipe">after</span> <span class="Constant">&lt;handle-special-character&gt;</span> [
  <span class="Delimiter">{</span>
    page-up?:boolean<span class="Special"> &lt;- </span>equal *c, <span class="Constant">2/ctrl-b</span>
    <span class="muControl">break-unless</span> page-up?
    top-of-screen:address:address:duplex-list:character<span class="Special"> &lt;- </span>get-address *editor, <span class="Constant">top-of-screen:offset</span>
    old-top:address:duplex-list:character<span class="Special"> &lt;- </span>copy *top-of-screen
<span class="Constant">    &lt;move-cursor-begin&gt;</span>
    editor<span class="Special"> &lt;- </span>page-up editor, screen-height
    undo-coalesce-tag:number<span class="Special"> &lt;- </span>copy <span class="Constant">0/never</span>
<span class="Constant">    &lt;move-cursor-end&gt;</span>
    no-movement?:boolean<span class="Special"> &lt;- </span>equal *top-of-screen, old-top
    go-render?<span class="Special"> &lt;- </span>not no-movement?
    <span class="muControl">reply</span>
  <span class="Delimiter">}</span>
]

<span class="muRecipe">after</span> <span class="Constant">&lt;handle-special-key&gt;</span> [
  <span class="Delimiter">{</span>
    page-up?:boolean<span class="Special"> &lt;- </span>equal *k, <span class="Constant">65519/page-up</span>
    <span class="muControl">break-unless</span> page-up?
    top-of-screen:address:address:duplex-list:character<span class="Special"> &lt;- </span>get-address *editor, <span class="Constant">top-of-screen:offset</span>
    old-top:address:duplex-list:character<span class="Special"> &lt;- </span>copy *top-of-screen
<span class="Constant">    &lt;move-cursor-begin&gt;</span>
    editor<span class="Special"> &lt;- </span>page-up editor, screen-height
    undo-coalesce-tag:number<span class="Special"> &lt;- </span>copy <span class="Constant">0/never</span>
<span class="Constant">    &lt;move-cursor-end&gt;</span>
    no-movement?:boolean<span class="Special"> &lt;- </span>equal *top-of-screen, old-top
    <span class="Comment"># don't bother re-rendering if nothing changed. todo: test this</span>
    go-render?<span class="Special"> &lt;- </span>not no-movement?
    <span class="muControl">reply</span>
  <span class="Delimiter">}</span>
]

<span class="muRecipe">recipe</span> page-up editor:address:editor-data, screen-height:number<span class="muRecipe"> -&gt; </span>editor:address:editor-data [
  <span class="Constant">local-scope</span>
  <span class="Constant">load-ingredients</span>
  max:number<span class="Special"> &lt;- </span>subtract screen-height, <span class="Constant">1/menu-bar</span>, <span class="Constant">1/overlapping-line</span>
  count:number<span class="Special"> &lt;- </span>copy <span class="Constant">0</span>
  top-of-screen:address:address:duplex-list:character<span class="Special"> &lt;- </span>get-address *editor, <span class="Constant">top-of-screen:offset</span>
  <span class="Delimiter">{</span>
    done?:boolean<span class="Special"> &lt;- </span>greater-or-equal count, max
    <span class="muControl">break-if</span> done?
    prev:address:duplex-list:character<span class="Special"> &lt;- </span>before-previous-line *top-of-screen, editor
    <span class="muControl">break-unless</span> prev
    *top-of-screen<span class="Special"> &lt;- </span>copy prev
    count<span class="Special"> &lt;- </span>add count, <span class="Constant">1</span>
    <span class="muControl">loop</span>
  <span class="Delimiter">}</span>
]

<span class="muScenario">scenario</span> editor-can-scroll-up-multiple-pages [
  <span class="Comment"># screen has 1 line for menu + 3 lines</span>
  assume-screen <span class="Constant">10/width</span>, <span class="Constant">4/height</span>
  <span class="Comment"># initialize editor with 8 lines</span>
  <span class="Constant">1</span>:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[a</span>
<span class="Constant">b</span>
<span class="Constant">c</span>
<span class="Constant">d</span>
<span class="Constant">e</span>
<span class="Constant">f</span>
<span class="Constant">g</span>
<span class="Constant">h]</span>
  <span class="Constant">2</span>:address:editor-data<span class="Special"> &lt;- </span>new-editor <span class="Constant">1</span>:address:array:character, screen:address:screen, <span class="Constant">0/left</span>, <span class="Constant">10/right</span>
  screen-should-contain [
   <span class="Constant"> .          .</span>
   <span class="Constant"> .a         .</span>
   <span class="Constant"> .b         .</span>
   <span class="Constant"> .c         .</span>
  ]
  <span class="Comment"># scroll down two pages</span>
  assume-console [
    press page-down
    press page-down
  ]
  run [
    editor-event-loop screen:address:screen, console:address:console, <span class="Constant">2</span>:address:editor-data
  ]
  <span class="Comment"># screen shows third page</span>
  screen-should-contain [
   <span class="Constant"> .          .</span>
   <span class="Constant"> .e         .</span>
   <span class="Constant"> .f         .</span>
   <span class="Constant"> .g         .</span>
  ]
  <span class="Comment"># scroll up</span>
  assume-console [
    press page-up
  ]
  run [
    editor-event-loop screen:address:screen, console:address:console, <span class="Constant">2</span>:address:editor-data
  ]
  <span class="Comment"># screen shows second page</span>
  screen-should-contain [
   <span class="Constant"> .          .</span>
   <span class="Constant"> .c         .</span>
   <span class="Constant"> .d         .</span>
   <span class="Constant"> .e         .</span>
  ]
  <span class="Comment"># scroll up again</span>
  assume-console [
    press page-up
  ]
  run [
    editor-event-loop screen:address:screen, console:address:console, <span class="Constant">2</span>:address:editor-data
  ]
  <span class="Comment"># screen shows original page again</span>
  screen-should-contain [
   <span class="Constant"> .          .</span>
   <span class="Constant"> .a         .</span>
   <span class="Constant"> .b         .</span>
   <span class="Constant"> .c         .</span>
  ]
]

<span class="muScenario">scenario</span> editor-can-scroll-up-wrapped-lines [
  <span class="Comment"># screen has 1 line for menu + 5 lines for text</span>
  assume-screen <span class="Constant">10/width</span>, <span class="Constant">6/height</span>
  <span class="Comment"># editor contains a long line in the first page</span>
  <span class="Constant">1</span>:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[a</span>
<span class="Constant">b</span>
<span class="Constant">cdefgh</span>
<span class="Constant">i</span>
<span class="Constant">j</span>
<span class="Constant">k</span>
<span class="Constant">l</span>
<span class="Constant">m</span>
<span class="Constant">n</span>
<span class="Constant">o]</span>
  <span class="Comment"># editor screen triggers wrap of last line</span>
  <span class="Constant">2</span>:address:editor-data<span class="Special"> &lt;- </span>new-editor <span class="Constant">1</span>:address:array:character, screen:address:screen, <span class="Constant">0/left</span>, <span class="Constant">4/right</span>
  <span class="Comment"># some part of last line is not displayed</span>
  screen-should-contain [
   <span class="Constant"> .          .</span>
   <span class="Constant"> .a         .</span>
   <span class="Constant"> .b         .</span>
   <span class="Constant"> .cde↩      .</span>
   <span class="Constant"> .fgh       .</span>
   <span class="Constant"> .i         .</span>
  ]
  <span class="Comment"># scroll down a page and a line</span>
  assume-console [
    press page-down
    left-click <span class="Constant">5</span>, <span class="Constant">0</span>
    press down-arrow
  ]
  run [
    editor-event-loop screen:address:screen, console:address:console, <span class="Constant">2</span>:address:editor-data
  ]
  <span class="Comment"># screen shows entire wrapped line</span>
  screen-should-contain [
   <span class="Constant"> .          .</span>
   <span class="Constant"> .j         .</span>
   <span class="Constant"> .k         .</span>
   <span class="Constant"> .l         .</span>
   <span class="Constant"> .m         .</span>
   <span class="Constant"> .n         .</span>
  ]
  <span class="Comment"># now scroll up one page</span>
  assume-console [
    press page-up
  ]
  run [
    editor-event-loop screen:address:screen, console:address:console, <span class="Constant">2</span>:address:editor-data
  ]
  <span class="Comment"># screen resets</span>
  screen-should-contain [
   <span class="Constant"> .          .</span>
   <span class="Constant"> .b         .</span>
   <span class="Constant"> .cde↩      .</span>
   <span class="Constant"> .fgh       .</span>
   <span class="Constant"> .i         .</span>
   <span class="Constant"> .j         .</span>
  ]
]

<span class="muScenario">scenario</span> editor-can-scroll-up-wrapped-lines-2 [
  <span class="Comment"># screen has 1 line for menu + 3 lines for text</span>
  assume-screen <span class="Constant">10/width</span>, <span class="Constant">4/height</span>
  <span class="Comment"># editor contains a very long line that occupies last two lines of screen</span>
  <span class="Comment"># and still has something left over</span>
  <span class="Constant">1</span>:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[a</span>
<span class="Constant">bcdefgh]</span>
  <span class="Constant">2</span>:address:editor-data<span class="Special"> &lt;- </span>new-editor <span class="Constant">1</span>:address:array:character, screen:address:screen, <span class="Constant">0/left</span>, <span class="Constant">4/right</span>
  <span class="Comment"># some part of last line is not displayed</span>
  screen-should-contain [
   <span class="Constant"> .          .</span>
   <span class="Constant"> .a         .</span>
   <span class="Constant"> .bcd↩      .</span>
   <span class="Constant"> .efg↩      .</span>
  ]
  <span class="Comment"># scroll down</span>
  assume-console [
    press page-down
  ]
  run [
    editor-event-loop screen:address:screen, console:address:console, <span class="Constant">2</span>:address:editor-data
  ]
  <span class="Comment"># screen shows entire wrapped line</span>
  screen-should-contain [
   <span class="Constant"> .          .</span>
   <span class="Constant"> .bcd↩      .</span>
   <span class="Constant"> .efg↩      .</span>
   <span class="Constant"> .h         .</span>
  ]
  <span class="Comment"># scroll back up</span>
  assume-console [
    press page-up
  ]
  run [
    editor-event-loop screen:address:screen, console:address:console, <span class="Constant">2</span>:address:editor-data
  ]
  <span class="Comment"># screen resets</span>
  screen-should-contain [
   <span class="Constant"> .          .</span>
   <span class="Constant"> .a         .</span>
   <span class="Constant"> .bcd↩      .</span>
   <span class="Constant"> .efg↩      .</span>
  ]
]

<span class="muScenario">scenario</span> editor-can-scroll-up-past-nonempty-lines [
  assume-screen <span class="Constant">10/width</span>, <span class="Constant">4/height</span>
  <span class="Comment"># text with empty line in second screen</span>
  <span class="Constant">1</span>:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[axx</span>
<span class="Constant">bxx</span>
<span class="Constant">cxx</span>
<span class="Constant">dxx</span>
<span class="Constant">exx</span>
<span class="Constant">fxx</span>
<span class="Constant">gxx</span>
<span class="Constant">hxx</span>
<span class="Constant">]</span>
  <span class="Constant">2</span>:address:editor-data<span class="Special"> &lt;- </span>new-editor <span class="Constant">1</span>:address:array:character, screen:address:screen, <span class="Constant">0/left</span>, <span class="Constant">4/right</span>
  screen-should-contain [
   <span class="Constant"> .          .</span>
   <span class="Constant"> .axx       .</span>
   <span class="Constant"> .bxx       .</span>
   <span class="Constant"> .cxx       .</span>
  ]
  assume-console [
    press page-down
  ]
  run [
    editor-event-loop screen:address:screen, console:address:console, <span class="Constant">2</span>:address:editor-data
  ]
  screen-should-contain [
   <span class="Constant"> .          .</span>
   <span class="Constant"> .cxx       .</span>
   <span class="Constant"> .dxx       .</span>
   <span class="Constant"> .exx       .</span>
  ]
  assume-console [
    press page-down
  ]
  run [
    editor-event-loop screen:address:screen, console:address:console, <span class="Constant">2</span>:address:editor-data
  ]
  screen-should-contain [
   <span class="Constant"> .          .</span>
   <span class="Constant"> .exx       .</span>
   <span class="Constant"> .fxx       .</span>
   <span class="Constant"> .gxx       .</span>
  ]
  <span class="Comment"># scroll back up past empty line</span>
  assume-console [
    press page-up
  ]
  run [
    editor-event-loop screen:address:screen, console:address:console, <span class="Constant">2</span>:address:editor-data
  ]
  screen-should-contain [
   <span class="Constant"> .          .</span>
   <span class="Constant"> .cxx       .</span>
   <span class="Constant"> .dxx       .</span>
   <span class="Constant"> .exx       .</span>
  ]
]

<span class="muScenario">scenario</span> editor-can-scroll-up-past-empty-lines [
  assume-screen <span class="Constant">10/width</span>, <span class="Constant">4/height</span>
  <span class="Comment"># text with empty line in second screen</span>
  <span class="Constant">1</span>:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[axy</span>
<span class="Constant">bxy</span>
<span class="Constant">cxy</span>

dxy
exy
fxy
gxy
]
  <span class="Constant">2</span>:address:editor-data<span class="Special"> &lt;- </span>new-editor <span class="Constant">1</span>:address:array:character, screen:address:screen, <span class="Constant">0/left</span>, <span class="Constant">4/right</span>
  screen-should-contain [
   <span class="Constant"> .          .</span>
   <span class="Constant"> .axy       .</span>
   <span class="Constant"> .bxy       .</span>
   <span class="Constant"> .cxy       .</span>
  ]
  assume-console [
    press page-down
  ]
  run [
    editor-event-loop screen:address:screen, console:address:console, <span class="Constant">2</span>:address:editor-data
  ]
  screen-should-contain [
   <span class="Constant"> .          .</span>
   <span class="Constant"> .cxy       .</span>
   <span class="Constant"> .          .</span>
   <span class="Constant"> .dxy       .</span>
  ]
  assume-console [
    press page-down
  ]
  run [
    editor-event-loop screen:address:screen, console:address:console, <span class="Constant">2</span>:address:editor-data
  ]
  screen-should-contain [
   <span class="Constant"> .          .</span>
   <span class="Constant"> .dxy       .</span>
   <span class="Constant"> .exy       .</span>
   <span class="Constant"> .fxy       .</span>
  ]
  <span class="Comment"># scroll back up past empty line</span>
  assume-console [
    press page-up
  ]
  run [
    editor-event-loop screen:address:screen, console:address:console, <span class="Constant">2</span>:address:editor-data
  ]
  screen-should-contain [
   <span class="Constant"> .          .</span>
   <span class="Constant"> .cxy       .</span>
   <span class="Constant"> .          .</span>
   <span class="Constant"> .dxy       .</span>
  ]
]
</pre>
</body>
</html>
<!-- vim: set foldmethod=manual : -->