diff options
-rw-r--r-- | README.md | 1 | ||||
-rw-r--r-- | plugins/exec.lua | 40 |
2 files changed, 41 insertions, 0 deletions
@@ -17,6 +17,7 @@ Plugin | Description [`detectindent`](plugins/detectindent.lua?raw=1) | Automatically detects and uses the indentation size and tab type of a loaded file [`drawwhitespace`](plugins/drawwhitespace.lua?raw=1) | Draws tabs and spaces *([screenshot](https://user-images.githubusercontent.com/3920290/80573013-22ae5800-89f7-11ea-9895-6362a1c0abc7.png))* [`eval`](plugins/eval.lua?raw=1) | Replaces selected Lua code with its evaluated result +[`exec`](plugins/exec.lua?raw=1) | Runs selected text through shell command and replaces with result [`gitstatus`](plugins/gitstatus.lua?raw=1) | Displays git branch and insert/delete count in status bar *([screenshot](https://user-images.githubusercontent.com/3920290/81107223-bcea3080-8f0e-11ea-8fc7-d03173f42e33.png))* [`gofmt`](plugins/gofmt.lua?raw=1) | Auto-formats the current go file, adds the missing imports and the missing return cases [`hidelinenumbers`](plugins/hidelinenumbers.lua?raw=1) | Hides the line numbers on the left of documents *([screenshot](https://user-images.githubusercontent.com/3920290/81692043-b8b19c00-9455-11ea-8d74-ad99be4b9c5f.png))* 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, +}) |