ke was actually all kinds of fuggled

This commit is contained in:
Kyle Isom 2020-02-11 17:16:31 -08:00
parent 2729a02ecd
commit d7c4c0800d
4 changed files with 175 additions and 170 deletions

View File

@ -10,7 +10,7 @@ all: build
.PHONY: build .PHONY: build
build: $(BIN) build: $(BIN)
$(BIN): $(OBJS) $(BIN): $(OBJS) defs.h
$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $(OBJS) $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $(OBJS)
.PHONY: clean .PHONY: clean

View File

@ -31,7 +31,7 @@ struct erow {
}; };
static struct { struct editor_t {
struct termios entry_term; struct termios entry_term;
int rows, cols; int rows, cols;
int curx, cury; int curx, cury;
@ -45,7 +45,10 @@ static struct {
int dirtyex; int dirtyex;
char msg[80]; char msg[80];
time_t msgtm; time_t msgtm;
} editor; } _editor;
static struct editor_t *editor = &_editor;
/* /*

View File

@ -1,4 +1,5 @@
#include <assert.h> #include <assert.h>
#include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@ -82,28 +83,28 @@ erow_update(struct erow *row)
void void
erow_insert(int at, char *s, int len) erow_insert(int at, char *s, int len)
{ {
if (at < 0 || at > editor.nrows) { if (at < 0 || at > editor->nrows) {
return; return;
} }
editor.row = realloc(editor.row, sizeof(struct erow) * (editor.nrows + 1)); editor->row = realloc(editor->row, sizeof(struct erow) * (editor->nrows + 1));
assert(editor.row != NULL); assert(editor->row != NULL);
if (at < editor.nrows) { if (at < editor->nrows) {
memmove(&editor.row[at+1], &editor.row[at], memmove(&editor->row[at+1], &editor->row[at],
sizeof(struct erow) * (editor.nrows - at + 1)); sizeof(struct erow) * (editor->nrows - at + 1));
} }
editor.row[at].size = len; editor->row[at].size = len;
editor.row[at].line = malloc(len+1); editor->row[at].line = malloc(len+1);
memcpy(editor.row[at].line, s, len); memcpy(editor->row[at].line, s, len);
editor.row[at].line[len] = '\0'; editor->row[at].line[len] = '\0';
editor.row[at].rsize = 0; editor->row[at].rsize = 0;
editor.row[at].render = NULL; editor->row[at].render = NULL;
erow_update(&editor.row[at]); erow_update(&editor->row[at]);
editor.nrows++; editor->nrows++;
} }

307
ke/main.c
View File

@ -103,15 +103,15 @@ get_winsz(int *rows, int *cols)
void void
delete_row(int at) delete_row(int at)
{ {
if (at < 0 || at >= editor.nrows) { if (at < 0 || at >= editor->nrows) {
return; return;
} }
erow_free(&editor.row[at]); erow_free(&editor->row[at]);
memmove(&editor.row[at], &editor.row[at+1], memmove(&editor->row[at], &editor->row[at+1],
sizeof(struct erow) * (editor.nrows - at - 1)); sizeof(struct erow) * (editor->nrows - at - 1));
editor.nrows--; editor->nrows--;
editor.dirty++; editor->dirty++;
} }
@ -124,7 +124,7 @@ row_append_row(struct erow *row, char *s, int len)
row->size += len; row->size += len;
row->line[row->size] = '\0'; row->line[row->size] = '\0';
erow_update(row); erow_update(row);
editor.dirty++; editor->dirty++;
} }
@ -157,7 +157,7 @@ row_delete_ch(struct erow *row, int at)
memmove(&row->line[at], &row->line[at+1], row->size+1); memmove(&row->line[at], &row->line[at+1], row->size+1);
row->size--; row->size--;
erow_update(row); erow_update(row);
editor.dirty++; editor->dirty++;
} }
@ -169,13 +169,13 @@ insertch(int16_t c)
* a row; it can just figure out where the cursor is * a row; it can just figure out where the cursor is
* at and what to do. * at and what to do.
*/ */
if (editor.cury == editor.nrows) { if (editor->cury == editor->nrows) {
erow_insert(editor.nrows, "", 0); erow_insert(editor->nrows, "", 0);
} }
row_insert_ch(&editor.row[editor.cury], editor.curx, c); row_insert_ch(&editor->row[editor->cury], editor->curx, c);
editor.curx++; editor->curx++;
editor.dirty++; editor->dirty++;
} }
@ -184,25 +184,25 @@ deletech()
{ {
struct erow *row = NULL; struct erow *row = NULL;
if (editor.cury == editor.nrows) { if (editor->cury == editor->nrows) {
return; return;
} }
if (editor.cury == 0 && editor.curx == 0) { if (editor->cury == 0 && editor->curx == 0) {
return; return;
} }
row = &editor.row[editor.cury]; row = &editor->row[editor->cury];
if (editor.curx > 0) { if (editor->curx > 0) {
row_delete_ch(row, editor.curx - 1); row_delete_ch(row, editor->curx - 1);
editor.curx--; editor->curx--;
} else { } else {
editor.curx = editor.row[editor.cury - 1].size; editor->curx = editor->row[editor->cury - 1].size;
row_append_row(&editor.row[editor.cury - 1], row->line, row_append_row(&editor->row[editor->cury - 1], row->line,
row->size); row->size);
delete_row(editor.cury); delete_row(editor->cury);
editor.cury--; editor->cury--;
} }
} }
@ -215,13 +215,14 @@ open_file(const char *filename)
ssize_t linelen; ssize_t linelen;
FILE *fp = NULL; FILE *fp = NULL;
free(editor.filename); free(editor->filename);
editor.filename = strdup(filename); editor->filename = strdup(filename);
assert(editor.filename != NULL); assert(editor->filename != NULL);
editor.dirty = 0; editor->dirty = 0;
if ((fp = fopen(filename, "r")) == NULL) { if ((fp = fopen(filename, "r")) == NULL) {
if (errno == ENOENT) { if (errno == ENOENT) {
printf("no such file");
return; return;
} }
die("fopen"); die("fopen");
@ -234,7 +235,7 @@ open_file(const char *filename)
linelen--; linelen--;
} }
erow_insert(editor.nrows, line, linelen); erow_insert(editor->nrows, line, linelen);
} }
} }
@ -255,9 +256,9 @@ rows_to_buffer(int *buflen)
char *buf = NULL; char *buf = NULL;
char *p = NULL; char *p = NULL;
for (j = 0; j < editor.nrows; j++) { for (j = 0; j < editor->nrows; j++) {
/* extra byte for newline */ /* extra byte for newline */
len += editor.row[j].size+1; len += editor->row[j].size+1;
} }
if (len == 0) { if (len == 0) {
@ -269,9 +270,9 @@ rows_to_buffer(int *buflen)
assert(buf != NULL); assert(buf != NULL);
p = buf; p = buf;
for (j = 0; j < editor.nrows; j++) { for (j = 0; j < editor->nrows; j++) {
memcpy(p, editor.row[j].line, editor.row[j].size); memcpy(p, editor->row[j].line, editor->row[j].size);
p += editor.row[j].size; p += editor->row[j].size;
*p++ = '\n'; *p++ = '\n';
} }
@ -287,16 +288,16 @@ save_file()
int status = 1; /* will be used as exit code */ int status = 1; /* will be used as exit code */
char *buf; char *buf;
if (editor.filename == NULL) { if (editor->filename == NULL) {
editor.filename = editor_prompt("Filename: %s", NULL); editor->filename = editor_prompt("Filename: %s", NULL);
if (editor.filename == NULL) { if (editor->filename == NULL) {
editor_set_status("Save aborted."); editor_set_status("Save aborted.");
return 0; return 0;
} }
} }
buf = rows_to_buffer(&len); buf = rows_to_buffer(&len);
if ((fd = open(editor.filename, O_RDWR|O_CREAT, 0644)) == -1) { if ((fd = open(editor->filename, O_RDWR|O_CREAT, 0644)) == -1) {
goto save_exit; goto save_exit;
} }
@ -321,12 +322,12 @@ save_exit:
if (status != 0) { if (status != 0) {
buf = strerror(errno); buf = strerror(errno);
editor_set_status("Error writing %s: %s", editor.filename, editor_set_status("Error writing %s: %s", editor->filename,
buf); buf);
} else { } else {
editor_set_status("Wrote %d bytes to %s.", len, editor_set_status("Wrote %d bytes to %s.", len,
editor.filename); editor->filename);
editor.dirty = 0; editor->dirty = 0;
} }
return status; return status;
@ -494,26 +495,26 @@ editor_find_callback(char *query, int16_t c)
} }
current = lmatch; current = lmatch;
for (i = 0; i < editor.nrows; i++) { for (i = 0; i < editor->nrows; i++) {
current += dir; current += dir;
if (current == -1) { if (current == -1) {
current = editor.nrows - 1; current = editor->nrows - 1;
} else if (current == editor.nrows) { } else if (current == editor->nrows) {
current = 0; current = 0;
} }
row = &editor.row[current]; row = &editor->row[current];
match = strstr(row->render, query); match = strstr(row->render, query);
if (match) { if (match) {
lmatch = current; lmatch = current;
editor.cury = current; editor->cury = current;
editor.curx = erow_render_to_cursor(row, editor->curx = erow_render_to_cursor(row,
match - row->render); match - row->render);
/* /*
* after this, scroll will put the match line at * after this, scroll will put the match line at
* the top of the screen. * the top of the screen.
*/ */
editor.rowoffs = editor.nrows; editor->rowoffs = editor->nrows;
break; break;
} }
} }
@ -526,20 +527,20 @@ void
editor_find() editor_find()
{ {
char *query; char *query;
int scx = editor.curx; int scx = editor->curx;
int scy = editor.cury; int scy = editor->cury;
int sco = editor.coloffs; int sco = editor->coloffs;
int sro = editor.rowoffs; int sro = editor->rowoffs;
query = editor_prompt("Search (ESC to cancel): %s", query = editor_prompt("Search (ESC to cancel): %s",
editor_find_callback); editor_find_callback);
if (query) { if (query) {
free(query); free(query);
} else { } else {
editor.curx = scx; editor->curx = scx;
editor.cury = scy; editor->cury = scy;
editor.coloffs = sco; editor->coloffs = sco;
editor.rowoffs = sro; editor->rowoffs = sro;
} }
display_refresh(); display_refresh();
@ -552,51 +553,51 @@ move_cursor(int16_t c)
struct erow *row; struct erow *row;
int reps; int reps;
row = (editor.cury >= editor.nrows) ? NULL : &editor.row[editor.cury]; row = (editor->cury >= editor->nrows) ? NULL : &editor->row[editor->cury];
switch (c) { switch (c) {
case ARROW_UP: case ARROW_UP:
case CTRL_KEY('p'): case CTRL_KEY('p'):
if (editor.cury > 0) { if (editor->cury > 0) {
editor.cury--; editor->cury--;
} }
break; break;
case ARROW_DOWN: case ARROW_DOWN:
case CTRL_KEY('n'): case CTRL_KEY('n'):
if (editor.cury < editor.nrows) { if (editor->cury < editor->nrows) {
editor.cury++; editor->cury++;
} }
break; break;
case ARROW_RIGHT: case ARROW_RIGHT:
case CTRL_KEY('f'): case CTRL_KEY('f'):
if (row && editor.curx < row->size) { if (row && editor->curx < row->size) {
editor.curx++; editor->curx++;
} else if (row && editor.curx == row->size) { } else if (row && editor->curx == row->size) {
editor.cury++; editor->cury++;
editor.curx = 0; editor->curx = 0;
} }
break; break;
case ARROW_LEFT: case ARROW_LEFT:
case CTRL_KEY('b'): case CTRL_KEY('b'):
if (editor.curx > 0) { if (editor->curx > 0) {
editor.curx--; editor->curx--;
} else if (editor.cury > 0) { } else if (editor->cury > 0) {
editor.cury--; editor->cury--;
editor.curx = editor.row[editor.cury].size; editor->curx = editor->row[editor->cury].size;
} }
break; break;
case PG_UP: case PG_UP:
case PG_DN: { case PG_DN: {
if (c == PG_UP) { if (c == PG_UP) {
editor.cury = editor.rowoffs; editor->cury = editor->rowoffs;
} else if (c == PG_DN) { } else if (c == PG_DN) {
editor.cury = editor.rowoffs + editor.rows-1; editor->cury = editor->rowoffs + editor->rows-1;
if (editor.cury > editor.nrows) { if (editor->cury > editor->nrows) {
editor.cury = editor.nrows; editor->cury = editor->nrows;
} }
} }
reps = editor.rows; reps = editor->rows;
while (--reps) { while (--reps) {
move_cursor(c == PG_UP ? ARROW_UP : ARROW_DOWN); move_cursor(c == PG_UP ? ARROW_UP : ARROW_DOWN);
} }
@ -604,22 +605,22 @@ move_cursor(int16_t c)
case HOME_KEY: case HOME_KEY:
case CTRL_KEY('a'): case CTRL_KEY('a'):
editor.curx = 0; editor->curx = 0;
break; break;
case END_KEY: case END_KEY:
case CTRL_KEY('e'): case CTRL_KEY('e'):
editor.curx = editor.row[editor.cury].size; editor->curx = editor->row[editor->cury].size;
break; break;
default: default:
break; break;
} }
row = (editor.cury >= editor.nrows) ? row = (editor->cury >= editor->nrows) ?
NULL : &editor.row[editor.cury]; NULL : &editor->row[editor->cury];
reps = row ? row->size : 0; reps = row ? row->size : 0;
if (editor.curx > reps) { if (editor->curx > reps) {
editor.curx = reps; editor->curx = reps;
} }
} }
@ -629,20 +630,20 @@ newline()
{ {
struct erow *row = NULL; struct erow *row = NULL;
if (editor.curx == 0) { if (editor->curx == 0) {
erow_insert(editor.cury, "", 0); erow_insert(editor->cury, "", 0);
} else { } else {
row = &editor.row[editor.cury]; row = &editor->row[editor->cury];
erow_insert(editor.cury + 1, &row->line[editor.curx], erow_insert(editor->cury + 1, &row->line[editor->curx],
row->size - editor.curx); row->size - editor->curx);
row = &editor.row[editor.cury]; row = &editor->row[editor->cury];
row->size = editor.curx; row->size = editor->curx;
row->line[row->size] = '\0'; row->line[row->size] = '\0';
erow_update(row); erow_update(row);
} }
editor.cury++; editor->cury++;
editor.curx = 0; editor->curx = 0;
} }
@ -652,9 +653,9 @@ process_kcommand(int16_t c)
switch (c) { switch (c) {
case 'q': case 'q':
case CTRL_KEY('q'): case CTRL_KEY('q'):
if (editor.dirty && editor.dirtyex) { if (editor->dirty && editor->dirtyex) {
editor_set_status("File not saved - C-k q again to quit."); editor_set_status("File not saved - C-k q again to quit.");
editor.dirtyex = 0; editor->dirtyex = 0;
return; return;
} }
exit(0); exit(0);
@ -682,7 +683,7 @@ process_kcommand(int16_t c)
break; break;
} }
editor.dirtyex = 1; editor->dirtyex = 1;
return; return;
} }
@ -699,7 +700,7 @@ process_normal(int16_t c)
newline(); newline();
break; break;
case CTRL_KEY('k'): case CTRL_KEY('k'):
editor.mode = MODE_KCOMMAND; editor->mode = MODE_KCOMMAND;
return; return;
case BACKSPACE: case BACKSPACE:
case CTRL_KEY('h'): case CTRL_KEY('h'):
@ -723,7 +724,7 @@ process_normal(int16_t c)
break; break;
} }
editor.dirtyex = 1; editor->dirtyex = 1;
} }
@ -738,10 +739,10 @@ process_keypress()
return 0; return 0;
} }
switch (editor.mode) { switch (editor->mode) {
case MODE_KCOMMAND: case MODE_KCOMMAND:
process_kcommand(c); process_kcommand(c);
editor.mode = MODE_NORMAL; editor->mode = MODE_NORMAL;
break; break;
case MODE_NORMAL: case MODE_NORMAL:
process_normal(c); process_normal(c);
@ -812,7 +813,7 @@ disable_termraw()
{ {
display_clear(NULL); display_clear(NULL);
if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &editor.entry_term) == -1) { if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &editor->entry_term) == -1) {
die("couldn't disable terminal raw mode"); die("couldn't disable terminal raw mode");
} }
} }
@ -821,7 +822,7 @@ disable_termraw()
void void
setup_terminal() setup_terminal()
{ {
if (tcgetattr(STDIN_FILENO, &editor.entry_term) == -1) { if (tcgetattr(STDIN_FILENO, &editor->entry_term) == -1) {
die("can't snapshot terminal settings"); die("can't snapshot terminal settings");
} }
atexit(disable_termraw); atexit(disable_termraw);
@ -832,19 +833,19 @@ setup_terminal()
void void
draw_rows(struct abuf *ab) draw_rows(struct abuf *ab)
{ {
assert(editor.cols >= 0); assert(editor->cols >= 0);
char buf[editor.cols]; char buf[editor->cols];
int buflen, filerow, padding; int buflen, filerow, padding;
int y; int y;
for (y = 0; y < editor.rows; y++) { for (y = 0; y < editor->rows; y++) {
filerow = y + editor.rowoffs; filerow = y + editor->rowoffs;
if (filerow >= editor.nrows) { if (filerow >= editor->nrows) {
if ((editor.nrows == 0) && (y == editor.rows / 3)) { if ((editor->nrows == 0) && (y == editor->rows / 3)) {
buflen = snprintf(buf, sizeof(buf), buflen = snprintf(buf, sizeof(buf),
"ke k%s", KE_VERSION); "ke k%s", KE_VERSION);
padding = (editor.rows - buflen) / 2; padding = (editor->rows - buflen) / 2;
if (padding) { if (padding) {
ab_append(ab, "|", 1); ab_append(ab, "|", 1);
@ -857,15 +858,15 @@ draw_rows(struct abuf *ab)
ab_append(ab, "|", 1); ab_append(ab, "|", 1);
} }
} else { } else {
buflen = editor.row[filerow].rsize - editor.coloffs; buflen = editor->row[filerow].rsize - editor->coloffs;
if (buflen < 0) { if (buflen < 0) {
buflen = 0; buflen = 0;
} }
if (buflen > editor.cols) { if (buflen > editor->cols) {
buflen = editor.cols; buflen = editor->cols;
} }
ab_append(ab, editor.row[filerow].render+editor.coloffs, ab_append(ab, editor->row[filerow].render+editor->coloffs,
buflen); buflen);
} }
ab_append(ab, ESCSEQ "K", 3); ab_append(ab, ESCSEQ "K", 3);
@ -877,21 +878,21 @@ draw_rows(struct abuf *ab)
void void
draw_status_bar(struct abuf *ab) draw_status_bar(struct abuf *ab)
{ {
char status[editor.cols]; char status[editor->cols];
char rstatus[editor.cols]; char rstatus[editor->cols];
int len, rlen; int len, rlen;
len = snprintf(status, sizeof(status), "%c%cke: %.20s - %d lines", len = snprintf(status, sizeof(status), "%c%cke: %.20s - %d lines",
editor.dirty ? '!' : '-', editor.dirty ? '!' : '-', editor->dirty ? '!' : '-', editor->dirty ? '!' : '-',
editor.filename ? editor.filename : "[no file]", editor->filename ? editor->filename : "[no file]",
editor.nrows); editor->nrows);
rlen = snprintf(rstatus, sizeof(rstatus), "L%d/%d C%d", rlen = snprintf(rstatus, sizeof(rstatus), "L%d/%d C%d",
editor.cury+1, editor.nrows, editor.curx+1); editor->cury+1, editor->nrows, editor->curx+1);
ab_append(ab, ESCSEQ "7m", 4); ab_append(ab, ESCSEQ "7m", 4);
ab_append(ab, status, len); ab_append(ab, status, len);
while (len < editor.cols) { while (len < editor->cols) {
if ((editor.cols - len) == rlen) { if ((editor->cols - len) == rlen) {
ab_append(ab, rstatus, rlen); ab_append(ab, rstatus, rlen);
break; break;
} }
@ -906,15 +907,15 @@ draw_status_bar(struct abuf *ab)
void void
draw_message_line(struct abuf *ab) draw_message_line(struct abuf *ab)
{ {
int len = strlen(editor.msg); int len = strlen(editor->msg);
ab_append(ab, ESCSEQ "K", 3); ab_append(ab, ESCSEQ "K", 3);
if (len > editor.cols) { if (len > editor->cols) {
len = editor.cols; len = editor->cols;
} }
if (len && ((time(NULL) - editor.msgtm) < MSG_TIMEO)) { if (len && ((time(NULL) - editor->msgtm) < MSG_TIMEO)) {
ab_append(ab, editor.msg, len); ab_append(ab, editor->msg, len);
} }
} }
@ -922,27 +923,27 @@ draw_message_line(struct abuf *ab)
void void
scroll() scroll()
{ {
editor.rx = 0; editor->rx = 0;
if (editor.cury < editor.nrows) { if (editor->cury < editor->nrows) {
editor.rx = erow_render_to_cursor( editor->rx = erow_render_to_cursor(
&editor.row[editor.cury], &editor->row[editor->cury],
editor.curx); editor->curx);
} }
if (editor.cury < editor.rowoffs) { if (editor->cury < editor->rowoffs) {
editor.rowoffs = editor.cury; editor->rowoffs = editor->cury;
} }
if (editor.cury >= editor.rowoffs + editor.rows) { if (editor->cury >= editor->rowoffs + editor->rows) {
editor.rowoffs = editor.cury - editor.rows + 1; editor->rowoffs = editor->cury - editor->rows + 1;
} }
if (editor.rx < editor.coloffs) { if (editor->rx < editor->coloffs) {
editor.coloffs = editor.rx; editor->coloffs = editor->rx;
} }
if (editor.rx >= editor.coloffs + editor.cols) { if (editor->rx >= editor->coloffs + editor->cols) {
editor.coloffs = editor.rx - editor.cols + 1; editor->coloffs = editor->rx - editor->cols + 1;
} }
} }
@ -963,8 +964,8 @@ display_refresh()
draw_message_line(&ab); draw_message_line(&ab);
snprintf(buf, sizeof(buf), ESCSEQ "%d;%dH", snprintf(buf, sizeof(buf), ESCSEQ "%d;%dH",
(editor.cury-editor.rowoffs)+1, (editor->cury-editor->rowoffs)+1,
(editor.rx-editor.coloffs)+1); (editor->rx-editor->coloffs)+1);
ab_append(&ab, buf, strnlen(buf, 32)); ab_append(&ab, buf, strnlen(buf, 32));
/* ab_append(&ab, ESCSEQ "1;2H", 7); */ /* ab_append(&ab, ESCSEQ "1;2H", 7); */
ab_append(&ab, ESCSEQ "?25h", 6); ab_append(&ab, ESCSEQ "?25h", 6);
@ -980,10 +981,10 @@ editor_set_status(const char *fmt, ...)
va_list ap; va_list ap;
va_start(ap, fmt); va_start(ap, fmt);
vsnprintf(editor.msg, sizeof(editor.msg), fmt, ap); vsnprintf(editor->msg, sizeof(editor->msg), fmt, ap);
va_end(ap); va_end(ap);
editor.msgtm = time(NULL); editor->msgtm = time(NULL);
} }
@ -1005,26 +1006,26 @@ loop()
void void
init_editor() init_editor()
{ {
editor.cols = 0; editor->cols = 0;
editor.rows = 0; editor->rows = 0;
if (get_winsz(&editor.rows, &editor.cols) == -1) { if (get_winsz(&editor->rows, &editor->cols) == -1) {
die("can't get window size"); die("can't get window size");
} }
editor.rows--; /* status bar */ editor->rows--; /* status bar */
editor.rows--; /* message line */ editor->rows--; /* message line */
editor.curx = editor.cury = 0; editor->curx = editor->cury = 0;
editor.rx = 0; editor->rx = 0;
editor.nrows = 0; editor->nrows = 0;
editor.rowoffs = editor.coloffs = 0; editor->rowoffs = editor->coloffs = 0;
editor.row = NULL; editor->row = NULL;
editor.msg[0] = '\0'; editor->msg[0] = '\0';
editor.msgtm = 0; editor->msgtm = 0;
editor.dirty = 0; editor->dirty = 0;
} }