mirror of
https://github.com/zoriya/auto-save.nvim.git
synced 2026-06-03 10:56:22 +00:00
fix #46 and format
This commit is contained in:
+133
-87
@@ -15,7 +15,7 @@ local BLACK = "#000000"
|
|||||||
local WHITE = "#ffffff"
|
local WHITE = "#ffffff"
|
||||||
|
|
||||||
api.nvim_create_augroup("AutoSave", {
|
api.nvim_create_augroup("AutoSave", {
|
||||||
clear = true,
|
clear = true,
|
||||||
})
|
})
|
||||||
|
|
||||||
local global_vars = {}
|
local global_vars = {}
|
||||||
@@ -24,7 +24,15 @@ local function set_buf_var(buf, name, value)
|
|||||||
if buf == nil then
|
if buf == nil then
|
||||||
global_vars[name] = value
|
global_vars[name] = value
|
||||||
else
|
else
|
||||||
api.nvim_buf_set_var(buf, 'autosave_' .. name, value)
|
local buffers = {}
|
||||||
|
local tabwins = vim.api.nvim_tabpage_list_wins(0)
|
||||||
|
for _, w in ipairs(tabwins) do
|
||||||
|
local buffer = vim.api.nvim_win_get_buf(w)
|
||||||
|
table.insert(buffers, buffer)
|
||||||
|
end
|
||||||
|
if vim.tbl_contains(buffers, buf) then
|
||||||
|
api.nvim_buf_set_var(buf, "autosave_" .. name, value)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -32,128 +40,166 @@ local function get_buf_var(buf, name)
|
|||||||
if buf == nil then
|
if buf == nil then
|
||||||
return global_vars[name]
|
return global_vars[name]
|
||||||
end
|
end
|
||||||
local success, mod = pcall(api.nvim_buf_get_var, buf, 'autosave_' .. name)
|
local success, mod = pcall(api.nvim_buf_get_var, buf, "autosave_" .. name)
|
||||||
return success and mod or nil
|
return success and mod or nil
|
||||||
end
|
end
|
||||||
|
|
||||||
local function debounce(lfn, duration)
|
local function debounce(lfn, duration)
|
||||||
local function inner_debounce()
|
local function inner_debounce()
|
||||||
local buf = api.nvim_get_current_buf()
|
local buf = api.nvim_get_current_buf()
|
||||||
if not get_buf_var(buf, 'queued') then
|
if not get_buf_var(buf, "queued") then
|
||||||
vim.defer_fn(function()
|
vim.defer_fn(function()
|
||||||
set_buf_var(buf, 'queued', false)
|
set_buf_var(buf, "queued", false)
|
||||||
lfn(buf)
|
lfn(buf)
|
||||||
end, duration)
|
end, duration)
|
||||||
set_buf_var(buf, 'queued', true)
|
set_buf_var(buf, "queued", true)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
return inner_debounce
|
return inner_debounce
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.save(buf)
|
function M.save(buf)
|
||||||
buf = buf or api.nvim_get_current_buf()
|
buf = buf or api.nvim_get_current_buf()
|
||||||
|
|
||||||
callback("before_asserting_save")
|
callback("before_asserting_save")
|
||||||
|
|
||||||
if cnf.opts.condition(buf) == false then
|
if cnf.opts.condition(buf) == false then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
if not api.nvim_buf_get_option(buf, 'modified') then
|
if not api.nvim_buf_get_option(buf, "modified") then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
callback("before_saving")
|
callback("before_saving")
|
||||||
|
|
||||||
if g.auto_save_abort == true then
|
if g.auto_save_abort == true then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
if cnf.opts.write_all_buffers then
|
if cnf.opts.write_all_buffers then
|
||||||
cmd("silent! wall")
|
cmd("silent! wall")
|
||||||
else
|
else
|
||||||
api.nvim_buf_call(buf, function () cmd("silent! write") end)
|
api.nvim_buf_call(buf, function()
|
||||||
end
|
cmd("silent! write")
|
||||||
|
end)
|
||||||
|
end
|
||||||
|
|
||||||
callback("after_saving")
|
callback("after_saving")
|
||||||
|
|
||||||
api.nvim_echo({ { (type(cnf.opts.execution_message.message) == "function" and cnf.opts.execution_message.message() or cnf.opts.execution_message.message), AUTO_SAVE_COLOR } }, true, {})
|
api.nvim_echo(
|
||||||
if cnf.opts.execution_message.cleaning_interval > 0 then
|
{
|
||||||
fn.timer_start(
|
{
|
||||||
cnf.opts.execution_message.cleaning_interval,
|
(
|
||||||
function()
|
type(cnf.opts.execution_message.message) == "function"
|
||||||
cmd([[echon '']])
|
and cnf.opts.execution_message.message()
|
||||||
end
|
or cnf.opts.execution_message.message
|
||||||
)
|
),
|
||||||
end
|
AUTO_SAVE_COLOR,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
true,
|
||||||
|
{}
|
||||||
|
)
|
||||||
|
if cnf.opts.execution_message.cleaning_interval > 0 then
|
||||||
|
fn.timer_start(cnf.opts.execution_message.cleaning_interval, function()
|
||||||
|
cmd([[echon '']])
|
||||||
|
end)
|
||||||
|
end
|
||||||
|
api.nvim_echo({
|
||||||
|
{
|
||||||
|
(
|
||||||
|
type(cnf.opts.execution_message.message) == "function"
|
||||||
|
and cnf.opts.execution_message.message()
|
||||||
|
or cnf.opts.execution_message.message
|
||||||
|
),
|
||||||
|
AUTO_SAVE_COLOR,
|
||||||
|
},
|
||||||
|
}, true, {})
|
||||||
|
if cnf.opts.execution_message.cleaning_interval > 0 then
|
||||||
|
fn.timer_start(cnf.opts.execution_message.cleaning_interval, function()
|
||||||
|
cmd([[echon '']])
|
||||||
|
end)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local save_func = (cnf.opts.debounce_delay > 0 and debounce(M.save, cnf.opts.debounce_delay) or M.save)
|
local save_func = (
|
||||||
|
cnf.opts.debounce_delay > 0 and debounce(M.save, cnf.opts.debounce_delay) or M.save
|
||||||
|
)
|
||||||
|
|
||||||
local function perform_save()
|
local function perform_save()
|
||||||
g.auto_save_abort = false
|
g.auto_save_abort = false
|
||||||
save_func()
|
save_func()
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.on()
|
function M.on()
|
||||||
api.nvim_create_autocmd(cnf.opts.trigger_events, {
|
api.nvim_create_autocmd(cnf.opts.trigger_events, {
|
||||||
callback = function()
|
callback = function()
|
||||||
perform_save()
|
perform_save()
|
||||||
end,
|
end,
|
||||||
pattern = "*",
|
pattern = "*",
|
||||||
group = "AutoSave",
|
group = "AutoSave",
|
||||||
})
|
})
|
||||||
|
|
||||||
api.nvim_create_autocmd({"VimEnter", "ColorScheme", "UIEnter"}, {
|
api.nvim_create_autocmd({ "VimEnter", "ColorScheme", "UIEnter" }, {
|
||||||
callback = function()
|
callback = function()
|
||||||
vim.schedule(function()
|
vim.schedule(function()
|
||||||
if cnf.opts.execution_message.dim > 0 then
|
if cnf.opts.execution_message.dim > 0 then
|
||||||
MSG_AREA = colors.get_hl("MsgArea")
|
MSG_AREA = colors.get_hl("MsgArea")
|
||||||
if MSG_AREA.foreground ~= nil then
|
if MSG_AREA.foreground ~= nil then
|
||||||
MSG_AREA.background = (MSG_AREA.background or colors.get_hl("Normal")["background"])
|
MSG_AREA.background = (
|
||||||
local foreground = (
|
MSG_AREA.background or colors.get_hl("Normal")["background"]
|
||||||
o.background == "dark" and
|
)
|
||||||
colors.darken((MSG_AREA.background or BLACK), cnf.opts.execution_message.dim, MSG_AREA.foreground or BLACK) or
|
local foreground = (
|
||||||
colors.lighten((MSG_AREA.background or WHITE), cnf.opts.execution_message.dim, MSG_AREA.foreground or WHITE)
|
o.background == "dark"
|
||||||
)
|
and colors.darken(
|
||||||
|
(MSG_AREA.background or BLACK),
|
||||||
|
cnf.opts.execution_message.dim,
|
||||||
|
MSG_AREA.foreground or BLACK
|
||||||
|
)
|
||||||
|
or colors.lighten(
|
||||||
|
(MSG_AREA.background or WHITE),
|
||||||
|
cnf.opts.execution_message.dim,
|
||||||
|
MSG_AREA.foreground or WHITE
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
colors.highlight("AutoSaveText", { fg = foreground })
|
colors.highlight("AutoSaveText", { fg = foreground })
|
||||||
AUTO_SAVE_COLOR = "AutoSaveText"
|
AUTO_SAVE_COLOR = "AutoSaveText"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
end,
|
end,
|
||||||
pattern = "*",
|
pattern = "*",
|
||||||
group = "AutoSave",
|
group = "AutoSave",
|
||||||
})
|
})
|
||||||
|
|
||||||
callback("enabling")
|
callback("enabling")
|
||||||
autosave_running = true
|
autosave_running = true
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.off()
|
function M.off()
|
||||||
|
api.nvim_create_augroup("AutoSave", {
|
||||||
|
clear = true,
|
||||||
|
})
|
||||||
|
|
||||||
api.nvim_create_augroup("AutoSave", {
|
callback("disabling")
|
||||||
clear = true,
|
autosave_running = false
|
||||||
})
|
|
||||||
|
|
||||||
callback("disabling")
|
|
||||||
autosave_running = false
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.toggle()
|
function M.toggle()
|
||||||
if autosave_running then
|
if autosave_running then
|
||||||
M.off()
|
M.off()
|
||||||
echo("off")
|
echo("off")
|
||||||
else
|
else
|
||||||
M.on()
|
M.on()
|
||||||
echo("on")
|
echo("on")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.setup(custom_opts)
|
function M.setup(custom_opts)
|
||||||
cnf:set_options(custom_opts)
|
cnf:set_options(custom_opts)
|
||||||
end
|
end
|
||||||
|
|
||||||
return M
|
return M
|
||||||
|
|||||||
Reference in New Issue
Block a user