aboutsummaryrefslogtreecommitdiffstats
path: root/noice.c
diff options
context:
space:
mode:
authorMartin Kühl <martin.kuehl@gmail.com>2015-11-20 16:42:33 +0100
committersin <sin@2f30.org>2015-11-26 13:37:03 +0000
commitd1ed0cff118a07327b3b237ea65836f5c673acbb (patch)
treeaf0753ee09bbf1d5e0edc9227566594f0eb7d328 /noice.c
parentd1d086d91edd89e5a8edcc3349488b3ed89f8c9d (diff)
downloadnoice-d1ed0cff118a07327b3b237ea65836f5c673acbb.tar.gz
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.
Diffstat (limited to 'noice.c')
-rw-r--r--noice.c23
1 files changed, 19 insertions, 4 deletions
diff --git a/noice.c b/noice.c
index 63c7b6b..8b27845 100644
--- a/noice.c
+++ b/noice.c
@@ -68,6 +68,7 @@ struct key {
int sym; /* Key pressed */
enum action act; /* Action */
char *run; /* Program to run */
+ char *env; /* Environment variable to run */
};
#include "config.h"
@@ -195,6 +196,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 *
openwith(char *file)
{
regex_t regex;
@@ -310,9 +322,9 @@ printprompt(char *str)
}
/* 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
-nextsel(char **run)
+nextsel(char **run, char **env)
{
int c, i;
@@ -325,6 +337,7 @@ nextsel(char **run)
for (i = 0; i < LEN(bindings); i++)
if (c == bindings[i].sym) {
*run = bindings[i].run;
+ *env = bindings[i].env;
return bindings[i].act;
}
@@ -630,7 +643,7 @@ browse(const char *ipath, const char *ifilter)
regex_t re;
char *newpath;
struct stat sb;
- char *name, *bin, *dir, *tmp, *run;
+ char *name, *bin, *dir, *tmp, *run, *env;
int nowtyping = 0;
oldpath = NULL;
@@ -653,7 +666,7 @@ begin:
if (nowtyping)
goto moretyping;
nochange:
- switch (nextsel(&run)) {
+ switch (nextsel(&run, &env)) {
case SEL_QUIT:
free(path);
free(fltr);
@@ -842,12 +855,14 @@ moretyping:
oldpath = makepath(path, dents[cur].name);
goto begin;
case SEL_RUN:
+ run = xgetenv(env, run);
exitcurses();
spawn(run, NULL, path);
initcurses();
break;
case SEL_RUNARG:
name = dents[cur].name;
+ run = xgetenv(env, run);
exitcurses();
spawn(run, name, path);
initcurses();
Un proyecto texto-plano.xyz