Skip to content

Commit

Permalink
feat: paste to system clipboard for macOS (stevearc#556)
Browse files Browse the repository at this point in the history
  • Loading branch information
stevalkr committed Jan 14, 2025
1 parent ac7c3e4 commit 4e94abf
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 9 deletions.
41 changes: 38 additions & 3 deletions lua/oil/actions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ M.refresh = {

local function open_cmdline_with_path(path)
local escaped =
vim.api.nvim_replace_termcodes(": " .. vim.fn.fnameescape(path) .. "<Home>", true, false, true)
vim.api.nvim_replace_termcodes(": " .. vim.fn.fnameescape(path) .. "<Home>", true, false, true)
vim.api.nvim_feedkeys(escaped, "n", false)
end

Expand Down Expand Up @@ -432,7 +432,6 @@ M.copy_to_system_clipboard = {
if fs.is_mac then
local cmd = "osascript -e 'on run args' -e 'set the clipboard to POSIX file (first item of args)' -e end '%s'"
local jid = vim.fn.jobstart(string.format(cmd, path), {
stdout_buffered = true,
on_exit = function(j, exit_code)
if exit_code ~= 0 then
vim.notify(string.format("Error copying '%s' to system clipboard", path), vim.log.levels.ERROR)
Expand All @@ -446,6 +445,42 @@ M.copy_to_system_clipboard = {
end,
}

M.paste_from_system_clipboard = {
desc = "Paste the system clipboard into the current oil directory",
callback = function()
local fs = require("oil.fs")
local dir = oil.get_current_dir()
if not dir then
return
end

if fs.is_mac then
local path = nil
local cmd = "osascript -e 'on run' -e 'POSIX path of (the clipboard as «class furl»)' -e end"
local jid = vim.fn.jobstart(cmd, {
stdout_buffered = true,
on_stdout = function(j, output)
if #output > 1 then
path = vim.uv.fs_realpath(output[1])
end
end,
on_exit = function(j, exit_code)
if exit_code ~= 0 or path == nil then
vim.notify(string.format("Error pasting file path from system clipboard"), vim.log.levels.ERROR)
return
end
local url = "oil://" .. path -- TODO: try to determine the adapter
local pos = vim.api.nvim_win_get_cursor(0)
vim.api.nvim_buf_set_lines(0, pos[1], pos[1], false, { url })
end,
})
assert(jid > 0, "Failed to start job")
else
vim.notify("System clipboard not supported on this platform", vim.log.levels.WARN)
end
end,
}

M.open_cmdline_dir = {
desc = "Open vim cmdline with current directory as an argument",
deprecated = true,
Expand Down Expand Up @@ -483,7 +518,7 @@ M.change_sort = {
order = order == "ascending" and "asc" or "desc"
oil.set_sort({
{ "type", "asc" },
{ col, order },
{ col, order },
})
end
)
Expand Down
38 changes: 32 additions & 6 deletions lua/oil/mutator/parser.lua
Original file line number Diff line number Diff line change
Expand Up @@ -296,12 +296,38 @@ M.parse = function(bufnr)
name, link = unpack(link_pieces)
end
check_dupe(name, i)
table.insert(diffs, {
type = "new",
name = name,
entry_type = entry_type,
link = link,
})

local url_pieces = vim.split(name, "://", { plain = true })
if #url_pieces == 2 then
local scheme = url_pieces[1] .. "://"
local path = url_pieces[2]
local stat, stat_err = vim.uv.fs_stat(path)
if stat_err then
table.insert(errors, {
message = stat_err,
lnum = i - 1,
end_lnum = i,
col = 0,
})
return
end
local parent_url = scheme .. vim.fn.fnamemodify(path, ":h")
local basename = vim.fn.fnamemodify(path, ":t")
local entry = cache.create_and_store_entry(parent_url, basename, stat.type)
table.insert(diffs, {
type = "new",
name = entry[FIELD_NAME],
entry_type = entry[FIELD_TYPE],
id = entry[FIELD_ID],
})
else
table.insert(diffs, {
type = "new",
name = name,
entry_type = entry_type,
link = link,
})
end
end
end
end)()
Expand Down

0 comments on commit 4e94abf

Please sign in to comment.