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