summaryrefslogtreecommitdiff
path: root/ted.c
diff options
context:
space:
mode:
authorLeo Tenenbaum <pommicket@gmail.com>2021-01-30 13:21:53 -0500
committerLeo Tenenbaum <pommicket@gmail.com>2021-01-30 13:21:53 -0500
commitb35a780479bbf5038a825d415f9ca49c92f6b425 (patch)
tree44e71007552714b03ae812e466495ec5c8a7e27b /ted.c
parenta61d90c32d4d6448148894872ebd91eb8f10fc2e (diff)
ctrl+q to quit (with unsaved changes dialog)
Diffstat (limited to 'ted.c')
-rw-r--r--ted.c58
1 files changed, 58 insertions, 0 deletions
diff --git a/ted.c b/ted.c
index 431768e..0e8be05 100644
--- a/ted.c
+++ b/ted.c
@@ -1,3 +1,5 @@
+static void menu_open(Ted *ted, Menu menu);
+
// this is a macro so we get -Wformat warnings
#define ted_seterr(buffer, ...) \
snprintf(ted->error, sizeof ted->error - 1, __VA_ARGS__)
@@ -196,3 +198,59 @@ static bool ted_new_file(Ted *ted) {
return false;
}
}
+
+// 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);
+}
+
+// are there any unsaved changes in any buffers?
+static bool ted_any_unsaved_changes(Ted *ted) {
+ bool *buffers_used = ted->buffers_used;
+ for (u16 i = 0; i < TED_MAX_BUFFERS; ++i) {
+ if (buffers_used[i]) {
+ if (buffer_unsaved_changes(&ted->buffers[i]))
+ return true;
+ }
+ }
+ return false;
+}
+
+// save all changes to all buffers with unsaved changes.
+// returns true if all buffers were saved successfully
+static bool ted_save_all(Ted *ted) {
+ bool success = true;
+ bool *buffers_used = ted->buffers_used;
+ for (u16 i = 0; i < TED_MAX_BUFFERS; ++i) {
+ if (buffers_used[i]) {
+ TextBuffer *buffer = &ted->buffers[i];
+ if (buffer_unsaved_changes(buffer)) {
+ if (buffer->filename && streq(buffer->filename, TED_UNTITLED)) {
+ ted_switch_to_buffer(ted, i);
+ menu_open(ted, MENU_SAVE_AS);
+ success = false; // we haven't saved this buffer yet; we've just opened the "save as" menu.
+ break;
+ } else {
+ if (!buffer_save(buffer))
+ success = false;
+ }
+ }
+ }
+ }
+ return success;
+}