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.
|
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
|
2015-05-17 13:06:32 -07:00
|
|
|
let argv = split(a:cmd, " ")
|
2018-01-30 14:35:33 -07:00
|
|
|
let out = systemlist(argv, (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
|
|
|
|
|
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
|
|
|
|
2017-06-28 00:34:47 -07:00
|
|
|
let s:copy = get(g:clipboard, 'copy', { '+': v:null, '*': v:null })
|
|
|
|
let s:paste = get(g:clipboard, 'paste', { '+': v:null, '*': 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')
|
2018-02-07 16:27:54 -07:00
|
|
|
elseif has('mac') && executable('pbpaste') && s:cmd_ok('pbpaste')
|
2016-10-28 17:48:49 -07:00
|
|
|
let s:copy['+'] = 'pbcopy'
|
|
|
|
let s:paste['+'] = 'pbpaste'
|
|
|
|
let s:copy['*'] = s:copy['+']
|
|
|
|
let s:paste['*'] = s:paste['+']
|
|
|
|
let s:cache_enabled = 0
|
|
|
|
return 'pbcopy'
|
2018-11-13 11:01:37 -07:00
|
|
|
elseif exists('$WAYLAND_DISPLAY') && executable('wl-copy') && executable('wl-paste')
|
|
|
|
let s:copy['+'] = 'wl-copy --foreground'
|
|
|
|
let s:paste['+'] = 'wl-paste --no-newline'
|
|
|
|
let s:copy['*'] = 'wl-copy --foreground --primary'
|
|
|
|
let s:paste['*'] = 'wl-paste --no-newline --primary'
|
|
|
|
return 'wl-copy'
|
2016-10-28 17:48:49 -07:00
|
|
|
elseif exists('$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'
|
2018-12-01 10:50:26 -07:00
|
|
|
elseif exists('$DISPLAY') && executable('xsel') && s:cmd_ok('xsel -o -b')
|
|
|
|
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'
|
|
|
|
return 'xsel'
|
2016-10-28 17:48:49 -07:00
|
|
|
elseif executable('lemonade')
|
|
|
|
let s:copy['+'] = 'lemonade copy'
|
|
|
|
let s:paste['+'] = 'lemonade paste'
|
|
|
|
let s:copy['*'] = 'lemonade copy'
|
|
|
|
let s:paste['*'] = 'lemonade paste'
|
|
|
|
return 'lemonade'
|
|
|
|
elseif executable('doitclient')
|
|
|
|
let s:copy['+'] = 'doitclient wclip'
|
|
|
|
let s:paste['+'] = 'doitclient wclip -r'
|
|
|
|
let s:copy['*'] = s:copy['+']
|
|
|
|
let s:paste['*'] = s:paste['+']
|
|
|
|
return 'doitclient'
|
2018-11-21 04:24:40 -07:00
|
|
|
elseif executable('win32yank.exe')
|
|
|
|
let s:copy['+'] = 'win32yank.exe -i --crlf'
|
|
|
|
let s:paste['+'] = 'win32yank.exe -o --lf'
|
2015-10-08 12:12:53 -07:00
|
|
|
let s:copy['*'] = s:copy['+']
|
|
|
|
let s:paste['*'] = s:paste['+']
|
|
|
|
return 'win32yank'
|
2017-06-15 00:15:56 -07:00
|
|
|
elseif exists('$TMUX') && executable('tmux')
|
|
|
|
let s:copy['+'] = 'tmux load-buffer -'
|
|
|
|
let s:paste['+'] = 'tmux save-buffer -'
|
|
|
|
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
|
|
|
|
|
|
|
|
if empty(provider#clipboard#Executable())
|
2017-08-19 17:13:04 -07:00
|
|
|
" provider#clipboard#Call() *must not* be defined if the provider is broken.
|
|
|
|
" Otherwise eval_has_provider() thinks the clipboard provider is
|
|
|
|
" functioning, and eval_call_provider() will happily call it.
|
2014-11-17 06:48:39 -07:00
|
|
|
finish
|
|
|
|
endif
|
|
|
|
|
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
|
2016-07-11 06:59:20 -07:00
|
|
|
return s:try_cmd(s:paste[a:reg])
|
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)
|
|
|
|
return 0
|
|
|
|
end
|
|
|
|
|
2017-11-25 02:37:41 -07:00
|
|
|
if s:selections[a:reg].owner > 0
|
2015-03-01 06:10:42 -07:00
|
|
|
" 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.
|
2017-11-25 02:37:41 -07:00
|
|
|
call jobstop(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]
|
|
|
|
let argv = split(s:copy[a:reg], " ")
|
2017-07-16 11:46:38 -07:00
|
|
|
let selection.argv = argv
|
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 = "/"
|
2015-03-01 06:10:42 -07:00
|
|
|
let jobid = jobstart(argv, selection)
|
2017-07-15 11:51:51 -07:00
|
|
|
if jobid > 0
|
|
|
|
call jobsend(jobid, a:lines)
|
|
|
|
call jobclose(jobid, 'stdin')
|
|
|
|
let selection.owner = jobid
|
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
|
2017-08-19 17:13:04 -07:00
|
|
|
return 0
|
2015-03-01 06:10:42 -07:00
|
|
|
endif
|
2017-08-19 17:13:04 -07:00
|
|
|
return 1
|
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
|