From b0e4215ec52acc07b1ff15b1bcc7562e1cdbf7c3 Mon Sep 17 00:00:00 2001 From: Florian Fischer Date: Sat, 4 May 2024 14:38:49 +0200 Subject: restore the previous file on abort This feature requires a vis build supporting changing a window's file from Lua. To enable it set the module's change_file_on_abort member to true. --- init.lua | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/init.lua b/init.lua index ce5145c..af96399 100644 --- a/init.lua +++ b/init.lua @@ -20,6 +20,8 @@ local lockfiles = dofile(script_path .. 'lockfiles.lua') local M = {} M.directory = '${XDG_STATE_HOME:-${HOME}/.local/state}/vis/locks' M.directory_expanded = nil +-- Enable this if vis supports changing a window's file from Lua +M.change_file_on_abort = false -- Detect available backends if os.execute('command -v whiptail') then @@ -66,6 +68,12 @@ local function prompt_backend(path, lfile_path, pid, backend) return (status == 0) and output:sub(1, 1) end +local cur_file +vis.events.subscribe(vis.events.FILE_OPEN, function() + -- remember the current file to reopen it when aborting an edit attempt + cur_file = vis.win and vis.win.file and (vis.win.file.path or vis.win.file.name) +end) + -- get a user confirmation -- return true if user wants to edit the locked file local function prompt(path, lfile_path, pid) @@ -76,10 +84,6 @@ local function prompt(path, lfile_path, pid) elseif output == 'q' then vis:exit(1) end - - -- else 'abort' or dialog canceled - vis:command('q') - vis:command('new') end local created_locks = {} @@ -97,7 +101,18 @@ vis.events.subscribe(vis.events.WIN_OPEN, function(win) created_locks[file.path] = lfile_path else if not prompt(file.path, lfile_path, lock_pid) then - -- return ~= nil to stop further event handling since we closed the window + -- 'abort' or dialog canceled + if cur_file and M.change_file_on_abort then + -- restore the previous file + win.file = cur_file + -- fire a new WIN_OPEN event because we changed the file + vis.events.emit(vis.events.WIN_OPEN, win) + else + vis:command('q') + vis:command('new') + end + -- stop handling the old FILE_OPEN event either we closed it + -- or changed its file and emitted a new WIN_OPEN event return true end end -- cgit v1.2.3