robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
63528f4686
master
1function ! remote#define#CommandOnHost ( host , method , sync , name , opts ) 2let prefix = '' 3 4if has_key ( a: opts , 'range' ) 5if a: opts . range == '' || a: opts . range == '%' 6" -range or -range=%, pass the line range in a list 7let prefix = '<line1>,<line2>' 8elseif matchstr ( a: opts . range , '\d' ) != '' 9" -range=N, pass the count 10let prefix = '<count>' 11endif 12elseif has_key ( a: opts , 'count' ) 13let prefix = '<count>' 14endif 15 16let forward_args = [ prefix . a: name ] 17 18if has_key ( a: opts , 'bang' ) 19call add ( forward_args , '<bang>' ) 20endif 21 22if has_key ( a: opts , 'register' ) 23call add ( forward_args , ' <register>' ) 24endif 25 26if has_key ( a: opts , 'nargs' ) 27call add ( forward_args , ' " . <q-args> . "' ) 28endif 29 30exe s: GetCommandPrefix ( a: name , a: opts ) 31 \. ' call remote#define#CommandBootstrap("' . a: host . '"' 32 \. ', "' . a: method . '"' 33 \. ', ' . string ( a: sync ) 34 \. ', "' . a: name . '"' 35 \. ', ' . string ( a: opts ) . '' 36 \. ', "' . join ( forward_args , '' ) . '"' 37 \. ')' 38endfunction 39 40 41function ! remote#define#CommandBootstrap ( host , method , sync , name , opts , forward ) 42let channel = remote#host#Require ( a: host ) 43 44if channel 45call remote#define#CommandOnChannel ( channel , a: method , a: sync , a: name , a: opts ) 46exe a: forward 47else 48exe 'delcommand ' . a: name 49echoerr 'Host "' a: host . '" is not available, deleting command "' . a: name . '"' 50endif 51endfunction 52 53 54function ! remote#define#CommandOnChannel ( channel , method , sync , name , opts ) 55let rpcargs = [ a: channel , '"' . a: method . '"' ] 56if has_key ( a: opts , 'nargs' ) 57" -nargs, pass arguments in a list 58call add ( rpcargs , '[<f-args>]' ) 59endif 60 61if has_key ( a: opts , 'range' ) 62if a: opts . range == '' || a: opts . range == '%' 63" -range or -range=%, pass the line range in a list 64call add ( rpcargs , '[<line1>, <line2>]' ) 65elseif matchstr ( a: opts . range , '\d' ) != '' 66" -range=N, pass the count 67call add ( rpcargs , '<count>' ) 68endif 69elseif has_key ( a: opts , 'count' ) 70" count 71call add ( rpcargs , '<count>' ) 72endif 73 74if has_key ( a: opts , 'bang' ) 75" bang 76call add ( rpcargs , '<q-bang> == "!"' ) 77endif 78 79if has_key ( a: opts , 'register' ) 80" register 81call add ( rpcargs , '<q-reg>' ) 82endif 83 84call s: AddEval ( rpcargs , a: opts ) 85exe s: GetCommandPrefix ( a: name , a: opts ) 86 \. ' call ' . s: GetRpcFunction ( a: sync ) . '(' . join ( rpcargs , ', ' ) . ')' 87endfunction 88 89 90function ! remote#define#AutocmdOnHost ( host , method , sync , name , opts ) 91let group = s: GetNextAutocmdGroup () 92let forward = '"doau ' . group . ' ' . a: name . ' ".' 93 \. 'fnameescape(expand("<amatch>"))' 94let a: opts . group = group 95let bootstrap_def = s: GetAutocmdPrefix ( a: name , a: opts ) 96 \. ' call remote#define#AutocmdBootstrap("' . a: host . '"' 97 \. ', "' . a: method . '"' 98 \. ', ' . string ( a: sync ) 99 \. ', "' . a: name . '"' 100 \. ', ' . string ( a: opts ) . '' 101 \. ', "' . escape ( forward , '"' ) . '"' 102 \. ')' 103exe bootstrap_def 104endfunction 105 106 107function ! remote#define#AutocmdBootstrap ( host , method , sync , name , opts , forward ) 108let channel = remote#host#Require ( a: host ) 109 110exe 'autocmd! ' . a: opts . group 111if channel 112call remote#define#AutocmdOnChannel ( channel , a: method , a: sync , a: name , 113 \a: opts ) 114exe eval ( a: forward ) 115else 116exe 'augroup! ' . a: opts . group 117echoerr 'Host "' a: host . '" for "' . a: name . '" autocmd is not available' 118endif 119endfunction 120 121 122function ! remote#define#AutocmdOnChannel ( channel , method , sync , name , opts ) 123let rpcargs = [ a: channel , '"' . a: method . '"' ] 124call s: AddEval ( rpcargs , a: opts ) 125 126let autocmd_def = s: GetAutocmdPrefix ( a: name , a: opts ) 127 \. ' call ' . s: GetRpcFunction ( a: sync ) . '(' . join ( rpcargs , ', ' ) . ')' 128exe autocmd_def 129endfunction 130 131 132function ! remote#define#FunctionOnHost ( host , method , sync , name , opts ) 133let group = s: GetNextAutocmdGroup () 134exe 'autocmd! ' . group . ' FuncUndefined ' . a: name 135 \. ' call remote#define#FunctionBootstrap("' . a: host . '"' 136 \. ', "' . a: method . '"' 137 \. ', ' . string ( a: sync ) 138 \. ', "' . a: name . '"' 139 \. ', ' . string ( a: opts ) 140 \. ', "' . group . '"' 141 \. ')' 142endfunction 143 144 145function ! remote#define#FunctionBootstrap ( host , method , sync , name , opts , group ) 146let channel = remote#host#Require ( a: host ) 147 148exe 'autocmd! ' . a: group 149exe 'augroup! ' . a: group 150if channel 151call remote#define#FunctionOnChannel ( channel , a: method , a: sync , a: name , 152 \a: opts ) 153else 154echoerr 'Host "' a: host . '" for "' . a: name . '" function is not available' 155endif 156endfunction 157 158 159function ! remote#define#FunctionOnChannel ( channel , method , sync , name , opts ) 160let rpcargs = [ a: channel , '"' . a: method . '"' , 'a:000' ] 161if has_key ( a: opts , 'range' ) 162call add ( rpcargs , '[a:firstline, a:lastline]' ) 163endif 164call s: AddEval ( rpcargs , a: opts ) 165 166let function_def = s: GetFunctionPrefix ( a: name , a: opts ) 167 \. 'return ' . s: GetRpcFunction ( a: sync ) . '(' . join ( rpcargs , ', ' ) . ')' 168 \. "\nendfunction" 169exe function_def 170endfunction 171 172let s: busy = {} 173let s: pending_notifications = {} 174 175function ! s: GetRpcFunction ( sync ) 176if a: sync ==# 'urgent' 177return 'rpcnotify' 178elseif a: sync 179return 'remote#define#request' 180endif 181return 'remote#define#notify' 182endfunction 183 184function ! remote#define#notify ( chan , ...) 185if get ( s: busy , a: chan , 0 ) > 0 186let pending = get ( s: pending_notifications , a: chan , []) 187call add ( pending , deepcopy ( a: 000 )) 188let s: pending_notifications [ a: chan ] = pending 189else 190call call ( 'rpcnotify' , [ a: chan ] + a: 000 ) 191endif 192endfunction 193 194function ! remote#define#request ( chan , ...) 195let s: busy [ a: chan ] = get ( s: busy , a: chan , 0 ) + 1 196let val = call ( 'rpcrequest' , [ a: chan ] + a: 000 ) 197let s: busy [ a: chan ] -= 1 198if s: busy [ a: chan ] == 0 199for msg in get ( s: pending_notifications , a: chan , []) 200call call ( 'rpcnotify' , [ a: chan ] + msg ) 201endfor 202let s: pending_notifications [ a: chan ] = [] 203endif 204return val 205endfunction 206 207function ! s: GetCommandPrefix ( name , opts ) 208return 'command!' . s: StringifyOpts ( a: opts , [ 'nargs' , 'complete' , 'range' , 209 \'count' , 'bang' , 'bar' , 'register' ]) . ' ' . a: name 210endfunction 211 212 213" Each msgpack-rpc autocommand has it's own unique group, which is derived 214" from an autoincrementing gid(group id). This is required for replacing the 215" autocmd implementation with the lazy-load mechanism 216let s: next_gid = 1 217function ! s: GetNextAutocmdGroup () 218let gid = s: next_gid 219let s: next_gid += 1 220 221let group_name = 'RPC_DEFINE_AUTOCMD_GROUP_' . gid 222" Ensure the group is defined 223exe 'augroup ' . group_name . ' | augroup END' 224return group_name 225endfunction 226 227 228function ! s: GetAutocmdPrefix ( name , opts ) 229if has_key ( a: opts , 'group' ) 230let group = a: opts . group 231else 232let group = s: GetNextAutocmdGroup () 233endif 234let rv = [ 'autocmd!' , group , a: name ] 235 236if has_key ( a: opts , 'pattern' ) 237call add ( rv , a: opts . pattern ) 238else 239call add ( rv , '*' ) 240endif 241 242if has_key ( a: opts , 'nested' ) && a: opts . nested 243call add ( rv , '++nested' ) 244endif 245 246if has_key ( a: opts , 'once' ) && a: opts . once 247call add ( rv , '++once' ) 248endif 249 250return join ( rv , ' ' ) 251endfunction 252 253 254function ! s: GetFunctionPrefix ( name , opts ) 255let res = "function! " . a: name . "(...)" 256if has_key ( a: opts , 'range' ) 257let res = res . " range" 258endif 259return res . "\n" 260endfunction 261 262 263function ! s: StringifyOpts ( opts , keys ) 264let rv = [] 265for key in a: keys 266if has_key ( a: opts , key ) 267call add ( rv , ' -' . key ) 268let val = a: opts [ key ] 269if type ( val ) != type ( '' ) || val != '' 270call add ( rv , '=' . val ) 271endif 272endif 273endfor 274return join ( rv , '' ) 275endfunction 276 277 278function ! s: AddEval ( rpcargs , opts ) 279if has_key ( a: opts , 'eval' ) 280if type ( a: opts . eval ) != type ( '' ) || a: opts . eval == '' 281throw "Eval option must be a non-empty string" 282endif 283" evaluate an expression and pass as argument 284call add ( a: rpcargs , 'eval("' . escape ( a: opts . eval , '"' ) . '")' ) 285endif 286endfunction