api/msgpack-rpc: Implement channel_close and expose to vimscript

Simple function for closing a channel by id
This commit is contained in:
Thiago de Arruda
2014-09-10 09:32:29 -03:00
parent 03f4d17fc9
commit 5060902930
2 changed files with 44 additions and 2 deletions

View File

@@ -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)
{

View File

@@ -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)