diff options
author | pommicket <pommicket@gmail.com> | 2022-12-31 12:31:33 -0500 |
---|---|---|
committer | pommicket <pommicket@gmail.com> | 2022-12-31 12:31:33 -0500 |
commit | 2a9f03edf21f8d6d0392a74eb0a6b2507c261a55 (patch) | |
tree | 115f07dc855100b088b4507f072f97ac71753cf1 /ted.c | |
parent | c1c684c2346042022ba1b4fa86ca0983ad2f32a1 (diff) |
ted_get_buffer_with_file, sort usages
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; |