How can i customize clipboard-provider using lua

clipboard provider description in the Neovim documentation:

g:clipboard can also use functions (see |lambda|) instead of strings.
For example this configuration uses the g:foo variable as a fake clipboard: >

    let g:clipboard = {
          \   'name': 'myClipboard',
          \   'copy': {
          \      '+': {lines, regtype -> extend(g:, {'foo': [lines, regtype]}) },
          \      '*': {lines, regtype -> extend(g:, {'foo': [lines, regtype]}) },
          \    },
          \   'paste': {
          \      '+': {-> get(g:, 'foo', [])},
          \      '*': {-> get(g:, 'foo', [])},
          \   },
          \ }

I don’t know how lambda expressions are converted when converting config from vim script to lua, Do you have any suggestions?

My vim script is configured as follows:

    let s:osc52_copy = { lines, regtype ->
      \ chansend(v:stderr, printf("\x1b]52;;%s\x1b\\", system("base64 | tr -d '\n'", join(lines, "\n"))))
      \ }
    let g:clipboard = {
      \ 'name': 'myClipboard',
      \     'copy': {
      \         '*': 'clipboard-provider copy',
      \         '+':  s:osc52_copy
      \     },
      \     'paste': {
      \         '*': 'clipboard-provider paste',
      \         '+': 'clipboard-provider paste'
      \     }
      \ }

I use osc52_copy to solve remote copy via ssh

1 Like

And currently, I can only bypass the problem in the following ways:

step 1, make a clipboard.lua:

local M = {}
local function should_add(event)
    local length = #event.regcontents - 1
    for _,line in ipairs(event.regcontents) do
        length = length + #line
        if length > 10000 then
            return false
        end
    end
    return true
end
M.handle_yank_post = function()
  local event = vim.v.event
  if should_add(event) then
    -- vim.fn.chansend(vim.v.stderr, vim.fn.printf("\x1b]52;;%s\x1b\\", vim.fn.system("base64 | tr -d '\n'", join(, "\n"))))
    local joined = vim.fn.join(event.regcontents, '\n')
    local based = vim.fn.system("base64 | tr -d '\n'", joined)
    vim.fn.chansend(vim.v.stderr, vim.fn.printf("\x1b]52;;%s\x1b\\", based))
  end
end
return M

step 2, add it in init.lua:

require "clipboard" 

step 3, add an autocommand for TextYankPost event:

vim.cmd [[
  augroup clip
    autocmd!
    autocmd TextYankPost * :lua require("clipboard").handle_yank_post()
  augroup end
]]
1 Like