aboutsummaryrefslogtreecommitdiff
path: root/src/nvim/api/ui.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/nvim/api/ui.c')
-rw-r--r--src/nvim/api/ui.c276
1 files changed, 63 insertions, 213 deletions
diff --git a/src/nvim/api/ui.c b/src/nvim/api/ui.c
index 625bcc6b4b..a9eaccfac5 100644
--- a/src/nvim/api/ui.c
+++ b/src/nvim/api/ui.c
@@ -1,3 +1,6 @@
+// This is an open source non-commercial project. Dear PVS-Studio, please check
+// it. PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
+
#include <assert.h>
#include <stddef.h>
#include <stdint.h>
@@ -12,9 +15,11 @@
#include "nvim/api/private/defs.h"
#include "nvim/api/private/helpers.h"
#include "nvim/popupmnu.h"
+#include "nvim/cursor_shape.h"
#ifdef INCLUDE_GENERATED_DECLARATIONS
# include "api/ui.c.generated.h"
+# include "ui_events_remote.generated.h"
#endif
typedef struct {
@@ -25,13 +30,13 @@ typedef struct {
static PMap(uint64_t) *connected_uis = NULL;
void remote_ui_init(void)
- FUNC_API_NOEXPORT
+ FUNC_API_NOEXPORT
{
connected_uis = pmap_new(uint64_t)();
}
void remote_ui_disconnect(uint64_t channel_id)
- FUNC_API_NOEXPORT
+ FUNC_API_NOEXPORT
{
UI *ui = pmap_get(uint64_t)(connected_uis, channel_id);
if (!ui) {
@@ -48,27 +53,27 @@ void remote_ui_disconnect(uint64_t channel_id)
void nvim_ui_attach(uint64_t channel_id, Integer width, Integer height,
Dictionary options, Error *err)
- FUNC_API_SINCE(1) FUNC_API_NOEVAL
+ FUNC_API_SINCE(1) FUNC_API_REMOTE_ONLY
{
if (pmap_has(uint64_t)(connected_uis, channel_id)) {
- api_set_error(err, Exception, _("UI already attached for channel"));
+ api_set_error(err, kErrorTypeException, "UI already attached for channel");
return;
}
if (width <= 0 || height <= 0) {
- api_set_error(err, Validation,
- _("Expected width > 0 and height > 0"));
+ api_set_error(err, kErrorTypeValidation,
+ "Expected width > 0 and height > 0");
return;
}
UI *ui = xcalloc(1, sizeof(UI));
ui->width = (int)width;
ui->height = (int)height;
ui->rgb = true;
- ui->pum_external = false;
ui->resize = remote_ui_resize;
ui->clear = remote_ui_clear;
ui->eol_clear = remote_ui_eol_clear;
ui->cursor_goto = remote_ui_cursor_goto;
+ ui->mode_info_set = remote_ui_mode_info_set;
ui->update_menu = remote_ui_update_menu;
ui->busy_start = remote_ui_busy_start;
ui->busy_stop = remote_ui_busy_stop;
@@ -90,9 +95,11 @@ void nvim_ui_attach(uint64_t channel_id, Integer width, Integer height,
ui->set_icon = remote_ui_set_icon;
ui->event = remote_ui_event;
+ memset(ui->ui_ext, 0, sizeof(ui->ui_ext));
+
for (size_t i = 0; i < options.size; i++) {
ui_set_option(ui, options.items[i].key, options.items[i].value, err);
- if (err->set) {
+ if (ERROR_SET(err)) {
xfree(ui);
return;
}
@@ -118,10 +125,10 @@ void ui_attach(uint64_t channel_id, Integer width, Integer height,
}
void nvim_ui_detach(uint64_t channel_id, Error *err)
- FUNC_API_SINCE(1) FUNC_API_NOEVAL
+ FUNC_API_SINCE(1) FUNC_API_REMOTE_ONLY
{
if (!pmap_has(uint64_t)(connected_uis, channel_id)) {
- api_set_error(err, Exception, _("UI is not attached for channel"));
+ api_set_error(err, kErrorTypeException, "UI is not attached for channel");
return;
}
remote_ui_disconnect(channel_id);
@@ -130,16 +137,16 @@ void nvim_ui_detach(uint64_t channel_id, Error *err)
void nvim_ui_try_resize(uint64_t channel_id, Integer width,
Integer height, Error *err)
- FUNC_API_SINCE(1) FUNC_API_NOEVAL
+ FUNC_API_SINCE(1) FUNC_API_REMOTE_ONLY
{
if (!pmap_has(uint64_t)(connected_uis, channel_id)) {
- api_set_error(err, Exception, _("UI is not attached for channel"));
+ api_set_error(err, kErrorTypeException, "UI is not attached for channel");
return;
}
if (width <= 0 || height <= 0) {
- api_set_error(err, Validation,
- _("Expected width > 0 and height > 0"));
+ api_set_error(err, kErrorTypeValidation,
+ "Expected width > 0 and height > 0");
return;
}
@@ -151,39 +158,64 @@ void nvim_ui_try_resize(uint64_t channel_id, Integer width,
void nvim_ui_set_option(uint64_t channel_id, String name,
Object value, Error *error)
- FUNC_API_SINCE(1) FUNC_API_NOEVAL
+ FUNC_API_SINCE(1) FUNC_API_REMOTE_ONLY
{
if (!pmap_has(uint64_t)(connected_uis, channel_id)) {
- api_set_error(error, Exception, _("UI is not attached for channel"));
+ api_set_error(error, kErrorTypeException, "UI is not attached for channel");
return;
}
UI *ui = pmap_get(uint64_t)(connected_uis, channel_id);
ui_set_option(ui, name, value, error);
- if (!error->set) {
+ if (!ERROR_SET(error)) {
ui_refresh();
}
}
-static void ui_set_option(UI *ui, String name, Object value, Error *error) {
- if (strcmp(name.data, "rgb") == 0) {
+static void ui_set_option(UI *ui, String name, Object value, Error *error)
+{
+#define UI_EXT_OPTION(o, e) \
+ do { \
+ if (strequal(name.data, #o)) { \
+ if (value.type != kObjectTypeBoolean) { \
+ api_set_error(error, kErrorTypeValidation, #o " must be a Boolean"); \
+ return; \
+ } \
+ ui->ui_ext[(e)] = value.data.boolean; \
+ return; \
+ } \
+ } while (0)
+
+ if (strequal(name.data, "rgb")) {
if (value.type != kObjectTypeBoolean) {
- api_set_error(error, Validation, _("rgb must be a Boolean"));
+ api_set_error(error, kErrorTypeValidation, "rgb must be a Boolean");
return;
}
ui->rgb = value.data.boolean;
- } else if (strcmp(name.data, "popupmenu_external") == 0) {
+ return;
+ }
+
+ UI_EXT_OPTION(ext_cmdline, kUICmdline);
+ UI_EXT_OPTION(ext_popupmenu, kUIPopupmenu);
+ UI_EXT_OPTION(ext_tabline, kUITabline);
+ UI_EXT_OPTION(ext_wildmenu, kUIWildmenu);
+
+ if (strequal(name.data, "popupmenu_external")) {
+ // LEGACY: Deprecated option, use `ui_ext` instead.
if (value.type != kObjectTypeBoolean) {
- api_set_error(error, Validation,
- _("popupmenu_external must be a Boolean"));
+ api_set_error(error, kErrorTypeValidation,
+ "popupmenu_external must be a Boolean");
return;
}
- ui->pum_external = value.data.boolean;
- } else {
- api_set_error(error, Validation, _("No such ui option"));
+ ui->ui_ext[kUIPopupmenu] = value.data.boolean;
+ return;
}
+
+ api_set_error(error, kErrorTypeValidation, "No such ui option");
+#undef UI_EXT_OPTION
}
+/// Pushes data into UI.UIData, to be consumed later by remote_ui_flush().
static void push_call(UI *ui, char *name, Array args)
{
Array call = ARRAY_DICT_INIT;
@@ -206,205 +238,23 @@ static void push_call(UI *ui, char *name, Array args)
kv_A(data->buffer, kv_size(data->buffer) - 1).data.array = call;
}
-static void remote_ui_resize(UI *ui, int width, int height)
-{
- Array args = ARRAY_DICT_INIT;
- ADD(args, INTEGER_OBJ(width));
- ADD(args, INTEGER_OBJ(height));
- push_call(ui, "resize", args);
-}
-
-static void remote_ui_clear(UI *ui)
-{
- Array args = ARRAY_DICT_INIT;
- push_call(ui, "clear", args);
-}
-
-static void remote_ui_eol_clear(UI *ui)
-{
- Array args = ARRAY_DICT_INIT;
- push_call(ui, "eol_clear", args);
-}
-
-static void remote_ui_cursor_goto(UI *ui, int row, int col)
-{
- Array args = ARRAY_DICT_INIT;
- ADD(args, INTEGER_OBJ(row));
- ADD(args, INTEGER_OBJ(col));
- push_call(ui, "cursor_goto", args);
-}
-
-static void remote_ui_update_menu(UI *ui)
-{
- Array args = ARRAY_DICT_INIT;
- push_call(ui, "update_menu", args);
-}
-
-static void remote_ui_busy_start(UI *ui)
-{
- Array args = ARRAY_DICT_INIT;
- push_call(ui, "busy_start", args);
-}
-
-static void remote_ui_busy_stop(UI *ui)
-{
- Array args = ARRAY_DICT_INIT;
- push_call(ui, "busy_stop", args);
-}
-
-static void remote_ui_mouse_on(UI *ui)
-{
- Array args = ARRAY_DICT_INIT;
- push_call(ui, "mouse_on", args);
-}
-
-static void remote_ui_mouse_off(UI *ui)
-{
- Array args = ARRAY_DICT_INIT;
- push_call(ui, "mouse_off", args);
-}
-
-static void remote_ui_mode_change(UI *ui, int mode)
-{
- Array args = ARRAY_DICT_INIT;
- if (mode == INSERT) {
- ADD(args, STRING_OBJ(cstr_to_string("insert")));
- } else if (mode == REPLACE) {
- ADD(args, STRING_OBJ(cstr_to_string("replace")));
- } else if (mode == CMDLINE) {
- ADD(args, STRING_OBJ(cstr_to_string("cmdline")));
- } else {
- assert(mode == NORMAL);
- ADD(args, STRING_OBJ(cstr_to_string("normal")));
- }
- push_call(ui, "mode_change", args);
-}
-
-static void remote_ui_set_scroll_region(UI *ui, int top, int bot, int left,
- int right)
-{
- Array args = ARRAY_DICT_INIT;
- ADD(args, INTEGER_OBJ(top));
- ADD(args, INTEGER_OBJ(bot));
- ADD(args, INTEGER_OBJ(left));
- ADD(args, INTEGER_OBJ(right));
- push_call(ui, "set_scroll_region", args);
-}
-
-static void remote_ui_scroll(UI *ui, int count)
-{
- Array args = ARRAY_DICT_INIT;
- ADD(args, INTEGER_OBJ(count));
- push_call(ui, "scroll", args);
-}
static void remote_ui_highlight_set(UI *ui, HlAttrs attrs)
{
Array args = ARRAY_DICT_INIT;
- Dictionary hl = ARRAY_DICT_INIT;
-
- if (attrs.bold) {
- PUT(hl, "bold", BOOLEAN_OBJ(true));
- }
-
- if (attrs.underline) {
- PUT(hl, "underline", BOOLEAN_OBJ(true));
- }
-
- if (attrs.undercurl) {
- PUT(hl, "undercurl", BOOLEAN_OBJ(true));
- }
-
- if (attrs.italic) {
- PUT(hl, "italic", BOOLEAN_OBJ(true));
- }
-
- if (attrs.reverse) {
- PUT(hl, "reverse", BOOLEAN_OBJ(true));
- }
-
- if (attrs.foreground != -1) {
- PUT(hl, "foreground", INTEGER_OBJ(attrs.foreground));
- }
-
- if (attrs.background != -1) {
- PUT(hl, "background", INTEGER_OBJ(attrs.background));
- }
-
- if (attrs.special != -1) {
- PUT(hl, "special", INTEGER_OBJ(attrs.special));
- }
+ Dictionary hl = hlattrs2dict(attrs);
ADD(args, DICTIONARY_OBJ(hl));
push_call(ui, "highlight_set", args);
}
-static void remote_ui_put(UI *ui, uint8_t *data, size_t size)
-{
- Array args = ARRAY_DICT_INIT;
- String str = { .data = xmemdupz(data, size), .size = size };
- ADD(args, STRING_OBJ(str));
- push_call(ui, "put", args);
-}
-
-static void remote_ui_bell(UI *ui)
-{
- Array args = ARRAY_DICT_INIT;
- push_call(ui, "bell", args);
-}
-
-static void remote_ui_visual_bell(UI *ui)
-{
- Array args = ARRAY_DICT_INIT;
- push_call(ui, "visual_bell", args);
-}
-
-static void remote_ui_update_fg(UI *ui, int fg)
-{
- Array args = ARRAY_DICT_INIT;
- ADD(args, INTEGER_OBJ(fg));
- push_call(ui, "update_fg", args);
-}
-
-static void remote_ui_update_bg(UI *ui, int bg)
-{
- Array args = ARRAY_DICT_INIT;
- ADD(args, INTEGER_OBJ(bg));
- push_call(ui, "update_bg", args);
-}
-
-static void remote_ui_update_sp(UI *ui, int sp)
-{
- Array args = ARRAY_DICT_INIT;
- ADD(args, INTEGER_OBJ(sp));
- push_call(ui, "update_sp", args);
-}
-
static void remote_ui_flush(UI *ui)
{
UIData *data = ui->data;
- channel_send_event(data->channel_id, "redraw", data->buffer);
- data->buffer = (Array)ARRAY_DICT_INIT;
-}
-
-static void remote_ui_suspend(UI *ui)
-{
- Array args = ARRAY_DICT_INIT;
- push_call(ui, "suspend", args);
-}
-
-static void remote_ui_set_title(UI *ui, char *title)
-{
- Array args = ARRAY_DICT_INIT;
- ADD(args, STRING_OBJ(cstr_to_string(title)));
- push_call(ui, "set_title", args);
-}
-
-static void remote_ui_set_icon(UI *ui, char *icon)
-{
- Array args = ARRAY_DICT_INIT;
- ADD(args, STRING_OBJ(cstr_to_string(icon)));
- push_call(ui, "set_icon", args);
+ if (data->buffer.size > 0) {
+ rpc_send_event(data->channel_id, "redraw", data->buffer);
+ data->buffer = (Array)ARRAY_DICT_INIT;
+ }
}
static void remote_ui_event(UI *ui, char *name, Array args, bool *args_consumed)