aboutsummaryrefslogtreecommitdiff
path: root/plugins/eval.lua
blob: c2eb19ea965cdc400d4c1837c024a84a82886c29 (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
-- mod-version:3
local core = require "core"
local command = require "core.command"


local function eval(str)
  local fn, err = load("return " .. str)
  if not fn then fn, err = load(str) end
  assert(fn, err)
  return tostring(fn())
end


command.add("core.docview", {
  ["eval:insert"] = function()
    core.command_view:enter("Evaluate And Insert Result", {
      submit = function(cmd)
        core.active_view.doc:text_input(eval(cmd))
      end
    })
  end,

  ["eval:replace"] = function()
    core.command_view:enter("Evaluate And Replace With Result", {
      submit = function(cmd)
        core.active_view.doc:replace(function(str)
          return eval(cmd)
        end)
      end
    })
  end,
})