diff --git a/README.md b/README.md index c6c5586a..e18087bb 100644 --- a/README.md +++ b/README.md @@ -91,6 +91,10 @@ require'nvim-tree'.setup { }, number = false, relativenumber = false + }, + trash = { + cmd = "trash", + require_confirm = true } } ``` @@ -192,6 +196,7 @@ highlight NvimTreeFolderIcon guibg=blue - type `gy` will copy absolute path to system clipboard - type `p` to paste from clipboard. Cut clipboard has precedence over copy (will prompt for confirmation) - type `d` to delete a file (will prompt for confirmation) +- type `D` to trash a file (configured in setup()) - type `]c` to go to next git item - type `[c` to go to prev git item - type `-` to navigate up to the parent directory of the current file/directory @@ -243,6 +248,7 @@ local list = { { key = "R", cb = tree_cb("refresh") }, { key = "a", cb = tree_cb("create") }, { key = "d", cb = tree_cb("remove") }, + { key = "D", cb = tree_cb("trash") }, { key = "r", cb = tree_cb("rename") }, { key = "", cb = tree_cb("full_rename") }, { key = "x", cb = tree_cb("cut") }, diff --git a/doc/nvim-tree-lua.txt b/doc/nvim-tree-lua.txt index 310c3500..ee26220a 100644 --- a/doc/nvim-tree-lua.txt +++ b/doc/nvim-tree-lua.txt @@ -120,6 +120,10 @@ function. filters = { dotfiles = false, custom = {} + }, + trash = { + cmd = "trash", + require_confirm = true, } } < @@ -321,6 +325,16 @@ Here is a list of the options available in the setup call: type: `{string}` default: `{}` +*nvim-tree.trash* +|trash|: configuration options for trashing + + - |trash.cmd|: the command used to trash items (must be installed on your system) + type: `string` + default: `"trash"` + + - |trash.require_confirm|: show a prompt before trashing takes place. + type: `boolean` + default: `true` ============================================================================== OPTIONS *nvim-tree-options* @@ -562,6 +576,7 @@ Defaults to: { key = "R", cb = tree_cb("refresh") }, { key = "a", cb = tree_cb("create") }, { key = "d", cb = tree_cb("remove") }, + { key = "D", cb = tree_cb("trash") }, { key = "r", cb = tree_cb("rename") }, { key = "", cb = tree_cb("full_rename") }, { key = "x", cb = tree_cb("cut") }, diff --git a/lua/nvim-tree.lua b/lua/nvim-tree.lua index bf829958..93477657 100644 --- a/lua/nvim-tree.lua +++ b/lua/nvim-tree.lua @@ -8,6 +8,7 @@ local renderer = require'nvim-tree.renderer' local fs = require'nvim-tree.fs' local view = require'nvim-tree.view' local utils = require'nvim-tree.utils' +local trash = require'nvim-tree.trash' local _config = { is_windows = vim.fn.has('win32') == 1 or vim.fn.has('win32unix') == 1, @@ -151,6 +152,7 @@ local keypress_funcs = { ) luv.unref(process.handle) end, + trash = function(node) trash.trash_node(node, _config) end, } function M.on_keypress(mode) @@ -453,6 +455,7 @@ function M.setup(conf) _config.system_open = opts.system_open _config.open_on_setup = opts.open_on_setup _config.ignore_ft_on_setup = opts.ignore_ft_on_setup + _config.trash = opts.trash or {} if type(opts.update_to_buf_dir) == "boolean" then utils.warn("update_to_buf_dir is now a table, see :help nvim-tree.update_to_buf_dir") _config.update_to_buf_dir = { diff --git a/lua/nvim-tree/trash.lua b/lua/nvim-tree/trash.lua new file mode 100644 index 00000000..78c5910c --- /dev/null +++ b/lua/nvim-tree/trash.lua @@ -0,0 +1,72 @@ +local M = {} + +local lib = require'nvim-tree.lib' +local utils = require'nvim-tree.utils' +local events = require'nvim-tree.events' +local api = vim.api + +local function clear_buffer(absolute_path) + local bufs = vim.fn.getbufinfo({bufloaded = 1, buflisted = 1}) + for _, buf in pairs(bufs) do + if buf.name == absolute_path then + if buf.hidden == 0 and #bufs > 1 then + local winnr = api.nvim_get_current_win() + api.nvim_set_current_win(buf.windows[1]) + vim.cmd(':bn') + api.nvim_set_current_win(winnr) + end + vim.api.nvim_buf_delete(buf.bufnr, {}) + return + end + end +end + +function M.trash_node(node, cfg) + if node.name == '..' then return end + + -- configs + if cfg.is_unix then + if cfg.trash.cmd == nil then cfg.trash.cmd = 'trash' end + if cfg.trash.require_confirm == nil then cfg.trash.require_confirm = true end + else + print('trash is currently a UNIX only feature!') + end + + -- trashes a path (file or folder) + local function trash_path(on_exit) + vim.fn.jobstart(cfg.trash.cmd.." "..node.absolute_path, { + detach = true, + on_exit = on_exit, + }) + end + + local is_confirmed = true + + -- confirmation prompt + if cfg.trash.require_confirm then + is_confirmed = false + print("Trash " ..node.name.. " ? y/n") + local ans = utils.get_user_input_char() + if ans:match('^y') then is_confirmed = true end + utils.clear_prompt() + end + + -- trashing + if is_confirmed then + if node.entries ~= nil and not node.link_to then + trash_path(function() + events._dispatch_folder_removed(node.absolute_path) + lib.refresh_tree() + end) + else + trash_path(function() + events._dispatch_file_removed(node.absolute_path) + clear_buffer(node.absolute_path) + lib.refresh_tree() + end) + end + + end +end + +return M diff --git a/lua/nvim-tree/view.lua b/lua/nvim-tree/view.lua index c14e9aea..a9374b35 100644 --- a/lua/nvim-tree/view.lua +++ b/lua/nvim-tree/view.lua @@ -61,6 +61,7 @@ M.View = { { key = "R", cb = M.nvim_tree_callback("refresh") }, { key = "a", cb = M.nvim_tree_callback("create") }, { key = "d", cb = M.nvim_tree_callback("remove") }, + { key = "D", cb = M.nvim_tree_callback("trash") }, { key = "r", cb = M.nvim_tree_callback("rename") }, { key = "", cb = M.nvim_tree_callback("full_rename") }, { key = "x", cb = M.nvim_tree_callback("cut") },