Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
brianhuster committed Nov 7, 2024
1 parent 7df8c1f commit af9773e
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 42 deletions.
14 changes: 7 additions & 7 deletions lua/autosave.lua
Original file line number Diff line number Diff line change
@@ -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

Expand Down
35 changes: 0 additions & 35 deletions lua/autosave/compat.lua

This file was deleted.

44 changes: 44 additions & 0 deletions lua/autosave/nvim.lua
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit af9773e

Please sign in to comment.