mirror of
https://github.com/SpaceVim/SpaceVim.git
synced 2025-02-09 10:20:06 +08:00
fix(autosave): fix lua autosave plugin
This commit is contained in:
parent
ad078317fb
commit
e9446e63fe
@ -6,115 +6,104 @@
|
|||||||
-- License: GPLv3
|
-- License: GPLv3
|
||||||
--=============================================================================
|
--=============================================================================
|
||||||
|
|
||||||
|
|
||||||
local default_opt = {
|
local default_opt = {
|
||||||
timeoutlen = 60 * 5 * 1000,
|
timeoutlen = 60 * 5 * 1000,
|
||||||
backupdir = '~/.cache/SpaceVim/backup/',
|
backupdir = '~/.cache/SpaceVim/backup/',
|
||||||
save_all_buffers = false,
|
save_all_buffers = false,
|
||||||
event = {'InsertLeave', 'TextChanged'},
|
event = { 'InsertLeave', 'TextChanged' },
|
||||||
filetype = {},
|
filetype = {},
|
||||||
filetypeExclude = {},
|
filetypeExclude = {},
|
||||||
buftypeExclude = {},
|
buftypeExclude = {},
|
||||||
bufNameExclude = {}
|
bufNameExclude = {},
|
||||||
}
|
}
|
||||||
|
|
||||||
local logger = require('spacevim.logger').derive('autosave')
|
local logger = require('spacevim.logger').derive('autosave')
|
||||||
local f = require('spacevim.api').import('file')
|
local f = require('spacevim.api').import('file')
|
||||||
|
|
||||||
|
|
||||||
local autosave_timer = -1
|
local autosave_timer = -1
|
||||||
|
|
||||||
local M = {}
|
local M = {}
|
||||||
|
|
||||||
function M.config(opt)
|
|
||||||
for option, value in ipairs(default_opt) do
|
|
||||||
if opt[option] ~= nil then
|
|
||||||
logger.debug('set option`' .. option .. '` to :' .. opt[option])
|
|
||||||
default_opt[option] = opt[option]
|
|
||||||
if option == 'timeoutlen' then
|
|
||||||
setup_timer(default_opt[option])
|
|
||||||
elseif option == 'event' then
|
|
||||||
setup_events()
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
end
|
|
||||||
|
|
||||||
local function location_path(bufname)
|
local function location_path(bufname)
|
||||||
if vim.fn.empty(default_opt.backupdir) == 1 then
|
if vim.fn.empty(default_opt.backupdir) == 1 then
|
||||||
return bufname
|
return bufname
|
||||||
else
|
else
|
||||||
local pth = f.unify_path(default_opt.backupdir, ':p') .. f.path_to_fname(bufname, '+=') .. '.backup'
|
local pth = f.unify_path(default_opt.backupdir, ':p')
|
||||||
logger.info('backup path is:' .. pth)
|
.. f.path_to_fname(bufname, '+=')
|
||||||
return pth
|
.. '.backup'
|
||||||
end
|
logger.info('backup path is:' .. pth)
|
||||||
|
return pth
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local function save_buffer(bufnr)
|
local function save_buffer(bufnr)
|
||||||
if vim.fn.getbufvar(bufnr, '&modified') == 1 and
|
if
|
||||||
vim.fn.empty(vim.fn.getbufvar(bufnr, '&buftype')) == 1 and
|
vim.fn.getbufvar(bufnr, '&modified') == 1
|
||||||
vim.fn.filewritable(vim.fn.bufname(bufnr)) == 1 and
|
and vim.fn.empty(vim.fn.getbufvar(bufnr, '&buftype')) == 1
|
||||||
vim.fn.empty(vim.fn.bufname(bufnr)) == 0 then
|
and vim.fn.filewritable(vim.fn.bufname(bufnr)) == 1
|
||||||
local lines = vim.fn.getbufline(bufnr, 1, '$')
|
and vim.fn.empty(vim.fn.bufname(bufnr)) == 0
|
||||||
local f = location_path(vim.fn.bufname(bufnr))
|
then
|
||||||
local ok, rst = pcall(vim.fn.writefile, lines, f)
|
local lines = vim.fn.getbufline(bufnr, 1, '$')
|
||||||
if not ok then
|
local f = location_path(vim.fn.bufname(bufnr))
|
||||||
logger.info('failed to autosave file:' .. f)
|
local ok, rst = pcall(vim.fn.writefile, lines, f)
|
||||||
logger.warn(rst)
|
if not ok then
|
||||||
end
|
logger.info('failed to autosave file:' .. f)
|
||||||
if vim.fn.empty(default_opt.backupdir) == 1 then
|
logger.warn(rst)
|
||||||
vim.fn.setbufvar(bufnr, '&modified', 0)
|
|
||||||
vim.cmd('silent checktime ' .. bufnr)
|
|
||||||
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
if vim.fn.empty(default_opt.backupdir) == 1 then
|
||||||
|
vim.fn.setbufvar(bufnr, '&modified', 0)
|
||||||
|
vim.cmd('silent checktime ' .. bufnr)
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local function auto_dosave(...)
|
local function auto_dosave(...)
|
||||||
if default_opt.save_all_buffers then
|
if default_opt.save_all_buffers then
|
||||||
for _, nr in ipairs(vim.fn.range(1, vim.fn.bufnr('$'))) do
|
for _, nr in ipairs(vim.fn.range(1, vim.fn.bufnr('$'))) do
|
||||||
save_buffer(nr)
|
save_buffer(nr)
|
||||||
end
|
|
||||||
|
|
||||||
else
|
|
||||||
save_buffer(vim.fn.bufnr('%'))
|
|
||||||
end
|
end
|
||||||
|
else
|
||||||
|
save_buffer(vim.fn.bufnr('%'))
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local function setup_timer(_timeoutlen)
|
local function setup_timer(_timeoutlen)
|
||||||
if vim.fn.has('timers') == 0 then
|
if vim.fn.has('timers') == 0 then
|
||||||
logger.warn('failed to setup timer, needs `+timers` feature!')
|
logger.warn('failed to setup timer, needs `+timers` feature!')
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
if _timeoutlen == 0 then
|
if _timeoutlen == 0 then
|
||||||
vim.fn.timer_stop(autosave_timer)
|
|
||||||
logger.debug('disabled autosave timer!')
|
|
||||||
end
|
|
||||||
if _timeoutlen < 1000 or _timeoutlen > 60 * 100 * 1000 then
|
|
||||||
local msg = "timeoutlen must be given in millisecods and can't be > 100*60*1000 (100 minutes) or < 1000 (1 second)"
|
|
||||||
logger.warn(msg)
|
|
||||||
return
|
|
||||||
end
|
|
||||||
vim.fn.timer_stop(autosave_timer)
|
vim.fn.timer_stop(autosave_timer)
|
||||||
autosave_timer = vim.fn.timer_start(_timeoutlen, auto_dosave, {['repeat'] = -1})
|
logger.debug('disabled autosave timer!')
|
||||||
if vim.fn.empty(autosave_timer) == 0 then
|
end
|
||||||
logger.debug('setup new autosave timer, timeoutlen:' .. _timeoutlen)
|
if _timeoutlen < 1000 or _timeoutlen > 60 * 100 * 1000 then
|
||||||
end
|
local msg =
|
||||||
|
"timeoutlen must be given in millisecods and can't be > 100*60*1000 (100 minutes) or < 1000 (1 second)"
|
||||||
|
logger.warn(msg)
|
||||||
|
return
|
||||||
|
end
|
||||||
|
vim.fn.timer_stop(autosave_timer)
|
||||||
|
autosave_timer = vim.fn.timer_start(_timeoutlen, auto_dosave, { ['repeat'] = -1 })
|
||||||
|
if vim.fn.empty(autosave_timer) == 0 then
|
||||||
|
logger.debug('setup new autosave timer, timeoutlen:' .. _timeoutlen)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
local function setup_events()
|
local function setup_events()
|
||||||
|
if #default_opt.event > 0 then
|
||||||
vim.api.nvim_create_autocmd(default_opt.event, {
|
vim.api.nvim_create_autocmd(default_opt.event, {
|
||||||
pattern = {'*'},
|
pattern = { '*' },
|
||||||
callback = auto_dosave,
|
callback = auto_dosave,
|
||||||
})
|
})
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
setup_timer(default_opt.timeoutlen)
|
function M.config(opt)
|
||||||
setup_events()
|
for option, value in pairs(default_opt) do
|
||||||
|
if opt[option] ~= nil then
|
||||||
|
logger.debug('set option`' .. option .. '` to :' .. vim.inspect(value))
|
||||||
|
default_opt[option] = value
|
||||||
|
end
|
||||||
|
end
|
||||||
|
setup_timer(default_opt.timeoutlen)
|
||||||
|
setup_events()
|
||||||
|
end
|
||||||
|
|
||||||
return M
|
return M
|
||||||
|
Loading…
Reference in New Issue
Block a user