From 2654d055b98937b1f74731157056fe09d80ddf19 Mon Sep 17 00:00:00 2001 From: Mike Lowis Date: Fri, 7 Oct 2016 10:52:34 -0400 Subject: [PATCH] Added ability to save file and exit. Also allow passing path to nonexistent file at launch --- buf.c | 24 +++++++++++++++++++++--- edit.h | 2 ++ foo | 1 + foo.txt | 1 + keyboard.c | 10 ++++++++-- 5 files changed, 33 insertions(+), 5 deletions(-) create mode 100644 foo create mode 100644 foo.txt diff --git a/buf.c b/buf.c index 846ec93..9385113 100644 --- a/buf.c +++ b/buf.c @@ -1,3 +1,5 @@ +#define _GNU_SOURCE +#include #include #include "edit.h" @@ -6,12 +8,28 @@ void buf_load(Buf* buf, char* path) { unsigned i = 0; Rune r; FILE* in = (!strcmp(path,"-") ? stdin : fopen(path, "rb")); - while (RUNE_EOF != (r = fgetrune(in))) - buf_ins(buf, i++, r); - fclose(in); + buf->path = (in == stdin ? NULL : strdup(path)); + if (in != NULL) { + while (RUNE_EOF != (r = fgetrune(in))) + buf_ins(buf, i++, r); + fclose(in); + } + /* Make sure it ends with a newline */ + if (r != '\n') + buf_ins(buf, i, (Rune)'\n'); buf->insert_mode = false; } +void buf_save(Buf* buf) { + if (!buf->path) return; + unsigned end = buf_end(buf); + FILE* out = fopen(buf->path, "wb"); + if (!out) return; + for (unsigned i = 0; i < end; i++) + fputrune(buf_get(buf, i), out); + fclose(out); +} + void buf_resize(Buf* buf, size_t sz) { /* allocate the new buffer and gap */ Buf copy = *buf; diff --git a/edit.h b/edit.h index b442ce2..a2c8b75 100644 --- a/edit.h +++ b/edit.h @@ -121,6 +121,7 @@ void handle_mouse(MouseEvent* mevnt); /* Buffer management functions *****************************************************************************/ typedef struct buf { + char* path; bool insert_mode; /* tracks current mode */ size_t bufsize; /* size of the buffer in runes */ Rune* bufstart; /* start of the data buffer */ @@ -130,6 +131,7 @@ typedef struct buf { } Buf; void buf_load(Buf* buf, char* path); +void buf_save(Buf* buf); void buf_init(Buf* buf); void buf_clr(Buf* buf); void buf_del(Buf* buf, unsigned pos); diff --git a/foo b/foo new file mode 100644 index 0000000..bf7e899 --- /dev/null +++ b/foo @@ -0,0 +1 @@ +asdasd diff --git a/foo.txt b/foo.txt new file mode 100644 index 0000000..076ef20 --- /dev/null +++ b/foo.txt @@ -0,0 +1 @@ +Testing edit and save diff --git a/keyboard.c b/keyboard.c index c6081e6..41cf646 100644 --- a/keyboard.c +++ b/keyboard.c @@ -5,6 +5,7 @@ extern unsigned CursorPos; static void special_keys(Rune key); static void control_keys(Rune key); +static void vi_keys(Rune key); void handle_key(Rune key) { /* ignore invalid keys */ @@ -18,8 +19,7 @@ void handle_key(Rune key) { else if (Buffer.insert_mode) buf_ins(&Buffer, CursorPos++, key); else - (void)0; - + vi_keys(key); } static void special_keys(Rune key) { @@ -44,6 +44,12 @@ static void control_keys(Rune key) { switch (key) { case KEY_ESCAPE: Buffer.insert_mode = false; break; case KEY_BACKSPACE: buf_del(&Buffer, --CursorPos); break; + case KEY_CTRL_W: buf_save(&Buffer); break; + case KEY_CTRL_Q: exit(0); break; default: buf_ins(&Buffer, CursorPos++, key); break; } } + +static void vi_keys(Rune key) { + (void)key; +} -- 2.49.0