From c77fc69367003edb43b480f1726db02641bdd0be Mon Sep 17 00:00:00 2001 From: Leo Tenenbaum Date: Thu, 4 Feb 2021 11:04:15 -0500 Subject: prevent opening the same file twice --- main.c | 1 - ted.c | 52 +++++++++++++++++++++++++++++++++------------------- 2 files changed, 33 insertions(+), 20 deletions(-) diff --git a/main.c b/main.c index 2a7fc1a..96820b6 100644 --- a/main.c +++ b/main.c @@ -1,6 +1,5 @@ // @TODO: // - popup to reload files and config on change -// - shouldn't be possible to open the same file twice // - find & replace (with regex) // - split // - completion diff --git a/ted.c b/ted.c index 73e0965..4b80a97 100644 --- a/ted.c +++ b/ted.c @@ -165,8 +165,41 @@ static Status ted_open_buffer(Ted *ted, u16 *buffer_idx, u16 *tab) { } } +// sets the active buffer to this buffer, and updates active_node, etc. accordingly +static void ted_switch_to_buffer(Ted *ted, u16 buffer_idx) { + ted->active_buffer = &ted->buffers[buffer_idx]; + // now we need to figure out where this buffer is + bool *nodes_used = ted->nodes_used; + for (u16 i = 0; i < TED_MAX_NODES; ++i) { + if (nodes_used[i]) { + Node *node = &ted->nodes[i]; + arr_foreach_ptr(node->tabs, u16, tab) { + if (buffer_idx == *tab) { + node->active_tab = (u16)(tab - node->tabs); + ted->active_node = node; + return; + } + } + } + } + assert(0); +} + // Returns true on success static bool ted_open_file(Ted *ted, char const *filename) { + // first, check if file is already open + bool *buffers_used = ted->buffers_used; + TextBuffer *buffers = ted->buffers; + for (u16 i = 0; i < TED_MAX_BUFFERS; ++i) { + if (buffers_used[i]) { + if (streq(filename, buffer_get_filename(&buffers[i]))) { + ted_switch_to_buffer(ted, i); + return true; + } + } + } + + // not open; we need to load it u16 buffer_idx, tab_idx; if (ted->active_buffer && buffer_is_untitled(ted->active_buffer) && buffer_empty(ted->active_buffer)) { // the active buffer is just an empty untitled buffer. open it here. @@ -204,25 +237,6 @@ static bool ted_new_file(Ted *ted) { } } -// sets the active buffer to this buffer, and updates active_node, etc. accordingly -static void ted_switch_to_buffer(Ted *ted, u16 buffer_idx) { - ted->active_buffer = &ted->buffers[buffer_idx]; - // now we need to figure out where this buffer is - bool *nodes_used = ted->nodes_used; - for (u16 i = 0; i < TED_MAX_NODES; ++i) { - if (nodes_used[i]) { - Node *node = &ted->nodes[i]; - arr_foreach_ptr(node->tabs, u16, tab) { - if (buffer_idx == *tab) { - node->active_tab = (u16)(tab - node->tabs); - ted->active_node = node; - return; - } - } - } - } - assert(0); -} // save all changes to all buffers with unsaved changes. // returns true if all buffers were saved successfully -- cgit v1.2.3