1 ## putting the environment together out of editors
  2 #
  3 # Consists of one editor on the left for recipes and one on the right for the
  4 # sandbox.
  5 
  6 def! main [
  7   local-scope
  8   open-console
  9   env:&:environment <- new-programming-environment 0/filesystem, 0/screen
 10   render-all 0/screen, env, render
 11   event-loop 0/screen, 0/console, env, 0/filesystem
 12   # never gets here
 13 ]
 14 
 15 container environment [
 16   recipes:&:editor
 17   current-sandbox:&:editor
 18   sandbox-in-focus?:bool  # false => cursor in recipes; true => cursor in current-sandbox
 19 ]
 20 
 21 def new-programming-environment resources:&:resources, screen:&:screen, test-sandbox-editor-contents:text -> result:&:environment [
 22   local-scope
 23   load-ingredients
 24   width:num <- screen-width screen
 25   result <- new environment:type
 26   # recipe editor on the left
 27   initial-recipe-contents:text <- slurp resources, [lesson/recipes.mu]  # ignore errors
 28   divider:num, _ <- divide-with-remainder width, 2
 29   recipes:&:editor <- new-editor initial-recipe-contents, 0/left, divider/right
 30   # sandbox editor on the right
 31   sandbox-left:num <- add divider, 1
 32   current-sandbox:&:editor <- new-editor test-sandbox-editor-contents, sandbox-left, width/right
 33   *result <- put *result, recipes:offset, recipes
 34   *result <- put *result, current-sandbox:offset, current-sandbox
 35   *result <- put *result, sandbox-in-focus?:offset, 0/false
 36   <programming-environment-initialization>
 37 ]
 38 
 39 def event-loop screen:&:screen, console:&:console, env:&:environment, resources:&:resources -> screen:&:screen, console:&:console, env:&:environment, resources:&:resources [
 40   local-scope
 41   load-ingredients
 42   recipes:&:editor <- get *env, recipes:offset
 43   current-sandbox:&:editor <- get *env, current-sandbox:offset
 44   sandbox-in-focus?:bool <- get *env, sandbox-in-focus?:offset
 45   # if we fall behind we'll stop updating the screen, but then we have to
 46   # render the entire screen when we catch up.
 47   # todo: test this
 48   render-all-on-no-more-events?:bool <- copy 0/false
 49   {
 50   ¦ # looping over each (keyboard or touch) event as it occurs
 51   ¦ +next-event
 52   ¦ e:event, found?:bool, quit?:bool, console <- read-event console
 53   ¦ loop-unless found?
 54   ¦ break-if quit?  # only in tests
 55   ¦ trace 10, [app], [next-event]
 56   ¦ <handle-event>
 57   ¦ # check for global events that will trigger regardless of which editor has focus
 58   ¦ {
 59   ¦ ¦ k:num, is-keycode?:bool <- maybe-convert e:event, keycode:variant
 60   ¦ ¦ break-unless is-keycode?
 61   ¦ ¦ <global-keypress>
 62   ¦ }
 63   ¦ {
 64   ¦ ¦ c:char, is-unicode?:bool <- maybe-convert e:event, text:variant
 65   ¦ ¦ break-unless is-unicode?
 66   ¦ ¦ <global-type>
 67   ¦ }
 68   ¦ # 'touch' event - send to both sides, see what picks it up
 69   ¦ {
 70   ¦ ¦ t:touch-event, is-touch?:bool <- maybe-convert e:event, touch:variant
 71   ¦ ¦ break-unless is-touch?
 72   ¦ ¦ # ignore all but 'left-click' events for now
 73   ¦ ¦ # todo: test this
 74   ¦ ¦ touch-type:num <- get t, type:offset
 75   ¦ ¦ is-left-click?:bool <- equal touch-type, 65513/mouse-left
 76   ¦ ¦ loop-unless is-left-click?, +next-event
 77   ¦ ¦ click-row:num <- get t, row:offset
 78   ¦ ¦ click-column:num <- get t, column:offset
 79   ¦ ¦ # later exceptions for non-editor touches will go here
 80   ¦ ¦ <global-touch>
 81   ¦ ¦ # send to both editors
 82   ¦ ¦ _ <- move-cursor-in-editor screen, recipes, t
 83   ¦ ¦ sandbox-in-focus?:bool <- move-cursor-in-editor screen, current-sandbox, t
 84   ¦ ¦ *env <- put *env, sandbox-in-focus?:offset, sandbox-in-focus?
 85   ¦ ¦ screen <- update-cursor screen, recipes, current-sandbox, sandbox-in-focus?, env
 86   ¦ ¦ loop +next-event
 87   ¦ }
 88   ¦ # 'resize' event - redraw editor
 89   ¦ # todo: test this after supporting resize in assume-console
 90   ¦ {
 91   ¦ ¦ r:resize-event, is-resize?:bool <- maybe-convert e:event, resize:variant
 92   ¦ ¦ break-unless is-resize?
 93   ¦ ¦ env, screen <- resize screen, env
 94   ¦ ¦ screen <- render-all screen, env, render-without-moving-cursor
 95   ¦ ¦ loop +next-event
 96   ¦ }
 97   ¦ # if it's not global and not a touch event, send to appropriate editor
 98   ¦ {
 99   ¦ ¦ hide-screen screen
100   ¦ ¦ sandbox-in-focus?:bool <- get *env, sandbox-in-focus?:offset
101   ¦ ¦ {
102   ¦ ¦ ¦ break-if sandbox-in-focus?
103   ¦ ¦ ¦ render?:bool <- handle-keyboard-event screen, recipes, e:event
104   ¦ ¦ }
105   ¦ ¦ {
106   ¦ ¦ ¦ break-unless sandbox-in-focus?
107   ¦ ¦ ¦ render?:bool <- handle-keyboard-event screen, current-sandbox, e:event
108   ¦ ¦ }
109   ¦ ¦ # try to batch up rendering if there are more events queued up
110   ¦ ¦ # to compensate for this additional code complexity, we always render both sides when we do render
111   ¦ ¦ render-all-on-no-more-events? <- or render-all-on-no-more-events?, render?
112   ¦ ¦ more-events?:bool <- has-more-events? console
113   ¦ ¦ {
114   ¦ ¦ ¦ break-if more-events?
115   ¦ ¦ ¦ break-unless render-all-on-no-more-events?
116   ¦ ¦ ¦ render-all-on-no-more-events? <- copy 0/false
117   ¦ ¦ ¦ screen <- render-all screen, env, render
118   ¦ ¦ }
119   ¦ ¦ screen <- update-cursor screen, recipes, current-sandbox, sandbox-in-focus?, env
120   ¦ ¦ show-screen screen
121   ¦ }
122   ¦ loop
123   }
124 ]
125 
126 def resize screen:&:screen, env:&:environment -> env:&:environment, screen:&:screen [
127   local-scope
128   load-ingredients
129   clear-screen screen  # update screen dimensions
130   width:num <- screen-width screen
131   divider:num, _ <- divide-with-remainder width, 2
132   # update recipe editor
133   recipes:&:editor <- get *env, recipes:offset
134   right:num <- subtract divider, 1
135   *recipes <- put *recipes, right:offset, right
136   # reset cursor (later we'll try to preserve its position)
137   *recipes <- put *recipes, cursor-row:offset, 1
138   *recipes <- put *recipes, cursor-column:offset, 0
139   # update sandbox editor
140   current-sandbox:&:editor <- get *env, current-sandbox:offset
141   left:num <- add divider, 1
142   *current-sandbox <- put *current-sandbox, left:offset, left
143   right:num <- subtract width, 1
144   *current-sandbox <- put *current-sandbox, right:offset, right
145   # reset cursor (later we'll try to preserve its position)
146   *current-sandbox <- put *current-sandbox, cursor-row:offset, 1
147   *current-sandbox <- put *current-sandbox, cursor-column:offset, left
148 ]
149 
150 # Variant of 'render' that updates cursor-row and cursor-column based on
151 # before-cursor (rather than the other way around). If before-cursor moves
152 # off-screen, it resets cursor-row and cursor-column.
153 def render-without-moving-cursor screen:&:screen, editor:&:editor -> last-row:num, last-column:num, screen:&:screen, editor:&:editor [
154   local-scope
155   load-ingredients
156   return-unless editor, 1/top, 0/left
157   left:num <- get *editor, left:offset
158   screen-height:num <- screen-height screen
159   right:num <- get *editor, right:offset
160   curr:&:duplex-list:char <- get *editor, top-of-screen:offset
161   prev:&:duplex-list:char <- copy curr  # just in case curr becomes null and we can't compute prev
162   curr <- next curr
163   color:num <- copy 7/white
164   row:num <- copy 1/top
165   column:num <- copy left
166   # save before-cursor
167   old-before-cursor:&:duplex-list:char <- get *editor, before-cursor:offset
168   # initialze cursor-row/cursor-column/before-cursor to the top of the screen
169   # by default
170   *editor <- put *editor, cursor-row:offset, row
171   *editor <- put *editor, cursor-column:offset, column
172   top-of-screen:&:duplex-list:char <- get *editor, top-of-screen:offset
173   *editor <- put *editor, before-cursor:offset, top-of-screen
174   screen <- move-cursor screen, row, column
175   {
176   ¦ +next-character
177   ¦ break-unless curr
178   ¦ off-screen?:bool <- greater-or-equal row, screen-height
179   ¦ break-if off-screen?
180   ¦ # if we find old-before-cursor still on the new resized screen, update
181   ¦ # editor.cursor-row and editor.cursor-column based on
182   ¦ # old-before-cursor
183   ¦ {
184   ¦ ¦ at-cursor?:bool <- equal old-before-cursor, prev
185   ¦ ¦ break-unless at-cursor?
186   ¦ ¦ *editor <- put *editor, cursor-row:offset, row
187   ¦ ¦ *editor <- put *editor, cursor-column:offset, column
188   ¦ ¦ *editor <- put *editor, before-cursor:offset, old-before-cursor
189   ¦ }
190   ¦ c:char <- get *curr, value:offset
191   ¦ <character-c-received>
192   ¦ {
193   ¦ ¦ # newline? move to left rather than 0
194   ¦ ¦ newline?:bool <- equal c, 10/newline
195   ¦ ¦ break-unless newline?
196   ¦ ¦ # clear rest of line in this window
197   ¦ ¦ clear-line-until screen, right
198   ¦ ¦ # skip to next line
199   ¦ ¦ row <- add row, 1
200   ¦ ¦ column <- copy left
201   ¦ ¦ screen <- move-cursor screen, row, column
202   ¦ ¦ curr <- next curr
203   ¦ ¦ prev <- next prev
204   ¦ ¦ loop +next-character
205   ¦ }
206   ¦ {
207   ¦ ¦ # at right? wrap. even if there's only one more letter left; we need
208   ¦ ¦ # room for clicking on the cursor after it.
209   ¦ ¦ at-right?:bool <- equal column, right
210   ¦ ¦ break-unless at-right?
211   ¦ ¦ # print wrap icon
212   ¦ ¦ wrap-icon:char <- copy 8617/loop-back-to-left
213   ¦ ¦ print screen, wrap-icon, 245/grey
214   ¦ ¦ column <- copy left
215   ¦ ¦ row <- add row, 1
216   ¦ ¦ screen <- move-cursor screen, row, column
217   ¦ ¦ # don't increment curr
218   ¦ ¦ loop +next-character
219   ¦ }
220   ¦ print screen, c, color
221   ¦ curr <- next curr
222   ¦ prev <- next prev
223   ¦ column <- add column, 1
224   ¦ loop
225   }
226   # save first character off-screen
227   *editor <- put *editor, bottom-of-screen:offset, curr
228   *editor <- put *editor, bottom:offset, row
229   return row, column
230 ]
231 
232 scenario point-at-multiple-editors [
233   local-scope
234   trace-until 100/app  # trace too long
235   assume-screen 30/width, 5/height
236   # initialize both halves of screen
237   assume-resources [
238   ¦ [lesson/recipes.mu] <- [
239   ¦ ¦ |abc|
240   ¦ ]
241   ]
242   env:&:environment <- new-programming-environment resources, screen, [def]  # contents of sandbox editor
243   # focus on both sides
244   assume-console [
245   ¦ left-click 1, 1
246   ¦ left-click 1, 17
247   ]
248   # check cursor column in each
249   run [
250   ¦ event-loop screen, console, env, resources
251   ¦ recipes:&:editor <- get *env, recipes:offset
252   ¦ 5:num/raw <- get *recipes, cursor-column:offset
253   ¦ sandbox:&:editor <- get *env, current-sandbox:offset
254   ¦ 7:num/raw <- get *sandbox, cursor-column:offset
255   ]
256   memory-should-contain [
257   ¦ 5 <- 1
258   ¦ 7 <- 17
259   ]
260 ]
261 
262 scenario edit-multiple-editors [
263   local-scope
264   trace-until 100/app  # trace too long
265   assume-screen 30/width, 5/height
266   # initialize both halves of screen
267   assume-resources [
268   ¦ [lesson/recipes.mu] <- [
269   ¦ ¦ |abc|
270   ¦ ]
271   ]
272   env:&:environment <- new-programming-environment resources, screen, [def]  # contents of sandbox
273   render-all screen, env, render
274   # type one letter in each of them
275   assume-console [
276   ¦ left-click 1, 1
277   ¦ type [0]
278   ¦ left-click 1, 17
279   ¦ type [1]
280   ]
281   run [
282   ¦ event-loop screen, console, env, resources
283   ¦ recipes:&:editor <- get *env, recipes:offset
284   ¦ 5:num/raw <- get *recipes, cursor-column:offset
285   ¦ sandbox:&:editor <- get *env, current-sandbox:offset
286   ¦ 7:num/raw <- get *sandbox, cursor-column:offset
287   ]
288   screen-should-contain [
289   ¦ .           run (F4)           .  # this line has a different background, but we don't test that yet
290   ¦ .a0bc           ╎d1ef          .
291   ¦ .               ╎──────────────.
292   ¦ .╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╎              .
293   ¦ .               ╎              .
294   ]
295   memory-should-contain [
296   ¦ 5 <- 2  # cursor column of recipe editor
297   ¦ 7 <- 18  # cursor column of sandbox editor
298   ]
299   # show the cursor at the right window
300   run [
301   ¦ cursor:char <- copy 9251/␣
302   ¦ print screen, cursor
303   ]
304   screen-should-contain [
305   ¦ .           run (F4)           .
306   ¦ .a0bc           ╎d1␣f          .
307   ¦ .               ╎──────────────.
308   ¦ .╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╎              .
309   ¦ .               ╎              .
310   ]
311 ]
312 
313 scenario editor-in-focus-keeps-cursor [
314   local-scope
315   trace-until 100/app  # trace too long
316   assume-screen 30/width, 5/height
317   assume-resources [
318   ¦ [lesson/recipes.mu] <- [
319   ¦ ¦ |abc|
320   ¦ ]
321   ]
322   env:&:environment <- new-programming-environment resources, screen, [def]
323   render-all screen, env, render
324   # initialize programming environment and highlight cursor
325   assume-console []
326   run [
327   ¦ event-loop screen, console, env, resources
328   ¦ cursor:char <- copy 9251/␣
329   ¦ print screen, cursor
330   ]
331   # is cursor at the right place?
332   screen-should-contain [
333   ¦ .           run (F4)           .
334   ¦ .␣bc            ╎def           .
335   ¦ .               ╎──────────────.
336   ¦ .╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╎              .
337   ¦ .               ╎              .
338   ]
339   # now try typing a letter
340   assume-console [
341   ¦ type [z]
342   ]
343   run [
344   ¦ event-loop screen, console, env, resources
345   ¦ cursor:char <- copy 9251/␣
346   ¦ print screen, cursor
347   ]
348   # cursor should still be right
349   screen-should-contain [
350   ¦ .           run (F4)           .
351   ¦ .z␣bc           ╎def           .
352   ¦ .               ╎──────────────.
353   ¦ .╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╎              .
354   ¦ .               ╎              .
355   ]
356 ]
357 
358 scenario backspace-in-sandbox-editor-joins-lines [
359   local-scope
360   trace-until 100/app  # trace too long
361   assume-screen 30/width, 5/height
362   assume-resources [
363   ]
364   # initialize sandbox side with two lines
365   test-sandbox-editor-contents:text <- new [abc
366 def]
367   env:&:environment <- new-programming-environment resources, screen, test-sandbox-editor-contents
368   render-all screen, env, render
369   screen-should-contain [
370   ¦ .           run (F4)           .
371   ¦ .               ╎abc           .
372   ¦ .╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╎def           .
373   ¦ .               ╎──────────────.
374   ¦ .               ╎              .
375   ]
376   # position cursor at start of second line and hit backspace
377   assume-console [
378   ¦ left-click 2, 16
379   ¦ press backspace
380   ]
381   run [
382   ¦ event-loop screen, console, env, resources
383   ¦ cursor:char <- copy 9251/␣
384   ¦ print screen, cursor
385   ]
386   # cursor moves to end of old line
387   screen-should-contain [
388   ¦ .           run (F4)           .
389   ¦ .               ╎abc␣ef        .
390   ¦ .╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╎──────────────.
391   ¦ .               ╎              .
392   ]
393 ]
394 
395 def render-all screen:&:screen, env:&:environment, {render-editor: (recipe (address screen) (address editor) -> number number (address screen) (address editor))} -> screen:&:screen, env:&:environment [
396   local-scope
397   load-ingredients
398   trace 10, [app], [render all]
399   hide-screen screen
400   # top menu
401   trace 11, [app], [render top menu]
402   width:num <- screen-width screen
403   draw-horizontal screen, 0, 0/left, width, 32/space, 0/black, 238/grey
404   button-start:num <- subtract width, 20
405   button-on-screen?:bool <- greater-or-equal button-start, 0
406   assert button-on-screen?, [screen too narrow for menu]
407   screen <- move-cursor screen, 0/row, button-start
408   print screen, [ run (F4) ], 255/white, 161/reddish
409   # dotted line down the middle
410   trace 11, [app], [render divider]
411   divider:num, _ <- divide-with-remainder width, 2
412   height:num <- screen-height screen
413   draw-vertical screen, divider, 1/top, height, 9482/vertical-dotted
414   #
415   screen <- render-recipes screen, env, render-editor
416   screen <- render-sandbox-side screen, env, render-editor
417   <render-components-end>
418   #
419   recipes:&:editor <- get *env, recipes:offset
420   current-sandbox:&:editor <- get *env, current-sandbox:offset
421   sandbox-in-focus?:bool <- get *env, sandbox-in-focus?:offset
422   screen <- update-cursor screen, recipes, current-sandbox, sandbox-in-focus?, env
423   #
424   show-screen screen
425 ]
426 
427 def render-recipes screen:&:screen, env:&:environment, {render-editor: (recipe (address screen) (address editor) -> number number (address screen) (address editor))} -> screen:&:screen, env:&:environment [
428   local-scope
429   load-ingredients
430   trace 11, [app], [render recipes]
431   recipes:&:editor <- get *env, recipes:offset
432   # render recipes
433   left:num <- get *recipes, left:offset
434   right:num <- get *recipes, right:offset
435   row:num, column:num, screen <- call render-editor, screen, recipes
436   clear-line-until screen, right
437   row <- add row, 1
438   <render-recipe-components-end>
439   # draw dotted line after recipes
440   draw-horizontal screen, row, left, right, 9480/horizontal-dotted
441   row <- add row, 1
442   clear-screen-from screen, row, left, left, right
443 ]
444 
445 # replaced in a later layer
446 def render-sandbox-side screen:&:screen, env:&:environment, {render-editor: (recipe (address screen) (address editor) -> number number (address screen) (address editor))} -> screen:&:screen, env:&:environment [
447   local-scope
448   load-ingredients
449   current-sandbox:&:editor <- get *env, current-sandbox:offset
450   left:num <- get *current-sandbox, left:offset
451   right:num <- get *current-sandbox, right:offset
452   row:num, column:num, screen, current-sandbox <- call render-editor, screen, current-sandbox
453   clear-line-until screen, right
454   row <- add row, 1
455   # draw solid line after code (you'll see why in later layers)
456   draw-horizontal screen, row, left, right
457   row <- add row, 1
458   clear-screen-from screen, row, left, left, right
459 ]
460 
461 def update-cursor screen:&:screen, recipes:&:editor, current-sandbox:&:editor, sandbox-in-focus?:bool, env:&:environment -> screen:&:screen [
462   local-scope
463   load-ingredients
464   <update-cursor-special-cases>
465   {
466   ¦ break-if sandbox-in-focus?
467   ¦ cursor-row:num <- get *recipes, cursor-row:offset
468   ¦ cursor-column:num <- get *recipes, cursor-column:offset
469   }
470   {
471   ¦ break-unless sandbox-in-focus?
472   ¦ cursor-row:num <- get *current-sandbox, cursor-row:offset
473   ¦ cursor-column:num <- get *current-sandbox, cursor-column:offset
474   }
475   screen <- move-cursor screen, cursor-row, cursor-column
476 ]
477 
478 # ctrl-l - redraw screen (just in case it printed junk somehow)
479 
480 after <global-type> [
481   {
482   ¦ redraw-screen?:bool <- equal c, 12/ctrl-l
483   ¦ break-unless redraw-screen?
484   ¦ screen <- render-all screen, env:&:environment, render
485   ¦ sync-screen screen
486   ¦ loop +next-event
487   }
488 ]
489 
490 # ctrl-n - switch focus
491 # todo: test this
492 
493 after <global-type> [
494   {
495   ¦ switch-side?:bool <- equal c, 14/ctrl-n
496   ¦ break-unless switch-side?
497   ¦ sandbox-in-focus?:bool <- get *env, sandbox-in-focus?:offset
498   ¦ sandbox-in-focus? <- not sandbox-in-focus?
499   ¦ *env <- put *env, sandbox-in-focus?:offset, sandbox-in-focus?
500   ¦ screen <- update-cursor screen, recipes, current-sandbox, sandbox-in-focus?, env
501   ¦ loop +next-event
502   }
503 ]
504 
505 ## helpers
506 
507 def draw-vertical screen:&:screen, col:num, y:num, bottom:num -> screen:&:screen [
508   local-scope
509   load-ingredients
510   style:char, style-found?:bool <- next-ingredient
511   {
512   ¦ break-if style-found?
513   ¦ style <- copy 9474/vertical
514   }
515   color:num, color-found?:bool <- next-ingredient
516   {
517   ¦ # default color to white
518   ¦ break-if color-found?
519   ¦ color <- copy 245/grey
520   }
521   {
522   ¦ continue?:bool <- lesser-than y, bottom
523   ¦ break-unless continue?
524   ¦ screen <- move-cursor screen, y, col
525   ¦ print screen, style, color
526   ¦ y <- add y, 1
527   ¦ loop
528   }
529 ]