shithub: orca

Download patch

ref: febfffdb6b83c6fe7bcd83a6409944bcf7e94af3
parent: 09d8618cf91cdeaf7c492c26202944fa4098adf1
author: cancel <cancel@cancel.fm>
date: Mon Dec 3 11:39:24 EST 2018

Clean up resizing logic, make it work with undo history

--- a/field.c
+++ b/field.c
@@ -40,22 +40,6 @@
                        src->width);
 }
 
-void field_resize_filled(Field* field, Usz height, Usz width, Glyph fill_char) {
-  assert(height <= ORCA_Y_MAX && width <= ORCA_X_MAX);
-  Usz old_height = field->height;
-  Usz old_width = field->width;
-  if (old_height == height && old_width == width)
-    return;
-  Usz old_cells = old_height * old_width;
-  Usz new_cells = height * width;
-  field->buffer = realloc(field->buffer, new_cells * sizeof(Glyph));
-  if (old_cells < new_cells) {
-    memset(field->buffer + old_cells, fill_char, (new_cells - old_cells) * sizeof(Glyph));
-  }
-  field->height = (U16)height;
-  field->width = (U16)width;
-}
-
 static inline bool glyph_char_is_valid(char c) { return c >= '!' && c <= '~'; }
 
 void field_fput(Field* f, FILE* stream) {
--- a/field.h
+++ b/field.h
@@ -6,7 +6,6 @@
 void field_deinit(Field* field);
 void field_resize_raw(Field* field, Usz height, Usz width);
 void field_resize_raw_if_necessary(Field* field, Usz height, Usz width);
-void field_resize_filled(Field* field, Usz height, Usz width, Glyph fill_char);
 void field_copy(Field* src, Field* dest);
 void field_fput(Field* field, FILE* stream);
 
--- a/tui_main.c
+++ b/tui_main.c
@@ -340,15 +340,19 @@
     tc->x = width - 1;
 }
 
-void tui_resize_grid(Field* field, Markmap_reusable* markmap,
-                     Field* scratch_field, Isz delta_h, Isz delta_w,
-                     Tui_cursor* tui_cursor, bool* needs_remarking) {
+void tui_resize_grid(Field* field, Markmap_reusable* markmap, Isz delta_h,
+                     Isz delta_w, Usz tick_num, Field* scratch_field,
+                     Undo_history* undo_hist, Tui_cursor* tui_cursor,
+                     bool* needs_remarking) {
   Isz new_height = (Isz)field->height + delta_h;
   Isz new_width = (Isz)field->width + delta_w;
   if (new_height < 1 || new_width < 1)
     return;
+  undo_history_push(undo_hist, field, tick_num);
   field_copy(field, scratch_field);
-  field_resize_filled(field, (Usz)new_height, (Usz)new_width, '.');
+  field_resize_raw(field, (Usz)new_height, (Usz)new_width);
+  // junky copies until i write a smarter thing
+  memset(field->buffer, '.', (Usz)new_height * (Usz)new_width * sizeof(Glyph));
   gbuffer_copy_subrect(scratch_field->buffer, field->buffer,
                        scratch_field->height, scratch_field->width,
                        field->height, field->width, 0, 0, 0, 0,
@@ -570,20 +574,20 @@
         ++ruler_spacing_y;
       break;
     case '(':
-      tui_resize_grid(&field, &markmap_r, &scratch_field, 0, -1, &tui_cursor,
-                      &needs_remarking);
+      tui_resize_grid(&field, &markmap_r, 0, -1, tick_num, &scratch_field,
+                      &undo_hist, &tui_cursor, &needs_remarking);
       break;
     case ')':
-      tui_resize_grid(&field, &markmap_r, &scratch_field, 0, 1, &tui_cursor,
-                      &needs_remarking);
+      tui_resize_grid(&field, &markmap_r, 0, 1, tick_num, &scratch_field,
+                      &undo_hist, &tui_cursor, &needs_remarking);
       break;
     case '_':
-      tui_resize_grid(&field, &markmap_r, &scratch_field, -1, 0, &tui_cursor,
-                      &needs_remarking);
+      tui_resize_grid(&field, &markmap_r, -1, 0, tick_num, &scratch_field,
+                      &undo_hist, &tui_cursor, &needs_remarking);
       break;
     case '+':
-      tui_resize_grid(&field, &markmap_r, &scratch_field, 1, 0, &tui_cursor,
-                      &needs_remarking);
+      tui_resize_grid(&field, &markmap_r, 1, 0, tick_num, &scratch_field,
+                      &undo_hist, &tui_cursor, &needs_remarking);
       break;
     case ' ':
       undo_history_push(&undo_hist, &field, tick_num);