2020-02-12 00:38:06 +00:00
|
|
|
#include <assert.h>
|
2020-02-12 14:30:34 +00:00
|
|
|
#include <ctype.h>
|
2020-02-12 01:16:31 +00:00
|
|
|
#include <stdio.h>
|
2020-02-12 00:38:06 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
|
|
|
|
#include "defs.h"
|
|
|
|
|
|
|
|
|
2020-02-12 14:30:34 +00:00
|
|
|
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)
|
|
|
|
{
|
2020-02-12 14:46:35 +00:00
|
|
|
c = c & 0x0f;
|
2020-02-12 14:30:34 +00:00
|
|
|
|
|
|
|
if (c < 10) {
|
|
|
|
return c + 0x30;
|
|
|
|
}
|
2020-02-12 14:46:35 +00:00
|
|
|
return c + 0x37;
|
2020-02-12 14:30:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-02-12 00:38:06 +00:00
|
|
|
int
|
|
|
|
erow_render_to_cursor(struct erow *row, int cx)
|
|
|
|
{
|
|
|
|
int rx = 0;
|
|
|
|
int j;
|
|
|
|
|
|
|
|
for (j = 0; j < cx; j++) {
|
|
|
|
if (row->line[j] == '\t') {
|
|
|
|
rx += (TAB_STOP-1) - (rx%TAB_STOP);
|
2020-02-12 14:46:35 +00:00
|
|
|
} else if (row->line[j] < 0x20) {
|
|
|
|
rx += 2;
|
2020-02-12 00:38:06 +00:00
|
|
|
}
|
|
|
|
rx++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return rx;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
erow_cursor_to_render(struct erow *row, int rx)
|
|
|
|
{
|
|
|
|
int cur_rx = 0;
|
|
|
|
int curx = 0;
|
|
|
|
|
|
|
|
for (curx = 0; curx < row->size; curx++) {
|
|
|
|
if (row->line[curx] == '\t') {
|
|
|
|
cur_rx += (TAB_STOP - 1) - (cur_rx % TAB_STOP);
|
2020-02-12 14:46:35 +00:00
|
|
|
} else if (row->line[curx] < 0x20) {
|
|
|
|
cur_rx += 2;
|
2020-02-12 00:38:06 +00:00
|
|
|
}
|
|
|
|
cur_rx++;
|
|
|
|
|
|
|
|
if (cur_rx > rx) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return curx;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
erow_update(struct erow *row)
|
|
|
|
{
|
|
|
|
int i = 0, j;
|
|
|
|
int tabs = 0;
|
2020-02-12 14:30:34 +00:00
|
|
|
int ctrl = 0;
|
2020-02-12 00:38:06 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* TODO(kyle): I'm not thrilled with this double-render.
|
|
|
|
*/
|
|
|
|
for (j = 0; j < row->size; j++) {
|
|
|
|
if (row->line[j] == '\t') {
|
|
|
|
tabs++;
|
2020-02-12 14:30:34 +00:00
|
|
|
} else if (!isprint(row->line[j])) {
|
|
|
|
ctrl++;
|
2020-02-12 00:38:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-12 22:28:34 +00:00
|
|
|
if (row->rsize) {
|
|
|
|
free(row->render);
|
|
|
|
}
|
2020-02-12 00:38:06 +00:00
|
|
|
row->render = NULL;
|
2020-02-12 14:30:34 +00:00
|
|
|
row->render = malloc(row->size + (tabs * (TAB_STOP-1)) + (ctrl * 3) + 1);
|
2020-02-12 00:38:06 +00:00
|
|
|
assert(row->render != NULL);
|
|
|
|
|
|
|
|
for (j = 0; j < row->size; j++) {
|
|
|
|
if (row->line[j] == '\t') {
|
|
|
|
do {
|
|
|
|
row->render[i++] = ' ';
|
|
|
|
} while ((i % TAB_STOP) != 0);
|
2020-02-12 14:30:34 +00:00
|
|
|
} 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]);
|
2020-02-12 00:38:06 +00:00
|
|
|
} else {
|
|
|
|
row->render[i++] = row->line[j];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
row->render[i] = '\0';
|
|
|
|
row->rsize = i;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
erow_insert(int at, char *s, int len)
|
|
|
|
{
|
2020-02-12 01:16:31 +00:00
|
|
|
if (at < 0 || at > editor->nrows) {
|
2020-02-12 00:38:06 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-02-12 01:16:31 +00:00
|
|
|
editor->row = realloc(editor->row, sizeof(struct erow) * (editor->nrows + 1));
|
|
|
|
assert(editor->row != NULL);
|
2020-02-12 00:38:06 +00:00
|
|
|
|
2020-02-12 01:16:31 +00:00
|
|
|
if (at < editor->nrows) {
|
2020-02-12 06:47:57 +00:00
|
|
|
printf("%d, %d\n", at, editor->nrows);
|
2020-02-12 01:16:31 +00:00
|
|
|
memmove(&editor->row[at+1], &editor->row[at],
|
|
|
|
sizeof(struct erow) * (editor->nrows - at + 1));
|
2020-02-12 00:38:06 +00:00
|
|
|
}
|
|
|
|
|
2020-02-12 01:16:31 +00:00
|
|
|
editor->row[at].size = len;
|
|
|
|
editor->row[at].line = malloc(len+1);
|
|
|
|
memcpy(editor->row[at].line, s, len);
|
|
|
|
editor->row[at].line[len] = '\0';
|
2020-02-12 00:38:06 +00:00
|
|
|
|
2020-02-12 01:16:31 +00:00
|
|
|
editor->row[at].rsize = 0;
|
|
|
|
editor->row[at].render = NULL;
|
2020-02-12 00:38:06 +00:00
|
|
|
|
2020-02-12 01:16:31 +00:00
|
|
|
erow_update(&editor->row[at]);
|
|
|
|
editor->nrows++;
|
2020-02-12 00:38:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
erow_free(struct erow *row)
|
|
|
|
{
|
|
|
|
free(row->render);
|
|
|
|
free(row->line);
|
|
|
|
row->render = NULL;
|
|
|
|
row->line = NULL;
|
|
|
|
}
|