diff options
Diffstat (limited to 'ted.c')
-rw-r--r-- | ted.c | 27 |
1 files changed, 18 insertions, 9 deletions
@@ -361,23 +361,32 @@ static Status ted_open_buffer(Ted *ted, u16 *buffer_idx, u16 *tab) { } } - -// Returns true on success -static bool ted_open_file(Ted *ted, char const *filename) { - char path[TED_PATH_MAX]; - ted_path_full(ted, filename, path, sizeof path); - - // first, check if file is already open +// Returns the buffer containing the file at `path`, or NULL if there is none. +static TextBuffer *ted_get_buffer_with_file(Ted *ted, const char *path) { bool *buffers_used = ted->buffers_used; TextBuffer *buffers = ted->buffers; for (u16 i = 0; i < TED_MAX_BUFFERS; ++i) { if (buffers_used[i]) { if (buffers[i].filename && paths_eq(path, buffers[i].filename)) { - ted_switch_to_buffer(ted, &buffers[i]); - return true; + return &buffers[i]; } } } + return NULL; +} + + +// Returns true on success +static bool ted_open_file(Ted *ted, char const *filename) { + char path[TED_PATH_MAX]; + ted_path_full(ted, filename, path, sizeof path); + + // first, check if file is already open + TextBuffer *already_open = ted_get_buffer_with_file(ted, path); + if (already_open) { + ted_switch_to_buffer(ted, already_open); + return true; + } // not open; we need to load it u16 buffer_idx, tab_idx; |