dvtm

dynamic virtual terminal manager - with my changes
git clone https://pi.duncano.de/git/dvtm.git
Log | Files | Refs | README | LICENSE

commit 1861093a6f4fceda2de90804ce06713aba6a7351
parent 7b96e9b6f9d3cf8e3059d923f28b2b3ac21af50e
Author: Marc André Tanner <mat@brain-dump.org>
Date:   Sat, 25 Oct 2014 16:51:34 +0200

Move editor configuration to config.def.h

Diffstat:
config.def.h | 17++++++++++++-----
dvtm.c | 49++++++++++++++++++++++++++++++++++---------------
2 files changed, 46 insertions(+), 20 deletions(-)

diff --git a/config.def.h b/config.def.h @@ -1,8 +1,3 @@ -/* specify a default editor and arguments which is used in case - * neither $DVTM_EDITOR nor $EDITOR is set. */ -static char editor[] = "vis"; -static const char *editor_args[] = { editor, "-", NULL }; - /* valid curses attributes are listed below they can be ORed * * A_NORMAL Normal display (no highlight) @@ -169,3 +164,15 @@ static Action actions[] = { static char const * const keytable[] = { /* add your custom key escape sequences */ }; + +/* editor to use for copy mode. If neither of DVTM_EDITOR, EDITOR and PAGER is + * set the first entry is chosen. Otherwise the array is consulted for supported + * options. A %d in argv is replaced by the line number at which the file should + * be opened. If filter is true the editor is expected to work even if stdout is + * redirected (i.e. not a terminal). + */ +static Editor editors[] = { + { .name = "vis", .argv = { "vis", "+%d", NULL }, .filter = true }, + { .name = "vim", .argv = { "vim", "+%d", "-", NULL }, .filter = false }, + { .name = "less", .argv = { "less", "+%d", "-", NULL }, .filter = false }, +}; diff --git a/dvtm.c b/dvtm.c @@ -153,6 +153,12 @@ typedef struct { size_t size; } Register; +typedef struct { + char *name; + const char *argv[4]; + bool filter; +} Editor; + #define countof(arr) (sizeof(arr) / sizeof((arr)[0])) #define sstrlen(str) (sizeof(str) - 1) #define max(x, y) ((x) > (y) ? (x) : (y)) @@ -837,27 +843,40 @@ copymode(const char *args[]) { if (!(sel->editor = vt_create(sel->h - sel->has_title_line, sel->w, 0))) return; - char *ed = getenv("DVTM_EDITOR"), argline[32]; - const char **argv; + char *ed = getenv("DVTM_EDITOR"); const char *cwd = NULL; const char *env[] = { "DVTM", VERSION, NULL }; int *to = &sel->editor_fds[0], *from = NULL; sel->editor_fds[0] = sel->editor_fds[1] = -1; - if (ed || (ed = getenv("EDITOR"))) { - argv = (const char*[]){ ed, "-", NULL }; - } else if ((ed = getenv("PAGER"))) { - argv = (const char*[]){ ed, NULL }; - } else { - ed = editor; - argv = editor_args; - } - if (!strcmp(ed, "vis") || !strcmp(ed, "vim") || !strcmp(ed, "less")) { - snprintf(argline, sizeof(argline), "+%d", vt_content_start(sel->app)); - argv = (const char*[]){ ed, argline, "-", NULL }; + if (!ed) + ed = getenv("EDITOR"); + if (!ed) + ed = getenv("PAGER"); + if (!ed) + ed = editors[0].name; + + const char **argv = (const char*[]){ ed, "-", NULL }; + + for (unsigned int i = 0; i < countof(editors); i++) { + if (!strcmp(editors[i].name, ed)) { + argv = (const char*[]){ ed, NULL, NULL, NULL, NULL, NULL, NULL }; + for (int j = 1; editors[i].argv[j]; j++) { + if (strstr(editors[i].argv[j], "%d")) { + char argline[32]; + int line = vt_content_start(sel->app); + snprintf(argline, sizeof(argline), "+%d", line); + argv[j] = argline; + } else { + argv[j] = editors[i].argv[j]; + } + } + if (editors[i].filter) + from = &sel->editor_fds[1]; + break; + } } - if (!strcmp(ed, "vis")) - from = &sel->editor_fds[1]; + if (vt_forkpty(sel->editor, ed, argv, cwd, env, to, from) < 0) { vt_destroy(sel->editor); sel->editor = NULL;