馃尡 tiny neovim plugin keeping your session safe
1if vim.g.loaded_session then
2 return
3end
4
5vim.g.loaded_session = true
6
7local group = vim.api.nvim_create_augroup('nivvie', { clear = true })
8
9vim.api.nvim_create_autocmd({ 'VimLeavePre' }, {
10 group = group,
11 callback = function()
12 require('nivvie').autosave()
13 end,
14})
15
16vim.api.nvim_create_autocmd({ 'StdinReadPost' }, {
17 group = group,
18 callback = function()
19 require('nivvie').stdin = true
20 end,
21})
22
23vim.api.nvim_create_autocmd({ 'DirChangedPre' }, {
24 group = group,
25 pattern = 'global',
26 callback = function()
27 require('nivvie').save()
28 end,
29})
30
31vim.api.nvim_create_autocmd({ 'DirChanged' }, {
32 group = group,
33 pattern = 'global',
34 callback = function()
35 require('nivvie').restore()
36 end,
37})
38
39vim.schedule(function()
40 vim.api.nvim_create_user_command('Nivvie', function(args)
41 if args.fargs[1] == 'save' then
42 require('nivvie').save(args.fargs[2])
43 return
44 elseif args.fargs[1] == 'restore' then
45 require('nivvie').restore(args.fargs[2])
46 return
47 elseif args.fargs[1] == 'delete' then
48 local path = require('nivvie').get_path(args.fargs[2])
49 if vim.uv.fs_stat(path) then
50 vim.uv.fs_unlink(path)
51 end
52 end
53 end, {
54 nargs = '+',
55 complete = function()
56 return {
57 'save',
58 'restore',
59 'delete',
60 }
61 end,
62 })
63end)
64
65----
66
67if vim.v.vim_did_enter > 0 then
68 vim.schedule(function()
69 require('nivvie').autorestore()
70 end)
71 return
72end
73
74vim.api.nvim_create_autocmd({ 'VimEnter' }, {
75 group = group,
76 callback = function()
77 vim.schedule(function()
78 require('nivvie').autorestore()
79 end)
80 end,
81})