plugins/README.md | 1 + src/nnn.c | 56 ++++++++++++++++++++++++++--------------------------- diff --git a/plugins/README.md b/plugins/README.md index bcd01cabfaf077702d7751a38e85d418b6708188..b2b43f6a230bc6459f4685f4af993d65a2d51298 100644 --- a/plugins/README.md +++ b/plugins/README.md @@ -206,6 +206,7 @@ 1. `$1`: The hovered file's name. 2. `$2`: The working directory (might differ from `$PWD` in case of symlinked paths; non-canonical). 3. `$3`: The picker mode output file (`-` for stdout) if `nnn` is executed as a file picker. - Sets the environment variable `NNN_PIPE` used to control `nnn` active directory. +- Exports the [special variables](https://github.com/jarun/nnn/wiki/Concepts#special-variables). Plugins can also read the `.selection` file in the config directory. diff --git a/src/nnn.c b/src/nnn.c index 0d250712f26519cd2bbcd42a06177201887cab95..146acbbbe492c0062200f7fbc3426fec6f6a7406 100644 --- a/src/nnn.c +++ b/src/nnn.c @@ -5051,6 +5051,30 @@ spawn(pager, g_tmpfpath, NULL, NULL, F_CLI | F_TTY); unlink(g_tmpfpath); } +static void setexports(void) +{ + char dvar[] = "d0"; + char fvar[] = "f0"; + + if (ndents) { + setenv(envs[ENV_NCUR], pdents[cur].name, 1); + xstrsncpy(g_ctx[cfg.curctx].c_name, pdents[cur].name, NAME_MAX + 1); + } else if (g_ctx[cfg.curctx].c_name[0]) + g_ctx[cfg.curctx].c_name[0] = '\0'; + + for (uchar_t i = 0; i < CTX_MAX; ++i) { + if (g_ctx[i].c_cfg.ctxactive) { + dvar[1] = fvar[1] = '1' + i; + setenv(dvar, g_ctx[i].c_path, 1); + + if (g_ctx[i].c_name[0]) { + mkpath(g_ctx[i].c_path, g_ctx[i].c_name, g_buf); + setenv(fvar, g_buf, 1); + } + } + } +} + static bool run_cmd_as_plugin(const char *file, char *runfile, uchar_t flags) { size_t len; @@ -5185,6 +5209,8 @@ if (!g_state.pluginit) { plctrl_init(); g_state.pluginit = 1; } + + setexports(); /* Check for run-cmd-as-plugin mode */ if (*file == '!') { @@ -5310,30 +5336,6 @@ return ret; } -static void setexports(char *buf) -{ - char dvar[] = "d0"; - char fvar[] = "f0"; - - if (ndents) { - setenv(envs[ENV_NCUR], pdents[cur].name, 1); - xstrsncpy(g_ctx[cfg.curctx].c_name, pdents[cur].name, NAME_MAX + 1); - } else if (g_ctx[cfg.curctx].c_name[0]) - g_ctx[cfg.curctx].c_name[0] = '\0'; - - for (uchar_t i = 0; i < CTX_MAX; ++i) { - if (g_ctx[i].c_cfg.ctxactive) { - dvar[1] = fvar[1] = '1' + i; - setenv(dvar, g_ctx[i].c_path, 1); - - if (g_ctx[i].c_name[0]) { - mkpath(g_ctx[i].c_path, g_ctx[i].c_name, buf); - setenv(fvar, buf, 1); - } - } - } -} - static bool handle_cmd(enum action sel, char *newpath) { endselection(FALSE); @@ -5341,7 +5343,7 @@ if (sel == SEL_LAUNCH) return launch_app(newpath); - setexports(newpath); + setexports(); if (sel == SEL_PROMPT) return prompt_run(); @@ -6800,8 +6802,6 @@ xstrsncpy(path, rundir, PATH_MAX); rundir[0] = '\0'; clearfilter(); - setexports(newpath); - if (chdir(path) == -1 || !run_plugin(&path, pent->name, runfile, &lastname, &lastdir)) { @@ -7535,8 +7535,6 @@ ++tmp; r = FALSE; /* Do not refresh dir after completion */ } else r = TRUE; - - setexports(newpath); if (!run_plugin(&path, tmp, (ndents ? pdents[cur].name : NULL), &lastname, &lastdir)) {