From dacc81b2523c0ee99a3f290a1e3f5b8bb3ff5f8f Mon Sep 17 00:00:00 2001 From: Charles Danesi Date: Fri, 22 May 2026 13:56:57 -0400 Subject: [PATCH] chore: add autosave for markdown files --- .config/nvim/lua/core/autosave.lua | 42 ++++++++++++++++++++++++++++++ .config/nvim/lua/core/options.lua | 1 + 2 files changed, 43 insertions(+) create mode 100644 .config/nvim/lua/core/autosave.lua diff --git a/.config/nvim/lua/core/autosave.lua b/.config/nvim/lua/core/autosave.lua new file mode 100644 index 0000000..0ce29d1 --- /dev/null +++ b/.config/nvim/lua/core/autosave.lua @@ -0,0 +1,42 @@ +local group = vim.api.nvim_create_augroup('NotesAutosave', { clear = true }) + +local note_filetypes = { + markdown = true, + telekasten = true, +} + +local function should_save(buf) + if not vim.api.nvim_buf_is_valid(buf) then return false end + if not note_filetypes[vim.bo[buf].filetype] then return false end + if not vim.bo[buf].modified then return false end + if vim.bo[buf].readonly or not vim.bo[buf].modifiable then return false end + if vim.api.nvim_buf_get_name(buf) == '' then return false end + return true +end + +local function save(buf) + if should_save(buf) then vim.api.nvim_buf_call(buf, function() + vim.cmd('silent! write') + end) end +end + +vim.api.nvim_create_autocmd({ + 'BufLeave', + 'WinLeave', + 'FocusLost', +}, { + group = group, + callback = function(args) + save(args.buf) + end, +}) + +vim.api.nvim_create_autocmd({ + 'CursorHold', + 'CursorHoldI', +}, { + group = group, + callback = function(args) + save(args.buf) + end, +}) diff --git a/.config/nvim/lua/core/options.lua b/.config/nvim/lua/core/options.lua index 1a07440..b441b0b 100644 --- a/.config/nvim/lua/core/options.lua +++ b/.config/nvim/lua/core/options.lua @@ -8,6 +8,7 @@ o.clipboard:append('unnamedplus') -- use system clipboard as the default registe o.cdhome = false -- :cd shows cwd o.backspace = 'indent,eol,start' o.spelllang = 'en_us' +o.updatetime = 30000 -- AutoSave interval = 30 seconds -- ───────────────────────────────────────────────────── appearance ── o.showmode = false o.termguicolors = true