From bffadf09c5588ac46fe299f7a9655bc5a47eb64a Mon Sep 17 00:00:00 2001 From: Arun Prakash Jana Date: Fri, 27 Dec 2019 13:19:23 +0530 Subject: [PATCH] Refactor plugin handling --- src/nnn.c | 136 +++++++++++++++++++++++++++--------------------------- src/nnn.h | 14 +++--- 2 files changed, 75 insertions(+), 75 deletions(-) diff --git a/src/nnn.c b/src/nnn.c index 783aebfb..68fa5e97 100644 --- a/src/nnn.c +++ b/src/nnn.c @@ -5245,10 +5245,76 @@ nochange: xstrlcpy(lastname, tmp, NAME_MAX + 1); goto begin; } + case SEL_PLUGKEY: // fallthrough + case SEL_PLUGIN: + /* Check if directory is accessible */ + if (!xdiraccess(plugindir)) { + printwarn(&presel); + goto nochange; + } + + if (sel == SEL_PLUGKEY) { + xstrlcpy(g_buf, messages[MSG_PLUGIN_KEYS], CMD_LEN_MAX); + printkeys(plug, g_buf + strlen(g_buf), PLUGIN_MAX); + printprompt(g_buf); + r = get_input(NULL); + tmp = get_kv_val(plug, NULL, r, PLUGIN_MAX, FALSE); + if (!tmp) { + printwait(messages[MSG_INVALID_KEY], &presel); + goto nochange; + } + + if (tmp[0] == '-' && tmp[1]) { + ++tmp; + r = FALSE; /* Do not refresh dir after completion */ + } else + r = TRUE; + + if (!run_selected_plugin(&path, tmp, newpath, + (ndents ? dents[cur].name : NULL), + &lastname, &lastdir)) { + printwait(messages[MSG_FAILED], &presel); + goto nochange; + } + + if (!r) { + clearprompt(); + goto nochange; + } + + if (ndents) + copycurname(); + } else { + cfg.runplugin ^= 1; + if (!cfg.runplugin && rundir[0]) { + /* + * If toggled, and still in the plugin dir, + * switch to original directory + */ + if (strcmp(path, plugindir) == 0) { + xstrlcpy(path, rundir, PATH_MAX); + xstrlcpy(lastname, runfile, NAME_MAX); + rundir[0] = runfile[0] = '\0'; + setdirwatch(); + goto begin; + } + + /* Otherwise, initiate choosing plugin again */ + cfg.runplugin = 1; + } + + xstrlcpy(rundir, path, PATH_MAX); + xstrlcpy(path, plugindir, PATH_MAX); + if (ndents) + xstrlcpy(runfile, dents[cur].name, NAME_MAX); + cfg.runctx = cfg.curctx; + lastname[0] = '\0'; + } + setdirwatch(); + clearfilter(); + goto begin; case SEL_EXEC: // fallthrough case SEL_SHELL: // fallthrough - case SEL_PLUGKEY: // fallthrough - case SEL_PLUGIN: // fallthrough case SEL_LAUNCH: // fallthrough case SEL_RUNCMD: endselection(); @@ -5262,72 +5328,6 @@ nochange: setenv(envs[ENV_NCUR], (ndents ? dents[cur].name : ""), 1); spawn(shell, NULL, NULL, path, F_CLI); break; - case SEL_PLUGKEY: // fallthrough - case SEL_PLUGIN: - /* Check if directory is accessible */ - if (!xdiraccess(plugindir)) { - printwarn(&presel); - goto nochange; - } - - if (sel == SEL_PLUGKEY) { - xstrlcpy(g_buf, messages[MSG_PLUGIN_KEYS], CMD_LEN_MAX); - printkeys(plug, g_buf + strlen(g_buf), PLUGIN_MAX); - printprompt(g_buf); - r = get_input(NULL); - tmp = get_kv_val(plug, NULL, r, PLUGIN_MAX, FALSE); - if (!tmp) { - printwait(messages[MSG_INVALID_KEY], &presel); - goto nochange; - } - - if (tmp[0] == '-' && tmp[1]) { - ++tmp; - r = FALSE; /* Do not refresh dir after completion */ - } else - r = TRUE; - - if (!run_selected_plugin(&path, tmp, newpath, - (ndents ? dents[cur].name : NULL), - &lastname, &lastdir)) { - printwait(messages[MSG_FAILED], &presel); - goto nochange; - } - - if (!r) { - clearprompt(); - goto nochange; - } - - if (ndents) - copycurname(); - } else { - cfg.runplugin ^= 1; - if (!cfg.runplugin && rundir[0]) { - /* - * If toggled, and still in the plugin dir, - * switch to original directory - */ - if (strcmp(path, plugindir) == 0) { - xstrlcpy(path, rundir, PATH_MAX); - xstrlcpy(lastname, runfile, NAME_MAX); - rundir[0] = runfile[0] = '\0'; - setdirwatch(); - goto begin; - } - break; - } - - xstrlcpy(rundir, path, PATH_MAX); - xstrlcpy(path, plugindir, PATH_MAX); - if (ndents) - xstrlcpy(runfile, dents[cur].name, NAME_MAX); - cfg.runctx = cfg.curctx; - lastname[0] = '\0'; - } - setdirwatch(); - clearfilter(); - goto begin; case SEL_LAUNCH: launch_app(path, newpath); diff --git a/src/nnn.h b/src/nnn.h index 8a495d1d..4db7d95a 100644 --- a/src/nnn.h +++ b/src/nnn.h @@ -91,10 +91,10 @@ enum action { SEL_REMOTE, SEL_UMOUNT, SEL_HELP, - SEL_EXEC, - SEL_SHELL, SEL_PLUGKEY, SEL_PLUGIN, + SEL_EXEC, + SEL_SHELL, SEL_LAUNCH, SEL_RUNCMD, SEL_RUNEDIT, @@ -237,17 +237,17 @@ static struct key bindings[] = { { 'u', SEL_UMOUNT }, /* Show help */ { '?', SEL_HELP }, - /* Execute file */ - { 'C', SEL_EXEC }, - /* Run command */ - { '!', SEL_SHELL }, - { CONTROL(']'), SEL_SHELL }, /* Plugin key */ { 'x', SEL_PLUGKEY }, { ';', SEL_PLUGKEY }, /* Run a plugin */ { 'i', SEL_PLUGIN }, { CONTROL('V'), SEL_PLUGIN }, + /* Execute file */ + { 'C', SEL_EXEC }, + /* Run command */ + { '!', SEL_SHELL }, + { CONTROL(']'), SEL_SHELL }, /* Launcher */ { '=', SEL_LAUNCH }, /* Run a command */