~martanne/devel

opening filename:lineno files on particular line v1 PROPOSED

Florian Fischer: 1
 allow proper transparent WIN_OPEN event handling

 1 files changed, 19 insertions(+), 3 deletions(-)
Hi,

I do not typically interact with refs - vis-quickfix(I think)allows me to
":make" & gives bindings to go through the make-produced refs from there.

Selecting in a terminal "file.c:111:1:" to then remove the :111:1 is a
pain in the ass & I still do it sometimes.

here's what you want - omitting file_exists:

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
	local _, _, path, line = string.find(ref, "^(.+):(%d+)$")
	if path == nil or line == nil or line == 0 or not file_exists(path) then
		return
	end
	win:close()
	vis:command("e "..path)
	vis:command("+"..line-1)
end)

The `-a` may cause trouble with vis-cursors(plugin that saves cursor
position).  If you have vis-cursors provide "-a ...:<n>" - how does the
cursor know who to trust????
On the other hand, if they are both plugins(vis-cursors &
vis-colon-file-?) that subscribe to the same event, then presumably
I can change the order of import(I haven't verified this).

Jeremy
Actually, how is it done? Is it just the order of `require` lines
in ~/.config/vis/visrc.lua?

So, if I put that snippet of code before `require` for
`vis-cursors`, would the opening process continue with modified
command line?

Best,

Matěj
That was my initial assumption.

Looking more into this, we(vis-colon-line) call win:close in the WIN_OPEN
event. Then on WIN_OPEN, vis-cursors nil-checks win, and then nil-checks
win.file, which throws an error.

I'm guessing this is a bug.

If a subscriber to WIN_OPEN calls win:close, should the rest of the
WIN_OPEN subscribers be called as well?

Jeremy
Hi Matěj,
Next
Export patchset (mbox)
How do I use this?

Copy & paste the following snippet into your terminal to import this patchset into git:

curl -s https://lists.sr.ht/~martanne/devel/patches/55207/mbox | git am -3
Learn more about email & git

[PATCH] allow proper transparent WIN_OPEN event handling Export this patch

Simply changing the file in a single WIN_OPEN event has one of two
possible problems.
1. Problem: We keep the current window
  Then all previously ran WIN_OPEN event handlers have observed a wrong
  file path, leading to broken syntax highlighting for example.
2. Problem: Use a new window
  This solves the 1. Problem by emitting a new WIN_OPEN event with
  the correct file path. However, this leads to plugins like vis-cursors
  to jump to a possibly different line rendering vis-open-extend useless.

The solution to both problems is to split our event handling in two
parts:

On the original event detect the file and the specified line.
Change to the file in a new window, remember the line and stop
the event handling for the first event.

On the second WIN_OPEN event for the correct file path simply
jump to the remembered line.

This allows both plugins vis-open-extend and vis-cursors to work,
if vis-open-extend is loaded after vis-cursors, so our event handler
runs after the one from vis-cursors.
---
 init.lua | 22 +++++++++++++++++++---
 1 file changed, 19 insertions(+), 3 deletions(-)

diff --git a/init.lua b/init.lua
index 2bb477c..3d46b89 100644
--- a/init.lua
+++ b/init.lua
@@ -7,22 +7,38 @@ local function file_exists(path)
	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 == 0 or not file_exists(path) then
	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)
	vis:command("+"..line-1)
--- or vis:feedkeys(tostring(line).."G") 
 -- Prevent other WIN_OPEN event handlers from running after we closed the window.
	return true
end)

-- 
2.46.2