feat: GH-44 lock marks when saving (#53)

Use `lockmarks` in write command to keep `[` and `]` marks after saving.

Thanks @simonmandlik

---------

Co-authored-by: okuuva <okuuva@users.noreply.github.com>
This commit is contained in:
Toni Müller
2024-04-25 08:34:17 +02:00
committed by GitHub
parent 213f92ad96
commit fddbf38851
3 changed files with 12 additions and 2 deletions

View File

@@ -113,6 +113,7 @@ EOF
condition = nil, condition = nil,
write_all_buffers = false, -- write all buffers when the current one meets `condition` write_all_buffers = false, -- write all buffers when the current one meets `condition`
noautocmd = false, -- do not execute autocmds when saving noautocmd = false, -- do not execute autocmds when saving
lockmarks = false, -- lock marks when saving, see `:h lockmarks` for more details
debounce_delay = 1000, -- delay after which a pending save is executed debounce_delay = 1000, -- delay after which a pending save is executed
-- log debug messages to 'auto-save.log' file in neovim cache directory, set to `true` to enable -- log debug messages to 'auto-save.log' file in neovim cache directory, set to `true` to enable
debug = false, debug = false,

View File

@@ -27,6 +27,7 @@ Config = {
condition = nil, condition = nil,
write_all_buffers = false, -- write all buffers when the current one meets `condition` write_all_buffers = false, -- write all buffers when the current one meets `condition`
noautocmd = false, -- do not execute autocmds when saving noautocmd = false, -- do not execute autocmds when saving
lockmarks = false, -- lock marks when saving, see `:h lockmarks` for more details
debounce_delay = 1000, -- delay after which a pending save is executed debounce_delay = 1000, -- delay after which a pending save is executed
-- log debug messages to 'auto-save.log' file in neovim cache directory, set to `true` to enable -- log debug messages to 'auto-save.log' file in neovim cache directory, set to `true` to enable
debug = false, -- print debug messages, set to `true` to enable debug = false, -- print debug messages, set to `true` to enable

View File

@@ -18,6 +18,7 @@ autocmds.create_augroup({ clear = true })
local timers_by_buffer = {} local timers_by_buffer = {}
--- @param buf number
local function cancel_timer(buf) local function cancel_timer(buf)
local timer = timers_by_buffer[buf] local timer = timers_by_buffer[buf]
if timer ~= nil then if timer ~= nil then
@@ -28,6 +29,9 @@ local function cancel_timer(buf)
end end
end end
--- @param lfn fun(buf: number) The function to debounce
--- @param duration number The debounce duration
--- @return fun(buf: number) debounced The debounced function
local function debounce(lfn, duration) local function debounce(lfn, duration)
local function inner_debounce(buf) local function inner_debounce(buf)
-- instead of canceling the timer we could check if there is one already running for this buffer and restart it (`:again`) -- instead of canceling the timer we could check if there is one already running for this buffer and restart it (`:again`)
@@ -72,6 +76,7 @@ local function should_be_saved(buf)
return true return true
end end
--- @param buf number
local function save(buf) local function save(buf)
if not api.nvim_buf_is_loaded(buf) then if not api.nvim_buf_is_loaded(buf) then
return return
@@ -86,11 +91,12 @@ local function save(buf)
autocmds.exec_autocmd("AutoSaveWritePre", { saved_buffer = buf }) autocmds.exec_autocmd("AutoSaveWritePre", { saved_buffer = buf })
local noautocmd = cnf.opts.noautocmd and "noautocmd " or "" local noautocmd = cnf.opts.noautocmd and "noautocmd " or ""
local lockmarks = cnf.opts.lockmarks and "lockmarks " or ""
if cnf.opts.write_all_buffers then if cnf.opts.write_all_buffers then
cmd(noautocmd .. "silent! wall") cmd(noautocmd .. lockmarks .. "silent! wall")
else else
api.nvim_buf_call(buf, function() api.nvim_buf_call(buf, function()
cmd(noautocmd .. "silent! write") cmd(noautocmd .. lockmarks .. "silent! write")
end) end)
end end
@@ -102,12 +108,14 @@ local function save(buf)
end end
end end
--- @param buf number
local function immediate_save(buf) local function immediate_save(buf)
cancel_timer(buf) cancel_timer(buf)
save(buf) save(buf)
end end
local save_func = nil local save_func = nil
--- @param buf number
local function defer_save(buf) local function defer_save(buf)
-- is it really needed to cache this function -- is it really needed to cache this function
-- TODO: remove? -- TODO: remove?