diff --git a/ce/edit.c b/ce/edit.c index 24b19fe..99d6a9d 100644 --- a/ce/edit.c +++ b/ce/edit.c @@ -82,6 +82,8 @@ static bool prepare_lines_cb (void* ctx, const char* start, size_t len) { if (editor.current_line == NULL) editor.current_line = line; + editor.total_lines++; + return true; } @@ -99,8 +101,8 @@ static void update_vert_scroll (size_t screen_rows) { if (editor.cursor.line < editor.row_offset) editor.row_offset = editor.cursor.line; - if (editor.cursor.line >= editor.row_offset + (screen_rows - 1)) - editor.row_offset = editor.cursor.line - (screen_rows - 1) + 1; + if (editor.cursor.line >= editor.row_offset + screen_rows) + editor.row_offset = editor.cursor.line - screen_rows + 1; } static size_t count_digits (size_t n) { @@ -131,30 +133,24 @@ void edit_start (const char* file_path, const char* text) { bool edit_run = true; while (edit_run) { - update_horz_scroll (cols); - update_vert_scroll (rows); + size_t gutter_width = count_digits (editor.total_lines); + size_t effective_cols = (cols > gutter_width) ? cols - gutter_width : cols; - int w; - const size_t backbuffer_max = 128 * 1024; - size_t bb_remaining = backbuffer_max; + update_horz_scroll (effective_cols); + update_vert_scroll (rows - 1); + + const size_t backbuffer_max = 256 * 1024; char* backbuffer = arena_malloc (&temp_arena, backbuffer_max); - memset (backbuffer, 0, backbuffer_max); char* bbptr = backbuffer; - w = snprintf (bbptr, bb_remaining, ANSIQ_CUR_HOME); - bbptr += w; - bb_remaining -= w; + memcpy (bbptr, ANSIQ_CUR_HOME, sizeof (ANSIQ_CUR_HOME) - 1); + bbptr += sizeof (ANSIQ_CUR_HOME) - 1; size_t lines_drawn = 0; size_t current_idx = 0; struct list_node_link *line_link, *tmp_line_link; - list_foreach (editor.lines, line_link, tmp_line_link) editor.total_lines++; - - size_t gutter_width = count_digits (editor.total_lines); - size_t effective_cols = (cols > gutter_width + 3) ? cols - (gutter_width + 3) : cols; - list_foreach (editor.lines, line_link, tmp_line_link) { if (current_idx < editor.row_offset) { current_idx++; @@ -166,14 +162,9 @@ void edit_start (const char* file_path, const char* text) { struct edit_line* line = list_entry (line_link, struct edit_line, lines_link); - w = snprintf (bbptr, bb_remaining, ANSIQ_SCR_CLR_LINE); - bbptr += w; - bb_remaining -= w; - - w = snprintf (bbptr, bb_remaining, LINE_NUMBERS_STYLE " %*zu " ANSIQ_GR_RESET " ", - (int)gutter_width, current_idx + 1); - bbptr += w; - bb_remaining -= w; + bbptr += snprintf (bbptr, (backbuffer_max - (bbptr - backbuffer)), + ANSIQ_SCR_CLR_LINE LINE_NUMBERS_STYLE "%*zu" ANSIQ_GR_RESET, + (int)gutter_width, current_idx + 1); size_t part1_len = line->gb.gap_start; size_t part2_len = line->gb.size - line->gb.gap_end; @@ -187,9 +178,8 @@ void edit_start (const char* file_path, const char* text) { if (len > effective_cols) len = effective_cols; - w = snprintf (bbptr, bb_remaining, "%.*s", (int)len, &line->gb.buffer[start]); - bbptr += w; - bb_remaining -= w; + memcpy (bbptr, &line->gb.buffer[start], len); + bbptr += len; current_col += len; } @@ -204,43 +194,28 @@ void edit_start (const char* file_path, const char* text) { if (current_col + len > effective_cols) len = effective_cols - current_col; - w = snprintf (bbptr, bb_remaining, "%.*s", (int)len, - &line->gb.buffer[line->gb.gap_end + skip]); - bbptr += w; - bb_remaining -= w; + memcpy (bbptr, &line->gb.buffer[line->gb.gap_end + skip], len); + bbptr += len; } } - w = snprintf (bbptr, bb_remaining, ANSIQ_SCR_CLR2END "\n"); - bbptr += w; - bb_remaining -= w; + bbptr += snprintf (bbptr, (backbuffer_max - (bbptr - backbuffer)), ANSIQ_SCR_CLR2END "\n"); lines_drawn++; current_idx++; } - w = snprintf (bbptr, bb_remaining, ANSIQ_DYN_CUR_SET ANSIQ_SCR_CLR_LINE, (int)rows, 1); - bbptr += w; - bb_remaining -= w; + bbptr += snprintf (bbptr, (backbuffer_max - (bbptr - backbuffer)), + ANSIQ_DYN_CUR_SET ANSIQ_SCR_CLR_LINE STATUS_LINE_STYLE + "%*s" ANSIQ_DYN_CUR_SET + " Editing %s; line %zu col %zu; Mode: %s" ANSIQ_GR_RESET ANSIQ_DYN_CUR_SET + ANSIQ_CUR_VISIBLE, + (int)rows, 1, (int)cols, "", (int)rows, 1, file_path, editor.cursor.line + 1, + editor.cursor.col + 1, string_modes[editor.mode], + (int)(editor.cursor.line - editor.row_offset) + 1, + (int)(editor.cursor.col - editor.col_offset) + 1 + (int)gutter_width); - w = snprintf (bbptr, bb_remaining, STATUS_LINE_STYLE "%*s" ANSIQ_DYN_CUR_SET, (int)cols, "", - (int)rows, 1); - bbptr += w; - bb_remaining -= w; - - w = snprintf (bbptr, bb_remaining, " Editing %s; line %zu col %zu; Mode: %s" ANSIQ_GR_RESET, - file_path, editor.cursor.line + 1, editor.cursor.col + 1, - string_modes[editor.mode]); - bbptr += w; - bb_remaining -= w; - - w = snprintf (bbptr, bb_remaining, ANSIQ_DYN_CUR_SET ANSIQ_CUR_VISIBLE, - (int)(editor.cursor.line - editor.row_offset) + 1, - (int)(editor.cursor.col - editor.col_offset) + 1 + (int)gutter_width + 3); - bbptr += w; - bb_remaining -= w; - - mail_send (e_pgid, backbuffer, strlen (backbuffer)); + mail_send (e_pgid, backbuffer, bbptr - backbuffer); uint8_t ch = 0; mail_receive (&ch, 1); @@ -291,6 +266,7 @@ void edit_start (const char* file_path, const char* text) { editor.cursor.col = 0; editor.cursor.line++; editor.current_line = new_line; + editor.total_lines++; } } break; case KB_DELETE: @@ -361,7 +337,6 @@ void edit_start (const char* file_path, const char* text) { break; } - editor.total_lines = 0; arena_reset (&temp_arena); }