2019-12-28 12:16:32 +01:00
|
|
|
local core = require "core"
|
|
|
|
local command = require "core.command"
|
|
|
|
|
|
|
|
|
2020-05-15 20:18:07 +02:00
|
|
|
local function exec(cmd, keep_newline)
|
2019-12-28 12:16:32 +01:00
|
|
|
local fp = io.popen(cmd, "r")
|
|
|
|
local res = fp:read("*a")
|
|
|
|
fp:close()
|
2020-05-15 20:18:07 +02:00
|
|
|
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) .. "'"
|
2019-12-28 12:16:32 +01:00
|
|
|
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)
|
2020-05-15 20:18:07 +02:00
|
|
|
return exec(
|
|
|
|
"printf %b " .. printfb_quote(str:gsub("%\n$", "") .. "\n") .. " | " .. cmd,
|
|
|
|
str:find("%\n$")
|
|
|
|
)
|
2019-12-28 12:16:32 +01:00
|
|
|
end)
|
|
|
|
end)
|
|
|
|
end,
|
|
|
|
})
|