2014-11-17 06:48:39 -07:00
|
|
|
" The clipboard provider uses shell commands to communicate with the clipboard.
|
2015-03-01 06:10:42 -07:00
|
|
|
" The provider function will only be registered if a supported command is
|
|
|
|
" available.
|
2019-08-03 18:54:06 -07:00
|
|
|
|
|
|
|
if exists('g:loaded_clipboard_provider')
|
|
|
|
finish
|
|
|
|
endif
|
2019-08-04 03:06:24 -07:00
|
|
|
" Default to 1. provider#clipboard#Executable() may set 2.
|
2019-08-03 18:54:06 -07:00
|
|
|
" To force a reload:
|
|
|
|
" :unlet g:loaded_clipboard_provider
|
|
|
|
" :runtime autoload/provider/clipboard.vim
|
2019-08-04 03:06:24 -07:00
|
|
|
let g:loaded_clipboard_provider = 1
|
2019-08-03 18:54:06 -07:00
|
|
|
|
2014-11-22 08:01:14 -07:00
|
|
|
let s:copy = {}
|
|
|
|
let s:paste = {}
|
2017-08-19 17:13:04 -07:00
|
|
|
let s:clipboard = {}
|
2014-11-17 06:48:39 -07:00
|
|
|
|
2015-03-01 06:10:42 -07:00
|
|
|
" When caching is enabled, store the jobid of the xclip/xsel process keeping
|
|
|
|
" ownership of the selection, so we know how long the cache is valid.
|
2017-11-25 02:37:41 -07:00
|
|
|
let s:selection = { 'owner': 0, 'data': [], 'stderr_buffered': v:true }
|
2015-03-01 06:10:42 -07:00
|
|
|
|
2017-06-28 00:34:47 -07:00
|
|
|
function! s:selection.on_exit(jobid, data, event) abort
|
2015-03-01 06:10:42 -07:00
|
|
|
" At this point this nvim instance might already have launched
|
|
|
|
" a new provider instance. Don't drop ownership in this case.
|
|
|
|
if self.owner == a:jobid
|
|
|
|
let self.owner = 0
|
|
|
|
endif
|
2017-07-24 07:32:07 -07:00
|
|
|
if a:data != 0
|
|
|
|
echohl WarningMsg
|
2017-11-25 02:37:41 -07:00
|
|
|
echomsg 'clipboard: error invoking '.get(self.argv, 0, '?').': '.join(self.stderr)
|
2017-07-24 07:32:07 -07:00
|
|
|
echohl None
|
|
|
|
endif
|
2017-07-16 11:46:38 -07:00
|
|
|
endfunction
|
|
|
|
|
2017-08-19 17:13:04 -07:00
|
|
|
let s:selections = { '*': s:selection, '+': copy(s:selection) }
|
2015-03-01 06:10:42 -07:00
|
|
|
|
2017-06-28 00:34:47 -07:00
|
|
|
function! s:try_cmd(cmd, ...) abort
|
2020-09-01 00:27:38 -07:00
|
|
|
let out = systemlist(a:cmd, (a:0 ? a:1 : ['']), 1)
|
2014-11-22 13:01:28 -07:00
|
|
|
if v:shell_error
|
2017-01-03 16:45:31 -07:00
|
|
|
if !exists('s:did_error_try_cmd')
|
|
|
|
echohl WarningMsg
|
2017-08-22 11:31:54 -07:00
|
|
|
echomsg "clipboard: error: ".(len(out) ? out[0] : v:shell_error)
|
2017-01-03 16:45:31 -07:00
|
|
|
echohl None
|
|
|
|
let s:did_error_try_cmd = 1
|
|
|
|
endif
|
2015-06-09 13:05:52 -07:00
|
|
|
return 0
|
2014-11-22 13:01:28 -07:00
|
|
|
endif
|
|
|
|
return out
|
|
|
|
endfunction
|
|
|
|
|
2017-01-04 07:10:31 -07:00
|
|
|
" Returns TRUE if `cmd` exits with success, else FALSE.
|
2017-06-28 00:34:47 -07:00
|
|
|
function! s:cmd_ok(cmd) abort
|
2017-01-04 07:10:31 -07:00
|
|
|
call system(a:cmd)
|
|
|
|
return v:shell_error == 0
|
|
|
|
endfunction
|
|
|
|
|
2020-09-01 00:27:38 -07:00
|
|
|
function! s:split_cmd(cmd) abort
|
|
|
|
return (type(a:cmd) == v:t_string) ? split(a:cmd, " ") : a:cmd
|
|
|
|
endfunction
|
|
|
|
|
2015-03-01 06:10:42 -07:00
|
|
|
let s:cache_enabled = 1
|
2016-10-29 05:35:15 -07:00
|
|
|
let s:err = ''
|
|
|
|
|
|
|
|
function! provider#clipboard#Error() abort
|
|
|
|
return s:err
|
|
|
|
endfunction
|
2016-10-28 17:48:49 -07:00
|
|
|
|
|
|
|
function! provider#clipboard#Executable() abort
|
2017-06-27 03:21:53 -07:00
|
|
|
if exists('g:clipboard')
|
2017-08-19 17:13:04 -07:00
|
|
|
if type({}) isnot# type(g:clipboard)
|
2018-12-04 13:38:20 -07:00
|
|
|
\ || type({}) isnot# type(get(g:clipboard, 'copy', v:null))
|
|
|
|
\ || type({}) isnot# type(get(g:clipboard, 'paste', v:null))
|
2017-08-19 17:13:04 -07:00
|
|
|
let s:err = 'clipboard: invalid g:clipboard'
|
|
|
|
return ''
|
|
|
|
endif
|
2018-12-01 08:30:50 -07:00
|
|
|
|
2020-09-01 00:27:38 -07:00
|
|
|
let s:copy = {}
|
|
|
|
let s:copy['+'] = s:split_cmd(get(g:clipboard.copy, '+', v:null))
|
|
|
|
let s:copy['*'] = s:split_cmd(get(g:clipboard.copy, '*', v:null))
|
|
|
|
|
|
|
|
let s:paste = {}
|
|
|
|
let s:paste['+'] = s:split_cmd(get(g:clipboard.paste, '+', v:null))
|
|
|
|
let s:paste['*'] = s:split_cmd(get(g:clipboard.paste, '*', v:null))
|
|
|
|
|
2017-08-20 13:17:03 -07:00
|
|
|
let s:cache_enabled = get(g:clipboard, 'cache_enabled', 0)
|
2017-06-28 00:34:47 -07:00
|
|
|
return get(g:clipboard, 'name', 'g:clipboard')
|
2019-01-10 01:11:36 -07:00
|
|
|
elseif has('mac')
|
2020-09-01 00:27:38 -07:00
|
|
|
let s:copy['+'] = ['pbcopy']
|
|
|
|
let s:paste['+'] = ['pbpaste']
|
2016-10-28 17:48:49 -07:00
|
|
|
let s:copy['*'] = s:copy['+']
|
|
|
|
let s:paste['*'] = s:paste['+']
|
|
|
|
let s:cache_enabled = 0
|
|
|
|
return 'pbcopy'
|
2020-10-29 18:35:20 -07:00
|
|
|
elseif !empty($WAYLAND_DISPLAY) && executable('wl-copy') && executable('wl-paste')
|
2020-09-01 00:27:38 -07:00
|
|
|
let s:copy['+'] = ['wl-copy', '--foreground', '--type', 'text/plain']
|
|
|
|
let s:paste['+'] = ['wl-paste', '--no-newline']
|
|
|
|
let s:copy['*'] = ['wl-copy', '--foreground', '--primary', '--type', 'text/plain']
|
|
|
|
let s:paste['*'] = ['wl-paste', '--no-newline', '--primary']
|
2018-11-13 11:01:37 -07:00
|
|
|
return 'wl-copy'
|
2022-11-18 08:39:56 -07:00
|
|
|
elseif !empty($WAYLAND_DISPLAY) && executable('waycopy') && executable('waypaste')
|
|
|
|
let s:copy['+'] = ['waycopy', '-t', 'text/plain']
|
|
|
|
let s:paste['+'] = ['waypaste', '-t', 'text/plain']
|
|
|
|
let s:copy['*'] = s:copy['+']
|
|
|
|
let s:paste['*'] = s:paste['+']
|
|
|
|
return 'wayclip'
|
2020-10-29 18:35:20 -07:00
|
|
|
elseif !empty($DISPLAY) && executable('xsel') && s:cmd_ok('xsel -o -b')
|
2020-09-01 00:27:38 -07:00
|
|
|
let s:copy['+'] = ['xsel', '--nodetach', '-i', '-b']
|
|
|
|
let s:paste['+'] = ['xsel', '-o', '-b']
|
|
|
|
let s:copy['*'] = ['xsel', '--nodetach', '-i', '-p']
|
|
|
|
let s:paste['*'] = ['xsel', '-o', '-p']
|
2018-12-01 10:50:26 -07:00
|
|
|
return 'xsel'
|
2022-11-06 20:46:58 -07:00
|
|
|
elseif !empty($DISPLAY) && executable('xclip')
|
|
|
|
let s:copy['+'] = ['xclip', '-quiet', '-i', '-selection', 'clipboard']
|
|
|
|
let s:paste['+'] = ['xclip', '-o', '-selection', 'clipboard']
|
|
|
|
let s:copy['*'] = ['xclip', '-quiet', '-i', '-selection', 'primary']
|
|
|
|
let s:paste['*'] = ['xclip', '-o', '-selection', 'primary']
|
|
|
|
return 'xclip'
|
2016-10-28 17:48:49 -07:00
|
|
|
elseif executable('lemonade')
|
2020-09-01 00:27:38 -07:00
|
|
|
let s:copy['+'] = ['lemonade', 'copy']
|
|
|
|
let s:paste['+'] = ['lemonade', 'paste']
|
|
|
|
let s:copy['*'] = ['lemonade', 'copy']
|
|
|
|
let s:paste['*'] = ['lemonade', 'paste']
|
2016-10-28 17:48:49 -07:00
|
|
|
return 'lemonade'
|
|
|
|
elseif executable('doitclient')
|
2020-09-01 00:27:38 -07:00
|
|
|
let s:copy['+'] = ['doitclient', 'wclip']
|
|
|
|
let s:paste['+'] = ['doitclient', 'wclip', '-r']
|
2016-10-28 17:48:49 -07:00
|
|
|
let s:copy['*'] = s:copy['+']
|
|
|
|
let s:paste['*'] = s:paste['+']
|
|
|
|
return 'doitclient'
|
2018-11-21 04:24:40 -07:00
|
|
|
elseif executable('win32yank.exe')
|
2020-04-15 05:54:23 -07:00
|
|
|
if has('wsl') && getftype(exepath('win32yank.exe')) == 'link'
|
|
|
|
let win32yank = resolve(exepath('win32yank.exe'))
|
|
|
|
else
|
|
|
|
let win32yank = 'win32yank.exe'
|
|
|
|
endif
|
2020-09-01 00:27:38 -07:00
|
|
|
let s:copy['+'] = [win32yank, '-i', '--crlf']
|
|
|
|
let s:paste['+'] = [win32yank, '-o', '--lf']
|
2015-10-08 12:12:53 -07:00
|
|
|
let s:copy['*'] = s:copy['+']
|
|
|
|
let s:paste['*'] = s:paste['+']
|
|
|
|
return 'win32yank'
|
2021-03-29 17:52:35 -07:00
|
|
|
elseif executable('termux-clipboard-set')
|
|
|
|
let s:copy['+'] = ['termux-clipboard-set']
|
|
|
|
let s:paste['+'] = ['termux-clipboard-get']
|
|
|
|
let s:copy['*'] = s:copy['+']
|
|
|
|
let s:paste['*'] = s:paste['+']
|
|
|
|
return 'termux-clipboard'
|
2020-10-29 18:35:20 -07:00
|
|
|
elseif !empty($TMUX) && executable('tmux')
|
2023-03-16 17:12:33 -07:00
|
|
|
let tmux_v = v:lua.vim.version.parse(system(['tmux', '-V']))
|
|
|
|
if !empty(tmux_v) && !v:lua.vim.version.lt(tmux_v, [3,2,0])
|
2022-11-04 19:43:11 -07:00
|
|
|
let s:copy['+'] = ['tmux', 'load-buffer', '-w', '-']
|
|
|
|
else
|
|
|
|
let s:copy['+'] = ['tmux', 'load-buffer', '-']
|
|
|
|
endif
|
2020-09-01 00:27:38 -07:00
|
|
|
let s:paste['+'] = ['tmux', 'save-buffer', '-']
|
2017-06-15 00:15:56 -07:00
|
|
|
let s:copy['*'] = s:copy['+']
|
|
|
|
let s:paste['*'] = s:paste['+']
|
|
|
|
return 'tmux'
|
2016-10-28 17:48:49 -07:00
|
|
|
endif
|
|
|
|
|
2017-08-19 17:13:04 -07:00
|
|
|
let s:err = 'clipboard: No clipboard tool. :help clipboard'
|
2016-10-28 17:48:49 -07:00
|
|
|
return ''
|
|
|
|
endfunction
|
|
|
|
|
2017-06-28 00:34:47 -07:00
|
|
|
function! s:clipboard.get(reg) abort
|
2018-12-01 08:30:50 -07:00
|
|
|
if type(s:paste[a:reg]) == v:t_func
|
|
|
|
return s:paste[a:reg]()
|
|
|
|
elseif s:selections[a:reg].owner > 0
|
2016-07-11 06:59:20 -07:00
|
|
|
return s:selections[a:reg].data
|
2015-03-01 06:10:42 -07:00
|
|
|
end
|
2021-06-18 02:03:12 -07:00
|
|
|
|
|
|
|
let clipboard_data = s:try_cmd(s:paste[a:reg])
|
2021-07-11 06:19:54 -07:00
|
|
|
if match(&clipboard, '\v(unnamed|unnamedplus)') >= 0
|
|
|
|
\ && type(clipboard_data) == v:t_list
|
|
|
|
\ && get(s:selections[a:reg].data, 0, []) ==# clipboard_data
|
2021-06-18 02:03:12 -07:00
|
|
|
" When system clipboard return is same as our cache return the cache
|
|
|
|
" as it contains regtype information
|
|
|
|
return s:selections[a:reg].data
|
|
|
|
end
|
|
|
|
return clipboard_data
|
2014-11-17 06:48:39 -07:00
|
|
|
endfunction
|
|
|
|
|
2017-06-28 00:34:47 -07:00
|
|
|
function! s:clipboard.set(lines, regtype, reg) abort
|
2015-08-05 06:57:34 -07:00
|
|
|
if a:reg == '"'
|
|
|
|
call s:clipboard.set(a:lines,a:regtype,'+')
|
|
|
|
if s:copy['*'] != s:copy['+']
|
|
|
|
call s:clipboard.set(a:lines,a:regtype,'*')
|
|
|
|
end
|
|
|
|
return 0
|
|
|
|
end
|
2018-12-01 08:30:50 -07:00
|
|
|
|
|
|
|
if type(s:copy[a:reg]) == v:t_func
|
|
|
|
call s:copy[a:reg](a:lines, a:regtype)
|
|
|
|
return 0
|
|
|
|
end
|
|
|
|
|
2015-03-01 06:10:42 -07:00
|
|
|
if s:cache_enabled == 0
|
|
|
|
call s:try_cmd(s:copy[a:reg], a:lines)
|
2021-06-18 02:03:12 -07:00
|
|
|
"Cache it anyway we can compare it later to get regtype of the yank
|
|
|
|
let s:selections[a:reg] = copy(s:selection)
|
|
|
|
let s:selections[a:reg].data = [a:lines, a:regtype]
|
2015-03-01 06:10:42 -07:00
|
|
|
return 0
|
|
|
|
end
|
|
|
|
|
2017-11-25 02:37:41 -07:00
|
|
|
if s:selections[a:reg].owner > 0
|
2019-08-14 00:58:52 -07:00
|
|
|
let prev_job = s:selections[a:reg].owner
|
2015-03-01 06:10:42 -07:00
|
|
|
end
|
2017-11-25 02:37:41 -07:00
|
|
|
let s:selections[a:reg] = copy(s:selection)
|
|
|
|
let selection = s:selections[a:reg]
|
2015-03-01 06:10:42 -07:00
|
|
|
let selection.data = [a:lines, a:regtype]
|
2020-09-01 00:27:38 -07:00
|
|
|
let selection.argv = s:copy[a:reg]
|
2016-01-04 09:15:45 -07:00
|
|
|
let selection.detach = s:cache_enabled
|
2016-05-24 22:20:32 -07:00
|
|
|
let selection.cwd = "/"
|
2020-09-01 00:27:38 -07:00
|
|
|
let jobid = jobstart(selection.argv, selection)
|
2017-07-15 11:51:51 -07:00
|
|
|
if jobid > 0
|
|
|
|
call jobsend(jobid, a:lines)
|
|
|
|
call jobclose(jobid, 'stdin')
|
2020-01-02 09:37:39 -07:00
|
|
|
" xclip does not close stdout when receiving input via stdin
|
2020-09-01 00:27:38 -07:00
|
|
|
if selection.argv[0] ==# 'xclip'
|
2020-01-02 01:41:36 -07:00
|
|
|
call jobclose(jobid, 'stdout')
|
|
|
|
endif
|
2017-07-15 11:51:51 -07:00
|
|
|
let selection.owner = jobid
|
2019-08-14 00:58:52 -07:00
|
|
|
let ret = 1
|
2017-07-16 11:46:38 -07:00
|
|
|
else
|
2015-03-01 06:10:42 -07:00
|
|
|
echohl WarningMsg
|
2017-07-16 11:46:38 -07:00
|
|
|
echomsg 'clipboard: failed to execute: '.(s:copy[a:reg])
|
2015-03-01 06:10:42 -07:00
|
|
|
echohl None
|
2019-08-14 00:58:52 -07:00
|
|
|
let ret = 1
|
|
|
|
endif
|
|
|
|
|
|
|
|
" The previous provider instance should exit when the new one takes
|
|
|
|
" ownership, but kill it to be sure we don't fill up the job table.
|
|
|
|
if exists('prev_job')
|
|
|
|
call timer_start(1000, {... ->
|
|
|
|
\ jobwait([prev_job], 0)[0] == -1
|
|
|
|
\ && jobstop(prev_job)})
|
2015-03-01 06:10:42 -07:00
|
|
|
endif
|
2019-08-14 00:58:52 -07:00
|
|
|
|
|
|
|
return ret
|
2014-11-17 06:48:39 -07:00
|
|
|
endfunction
|
|
|
|
|
2017-06-28 00:34:47 -07:00
|
|
|
function! provider#clipboard#Call(method, args) abort
|
2017-08-22 11:31:54 -07:00
|
|
|
if get(s:, 'here', v:false) " Clipboard provider must not recurse. #7184
|
|
|
|
return 0
|
|
|
|
endif
|
|
|
|
let s:here = v:true
|
|
|
|
try
|
|
|
|
return call(s:clipboard[a:method],a:args,s:clipboard)
|
|
|
|
finally
|
|
|
|
let s:here = v:false
|
|
|
|
endtry
|
2014-11-17 06:48:39 -07:00
|
|
|
endfunction
|
2019-06-09 10:22:10 -07:00
|
|
|
|
2019-08-03 18:54:06 -07:00
|
|
|
" eval_has_provider() decides based on this variable.
|
2019-08-04 03:06:24 -07:00
|
|
|
let g:loaded_clipboard_provider = empty(provider#clipboard#Executable()) ? 1 : 2
|