summaryrefslogtreecommitdiff
path: root/lua/mo/oil-util.lua
diff options
context:
space:
mode:
Diffstat (limited to 'lua/mo/oil-util.lua')
-rw-r--r--lua/mo/oil-util.lua67
1 files changed, 67 insertions, 0 deletions
diff --git a/lua/mo/oil-util.lua b/lua/mo/oil-util.lua
new file mode 100644
index 0000000..1b00fa7
--- /dev/null
+++ b/lua/mo/oil-util.lua
@@ -0,0 +1,67 @@
+local M = {}
+
+local notif = require("mo.notif")
+
+---@param opts { full_path: boolean }
+M.copy_to_clipboard = function(opts)
+ opts = opts or { full_path = true }
+
+ local oil = require("oil")
+ local dir = oil.get_current_dir()
+ if not dir then
+ notif.err("System clipboard only works for local files")
+ return
+ end
+
+ local entries = {}
+ local mode = vim.api.nvim_get_mode().mode
+ if mode == "v" or mode == "V" then
+ local start = vim.fn.getpos("v")
+ local end_ = vim.fn.getpos(".")
+ local start_row = start[2]
+ local end_row = end_[2]
+
+ if start_row > end_row then
+ start_row, end_row = end_row, start_row
+ end
+
+ for i = start_row, end_row do
+ table.insert(entries, oil.get_entry_on_line(0, i))
+ end
+
+ vim.api.nvim_feedkeys(
+ vim.api.nvim_replace_termcodes("<Esc>", true, false, true),
+ "n",
+ true
+ )
+ else
+ table.insert(entries, oil.get_cursor_entry())
+ end
+
+ -- This removes holes in the list-like table
+ entries = vim.tbl_values(entries)
+
+ if #entries == 0 then
+ vim.notify(
+ "Could not find local file under cursor",
+ vim.log.levels.WARN
+ )
+ return
+ end
+ local paths = {}
+ for _, entry in ipairs(entries) do
+ if opts.full_path then
+ table.insert(paths, dir .. entry.name)
+ else
+ table.insert(paths, entry.name)
+ end
+ end
+ require("vim.ui.clipboard.osc52").copy("+")(paths)
+ if #paths == 1 then
+ notif.info(string.format("Copied '%s' to system clipboard", paths[1]))
+ else
+ notif.info(string.format("Copied %d files to system clipboard", #paths))
+ end
+end
+
+return M