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