]> git.mdlowis.com Git - projs/tide.git/commitdiff
Added ability to save file and exit. Also allow passing path to nonexistent file...
authorMike Lowis <mike.lowis@gentex.com>
Fri, 7 Oct 2016 14:52:34 +0000 (10:52 -0400)
committerMike Lowis <mike.lowis@gentex.com>
Fri, 7 Oct 2016 14:52:34 +0000 (10:52 -0400)
buf.c
edit.h
foo [new file with mode: 0644]
foo.txt [new file with mode: 0644]
keyboard.c

diff --git a/buf.c b/buf.c
index 846ec939b2c55799a7101aa02e98d36317ce3bb7..93851132c91fa4df6814cf934931c8583527063d 100644 (file)
--- a/buf.c
+++ b/buf.c
@@ -1,3 +1,5 @@
+#define _GNU_SOURCE
+#include <string.h>
 #include <assert.h>
 #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 b442ce2c5d422fea14811739ad03a5300df7a190..a2c8b7527760e5f550a03ffb0349a5bc12d92cfa 100644 (file)
--- 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 (file)
index 0000000..bf7e899
--- /dev/null
+++ b/foo
@@ -0,0 +1 @@
+asdasd
diff --git a/foo.txt b/foo.txt
new file mode 100644 (file)
index 0000000..076ef20
--- /dev/null
+++ b/foo.txt
@@ -0,0 +1 @@
+Testing edit and save
index c6081e679fb16f26dd0c32bed163d800a038a495..41cf6464b79d7513a759257488090e198e2ede68 100644 (file)
@@ -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;
+}