mirror of
https://github.com/hyzendust/KickestEnd.nvim.git
synced 2026-02-15 09:21:12 +01:00
Fix: added ability to remember entered password for the session.
This commit is contained in:
@@ -135,6 +135,7 @@ vim.keymap.set('n', '<leader>sh', function()
|
|||||||
end, { desc = 'New buffer in vertical split (right)' })
|
end, { desc = 'New buffer in vertical split (right)' })
|
||||||
|
|
||||||
-- [[ Save buffer ]]
|
-- [[ Save buffer ]]
|
||||||
|
local sudo_password_cache = nil -- store sudo password for session
|
||||||
local function smart_save(force_save_as)
|
local function smart_save(force_save_as)
|
||||||
local current_path = vim.api.nvim_buf_get_name(0)
|
local current_path = vim.api.nvim_buf_get_name(0)
|
||||||
-- Compute default input (prefill directory + filename)
|
-- Compute default input (prefill directory + filename)
|
||||||
@@ -149,8 +150,8 @@ local function smart_save(force_save_as)
|
|||||||
local default_name = current_path ~= '' and vim.fn.fnamemodify(current_path, ':t') or ''
|
local default_name = current_path ~= '' and vim.fn.fnamemodify(current_path, ':t') or ''
|
||||||
local default_input = default_dir .. default_name
|
local default_input = default_dir .. default_name
|
||||||
-- Decide whether to ask for filename
|
-- Decide whether to ask for filename
|
||||||
|
local filename = current_path
|
||||||
if current_path == '' or force_save_as then
|
if current_path == '' or force_save_as then
|
||||||
local filename
|
|
||||||
local overwrite = false
|
local overwrite = false
|
||||||
while true do
|
while true do
|
||||||
-- Ask user for filename
|
-- Ask user for filename
|
||||||
@@ -174,45 +175,46 @@ local function smart_save(force_save_as)
|
|||||||
break -- file doesn't exist, safe to write
|
break -- file doesn't exist, safe to write
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
-- If Save As filename is same as current file, just write and return
|
end
|
||||||
if filename == current_path then
|
-- Read buffer content
|
||||||
local write_cmd = overwrite and 'write!' or 'write'
|
local lines = vim.api.nvim_buf_get_lines(0, 0, -1, true)
|
||||||
vim.cmd(write_cmd)
|
local content = table.concat(lines, '\n')
|
||||||
print('Saved ' .. filename)
|
-- Determine if the file is writable
|
||||||
|
local writable = vim.fn.filereadable(filename) == 1 and vim.fn.filewritable(filename) == 1
|
||||||
|
if writable then
|
||||||
|
-- Normal writable file
|
||||||
|
if current_path == '' or force_save_as then
|
||||||
|
vim.cmd('write ' .. vim.fn.fnameescape(filename))
|
||||||
|
else
|
||||||
|
vim.cmd 'write'
|
||||||
|
end
|
||||||
|
else
|
||||||
|
-- File requires sudo
|
||||||
|
-- Prompt for password once per session
|
||||||
|
if not sudo_password_cache then
|
||||||
|
local pass = vim.fn.inputsecret 'sudo password: '
|
||||||
|
if pass == '' or pass == nil then
|
||||||
|
vim.notify('Sudo password required, save cancelled', vim.log.levels.WARN)
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
-- For no-name buffers or Save As, write and set buffer name
|
sudo_password_cache = pass
|
||||||
if current_path == '' then
|
end
|
||||||
-- No-name buffer: set name first
|
-- Write buffer content to sudo tee
|
||||||
vim.api.nvim_buf_set_name(0, filename)
|
local sudo_cmd = string.format('sudo -S tee %s > /dev/null', vim.fn.fnameescape(filename))
|
||||||
-- Force overwrite if file exists
|
local ok = vim.fn.system(sudo_cmd, sudo_password_cache .. '\n' .. content)
|
||||||
local write_cmd = 'write!'
|
if vim.v.shell_error ~= 0 then
|
||||||
vim.cmd(write_cmd)
|
vim.notify('Failed to write with sudo: ' .. ok, vim.log.levels.ERROR)
|
||||||
-- Clear modified flag so future saves work correctly
|
-- clear cached password to allow retry next time
|
||||||
vim.api.nvim_buf_set_option(0, 'modified', false)
|
sudo_password_cache = nil
|
||||||
else
|
return
|
||||||
-- Named buffer: normal Save As logic
|
end
|
||||||
local write_cmd = overwrite and 'write!' or 'write'
|
end
|
||||||
vim.cmd(write_cmd .. ' ' .. vim.fn.fnameescape(filename))
|
|
||||||
-- Preserve cursor position and undo history
|
-- Preserve cursor position and undo history
|
||||||
local old_buf = vim.api.nvim_get_current_buf()
|
|
||||||
local cursor_pos = vim.api.nvim_win_get_cursor(0)
|
local cursor_pos = vim.api.nvim_win_get_cursor(0)
|
||||||
local undo_history = vim.fn.getbufinfo(old_buf)[1].changedtick
|
vim.api.nvim_buf_set_option(0, 'modified', false)
|
||||||
-- Open the new file in a fresh buffer
|
|
||||||
vim.cmd('edit ' .. vim.fn.fnameescape(filename))
|
|
||||||
-- Restore cursor
|
|
||||||
vim.api.nvim_win_set_cursor(0, cursor_pos)
|
vim.api.nvim_win_set_cursor(0, cursor_pos)
|
||||||
-- Restore undo history
|
|
||||||
vim.cmd 'undojoin'
|
print('Saved ' .. filename)
|
||||||
-- Delete the old buffer without saving
|
|
||||||
vim.api.nvim_buf_delete(old_buf, { force = true })
|
|
||||||
end
|
|
||||||
print('Saved as ' .. filename)
|
|
||||||
else
|
|
||||||
-- Buffer already has a name, just save it
|
|
||||||
vim.cmd 'w'
|
|
||||||
print('Saved ' .. current_path)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
-- Save current buffer
|
-- Save current buffer
|
||||||
vim.keymap.set('n', '<leader>w', function()
|
vim.keymap.set('n', '<leader>w', function()
|
||||||
@@ -222,6 +224,12 @@ end, { desc = 'Save buffer' })
|
|||||||
vim.keymap.set('n', '<leader>W', function()
|
vim.keymap.set('n', '<leader>W', function()
|
||||||
smart_save(true) -- force Save As
|
smart_save(true) -- force Save As
|
||||||
end, { desc = 'Save As' })
|
end, { desc = 'Save As' })
|
||||||
|
-- Clear sudo password cache on exit
|
||||||
|
vim.api.nvim_create_autocmd('VimLeavePre', {
|
||||||
|
callback = function()
|
||||||
|
sudo_password_cache = nil
|
||||||
|
end,
|
||||||
|
})
|
||||||
|
|
||||||
-- [[ Close current window ]]
|
-- [[ Close current window ]]
|
||||||
-- Helper function to check how many windows show a buffer
|
-- Helper function to check how many windows show a buffer
|
||||||
|
|||||||
Reference in New Issue
Block a user