tInsert characters to row buffer in insert mode - ve - a minimal text editor (work in progress) (HTM) git clone git://src.adamsgaard.dk/ve (DIR) Log (DIR) Files (DIR) Refs (DIR) README (DIR) LICENSE --- (DIR) commit 27dc307eae9e93c25bc9bba5fa30e92fc2eba879 (DIR) parent 0ebc6405b29708897e7f73c3216dbabaef92e852 (HTM) Author: Anders Damsgaard <anders@adamsgaard.dk> Date: Tue, 6 Aug 2019 15:51:22 +0200 Insert characters to row buffer in insert mode Diffstat: M input.c | 3 +++ M row.c | 16 +++++++++++++--- M row.h | 1 + 3 files changed, 17 insertions(+), 3 deletions(-) --- (DIR) diff --git a/input.c b/input.c t@@ -2,6 +2,7 @@ #include <stdlib.h> #include "byote.h" #include "terminal.h" +#include "edit.h" #define CTRL_KEY(k) ((k) & 0x1f) t@@ -126,6 +127,8 @@ editor_process_keypress() case 27: /* escape */ E.mode = 0; break; + default: + editor_insert_char(c); } } } (DIR) diff --git a/row.c b/row.c t@@ -1,9 +1,6 @@ #include <stdlib.h> -#include <stdio.h> -#include <sys/types.h> #include <string.h> #include "byote.h" -#include "terminal.h" /* navigate over tab-representative string as one character */ int t@@ -69,3 +66,16 @@ editor_append_row(char *s, size_t len) ++E.num_rows; } + +/* insert character to row, making sure to allocate memory accordingly */ +void +editor_row_insert_char(eRow *row, int i, int c) +{ + if (i<0 || i>row->size) + i = row->size; + row->chars = realloc(row->chars, row->size + 2); + memmove(&row->chars[i+1], &row->chars[i], row->size - i+1); + row->size++; + row->chars[i] = c; + editor_update_row(row); +} (DIR) diff --git a/row.h b/row.h t@@ -5,5 +5,6 @@ int editor_row_cursor_x_to_rx(eRow *row, int cursor_x); void editor_append_row(char *s, size_t len); +void editor_row_insert_char(eRow *row, int i, int c); #endif