aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFlorian Fischer <florian.fischer@muhq.space>2024-05-03 21:04:28 +0200
committerFlorian Fischer <florian.fischer@muhq.space>2024-05-04 11:31:16 +0200
commitf3f2fd6ce357b2cad9b2d1d75092d894b81e26a3 (patch)
treecc74da50040198e679e07d91641d4176d99df3b3
parente70c92f0e351e26e81a7ea59682cf05c3a3bd909 (diff)
downloadvis-lockfiles-f3f2fd6ce357b2cad9b2d1d75092d894b81e26a3.tar.gz
vis-lockfiles-f3f2fd6ce357b2cad9b2d1d75092d894b81e26a3.zip
initial draft supporting autoclose and abort
Currently this breaks with my config which sets window options during WIN_OPEN events which causes a UI_DRAW event closing the window during its WIN_OPEN event.
-rw-r--r--init.lua82
1 files changed, 77 insertions, 5 deletions
diff --git a/init.lua b/init.lua
index 408d20c..5ef18a8 100644
--- a/init.lua
+++ b/init.lua
@@ -17,6 +17,19 @@ local script_path = source_str:match('(.*/)')
local lockfiles = dofile(script_path .. 'lockfiles.lua')
+-- function used for all actions resulting from a FILE_OPEN event that can not
+-- be done during the handler
+local pending_action = nil
+
+local function count_windows()
+ -- Count the currently open vis windows
+ local n = 0
+ for _ in vis:windows() do
+ n = n + 1
+ end
+ return n
+end
+
local M = {}
M.directory = '${XDG_STATE_HOME:-${HOME}/.local/state}/vis/locks'
M.directory_expanded = nil
@@ -58,9 +71,24 @@ local function prompt(path, lfile_path, pid)
end
end
- -- TODO: Auto close the prompt window
- -- Currently it fails because it is last only open window
- -- prompt_win:close(true)
+ -- Queue up the closing of our prompt window
+ pending_action = function()
+ if count_windows() == 1 then
+ return false
+ end
+
+ -- There are more windows than our prompt window, therefore we can close it
+ for win in vis:windows() do
+ if win.file:content(0, win.file.size) == msg then
+ win:close(true)
+ break
+ end
+ end
+
+ -- We closed our prompt window or did not find it
+ return true
+ end
+
return result
end
@@ -71,13 +99,47 @@ vis.events.subscribe(vis.events.FILE_OPEN, function(file)
-- either a noname buffer, or already locked by us
return
end
+
+ local initial_window = vis.win == nil
+ local cur_file = vis.win and vis.win.file and (vis.win.file.path or vis.win.file.name)
+
local lfile_path = lockfiles.lock_file_path(get_lock_dir(), file.path)
local lock_pid = lockfiles.create_lock_file(get_lock_dir(), lfile_path)
if lockfiles.vis_pid == lock_pid then
created_locks[file.path] = lfile_path
elseif not prompt(file.path, lfile_path, lock_pid) then
- -- TODO: abort
- vis:exit(1)
+ -- There is no window, therefore abort means exit
+ if initial_window then
+ vis:exit(1)
+ end
+
+ local prev_pending_action = pending_action
+ pending_action = function()
+ if prev_pending_action and not prev_pending_action() then
+ return false
+ end
+
+ pending_action = nil
+ vis:command('o ' .. (cur_file or ''))
+ pending_action = function()
+ os.execute('echo close locked file action >> lockfiles.log')
+ if count_windows() == 1 then
+ return false
+ end
+
+ -- close the window containing the locked file
+ for win in vis:windows() do
+ os.execute('echo ' .. ((win.file and win.file.path) or 'nil') .. ' >> lockfiles.log')
+ os.execute('printf %s \'' .. debug.traceback() .. '\n\' >> lockfiles.log')
+ if (win.file and win.file.path) == file.path then
+ win:close()
+ return true
+ end
+ end
+
+ return false
+ end
+ end
end
end)
@@ -101,4 +163,14 @@ vis.events.subscribe(vis.events.QUIT, function()
created_locks = {}
end)
+vis.events.subscribe(vis.events.UI_DRAW, function()
+ if not pending_action or not vis.win then
+ return
+ end
+
+ if pending_action() then
+ pending_action = nil
+ end
+end)
+
return M