aboutsummaryrefslogtreecommitdiff
path: root/runtime/autoload/provider/clipboard.vim
diff options
context:
space:
mode:
Diffstat (limited to 'runtime/autoload/provider/clipboard.vim')
-rw-r--r--runtime/autoload/provider/clipboard.vim102
1 files changed, 67 insertions, 35 deletions
diff --git a/runtime/autoload/provider/clipboard.vim b/runtime/autoload/provider/clipboard.vim
index c7cb14ded7..1bcc1dea74 100644
--- a/runtime/autoload/provider/clipboard.vim
+++ b/runtime/autoload/provider/clipboard.vim
@@ -22,54 +22,85 @@ function! s:try_cmd(cmd, ...)
let argv = split(a:cmd, " ")
let out = a:0 ? systemlist(argv, a:1, 1) : systemlist(argv, [''], 1)
if v:shell_error
- echohl WarningMsg
- echo "clipboard: error: ".(len(out) ? out[0] : '')
- echohl None
+ if !exists('s:did_error_try_cmd')
+ echohl WarningMsg
+ echomsg "clipboard: error: ".(len(out) ? out[0] : '')
+ echohl None
+ let s:did_error_try_cmd = 1
+ endif
return 0
endif
return out
endfunction
+" Returns TRUE if `cmd` exits with success, else FALSE.
+function! s:cmd_ok(cmd)
+ call system(a:cmd)
+ return v:shell_error == 0
+endfunction
+
let s:cache_enabled = 1
-if executable('pbcopy')
- let s:copy['+'] = 'pbcopy'
- let s:paste['+'] = 'pbpaste'
- let s:copy['*'] = s:copy['+']
- let s:paste['*'] = s:paste['+']
- let s:cache_enabled = 0
-elseif exists('$DISPLAY') && executable('xsel')
- 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'
-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'
-elseif executable('lemonade')
- let s:copy['+'] = 'lemonade copy'
- let s:paste['+'] = 'lemonade paste'
- let s:copy['*'] = 'lemonade copy'
- let s:paste['*'] = 'lemonade paste'
-elseif executable('doitclient')
- let s:copy['+'] = 'doitclient wclip'
- let s:paste['+'] = 'doitclient wclip -r'
- let s:copy['*'] = s:copy['+']
- let s:paste['*'] = s:paste['+']
-else
- echom 'clipboard: No clipboard tool available. See :help nvim-clipboard'
+let s:err = ''
+
+function! provider#clipboard#Error() abort
+ return s:err
+endfunction
+
+function! provider#clipboard#Executable() abort
+ if has('mac') && executable('pbcopy')
+ 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'
+ 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'
+ 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'
+ 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'
+ elseif executable('win32yank')
+ let s:copy['+'] = 'win32yank -i --crlf'
+ let s:paste['+'] = 'win32yank -o --lf'
+ let s:copy['*'] = s:copy['+']
+ let s:paste['*'] = s:paste['+']
+ return 'win32yank'
+ endif
+
+ let s:err = 'clipboard: No clipboard tool available. See :help clipboard'
+ return ''
+endfunction
+
+if empty(provider#clipboard#Executable())
finish
endif
let s:clipboard = {}
function! s:clipboard.get(reg)
- let reg = a:reg == '"' ? '+' : a:reg
- if s:selections[reg].owner > 0
- return s:selections[reg].data
+ if s:selections[a:reg].owner > 0
+ return s:selections[a:reg].data
end
- return s:try_cmd(s:paste[reg])
+ return s:try_cmd(s:paste[a:reg])
endfunction
function! s:clipboard.set(lines, regtype, reg)
@@ -94,6 +125,7 @@ function! s:clipboard.set(lines, regtype, reg)
let selection.data = [a:lines, a:regtype]
let argv = split(s:copy[a:reg], " ")
let selection.detach = s:cache_enabled
+ let selection.cwd = "/"
let jobid = jobstart(argv, selection)
if jobid <= 0
echohl WarningMsg