exec.lua 648 B

12345678910111213141516171819202122232425262728
  1. local core = require "core"
  2. local command = require "core.command"
  3. local function exec(cmd)
  4. local fp = io.popen(cmd, "r")
  5. local res = fp:read("*a")
  6. fp:close()
  7. return res:gsub("%\n$", "")
  8. end
  9. command.add("core.docview", {
  10. ["exec:insert"] = function()
  11. core.command_view:enter("Insert Result Of Command", function(cmd)
  12. core.active_view.doc:text_input(exec(cmd))
  13. end)
  14. end,
  15. ["exec:replace"] = function()
  16. core.command_view:enter("Replace With Result Of Command", function(cmd)
  17. core.active_view.doc:replace(function(str)
  18. return exec(string.format("echo %q | %s", str, cmd))
  19. end)
  20. end)
  21. end,
  22. })