2014-11-17 06:46:13 -07:00
|
|
|
let s:hosts = {}
|
2015-06-24 16:18:44 -07:00
|
|
|
let s:plugin_patterns = {}
|
2014-11-21 06:07:59 -07:00
|
|
|
let s:remote_plugins_manifest = fnamemodify($MYVIMRC, ':p:h')
|
|
|
|
\.'/.'.fnamemodify($MYVIMRC, ':t').'-rplugin~'
|
2014-11-17 06:46:13 -07:00
|
|
|
|
|
|
|
|
|
|
|
" Register a host by associating it with a factory(funcref)
|
2015-06-24 16:18:44 -07:00
|
|
|
function! remote#host#Register(name, pattern, factory)
|
2014-11-17 06:46:13 -07:00
|
|
|
let s:hosts[a:name] = {'factory': a:factory, 'channel': 0, 'initialized': 0}
|
2015-06-24 16:18:44 -07:00
|
|
|
let s:plugin_patterns[a:name] = a:pattern
|
2014-11-17 06:46:13 -07:00
|
|
|
if type(a:factory) == type(1) && a:factory
|
|
|
|
" Passed a channel directly
|
|
|
|
let s:hosts[a:name].channel = a:factory
|
|
|
|
endif
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
|
|
|
|
" Register a clone to an existing host. The new host will use the same factory
|
|
|
|
" as `source`, but it will run as a different process. This can be used by
|
|
|
|
" plugins that should run isolated from other plugins created for the same host
|
|
|
|
" type
|
2014-11-21 06:07:59 -07:00
|
|
|
function! remote#host#RegisterClone(name, orig_name)
|
2014-11-17 06:46:13 -07:00
|
|
|
if !has_key(s:hosts, a:orig_name)
|
|
|
|
throw 'No host named "'.a:orig_name.'" is registered'
|
|
|
|
endif
|
|
|
|
let Factory = s:hosts[a:orig_name].factory
|
2015-03-20 15:55:39 -07:00
|
|
|
let s:hosts[a:name] = {
|
|
|
|
\ 'factory': Factory,
|
|
|
|
\ 'channel': 0,
|
|
|
|
\ 'initialized': 0,
|
|
|
|
\ 'orig_name': a:orig_name
|
|
|
|
\ }
|
2014-11-17 06:46:13 -07:00
|
|
|
endfunction
|
|
|
|
|
|
|
|
|
|
|
|
" Get a host channel, bootstrapping it if necessary
|
2014-11-21 06:07:59 -07:00
|
|
|
function! remote#host#Require(name)
|
2014-11-17 06:46:13 -07:00
|
|
|
if !has_key(s:hosts, a:name)
|
|
|
|
throw 'No host named "'.a:name.'" is registered'
|
|
|
|
endif
|
|
|
|
let host = s:hosts[a:name]
|
|
|
|
if !host.channel && !host.initialized
|
2015-06-28 05:54:42 -07:00
|
|
|
let host_info = {
|
|
|
|
\ 'name': a:name,
|
|
|
|
\ 'orig_name': get(host, 'orig_name', a:name)
|
|
|
|
\ }
|
|
|
|
let host.channel = call(host.factory, [host_info])
|
2014-11-17 06:46:13 -07:00
|
|
|
let host.initialized = 1
|
|
|
|
endif
|
|
|
|
return host.channel
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
|
2014-11-21 06:07:59 -07:00
|
|
|
function! remote#host#IsRunning(name)
|
2014-11-17 06:46:13 -07:00
|
|
|
if !has_key(s:hosts, a:name)
|
|
|
|
throw 'No host named "'.a:name.'" is registered'
|
|
|
|
endif
|
|
|
|
return s:hosts[a:name].channel != 0
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
|
2015-03-20 15:55:39 -07:00
|
|
|
" Example of registering a Python plugin with two commands (one async), one
|
|
|
|
" autocmd (async) and one function (sync):
|
2014-11-17 06:46:13 -07:00
|
|
|
"
|
|
|
|
" let s:plugin_path = expand('<sfile>:p:h').'/nvim_plugin.py'
|
2014-11-21 06:07:59 -07:00
|
|
|
" call remote#host#RegisterPlugin('python', s:plugin_path, [
|
2014-11-17 06:46:13 -07:00
|
|
|
" \ {'type': 'command', 'name': 'PyCmd', 'sync': 1, 'opts': {}},
|
|
|
|
" \ {'type': 'command', 'name': 'PyAsyncCmd', 'sync': 0, 'opts': {'eval': 'cursor()'}},
|
|
|
|
" \ {'type': 'autocmd', 'name': 'BufEnter', 'sync': 0, 'opts': {'eval': 'expand("<afile>")'}},
|
|
|
|
" \ {'type': 'function', 'name': 'PyFunc', 'sync': 1, 'opts': {}}
|
|
|
|
" \ ])
|
|
|
|
"
|
|
|
|
" The third item in a declaration is a boolean: non zero means the command,
|
|
|
|
" autocommand or function will be executed synchronously with rpcrequest.
|
2014-11-21 06:07:59 -07:00
|
|
|
function! remote#host#RegisterPlugin(host, path, specs)
|
2015-06-24 16:18:44 -07:00
|
|
|
let plugins = remote#host#PluginsForHost(a:host)
|
2014-11-17 06:46:13 -07:00
|
|
|
|
|
|
|
for plugin in plugins
|
|
|
|
if plugin.path == a:path
|
|
|
|
throw 'Plugin "'.a:path.'" is already registered'
|
|
|
|
endif
|
|
|
|
endfor
|
|
|
|
|
2014-11-21 06:07:59 -07:00
|
|
|
if remote#host#IsRunning(a:host)
|
2014-11-17 06:46:13 -07:00
|
|
|
" For now we won't allow registration of plugins when the host is already
|
|
|
|
" running.
|
|
|
|
throw 'Host "'.a:host.'" is already running'
|
|
|
|
endif
|
|
|
|
|
|
|
|
for spec in a:specs
|
|
|
|
let type = spec.type
|
|
|
|
let name = spec.name
|
|
|
|
let sync = spec.sync
|
|
|
|
let opts = spec.opts
|
|
|
|
let rpc_method = a:path
|
|
|
|
if type == 'command'
|
|
|
|
let rpc_method .= ':command:'.name
|
2014-11-21 06:07:59 -07:00
|
|
|
call remote#define#CommandOnHost(a:host, rpc_method, sync, name, opts)
|
2014-11-17 06:46:13 -07:00
|
|
|
elseif type == 'autocmd'
|
|
|
|
" Since multiple handlers can be attached to the same autocmd event by a
|
|
|
|
" single plugin, we need a way to uniquely identify the rpc method to
|
|
|
|
" call. The solution is to append the autocmd pattern to the method
|
|
|
|
" name(This still has a limit: one handler per event/pattern combo, but
|
|
|
|
" there's no need to allow plugins define multiple handlers in that case)
|
|
|
|
let rpc_method .= ':autocmd:'.name.':'.get(opts, 'pattern', '*')
|
2014-11-21 06:07:59 -07:00
|
|
|
call remote#define#AutocmdOnHost(a:host, rpc_method, sync, name, opts)
|
2014-11-17 06:46:13 -07:00
|
|
|
elseif type == 'function'
|
|
|
|
let rpc_method .= ':function:'.name
|
2014-11-21 06:07:59 -07:00
|
|
|
call remote#define#FunctionOnHost(a:host, rpc_method, sync, name, opts)
|
2014-11-17 06:46:13 -07:00
|
|
|
else
|
|
|
|
echoerr 'Invalid declaration type: '.type
|
|
|
|
endif
|
|
|
|
endfor
|
|
|
|
|
|
|
|
call add(plugins, {'path': a:path, 'specs': a:specs})
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
|
2014-11-21 06:07:59 -07:00
|
|
|
function! remote#host#LoadRemotePlugins()
|
|
|
|
if filereadable(s:remote_plugins_manifest)
|
|
|
|
exe 'source '.s:remote_plugins_manifest
|
2014-11-17 06:46:13 -07:00
|
|
|
endif
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
|
|
|
|
function! s:RegistrationCommands(host)
|
|
|
|
" Register a temporary host clone for discovering specs
|
|
|
|
let host_id = a:host.'-registration-clone'
|
2014-11-21 06:07:59 -07:00
|
|
|
call remote#host#RegisterClone(host_id, a:host)
|
2014-11-17 06:46:13 -07:00
|
|
|
let pattern = s:plugin_patterns[a:host]
|
2014-11-21 06:07:59 -07:00
|
|
|
let paths = globpath(&rtp, 'rplugin/'.a:host.'/'.pattern, 0, 1)
|
2015-05-30 02:33:45 -07:00
|
|
|
if empty(paths)
|
2014-11-24 14:53:21 -07:00
|
|
|
return []
|
|
|
|
endif
|
2015-05-30 02:33:45 -07:00
|
|
|
|
2014-11-17 06:46:13 -07:00
|
|
|
for path in paths
|
2014-11-21 06:07:59 -07:00
|
|
|
call remote#host#RegisterPlugin(host_id, path, [])
|
2014-11-17 06:46:13 -07:00
|
|
|
endfor
|
2014-11-21 06:07:59 -07:00
|
|
|
let channel = remote#host#Require(host_id)
|
2014-11-17 06:46:13 -07:00
|
|
|
let lines = []
|
|
|
|
for path in paths
|
|
|
|
let specs = rpcrequest(channel, 'specs', path)
|
2015-04-13 12:43:52 -07:00
|
|
|
if type(specs) != type([])
|
|
|
|
" host didn't return a spec list, indicates a failure while loading a
|
|
|
|
" plugin
|
|
|
|
continue
|
|
|
|
endif
|
2014-11-21 06:07:59 -07:00
|
|
|
call add(lines, "call remote#host#RegisterPlugin('".a:host
|
2014-11-17 06:46:13 -07:00
|
|
|
\ ."', '".path."', [")
|
|
|
|
for spec in specs
|
|
|
|
call add(lines, " \\ ".string(spec).",")
|
|
|
|
endfor
|
|
|
|
call add(lines, " \\ ])")
|
|
|
|
endfor
|
2015-05-30 02:33:45 -07:00
|
|
|
echomsg printf("remote/host: %s host registered plugins %s",
|
|
|
|
\ a:host, string(map(copy(paths), "fnamemodify(v:val, ':t')")))
|
|
|
|
|
2014-11-17 06:46:13 -07:00
|
|
|
" Delete the temporary host clone
|
|
|
|
call rpcstop(s:hosts[host_id].channel)
|
|
|
|
call remove(s:hosts, host_id)
|
|
|
|
call remove(s:plugins_for_host, host_id)
|
|
|
|
return lines
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
|
2014-11-21 06:07:59 -07:00
|
|
|
function! s:UpdateRemotePlugins()
|
2014-11-17 06:46:13 -07:00
|
|
|
let commands = []
|
|
|
|
let hosts = keys(s:hosts)
|
|
|
|
for host in hosts
|
|
|
|
if has_key(s:plugin_patterns, host)
|
|
|
|
let commands = commands
|
|
|
|
\ + ['" '.host.' plugins']
|
|
|
|
\ + s:RegistrationCommands(host)
|
|
|
|
\ + ['', '']
|
|
|
|
endif
|
|
|
|
endfor
|
2014-11-21 06:07:59 -07:00
|
|
|
call writefile(commands, s:remote_plugins_manifest)
|
2014-11-17 06:46:13 -07:00
|
|
|
endfunction
|
|
|
|
|
|
|
|
|
2014-11-21 06:07:59 -07:00
|
|
|
command! UpdateRemotePlugins call s:UpdateRemotePlugins()
|
2014-11-17 06:46:13 -07:00
|
|
|
|
|
|
|
|
|
|
|
let s:plugins_for_host = {}
|
2015-06-24 16:18:44 -07:00
|
|
|
function! remote#host#PluginsForHost(host)
|
2014-11-17 06:46:13 -07:00
|
|
|
if !has_key(s:plugins_for_host, a:host)
|
|
|
|
let s:plugins_for_host[a:host] = []
|
|
|
|
end
|
|
|
|
return s:plugins_for_host[a:host]
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
|
|
|
|
" Registration of standard hosts
|
|
|
|
|
2015-03-20 15:55:39 -07:00
|
|
|
" Python/Python3 {{{
|
2015-06-28 05:54:42 -07:00
|
|
|
function! s:RequirePythonHost(host)
|
|
|
|
let ver = (a:host.orig_name ==# 'python') ? 2 : 3
|
2015-03-20 15:55:39 -07:00
|
|
|
|
2014-11-17 06:46:13 -07:00
|
|
|
" Python host arguments
|
|
|
|
let args = ['-c', 'import neovim; neovim.start_host()']
|
|
|
|
|
2015-03-20 15:55:39 -07:00
|
|
|
" Collect registered Python plugins into args
|
2015-06-28 05:54:42 -07:00
|
|
|
let python_plugins = remote#host#PluginsForHost(a:host.name)
|
2014-11-17 06:46:13 -07:00
|
|
|
for plugin in python_plugins
|
|
|
|
call add(args, plugin.path)
|
|
|
|
endfor
|
|
|
|
|
|
|
|
try
|
2015-03-20 15:55:39 -07:00
|
|
|
let channel_id = rpcstart((ver == '2' ?
|
|
|
|
\ provider#python#Prog() : provider#python3#Prog()), args)
|
2014-11-17 06:46:13 -07:00
|
|
|
if rpcrequest(channel_id, 'poll') == 'ok'
|
|
|
|
return channel_id
|
|
|
|
endif
|
|
|
|
catch
|
2015-03-20 15:55:39 -07:00
|
|
|
echomsg v:exception
|
2014-11-17 06:46:13 -07:00
|
|
|
endtry
|
2015-05-08 03:09:59 -07:00
|
|
|
throw 'Failed to load Python host. You can try to see what happened '.
|
|
|
|
\ 'by starting Neovim with the environment variable '.
|
|
|
|
\ '$NVIM_PYTHON_LOG_FILE set to a file and opening '.
|
2015-04-13 12:43:52 -07:00
|
|
|
\ 'the generated log file. Also, the host stderr will be available '.
|
2015-05-08 03:09:59 -07:00
|
|
|
\ 'in Neovim log, so it may contain useful information. '.
|
|
|
|
\ 'See also ~/.nvimlog.'
|
2014-11-17 06:46:13 -07:00
|
|
|
endfunction
|
|
|
|
|
2015-06-24 16:18:44 -07:00
|
|
|
call remote#host#Register('python', '*.py', function('s:RequirePythonHost'))
|
|
|
|
call remote#host#Register('python3', '*.py', function('s:RequirePythonHost'))
|
2014-11-17 06:46:13 -07:00
|
|
|
" }}}
|