summaryrefslogtreecommitdiff
path: root/session.c
diff options
context:
space:
mode:
authorLeo Tenenbaum <pommicket@gmail.com>2021-03-02 17:38:16 -0500
committerLeo Tenenbaum <pommicket@gmail.com>2021-03-02 17:38:16 -0500
commit00b639118e32c7e65a402ddf70a57d0238b5abe1 (patch)
tree2eb42a40dbcd68337842b75afaa092cb0e41f603 /session.c
parent64bfab15c19ef0e0055b7b80b315a872b8946ce4 (diff)
fix restore session if no active buffer on close
also more command selector
Diffstat (limited to 'session.c')
-rw-r--r--session.c37
1 files changed, 29 insertions, 8 deletions
diff --git a/session.c b/session.c
index cbd84b0..4895f34 100644
--- a/session.c
+++ b/session.c
@@ -93,8 +93,8 @@ static void session_read_buffer(Ted *ted, FILE *fp) {
static void session_write_file(Ted *ted, FILE *fp) {
fwrite(SESSION_VERSION, 1, sizeof SESSION_VERSION, fp);
- write_u16(fp, (u16)(ted->active_node - ted->nodes)); // active node idx
- write_u16(fp, (u16)(ted->active_buffer - ted->buffers)); // active buffer idx
+ write_u16(fp, ted->active_node ? (u16)(ted->active_node - ted->nodes) : U16_MAX); // active node idx
+ write_u16(fp, ted->active_buffer ? (u16)(ted->active_buffer - ted->buffers) : U16_MAX); // active buffer idx
u16 nnodes = 0;
for (u16 i = 0; i < TED_MAX_NODES; ++i)
@@ -121,8 +121,8 @@ static void session_read_file(Ted *ted, FILE *fp) {
return; // wrong version
}
- u16 active_node_idx = clamp_u16(read_u16(fp), 0, TED_MAX_NODES);
- u16 active_buffer_idx = clamp_u16(read_u16(fp), 0, TED_MAX_BUFFERS);
+ u16 active_node_idx = read_u16(fp);
+ u16 active_buffer_idx = read_u16(fp);
u16 nnodes = clamp_u16(read_u16(fp), 0, TED_MAX_NODES);
for (u16 i = 0; i < nnodes; ++i) {
@@ -134,10 +134,31 @@ static void session_read_file(Ted *ted, FILE *fp) {
session_read_buffer(ted, fp);
}
- if (ted->nodes_used[active_node_idx])
- ted->active_node = &ted->nodes[active_node_idx];
- if (ted->buffers_used[active_buffer_idx])
- ted->active_buffer = &ted->buffers[active_buffer_idx];
+ if (active_node_idx == U16_MAX) {
+ ted->active_node = NULL;
+ } else {
+ active_node_idx = clamp_u16(active_node_idx, 0, TED_MAX_NODES);
+ if (ted->nodes_used[active_node_idx])
+ ted->active_node = &ted->nodes[active_node_idx];
+ }
+
+ if (active_buffer_idx == U16_MAX) {
+ ted->active_buffer = NULL;
+ } else {
+ active_buffer_idx = clamp_u16(active_buffer_idx, 0, TED_MAX_BUFFERS);
+ if (ted->buffers_used[active_buffer_idx])
+ ted->active_buffer = &ted->buffers[active_buffer_idx];
+ }
+
+ if (nbuffers && !ted->active_buffer) {
+ // set active buffer to something
+ for (u16 i = 0; i < TED_MAX_BUFFERS; ++i) {
+ if (ted->buffers_used[i]) {
+ ted_switch_to_buffer(ted, &ted->buffers[i]);
+ break;
+ }
+ }
+ }
}
static void session_write(Ted *ted) {