feat: make trash command more robust

remotes/origin/HEAD
Luca Saccarola 2023-07-02 21:22:41 +02:00
parent 18885a695b
commit 274f1abdfd
2 changed files with 20 additions and 27 deletions

View File

@ -6,13 +6,8 @@ local fn = vim.fn
local cmd = vim.cmd local cmd = vim.cmd
local function bwipeout(bufnr) local function bwipeout(bufnr)
local bufnr_int = bufnr and vim.fn.bufnr(bufnr) or vim.fn.bufnr('%') bufnr = bufnr and fn.bufnr(bufnr) or 0
vim.api.nvim_buf_delete(bufnr, { force = true })
if pcall(require, 'bufdelete') then
require 'bufdelete'.bufwipeout(bufnr_int)
else
vim.cmd.bwipeout(bufnr_int)
end
end end
local function leaveVisualMode() local function leaveVisualMode()
@ -215,17 +210,16 @@ function M.trashFile(opts)
local currentFile = expand("%:p") local currentFile = expand("%:p")
local filename = expand("%:t") local filename = expand("%:t")
-- os.rename fails if trash is on different filesystem if fileExists(trash .. filename) then
local success, errormsg = pcall(cmd.write, trash .. filename) filename = filename .. "~"
if success then
success, errormsg = os.remove(currentFile)
end end
local success, err = pcall(vim.loop.fs_rename, currentFile, trash .. filename)
if success then if success then
bwipeout() bwipeout()
vim.notify('"' .. filename .. '" deleted.') vim.notify(("%q deleted"):format(filename))
else else
vim.notify("Could not delete file: " .. errormsg, logError) vim.notify("Could not delete file: " .. err, logError)
end end
end end

View File

@ -1,18 +1,17 @@
if vim.fn.exists("g:genghis_disable_commands") == 0 then if vim.g.genghis_disable_commands then
vim.g.genghis_disable_commands = false vim.g.genghis_disable_commands = false
end end
if not vim.g.genghis_disable_commands then if not vim.g.genghis_disable_commands then
local command = vim.api.nvim_create_user_command local command = vim.api.nvim_create_user_command
local genghis = require("genghis") local genghis = require("genghis")
command("NewFromSelection", function() genghis.moveSelectionToNewFile() end, { range = true })
command("NewFromSelection", function() genghis.moveSelectionToNewFile() end, { range = true }) command("Duplicate", function() genghis.duplicateFile() end, {})
command("Duplicate", function() genghis.duplicateFile() end, {}) command("Rename", function() genghis.renameFile() end, {})
command("Rename", function() genghis.renameFile() end, {}) command("Trash", function() genghis.trashFile() end, {})
command("Trash", function() genghis.trashFile() end, {}) command("Move", function() genghis.moveAndRenameFile() end, {})
command("Move", function() genghis.moveAndRenameFile() end, {}) command("CopyFilename", function() genghis.copyFilename() end, {})
command("CopyFilename", function() genghis.copyFilename() end, {}) command("CopyFilepath", function() genghis.copyFilepath() end, {})
command("CopyFilepath", function() genghis.copyFilepath() end, {}) command("Chmodx", function() genghis.chmodx() end, {})
command("Chmodx", function() genghis.chmodx() end, {}) command("New", function() genghis.createNewFile() end, {})
command("New", function() genghis.createNewFile() end, {})
end end