diff options
| author | Mohammad Reza Karimi <m.r.karimi.j@gmail.com> | 2026-01-20 15:55:27 -0500 |
|---|---|---|
| committer | Mohammad Reza Karimi <m.r.karimi.j@gmail.com> | 2026-01-20 15:55:27 -0500 |
| commit | aa5717f00fd14430c80939a68d7107c26de6e554 (patch) | |
| tree | 784e99d9b018e0554369c41cb33e2e195e28d3c6 /lua/mo/oil-util.lua | |
first commit
Diffstat (limited to 'lua/mo/oil-util.lua')
| -rw-r--r-- | lua/mo/oil-util.lua | 67 |
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 |
