aboutsummaryrefslogtreecommitdiff
path: root/init.lua
blob: 3d46b891b7aa0685b54e985c0fbdc2cb04833a70 (plain)
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
local function file_exists(path)
	local f = io.open(path)
	local out = f ~= nil
	if out then
		io.close(f)
	end
	return out
end

--- Store the paths we opened to change their line in the second WIN_OPEN event
local paths = {}

vis.events.subscribe(vis.events.WIN_OPEN, function(win)
	if win.file == nil or win.file.path == nil then return end
	local ref = win.file.path

	-- If this is a file we opened, simply go to the saved line.
	if paths[ref] then
		vis:feedkeys(tostring(paths[ref]).."G")
		paths[ref] = nil
		return
	end

	-- if the file with colon actually exists, don't do anything
	if file_exists(ref) then
		return
	end

	local _, _, path, line = string.find(ref, "^(.+):(%d+)$")
	if path == nil or line == nil or line == 1 or not file_exists(path) then
		return
	end

	-- Store the file's path to go to line in the next WIN_OPEN event
	paths[path] = line
	win:close()
	win.file = path
	-- Open the actual file path. This creates a new window and emits a new
	-- WIN_OPEN event, which we use to set the correct line.
	vis:command("e "..path)
 -- Prevent other WIN_OPEN event handlers from running after we closed the window.
	return true
end)