diff --git a/lua/autosave.lua b/lua/autosave.lua index 6f0c811..a180682 100644 --- a/lua/autosave.lua +++ b/lua/autosave.lua @@ -1,22 +1,22 @@ -local compat = require('autosave.compat') -local bool = compat.bool +local nvim = require('autosave.nvim') +local bool = nvim.bool local M = {} local function hasFileName() - local filename = compat.bufname() + local filename = nvim.buf_get_name() return filename ~= "" or filename ~= "[No Name]" end function M.save() - local buftype = compat.option('buftype') - local modified = bool(compat.option('modified')) - local modifiable = bool(compat.option('modifiable')) + local buftype = nvim.eval('&buftype') + local modified = bool(nvim.eval('&modified')) + local modifiable = bool(nvim.eval('&modifiable')) if buftype ~= "" then return end if bool(vim.g.autosave_enabled) and hasFileName and modifiable and modified then - compat.cmd('silent! write') + nvim.exec2('silent! write') end end diff --git a/lua/autosave/compat.lua b/lua/autosave/compat.lua deleted file mode 100644 index 5754322..0000000 --- a/lua/autosave/compat.lua +++ /dev/null @@ -1,35 +0,0 @@ -local M = {} - -function M.cmd(command) - if M.bool(vim.fn.has('nvim')) then - vim.cmd(command) - else - vim.command(command) - end -end - -function M.bool(value) - if value == 0 or not value then - return false - else - return true - end -end - -function M.bufname() - if M.bool(vim.fn.has('nvim')) then - return vim.api.nvim_buf_get_name(0) - else - return vim.buffer().fname - end -end - -function M.option(key) - if M.bool(vim.fn.has('nvim')) then - return vim.o[key] - else - return vim.eval('&' .. key) - end -end - -return M diff --git a/lua/autosave/nvim.lua b/lua/autosave/nvim.lua new file mode 100644 index 0000000..1336411 --- /dev/null +++ b/lua/autosave/nvim.lua @@ -0,0 +1,44 @@ +local M = {} + +--- Execute block of VimL code +--- @param command string +--- @param opts table +--- - output boolean +--- @return string|nil +function M.exec2(command, opts) + if M.bool(vim.fn.has('nvim')) then + vim.api.nvim_exec2(command, opts) + else + if opts and opts.output then + return vim.fn.execute(command) + else + vim.command(command) + end + end +end + +function M.bool(value) + if value == 0 or not value then + return false + else + return true + end +end + +function M.buf_get_name() + if M.bool(vim.fn.has('nvim')) then + return vim.api.nvim_buf_get_name(0) + else + return vim.buffer().fname + end +end + +function M.eval(expr) + if M.bool(vim.fn.has('nvim')) then + return vim.api.nvim_eval(expr) + else + return vim.eval(expr) + end +end + +return M