shithub: vexed

Download patch

ref: f1cc5bc056cb87c683a32df0e1d98c52ccf76484
parent: d743498615c8e3b3f09578b924a96ac4faea85df
author: phil9 <telephil9@gmail.com>
date: Mon Dec 27 19:27:49 EST 2021

implement file saving

	it is now possible to save the file using the
	mouse3 menu

--- a/buf.c
+++ b/buf.c
@@ -38,7 +38,14 @@
 
 int writefile(Buffer *buf, char *filename)
 {
-	USED(buf);
-	USED(filename);
-	return -1;
+	int fd, n;
+
+	fd = open(filename, OWRITE|OTRUNC);
+	if(fd < 0)
+		return -1;
+	n = write(fd, buf->data, buf->count);
+	if(n < 0 || n != buf->count)
+		return -1;
+	close(fd);
+	return 0;
 }
--- a/vexed.c
+++ b/vexed.c
@@ -21,6 +21,10 @@
 	Scrollwidth = 12,
 };
 
+enum { Msave, Mquit, };
+char *menu3str[] = { "save", "quit", 0 };
+Menu menu3 = { menu3str };
+
 const char	*filename;
 int modified;
 Buffer buf;
@@ -163,6 +167,27 @@
 }
 
 void
+menu3hit(void)
+{
+	int n;
+
+	n = menuhit(3, mctl, &menu3, nil);
+	switch(n){
+	case Msave:
+		if(!modified)
+			return;
+		if(writefile(&buf, filename) < 0)
+			sysfatal("writefile: %r");
+		modified = 0;
+		redraw();
+		break;
+	case Mquit:
+		threadexitsall(nil);
+		break;
+	}
+}
+
+void
 emouse(Mouse *m)
 {
 	int n;
@@ -187,6 +212,8 @@
 				sel = n;
 				redraw();
 			}
+		}else if(m->buttons == 4){
+			menu3hit();
 		}else if(m->buttons == 8){
 			scroll(-scrollsize);
 		}else if(m->buttons == 16){