editor updates

This commit is contained in:
Kyle Isom 2020-02-13 00:28:34 +02:00
parent a72621c77f
commit c584777662
8 changed files with 69 additions and 21 deletions

View File

@ -2,10 +2,10 @@ BIN := ke
OBJS := abuf.o erow.o main.o OBJS := abuf.o erow.o main.o
LDFLAGS := LDFLAGS :=
CFLAGS := -pedantic -Wall -Werror -Wextra -O0 -std=c99 -g CFLAGS := -pedantic -Wall -Werror -Wextra -O2 -std=c99 -g -fno-builtin-memmove
.PHONY: all .PHONY: all
all: build all: build install
.PHONY: build .PHONY: build
build: $(BIN) build: $(BIN)
@ -27,6 +27,6 @@ keypress: keypress.c
.PHONY: install .PHONY: install
install: $(BIN) install: $(BIN)
cp $(BIN) $(HOME)/bin/ install -C $(BIN) $(HOME)/bin/
%.o: %.c %.o: %.c

View File

@ -92,7 +92,9 @@ erow_update(struct erow *row)
} }
} }
free(row->render); if (row->rsize) {
free(row->render);
}
row->render = NULL; row->render = NULL;
row->render = malloc(row->size + (tabs * (TAB_STOP-1)) + (ctrl * 3) + 1); row->render = malloc(row->size + (tabs * (TAB_STOP-1)) + (ctrl * 3) + 1);
assert(row->render != NULL); assert(row->render != NULL);

View File

@ -21,7 +21,7 @@
#include "defs.h" #include "defs.h"
#define KE_VERSION "0.9.0" #define KE_VERSION "0.9.1"
#define ESCSEQ "\x1b[" #define ESCSEQ "\x1b["
#define CTRL_KEY(key) ((key)&0x1f) #define CTRL_KEY(key) ((key)&0x1f)
#define TAB_STOP 8 #define TAB_STOP 8
@ -118,8 +118,8 @@ goto_line()
return; return;
} }
editor->cury = lineno; editor->cury = lineno - 1;
editor->rowoffs = editor->nrows; editor->rowoffs = editor->cury - (editor->rows / 2);
} }
@ -207,7 +207,7 @@ deletech()
{ {
struct erow *row = NULL; struct erow *row = NULL;
if (editor->cury == editor->nrows) { if (editor->cury >= editor->nrows) {
return; return;
} }
@ -245,7 +245,7 @@ open_file(const char *filename)
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"); editor_set_status("[new file]");
return; return;
} }
die("fopen"); die("fopen");
@ -311,6 +311,11 @@ 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->dirty) {
editor_set_status("No changes to save.");
return 0;
}
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) {
@ -341,7 +346,10 @@ save_file()
save_exit: save_exit:
if (fd) close(fd); if (fd) close(fd);
if (buf) free(buf); if (buf) {
free(buf);
buf = NULL;
}
if (status != 0) { if (status != 0) {
buf = strerror(errno); buf = strerror(errno);
@ -650,6 +658,9 @@ move_cursor(int16_t c)
break; break;
case END_KEY: case END_KEY:
case CTRL_KEY('e'): case CTRL_KEY('e'):
if (editor->nrows == 0) {
break;
}
editor->curx = editor->row[editor->cury].size; editor->curx = editor->row[editor->cury].size;
break; break;
default: default:
@ -712,6 +723,7 @@ process_kcommand(int16_t c)
process_normal(DEL_KEY); process_normal(DEL_KEY);
} }
break; break;
case 'g':
case CTRL_KEY('g'): case CTRL_KEY('g'):
goto_line(); goto_line();
break; break;
@ -916,6 +928,7 @@ draw_rows(struct abuf *ab)
ab_append(ab, "|", 1); ab_append(ab, "|", 1);
} }
} else { } else {
erow_update(&editor->row[filerow]);
buflen = editor->row[filerow].rsize - editor->coloffs; buflen = editor->row[filerow].rsize - editor->coloffs;
if (buflen < 0) { if (buflen < 0) {
buflen = 0; buflen = 0;
@ -941,7 +954,8 @@ draw_status_bar(struct abuf *ab)
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->mode == MODE_KCOMMAND ? 'K' : 'N',
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",
@ -1049,11 +1063,9 @@ editor_set_status(const char *fmt, ...)
void void
loop() loop()
{ {
int up = 1;
while (1) { while (1) {
if (up) display_refresh(); display_refresh();
up = process_keypress(); while (process_keypress() != 0) ;
} }
} }

View File

@ -40,6 +40,7 @@ static struct {
/* terminal.c */ /* terminal.c */
void nextline(); /* TODO: destroy this */
void terminal_refresh(); void terminal_refresh();
void terminal_init(); void terminal_init();
void terminal_deinit(); void terminal_deinit();
@ -49,5 +50,8 @@ int terminal_getch();
/* input.c */ /* input.c */
int handle_keypress(int c); int handle_keypress(int c);
/* file_buffer.c */
void init_buffer(struct buffer *buf)
#endif /* KTE_DEFS_H */ #endif /* KTE_DEFS_H */

10
kte/file_buffer.c Normal file
View File

@ -0,0 +1,10 @@
#include "defs.h"
void
init_buffer(struct buffer *buf)
{
/* put the cursor at the top of the file */
buf->curx = 0;
buf->cury = 0;
}

View File

@ -17,7 +17,8 @@ handle_keypress(int c)
static int mode = MODE_NORMAL; static int mode = MODE_NORMAL;
if (mode == MODE_NORMAL) { if (mode == MODE_NORMAL) {
printf("%02x\t", c); printf("%02x", c);
nextline();
} }
return 1; return 1;

View File

@ -11,11 +11,9 @@
#include "defs.h" #include "defs.h"
void struct buffer *buffers;
deinit() int nbufs;
{ int cbuf; /* current buffer */
endwin();
}
int int
@ -23,6 +21,10 @@ main()
{ {
int c, up = 1; int c, up = 1;
buffers = calloc(1, sizeof(struct buffer));
nbufs = 1;
cbuf = 0;
terminal_init(); terminal_init();
terminal_message("welcome to KTE", 14); terminal_message("welcome to KTE", 14);
terminal_refresh(); terminal_refresh();

View File

@ -5,6 +5,18 @@
#include "defs.h" #include "defs.h"
*/
static int cury = 0;
void
nextline()
{
printf("\t%d", cury);
wrefresh(editor.main);
cury++;
}
void void
terminal_refresh() terminal_refresh()
{ {
@ -14,6 +26,11 @@ terminal_refresh()
if ((time(NULL) - editor.msgtm) > KTE_MSG_TIME) { if ((time(NULL) - editor.msgtm) > KTE_MSG_TIME) {
wrefresh(editor.message); wrefresh(editor.message);
} }
wmove(editor.main, cury, 0);
if (cury > LINES-3) {
cury = 0;
}
} }