about summary refs log tree commit diff stats
path: root/apps/ex3
Commit message (Expand)AuthorAgeFilesLines
* 6622 - new syscalls: time and ntimeKartik Agaram2020-07-081-0/+0
* 6604 - new appKartik Agaram2020-07-011-0/+0
* 6409 - primitives for text-mode UIsKartik Agaram2020-05-271-0/+0
* 5856Kartik Agaram2020-01-011-0/+0
5465e1220d73e237c51897b7d1211ec53b0dc04'>f5465e12 ^
a654e4ec ^

f5465e12 ^

a654e4ec ^
e6056999 ^
a654e4ec ^
f5465e12 ^
3e1349d2 ^
6c69569a ^
3e1349d2 ^
4690ce81 ^
a654e4ec ^
f5465e12 ^
3e1349d2 ^

f5465e12 ^

a654e4ec ^





f5465e12 ^

a654e4ec ^
f5465e12 ^

db1f56c8 ^
f5465e12 ^
0ca56ed8 ^
f5465e12 ^
76755b28 ^
f5465e12 ^







db1f56c8 ^
6c69569a ^
f5465e12 ^

6c69569a ^
f5465e12 ^








cebb5fca ^
f5465e12 ^
f344b250 ^


f5465e12 ^
f344b250 ^
f5465e12 ^


f344b250 ^


f5465e12 ^
f344b250 ^

f5465e12 ^

76755b28 ^


cebb5fca ^
f5465e12 ^
76755b28 ^
f5465e12 ^
f918675c ^

f5465e12 ^
f918675c ^

32b8fac2 ^
f918675c ^

32b8fac2 ^
f918675c ^




f5465e12 ^
f918675c ^
f5465e12 ^
f5465e12 ^

cebb5fca ^
f5465e12 ^
76755b28 ^
f5465e12 ^
f918675c ^



f5465e12 ^
f918675c ^
f5465e12 ^
f918675c ^
f5465e12 ^
f918675c ^
d2bd40bf ^
f5465e12 ^
f918675c ^

f5465e12 ^

f918675c ^
f5465e12 ^


6c69569a ^
f5465e12 ^

f918675c ^

f5465e12 ^



f918675c ^
f5465e12 ^
f918675c ^




f5465e12 ^







f5465e12 ^


cebb5fca ^
f5465e12 ^
76755b28 ^
f918675c ^



f5465e12 ^
f918675c ^


f5465e12 ^

f918675c ^






f5465e12 ^


f918675c ^
f5465e12 ^
cebb5fca ^
f5465e12 ^


f918675c ^
f5465e12 ^
f918675c ^
f5465e12 ^
f918675c ^
f5465e12 ^
f918675c ^
f5465e12 ^


f918675c ^
f5465e12 ^


f918675c ^
f5465e12 ^
f918675c ^
f5465e12 ^
f918675c ^

f5465e12 ^

2f02189d ^
f5465e12 ^
f918675c ^




af7349d5 ^
f5465e12 ^



f918675c ^
f5465e12 ^

f918675c ^
d009e158 ^
f918675c ^


f5465e12 ^
af7349d5 ^
f5465e12 ^
db1f56c8 ^
f918675c ^


f5465e12 ^


f918675c ^
f5465e12 ^

f918675c ^




f5465e12 ^
f918675c ^


f5465e12 ^
f918675c ^




f5465e12 ^

f344b250 ^
f5465e12 ^
76755b28 ^
f5465e12 ^



f918675c ^
f5465e12 ^

f918675c ^
2f02189d ^
f5465e12 ^
f918675c ^
f5465e12 ^

f344b250 ^
f5465e12 ^
76755b28 ^
f918675c ^


f5465e12 ^
f918675c ^
f5465e12 ^
f918675c ^
2f02189d ^
f918675c ^
f5465e12 ^




6c69569a ^
f5465e12 ^

f918675c ^
f5465e12 ^
6c69569a ^
f5465e12 ^









6c69569a ^
f5465e12 ^

f918675c ^
6c69569a ^
f5465e12 ^








6c69569a ^
f5465e12 ^

f918675c ^
f5465e12 ^
6c69569a ^
f5465e12 ^









6c69569a ^
f5465e12 ^

f918675c ^
6c69569a ^
f5465e12 ^















6c69569a ^
f5465e12 ^

f918675c ^
6c69569a ^
f5465e12 ^

















