-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(Autocommand): refactor and also add auto save cmd
- Loading branch information
1 parent
2bb8dbc
commit 7e74151
Showing
3 changed files
with
93 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
local autocmd = vim.api.nvim_create_autocmd | ||
local augroup = vim.api.nvim_create_augroup("my_cfg", {}) | ||
|
||
---@type table<buffer, uv_timer_t> | ||
local timers = {} | ||
local timeout = 10000 | ||
|
||
local function save(buf) | ||
vim.api.nvim_buf_call(buf, function() vim.cmd("noautocmd update") end) | ||
end | ||
|
||
autocmd({ "InsertLeave", "TextChanged" }, { | ||
group = augroup, | ||
desc = "Schedule auto-saving", | ||
callback = function(event) | ||
local bo = vim.bo[event.buf] | ||
if event.file == "" or bo.buftype ~= "" or bo.filetype == "gitcommit" or bo.readonly or not bo.modified then | ||
return | ||
end | ||
|
||
local timer = timers[event.buf] | ||
if timer then | ||
if timer:is_active() then timer:stop() end | ||
else | ||
timer = vim.uv.new_timer() | ||
timers[event.buf] = timer | ||
end | ||
timer:start(timeout, 0, vim.schedule_wrap(function() save(event.buf) end)) | ||
end, | ||
}) | ||
|
||
autocmd({ "FocusLost", "ExitPre", "TermEnter" }, { | ||
group = augroup, | ||
desc = "Save immediately", | ||
callback = function(event) | ||
for buf, timer in pairs(timers) do | ||
if timer:is_active() then | ||
timer:stop() | ||
save(buf) | ||
end | ||
end | ||
end, | ||
}) | ||
|
||
autocmd({ "BufWritePost", "InsertEnter" }, { | ||
group = augroup, | ||
desc = "Cancel scheduled auto-saving", | ||
callback = function(event) | ||
local timer = timers[event.buf] | ||
if timer and timer:is_active() then timer:stop() end | ||
end, | ||
}) | ||
|
||
autocmd({ "BufDelete" }, { | ||
group = augroup, | ||
desc = "Remove timer", | ||
callback = function(event) | ||
local timer = timers[event.buf] | ||
if timer then | ||
timers[event.buf] = nil | ||
if timer:is_active() then timer:stop() end | ||
timer:close() | ||
end | ||
end, | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters