blob: da6e9befab04b5de40258b61aaeefee21739ae12 (
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
|
-- mod-version:3
local core = require "core"
local command = require "core.command"
local contextmenu = require "plugins.contextmenu"
local keymap = require "core.keymap"
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(dv)
core.command_view:enter("Evaluate And Insert Result", {
submit = function(cmd)
dv.doc:text_input(eval(cmd))
end
})
end,
["eval:replace"] = function(dv)
core.command_view:enter("Evaluate And Replace With Result", {
submit = function(cmd)
dv.doc:replace(function(str)
return eval(cmd)
end)
end
})
end,
["eval:selected"] = function(dv)
if dv.doc:has_selection() then
local text = dv.doc:get_text(dv.doc:get_selection())
dv.doc:text_input(eval(text))
else
local line = dv.doc:get_selection()
local text = dv.doc.lines[line]
dv.doc:insert(line+1, 0, "= " .. eval(text) .. "\n")
end
end,
})
contextmenu:register("core.docview", {
{ text = "Evaluate Selected", command = "eval:selected" }
})
keymap.add { ["ctrl+alt+return"] = "eval:selected" }
|