github.com/arnodel/golua@v0.0.0-20230215163904-e0b5347eaaa1/cmd/golua-repl/init.lua (about)

     1  return function(app)
     2      app.Log("Starting init.lua")
     3      app.BindEvents("app", "Ctrl-D", app.Quit)
     4      app.UnbindEvents("app", "Ctrl-C") -- This is the default key to quit
     5      app.BindEvents("app", "Resize.Size", function(win, size) app.Resize(size.W, size.H) end)
     6  
     7      app.BindEvents("app", "Ctrl-X Ctrl-N", function() app.SwitchWindow() end)
     8  
     9      local function runCurrentChunk(win)
    10          local more, err = win.Buffer().RunCurrent()
    11          if err ~= nil then
    12              app.Logf("Lua error: %s", err)
    13          else
    14              win.MoveCursorToEnd()   
    15          end
    16          return more
    17      end
    18  
    19      -- -- This requires pressing "Enter" twice to evaluate a chunk
    20      -- -- I like it but perhaps I'm the only one!
    21      -- app.BindEvents("luarepl", "Enter", function(win)
    22      --     local l = win.CursorLine()
    23      --     if win.Buffer().IsCurrentLast(l) then
    24      --         win.DeleteRune()
    25      --         runCurrentChunk(win)
    26      --     elseif win.SplitLine(true) ~= nil then
    27      --         -- Try copying
    28      --         local err = win.Buffer().CopyToCurrent(l)
    29      --         if err ~= nil then
    30      --             app.Logf("Lua error: %s", err)
    31      --         else
    32      --             win.MoveCursorToEnd()
    33      --         end
    34      --     end
    35      -- end)
    36  
    37      app.BindEvents("luarepl", "Enter", function(win)
    38          local l, c = win.CursorPos()
    39          if win.Buffer().IsEndOfCurrentInput(l, c) then
    40              if runCurrentChunk(win) then
    41                  win.SplitLine(true)
    42              end
    43          elseif win.SplitLine(true) ~= nil then
    44              -- Try copying
    45              local err = win.Buffer().CopyToCurrent(l)
    46              if err ~= nil then
    47                  app.Logf("Lua error: %s", err)
    48              else
    49                  win.MoveCursorToEnd()
    50              end
    51          end
    52      end)
    53  
    54      app.BindEvents("luarepl", "Ctrl-X Ctrl-D", function(win)
    55          win.Buffer().ResetCurrent()
    56          win.MoveCursorToEnd()
    57      end)
    58  end