aboutsummaryrefslogtreecommitdiff
path: root/plugins/exec.lua
diff options
context:
space:
mode:
authorrxi <rxi@users.noreply.github.com>2020-05-16 10:02:52 +0100
committerrxi <rxi@users.noreply.github.com>2020-05-16 10:02:52 +0100
commit0d7fa4b63c8448a2939b4fb5172e01c149c41b24 (patch)
treef6e2baa425bad8711aba70bf0fe79a5b87fd6125 /plugins/exec.lua
parent6890de6bad7992b37169f9faff028b71892b8b73 (diff)
downloadlite-xl-plugins-0d7fa4b63c8448a2939b4fb5172e01c149c41b24.tar.gz
lite-xl-plugins-0d7fa4b63c8448a2939b4fb5172e01c149c41b24.zip
Added plugin `exec`
Diffstat (limited to 'plugins/exec.lua')
-rw-r--r--plugins/exec.lua40
1 files changed, 40 insertions, 0 deletions
diff --git a/plugins/exec.lua b/plugins/exec.lua
new file mode 100644
index 0000000..e3decfd
--- /dev/null
+++ b/plugins/exec.lua
@@ -0,0 +1,40 @@
+local core = require "core"
+local command = require "core.command"
+
+
+local function exec(cmd, keep_newline)
+ local fp = io.popen(cmd, "r")
+ local res = fp:read("*a")
+ fp:close()
+ return keep_newline and res or res:gsub("%\n$", "")
+end
+
+
+local function printfb_quote(str)
+ local sub = {
+ ["\\"] = "\\\\",
+ [string.char(0)] = "\\0000",
+ ["'"] = "'\\''",
+ }
+ return "'" .. str:gsub(".", sub) .. "'"
+end
+
+
+command.add("core.docview", {
+ ["exec:insert"] = function()
+ core.command_view:enter("Insert Result Of Command", function(cmd)
+ core.active_view.doc:text_input(exec(cmd))
+ end)
+ end,
+
+ ["exec:replace"] = function()
+ core.command_view:enter("Replace With Result Of Command", function(cmd)
+ core.active_view.doc:replace(function(str)
+ return exec(
+ "printf %b " .. printfb_quote(str:gsub("%\n$", "") .. "\n") .. " | " .. cmd,
+ str:find("%\n$")
+ )
+ end)
+ end)
+ end,
+})