### Fixed - `BufWritePre` formatters (e.g. conform.nvim format-on-save) now run on autosave. The trigger autocmd now uses `nested = true`, so the `:write` invoked from inside the `InsertLeave` / `TextChanged` / `FocusLost` handler fires nested autocmds (see `:help autocmd-nested`). ### Changed - Skip saves for unmodified, unnamed, or special (`buftype`) buffers instead of always attempting a write. - Drop `:wall` — autosave only writes the buffer the event fired on. - Use `silent` (not `silent!`) so write errors surface instead of being swallowed.
33 lines
827 B
Lua
33 lines
827 B
Lua
local M = {}
|
|
|
|
function M.setup(opts)
|
|
opts = opts or {}
|
|
local events = opts.events or { "InsertLeave", "TextChanged", "FocusLost" }
|
|
|
|
vim.opt.autowrite = true
|
|
vim.opt.autowriteall = true
|
|
|
|
vim.api.nvim_create_autocmd(events, {
|
|
group = vim.api.nvim_create_augroup("tiny-autosave", { clear = true }),
|
|
nested = true,
|
|
callback = function(args)
|
|
local buf = args.buf
|
|
if vim.bo[buf].buftype ~= "" or not vim.bo[buf].modified then
|
|
return
|
|
end
|
|
local name = vim.api.nvim_buf_get_name(buf)
|
|
if name == "" then
|
|
return
|
|
end
|
|
if vim.fn.filereadable(name) == 0 then
|
|
vim.fn.mkdir(vim.fn.fnamemodify(name, ":p:h"), "p")
|
|
end
|
|
vim.api.nvim_buf_call(buf, function()
|
|
vim.cmd("silent keepalt keepjumps write")
|
|
end)
|
|
end,
|
|
})
|
|
end
|
|
|
|
return M
|