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

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 the file with colon actually exists, don't do anything
	if file_exists(ref) then
		return
	end

	local path, line, column = ref:match("^(.-):(%d+):?(%d*):?$")
	if not path or not line or tonumber(line) == 1 or not file_exists(path) then
		return
	end

	win.file = path
	-- fire a new WIN_OPEN event because we changed the file
	vis.events.emit(vis.events.WIN_OPEN, win)

	-- After the other handlers ran we set the position
	column = column or 1
	local pos = win.file:pos_by_linecol(line, column)
	vis.win.selection.pos = pos

	-- Stop handling the old FILE_OPEN event since we changed the file and emitted
	-- a new WIN_OPEN event

	-- Prevent further WIN_OPEN event handlers from running
	return true
end)