hopefully fixing the rendering bugs

This commit is contained in:
Kyle Isom 2020-02-12 06:30:34 -08:00
parent fdd550c278
commit 50200a3303
3 changed files with 60 additions and 3 deletions

View File

@ -1,4 +1,5 @@
[ ] goto-line
[ ] text-corruption bug
[ ] alt-modifiers
[ ] refresh-screen
[ ] functions -> keymapping?

View File

@ -1,4 +1,5 @@
#include <assert.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@ -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];
}

View File

@ -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);