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 = {}
|
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.
|
|
|
|
let s:selection = { 'owner': 0, 'data': [] }
|
|
|
|
|
|
|
|
function! s:selection.on_exit(jobid, data, event)
|
|
|
|
" 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
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
let s:selections = { '*': s:selection, '+': copy(s:selection)}
|
|
|
|
|
2014-11-22 13:01:28 -07:00
|
|
|
function! s:try_cmd(cmd, ...)
|
2015-05-17 13:06:32 -07:00
|
|
|
let argv = split(a:cmd, " ")
|
|
|
|
let out = a:0 ? systemlist(argv, a:1, 1) : systemlist(argv, [''], 1)
|
2014-11-22 13:01:28 -07:00
|
|
|
if v:shell_error
|
2015-06-09 13:05:52 -07:00
|
|
|
echohl WarningMsg
|
2014-11-22 13:01:28 -07:00
|
|
|
echo "clipboard: error: ".(len(out) ? out[0] : '')
|
2015-06-09 13:05:52 -07:00
|
|
|
echohl None
|
|
|
|
return 0
|
2014-11-22 13:01:28 -07:00
|
|
|
endif
|
|
|
|
return out
|
|
|
|
endfunction
|
|
|
|
|
2015-03-01 06:10:42 -07:00
|
|
|
let s:cache_enabled = 1
|
2014-11-17 06:48:39 -07:00
|
|
|
if executable('pbcopy')
|
2014-11-22 08:01:14 -07:00
|
|
|
let s:copy['+'] = 'pbcopy'
|
|
|
|
let s:paste['+'] = 'pbpaste'
|
|
|
|
let s:copy['*'] = s:copy['+']
|
|
|
|
let s:paste['*'] = s:paste['+']
|
2015-03-01 06:10:42 -07:00
|
|
|
let s:cache_enabled = 0
|
2014-11-17 06:48:39 -07:00
|
|
|
elseif executable('xclip')
|
2015-03-01 06:10:42 -07:00
|
|
|
let s:copy['+'] = 'xclip -quiet -i -selection clipboard'
|
2014-11-22 08:01:14 -07:00
|
|
|
let s:paste['+'] = 'xclip -o -selection clipboard'
|
2015-03-01 06:10:42 -07:00
|
|
|
let s:copy['*'] = 'xclip -quiet -i -selection primary'
|
2014-11-22 08:01:14 -07:00
|
|
|
let s:paste['*'] = 'xclip -o -selection primary'
|
|
|
|
elseif executable('xsel')
|
2015-03-01 06:10:42 -07:00
|
|
|
let s:copy['+'] = 'xsel --nodetach -i -b'
|
2014-11-22 08:01:14 -07:00
|
|
|
let s:paste['+'] = 'xsel -o -b'
|
2015-03-01 06:10:42 -07:00
|
|
|
let s:copy['*'] = 'xsel --nodetach -i -p'
|
2014-11-22 08:01:14 -07:00
|
|
|
let s:paste['*'] = 'xsel -o -p'
|
2014-11-22 13:01:28 -07:00
|
|
|
else
|
2015-06-09 13:05:52 -07:00
|
|
|
echom 'clipboard: No clipboard tool available. See :help nvim-clipboard'
|
2014-11-17 06:48:39 -07:00
|
|
|
finish
|
|
|
|
endif
|
|
|
|
|
2014-11-22 13:01:28 -07:00
|
|
|
let s:clipboard = {}
|
2014-11-17 06:48:39 -07:00
|
|
|
|
2014-11-22 08:01:14 -07:00
|
|
|
function! s:clipboard.get(reg)
|
2015-03-01 06:10:42 -07:00
|
|
|
if s:selections[a:reg].owner > 0
|
|
|
|
return s:selections[a:reg].data
|
|
|
|
end
|
2014-11-22 08:01:14 -07:00
|
|
|
return s:try_cmd(s:paste[a:reg])
|
2014-11-17 06:48:39 -07:00
|
|
|
endfunction
|
|
|
|
|
2014-11-22 11:16:04 -07:00
|
|
|
function! s:clipboard.set(lines, regtype, reg)
|
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
|
|
|
|
|
|
|
|
let selection = s:selections[a:reg]
|
|
|
|
if selection.owner > 0
|
|
|
|
" 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.
|
|
|
|
call jobstop(selection.owner)
|
|
|
|
end
|
|
|
|
let selection.data = [a:lines, a:regtype]
|
|
|
|
let argv = split(s:copy[a:reg], " ")
|
|
|
|
let jobid = jobstart(argv, selection)
|
|
|
|
if jobid <= 0
|
|
|
|
echohl WarningMsg
|
|
|
|
echo "clipboard: error when invoking provider"
|
|
|
|
echohl None
|
|
|
|
return 0
|
|
|
|
endif
|
|
|
|
call jobsend(jobid, a:lines)
|
|
|
|
call jobclose(jobid, 'stdin')
|
|
|
|
let selection.owner = jobid
|
2014-11-17 06:48:39 -07:00
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! provider#clipboard#Call(method, args)
|
2014-11-22 08:01:14 -07:00
|
|
|
return call(s:clipboard[a:method],a:args,s:clipboard)
|
2014-11-17 06:48:39 -07:00
|
|
|
endfunction
|