Repository navigation
Expand file tree
/
Copy pathvis-autocomplete.lua
More file actions
63 lines (56 loc) · 1.69 KB
/
Copy pathvis-autocomplete.lua
File metadata and controls
63 lines (56 loc) · 1.69 KB
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
59
60
61
62
63
local module = {}
local anum = 1
local alist = {}
function autocomplete_set(arg)
alist = arg
end
function autocomplete_add(text)
table.insert(alist, text)
end
function autocomplete(incr)
if #alist == 0 then return end
fold = anum
local pos = vis.win.selection.pos
local lin = vis.win.selection.line
local col = vis.win.selection.col
local range = vis.win.file:text_object_longword(pos-1);
if vis.win.file:content(range.start, 1) == '(' or vis.win.file:content(range.start, 1) == '[' then
range.start = range.start+1
end
if vis.win.file:content(range.finish-1, 1) == ')' or vis.win.file:content(range.finish-1, 1) == ']' then
range.finish = range.finish-1
end
pre = vis.win.file:content(range.start, pos-range.start)
completion = ''
while true do
anum = anum + incr
if anum > #alist then anum = 1 end
if anum < 1 then anum = #alist end
l = pre:len()
if alist[anum] then
if pre == alist[anum]:sub(1,l) then
completion = alist[anum]
break
end
end
if anum == fold then break end
end
if completion == '' then return end
vis.win.file:delete(range)
vis.win.file:insert(range.start, completion)
vis.win.selection:to(lin,col)
end
vis:map(vis.modes.INSERT, "<S-Tab>", function(keys) autocomplete( 1) end)
vis:map(vis.modes.INSERT, "<M-Tab>", function(keys) autocomplete(-1) end)
---- you need to feed a list of words in your visrc file to use this plugin; e.g.:
--
--local auto = {}
--local p = io.popen('find ~/Documents/* -maxdepth 1 -type f')
--for path in p:lines() do
-- local l = path:len()
-- i, j = string.find(path, 'Documents/')
-- --autocomplete_add(string.sub(path, j+1, l-4))
-- table.insert(auto, string.sub(path, j+1, l-4))
--end
--autocomplete_set(auto)
return module