diff options
author | Jakob Kaivo <jkk@ung.org> | 2022-04-18 20:32:59 -0400 |
---|---|---|
committer | Jakob Kaivo <jkk@ung.org> | 2022-04-18 20:32:59 -0400 |
commit | 9d6384a67dd6c2e86bbd060871644e06211cae16 (patch) | |
tree | 8b4c1d6dfc6487c0a5bbc15b55eaa72b6ed99773 | |
parent | e7b5a5613219363afdf9b0c4270ce6263c34d4fe (diff) |
first implementation of v command
-rw-r--r-- | more.c | 49 |
1 files changed, 46 insertions, 3 deletions
@@ -2,10 +2,13 @@ #include <curses.h> #include <ctype.h> #include <errno.h> +#include <libgen.h> #include <locale.h> +#include <spawn.h> #include <stdio.h> #include <stdlib.h> #include <string.h> +#include <sys/wait.h> #include <term.h> #include <unistd.h> #include <wordexp.h> @@ -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 '=': |