robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
310d01d8fa
master
1" The clipboard provider uses shell commands to communicate with the clipboard. 2" The provider function will only be registered if a supported command is 3" available. 4 5if exists ( 'g:loaded_clipboard_provider' ) 6finish 7endif 8" Default to 0. provider#clipboard#Executable() may set 2. 9" To force a reload: 10" :unlet g:loaded_clipboard_provider 11" :runtime autoload/provider/clipboard.vim 12let g: loaded_clipboard_provider = 0 13 14let s: copy = {} 15let s: paste = {} 16let s: clipboard = {} 17 18" When caching is enabled, store the jobid of the xclip/xsel process keeping 19" ownership of the selection, so we know how long the cache is valid. 20let s: selection = { 'owner' : 0 , 'data' : [], 'stderr_buffered' : v: true } 21 22function ! s: selection . on_exit ( jobid , data , event ) abort 23" At this point this nvim instance might already have launched 24" a new provider instance. Don't drop ownership in this case. 25if self . owner == a: jobid 26let self . owner = 0 27endif 28" Don't print if exit code is >= 128 ( exit is 128+SIGNUM if by signal (e.g. 143 on SIGTERM)) 29if a: data > 0 && a: data < 128 30echohl WarningMsg 31echomsg 'clipboard: error invoking ' . get ( self . argv , 0 , '?' ) . ': ' . join ( self . stderr ) 32echohl None 33endif 34endfunction 35 36let s: selections = { '*' : s: selection , '+' : copy ( s: selection ) } 37 38function ! s: try_cmd ( cmd , ...) abort 39let out = systemlist ( a: cmd , ( a: 0 ? a: 1 : [ '' ]), 1 ) 40if v: shell_error 41if ! exists ( 's:did_error_try_cmd' ) 42echohl WarningMsg 43echomsg "clipboard: error: " . ( len ( out ) ? out [ 0 ] : v: shell_error ) 44echohl None 45let s: did_error_try_cmd = 1 46endif 47return 0 48endif 49return out 50endfunction 51 52" Returns TRUE if `cmd` exits with success, else FALSE. 53function ! s: cmd_ok ( cmd ) abort 54call system ( a: cmd ) 55return v: shell_error == 0 56endfunction 57 58function ! s: split_cmd ( cmd ) abort 59return ( type ( a: cmd ) == v: t_string ) ? split ( a: cmd , " " ) : a: cmd 60endfunction 61 62function ! s: set_osc52 () abort 63let s: copy [ '+' ] = v: lua . require 'vim.ui.clipboard.osc52'.copy('+') 64let s: copy [ '*' ] = v: lua . require 'vim.ui.clipboard.osc52'.copy('*') 65let s: paste [ '+' ] = v: lua . require 'vim.ui.clipboard.osc52'.paste('+') 66let s: paste [ '*' ] = v: lua . require 'vim.ui.clipboard.osc52'.paste('*') 67return 'OSC 52' 68endfunction 69 70function ! s: set_pbcopy () abort 71let s: copy [ '+' ] = [ 'pbcopy' ] 72let s: paste [ '+' ] = [ 'pbpaste' ] 73let s: copy [ '*' ] = s: copy [ '+' ] 74let s: paste [ '*' ] = s: paste [ '+' ] 75let s: cache_enabled = 0 76return 'pbcopy' 77endfunction 78 79function ! s: set_wayland () abort 80let s: copy [ '+' ] = [ 'wl-copy' , '--type' , 'text/plain' ] 81let s: paste [ '+' ] = [ 'wl-paste' , '--no-newline' ] 82let s: copy [ '*' ] = [ 'wl-copy' , '--primary' , '--type' , 'text/plain' ] 83let s: paste [ '*' ] = [ 'wl-paste' , '--no-newline' , '--primary' ] 84return 'wl-copy' 85endfunction 86 87function ! s: set_wayclip () abort 88let s: copy [ '+' ] = [ 'waycopy' ] 89let s: paste [ '+' ] = [ 'waypaste' ] 90let s: copy [ '*' ] = [ 'waycopy' , '-p' ] 91let s: paste [ '*' ] = [ 'waypaste' , '-p' ] 92return 'wayclip' 93endfunction 94 95function ! s: set_xsel () abort 96let s: copy [ '+' ] = [ 'xsel' , '--nodetach' , '-i' , '-b' ] 97let s: paste [ '+' ] = [ 'xsel' , '-o' , '-b' ] 98let s: copy [ '*' ] = [ 'xsel' , '--nodetach' , '-i' , '-p' ] 99let s: paste [ '*' ] = [ 'xsel' , '-o' , '-p' ] 100return 'xsel' 101endfunction 102 103function ! s: set_xclip () abort 104let s: copy [ '+' ] = [ 'xclip' , '-quiet' , '-i' , '-selection' , 'clipboard' ] 105let s: paste [ '+' ] = [ 'xclip' , '-o' , '-selection' , 'clipboard' ] 106let s: copy [ '*' ] = [ 'xclip' , '-quiet' , '-i' , '-selection' , 'primary' ] 107let s: paste [ '*' ] = [ 'xclip' , '-o' , '-selection' , 'primary' ] 108return 'xclip' 109endfunction 110 111function ! s: set_lemonade () abort 112let s: copy [ '+' ] = [ 'lemonade' , 'copy' ] 113let s: paste [ '+' ] = [ 'lemonade' , 'paste' ] 114let s: copy [ '*' ] = [ 'lemonade' , 'copy' ] 115let s: paste [ '*' ] = [ 'lemonade' , 'paste' ] 116return 'lemonade' 117endfunction 118 119function ! s: set_doitclient () abort 120let s: copy [ '+' ] = [ 'doitclient' , 'wclip' ] 121let s: paste [ '+' ] = [ 'doitclient' , 'wclip' , '-r' ] 122let s: copy [ '*' ] = s: copy [ '+' ] 123let s: paste [ '*' ] = s: paste [ '+' ] 124return 'doitclient' 125endfunction 126 127function ! s: set_win32yank () abort 128if has ( 'wsl' ) && getftype ( exepath ( 'win32yank.exe' )) == 'link' 129let win32yank = resolve ( exepath ( 'win32yank.exe' )) 130else 131let win32yank = 'win32yank.exe' 132endif 133let s: copy [ '+' ] = [ win32yank , '-i' , '--crlf' ] 134let s: paste [ '+' ] = [ win32yank , '-o' , '--lf' ] 135let s: copy [ '*' ] = s: copy [ '+' ] 136let s: paste [ '*' ] = s: paste [ '+' ] 137return 'win32yank' 138endfunction 139 140function ! s: set_putclip () abort 141let s: copy [ '+' ] = [ 'putclip' ] 142let s: paste [ '+' ] = [ 'getclip' ] 143let s: copy [ '*' ] = s: copy [ '+' ] 144let s: paste [ '*' ] = s: paste [ '+' ] 145return 'putclip' 146endfunction 147 148function ! s: set_clip () abort 149let s: copy [ '+' ] = [ 'clip' ] 150let s: paste [ '+' ] = [ 'powershell' , '-NoProfile' , '-NoLogo' , '-Command' , 'Get-Clipboard' ] 151let s: copy [ '*' ] = s: copy [ '+' ] 152let s: paste [ '*' ] = s: paste [ '+' ] 153return 'clip' 154endfunction 155 156function ! s: set_termux () abort 157let s: copy [ '+' ] = [ 'termux-clipboard-set' ] 158let s: paste [ '+' ] = [ 'termux-clipboard-get' ] 159let s: copy [ '*' ] = s: copy [ '+' ] 160let s: paste [ '*' ] = s: paste [ '+' ] 161return 'termux-clipboard' 162endfunction 163 164function ! s: set_tmux () abort 165let tmux_v = v: lua . vim . version . parse ( system ([ 'tmux' , '-V' ])) 166if ! empty ( tmux_v ) && ! v: lua . vim . version . lt ( tmux_v , [ 3 , 2 , 0 ]) 167let s: copy [ '+' ] = [ 'tmux' , 'load-buffer' , '-w' , '-' ] 168else 169let s: copy [ '+' ] = [ 'tmux' , 'load-buffer' , '-' ] 170endif 171let s: paste [ '+' ] = [ 'sh' , '-c' , 'tmux refresh-client -l && sleep 0.05 && tmux save-buffer -' ] 172let s: copy [ '*' ] = s: copy [ '+' ] 173let s: paste [ '*' ] = s: paste [ '+' ] 174return 'tmux' 175endfunction 176 177let s: cache_enabled = 1 178let s: err = '' 179 180function ! provider#clipboard#Error () abort 181return s: err 182endfunction 183 184function ! provider#clipboard#Executable () abort 185" Setting g:clipboard to v:false explicitly opts-in to using the "builtin" clipboard providers below 186if exists ( 'g:clipboard' ) && g: clipboard isnot# v: false 187if v: t_string ==# type ( g: clipboard ) 188" Handle string form of g:clipboard for all builtin providers 189if 'osc52' == g: clipboard 190" User opted-in to OSC 52 by manually setting g:clipboard. 191return s: set_osc52 () 192elseif 'pbcopy' == g: clipboard 193return s: set_pbcopy () 194elseif 'wl-copy' == g: clipboard 195return s: set_wayland () 196elseif 'wayclip' == g: clipboard 197return s: set_wayclip () 198elseif 'xsel' == g: clipboard 199return s: set_xsel () 200elseif 'xclip' == g: clipboard 201return s: set_xclip () 202elseif 'lemonade' == g: clipboard 203return s: set_lemonade () 204elseif 'doitclient' == g: clipboard 205return s: set_doitclient () 206elseif 'win32yank' == g: clipboard 207return s: set_win32yank () 208elseif 'putclip' == g: clipboard 209return s: set_putclip () 210elseif 'clip' == g: clipboard 211return s: set_clip () 212elseif 'termux' == g: clipboard 213return s: set_termux () 214elseif 'tmux' == g: clipboard 215return s: set_tmux () 216endif 217endif 218 219if type ({}) isnot# type ( g: clipboard ) 220 \|| type ({}) isnot# type ( get ( g: clipboard , 'copy' , v: null )) 221 \|| type ({}) isnot# type ( get ( g: clipboard , 'paste' , v: null )) 222let s: err = 'clipboard: invalid g:clipboard' 223return '' 224endif 225 226let s: copy = {} 227let s: copy [ '+' ] = s: split_cmd ( get ( g: clipboard . copy , '+' , v: null )) 228let s: copy [ '*' ] = s: split_cmd ( get ( g: clipboard . copy , '*' , v: null )) 229 230let s: paste = {} 231let s: paste [ '+' ] = s: split_cmd ( get ( g: clipboard . paste , '+' , v: null )) 232let s: paste [ '*' ] = s: split_cmd ( get ( g: clipboard . paste , '*' , v: null )) 233 234let s: cache_enabled = get ( g: clipboard , 'cache_enabled' , 0 ) 235return get ( g: clipboard , 'name' , 'g:clipboard' ) 236elseif has ( 'mac' ) 237return s: set_pbcopy () 238elseif ! empty ( $ WAYLAND_DISPLAY ) && executable ( 'wl-copy' ) && executable ( 'wl-paste' ) 239return s: set_wayland () 240elseif ! empty ( $ WAYLAND_DISPLAY ) && executable ( 'waycopy' ) && executable ( 'waypaste' ) 241return s: set_wayclip () 242elseif ! empty ( $ DISPLAY ) && executable ( 'xsel' ) && s: cmd_ok ( 'xsel -o -b' ) 243return s: set_xsel () 244elseif ! empty ( $ DISPLAY ) && executable ( 'xclip' ) 245return s: set_xclip () 246elseif executable ( 'lemonade' ) 247return s: set_lemonade () 248elseif executable ( 'doitclient' ) 249return s: set_doitclient () 250elseif executable ( 'win32yank.exe' ) 251return s: set_win32yank () 252elseif executable ( 'putclip' ) && executable ( 'getclip' ) 253return s: set_putclip () 254elseif executable ( 'clip' ) && executable ( 'powershell' ) 255return s: set_clip () 256elseif executable ( 'termux-clipboard-set' ) 257return s: set_termux () 258elseif ! empty ( $ TMUX ) && executable ( 'tmux' ) 259return s: set_tmux () 260elseif get ( get ( g:, 'termfeatures' , {}), 'osc52' ) && & clipboard ==# '' 261" Don't use OSC 52 when 'clipboard' is set. It can be slow and cause a lot 262" of user prompts. Users can opt-in to it by setting g:clipboard manually. 263return s: set_osc52 () 264endif 265 266let s: err = 'clipboard: No clipboard tool. :help clipboard' 267return '' 268endfunction 269 270function ! s: clipboard . get ( reg ) abort 271if s: selections [ a: reg ]. owner > 0 272return s: selections [ a: reg ]. data 273end 274 275let clipboard_data = type ( s: paste [ a: reg ]) == v: t_func ? s: paste [ a: reg ]() : s: try_cmd ( s: paste [ a: reg ]) 276if match ( & clipboard , '\v(unnamed|unnamedplus)' ) >= 0 277 \&& type ( clipboard_data ) == v: t_list 278 \&& get ( s: selections [ a: reg ]. data , 0 , []) ==# clipboard_data 279" When system clipboard return is same as our cache return the cache 280" as it contains regtype information 281return s: selections [ a: reg ]. data 282end 283return clipboard_data 284endfunction 285 286function ! s: clipboard . set ( lines , regtype , reg ) abort 287if a: reg == '"' 288call s: clipboard . set ( a: lines , a: regtype , '+' ) 289if s: copy [ '*' ] != s: copy [ '+' ] 290call s: clipboard . set ( a: lines , a: regtype , '*' ) 291end 292return 0 293end 294 295if s: cache_enabled == 0 || type ( s: copy [ a: reg ]) == v: t_func 296if type ( s: copy [ a: reg ]) == v: t_func 297call s: copy [ a: reg ]( a: lines , a: regtype ) 298else 299call s: try_cmd ( s: copy [ a: reg ], a: lines ) 300endif 301"Cache it anyway we can compare it later to get regtype of the yank 302let s: selections [ a: reg ] = copy ( s: selection ) 303let s: selections [ a: reg ]. data = [ a: lines , a: regtype ] 304return 0 305end 306 307if s: selections [ a: reg ]. owner > 0 308let prev_job = s: selections [ a: reg ]. owner 309end 310let s: selections [ a: reg ] = copy ( s: selection ) 311let selection = s: selections [ a: reg ] 312let selection . data = [ a: lines , a: regtype ] 313let selection . argv = s: copy [ a: reg ] 314let selection . detach = s: cache_enabled 315let selection . cwd = "/" 316let jobid = jobstart ( selection . argv , selection ) 317if jobid > 0 318call jobsend ( jobid , a: lines ) 319call jobclose ( jobid , 'stdin' ) 320" xclip does not close stdout when receiving input via stdin 321if selection . argv [ 0 ] ==# 'xclip' 322call jobclose ( jobid , 'stdout' ) 323endif 324let selection . owner = jobid 325let ret = 1 326else 327echohl WarningMsg 328echomsg 'clipboard: failed to execute: ' . ( s: copy [ a: reg ]) 329echohl None 330let ret = 1 331endif 332 333" The previous provider instance should exit when the new one takes 334" ownership, but kill it to be sure we don't fill up the job table. 335if exists ( 'prev_job' ) 336call timer_start ( 1000 , { ... -> 337 \ jobwait([prev_job],0)[0] == -1 338 \ && jobstop(prev_job)}) 339endif 340 341return ret 342endfunction 343 344function ! provider#clipboard#Call ( method , args ) abort 345if get ( s:, 'here' , v: false ) " Clipboard provider must not recurse. #7184 346return 0 347endif 348let s: here = v: true 349try 350return call ( s: clipboard [ a: method ], a: args , s: clipboard ) 351finally 352let s: here = v: false 353endtry 354endfunction 355 356" eval_has_provider() decides based on this variable. 357let g: loaded_clipboard_provider = empty ( provider#clipboard#Executable ()) ? 0 : 2