1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
local M = {}
local function getPath(str, sep)
sep = sep or '/'
if str:sub(1, 1) == '@' then str = str:sub(2) end
return str:match("(.*" .. sep .. ")")
end
local function rtrim(s)
local n = #s
while n > 0 and s:find("^%s", n) do n = n - 1 end
return s:sub(1, n)
end
M.get_query = function()
local line = vis.win.selection.line
local pos = vis.win.selection.col
local str = vis.win.file.lines[line]
local len_str = string.len(str)
local URLchars = '[^a-zA-Z0-9%?._=+;&/:@#-]'
local to = str:find(URLchars, pos)
if to == nil then to = len_str else to = to - 1 end
local from = str:reverse():find(URLchars, len_str - pos + 1)
if from == nil then from = 1 else from = len_str - from + 2 end
return str:sub(from, to)
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)
local out = rtrim(ahandle:read("*a"))
ahandle:close()
-- local status, out, err = vis:pipe(vis.win.file, vis.win.selection.range, cmd)
vis.win.file.lines[line] = out
end
vis:map(vis.modes.NORMAL, "gx", function()
local cur_word = M.get_query()
-- https://bugzilla.suse.com/show_bug.cgi?id=1130840
-- https://bugs.freedesktop.org/show_bug.cgi?id=103807 x
-- https://www.damejidlo.cz/potrefena-husa-vinohrady
-- gh#python/cpython#7778 or bpo#34032
-- (https://src.adamsgaard.dk/)
if M.gx_cmd == nil then M.gx_cmd = 'setsid xdg-open' end
local command = M.gx_cmd .. " '" .. cur_word .. "'"
-- print("command = '" .. command .. "'")
os.execute(command)
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")
return M
|