When closing Neovim, I'd like to store the session, but only if there are any files open. If I only have the dashboard open, or an empty unsaved buffer, I want to skip this step. So, I need a way to check if this is the case.
So-far
I thought I can just loop through the buffers and then through the lines of each buffer, like so:
function are_all_buffers_unnamed_and_empty()
for _, bufnr in ipairs(vim.api.nvim_list_bufs()) do
local bufname = vim.api.nvim_buf_get_name(bufnr)
if bufname ~= '' then
return false -- If any buffer has a name, return false
end
local lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false)
if #lines > 1 or (#lines == 1 and #lines[1] > 0) then
return false -- If any buffer has content, return false
end
end
return true -- All buffers are unnamed and empty
end
Problem
This almost works, but:
If there is a notification showing (i.e., drawn on top of the buffer), this is also looped over, and the function returns false
. For example, in this case:
Is there a way to exclude these "trivial" buffers such as file explorers, dashboards, and notifications?