From 25c3f58bd9550b18c3f289c9bee93a726359f810 Mon Sep 17 00:00:00 2001 From: Alin Mr Date: Fri, 3 May 2024 21:37:02 +0000 Subject: init.lua: working version --- init.lua | 85 ++++++++++++++++------------------------------------------------ 1 file changed, 21 insertions(+), 64 deletions(-) (limited to 'init.lua') diff --git a/init.lua b/init.lua index 5ef18a8..03da38e 100644 --- a/init.lua +++ b/init.lua @@ -21,6 +21,7 @@ local lockfiles = dofile(script_path .. 'lockfiles.lua') -- be done during the handler local pending_action = nil +--[[ local function count_windows() -- Count the currently open vis windows local n = 0 @@ -29,6 +30,7 @@ local function count_windows() end return n end +]] -- local M = {} M.directory = '${XDG_STATE_HOME:-${HOME}/.local/state}/vis/locks' @@ -49,97 +51,52 @@ local function prompt(path, lfile_path, pid) local msg = 'Lock file found!\n\t lock file: "' .. lfile_path .. '"\n\tfile name: ' .. path .. '\n\tprocess ID: ' .. pid - vis:command('open') - local prompt_win = vis.win - prompt_win.file:insert(0, msg) - prompt_win.selection.pos = 0 - - vis:redraw() - local menu_prompt = '(E)dit anyway, (Q)uit, [A]bort' - local cmd = 'printf %s \'' .. choices .. '\' | vis-menu -b -p \'' .. menu_prompt .. '\'' + local cmd1 = 'printf >/dev/tty %s \'' .. msg .. '\n\'' + local cmd2 = 'printf %s \'' .. choices .. '\'' + local cmd3 = 'vis-menu -b -p \'' .. menu_prompt .. '\'' + local cmd = cmd1 .. ';' .. cmd2 .. '|' .. cmd3 - local status, output = vis:pipe(prompt_win.file, {start = 0, finish = 0}, cmd) + local status, output = vis:pipe(cmd, true) vis:redraw() - local result = false if status == 0 then if output:sub(1, 1) == 'e' then - result = true + return true elseif output:sub(1, 1) == 'q' then vis:exit(1) end end - -- Queue up the closing of our prompt window - pending_action = function() - if count_windows() == 1 then - return false - end + -- else 'abort' or dialog canceled - -- 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 + local hnd1 + hnd1 = function() + if vis.win.file and vis.win.file.path == path then + vis.events.unsubscribe(vis.events.UI_DRAW, hnd1) + vis:command('q') + vis:command('new') end - - -- We closed our prompt window or did not find it - return true end - - return result + vis.events.subscribe(vis.events.UI_DRAW, hnd1) + vis:redraw() end local created_locks = {} -vis.events.subscribe(vis.events.FILE_OPEN, function(file) +vis.events.subscribe(vis.events.WIN_OPEN, function(win) + local file = win.file if created_locks[file.path] or not file.path then -- 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 - -- 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 + else + prompt(file.path, lfile_path, lock_pid) end end) -- cgit v1.2.3