tAllow saving of file to disk - ve - a minimal text editor (work in progress)
(HTM) git clone git://src.adamsgaard.dk/ve
(DIR) Log
(DIR) Files
(DIR) Refs
(DIR) README
(DIR) LICENSE
---
(DIR) commit f0699261a58f3c5e8e5eaff0e85553d4b7475fde
(DIR) parent d5fa4d87e9cc60c1d421b57b507e53690f5a0c8d
(HTM) Author: Anders Damsgaard <anders@adamsgaard.dk>
Date: Tue, 6 Aug 2019 16:09:03 +0200
Allow saving of file to disk
Diffstat:
M input.c | 20 +++++++++++++++++++-
M io.c | 46 +++++++++++++++++++++++++++++++
M io.h | 1 +
3 files changed, 66 insertions(+), 1 deletion(-)
---
(DIR) diff --git a/input.c b/input.c
t@@ -3,6 +3,7 @@
#include "byote.h"
#include "terminal.h"
#include "edit.h"
+#include "io.h"
#define CTRL_KEY(k) ((k) & 0x1f)
t@@ -75,6 +76,10 @@ editor_process_keypress()
editor_move_cursor(c);
break;
+ case CTRL_KEY('w'):
+ file_save(E.filename);
+ break;
+
case CTRL_KEY('f'):
i = E.screen_rows;
while (i--)
t@@ -125,9 +130,22 @@ editor_process_keypress()
} else if (E.mode == 1) { /* insert mode */
switch (c) {
case CTRL_KEY('c'):
- case 27: /* escape */
+ case '\x1b': /* escape */
E.mode = 0;
break;
+
+ case CTRL_KEY('\r'): /* enter */
+ /* TODO */
+ break;
+
+ case 127: /* backspace */
+ case CTRL_KEY('h'):
+ /* TODO */
+ break;
+
+ case CTRL_KEY('l'):
+ break;
+
default:
editor_insert_char(c);
break;
(DIR) diff --git a/io.c b/io.c
t@@ -7,9 +7,12 @@
#include <stdio.h>
#include <sys/types.h>
#include <string.h>
+#include <fcntl.h>
+#include <unistd.h>
#include "byote.h"
#include "terminal.h"
#include "row.h"
+#include "output.h"
void
file_open(char *filename)
t@@ -37,3 +40,46 @@ file_open(char *filename)
free(line);
fclose(fp);
}
+
+/* convert rows to one long char array of length buflen */
+char*
+editor_concatenate_rows(int *buflen)
+{
+ int totlen, j;
+ char *buf, *p;
+
+ totlen = 0;
+ for (j=0; j<E.num_rows; ++j)
+ totlen += E.row[j].size + 1; /* add space for newline char */
+ *buflen = totlen;
+
+ buf = malloc(totlen);
+ p = buf;
+ for (j=0; j<E.num_rows; ++j) {
+ memcpy(p, E.row[j].chars, E.row[j].size);
+ p += E.row[j].size;
+ *p = '\n';
+ p++;
+ }
+ return buf;
+}
+
+void
+file_save(char *filename)
+{
+ int len, fd;
+ char *buf;
+
+ if (filename == NULL) {
+ editor_set_status_message("error: no filename specified");
+ return;
+ }
+
+ buf = editor_concatenate_rows(&len);
+ fd = open(filename, O_RDWR | O_CREAT, 0644);
+ ftruncate(fd, len);
+ write(fd, buf, len);
+ close(fd);
+ free(buf);
+ editor_set_status_message("%s written", filename);
+}
(DIR) diff --git a/io.h b/io.h
t@@ -2,5 +2,6 @@
#define IO_H_
void file_open(char *filename);
+void file_save(char *filename);
#endif