tPrompt for file name on save - ve - a minimal text editor (work in progress) | |
git clone git://src.adamsgaard.dk/ve | |
Log | |
Files | |
Refs | |
README | |
LICENSE | |
--- | |
commit 68ebb8e650025d82f30c96de9564f924c6bfdb37 | |
parent 36ee312271f23de904472f0ae2208489cec7cdfd | |
Author: Anders Damsgaard <[email protected]> | |
Date: Tue, 6 Aug 2019 21:01:08 +0200 | |
Prompt for file name on save | |
Diffstat: | |
M input.c | 35 +++++++++++++++++++++++++++++… | |
M input.h | 1 + | |
M io.c | 8 ++++---- | |
3 files changed, 40 insertions(+), 4 deletions(-) | |
--- | |
diff --git a/input.c b/input.c | |
t@@ -1,5 +1,6 @@ | |
#include <unistd.h> | |
#include <stdlib.h> | |
+#include <ctype.h> | |
#include "te.h" | |
#include "terminal.h" | |
#include "edit.h" | |
t@@ -9,6 +10,40 @@ | |
#define CTRL_KEY(k) ((k) & 0x1f) | |
+/* prompt is expected to be a format string containing a %s */ | |
+char* | |
+editor_prompt(char *prompt) | |
+{ | |
+ size_t bufsize, buflen; | |
+ char *buf; | |
+ int c; | |
+ | |
+ bufsize = 128; | |
+ buflen = 0; | |
+ buf = malloc(bufsize); | |
+ buf[0] = '\0'; | |
+ | |
+ while (1) { | |
+ editor_set_status_message(prompt, buf); | |
+ editor_refresh_screen(); | |
+ | |
+ c = editor_read_key(); | |
+ if (c == '\r') { | |
+ if (buflen != 0) { | |
+ editor_set_status_message(""); | |
+ return buf; | |
+ } | |
+ } else if (!iscntrl(c) && c < 128) { | |
+ if (buflen == bufsize - 1) { | |
+ bufsize *= 2; | |
+ buf = realloc(buf, bufsize); | |
+ } | |
+ buf[buflen++] = c; | |
+ buf[buflen] = '\0'; | |
+ } | |
+ } | |
+} | |
+ | |
/* move cursor according to screen, file, and line limits */ | |
void | |
editor_move_cursor(char key) | |
diff --git a/input.h b/input.h | |
t@@ -1,6 +1,7 @@ | |
#ifndef INPUT_H_ | |
#define INPUT_H_ | |
+char* editor_prompt(char *prompt); | |
void editor_process_keypress(); | |
#endif | |
diff --git a/io.c b/io.c | |
t@@ -14,6 +14,7 @@ | |
#include "terminal.h" | |
#include "row.h" | |
#include "output.h" | |
+#include "input.h" | |
void | |
file_open(char *filename) | |
t@@ -72,10 +73,8 @@ file_save(char *filename) | |
int len, fd; | |
char *buf; | |
- if (filename == NULL) { | |
- editor_set_status_message("error: no filename specified"); | |
- return; | |
- } | |
+ if (filename == NULL) | |
+ filename = editor_prompt("save as: %s"); | |
buf = editor_concatenate_rows(&len); | |
t@@ -85,6 +84,7 @@ file_save(char *filename) | |
if (write(fd, buf, len) == len) { | |
close(fd); | |
free(buf); | |
+ E.filename = filename; | |
E.file_changed = 0; | |
editor_set_status_message("%d bytes written to… | |
return; |