mirror of
https://github.com/zoriya/auto-save.nvim.git
synced 2025-12-06 06:36:11 +00:00
first commit
This commit is contained in:
21
.editorconfig
Normal file
21
.editorconfig
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
# EditorConfig helps developers define and maintain consistent
|
||||||
|
# coding styles between different editors and IDEs
|
||||||
|
# EditorConfig is awesome: https://EditorConfig.org
|
||||||
|
|
||||||
|
root = true
|
||||||
|
|
||||||
|
|
||||||
|
[*]
|
||||||
|
end_of_line = lf
|
||||||
|
charset = utf-8
|
||||||
|
trim_trailing_whitespace = true
|
||||||
|
insert_final_newline = true
|
||||||
|
indent_style = tabs
|
||||||
|
indent_size = 4
|
||||||
|
|
||||||
|
[*.txt]
|
||||||
|
indent_style = tab
|
||||||
|
indent_size = 4
|
||||||
|
|
||||||
|
[*.{diff,md}]
|
||||||
|
trim_trailing_whitespace = false
|
||||||
7
CHANGELOG.md
Normal file
7
CHANGELOG.md
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
# Changelog
|
||||||
|
|
||||||
|
All notable changes to this project will be documented in this file.
|
||||||
|
|
||||||
|
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
|
||||||
|
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html)
|
||||||
|
|
||||||
0
doc/autosave.txt
Normal file
0
doc/autosave.txt
Normal file
36
lua/autosave/config.lua
Normal file
36
lua/autosave/config.lua
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
local config = {}
|
||||||
|
|
||||||
|
--
|
||||||
|
|
||||||
|
config.options = {
|
||||||
|
enabled = true,
|
||||||
|
execution_message = "AutoSave: saved at " .. vim.fn.strftime("%H:%M:%S"),
|
||||||
|
events = {"InsertLeave", "TextChanged"},
|
||||||
|
write_all_buffers = false,
|
||||||
|
on_off_commands = false,
|
||||||
|
save_only_if_exists = true,
|
||||||
|
excluded_filetypes = {},
|
||||||
|
}
|
||||||
|
|
||||||
|
function config.set_options(opts)
|
||||||
|
opts = opts or {}
|
||||||
|
|
||||||
|
for opt, _ in pairs(opts) do
|
||||||
|
-- check if option exists in the config's table
|
||||||
|
if (config.options[opt] ~= nil) then -- not nil
|
||||||
|
-- chec if option is a table
|
||||||
|
if (type(opts[opt]) == "table") then -- if table
|
||||||
|
for inner_opt, _ in pairs(opts[opt]) do
|
||||||
|
-- table contains element by that key
|
||||||
|
if (config.options[opt][inner_opt] ~= nil) then -- not nil
|
||||||
|
config.options[opt][inner_opt] = opts[opt][inner_opt]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
else
|
||||||
|
config.options[opt] = opts[opt]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
return config
|
||||||
19
lua/autosave/init.lua
Normal file
19
lua/autosave/init.lua
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
|
||||||
|
local opts = require("autosave.config").options
|
||||||
|
local cmd = vim.cmd
|
||||||
|
|
||||||
|
local M = {}
|
||||||
|
|
||||||
|
local function setup_commands()
|
||||||
|
if (opts["on_off_commands"] == true) then
|
||||||
|
cmd([[command! ASOn lua require'autosave.main'.main('on')]])
|
||||||
|
cmd([[command! ASOff lua require'autosave.main'.main('off')]])
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function M.setup(custom_opts)
|
||||||
|
require("autosave.config").set_options(custom_opts)
|
||||||
|
setup_commands()
|
||||||
|
end
|
||||||
|
|
||||||
|
return M
|
||||||
66
lua/autosave/main.lua
Normal file
66
lua/autosave/main.lua
Normal file
@@ -0,0 +1,66 @@
|
|||||||
|
local cmd = vim.cmd
|
||||||
|
|
||||||
|
local opts = require("autosave.config").options
|
||||||
|
local autocmds = require("autosave.modules.autocmds")
|
||||||
|
local autosave = require("autosave")
|
||||||
|
|
||||||
|
local M = {}
|
||||||
|
|
||||||
|
|
||||||
|
local function set_status(value)
|
||||||
|
status_autosave = value
|
||||||
|
end
|
||||||
|
|
||||||
|
local function get_status()
|
||||||
|
return status_autosave
|
||||||
|
end
|
||||||
|
|
||||||
|
local function on()
|
||||||
|
|
||||||
|
if (autosave.hook_before_on ~= nil) then
|
||||||
|
autosave.hook_before_on()
|
||||||
|
end
|
||||||
|
|
||||||
|
autocmds.load_autocommands()
|
||||||
|
set_status('on')
|
||||||
|
|
||||||
|
if (autosave.hook_after_on ~= nil) then
|
||||||
|
autosave.hook_after_on()
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
local function off()
|
||||||
|
|
||||||
|
if (autosave.hook_before_off ~= nil) then
|
||||||
|
autosave.hook_before_off()
|
||||||
|
end
|
||||||
|
|
||||||
|
autocmds.unload_autocommands()
|
||||||
|
set_status('off')
|
||||||
|
|
||||||
|
if (autosave.hook_after_off ~= nil) then
|
||||||
|
autosave.hook_after_off()
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function M.main(option)
|
||||||
|
option = option or 'load'
|
||||||
|
|
||||||
|
if (option == 'toggle') then
|
||||||
|
if (get_status() == 'on') then
|
||||||
|
off()
|
||||||
|
else
|
||||||
|
on()
|
||||||
|
end
|
||||||
|
elseif (option == 'on') then
|
||||||
|
on()
|
||||||
|
elseif (option == 'off') then
|
||||||
|
off()
|
||||||
|
elseif (option == 'startup') then
|
||||||
|
if (opts["enabled"] == true) then
|
||||||
|
on()
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
return M
|
||||||
91
lua/autosave/modules/autocmds.lua
Normal file
91
lua/autosave/modules/autocmds.lua
Normal file
@@ -0,0 +1,91 @@
|
|||||||
|
local opts = require("autosave.config").options
|
||||||
|
|
||||||
|
local api = vim.api
|
||||||
|
local fn = vim.fn
|
||||||
|
local cmd = vim.cmd
|
||||||
|
|
||||||
|
local M = {}
|
||||||
|
|
||||||
|
local function table_has_value(tbl, value)
|
||||||
|
for key, value in pairs(tbl) do
|
||||||
|
if (tbl[key] == value) then
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
|
||||||
|
local function set_modified(value)
|
||||||
|
modified = value
|
||||||
|
end
|
||||||
|
|
||||||
|
local function get_modified()
|
||||||
|
return modified
|
||||||
|
end
|
||||||
|
|
||||||
|
local function do_save()
|
||||||
|
if (opts["save_only_if_exists"] == true) then
|
||||||
|
if (fn.filereadable(fn.expand("%:p")) == 1) then
|
||||||
|
if not (next(opts["excluded_filetypes"]) == nil) then
|
||||||
|
if not (table_has_value(opts["excluded_filetypes"], api.nvim_eval([[&filetype]]))) then
|
||||||
|
-- might use update, but in that case it can't be checekd if a file was modified and so it will always
|
||||||
|
-- print opts["execution_message"]
|
||||||
|
if (api.nvim_eval([[&modified]]) == 1) then
|
||||||
|
cmd("write")
|
||||||
|
if (get_modified() == nil) then set_modified(true) end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
local function save()
|
||||||
|
if (opts["write_all_buffers"] == true) then
|
||||||
|
cmd([[call g:AutoSaveBufDo("lua require'autosave.modules.autocmds'.do_save()")]])
|
||||||
|
else
|
||||||
|
do_save()
|
||||||
|
end
|
||||||
|
|
||||||
|
if (opts["execution_message"] ~= "" and get_modified() == true) then
|
||||||
|
print(opts["execution_message"])
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
local function parse_events()
|
||||||
|
local events = ""
|
||||||
|
|
||||||
|
if (next(opts["events"]) == nil) then
|
||||||
|
events = "InsertLeave"
|
||||||
|
else
|
||||||
|
for event, _ in pairs(opts["events"]) do
|
||||||
|
events = events .. opts["events"][event] .. ","
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
return events
|
||||||
|
end
|
||||||
|
|
||||||
|
function M.load_autocommands()
|
||||||
|
api.nvim_exec(
|
||||||
|
[[
|
||||||
|
augroup autosave_save
|
||||||
|
autocmd!
|
||||||
|
autocmd ]] ..
|
||||||
|
parse_events() .. [[ * execute "lua require'autosave.modules.autocmds'.save()"
|
||||||
|
augroup END
|
||||||
|
]],
|
||||||
|
false
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
|
function M.unload_autocommands()
|
||||||
|
api.nvim_exec([[
|
||||||
|
augroup autosave_save
|
||||||
|
autocmd!
|
||||||
|
augroup END
|
||||||
|
]], false)
|
||||||
|
end
|
||||||
|
|
||||||
|
return M
|
||||||
15
lua/autosave/utils/viml_funcs.lua
Normal file
15
lua/autosave/utils/viml_funcs.lua
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
local api = vim.api
|
||||||
|
|
||||||
|
-- original source: https://vim.fandom.com/wiki/Run_a_command_in_multiple_buffers
|
||||||
|
-- like bufdo but restore the current buffer.
|
||||||
|
api.nvim_exec(
|
||||||
|
[[
|
||||||
|
function! g:AutoSaveBufDo(command)
|
||||||
|
let currBuff=bufnr("%")
|
||||||
|
execute 'bufdo ' . a:command
|
||||||
|
execute 'buffer ' . currBuff
|
||||||
|
endfunction
|
||||||
|
com! -nargs=+ -complete=command Bufdo call BufDo(<q-args>)
|
||||||
|
]],
|
||||||
|
false
|
||||||
|
)
|
||||||
22
plugin/ascmds.vim
Normal file
22
plugin/ascmds.vim
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
" GPL-3.0 License
|
||||||
|
|
||||||
|
" prevent the plugin from loading twice
|
||||||
|
if exists('g:loaded_autosave') | finish | endif
|
||||||
|
|
||||||
|
let s:save_cpo = &cpo " save user coptions
|
||||||
|
set cpo&vim " reset them to defaults
|
||||||
|
|
||||||
|
" main {{{
|
||||||
|
lua require('autosave.main').main('startup')
|
||||||
|
" }}}
|
||||||
|
|
||||||
|
" Interface {{{
|
||||||
|
command! ASToggle lua require'autosave.main'.main('toggle')
|
||||||
|
" }}}
|
||||||
|
|
||||||
|
|
||||||
|
let &cpo = s:save_cpo " restore after
|
||||||
|
unlet s:save_cpo
|
||||||
|
|
||||||
|
" set to true the var that controls the plugin's loading
|
||||||
|
let g:loaded_autosave = 1
|
||||||
5
research.txt
Normal file
5
research.txt
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
This plugin is for automatically saving files.
|
||||||
|
|
||||||
|
|
||||||
|
it should only write if:
|
||||||
|
1. buffer has been modified
|
||||||
Reference in New Issue
Block a user