aboutsummaryrefslogtreecommitdiff
path: root/init.lua
diff options
context:
space:
mode:
authorFlorian Fischer <florian.fischer@muhq.space>2024-05-04 14:38:49 +0200
committerFlorian Fischer <florian.fischer@muhq.space>2024-05-10 08:46:51 +0200
commitb0e4215ec52acc07b1ff15b1bcc7562e1cdbf7c3 (patch)
tree24271c3e4caf83f8550b5070b41ad0a5df4d70d1 /init.lua
parente814360daa0227124b83da1d83c4925f1f4665a3 (diff)
downloadvis-lockfiles-b0e4215ec52acc07b1ff15b1bcc7562e1cdbf7c3.tar.gz
vis-lockfiles-b0e4215ec52acc07b1ff15b1bcc7562e1cdbf7c3.zip
restore the previous file on abortimprove_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.
Diffstat (limited to 'init.lua')
-rw-r--r--init.lua25
1 files 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