From fb2a0b9998a599f9e8a9a1e911fd43345778f6ef Mon Sep 17 00:00:00 2001 From: Leo Tenenbaum Date: Sun, 28 Feb 2021 14:41:58 -0500 Subject: :split-swap --- base.h | 6 ++++++ command.c | 3 +++ command.h | 8 ++++++-- main.c | 1 - node.c | 19 ++++++++++++++++--- ted.c | 8 ++++++++ ted.cfg | 1 + 7 files changed, 40 insertions(+), 6 deletions(-) diff --git a/base.h b/base.h index c2cd80d..8994daf 100644 --- a/base.h +++ b/base.h @@ -32,6 +32,12 @@ #include #include +#if !__TINYC__ && __STDC_VERSION__ >= 201112 +#define static_assert_if_possible(cond) _Static_assert(cond, "Static assertion failed"); +#else +#define static_assert_if_possible(cond) +#endif + typedef uint8_t u8; typedef uint16_t u16; typedef uint32_t u32; diff --git a/command.c b/command.c index 5d98922..05f7c48 100644 --- a/command.c +++ b/command.c @@ -317,5 +317,8 @@ void command_execute(Ted *ted, Command c, i64 argument) { case CMD_SPLIT_JOIN: node_join(ted, node); break; + case CMD_SPLIT_SWAP: + node_split_swap(ted); + break; } } diff --git a/command.h b/command.h index a8efe35..ad5034c 100644 --- a/command.h +++ b/command.h @@ -75,7 +75,8 @@ ENUM_U16 { CMD_SPLIT_HORIZONTAL, CMD_SPLIT_VERTICAL, CMD_SPLIT_JOIN, - + CMD_SPLIT_SWAP, // go to the other side of a split + CMD_ESCAPE, // by default this is the escape key. closes menus, etc. CMD_COUNT @@ -85,7 +86,7 @@ typedef struct { char const *name; Command cmd; } CommandName; -static CommandName const command_names[CMD_COUNT] = { +static CommandName const command_names[] = { {"unknown", CMD_UNKNOWN}, {"noop", CMD_NOOP}, {"left", CMD_LEFT}, @@ -147,6 +148,9 @@ static CommandName const command_names[CMD_COUNT] = { {"split-horizontal", CMD_SPLIT_HORIZONTAL}, {"split-vertical", CMD_SPLIT_VERTICAL}, {"split-join", CMD_SPLIT_JOIN}, + {"split-swap", CMD_SPLIT_SWAP}, {"escape", CMD_ESCAPE}, }; +static_assert_if_possible(arr_count(command_names) == CMD_COUNT) + diff --git a/main.c b/main.c index 1d4e528..1bf3612 100644 --- a/main.c +++ b/main.c @@ -1,5 +1,4 @@ // @TODO: -// - :split-swap // - fix: ctrl+f something, then switch to another tab (hl rects still showing up) // - Windows installation diff --git a/node.c b/node.c index 1da07ba..3a404cb 100644 --- a/node.c +++ b/node.c @@ -137,8 +137,7 @@ static void node_close(Ted *ted, u16 node_idx) { // make sure we don't set the active node to a split while (!new_active_node->tabs) new_active_node = &ted->nodes[new_active_node->split_a]; - ted->active_node = new_active_node; - ted->active_buffer = &ted->buffers[ted->active_node->tabs[ted->active_node->active_tab]]; + ted_node_switch(ted, new_active_node); } } } @@ -390,7 +389,21 @@ static void node_split(Ted *ted, Node *node, bool vertical) { node->split_vertical = vertical; node->split_pos = 0.5f; if (node == ted->active_node) - ted->active_node = &ted->nodes[right_idx]; + ted_node_switch(ted, &ted->nodes[right_idx]); + } + } +} + +// swap to the other side of a split +static void node_split_swap(Ted *ted) { + assert(ted->active_node); + u16 active_node_idx = (u16)(ted->active_node - ted->nodes); + Node *parent = &ted->nodes[node_parent(ted, active_node_idx)]; + if (parent) { + if (parent->split_a == active_node_idx) { + ted_node_switch(ted, &ted->nodes[parent->split_b]); + } else { + ted_node_switch(ted, &ted->nodes[parent->split_a]); } } } diff --git a/ted.c b/ted.c index fcc1bb1..17f2205 100644 --- a/ted.c +++ b/ted.c @@ -130,6 +130,13 @@ static i32 ted_new_node(Ted *ted) { } +// switch to this node +static void ted_node_switch(Ted *ted, Node *node) { + ted->active_node = node; + assert(node->tabs); + ted->active_buffer = &ted->buffers[node->tabs[node->active_tab]]; +} + static bool node_tab_close(Ted *ted, Node *node, u16 index); // Open a new buffer. Fills out *tab to the index of the tab used, and *buffer_idx to the index of the buffer. @@ -187,6 +194,7 @@ static void ted_switch_to_buffer(Ted *ted, u16 buffer_idx) { assert(0); } + // Returns true on success static bool ted_open_file(Ted *ted, char const *filename) { // first, check if file is already open diff --git a/ted.cfg b/ted.cfg index 1d4e64e..070a419 100644 --- a/ted.cfg +++ b/ted.cfg @@ -125,6 +125,7 @@ Ctrl+/ = :split-horizontal Ctrl+Shift+/ = :split-vertical # unsplit Ctrl+j = :split-join +Ctrl+tab = :split-swap Escape = :escape -- cgit v1.2.3