mirror of
https://github.com/neovim/neovim.git
synced 2025-02-25 18:55:25 -06:00
clipboard: keep track of ownership and cache clipboard data locally
Helped-By: Justin M. Keyes <justinkz@gmail.com>
This commit is contained in:
parent
a0e89978bd
commit
d133502e98
@ -1,9 +1,23 @@
|
|||||||
" The clipboard provider uses shell commands to communicate with the clipboard.
|
" The clipboard provider uses shell commands to communicate with the clipboard.
|
||||||
" The provider function will only be registered if one of the supported
|
" The provider function will only be registered if a supported command is
|
||||||
" commands are available.
|
" available.
|
||||||
let s:copy = {}
|
let s:copy = {}
|
||||||
let s:paste = {}
|
let s:paste = {}
|
||||||
|
|
||||||
|
" 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)}
|
||||||
|
|
||||||
function! s:try_cmd(cmd, ...)
|
function! s:try_cmd(cmd, ...)
|
||||||
let argv = split(a:cmd, " ")
|
let argv = split(a:cmd, " ")
|
||||||
let out = a:0 ? systemlist(argv, a:1, 1) : systemlist(argv, [''], 1)
|
let out = a:0 ? systemlist(argv, a:1, 1) : systemlist(argv, [''], 1)
|
||||||
@ -14,20 +28,23 @@ function! s:try_cmd(cmd, ...)
|
|||||||
return out
|
return out
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
|
let s:cache_enabled = 1
|
||||||
|
|
||||||
if executable('pbcopy')
|
if executable('pbcopy')
|
||||||
let s:copy['+'] = 'pbcopy'
|
let s:copy['+'] = 'pbcopy'
|
||||||
let s:paste['+'] = 'pbpaste'
|
let s:paste['+'] = 'pbpaste'
|
||||||
let s:copy['*'] = s:copy['+']
|
let s:copy['*'] = s:copy['+']
|
||||||
let s:paste['*'] = s:paste['+']
|
let s:paste['*'] = s:paste['+']
|
||||||
|
let s:cache_enabled = 0
|
||||||
elseif executable('xclip')
|
elseif executable('xclip')
|
||||||
let s:copy['+'] = 'xclip -i -selection clipboard'
|
let s:copy['+'] = 'xclip -quiet -i -selection clipboard'
|
||||||
let s:paste['+'] = 'xclip -o -selection clipboard'
|
let s:paste['+'] = 'xclip -o -selection clipboard'
|
||||||
let s:copy['*'] = 'xclip -i -selection primary'
|
let s:copy['*'] = 'xclip -quiet -i -selection primary'
|
||||||
let s:paste['*'] = 'xclip -o -selection primary'
|
let s:paste['*'] = 'xclip -o -selection primary'
|
||||||
elseif executable('xsel')
|
elseif executable('xsel')
|
||||||
let s:copy['+'] = 'xsel -i -b'
|
let s:copy['+'] = 'xsel --nodetach -i -b'
|
||||||
let s:paste['+'] = 'xsel -o -b'
|
let s:paste['+'] = 'xsel -o -b'
|
||||||
let s:copy['*'] = 'xsel -i -p'
|
let s:copy['*'] = 'xsel --nodetach -i -p'
|
||||||
let s:paste['*'] = 'xsel -o -p'
|
let s:paste['*'] = 'xsel -o -p'
|
||||||
else
|
else
|
||||||
echom 'clipboard: No shell command for communicating with the clipboard found.'
|
echom 'clipboard: No shell command for communicating with the clipboard found.'
|
||||||
@ -37,11 +54,36 @@ endif
|
|||||||
let s:clipboard = {}
|
let s:clipboard = {}
|
||||||
|
|
||||||
function! s:clipboard.get(reg)
|
function! s:clipboard.get(reg)
|
||||||
|
if s:selections[a:reg].owner > 0
|
||||||
|
return s:selections[a:reg].data
|
||||||
|
end
|
||||||
return s:try_cmd(s:paste[a:reg])
|
return s:try_cmd(s:paste[a:reg])
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
function! s:clipboard.set(lines, regtype, reg)
|
function! s:clipboard.set(lines, regtype, reg)
|
||||||
call s:try_cmd(s:copy[a:reg], a:lines)
|
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
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
function! provider#clipboard#Call(method, args)
|
function! provider#clipboard#Call(method, args)
|
||||||
|
Loading…
Reference in New Issue
Block a user