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

View File

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