dotfiles/config/common/nvim/lua/utils/quit.lua

35 lines
849 B
Lua

local M = {}
count_bufs_by_type = function(loaded_only)
loaded_only = (loaded_only == nil and true or loaded_only)
count = {
normal = 0,
acwrite = 0,
help = 0,
nofile = 0,
nowrite = 0,
quickfix = 0,
terminal = 0,
prompt = 0
}
buftypes = vim.api.nvim_list_bufs()
for _, bufname in pairs(buftypes) do
if (not loaded_only) or vim.api.nvim_buf_is_loaded(bufname) then
buftype = vim.api.nvim_buf_get_option(bufname, 'buftype')
buftype = buftype ~= '' and buftype or 'normal'
count[buftype] = count[buftype] + 1
end
end
return count
end
function M.close_buffer()
local bufTable = count_bufs_by_type()
if (bufTable.normal <= 1) then
local result = vim.api.nvim_exec([[:wq]], true)
else
local result = vim.api.nvim_exec([[:update | bdelete]], true)
end
end
return M