Prevent saving an "empty" session
okuuva opened this issue · comments
My use case is the following:
I want to autoload a session for the cwd at start if it exits. If not I want to open alpha dashboard. I have setup alpha
not to start automatically and have the following function as on_autoload_no_session
function:
local on_autoload_no_session = function()
-- need to pass true (on_vimenter) so that alpha clears the unnamed buffer vim creates at start
require("alpha").start(true)
end
The first run everything goes fine, alpha
dashboard starts up:
(just noticed the typo in "Find sesssion" :))
Then I hit <leader>q
which is bound to :qa!
in the dashboard settings.
The next time I open nvim in the same cwd, persisted
loads practically an empty session:
Here's the actual session.vim file saved:
let SessionLoad = 1
let s:so_save = &g:so | let s:siso_save = &g:siso | setg so=0 siso=0 | setl so=-1 siso=-1
let v:this_session=expand("<sfile>:p")
silent only
cd ~/gits/persisted.nvim
if expand('%') == '' && !&modified && line('$') <= 1 && getline(1) == ''
let s:wipebuf = bufnr('%')
endif
let s:shortmess_save = &shortmess
if &shortmess =~ 'A'
set shortmess=aoOA
else
set shortmess=aoO
endif
argglobal
%argdel
set lines=61 columns=232
if exists('s:wipebuf') && len(win_findbuf(s:wipebuf)) == 0 && getbufvar(s:wipebuf, '&buftype') isnot# 'terminal'
silent exe 'bwipe ' . s:wipebuf
endif
unlet! s:wipebuf
set winheight=1 winwidth=20
let &shortmess = s:shortmess_save
let s:sx = expand("<sfile>:p:r")."x.vim"
if filereadable(s:sx)
exe "source " . fnameescape(s:sx)
endif
let &g:so = s:so_save | let &g:siso = s:siso_save
set hlsearch
nohlsearch
doautoall SessionLoadPost
unlet SessionLoad
" vim: set ft=vim :
What should've happened (imo) is that the alpha
dashboard would've opened instead of this "session".
My question is: is there a way to prevent persisted
from saving such an "empty" session?
Are you lazy loading the plugin? I use this for my config:
use({
"olimorris/persisted.nvim",
module = "persisted", -- For lazy loading
config = function()
require("persisted").setup()
require("telescope").load_extension("persisted") -- To load the telescope extension
end,
})
and my "Find Session" command calls require("persisted").load()
.
I take your point though that there could be times when an empty session is saved to disk. Defo a risk if you don't lazy load.
Hmm, how would lazyloading work with autoloading sessions? My use case is that I want to load alpha dashboard if session autoload fails, my reasoning being that manually loading an existing session for the current workdir is an unnecessary extra step.
It wouldn't; misread that first part of the issue 😄. Really good use case btw.
I think there's two components we'd need:
- A global variable to inform other plugins (or your config) that no session has been autoloaded (a global variable feels like the cleanest way of doing this)
- The ability to ignore the
alpha
filetype, ensuring that a blank session is not saved to disk if you quit Neovim from the dashboard
Do you agree?
That should do it, or at least I can't think of a reason why that wouldn't work.
I've marked this as an enhancement and will try and pick this up in a couple of weeks
Another option is to use the return value of a callback (e.g. true
or false
) to determine whether a session should be auto-saved — that way, any condition can be implemented. We could either use the existing before_save
or introduce a new callback like should_save
or should_autosave
, or perhaps even allow the user to pass a callback to the existing autosave
as an alternative to a boolean. It shouldn't affect manual SessionSave
s, of course.
@loqusion - really good suggestion! Much cleaner and will cover anyone's edge case. I've added this in the latest commit
That's really nice, works like a charm and makes it easy to update if there's more conditions in the future! Takkâ ennuv!