Skip to content

Commit

Permalink
v0.4.0
Browse files Browse the repository at this point in the history
  • Loading branch information
brianhuster committed Nov 8, 2024
1 parent 95f5396 commit 47136ee
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 37 deletions.
24 changes: 19 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,27 +58,41 @@ git clone --depth 1 https://github.com/brianhuster/live-preview.nvim ~/.vim/pack

# Usage

## Enabling and Disabling Autosave
To enable autosave
```vim
:Autosave on
```

To toggle autosave on and off, you can use the `:AutoSave toggle` command. This command will enable autosave if it's currently disabled, and disable it if it's currently enabled.
To disable autosave
```vim
:Autosave off
```

## Checking Autosave Status
To toggle autosave
```vim
:Autosave toggle
```

To check the current autosave status, you can use the `:AutoSave status` command. This command will display a notification indicating whether autosave is currently enabled or disabled.
To check the current autosave status
```vim
:Autosave status
```

## Configuration

* Vimscript

```vim
g:autosave_enabled = v:true " Enable autosave when the plugin is loaded. Set to v:false to disable autosave, and only enable it when you run the :AutoSave toggle command.
g:autosave_disable_inside_paths = [] " A list of paths inside which autosave should be disabled. In Neovim, it is recommended to set this to [stdpath('config')] to disable autosave for files inside your Neovim configuration directory, so that Neovim doesn't reload whenever you type inside your configuration files.
```

* Lua
* Lua (only available in Neovim)

```lua
require("autosave").setup({
enabled = true, -- Enable autosave when the plugin is loaded. Set to false to disable autosave, and only enable it when you run the :AutoSave toggle command.
disable_inside_paths = {}, -- A list of paths inside which autosave should be disabled. In Neovim, it is recommended to set this to {vim.fn.stdpath('config')} to disable autosave for files inside your Neovim configuration directory, so that Neovim doesn't reload whenever you type inside your configuration files.
})
```

Expand Down
4 changes: 3 additions & 1 deletion RELEASE.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
## New features

Add checkhealth for Neovim users
New configuration options :

`g:autosave_disable_inside_paths` or `opts.disable_inside_paths` : A list of paths inside which autosave should be disabled. In Neovim, it is recommended to set this to [stdpath('config')] to disable autosave for files inside your Neovim configuration directory, so that Neovim doesn't reload whenever you type inside your configuration files.
72 changes: 56 additions & 16 deletions lua/autosave.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,70 @@ local bool = nvim.bool

local M = {}

local function hasFileName()
local filename = nvim.buf_get_name()
return filename ~= "" or filename ~= "[No Name]"
end

function M.save()
local buftype = nvim.eval('&buftype')
local autosave_enabled = bool(vim.g.autosave_enabled) and bool(vim.b.autosave_enabled)
print("Autosave enabled: " .. tostring(autosave_enabled))
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
if autosave_enabled and modifiable and modified then
nvim.exec2('silent! write', {})
end
end

function M.on()
vim.g.autosave_enabled = true
print("Autosave enabled")
end

function M.off()
vim.g.autosave_enabled = false
print("Autosave disabled")
end

function M.toggle()
vim.g.autosave_enabled = not bool(vim.g.autosave_enabled)
if bool(vim.g.autosave_enabled) then
print("Autosave enabled")
print("Autosave " .. (bool(vim.g.autosave_enabled) and "enabled" or "disabled"))
end

--- Execute Autosave command with args
--- @param args string
function M.execute(args)
if args[1] == "on" then
M.on()
elseif args[1] == "off" then
M.off()
elseif args[1] == "toggle" then
M.toggle()
elseif args[1] == "status" then
M.print_status()
else
print("Autosave disabled")
print("Invalid subcommand: " .. args)
end
end

--- Check if a buffer can be autosaved
--- @return boolean
function M.check_buffer()
--- Check if being a normal buffer
if nvim.eval("&buftype") ~= "" then
vim.b.autosave_enabled = false
return false
end

local path = nvim.buf_get_name()
for i = 1, #vim.g.autosave_disable_inside_paths do
local pattern = vim.g.autosave_disable_inside_paths[i]
pattern = vim.fn.expand(pattern)
if path:sub(1, #pattern) == pattern then
vim.b.autosave_enabled = false
return false
end
end
vim.b.autosave_enabled = true
return true
end

function M.status()
function M.print_status()
if bool(vim.g.autosave_enabled) then
print("Autosave is currently enabled")
else
Expand All @@ -44,11 +81,14 @@ function M.setup(user_config)
end

local default_config = {
enabled = true
enabled = true,
disable_inside_paths = {},
}

local config = vim.tbl_deep_extend("force", default_config, user_config or {})
vim.g.autosave_enabled = config.enabled
for key, value in pairs(config) do
vim.g["autosave_" .. key] = value
end
end

return M
25 changes: 10 additions & 15 deletions plugin/autosave.vim
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
" Check if the current Vim or Neovim version is compatible
let compatible = v:false
let min_nvim = '0.9'
let min_vim = '8.2.3288'
Expand All @@ -7,37 +8,32 @@ endif
if has("patch-" .. min_vim) && has("lua")
let compatible = v:true
endif

if !compatible
echoerr printf("autosave.vim requires Neovim %s or Vim %s with +lua feature", min_nvim, min_vim)
echoerr printf("autosave.vim requires Vim >= %s with +lua feature or Neovim >= %s", min_vim, min_nvim)
finish
endif


" Set the default values for configuration variables
if !exists('g:autosave_enabled')
let g:autosave_enabled = v:true
endif
if !exists('g:autosave_disable_on_path')
let g:autosave_disable_inside_paths = []
endif

" Create an autocmd group for autosave
augroup AutoSaveGroup
autocmd!
autocmd BufEnter * lua require'autosave'.check_buffer()
autocmd InsertLeave,TextChanged,TextChangedI * lua require'autosave'.save()
augroup END

" Create a user command for Autosave
command! -nargs=1 -complete=customlist,AutosaveComplete Autosave call s:Autosave(<f-args>)

function! s:Autosave(args) abort
if a:args == 'toggle'
lua require'autosave'.toggle()
elseif a:args == 'status'
lua require'autosave'.status()
else
echo "Unknown argument: " . a:args
endif
endfunction
command! -nargs=1 -complete=customlist,AutosaveComplete Autosave call luaeval("require'autosave'.execute(_A)", [<f-args>])

function! AutosaveComplete(ArgLead, CmdLine, CursorPos) abort
let subcommands = ['toggle', 'status']
let subcommands = ['on', 'off', 'toggle', 'status']
let cmd_parts = split(a:CmdLine, ' ')
if len(cmd_parts) < 2
return subcommands
Expand All @@ -50,5 +46,4 @@ function! AutosaveComplete(ArgLead, CmdLine, CursorPos) abort
endif
endfunction

autocmd BufRead,BufNewFile */autosave.nvim/doc/*.txt set filetype=help
set autowriteall

0 comments on commit 47136ee

Please sign in to comment.