vip

VI Plus
git clone https://codeberg.org/night0721/vip
Log | Files | Refs | README | LICENSE

commit 5549a3d93ec28ecebf0b904b00bb5d7acb4e2d98
parent 4854d4bceec950ee483994714fb7efddcc286670
Author: night0721 <[email protected]>
Date:   Wed,  3 Jul 2024 03:00:14 +0100

Prompt at bar

Diffstat:
Minclude/vip.h | 1+
Msrc/vip.c | 42+++++++++++++++++++++++++++++++++++++++++-
2 files changed, 42 insertions(+), 1 deletion(-)

diff --git a/include/vip.h b/include/vip.h @@ -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; diff --git a/src/vip.c b/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];