diff options
author | Matěj Cepl <mcepl@cepl.eu> | 2020-06-08 20:56:58 +0200 |
---|---|---|
committer | Matěj Cepl <mcepl@cepl.eu> | 2020-06-08 20:56:58 +0200 |
commit | 258c39354764068721752de5cf3a1817e7cdc629 (patch) | |
tree | 4400109653785b3fc0a40b168a2c5719b9c5ed28 /init.lua | |
parent | 4af14ea4b4250a46f7784dca6a60c5c720865f8d (diff) | |
download | vis-jump-258c39354764068721752de5cf3a1817e7cdc629.tar.gz |
Use vis:pipe after all, but save the original construct before.
Diffstat (limited to 'init.lua')
-rw-r--r-- | init.lua | 36 |
1 files changed, 29 insertions, 7 deletions
@@ -1,6 +1,7 @@ local M = {} local function getPath(str, sep) + vis:info('str = ' .. str) sep = sep or '/' if str:sub(1, 1) == '@' then str = str:sub(2) end return str:match("(.*" .. sep .. ")") @@ -28,11 +29,20 @@ end M.replace_URLs = function() local line = vis.win.selection.line - local cmd = getPath(debug.getinfo(2,'S').source) .. "abbrevURL.lua '" .. vis.win.file.lines[line] .. "'" - local ahandle = io.popen(cmd) + -- read/write pipes in Lua are close to impossible + -- http://lua-users.org/lists/lua-l/2007-10/msg00189.html + -- perhaps + -- local status, out, err = vis:pipe(vis.win.file, vis.win.selection.range, cmd) + -- but it doesn't work + local tmpfile = os.tmpname() + local cmd = getPath(debug.getinfo(2,'S').source) .. "abbrevURL.lua >" .. tmpfile + local ahandle = io.popen(cmd, 'w') + ahandle:write(vis.win.file.lines[line]) + ahandle:close() + ahandle = io.open(tmpfile) local out = rtrim(ahandle:read("*a")) ahandle:close() - -- local status, out, err = vis:pipe(vis.win.file, vis.win.selection.range, cmd) + os.remove(tmpfile) vis.win.file.lines[line] = out end @@ -50,9 +60,21 @@ vis:map(vis.modes.NORMAL, "gx", function() end, "Jump to URL") -- https://en.opensuse.org/openSUSE:Packaging_Patches_guidelines#Current_set_of_abbreviations -vis:map(vis.modes.NORMAL, "gG", function() - M.replace_URLs() - vis.win:draw() -end, "Shorten URLs") +-- vis:map(vis.modes.NORMAL, "gG", function() +-- M.replace_URLs() +-- vis.win:draw() +-- end, "Shorten URLs") +vis:operator_new("gG", function(file, range, pos) + -- local cmd = getPath(debug.getinfo(2,'S').source) .. "abbrevURL.lua" + local cmd = "abbrevURL" + local status, out, err = vis:pipe(file, range, cmd) + if not status then + vis:info(err) + else + file:delete(range) + file:insert(range.start, out) + end + return range.start -- new cursor location +end, "Formatting operator, abbreviate URL") return M |