mirror of
https://github.com/SpaceVim/SpaceVim.git
synced 2025-02-13 13:37:57 +08:00
fix(autosave): fix lua autosave plugin
This commit is contained in:
parent
ad078317fb
commit
e9446e63fe
@ -6,7 +6,6 @@
|
|||||||
-- 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/',
|
||||||
@ -15,47 +14,33 @@ local default_opt = {
|
|||||||
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')
|
||||||
|
.. f.path_to_fname(bufname, '+=')
|
||||||
|
.. '.backup'
|
||||||
logger.info('backup path is:' .. pth)
|
logger.info('backup path is:' .. pth)
|
||||||
return pth
|
return pth
|
||||||
end
|
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
|
||||||
|
and vim.fn.empty(vim.fn.bufname(bufnr)) == 0
|
||||||
|
then
|
||||||
local lines = vim.fn.getbufline(bufnr, 1, '$')
|
local lines = vim.fn.getbufline(bufnr, 1, '$')
|
||||||
local f = location_path(vim.fn.bufname(bufnr))
|
local f = location_path(vim.fn.bufname(bufnr))
|
||||||
local ok, rst = pcall(vim.fn.writefile, lines, f)
|
local ok, rst = pcall(vim.fn.writefile, lines, f)
|
||||||
@ -66,10 +51,8 @@ local function save_buffer(bufnr)
|
|||||||
if vim.fn.empty(default_opt.backupdir) == 1 then
|
if vim.fn.empty(default_opt.backupdir) == 1 then
|
||||||
vim.fn.setbufvar(bufnr, '&modified', 0)
|
vim.fn.setbufvar(bufnr, '&modified', 0)
|
||||||
vim.cmd('silent checktime ' .. bufnr)
|
vim.cmd('silent checktime ' .. bufnr)
|
||||||
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
local function auto_dosave(...)
|
local function auto_dosave(...)
|
||||||
@ -77,13 +60,10 @@ local function auto_dosave(...)
|
|||||||
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
|
end
|
||||||
|
|
||||||
else
|
else
|
||||||
save_buffer(vim.fn.bufnr('%'))
|
save_buffer(vim.fn.bufnr('%'))
|
||||||
end
|
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!')
|
||||||
@ -94,7 +74,8 @@ local function setup_timer(_timeoutlen)
|
|||||||
logger.debug('disabled autosave timer!')
|
logger.debug('disabled autosave timer!')
|
||||||
end
|
end
|
||||||
if _timeoutlen < 1000 or _timeoutlen > 60 * 100 * 1000 then
|
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)"
|
local msg =
|
||||||
|
"timeoutlen must be given in millisecods and can't be > 100*60*1000 (100 minutes) or < 1000 (1 second)"
|
||||||
logger.warn(msg)
|
logger.warn(msg)
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
@ -103,18 +84,26 @@ local function setup_timer(_timeoutlen)
|
|||||||
if vim.fn.empty(autosave_timer) == 0 then
|
if vim.fn.empty(autosave_timer) == 0 then
|
||||||
logger.debug('setup new autosave timer, timeoutlen:' .. _timeoutlen)
|
logger.debug('setup new autosave timer, timeoutlen:' .. _timeoutlen)
|
||||||
end
|
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
|
||||||
|
|
||||||
|
function M.config(opt)
|
||||||
|
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_timer(default_opt.timeoutlen)
|
||||||
setup_events()
|
setup_events()
|
||||||
|
end
|
||||||
|
|
||||||
return M
|
return M
|
||||||
|
Loading…
Reference in New Issue
Block a user