From 9d6384a67dd6c2e86bbd060871644e06211cae16 Mon Sep 17 00:00:00 2001 From: Jakob Kaivo Date: Mon, 18 Apr 2022 20:32:59 -0400 Subject: first implementation of v command --- more.c | 49 ++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 46 insertions(+), 3 deletions(-) diff --git a/more.c b/more.c index 8db2d87..02d3e07 100644 --- a/more.c +++ b/more.c @@ -2,10 +2,13 @@ #include #include #include +#include #include +#include #include #include #include +#include #include #include #include @@ -83,6 +86,46 @@ static void jump(struct more_file *mf) } } +static struct more_file more_invoke_editor(struct more_file *mf) +{ + extern char **environ; + char *editor = getenv("EDITOR"); + if (editor == NULL) { + editor = "vi"; + } + + char line[64]; + snprintf(line, sizeof(line), "%zd", mf->topline); + char *argv[] = { editor, "-c", line, mf->path, NULL }; + + char *base = editor; + char *slash = strrchr(editor, '/'); + if (slash) { + base = slash + 1; + } + + if (strcmp(base, "ex") && strcmp(base, "vi")) { + argv[1] = mf->path; + argv[2] = NULL; + } + + struct more_file ret = *mf; + + def_prog_mode(); + reset_shell_mode(); + pid_t pid; + if (posix_spawnp(&pid, editor, NULL, NULL, argv, environ) == 0) { + char *path = strdup(mf->path); + more_close(mf); + waitpid(pid, NULL, 0); + ret = more_open(path); + free(path); + } + reset_prog_mode(); + + return ret; +} + static struct more_file more_examine(struct more_file *mf) { wprintw(more_status, "e"); @@ -237,7 +280,7 @@ static int more(const char *path) //repeatsearch(-count); break; - case ':': { + case ':': waddch(more_status, c); echo(); int c2 = wgetch(more_status); @@ -268,10 +311,10 @@ static int more(const char *path) default: break; } - } + break; case 'v': - // invoke_editor(); + more_invoke_editor(&mf); break; case '=': -- cgit v1.2.1