tAllow insert of newline - 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 882d0a17ff8cbbbd085bd4df669e4abc3bad0b82
 (DIR) parent 5a34030990e5e1ea2cd8c30403c22c85c4ea088c
 (HTM) Author: Anders Damsgaard <anders@adamsgaard.dk>
       Date:   Tue,  6 Aug 2019 20:20:13 +0200
       
       Allow insert of newline
       
       Diffstat:
         M edit.c                              |      26 ++++++++++++++++++++++++--
         M edit.h                              |       1 +
         M input.c                             |       4 +++-
         M io.c                                |       2 +-
         M row.c                               |      22 ++++++++++++----------
         M row.h                               |       3 ++-
       
       6 files changed, 43 insertions(+), 15 deletions(-)
       ---
 (DIR) diff --git a/edit.c b/edit.c
       t@@ -5,17 +5,39 @@ void
        editor_insert_char(int c)
        {
                if (E.cursor_y == E.num_rows)
       -                editor_append_row("", 0);
       +                editor_row_insert(E.num_rows, "", 0);
                editor_row_insert_char(&E.row[E.cursor_y], E.cursor_x, c);
                E.cursor_x++;
        }
        
        void
       +editor_insert_new_line()
       +{
       +        eRow *row;
       +        if (E.cursor_x == 0) {
       +                editor_row_insert(E.cursor_y, "", 0);
       +        } else {
       +                row = &E.row[E.cursor_y];
       +                editor_row_insert(E.cursor_y + 1, &row->chars[E.cursor_x],
       +                                  row->size - E.cursor_x);
       +                row = &E.row[E.cursor_y];
       +                row->size = E.cursor_x;
       +                row->chars[row->size] = '\0';
       +                editor_row_update(row);
       +        }
       +        E.cursor_y++;
       +        E.cursor_x = 0;
       +}
       +
       +/* delete a character before the cursor, and allow for deletion across rows */
       +void
        editor_delete_char_left()
        {
       +        eRow *row;
                if (E.cursor_y == E.num_rows || (E.cursor_x == 0 && E.cursor_y == 0))
                        return;
       -        eRow *row = &E.row[E.cursor_y];
       +        
       +        row = &E.row[E.cursor_y];
                if (E.cursor_x > 0) {
                        editor_row_delete_char(row, E.cursor_x - 1);
                        E.cursor_x--;
 (DIR) diff --git a/edit.h b/edit.h
       t@@ -2,6 +2,7 @@
        #define EDIT_H_
        
        void editor_insert_char(int c);
       +void editor_insert_new_line();
        void editor_delete_char_left();
        void editor_delete_char_right();
        
 (DIR) diff --git a/input.c b/input.c
       t@@ -5,6 +5,7 @@
        #include "edit.h"
        #include "output.h"
        #include "io.h"
       +#include "row.h"
        
        #define CTRL_KEY(k) ((k) & 0x1f)
        
       t@@ -155,11 +156,12 @@ editor_process_keypress()
                                        break;
                                
                                case CTRL_KEY('\r'): /* enter */
       -                                /* TODO */
       +                                editor_insert_new_line();
                                        break;
        
                                case 127:  /* backspace */
                                case CTRL_KEY('h'):
       +                                editor_move_cursor('h');
                                        editor_delete_char_left();
                                        break;
        
 (DIR) diff --git a/io.c b/io.c
       t@@ -36,7 +36,7 @@ file_open(char *filename)
                        while (linelen > 0 && (line[linelen - 1] == '\n' ||
                                               line[linelen - 1] == '\r'))
                                linelen--;
       -                editor_append_row(line, linelen);
       +                editor_row_insert(E.num_rows, line, linelen);
                }
                free(line);
                fclose(fp);
 (DIR) diff --git a/row.c b/row.c
       t@@ -18,7 +18,7 @@ editor_row_cursor_x_to_rx(eRow *row, int cursor_x)
        
        /* translate tabs before display */
        void
       -editor_update_row(eRow* row)
       +editor_row_update(eRow* row)
        {
                int j, idx, tabs;
        
       t@@ -49,12 +49,14 @@ editor_update_row(eRow* row)
        
        /* add row to buffer */
        void
       -editor_append_row(char *s, size_t len)
       +editor_row_insert(int i, char *s, size_t len)
        {
       -        int i;
       +        if (i<0 || i>E.num_rows)
       +                return;
        
                E.row = realloc(E.row, sizeof(eRow) * (E.num_rows + 1));
       -        i = E.num_rows;
       +        memmove(&E.row[i+1], &E.row[i], sizeof(eRow) * (E.num_rows - i));
       +
                E.row[i].size = len;
                E.row[i].chars = malloc(len + 1);
                memcpy(E.row[i].chars, s, len);
       t@@ -62,7 +64,7 @@ editor_append_row(char *s, size_t len)
        
                E.row[i].rsize = 0;
                E.row[i].rchars = NULL;
       -        editor_update_row(&E.row[i]);
       +        editor_row_update(&E.row[i]);
        
                ++E.num_rows;
                E.file_changed = 1;
       t@@ -78,7 +80,7 @@ editor_row_insert_char(eRow *row, int i, int c)
                memmove(&row->chars[i+1], &row->chars[i], row->size - i+1);
                row->size++;
                row->chars[i] = c;
       -        editor_update_row(row);
       +        editor_row_update(row);
                E.file_changed = 1;
        }
        
       t@@ -90,7 +92,7 @@ editor_row_append_string(eRow *row, char *s, size_t len)
                memcpy(&row->chars[row->size], s, len);
                row->size += len;
                row->chars[row->size] = '\0';
       -        editor_update_row(row);
       +        editor_row_update(row);
                E.file_changed = 1;
        }
        
       t@@ -101,7 +103,7 @@ editor_row_delete_char(eRow *row, int i)
                        return;
                memmove(&row->chars[i], &row->chars[i+1], row->size-i);
                row->size--;
       -        editor_update_row(row);
       +        editor_row_update(row);
                E.file_changed = 1;
        }
        
       t@@ -115,10 +117,10 @@ editor_row_free(eRow *row)
        void
        editor_row_delete(int i)
        {
       -        if (i<0 || i >= E.num_rows)
       +        if (i<0 || i>=E.num_rows)
                        return;
                editor_row_free(&E.row[i]);
       -        memmove(&E.row[i], &E.row[i+1], sizeof(eRow)*(E.num_rows-i-1));
       +        memmove(&E.row[i], &E.row[i+1], sizeof(eRow)*(E.num_rows - i - 1));
                E.num_rows--;
                E.file_changed = 1;
        }
 (DIR) diff --git a/row.h b/row.h
       t@@ -4,8 +4,9 @@
        #include "byote.h"
        
        int editor_row_cursor_x_to_rx(eRow *row, int cursor_x);
       -void editor_append_row(char *s, size_t len);
       +void editor_row_update(eRow* row);
        void editor_row_append_string(eRow *row, char *s, size_t len);
       +void editor_row_insert(int i, char *s, size_t len);
        void editor_row_insert_char(eRow *row, int i, int c);
        void editor_row_delete_char(eRow *row, int i);
        void editor_row_delete(int i);