diff options
Diffstat (limited to '.config/nvim/lua')
| -rw-r--r-- | .config/nvim/lua/autosave.lua | 80 | ||||
| -rw-r--r-- | .config/nvim/lua/vimrc.lua | 68 |
2 files changed, 114 insertions, 34 deletions
diff --git a/.config/nvim/lua/autosave.lua b/.config/nvim/lua/autosave.lua new file mode 100644 index 0000000..ae481ed --- /dev/null +++ b/.config/nvim/lua/autosave.lua @@ -0,0 +1,80 @@ +local M = {} + + +local INTERVAL_MS = 10 * 1000 +local SILENT = true + + +-- Because a timer cannot be converted to Vim value, +-- store indice of timers instead of timer instances. +local timers = {} + + +local function handler() + if not vim.bo.modified then + return + end + if vim.bo.readonly then + return + end + if vim.bo.buftype ~= '' then + return + end + if vim.fn.bufname() == '' then + return + end + + if not SILENT then + vim.cmd([[echohl Comment]]) + vim.cmd([[echo 'Auto-saving...']]) + vim.cmd([[echohl None]]) + end + + vim.cmd([[silent! write]]) + + if not SILENT then + vim.cmd([[echohl Comment]]) + vim.cmd([[echo 'Saved.']]) + vim.cmd([[echohl None]]) + end +end + + +function M.enable() + if vim.b.autosave_timer_id then + return + end + + local timer = vim.loop.new_timer() + timer:start(INTERVAL_MS, INTERVAL_MS, vim.schedule_wrap(handler)) + local tid = #timers + 1 + timers[tid] = timer + vim.b.autosave_timer_id = tid +end + + +function M.disable() + if not vim.b.autosave_timer_id then + return + end + + local tid = vim.b.autosave_timer_id + vim.b.autosave_timer_id = nil + if not timers[tid] then + return + end + timers[tid]:close() + timers[tid] = nil +end + + +function M.toggle() + if vim.b.autosave_timer_id then + M.disable() + else + M.enable() + end +end + + +return M diff --git a/.config/nvim/lua/vimrc.lua b/.config/nvim/lua/vimrc.lua index 0a2b9d9..15925e1 100644 --- a/.config/nvim/lua/vimrc.lua +++ b/.config/nvim/lua/vimrc.lua @@ -1,11 +1,11 @@ -local vimrc = {} +local M = {} local autocmd_callbacks = {} -vimrc.autocmd_callbacks = autocmd_callbacks +M.autocmd_callbacks = autocmd_callbacks -function vimrc.autocmd(event, filter, callback) +function M.autocmd(event, filter, callback) local callback_id = #autocmd_callbacks + 1 autocmd_callbacks[callback_id] = callback vim.cmd(('autocmd Vimrc %s %s lua vimrc.autocmd_callbacks[%d]()'):format( @@ -16,7 +16,7 @@ end -function vimrc.after_ftplugin(ft, callback) +function M.after_ftplugin(ft, callback) local var_name = 'did_ftplugin_' .. ft .. '_after' if vim.b[var_name] ~= nil then return @@ -29,42 +29,42 @@ end -local SPACE = true -local TAB = false - -local indentation_settings = { - c = { style = SPACE, width = 4 }, - cmake = { style = SPACE, width = 2 }, - cpp = { style = SPACE, width = 4 }, - css = { style = SPACE, width = 2 }, - go = { style = TAB, width = 4 }, - haskell = { style = SPACE, width = 4 }, - html = { style = SPACE, width = 2 }, - javascript = { style = SPACE, width = 2 }, - json = { style = SPACE, width = 2 }, - lisp = { style = SPACE, width = 2 }, - lua = { style = SPACE, width = 3 }, - markdown = { style = SPACE, width = 4 }, - php = { style = SPACE, width = 2 }, - python = { style = SPACE, width = 4 }, - ruby = { style = SPACE, width = 2 }, - toml = { style = SPACE, width = 2 }, - typescript = { style = SPACE, width = 2 }, - vim = { style = SPACE, width = 4 }, - yaml = { style = SPACE, width = 2 }, -} - -function vimrc.register_filetype_autocmds_for_indentation() +function M.register_filetype_autocmds_for_indentation() + local SPACE = true + local TAB = false + + local indentation_settings = { + c = { style = SPACE, width = 4 }, + cmake = { style = SPACE, width = 2 }, + cpp = { style = SPACE, width = 4 }, + css = { style = SPACE, width = 2 }, + go = { style = TAB, width = 4 }, + haskell = { style = SPACE, width = 4 }, + html = { style = SPACE, width = 2 }, + javascript = { style = SPACE, width = 2 }, + json = { style = SPACE, width = 2 }, + lisp = { style = SPACE, width = 2 }, + lua = { style = SPACE, width = 3 }, + markdown = { style = SPACE, width = 4 }, + php = { style = SPACE, width = 2 }, + python = { style = SPACE, width = 4 }, + ruby = { style = SPACE, width = 2 }, + toml = { style = SPACE, width = 2 }, + typescript = { style = SPACE, width = 2 }, + vim = { style = SPACE, width = 4 }, + yaml = { style = SPACE, width = 2 }, + } + for ft, setting in pairs(indentation_settings) do - vim.cmd(([[autocmd Vimrc FileType %s call v:lua.vimrc._set_indentation(%s, %d)]]):format( + vim.cmd(([[autocmd Vimrc FileType %s lua vimrc._set_indentation(%s, %d)]]):format( ft, - setting.style and 'v:true' or 'v:false', + setting.style, setting.width )) end end -function vimrc._set_indentation(style, width) +function M._set_indentation(style, width) vim.bo.expandtab = style vim.bo.tabstop = width vim.bo.shiftwidth = width @@ -77,4 +77,4 @@ end -return vimrc +return M |
