Closed Kamilcuk closed 2 weeks ago
Does this PR follow the [Contribution Guidelines](development guidelines)? Following is a partial checklist:
Proper conventional commit scoping:
If you are adding a new plugin, the scope would be the name of the category it is being added into. ex. feat(utility): added noice.nvim plugin
If you are modifying a pre-existing plugin or pack, the scope would be the name of the plugin folder. ex. fix(noice-nvim): fix LSP handler error
[x] Pull request title has the appropriate conventional commit type and scope where the scope is the name of the pre-existing directory in the project as described above
[x] README
is properly formatted and uses fenced in links with <url>
unless they are inside a [title](url)
[x] Proper usage of opts
table rather than setting things up with the config
function.
Do you have the link to our documentation?
Link to docs: https://docs.astronvim.com/recipes/sessions/
@Kamilcuk here are 2 options:
true
, this might work? hopefully but it might be the same as once = true
:return {
"AstroNvim/astrocore",
---@type AstroCoreOpts
opts = {
autocmds = {
-- disable alpha autostart
alpha_autostart = false,
restore_session = {
{
event = { "VimEnter", "StdinReadPost" },
desc = "Restore previous directory session if neovim opened with no arguments",
nested = true, -- trigger other autocommands as buffers open
callback = function(args)
-- Only load the session if nvim was started with no args
if args.event == "VimEnter" and vim.fn.argc(-1) == 0 then
-- try to load a directory session using the current working directory
require("resession").load(vim.fn.getcwd(), { dir = "dirsession", silence_errors = true })
end
return true
end,
},
},
},
},
}
return {
"AstroNvim/astrocore",
---@type AstroCoreOpts
opts = {
autocmds = {
-- disable alpha autostart
alpha_autostart = false,
restore_session = {
{
event = { "VimEnter", "StdinReadPost" },
desc = "Restore previous directory session if neovim opened with no arguments",
nested = true, -- trigger other autocommands as buffers open
callback = function(args)
-- Only load the session if nvim was started with no args
if args.event == "VimEnter" and vim.fn.argc(-1) == 0 then
-- try to load a directory session using the current working directory
require("resession").load(vim.fn.getcwd(), { dir = "dirsession", silence_errors = true })
end
vim.api.nvim_del_augroup_by_name "restore_session"
end,
},
},
},
},
}
hi:
callback = function(args) print(args.event) .....
and then executing man man
. I am getting both events::messages
StdinReadPost
VimEnter
Both events are printed, so it does not work.
I would be happy with that, but now I have a better version:
{
"AstroNvim/astrocore",
---@type AstroCoreOpts
opts = {
autocmds = {
-- disable alpha autostart
alpha_autostart = false,
restore_session = {
{
event = { "VimEnter" },
desc = "Restore previous directory session if neovim opened with no arguments",
nested = true, -- trigger other autocommands as buffers open
callback = function(args)
local should_skip
local lines = vim.api.nvim_buf_get_lines(0, 0, 2, false)
if
vim.fn.argc() > 0 -- don't start when opening a file
or #lines > 1 -- don't open if current buffer has more than 1 line
or (#lines == 1 and lines[1]:len() > 0) -- don't open the current buffer if it has anything on the first line
or #vim.tbl_filter(function(bufnr) return vim.bo[bufnr].buflisted end, vim.api.nvim_list_bufs()) > 1 -- don't open if any listed buffers
or not vim.o.modifiable -- don't open if not modifiable
then
should_skip = true
else
for _, arg in pairs(vim.v.argv) do
if arg == "-b" or arg == "-c" or vim.startswith(arg, "+") or arg == "-S" then
should_skip = true
break
end
end
end
if should_skip then return end
if
vim.tbl_contains(require("resession").list { dir = "dirsession" }, (vim.fn.getcwd():gsub("/", "_")))
then
require("resession").load(vim.fn.getcwd(), { dir = "dirsession" })
else
require("lazy").load { plugins = { "alpha-nvim" } }
require("alpha").start(true)
vim.schedule(function() vim.cmd.doautocmd "FileType" end)
end
end,
},
},
},
},
},
I don't like the idea that alpha will not start never. So I copied code from https://github.com/AstroNvim/AstroNvim/blob/365aa6e083dcd25fa3d1c8a2515d7e71a03d51d3/lua/astronvim/plugins/alpha.lua#L44 . If there is a session, use it, otherwise show alpha. This actually works and shows alpha on directories without session, but loads session with directories with session. And also echo hi | nvim
and man man
both work, because I think they catch not vim.o.modifiable
.
Nice! I like this one! I think the only thing is adding protection against if alpha isn't installed
📑 Description
Adds auto-session-restore like in documentation, but also detect if nvim has been started with stdin redirected, like
stream | nvim
or likeexport MAPAGER='nvim +Man!'; man man
.The stdin detection is taken from https://github.com/thaerkh/vim-workspace/blob/master/plugin/workspace.vim#L268 .
ℹ Additional Information
Would be nice to add the stdin input handling also to documentation.