From 6317d0214817a8b5f56696b7e51735723372b79e Mon Sep 17 00:00:00 2001 From: "Kenneth Benzie (Benie)" Date: Sun, 17 Mar 2024 14:01:36 +0000 Subject: [PATCH] Extend :Remove to accept bang :Remove! This patch changes the behaviour `:Remove` to only remove the file, leaving the current buffer untouched. `:Remove!` now has the old behaviour of removing the file and the buffer. --- lua/commands.lua | 36 ++++++++++++++++++++++++++---------- 1 file changed, 26 insertions(+), 10 deletions(-) diff --git a/lua/commands.lua b/lua/commands.lua index 26101e6..19d76cd 100644 --- a/lua/commands.lua +++ b/lua/commands.lua @@ -15,17 +15,33 @@ end, { nargs = 1 }) -- vim.loop.fs_rename -- :Remove the file associated with the current buffer, then delete the buffer -vim.api.nvim_create_user_command('Remove', function() +vim.api.nvim_create_user_command('Remove', function(opts) local path = vim.fn.expand('%:p') - vim.loop.fs_unlink(path, function(err, success) - if success then - vim.schedule(function() - vim.api.nvim_buf_delete(vim.api.nvim_get_current_buf(), {}) - end) - else - error(err) + -- Using opts.bang in the callback can cause a SEGFAULT, instead use it + -- before invoking the async unlink to select which callback should be called + -- on completion. + local callback = nil + if opts.bang then + -- Invoked as :Remove! so also delete the buffer. + callback = function(err, success) + if success then + vim.schedule(function() + vim.api.nvim_buf_delete(vim.api.nvim_get_current_buf(), {}) + end) + else + error(err) + end end - end) -end, {}) + else + -- Invoked as :Remove so don't delete the buffer. + callback = function(err, success) + if not success then + error(err) + end + end + end + -- Actually remove the file using the selecte callback. + vim.loop.fs_unlink(path, callback) +end, { bang = true }) -- TODO: :Move