From a459a19c9c78433a4ec8546dd8dcf14c68d087c7 Mon Sep 17 00:00:00 2001 From: Jarno Mäkipää Date: Sat, 25 Jan 2020 20:16:49 +0200 Subject: vi: Add tests Test file integrity after load, move, delete and save+exit. Drawing of buffer is not tested yet. Added -s script option, accept file that is run as startup script of commands. File is parsed byte at time and handled as you had typed it. If EOF has been reached without editor close command, editing is continued normally using keyboard. This functionality is in vim and neovim, but not in POSIX vi standard. nvi (vi used in some macs) has -s with different meaning... Some simple tests added, dw last line test fails, so test is disabled. --- toys/pending/vi.c | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) (limited to 'toys/pending/vi.c') diff --git a/toys/pending/vi.c b/toys/pending/vi.c index 9c47a4c5..a21867b2 100644 --- a/toys/pending/vi.c +++ b/toys/pending/vi.c @@ -5,13 +5,14 @@ * * See http://pubs.opengroup.org/onlinepubs/9699919799/utilities/vi.html -USE_VI(NEWTOY(vi, "<1>1", TOYFLAG_USR|TOYFLAG_BIN)) +USE_VI(NEWTOY(vi, ">1s:", TOYFLAG_USR|TOYFLAG_BIN)) config VI bool "vi" default n help - usage: vi FILE + usage: vi [-s script] FILE + -s script: run script file Visual text editor. Predates the existence of standardized cursor keys, so the controls are weird and historical. */ @@ -20,6 +21,7 @@ config VI #include "toys.h" GLOBALS( + char *s; int cur_col; int cur_row; int scr_row; @@ -885,6 +887,8 @@ void vi_main(void) char vi_buf[16] = {0}; char utf8_code[8] = {0}; int utf8_dec_p = 0, vi_buf_pos = 0; + FILE *script = 0; + if (FLAG(s)) script = fopen(TT.s, "r"); TT.il = xzalloc(sizeof(struct str_line)); TT.il->data = xzalloc(80); @@ -912,7 +916,15 @@ void vi_main(void) draw_page(); for (;;) { - int key = scan_key(keybuf, -1); + int key = 0; + if (script) { + key = fgetc(script); + if (key == EOF) { + fclose(script); + script = 0; + key = scan_key(keybuf, -1); + } + } else key = scan_key(keybuf, -1); if (key == -1) goto cleanup_vi; @@ -983,6 +995,7 @@ void vi_main(void) TT.il->len = 0; memset(TT.il->data, 0, TT.il->alloc); break; + case 0x0A: case 0x0D: if (run_ex_cmd(TT.il->data) == -1) goto cleanup_vi; @@ -1018,6 +1031,7 @@ void vi_main(void) TT.il->len -= shrink; } break; + case 0x0A: case 0x0D: //insert newline // -- cgit v1.2.3