login

<     >

2016-12-05 21:12:43 (UTC-02:00)

Marcel Rodrigues <marcelgmr@gmail.com>

Allow user to set Rover-specific programs, e.g. ROVER_SHELL.

diff --git a/README.md b/README.md
index 9489632..f6f9a9b 100644
--- a/README.md
+++ b/README.md
@@ -41,7 +41,7 @@ Quick Start
   RETURN - open $SHELL on the current directory
    SPACE - open $PAGER with the selected file
        e - open $EDITOR with the selected file
-       o - open $ROVER_OPEN with the selected file
+       o - open $OPEN with the selected file
        / - start incremental search (RETURN to finish)
    f/d/s - toggle file/directory/hidden listing
      n/N - create new file/directory
@@ -78,6 +78,12 @@ via the appropriate environment variables. For example, to specify an editor:
  $ EDITOR=vi rover
  ```
 
+ Rover will first check for variables prefixed  with ROVER_. This can be used to
+change Rover behavior without interfering with the global environment:
+ ```
+ $ ROVER_EDITOR=vi rover
+ ```
+
  Please read rover(1) for more information.
 
 

diff --git a/rover.1 b/rover.1
index 1b97eb4..321a61c 100644
--- a/rover.1
+++ b/rover.1
@@ -112,7 +112,7 @@ Open \fB$PAGER\fR with the selected file.
 Open \fB$EDITOR\fR with the selected file.
 .TP
 .B o
-Open \fB$ROVER_OPEN\fR with the selected file.
+Open \fB$OPEN\fR with the selected file.
 .TP
 .B /
 Start incremental search.
@@ -194,9 +194,13 @@ subprocess. This allows one to use the selection as part of an arbitrary command
 by first invoking a shell from Rover (see the \fBCOMMANDS\fR section) and then
 typing something like \fBgrep abc "$RVSEL"\fR.
 .TP
-.B ROVER_OPEN
+.B OPEN
 This variable can be set to a command accepting a single argument: a filename.
 The command is supposed to open the given file with an appropriate program.
+.TP
+.B ROVER_SHELL, ROVER_PAGER, ROVER_EDITOR, ROVER_OPEN
+If any of these variables are set, they override \fBSHELL\fR, \fBPAGER\fR,
+\fBEDITOR\fR and \fBOPEN\fR, respectivelly.
 .SH CONFIGURATION
 .PP
 If you want to change Rover key bindings or colors, you can edit the

diff --git a/rover.c b/rover.c
index 5e43e7a..2665a98 100644
--- a/rover.c
+++ b/rover.c
@@ -38,6 +38,12 @@ static char BUF2[BUFLEN];
 static char INPUT[BUFLEN];
 static wchar_t WBUF[BUFLEN];
 
+/* Paths to external programs. */
+static char *user_shell;
+static char *user_pager;
+static char *user_editor;
+static char *user_open;
+
 /* Listing view parameters. */
 #define HEIGHT      (LINES-4)
 #define STATUSPOS   (COLS-16)
@@ -311,6 +317,20 @@ rover_get_wch(wint_t *wch)
     return ret;
 }
 
+/* Get user programs from the environment. */
+
+#define ROVER_ENV(dst, src) if ((dst = getenv("ROVER_" #src)) == NULL) \
+                                dst = getenv(#src);
+
+static void
+get_user_programs()
+{
+    ROVER_ENV(user_shell, SHELL)
+    ROVER_ENV(user_pager, PAGER)
+    ROVER_ENV(user_editor, EDITOR)
+    ROVER_ENV(user_open, OPEN)
+}
+
 /* Do a fork-exec to external program (e.g. $EDITOR). */
 static void
 spawn(char **args)
@@ -359,9 +379,8 @@ done:
 }
 
 static int
-open_with_env(const char *env, char *path)
+open_with_env(char *program, char *path)
 {
-    char *program = getenv(env);
     if (program) {
 #ifdef RV_SHELL
         strncpy(BUF1, program, BUFLEN - 1);
@@ -1036,6 +1055,7 @@ main(int argc, char *argv[])
             }
         }
     }
+    get_user_programs();
     init_term();
     rover.nfiles = 0;
     for (i = 0; i < 10; i++) {
@@ -1164,7 +1184,7 @@ main(int argc, char *argv[])
         } else if (!strcmp(key, RVK_REFRESH)) {
             reload();
         } else if (!strcmp(key, RVK_SHELL)) {
-            program = getenv("SHELL");
+            program = user_shell;
             if (program) {
 #ifdef RV_SHELL
                 spawn((char *[]) {RV_SHELL, "-c", program, NULL});
@@ -1175,15 +1195,15 @@ main(int argc, char *argv[])
             }
         } else if (!strcmp(key, RVK_VIEW)) {
             if (!rover.nfiles || S_ISDIR(EMODE(ESEL))) continue;
-            if (open_with_env("PAGER", ENAME(ESEL)))
+            if (open_with_env(user_pager, ENAME(ESEL)))
                 cd(0);
         } else if (!strcmp(key, RVK_EDIT)) {
             if (!rover.nfiles || S_ISDIR(EMODE(ESEL))) continue;
-            if (open_with_env("EDITOR", ENAME(ESEL)))
+            if (open_with_env(user_editor, ENAME(ESEL)))
                 cd(0);
         } else if (!strcmp(key, RVK_OPEN)) {
             if (!rover.nfiles || S_ISDIR(EMODE(ESEL))) continue;
-            if (open_with_env("ROVER_OPEN", ENAME(ESEL)))
+            if (open_with_env(user_open, ENAME(ESEL)))
                 cd(0);
         } else if (!strcmp(key, RVK_SEARCH)) {
             int oldsel, oldscroll, length;