diff options
author | Thiago de Arruda <tpadilha84@gmail.com> | 2014-07-17 12:06:31 -0300 |
---|---|---|
committer | Thiago de Arruda <tpadilha84@gmail.com> | 2014-07-17 12:06:31 -0300 |
commit | 953d61cbf82d5f1acd68bd1ae2101d92f5ec5492 (patch) | |
tree | d4aa1fe08ad3f0a7e27b6628aad4925cd1fbfb2a /src/nvim/api/vim.c | |
parent | b92630c2fff7950141630f0d62b11404d0589ece (diff) | |
parent | 4dc642aa622cfac09f2f4752907137d68d8508fe (diff) | |
download | rneovim-953d61cbf82d5f1acd68bd1ae2101d92f5ec5492.tar.gz rneovim-953d61cbf82d5f1acd68bd1ae2101d92f5ec5492.tar.bz2 rneovim-953d61cbf82d5f1acd68bd1ae2101d92f5ec5492.zip |
Merge PR #895 'Core service providers...'
Diffstat (limited to 'src/nvim/api/vim.c')
-rw-r--r-- | src/nvim/api/vim.c | 48 |
1 files changed, 33 insertions, 15 deletions
diff --git a/src/nvim/api/vim.c b/src/nvim/api/vim.c index 03b9257d79..6c793cbc54 100644 --- a/src/nvim/api/vim.c +++ b/src/nvim/api/vim.c @@ -10,6 +10,7 @@ #include "nvim/api/private/defs.h" #include "nvim/api/buffer.h" #include "nvim/os/channel.h" +#include "nvim/os/provider.h" #include "nvim/vim.h" #include "nvim/buffer.h" #include "nvim/window.h" @@ -503,6 +504,22 @@ void vim_unsubscribe(uint64_t channel_id, String event) channel_unsubscribe(channel_id, e); } +/// Registers the channel as the provider for `method`. This fails if +/// a provider for `method` is already registered. +/// +/// @param channel_id The channel id +/// @param method The method name +/// @param[out] err Details of an error that may have occurred +void vim_register_provider(uint64_t channel_id, String method, Error *err) +{ + char buf[METHOD_MAXLEN]; + xstrlcpy(buf, method.data, sizeof(buf)); + + if (!provider_register(buf, channel_id)) { + set_api_error("Provider already registered", err); + } +} + /// Writes a message to vim output or error buffer. The string is split /// and flushed after each newline. Incomplete lines are kept for writing /// later. @@ -512,23 +529,24 @@ void vim_unsubscribe(uint64_t channel_id, String event) /// `emsg` instead of `msg` to print each line) static void write_msg(String message, bool to_err) { - static int pos = 0; - static char line_buf[LINE_BUFFER_SIZE]; + static int out_pos = 0, err_pos = 0; + static char out_line_buf[LINE_BUFFER_SIZE], err_line_buf[LINE_BUFFER_SIZE]; + +#define PUSH_CHAR(i, pos, line_buf, msg) \ + if (message.data[i] == NL || pos == LINE_BUFFER_SIZE - 1) { \ + line_buf[pos] = NUL; \ + msg((uint8_t *)line_buf); \ + pos = 0; \ + continue; \ + } \ + \ + line_buf[pos++] = message.data[i]; for (uint32_t i = 0; i < message.size; i++) { - if (message.data[i] == NL || pos == LINE_BUFFER_SIZE - 1) { - // Flush line - line_buf[pos] = NUL; - if (to_err) { - emsg((uint8_t *)line_buf); - } else { - msg((uint8_t *)line_buf); - } - - pos = 0; - continue; + if (to_err) { + PUSH_CHAR(i, err_pos, err_line_buf, emsg); + } else { + PUSH_CHAR(i, out_pos, out_line_buf, msg); } - - line_buf[pos++] = message.data[i]; } } |