summaryrefslogtreecommitdiff
path: root/runtime/lua/core/cmdline.lua
blob: 4d7e44a1023629461e95604e8a36640ee0ff848e (plain)
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
local M = {}

function M.eval_line(line)
  local chunk, load_err = load("return " .. line, "=lemacs-cmd", "t", _G)
  if not chunk then
    chunk, load_err = load(line, "=lemacs-cmd", "t", _G)
  end

  if not chunk then
    return false, "lua compile error: " .. tostring(load_err)
  end

  local ok, result = pcall(chunk)
  if not ok then
    return false, "lua runtime error: " .. tostring(result)
  end

  if result ~= nil then
    return true, "=> " .. tostring(result)
  end

  return true, "lua ok"
end

function M.eval_command(state, line)
  local name = (line or ""):gsub("^%s+", ""):gsub("%s+$", "")
  if name == "" then
    return false, "command prompt empty"
  end

  local app = require("core.app")
  if app.try_named_command_extension(name) then
    return true, nil
  end

  local command = require("core.commands")
  local ok, err = command.call(state, name)
  if not ok then
    return false, err
  end

  return true, nil
end

function M.enter(state, ui, palette, kind)
  state.command_mode = true
  state.command_kind = kind or "lua"
  state.command_text = ""
  ui.update_commandline_buffer(state)
  ui.update_modeline(state)
  ui.setup_layout(state, palette)
end

function M.exit(state, ui, palette)
  state.command_mode = false
  state.command_kind = "lua"
  state.command_text = ""
  ui.update_commandline_buffer(state)
  ui.update_modeline(state)
  ui.setup_layout(state, palette)
end

return M