Support commands specified by environment variables

Specifying commands by name in config.h means that
every time one switches ones editor (for example),
one has to modify every config.h file referencing that editor
and then recompile every corresponding program.

This change adds a string `env` for specifying an environment
variable to `struct key` and uses it to optionally specify the
command to run on SEL_{RUN,RUNARG}.
The `run` string is used as a fallback when the environment variable
has not been specified or is not set.
It also updates `config.def.h` to demonstrate this new capability.
This commit is contained in:
Martin Kühl 2015-11-20 16:42:33 +01:00 committed by sin
parent d1d086d91e
commit d1ed0cff11
2 changed files with 23 additions and 7 deletions

View file

@ -62,8 +62,9 @@ struct key bindings[] = {
{ 't', SEL_MTIME }, { 't', SEL_MTIME },
{ CONTROL('L'), SEL_REDRAW }, { CONTROL('L'), SEL_REDRAW },
/* Run command */ /* Run command */
{ '!', SEL_RUN, "sh" }, { 'z', SEL_RUN, "top" },
{ 'z', SEL_RUN, "top" }, { '!', SEL_RUN, "sh", "SHELL" },
/* Run command with argument */ /* Run command with argument */
{ 'e', SEL_RUNARG, "vi" }, { 'e', SEL_RUNARG, "vi", "EDITOR" },
{ 'p', SEL_RUNARG, "less", "PAGER" },
}; };

23
noice.c
View file

@ -68,6 +68,7 @@ struct key {
int sym; /* Key pressed */ int sym; /* Key pressed */
enum action act; /* Action */ enum action act; /* Action */
char *run; /* Program to run */ char *run; /* Program to run */
char *env; /* Environment variable to run */
}; };
#include "config.h" #include "config.h"
@ -194,6 +195,17 @@ spawn(const char *file, const char *arg, const char *dir)
} }
} }
char *
xgetenv(char *name, char *fallback)
{
if (name == NULL)
return fallback;
char *value = getenv(name);
if (value)
return value;
return fallback;
}
char * char *
openwith(char *file) openwith(char *file)
{ {
@ -310,9 +322,9 @@ printprompt(char *str)
} }
/* Returns SEL_* if key is bound and 0 otherwise /* Returns SEL_* if key is bound and 0 otherwise
Also modifies the run pointer (used on SEL_{RUN,RUNARG}) */ Also modifies the run and env pointers (used on SEL_{RUN,RUNARG}) */
int int
nextsel(char **run) nextsel(char **run, char **env)
{ {
int c, i; int c, i;
@ -325,6 +337,7 @@ nextsel(char **run)
for (i = 0; i < LEN(bindings); i++) for (i = 0; i < LEN(bindings); i++)
if (c == bindings[i].sym) { if (c == bindings[i].sym) {
*run = bindings[i].run; *run = bindings[i].run;
*env = bindings[i].env;
return bindings[i].act; return bindings[i].act;
} }
@ -630,7 +643,7 @@ browse(const char *ipath, const char *ifilter)
regex_t re; regex_t re;
char *newpath; char *newpath;
struct stat sb; struct stat sb;
char *name, *bin, *dir, *tmp, *run; char *name, *bin, *dir, *tmp, *run, *env;
int nowtyping = 0; int nowtyping = 0;
oldpath = NULL; oldpath = NULL;
@ -653,7 +666,7 @@ begin:
if (nowtyping) if (nowtyping)
goto moretyping; goto moretyping;
nochange: nochange:
switch (nextsel(&run)) { switch (nextsel(&run, &env)) {
case SEL_QUIT: case SEL_QUIT:
free(path); free(path);
free(fltr); free(fltr);
@ -842,12 +855,14 @@ moretyping:
oldpath = makepath(path, dents[cur].name); oldpath = makepath(path, dents[cur].name);
goto begin; goto begin;
case SEL_RUN: case SEL_RUN:
run = xgetenv(env, run);
exitcurses(); exitcurses();
spawn(run, NULL, path); spawn(run, NULL, path);
initcurses(); initcurses();
break; break;
case SEL_RUNARG: case SEL_RUNARG:
name = dents[cur].name; name = dents[cur].name;
run = xgetenv(env, run);
exitcurses(); exitcurses();
spawn(run, name, path); spawn(run, name, path);
initcurses(); initcurses();