6c69569a ^
f5465e12 ^

f918675c ^


f5465e12 ^






f918675c ^

f5465e12 ^


9a81d746 ^
f5465e12 ^

6c69569a ^
f5465e12 ^

f918675c ^
f5465e12 ^

6c69569a ^
f5465e12 ^
























f918675c ^
f5465e12 ^

f5465e12 ^
f344b250 ^
f5465e12 ^
76755b28 ^
f918675c ^
f5465e12 ^


f918675c ^
f5465e12 ^
f918675c ^

af7349d5 ^
f5465e12 ^


f918675c ^
f5465e12 ^
f918675c ^
f5465e12 ^
f918675c ^

af7349d5 ^
f5465e12 ^



f918675c ^
f5465e12 ^
f918675c ^
af7349d5 ^
f5465e12 ^


f918675c ^
f5465e12 ^
f918675c ^
f5465e12 ^
f918675c ^
af7349d5 ^
f5465e12 ^


4690ce81 ^
f5465e12 ^


6c69569a ^
f5465e12 ^

f918675c ^
f5465e12 ^

6c69569a ^
f5465e12 ^


















a654e4ec ^
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




                                                                                          

                                                
                                   

                                                                                         

                       
                                                                                                 
                                                                                            
                                      
                           
                              
                            
                              
                            
                             
                                   

                               

        





                               

       
                         

                                                                                                              
                                                                                                     
                                                           
                                                   
                                           
                                                







                                                                                                                                      
                                                                                 
                                           

                                                                                              
                                                                                                                                                     








                                                                       
                                              
                                                                                                                   


                                         
                                                                   
                                      


                                                                                                                         


            
                                                                 

                   

 


                                                                                                       
                                                                                                                                                                            
                                           
                                                
                                                      

                                                                                          
                                                                 

                                                                                                           
                                                              

                                                                                                                                              
                                                          




                                                                                                                                                
                                                                                  
                                                                               
                                                             

 
                                                                                                                                      
                                           
                                                
                                                            



                                                                                 
                                                                                            
                                                                                                                       
                                  
                                                                           
                                                 
                                                              
                  
                                            

                                                                                   

                                       
                                                                   


                                                                          
                                           

                                                                                             

                                                                                                                                                                                                       



                                                                          
                                                                                                                                                                                       
                                                                          




                                                                                                                                                         







                                         


                                                                                                      
                                                                                                                                                                                               
                                           
                                                



                                                                                                                                                                                          
                                                  


                                                                                                                                                                   

                                                           






                                                                                                                                         


                                                    
                                                                                           
                                                       
                                                              


                                                                                                           
                                                                                  
                                                                
                                                                                    
                                                            
                                                                
                                    
                                                                                                   


                                                                        
                                                                                                        


                                                               
                                                                                    
                                                                  
                                                                                                 
                                                                   

                                                                    

                                                                      
                                    
                                                      




                                                                                     
                                                                                       



                                                                                                       
                                                                           

                                                           
                                                                                                                 
                                                                     


                                                                                     
                                                         
                                                                                       
                                    
                          


                                                                                         


                                                                
                                                                                                                     

                                                                                     




                                                                                                                
                                                              


                                                                
                                  




                                                                                                                           

 
                                                                                                                                                                           
                                           
                                                



                                                                                     
                                                                     

                                                            
                                                                           
                                
                                               
                                                                   

 
                                                                                                                                                                  
                                           
                                                


                                                                                 
                                  
                                                                                                    
                                                                
                                                                           
                                  
                                                                                   




                                                                                 
                                           

                                                                                             
                                                                                    
                                  
                                                                                                                  









                                                                           
                                           

                                                                                             
                                                                                     
                                                                                                                  








                                                                                           
                                           

                                                                                             
                                                                                    
                                  
                                                                                                                  









                                                                            
                                           

                                                                                             
                                                                                         
                                                                                                                  















                                                                                   
                                           

                                                                                             
                                                                                       
                                                                                                                  

















                                                                                                         
                                           

                                                                                             


                                                                                                                                                                                                   






                                         

                                                                                                                                                    


   
                                                              

                                                                 
                                           

                                                                                             
                                                                                    

                                  
                                                                                                                  
























                                                                                                
                                                             

 
                                                                                                        
                                                                                                             
                                           
                                                
                                                                                                            


                                                                                                  
                                                                                                         
                                                                 

                                                                                                                                          
                                                                           


                                                                                                      
                                                                                                                  
                                                              
                                                                                                             
                                                               

                                                                                                                                           
                                                                           



                                                                                                                  
                                                                                                               
                                                                    
                                                                                     
                                                                           


                                                                                                   
                                                                                                          
                                                             
                                                                                                              
                                                                  
                                                                                       
                                                                           


                                                    
                                             


                                                                   
                                           

                                                                                             
                                                                                    

                                       
                                                                                                                  


















                                                                       
                                     
