diff --git a/autoload/SpaceVim/api.vim b/autoload/SpaceVim/api.vim index 0087570b2..2196f876a 100644 --- a/autoload/SpaceVim/api.vim +++ b/autoload/SpaceVim/api.vim @@ -32,9 +32,8 @@ let s:apis = {} "" -"@public -"Import API base the given {name}, and return the API object. for all -"available APIs please check |spacevim-api| +" Import API base the given {name}, and return the API object. for all +" available APIs please check |spacevim-api| function! SpaceVim#api#import(name) abort if has_key(s:apis, a:name) return deepcopy(s:apis[a:name]) @@ -48,11 +47,33 @@ function! SpaceVim#api#import(name) abort return p endfunction +"" +" Register new {api} named {name}, 1 will be returned if succeed. for all +" available APIs please check |spacevim-api| +" +" Example: +" > +" let s:hello = {} +" function! s:hello.say(who) +" echo 'hello! ' . a:who +" endfunction +" call SpaceVim#api#register('hello', s:hello) +" +" " after registering api, you can use this api +" " in other vim scripts like this: +" let s:H = SpaceVim#api#import('hello') +" call s:H.say('wsdjeg') +" < function! SpaceVim#api#register(name, api) abort - if !empty(SpaceVim#api#import(a:name)) - echoerr '[SpaceVim api] Api : ' . a:name . ' already existed!' + " the name must be string + if type(a:name) ==# type('') && strlen(a:name) >= 1 + if !empty(SpaceVim#api#import(a:name)) + call SpaceVim#logger#warn('API:' . a:name . ' already existed!') + else + let s:apis[a:name] = deepcopy(a:api) + endif else - let s:apis[a:name] = deepcopy(a:api) + call SpaceVim#logger#warn('failed to register api, the name must be a no empty string!') endif endfunction diff --git a/doc/SpaceVim.txt b/doc/SpaceVim.txt index b06a6fca6..b45bfccb4 100644 --- a/doc/SpaceVim.txt +++ b/doc/SpaceVim.txt @@ -1472,6 +1472,24 @@ SpaceVim#api#import({name}) *SpaceVim#api#import()* Import API base the given {name}, and return the API object. for all available APIs please check |spacevim-api| +SpaceVim#api#register({name}, {api}) *SpaceVim#api#register()* + Register new {api} named {name}, 1 will be returned if succeed. for all + available APIs please check |spacevim-api| + + Example: +> + let s:hello = {} + function! s:hello.say(who) + echo 'hello! ' . a:who + endfunction + call SpaceVim#api#register('hello', s:hello) + + " after registering api, you can use this api + " in other vim scripts like this: + let s:H = SpaceVim#api#import('hello') + call s:H.say('wsdjeg') +< + SpaceVim#custom#SPC({m}, {keys}, {cmd}, {desc}, {is_cmd}) *SpaceVim#custom#SPC()* The first parameter sets the type of shortcut key, which can be `nnoremap`