From a230f0ac3fbd0a9540329b7951b1e8f3eda797de Mon Sep 17 00:00:00 2001 From: Florian Fischer Date: Fri, 15 May 2026 23:47:29 +0200 Subject: simplify code by use vis' capability to change files during WIN_OPEN --- LICENSE | 1 + README.md | 6 ------ init.lua | 25 ++++++++----------------- 3 files changed, 9 insertions(+), 23 deletions(-) diff --git a/LICENSE b/LICENSE index fa6b85a..d7fc67c 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,7 @@ MIT License Copyright (c) 2024 Matěj Cepl +Copyright (c) 2026 Florian Fischer Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/README.md b/README.md index f3a1e7a..b376141 100644 --- a/README.md +++ b/README.md @@ -6,9 +6,3 @@ $ vis filename.txt:9 ``` opens the file `filename.txt` on line 9. - -All issues, questions, complaints, or (even better!) patches -should be send via email to -[~mcepl/devel@lists.sr.ht](mailto:~mcepl/devel@lists.sr.ht) email -list (for patches use [git send-email](https://git-send-email.io/)). - diff --git a/init.lua b/init.lua index 3d46b89..3f830cd 100644 --- a/init.lua +++ b/init.lua @@ -7,20 +7,10 @@ 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 @@ -31,14 +21,15 @@ vis.events.subscribe(vis.events.WIN_OPEN, function(win) 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. + -- 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. + vis:feedkeys(line.."G") + + -- Stop handling the old FILE_OPEN event since we changed the file and emitted + -- a new WIN_OPEN event return true end) -- cgit v1.2.3