Merge pull request #101 from demotulatingswan/master

Fixed various issues in exec:replace
This commit is contained in:
rxi 2020-05-16 09:59:58 +01:00 committed by GitHub
commit 0dd4811465
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 16 additions and 3 deletions

View File

@ -2,11 +2,21 @@ local core = require "core"
local command = require "core.command"
local function exec(cmd)
local function exec(cmd, keep_newline)
local fp = io.popen(cmd, "r")
local res = fp:read("*a")
fp:close()
return res:gsub("%\n$", "")
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
@ -20,7 +30,10 @@ command.add("core.docview", {
["exec:replace"] = function()
core.command_view:enter("Replace With Result Of Command", function(cmd)
core.active_view.doc:replace(function(str)
return exec(string.format("echo %q | %s", str, cmd))
return exec(
"printf %b " .. printfb_quote(str:gsub("%\n$", "") .. "\n") .. " | " .. cmd,
str:find("%\n$")
)
end)
end)
end,