From 50200a3303d295e4588fc8a5af4a3938fbcd4a32 Mon Sep 17 00:00:00 2001 From: Kyle Isom Date: Wed, 12 Feb 2020 06:30:34 -0800 Subject: [PATCH] hopefully fixing the rendering bugs --- ke/TODO | 3 ++- ke/erow.c | 34 +++++++++++++++++++++++++++++++++- ke/main.c | 26 +++++++++++++++++++++++++- 3 files changed, 60 insertions(+), 3 deletions(-) diff --git a/ke/TODO b/ke/TODO index 3749547..5f1431b 100644 --- a/ke/TODO +++ b/ke/TODO @@ -1,4 +1,5 @@ [ ] goto-line [ ] text-corruption bug [ ] alt-modifiers - +[ ] refresh-screen +[ ] functions -> keymapping? diff --git a/ke/erow.c b/ke/erow.c index fddf82c..45d1140 100644 --- a/ke/erow.c +++ b/ke/erow.c @@ -1,4 +1,5 @@ #include +#include #include #include #include @@ -7,6 +8,30 @@ #include "defs.h" +static char +byte_to_hihex(char c) +{ + c = (c >> 4) & 0xf; + + if (c < 10) { + return c + 0x30; + } + return c + 0x41; +} + + +static char +byte_to_lohex(char c) +{ + c &= 0x0f; + + if (c < 10) { + return c + 0x30; + } + return c + 0x41; +} + + int erow_render_to_cursor(struct erow *row, int cx) { @@ -50,6 +75,7 @@ erow_update(struct erow *row) { int i = 0, j; int tabs = 0; + int ctrl = 0; /* * TODO(kyle): I'm not thrilled with this double-render. @@ -57,12 +83,14 @@ erow_update(struct erow *row) for (j = 0; j < row->size; j++) { if (row->line[j] == '\t') { tabs++; + } else if (!isprint(row->line[j])) { + ctrl++; } } free(row->render); row->render = NULL; - row->render = malloc(row->size + (tabs * (TAB_STOP-1)) + 1); + row->render = malloc(row->size + (tabs * (TAB_STOP-1)) + (ctrl * 3) + 1); assert(row->render != NULL); for (j = 0; j < row->size; j++) { @@ -70,6 +98,10 @@ erow_update(struct erow *row) do { row->render[i++] = ' '; } while ((i % TAB_STOP) != 0); + } else if (!isprint(row->line[j])) { + row->render[i++] = '\\'; + row->render[i++] = byte_to_hihex(row->line[j]); + row->render[i++] = byte_to_lohex(row->line[j]); } else { row->render[i++] = row->line[j]; } diff --git a/ke/main.c b/ke/main.c index 8fbaab0..5b1f1e8 100644 --- a/ke/main.c +++ b/ke/main.c @@ -102,6 +102,27 @@ get_winsz(int *rows, int *cols) } +void +goto_line() +{ + int lineno = 0; + char *query = editor_prompt("Line: %s", NULL); + + if (query == NULL) { + return; + } + + lineno = atoi(query); + if (lineno < 1 || lineno >= editor->nrows) { + editor_set_status("Line number must be between 1 and %d.", editor->nrows); + return; + } + + editor->cury = lineno; + editor->rowoffs = editor->nrows; +} + + void delete_row(int at) { @@ -175,7 +196,7 @@ insertch(int16_t c) erow_insert(editor->nrows, "", 0); } - row_insert_ch(&editor->row[editor->cury], editor->curx, c); + row_insert_ch(&editor->row[editor->cury], editor->curx, (char)(c & 0xff)); editor->curx++; editor->dirty++; } @@ -691,6 +712,9 @@ process_kcommand(int16_t c) process_normal(DEL_KEY); } break; + case CTRL_KEY('g'): + goto_line(); + break; case BACKSPACE: while (editor->curx > 0) { process_normal(BACKSPACE);