mirror of
https://github.com/hyzendust/KickestEnd.nvim.git
synced 2026-02-15 05:01:14 +01:00
Add: preserve undo history using neotree toggle while closing a buffer
This commit is contained in:
157
lua/keymaps.lua
157
lua/keymaps.lua
@@ -209,11 +209,7 @@ local function smart_save(force_save_as)
|
|||||||
return
|
return
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
-- Preserve cursor position and undo history
|
|
||||||
local cursor_pos = vim.api.nvim_win_get_cursor(0)
|
|
||||||
vim.api.nvim_buf_set_option(0, 'modified', false)
|
vim.api.nvim_buf_set_option(0, 'modified', false)
|
||||||
vim.api.nvim_win_set_cursor(0, cursor_pos)
|
|
||||||
|
|
||||||
print('Saved ' .. filename)
|
print('Saved ' .. filename)
|
||||||
end
|
end
|
||||||
-- Save current buffer
|
-- Save current buffer
|
||||||
@@ -268,42 +264,47 @@ local function is_alpha_running()
|
|||||||
end
|
end
|
||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
|
-- Helper function: toggle Undotree to preserve undo history for root-protected files
|
||||||
|
local function toggle_undotree_twice(callback)
|
||||||
|
local filename = vim.api.nvim_buf_get_name(0)
|
||||||
|
if filename ~= '' and vim.fn.filereadable(filename) == 1 and vim.fn.filewritable(filename) == 0 then
|
||||||
|
if vim.fn.exists ':UndotreeToggle' == 2 then
|
||||||
|
vim.cmd 'UndotreeToggle'
|
||||||
|
vim.defer_fn(function()
|
||||||
|
vim.cmd 'UndotreeToggle'
|
||||||
|
if callback then
|
||||||
|
vim.defer_fn(callback, 50)
|
||||||
|
end
|
||||||
|
end, 50)
|
||||||
|
elseif callback then
|
||||||
|
callback()
|
||||||
|
end
|
||||||
|
else
|
||||||
|
if callback then
|
||||||
|
callback()
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
-- Main function
|
-- Main function
|
||||||
local function close_window(mode)
|
local function close_window(mode)
|
||||||
local bufnr = vim.api.nvim_get_current_buf()
|
local function do_close()
|
||||||
local modified = vim.bo.modified
|
local bufnr = vim.api.nvim_get_current_buf()
|
||||||
local buftype = vim.api.nvim_buf_get_option(bufnr, 'buftype') -- detect terminal buffer
|
local modified = vim.bo.modified
|
||||||
local win_count = buffer_window_count(bufnr)
|
local buftype = vim.api.nvim_buf_get_option(bufnr, 'buftype') -- detect terminal buffer
|
||||||
local total_listed = listed_buffer_count()
|
local win_count = buffer_window_count(bufnr)
|
||||||
-- Handle terminal buffers separately
|
local total_listed = listed_buffer_count()
|
||||||
if buftype == 'terminal' then
|
-- Handle terminal buffers separately
|
||||||
if win_count > 1 then
|
if buftype == 'terminal' then
|
||||||
vim.cmd 'close'
|
if win_count > 1 then
|
||||||
else
|
vim.cmd 'close'
|
||||||
vim.api.nvim_buf_delete(bufnr, { force = true })
|
|
||||||
end
|
|
||||||
return
|
|
||||||
end
|
|
||||||
-- Handle saving/discarding logic
|
|
||||||
if modified then
|
|
||||||
if mode == 'save' then
|
|
||||||
if vim.api.nvim_buf_get_name(bufnr) == '' then
|
|
||||||
local filename = vim.fn.input('Save as: ', '', 'file')
|
|
||||||
if filename ~= '' then
|
|
||||||
vim.cmd('saveas ' .. vim.fn.fnameescape(filename))
|
|
||||||
else
|
|
||||||
print 'Save cancelled'
|
|
||||||
return
|
|
||||||
end
|
|
||||||
else
|
else
|
||||||
vim.cmd 'w'
|
vim.api.nvim_buf_delete(bufnr, { force = true })
|
||||||
end
|
end
|
||||||
elseif mode == 'discard' then
|
return
|
||||||
-- just continue and close without saving
|
end
|
||||||
else
|
-- Handle saving/discarding logic
|
||||||
-- ask user
|
if modified then
|
||||||
local choice = vim.fn.input 'Buffer modified! Save (y), Discard (n), Cancel (any other key)? '
|
if mode == 'save' then
|
||||||
if choice:lower() == 'y' then
|
|
||||||
if vim.api.nvim_buf_get_name(bufnr) == '' then
|
if vim.api.nvim_buf_get_name(bufnr) == '' then
|
||||||
local filename = vim.fn.input('Save as: ', '', 'file')
|
local filename = vim.fn.input('Save as: ', '', 'file')
|
||||||
if filename ~= '' then
|
if filename ~= '' then
|
||||||
@@ -315,42 +316,62 @@ local function close_window(mode)
|
|||||||
else
|
else
|
||||||
vim.cmd 'w'
|
vim.cmd 'w'
|
||||||
end
|
end
|
||||||
elseif choice:lower() == 'n' then
|
elseif mode == 'discard' then
|
||||||
-- discard changes
|
-- just continue and close without saving
|
||||||
else
|
else
|
||||||
print 'Quit cancelled'
|
-- ask user
|
||||||
return
|
local choice = vim.fn.input 'Buffer modified! Save (y), Discard (n), Cancel (any other key)? '
|
||||||
|
if choice:lower() == 'y' then
|
||||||
|
if vim.api.nvim_buf_get_name(bufnr) == '' then
|
||||||
|
local filename = vim.fn.input('Save as: ', '', 'file')
|
||||||
|
if filename ~= '' then
|
||||||
|
vim.cmd('saveas ' .. vim.fn.fnameescape(filename))
|
||||||
|
else
|
||||||
|
print 'Save cancelled'
|
||||||
|
return
|
||||||
|
end
|
||||||
|
else
|
||||||
|
vim.cmd 'w'
|
||||||
|
end
|
||||||
|
elseif choice:lower() == 'n' then
|
||||||
|
-- discard changes
|
||||||
|
else
|
||||||
|
print 'Quit cancelled'
|
||||||
|
return
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
-- Special case: only 1 listed buffer but an Alpha dashboard exists somewhere
|
||||||
|
if total_listed == 1 and is_alpha_running() then
|
||||||
|
-- Just close the current window instead of quitting Neovim
|
||||||
|
vim.cmd 'close'
|
||||||
|
return
|
||||||
|
end
|
||||||
|
-- Special case: last listed buffer in last window
|
||||||
|
if total_listed == 1 and win_count == 1 then
|
||||||
|
if modified and mode ~= 'save' then
|
||||||
|
-- if buffer had unsaved changes and user chose to discard, force quit
|
||||||
|
vim.cmd 'qa!'
|
||||||
|
else
|
||||||
|
vim.cmd 'qa'
|
||||||
|
end
|
||||||
|
return
|
||||||
|
end
|
||||||
|
-- Close logic
|
||||||
|
if win_count > 1 then
|
||||||
|
-- Buffer is visible in other windows/tabs: just close current window
|
||||||
|
vim.cmd 'close'
|
||||||
|
else
|
||||||
|
-- Buffer is only open in this window: delete the entire buffer from memory
|
||||||
|
if modified and mode ~= 'save' then
|
||||||
|
vim.cmd 'bwipeout!' -- discard changes
|
||||||
|
else
|
||||||
|
vim.cmd 'bdelete'
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
-- Special case: only 1 listed buffer but an Alpha dashboard exists somewhere
|
-- Toggle Undotree before closing to ensure undo history is preserved
|
||||||
if total_listed == 1 and is_alpha_running() then
|
toggle_undotree_twice(do_close)
|
||||||
-- Just close the current window instead of quitting Neovim
|
|
||||||
vim.cmd 'close'
|
|
||||||
return
|
|
||||||
end
|
|
||||||
-- Special case: last listed buffer in last window
|
|
||||||
if total_listed == 1 and win_count == 1 then
|
|
||||||
if modified and mode ~= 'save' then
|
|
||||||
-- if buffer had unsaved changes and user chose to discard, force quit
|
|
||||||
vim.cmd 'qa!'
|
|
||||||
else
|
|
||||||
vim.cmd 'qa'
|
|
||||||
end
|
|
||||||
return
|
|
||||||
end
|
|
||||||
-- Close logic
|
|
||||||
if win_count > 1 then
|
|
||||||
-- Buffer is visible in other windows/tabs: just close current window
|
|
||||||
vim.cmd 'close'
|
|
||||||
else
|
|
||||||
-- Buffer is only open in this window: delete the entire buffer from memory
|
|
||||||
if modified and mode ~= 'save' then
|
|
||||||
vim.cmd 'bwipeout!' -- discard changes
|
|
||||||
else
|
|
||||||
vim.cmd 'bdelete'
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
-- Asks what to do if the window is unsaved, otherwise just close
|
-- Asks what to do if the window is unsaved, otherwise just close
|
||||||
vim.keymap.set('n', '<leader>q', function()
|
vim.keymap.set('n', '<leader>q', function()
|
||||||
|
|||||||
Reference in New Issue
Block a user