1
0
mirror of https://github.com/SpaceVim/SpaceVim.git synced 2025-02-03 02:10:05 +08:00

fix(clipboard): fix clipboard support for vim8

This commit is contained in:
wsdjeg 2022-06-12 09:59:33 +08:00
parent c6f02e3be0
commit 99717ac7aa

View File

@ -13,67 +13,83 @@ function! s:set_command() abort
" the logic is based on nvim's clipboard provider " the logic is based on nvim's clipboard provider
" in vim8, system() do not support list argv
if has('mac') if has('mac')
let yank = ['pbcopy'] let yank = 'pbcopy'
let paste = ['pbpaste'] let paste = 'pbpaste'
elseif !empty($WAYLAND_DISPLAY) && executable('wl-copy') && executable('wl-paste') elseif !empty($WAYLAND_DISPLAY) && executable('wl-copy') && executable('wl-paste')
let yank = ['wl-copy', '--foreground', '--type', 'text/plain'] let yank = 'wl-copy --foreground --type text/plain'
let paste = ['wl-paste', '--no-newline'] let paste = 'wl-paste --no-newline'
elseif !empty($DISPLAY) && executable('xclip') elseif !empty($DISPLAY) && executable('xclip')
let yank = ['xclip', '-quiet', '-i', '-selection', 'clipboard'] let yank = 'xclip -quiet -i -selection clipboard'
let paste = ['xclip', '-o', '-selection', 'clipboard'] let paste = 'xclip -o -selection clipboard'
elseif !empty($DISPLAY) && executable('xsel') elseif !empty($DISPLAY) && executable('xsel')
let yank = ['xsel', '--nodetach', '-i', '-b'] let yank = 'xsel --nodetach -i -b'
let paste = ['xsel', '-o', '-b'] let paste = 'xsel -o -b'
elseif executable('lemonade') elseif executable('lemonade')
let yank = ['lemonade', 'copy'] let yank = 'lemonade copy'
let paste = ['lemonade', 'paste'] let paste = 'lemonade paste'
elseif executable('doitclient') elseif executable('doitclient')
let yank = ['doitclient', 'wclip'] let yank = 'doitclient wclip'
let paste = ['doitclient', 'wclip', '-r'] let paste = 'doitclient wclip -r'
elseif executable('win32yank.exe') elseif executable('win32yank.exe')
if has('wsl') && getftype(exepath('win32yank.exe')) == 'link' if has('wsl') && getftype(exepath('win32yank.exe')) == 'link'
let win32yank = resolve(exepath('win32yank.exe')) let win32yank = resolve(exepath('win32yank.exe'))
else else
let win32yank = 'win32yank.exe' let win32yank = 'win32yank.exe'
endif endif
let yank = [win32yank, '-i', '--crlf'] let yank = shellescape(win32yank) . ' -i --crlf'
let paste = [win32yank, '-o', '--lf'] let paste = shellescape(win32yank) . ' -o --lf'
elseif executable('termux-clipboard-set') elseif executable('termux-clipboard-set')
let yank = ['termux-clipboard-set'] let yank = 'termux-clipboard-set'
let paste = ['termux-clipboard-get'] let paste = 'termux-clipboard-get'
elseif !empty($TMUX) && executable('tmux') elseif !empty($TMUX) && executable('tmux')
let yank = ['tmux', 'load-buffer', '-'] let yank = 'tmux load-buffer -'
let paste = ['tmux', 'save-buffer', '-'] let paste = 'tmux save-buffer -'
endif endif
return [yank, paste] return [yank, paste]
endfunction endfunction
" yank to system clipboard " yank to system clipboard
function! clipboard#yank() abort function! clipboard#yank() abort
call system(s:yank_cmd, s:get_selection_text()) if !empty(s:yank_cmd)
call system(s:yank_cmd, s:get_selection_text())
else
if has('clipboard')
let @+ = s:get_selection_text()
else
endif
endif
endfunction endfunction
" The mode can be `p` or `P` " The mode can be `p` or `P`
function! clipboard#paste(mode) abort function! clipboard#paste(mode) abort
let @" = system(s:paste_cmd) if !empty(s:paste_cmd)
let @" = system(s:paste_cmd)
else
if has('clipboard')
let @" = @+
else
endif
endif
return a:mode return a:mode
endfunction endfunction
function! s:get_selection_text() function! s:get_selection_text()
let [begin, end] = [getpos("'<"), getpos("'>")] let [begin, end] = [getpos("'<"), getpos("'>")]
let lastchar = matchstr(getline(end[1])[end[2]-1 :], '.') let lastchar = matchstr(getline(end[1])[end[2]-1 :], '.')
if begin[1] ==# end[1] if begin[1] ==# end[1]
let lines = [getline(begin[1])[begin[2]-1 : end[2]-2]] let lines = [getline(begin[1])[begin[2]-1 : end[2]-2]]
else else
let lines = [getline(begin[1])[begin[2]-1 :]] let lines = [getline(begin[1])[begin[2]-1 :]]
\ + (end[1] - begin[1] <# 2 ? [] : getline(begin[1]+1, end[1]-1)) \ + (end[1] - begin[1] <# 2 ? [] : getline(begin[1]+1, end[1]-1))
\ + [getline(end[1])[: end[2]-2]] \ + [getline(end[1])[: end[2]-2]]
endif endif
return join(lines, "\n") . lastchar . (visualmode() ==# 'V' ? "\n" : '') return join(lines, "\n") . lastchar . (visualmode() ==# 'V' ? "\n" : '')
endfunction endfunction
let [s:yank_cmd, s:paste_cmd] = s:set_command() let [s:yank_cmd, s:paste_cmd] = s:set_command()