feature request: after_delete callback or remove deleted file from jumplist
Did you check existing requests?
- [x] I have searched the existing issues
Describe the feature
After deleting a file, using ctrl+o to jump to previous location causes errors if that file was navigated to recently. So, I need an after_delete callback so I could delete the file from jump_list, or the plugin could do it for me since there are similar features.
Provide background
No response
What is the significance of this feature?
strongly desired
Additional details
No response
There is an OilActionsPost User autocmd that might serve your purpose.
Right now when you delete files with Oil, we don't delete the buffers (if those files are open). I've been toying with the idea of providing an option to change that, but it's not something that matters much to me. If someone wanted to take a crack at a PR for that, I would review it.
I currently have this and It seems to be working. It will probably fail in some corner case.
Obviously it would be better if this was in the plugin. I might implement it but I don't feel like doing so at the moment.
local augroup = vim.api.nvim_create_augroup("myoil", { clear = true })
vim.api.nvim_create_autocmd("User", {
pattern = "OilActionsPost",
group = augroup,
callback = function(e)
if e.data.actions == nil then
return
end
for _, action in ipairs(e.data.actions) do
if action.entry_type == "file" and action.type == "delete" then
local file = action.url:sub(7)
local bufnr = vim.fn.bufnr(file)
if bufnr >= 0 then
vim.api.nvim_buf_delete(bufnr, { force = true })
end
end
end
end,
})
I'll just add my 2 cents here, fixed the implementation to also work on windows + added some error reporting, thank you @ertucode for the inspiration
local augroup = vim.api.nvim_create_augroup("UnloadBufferWhenFileDeleted", { clear = true })
vim.api.nvim_create_autocmd("User", {
pattern = "OilActionsPost",
group = augroup,
callback = function(args)
-- If err is non-null, we encountered an error while processing the actions
if args.data.err then
vim.notify(vim.inspect(args.data.err), vim.log.levels.ERROR)
return
end
if not args.data.actions then
vim.notify("no actions", vim.log.levels.ERROR)
return
end
vim.iter(args.data.actions)
:filter(function(action)
return action.type == "delete" and action.entry_type == "file"
end)
:each(function(action)
local file_url = action.url:gsub("oil://", "file://", 1)
-- to fix windows paths
if
file_url:match("/%u/") ~= nil -- finds /C/ or /D/
and vim.uv.os_uname().sysname:find("Windows", 1, true) == 1 -- matches Windows_NT
then
file_url = file_url:gsub("/(%u)/", "/%1:/", 1) -- replaces /C/ with /C:/
end
local bufnr = vim.uri_to_bufnr(file_url)
if bufnr < 0 then
vim.notify("bufnr not found for uri " .. file_url, vim.log.levels.ERROR)
return
end
local file_name = vim.uri_to_fname(file_url)
vim.notify(("deleting buffer %d for file '%s'"):format(bufnr, file_name), vim.log.levels.DEBUG)
vim.bo[bufnr].buflisted = false
vim.api.nvim_buf_delete(bufnr, { force = true, unload = true })
end)
end,
})