diff options
author | Thiago de Arruda <tpadilha84@gmail.com> | 2014-09-10 09:32:29 -0300 |
---|---|---|
committer | Thiago de Arruda <tpadilha84@gmail.com> | 2014-09-12 13:25:29 -0300 |
commit | 50609029301d0baac36d7500c2d80da9efb41861 (patch) | |
tree | bcb0c40efe2c9b30edb437929429a12b61f75fd4 /src | |
parent | 03f4d17fc9d72c8862daf575804e0104e3aeb31b (diff) | |
download | rneovim-50609029301d0baac36d7500c2d80da9efb41861.tar.gz rneovim-50609029301d0baac36d7500c2d80da9efb41861.tar.bz2 rneovim-50609029301d0baac36d7500c2d80da9efb41861.zip |
api/msgpack-rpc: Implement `channel_close` and expose to vimscript
Simple function for closing a channel by id
Diffstat (limited to 'src')
-rw-r--r-- | src/nvim/eval.c | 21 | ||||
-rw-r--r-- | src/nvim/os/channel.c | 25 |
2 files changed, 44 insertions, 2 deletions
diff --git a/src/nvim/eval.c b/src/nvim/eval.c index 8b370e9948..8becb29b26 100644 --- a/src/nvim/eval.c +++ b/src/nvim/eval.c @@ -6306,6 +6306,7 @@ static struct fst { {"acos", 1, 1, f_acos}, /* WJMc */ {"add", 2, 2, f_add}, {"and", 2, 2, f_and}, + {"api_close", 1, 1, f_api_close}, {"api_spawn", 1, 2, f_api_spawn}, {"append", 2, 2, f_append}, {"argc", 0, 0, f_argc}, @@ -7054,6 +7055,26 @@ static void f_and(typval_T *argvars, typval_T *rettv) & get_tv_number_chk(&argvars[1], NULL); } +// "api_close(prog, argv)" function +static void f_api_close(typval_T *argvars, typval_T *rettv) +{ + rettv->v_type = VAR_NUMBER; + rettv->vval.v_number = 0; + + if (check_restricted() || check_secure()) { + return; + } + + if (argvars[0].v_type != VAR_NUMBER) { + // Wrong argument types + EMSG(_(e_invarg)); + return; + } + + rettv->vval.v_number = channel_close(argvars[0].vval.v_number); +} + + // "api_spawn(prog, argv)" function static void f_api_spawn(typval_T *argvars, typval_T *rettv) { diff --git a/src/nvim/os/channel.c b/src/nvim/os/channel.c index 5a42db112e..ad8f378dfc 100644 --- a/src/nvim/os/channel.c +++ b/src/nvim/os/channel.c @@ -267,6 +267,23 @@ void channel_unsubscribe(uint64_t id, char *event) unsubscribe(channel, event); } +/// Closes a channel +/// +/// @param id The channel id +/// @return true if successful, false otherwise +bool channel_close(uint64_t id) +{ + Channel *channel; + + if (!(channel = pmap_get(uint64_t)(channels, id)) || !channel->enabled) { + return false; + } + + channel_kill(channel); + channel->enabled = false; + return true; +} + /// Creates an API channel from stdin/stdout. This is used when embedding /// Neovim static void channel_from_stdio(void) @@ -496,7 +513,13 @@ static void close_channel(Channel *channel) pmap_free(cstr_t)(channel->subscribed_events); kv_destroy(channel->call_stack); + channel_kill(channel); + free(channel); +} + +static void channel_kill(Channel *channel) +{ if (channel->is_job) { if (channel->data.job) { job_stop(channel->data.job); @@ -511,8 +534,6 @@ static void close_channel(Channel *channel) mch_exit(0); } } - - free(channel); } static void close_cb(uv_handle_t *handle) |