aboutsummaryrefslogtreecommitdiff
path: root/plugins/eval.lua
diff options
context:
space:
mode:
authorEric Gaudet <gaudet.eric@gmail.com>2022-12-28 16:25:26 -0800
committerGitHub <noreply@github.com>2022-12-28 20:25:26 -0400
commit25b0ebd36838188fa5c1568e16afd79bb0dcb67d (patch)
tree56545c671d5224af43e5abf4fda65acf532ca0c5 /plugins/eval.lua
parente77616d5e0fa34b7b558cb93d096e3c9dfb5fad8 (diff)
downloadlite-xl-plugins-25b0ebd36838188fa5c1568e16afd79bb0dcb67d.tar.gz
lite-xl-plugins-25b0ebd36838188fa5c1568e16afd79bb0dcb67d.zip
Extend eval plugin with eval:selected (#177)
- if text is selected, replace this text with eval result - if no text is selected, eval the current line and insert the result in the next line with "= " prefix. - Adding ctrl+alt+return default keymap
Diffstat (limited to 'plugins/eval.lua')
-rw-r--r--plugins/eval.lua21
1 files changed, 21 insertions, 0 deletions
diff --git a/plugins/eval.lua b/plugins/eval.lua
index 1f507fb..da6e9be 100644
--- a/plugins/eval.lua
+++ b/plugins/eval.lua
@@ -1,6 +1,8 @@
-- 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)
@@ -29,4 +31,23 @@ command.add("core.docview", {
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" }