function pan_viewport_to_contain_cursor(node) local safety_margin = 2*node.editor.line_height cursor1 = {39, 1} local cursor_sy = node.y + y_of_schema1(node.editor, node.editor.cursor1)/Viewport.zoom if Text.lt1({line=1, pos=1}, node.editor.screen_top1) then Viewport.y = node.y + y_of_schema1(node.editor, node.editor.screen_top1)/Viewport.zoom end if cursor_sy > Viewport.y + App.screen.height/Viewport.zoom - safety_margin then local cursor_y = y_of_schema1(node.editor, node.editor.cursor1) if Viewport.y < node.y then -- node starts within Viewport local max_cursor_y = App.screen.height - scale(node.y - Viewport.y) - safety_margin if cursor_y > max_cursor_y then -- set Viewport.y so cursor_y == max_cursor_y -- equation: cursor_y == App.screen.height - (node.y - Viewport.y)*Viewport.zoom - safety_margin -- solve for Viewport.y Viewport.y = node.y - (App.screen.height - cursor_y - safety_margin)/Viewport.zoom end else -- node extends above Viewport local screen_top_y = y_of_schema1(node.editor, node.editor.screen_top1) local min_screen_top_y = cursor_y + safety_margin - App.screen.height if screen_top_y < min_screen_top_y then screen_top_y = min_screen_top_y Viewport.y = node.y + screen_top_y/Viewport.zoom end end elseif cursor_sy < Viewport.y + safety_margin then local cursor_y = y_of_schema1(node.editor, node.editor.cursor1) if Viewport.y < node.y then -- node starts within Viewport local max_cursor_y = App.screen.height - scale(node.y - Viewport.y) - safety_margin if cursor_y > max_cursor_y then -- set Viewport.y so cursor_y == max_cursor_y -- equation: cursor_y == App.screen.height - (node.y - Viewport.y)*Viewport.zoom - safety_margin -- solve for Viewport.y Viewport.y = node.y - (App.screen.height - cursor_y - safety_margin)/Viewport.zoom end else -- node extends above Viewport local screen_top_y = y_of_schema1(node.editor, node.editor.screen_top1) local max_screen_top_y = cursor_y - safety_margin if screen_top_y > max_screen_top_y then screen_top_y = max_screen_top_y Viewport.y = node.y + screen_top_y/Viewport.zoom end end end end