fix: trash in *Linux* using a trash command, fixes #37

remotes/origin/HEAD
Steven Xu 2023-12-31 04:02:15 +11:00 committed by pseudometa
parent 491b822d9e
commit 50bc0cce60
2 changed files with 45 additions and 6 deletions

View File

@ -57,7 +57,7 @@ keymap("n", "<leader>rf", genghis.renameFile)
keymap("n", "<leader>mf", genghis.moveAndRenameFile)
keymap("n", "<leader>nf", genghis.createNewFile)
keymap("n", "<leader>yf", genghis.duplicateFile)
keymap("n", "<leader>df", function() genghis.trashFile { trashLocation = "your/path" } end) -- default: "$HOME/.Trash".
keymap("n", "<leader>df", genghis.trashFile)
keymap("x", "<leader>x", genghis.moveSelectionToNewFile)
```
@ -73,6 +73,14 @@ Line Mode command; the selection is moved linewise.)
- `.moveAndRenameFile` or `:Move`: Move and Rename the current file. Keeps the
old name if the new path ends with `/`. Works like the UNIX `mv` command. Best
used with [autocompletion of directories](#autocompletion-of-directories).
- `.trashFile`: Trash the current file.
- Use `trashCmd` to specify an external trash command. It defaults to
`gio trash` on *Linux*, `trash` on *Mac* and *Windows*.
- Otherwise specify `trashLocation` to move the file to that directory. It
defaults to `$HOME/.Trash`. This does NOT trash the file the usual way. The
trashed file is NOT restorable to its original path.
- For backwards compatibility, the default behavior on *Mac* moves files to
`$HOME/.Trash`.
The following applies to all commands above:
- If no extension has been provided, uses the extension of the original file.

View File

@ -160,19 +160,50 @@ function M.chmodx()
cmd.edit()
end
---@param opts? {trashLocation: string}
---@param opts? {trashCmd: string, trashLocation: string}
function M.trashFile(opts)
cmd.update { bang = true }
local home = os.getenv("HOME")
local oldFilePath = vim.api.nvim_buf_get_name(0)
local oldName = vim.fs.basename(oldFilePath)
local trashCmd
if opts ~= nil and opts.trashCmd ~= nil then
trashCmd = opts.trashCmd
else
if fn.has("linux") == 1 then
trashCmd = "gio trash"
elseif fn.has("windows") == 1 then
trashCmd = "trash"
end
end
if trashCmd ~= nil then
-- Use a trash command
local trashArgs = vim.split(trashCmd, " ")
table.insert(trashArgs, oldFilePath)
local errMsg = ""
vim.fn.jobstart(trashArgs, {
detach = true,
on_stderr = function (_, data)
errMsg = errMsg .. (data and table.concat(data, " "))
end,
on_exit = function(_, rc)
if rc == 0 then
u.bwipeout()
u.notify(("%q deleted"):format(oldName))
else
u.notify(("Trashing %q failed: " .. errMsg):format(oldName), "warn")
end
end,
})
return
end
-- Default trash locations
local trash
if fn.has("linux") == 1 then
local xdg_data = os.getenv("XDG_DATA_HOME")
trash = xdg_data and xdg_data .. "/Trash/" or home .. "/.local/share/Trash/"
elseif fn.has("macunix") == 1 then
if fn.has("macunix") == 1 then
-- INFO macOS moves files to the icloud trash, if they are deleted from
-- icloud folder, otherwise they go the user trash folder
local iCloudPath = home .. "/Library/Mobile Documents/com~apple~CloudDocs"