Save as functionality

This commit is contained in:
Timothy Warren 2019-08-20 13:39:30 -04:00
parent 9248b9d560
commit 827be306ef

57
kilo.c
View File

@ -68,6 +68,8 @@ struct editorConfig E;
/*** prototypes ***/
void editorSetStatusMessage(const char *fmt, ...);
void editorRefreshScreen();
char *editorPrompt(char *prompt);
/*** terminal ***/
@ -499,7 +501,12 @@ void editorSave()
{
if (E.filename == NULL)
{
return;
E.filename = editorPrompt("Save as: %s (ESC to cancel)");
if (E.filename == NULL)
{
editorSetStatusMessage("Save aborted");
return;
}
}
int len;
@ -728,6 +735,54 @@ void editorSetStatusMessage(const char *fmt, ...)
/*** input ***/
char *editorPrompt(char *prompt)
{
size_t bufsize = 128;
char *buf = malloc(bufsize);
size_t buflen = 0;
buf[0] = '\0';
while (1)
{
editorSetStatusMessage(prompt, buf);
editorRefreshScreen();
int c = editorReadKey();
if (c == DEL_KEY || c == CTRL_KEY('h') || c == BACKSPACE)
{
if (buflen != 0)
{
buf[buflen--] = '\0';
}
}
else if (c == '\x1b')
{
editorSetStatusMessage("");
free(buf);
return NULL;
}
else if (c == '\r')
{
if (buflen != 0)
{
editorSetStatusMessage("");
return buf;
}
}
else if (!iscntrl(c) && c < 128)
{
if (buflen == bufsize -1)
{
bufsize *= 2;
buf = realloc(buf, bufsize);
}
buf[buflen++] = c;
buf[buflen] = '\0';
}
}
}
void editorMoveCursor(int key)
{
erow *row = (E.cy >= E.numrows) ? NULL : &E.row[E.cy];