<!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/001-editor.mu</title>
<meta name="Generator" content="Vim/7.4">
<meta name="plugin-version" content="vim7.4_v2">
<meta name="syntax" content="none">
<meta name="settings" content="use_css,pre_wrap,no_foldcolumn,expand_tabs,prevent_copy=">
<meta name="colorscheme" content="minimal">
<style type="text/css">
<!--
pre { white-space: pre-wrap; font-family: monospace; color: #eeeeee; background-color: #080808; }
body { font-size: 12pt; font-family: monospace; color: #eeeeee; background-color: #080808; }
* { font-size: 12pt; font-size: 1em; }
.muData { color: #ffff00; }
.muControl { color: #c0a020; }
.Special { color: #c00000; }
.Delimiter { color: #800080; }
.Comment { color: #9090ff; }
.Constant { color: #00a0a0; }
.SalientComment { color: #00ffff; }
.muRecipe { color: #ff8700; }
.muScenario { color: #00af00; }
-->
</style>

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

-->
</script>
</head>
<body>
<pre id='vimCodeElement'>
<span class="SalientComment">## the basic editor data structure, and how it displays text to the screen</span>

<span class="Comment"># temporary main for this layer: just render the given text at the given</span>
<span class="Comment"># screen dimensions, then stop</span>
<span class="muRecipe">def!</span> main text:text [
  <span class="Constant">local-scope</span>
  <span class="Constant">load-ingredients</span>
  open-console
  hide-screen <span class="Constant">0/screen</span>
  new-editor text, <span class="Constant">0/screen</span>, <span class="Constant">0/left</span>, <span class="Constant">5/right</span>
  show-screen <span class="Constant">0/screen</span>
  wait-for-event <span class="Constant">0/console</span>
  close-console
]

<span class="muScenario">scenario</span> editor-initially-prints-text-to-screen [
  <span class="Constant">local-scope</span>
  assume-screen <span class="Constant">10/width</span>, <span class="Constant">5/height</span>
  run [
    new-editor <span class="Constant">[abc]</span>, screen:&amp;:screen, <span class="Constant">0/left</span>, <span class="Constant">10/right</span>
  ]
  screen-should-contain [
    <span class="Comment"># top line of screen reserved for menu</span>
   <span class="Constant"> .          .</span>
   <span class="Constant"> .abc       .</span>
   <span class="Constant"> .          .</span>
  ]
]

<span class="muData">container</span> editor [
  <span class="Comment"># editable text: doubly linked list of characters (head contains a special sentinel)</span>
  data:&amp;:duplex-list:char
  top-of-screen:&amp;:duplex-list:char
  bottom-of-screen:&amp;:duplex-list:char
  <span class="Comment"># location before cursor inside data</span>
  before-cursor:&amp;:duplex-list:char

  <span class="Comment"># raw bounds of display area on screen</span>
  <span class="Comment"># always displays from row 1 (leaving row 0 for a menu) and at most until bottom of screen</span>
  left:num
  right:num
  bottom:num
  <span class="Comment"># raw screen coordinates of cursor</span>
  cursor-row:num
  cursor-column:num
]

<span class="Comment"># creates a new editor widget and renders its initial appearance to screen</span>
<span class="Comment">#   top/left/right constrain the screen area available to the new editor</span>
<span class="Comment">#   right is exclusive</span>
<span class="muRecipe">def</span> new-editor s:text, screen:&amp;:screen, left:num, right:num<span class="muRecipe"> -&gt; </span>result:&amp;:editor, screen:&amp;:screen [
  <span class="Constant">local-scope</span>
  <span class="Constant">load-ingredients</span>
  <span class="Comment"># no clipping of bounds</span>
  right <span class="Special">&lt;-</span> subtract right,<span class="Constant"> 1</span>
  result <span class="Special">&lt;-</span> new <span class="Constant">editor:type</span>
  <span class="Comment"># initialize screen-related fields</span>
  *result <span class="Special">&lt;-</span> put *result, <span class="Constant">left:offset</span>, left
  *result <span class="Special">&lt;-</span> put *result, <span class="Constant">right:offset</span>, right
  <span class="Comment"># initialize cursor coordinates</span>
  *result <span class="Special">&lt;-</span> put *result, <span class="Constant">cursor-row:offset</span>, <span class="Constant">1/top</span>
  *result <span class="Special">&lt;-</span> put *result, <span class="Constant">cursor-column:offset</span>, left
  <span class="Comment"># initialize empty contents</span>
  init:&amp;:duplex-list:char <span class="Special">&lt;-</span> push <span class="Constant">167/§</span>, <span class="Constant">0/tail</span>
  *result <span class="Special">&lt;-</span> put *result, <span class="Constant">data:offset</span>, init
  *result <span class="Special">&lt;-</span> put *result, <span class="Constant">top-of-screen:offset</span>, init
  *result <span class="Special">&lt;-</span> put *result, <span class="Constant">before-cursor:offset</span>, init
  result <span class="Special">&lt;-</span> insert-text result, s
  <span class="Comment"># initial render to screen, just for some old tests</span>
  _, _, screen, result <span class="Special">&lt;-</span> render screen, result
<span class="Constant">  &lt;editor-initialization&gt;</span>
]

<span class="muRecipe">def</span> insert-text editor:&amp;:editor, text:text<span class="muRecipe"> -&gt; </span>editor:&amp;:editor [
  <span class="Constant">local-scope</span>
  <span class="Constant">load-ingredients</span>
  <span class="Comment"># early exit if text is empty</span>
  <span class="muControl">return-unless</span> text, editor/same-as-ingredient:0
  len:num <span class="Special">&lt;-</span> length *text
  <span class="muControl">return-unless</span> len, editor/same-as-ingredient:0
  idx:num <span class="Special">&lt;-</span> copy<span class="Constant"> 0</span>
  <span class="Comment"># now we can start appending the rest, character by character</span>
  curr:&amp;:duplex-list:char <span class="Special">&lt;-</span> get *editor, <span class="Constant">data:offset</span>
  <span class="Delimiter">{</span>
    done?:bool <span class="Special">&lt;-</span> greater-or-equal idx, len
    <span class="muControl">break-if</span> done?
    c:char <span class="Special">&lt;-</span> index *text, idx
    insert c, curr
    <span class="Comment"># next iter</span>
    curr <span class="Special">&lt;-</span> next curr
    idx <span class="Special">&lt;-</span> add idx,<span class="Constant"> 1</span>
    <span class="muControl">loop</span>
  <span class="Delimiter">}</span>
  <span class="muControl">return</span> editor/same-as-ingredient:0
]

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

<span class="Comment"># Assumes cursor should be at coordinates (cursor-row, cursor-column) and</span>
<span class="Comment"># updates before-cursor to match. Might also move coordinates if they're</span>
<span class="Comment"># outside text.</span>
<span class="muRecipe">def</span> render screen:&amp;:screen, editor:&amp;:editor<span class="muRecipe"> -&gt; </span>last-row:num, last-column:num, screen:&amp;:screen, editor:&amp;:editor [
  <span class="Constant">local-scope</span>
  <span class="Constant">load-ingredients</span>
  <span class="muControl">return-unless</span> editor, <span class="Constant">1/top</span>, <span class="Constant">0/left</span>, screen/same-as-ingredient:0, editor/same-as-ingredient:1
  left:num <span class="Special">&lt;-</span> get *editor, <span class="Constant">left:offset</span>
  screen-height:num <span class="Special">&lt;-</span> screen-height screen
  right:num <span class="Special">&lt;-</span> get *editor, <span class="Constant">right:offset</span>
  <span class="Comment"># traversing editor</span>
  curr:&amp;:duplex-list:char <span class="Special">&lt;-</span> get *editor, <span class="Constant">top-of-screen:offset</span>
  prev:&amp;:duplex-list:char <span class="Special">&lt;-</span> copy curr  <span class="Comment"># just in case curr becomes null and we can't compute prev</span>
  curr <span class="Special">&lt;-</span> next curr
  <span class="Comment"># traversing screen</span>
<span class="Constant">  +render-loop-initialization</span>
  color:num <span class="Special">&lt;-</span> copy <span class="Constant">7/white</span>
  row:num <span class="Special">&lt;-</span> copy <span class="Constant">1/top</span>
  column:num <span class="Special">&lt;-</span> copy left
  cursor-row:num <span class="Special">&lt;-</span> get *editor, <span class="Constant">cursor-row:offset</span>
  cursor-column:num <span class="Special">&lt;-</span> get *editor, <span class="Constant">cursor-column:offset</span>
  before-cursor:&amp;:duplex-list:char <span class="Special">&lt;-</span> get *editor, <span class="Constant">before-cursor:offset</span>
  screen <span class="Special">&lt;-</span> move-cursor screen, row, column
  <span class="Delimiter">{</span>
<span class="Constant">    +next-character</span>
    <span class="muControl">break-unless</span> curr
    off-screen?:bool <span class="Special">&lt;-</span> greater-or-equal row, screen-height
    <span class="muControl">break-if</span> off-screen?
    <span class="Comment"># update editor.before-cursor</span>
    <span class="Comment"># Doing so at the start of each iteration ensures it stays one step behind</span>
    <span class="Comment"># the current character.</span>
    <span class="Delimiter">{</span>
      at-cursor-row?:bool <span class="Special">&lt;-</span> equal row, cursor-row
      <span class="muControl">break-unless</span> at-cursor-row?
      at-cursor?:bool <span class="Special">&lt;-</span> equal column, cursor-column
      <span class="muControl">break-unless</span> at-cursor?
      before-cursor <span class="Special">&lt;-</span> copy prev
    <span class="Delimiter">}</span>
    c:char <span class="Special">&lt;-</span> get *curr, <span class="Constant">value:offset</span>
<span class="Constant">    &lt;character-c-received&gt;</span>
    <span class="Delimiter">{</span>
      <span class="Comment"># newline? move to left rather than 0</span>
      newline?:bool <span class="Special">&lt;-</span> equal c, <span class="Constant">10/newline</span>
      <span class="muControl">break-unless</span> newline?
      <span class="Comment"># adjust cursor if necessary</span>
      <span class="Delimiter">{</span>
        at-cursor-row?:bool <span class="Special">&lt;-</span> equal row, cursor-row
        <span class="muControl">break-unless</span> at-cursor-row?
        left-of-cursor?:bool <span class="Special">&lt;-</span> lesser-than column, cursor-column
        <span class="muControl">break-unless</span> left-of-cursor?
        cursor-column <span class="Special">&lt;-</span> copy column
        before-cursor <span class="Special">&lt;-</span> prev curr
      <span class="Delimiter">}</span>
      <span class="Comment"># clear rest of line in this window</span>
      clear-line-until screen, right
      <span class="Comment"># skip to next line</span>
      row <span class="Special">&lt;-</span> add row,<span class="Constant"> 1</span>
      column <span class="Special">&lt;-</span> copy left
      screen <span class="Special">&lt;-</span> move-cursor screen, row, column
      curr <span class="Special">&lt;-</span> next curr
      prev <span class="Special">&lt;-</span> next prev
      <span class="muControl">loop</span> <span class="Constant">+next-character</span>
    <span class="Delimiter">}</span>
    <span class="Delimiter">{</span>
      <span class="Comment"># at right? wrap. even if there's only one more letter left; we need</span>
      <span class="Comment"># room for clicking on the cursor after it.</span>
      at-right?:bool <span class="Special">&lt;-</span> equal column, right
      <span class="muControl">break-unless</span> at-right?
      <span class="Comment"># print wrap icon</span>
      wrap-icon:char <span class="Special">&lt;-</span> copy <span class="Constant">8617/loop-back-to-left</span>
      print screen, wrap-icon, <span class="Constant">245/grey</span>
      column <span class="Special">&lt;-</span> copy left
      row <span class="Special">&lt;-</span> add row,<span class="Constant"> 1</span>
      screen <span class="Special">&lt;-</span> move-cursor screen, row, column
      <span class="Comment"># don't increment curr</span>
      <span class="muControl">loop</span> <span class="Constant">+next-character</span>
    <span class="Delimiter">}</span>
    print screen, c, color
    curr <span class="Special">&lt;-</span> next curr
    prev <span class="Special">&lt;-</span> next prev
    column <span class="Special">&lt;-</span> add column,<span class="Constant"> 1</span>
    <span class="muControl">loop</span>
  <span class="Delimiter">}</span>
  <span class="Comment"># save first character off-screen</span>
  *editor <span class="Special">&lt;-</span> put *editor, <span class="Constant">bottom-of-screen:offset</span>, curr
  <span class="Comment"># is cursor to the right of the last line? move to end</span>
  <span class="Delimiter">{</span>
    at-cursor-row?:bool <span class="Special">&lt;-</span> equal row, cursor-row
    cursor-outside-line?:bool <span class="Special">&lt;-</span> lesser-or-equal column, cursor-column
    before-cursor-on-same-line?:bool <span class="Special">&lt;-</span> and at-cursor-row?, cursor-outside-line?
    above-cursor-row?:bool <span class="Special">&lt;-</span> lesser-than row, cursor-row
    before-cursor?:bool <span class="Special">&lt;-</span> or before-cursor-on-same-line?, above-cursor-row?
    <span class="muControl">break-unless</span> before-cursor?
    cursor-row <span class="Special">&lt;-</span> copy row
    cursor-column <span class="Special">&lt;-</span> copy column
    before-cursor <span class="Special">&lt;-</span> copy prev
  <span class="Delimiter">}</span>
  *editor <span class="Special">&lt;-</span> put *editor, <span class="Constant">bottom:offset</span>, row
  *editor <span class="Special">&lt;-</span> put *editor, <span class="Constant">cursor-row:offset</span>, cursor-row
  *editor <span class="Special">&lt;-</span> put *editor, <span class="Constant">cursor-column:offset</span>, cursor-column
  *editor <span class="Special">&lt;-</span> put *editor, <span class="Constant">before-cursor:offset</span>, before-cursor
  <span class="muControl">return</span> row, column, screen/same-as-ingredient:0, editor/same-as-ingredient:1
]

<span class="muRecipe">def</span> clear-screen-from screen:&amp;:screen, row:num, column:num, left:num, right:num<span class="muRecipe"> -&gt; </span>screen:&amp;:screen [
  <span class="Constant">local-scope</span>
  <span class="Constant">load-ingredients</span>
  <span class="Comment"># if it's the real screen, use the optimized primitive</span>
  <span class="Delimiter">{</span>
    <span class="muControl">break-if</span> screen
    clear-display-from row, column, left, right
    <span class="muControl">return</span> screen/same-as-ingredient:0
  <span class="Delimiter">}</span>
  <span class="Comment"># if not, go the slower route</span>
  screen <span class="Special">&lt;-</span> move-cursor screen, row, column
  clear-line-until screen, right
  clear-rest-of-screen screen, row, left, right
  <span class="muControl">return</span> screen/same-as-ingredient:0
]

<span class="muRecipe">def</span> clear-rest-of-screen screen:&amp;:screen, row:num, left:num, right:num<span class="muRecipe"> -&gt; </span>screen:&amp;:screen [
  <span class="Constant">local-scope</span>
  <span class="Constant">load-ingredients</span>
  row <span class="Special">&lt;-</span> add row,<span class="Constant"> 1</span>
  screen <span class="Special">&lt;-</span> move-cursor screen, row, left
  screen-height:num <span class="Special">&lt;-</span> screen-height screen
  <span class="Delimiter">{</span>
    at-bottom-of-screen?:bool <span class="Special">&lt;-</span> greater-or-equal row, screen-height
    <span class="muControl">break-if</span> at-bottom-of-screen?
    screen <span class="Special">&lt;-</span> move-cursor screen, row, left
    clear-line-until screen, right
    row <span class="Special">&lt;-</span> add row,<span class="Constant"> 1</span>
    <span class="muControl">loop</span>
  <span class="Delimiter">}</span>
]

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

<span class="muScenario">scenario</span> editor-initially-handles-offsets [
  <span class="Constant">local-scope</span>
  assume-screen <span class="Constant">5/width</span>, <span class="Constant">5/height</span>
  run [
    s:text <span class="Special">&lt;-</span> new <span class="Constant">[abc]</span>
    new-editor s, screen:&amp;:screen, <span class="Constant">1/left</span>, <span class="Constant">5/right</span>
  ]
  screen-should-contain [
   <span class="Constant"> .     .</span>
   <span class="Constant"> . abc .</span>
   <span class="Constant"> .     .</span>
  ]
]

<span class="muScenario">scenario</span> editor-initially-prints-multiple-lines-at-offset [
  <span class="Constant">local-scope</span>
  assume-screen <span class="Constant">5/width</span>, <span class="Constant">5/height</span>
  run [
    s:text <span class="Special">&lt;-</span> new <span class="Constant">[abc</span>
<span class="Constant">def]</span>
    new-editor s, screen:&amp;:screen, <span class="Constant">1/left</span>, <span class="Constant">5/right</span>
  ]
  screen-should-contain [
   <span class="Constant"> .     .</span>
   <span class="Constant"> . abc .</span>
   <span class="Constant"> . def .</span>
   <span class="Constant"> .     .</span>
  ]
]

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

<span class="muScenario">scenario</span> editor-initially-wraps-barely-long-lines [
  <span class="Constant">local-scope</span>
  assume-screen <span class="Constant">5/width</span>, <span class="Constant">5/height</span>
  run [
    s:text <span class="Special">&lt;-</span> new <span class="Constant">[abcde]</span>
    new-editor s, screen:&amp;:screen, <span class="Constant">0/left</span>, <span class="Constant">5/right</span>
  ]
  <span class="Comment"># still wrap, even though the line would fit. We need room to click on the</span>
  <span class="Comment"># end of the line</span>
  screen-should-contain [
   <span class="Constant"> .     .</span>
<span class="Constant">    .abcd↩.</span>
   <span class="Constant"> .e    .</span>
   <span class="Constant"> .     .</span>
  ]
  screen-should-contain-in-color <span class="Constant">245/grey</span> [
   <span class="Constant"> .     .</span>
   <span class="Constant"> .    ↩.</span>
   <span class="Constant"> .     .</span>
   <span class="Constant"> .     .</span>
  ]
]

<span class="muScenario">scenario</span> editor-initializes-empty-text [
  <span class="Constant">local-scope</span>
  assume-screen <span class="Constant">5/width</span>, <span class="Constant">5/height</span>
  run [
    e:&amp;:editor <span class="Special">&lt;-</span> new-editor <span class="Constant">[]</span>, screen:&amp;:screen, <span class="Constant">0/left</span>, <span class="Constant">5/right</span>
    3:num/<span class="Special">raw</span> <span class="Special">&lt;-</span> get *e, <span class="Constant">cursor-row:offset</span>
    4:num/<span class="Special">raw</span> <span class="Special">&lt;-</span> get *e, <span class="Constant">cursor-column:offset</span>
  ]
  screen-should-contain [
   <span class="Constant"> .     .</span>
   <span class="Constant"> .     .</span>
   <span class="Constant"> .     .</span>
  ]
  memory-should-contain [
   <span class="Constant"> 3</span> <span class="Special">&lt;-</span><span class="Constant"> 1</span>  <span class="Comment"># cursor row</span>
   <span class="Constant"> 4</span> <span class="Special">&lt;-</span><span class="Constant"> 0</span>  <span class="Comment"># cursor column</span>
  ]
]

<span class="Comment"># just a little color for Mu code</span>

<span class="muScenario">scenario</span> render-colors-comments [
  <span class="Constant">local-scope</span>
  assume-screen <span class="Constant">5/width</span>, <span class="Constant">5/height</span>
  run [
    s:text <span class="Special">&lt;-</span> new <span class="Constant">[abc</span>
<span class="Constant"># de</span>
<span class="Constant">f]</span>
    new-editor s, screen:&amp;:screen, <span class="Constant">0/left</span>, <span class="Constant">5/right</span>
  ]
  screen-should-contain [
   <span class="Constant"> .     .</span>
   <span class="Constant"> .abc  .</span>
   <span class="Constant"> .# de .</span>
   <span class="Constant"> .f    .</span>
   <span class="Constant"> .     .</span>
  ]
  screen-should-contain-in-color <span class="Constant">12/lightblue</span>, [
   <span class="Constant"> .     .</span>
   <span class="Constant"> .     .</span>
   <span class="Constant"> .# de .</span>
   <span class="Constant"> .     .</span>
   <span class="Constant"> .     .</span>
  ]
  screen-should-contain-in-color <span class="Constant">7/white</span>, [
   <span class="Constant"> .     .</span>
   <span class="Constant"> .abc  .</span>
   <span class="Constant"> .     .</span>
   <span class="Constant"> .f    .</span>
   <span class="Constant"> .     .</span>
  ]
]

<span class="muRecipe">after</span> <span class="Constant">&lt;character-c-received&gt;</span> [
  color <span class="Special">&lt;-</span> get-color color, c
]

<span class="Comment"># so far the previous color is all the information we need; that may change</span>
<span class="muRecipe">def</span> get-color color:num, c:char<span class="muRecipe"> -&gt; </span>color:num [
  <span class="Constant">local-scope</span>
  <span class="Constant">load-ingredients</span>
  color-is-white?:bool <span class="Special">&lt;-</span> equal color, <span class="Constant">7/white</span>
  <span class="Comment"># if color is white and next character is '#', switch color to blue</span>
  <span class="Delimiter">{</span>
    <span class="muControl">break-unless</span> color-is-white?
    starting-comment?:bool <span class="Special">&lt;-</span> equal c, <span class="Constant">35/#</span>
    <span class="muControl">break-unless</span> starting-comment?
    trace<span class="Constant"> 90</span>, <span class="Constant">[app]</span>, <span class="Constant">[switch color back to blue]</span>
    color <span class="Special">&lt;-</span> copy <span class="Constant">12/lightblue</span>
    <span class="muControl">jump</span> <span class="Constant">+exit</span>
  <span class="Delimiter">}</span>
  <span class="Comment"># if color is blue and next character is newline, switch color to white</span>
  <span class="Delimiter">{</span>
    color-is-blue?:bool <span class="Special">&lt;-</span> equal color, <span class="Constant">12/lightblue</span>
    <span class="muControl">break-unless</span> color-is-blue?
    ending-comment?:bool <span class="Special">&lt;-</span> equal c, <span class="Constant">10/newline</span>
    <span class="muControl">break-unless</span> ending-comment?
    trace<span class="Constant"> 90</span>, <span class="Constant">[app]</span>, <span class="Constant">[switch color back to white]</span>
    color <span class="Special">&lt;-</span> copy <span class="Constant">7/white</span>
    <span class="muControl">jump</span> <span class="Constant">+exit</span>
  <span class="Delimiter">}</span>
  <span class="Comment"># if color is white (no comments) and next character is '&lt;', switch color to red</span>
  <span class="Delimiter">{</span>
    <span class="muControl">break-unless</span> color-is-white?
    starting-assignment?:bool <span class="Special">&lt;-</span> equal c, <span class="Constant">60/&lt;</span>
    <span class="muControl">break-unless</span> starting-assignment?
    color <span class="Special">&lt;-</span> copy <span class="Constant">1/red</span>
    <span class="muControl">jump</span> <span class="Constant">+exit</span>
  <span class="Delimiter">}</span>
  <span class="Comment"># if color is red and next character is space, switch color to white</span>
  <span class="Delimiter">{</span>
    color-is-red?:bool <span class="Special">&lt;-</span> equal color, <span class="Constant">1/red</span>
    <span class="muControl">break-unless</span> color-is-red?
    ending-assignment?:bool <span class="Special">&lt;-</span> equal c, <span class="Constant">32/space</span>
    <span class="muControl">break-unless</span> ending-assignment?
    color <span class="Special">&lt;-</span> copy <span class="Constant">7/white</span>
    <span class="muControl">jump</span> <span class="Constant">+exit</span>
  <span class="Delimiter">}</span>
  <span class="Comment"># otherwise no change</span>
<span class="Constant">  +exit</span>
  <span class="muControl">return</span> color
]

<span class="muScenario">scenario</span> render-colors-assignment [
  <span class="Constant">local-scope</span>
  assume-screen <span class="Constant">8/width</span>, <span class="Constant">5/height</span>
  run [
    s:text <span class="Special">&lt;-</span> new <span class="Constant">[abc</span>
<span class="Constant">d &lt;- e</span>
<span class="Constant">f]</span>
    new-editor s, screen:&amp;:screen, <span class="Constant">0/left</span>, <span class="Constant">8/right</span>
  ]
  screen-should-contain [
   <span class="Constant"> .        .</span>
   <span class="Constant"> .abc     .</span>
   <span class="Constant"> .d &lt;- e  .</span>
   <span class="Constant"> .f       .</span>
   <span class="Constant"> .        .</span>
  ]
  screen-should-contain-in-color <span class="Constant">1/red</span>, [
   <span class="Constant"> .        .</span>
   <span class="Constant"> .        .</span>
   <span class="Constant"> .  &lt;-    .</span>
   <span class="Constant"> .        .</span>
   <span class="Constant"> .        .</span>
  ]
]
</pre>
</body>
</html>
<!-- vim: set foldmethod=manual : -->