Prompt at bar
This commit is contained in:
parent
4854d4bcee
commit
5549a3d93e
2 changed files with 42 additions and 1 deletions
|
@ -62,6 +62,7 @@ void abAppend(struct abuf *ab, const char *s, int len);
|
|||
void append_row(char *s, size_t len);
|
||||
void row_insert_char(row *row, int at, int c);
|
||||
void row_del_char(row *row, int at);
|
||||
char *prompt_editor(char *prompt);
|
||||
|
||||
extern editor vip;
|
||||
|
||||
|
|
42
src/vip.c
42
src/vip.c
|
@ -117,7 +117,13 @@ void open_editor(char *filename)
|
|||
|
||||
void save_file()
|
||||
{
|
||||
if (vip.filename == NULL) return;
|
||||
if (vip.filename == NULL) {
|
||||
vip.filename = prompt_editor("Save as: %s (ESC to cancel)");
|
||||
if (vip.filename == NULL) {
|
||||
set_status_bar_message("Save aborted");
|
||||
return;
|
||||
}
|
||||
}
|
||||
int len;
|
||||
char *buf = rows_to_str(&len);
|
||||
int fd = open(vip.filename, O_RDWR | O_CREAT, 0644);
|
||||
|
@ -228,6 +234,40 @@ void refresh_screen()
|
|||
abFree(&ab);
|
||||
}
|
||||
|
||||
char *prompt_editor(char *prompt)
|
||||
{
|
||||
size_t bufsize = 128;
|
||||
char *buf = malloc(bufsize);
|
||||
size_t buflen = 0;
|
||||
buf[0] = '\0';
|
||||
while (1) {
|
||||
set_status_bar_message(prompt, buf);
|
||||
refresh_screen();
|
||||
int c = read_key();
|
||||
if (c == DEL_KEY || c == CTRL_KEY('h') || c == BACKSPACE) {
|
||||
if (buflen != 0) {
|
||||
buf[--buflen] = '\0';
|
||||
}
|
||||
} else if (c == '\x1b') {
|
||||
set_status_bar_message("");
|
||||
free(buf);
|
||||
return NULL;
|
||||
} else if (c == '\r') {
|
||||
if (buflen != 0) {
|
||||
set_status_bar_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';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void move_cursor(int key)
|
||||
{
|
||||
row *row = (vip.cy >= vip.rows) ? NULL : &vip.row[vip.cy];
|
||||
|
|
Loading…
Reference in a new issue