diff --git a/README.md b/README.md index a144d97..d0187dd 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ badge -Lightweight file operations without a full-blown file management plugin. +Lightweight and quick file operations without being a full-blown file manager. @@ -71,16 +71,17 @@ and moves the current selection to that new file. (Note that this is a Visual Line Mode command; the selection is moved linewise.) - `.renameFile` or `:Rename`: Rename the current file. - `.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 +old name if the new path ends with `/`. Works like the UNIX `mv` command. Can be 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`. +- `.trashFile{trashCmd = "your_cli"}` or `:Trash`: Move the current file +to the trash location. + * Defaults to `gio trash` on *Linux*, `trash` on *Mac* and *Windows*. + * If [bufdelete.nvim](https://github.com/famiu/bufdelete.nvim) is available, + `require'bufdelete.nvim'.bufwipeout` would be used to keep window layout + intact instead of `vim.cmd.bwipeout`. + +> [!NOTE] +> The trash CLIs are not available by default, and must be installed. The following applies to all commands above: - If no extension has been provided, uses the extension of the original file. @@ -97,14 +98,6 @@ use this information to update various code parts, for example `use` or `import` statements. ### File Utility Commands -- `.trashFile{trashLocation = "/your/path/"}` or `:Trash`: Move the current file -to the trash location. [Defaults to the operating-system-specific trash -directory.](https://github.com/chrisgrieser/nvim-genghis/blob/main/lua/genghis.lua#L164) -⚠️ Any existing file in the trash location with the same name is overwritten, -making that file irretrievable. If -[bufdelete.nvim](https://github.com/famiu/bufdelete.nvim) is available, -`require'bufdelete.nvim'.bufwipeout` would be used to keep window layout intact -instead of `vim.cmd.bwipeout`. - `.copyFilename` or `:CopyFilename`: Copy the file name. When `clipboard="unnamed[plus]"` has been set, copies to the `+` register, otherwise to `"`. diff --git a/lua/genghis/init.lua b/lua/genghis/init.lua index b216ccf..3f443c7 100644 --- a/lua/genghis/init.lua +++ b/lua/genghis/init.lua @@ -160,73 +160,42 @@ function M.chmodx() cmd.edit() end ----@param opts? {trashCmd: string, trashLocation: string} +---@param opts? { trashCmd: string} function M.trashFile(opts) - cmd.update { bang = true } - local home = os.getenv("HOME") + if opts == nil then opts = {} end + local defaultTrashCmds = { + macos = "trash", -- not installed by default + windows = "trash", -- not installed by default + linux = "gio trash", + } + + cmd("silent! update") + + local system + if fn.has("macos") == 1 then system = "macos" end + if fn.has("linux") == 1 then system = "linux" end + if fn.has("windows") == 1 then system = "windows" end + local trashCmd = opts.trashCmd or defaultTrashCmds[system] + + -- Use a trash command + local trashArgs = vim.split(trashCmd, " ") local oldFilePath = vim.api.nvim_buf_get_name(0) - local oldName = vim.fs.basename(oldFilePath) + table.insert(trashArgs, 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("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" - local isInICloud = oldFilePath:sub(1, #iCloudPath) == iCloudPath - trash = isInICloud and iCloudPath .. "/.Trash/" or home .. "/.Trash/" - else - -- TODO better support for windows - trash = home .. "/.Trash/" - end - - -- overwrite trash location, if specified by user - if opts and opts.trashLocation then - trash = opts.trashLocation - if not (trash:find("/$")) then trash = trash .. "/" end - end - - fn.mkdir(trash, "p") - if u.fileExists(trash .. oldName) then oldName = oldName .. "~" end - - if mv.moveFile(oldFilePath, trash .. oldName) then - u.bwipeout() - u.notify(("%q deleted"):format(oldName)) - end + local errMsg = "" + vim.fn.jobstart(trashArgs, { + detach = true, + on_stderr = function(_, data) errMsg = errMsg .. (data and table.concat(data, " ")) end, + on_exit = function(_, rc) + local oldName = vim.fs.basename(oldFilePath) + if rc == 0 then + u.bwipeout() + u.notify(("%q deleted"):format(oldName)) + else + u.notify(("Trashing %q failed: " .. errMsg):format(oldName), "warn") + end + end, + }) end --------------------------------------------------------------------------------