feat!: use prompts similar to :confirm in buf_kill (#4186)

This commit is contained in:
LostNeophyte 2023-05-23 22:16:43 +02:00 committed by GitHub
parent 3dce3d86d4
commit e548d6bc99
WARNING! Although there is a key with this ID in the database it does not verify this commit! This commit is SUSPICIOUS.
GPG key ID: 4AEE18F83AFDEB23

View file

@ -178,7 +178,7 @@ function M.buf_kill(kill_command, bufnr, force)
local bo = vim.bo
local api = vim.api
local fmt = string.format
local fnamemodify = vim.fn.fnamemodify
local fn = vim.fn
if bufnr == 0 or bufnr == nil then
bufnr = api.nvim_get_current_buf()
@ -187,19 +187,24 @@ function M.buf_kill(kill_command, bufnr, force)
local bufname = api.nvim_buf_get_name(bufnr)
if not force then
local warning
local choice
if bo[bufnr].modified then
warning = fmt([[No write since last change for (%s)]], fnamemodify(bufname, ":t"))
choice = fn.confirm(fmt([[Save changes to "%s"?]], bufname), "&Yes\n&No\n&Cancel")
if choice == 1 then
vim.api.nvim_buf_call(bufnr, function()
vim.cmd("w")
end)
elseif choice == 2 then
force = true
else return
end
elseif api.nvim_buf_get_option(bufnr, "buftype") == "terminal" then
warning = fmt([[Terminal %s will be killed]], bufname)
end
if warning then
vim.ui.input({
prompt = string.format([[%s. Close it anyway? [y]es or [n]o (default: no): ]], warning),
}, function(choice)
if choice ~= nil and choice:match "ye?s?" then M.buf_kill(kill_command, bufnr, true) end
end)
return
choice = fn.confirm(fmt([[Close "%s"?]], bufname), "&Yes\n&No\n&Cancel")
if choice == 1 then
force = true
else
return
end
end
end