From af453b561606c2718d4d2d9ff7d4450c26892162 Mon Sep 17 00:00:00 2001 From: demotulatingswan Date: Tue, 9 Jun 2020 22:58:49 +0200 Subject: Fixed executing multiple commands in one go with exec:replace --- plugins/exec.lua | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) (limited to 'plugins/exec.lua') diff --git a/plugins/exec.lua b/plugins/exec.lua index e3decfd..fe87afc 100644 --- a/plugins/exec.lua +++ b/plugins/exec.lua @@ -10,13 +10,18 @@ local function exec(cmd, keep_newline) end +local function shell_quote(str) + return "'" .. str:gsub("'", "'\\''") .. "'" +end + + +local printfb_sub = { + ["\\"] = "\\\\", + ["\0"] = "\\0000", + ["'"] = "'\\''", +} local function printfb_quote(str) - local sub = { - ["\\"] = "\\\\", - [string.char(0)] = "\\0000", - ["'"] = "'\\''", - } - return "'" .. str:gsub(".", sub) .. "'" + return "'" .. str:gsub(".", printfb_sub) .. "'" end @@ -31,7 +36,7 @@ command.add("core.docview", { 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, + "printf %b " .. printfb_quote(str:gsub("%\n$", "") .. "\n") .. " | eval '' " .. shell_quote(cmd), str:find("%\n$") ) end) -- cgit v1.2.3