aboutsummaryrefslogtreecommitdiff
path: root/lua/plugins/complete-filename.lua
blob: 98e8ec30bff0fe0c55dc39f743fde6cb3322dc7f (plain) (blame)
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
-- complete file path at primary selection location using vis-complete(1)

vis:map(vis.modes.INSERT, "<C-x><C-f>", function()
	local win = vis.win
	local file = win.file
	local pos = win.selection.pos
	if not pos then return end
	-- TODO do something clever here
	local range = file:text_object_longword(pos > 0 and pos-1 or pos);
	if not range then return end
	if range.finish > pos then range.finish = pos end
	if range.start == range.finish then return end
	local prefix = file:content(range)
	if not prefix then return end
	local cmd = string.format("vis-complete --file '%s'", prefix:gsub("'", "'\\''"))
	local status, out, err = vis:pipe(file, { start = 0, finish = 0 }, cmd)
	if status ~= 0 or not out then
		if err then vis:info(err) end
		return
	end
	file:insert(pos, out)
	win.selection.pos = pos + #out
end, "Complete file path")

-- complete file path at primary selection location using vis-open(1)

vis:map(vis.modes.INSERT, "<C-x><C-o>", function()
	local win = vis.win
	local file = win.file
	local pos = win.selection.pos
	if not pos then return end
	-- TODO do something clever here
	local range = file:text_object_longword(pos > 0 and pos-1 or pos);
	if not range then return end
	if range.finish > pos then range.finish = pos end
	local prefix = file:content(range)
	if not prefix then return end
	if prefix:match("^%s*$") then
		prefix = ""
		range.start = pos
		range.finish = pos
	end
	local cmd = string.format("vis-open -- '%s'*", prefix:gsub("'", "'\\''"))
	local status, out, err = vis:pipe(file, { start = 0, finish = 0 }, cmd)
	if status ~= 0 or not out then
		if err then vis:info(err) end
		return
	end
	out = out:gsub("\n$", "")
	file:delete(range)
	file:insert(range.start, out)
	win.selection.pos = range.start + #out
end, "Complete file name")